pcnet32.c 72 KB

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