pcnet32.c 83 KB

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