pcnet32.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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.31a"
  25. #define DRV_RELDATE "12.Sep.2005"
  26. #define PFX DRV_NAME ": "
  27. static const char *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_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  57. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  58. /*
  59. * Adapters that were sold with IBM's RS/6000 or pSeries hardware have
  60. * the incorrect vendor id.
  61. */
  62. { PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_AMD_LANCE, PCI_ANY_ID, PCI_ANY_ID,
  63. PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, 0 },
  64. { 0, }
  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 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. PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD, /* 14 MII 100BaseTx-FD */
  110. PCNET32_PORT_ASEL /* 15 not supported */
  111. };
  112. static const char pcnet32_gstrings_test[][ETH_GSTRING_LEN] = {
  113. "Loopback test (offline)"
  114. };
  115. #define PCNET32_TEST_LEN (sizeof(pcnet32_gstrings_test) / ETH_GSTRING_LEN)
  116. #define PCNET32_NUM_REGS 168
  117. #define MAX_UNITS 8 /* More are supported, limit only on options */
  118. static int options[MAX_UNITS];
  119. static int full_duplex[MAX_UNITS];
  120. static int homepna[MAX_UNITS];
  121. /*
  122. * Theory of Operation
  123. *
  124. * This driver uses the same software structure as the normal lance
  125. * driver. So look for a verbose description in lance.c. The differences
  126. * to the normal lance driver is the use of the 32bit mode of PCnet32
  127. * and PCnetPCI chips. Because these chips are 32bit chips, there is no
  128. * 16MB limitation and we don't need bounce buffers.
  129. */
  130. /*
  131. * History:
  132. * v0.01: Initial version
  133. * only tested on Alpha Noname Board
  134. * v0.02: changed IRQ handling for new interrupt scheme (dev_id)
  135. * tested on a ASUS SP3G
  136. * v0.10: fixed an odd problem with the 79C974 in a Compaq Deskpro XL
  137. * looks like the 974 doesn't like stopping and restarting in a
  138. * short period of time; now we do a reinit of the lance; the
  139. * bug was triggered by doing ifconfig eth0 <ip> broadcast <addr>
  140. * and hangs the machine (thanks to Klaus Liedl for debugging)
  141. * v0.12: by suggestion from Donald Becker: Renamed driver to pcnet32,
  142. * made it standalone (no need for lance.c)
  143. * v0.13: added additional PCI detecting for special PCI devices (Compaq)
  144. * v0.14: stripped down additional PCI probe (thanks to David C Niemi
  145. * and sveneric@xs4all.nl for testing this on their Compaq boxes)
  146. * v0.15: added 79C965 (VLB) probe
  147. * added interrupt sharing for PCI chips
  148. * v0.16: fixed set_multicast_list on Alpha machines
  149. * v0.17: removed hack from dev.c; now pcnet32 uses ethif_probe in Space.c
  150. * v0.19: changed setting of autoselect bit
  151. * v0.20: removed additional Compaq PCI probe; there is now a working one
  152. * in arch/i386/bios32.c
  153. * v0.21: added endian conversion for ppc, from work by cort@cs.nmt.edu
  154. * v0.22: added printing of status to ring dump
  155. * v0.23: changed enet_statistics to net_devive_stats
  156. * v0.90: added multicast filter
  157. * added module support
  158. * changed irq probe to new style
  159. * added PCnetFast chip id
  160. * added fix for receive stalls with Intel saturn chipsets
  161. * added in-place rx skbs like in the tulip driver
  162. * minor cleanups
  163. * v0.91: added PCnetFast+ chip id
  164. * back port to 2.0.x
  165. * v1.00: added some stuff from Donald Becker's 2.0.34 version
  166. * added support for byte counters in net_dev_stats
  167. * v1.01: do ring dumps, only when debugging the driver
  168. * increased the transmit timeout
  169. * v1.02: fixed memory leak in pcnet32_init_ring()
  170. * v1.10: workaround for stopped transmitter
  171. * added port selection for modules
  172. * detect special T1/E1 WAN card and setup port selection
  173. * v1.11: fixed wrong checking of Tx errors
  174. * v1.20: added check of return value kmalloc (cpeterso@cs.washington.edu)
  175. * added save original kmalloc addr for freeing (mcr@solidum.com)
  176. * added support for PCnetHome chip (joe@MIT.EDU)
  177. * rewritten PCI card detection
  178. * added dwio mode to get driver working on some PPC machines
  179. * v1.21: added mii selection and mii ioctl
  180. * v1.22: changed pci scanning code to make PPC people happy
  181. * fixed switching to 32bit mode in pcnet32_open() (thanks
  182. * to Michael Richard <mcr@solidum.com> for noticing this one)
  183. * added sub vendor/device id matching (thanks again to
  184. * Michael Richard <mcr@solidum.com>)
  185. * added chip id for 79c973/975 (thanks to Zach Brown <zab@zabbo.net>)
  186. * v1.23 fixed small bug, when manual selecting MII speed/duplex
  187. * v1.24 Applied Thomas' patch to use TxStartPoint and thus decrease TxFIFO
  188. * underflows. Added tx_start_pt module parameter. Increased
  189. * TX_RING_SIZE from 16 to 32. Added #ifdef'd code to use DXSUFLO
  190. * for FAST[+] chipsets. <kaf@fc.hp.com>
  191. * v1.24ac Added SMP spinlocking - Alan Cox <alan@redhat.com>
  192. * v1.25kf Added No Interrupt on successful Tx for some Tx's <kaf@fc.hp.com>
  193. * v1.26 Converted to pci_alloc_consistent, Jamey Hicks / George France
  194. * <jamey@crl.dec.com>
  195. * - Fixed a few bugs, related to running the controller in 32bit mode.
  196. * 23 Oct, 2000. Carsten Langgaard, carstenl@mips.com
  197. * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
  198. * v1.26p Fix oops on rmmod+insmod; plug i/o resource leak - Paul Gortmaker
  199. * v1.27 improved CSR/PROM address detection, lots of cleanups,
  200. * new pcnet32vlb module option, HP-PARISC support,
  201. * added module parameter descriptions,
  202. * initial ethtool support - Helge Deller <deller@gmx.de>
  203. * v1.27a Sun Feb 10 2002 Go Taniguchi <go@turbolinux.co.jp>
  204. * use alloc_etherdev and register_netdev
  205. * fix pci probe not increment cards_found
  206. * FD auto negotiate error workaround for xSeries250
  207. * clean up and using new mii module
  208. * v1.27b Sep 30 2002 Kent Yoder <yoder1@us.ibm.com>
  209. * Added timer for cable connection state changes.
  210. * v1.28 20 Feb 2004 Don Fry <brazilnut@us.ibm.com>
  211. * Jon Mason <jonmason@us.ibm.com>, Chinmay Albal <albal@in.ibm.com>
  212. * Now uses ethtool_ops, netif_msg_* and generic_mii_ioctl.
  213. * Fixes bogus 'Bus master arbitration failure', pci_[un]map_single
  214. * length errors, and transmit hangs. Cleans up after errors in open.
  215. * Jim Lewis <jklewis@us.ibm.com> added ethernet loopback test.
  216. * Thomas Munck Steenholdt <tmus@tmus.dk> non-mii ioctl corrections.
  217. * v1.29 6 Apr 2004 Jim Lewis <jklewis@us.ibm.com> added physical
  218. * identification code (blink led's) and register dump.
  219. * Don Fry added timer for 971/972 so skbufs don't remain on tx ring
  220. * forever.
  221. * v1.30 18 May 2004 Don Fry removed timer and Last Transmit Interrupt
  222. * (ltint) as they added complexity and didn't give good throughput.
  223. * v1.30a 22 May 2004 Don Fry limit frames received during interrupt.
  224. * v1.30b 24 May 2004 Don Fry fix bogus tx carrier errors with 79c973,
  225. * assisted by Bruce Penrod <bmpenrod@endruntechnologies.com>.
  226. * v1.30c 25 May 2004 Don Fry added netif_wake_queue after pcnet32_restart.
  227. * v1.30d 01 Jun 2004 Don Fry discard oversize rx packets.
  228. * v1.30e 11 Jun 2004 Don Fry recover after fifo error and rx hang.
  229. * v1.30f 16 Jun 2004 Don Fry cleanup IRQ to allow 0 and 1 for PCI,
  230. * expanding on suggestions from Ralf Baechle <ralf@linux-mips.org>,
  231. * and Brian Murphy <brian@murphy.dk>.
  232. * v1.30g 22 Jun 2004 Patrick Simmons <psimmons@flash.net> added option
  233. * homepna for selecting HomePNA mode for PCNet/Home 79C978.
  234. * v1.30h 24 Jun 2004 Don Fry correctly select auto, speed, duplex in bcr32.
  235. * v1.30i 28 Jun 2004 Don Fry change to use module_param.
  236. * v1.30j 29 Apr 2005 Don Fry fix skb/map leak with loopback test.
  237. * v1.31 02 Sep 2005 Hubert WS Lin <wslin@tw.ibm.c0m> added set_ringparam().
  238. * v1.31a 12 Sep 2005 Hubert WS Lin <wslin@tw.ibm.c0m> set min ring size to 4
  239. * to allow loopback test to work unchanged.
  240. */
  241. /*
  242. * Set the number of Tx and Rx buffers, using Log_2(# buffers).
  243. * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
  244. * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
  245. */
  246. #ifndef PCNET32_LOG_TX_BUFFERS
  247. #define PCNET32_LOG_TX_BUFFERS 4
  248. #define PCNET32_LOG_RX_BUFFERS 5
  249. #define PCNET32_LOG_MAX_TX_BUFFERS 9 /* 2^9 == 512 */
  250. #define PCNET32_LOG_MAX_RX_BUFFERS 9
  251. #endif
  252. #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
  253. #define TX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_TX_BUFFERS))
  254. #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
  255. #define RX_MAX_RING_SIZE (1 << (PCNET32_LOG_MAX_RX_BUFFERS))
  256. #define PKT_BUF_SZ 1544
  257. /* Offsets from base I/O address. */
  258. #define PCNET32_WIO_RDP 0x10
  259. #define PCNET32_WIO_RAP 0x12
  260. #define PCNET32_WIO_RESET 0x14
  261. #define PCNET32_WIO_BDP 0x16
  262. #define PCNET32_DWIO_RDP 0x10
  263. #define PCNET32_DWIO_RAP 0x14
  264. #define PCNET32_DWIO_RESET 0x18
  265. #define PCNET32_DWIO_BDP 0x1C
  266. #define PCNET32_TOTAL_SIZE 0x20
  267. /* The PCNET32 Rx and Tx ring descriptors. */
  268. struct pcnet32_rx_head {
  269. u32 base;
  270. s16 buf_length;
  271. s16 status;
  272. u32 msg_length;
  273. u32 reserved;
  274. };
  275. struct pcnet32_tx_head {
  276. u32 base;
  277. s16 length;
  278. s16 status;
  279. u32 misc;
  280. u32 reserved;
  281. };
  282. /* The PCNET32 32-Bit initialization block, described in databook. */
  283. struct pcnet32_init_block {
  284. u16 mode;
  285. u16 tlen_rlen;
  286. u8 phys_addr[6];
  287. u16 reserved;
  288. u32 filter[2];
  289. /* Receive and transmit ring base, along with extra bits. */
  290. u32 rx_ring;
  291. u32 tx_ring;
  292. };
  293. /* PCnet32 access functions */
  294. struct pcnet32_access {
  295. u16 (*read_csr)(unsigned long, int);
  296. void (*write_csr)(unsigned long, int, u16);
  297. u16 (*read_bcr)(unsigned long, int);
  298. void (*write_bcr)(unsigned long, int, u16);
  299. u16 (*read_rap)(unsigned long);
  300. void (*write_rap)(unsigned long, u16);
  301. void (*reset)(unsigned long);
  302. };
  303. /*
  304. * The first field of pcnet32_private is read by the ethernet device
  305. * so the structure should be allocated using pci_alloc_consistent().
  306. */
  307. struct pcnet32_private {
  308. struct pcnet32_init_block init_block;
  309. /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
  310. struct pcnet32_rx_head *rx_ring;
  311. struct pcnet32_tx_head *tx_ring;
  312. dma_addr_t dma_addr; /* DMA address of beginning of this
  313. object, returned by
  314. pci_alloc_consistent */
  315. struct pci_dev *pci_dev; /* Pointer to the associated pci device
  316. structure */
  317. const char *name;
  318. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  319. struct sk_buff **tx_skbuff;
  320. struct sk_buff **rx_skbuff;
  321. dma_addr_t *tx_dma_addr;
  322. dma_addr_t *rx_dma_addr;
  323. struct pcnet32_access a;
  324. spinlock_t lock; /* Guard lock */
  325. unsigned int cur_rx, cur_tx; /* The next free ring entry */
  326. unsigned int rx_ring_size; /* current rx ring size */
  327. unsigned int tx_ring_size; /* current tx ring size */
  328. unsigned int rx_mod_mask; /* rx ring modular mask */
  329. unsigned int tx_mod_mask; /* tx ring modular mask */
  330. unsigned short rx_len_bits;
  331. unsigned short tx_len_bits;
  332. dma_addr_t rx_ring_dma_addr;
  333. dma_addr_t tx_ring_dma_addr;
  334. unsigned int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
  335. struct net_device_stats stats;
  336. char tx_full;
  337. int options;
  338. unsigned int shared_irq:1, /* shared irq possible */
  339. dxsuflo:1, /* disable transmit stop on uflo */
  340. mii:1; /* mii port available */
  341. struct net_device *next;
  342. struct mii_if_info mii_if;
  343. struct timer_list watchdog_timer;
  344. struct timer_list blink_timer;
  345. u32 msg_enable; /* debug message level */
  346. };
  347. static void pcnet32_probe_vlbus(void);
  348. static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
  349. static int pcnet32_probe1(unsigned long, int, struct pci_dev *);
  350. static int pcnet32_open(struct net_device *);
  351. static int pcnet32_init_ring(struct net_device *);
  352. static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
  353. static int pcnet32_rx(struct net_device *);
  354. static void pcnet32_tx_timeout (struct net_device *dev);
  355. static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
  356. static int pcnet32_close(struct net_device *);
  357. static struct net_device_stats *pcnet32_get_stats(struct net_device *);
  358. static void pcnet32_load_multicast(struct net_device *dev);
  359. static void pcnet32_set_multicast_list(struct net_device *);
  360. static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
  361. static void pcnet32_watchdog(struct net_device *);
  362. static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
  363. static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val);
  364. static void pcnet32_restart(struct net_device *dev, unsigned int csr0_bits);
  365. static void pcnet32_ethtool_test(struct net_device *dev,
  366. struct ethtool_test *eth_test, u64 *data);
  367. static int pcnet32_loopback_test(struct net_device *dev, uint64_t *data1);
  368. static int pcnet32_phys_id(struct net_device *dev, u32 data);
  369. static void pcnet32_led_blink_callback(struct net_device *dev);
  370. static int pcnet32_get_regs_len(struct net_device *dev);
  371. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  372. void *ptr);
  373. static void pcnet32_purge_tx_ring(struct net_device *dev);
  374. static int pcnet32_alloc_ring(struct net_device *dev);
  375. static void pcnet32_free_ring(struct net_device *dev);
  376. enum pci_flags_bit {
  377. PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
  378. PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
  379. };
  380. static u16 pcnet32_wio_read_csr (unsigned long addr, int index)
  381. {
  382. outw (index, addr+PCNET32_WIO_RAP);
  383. return inw (addr+PCNET32_WIO_RDP);
  384. }
  385. static void pcnet32_wio_write_csr (unsigned long addr, int index, u16 val)
  386. {
  387. outw (index, addr+PCNET32_WIO_RAP);
  388. outw (val, addr+PCNET32_WIO_RDP);
  389. }
  390. static u16 pcnet32_wio_read_bcr (unsigned long addr, int index)
  391. {
  392. outw (index, addr+PCNET32_WIO_RAP);
  393. return inw (addr+PCNET32_WIO_BDP);
  394. }
  395. static void pcnet32_wio_write_bcr (unsigned long addr, int index, u16 val)
  396. {
  397. outw (index, addr+PCNET32_WIO_RAP);
  398. outw (val, addr+PCNET32_WIO_BDP);
  399. }
  400. static u16 pcnet32_wio_read_rap (unsigned long addr)
  401. {
  402. return inw (addr+PCNET32_WIO_RAP);
  403. }
  404. static void pcnet32_wio_write_rap (unsigned long addr, u16 val)
  405. {
  406. outw (val, addr+PCNET32_WIO_RAP);
  407. }
  408. static void pcnet32_wio_reset (unsigned long addr)
  409. {
  410. inw (addr+PCNET32_WIO_RESET);
  411. }
  412. static int pcnet32_wio_check (unsigned long addr)
  413. {
  414. outw (88, addr+PCNET32_WIO_RAP);
  415. return (inw (addr+PCNET32_WIO_RAP) == 88);
  416. }
  417. static struct pcnet32_access pcnet32_wio = {
  418. .read_csr = pcnet32_wio_read_csr,
  419. .write_csr = pcnet32_wio_write_csr,
  420. .read_bcr = pcnet32_wio_read_bcr,
  421. .write_bcr = pcnet32_wio_write_bcr,
  422. .read_rap = pcnet32_wio_read_rap,
  423. .write_rap = pcnet32_wio_write_rap,
  424. .reset = pcnet32_wio_reset
  425. };
  426. static u16 pcnet32_dwio_read_csr (unsigned long addr, int index)
  427. {
  428. outl (index, addr+PCNET32_DWIO_RAP);
  429. return (inl (addr+PCNET32_DWIO_RDP) & 0xffff);
  430. }
  431. static void pcnet32_dwio_write_csr (unsigned long addr, int index, u16 val)
  432. {
  433. outl (index, addr+PCNET32_DWIO_RAP);
  434. outl (val, addr+PCNET32_DWIO_RDP);
  435. }
  436. static u16 pcnet32_dwio_read_bcr (unsigned long addr, int index)
  437. {
  438. outl (index, addr+PCNET32_DWIO_RAP);
  439. return (inl (addr+PCNET32_DWIO_BDP) & 0xffff);
  440. }
  441. static void pcnet32_dwio_write_bcr (unsigned long addr, int index, u16 val)
  442. {
  443. outl (index, addr+PCNET32_DWIO_RAP);
  444. outl (val, addr+PCNET32_DWIO_BDP);
  445. }
  446. static u16 pcnet32_dwio_read_rap (unsigned long addr)
  447. {
  448. return (inl (addr+PCNET32_DWIO_RAP) & 0xffff);
  449. }
  450. static void pcnet32_dwio_write_rap (unsigned long addr, u16 val)
  451. {
  452. outl (val, addr+PCNET32_DWIO_RAP);
  453. }
  454. static void pcnet32_dwio_reset (unsigned long addr)
  455. {
  456. inl (addr+PCNET32_DWIO_RESET);
  457. }
  458. static int pcnet32_dwio_check (unsigned long addr)
  459. {
  460. outl (88, addr+PCNET32_DWIO_RAP);
  461. return ((inl (addr+PCNET32_DWIO_RAP) & 0xffff) == 88);
  462. }
  463. static struct pcnet32_access pcnet32_dwio = {
  464. .read_csr = pcnet32_dwio_read_csr,
  465. .write_csr = pcnet32_dwio_write_csr,
  466. .read_bcr = pcnet32_dwio_read_bcr,
  467. .write_bcr = pcnet32_dwio_write_bcr,
  468. .read_rap = pcnet32_dwio_read_rap,
  469. .write_rap = pcnet32_dwio_write_rap,
  470. .reset = pcnet32_dwio_reset
  471. };
  472. #ifdef CONFIG_NET_POLL_CONTROLLER
  473. static void pcnet32_poll_controller(struct net_device *dev)
  474. {
  475. disable_irq(dev->irq);
  476. pcnet32_interrupt(0, dev, NULL);
  477. enable_irq(dev->irq);
  478. }
  479. #endif
  480. static int pcnet32_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  481. {
  482. struct pcnet32_private *lp = dev->priv;
  483. unsigned long flags;
  484. int r = -EOPNOTSUPP;
  485. if (lp->mii) {
  486. spin_lock_irqsave(&lp->lock, flags);
  487. mii_ethtool_gset(&lp->mii_if, cmd);
  488. spin_unlock_irqrestore(&lp->lock, flags);
  489. r = 0;
  490. }
  491. return r;
  492. }
  493. static int pcnet32_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  494. {
  495. struct pcnet32_private *lp = dev->priv;
  496. unsigned long flags;
  497. int r = -EOPNOTSUPP;
  498. if (lp->mii) {
  499. spin_lock_irqsave(&lp->lock, flags);
  500. r = mii_ethtool_sset(&lp->mii_if, cmd);
  501. spin_unlock_irqrestore(&lp->lock, flags);
  502. }
  503. return r;
  504. }
  505. static void pcnet32_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  506. {
  507. struct pcnet32_private *lp = dev->priv;
  508. strcpy (info->driver, DRV_NAME);
  509. strcpy (info->version, DRV_VERSION);
  510. if (lp->pci_dev)
  511. strcpy (info->bus_info, pci_name(lp->pci_dev));
  512. else
  513. sprintf(info->bus_info, "VLB 0x%lx", dev->base_addr);
  514. }
  515. static u32 pcnet32_get_link(struct net_device *dev)
  516. {
  517. struct pcnet32_private *lp = dev->priv;
  518. unsigned long flags;
  519. int r;
  520. spin_lock_irqsave(&lp->lock, flags);
  521. if (lp->mii) {
  522. r = mii_link_ok(&lp->mii_if);
  523. } else {
  524. ulong ioaddr = dev->base_addr; /* card base I/O address */
  525. r = (lp->a.read_bcr(ioaddr, 4) != 0xc0);
  526. }
  527. spin_unlock_irqrestore(&lp->lock, flags);
  528. return r;
  529. }
  530. static u32 pcnet32_get_msglevel(struct net_device *dev)
  531. {
  532. struct pcnet32_private *lp = dev->priv;
  533. return lp->msg_enable;
  534. }
  535. static void pcnet32_set_msglevel(struct net_device *dev, u32 value)
  536. {
  537. struct pcnet32_private *lp = dev->priv;
  538. lp->msg_enable = value;
  539. }
  540. static int pcnet32_nway_reset(struct net_device *dev)
  541. {
  542. struct pcnet32_private *lp = dev->priv;
  543. unsigned long flags;
  544. int r = -EOPNOTSUPP;
  545. if (lp->mii) {
  546. spin_lock_irqsave(&lp->lock, flags);
  547. r = mii_nway_restart(&lp->mii_if);
  548. spin_unlock_irqrestore(&lp->lock, flags);
  549. }
  550. return r;
  551. }
  552. static void pcnet32_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  553. {
  554. struct pcnet32_private *lp = dev->priv;
  555. ering->tx_max_pending = TX_MAX_RING_SIZE - 1;
  556. ering->tx_pending = lp->tx_ring_size - 1;
  557. ering->rx_max_pending = RX_MAX_RING_SIZE - 1;
  558. ering->rx_pending = lp->rx_ring_size - 1;
  559. }
  560. static int pcnet32_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  561. {
  562. struct pcnet32_private *lp = dev->priv;
  563. unsigned long flags;
  564. int i;
  565. if (ering->rx_mini_pending || ering->rx_jumbo_pending)
  566. return -EINVAL;
  567. if (netif_running(dev))
  568. pcnet32_close(dev);
  569. spin_lock_irqsave(&lp->lock, flags);
  570. pcnet32_free_ring(dev);
  571. lp->tx_ring_size = min(ering->tx_pending, (unsigned int) TX_MAX_RING_SIZE);
  572. lp->rx_ring_size = min(ering->rx_pending, (unsigned int) RX_MAX_RING_SIZE);
  573. /* set the minimum ring size to 4, to allow the loopback test to work
  574. * unchanged.
  575. */
  576. for (i = 2; i <= PCNET32_LOG_MAX_TX_BUFFERS; i++) {
  577. if (lp->tx_ring_size <= (1 << i))
  578. break;
  579. }
  580. lp->tx_ring_size = (1 << i);
  581. lp->tx_mod_mask = lp->tx_ring_size - 1;
  582. lp->tx_len_bits = (i << 12);
  583. for (i = 2; i <= PCNET32_LOG_MAX_RX_BUFFERS; i++) {
  584. if (lp->rx_ring_size <= (1 << i))
  585. break;
  586. }
  587. lp->rx_ring_size = (1 << i);
  588. lp->rx_mod_mask = lp->rx_ring_size - 1;
  589. lp->rx_len_bits = (i << 4);
  590. if (pcnet32_alloc_ring(dev)) {
  591. pcnet32_free_ring(dev);
  592. return -ENOMEM;
  593. }
  594. spin_unlock_irqrestore(&lp->lock, flags);
  595. if (pcnet32_debug & NETIF_MSG_DRV)
  596. printk(KERN_INFO PFX "Ring Param Settings: RX: %d, TX: %d\n", lp->rx_ring_size, lp->tx_ring_size);
  597. if (netif_running(dev))
  598. pcnet32_open(dev);
  599. return 0;
  600. }
  601. static void pcnet32_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  602. {
  603. memcpy(data, pcnet32_gstrings_test, sizeof(pcnet32_gstrings_test));
  604. }
  605. static int pcnet32_self_test_count(struct net_device *dev)
  606. {
  607. return PCNET32_TEST_LEN;
  608. }
  609. static void pcnet32_ethtool_test(struct net_device *dev,
  610. struct ethtool_test *test, u64 *data)
  611. {
  612. struct pcnet32_private *lp = dev->priv;
  613. int rc;
  614. if (test->flags == ETH_TEST_FL_OFFLINE) {
  615. rc = pcnet32_loopback_test(dev, data);
  616. if (rc) {
  617. if (netif_msg_hw(lp))
  618. printk(KERN_DEBUG "%s: Loopback test failed.\n", dev->name);
  619. test->flags |= ETH_TEST_FL_FAILED;
  620. } else if (netif_msg_hw(lp))
  621. printk(KERN_DEBUG "%s: Loopback test passed.\n", dev->name);
  622. } else if (netif_msg_hw(lp))
  623. printk(KERN_DEBUG "%s: No tests to run (specify 'Offline' on ethtool).", dev->name);
  624. } /* end pcnet32_ethtool_test */
  625. static int pcnet32_loopback_test(struct net_device *dev, uint64_t *data1)
  626. {
  627. struct pcnet32_private *lp = dev->priv;
  628. struct pcnet32_access *a = &lp->a; /* access to registers */
  629. ulong ioaddr = dev->base_addr; /* card base I/O address */
  630. struct sk_buff *skb; /* sk buff */
  631. int x, i; /* counters */
  632. int numbuffs = 4; /* number of TX/RX buffers and descs */
  633. u16 status = 0x8300; /* TX ring status */
  634. u16 teststatus; /* test of ring status */
  635. int rc; /* return code */
  636. int size; /* size of packets */
  637. unsigned char *packet; /* source packet data */
  638. static int data_len = 60; /* length of source packets */
  639. unsigned long flags;
  640. unsigned long ticks;
  641. *data1 = 1; /* status of test, default to fail */
  642. rc = 1; /* default to fail */
  643. if (netif_running(dev))
  644. pcnet32_close(dev);
  645. spin_lock_irqsave(&lp->lock, flags);
  646. /* Reset the PCNET32 */
  647. lp->a.reset (ioaddr);
  648. /* switch pcnet32 to 32bit mode */
  649. lp->a.write_bcr (ioaddr, 20, 2);
  650. lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  651. lp->init_block.filter[0] = 0;
  652. lp->init_block.filter[1] = 0;
  653. /* purge & init rings but don't actually restart */
  654. pcnet32_restart(dev, 0x0000);
  655. lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
  656. /* Initialize Transmit buffers. */
  657. size = data_len + 15;
  658. for (x=0; x<numbuffs; x++) {
  659. if (!(skb = dev_alloc_skb(size))) {
  660. if (netif_msg_hw(lp))
  661. printk(KERN_DEBUG "%s: Cannot allocate skb at line: %d!\n",
  662. dev->name, __LINE__);
  663. goto clean_up;
  664. } else {
  665. packet = skb->data;
  666. skb_put(skb, size); /* create space for data */
  667. lp->tx_skbuff[x] = skb;
  668. lp->tx_ring[x].length = le16_to_cpu(-skb->len);
  669. lp->tx_ring[x].misc = 0;
  670. /* put DA and SA into the skb */
  671. for (i=0; i<6; i++)
  672. *packet++ = dev->dev_addr[i];
  673. for (i=0; i<6; i++)
  674. *packet++ = dev->dev_addr[i];
  675. /* type */
  676. *packet++ = 0x08;
  677. *packet++ = 0x06;
  678. /* packet number */
  679. *packet++ = x;
  680. /* fill packet with data */
  681. for (i=0; i<data_len; i++)
  682. *packet++ = i;
  683. lp->tx_dma_addr[x] = pci_map_single(lp->pci_dev, skb->data,
  684. skb->len, PCI_DMA_TODEVICE);
  685. lp->tx_ring[x].base = (u32)le32_to_cpu(lp->tx_dma_addr[x]);
  686. wmb(); /* Make sure owner changes after all others are visible */
  687. lp->tx_ring[x].status = le16_to_cpu(status);
  688. }
  689. }
  690. x = a->read_bcr(ioaddr, 32); /* set internal loopback in BSR32 */
  691. x = x | 0x0002;
  692. a->write_bcr(ioaddr, 32, x);
  693. lp->a.write_csr (ioaddr, 15, 0x0044); /* set int loopback in CSR15 */
  694. teststatus = le16_to_cpu(0x8000);
  695. lp->a.write_csr(ioaddr, 0, 0x0002); /* Set STRT bit */
  696. /* Check status of descriptors */
  697. for (x=0; x<numbuffs; x++) {
  698. ticks = 0;
  699. rmb();
  700. while ((lp->rx_ring[x].status & teststatus) && (ticks < 200)) {
  701. spin_unlock_irqrestore(&lp->lock, flags);
  702. mdelay(1);
  703. spin_lock_irqsave(&lp->lock, flags);
  704. rmb();
  705. ticks++;
  706. }
  707. if (ticks == 200) {
  708. if (netif_msg_hw(lp))
  709. printk("%s: Desc %d failed to reset!\n",dev->name,x);
  710. break;
  711. }
  712. }
  713. lp->a.write_csr(ioaddr, 0, 0x0004); /* Set STOP bit */
  714. wmb();
  715. if (netif_msg_hw(lp) && netif_msg_pktdata(lp)) {
  716. printk(KERN_DEBUG "%s: RX loopback packets:\n", dev->name);
  717. for (x=0; x<numbuffs; x++) {
  718. printk(KERN_DEBUG "%s: Packet %d:\n", dev->name, x);
  719. skb = lp->rx_skbuff[x];
  720. for (i=0; i<size; i++) {
  721. printk("%02x ", *(skb->data+i));
  722. }
  723. printk("\n");
  724. }
  725. }
  726. x = 0;
  727. rc = 0;
  728. while (x<numbuffs && !rc) {
  729. skb = lp->rx_skbuff[x];
  730. packet = lp->tx_skbuff[x]->data;
  731. for (i=0; i<size; i++) {
  732. if (*(skb->data+i) != packet[i]) {
  733. if (netif_msg_hw(lp))
  734. printk(KERN_DEBUG "%s: Error in compare! %2x - %02x %02x\n",
  735. dev->name, i, *(skb->data+i), packet[i]);
  736. rc = 1;
  737. break;
  738. }
  739. }
  740. x++;
  741. }
  742. if (!rc) {
  743. *data1 = 0;
  744. }
  745. clean_up:
  746. pcnet32_purge_tx_ring(dev);
  747. x = a->read_csr(ioaddr, 15) & 0xFFFF;
  748. a->write_csr(ioaddr, 15, (x & ~0x0044)); /* reset bits 6 and 2 */
  749. x = a->read_bcr(ioaddr, 32); /* reset internal loopback */
  750. x = x & ~0x0002;
  751. a->write_bcr(ioaddr, 32, x);
  752. spin_unlock_irqrestore(&lp->lock, flags);
  753. if (netif_running(dev)) {
  754. pcnet32_open(dev);
  755. } else {
  756. lp->a.write_bcr (ioaddr, 20, 4); /* return to 16bit mode */
  757. }
  758. return(rc);
  759. } /* end pcnet32_loopback_test */
  760. static void pcnet32_led_blink_callback(struct net_device *dev)
  761. {
  762. struct pcnet32_private *lp = dev->priv;
  763. struct pcnet32_access *a = &lp->a;
  764. ulong ioaddr = dev->base_addr;
  765. unsigned long flags;
  766. int i;
  767. spin_lock_irqsave(&lp->lock, flags);
  768. for (i=4; i<8; i++) {
  769. a->write_bcr(ioaddr, i, a->read_bcr(ioaddr, i) ^ 0x4000);
  770. }
  771. spin_unlock_irqrestore(&lp->lock, flags);
  772. mod_timer(&lp->blink_timer, PCNET32_BLINK_TIMEOUT);
  773. }
  774. static int pcnet32_phys_id(struct net_device *dev, u32 data)
  775. {
  776. struct pcnet32_private *lp = dev->priv;
  777. struct pcnet32_access *a = &lp->a;
  778. ulong ioaddr = dev->base_addr;
  779. unsigned long flags;
  780. int i, regs[4];
  781. if (!lp->blink_timer.function) {
  782. init_timer(&lp->blink_timer);
  783. lp->blink_timer.function = (void *) pcnet32_led_blink_callback;
  784. lp->blink_timer.data = (unsigned long) dev;
  785. }
  786. /* Save the current value of the bcrs */
  787. spin_lock_irqsave(&lp->lock, flags);
  788. for (i=4; i<8; i++) {
  789. regs[i-4] = a->read_bcr(ioaddr, i);
  790. }
  791. spin_unlock_irqrestore(&lp->lock, flags);
  792. mod_timer(&lp->blink_timer, jiffies);
  793. set_current_state(TASK_INTERRUPTIBLE);
  794. if ((!data) || (data > (u32)(MAX_SCHEDULE_TIMEOUT / HZ)))
  795. data = (u32)(MAX_SCHEDULE_TIMEOUT / HZ);
  796. msleep_interruptible(data * 1000);
  797. del_timer_sync(&lp->blink_timer);
  798. /* Restore the original value of the bcrs */
  799. spin_lock_irqsave(&lp->lock, flags);
  800. for (i=4; i<8; i++) {
  801. a->write_bcr(ioaddr, i, regs[i-4]);
  802. }
  803. spin_unlock_irqrestore(&lp->lock, flags);
  804. return 0;
  805. }
  806. static int pcnet32_get_regs_len(struct net_device *dev)
  807. {
  808. return(PCNET32_NUM_REGS * sizeof(u16));
  809. }
  810. static void pcnet32_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  811. void *ptr)
  812. {
  813. int i, csr0;
  814. u16 *buff = ptr;
  815. struct pcnet32_private *lp = dev->priv;
  816. struct pcnet32_access *a = &lp->a;
  817. ulong ioaddr = dev->base_addr;
  818. int ticks;
  819. unsigned long flags;
  820. spin_lock_irqsave(&lp->lock, flags);
  821. csr0 = a->read_csr(ioaddr, 0);
  822. if (!(csr0 & 0x0004)) { /* If not stopped */
  823. /* set SUSPEND (SPND) - CSR5 bit 0 */
  824. a->write_csr(ioaddr, 5, 0x0001);
  825. /* poll waiting for bit to be set */
  826. ticks = 0;
  827. while (!(a->read_csr(ioaddr, 5) & 0x0001)) {
  828. spin_unlock_irqrestore(&lp->lock, flags);
  829. mdelay(1);
  830. spin_lock_irqsave(&lp->lock, flags);
  831. ticks++;
  832. if (ticks > 200) {
  833. if (netif_msg_hw(lp))
  834. printk(KERN_DEBUG "%s: Error getting into suspend!\n",
  835. dev->name);
  836. break;
  837. }
  838. }
  839. }
  840. /* read address PROM */
  841. for (i=0; i<16; i += 2)
  842. *buff++ = inw(ioaddr + i);
  843. /* read control and status registers */
  844. for (i=0; i<90; i++) {
  845. *buff++ = a->read_csr(ioaddr, i);
  846. }
  847. *buff++ = a->read_csr(ioaddr, 112);
  848. *buff++ = a->read_csr(ioaddr, 114);
  849. /* read bus configuration registers */
  850. for (i=0; i<36; i++) {
  851. *buff++ = a->read_bcr(ioaddr, i);
  852. }
  853. /* read mii phy registers */
  854. if (lp->mii) {
  855. for (i=0; i<32; i++) {
  856. lp->a.write_bcr(ioaddr, 33, ((lp->mii_if.phy_id) << 5) | i);
  857. *buff++ = lp->a.read_bcr(ioaddr, 34);
  858. }
  859. }
  860. if (!(csr0 & 0x0004)) { /* If not stopped */
  861. /* clear SUSPEND (SPND) - CSR5 bit 0 */
  862. a->write_csr(ioaddr, 5, 0x0000);
  863. }
  864. i = buff - (u16 *)ptr;
  865. for (; i < PCNET32_NUM_REGS; i++)
  866. *buff++ = 0;
  867. spin_unlock_irqrestore(&lp->lock, flags);
  868. }
  869. static struct ethtool_ops pcnet32_ethtool_ops = {
  870. .get_settings = pcnet32_get_settings,
  871. .set_settings = pcnet32_set_settings,
  872. .get_drvinfo = pcnet32_get_drvinfo,
  873. .get_msglevel = pcnet32_get_msglevel,
  874. .set_msglevel = pcnet32_set_msglevel,
  875. .nway_reset = pcnet32_nway_reset,
  876. .get_link = pcnet32_get_link,
  877. .get_ringparam = pcnet32_get_ringparam,
  878. .set_ringparam = pcnet32_set_ringparam,
  879. .get_tx_csum = ethtool_op_get_tx_csum,
  880. .get_sg = ethtool_op_get_sg,
  881. .get_tso = ethtool_op_get_tso,
  882. .get_strings = pcnet32_get_strings,
  883. .self_test_count = pcnet32_self_test_count,
  884. .self_test = pcnet32_ethtool_test,
  885. .phys_id = pcnet32_phys_id,
  886. .get_regs_len = pcnet32_get_regs_len,
  887. .get_regs = pcnet32_get_regs,
  888. .get_perm_addr = ethtool_op_get_perm_addr,
  889. };
  890. /* only probes for non-PCI devices, the rest are handled by
  891. * pci_register_driver via pcnet32_probe_pci */
  892. static void __devinit
  893. pcnet32_probe_vlbus(void)
  894. {
  895. unsigned int *port, ioaddr;
  896. /* search for PCnet32 VLB cards at known addresses */
  897. for (port = pcnet32_portlist; (ioaddr = *port); port++) {
  898. if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
  899. /* check if there is really a pcnet chip on that ioaddr */
  900. if ((inb(ioaddr + 14) == 0x57) && (inb(ioaddr + 15) == 0x57)) {
  901. pcnet32_probe1(ioaddr, 0, NULL);
  902. } else {
  903. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  904. }
  905. }
  906. }
  907. }
  908. static int __devinit
  909. pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
  910. {
  911. unsigned long ioaddr;
  912. int err;
  913. err = pci_enable_device(pdev);
  914. if (err < 0) {
  915. if (pcnet32_debug & NETIF_MSG_PROBE)
  916. printk(KERN_ERR PFX "failed to enable device -- err=%d\n", err);
  917. return err;
  918. }
  919. pci_set_master(pdev);
  920. ioaddr = pci_resource_start (pdev, 0);
  921. if (!ioaddr) {
  922. if (pcnet32_debug & NETIF_MSG_PROBE)
  923. printk (KERN_ERR PFX "card has no PCI IO resources, aborting\n");
  924. return -ENODEV;
  925. }
  926. if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
  927. if (pcnet32_debug & NETIF_MSG_PROBE)
  928. printk(KERN_ERR PFX "architecture does not support 32bit PCI busmaster DMA\n");
  929. return -ENODEV;
  930. }
  931. if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") == NULL) {
  932. if (pcnet32_debug & NETIF_MSG_PROBE)
  933. printk(KERN_ERR PFX "io address range already allocated\n");
  934. return -EBUSY;
  935. }
  936. err = pcnet32_probe1(ioaddr, 1, pdev);
  937. if (err < 0) {
  938. pci_disable_device(pdev);
  939. }
  940. return err;
  941. }
  942. /* pcnet32_probe1
  943. * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
  944. * pdev will be NULL when called from pcnet32_probe_vlbus.
  945. */
  946. static int __devinit
  947. pcnet32_probe1(unsigned long ioaddr, int shared, struct pci_dev *pdev)
  948. {
  949. struct pcnet32_private *lp;
  950. dma_addr_t lp_dma_addr;
  951. int i, media;
  952. int fdx, mii, fset, dxsuflo;
  953. int chip_version;
  954. char *chipname;
  955. struct net_device *dev;
  956. struct pcnet32_access *a = NULL;
  957. u8 promaddr[6];
  958. int ret = -ENODEV;
  959. /* reset the chip */
  960. pcnet32_wio_reset(ioaddr);
  961. /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
  962. if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
  963. a = &pcnet32_wio;
  964. } else {
  965. pcnet32_dwio_reset(ioaddr);
  966. if (pcnet32_dwio_read_csr(ioaddr, 0) == 4 && pcnet32_dwio_check(ioaddr)) {
  967. a = &pcnet32_dwio;
  968. } else
  969. goto err_release_region;
  970. }
  971. chip_version = a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr,89) << 16);
  972. if ((pcnet32_debug & NETIF_MSG_PROBE) && (pcnet32_debug & NETIF_MSG_HW))
  973. printk(KERN_INFO " PCnet chip version is %#x.\n", chip_version);
  974. if ((chip_version & 0xfff) != 0x003) {
  975. if (pcnet32_debug & NETIF_MSG_PROBE)
  976. printk(KERN_INFO PFX "Unsupported chip version.\n");
  977. goto err_release_region;
  978. }
  979. /* initialize variables */
  980. fdx = mii = fset = dxsuflo = 0;
  981. chip_version = (chip_version >> 12) & 0xffff;
  982. switch (chip_version) {
  983. case 0x2420:
  984. chipname = "PCnet/PCI 79C970"; /* PCI */
  985. break;
  986. case 0x2430:
  987. if (shared)
  988. chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
  989. else
  990. chipname = "PCnet/32 79C965"; /* 486/VL bus */
  991. break;
  992. case 0x2621:
  993. chipname = "PCnet/PCI II 79C970A"; /* PCI */
  994. fdx = 1;
  995. break;
  996. case 0x2623:
  997. chipname = "PCnet/FAST 79C971"; /* PCI */
  998. fdx = 1; mii = 1; fset = 1;
  999. break;
  1000. case 0x2624:
  1001. chipname = "PCnet/FAST+ 79C972"; /* PCI */
  1002. fdx = 1; mii = 1; fset = 1;
  1003. break;
  1004. case 0x2625:
  1005. chipname = "PCnet/FAST III 79C973"; /* PCI */
  1006. fdx = 1; mii = 1;
  1007. break;
  1008. case 0x2626:
  1009. chipname = "PCnet/Home 79C978"; /* PCI */
  1010. fdx = 1;
  1011. /*
  1012. * This is based on specs published at www.amd.com. This section
  1013. * assumes that a card with a 79C978 wants to go into standard
  1014. * ethernet mode. The 79C978 can also go into 1Mb HomePNA mode,
  1015. * and the module option homepna=1 can select this instead.
  1016. */
  1017. media = a->read_bcr(ioaddr, 49);
  1018. media &= ~3; /* default to 10Mb ethernet */
  1019. if (cards_found < MAX_UNITS && homepna[cards_found])
  1020. media |= 1; /* switch to home wiring mode */
  1021. if (pcnet32_debug & NETIF_MSG_PROBE)
  1022. printk(KERN_DEBUG PFX "media set to %sMbit mode.\n",
  1023. (media & 1) ? "1" : "10");
  1024. a->write_bcr(ioaddr, 49, media);
  1025. break;
  1026. case 0x2627:
  1027. chipname = "PCnet/FAST III 79C975"; /* PCI */
  1028. fdx = 1; mii = 1;
  1029. break;
  1030. case 0x2628:
  1031. chipname = "PCnet/PRO 79C976";
  1032. fdx = 1; mii = 1;
  1033. break;
  1034. default:
  1035. if (pcnet32_debug & NETIF_MSG_PROBE)
  1036. printk(KERN_INFO PFX "PCnet version %#x, no PCnet32 chip.\n",
  1037. chip_version);
  1038. goto err_release_region;
  1039. }
  1040. /*
  1041. * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
  1042. * starting until the packet is loaded. Strike one for reliability, lose
  1043. * one for latency - although on PCI this isnt a big loss. Older chips
  1044. * have FIFO's smaller than a packet, so you can't do this.
  1045. * Turn on BCR18:BurstRdEn and BCR18:BurstWrEn.
  1046. */
  1047. if (fset) {
  1048. a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0860));
  1049. a->write_csr(ioaddr, 80, (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
  1050. dxsuflo = 1;
  1051. }
  1052. dev = alloc_etherdev(0);
  1053. if (!dev) {
  1054. if (pcnet32_debug & NETIF_MSG_PROBE)
  1055. printk(KERN_ERR PFX "Memory allocation failed.\n");
  1056. ret = -ENOMEM;
  1057. goto err_release_region;
  1058. }
  1059. SET_NETDEV_DEV(dev, &pdev->dev);
  1060. if (pcnet32_debug & NETIF_MSG_PROBE)
  1061. printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
  1062. /* In most chips, after a chip reset, the ethernet address is read from the
  1063. * station address PROM at the base address and programmed into the
  1064. * "Physical Address Registers" CSR12-14.
  1065. * As a precautionary measure, we read the PROM values and complain if
  1066. * they disagree with the CSRs. Either way, we use the CSR values, and
  1067. * double check that they are valid.
  1068. */
  1069. for (i = 0; i < 3; i++) {
  1070. unsigned int val;
  1071. val = a->read_csr(ioaddr, i+12) & 0x0ffff;
  1072. /* There may be endianness issues here. */
  1073. dev->dev_addr[2*i] = val & 0x0ff;
  1074. dev->dev_addr[2*i+1] = (val >> 8) & 0x0ff;
  1075. }
  1076. /* read PROM address and compare with CSR address */
  1077. for (i = 0; i < 6; i++)
  1078. promaddr[i] = inb(ioaddr + i);
  1079. if (memcmp(promaddr, dev->dev_addr, 6)
  1080. || !is_valid_ether_addr(dev->dev_addr)) {
  1081. #ifndef __powerpc__
  1082. if (is_valid_ether_addr(promaddr)) {
  1083. #else
  1084. if (!is_valid_ether_addr(dev->dev_addr)
  1085. && is_valid_ether_addr(promaddr)) {
  1086. #endif
  1087. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1088. printk(" warning: CSR address invalid,\n");
  1089. printk(KERN_INFO " using instead PROM address of");
  1090. }
  1091. memcpy(dev->dev_addr, promaddr, 6);
  1092. }
  1093. }
  1094. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1095. /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
  1096. if (!is_valid_ether_addr(dev->perm_addr))
  1097. memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
  1098. if (pcnet32_debug & NETIF_MSG_PROBE) {
  1099. for (i = 0; i < 6; i++)
  1100. printk(" %2.2x", dev->dev_addr[i]);
  1101. /* Version 0x2623 and 0x2624 */
  1102. if (((chip_version + 1) & 0xfffe) == 0x2624) {
  1103. i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
  1104. printk("\n" KERN_INFO " tx_start_pt(0x%04x):",i);
  1105. switch(i>>10) {
  1106. case 0: printk(" 20 bytes,"); break;
  1107. case 1: printk(" 64 bytes,"); break;
  1108. case 2: printk(" 128 bytes,"); break;
  1109. case 3: printk("~220 bytes,"); break;
  1110. }
  1111. i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
  1112. printk(" BCR18(%x):",i&0xffff);
  1113. if (i & (1<<5)) printk("BurstWrEn ");
  1114. if (i & (1<<6)) printk("BurstRdEn ");
  1115. if (i & (1<<7)) printk("DWordIO ");
  1116. if (i & (1<<11)) printk("NoUFlow ");
  1117. i = a->read_bcr(ioaddr, 25);
  1118. printk("\n" KERN_INFO " SRAMSIZE=0x%04x,",i<<8);
  1119. i = a->read_bcr(ioaddr, 26);
  1120. printk(" SRAM_BND=0x%04x,",i<<8);
  1121. i = a->read_bcr(ioaddr, 27);
  1122. if (i & (1<<14)) printk("LowLatRx");
  1123. }
  1124. }
  1125. dev->base_addr = ioaddr;
  1126. /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
  1127. if ((lp = pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
  1128. if (pcnet32_debug & NETIF_MSG_PROBE)
  1129. printk(KERN_ERR PFX "Consistent memory allocation failed.\n");
  1130. ret = -ENOMEM;
  1131. goto err_free_netdev;
  1132. }
  1133. memset(lp, 0, sizeof(*lp));
  1134. lp->dma_addr = lp_dma_addr;
  1135. lp->pci_dev = pdev;
  1136. spin_lock_init(&lp->lock);
  1137. SET_MODULE_OWNER(dev);
  1138. SET_NETDEV_DEV(dev, &pdev->dev);
  1139. dev->priv = lp;
  1140. lp->name = chipname;
  1141. lp->shared_irq = shared;
  1142. lp->tx_ring_size = TX_RING_SIZE; /* default tx ring size */
  1143. lp->rx_ring_size = RX_RING_SIZE; /* default rx ring size */
  1144. lp->tx_mod_mask = lp->tx_ring_size - 1;
  1145. lp->rx_mod_mask = lp->rx_ring_size - 1;
  1146. lp->tx_len_bits = (PCNET32_LOG_TX_BUFFERS << 12);
  1147. lp->rx_len_bits = (PCNET32_LOG_RX_BUFFERS << 4);
  1148. lp->mii_if.full_duplex = fdx;
  1149. lp->mii_if.phy_id_mask = 0x1f;
  1150. lp->mii_if.reg_num_mask = 0x1f;
  1151. lp->dxsuflo = dxsuflo;
  1152. lp->mii = mii;
  1153. lp->msg_enable = pcnet32_debug;
  1154. if ((cards_found >= MAX_UNITS) || (options[cards_found] > sizeof(options_mapping)))
  1155. lp->options = PCNET32_PORT_ASEL;
  1156. else
  1157. lp->options = options_mapping[options[cards_found]];
  1158. lp->mii_if.dev = dev;
  1159. lp->mii_if.mdio_read = mdio_read;
  1160. lp->mii_if.mdio_write = mdio_write;
  1161. if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
  1162. ((cards_found>=MAX_UNITS) || full_duplex[cards_found]))
  1163. lp->options |= PCNET32_PORT_FD;
  1164. if (!a) {
  1165. if (pcnet32_debug & NETIF_MSG_PROBE)
  1166. printk(KERN_ERR PFX "No access methods\n");
  1167. ret = -ENODEV;
  1168. goto err_free_consistent;
  1169. }
  1170. lp->a = *a;
  1171. if (pcnet32_alloc_ring(dev)) {
  1172. ret = -ENOMEM;
  1173. goto err_free_ring;
  1174. }
  1175. /* detect special T1/E1 WAN card by checking for MAC address */
  1176. if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0
  1177. && dev->dev_addr[2] == 0x75)
  1178. lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
  1179. lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
  1180. lp->init_block.tlen_rlen = le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
  1181. for (i = 0; i < 6; i++)
  1182. lp->init_block.phys_addr[i] = dev->dev_addr[i];
  1183. lp->init_block.filter[0] = 0x00000000;
  1184. lp->init_block.filter[1] = 0x00000000;
  1185. lp->init_block.rx_ring = (u32)le32_to_cpu(lp->rx_ring_dma_addr);
  1186. lp->init_block.tx_ring = (u32)le32_to_cpu(lp->tx_ring_dma_addr);
  1187. /* switch pcnet32 to 32bit mode */
  1188. a->write_bcr(ioaddr, 20, 2);
  1189. a->write_csr(ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private,
  1190. init_block)) & 0xffff);
  1191. a->write_csr(ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private,
  1192. init_block)) >> 16);
  1193. if (pdev) { /* use the IRQ provided by PCI */
  1194. dev->irq = pdev->irq;
  1195. if (pcnet32_debug & NETIF_MSG_PROBE)
  1196. printk(" assigned IRQ %d.\n", dev->irq);
  1197. } else {
  1198. unsigned long irq_mask = probe_irq_on();
  1199. /*
  1200. * To auto-IRQ we enable the initialization-done and DMA error
  1201. * interrupts. For ISA boards we get a DMA error, but VLB and PCI
  1202. * boards will work.
  1203. */
  1204. /* Trigger an initialization just for the interrupt. */
  1205. a->write_csr (ioaddr, 0, 0x41);
  1206. mdelay (1);
  1207. dev->irq = probe_irq_off (irq_mask);
  1208. if (!dev->irq) {
  1209. if (pcnet32_debug & NETIF_MSG_PROBE)
  1210. printk(", failed to detect IRQ line.\n");
  1211. ret = -ENODEV;
  1212. goto err_free_ring;
  1213. }
  1214. if (pcnet32_debug & NETIF_MSG_PROBE)
  1215. printk(", probed IRQ %d.\n", dev->irq);
  1216. }
  1217. /* Set the mii phy_id so that we can query the link state */
  1218. if (lp->mii)
  1219. lp->mii_if.phy_id = ((lp->a.read_bcr (ioaddr, 33)) >> 5) & 0x1f;
  1220. init_timer (&lp->watchdog_timer);
  1221. lp->watchdog_timer.data = (unsigned long) dev;
  1222. lp->watchdog_timer.function = (void *) &pcnet32_watchdog;
  1223. /* The PCNET32-specific entries in the device structure. */
  1224. dev->open = &pcnet32_open;
  1225. dev->hard_start_xmit = &pcnet32_start_xmit;
  1226. dev->stop = &pcnet32_close;
  1227. dev->get_stats = &pcnet32_get_stats;
  1228. dev->set_multicast_list = &pcnet32_set_multicast_list;
  1229. dev->do_ioctl = &pcnet32_ioctl;
  1230. dev->ethtool_ops = &pcnet32_ethtool_ops;
  1231. dev->tx_timeout = pcnet32_tx_timeout;
  1232. dev->watchdog_timeo = (5*HZ);
  1233. #ifdef CONFIG_NET_POLL_CONTROLLER
  1234. dev->poll_controller = pcnet32_poll_controller;
  1235. #endif
  1236. /* Fill in the generic fields of the device structure. */
  1237. if (register_netdev(dev))
  1238. goto err_free_ring;
  1239. if (pdev) {
  1240. pci_set_drvdata(pdev, dev);
  1241. } else {
  1242. lp->next = pcnet32_dev;
  1243. pcnet32_dev = dev;
  1244. }
  1245. if (pcnet32_debug & NETIF_MSG_PROBE)
  1246. printk(KERN_INFO "%s: registered as %s\n", dev->name, lp->name);
  1247. cards_found++;
  1248. /* enable LED writes */
  1249. a->write_bcr(ioaddr, 2, a->read_bcr(ioaddr, 2) | 0x1000);
  1250. return 0;
  1251. err_free_ring:
  1252. pcnet32_free_ring(dev);
  1253. err_free_consistent:
  1254. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  1255. err_free_netdev:
  1256. free_netdev(dev);
  1257. err_release_region:
  1258. release_region(ioaddr, PCNET32_TOTAL_SIZE);
  1259. return ret;
  1260. }
  1261. static int pcnet32_alloc_ring(struct net_device *dev)
  1262. {
  1263. struct pcnet32_private *lp = dev->priv;
  1264. if ((lp->tx_ring = pci_alloc_consistent(lp->pci_dev, sizeof(struct pcnet32_tx_head) * lp->tx_ring_size,
  1265. &lp->tx_ring_dma_addr)) == NULL) {
  1266. if (pcnet32_debug & NETIF_MSG_DRV)
  1267. printk(KERN_ERR PFX "Consistent memory allocation failed.\n");
  1268. return -ENOMEM;
  1269. }
  1270. if ((lp->rx_ring = pci_alloc_consistent(lp->pci_dev, sizeof(struct pcnet32_rx_head) * lp->rx_ring_size,
  1271. &lp->rx_ring_dma_addr)) == NULL) {
  1272. if (pcnet32_debug & NETIF_MSG_DRV)
  1273. printk(KERN_ERR PFX "Consistent memory allocation failed.\n");
  1274. return -ENOMEM;
  1275. }
  1276. if (!(lp->tx_dma_addr = kmalloc(sizeof(dma_addr_t) * lp->tx_ring_size, GFP_ATOMIC))) {
  1277. if (pcnet32_debug & NETIF_MSG_DRV)
  1278. printk(KERN_ERR PFX "Memory allocation failed.\n");
  1279. return -ENOMEM;
  1280. }
  1281. memset(lp->tx_dma_addr, 0, sizeof(dma_addr_t) * lp->tx_ring_size);
  1282. if (!(lp->rx_dma_addr = kmalloc(sizeof(dma_addr_t) * lp->rx_ring_size, GFP_ATOMIC))) {
  1283. if (pcnet32_debug & NETIF_MSG_DRV)
  1284. printk(KERN_ERR PFX "Memory allocation failed.\n");
  1285. return -ENOMEM;
  1286. }
  1287. memset(lp->rx_dma_addr, 0, sizeof(dma_addr_t) * lp->rx_ring_size);
  1288. if (!(lp->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * lp->tx_ring_size, GFP_ATOMIC))) {
  1289. if (pcnet32_debug & NETIF_MSG_DRV)
  1290. printk(KERN_ERR PFX "Memory allocation failed.\n");
  1291. return -ENOMEM;
  1292. }
  1293. memset(lp->tx_skbuff, 0, sizeof(struct sk_buff *) * lp->tx_ring_size);
  1294. if (!(lp->rx_skbuff = kmalloc(sizeof(struct sk_buff *) * lp->rx_ring_size, GFP_ATOMIC))) {
  1295. if (pcnet32_debug & NETIF_MSG_DRV)
  1296. printk(KERN_ERR PFX "Memory allocation failed.\n");
  1297. return -ENOMEM;
  1298. }
  1299. memset(lp->rx_skbuff, 0, sizeof(struct sk_buff *) * lp->rx_ring_size);
  1300. return 0;
  1301. }
  1302. static void pcnet32_free_ring(struct net_device *dev)
  1303. {
  1304. struct pcnet32_private *lp = dev->priv;
  1305. kfree(lp->tx_skbuff);
  1306. lp->tx_skbuff = NULL;
  1307. kfree(lp->rx_skbuff);
  1308. lp->rx_skbuff = NULL;
  1309. kfree(lp->tx_dma_addr);
  1310. lp->tx_dma_addr = NULL;
  1311. kfree(lp->rx_dma_addr);
  1312. lp->rx_dma_addr = NULL;
  1313. if (lp->tx_ring) {
  1314. pci_free_consistent(lp->pci_dev, sizeof(struct pcnet32_tx_head) * lp->tx_ring_size,
  1315. lp->tx_ring, lp->tx_ring_dma_addr);
  1316. lp->tx_ring = NULL;
  1317. }
  1318. if (lp->rx_ring) {
  1319. pci_free_consistent(lp->pci_dev, sizeof(struct pcnet32_rx_head) * lp->rx_ring_size,
  1320. lp->rx_ring, lp->rx_ring_dma_addr);
  1321. lp->rx_ring = NULL;
  1322. }
  1323. }
  1324. static int
  1325. pcnet32_open(struct net_device *dev)
  1326. {
  1327. struct pcnet32_private *lp = dev->priv;
  1328. unsigned long ioaddr = dev->base_addr;
  1329. u16 val;
  1330. int i;
  1331. int rc;
  1332. unsigned long flags;
  1333. if (request_irq(dev->irq, &pcnet32_interrupt,
  1334. lp->shared_irq ? SA_SHIRQ : 0, dev->name, (void *)dev)) {
  1335. return -EAGAIN;
  1336. }
  1337. spin_lock_irqsave(&lp->lock, flags);
  1338. /* Check for a valid station address */
  1339. if (!is_valid_ether_addr(dev->dev_addr)) {
  1340. rc = -EINVAL;
  1341. goto err_free_irq;
  1342. }
  1343. /* Reset the PCNET32 */
  1344. lp->a.reset (ioaddr);
  1345. /* switch pcnet32 to 32bit mode */
  1346. lp->a.write_bcr (ioaddr, 20, 2);
  1347. if (netif_msg_ifup(lp))
  1348. printk(KERN_DEBUG "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
  1349. dev->name, dev->irq,
  1350. (u32) (lp->tx_ring_dma_addr),
  1351. (u32) (lp->rx_ring_dma_addr),
  1352. (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)));
  1353. /* set/reset autoselect bit */
  1354. val = lp->a.read_bcr (ioaddr, 2) & ~2;
  1355. if (lp->options & PCNET32_PORT_ASEL)
  1356. val |= 2;
  1357. lp->a.write_bcr (ioaddr, 2, val);
  1358. /* handle full duplex setting */
  1359. if (lp->mii_if.full_duplex) {
  1360. val = lp->a.read_bcr (ioaddr, 9) & ~3;
  1361. if (lp->options & PCNET32_PORT_FD) {
  1362. val |= 1;
  1363. if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
  1364. val |= 2;
  1365. } else if (lp->options & PCNET32_PORT_ASEL) {
  1366. /* workaround of xSeries250, turn on for 79C975 only */
  1367. i = ((lp->a.read_csr(ioaddr, 88) |
  1368. (lp->a.read_csr(ioaddr,89) << 16)) >> 12) & 0xffff;
  1369. if (i == 0x2627)
  1370. val |= 3;
  1371. }
  1372. lp->a.write_bcr (ioaddr, 9, val);
  1373. }
  1374. /* set/reset GPSI bit in test register */
  1375. val = lp->a.read_csr (ioaddr, 124) & ~0x10;
  1376. if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
  1377. val |= 0x10;
  1378. lp->a.write_csr (ioaddr, 124, val);
  1379. /* Allied Telesyn AT 2700/2701 FX looses the link, so skip that */
  1380. if (lp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_AT &&
  1381. (lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2700FX ||
  1382. lp->pci_dev->subsystem_device == PCI_SUBDEVICE_ID_AT_2701FX)) {
  1383. printk(KERN_DEBUG "%s: Skipping PHY selection.\n", dev->name);
  1384. } else {
  1385. /*
  1386. * 24 Jun 2004 according AMD, in order to change the PHY,
  1387. * DANAS (or DISPM for 79C976) must be set; then select the speed,
  1388. * duplex, and/or enable auto negotiation, and clear DANAS
  1389. */
  1390. if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
  1391. lp->a.write_bcr(ioaddr, 32,
  1392. lp->a.read_bcr(ioaddr, 32) | 0x0080);
  1393. /* disable Auto Negotiation, set 10Mpbs, HD */
  1394. val = lp->a.read_bcr(ioaddr, 32) & ~0xb8;
  1395. if (lp->options & PCNET32_PORT_FD)
  1396. val |= 0x10;
  1397. if (lp->options & PCNET32_PORT_100)
  1398. val |= 0x08;
  1399. lp->a.write_bcr (ioaddr, 32, val);
  1400. } else {
  1401. if (lp->options & PCNET32_PORT_ASEL) {
  1402. lp->a.write_bcr(ioaddr, 32,
  1403. lp->a.read_bcr(ioaddr, 32) | 0x0080);
  1404. /* enable auto negotiate, setup, disable fd */
  1405. val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
  1406. val |= 0x20;
  1407. lp->a.write_bcr(ioaddr, 32, val);
  1408. }
  1409. }
  1410. }
  1411. #ifdef DO_DXSUFLO
  1412. if (lp->dxsuflo) { /* Disable transmit stop on underflow */
  1413. val = lp->a.read_csr (ioaddr, 3);
  1414. val |= 0x40;
  1415. lp->a.write_csr (ioaddr, 3, val);
  1416. }
  1417. #endif
  1418. lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  1419. pcnet32_load_multicast(dev);
  1420. if (pcnet32_init_ring(dev)) {
  1421. rc = -ENOMEM;
  1422. goto err_free_ring;
  1423. }
  1424. /* Re-initialize the PCNET32, and start it when done. */
  1425. lp->a.write_csr (ioaddr, 1, (lp->dma_addr +
  1426. offsetof(struct pcnet32_private, init_block)) & 0xffff);
  1427. lp->a.write_csr (ioaddr, 2, (lp->dma_addr +
  1428. offsetof(struct pcnet32_private, init_block)) >> 16);
  1429. lp->a.write_csr (ioaddr, 4, 0x0915);
  1430. lp->a.write_csr (ioaddr, 0, 0x0001);
  1431. netif_start_queue(dev);
  1432. /* If we have mii, print the link status and start the watchdog */
  1433. if (lp->mii) {
  1434. mii_check_media (&lp->mii_if, netif_msg_link(lp), 1);
  1435. mod_timer (&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
  1436. }
  1437. i = 0;
  1438. while (i++ < 100)
  1439. if (lp->a.read_csr (ioaddr, 0) & 0x0100)
  1440. break;
  1441. /*
  1442. * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
  1443. * reports that doing so triggers a bug in the '974.
  1444. */
  1445. lp->a.write_csr (ioaddr, 0, 0x0042);
  1446. if (netif_msg_ifup(lp))
  1447. printk(KERN_DEBUG "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
  1448. dev->name, i, (u32) (lp->dma_addr +
  1449. offsetof(struct pcnet32_private, init_block)),
  1450. lp->a.read_csr(ioaddr, 0));
  1451. spin_unlock_irqrestore(&lp->lock, flags);
  1452. return 0; /* Always succeed */
  1453. err_free_ring:
  1454. /* free any allocated skbuffs */
  1455. for (i = 0; i < lp->rx_ring_size; i++) {
  1456. lp->rx_ring[i].status = 0;
  1457. if (lp->rx_skbuff[i]) {
  1458. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i], PKT_BUF_SZ-2,
  1459. PCI_DMA_FROMDEVICE);
  1460. dev_kfree_skb(lp->rx_skbuff[i]);
  1461. }
  1462. lp->rx_skbuff[i] = NULL;
  1463. lp->rx_dma_addr[i] = 0;
  1464. }
  1465. pcnet32_free_ring(dev);
  1466. /*
  1467. * Switch back to 16bit mode to avoid problems with dumb
  1468. * DOS packet driver after a warm reboot
  1469. */
  1470. lp->a.write_bcr (ioaddr, 20, 4);
  1471. err_free_irq:
  1472. spin_unlock_irqrestore(&lp->lock, flags);
  1473. free_irq(dev->irq, dev);
  1474. return rc;
  1475. }
  1476. /*
  1477. * The LANCE has been halted for one reason or another (busmaster memory
  1478. * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
  1479. * etc.). Modern LANCE variants always reload their ring-buffer
  1480. * configuration when restarted, so we must reinitialize our ring
  1481. * context before restarting. As part of this reinitialization,
  1482. * find all packets still on the Tx ring and pretend that they had been
  1483. * sent (in effect, drop the packets on the floor) - the higher-level
  1484. * protocols will time out and retransmit. It'd be better to shuffle
  1485. * these skbs to a temp list and then actually re-Tx them after
  1486. * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
  1487. */
  1488. static void
  1489. pcnet32_purge_tx_ring(struct net_device *dev)
  1490. {
  1491. struct pcnet32_private *lp = dev->priv;
  1492. int i;
  1493. for (i = 0; i < lp->tx_ring_size; i++) {
  1494. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1495. wmb(); /* Make sure adapter sees owner change */
  1496. if (lp->tx_skbuff[i]) {
  1497. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
  1498. lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
  1499. dev_kfree_skb_any(lp->tx_skbuff[i]);
  1500. }
  1501. lp->tx_skbuff[i] = NULL;
  1502. lp->tx_dma_addr[i] = 0;
  1503. }
  1504. }
  1505. /* Initialize the PCNET32 Rx and Tx rings. */
  1506. static int
  1507. pcnet32_init_ring(struct net_device *dev)
  1508. {
  1509. struct pcnet32_private *lp = dev->priv;
  1510. int i;
  1511. lp->tx_full = 0;
  1512. lp->cur_rx = lp->cur_tx = 0;
  1513. lp->dirty_rx = lp->dirty_tx = 0;
  1514. for (i = 0; i < lp->rx_ring_size; i++) {
  1515. struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
  1516. if (rx_skbuff == NULL) {
  1517. if (!(rx_skbuff = lp->rx_skbuff[i] = dev_alloc_skb (PKT_BUF_SZ))) {
  1518. /* there is not much, we can do at this point */
  1519. if (pcnet32_debug & NETIF_MSG_DRV)
  1520. printk(KERN_ERR "%s: pcnet32_init_ring dev_alloc_skb failed.\n",
  1521. dev->name);
  1522. return -1;
  1523. }
  1524. skb_reserve (rx_skbuff, 2);
  1525. }
  1526. rmb();
  1527. if (lp->rx_dma_addr[i] == 0)
  1528. lp->rx_dma_addr[i] = pci_map_single(lp->pci_dev, rx_skbuff->data,
  1529. PKT_BUF_SZ-2, PCI_DMA_FROMDEVICE);
  1530. lp->rx_ring[i].base = (u32)le32_to_cpu(lp->rx_dma_addr[i]);
  1531. lp->rx_ring[i].buf_length = le16_to_cpu(2-PKT_BUF_SZ);
  1532. wmb(); /* Make sure owner changes after all others are visible */
  1533. lp->rx_ring[i].status = le16_to_cpu(0x8000);
  1534. }
  1535. /* The Tx buffer address is filled in as needed, but we do need to clear
  1536. * the upper ownership bit. */
  1537. for (i = 0; i < lp->tx_ring_size; i++) {
  1538. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1539. wmb(); /* Make sure adapter sees owner change */
  1540. lp->tx_ring[i].base = 0;
  1541. lp->tx_dma_addr[i] = 0;
  1542. }
  1543. lp->init_block.tlen_rlen = le16_to_cpu(lp->tx_len_bits | lp->rx_len_bits);
  1544. for (i = 0; i < 6; i++)
  1545. lp->init_block.phys_addr[i] = dev->dev_addr[i];
  1546. lp->init_block.rx_ring = (u32)le32_to_cpu(lp->rx_ring_dma_addr);
  1547. lp->init_block.tx_ring = (u32)le32_to_cpu(lp->tx_ring_dma_addr);
  1548. wmb(); /* Make sure all changes are visible */
  1549. return 0;
  1550. }
  1551. /* the pcnet32 has been issued a stop or reset. Wait for the stop bit
  1552. * then flush the pending transmit operations, re-initialize the ring,
  1553. * and tell the chip to initialize.
  1554. */
  1555. static void
  1556. pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
  1557. {
  1558. struct pcnet32_private *lp = dev->priv;
  1559. unsigned long ioaddr = dev->base_addr;
  1560. int i;
  1561. /* wait for stop */
  1562. for (i=0; i<100; i++)
  1563. if (lp->a.read_csr(ioaddr, 0) & 0x0004)
  1564. break;
  1565. if (i >= 100 && netif_msg_drv(lp))
  1566. printk(KERN_ERR "%s: pcnet32_restart timed out waiting for stop.\n",
  1567. dev->name);
  1568. pcnet32_purge_tx_ring(dev);
  1569. if (pcnet32_init_ring(dev))
  1570. return;
  1571. /* ReInit Ring */
  1572. lp->a.write_csr (ioaddr, 0, 1);
  1573. i = 0;
  1574. while (i++ < 1000)
  1575. if (lp->a.read_csr (ioaddr, 0) & 0x0100)
  1576. break;
  1577. lp->a.write_csr (ioaddr, 0, csr0_bits);
  1578. }
  1579. static void
  1580. pcnet32_tx_timeout (struct net_device *dev)
  1581. {
  1582. struct pcnet32_private *lp = dev->priv;
  1583. unsigned long ioaddr = dev->base_addr, flags;
  1584. spin_lock_irqsave(&lp->lock, flags);
  1585. /* Transmitter timeout, serious problems. */
  1586. if (pcnet32_debug & NETIF_MSG_DRV)
  1587. printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
  1588. dev->name, lp->a.read_csr(ioaddr, 0));
  1589. lp->a.write_csr (ioaddr, 0, 0x0004);
  1590. lp->stats.tx_errors++;
  1591. if (netif_msg_tx_err(lp)) {
  1592. int i;
  1593. printk(KERN_DEBUG " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
  1594. lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
  1595. lp->cur_rx);
  1596. for (i = 0 ; i < lp->rx_ring_size; i++)
  1597. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  1598. le32_to_cpu(lp->rx_ring[i].base),
  1599. (-le16_to_cpu(lp->rx_ring[i].buf_length)) & 0xffff,
  1600. le32_to_cpu(lp->rx_ring[i].msg_length),
  1601. le16_to_cpu(lp->rx_ring[i].status));
  1602. for (i = 0 ; i < lp->tx_ring_size; i++)
  1603. printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
  1604. le32_to_cpu(lp->tx_ring[i].base),
  1605. (-le16_to_cpu(lp->tx_ring[i].length)) & 0xffff,
  1606. le32_to_cpu(lp->tx_ring[i].misc),
  1607. le16_to_cpu(lp->tx_ring[i].status));
  1608. printk("\n");
  1609. }
  1610. pcnet32_restart(dev, 0x0042);
  1611. dev->trans_start = jiffies;
  1612. netif_wake_queue(dev);
  1613. spin_unlock_irqrestore(&lp->lock, flags);
  1614. }
  1615. static int
  1616. pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1617. {
  1618. struct pcnet32_private *lp = dev->priv;
  1619. unsigned long ioaddr = dev->base_addr;
  1620. u16 status;
  1621. int entry;
  1622. unsigned long flags;
  1623. spin_lock_irqsave(&lp->lock, flags);
  1624. if (netif_msg_tx_queued(lp)) {
  1625. printk(KERN_DEBUG "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
  1626. dev->name, lp->a.read_csr(ioaddr, 0));
  1627. }
  1628. /* Default status -- will not enable Successful-TxDone
  1629. * interrupt when that option is available to us.
  1630. */
  1631. status = 0x8300;
  1632. /* Fill in a Tx ring entry */
  1633. /* Mask to ring buffer boundary. */
  1634. entry = lp->cur_tx & lp->tx_mod_mask;
  1635. /* Caution: the write order is important here, set the status
  1636. * with the "ownership" bits last. */
  1637. lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
  1638. lp->tx_ring[entry].misc = 0x00000000;
  1639. lp->tx_skbuff[entry] = skb;
  1640. lp->tx_dma_addr[entry] = pci_map_single(lp->pci_dev, skb->data, skb->len,
  1641. PCI_DMA_TODEVICE);
  1642. lp->tx_ring[entry].base = (u32)le32_to_cpu(lp->tx_dma_addr[entry]);
  1643. wmb(); /* Make sure owner changes after all others are visible */
  1644. lp->tx_ring[entry].status = le16_to_cpu(status);
  1645. lp->cur_tx++;
  1646. lp->stats.tx_bytes += skb->len;
  1647. /* Trigger an immediate send poll. */
  1648. lp->a.write_csr (ioaddr, 0, 0x0048);
  1649. dev->trans_start = jiffies;
  1650. if (lp->tx_ring[(entry+1) & lp->tx_mod_mask].base != 0) {
  1651. lp->tx_full = 1;
  1652. netif_stop_queue(dev);
  1653. }
  1654. spin_unlock_irqrestore(&lp->lock, flags);
  1655. return 0;
  1656. }
  1657. /* The PCNET32 interrupt handler. */
  1658. static irqreturn_t
  1659. pcnet32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1660. {
  1661. struct net_device *dev = dev_id;
  1662. struct pcnet32_private *lp;
  1663. unsigned long ioaddr;
  1664. u16 csr0,rap;
  1665. int boguscnt = max_interrupt_work;
  1666. int must_restart;
  1667. if (!dev) {
  1668. if (pcnet32_debug & NETIF_MSG_INTR)
  1669. printk (KERN_DEBUG "%s(): irq %d for unknown device\n",
  1670. __FUNCTION__, irq);
  1671. return IRQ_NONE;
  1672. }
  1673. ioaddr = dev->base_addr;
  1674. lp = dev->priv;
  1675. spin_lock(&lp->lock);
  1676. rap = lp->a.read_rap(ioaddr);
  1677. while ((csr0 = lp->a.read_csr (ioaddr, 0)) & 0x8f00 && --boguscnt >= 0) {
  1678. if (csr0 == 0xffff) {
  1679. break; /* PCMCIA remove happened */
  1680. }
  1681. /* Acknowledge all of the current interrupt sources ASAP. */
  1682. lp->a.write_csr (ioaddr, 0, csr0 & ~0x004f);
  1683. must_restart = 0;
  1684. if (netif_msg_intr(lp))
  1685. printk(KERN_DEBUG "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
  1686. dev->name, csr0, lp->a.read_csr (ioaddr, 0));
  1687. if (csr0 & 0x0400) /* Rx interrupt */
  1688. pcnet32_rx(dev);
  1689. if (csr0 & 0x0200) { /* Tx-done interrupt */
  1690. unsigned int dirty_tx = lp->dirty_tx;
  1691. int delta;
  1692. while (dirty_tx != lp->cur_tx) {
  1693. int entry = dirty_tx & lp->tx_mod_mask;
  1694. int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
  1695. if (status < 0)
  1696. break; /* It still hasn't been Txed */
  1697. lp->tx_ring[entry].base = 0;
  1698. if (status & 0x4000) {
  1699. /* There was an major error, log it. */
  1700. int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
  1701. lp->stats.tx_errors++;
  1702. if (netif_msg_tx_err(lp))
  1703. printk(KERN_ERR "%s: Tx error status=%04x err_status=%08x\n",
  1704. dev->name, status, err_status);
  1705. if (err_status & 0x04000000) lp->stats.tx_aborted_errors++;
  1706. if (err_status & 0x08000000) lp->stats.tx_carrier_errors++;
  1707. if (err_status & 0x10000000) lp->stats.tx_window_errors++;
  1708. #ifndef DO_DXSUFLO
  1709. if (err_status & 0x40000000) {
  1710. lp->stats.tx_fifo_errors++;
  1711. /* Ackk! On FIFO errors the Tx unit is turned off! */
  1712. /* Remove this verbosity later! */
  1713. if (netif_msg_tx_err(lp))
  1714. printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
  1715. dev->name, csr0);
  1716. must_restart = 1;
  1717. }
  1718. #else
  1719. if (err_status & 0x40000000) {
  1720. lp->stats.tx_fifo_errors++;
  1721. if (! lp->dxsuflo) { /* If controller doesn't recover ... */
  1722. /* Ackk! On FIFO errors the Tx unit is turned off! */
  1723. /* Remove this verbosity later! */
  1724. if (netif_msg_tx_err(lp))
  1725. printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
  1726. dev->name, csr0);
  1727. must_restart = 1;
  1728. }
  1729. }
  1730. #endif
  1731. } else {
  1732. if (status & 0x1800)
  1733. lp->stats.collisions++;
  1734. lp->stats.tx_packets++;
  1735. }
  1736. /* We must free the original skb */
  1737. if (lp->tx_skbuff[entry]) {
  1738. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[entry],
  1739. lp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
  1740. dev_kfree_skb_irq(lp->tx_skbuff[entry]);
  1741. lp->tx_skbuff[entry] = NULL;
  1742. lp->tx_dma_addr[entry] = 0;
  1743. }
  1744. dirty_tx++;
  1745. }
  1746. delta = (lp->cur_tx - dirty_tx) & (lp->tx_mod_mask + lp->tx_ring_size);
  1747. if (delta > lp->tx_ring_size) {
  1748. if (netif_msg_drv(lp))
  1749. printk(KERN_ERR "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
  1750. dev->name, dirty_tx, lp->cur_tx, lp->tx_full);
  1751. dirty_tx += lp->tx_ring_size;
  1752. delta -= lp->tx_ring_size;
  1753. }
  1754. if (lp->tx_full &&
  1755. netif_queue_stopped(dev) &&
  1756. delta < lp->tx_ring_size - 2) {
  1757. /* The ring is no longer full, clear tbusy. */
  1758. lp->tx_full = 0;
  1759. netif_wake_queue (dev);
  1760. }
  1761. lp->dirty_tx = dirty_tx;
  1762. }
  1763. /* Log misc errors. */
  1764. if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
  1765. if (csr0 & 0x1000) {
  1766. /*
  1767. * this happens when our receive ring is full. This shouldn't
  1768. * be a problem as we will see normal rx interrupts for the frames
  1769. * in the receive ring. But there are some PCI chipsets (I can
  1770. * reproduce this on SP3G with Intel saturn chipset) which have
  1771. * sometimes problems and will fill up the receive ring with
  1772. * error descriptors. In this situation we don't get a rx
  1773. * interrupt, but a missed frame interrupt sooner or later.
  1774. * So we try to clean up our receive ring here.
  1775. */
  1776. pcnet32_rx(dev);
  1777. lp->stats.rx_errors++; /* Missed a Rx frame. */
  1778. }
  1779. if (csr0 & 0x0800) {
  1780. if (netif_msg_drv(lp))
  1781. printk(KERN_ERR "%s: Bus master arbitration failure, status %4.4x.\n",
  1782. dev->name, csr0);
  1783. /* unlike for the lance, there is no restart needed */
  1784. }
  1785. if (must_restart) {
  1786. /* reset the chip to clear the error condition, then restart */
  1787. lp->a.reset(ioaddr);
  1788. lp->a.write_csr(ioaddr, 4, 0x0915);
  1789. pcnet32_restart(dev, 0x0002);
  1790. netif_wake_queue(dev);
  1791. }
  1792. }
  1793. /* Set interrupt enable. */
  1794. lp->a.write_csr (ioaddr, 0, 0x0040);
  1795. lp->a.write_rap (ioaddr,rap);
  1796. if (netif_msg_intr(lp))
  1797. printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
  1798. dev->name, lp->a.read_csr (ioaddr, 0));
  1799. spin_unlock(&lp->lock);
  1800. return IRQ_HANDLED;
  1801. }
  1802. static int
  1803. pcnet32_rx(struct net_device *dev)
  1804. {
  1805. struct pcnet32_private *lp = dev->priv;
  1806. int entry = lp->cur_rx & lp->rx_mod_mask;
  1807. int boguscnt = lp->rx_ring_size / 2;
  1808. /* If we own the next entry, it's a new packet. Send it up. */
  1809. while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
  1810. int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
  1811. if (status != 0x03) { /* There was an error. */
  1812. /*
  1813. * There is a tricky error noted by John Murphy,
  1814. * <murf@perftech.com> to Russ Nelson: Even with full-sized
  1815. * buffers it's possible for a jabber packet to use two
  1816. * buffers, with only the last correctly noting the error.
  1817. */
  1818. if (status & 0x01) /* Only count a general error at the */
  1819. lp->stats.rx_errors++; /* end of a packet.*/
  1820. if (status & 0x20) lp->stats.rx_frame_errors++;
  1821. if (status & 0x10) lp->stats.rx_over_errors++;
  1822. if (status & 0x08) lp->stats.rx_crc_errors++;
  1823. if (status & 0x04) lp->stats.rx_fifo_errors++;
  1824. lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
  1825. } else {
  1826. /* Malloc up new buffer, compatible with net-2e. */
  1827. short pkt_len = (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)-4;
  1828. struct sk_buff *skb;
  1829. /* Discard oversize frames. */
  1830. if (unlikely(pkt_len > PKT_BUF_SZ - 2)) {
  1831. if (netif_msg_drv(lp))
  1832. printk(KERN_ERR "%s: Impossible packet size %d!\n",
  1833. dev->name, pkt_len);
  1834. lp->stats.rx_errors++;
  1835. } else if (pkt_len < 60) {
  1836. if (netif_msg_rx_err(lp))
  1837. printk(KERN_ERR "%s: Runt packet!\n", dev->name);
  1838. lp->stats.rx_errors++;
  1839. } else {
  1840. int rx_in_place = 0;
  1841. if (pkt_len > rx_copybreak) {
  1842. struct sk_buff *newskb;
  1843. if ((newskb = dev_alloc_skb(PKT_BUF_SZ))) {
  1844. skb_reserve (newskb, 2);
  1845. skb = lp->rx_skbuff[entry];
  1846. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[entry],
  1847. PKT_BUF_SZ-2, PCI_DMA_FROMDEVICE);
  1848. skb_put (skb, pkt_len);
  1849. lp->rx_skbuff[entry] = newskb;
  1850. newskb->dev = dev;
  1851. lp->rx_dma_addr[entry] =
  1852. pci_map_single(lp->pci_dev, newskb->data,
  1853. PKT_BUF_SZ-2, PCI_DMA_FROMDEVICE);
  1854. lp->rx_ring[entry].base = le32_to_cpu(lp->rx_dma_addr[entry]);
  1855. rx_in_place = 1;
  1856. } else
  1857. skb = NULL;
  1858. } else {
  1859. skb = dev_alloc_skb(pkt_len+2);
  1860. }
  1861. if (skb == NULL) {
  1862. int i;
  1863. if (netif_msg_drv(lp))
  1864. printk(KERN_ERR "%s: Memory squeeze, deferring packet.\n",
  1865. dev->name);
  1866. for (i = 0; i < lp->rx_ring_size; i++)
  1867. if ((short)le16_to_cpu(lp->rx_ring[(entry+i)
  1868. & lp->rx_mod_mask].status) < 0)
  1869. break;
  1870. if (i > lp->rx_ring_size -2) {
  1871. lp->stats.rx_dropped++;
  1872. lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
  1873. wmb(); /* Make sure adapter sees owner change */
  1874. lp->cur_rx++;
  1875. }
  1876. break;
  1877. }
  1878. skb->dev = dev;
  1879. if (!rx_in_place) {
  1880. skb_reserve(skb,2); /* 16 byte align */
  1881. skb_put(skb,pkt_len); /* Make room */
  1882. pci_dma_sync_single_for_cpu(lp->pci_dev,
  1883. lp->rx_dma_addr[entry],
  1884. PKT_BUF_SZ-2,
  1885. PCI_DMA_FROMDEVICE);
  1886. eth_copy_and_sum(skb,
  1887. (unsigned char *)(lp->rx_skbuff[entry]->data),
  1888. pkt_len,0);
  1889. pci_dma_sync_single_for_device(lp->pci_dev,
  1890. lp->rx_dma_addr[entry],
  1891. PKT_BUF_SZ-2,
  1892. PCI_DMA_FROMDEVICE);
  1893. }
  1894. lp->stats.rx_bytes += skb->len;
  1895. skb->protocol=eth_type_trans(skb,dev);
  1896. netif_rx(skb);
  1897. dev->last_rx = jiffies;
  1898. lp->stats.rx_packets++;
  1899. }
  1900. }
  1901. /*
  1902. * The docs say that the buffer length isn't touched, but Andrew Boyd
  1903. * of QNX reports that some revs of the 79C965 clear it.
  1904. */
  1905. lp->rx_ring[entry].buf_length = le16_to_cpu(2-PKT_BUF_SZ);
  1906. wmb(); /* Make sure owner changes after all others are visible */
  1907. lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
  1908. entry = (++lp->cur_rx) & lp->rx_mod_mask;
  1909. if (--boguscnt <= 0) break; /* don't stay in loop forever */
  1910. }
  1911. return 0;
  1912. }
  1913. static int
  1914. pcnet32_close(struct net_device *dev)
  1915. {
  1916. unsigned long ioaddr = dev->base_addr;
  1917. struct pcnet32_private *lp = dev->priv;
  1918. int i;
  1919. unsigned long flags;
  1920. del_timer_sync(&lp->watchdog_timer);
  1921. netif_stop_queue(dev);
  1922. spin_lock_irqsave(&lp->lock, flags);
  1923. lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
  1924. if (netif_msg_ifdown(lp))
  1925. printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
  1926. dev->name, lp->a.read_csr (ioaddr, 0));
  1927. /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
  1928. lp->a.write_csr (ioaddr, 0, 0x0004);
  1929. /*
  1930. * Switch back to 16bit mode to avoid problems with dumb
  1931. * DOS packet driver after a warm reboot
  1932. */
  1933. lp->a.write_bcr (ioaddr, 20, 4);
  1934. spin_unlock_irqrestore(&lp->lock, flags);
  1935. free_irq(dev->irq, dev);
  1936. spin_lock_irqsave(&lp->lock, flags);
  1937. /* free all allocated skbuffs */
  1938. for (i = 0; i < lp->rx_ring_size; i++) {
  1939. lp->rx_ring[i].status = 0;
  1940. wmb(); /* Make sure adapter sees owner change */
  1941. if (lp->rx_skbuff[i]) {
  1942. pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i], PKT_BUF_SZ-2,
  1943. PCI_DMA_FROMDEVICE);
  1944. dev_kfree_skb(lp->rx_skbuff[i]);
  1945. }
  1946. lp->rx_skbuff[i] = NULL;
  1947. lp->rx_dma_addr[i] = 0;
  1948. }
  1949. for (i = 0; i < lp->tx_ring_size; i++) {
  1950. lp->tx_ring[i].status = 0; /* CPU owns buffer */
  1951. wmb(); /* Make sure adapter sees owner change */
  1952. if (lp->tx_skbuff[i]) {
  1953. pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i],
  1954. lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
  1955. dev_kfree_skb(lp->tx_skbuff[i]);
  1956. }
  1957. lp->tx_skbuff[i] = NULL;
  1958. lp->tx_dma_addr[i] = 0;
  1959. }
  1960. spin_unlock_irqrestore(&lp->lock, flags);
  1961. return 0;
  1962. }
  1963. static struct net_device_stats *
  1964. pcnet32_get_stats(struct net_device *dev)
  1965. {
  1966. struct pcnet32_private *lp = dev->priv;
  1967. unsigned long ioaddr = dev->base_addr;
  1968. u16 saved_addr;
  1969. unsigned long flags;
  1970. spin_lock_irqsave(&lp->lock, flags);
  1971. saved_addr = lp->a.read_rap(ioaddr);
  1972. lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
  1973. lp->a.write_rap(ioaddr, saved_addr);
  1974. spin_unlock_irqrestore(&lp->lock, flags);
  1975. return &lp->stats;
  1976. }
  1977. /* taken from the sunlance driver, which it took from the depca driver */
  1978. static void pcnet32_load_multicast (struct net_device *dev)
  1979. {
  1980. struct pcnet32_private *lp = dev->priv;
  1981. volatile struct pcnet32_init_block *ib = &lp->init_block;
  1982. volatile u16 *mcast_table = (u16 *)&ib->filter;
  1983. struct dev_mc_list *dmi=dev->mc_list;
  1984. char *addrs;
  1985. int i;
  1986. u32 crc;
  1987. /* set all multicast bits */
  1988. if (dev->flags & IFF_ALLMULTI) {
  1989. ib->filter[0] = 0xffffffff;
  1990. ib->filter[1] = 0xffffffff;
  1991. return;
  1992. }
  1993. /* clear the multicast filter */
  1994. ib->filter[0] = 0;
  1995. ib->filter[1] = 0;
  1996. /* Add addresses */
  1997. for (i = 0; i < dev->mc_count; i++) {
  1998. addrs = dmi->dmi_addr;
  1999. dmi = dmi->next;
  2000. /* multicast address? */
  2001. if (!(*addrs & 1))
  2002. continue;
  2003. crc = ether_crc_le(6, addrs);
  2004. crc = crc >> 26;
  2005. mcast_table [crc >> 4] = le16_to_cpu(
  2006. le16_to_cpu(mcast_table [crc >> 4]) | (1 << (crc & 0xf)));
  2007. }
  2008. return;
  2009. }
  2010. /*
  2011. * Set or clear the multicast filter for this adaptor.
  2012. */
  2013. static void pcnet32_set_multicast_list(struct net_device *dev)
  2014. {
  2015. unsigned long ioaddr = dev->base_addr, flags;
  2016. struct pcnet32_private *lp = dev->priv;
  2017. spin_lock_irqsave(&lp->lock, flags);
  2018. if (dev->flags&IFF_PROMISC) {
  2019. /* Log any net taps. */
  2020. if (netif_msg_hw(lp))
  2021. printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
  2022. lp->init_block.mode = le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) << 7);
  2023. } else {
  2024. lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
  2025. pcnet32_load_multicast (dev);
  2026. }
  2027. lp->a.write_csr (ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
  2028. pcnet32_restart(dev, 0x0042); /* Resume normal operation */
  2029. netif_wake_queue(dev);
  2030. spin_unlock_irqrestore(&lp->lock, flags);
  2031. }
  2032. /* This routine assumes that the lp->lock is held */
  2033. static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
  2034. {
  2035. struct pcnet32_private *lp = dev->priv;
  2036. unsigned long ioaddr = dev->base_addr;
  2037. u16 val_out;
  2038. if (!lp->mii)
  2039. return 0;
  2040. lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2041. val_out = lp->a.read_bcr(ioaddr, 34);
  2042. return val_out;
  2043. }
  2044. /* This routine assumes that the lp->lock is held */
  2045. static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
  2046. {
  2047. struct pcnet32_private *lp = dev->priv;
  2048. unsigned long ioaddr = dev->base_addr;
  2049. if (!lp->mii)
  2050. return;
  2051. lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
  2052. lp->a.write_bcr(ioaddr, 34, val);
  2053. }
  2054. static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2055. {
  2056. struct pcnet32_private *lp = dev->priv;
  2057. int rc;
  2058. unsigned long flags;
  2059. /* SIOC[GS]MIIxxx ioctls */
  2060. if (lp->mii) {
  2061. spin_lock_irqsave(&lp->lock, flags);
  2062. rc = generic_mii_ioctl(&lp->mii_if, if_mii(rq), cmd, NULL);
  2063. spin_unlock_irqrestore(&lp->lock, flags);
  2064. } else {
  2065. rc = -EOPNOTSUPP;
  2066. }
  2067. return rc;
  2068. }
  2069. static void pcnet32_watchdog(struct net_device *dev)
  2070. {
  2071. struct pcnet32_private *lp = dev->priv;
  2072. unsigned long flags;
  2073. /* Print the link status if it has changed */
  2074. if (lp->mii) {
  2075. spin_lock_irqsave(&lp->lock, flags);
  2076. mii_check_media (&lp->mii_if, netif_msg_link(lp), 0);
  2077. spin_unlock_irqrestore(&lp->lock, flags);
  2078. }
  2079. mod_timer (&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
  2080. }
  2081. static void __devexit pcnet32_remove_one(struct pci_dev *pdev)
  2082. {
  2083. struct net_device *dev = pci_get_drvdata(pdev);
  2084. if (dev) {
  2085. struct pcnet32_private *lp = dev->priv;
  2086. unregister_netdev(dev);
  2087. pcnet32_free_ring(dev);
  2088. release_region(dev->base_addr, PCNET32_TOTAL_SIZE);
  2089. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  2090. free_netdev(dev);
  2091. pci_disable_device(pdev);
  2092. pci_set_drvdata(pdev, NULL);
  2093. }
  2094. }
  2095. static struct pci_driver pcnet32_driver = {
  2096. .name = DRV_NAME,
  2097. .probe = pcnet32_probe_pci,
  2098. .remove = __devexit_p(pcnet32_remove_one),
  2099. .id_table = pcnet32_pci_tbl,
  2100. };
  2101. /* An additional parameter that may be passed in... */
  2102. static int debug = -1;
  2103. static int tx_start_pt = -1;
  2104. static int pcnet32_have_pci;
  2105. module_param(debug, int, 0);
  2106. MODULE_PARM_DESC(debug, DRV_NAME " debug level");
  2107. module_param(max_interrupt_work, int, 0);
  2108. MODULE_PARM_DESC(max_interrupt_work, DRV_NAME " maximum events handled per interrupt");
  2109. module_param(rx_copybreak, int, 0);
  2110. MODULE_PARM_DESC(rx_copybreak, DRV_NAME " copy breakpoint for copy-only-tiny-frames");
  2111. module_param(tx_start_pt, int, 0);
  2112. MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
  2113. module_param(pcnet32vlb, int, 0);
  2114. MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
  2115. module_param_array(options, int, NULL, 0);
  2116. MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
  2117. module_param_array(full_duplex, int, NULL, 0);
  2118. MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
  2119. /* Module Parameter for HomePNA cards added by Patrick Simmons, 2004 */
  2120. module_param_array(homepna, int, NULL, 0);
  2121. MODULE_PARM_DESC(homepna, DRV_NAME " mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet");
  2122. MODULE_AUTHOR("Thomas Bogendoerfer");
  2123. MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
  2124. MODULE_LICENSE("GPL");
  2125. #define PCNET32_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  2126. static int __init pcnet32_init_module(void)
  2127. {
  2128. printk(KERN_INFO "%s", version);
  2129. pcnet32_debug = netif_msg_init(debug, PCNET32_MSG_DEFAULT);
  2130. if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
  2131. tx_start = tx_start_pt;
  2132. /* find the PCI devices */
  2133. if (!pci_module_init(&pcnet32_driver))
  2134. pcnet32_have_pci = 1;
  2135. /* should we find any remaining VLbus devices ? */
  2136. if (pcnet32vlb)
  2137. pcnet32_probe_vlbus();
  2138. if (cards_found && (pcnet32_debug & NETIF_MSG_PROBE))
  2139. printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
  2140. return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
  2141. }
  2142. static void __exit pcnet32_cleanup_module(void)
  2143. {
  2144. struct net_device *next_dev;
  2145. while (pcnet32_dev) {
  2146. struct pcnet32_private *lp = pcnet32_dev->priv;
  2147. next_dev = lp->next;
  2148. unregister_netdev(pcnet32_dev);
  2149. pcnet32_free_ring(pcnet32_dev);
  2150. release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
  2151. pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
  2152. free_netdev(pcnet32_dev);
  2153. pcnet32_dev = next_dev;
  2154. }
  2155. if (pcnet32_have_pci)
  2156. pci_unregister_driver(&pcnet32_driver);
  2157. }
  2158. module_init(pcnet32_init_module);
  2159. module_exit(pcnet32_cleanup_module);
  2160. /*
  2161. * Local variables:
  2162. * c-indent-level: 4
  2163. * tab-width: 8
  2164. * End:
  2165. */