pcnet32.c 76 KB

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