pcnet32.c 78 KB

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