pcnet32.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  1. /* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
  2. /*
  3. * Copyright 1996-1999 Thomas Bogendoerfer
  4. *
  5. * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
  6. *
  7. * Copyright 1993 United States Government as represented by the
  8. * Director, National Security Agency.
  9. *
  10. * This software may be used and distributed according to the terms
  11. * of the GNU General Public License, incorporated herein by reference.
  12. *
  13. * This driver is for PCnet32 and PCnetPCI based ethercards
  14. */
  15. /**************************************************************************
  16. * 23 Oct, 2000.
  17. * Fixed a few bugs, related to running the controller in 32bit mode.
  18. *
  19. * Carsten Langgaard, carstenl@mips.com
  20. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  21. *
  22. *************************************************************************/
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #define DRV_NAME "pcnet32"
  25. #define DRV_VERSION "1.35"
  26. #define DRV_RELDATE "21.Apr.2008"
  27. #define PFX DRV_NAME ": "
  28. static const char *const version =
  29. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/sched.h>
  33. #include <linux/string.h>
  34. #include <linux/errno.h>
  35. #include <linux/ioport.h>
  36. #include <linux/slab.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/pci.h>
  39. #include <linux/delay.h>
  40. #include <linux/init.h>
  41. #include <linux/ethtool.h>
  42. #include <linux/mii.h>
  43. #include <linux/crc32.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/etherdevice.h>
  46. #include <linux/if_ether.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/moduleparam.h>
  50. #include <linux/bitops.h>
  51. #include <linux/io.h>
  52. #include <linux/uaccess.h>
  53. #include <asm/dma.h>
  54. #include <asm/irq.h>
  55. /*
  56. * PCI device identifiers for "new style" Linux PCI Device Drivers
  57. */
  58. static const struct pci_device_id pcnet32_pci_tbl[] = {
  59. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME), },
  60. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE), },
  61. /*
  62. * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
  63. * the incorrect vendor id.
  64. */
  65. { PCI_DEVICE(PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE),
  66. .class = (PCI_CLASS_NETWORK_ETHERNET << 8), .class_mask = 0xffff00, },
  67. { } /* terminate list */
  68. };
  69. MODULE_DEVICE_TABLE(pci, pcnet32_pci_tbl);
  70. static int cards_found;
  71. /*
  72. * VLB I/O addresses
  73. */
  74. static unsigned int pcnet32_portlist[] =
  75. { 0x300, 0x320, 0x340, 0x360, 0 };
  76. static int pcnet32_debug;
  77. static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
  78. static int pcnet32vlb; /* check for VLB cards ? */
  79. static struct net_device *pcnet32_dev;
  80. static int max_interrupt_work = 2;
  81. static int rx_copybreak = 200;
  82. #define PCNET32_PORT_AUI 0x00
  83. #define PCNET32_PORT_10BT 0x01
  84. #define PCNET32_PORT_GPSI 0x02
  85. #define PCNET32_PORT_MII 0x03
  86. #define PCNET32_PORT_PORTSEL 0x03
  87. #define PCNET32_PORT_ASEL 0x04
  88. #define PCNET32_PORT_100 0x40
  89. #define PCNET32_PORT_FD 0x80
  90. #define PCNET32_DMA_MASK 0xffffffff
  91. #define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
  92. #define PCNET32_BLINK_TIMEOUT (jiffies + (HZ/4))
  93. /*
  94. * table to translate option values from tulip
  95. * to internal options
  96. */
  97. static const unsigned char options_mapping[] = {
  98. PCNET32_PORT_ASEL, /* 0 Auto-select */
  99. PCNET32_PORT_AUI, /* 1 BNC/AUI */
  100. PCNET32_PORT_AUI, /* 2 AUI/BNC */
  101. PCNET32_PORT_ASEL, /* 3 not supported */
  102. PCNET32_PORT_10BT | PCNET32_PORT_FD, /* 4 10baseT-FD */
  103. PCNET32_PORT_ASEL, /* 5 not supported */
  104. PCNET32_PORT_ASEL, /* 6 not supported */
  105. PCNET32_PORT_ASEL, /* 7 not supported */
  106. PCNET32_PORT_ASEL, /* 8 not supported */
  107. PCNET32_PORT_MII, /* 9 MII 10baseT */
  108. PCNET32_PORT_MII | PCNET32_PORT_FD, /* 10 MII 10baseT-FD */
  109. PCNET32_PORT_MII, /* 11 MII (autosel) */
  110. PCNET32_PORT_10BT, /* 12 10BaseT */
  111. PCNET32_PORT_MII | PCNET32_PORT_100, /* 13 MII 100BaseTx */
  112. /* 14 MII 100BaseTx-FD */
  113. PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
  114. PCNET32_PORT_ASEL /* 15 not supported */
  115. };
  116. static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
  117. "Loopback test (offline)"
  118. };
  119. #define PCNET32_TEST_LEN ARRAY_SIZE(pcnet32_gstrings_test)
  120. #define PCNET32_NUM_REGS 136
  121. #define MAX_UNITS 8 /* More are supported, limit only on options */
  122. static int options[MAX_UNITS];
  123. static int full_duplex[MAX_UNITS];
  124. static int homepna[MAX_UNITS];
  125. /*
  126. * Theory of Operation
  127. *
  128. * This driver uses the same software structure as the normal lance
  129. * driver. So look for a verbose description in lance.c. The differences
  130. * to the normal lance driver is the use of the 32bit mode of PCnet32
  131. * and PCnetPCI chips. Because these chips are 32bit chips, there is no
  132. * 16MB limitation and we don't need bounce buffers.
  133. */
  134. /*
  135. * Set the number of Tx and Rx buffers, using Log_2(# buffers).
  136. * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
  137. * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
  138. */
  139. #ifndef PCNET32_LOG_TX_BUFFERS
  140. #define PCNET32_LOG_TX_BUFFERS 4
  141. #define PCNET32_LOG_RX_BUFFERS 5
  142. #define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
  143. #define PCNET32_LOG_MAX_RX_BUFFERS 9
  144. #endif
  145. #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
  146. #define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
  147. #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
  148. #define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
  149. #define PKT_BUF_SKB 1544
  150. /* actual buffer length after being aligned */
  151. #define PKT_BUF_SIZE (PKT_BUF_SKB - NET_IP_ALIGN)
  152. /* chip wants twos complement of the (aligned) buffer length */
  153. #define NEG_BUF_SIZE (NET_IP_ALIGN - PKT_BUF_SKB)
  154. /* Offsets from base I/O address. */
  155. #define PCNET32_WIO_RDP 0x10
  156. #define PCNET32_WIO_RAP 0x12
  157. #define PCNET32_WIO_RESET 0x14
  158. #define PCNET32_WIO_BDP 0x16
  159. #define PCNET32_DWIO_RDP 0x10
  160. #define PCNET32_DWIO_RAP 0x14
  161. #define PCNET32_DWIO_RESET 0x18
  162. #define PCNET32_DWIO_BDP 0x1C
  163. #define PCNET32_TOTAL_SIZE 0x20
  164. #define CSR0 0
  165. #define CSR0_INIT 0x1
  166. #define CSR0_START 0x2
  167. #define CSR0_STOP 0x4
  168. #define CSR0_TXPOLL 0x8
  169. #define CSR0_INTEN 0x40
  170. #define CSR0_IDON 0x0100
  171. #define CSR0_NORMAL (CSR0_START | CSR0_INTEN)
  172. #define PCNET32_INIT_LOW 1
  173. #define PCNET32_INIT_HIGH 2
  174. #define CSR3 3
  175. #define CSR4 4
  176. #define CSR5 5
  177. #define CSR5_SUSPEND 0x0001
  178. #define CSR15 15
  179. #define PCNET32_MC_FILTER 8
  180. #define PCNET32_79C970A 0x2621
  181. /* The PCNET32 Rx and Tx ring descriptors. */
  182. struct pcnet32_rx_head {
  183. __le32 base;
  184. __le16 buf_length; /* two`s complement of length */
  185. __le16 status;
  186. __le32 msg_length;
  187. __le32 reserved;
  188. };
  189. struct pcnet32_tx_head {
  190. __le32 base;
  191. __le16 length; /* two`s complement of length */
  192. __le16 status;
  193. __le32 misc;
  194. __le32 reserved;
  195. };
  196. /* The PCNET32 32-Bit initialization block, described in databook. */
  197. struct pcnet32_init_block {
  198. __le16 mode;
  199. __le16 tlen_rlen;
  200. u8 phys_addr[6];
  201. __le16 reserved;
  202. __le32 filter[2];
  203. /* Receive and transmit ring base, along with extra bits. */
  204. __le32 rx_ring;
  205. __le32 tx_ring;
  206. };
  207. /* PCnet32 access functions */
  208. struct pcnet32_access {
  209. u16 (*read_csr) (unsigned long, int);
  210. void (*write_csr) (unsigned long, int, u16);
  211. u16 (*read_bcr) (unsigned long, int);
  212. void (*write_bcr) (unsigned long, int, u16);
  213. u16 (*read_rap) (unsigned long);
  214. void (*write_rap) (unsigned long, u16);
  215. void (*reset) (unsigned long);
  216. };
  217. /*
  218. * The first field of pcnet32_private is read by the ethernet device
  219. * so the structure should be allocated using pci_alloc_consistent().
  220. */
  221. struct pcnet32_private {
  222. struct pcnet32_init_block *init_block;
  223. /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
  224. struct pcnet32_rx_head *rx_ring;
  225. struct pcnet32_tx_head *tx_ring;
  226. dma_addr_t init_dma_addr;/* DMA address of beginning of the init block,
  227. returned by pci_alloc_consistent */
  228. struct pci_dev *pci_dev;
  229. const char *name;
  230. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  231. struct sk_buff **tx_skbuff;
  232. struct sk_buff **rx_skbuff;
  233. dma_addr_t *tx_dma_addr;
  234. dma_addr_t *rx_dma_addr;
  235. const struct pcnet32_access *a;
  236. spinlock_t lock; /* Guard lock */
  237. unsigned int cur_rx, cur_tx; /* The next free ring entry */
  238. unsigned int rx_ring_size; /* current rx ring size */
  239. unsigned int tx_ring_size; /* current tx ring size */
  240. unsigned int rx_mod_mask; /* rx ring modular mask */
  241. unsigned int tx_mod_mask; /* tx ring modular mask */
  242. unsigned short rx_len_bits;
  243. unsigned short tx_len_bits;
  244. dma_addr_t rx_ring_dma_addr;
  245. dma_addr_t tx_ring_dma_addr;
  246. unsigned int dirty_rx, /* ring entries to be freed. */
  247. dirty_tx;
  248. struct net_device *dev;
  249. struct napi_struct napi;
  250. char tx_full;
  251. char phycount; /* number of phys found */
  252. int options;
  253. unsigned int shared_irq:1, /* shared irq possible */
  254. dxsuflo:1, /* disable transmit stop on uflo */
  255. mii:1, /* mii port available */
  256. autoneg:1, /* autoneg enabled */
  257. port_tp:1, /* port set to TP */
  258. fdx:1; /* full duplex enabled */
  259. struct net_device *next;
  260. struct mii_if_info mii_if;
  261. struct timer_list watchdog_timer;
  262. u32 msg_enable; /* debug message level */
  263. /* each bit indicates an available PHY */
  264. u32 phymask;
  265. unsigned short chip_version; /* which variant this is */
  266. /* saved registers during ethtool blink */
  267. u16 save_regs[4];
  268. };
  269. static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
  270. static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
  271. static int pcnet32_open(struct net_device *);
  272. static int pcnet32_init_ring(struct net_device *);
  273. static netdev_tx_t pcnet32_start_xmit(struct sk_buff *,
  274. struct net_device *);
  275. static void pcnet32_tx_timeout(struct net_device *dev);
  276. static irqreturn_t pcnet32_interrupt(int, void *);
  277. static int pcnet32_close(struct net_device *);
  278. static struct net_device_stats *pcnet32_get_stats(struct net_device *);
  279. static void pcnet32_load_multicast(struct net_device *dev);
  280. static void pcnet32_set_multicast_list(struct net_device *);
  281. static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
  282. static void pcnet32_watchdog(struct timer_list *);
  283. static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
  284. static void mdio_write(struct net_device *dev, int phy_id, int reg_num,
  285. int val);
  286. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
  287. static void pcnet32_ethtool_test(struct net_device *dev,
  288. struct ethtool_test *eth_test, u64 * data);
  289. static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1);
  290. static int pcnet32_get_regs_len(struct net_device *dev);
  291. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  292. void *ptr);
  293. static void pcnet32_purge_tx_ring(struct net_device *dev);
  294. static int pcnet32_alloc_ring(struct net_device *dev, const char *name);
  295. static void pcnet32_free_ring(struct net_device *dev);
  296. static void pcnet32_check_media(struct net_device *dev, int verbose);
  297. static u16 pcnet32_wio_read_csr(unsigned long addr, int index)
  298. {
  299. outw(index, addr + PCNET32_WIO_RAP);
  300. return inw(addr + PCNET32_WIO_RDP);
  301. }
  302. static void pcnet32_wio_write_csr(unsigned long addr, int index, u16 val)
  303. {
  304. outw(index, addr + PCNET32_WIO_RAP);
  305. outw(val, addr + PCNET32_WIO_RDP);
  306. }
  307. static u16 pcnet32_wio_read_bcr(unsigned long addr, int index)
  308. {
  309. outw(index, addr + PCNET32_WIO_RAP);
  310. return inw(addr + PCNET32_WIO_BDP);
  311. }
  312. static void pcnet32_wio_write_bcr(unsigned long addr, int index, u16 val)
  313. {
  314. outw(index, addr + PCNET32_WIO_RAP);
  315. outw(val, addr + PCNET32_WIO_BDP);
  316. }
  317. static u16 pcnet32_wio_read_rap(unsigned long addr)
  318. {
  319. return inw(addr + PCNET32_WIO_RAP);
  320. }
  321. static void pcnet32_wio_write_rap(unsigned long addr, u16 val)
  322. {
  323. outw(val, addr + PCNET32_WIO_RAP);
  324. }
  325. static void pcnet32_wio_reset(unsigned long addr)
  326. {
  327. inw(addr + PCNET32_WIO_RESET);
  328. }
  329. static int pcnet32_wio_check(unsigned long addr)
  330. {
  331. outw(88, addr + PCNET32_WIO_RAP);
  332. return inw(addr + PCNET32_WIO_RAP) == 88;
  333. }
  334. static const struct pcnet32_access pcnet32_wio = {
  335. .read_csr = pcnet32_wio_read_csr,
  336. .write_csr = pcnet32_wio_write_csr,
  337. .read_bcr = pcnet32_wio_read_bcr,
  338. .write_bcr = pcnet32_wio_write_bcr,
  339. .read_rap = pcnet32_wio_read_rap,
  340. .write_rap = pcnet32_wio_write_rap,
  341. .reset = pcnet32_wio_reset
  342. };
  343. static u16 pcnet32_dwio_read_csr(unsigned long addr, int index)
  344. {
  345. outl(index, addr + PCNET32_DWIO_RAP);
  346. return inl(addr + PCNET32_DWIO_RDP) & 0xffff;
  347. }
  348. static void pcnet32_dwio_write_csr(unsigned long addr, int index, u16 val)
  349. {
  350. outl(index, addr + PCNET32_DWIO_RAP);
  351. outl(val, addr + PCNET32_DWIO_RDP);
  352. }
  353. static u16 pcnet32_dwio_read_bcr(unsigned long addr, int index)
  354. {
  355. outl(index, addr + PCNET32_DWIO_RAP);
  356. return inl(addr + PCNET32_DWIO_BDP) & 0xffff;
  357. }
  358. static void pcnet32_dwio_write_bcr(unsigned long addr, int index, u16 val)
  359. {
  360. outl(index, addr + PCNET32_DWIO_RAP);
  361. outl(val, addr + PCNET32_DWIO_BDP);
  362. }
  363. static u16 pcnet32_dwio_read_rap(unsigned long addr)
  364. {
  365. return inl(addr + PCNET32_DWIO_RAP) & 0xffff;
  366. }
  367. static void pcnet32_dwio_write_rap(unsigned long addr, u16 val)
  368. {
  369. outl(val, addr + PCNET32_DWIO_RAP);
  370. }
  371. static void pcnet32_dwio_reset(unsigned long addr)
  372. {
  373. inl(addr + PCNET32_DWIO_RESET);
  374. }
  375. static int pcnet32_dwio_check(unsigned long addr)
  376. {
  377. outl(88, addr + PCNET32_DWIO_RAP);
  378. return (inl(addr + PCNET32_DWIO_RAP) & 0xffff) == 88;
  379. }
  380. static const struct pcnet32_access pcnet32_dwio = {
  381. .read_csr = pcnet32_dwio_read_csr,
  382. .write_csr = pcnet32_dwio_write_csr,
  383. .read_bcr = pcnet32_dwio_read_bcr,
  384. .write_bcr = pcnet32_dwio_write_bcr,
  385. .read_rap = pcnet32_dwio_read_rap,
  386. .write_rap = pcnet32_dwio_write_rap,
  387. .reset = pcnet32_dwio_reset
  388. };
  389. static void pcnet32_netif_stop(struct net_device *dev)
  390. {
  391. struct pcnet32_private *lp = netdev_priv(dev);
  392. netif_trans_update(dev); /* prevent tx timeout */
  393. napi_disable(&lp->napi);
  394. netif_tx_disable(dev);
  395. }
  396. static void pcnet32_netif_start(struct net_device *dev)
  397. {
  398. struct pcnet32_private *lp = netdev_priv(dev);
  399. ulong ioaddr = dev->base_addr;
  400. u16 val;
  401. netif_wake_queue(dev);
  402. val = lp->a->read_csr(ioaddr, CSR3);
  403. val &= 0x00ff;
  404. lp->a->write_csr(ioaddr, CSR3, val);
  405. napi_enable(&lp->napi);
  406. }
  407. /*
  408. * Allocate space for the new sized tx ring.
  409. * Free old resources
  410. * Save new resources.
  411. * Any failure keeps old resources.
  412. * Must be called with lp->lock held.
  413. */
  414. static void pcnet32_realloc_tx_ring(struct net_device *dev,
  415. struct pcnet32_private *lp,
  416. unsigned int size)
  417. {
  418. dma_addr_t new_ring_dma_addr;
  419. dma_addr_t *new_dma_addr_list;
  420. struct pcnet32_tx_head *new_tx_ring;
  421. struct sk_buff **new_skb_list;
  422. unsigned int entries = BIT(size);
  423. pcnet32_purge_tx_ring(dev);
  424. new_tx_ring =
  425. pci_zalloc_consistent(lp->pci_dev,
  426. sizeof(struct pcnet32_tx_head) * entries,
  427. &new_ring_dma_addr);
  428. if (new_tx_ring == NULL)
  429. return;
  430. new_dma_addr_list = kcalloc(entries, sizeof(dma_addr_t), GFP_ATOMIC);
  431. if (!new_dma_addr_list)
  432. goto free_new_tx_ring;
  433. new_skb_list = kcalloc(entries, sizeof(struct sk_buff *), GFP_ATOMIC);
  434. if (!new_skb_list)
  435. goto free_new_lists;
  436. kfree(lp->tx_skbuff);
  437. kfree(lp->tx_dma_addr);
  438. pci_free_consistent(lp->pci_dev,
  439. sizeof(struct pcnet32_tx_head) * lp->tx_ring_size,
  440. lp->tx_ring, lp->tx_ring_dma_addr);
  441. lp->tx_ring_size = entries;
  442. lp->tx_mod_mask = lp->tx_ring_size - 1;
  443. lp->tx_len_bits = (size << 12);
  444. lp->tx_ring = new_tx_ring;
  445. lp->tx_ring_dma_addr = new_ring_dma_addr;
  446. lp->tx_dma_addr = new_dma_addr_list;
  447. lp->tx_skbuff = new_skb_list;
  448. return;
  449. free_new_lists:
  450. kfree(new_dma_addr_list);
  451. free_new_tx_ring:
  452. pci_free_consistent(lp->pci_dev,
  453. sizeof(struct pcnet32_tx_head) * entries,
  454. new_tx_ring,
  455. new_ring_dma_addr);
  456. }
  457. /*
  458. * Allocate space for the new sized rx ring.
  459. * Re-use old receive buffers.
  460. * alloc extra buffers
  461. * free unneeded buffers
  462. * free unneeded buffers
  463. * Save new resources.
  464. * Any failure keeps old resources.
  465. * Must be called with lp->lock held.
  466. */
  467. static void pcnet32_realloc_rx_ring(struct net_device *dev,
  468. struct pcnet32_private *lp,
  469. unsigned int size)
  470. {
  471. dma_addr_t new_ring_dma_addr;
  472. dma_addr_t *new_dma_addr_list;
  473. struct pcnet32_rx_head *new_rx_ring;
  474. struct sk_buff **new_skb_list;
  475. int new, overlap;
  476. unsigned int entries = BIT(size);
  477. new_rx_ring =
  478. pci_zalloc_consistent(lp->pci_dev,
  479. sizeof(struct pcnet32_rx_head) * entries,
  480. &new_ring_dma_addr);
  481. if (new_rx_ring == NULL)
  482. return;
  483. new_dma_addr_list = kcalloc(entries, sizeof(dma_addr_t), GFP_ATOMIC);
  484. if (!new_dma_addr_list)
  485. goto free_new_rx_ring;
  486. new_skb_list = kcalloc(entries, sizeof(struct sk_buff *), GFP_ATOMIC);
  487. if (!new_skb_list)
  488. goto free_new_lists;
  489. /* first copy the current receive buffers */
  490. overlap = min(entries, lp->rx_ring_size);
  491. for (new = 0; new < overlap; new++) {
  492. new_rx_ring[new] = lp->rx_ring[new];
  493. new_dma_addr_list[new] = lp->rx_dma_addr[new];
  494. new_skb_list[new] = lp->rx_skbuff[new];
  495. }
  496. /* now allocate any new buffers needed */
  497. for (; new < entries; new++) {
  498. struct sk_buff *rx_skbuff;
  499. new_skb_list[new] = netdev_alloc_skb(dev, PKT_BUF_SKB);
  500. rx_skbuff = new_skb_list[new];
  501. if (!rx_skbuff) {
  502. /* keep the original lists and buffers */
  503. netif_err(lp, drv, dev, "%s netdev_alloc_skb failed\n",
  504. __func__);
  505. goto free_all_new;
  506. }
  507. skb_reserve(rx_skbuff, NET_IP_ALIGN);
  508. new_dma_addr_list[new] =
  509. pci_map_single(lp->pci_dev, rx_skbuff->data,
  510. PKT_BUF_SIZE, PCI_DMA_FROMDEVICE);
  511. if (pci_dma_mapping_error(lp->pci_dev,
  512. new_dma_addr_list[new])) {
  513. netif_err(lp, drv, dev, "%s dma mapping failed\n",
  514. __func__);
  515. dev_kfree_skb(new_skb_list[new]);
  516. goto free_all_new;
  517. }
  518. new_rx_ring[new].base = cpu_to_le32(new_dma_addr_list[new]);
  519. new_rx_ring[new].buf_length = cpu_to_le16(NEG_BUF_SIZE);
  520. new_rx_ring[new].status = cpu_to_le16(0x8000);
  521. }
  522. /* and free any unneeded buffers */
  523. for (; new < lp->rx_ring_size; new++) {
  524. if (lp->rx_skbuff[new]) {
  525. if (!pci_dma_mapping_error(lp->pci_dev,
  526. lp->rx_dma_addr[new]))
  527. pci_unmap_single(lp->pci_dev,
  528. lp->rx_dma_addr[new],
  529. PKT_BUF_SIZE,
  530. PCI_DMA_FROMDEVICE);
  531. dev_kfree_skb(lp->rx_skbuff[new]);
  532. }
  533. }
  534. kfree(lp->rx_skbuff);
  535. kfree(lp->rx_dma_addr);
  536. pci_free_consistent(lp->pci_dev,
  537. sizeof(struct pcnet32_rx_head) *
  538. lp->rx_ring_size, lp->rx_ring,
  539. lp->rx_ring_dma_addr);
  540. lp->rx_ring_size = entries;
  541. lp->rx_mod_mask = lp->rx_ring_size - 1;
  542. lp->rx_len_bits = (size << 4);
  543. lp->rx_ring = new_rx_ring;
  544. lp->rx_ring_dma_addr = new_ring_dma_addr;
  545. lp->rx_dma_addr = new_dma_addr_list;
  546. lp->rx_skbuff = new_skb_list;
  547. return;
  548. free_all_new:
  549. while (--new >= lp->rx_ring_size) {
  550. if (new_skb_list[new]) {
  551. if (!pci_dma_mapping_error(lp->pci_dev,
  552. new_dma_addr_list[new]))
  553. pci_unmap_single(lp->pci_dev,
  554. new_dma_addr_list[new],
  555. PKT_BUF_SIZE,
  556. PCI_DMA_FROMDEVICE);
  557. dev_kfree_skb(new_skb_list[new]);
  558. }
  559. }
  560. kfree(new_skb_list);
  561. free_new_lists:
  562. kfree(new_dma_addr_list);
  563. free_new_rx_ring:
  564. pci_free_consistent(lp->pci_dev,
  565. sizeof(struct pcnet32_rx_head) * entries,
  566. new_rx_ring,
  567. new_ring_dma_addr);
  568. }
  569. static void pcnet32_purge_rx_ring(struct net_device *dev)
  570. {
  571. struct pcnet32_private *lp = netdev_priv(dev);
  572. int i;
  573. /* free all allocated skbuffs */
  574. for (i = 0; i < lp->rx_ring_size; i++) {
  575. lp->rx_ring[i].status = 0; /* CPU owns buffer */
  576. wmb(); /* Make sure adapter sees owner change */
  577. if (lp->rx_skbuff[i]) {
  578. if (!pci_dma_mapping_error(lp->pci_dev,
  579. lp->rx_dma_addr[i]))
  580. pci_unmap_single(lp->pci_dev,
  581. lp->rx_dma_addr[i],
  582. PKT_BUF_SIZE,
  583. PCI_DMA_FROMDEVICE);
  584. dev_kfree_skb_any(lp->rx_skbuff[i]);
  585. }
  586. lp->rx_skbuff[i] = NULL;
  587. lp->rx_dma_addr[i] = 0;
  588. }
  589. }
  590. #ifdef CONFIG_NET_POLL_CONTROLLER
  591. static void pcnet32_poll_controller(struct net_device *dev)
  592. {
  593. disable_irq(dev->irq);
  594. pcnet32_interrupt(0, dev);
  595. enable_irq(dev->irq);
  596. }
  597. #endif
  598. /*
  599. * lp->lock must be held.
  600. */
  601. static int pcnet32_suspend(struct net_device *dev, unsigned long *flags,
  602. int can_sleep)
  603. {
  604. int csr5;
  605. struct pcnet32_private *lp = netdev_priv(dev);
  606. const struct pcnet32_access *a = lp->a;
  607. ulong ioaddr = dev->base_addr;
  608. int ticks;
  609. /* really old chips have to be stopped. */
  610. if (lp->chip_version < PCNET32_79C970A)
  611. return 0;
  612. /* set SUSPEND (SPND) - CSR5 bit 0 */
  613. csr5 = a->read_csr(ioaddr, CSR5);
  614. a->write_csr(ioaddr, CSR5, csr5 | CSR5_SUSPEND);
  615. /* poll waiting for bit to be set */
  616. ticks = 0;
  617. while (!(a->read_csr(ioaddr, CSR5) & CSR5_SUSPEND)) {
  618. spin_unlock_irqrestore(&lp->lock, *flags);
  619. if (can_sleep)
  620. msleep(1);
  621. else
  622. mdelay(1);
  623. spin_lock_irqsave(&lp->lock, *flags);
  624. ticks++;
  625. if (ticks > 200) {
  626. netif_printk(lp, hw, KERN_DEBUG, dev,
  627. "Error getting into suspend!\n");
  628. return 0;
  629. }
  630. }
  631. return 1;
  632. }
  633. static void pcnet32_clr_suspend(struct pcnet32_private *lp, ulong ioaddr)
  634. {
  635. int csr5 = lp->a->read_csr(ioaddr, CSR5);
  636. /* clear SUSPEND (SPND) - CSR5 bit 0 */
  637. lp->a->write_csr(ioaddr, CSR5, csr5 & ~CSR5_SUSPEND);
  638. }
  639. static int pcnet32_get_link_ksettings(struct net_device *dev,
  640. struct ethtool_link_ksettings *cmd)
  641. {
  642. struct pcnet32_private *lp = netdev_priv(dev);
  643. unsigned long flags;
  644. spin_lock_irqsave(&lp->lock, flags);
  645. if (lp->mii) {
  646. mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
  647. } else if (lp->chip_version == PCNET32_79C970A) {
  648. if (lp->autoneg) {
  649. cmd->base.autoneg = AUTONEG_ENABLE;
  650. if (lp->a->read_bcr(dev->base_addr, 4) == 0xc0)
  651. cmd->base.port = PORT_AUI;
  652. else
  653. cmd->base.port = PORT_TP;
  654. } else {
  655. cmd->base.autoneg = AUTONEG_DISABLE;
  656. cmd->base.port = lp->port_tp ? PORT_TP : PORT_AUI;
  657. }
  658. cmd->base.duplex = lp->fdx ? DUPLEX_FULL : DUPLEX_HALF;
  659. cmd->base.speed = SPEED_10;
  660. ethtool_convert_legacy_u32_to_link_mode(
  661. cmd->link_modes.supported,
  662. SUPPORTED_TP | SUPPORTED_AUI);
  663. }
  664. spin_unlock_irqrestore(&lp->lock, flags);
  665. return 0;
  666. }
  667. static int pcnet32_set_link_ksettings(struct net_device *dev,
  668. const struct ethtool_link_ksettings *cmd)
  669. {
  670. struct pcnet32_private *lp = netdev_priv(dev);
  671. ulong ioaddr = dev->base_addr;
  672. unsigned long flags;
  673. int r = -EOPNOTSUPP;
  674. int suspended, bcr2, bcr9, csr15;
  675. spin_lock_irqsave(&lp->lock, flags);
  676. if (lp->mii) {
  677. r = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
  678. } else if (lp->chip_version == PCNET32_79C970A) {
  679. suspended = pcnet32_suspend(dev, &flags, 0);
  680. if (!suspended)
  681. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP);
  682. lp->autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
  683. bcr2 = lp->a->read_bcr(ioaddr, 2);
  684. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  685. lp->a->write_bcr(ioaddr, 2, bcr2 | 0x0002);
  686. } else {
  687. lp->a->write_bcr(ioaddr, 2, bcr2 & ~0x0002);
  688. lp->port_tp = cmd->base.port == PORT_TP;
  689. csr15 = lp->a->read_csr(ioaddr, CSR15) & ~0x0180;
  690. if (cmd->base.port == PORT_TP)
  691. csr15 |= 0x0080;
  692. lp->a->write_csr(ioaddr, CSR15, csr15);
  693. lp->init_block->mode = cpu_to_le16(csr15);
  694. lp->fdx = cmd->base.duplex == DUPLEX_FULL;
  695. bcr9 = lp->a->read_bcr(ioaddr, 9) & ~0x0003;
  696. if (cmd->base.duplex == DUPLEX_FULL)
  697. bcr9 |= 0x0003;
  698. lp->a->write_bcr(ioaddr, 9, bcr9);
  699. }
  700. if (suspended)
  701. pcnet32_clr_suspend(lp, ioaddr);
  702. else if (netif_running(dev))
  703. pcnet32_restart(dev, CSR0_NORMAL);
  704. r = 0;
  705. }
  706. spin_unlock_irqrestore(&lp->lock, flags);
  707. return r;
  708. }
  709. static void pcnet32_get_drvinfo(struct net_device *dev,
  710. struct ethtool_drvinfo *info)
  711. {
  712. struct pcnet32_private *lp = netdev_priv(dev);
  713. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  714. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  715. if (lp->pci_dev)
  716. strlcpy(info->bus_info, pci_name(lp->pci_dev),
  717. sizeof(info->bus_info));
  718. else
  719. snprintf(info->bus_info, sizeof(info->bus_info),
  720. "VLB 0x%lx", dev->base_addr);
  721. }
  722. static u32 pcnet32_get_link(struct net_device *dev)
  723. {
  724. struct pcnet32_private *lp = netdev_priv(dev);
  725. unsigned long flags;
  726. int r;
  727. spin_lock_irqsave(&lp->lock, flags);
  728. if (lp->mii) {
  729. r = mii_link_ok(&lp->mii_if);
  730. } else if (lp->chip_version == PCNET32_79C970A) {
  731. ulong ioaddr = dev->base_addr; /* card base I/O address */
  732. /* only read link if port is set to TP */
  733. if (!lp->autoneg && lp->port_tp)
  734. r = (lp->a->read_bcr(ioaddr, 4) != 0xc0);
  735. else /* link always up for AUI port or port auto select */
  736. r = 1;
  737. } else if (lp->chip_version > PCNET32_79C970A) {
  738. ulong ioaddr = dev->base_addr; /* card base I/O address */
  739. r = (lp->a->read_bcr(ioaddr, 4) != 0xc0);
  740. } else { /* can not detect link on really old chips */
  741. r = 1;
  742. }
  743. spin_unlock_irqrestore(&lp->lock, flags);
  744. return r;
  745. }
  746. static u32 pcnet32_get_msglevel(struct net_device *dev)
  747. {
  748. struct pcnet32_private *lp = netdev_priv(dev);
  749. return lp->msg_enable;
  750. }
  751. static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
  752. {
  753. struct pcnet32_private *lp = netdev_priv(dev);
  754. lp->msg_enable = value;
  755. }
  756. static int pcnet32_nway_reset(struct net_device *dev)
  757. {
  758. struct pcnet32_private *lp = netdev_priv(dev);
  759. unsigned long flags;
  760. int r = -EOPNOTSUPP;
  761. if (lp->mii) {
  762. spin_lock_irqsave(&lp->lock, flags);
  763. r = mii_nway_restart(&lp->mii_if);
  764. spin_unlock_irqrestore(&lp->lock, flags);
  765. }
  766. return r;
  767. }
  768. static void pcnet32_get_ringparam(struct net_device *dev,
  769. struct ethtool_ringparam *ering)
  770. {
  771. struct pcnet32_private *lp = netdev_priv(dev);
  772. ering->tx_max_pending = TX_MAX_RING_SIZE;
  773. ering->tx_pending = lp->tx_ring_size;
  774. ering->rx_max_pending = RX_MAX_RING_SIZE;
  775. ering->rx_pending = lp->rx_ring_size;
  776. }
  777. static int pcnet32_set_ringparam(struct net_device *dev,
  778. struct ethtool_ringparam *ering)
  779. {
  780. struct pcnet32_private *lp = netdev_priv(dev);
  781. unsigned long flags;
  782. unsigned int size;
  783. ulong ioaddr = dev->base_addr;
  784. int i;
  785. if (ering->rx_mini_pending || ering->rx_jumbo_pending)
  786. return -EINVAL;
  787. if (netif_running(dev))
  788. pcnet32_netif_stop(dev);
  789. spin_lock_irqsave(&lp->lock, flags);
  790. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
  791. size = min(ering->tx_pending, (unsigned int)TX_MAX_RING_SIZE);
  792. /* set the minimum ring size to 4, to allow the loopback test to work
  793. * unchanged.
  794. */
  795. for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
  796. if (size <= (1 << i))
  797. break;
  798. }
  799. if ((1 << i) != lp->tx_ring_size)
  800. pcnet32_realloc_tx_ring(dev, lp, i);
  801. size = min(ering->rx_pending, (unsigned int)RX_MAX_RING_SIZE);
  802. for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
  803. if (size <= (1 << i))
  804. break;
  805. }
  806. if ((1 << i) != lp->rx_ring_size)
  807. pcnet32_realloc_rx_ring(dev, lp, i);
  808. lp->napi.weight = lp->rx_ring_size / 2;
  809. if (netif_running(dev)) {
  810. pcnet32_netif_start(dev);
  811. pcnet32_restart(dev, CSR0_NORMAL);
  812. }
  813. spin_unlock_irqrestore(&lp->lock, flags);
  814. netif_info(lp, drv, dev, "Ring Param Settings: RX: %d, TX: %d\n",
  815. lp->rx_ring_size, lp->tx_ring_size);
  816. return 0;
  817. }
  818. static void pcnet32_get_strings(struct net_device *dev, u32 stringset,
  819. u8 *data)
  820. {
  821. memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
  822. }
  823. static int pcnet32_get_sset_count(struct net_device *dev, int sset)
  824. {
  825. switch (sset) {
  826. case ETH_SS_TEST:
  827. return PCNET32_TEST_LEN;
  828. default:
  829. return -EOPNOTSUPP;
  830. }
  831. }
  832. static void pcnet32_ethtool_test(struct net_device *dev,
  833. struct ethtool_test *test, u64 * data)
  834. {
  835. struct pcnet32_private *lp = netdev_priv(dev);
  836. int rc;
  837. if (test->flags == ETH_TEST_FL_OFFLINE) {
  838. rc = pcnet32_loopback_test(dev, data);
  839. if (rc) {
  840. netif_printk(lp, hw, KERN_DEBUG, dev,
  841. "Loopback test failed\n");
  842. test->flags |= ETH_TEST_FL_FAILED;
  843. } else
  844. netif_printk(lp, hw, KERN_DEBUG, dev,
  845. "Loopback test passed\n");
  846. } else
  847. netif_printk(lp, hw, KERN_DEBUG, dev,
  848. "No tests to run (specify 'Offline' on ethtool)\n");
  849. } /* end pcnet32_ethtool_test */
  850. static int pcnet32_loopback_test(struct net_device *dev, uint64_t * data1)
  851. {
  852. struct pcnet32_private *lp = netdev_priv(dev);
  853. const struct pcnet32_access *a = lp->a; /* access to registers */
  854. ulong ioaddr = dev->base_addr; /* card base I/O address */
  855. struct sk_buff *skb; /* sk buff */
  856. int x, i; /* counters */
  857. int numbuffs = 4; /* number of TX/RX buffers and descs */
  858. u16 status = 0x8300; /* TX ring status */
  859. __le16 teststatus; /* test of ring status */
  860. int rc; /* return code */
  861. int size; /* size of packets */
  862. unsigned char *packet; /* source packet data */
  863. static const int data_len = 60; /* length of source packets */
  864. unsigned long flags;
  865. unsigned long ticks;
  866. rc = 1; /* default to fail */
  867. if (netif_running(dev))
  868. pcnet32_netif_stop(dev);
  869. spin_lock_irqsave(&lp->lock, flags);
  870. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP); /* stop the chip */
  871. numbuffs = min(numbuffs, (int)min(lp->rx_ring_size, lp->tx_ring_size));
  872. /* Reset the PCNET32 */
  873. lp->a->reset(ioaddr);
  874. lp->a->write_csr(ioaddr, CSR4, 0x0915); /* auto tx pad */
  875. /* switch pcnet32 to 32bit mode */
  876. lp->a->write_bcr(ioaddr, 20, 2);
  877. /* purge & init rings but don't actually restart */
  878. pcnet32_restart(dev, 0x0000);
  879. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP); /* Set STOP bit */
  880. /* Initialize Transmit buffers. */
  881. size = data_len + 15;
  882. for (x = 0; x < numbuffs; x++) {
  883. skb = netdev_alloc_skb(dev, size);
  884. if (!skb) {
  885. netif_printk(lp, hw, KERN_DEBUG, dev,
  886. "Cannot allocate skb at line: %d!\n",
  887. __LINE__);
  888. goto clean_up;
  889. }
  890. packet = skb->data;
  891. skb_put(skb, size); /* create space for data */
  892. lp->tx_skbuff[x] = skb;
  893. lp->tx_ring[x].length = cpu_to_le16(-skb->len);
  894. lp->tx_ring[x].misc = 0;
  895. /* put DA and SA into the skb */
  896. for (i = 0; i < 6; i++)
  897. *packet++ = dev->dev_addr[i];
  898. for (i = 0; i < 6; i++)
  899. *packet++ = dev->dev_addr[i];
  900. /* type */
  901. *packet++ = 0x08;
  902. *packet++ = 0x06;
  903. /* packet number */
  904. *packet++ = x;
  905. /* fill packet with data */
  906. for (i = 0; i < data_len; i++)
  907. *packet++ = i;
  908. lp->tx_dma_addr[x] =
  909. pci_map_single(lp->pci_dev, skb->data, skb->len,
  910. PCI_DMA_TODEVICE);
  911. if (pci_dma_mapping_error(lp->pci_dev, lp->tx_dma_addr[x])) {
  912. netif_printk(lp, hw, KERN_DEBUG, dev,
  913. "DMA mapping error at line: %d!\n",
  914. __LINE__);
  915. goto clean_up;
  916. }
  917. lp->tx_ring[x].base = cpu_to_le32(lp->tx_dma_addr[x]);
  918. wmb(); /* Make sure owner changes after all others are visible */
  919. lp->tx_ring[x].status = cpu_to_le16(status);
  920. }
  921. x = a->read_bcr(ioaddr, 32); /* set internal loopback in BCR32 */
  922. a->write_bcr(ioaddr, 32, x | 0x0002);
  923. /* set int loopback in CSR15 */
  924. x = a->read_csr(ioaddr, CSR15) & 0xfffc;
  925. lp->a->write_csr(ioaddr, CSR15, x | 0x0044);
  926. teststatus = cpu_to_le16(0x8000);
  927. lp->a->write_csr(ioaddr, CSR0, CSR0_START); /* Set STRT bit */
  928. /* Check status of descriptors */
  929. for (x = 0; x < numbuffs; x++) {
  930. ticks = 0;
  931. rmb();
  932. while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
  933. spin_unlock_irqrestore(&lp->lock, flags);
  934. msleep(1);
  935. spin_lock_irqsave(&lp->lock, flags);
  936. rmb();
  937. ticks++;
  938. }
  939. if (ticks == 200) {
  940. netif_err(lp, hw, dev, "Desc %d failed to reset!\n", x);
  941. break;
  942. }
  943. }
  944. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP); /* Set STOP bit */
  945. wmb();
  946. if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
  947. netdev_printk(KERN_DEBUG, dev, "RX loopback packets:\n");
  948. for (x = 0; x < numbuffs; x++) {
  949. netdev_printk(KERN_DEBUG, dev, "Packet %d: ", x);
  950. skb = lp->rx_skbuff[x];
  951. for (i = 0; i < size; i++)
  952. pr_cont(" %02x", *(skb->data + i));
  953. pr_cont("\n");
  954. }
  955. }
  956. x = 0;
  957. rc = 0;
  958. while (x < numbuffs && !rc) {
  959. skb = lp->rx_skbuff[x];
  960. packet = lp->tx_skbuff[x]->data;
  961. for (i = 0; i < size; i++) {
  962. if (*(skb->data + i) != packet[i]) {
  963. netif_printk(lp, hw, KERN_DEBUG, dev,
  964. "Error in compare! %2x - %02x %02x\n",
  965. i, *(skb->data + i), packet[i]);
  966. rc = 1;
  967. break;
  968. }
  969. }
  970. x++;
  971. }
  972. clean_up:
  973. *data1 = rc;
  974. pcnet32_purge_tx_ring(dev);
  975. x = a->read_csr(ioaddr, CSR15);
  976. a->write_csr(ioaddr, CSR15, (x & ~0x0044)); /* reset bits 6 and 2 */
  977. x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
  978. a->write_bcr(ioaddr, 32, (x & ~0x0002));
  979. if (netif_running(dev)) {
  980. pcnet32_netif_start(dev);
  981. pcnet32_restart(dev, CSR0_NORMAL);
  982. } else {
  983. pcnet32_purge_rx_ring(dev);
  984. lp->a->write_bcr(ioaddr, 20, 4); /* return to 16bit mode */
  985. }
  986. spin_unlock_irqrestore(&lp->lock, flags);
  987. return rc;
  988. } /* end pcnet32_loopback_test */
  989. static int pcnet32_set_phys_id(struct net_device *dev,
  990. enum ethtool_phys_id_state state)
  991. {
  992. struct pcnet32_private *lp = netdev_priv(dev);
  993. const struct pcnet32_access *a = lp->a;
  994. ulong ioaddr = dev->base_addr;
  995. unsigned long flags;
  996. int i;
  997. switch (state) {
  998. case ETHTOOL_ID_ACTIVE:
  999. /* Save the current value of the bcrs */
  1000. spin_lock_irqsave(&lp->lock, flags);
  1001. for (i = 4; i < 8; i++)
  1002. lp->save_regs[i - 4] = a->read_bcr(ioaddr, i);
  1003. spin_unlock_irqrestore(&lp->lock, flags);
  1004. return 2; /* cycle on/off twice per second */
  1005. case ETHTOOL_ID_ON:
  1006. case ETHTOOL_ID_OFF:
  1007. /* Blink the led */
  1008. spin_lock_irqsave(&lp->lock, flags);
  1009. for (i = 4; i < 8; i++)
  1010. a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
  1011. spin_unlock_irqrestore(&lp->lock, flags);
  1012. break;
  1013. case ETHTOOL_ID_INACTIVE:
  1014. /* Restore the original value of the bcrs */
  1015. spin_lock_irqsave(&lp->lock, flags);
  1016. for (i = 4; i < 8; i++)
  1017. a->write_bcr(ioaddr, i, lp->save_regs[i - 4]);
  1018. spin_unlock_irqrestore(&lp->lock, flags);
  1019. }
  1020. return 0;
  1021. }
  1022. /*
  1023. * process one receive descriptor entry
  1024. */
  1025. static void pcnet32_rx_entry(struct net_device *dev,
  1026. struct pcnet32_private *lp,
  1027. struct pcnet32_rx_head *rxp,
  1028. int entry)
  1029. {
  1030. int status = (short)le16_to_cpu(rxp->status) >> 8;
  1031. int rx_in_place = 0;
  1032. struct sk_buff *skb;
  1033. short pkt_len;
  1034. if (status != 0x03) { /* There was an error. */
  1035. /*
  1036. * There is a tricky error noted by John Murphy,
  1037. * <murf@perftech.com> to Russ Nelson: Even with full-sized
  1038. * buffers it's possible for a jabber packet to use two
  1039. * buffers, with only the last correctly noting the error.
  1040. */
  1041. if (status & 0x01) /* Only count a general error at the */
  1042. dev->stats.rx_errors++; /* end of a packet. */
  1043. if (status & 0x20)
  1044. dev->stats.rx_frame_errors++;
  1045. if (status & 0x10)
  1046. dev->stats.rx_over_errors++;
  1047. if (status & 0x08)
  1048. dev->stats.rx_crc_errors++;
  1049. if (status & 0x04)
  1050. dev->stats.rx_fifo_errors++;
  1051. return;
  1052. }
  1053. pkt_len = (le32_to_cpu(rxp->msg_length) & 0xfff) - 4;
  1054. /* Discard oversize frames. */
  1055. if (unlikely(pkt_len > PKT_BUF_SIZE)) {
  1056. netif_err(lp, drv, dev, "Impossible packet size %d!\n",
  1057. pkt_len);
  1058. dev->stats.rx_errors++;
  1059. return;
  1060. }
  1061. if (pkt_len < 60) {
  1062. netif_err(lp, rx_err, dev, "Runt packet!\n");
  1063. dev->stats.rx_errors++;
  1064. return;
  1065. }
  1066. if (pkt_len > rx_copybreak) {
  1067. struct sk_buff *newskb;
  1068. dma_addr_t new_dma_addr;
  1069. newskb = netdev_alloc_skb(dev, PKT_BUF_SKB);
  1070. /*
  1071. * map the new buffer, if mapping fails, drop the packet and
  1072. * reuse the old buffer
  1073. */
  1074. if (newskb) {
  1075. skb_reserve(newskb, NET_IP_ALIGN);
  1076. new_dma_addr = pci_map_single(lp->pci_dev,
  1077. newskb->data,
  1078. PKT_BUF_SIZE,
  1079. PCI_DMA_FROMDEVICE);
  1080. if (pci_dma_mapping_error(lp->pci_dev, new_dma_addr)) {
  1081. netif_err(lp, rx_err, dev,
  1082. "DMA mapping error.\n");
  1083. dev_kfree_skb(newskb);
  1084. skb = NULL;
  1085. } else {
  1086. skb = lp->rx_skbuff[entry];
  1087. pci_unmap_single(lp->pci_dev,
  1088. lp->rx_dma_addr[entry],
  1089. PKT_BUF_SIZE,
  1090. PCI_DMA_FROMDEVICE);
  1091. skb_put(skb, pkt_len);
  1092. lp->rx_skbuff[entry] = newskb;
  1093. lp->rx_dma_addr[entry] = new_dma_addr;
  1094. rxp->base = cpu_to_le32(new_dma_addr);
  1095. rx_in_place = 1;
  1096. }
  1097. } else
  1098. skb = NULL;
  1099. } else
  1100. skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
  1101. if (skb == NULL) {
  1102. dev->stats.rx_dropped++;
  1103. return;
  1104. }
  1105. if (!rx_in_place) {
  1106. skb_reserve(skb, NET_IP_ALIGN);
  1107. skb_put(skb, pkt_len); /* Make room */
  1108. pci_dma_sync_single_for_cpu(lp->pci_dev,
  1109. lp->rx_dma_addr[entry],
  1110. pkt_len,
  1111. PCI_DMA_FROMDEVICE);
  1112. skb_copy_to_linear_data(skb,
  1113. (unsigned char *)(lp->rx_skbuff[entry]->data),
  1114. pkt_len);
  1115. pci_dma_sync_single_for_device(lp->pci_dev,
  1116. lp->rx_dma_addr[entry],
  1117. pkt_len,
  1118. PCI_DMA_FROMDEVICE);
  1119. }
  1120. dev->stats.rx_bytes += skb->len;
  1121. skb->protocol = eth_type_trans(skb, dev);
  1122. netif_receive_skb(skb);
  1123. dev->stats.rx_packets++;
  1124. }
  1125. static int pcnet32_rx(struct net_device *dev, int budget)
  1126. {
  1127. struct pcnet32_private *lp = netdev_priv(dev);
  1128. int entry = lp->cur_rx & lp->rx_mod_mask;
  1129. struct pcnet32_rx_head *rxp = &lp->rx_ring[entry];
  1130. int npackets = 0;
  1131. /* If we own the next entry, it's a new packet. Send it up. */
  1132. while (npackets < budget && (short)le16_to_cpu(rxp->status) >= 0) {
  1133. pcnet32_rx_entry(dev, lp, rxp, entry);
  1134. npackets += 1;
  1135. /*
  1136. * The docs say that the buffer length isn't touched, but Andrew
  1137. * Boyd of QNX reports that some revs of the 79C965 clear it.
  1138. */
  1139. rxp->buf_length = cpu_to_le16(NEG_BUF_SIZE);
  1140. wmb(); /* Make sure owner changes after others are visible */
  1141. rxp->status = cpu_to_le16(0x8000);
  1142. entry = (++lp->cur_rx) & lp->rx_mod_mask;
  1143. rxp = &lp->rx_ring[entry];
  1144. }
  1145. return npackets;
  1146. }
  1147. static int pcnet32_tx(struct net_device *dev)
  1148. {
  1149. struct pcnet32_private *lp = netdev_priv(dev);
  1150. unsigned int dirty_tx = lp->dirty_tx;
  1151. int delta;
  1152. int must_restart = 0;
  1153. while (dirty_tx != lp->cur_tx) {
  1154. int entry = dirty_tx & lp->tx_mod_mask;
  1155. int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
  1156. if (status < 0)
  1157. break; /* It still hasn't been Txed */
  1158. lp->tx_ring[entry].base = 0;
  1159. if (status & 0x4000) {
  1160. /* There was a major error, log it. */
  1161. int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
  1162. dev->stats.tx_errors++;
  1163. netif_err(lp, tx_err, dev,
  1164. "Tx error status=%04x err_status=%08x\n",
  1165. status, err_status);
  1166. if (err_status & 0x04000000)
  1167. dev->stats.tx_aborted_errors++;
  1168. if (err_status & 0x08000000)
  1169. dev->stats.tx_carrier_errors++;
  1170. if (err_status & 0x10000000)
  1171. dev->stats.tx_window_errors++;
  1172. #ifndef DO_DXSUFLO
  1173. if (err_status & 0x40000000) {
  1174. dev->stats.tx_fifo_errors++;
  1175. /* Ackk! On FIFO errors the Tx unit is turned off! */
  1176. /* Remove this verbosity later! */
  1177. netif_err(lp, tx_err, dev, "Tx FIFO error!\n");
  1178. must_restart = 1;
  1179. }
  1180. #else
  1181. if (err_status & 0x40000000) {
  1182. dev->stats.tx_fifo_errors++;
  1183. if (!lp->dxsuflo) { /* If controller doesn't recover ... */
  1184. /* Ackk! On FIFO errors the Tx unit is turned off! */
  1185. /* Remove this verbosity later! */
  1186. netif_err(lp, tx_err, dev, "Tx FIFO error!\n");
  1187. must_restart = 1;
  1188. }
  1189. }
  1190. #endif
  1191. } else {
  1192. if (status & 0x1800)
  1193. dev->stats.collisions++;
  1194. dev->stats.tx_packets++;
  1195. }
  1196. /* We must free the original skb */
  1197. if (lp->tx_skbuff[entry]) {
  1198. pci_unmap_single(lp->pci_dev,
  1199. lp->tx_dma_addr[entry],
  1200. lp->tx_skbuff[entry]->
  1201. len, PCI_DMA_TODEVICE);
  1202. dev_kfree_skb_any(lp->tx_skbuff[entry]);
  1203. lp->tx_skbuff[entry] = NULL;
  1204. lp->tx_dma_addr[entry] = 0;
  1205. }
  1206. dirty_tx++;
  1207. }
  1208. delta = (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask + lp->tx_ring_size);
  1209. if (delta > lp->tx_ring_size) {
  1210. netif_err(lp, drv, dev, "out-of-sync dirty pointer, %d vs. %d, full=%d\n",
  1211. dirty_tx, lp->cur_tx, lp->tx_full);
  1212. dirty_tx += lp->tx_ring_size;
  1213. delta -= lp->tx_ring_size;
  1214. }
  1215. if (lp->tx_full &&
  1216. netif_queue_stopped(dev) &&
  1217. delta < lp->tx_ring_size - 2) {
  1218. /* The ring is no longer full, clear tbusy. */
  1219. lp->tx_full = 0;
  1220. netif_wake_queue(dev);
  1221. }
  1222. lp->dirty_tx = dirty_tx;
  1223. return must_restart;
  1224. }
  1225. static int pcnet32_poll(struct napi_struct *napi, int budget)
  1226. {
  1227. struct pcnet32_private *lp = container_of(napi, struct pcnet32_private, napi);
  1228. struct net_device *dev = lp->dev;
  1229. unsigned long ioaddr = dev->base_addr;
  1230. unsigned long flags;
  1231. int work_done;
  1232. u16 val;
  1233. work_done = pcnet32_rx(dev, budget);
  1234. spin_lock_irqsave(&lp->lock, flags);
  1235. if (pcnet32_tx(dev)) {
  1236. /* reset the chip to clear the error condition, then restart */
  1237. lp->a->reset(ioaddr);
  1238. lp->a->write_csr(ioaddr, CSR4, 0x0915); /* auto tx pad */
  1239. pcnet32_restart(dev, CSR0_START);
  1240. netif_wake_queue(dev);
  1241. }
  1242. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1243. /* clear interrupt masks */
  1244. val = lp->a->read_csr(ioaddr, CSR3);
  1245. val &= 0x00ff;
  1246. lp->a->write_csr(ioaddr, CSR3, val);
  1247. /* Set interrupt enable. */
  1248. lp->a->write_csr(ioaddr, CSR0, CSR0_INTEN);
  1249. }
  1250. spin_unlock_irqrestore(&lp->lock, flags);
  1251. return work_done;
  1252. }
  1253. #define PCNET32_REGS_PER_PHY 32
  1254. #define PCNET32_MAX_PHYS 32
  1255. static int pcnet32_get_regs_len(struct net_device *dev)
  1256. {
  1257. struct pcnet32_private *lp = netdev_priv(dev);
  1258. int j = lp->phycount * PCNET32_REGS_PER_PHY;
  1259. return (PCNET32_NUM_REGS + j) * sizeof(u16);
  1260. }
  1261. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1262. void *ptr)
  1263. {
  1264. int i, csr0;
  1265. u16 *buff = ptr;
  1266. struct pcnet32_private *lp = netdev_priv(dev);
  1267. const struct pcnet32_access *a = lp->a;
  1268. ulong ioaddr = dev->base_addr;
  1269. unsigned long flags;
  1270. spin_lock_irqsave(&lp->lock, flags);
  1271. csr0 = a->read_csr(ioaddr, CSR0);
  1272. if (!(csr0 & CSR0_STOP)) /* If not stopped */
  1273. pcnet32_suspend(dev, &flags, 1);
  1274. /* read address PROM */
  1275. for (i = 0; i < 16; i += 2)
  1276. *buff++ = inw(ioaddr + i);
  1277. /* read control and status registers */
  1278. for (i = 0; i < 90; i++)
  1279. *buff++ = a->read_csr(ioaddr, i);
  1280. *buff++ = a->read_csr(ioaddr, 112);
  1281. *buff++ = a->read_csr(ioaddr, 114);
  1282. /* read bus configuration registers */
  1283. for (i = 0; i < 30; i++)
  1284. *buff++ = a->read_bcr(ioaddr, i);
  1285. *buff++ = 0; /* skip bcr30 so as not to hang 79C976 */
  1286. for (i = 31; i < 36; i++)
  1287. *buff++ = a->read_bcr(ioaddr, i);
  1288. /* read mii phy registers */
  1289. if (lp->mii) {
  1290. int j;
  1291. for (j = 0; j < PCNET32_MAX_PHYS; j++) {
  1292. if (lp->phymask & (1 << j)) {
  1293. for (i = 0; i < PCNET32_REGS_PER_PHY; i++) {
  1294. lp->a->write_bcr(ioaddr, 33,
  1295. (j << 5) | i);
  1296. *buff++ = lp->a->read_bcr(ioaddr, 34);
  1297. }
  1298. }
  1299. }
  1300. }
  1301. if (!(csr0 & CSR0_STOP)) /* If not stopped */
  1302. pcnet32_clr_suspend(lp, ioaddr);
  1303. spin_unlock_irqrestore(&lp->lock, flags);
  1304. }
  1305. static const struct ethtool_ops pcnet32_ethtool_ops = {
  1306. .get_drvinfo = pcnet32_get_drvinfo,
  1307. .get_msglevel = pcnet32_get_msglevel,
  1308. .set_msglevel = pcnet32_set_msglevel,
  1309. .nway_reset = pcnet32_nway_reset,
  1310. .get_link = pcnet32_get_link,
  1311. .get_ringparam = pcnet32_get_ringparam,
  1312. .set_ringparam = pcnet32_set_ringparam,
  1313. .get_strings = pcnet32_get_strings,
  1314. .self_test = pcnet32_ethtool_test,
  1315. .set_phys_id = pcnet32_set_phys_id,
  1316. .get_regs_len = pcnet32_get_regs_len,
  1317. .get_regs = pcnet32_get_regs,
  1318. .get_sset_count = pcnet32_get_sset_count,
  1319. .get_link_ksettings = pcnet32_get_link_ksettings,
  1320. .set_link_ksettings = pcnet32_set_link_ksettings,
  1321. };
  1322. /* only probes for non-PCI devices, the rest are handled by
  1323. * pci_register_driver via pcnet32_probe_pci */
  1324. static void pcnet32_probe_vlbus(unsigned int *pcnet32_portlist)
  1325. {
  1326. unsigned int *port, ioaddr;
  1327. /* search for PCnet32 VLB cards at known addresses */
  1328. for (port = pcnet32_portlist; (ioaddr = *port); port++) {
  1329. if (request_region
  1330. (ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
  1331. /* check if there is really a pcnet chip on that ioaddr */
  1332. if ((inb(ioaddr + 14) == 0x57) &&
  1333. (inb(ioaddr + 15) == 0x57)) {
  1334. pcnet32_probe1(ioaddr, 0, NULL);
  1335. } else {
  1336. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  1337. }
  1338. }
  1339. }
  1340. }
  1341. static int
  1342. pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
  1343. {
  1344. unsigned long ioaddr;
  1345. int err;
  1346. err = pci_enable_device(pdev);
  1347. if (err < 0) {
  1348. if (pcnet32_debug & NETIF_MSG_PROBE)
  1349. pr_err("failed to enable device -- err=%d\n", err);
  1350. return err;
  1351. }
  1352. pci_set_master(pdev);
  1353. if (!pci_resource_len(pdev, 0)) {
  1354. if (pcnet32_debug & NETIF_MSG_PROBE)
  1355. pr_err("card has no PCI IO resources, aborting\n");
  1356. err = -ENODEV;
  1357. goto err_disable_dev;
  1358. }
  1359. err = pci_set_dma_mask(pdev, PCNET32_DMA_MASK);
  1360. if (err) {
  1361. if (pcnet32_debug & NETIF_MSG_PROBE)
  1362. pr_err("architecture does not support 32bit PCI busmaster DMA\n");
  1363. goto err_disable_dev;
  1364. }
  1365. ioaddr = pci_resource_start(pdev, 0);
  1366. if (!request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci")) {
  1367. if (pcnet32_debug & NETIF_MSG_PROBE)
  1368. pr_err("io address range already allocated\n");
  1369. err = -EBUSY;
  1370. goto err_disable_dev;
  1371. }
  1372. err = pcnet32_probe1(ioaddr, 1, pdev);
  1373. err_disable_dev:
  1374. if (err < 0)
  1375. pci_disable_device(pdev);
  1376. return err;
  1377. }
  1378. static const struct net_device_ops pcnet32_netdev_ops = {
  1379. .ndo_open = pcnet32_open,
  1380. .ndo_stop = pcnet32_close,
  1381. .ndo_start_xmit = pcnet32_start_xmit,
  1382. .ndo_tx_timeout = pcnet32_tx_timeout,
  1383. .ndo_get_stats = pcnet32_get_stats,
  1384. .ndo_set_rx_mode = pcnet32_set_multicast_list,
  1385. .ndo_do_ioctl = pcnet32_ioctl,
  1386. .ndo_set_mac_address = eth_mac_addr,
  1387. .ndo_validate_addr = eth_validate_addr,
  1388. #ifdef CONFIG_NET_POLL_CONTROLLER
  1389. .ndo_poll_controller = pcnet32_poll_controller,
  1390. #endif
  1391. };
  1392. /* pcnet32_probe1
  1393. * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
  1394. * pdev will be NULL when called from pcnet32_probe_vlbus.
  1395. */
  1396. static int
  1397. pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
  1398. {
  1399. struct pcnet32_private *lp;
  1400. int i, media;
  1401. int fdx, mii, fset, dxsuflo, sram;
  1402. int chip_version;
  1403. char *chipname;
  1404. struct net_device *dev;
  1405. const struct pcnet32_access *a = NULL;
  1406. u8 promaddr[ETH_ALEN];
  1407. int ret = -ENODEV;
  1408. /* reset the chip */
  1409. pcnet32_wio_reset(ioaddr);
  1410. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  1411. if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
  1412. a = &pcnet32_wio;
  1413. } else {
  1414. pcnet32_dwio_reset(ioaddr);
  1415. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4 &&
  1416. pcnet32_dwio_check(ioaddr)) {
  1417. a = &pcnet32_dwio;
  1418. } else {
  1419. if (pcnet32_debug & NETIF_MSG_PROBE)
  1420. pr_err("No access methods\n");
  1421. goto err_release_region;
  1422. }
  1423. }
  1424. chip_version =
  1425. a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr, 89) << 16);
  1426. if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
  1427. pr_info(" PCnet chip version is %#x\n", chip_version);
  1428. if ((chip_version & 0xfff) != 0x003) {
  1429. if (pcnet32_debug & NETIF_MSG_PROBE)
  1430. pr_info("Unsupported chip version\n");
  1431. goto err_release_region;
  1432. }
  1433. /* initialize variables */
  1434. fdx = mii = fset = dxsuflo = sram = 0;
  1435. chip_version = (chip_version >> 12) & 0xffff;
  1436. switch (chip_version) {
  1437. case 0x2420:
  1438. chipname = "PCnet/PCI 79C970"; /* PCI */
  1439. break;
  1440. case 0x2430:
  1441. if (shared)
  1442. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  1443. else
  1444. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  1445. break;
  1446. case 0x2621:
  1447. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  1448. fdx = 1;
  1449. break;
  1450. case 0x2623:
  1451. chipname = "PCnet/FAST 79C971"; /* PCI */
  1452. fdx = 1;
  1453. mii = 1;
  1454. fset = 1;
  1455. break;
  1456. case 0x2624:
  1457. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  1458. fdx = 1;
  1459. mii = 1;
  1460. fset = 1;
  1461. break;
  1462. case 0x2625:
  1463. chipname = "PCnet/FAST III 79C973"; /* PCI */
  1464. fdx = 1;
  1465. mii = 1;
  1466. sram = 1;
  1467. break;
  1468. case 0x2626:
  1469. chipname = "PCnet/Home 79C978"; /* PCI */
  1470. fdx = 1;
  1471. /*
  1472. * This is based on specs published at www.amd.com. This section
  1473. * assumes that a card with a 79C978 wants to go into standard
  1474. * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
  1475. * and the module option homepna=1 can select this instead.
  1476. */
  1477. media = a->read_bcr(ioaddr, 49);
  1478. media &= ~3; /* default to 10Mb ethernet */
  1479. if (cards_found < MAX_UNITS && homepna[cards_found])
  1480. media |= 1; /* switch to home wiring mode */
  1481. if (pcnet32_debug & NETIF_MSG_PROBE)
  1482. printk(KERN_DEBUG PFX "media set to %sMbit mode\n",
  1483. (media & 1) ? "1" : "10");
  1484. a->write_bcr(ioaddr, 49, media);
  1485. break;
  1486. case 0x2627:
  1487. chipname = "PCnet/FAST III 79C975"; /* PCI */
  1488. fdx = 1;
  1489. mii = 1;
  1490. sram = 1;
  1491. break;
  1492. case 0x2628:
  1493. chipname = "PCnet/PRO 79C976";
  1494. fdx = 1;
  1495. mii = 1;
  1496. break;
  1497. default:
  1498. if (pcnet32_debug & NETIF_MSG_PROBE)
  1499. pr_info("PCnet version %#x, no PCnet32 chip\n",
  1500. chip_version);
  1501. goto err_release_region;
  1502. }
  1503. /*
  1504. * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
  1505. * starting until the packet is loaded. Strike one for reliability, lose
  1506. * one for latency - although on PCI this isn't a big loss. Older chips
  1507. * have FIFO's smaller than a packet, so you can't do this.
  1508. * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
  1509. */
  1510. if (fset) {
  1511. a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
  1512. a->write_csr(ioaddr, 80,
  1513. (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
  1514. dxsuflo = 1;
  1515. }
  1516. /*
  1517. * The Am79C973/Am79C975 controllers come with 12K of SRAM
  1518. * which we can use for the Tx/Rx buffers but most importantly,
  1519. * the use of SRAM allow us to use the BCR18:NOUFLO bit to avoid
  1520. * Tx fifo underflows.
  1521. */
  1522. if (sram) {
  1523. /*
  1524. * The SRAM is being configured in two steps. First we
  1525. * set the SRAM size in the BCR25:SRAM_SIZE bits. According
  1526. * to the datasheet, each bit corresponds to a 512-byte
  1527. * page so we can have at most 24 pages. The SRAM_SIZE
  1528. * holds the value of the upper 8 bits of the 16-bit SRAM size.
  1529. * The low 8-bits start at 0x00 and end at 0xff. So the
  1530. * address range is from 0x0000 up to 0x17ff. Therefore,
  1531. * the SRAM_SIZE is set to 0x17. The next step is to set
  1532. * the BCR26:SRAM_BND midway through so the Tx and Rx
  1533. * buffers can share the SRAM equally.
  1534. */
  1535. a->write_bcr(ioaddr, 25, 0x17);
  1536. a->write_bcr(ioaddr, 26, 0xc);
  1537. /* And finally enable the NOUFLO bit */
  1538. a->write_bcr(ioaddr, 18, a->read_bcr(ioaddr, 18) | (1 << 11));
  1539. }
  1540. dev = alloc_etherdev(sizeof(*lp));
  1541. if (!dev) {
  1542. ret = -ENOMEM;
  1543. goto err_release_region;
  1544. }
  1545. if (pdev)
  1546. SET_NETDEV_DEV(dev, &pdev->dev);
  1547. if (pcnet32_debug & NETIF_MSG_PROBE)
  1548. pr_info("%s at %#3lx,", chipname, ioaddr);
  1549. /* In most chips, after a chip reset, the ethernet address is read from the
  1550. * station address PROM at the base address and programmed into the
  1551. * "Physical Address Registers" CSR12-14.
  1552. * As a precautionary measure, we read the PROM values and complain if
  1553. * they disagree with the CSRs. If they miscompare, and the PROM addr
  1554. * is valid, then the PROM addr is used.
  1555. */
  1556. for (i = 0; i < 3; i++) {
  1557. unsigned int val;
  1558. val = a->read_csr(ioaddr, i + 12) & 0x0ffff;
  1559. /* There may be endianness issues here. */
  1560. dev->dev_addr[2 * i] = val & 0x0ff;
  1561. dev->dev_addr[2 * i + 1] = (val >> 8) & 0x0ff;
  1562. }
  1563. /* read PROM address and compare with CSR address */
  1564. for (i = 0; i < ETH_ALEN; i++)
  1565. promaddr[i] = inb(ioaddr + i);
  1566. if (!ether_addr_equal(promaddr, dev->dev_addr) ||
  1567. !is_valid_ether_addr(dev->dev_addr)) {
  1568. if (is_valid_ether_addr(promaddr)) {
  1569. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1570. pr_cont(" warning: CSR address invalid,\n");
  1571. pr_info(" using instead PROM address of");
  1572. }
  1573. memcpy(dev->dev_addr, promaddr, ETH_ALEN);
  1574. }
  1575. }
  1576. /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
  1577. if (!is_valid_ether_addr(dev->dev_addr))
  1578. eth_zero_addr(dev->dev_addr);
  1579. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1580. pr_cont(" %pM", dev->dev_addr);
  1581. /* Version 0x2623 and 0x2624 */
  1582. if (((chip_version + 1) & 0xfffe) == 0x2624) {
  1583. i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
  1584. pr_info(" tx_start_pt(0x%04x):", i);
  1585. switch (i >> 10) {
  1586. case 0:
  1587. pr_cont(" 20 bytes,");
  1588. break;
  1589. case 1:
  1590. pr_cont(" 64 bytes,");
  1591. break;
  1592. case 2:
  1593. pr_cont(" 128 bytes,");
  1594. break;
  1595. case 3:
  1596. pr_cont("~220 bytes,");
  1597. break;
  1598. }
  1599. i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
  1600. pr_cont(" BCR18(%x):", i & 0xffff);
  1601. if (i & (1 << 5))
  1602. pr_cont("BurstWrEn ");
  1603. if (i & (1 << 6))
  1604. pr_cont("BurstRdEn ");
  1605. if (i & (1 << 7))
  1606. pr_cont("DWordIO ");
  1607. if (i & (1 << 11))
  1608. pr_cont("NoUFlow ");
  1609. i = a->read_bcr(ioaddr, 25);
  1610. pr_info(" SRAMSIZE=0x%04x,", i << 8);
  1611. i = a->read_bcr(ioaddr, 26);
  1612. pr_cont(" SRAM_BND=0x%04x,", i << 8);
  1613. i = a->read_bcr(ioaddr, 27);
  1614. if (i & (1 << 14))
  1615. pr_cont("LowLatRx");
  1616. }
  1617. }
  1618. dev->base_addr = ioaddr;
  1619. lp = netdev_priv(dev);
  1620. /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
  1621. lp->init_block = pci_alloc_consistent(pdev, sizeof(*lp->init_block),
  1622. &lp->init_dma_addr);
  1623. if (!lp->init_block) {
  1624. if (pcnet32_debug & NETIF_MSG_PROBE)
  1625. pr_err("Consistent memory allocation failed\n");
  1626. ret = -ENOMEM;
  1627. goto err_free_netdev;
  1628. }
  1629. lp->pci_dev = pdev;
  1630. lp->dev = dev;
  1631. spin_lock_init(&lp->lock);
  1632. lp->name = chipname;
  1633. lp->shared_irq = shared;
  1634. lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
  1635. lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
  1636. lp->tx_mod_mask = lp->tx_ring_size - 1;
  1637. lp->rx_mod_mask = lp->rx_ring_size - 1;
  1638. lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
  1639. lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
  1640. lp->mii_if.full_duplex = fdx;
  1641. lp->mii_if.phy_id_mask = 0x1f;
  1642. lp->mii_if.reg_num_mask = 0x1f;
  1643. lp->dxsuflo = dxsuflo;
  1644. lp->mii = mii;
  1645. lp->chip_version = chip_version;
  1646. lp->msg_enable = pcnet32_debug;
  1647. if ((cards_found >= MAX_UNITS) ||
  1648. (options[cards_found] >= sizeof(options_mapping)))
  1649. lp->options = PCNET32_PORT_ASEL;
  1650. else
  1651. lp->options = options_mapping[options[cards_found]];
  1652. /* force default port to TP on 79C970A so link detection can work */
  1653. if (lp->chip_version == PCNET32_79C970A)
  1654. lp->options = PCNET32_PORT_10BT;
  1655. lp->mii_if.dev = dev;
  1656. lp->mii_if.mdio_read = mdio_read;
  1657. lp->mii_if.mdio_write = mdio_write;
  1658. /* napi.weight is used in both the napi and non-napi cases */
  1659. lp->napi.weight = lp->rx_ring_size / 2;
  1660. netif_napi_add(dev, &lp->napi, pcnet32_poll, lp->rx_ring_size / 2);
  1661. if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
  1662. ((cards_found >= MAX_UNITS) || full_duplex[cards_found]))
  1663. lp->options |= PCNET32_PORT_FD;
  1664. lp->a = a;
  1665. /* prior to register_netdev, dev->name is not yet correct */
  1666. if (pcnet32_alloc_ring(dev, pci_name(lp->pci_dev))) {
  1667. ret = -ENOMEM;
  1668. goto err_free_ring;
  1669. }
  1670. /* detect special T1/E1 WAN card by checking for MAC address */
  1671. if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0 &&
  1672. dev->dev_addr[2] == 0x75)
  1673. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  1674. lp->init_block->mode = cpu_to_le16(0x0003); /* Disable Rx and Tx. */
  1675. lp->init_block->tlen_rlen =
  1676. cpu_to_le16(lp->tx_len_bits | lp->rx_len_bits);
  1677. for (i = 0; i < 6; i++)
  1678. lp->init_block->phys_addr[i] = dev->dev_addr[i];
  1679. lp->init_block->filter[0] = 0x00000000;
  1680. lp->init_block->filter[1] = 0x00000000;
  1681. lp->init_block->rx_ring = cpu_to_le32(lp->rx_ring_dma_addr);
  1682. lp->init_block->tx_ring = cpu_to_le32(lp->tx_ring_dma_addr);
  1683. /* switch pcnet32 to 32bit mode */
  1684. a->write_bcr(ioaddr, 20, 2);
  1685. a->write_csr(ioaddr, 1, (lp->init_dma_addr & 0xffff));
  1686. a->write_csr(ioaddr, 2, (lp->init_dma_addr >> 16));
  1687. if (pdev) { /* use the IRQ provided by PCI */
  1688. dev->irq = pdev->irq;
  1689. if (pcnet32_debug & NETIF_MSG_PROBE)
  1690. pr_cont(" assigned IRQ %d\n", dev->irq);
  1691. } else {
  1692. unsigned long irq_mask = probe_irq_on();
  1693. /*
  1694. * To auto-IRQ we enable the initialization-done and DMA error
  1695. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  1696. * boards will work.
  1697. */
  1698. /* Trigger an initialization just for the interrupt. */
  1699. a->write_csr(ioaddr, CSR0, CSR0_INTEN | CSR0_INIT);
  1700. mdelay(1);
  1701. dev->irq = probe_irq_off(irq_mask);
  1702. if (!dev->irq) {
  1703. if (pcnet32_debug & NETIF_MSG_PROBE)
  1704. pr_cont(", failed to detect IRQ line\n");
  1705. ret = -ENODEV;
  1706. goto err_free_ring;
  1707. }
  1708. if (pcnet32_debug & NETIF_MSG_PROBE)
  1709. pr_cont(", probed IRQ %d\n", dev->irq);
  1710. }
  1711. /* Set the mii phy_id so that we can query the link state */
  1712. if (lp->mii) {
  1713. /* lp->phycount and lp->phymask are set to 0 by memset above */
  1714. lp->mii_if.phy_id = ((lp->a->read_bcr(ioaddr, 33)) >> 5) & 0x1f;
  1715. /* scan for PHYs */
  1716. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  1717. unsigned short id1, id2;
  1718. id1 = mdio_read(dev, i, MII_PHYSID1);
  1719. if (id1 == 0xffff)
  1720. continue;
  1721. id2 = mdio_read(dev, i, MII_PHYSID2);
  1722. if (id2 == 0xffff)
  1723. continue;
  1724. if (i == 31 && ((chip_version + 1) & 0xfffe) == 0x2624)
  1725. continue; /* 79C971 & 79C972 have phantom phy at id 31 */
  1726. lp->phycount++;
  1727. lp->phymask |= (1 << i);
  1728. lp->mii_if.phy_id = i;
  1729. if (pcnet32_debug & NETIF_MSG_PROBE)
  1730. pr_info("Found PHY %04x:%04x at address %d\n",
  1731. id1, id2, i);
  1732. }
  1733. lp->a->write_bcr(ioaddr, 33, (lp->mii_if.phy_id) << 5);
  1734. if (lp->phycount > 1)
  1735. lp->options |= PCNET32_PORT_MII;
  1736. }
  1737. timer_setup(&lp->watchdog_timer, pcnet32_watchdog, 0);
  1738. /* The PCNET32-specific entries in the device structure. */
  1739. dev->netdev_ops = &pcnet32_netdev_ops;
  1740. dev->ethtool_ops = &pcnet32_ethtool_ops;
  1741. dev->watchdog_timeo = (5 * HZ);
  1742. /* Fill in the generic fields of the device structure. */
  1743. if (register_netdev(dev))
  1744. goto err_free_ring;
  1745. if (pdev) {
  1746. pci_set_drvdata(pdev, dev);
  1747. } else {
  1748. lp->next = pcnet32_dev;
  1749. pcnet32_dev = dev;
  1750. }
  1751. if (pcnet32_debug & NETIF_MSG_PROBE)
  1752. pr_info("%s: registered as %s\n", dev->name, lp->name);
  1753. cards_found++;
  1754. /* enable LED writes */
  1755. a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
  1756. return 0;
  1757. err_free_ring:
  1758. pcnet32_free_ring(dev);
  1759. pci_free_consistent(lp->pci_dev, sizeof(*lp->init_block),
  1760. lp->init_block, lp->init_dma_addr);
  1761. err_free_netdev:
  1762. free_netdev(dev);
  1763. err_release_region:
  1764. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  1765. return ret;
  1766. }
  1767. /* if any allocation fails, caller must also call pcnet32_free_ring */
  1768. static int pcnet32_alloc_ring(struct net_device *dev, const char *name)
  1769. {
  1770. struct pcnet32_private *lp = netdev_priv(dev);
  1771. lp->tx_ring = pci_alloc_consistent(lp->pci_dev,
  1772. sizeof(struct pcnet32_tx_head) *
  1773. lp->tx_ring_size,
  1774. &lp->tx_ring_dma_addr);
  1775. if (lp->tx_ring == NULL) {
  1776. netif_err(lp, drv, dev, "Consistent memory allocation failed\n");
  1777. return -ENOMEM;
  1778. }
  1779. lp->rx_ring = pci_alloc_consistent(lp->pci_dev,
  1780. sizeof(struct pcnet32_rx_head) *
  1781. lp->rx_ring_size,
  1782. &lp->rx_ring_dma_addr);
  1783. if (lp->rx_ring == NULL) {
  1784. netif_err(lp, drv, dev, "Consistent memory allocation failed\n");
  1785. return -ENOMEM;
  1786. }
  1787. lp->tx_dma_addr = kcalloc(lp->tx_ring_size, sizeof(dma_addr_t),
  1788. GFP_KERNEL);
  1789. if (!lp->tx_dma_addr)
  1790. return -ENOMEM;
  1791. lp->rx_dma_addr = kcalloc(lp->rx_ring_size, sizeof(dma_addr_t),
  1792. GFP_KERNEL);
  1793. if (!lp->rx_dma_addr)
  1794. return -ENOMEM;
  1795. lp->tx_skbuff = kcalloc(lp->tx_ring_size, sizeof(struct sk_buff *),
  1796. GFP_KERNEL);
  1797. if (!lp->tx_skbuff)
  1798. return -ENOMEM;
  1799. lp->rx_skbuff = kcalloc(lp->rx_ring_size, sizeof(struct sk_buff *),
  1800. GFP_KERNEL);
  1801. if (!lp->rx_skbuff)
  1802. return -ENOMEM;
  1803. return 0;
  1804. }
  1805. static void pcnet32_free_ring(struct net_device *dev)
  1806. {
  1807. struct pcnet32_private *lp = netdev_priv(dev);
  1808. kfree(lp->tx_skbuff);
  1809. lp->tx_skbuff = NULL;
  1810. kfree(lp->rx_skbuff);
  1811. lp->rx_skbuff = NULL;
  1812. kfree(lp->tx_dma_addr);
  1813. lp->tx_dma_addr = NULL;
  1814. kfree(lp->rx_dma_addr);
  1815. lp->rx_dma_addr = NULL;
  1816. if (lp->tx_ring) {
  1817. pci_free_consistent(lp->pci_dev,
  1818. sizeof(struct pcnet32_tx_head) *
  1819. lp->tx_ring_size, lp->tx_ring,
  1820. lp->tx_ring_dma_addr);
  1821. lp->tx_ring = NULL;
  1822. }
  1823. if (lp->rx_ring) {
  1824. pci_free_consistent(lp->pci_dev,
  1825. sizeof(struct pcnet32_rx_head) *
  1826. lp->rx_ring_size, lp->rx_ring,
  1827. lp->rx_ring_dma_addr);
  1828. lp->rx_ring = NULL;
  1829. }
  1830. }
  1831. static int pcnet32_open(struct net_device *dev)
  1832. {
  1833. struct pcnet32_private *lp = netdev_priv(dev);
  1834. struct pci_dev *pdev = lp->pci_dev;
  1835. unsigned long ioaddr = dev->base_addr;
  1836. u16 val;
  1837. int i;
  1838. int rc;
  1839. unsigned long flags;
  1840. if (request_irq(dev->irq, pcnet32_interrupt,
  1841. lp->shared_irq ? IRQF_SHARED : 0, dev->name,
  1842. (void *)dev)) {
  1843. return -EAGAIN;
  1844. }
  1845. spin_lock_irqsave(&lp->lock, flags);
  1846. /* Check for a valid station address */
  1847. if (!is_valid_ether_addr(dev->dev_addr)) {
  1848. rc = -EINVAL;
  1849. goto err_free_irq;
  1850. }
  1851. /* Reset the PCNET32 */
  1852. lp->a->reset(ioaddr);
  1853. /* switch pcnet32 to 32bit mode */
  1854. lp->a->write_bcr(ioaddr, 20, 2);
  1855. netif_printk(lp, ifup, KERN_DEBUG, dev,
  1856. "%s() irq %d tx/rx rings %#x/%#x init %#x\n",
  1857. __func__, dev->irq, (u32) (lp->tx_ring_dma_addr),
  1858. (u32) (lp->rx_ring_dma_addr),
  1859. (u32) (lp->init_dma_addr));
  1860. lp->autoneg = !!(lp->options & PCNET32_PORT_ASEL);
  1861. lp->port_tp = !!(lp->options & PCNET32_PORT_10BT);
  1862. lp->fdx = !!(lp->options & PCNET32_PORT_FD);
  1863. /* set/reset autoselect bit */
  1864. val = lp->a->read_bcr(ioaddr, 2) & ~2;
  1865. if (lp->options & PCNET32_PORT_ASEL)
  1866. val |= 2;
  1867. lp->a->write_bcr(ioaddr, 2, val);
  1868. /* handle full duplex setting */
  1869. if (lp->mii_if.full_duplex) {
  1870. val = lp->a->read_bcr(ioaddr, 9) & ~3;
  1871. if (lp->options & PCNET32_PORT_FD) {
  1872. val |= 1;
  1873. if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
  1874. val |= 2;
  1875. } else if (lp->options & PCNET32_PORT_ASEL) {
  1876. /* workaround of xSeries250, turn on for 79C975 only */
  1877. if (lp->chip_version == 0x2627)
  1878. val |= 3;
  1879. }
  1880. lp->a->write_bcr(ioaddr, 9, val);
  1881. }
  1882. /* set/reset GPSI bit in test register */
  1883. val = lp->a->read_csr(ioaddr, 124) & ~0x10;
  1884. if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
  1885. val |= 0x10;
  1886. lp->a->write_csr(ioaddr, 124, val);
  1887. /* Allied Telesyn AT 2700/2701 FX are 100Mbit only and do not negotiate */
  1888. if (pdev && pdev->subsystem_vendor == PCI_VENDOR_ID_AT &&
  1889. (pdev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
  1890. pdev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
  1891. if (lp->options & PCNET32_PORT_ASEL) {
  1892. lp->options = PCNET32_PORT_FD | PCNET32_PORT_100;
  1893. netif_printk(lp, link, KERN_DEBUG, dev,
  1894. "Setting 100Mb-Full Duplex\n");
  1895. }
  1896. }
  1897. if (lp->phycount < 2) {
  1898. /*
  1899. * 24 Jun 2004 according AMD, in order to change the PHY,
  1900. * DANAS (or DISPM for 79C976) must be set; then select the speed,
  1901. * duplex, and/or enable auto negotiation, and clear DANAS
  1902. */
  1903. if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
  1904. lp->a->write_bcr(ioaddr, 32,
  1905. lp->a->read_bcr(ioaddr, 32) | 0x0080);
  1906. /* disable Auto Negotiation, set 10Mpbs, HD */
  1907. val = lp->a->read_bcr(ioaddr, 32) & ~0xb8;
  1908. if (lp->options & PCNET32_PORT_FD)
  1909. val |= 0x10;
  1910. if (lp->options & PCNET32_PORT_100)
  1911. val |= 0x08;
  1912. lp->a->write_bcr(ioaddr, 32, val);
  1913. } else {
  1914. if (lp->options & PCNET32_PORT_ASEL) {
  1915. lp->a->write_bcr(ioaddr, 32,
  1916. lp->a->read_bcr(ioaddr,
  1917. 32) | 0x0080);
  1918. /* enable auto negotiate, setup, disable fd */
  1919. val = lp->a->read_bcr(ioaddr, 32) & ~0x98;
  1920. val |= 0x20;
  1921. lp->a->write_bcr(ioaddr, 32, val);
  1922. }
  1923. }
  1924. } else {
  1925. int first_phy = -1;
  1926. u16 bmcr;
  1927. u32 bcr9;
  1928. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  1929. /*
  1930. * There is really no good other way to handle multiple PHYs
  1931. * other than turning off all automatics
  1932. */
  1933. val = lp->a->read_bcr(ioaddr, 2);
  1934. lp->a->write_bcr(ioaddr, 2, val & ~2);
  1935. val = lp->a->read_bcr(ioaddr, 32);
  1936. lp->a->write_bcr(ioaddr, 32, val & ~(1 << 7)); /* stop MII manager */
  1937. if (!(lp->options & PCNET32_PORT_ASEL)) {
  1938. /* setup ecmd */
  1939. ecmd.port = PORT_MII;
  1940. ecmd.transceiver = XCVR_INTERNAL;
  1941. ecmd.autoneg = AUTONEG_DISABLE;
  1942. ethtool_cmd_speed_set(&ecmd,
  1943. (lp->options & PCNET32_PORT_100) ?
  1944. SPEED_100 : SPEED_10);
  1945. bcr9 = lp->a->read_bcr(ioaddr, 9);
  1946. if (lp->options & PCNET32_PORT_FD) {
  1947. ecmd.duplex = DUPLEX_FULL;
  1948. bcr9 |= (1 << 0);
  1949. } else {
  1950. ecmd.duplex = DUPLEX_HALF;
  1951. bcr9 |= ~(1 << 0);
  1952. }
  1953. lp->a->write_bcr(ioaddr, 9, bcr9);
  1954. }
  1955. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  1956. if (lp->phymask & (1 << i)) {
  1957. /* isolate all but the first PHY */
  1958. bmcr = mdio_read(dev, i, MII_BMCR);
  1959. if (first_phy == -1) {
  1960. first_phy = i;
  1961. mdio_write(dev, i, MII_BMCR,
  1962. bmcr & ~BMCR_ISOLATE);
  1963. } else {
  1964. mdio_write(dev, i, MII_BMCR,
  1965. bmcr | BMCR_ISOLATE);
  1966. }
  1967. /* use mii_ethtool_sset to setup PHY */
  1968. lp->mii_if.phy_id = i;
  1969. ecmd.phy_address = i;
  1970. if (lp->options & PCNET32_PORT_ASEL) {
  1971. mii_ethtool_gset(&lp->mii_if, &ecmd);
  1972. ecmd.autoneg = AUTONEG_ENABLE;
  1973. }
  1974. mii_ethtool_sset(&lp->mii_if, &ecmd);
  1975. }
  1976. }
  1977. lp->mii_if.phy_id = first_phy;
  1978. netif_info(lp, link, dev, "Using PHY number %d\n", first_phy);
  1979. }
  1980. #ifdef DO_DXSUFLO
  1981. if (lp->dxsuflo) { /* Disable transmit stop on underflow */
  1982. val = lp->a->read_csr(ioaddr, CSR3);
  1983. val |= 0x40;
  1984. lp->a->write_csr(ioaddr, CSR3, val);
  1985. }
  1986. #endif
  1987. lp->init_block->mode =
  1988. cpu_to_le16((lp->options & PCNET32_PORT_PORTSEL) << 7);
  1989. pcnet32_load_multicast(dev);
  1990. if (pcnet32_init_ring(dev)) {
  1991. rc = -ENOMEM;
  1992. goto err_free_ring;
  1993. }
  1994. napi_enable(&lp->napi);
  1995. /* Re-initialize the PCNET32, and start it when done. */
  1996. lp->a->write_csr(ioaddr, 1, (lp->init_dma_addr & 0xffff));
  1997. lp->a->write_csr(ioaddr, 2, (lp->init_dma_addr >> 16));
  1998. lp->a->write_csr(ioaddr, CSR4, 0x0915); /* auto tx pad */
  1999. lp->a->write_csr(ioaddr, CSR0, CSR0_INIT);
  2000. netif_start_queue(dev);
  2001. if (lp->chip_version >= PCNET32_79C970A) {
  2002. /* Print the link status and start the watchdog */
  2003. pcnet32_check_media(dev, 1);
  2004. mod_timer(&lp->watchdog_timer, PCNET32_WATCHDOG_TIMEOUT);
  2005. }
  2006. i = 0;
  2007. while (i++ < 100)
  2008. if (lp->a->read_csr(ioaddr, CSR0) & CSR0_IDON)
  2009. break;
  2010. /*
  2011. * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
  2012. * reports that doing so triggers a bug in the '974.
  2013. */
  2014. lp->a->write_csr(ioaddr, CSR0, CSR0_NORMAL);
  2015. netif_printk(lp, ifup, KERN_DEBUG, dev,
  2016. "pcnet32 open after %d ticks, init block %#x csr0 %4.4x\n",
  2017. i,
  2018. (u32) (lp->init_dma_addr),
  2019. lp->a->read_csr(ioaddr, CSR0));
  2020. spin_unlock_irqrestore(&lp->lock, flags);
  2021. return 0; /* Always succeed */
  2022. err_free_ring:
  2023. /* free any allocated skbuffs */
  2024. pcnet32_purge_rx_ring(dev);
  2025. /*
  2026. * Switch back to 16bit mode to avoid problems with dumb
  2027. * DOS packet driver after a warm reboot
  2028. */
  2029. lp->a->write_bcr(ioaddr, 20, 4);
  2030. err_free_irq:
  2031. spin_unlock_irqrestore(&lp->lock, flags);
  2032. free_irq(dev->irq, dev);
  2033. return rc;
  2034. }
  2035. /*
  2036. * The LANCE has been halted for one reason or another (busmaster memory
  2037. * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
  2038. * etc.). Modern LANCE variants always reload their ring-buffer
  2039. * configuration when restarted, so we must reinitialize our ring
  2040. * context before restarting. As part of this reinitialization,
  2041. * find all packets still on the Tx ring and pretend that they had been
  2042. * sent (in effect, drop the packets on the floor) - the higher-level
  2043. * protocols will time out and retransmit. It'd be better to shuffle
  2044. * these skbs to a temp list and then actually re-Tx them after
  2045. * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
  2046. */
  2047. static void pcnet32_purge_tx_ring(struct net_device *dev)
  2048. {
  2049. struct pcnet32_private *lp = netdev_priv(dev);
  2050. int i;
  2051. for (i = 0; i < lp->tx_ring_size; i++) {
  2052. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  2053. wmb(); /* Make sure adapter sees owner change */
  2054. if (lp->tx_skbuff[i]) {
  2055. if (!pci_dma_mapping_error(lp->pci_dev,
  2056. lp->tx_dma_addr[i]))
  2057. pci_unmap_single(lp->pci_dev,
  2058. lp->tx_dma_addr[i],
  2059. lp->tx_skbuff[i]->len,
  2060. PCI_DMA_TODEVICE);
  2061. dev_kfree_skb_any(lp->tx_skbuff[i]);
  2062. }
  2063. lp->tx_skbuff[i] = NULL;
  2064. lp->tx_dma_addr[i] = 0;
  2065. }
  2066. }
  2067. /* Initialize the PCNET32 Rx and Tx rings. */
  2068. static int pcnet32_init_ring(struct net_device *dev)
  2069. {
  2070. struct pcnet32_private *lp = netdev_priv(dev);
  2071. int i;
  2072. lp->tx_full = 0;
  2073. lp->cur_rx = lp->cur_tx = 0;
  2074. lp->dirty_rx = lp->dirty_tx = 0;
  2075. for (i = 0; i < lp->rx_ring_size; i++) {
  2076. struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
  2077. if (rx_skbuff == NULL) {
  2078. lp->rx_skbuff[i] = netdev_alloc_skb(dev, PKT_BUF_SKB);
  2079. rx_skbuff = lp->rx_skbuff[i];
  2080. if (!rx_skbuff) {
  2081. /* there is not much we can do at this point */
  2082. netif_err(lp, drv, dev, "%s netdev_alloc_skb failed\n",
  2083. __func__);
  2084. return -1;
  2085. }
  2086. skb_reserve(rx_skbuff, NET_IP_ALIGN);
  2087. }
  2088. rmb();
  2089. if (lp->rx_dma_addr[i] == 0) {
  2090. lp->rx_dma_addr[i] =
  2091. pci_map_single(lp->pci_dev, rx_skbuff->data,
  2092. PKT_BUF_SIZE, PCI_DMA_FROMDEVICE);
  2093. if (pci_dma_mapping_error(lp->pci_dev,
  2094. lp->rx_dma_addr[i])) {
  2095. /* there is not much we can do at this point */
  2096. netif_err(lp, drv, dev,
  2097. "%s pci dma mapping error\n",
  2098. __func__);
  2099. return -1;
  2100. }
  2101. }
  2102. lp->rx_ring[i].base = cpu_to_le32(lp->rx_dma_addr[i]);
  2103. lp->rx_ring[i].buf_length = cpu_to_le16(NEG_BUF_SIZE);
  2104. wmb(); /* Make sure owner changes after all others are visible */
  2105. lp->rx_ring[i].status = cpu_to_le16(0x8000);
  2106. }
  2107. /* The Tx buffer address is filled in as needed, but we do need to clear
  2108. * the upper ownership bit. */
  2109. for (i = 0; i < lp->tx_ring_size; i++) {
  2110. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  2111. wmb(); /* Make sure adapter sees owner change */
  2112. lp->tx_ring[i].base = 0;
  2113. lp->tx_dma_addr[i] = 0;
  2114. }
  2115. lp->init_block->tlen_rlen =
  2116. cpu_to_le16(lp->tx_len_bits | lp->rx_len_bits);
  2117. for (i = 0; i < 6; i++)
  2118. lp->init_block->phys_addr[i] = dev->dev_addr[i];
  2119. lp->init_block->rx_ring = cpu_to_le32(lp->rx_ring_dma_addr);
  2120. lp->init_block->tx_ring = cpu_to_le32(lp->tx_ring_dma_addr);
  2121. wmb(); /* Make sure all changes are visible */
  2122. return 0;
  2123. }
  2124. /* the pcnet32 has been issued a stop or reset. Wait for the stop bit
  2125. * then flush the pending transmit operations, re-initialize the ring,
  2126. * and tell the chip to initialize.
  2127. */
  2128. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
  2129. {
  2130. struct pcnet32_private *lp = netdev_priv(dev);
  2131. unsigned long ioaddr = dev->base_addr;
  2132. int i;
  2133. /* wait for stop */
  2134. for (i = 0; i < 100; i++)
  2135. if (lp->a->read_csr(ioaddr, CSR0) & CSR0_STOP)
  2136. break;
  2137. if (i >= 100)
  2138. netif_err(lp, drv, dev, "%s timed out waiting for stop\n",
  2139. __func__);
  2140. pcnet32_purge_tx_ring(dev);
  2141. if (pcnet32_init_ring(dev))
  2142. return;
  2143. /* ReInit Ring */
  2144. lp->a->write_csr(ioaddr, CSR0, CSR0_INIT);
  2145. i = 0;
  2146. while (i++ < 1000)
  2147. if (lp->a->read_csr(ioaddr, CSR0) & CSR0_IDON)
  2148. break;
  2149. lp->a->write_csr(ioaddr, CSR0, csr0_bits);
  2150. }
  2151. static void pcnet32_tx_timeout(struct net_device *dev)
  2152. {
  2153. struct pcnet32_private *lp = netdev_priv(dev);
  2154. unsigned long ioaddr = dev->base_addr, flags;
  2155. spin_lock_irqsave(&lp->lock, flags);
  2156. /* Transmitter timeout, serious problems. */
  2157. if (pcnet32_debug & NETIF_MSG_DRV)
  2158. pr_err("%s: transmit timed out, status %4.4x, resetting\n",
  2159. dev->name, lp->a->read_csr(ioaddr, CSR0));
  2160. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP);
  2161. dev->stats.tx_errors++;
  2162. if (netif_msg_tx_err(lp)) {
  2163. int i;
  2164. printk(KERN_DEBUG
  2165. " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
  2166. lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
  2167. lp->cur_rx);
  2168. for (i = 0; i < lp->rx_ring_size; i++)
  2169. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  2170. le32_to_cpu(lp->rx_ring[i].base),
  2171. (-le16_to_cpu(lp->rx_ring[i].buf_length)) &
  2172. 0xffff, le32_to_cpu(lp->rx_ring[i].msg_length),
  2173. le16_to_cpu(lp->rx_ring[i].status));
  2174. for (i = 0; i < lp->tx_ring_size; i++)
  2175. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  2176. le32_to_cpu(lp->tx_ring[i].base),
  2177. (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
  2178. le32_to_cpu(lp->tx_ring[i].misc),
  2179. le16_to_cpu(lp->tx_ring[i].status));
  2180. printk("\n");
  2181. }
  2182. pcnet32_restart(dev, CSR0_NORMAL);
  2183. netif_trans_update(dev); /* prevent tx timeout */
  2184. netif_wake_queue(dev);
  2185. spin_unlock_irqrestore(&lp->lock, flags);
  2186. }
  2187. static netdev_tx_t pcnet32_start_xmit(struct sk_buff *skb,
  2188. struct net_device *dev)
  2189. {
  2190. struct pcnet32_private *lp = netdev_priv(dev);
  2191. unsigned long ioaddr = dev->base_addr;
  2192. u16 status;
  2193. int entry;
  2194. unsigned long flags;
  2195. spin_lock_irqsave(&lp->lock, flags);
  2196. netif_printk(lp, tx_queued, KERN_DEBUG, dev,
  2197. "%s() called, csr0 %4.4x\n",
  2198. __func__, lp->a->read_csr(ioaddr, CSR0));
  2199. /* Default status -- will not enable Successful-TxDone
  2200. * interrupt when that option is available to us.
  2201. */
  2202. status = 0x8300;
  2203. /* Fill in a Tx ring entry */
  2204. /* Mask to ring buffer boundary. */
  2205. entry = lp->cur_tx & lp->tx_mod_mask;
  2206. /* Caution: the write order is important here, set the status
  2207. * with the "ownership" bits last. */
  2208. lp->tx_ring[entry].length = cpu_to_le16(-skb->len);
  2209. lp->tx_ring[entry].misc = 0x00000000;
  2210. lp->tx_dma_addr[entry] =
  2211. pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
  2212. if (pci_dma_mapping_error(lp->pci_dev, lp->tx_dma_addr[entry])) {
  2213. dev_kfree_skb_any(skb);
  2214. dev->stats.tx_dropped++;
  2215. goto drop_packet;
  2216. }
  2217. lp->tx_skbuff[entry] = skb;
  2218. lp->tx_ring[entry].base = cpu_to_le32(lp->tx_dma_addr[entry]);
  2219. wmb(); /* Make sure owner changes after all others are visible */
  2220. lp->tx_ring[entry].status = cpu_to_le16(status);
  2221. lp->cur_tx++;
  2222. dev->stats.tx_bytes += skb->len;
  2223. /* Trigger an immediate send poll. */
  2224. lp->a->write_csr(ioaddr, CSR0, CSR0_INTEN | CSR0_TXPOLL);
  2225. if (lp->tx_ring[(entry + 1) & lp->tx_mod_mask].base != 0) {
  2226. lp->tx_full = 1;
  2227. netif_stop_queue(dev);
  2228. }
  2229. drop_packet:
  2230. spin_unlock_irqrestore(&lp->lock, flags);
  2231. return NETDEV_TX_OK;
  2232. }
  2233. /* The PCNET32 interrupt handler. */
  2234. static irqreturn_t
  2235. pcnet32_interrupt(int irq, void *dev_id)
  2236. {
  2237. struct net_device *dev = dev_id;
  2238. struct pcnet32_private *lp;
  2239. unsigned long ioaddr;
  2240. u16 csr0;
  2241. int boguscnt = max_interrupt_work;
  2242. ioaddr = dev->base_addr;
  2243. lp = netdev_priv(dev);
  2244. spin_lock(&lp->lock);
  2245. csr0 = lp->a->read_csr(ioaddr, CSR0);
  2246. while ((csr0 & 0x8f00) && --boguscnt >= 0) {
  2247. if (csr0 == 0xffff)
  2248. break; /* PCMCIA remove happened */
  2249. /* Acknowledge all of the current interrupt sources ASAP. */
  2250. lp->a->write_csr(ioaddr, CSR0, csr0 & ~0x004f);
  2251. netif_printk(lp, intr, KERN_DEBUG, dev,
  2252. "interrupt csr0=%#2.2x new csr=%#2.2x\n",
  2253. csr0, lp->a->read_csr(ioaddr, CSR0));
  2254. /* Log misc errors. */
  2255. if (csr0 & 0x4000)
  2256. dev->stats.tx_errors++; /* Tx babble. */
  2257. if (csr0 & 0x1000) {
  2258. /*
  2259. * This happens when our receive ring is full. This
  2260. * shouldn't be a problem as we will see normal rx
  2261. * interrupts for the frames in the receive ring. But
  2262. * there are some PCI chipsets (I can reproduce this
  2263. * on SP3G with Intel saturn chipset) which have
  2264. * sometimes problems and will fill up the receive
  2265. * ring with error descriptors. In this situation we
  2266. * don't get a rx interrupt, but a missed frame
  2267. * interrupt sooner or later.
  2268. */
  2269. dev->stats.rx_errors++; /* Missed a Rx frame. */
  2270. }
  2271. if (csr0 & 0x0800) {
  2272. netif_err(lp, drv, dev, "Bus master arbitration failure, status %4.4x\n",
  2273. csr0);
  2274. /* unlike for the lance, there is no restart needed */
  2275. }
  2276. if (napi_schedule_prep(&lp->napi)) {
  2277. u16 val;
  2278. /* set interrupt masks */
  2279. val = lp->a->read_csr(ioaddr, CSR3);
  2280. val |= 0x5f00;
  2281. lp->a->write_csr(ioaddr, CSR3, val);
  2282. __napi_schedule(&lp->napi);
  2283. break;
  2284. }
  2285. csr0 = lp->a->read_csr(ioaddr, CSR0);
  2286. }
  2287. netif_printk(lp, intr, KERN_DEBUG, dev,
  2288. "exiting interrupt, csr0=%#4.4x\n",
  2289. lp->a->read_csr(ioaddr, CSR0));
  2290. spin_unlock(&lp->lock);
  2291. return IRQ_HANDLED;
  2292. }
  2293. static int pcnet32_close(struct net_device *dev)
  2294. {
  2295. unsigned long ioaddr = dev->base_addr;
  2296. struct pcnet32_private *lp = netdev_priv(dev);
  2297. unsigned long flags;
  2298. del_timer_sync(&lp->watchdog_timer);
  2299. netif_stop_queue(dev);
  2300. napi_disable(&lp->napi);
  2301. spin_lock_irqsave(&lp->lock, flags);
  2302. dev->stats.rx_missed_errors = lp->a->read_csr(ioaddr, 112);
  2303. netif_printk(lp, ifdown, KERN_DEBUG, dev,
  2304. "Shutting down ethercard, status was %2.2x\n",
  2305. lp->a->read_csr(ioaddr, CSR0));
  2306. /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
  2307. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP);
  2308. /*
  2309. * Switch back to 16bit mode to avoid problems with dumb
  2310. * DOS packet driver after a warm reboot
  2311. */
  2312. lp->a->write_bcr(ioaddr, 20, 4);
  2313. spin_unlock_irqrestore(&lp->lock, flags);
  2314. free_irq(dev->irq, dev);
  2315. spin_lock_irqsave(&lp->lock, flags);
  2316. pcnet32_purge_rx_ring(dev);
  2317. pcnet32_purge_tx_ring(dev);
  2318. spin_unlock_irqrestore(&lp->lock, flags);
  2319. return 0;
  2320. }
  2321. static struct net_device_stats *pcnet32_get_stats(struct net_device *dev)
  2322. {
  2323. struct pcnet32_private *lp = netdev_priv(dev);
  2324. unsigned long ioaddr = dev->base_addr;
  2325. unsigned long flags;
  2326. spin_lock_irqsave(&lp->lock, flags);
  2327. dev->stats.rx_missed_errors = lp->a->read_csr(ioaddr, 112);
  2328. spin_unlock_irqrestore(&lp->lock, flags);
  2329. return &dev->stats;
  2330. }
  2331. /* taken from the sunlance driver, which it took from the depca driver */
  2332. static void pcnet32_load_multicast(struct net_device *dev)
  2333. {
  2334. struct pcnet32_private *lp = netdev_priv(dev);
  2335. volatile struct pcnet32_init_block *ib = lp->init_block;
  2336. volatile __le16 *mcast_table = (__le16 *)ib->filter;
  2337. struct netdev_hw_addr *ha;
  2338. unsigned long ioaddr = dev->base_addr;
  2339. int i;
  2340. u32 crc;
  2341. /* set all multicast bits */
  2342. if (dev->flags & IFF_ALLMULTI) {
  2343. ib->filter[0] = cpu_to_le32(~0U);
  2344. ib->filter[1] = cpu_to_le32(~0U);
  2345. lp->a->write_csr(ioaddr, PCNET32_MC_FILTER, 0xffff);
  2346. lp->a->write_csr(ioaddr, PCNET32_MC_FILTER+1, 0xffff);
  2347. lp->a->write_csr(ioaddr, PCNET32_MC_FILTER+2, 0xffff);
  2348. lp->a->write_csr(ioaddr, PCNET32_MC_FILTER+3, 0xffff);
  2349. return;
  2350. }
  2351. /* clear the multicast filter */
  2352. ib->filter[0] = 0;
  2353. ib->filter[1] = 0;
  2354. /* Add addresses */
  2355. netdev_for_each_mc_addr(ha, dev) {
  2356. crc = ether_crc_le(6, ha->addr);
  2357. crc = crc >> 26;
  2358. mcast_table[crc >> 4] |= cpu_to_le16(1 << (crc & 0xf));
  2359. }
  2360. for (i = 0; i < 4; i++)
  2361. lp->a->write_csr(ioaddr, PCNET32_MC_FILTER + i,
  2362. le16_to_cpu(mcast_table[i]));
  2363. }
  2364. /*
  2365. * Set or clear the multicast filter for this adaptor.
  2366. */
  2367. static void pcnet32_set_multicast_list(struct net_device *dev)
  2368. {
  2369. unsigned long ioaddr = dev->base_addr, flags;
  2370. struct pcnet32_private *lp = netdev_priv(dev);
  2371. int csr15, suspended;
  2372. spin_lock_irqsave(&lp->lock, flags);
  2373. suspended = pcnet32_suspend(dev, &flags, 0);
  2374. csr15 = lp->a->read_csr(ioaddr, CSR15);
  2375. if (dev->flags & IFF_PROMISC) {
  2376. /* Log any net taps. */
  2377. netif_info(lp, hw, dev, "Promiscuous mode enabled\n");
  2378. lp->init_block->mode =
  2379. cpu_to_le16(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) <<
  2380. 7);
  2381. lp->a->write_csr(ioaddr, CSR15, csr15 | 0x8000);
  2382. } else {
  2383. lp->init_block->mode =
  2384. cpu_to_le16((lp->options & PCNET32_PORT_PORTSEL) << 7);
  2385. lp->a->write_csr(ioaddr, CSR15, csr15 & 0x7fff);
  2386. pcnet32_load_multicast(dev);
  2387. }
  2388. if (suspended) {
  2389. pcnet32_clr_suspend(lp, ioaddr);
  2390. } else {
  2391. lp->a->write_csr(ioaddr, CSR0, CSR0_STOP);
  2392. pcnet32_restart(dev, CSR0_NORMAL);
  2393. netif_wake_queue(dev);
  2394. }
  2395. spin_unlock_irqrestore(&lp->lock, flags);
  2396. }
  2397. /* This routine assumes that the lp->lock is held */
  2398. static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
  2399. {
  2400. struct pcnet32_private *lp = netdev_priv(dev);
  2401. unsigned long ioaddr = dev->base_addr;
  2402. u16 val_out;
  2403. if (!lp->mii)
  2404. return 0;
  2405. lp->a->write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2406. val_out = lp->a->read_bcr(ioaddr, 34);
  2407. return val_out;
  2408. }
  2409. /* This routine assumes that the lp->lock is held */
  2410. static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
  2411. {
  2412. struct pcnet32_private *lp = netdev_priv(dev);
  2413. unsigned long ioaddr = dev->base_addr;
  2414. if (!lp->mii)
  2415. return;
  2416. lp->a->write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2417. lp->a->write_bcr(ioaddr, 34, val);
  2418. }
  2419. static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2420. {
  2421. struct pcnet32_private *lp = netdev_priv(dev);
  2422. int rc;
  2423. unsigned long flags;
  2424. /* SIOC[GS]MIIxxx ioctls */
  2425. if (lp->mii) {
  2426. spin_lock_irqsave(&lp->lock, flags);
  2427. rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
  2428. spin_unlock_irqrestore(&lp->lock, flags);
  2429. } else {
  2430. rc = -EOPNOTSUPP;
  2431. }
  2432. return rc;
  2433. }
  2434. static int pcnet32_check_otherphy(struct net_device *dev)
  2435. {
  2436. struct pcnet32_private *lp = netdev_priv(dev);
  2437. struct mii_if_info mii = lp->mii_if;
  2438. u16 bmcr;
  2439. int i;
  2440. for (i = 0; i < PCNET32_MAX_PHYS; i++) {
  2441. if (i == lp->mii_if.phy_id)
  2442. continue; /* skip active phy */
  2443. if (lp->phymask & (1 << i)) {
  2444. mii.phy_id = i;
  2445. if (mii_link_ok(&mii)) {
  2446. /* found PHY with active link */
  2447. netif_info(lp, link, dev, "Using PHY number %d\n",
  2448. i);
  2449. /* isolate inactive phy */
  2450. bmcr =
  2451. mdio_read(dev, lp->mii_if.phy_id, MII_BMCR);
  2452. mdio_write(dev, lp->mii_if.phy_id, MII_BMCR,
  2453. bmcr | BMCR_ISOLATE);
  2454. /* de-isolate new phy */
  2455. bmcr = mdio_read(dev, i, MII_BMCR);
  2456. mdio_write(dev, i, MII_BMCR,
  2457. bmcr & ~BMCR_ISOLATE);
  2458. /* set new phy address */
  2459. lp->mii_if.phy_id = i;
  2460. return 1;
  2461. }
  2462. }
  2463. }
  2464. return 0;
  2465. }
  2466. /*
  2467. * Show the status of the media. Similar to mii_check_media however it
  2468. * correctly shows the link speed for all (tested) pcnet32 variants.
  2469. * Devices with no mii just report link state without speed.
  2470. *
  2471. * Caller is assumed to hold and release the lp->lock.
  2472. */
  2473. static void pcnet32_check_media(struct net_device *dev, int verbose)
  2474. {
  2475. struct pcnet32_private *lp = netdev_priv(dev);
  2476. int curr_link;
  2477. int prev_link = netif_carrier_ok(dev) ? 1 : 0;
  2478. u32 bcr9;
  2479. if (lp->mii) {
  2480. curr_link = mii_link_ok(&lp->mii_if);
  2481. } else if (lp->chip_version == PCNET32_79C970A) {
  2482. ulong ioaddr = dev->base_addr; /* card base I/O address */
  2483. /* only read link if port is set to TP */
  2484. if (!lp->autoneg && lp->port_tp)
  2485. curr_link = (lp->a->read_bcr(ioaddr, 4) != 0xc0);
  2486. else /* link always up for AUI port or port auto select */
  2487. curr_link = 1;
  2488. } else {
  2489. ulong ioaddr = dev->base_addr; /* card base I/O address */
  2490. curr_link = (lp->a->read_bcr(ioaddr, 4) != 0xc0);
  2491. }
  2492. if (!curr_link) {
  2493. if (prev_link || verbose) {
  2494. netif_carrier_off(dev);
  2495. netif_info(lp, link, dev, "link down\n");
  2496. }
  2497. if (lp->phycount > 1) {
  2498. curr_link = pcnet32_check_otherphy(dev);
  2499. prev_link = 0;
  2500. }
  2501. } else if (verbose || !prev_link) {
  2502. netif_carrier_on(dev);
  2503. if (lp->mii) {
  2504. if (netif_msg_link(lp)) {
  2505. struct ethtool_cmd ecmd = {
  2506. .cmd = ETHTOOL_GSET };
  2507. mii_ethtool_gset(&lp->mii_if, &ecmd);
  2508. netdev_info(dev, "link up, %uMbps, %s-duplex\n",
  2509. ethtool_cmd_speed(&ecmd),
  2510. (ecmd.duplex == DUPLEX_FULL)
  2511. ? "full" : "half");
  2512. }
  2513. bcr9 = lp->a->read_bcr(dev->base_addr, 9);
  2514. if ((bcr9 & (1 << 0)) != lp->mii_if.full_duplex) {
  2515. if (lp->mii_if.full_duplex)
  2516. bcr9 |= (1 << 0);
  2517. else
  2518. bcr9 &= ~(1 << 0);
  2519. lp->a->write_bcr(dev->base_addr, 9, bcr9);
  2520. }
  2521. } else {
  2522. netif_info(lp, link, dev, "link up\n");
  2523. }
  2524. }
  2525. }
  2526. /*
  2527. * Check for loss of link and link establishment.
  2528. * Could possibly be changed to use mii_check_media instead.
  2529. */
  2530. static void pcnet32_watchdog(struct timer_list *t)
  2531. {
  2532. struct pcnet32_private *lp = from_timer(lp, t, watchdog_timer);
  2533. struct net_device *dev = lp->dev;
  2534. unsigned long flags;
  2535. /* Print the link status if it has changed */
  2536. spin_lock_irqsave(&lp->lock, flags);
  2537. pcnet32_check_media(dev, 0);
  2538. spin_unlock_irqrestore(&lp->lock, flags);
  2539. mod_timer(&lp->watchdog_timer, round_jiffies(PCNET32_WATCHDOG_TIMEOUT));
  2540. }
  2541. static int pcnet32_pm_suspend(struct pci_dev *pdev, pm_message_t state)
  2542. {
  2543. struct net_device *dev = pci_get_drvdata(pdev);
  2544. if (netif_running(dev)) {
  2545. netif_device_detach(dev);
  2546. pcnet32_close(dev);
  2547. }
  2548. pci_save_state(pdev);
  2549. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2550. return 0;
  2551. }
  2552. static int pcnet32_pm_resume(struct pci_dev *pdev)
  2553. {
  2554. struct net_device *dev = pci_get_drvdata(pdev);
  2555. pci_set_power_state(pdev, PCI_D0);
  2556. pci_restore_state(pdev);
  2557. if (netif_running(dev)) {
  2558. pcnet32_open(dev);
  2559. netif_device_attach(dev);
  2560. }
  2561. return 0;
  2562. }
  2563. static void pcnet32_remove_one(struct pci_dev *pdev)
  2564. {
  2565. struct net_device *dev = pci_get_drvdata(pdev);
  2566. if (dev) {
  2567. struct pcnet32_private *lp = netdev_priv(dev);
  2568. unregister_netdev(dev);
  2569. pcnet32_free_ring(dev);
  2570. release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
  2571. pci_free_consistent(lp->pci_dev, sizeof(*lp->init_block),
  2572. lp->init_block, lp->init_dma_addr);
  2573. free_netdev(dev);
  2574. pci_disable_device(pdev);
  2575. }
  2576. }
  2577. static struct pci_driver pcnet32_driver = {
  2578. .name = DRV_NAME,
  2579. .probe = pcnet32_probe_pci,
  2580. .remove = pcnet32_remove_one,
  2581. .id_table = pcnet32_pci_tbl,
  2582. .suspend = pcnet32_pm_suspend,
  2583. .resume = pcnet32_pm_resume,
  2584. };
  2585. /* An additional parameter that may be passed in... */
  2586. static int debug = -1;
  2587. static int tx_start_pt = -1;
  2588. static int pcnet32_have_pci;
  2589. module_param(debug, int, 0);
  2590. MODULE_PARM_DESC(debug, DRV_NAME " debug level");
  2591. module_param(max_interrupt_work, int, 0);
  2592. MODULE_PARM_DESC(max_interrupt_work,
  2593. DRV_NAME " maximum events handled per interrupt");
  2594. module_param(rx_copybreak, int, 0);
  2595. MODULE_PARM_DESC(rx_copybreak,
  2596. DRV_NAME " copy breakpoint for copy-only-tiny-frames");
  2597. module_param(tx_start_pt, int, 0);
  2598. MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
  2599. module_param(pcnet32vlb, int, 0);
  2600. MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
  2601. module_param_array(options, int, NULL, 0);
  2602. MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
  2603. module_param_array(full_duplex, int, NULL, 0);
  2604. MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
  2605. /* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
  2606. module_param_array(homepna, int, NULL, 0);
  2607. MODULE_PARM_DESC(homepna,
  2608. DRV_NAME
  2609. " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
  2610. MODULE_AUTHOR("Thomas Bogendoerfer");
  2611. MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
  2612. MODULE_LICENSE("GPL");
  2613. #define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  2614. static int __init pcnet32_init_module(void)
  2615. {
  2616. pr_info("%s", version);
  2617. pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
  2618. if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
  2619. tx_start = tx_start_pt;
  2620. /* find the PCI devices */
  2621. if (!pci_register_driver(&pcnet32_driver))
  2622. pcnet32_have_pci = 1;
  2623. /* should we find any remaining VLbus devices ? */
  2624. if (pcnet32vlb)
  2625. pcnet32_probe_vlbus(pcnet32_portlist);
  2626. if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
  2627. pr_info("%d cards_found\n", cards_found);
  2628. return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
  2629. }
  2630. static void __exit pcnet32_cleanup_module(void)
  2631. {
  2632. struct net_device *next_dev;
  2633. while (pcnet32_dev) {
  2634. struct pcnet32_private *lp = netdev_priv(pcnet32_dev);
  2635. next_dev = lp->next;
  2636. unregister_netdev(pcnet32_dev);
  2637. pcnet32_free_ring(pcnet32_dev);
  2638. release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
  2639. pci_free_consistent(lp->pci_dev, sizeof(*lp->init_block),
  2640. lp->init_block, lp->init_dma_addr);
  2641. free_netdev(pcnet32_dev);
  2642. pcnet32_dev = next_dev;
  2643. }
  2644. if (pcnet32_have_pci)
  2645. pci_unregister_driver(&pcnet32_driver);
  2646. }
  2647. module_init(pcnet32_init_module);
  2648. module_exit(pcnet32_cleanup_module);
  2649. /*
  2650. * Local variables:
  2651. * c-indent-level: 4
  2652. * tab-width: 8
  2653. * End:
  2654. */