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