pcnet32.c 72 KB

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