pcnet32.c 77 KB

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