pcnet32.c 78 KB

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