via-rhine.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  1. /* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
  2. /*
  3. Written 1998-2001 by Donald Becker.
  4. Current Maintainer: Roger Luethi <rl@hellgate.ch>
  5. This software may be used and distributed according to the terms of
  6. the GNU General Public License (GPL), incorporated herein by reference.
  7. Drivers based on or derived from this code fall under the GPL and must
  8. retain the authorship, copyright and license notice. This file is not
  9. a complete program and may only be used when the entire operating
  10. system is licensed under the GPL.
  11. This driver is designed for the VIA VT86C100A Rhine-I.
  12. It also works with the Rhine-II (6102) and Rhine-III (6105/6105L/6105LOM
  13. and management NIC 6105M).
  14. The author may be reached as becker@scyld.com, or C/O
  15. Scyld Computing Corporation
  16. 410 Severn Ave., Suite 210
  17. Annapolis MD 21403
  18. This driver contains some changes from the original Donald Becker
  19. version. He may or may not be interested in bug reports on this
  20. code. You can find his versions at:
  21. http://www.scyld.com/network/via-rhine.html
  22. [link no longer provides useful info -jgarzik]
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #define DRV_NAME "via-rhine"
  26. #define DRV_VERSION "1.5.0"
  27. #define DRV_RELDATE "2010-10-09"
  28. /* A few user-configurable values.
  29. These may be modified when a driver module is loaded. */
  30. #define DEBUG
  31. static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
  32. static int max_interrupt_work = 20;
  33. /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
  34. Setting to > 1518 effectively disables this feature. */
  35. #if defined(__alpha__) || defined(__arm__) || defined(__hppa__) || \
  36. defined(CONFIG_SPARC) || defined(__ia64__) || \
  37. defined(__sh__) || defined(__mips__)
  38. static int rx_copybreak = 1518;
  39. #else
  40. static int rx_copybreak;
  41. #endif
  42. /* Work-around for broken BIOSes: they are unable to get the chip back out of
  43. power state D3 so PXE booting fails. bootparam(7): via-rhine.avoid_D3=1 */
  44. static int avoid_D3;
  45. /*
  46. * In case you are looking for 'options[]' or 'full_duplex[]', they
  47. * are gone. Use ethtool(8) instead.
  48. */
  49. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
  50. The Rhine has a 64 element 8390-like hash table. */
  51. static const int multicast_filter_limit = 32;
  52. /* Operational parameters that are set at compile time. */
  53. /* Keep the ring sizes a power of two for compile efficiency.
  54. The compiler will convert <unsigned>'%'<2^N> into a bit mask.
  55. Making the Tx ring too large decreases the effectiveness of channel
  56. bonding and packet priority.
  57. There are no ill effects from too-large receive rings. */
  58. #define TX_RING_SIZE 16
  59. #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
  60. #define RX_RING_SIZE 64
  61. /* Operational parameters that usually are not changed. */
  62. /* Time in jiffies before concluding the transmitter is hung. */
  63. #define TX_TIMEOUT (2*HZ)
  64. #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
  65. #include <linux/module.h>
  66. #include <linux/moduleparam.h>
  67. #include <linux/kernel.h>
  68. #include <linux/string.h>
  69. #include <linux/timer.h>
  70. #include <linux/errno.h>
  71. #include <linux/ioport.h>
  72. #include <linux/interrupt.h>
  73. #include <linux/pci.h>
  74. #include <linux/dma-mapping.h>
  75. #include <linux/netdevice.h>
  76. #include <linux/etherdevice.h>
  77. #include <linux/skbuff.h>
  78. #include <linux/init.h>
  79. #include <linux/delay.h>
  80. #include <linux/mii.h>
  81. #include <linux/ethtool.h>
  82. #include <linux/crc32.h>
  83. #include <linux/if_vlan.h>
  84. #include <linux/bitops.h>
  85. #include <linux/workqueue.h>
  86. #include <asm/processor.h> /* Processor type for cache alignment. */
  87. #include <asm/io.h>
  88. #include <asm/irq.h>
  89. #include <asm/uaccess.h>
  90. #include <linux/dmi.h>
  91. /* These identify the driver base version and may not be removed. */
  92. static const char version[] __devinitconst =
  93. "v1.10-LK" DRV_VERSION " " DRV_RELDATE " Written by Donald Becker";
  94. /* This driver was written to use PCI memory space. Some early versions
  95. of the Rhine may only work correctly with I/O space accesses. */
  96. #ifdef CONFIG_VIA_RHINE_MMIO
  97. #define USE_MMIO
  98. #else
  99. #endif
  100. MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
  101. MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
  102. MODULE_LICENSE("GPL");
  103. module_param(max_interrupt_work, int, 0);
  104. module_param(debug, int, 0);
  105. module_param(rx_copybreak, int, 0);
  106. module_param(avoid_D3, bool, 0);
  107. MODULE_PARM_DESC(max_interrupt_work, "VIA Rhine maximum events handled per interrupt");
  108. MODULE_PARM_DESC(debug, "VIA Rhine debug level (0-7)");
  109. MODULE_PARM_DESC(rx_copybreak, "VIA Rhine copy breakpoint for copy-only-tiny-frames");
  110. MODULE_PARM_DESC(avoid_D3, "Avoid power state D3 (work-around for broken BIOSes)");
  111. #define MCAM_SIZE 32
  112. #define VCAM_SIZE 32
  113. /*
  114. Theory of Operation
  115. I. Board Compatibility
  116. This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
  117. controller.
  118. II. Board-specific settings
  119. Boards with this chip are functional only in a bus-master PCI slot.
  120. Many operational settings are loaded from the EEPROM to the Config word at
  121. offset 0x78. For most of these settings, this driver assumes that they are
  122. correct.
  123. If this driver is compiled to use PCI memory space operations the EEPROM
  124. must be configured to enable memory ops.
  125. III. Driver operation
  126. IIIa. Ring buffers
  127. This driver uses two statically allocated fixed-size descriptor lists
  128. formed into rings by a branch from the final descriptor to the beginning of
  129. the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
  130. IIIb/c. Transmit/Receive Structure
  131. This driver attempts to use a zero-copy receive and transmit scheme.
  132. Alas, all data buffers are required to start on a 32 bit boundary, so
  133. the driver must often copy transmit packets into bounce buffers.
  134. The driver allocates full frame size skbuffs for the Rx ring buffers at
  135. open() time and passes the skb->data field to the chip as receive data
  136. buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
  137. a fresh skbuff is allocated and the frame is copied to the new skbuff.
  138. When the incoming frame is larger, the skbuff is passed directly up the
  139. protocol stack. Buffers consumed this way are replaced by newly allocated
  140. skbuffs in the last phase of rhine_rx().
  141. The RX_COPYBREAK value is chosen to trade-off the memory wasted by
  142. using a full-sized skbuff for small frames vs. the copying costs of larger
  143. frames. New boards are typically used in generously configured machines
  144. and the underfilled buffers have negligible impact compared to the benefit of
  145. a single allocation size, so the default value of zero results in never
  146. copying packets. When copying is done, the cost is usually mitigated by using
  147. a combined copy/checksum routine. Copying also preloads the cache, which is
  148. most useful with small frames.
  149. Since the VIA chips are only able to transfer data to buffers on 32 bit
  150. boundaries, the IP header at offset 14 in an ethernet frame isn't
  151. longword aligned for further processing. Copying these unaligned buffers
  152. has the beneficial effect of 16-byte aligning the IP header.
  153. IIId. Synchronization
  154. The driver runs as two independent, single-threaded flows of control. One
  155. is the send-packet routine, which enforces single-threaded use by the
  156. netdev_priv(dev)->lock spinlock. The other thread is the interrupt handler,
  157. which is single threaded by the hardware and interrupt handling software.
  158. The send packet thread has partial control over the Tx ring. It locks the
  159. netdev_priv(dev)->lock whenever it's queuing a Tx packet. If the next slot in
  160. the ring is not available it stops the transmit queue by
  161. calling netif_stop_queue.
  162. The interrupt handler has exclusive control over the Rx ring and records stats
  163. from the Tx ring. After reaping the stats, it marks the Tx queue entry as
  164. empty by incrementing the dirty_tx mark. If at least half of the entries in
  165. the Rx ring are available the transmit queue is woken up if it was stopped.
  166. IV. Notes
  167. IVb. References
  168. Preliminary VT86C100A manual from http://www.via.com.tw/
  169. http://www.scyld.com/expert/100mbps.html
  170. http://www.scyld.com/expert/NWay.html
  171. ftp://ftp.via.com.tw/public/lan/Products/NIC/VT86C100A/Datasheet/VT86C100A03.pdf
  172. ftp://ftp.via.com.tw/public/lan/Products/NIC/VT6102/Datasheet/VT6102_021.PDF
  173. IVc. Errata
  174. The VT86C100A manual is not reliable information.
  175. The 3043 chip does not handle unaligned transmit or receive buffers, resulting
  176. in significant performance degradation for bounce buffer copies on transmit
  177. and unaligned IP headers on receive.
  178. The chip does not pad to minimum transmit length.
  179. */
  180. /* This table drives the PCI probe routines. It's mostly boilerplate in all
  181. of the drivers, and will likely be provided by some future kernel.
  182. Note the matching code -- the first table entry matchs all 56** cards but
  183. second only the 1234 card.
  184. */
  185. enum rhine_revs {
  186. VT86C100A = 0x00,
  187. VTunknown0 = 0x20,
  188. VT6102 = 0x40,
  189. VT8231 = 0x50, /* Integrated MAC */
  190. VT8233 = 0x60, /* Integrated MAC */
  191. VT8235 = 0x74, /* Integrated MAC */
  192. VT8237 = 0x78, /* Integrated MAC */
  193. VTunknown1 = 0x7C,
  194. VT6105 = 0x80,
  195. VT6105_B0 = 0x83,
  196. VT6105L = 0x8A,
  197. VT6107 = 0x8C,
  198. VTunknown2 = 0x8E,
  199. VT6105M = 0x90, /* Management adapter */
  200. };
  201. enum rhine_quirks {
  202. rqWOL = 0x0001, /* Wake-On-LAN support */
  203. rqForceReset = 0x0002,
  204. rq6patterns = 0x0040, /* 6 instead of 4 patterns for WOL */
  205. rqStatusWBRace = 0x0080, /* Tx Status Writeback Error possible */
  206. rqRhineI = 0x0100, /* See comment below */
  207. };
  208. /*
  209. * rqRhineI: VT86C100A (aka Rhine-I) uses different bits to enable
  210. * MMIO as well as for the collision counter and the Tx FIFO underflow
  211. * indicator. In addition, Tx and Rx buffers need to 4 byte aligned.
  212. */
  213. /* Beware of PCI posted writes */
  214. #define IOSYNC do { ioread8(ioaddr + StationAddr); } while (0)
  215. static DEFINE_PCI_DEVICE_TABLE(rhine_pci_tbl) = {
  216. { 0x1106, 0x3043, PCI_ANY_ID, PCI_ANY_ID, }, /* VT86C100A */
  217. { 0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, }, /* VT6102 */
  218. { 0x1106, 0x3106, PCI_ANY_ID, PCI_ANY_ID, }, /* 6105{,L,LOM} */
  219. { 0x1106, 0x3053, PCI_ANY_ID, PCI_ANY_ID, }, /* VT6105M */
  220. { } /* terminate list */
  221. };
  222. MODULE_DEVICE_TABLE(pci, rhine_pci_tbl);
  223. /* Offsets to the device registers. */
  224. enum register_offsets {
  225. StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
  226. ChipCmd1=0x09, TQWake=0x0A,
  227. IntrStatus=0x0C, IntrEnable=0x0E,
  228. MulticastFilter0=0x10, MulticastFilter1=0x14,
  229. RxRingPtr=0x18, TxRingPtr=0x1C, GFIFOTest=0x54,
  230. MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E, PCIBusConfig1=0x6F,
  231. MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72, MACRegEEcsr=0x74,
  232. ConfigA=0x78, ConfigB=0x79, ConfigC=0x7A, ConfigD=0x7B,
  233. RxMissed=0x7C, RxCRCErrs=0x7E, MiscCmd=0x81,
  234. StickyHW=0x83, IntrStatus2=0x84,
  235. CamMask=0x88, CamCon=0x92, CamAddr=0x93,
  236. WOLcrSet=0xA0, PwcfgSet=0xA1, WOLcgSet=0xA3, WOLcrClr=0xA4,
  237. WOLcrClr1=0xA6, WOLcgClr=0xA7,
  238. PwrcsrSet=0xA8, PwrcsrSet1=0xA9, PwrcsrClr=0xAC, PwrcsrClr1=0xAD,
  239. };
  240. /* Bits in ConfigD */
  241. enum backoff_bits {
  242. BackOptional=0x01, BackModify=0x02,
  243. BackCaptureEffect=0x04, BackRandom=0x08
  244. };
  245. /* Bits in the TxConfig (TCR) register */
  246. enum tcr_bits {
  247. TCR_PQEN=0x01,
  248. TCR_LB0=0x02, /* loopback[0] */
  249. TCR_LB1=0x04, /* loopback[1] */
  250. TCR_OFSET=0x08,
  251. TCR_RTGOPT=0x10,
  252. TCR_RTFT0=0x20,
  253. TCR_RTFT1=0x40,
  254. TCR_RTSF=0x80,
  255. };
  256. /* Bits in the CamCon (CAMC) register */
  257. enum camcon_bits {
  258. CAMC_CAMEN=0x01,
  259. CAMC_VCAMSL=0x02,
  260. CAMC_CAMWR=0x04,
  261. CAMC_CAMRD=0x08,
  262. };
  263. /* Bits in the PCIBusConfig1 (BCR1) register */
  264. enum bcr1_bits {
  265. BCR1_POT0=0x01,
  266. BCR1_POT1=0x02,
  267. BCR1_POT2=0x04,
  268. BCR1_CTFT0=0x08,
  269. BCR1_CTFT1=0x10,
  270. BCR1_CTSF=0x20,
  271. BCR1_TXQNOBK=0x40, /* for VT6105 */
  272. BCR1_VIDFR=0x80, /* for VT6105 */
  273. BCR1_MED0=0x40, /* for VT6102 */
  274. BCR1_MED1=0x80, /* for VT6102 */
  275. };
  276. #ifdef USE_MMIO
  277. /* Registers we check that mmio and reg are the same. */
  278. static const int mmio_verify_registers[] = {
  279. RxConfig, TxConfig, IntrEnable, ConfigA, ConfigB, ConfigC, ConfigD,
  280. 0
  281. };
  282. #endif
  283. /* Bits in the interrupt status/mask registers. */
  284. enum intr_status_bits {
  285. IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
  286. IntrTxDone=0x0002, IntrTxError=0x0008, IntrTxUnderrun=0x0210,
  287. IntrPCIErr=0x0040,
  288. IntrStatsMax=0x0080, IntrRxEarly=0x0100,
  289. IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
  290. IntrTxAborted=0x2000, IntrLinkChange=0x4000,
  291. IntrRxWakeUp=0x8000,
  292. IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
  293. IntrTxDescRace=0x080000, /* mapped from IntrStatus2 */
  294. IntrTxErrSummary=0x082218,
  295. };
  296. /* Bits in WOLcrSet/WOLcrClr and PwrcsrSet/PwrcsrClr */
  297. enum wol_bits {
  298. WOLucast = 0x10,
  299. WOLmagic = 0x20,
  300. WOLbmcast = 0x30,
  301. WOLlnkon = 0x40,
  302. WOLlnkoff = 0x80,
  303. };
  304. /* The Rx and Tx buffer descriptors. */
  305. struct rx_desc {
  306. __le32 rx_status;
  307. __le32 desc_length; /* Chain flag, Buffer/frame length */
  308. __le32 addr;
  309. __le32 next_desc;
  310. };
  311. struct tx_desc {
  312. __le32 tx_status;
  313. __le32 desc_length; /* Chain flag, Tx Config, Frame length */
  314. __le32 addr;
  315. __le32 next_desc;
  316. };
  317. /* Initial value for tx_desc.desc_length, Buffer size goes to bits 0-10 */
  318. #define TXDESC 0x00e08000
  319. enum rx_status_bits {
  320. RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
  321. };
  322. /* Bits in *_desc.*_status */
  323. enum desc_status_bits {
  324. DescOwn=0x80000000
  325. };
  326. /* Bits in *_desc.*_length */
  327. enum desc_length_bits {
  328. DescTag=0x00010000
  329. };
  330. /* Bits in ChipCmd. */
  331. enum chip_cmd_bits {
  332. CmdInit=0x01, CmdStart=0x02, CmdStop=0x04, CmdRxOn=0x08,
  333. CmdTxOn=0x10, Cmd1TxDemand=0x20, CmdRxDemand=0x40,
  334. Cmd1EarlyRx=0x01, Cmd1EarlyTx=0x02, Cmd1FDuplex=0x04,
  335. Cmd1NoTxPoll=0x08, Cmd1Reset=0x80,
  336. };
  337. struct rhine_private {
  338. /* Bit mask for configured VLAN ids */
  339. unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
  340. /* Descriptor rings */
  341. struct rx_desc *rx_ring;
  342. struct tx_desc *tx_ring;
  343. dma_addr_t rx_ring_dma;
  344. dma_addr_t tx_ring_dma;
  345. /* The addresses of receive-in-place skbuffs. */
  346. struct sk_buff *rx_skbuff[RX_RING_SIZE];
  347. dma_addr_t rx_skbuff_dma[RX_RING_SIZE];
  348. /* The saved address of a sent-in-place packet/buffer, for later free(). */
  349. struct sk_buff *tx_skbuff[TX_RING_SIZE];
  350. dma_addr_t tx_skbuff_dma[TX_RING_SIZE];
  351. /* Tx bounce buffers (Rhine-I only) */
  352. unsigned char *tx_buf[TX_RING_SIZE];
  353. unsigned char *tx_bufs;
  354. dma_addr_t tx_bufs_dma;
  355. struct pci_dev *pdev;
  356. long pioaddr;
  357. struct net_device *dev;
  358. struct napi_struct napi;
  359. spinlock_t lock;
  360. struct work_struct reset_task;
  361. /* Frequently used values: keep some adjacent for cache effect. */
  362. u32 quirks;
  363. struct rx_desc *rx_head_desc;
  364. unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
  365. unsigned int cur_tx, dirty_tx;
  366. unsigned int rx_buf_sz; /* Based on MTU+slack. */
  367. u8 wolopts;
  368. u8 tx_thresh, rx_thresh;
  369. struct mii_if_info mii_if;
  370. void __iomem *base;
  371. };
  372. #define BYTE_REG_BITS_ON(x, p) do { iowrite8((ioread8((p))|(x)), (p)); } while (0)
  373. #define WORD_REG_BITS_ON(x, p) do { iowrite16((ioread16((p))|(x)), (p)); } while (0)
  374. #define DWORD_REG_BITS_ON(x, p) do { iowrite32((ioread32((p))|(x)), (p)); } while (0)
  375. #define BYTE_REG_BITS_IS_ON(x, p) (ioread8((p)) & (x))
  376. #define WORD_REG_BITS_IS_ON(x, p) (ioread16((p)) & (x))
  377. #define DWORD_REG_BITS_IS_ON(x, p) (ioread32((p)) & (x))
  378. #define BYTE_REG_BITS_OFF(x, p) do { iowrite8(ioread8((p)) & (~(x)), (p)); } while (0)
  379. #define WORD_REG_BITS_OFF(x, p) do { iowrite16(ioread16((p)) & (~(x)), (p)); } while (0)
  380. #define DWORD_REG_BITS_OFF(x, p) do { iowrite32(ioread32((p)) & (~(x)), (p)); } while (0)
  381. #define BYTE_REG_BITS_SET(x, m, p) do { iowrite8((ioread8((p)) & (~(m)))|(x), (p)); } while (0)
  382. #define WORD_REG_BITS_SET(x, m, p) do { iowrite16((ioread16((p)) & (~(m)))|(x), (p)); } while (0)
  383. #define DWORD_REG_BITS_SET(x, m, p) do { iowrite32((ioread32((p)) & (~(m)))|(x), (p)); } while (0)
  384. static int mdio_read(struct net_device *dev, int phy_id, int location);
  385. static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
  386. static int rhine_open(struct net_device *dev);
  387. static void rhine_reset_task(struct work_struct *work);
  388. static void rhine_tx_timeout(struct net_device *dev);
  389. static netdev_tx_t rhine_start_tx(struct sk_buff *skb,
  390. struct net_device *dev);
  391. static irqreturn_t rhine_interrupt(int irq, void *dev_instance);
  392. static void rhine_tx(struct net_device *dev);
  393. static int rhine_rx(struct net_device *dev, int limit);
  394. static void rhine_error(struct net_device *dev, int intr_status);
  395. static void rhine_set_rx_mode(struct net_device *dev);
  396. static struct net_device_stats *rhine_get_stats(struct net_device *dev);
  397. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  398. static const struct ethtool_ops netdev_ethtool_ops;
  399. static int rhine_close(struct net_device *dev);
  400. static void rhine_shutdown (struct pci_dev *pdev);
  401. static void rhine_vlan_rx_add_vid(struct net_device *dev, unsigned short vid);
  402. static void rhine_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid);
  403. static void rhine_set_cam(void __iomem *ioaddr, int idx, u8 *addr);
  404. static void rhine_set_vlan_cam(void __iomem *ioaddr, int idx, u8 *addr);
  405. static void rhine_set_cam_mask(void __iomem *ioaddr, u32 mask);
  406. static void rhine_set_vlan_cam_mask(void __iomem *ioaddr, u32 mask);
  407. static void rhine_init_cam_filter(struct net_device *dev);
  408. static void rhine_update_vcam(struct net_device *dev);
  409. #define RHINE_WAIT_FOR(condition) \
  410. do { \
  411. int i = 1024; \
  412. while (!(condition) && --i) \
  413. ; \
  414. if (debug > 1 && i < 512) \
  415. pr_info("%4d cycles used @ %s:%d\n", \
  416. 1024 - i, __func__, __LINE__); \
  417. } while (0)
  418. static inline u32 get_intr_status(struct net_device *dev)
  419. {
  420. struct rhine_private *rp = netdev_priv(dev);
  421. void __iomem *ioaddr = rp->base;
  422. u32 intr_status;
  423. intr_status = ioread16(ioaddr + IntrStatus);
  424. /* On Rhine-II, Bit 3 indicates Tx descriptor write-back race. */
  425. if (rp->quirks & rqStatusWBRace)
  426. intr_status |= ioread8(ioaddr + IntrStatus2) << 16;
  427. return intr_status;
  428. }
  429. /*
  430. * Get power related registers into sane state.
  431. * Notify user about past WOL event.
  432. */
  433. static void rhine_power_init(struct net_device *dev)
  434. {
  435. struct rhine_private *rp = netdev_priv(dev);
  436. void __iomem *ioaddr = rp->base;
  437. u16 wolstat;
  438. if (rp->quirks & rqWOL) {
  439. /* Make sure chip is in power state D0 */
  440. iowrite8(ioread8(ioaddr + StickyHW) & 0xFC, ioaddr + StickyHW);
  441. /* Disable "force PME-enable" */
  442. iowrite8(0x80, ioaddr + WOLcgClr);
  443. /* Clear power-event config bits (WOL) */
  444. iowrite8(0xFF, ioaddr + WOLcrClr);
  445. /* More recent cards can manage two additional patterns */
  446. if (rp->quirks & rq6patterns)
  447. iowrite8(0x03, ioaddr + WOLcrClr1);
  448. /* Save power-event status bits */
  449. wolstat = ioread8(ioaddr + PwrcsrSet);
  450. if (rp->quirks & rq6patterns)
  451. wolstat |= (ioread8(ioaddr + PwrcsrSet1) & 0x03) << 8;
  452. /* Clear power-event status bits */
  453. iowrite8(0xFF, ioaddr + PwrcsrClr);
  454. if (rp->quirks & rq6patterns)
  455. iowrite8(0x03, ioaddr + PwrcsrClr1);
  456. if (wolstat) {
  457. char *reason;
  458. switch (wolstat) {
  459. case WOLmagic:
  460. reason = "Magic packet";
  461. break;
  462. case WOLlnkon:
  463. reason = "Link went up";
  464. break;
  465. case WOLlnkoff:
  466. reason = "Link went down";
  467. break;
  468. case WOLucast:
  469. reason = "Unicast packet";
  470. break;
  471. case WOLbmcast:
  472. reason = "Multicast/broadcast packet";
  473. break;
  474. default:
  475. reason = "Unknown";
  476. }
  477. netdev_info(dev, "Woke system up. Reason: %s\n",
  478. reason);
  479. }
  480. }
  481. }
  482. static void rhine_chip_reset(struct net_device *dev)
  483. {
  484. struct rhine_private *rp = netdev_priv(dev);
  485. void __iomem *ioaddr = rp->base;
  486. iowrite8(Cmd1Reset, ioaddr + ChipCmd1);
  487. IOSYNC;
  488. if (ioread8(ioaddr + ChipCmd1) & Cmd1Reset) {
  489. netdev_info(dev, "Reset not complete yet. Trying harder.\n");
  490. /* Force reset */
  491. if (rp->quirks & rqForceReset)
  492. iowrite8(0x40, ioaddr + MiscCmd);
  493. /* Reset can take somewhat longer (rare) */
  494. RHINE_WAIT_FOR(!(ioread8(ioaddr + ChipCmd1) & Cmd1Reset));
  495. }
  496. if (debug > 1)
  497. netdev_info(dev, "Reset %s\n",
  498. (ioread8(ioaddr + ChipCmd1) & Cmd1Reset) ?
  499. "failed" : "succeeded");
  500. }
  501. #ifdef USE_MMIO
  502. static void enable_mmio(long pioaddr, u32 quirks)
  503. {
  504. int n;
  505. if (quirks & rqRhineI) {
  506. /* More recent docs say that this bit is reserved ... */
  507. n = inb(pioaddr + ConfigA) | 0x20;
  508. outb(n, pioaddr + ConfigA);
  509. } else {
  510. n = inb(pioaddr + ConfigD) | 0x80;
  511. outb(n, pioaddr + ConfigD);
  512. }
  513. }
  514. #endif
  515. /*
  516. * Loads bytes 0x00-0x05, 0x6E-0x6F, 0x78-0x7B from EEPROM
  517. * (plus 0x6C for Rhine-I/II)
  518. */
  519. static void __devinit rhine_reload_eeprom(long pioaddr, struct net_device *dev)
  520. {
  521. struct rhine_private *rp = netdev_priv(dev);
  522. void __iomem *ioaddr = rp->base;
  523. outb(0x20, pioaddr + MACRegEEcsr);
  524. RHINE_WAIT_FOR(!(inb(pioaddr + MACRegEEcsr) & 0x20));
  525. #ifdef USE_MMIO
  526. /*
  527. * Reloading from EEPROM overwrites ConfigA-D, so we must re-enable
  528. * MMIO. If reloading EEPROM was done first this could be avoided, but
  529. * it is not known if that still works with the "win98-reboot" problem.
  530. */
  531. enable_mmio(pioaddr, rp->quirks);
  532. #endif
  533. /* Turn off EEPROM-controlled wake-up (magic packet) */
  534. if (rp->quirks & rqWOL)
  535. iowrite8(ioread8(ioaddr + ConfigA) & 0xFC, ioaddr + ConfigA);
  536. }
  537. #ifdef CONFIG_NET_POLL_CONTROLLER
  538. static void rhine_poll(struct net_device *dev)
  539. {
  540. disable_irq(dev->irq);
  541. rhine_interrupt(dev->irq, (void *)dev);
  542. enable_irq(dev->irq);
  543. }
  544. #endif
  545. static int rhine_napipoll(struct napi_struct *napi, int budget)
  546. {
  547. struct rhine_private *rp = container_of(napi, struct rhine_private, napi);
  548. struct net_device *dev = rp->dev;
  549. void __iomem *ioaddr = rp->base;
  550. int work_done;
  551. work_done = rhine_rx(dev, budget);
  552. if (work_done < budget) {
  553. napi_complete(napi);
  554. iowrite16(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
  555. IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
  556. IntrTxDone | IntrTxError | IntrTxUnderrun |
  557. IntrPCIErr | IntrStatsMax | IntrLinkChange,
  558. ioaddr + IntrEnable);
  559. }
  560. return work_done;
  561. }
  562. static void __devinit rhine_hw_init(struct net_device *dev, long pioaddr)
  563. {
  564. struct rhine_private *rp = netdev_priv(dev);
  565. /* Reset the chip to erase previous misconfiguration. */
  566. rhine_chip_reset(dev);
  567. /* Rhine-I needs extra time to recuperate before EEPROM reload */
  568. if (rp->quirks & rqRhineI)
  569. msleep(5);
  570. /* Reload EEPROM controlled bytes cleared by soft reset */
  571. rhine_reload_eeprom(pioaddr, dev);
  572. }
  573. static const struct net_device_ops rhine_netdev_ops = {
  574. .ndo_open = rhine_open,
  575. .ndo_stop = rhine_close,
  576. .ndo_start_xmit = rhine_start_tx,
  577. .ndo_get_stats = rhine_get_stats,
  578. .ndo_set_multicast_list = rhine_set_rx_mode,
  579. .ndo_change_mtu = eth_change_mtu,
  580. .ndo_validate_addr = eth_validate_addr,
  581. .ndo_set_mac_address = eth_mac_addr,
  582. .ndo_do_ioctl = netdev_ioctl,
  583. .ndo_tx_timeout = rhine_tx_timeout,
  584. .ndo_vlan_rx_add_vid = rhine_vlan_rx_add_vid,
  585. .ndo_vlan_rx_kill_vid = rhine_vlan_rx_kill_vid,
  586. #ifdef CONFIG_NET_POLL_CONTROLLER
  587. .ndo_poll_controller = rhine_poll,
  588. #endif
  589. };
  590. static int __devinit rhine_init_one(struct pci_dev *pdev,
  591. const struct pci_device_id *ent)
  592. {
  593. struct net_device *dev;
  594. struct rhine_private *rp;
  595. int i, rc;
  596. u32 quirks;
  597. long pioaddr;
  598. long memaddr;
  599. void __iomem *ioaddr;
  600. int io_size, phy_id;
  601. const char *name;
  602. #ifdef USE_MMIO
  603. int bar = 1;
  604. #else
  605. int bar = 0;
  606. #endif
  607. /* when built into the kernel, we only print version if device is found */
  608. #ifndef MODULE
  609. pr_info_once("%s\n", version);
  610. #endif
  611. io_size = 256;
  612. phy_id = 0;
  613. quirks = 0;
  614. name = "Rhine";
  615. if (pdev->revision < VTunknown0) {
  616. quirks = rqRhineI;
  617. io_size = 128;
  618. }
  619. else if (pdev->revision >= VT6102) {
  620. quirks = rqWOL | rqForceReset;
  621. if (pdev->revision < VT6105) {
  622. name = "Rhine II";
  623. quirks |= rqStatusWBRace; /* Rhine-II exclusive */
  624. }
  625. else {
  626. phy_id = 1; /* Integrated PHY, phy_id fixed to 1 */
  627. if (pdev->revision >= VT6105_B0)
  628. quirks |= rq6patterns;
  629. if (pdev->revision < VT6105M)
  630. name = "Rhine III";
  631. else
  632. name = "Rhine III (Management Adapter)";
  633. }
  634. }
  635. rc = pci_enable_device(pdev);
  636. if (rc)
  637. goto err_out;
  638. /* this should always be supported */
  639. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  640. if (rc) {
  641. dev_err(&pdev->dev,
  642. "32-bit PCI DMA addresses not supported by the card!?\n");
  643. goto err_out;
  644. }
  645. /* sanity check */
  646. if ((pci_resource_len(pdev, 0) < io_size) ||
  647. (pci_resource_len(pdev, 1) < io_size)) {
  648. rc = -EIO;
  649. dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n");
  650. goto err_out;
  651. }
  652. pioaddr = pci_resource_start(pdev, 0);
  653. memaddr = pci_resource_start(pdev, 1);
  654. pci_set_master(pdev);
  655. dev = alloc_etherdev(sizeof(struct rhine_private));
  656. if (!dev) {
  657. rc = -ENOMEM;
  658. dev_err(&pdev->dev, "alloc_etherdev failed\n");
  659. goto err_out;
  660. }
  661. SET_NETDEV_DEV(dev, &pdev->dev);
  662. rp = netdev_priv(dev);
  663. rp->dev = dev;
  664. rp->quirks = quirks;
  665. rp->pioaddr = pioaddr;
  666. rp->pdev = pdev;
  667. rc = pci_request_regions(pdev, DRV_NAME);
  668. if (rc)
  669. goto err_out_free_netdev;
  670. ioaddr = pci_iomap(pdev, bar, io_size);
  671. if (!ioaddr) {
  672. rc = -EIO;
  673. dev_err(&pdev->dev,
  674. "ioremap failed for device %s, region 0x%X @ 0x%lX\n",
  675. pci_name(pdev), io_size, memaddr);
  676. goto err_out_free_res;
  677. }
  678. #ifdef USE_MMIO
  679. enable_mmio(pioaddr, quirks);
  680. /* Check that selected MMIO registers match the PIO ones */
  681. i = 0;
  682. while (mmio_verify_registers[i]) {
  683. int reg = mmio_verify_registers[i++];
  684. unsigned char a = inb(pioaddr+reg);
  685. unsigned char b = readb(ioaddr+reg);
  686. if (a != b) {
  687. rc = -EIO;
  688. dev_err(&pdev->dev,
  689. "MMIO do not match PIO [%02x] (%02x != %02x)\n",
  690. reg, a, b);
  691. goto err_out_unmap;
  692. }
  693. }
  694. #endif /* USE_MMIO */
  695. dev->base_addr = (unsigned long)ioaddr;
  696. rp->base = ioaddr;
  697. /* Get chip registers into a sane state */
  698. rhine_power_init(dev);
  699. rhine_hw_init(dev, pioaddr);
  700. for (i = 0; i < 6; i++)
  701. dev->dev_addr[i] = ioread8(ioaddr + StationAddr + i);
  702. if (!is_valid_ether_addr(dev->dev_addr)) {
  703. /* Report it and use a random ethernet address instead */
  704. netdev_err(dev, "Invalid MAC address: %pM\n", dev->dev_addr);
  705. random_ether_addr(dev->dev_addr);
  706. netdev_info(dev, "Using random MAC address: %pM\n",
  707. dev->dev_addr);
  708. }
  709. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  710. /* For Rhine-I/II, phy_id is loaded from EEPROM */
  711. if (!phy_id)
  712. phy_id = ioread8(ioaddr + 0x6C);
  713. dev->irq = pdev->irq;
  714. spin_lock_init(&rp->lock);
  715. INIT_WORK(&rp->reset_task, rhine_reset_task);
  716. rp->mii_if.dev = dev;
  717. rp->mii_if.mdio_read = mdio_read;
  718. rp->mii_if.mdio_write = mdio_write;
  719. rp->mii_if.phy_id_mask = 0x1f;
  720. rp->mii_if.reg_num_mask = 0x1f;
  721. /* The chip-specific entries in the device structure. */
  722. dev->netdev_ops = &rhine_netdev_ops;
  723. dev->ethtool_ops = &netdev_ethtool_ops,
  724. dev->watchdog_timeo = TX_TIMEOUT;
  725. netif_napi_add(dev, &rp->napi, rhine_napipoll, 64);
  726. if (rp->quirks & rqRhineI)
  727. dev->features |= NETIF_F_SG|NETIF_F_HW_CSUM;
  728. if (pdev->revision >= VT6105M)
  729. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
  730. NETIF_F_HW_VLAN_FILTER;
  731. /* dev->name not defined before register_netdev()! */
  732. rc = register_netdev(dev);
  733. if (rc)
  734. goto err_out_unmap;
  735. netdev_info(dev, "VIA %s at 0x%lx, %pM, IRQ %d\n",
  736. name,
  737. #ifdef USE_MMIO
  738. memaddr,
  739. #else
  740. (long)ioaddr,
  741. #endif
  742. dev->dev_addr, pdev->irq);
  743. pci_set_drvdata(pdev, dev);
  744. {
  745. u16 mii_cmd;
  746. int mii_status = mdio_read(dev, phy_id, 1);
  747. mii_cmd = mdio_read(dev, phy_id, MII_BMCR) & ~BMCR_ISOLATE;
  748. mdio_write(dev, phy_id, MII_BMCR, mii_cmd);
  749. if (mii_status != 0xffff && mii_status != 0x0000) {
  750. rp->mii_if.advertising = mdio_read(dev, phy_id, 4);
  751. netdev_info(dev,
  752. "MII PHY found at address %d, status 0x%04x advertising %04x Link %04x\n",
  753. phy_id,
  754. mii_status, rp->mii_if.advertising,
  755. mdio_read(dev, phy_id, 5));
  756. /* set IFF_RUNNING */
  757. if (mii_status & BMSR_LSTATUS)
  758. netif_carrier_on(dev);
  759. else
  760. netif_carrier_off(dev);
  761. }
  762. }
  763. rp->mii_if.phy_id = phy_id;
  764. if (debug > 1 && avoid_D3)
  765. netdev_info(dev, "No D3 power state at shutdown\n");
  766. return 0;
  767. err_out_unmap:
  768. pci_iounmap(pdev, ioaddr);
  769. err_out_free_res:
  770. pci_release_regions(pdev);
  771. err_out_free_netdev:
  772. free_netdev(dev);
  773. err_out:
  774. return rc;
  775. }
  776. static int alloc_ring(struct net_device* dev)
  777. {
  778. struct rhine_private *rp = netdev_priv(dev);
  779. void *ring;
  780. dma_addr_t ring_dma;
  781. ring = pci_alloc_consistent(rp->pdev,
  782. RX_RING_SIZE * sizeof(struct rx_desc) +
  783. TX_RING_SIZE * sizeof(struct tx_desc),
  784. &ring_dma);
  785. if (!ring) {
  786. netdev_err(dev, "Could not allocate DMA memory\n");
  787. return -ENOMEM;
  788. }
  789. if (rp->quirks & rqRhineI) {
  790. rp->tx_bufs = pci_alloc_consistent(rp->pdev,
  791. PKT_BUF_SZ * TX_RING_SIZE,
  792. &rp->tx_bufs_dma);
  793. if (rp->tx_bufs == NULL) {
  794. pci_free_consistent(rp->pdev,
  795. RX_RING_SIZE * sizeof(struct rx_desc) +
  796. TX_RING_SIZE * sizeof(struct tx_desc),
  797. ring, ring_dma);
  798. return -ENOMEM;
  799. }
  800. }
  801. rp->rx_ring = ring;
  802. rp->tx_ring = ring + RX_RING_SIZE * sizeof(struct rx_desc);
  803. rp->rx_ring_dma = ring_dma;
  804. rp->tx_ring_dma = ring_dma + RX_RING_SIZE * sizeof(struct rx_desc);
  805. return 0;
  806. }
  807. static void free_ring(struct net_device* dev)
  808. {
  809. struct rhine_private *rp = netdev_priv(dev);
  810. pci_free_consistent(rp->pdev,
  811. RX_RING_SIZE * sizeof(struct rx_desc) +
  812. TX_RING_SIZE * sizeof(struct tx_desc),
  813. rp->rx_ring, rp->rx_ring_dma);
  814. rp->tx_ring = NULL;
  815. if (rp->tx_bufs)
  816. pci_free_consistent(rp->pdev, PKT_BUF_SZ * TX_RING_SIZE,
  817. rp->tx_bufs, rp->tx_bufs_dma);
  818. rp->tx_bufs = NULL;
  819. }
  820. static void alloc_rbufs(struct net_device *dev)
  821. {
  822. struct rhine_private *rp = netdev_priv(dev);
  823. dma_addr_t next;
  824. int i;
  825. rp->dirty_rx = rp->cur_rx = 0;
  826. rp->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
  827. rp->rx_head_desc = &rp->rx_ring[0];
  828. next = rp->rx_ring_dma;
  829. /* Init the ring entries */
  830. for (i = 0; i < RX_RING_SIZE; i++) {
  831. rp->rx_ring[i].rx_status = 0;
  832. rp->rx_ring[i].desc_length = cpu_to_le32(rp->rx_buf_sz);
  833. next += sizeof(struct rx_desc);
  834. rp->rx_ring[i].next_desc = cpu_to_le32(next);
  835. rp->rx_skbuff[i] = NULL;
  836. }
  837. /* Mark the last entry as wrapping the ring. */
  838. rp->rx_ring[i-1].next_desc = cpu_to_le32(rp->rx_ring_dma);
  839. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  840. for (i = 0; i < RX_RING_SIZE; i++) {
  841. struct sk_buff *skb = netdev_alloc_skb(dev, rp->rx_buf_sz);
  842. rp->rx_skbuff[i] = skb;
  843. if (skb == NULL)
  844. break;
  845. skb->dev = dev; /* Mark as being used by this device. */
  846. rp->rx_skbuff_dma[i] =
  847. pci_map_single(rp->pdev, skb->data, rp->rx_buf_sz,
  848. PCI_DMA_FROMDEVICE);
  849. rp->rx_ring[i].addr = cpu_to_le32(rp->rx_skbuff_dma[i]);
  850. rp->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
  851. }
  852. rp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
  853. }
  854. static void free_rbufs(struct net_device* dev)
  855. {
  856. struct rhine_private *rp = netdev_priv(dev);
  857. int i;
  858. /* Free all the skbuffs in the Rx queue. */
  859. for (i = 0; i < RX_RING_SIZE; i++) {
  860. rp->rx_ring[i].rx_status = 0;
  861. rp->rx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
  862. if (rp->rx_skbuff[i]) {
  863. pci_unmap_single(rp->pdev,
  864. rp->rx_skbuff_dma[i],
  865. rp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  866. dev_kfree_skb(rp->rx_skbuff[i]);
  867. }
  868. rp->rx_skbuff[i] = NULL;
  869. }
  870. }
  871. static void alloc_tbufs(struct net_device* dev)
  872. {
  873. struct rhine_private *rp = netdev_priv(dev);
  874. dma_addr_t next;
  875. int i;
  876. rp->dirty_tx = rp->cur_tx = 0;
  877. next = rp->tx_ring_dma;
  878. for (i = 0; i < TX_RING_SIZE; i++) {
  879. rp->tx_skbuff[i] = NULL;
  880. rp->tx_ring[i].tx_status = 0;
  881. rp->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
  882. next += sizeof(struct tx_desc);
  883. rp->tx_ring[i].next_desc = cpu_to_le32(next);
  884. if (rp->quirks & rqRhineI)
  885. rp->tx_buf[i] = &rp->tx_bufs[i * PKT_BUF_SZ];
  886. }
  887. rp->tx_ring[i-1].next_desc = cpu_to_le32(rp->tx_ring_dma);
  888. }
  889. static void free_tbufs(struct net_device* dev)
  890. {
  891. struct rhine_private *rp = netdev_priv(dev);
  892. int i;
  893. for (i = 0; i < TX_RING_SIZE; i++) {
  894. rp->tx_ring[i].tx_status = 0;
  895. rp->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
  896. rp->tx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
  897. if (rp->tx_skbuff[i]) {
  898. if (rp->tx_skbuff_dma[i]) {
  899. pci_unmap_single(rp->pdev,
  900. rp->tx_skbuff_dma[i],
  901. rp->tx_skbuff[i]->len,
  902. PCI_DMA_TODEVICE);
  903. }
  904. dev_kfree_skb(rp->tx_skbuff[i]);
  905. }
  906. rp->tx_skbuff[i] = NULL;
  907. rp->tx_buf[i] = NULL;
  908. }
  909. }
  910. static void rhine_check_media(struct net_device *dev, unsigned int init_media)
  911. {
  912. struct rhine_private *rp = netdev_priv(dev);
  913. void __iomem *ioaddr = rp->base;
  914. mii_check_media(&rp->mii_if, debug, init_media);
  915. if (rp->mii_if.full_duplex)
  916. iowrite8(ioread8(ioaddr + ChipCmd1) | Cmd1FDuplex,
  917. ioaddr + ChipCmd1);
  918. else
  919. iowrite8(ioread8(ioaddr + ChipCmd1) & ~Cmd1FDuplex,
  920. ioaddr + ChipCmd1);
  921. if (debug > 1)
  922. netdev_info(dev, "force_media %d, carrier %d\n",
  923. rp->mii_if.force_media, netif_carrier_ok(dev));
  924. }
  925. /* Called after status of force_media possibly changed */
  926. static void rhine_set_carrier(struct mii_if_info *mii)
  927. {
  928. if (mii->force_media) {
  929. /* autoneg is off: Link is always assumed to be up */
  930. if (!netif_carrier_ok(mii->dev))
  931. netif_carrier_on(mii->dev);
  932. }
  933. else /* Let MMI library update carrier status */
  934. rhine_check_media(mii->dev, 0);
  935. if (debug > 1)
  936. netdev_info(mii->dev, "force_media %d, carrier %d\n",
  937. mii->force_media, netif_carrier_ok(mii->dev));
  938. }
  939. /**
  940. * rhine_set_cam - set CAM multicast filters
  941. * @ioaddr: register block of this Rhine
  942. * @idx: multicast CAM index [0..MCAM_SIZE-1]
  943. * @addr: multicast address (6 bytes)
  944. *
  945. * Load addresses into multicast filters.
  946. */
  947. static void rhine_set_cam(void __iomem *ioaddr, int idx, u8 *addr)
  948. {
  949. int i;
  950. iowrite8(CAMC_CAMEN, ioaddr + CamCon);
  951. wmb();
  952. /* Paranoid -- idx out of range should never happen */
  953. idx &= (MCAM_SIZE - 1);
  954. iowrite8((u8) idx, ioaddr + CamAddr);
  955. for (i = 0; i < 6; i++, addr++)
  956. iowrite8(*addr, ioaddr + MulticastFilter0 + i);
  957. udelay(10);
  958. wmb();
  959. iowrite8(CAMC_CAMWR | CAMC_CAMEN, ioaddr + CamCon);
  960. udelay(10);
  961. iowrite8(0, ioaddr + CamCon);
  962. }
  963. /**
  964. * rhine_set_vlan_cam - set CAM VLAN filters
  965. * @ioaddr: register block of this Rhine
  966. * @idx: VLAN CAM index [0..VCAM_SIZE-1]
  967. * @addr: VLAN ID (2 bytes)
  968. *
  969. * Load addresses into VLAN filters.
  970. */
  971. static void rhine_set_vlan_cam(void __iomem *ioaddr, int idx, u8 *addr)
  972. {
  973. iowrite8(CAMC_CAMEN | CAMC_VCAMSL, ioaddr + CamCon);
  974. wmb();
  975. /* Paranoid -- idx out of range should never happen */
  976. idx &= (VCAM_SIZE - 1);
  977. iowrite8((u8) idx, ioaddr + CamAddr);
  978. iowrite16(*((u16 *) addr), ioaddr + MulticastFilter0 + 6);
  979. udelay(10);
  980. wmb();
  981. iowrite8(CAMC_CAMWR | CAMC_CAMEN, ioaddr + CamCon);
  982. udelay(10);
  983. iowrite8(0, ioaddr + CamCon);
  984. }
  985. /**
  986. * rhine_set_cam_mask - set multicast CAM mask
  987. * @ioaddr: register block of this Rhine
  988. * @mask: multicast CAM mask
  989. *
  990. * Mask sets multicast filters active/inactive.
  991. */
  992. static void rhine_set_cam_mask(void __iomem *ioaddr, u32 mask)
  993. {
  994. iowrite8(CAMC_CAMEN, ioaddr + CamCon);
  995. wmb();
  996. /* write mask */
  997. iowrite32(mask, ioaddr + CamMask);
  998. /* disable CAMEN */
  999. iowrite8(0, ioaddr + CamCon);
  1000. }
  1001. /**
  1002. * rhine_set_vlan_cam_mask - set VLAN CAM mask
  1003. * @ioaddr: register block of this Rhine
  1004. * @mask: VLAN CAM mask
  1005. *
  1006. * Mask sets VLAN filters active/inactive.
  1007. */
  1008. static void rhine_set_vlan_cam_mask(void __iomem *ioaddr, u32 mask)
  1009. {
  1010. iowrite8(CAMC_CAMEN | CAMC_VCAMSL, ioaddr + CamCon);
  1011. wmb();
  1012. /* write mask */
  1013. iowrite32(mask, ioaddr + CamMask);
  1014. /* disable CAMEN */
  1015. iowrite8(0, ioaddr + CamCon);
  1016. }
  1017. /**
  1018. * rhine_init_cam_filter - initialize CAM filters
  1019. * @dev: network device
  1020. *
  1021. * Initialize (disable) hardware VLAN and multicast support on this
  1022. * Rhine.
  1023. */
  1024. static void rhine_init_cam_filter(struct net_device *dev)
  1025. {
  1026. struct rhine_private *rp = netdev_priv(dev);
  1027. void __iomem *ioaddr = rp->base;
  1028. /* Disable all CAMs */
  1029. rhine_set_vlan_cam_mask(ioaddr, 0);
  1030. rhine_set_cam_mask(ioaddr, 0);
  1031. /* disable hardware VLAN support */
  1032. BYTE_REG_BITS_ON(TCR_PQEN, ioaddr + TxConfig);
  1033. BYTE_REG_BITS_OFF(BCR1_VIDFR, ioaddr + PCIBusConfig1);
  1034. }
  1035. /**
  1036. * rhine_update_vcam - update VLAN CAM filters
  1037. * @rp: rhine_private data of this Rhine
  1038. *
  1039. * Update VLAN CAM filters to match configuration change.
  1040. */
  1041. static void rhine_update_vcam(struct net_device *dev)
  1042. {
  1043. struct rhine_private *rp = netdev_priv(dev);
  1044. void __iomem *ioaddr = rp->base;
  1045. u16 vid;
  1046. u32 vCAMmask = 0; /* 32 vCAMs (6105M and better) */
  1047. unsigned int i = 0;
  1048. for_each_set_bit(vid, rp->active_vlans, VLAN_N_VID) {
  1049. rhine_set_vlan_cam(ioaddr, i, (u8 *)&vid);
  1050. vCAMmask |= 1 << i;
  1051. if (++i >= VCAM_SIZE)
  1052. break;
  1053. }
  1054. rhine_set_vlan_cam_mask(ioaddr, vCAMmask);
  1055. }
  1056. static void rhine_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
  1057. {
  1058. struct rhine_private *rp = netdev_priv(dev);
  1059. spin_lock_irq(&rp->lock);
  1060. set_bit(vid, rp->active_vlans);
  1061. rhine_update_vcam(dev);
  1062. spin_unlock_irq(&rp->lock);
  1063. }
  1064. static void rhine_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
  1065. {
  1066. struct rhine_private *rp = netdev_priv(dev);
  1067. spin_lock_irq(&rp->lock);
  1068. clear_bit(vid, rp->active_vlans);
  1069. rhine_update_vcam(dev);
  1070. spin_unlock_irq(&rp->lock);
  1071. }
  1072. static void init_registers(struct net_device *dev)
  1073. {
  1074. struct rhine_private *rp = netdev_priv(dev);
  1075. void __iomem *ioaddr = rp->base;
  1076. int i;
  1077. for (i = 0; i < 6; i++)
  1078. iowrite8(dev->dev_addr[i], ioaddr + StationAddr + i);
  1079. /* Initialize other registers. */
  1080. iowrite16(0x0006, ioaddr + PCIBusConfig); /* Tune configuration??? */
  1081. /* Configure initial FIFO thresholds. */
  1082. iowrite8(0x20, ioaddr + TxConfig);
  1083. rp->tx_thresh = 0x20;
  1084. rp->rx_thresh = 0x60; /* Written in rhine_set_rx_mode(). */
  1085. iowrite32(rp->rx_ring_dma, ioaddr + RxRingPtr);
  1086. iowrite32(rp->tx_ring_dma, ioaddr + TxRingPtr);
  1087. rhine_set_rx_mode(dev);
  1088. if (rp->pdev->revision >= VT6105M)
  1089. rhine_init_cam_filter(dev);
  1090. napi_enable(&rp->napi);
  1091. /* Enable interrupts by setting the interrupt mask. */
  1092. iowrite16(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
  1093. IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
  1094. IntrTxDone | IntrTxError | IntrTxUnderrun |
  1095. IntrPCIErr | IntrStatsMax | IntrLinkChange,
  1096. ioaddr + IntrEnable);
  1097. iowrite16(CmdStart | CmdTxOn | CmdRxOn | (Cmd1NoTxPoll << 8),
  1098. ioaddr + ChipCmd);
  1099. rhine_check_media(dev, 1);
  1100. }
  1101. /* Enable MII link status auto-polling (required for IntrLinkChange) */
  1102. static void rhine_enable_linkmon(void __iomem *ioaddr)
  1103. {
  1104. iowrite8(0, ioaddr + MIICmd);
  1105. iowrite8(MII_BMSR, ioaddr + MIIRegAddr);
  1106. iowrite8(0x80, ioaddr + MIICmd);
  1107. RHINE_WAIT_FOR((ioread8(ioaddr + MIIRegAddr) & 0x20));
  1108. iowrite8(MII_BMSR | 0x40, ioaddr + MIIRegAddr);
  1109. }
  1110. /* Disable MII link status auto-polling (required for MDIO access) */
  1111. static void rhine_disable_linkmon(void __iomem *ioaddr, u32 quirks)
  1112. {
  1113. iowrite8(0, ioaddr + MIICmd);
  1114. if (quirks & rqRhineI) {
  1115. iowrite8(0x01, ioaddr + MIIRegAddr); // MII_BMSR
  1116. /* Can be called from ISR. Evil. */
  1117. mdelay(1);
  1118. /* 0x80 must be set immediately before turning it off */
  1119. iowrite8(0x80, ioaddr + MIICmd);
  1120. RHINE_WAIT_FOR(ioread8(ioaddr + MIIRegAddr) & 0x20);
  1121. /* Heh. Now clear 0x80 again. */
  1122. iowrite8(0, ioaddr + MIICmd);
  1123. }
  1124. else
  1125. RHINE_WAIT_FOR(ioread8(ioaddr + MIIRegAddr) & 0x80);
  1126. }
  1127. /* Read and write over the MII Management Data I/O (MDIO) interface. */
  1128. static int mdio_read(struct net_device *dev, int phy_id, int regnum)
  1129. {
  1130. struct rhine_private *rp = netdev_priv(dev);
  1131. void __iomem *ioaddr = rp->base;
  1132. int result;
  1133. rhine_disable_linkmon(ioaddr, rp->quirks);
  1134. /* rhine_disable_linkmon already cleared MIICmd */
  1135. iowrite8(phy_id, ioaddr + MIIPhyAddr);
  1136. iowrite8(regnum, ioaddr + MIIRegAddr);
  1137. iowrite8(0x40, ioaddr + MIICmd); /* Trigger read */
  1138. RHINE_WAIT_FOR(!(ioread8(ioaddr + MIICmd) & 0x40));
  1139. result = ioread16(ioaddr + MIIData);
  1140. rhine_enable_linkmon(ioaddr);
  1141. return result;
  1142. }
  1143. static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
  1144. {
  1145. struct rhine_private *rp = netdev_priv(dev);
  1146. void __iomem *ioaddr = rp->base;
  1147. rhine_disable_linkmon(ioaddr, rp->quirks);
  1148. /* rhine_disable_linkmon already cleared MIICmd */
  1149. iowrite8(phy_id, ioaddr + MIIPhyAddr);
  1150. iowrite8(regnum, ioaddr + MIIRegAddr);
  1151. iowrite16(value, ioaddr + MIIData);
  1152. iowrite8(0x20, ioaddr + MIICmd); /* Trigger write */
  1153. RHINE_WAIT_FOR(!(ioread8(ioaddr + MIICmd) & 0x20));
  1154. rhine_enable_linkmon(ioaddr);
  1155. }
  1156. static int rhine_open(struct net_device *dev)
  1157. {
  1158. struct rhine_private *rp = netdev_priv(dev);
  1159. void __iomem *ioaddr = rp->base;
  1160. int rc;
  1161. rc = request_irq(rp->pdev->irq, rhine_interrupt, IRQF_SHARED, dev->name,
  1162. dev);
  1163. if (rc)
  1164. return rc;
  1165. if (debug > 1)
  1166. netdev_dbg(dev, "%s() irq %d\n", __func__, rp->pdev->irq);
  1167. rc = alloc_ring(dev);
  1168. if (rc) {
  1169. free_irq(rp->pdev->irq, dev);
  1170. return rc;
  1171. }
  1172. alloc_rbufs(dev);
  1173. alloc_tbufs(dev);
  1174. rhine_chip_reset(dev);
  1175. init_registers(dev);
  1176. if (debug > 2)
  1177. netdev_dbg(dev, "%s() Done - status %04x MII status: %04x\n",
  1178. __func__, ioread16(ioaddr + ChipCmd),
  1179. mdio_read(dev, rp->mii_if.phy_id, MII_BMSR));
  1180. netif_start_queue(dev);
  1181. return 0;
  1182. }
  1183. static void rhine_reset_task(struct work_struct *work)
  1184. {
  1185. struct rhine_private *rp = container_of(work, struct rhine_private,
  1186. reset_task);
  1187. struct net_device *dev = rp->dev;
  1188. /* protect against concurrent rx interrupts */
  1189. disable_irq(rp->pdev->irq);
  1190. napi_disable(&rp->napi);
  1191. spin_lock_bh(&rp->lock);
  1192. /* clear all descriptors */
  1193. free_tbufs(dev);
  1194. free_rbufs(dev);
  1195. alloc_tbufs(dev);
  1196. alloc_rbufs(dev);
  1197. /* Reinitialize the hardware. */
  1198. rhine_chip_reset(dev);
  1199. init_registers(dev);
  1200. spin_unlock_bh(&rp->lock);
  1201. enable_irq(rp->pdev->irq);
  1202. dev->trans_start = jiffies; /* prevent tx timeout */
  1203. dev->stats.tx_errors++;
  1204. netif_wake_queue(dev);
  1205. }
  1206. static void rhine_tx_timeout(struct net_device *dev)
  1207. {
  1208. struct rhine_private *rp = netdev_priv(dev);
  1209. void __iomem *ioaddr = rp->base;
  1210. netdev_warn(dev, "Transmit timed out, status %04x, PHY status %04x, resetting...\n",
  1211. ioread16(ioaddr + IntrStatus),
  1212. mdio_read(dev, rp->mii_if.phy_id, MII_BMSR));
  1213. schedule_work(&rp->reset_task);
  1214. }
  1215. static netdev_tx_t rhine_start_tx(struct sk_buff *skb,
  1216. struct net_device *dev)
  1217. {
  1218. struct rhine_private *rp = netdev_priv(dev);
  1219. void __iomem *ioaddr = rp->base;
  1220. unsigned entry;
  1221. unsigned long flags;
  1222. /* Caution: the write order is important here, set the field
  1223. with the "ownership" bits last. */
  1224. /* Calculate the next Tx descriptor entry. */
  1225. entry = rp->cur_tx % TX_RING_SIZE;
  1226. if (skb_padto(skb, ETH_ZLEN))
  1227. return NETDEV_TX_OK;
  1228. rp->tx_skbuff[entry] = skb;
  1229. if ((rp->quirks & rqRhineI) &&
  1230. (((unsigned long)skb->data & 3) || skb_shinfo(skb)->nr_frags != 0 || skb->ip_summed == CHECKSUM_PARTIAL)) {
  1231. /* Must use alignment buffer. */
  1232. if (skb->len > PKT_BUF_SZ) {
  1233. /* packet too long, drop it */
  1234. dev_kfree_skb(skb);
  1235. rp->tx_skbuff[entry] = NULL;
  1236. dev->stats.tx_dropped++;
  1237. return NETDEV_TX_OK;
  1238. }
  1239. /* Padding is not copied and so must be redone. */
  1240. skb_copy_and_csum_dev(skb, rp->tx_buf[entry]);
  1241. if (skb->len < ETH_ZLEN)
  1242. memset(rp->tx_buf[entry] + skb->len, 0,
  1243. ETH_ZLEN - skb->len);
  1244. rp->tx_skbuff_dma[entry] = 0;
  1245. rp->tx_ring[entry].addr = cpu_to_le32(rp->tx_bufs_dma +
  1246. (rp->tx_buf[entry] -
  1247. rp->tx_bufs));
  1248. } else {
  1249. rp->tx_skbuff_dma[entry] =
  1250. pci_map_single(rp->pdev, skb->data, skb->len,
  1251. PCI_DMA_TODEVICE);
  1252. rp->tx_ring[entry].addr = cpu_to_le32(rp->tx_skbuff_dma[entry]);
  1253. }
  1254. rp->tx_ring[entry].desc_length =
  1255. cpu_to_le32(TXDESC | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
  1256. if (unlikely(vlan_tx_tag_present(skb))) {
  1257. rp->tx_ring[entry].tx_status = cpu_to_le32((vlan_tx_tag_get(skb)) << 16);
  1258. /* request tagging */
  1259. rp->tx_ring[entry].desc_length |= cpu_to_le32(0x020000);
  1260. }
  1261. else
  1262. rp->tx_ring[entry].tx_status = 0;
  1263. /* lock eth irq */
  1264. spin_lock_irqsave(&rp->lock, flags);
  1265. wmb();
  1266. rp->tx_ring[entry].tx_status |= cpu_to_le32(DescOwn);
  1267. wmb();
  1268. rp->cur_tx++;
  1269. /* Non-x86 Todo: explicitly flush cache lines here. */
  1270. if (vlan_tx_tag_present(skb))
  1271. /* Tx queues are bits 7-0 (first Tx queue: bit 7) */
  1272. BYTE_REG_BITS_ON(1 << 7, ioaddr + TQWake);
  1273. /* Wake the potentially-idle transmit channel */
  1274. iowrite8(ioread8(ioaddr + ChipCmd1) | Cmd1TxDemand,
  1275. ioaddr + ChipCmd1);
  1276. IOSYNC;
  1277. if (rp->cur_tx == rp->dirty_tx + TX_QUEUE_LEN)
  1278. netif_stop_queue(dev);
  1279. spin_unlock_irqrestore(&rp->lock, flags);
  1280. if (debug > 4) {
  1281. netdev_dbg(dev, "Transmit frame #%d queued in slot %d\n",
  1282. rp->cur_tx-1, entry);
  1283. }
  1284. return NETDEV_TX_OK;
  1285. }
  1286. /* The interrupt handler does all of the Rx thread work and cleans up
  1287. after the Tx thread. */
  1288. static irqreturn_t rhine_interrupt(int irq, void *dev_instance)
  1289. {
  1290. struct net_device *dev = dev_instance;
  1291. struct rhine_private *rp = netdev_priv(dev);
  1292. void __iomem *ioaddr = rp->base;
  1293. u32 intr_status;
  1294. int boguscnt = max_interrupt_work;
  1295. int handled = 0;
  1296. while ((intr_status = get_intr_status(dev))) {
  1297. handled = 1;
  1298. /* Acknowledge all of the current interrupt sources ASAP. */
  1299. if (intr_status & IntrTxDescRace)
  1300. iowrite8(0x08, ioaddr + IntrStatus2);
  1301. iowrite16(intr_status & 0xffff, ioaddr + IntrStatus);
  1302. IOSYNC;
  1303. if (debug > 4)
  1304. netdev_dbg(dev, "Interrupt, status %08x\n",
  1305. intr_status);
  1306. if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
  1307. IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf)) {
  1308. iowrite16(IntrTxAborted |
  1309. IntrTxDone | IntrTxError | IntrTxUnderrun |
  1310. IntrPCIErr | IntrStatsMax | IntrLinkChange,
  1311. ioaddr + IntrEnable);
  1312. napi_schedule(&rp->napi);
  1313. }
  1314. if (intr_status & (IntrTxErrSummary | IntrTxDone)) {
  1315. if (intr_status & IntrTxErrSummary) {
  1316. /* Avoid scavenging before Tx engine turned off */
  1317. RHINE_WAIT_FOR(!(ioread8(ioaddr+ChipCmd) & CmdTxOn));
  1318. if (debug > 2 &&
  1319. ioread8(ioaddr+ChipCmd) & CmdTxOn)
  1320. netdev_warn(dev,
  1321. "%s: Tx engine still on\n",
  1322. __func__);
  1323. }
  1324. rhine_tx(dev);
  1325. }
  1326. /* Abnormal error summary/uncommon events handlers. */
  1327. if (intr_status & (IntrPCIErr | IntrLinkChange |
  1328. IntrStatsMax | IntrTxError | IntrTxAborted |
  1329. IntrTxUnderrun | IntrTxDescRace))
  1330. rhine_error(dev, intr_status);
  1331. if (--boguscnt < 0) {
  1332. netdev_warn(dev, "Too much work at interrupt, status=%#08x\n",
  1333. intr_status);
  1334. break;
  1335. }
  1336. }
  1337. if (debug > 3)
  1338. netdev_dbg(dev, "exiting interrupt, status=%08x\n",
  1339. ioread16(ioaddr + IntrStatus));
  1340. return IRQ_RETVAL(handled);
  1341. }
  1342. /* This routine is logically part of the interrupt handler, but isolated
  1343. for clarity. */
  1344. static void rhine_tx(struct net_device *dev)
  1345. {
  1346. struct rhine_private *rp = netdev_priv(dev);
  1347. int txstatus = 0, entry = rp->dirty_tx % TX_RING_SIZE;
  1348. spin_lock(&rp->lock);
  1349. /* find and cleanup dirty tx descriptors */
  1350. while (rp->dirty_tx != rp->cur_tx) {
  1351. txstatus = le32_to_cpu(rp->tx_ring[entry].tx_status);
  1352. if (debug > 6)
  1353. netdev_dbg(dev, "Tx scavenge %d status %08x\n",
  1354. entry, txstatus);
  1355. if (txstatus & DescOwn)
  1356. break;
  1357. if (txstatus & 0x8000) {
  1358. if (debug > 1)
  1359. netdev_dbg(dev, "Transmit error, Tx status %08x\n",
  1360. txstatus);
  1361. dev->stats.tx_errors++;
  1362. if (txstatus & 0x0400)
  1363. dev->stats.tx_carrier_errors++;
  1364. if (txstatus & 0x0200)
  1365. dev->stats.tx_window_errors++;
  1366. if (txstatus & 0x0100)
  1367. dev->stats.tx_aborted_errors++;
  1368. if (txstatus & 0x0080)
  1369. dev->stats.tx_heartbeat_errors++;
  1370. if (((rp->quirks & rqRhineI) && txstatus & 0x0002) ||
  1371. (txstatus & 0x0800) || (txstatus & 0x1000)) {
  1372. dev->stats.tx_fifo_errors++;
  1373. rp->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
  1374. break; /* Keep the skb - we try again */
  1375. }
  1376. /* Transmitter restarted in 'abnormal' handler. */
  1377. } else {
  1378. if (rp->quirks & rqRhineI)
  1379. dev->stats.collisions += (txstatus >> 3) & 0x0F;
  1380. else
  1381. dev->stats.collisions += txstatus & 0x0F;
  1382. if (debug > 6)
  1383. netdev_dbg(dev, "collisions: %1.1x:%1.1x\n",
  1384. (txstatus >> 3) & 0xF,
  1385. txstatus & 0xF);
  1386. dev->stats.tx_bytes += rp->tx_skbuff[entry]->len;
  1387. dev->stats.tx_packets++;
  1388. }
  1389. /* Free the original skb. */
  1390. if (rp->tx_skbuff_dma[entry]) {
  1391. pci_unmap_single(rp->pdev,
  1392. rp->tx_skbuff_dma[entry],
  1393. rp->tx_skbuff[entry]->len,
  1394. PCI_DMA_TODEVICE);
  1395. }
  1396. dev_kfree_skb_irq(rp->tx_skbuff[entry]);
  1397. rp->tx_skbuff[entry] = NULL;
  1398. entry = (++rp->dirty_tx) % TX_RING_SIZE;
  1399. }
  1400. if ((rp->cur_tx - rp->dirty_tx) < TX_QUEUE_LEN - 4)
  1401. netif_wake_queue(dev);
  1402. spin_unlock(&rp->lock);
  1403. }
  1404. /**
  1405. * rhine_get_vlan_tci - extract TCI from Rx data buffer
  1406. * @skb: pointer to sk_buff
  1407. * @data_size: used data area of the buffer including CRC
  1408. *
  1409. * If hardware VLAN tag extraction is enabled and the chip indicates a 802.1Q
  1410. * packet, the extracted 802.1Q header (2 bytes TPID + 2 bytes TCI) is 4-byte
  1411. * aligned following the CRC.
  1412. */
  1413. static inline u16 rhine_get_vlan_tci(struct sk_buff *skb, int data_size)
  1414. {
  1415. u8 *trailer = (u8 *)skb->data + ((data_size + 3) & ~3) + 2;
  1416. return be16_to_cpup((__be16 *)trailer);
  1417. }
  1418. /* Process up to limit frames from receive ring */
  1419. static int rhine_rx(struct net_device *dev, int limit)
  1420. {
  1421. struct rhine_private *rp = netdev_priv(dev);
  1422. int count;
  1423. int entry = rp->cur_rx % RX_RING_SIZE;
  1424. if (debug > 4) {
  1425. netdev_dbg(dev, "%s(), entry %d status %08x\n",
  1426. __func__, entry,
  1427. le32_to_cpu(rp->rx_head_desc->rx_status));
  1428. }
  1429. /* If EOP is set on the next entry, it's a new packet. Send it up. */
  1430. for (count = 0; count < limit; ++count) {
  1431. struct rx_desc *desc = rp->rx_head_desc;
  1432. u32 desc_status = le32_to_cpu(desc->rx_status);
  1433. u32 desc_length = le32_to_cpu(desc->desc_length);
  1434. int data_size = desc_status >> 16;
  1435. if (desc_status & DescOwn)
  1436. break;
  1437. if (debug > 4)
  1438. netdev_dbg(dev, "%s() status is %08x\n",
  1439. __func__, desc_status);
  1440. if ((desc_status & (RxWholePkt | RxErr)) != RxWholePkt) {
  1441. if ((desc_status & RxWholePkt) != RxWholePkt) {
  1442. netdev_warn(dev,
  1443. "Oversized Ethernet frame spanned multiple buffers, "
  1444. "entry %#x length %d status %08x!\n",
  1445. entry, data_size,
  1446. desc_status);
  1447. netdev_warn(dev,
  1448. "Oversized Ethernet frame %p vs %p\n",
  1449. rp->rx_head_desc,
  1450. &rp->rx_ring[entry]);
  1451. dev->stats.rx_length_errors++;
  1452. } else if (desc_status & RxErr) {
  1453. /* There was a error. */
  1454. if (debug > 2)
  1455. netdev_dbg(dev, "%s() Rx error was %08x\n",
  1456. __func__, desc_status);
  1457. dev->stats.rx_errors++;
  1458. if (desc_status & 0x0030)
  1459. dev->stats.rx_length_errors++;
  1460. if (desc_status & 0x0048)
  1461. dev->stats.rx_fifo_errors++;
  1462. if (desc_status & 0x0004)
  1463. dev->stats.rx_frame_errors++;
  1464. if (desc_status & 0x0002) {
  1465. /* this can also be updated outside the interrupt handler */
  1466. spin_lock(&rp->lock);
  1467. dev->stats.rx_crc_errors++;
  1468. spin_unlock(&rp->lock);
  1469. }
  1470. }
  1471. } else {
  1472. struct sk_buff *skb = NULL;
  1473. /* Length should omit the CRC */
  1474. int pkt_len = data_size - 4;
  1475. u16 vlan_tci = 0;
  1476. /* Check if the packet is long enough to accept without
  1477. copying to a minimally-sized skbuff. */
  1478. if (pkt_len < rx_copybreak)
  1479. skb = netdev_alloc_skb_ip_align(dev, pkt_len);
  1480. if (skb) {
  1481. pci_dma_sync_single_for_cpu(rp->pdev,
  1482. rp->rx_skbuff_dma[entry],
  1483. rp->rx_buf_sz,
  1484. PCI_DMA_FROMDEVICE);
  1485. skb_copy_to_linear_data(skb,
  1486. rp->rx_skbuff[entry]->data,
  1487. pkt_len);
  1488. skb_put(skb, pkt_len);
  1489. pci_dma_sync_single_for_device(rp->pdev,
  1490. rp->rx_skbuff_dma[entry],
  1491. rp->rx_buf_sz,
  1492. PCI_DMA_FROMDEVICE);
  1493. } else {
  1494. skb = rp->rx_skbuff[entry];
  1495. if (skb == NULL) {
  1496. netdev_err(dev, "Inconsistent Rx descriptor chain\n");
  1497. break;
  1498. }
  1499. rp->rx_skbuff[entry] = NULL;
  1500. skb_put(skb, pkt_len);
  1501. pci_unmap_single(rp->pdev,
  1502. rp->rx_skbuff_dma[entry],
  1503. rp->rx_buf_sz,
  1504. PCI_DMA_FROMDEVICE);
  1505. }
  1506. if (unlikely(desc_length & DescTag))
  1507. vlan_tci = rhine_get_vlan_tci(skb, data_size);
  1508. skb->protocol = eth_type_trans(skb, dev);
  1509. if (unlikely(desc_length & DescTag))
  1510. __vlan_hwaccel_put_tag(skb, vlan_tci);
  1511. netif_receive_skb(skb);
  1512. dev->stats.rx_bytes += pkt_len;
  1513. dev->stats.rx_packets++;
  1514. }
  1515. entry = (++rp->cur_rx) % RX_RING_SIZE;
  1516. rp->rx_head_desc = &rp->rx_ring[entry];
  1517. }
  1518. /* Refill the Rx ring buffers. */
  1519. for (; rp->cur_rx - rp->dirty_rx > 0; rp->dirty_rx++) {
  1520. struct sk_buff *skb;
  1521. entry = rp->dirty_rx % RX_RING_SIZE;
  1522. if (rp->rx_skbuff[entry] == NULL) {
  1523. skb = netdev_alloc_skb(dev, rp->rx_buf_sz);
  1524. rp->rx_skbuff[entry] = skb;
  1525. if (skb == NULL)
  1526. break; /* Better luck next round. */
  1527. skb->dev = dev; /* Mark as being used by this device. */
  1528. rp->rx_skbuff_dma[entry] =
  1529. pci_map_single(rp->pdev, skb->data,
  1530. rp->rx_buf_sz,
  1531. PCI_DMA_FROMDEVICE);
  1532. rp->rx_ring[entry].addr = cpu_to_le32(rp->rx_skbuff_dma[entry]);
  1533. }
  1534. rp->rx_ring[entry].rx_status = cpu_to_le32(DescOwn);
  1535. }
  1536. return count;
  1537. }
  1538. /*
  1539. * Clears the "tally counters" for CRC errors and missed frames(?).
  1540. * It has been reported that some chips need a write of 0 to clear
  1541. * these, for others the counters are set to 1 when written to and
  1542. * instead cleared when read. So we clear them both ways ...
  1543. */
  1544. static inline void clear_tally_counters(void __iomem *ioaddr)
  1545. {
  1546. iowrite32(0, ioaddr + RxMissed);
  1547. ioread16(ioaddr + RxCRCErrs);
  1548. ioread16(ioaddr + RxMissed);
  1549. }
  1550. static void rhine_restart_tx(struct net_device *dev) {
  1551. struct rhine_private *rp = netdev_priv(dev);
  1552. void __iomem *ioaddr = rp->base;
  1553. int entry = rp->dirty_tx % TX_RING_SIZE;
  1554. u32 intr_status;
  1555. /*
  1556. * If new errors occurred, we need to sort them out before doing Tx.
  1557. * In that case the ISR will be back here RSN anyway.
  1558. */
  1559. intr_status = get_intr_status(dev);
  1560. if ((intr_status & IntrTxErrSummary) == 0) {
  1561. /* We know better than the chip where it should continue. */
  1562. iowrite32(rp->tx_ring_dma + entry * sizeof(struct tx_desc),
  1563. ioaddr + TxRingPtr);
  1564. iowrite8(ioread8(ioaddr + ChipCmd) | CmdTxOn,
  1565. ioaddr + ChipCmd);
  1566. if (rp->tx_ring[entry].desc_length & cpu_to_le32(0x020000))
  1567. /* Tx queues are bits 7-0 (first Tx queue: bit 7) */
  1568. BYTE_REG_BITS_ON(1 << 7, ioaddr + TQWake);
  1569. iowrite8(ioread8(ioaddr + ChipCmd1) | Cmd1TxDemand,
  1570. ioaddr + ChipCmd1);
  1571. IOSYNC;
  1572. }
  1573. else {
  1574. /* This should never happen */
  1575. if (debug > 1)
  1576. netdev_warn(dev, "%s() Another error occurred %08x\n",
  1577. __func__, intr_status);
  1578. }
  1579. }
  1580. static void rhine_error(struct net_device *dev, int intr_status)
  1581. {
  1582. struct rhine_private *rp = netdev_priv(dev);
  1583. void __iomem *ioaddr = rp->base;
  1584. spin_lock(&rp->lock);
  1585. if (intr_status & IntrLinkChange)
  1586. rhine_check_media(dev, 0);
  1587. if (intr_status & IntrStatsMax) {
  1588. dev->stats.rx_crc_errors += ioread16(ioaddr + RxCRCErrs);
  1589. dev->stats.rx_missed_errors += ioread16(ioaddr + RxMissed);
  1590. clear_tally_counters(ioaddr);
  1591. }
  1592. if (intr_status & IntrTxAborted) {
  1593. if (debug > 1)
  1594. netdev_info(dev, "Abort %08x, frame dropped\n",
  1595. intr_status);
  1596. }
  1597. if (intr_status & IntrTxUnderrun) {
  1598. if (rp->tx_thresh < 0xE0)
  1599. BYTE_REG_BITS_SET((rp->tx_thresh += 0x20), 0x80, ioaddr + TxConfig);
  1600. if (debug > 1)
  1601. netdev_info(dev, "Transmitter underrun, Tx threshold now %02x\n",
  1602. rp->tx_thresh);
  1603. }
  1604. if (intr_status & IntrTxDescRace) {
  1605. if (debug > 2)
  1606. netdev_info(dev, "Tx descriptor write-back race\n");
  1607. }
  1608. if ((intr_status & IntrTxError) &&
  1609. (intr_status & (IntrTxAborted |
  1610. IntrTxUnderrun | IntrTxDescRace)) == 0) {
  1611. if (rp->tx_thresh < 0xE0) {
  1612. BYTE_REG_BITS_SET((rp->tx_thresh += 0x20), 0x80, ioaddr + TxConfig);
  1613. }
  1614. if (debug > 1)
  1615. netdev_info(dev, "Unspecified error. Tx threshold now %02x\n",
  1616. rp->tx_thresh);
  1617. }
  1618. if (intr_status & (IntrTxAborted | IntrTxUnderrun | IntrTxDescRace |
  1619. IntrTxError))
  1620. rhine_restart_tx(dev);
  1621. if (intr_status & ~(IntrLinkChange | IntrStatsMax | IntrTxUnderrun |
  1622. IntrTxError | IntrTxAborted | IntrNormalSummary |
  1623. IntrTxDescRace)) {
  1624. if (debug > 1)
  1625. netdev_err(dev, "Something Wicked happened! %08x\n",
  1626. intr_status);
  1627. }
  1628. spin_unlock(&rp->lock);
  1629. }
  1630. static struct net_device_stats *rhine_get_stats(struct net_device *dev)
  1631. {
  1632. struct rhine_private *rp = netdev_priv(dev);
  1633. void __iomem *ioaddr = rp->base;
  1634. unsigned long flags;
  1635. spin_lock_irqsave(&rp->lock, flags);
  1636. dev->stats.rx_crc_errors += ioread16(ioaddr + RxCRCErrs);
  1637. dev->stats.rx_missed_errors += ioread16(ioaddr + RxMissed);
  1638. clear_tally_counters(ioaddr);
  1639. spin_unlock_irqrestore(&rp->lock, flags);
  1640. return &dev->stats;
  1641. }
  1642. static void rhine_set_rx_mode(struct net_device *dev)
  1643. {
  1644. struct rhine_private *rp = netdev_priv(dev);
  1645. void __iomem *ioaddr = rp->base;
  1646. u32 mc_filter[2]; /* Multicast hash filter */
  1647. u8 rx_mode = 0x0C; /* Note: 0x02=accept runt, 0x01=accept errs */
  1648. struct netdev_hw_addr *ha;
  1649. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1650. rx_mode = 0x1C;
  1651. iowrite32(0xffffffff, ioaddr + MulticastFilter0);
  1652. iowrite32(0xffffffff, ioaddr + MulticastFilter1);
  1653. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  1654. (dev->flags & IFF_ALLMULTI)) {
  1655. /* Too many to match, or accept all multicasts. */
  1656. iowrite32(0xffffffff, ioaddr + MulticastFilter0);
  1657. iowrite32(0xffffffff, ioaddr + MulticastFilter1);
  1658. } else if (rp->pdev->revision >= VT6105M) {
  1659. int i = 0;
  1660. u32 mCAMmask = 0; /* 32 mCAMs (6105M and better) */
  1661. netdev_for_each_mc_addr(ha, dev) {
  1662. if (i == MCAM_SIZE)
  1663. break;
  1664. rhine_set_cam(ioaddr, i, ha->addr);
  1665. mCAMmask |= 1 << i;
  1666. i++;
  1667. }
  1668. rhine_set_cam_mask(ioaddr, mCAMmask);
  1669. } else {
  1670. memset(mc_filter, 0, sizeof(mc_filter));
  1671. netdev_for_each_mc_addr(ha, dev) {
  1672. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  1673. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1674. }
  1675. iowrite32(mc_filter[0], ioaddr + MulticastFilter0);
  1676. iowrite32(mc_filter[1], ioaddr + MulticastFilter1);
  1677. }
  1678. /* enable/disable VLAN receive filtering */
  1679. if (rp->pdev->revision >= VT6105M) {
  1680. if (dev->flags & IFF_PROMISC)
  1681. BYTE_REG_BITS_OFF(BCR1_VIDFR, ioaddr + PCIBusConfig1);
  1682. else
  1683. BYTE_REG_BITS_ON(BCR1_VIDFR, ioaddr + PCIBusConfig1);
  1684. }
  1685. BYTE_REG_BITS_ON(rx_mode, ioaddr + RxConfig);
  1686. }
  1687. static void netdev_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1688. {
  1689. struct rhine_private *rp = netdev_priv(dev);
  1690. strcpy(info->driver, DRV_NAME);
  1691. strcpy(info->version, DRV_VERSION);
  1692. strcpy(info->bus_info, pci_name(rp->pdev));
  1693. }
  1694. static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1695. {
  1696. struct rhine_private *rp = netdev_priv(dev);
  1697. int rc;
  1698. spin_lock_irq(&rp->lock);
  1699. rc = mii_ethtool_gset(&rp->mii_if, cmd);
  1700. spin_unlock_irq(&rp->lock);
  1701. return rc;
  1702. }
  1703. static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1704. {
  1705. struct rhine_private *rp = netdev_priv(dev);
  1706. int rc;
  1707. spin_lock_irq(&rp->lock);
  1708. rc = mii_ethtool_sset(&rp->mii_if, cmd);
  1709. spin_unlock_irq(&rp->lock);
  1710. rhine_set_carrier(&rp->mii_if);
  1711. return rc;
  1712. }
  1713. static int netdev_nway_reset(struct net_device *dev)
  1714. {
  1715. struct rhine_private *rp = netdev_priv(dev);
  1716. return mii_nway_restart(&rp->mii_if);
  1717. }
  1718. static u32 netdev_get_link(struct net_device *dev)
  1719. {
  1720. struct rhine_private *rp = netdev_priv(dev);
  1721. return mii_link_ok(&rp->mii_if);
  1722. }
  1723. static u32 netdev_get_msglevel(struct net_device *dev)
  1724. {
  1725. return debug;
  1726. }
  1727. static void netdev_set_msglevel(struct net_device *dev, u32 value)
  1728. {
  1729. debug = value;
  1730. }
  1731. static void rhine_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1732. {
  1733. struct rhine_private *rp = netdev_priv(dev);
  1734. if (!(rp->quirks & rqWOL))
  1735. return;
  1736. spin_lock_irq(&rp->lock);
  1737. wol->supported = WAKE_PHY | WAKE_MAGIC |
  1738. WAKE_UCAST | WAKE_MCAST | WAKE_BCAST; /* Untested */
  1739. wol->wolopts = rp->wolopts;
  1740. spin_unlock_irq(&rp->lock);
  1741. }
  1742. static int rhine_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1743. {
  1744. struct rhine_private *rp = netdev_priv(dev);
  1745. u32 support = WAKE_PHY | WAKE_MAGIC |
  1746. WAKE_UCAST | WAKE_MCAST | WAKE_BCAST; /* Untested */
  1747. if (!(rp->quirks & rqWOL))
  1748. return -EINVAL;
  1749. if (wol->wolopts & ~support)
  1750. return -EINVAL;
  1751. spin_lock_irq(&rp->lock);
  1752. rp->wolopts = wol->wolopts;
  1753. spin_unlock_irq(&rp->lock);
  1754. return 0;
  1755. }
  1756. static const struct ethtool_ops netdev_ethtool_ops = {
  1757. .get_drvinfo = netdev_get_drvinfo,
  1758. .get_settings = netdev_get_settings,
  1759. .set_settings = netdev_set_settings,
  1760. .nway_reset = netdev_nway_reset,
  1761. .get_link = netdev_get_link,
  1762. .get_msglevel = netdev_get_msglevel,
  1763. .set_msglevel = netdev_set_msglevel,
  1764. .get_wol = rhine_get_wol,
  1765. .set_wol = rhine_set_wol,
  1766. };
  1767. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1768. {
  1769. struct rhine_private *rp = netdev_priv(dev);
  1770. int rc;
  1771. if (!netif_running(dev))
  1772. return -EINVAL;
  1773. spin_lock_irq(&rp->lock);
  1774. rc = generic_mii_ioctl(&rp->mii_if, if_mii(rq), cmd, NULL);
  1775. spin_unlock_irq(&rp->lock);
  1776. rhine_set_carrier(&rp->mii_if);
  1777. return rc;
  1778. }
  1779. static int rhine_close(struct net_device *dev)
  1780. {
  1781. struct rhine_private *rp = netdev_priv(dev);
  1782. void __iomem *ioaddr = rp->base;
  1783. napi_disable(&rp->napi);
  1784. cancel_work_sync(&rp->reset_task);
  1785. netif_stop_queue(dev);
  1786. spin_lock_irq(&rp->lock);
  1787. if (debug > 1)
  1788. netdev_dbg(dev, "Shutting down ethercard, status was %04x\n",
  1789. ioread16(ioaddr + ChipCmd));
  1790. /* Switch to loopback mode to avoid hardware races. */
  1791. iowrite8(rp->tx_thresh | 0x02, ioaddr + TxConfig);
  1792. /* Disable interrupts by clearing the interrupt mask. */
  1793. iowrite16(0x0000, ioaddr + IntrEnable);
  1794. /* Stop the chip's Tx and Rx processes. */
  1795. iowrite16(CmdStop, ioaddr + ChipCmd);
  1796. spin_unlock_irq(&rp->lock);
  1797. free_irq(rp->pdev->irq, dev);
  1798. free_rbufs(dev);
  1799. free_tbufs(dev);
  1800. free_ring(dev);
  1801. return 0;
  1802. }
  1803. static void __devexit rhine_remove_one(struct pci_dev *pdev)
  1804. {
  1805. struct net_device *dev = pci_get_drvdata(pdev);
  1806. struct rhine_private *rp = netdev_priv(dev);
  1807. unregister_netdev(dev);
  1808. pci_iounmap(pdev, rp->base);
  1809. pci_release_regions(pdev);
  1810. free_netdev(dev);
  1811. pci_disable_device(pdev);
  1812. pci_set_drvdata(pdev, NULL);
  1813. }
  1814. static void rhine_shutdown (struct pci_dev *pdev)
  1815. {
  1816. struct net_device *dev = pci_get_drvdata(pdev);
  1817. struct rhine_private *rp = netdev_priv(dev);
  1818. void __iomem *ioaddr = rp->base;
  1819. if (!(rp->quirks & rqWOL))
  1820. return; /* Nothing to do for non-WOL adapters */
  1821. rhine_power_init(dev);
  1822. /* Make sure we use pattern 0, 1 and not 4, 5 */
  1823. if (rp->quirks & rq6patterns)
  1824. iowrite8(0x04, ioaddr + WOLcgClr);
  1825. if (rp->wolopts & WAKE_MAGIC) {
  1826. iowrite8(WOLmagic, ioaddr + WOLcrSet);
  1827. /*
  1828. * Turn EEPROM-controlled wake-up back on -- some hardware may
  1829. * not cooperate otherwise.
  1830. */
  1831. iowrite8(ioread8(ioaddr + ConfigA) | 0x03, ioaddr + ConfigA);
  1832. }
  1833. if (rp->wolopts & (WAKE_BCAST|WAKE_MCAST))
  1834. iowrite8(WOLbmcast, ioaddr + WOLcgSet);
  1835. if (rp->wolopts & WAKE_PHY)
  1836. iowrite8(WOLlnkon | WOLlnkoff, ioaddr + WOLcrSet);
  1837. if (rp->wolopts & WAKE_UCAST)
  1838. iowrite8(WOLucast, ioaddr + WOLcrSet);
  1839. if (rp->wolopts) {
  1840. /* Enable legacy WOL (for old motherboards) */
  1841. iowrite8(0x01, ioaddr + PwcfgSet);
  1842. iowrite8(ioread8(ioaddr + StickyHW) | 0x04, ioaddr + StickyHW);
  1843. }
  1844. /* Hit power state D3 (sleep) */
  1845. if (!avoid_D3)
  1846. iowrite8(ioread8(ioaddr + StickyHW) | 0x03, ioaddr + StickyHW);
  1847. /* TODO: Check use of pci_enable_wake() */
  1848. }
  1849. #ifdef CONFIG_PM
  1850. static int rhine_suspend(struct pci_dev *pdev, pm_message_t state)
  1851. {
  1852. struct net_device *dev = pci_get_drvdata(pdev);
  1853. struct rhine_private *rp = netdev_priv(dev);
  1854. unsigned long flags;
  1855. if (!netif_running(dev))
  1856. return 0;
  1857. napi_disable(&rp->napi);
  1858. netif_device_detach(dev);
  1859. pci_save_state(pdev);
  1860. spin_lock_irqsave(&rp->lock, flags);
  1861. rhine_shutdown(pdev);
  1862. spin_unlock_irqrestore(&rp->lock, flags);
  1863. free_irq(dev->irq, dev);
  1864. return 0;
  1865. }
  1866. static int rhine_resume(struct pci_dev *pdev)
  1867. {
  1868. struct net_device *dev = pci_get_drvdata(pdev);
  1869. struct rhine_private *rp = netdev_priv(dev);
  1870. unsigned long flags;
  1871. int ret;
  1872. if (!netif_running(dev))
  1873. return 0;
  1874. if (request_irq(dev->irq, rhine_interrupt, IRQF_SHARED, dev->name, dev))
  1875. netdev_err(dev, "request_irq failed\n");
  1876. ret = pci_set_power_state(pdev, PCI_D0);
  1877. if (debug > 1)
  1878. netdev_info(dev, "Entering power state D0 %s (%d)\n",
  1879. ret ? "failed" : "succeeded", ret);
  1880. pci_restore_state(pdev);
  1881. spin_lock_irqsave(&rp->lock, flags);
  1882. #ifdef USE_MMIO
  1883. enable_mmio(rp->pioaddr, rp->quirks);
  1884. #endif
  1885. rhine_power_init(dev);
  1886. free_tbufs(dev);
  1887. free_rbufs(dev);
  1888. alloc_tbufs(dev);
  1889. alloc_rbufs(dev);
  1890. init_registers(dev);
  1891. spin_unlock_irqrestore(&rp->lock, flags);
  1892. netif_device_attach(dev);
  1893. return 0;
  1894. }
  1895. #endif /* CONFIG_PM */
  1896. static struct pci_driver rhine_driver = {
  1897. .name = DRV_NAME,
  1898. .id_table = rhine_pci_tbl,
  1899. .probe = rhine_init_one,
  1900. .remove = __devexit_p(rhine_remove_one),
  1901. #ifdef CONFIG_PM
  1902. .suspend = rhine_suspend,
  1903. .resume = rhine_resume,
  1904. #endif /* CONFIG_PM */
  1905. .shutdown = rhine_shutdown,
  1906. };
  1907. static struct dmi_system_id __initdata rhine_dmi_table[] = {
  1908. {
  1909. .ident = "EPIA-M",
  1910. .matches = {
  1911. DMI_MATCH(DMI_BIOS_VENDOR, "Award Software International, Inc."),
  1912. DMI_MATCH(DMI_BIOS_VERSION, "6.00 PG"),
  1913. },
  1914. },
  1915. {
  1916. .ident = "KV7",
  1917. .matches = {
  1918. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  1919. DMI_MATCH(DMI_BIOS_VERSION, "6.00 PG"),
  1920. },
  1921. },
  1922. { NULL }
  1923. };
  1924. static int __init rhine_init(void)
  1925. {
  1926. /* when a module, this is printed whether or not devices are found in probe */
  1927. #ifdef MODULE
  1928. pr_info("%s\n", version);
  1929. #endif
  1930. if (dmi_check_system(rhine_dmi_table)) {
  1931. /* these BIOSes fail at PXE boot if chip is in D3 */
  1932. avoid_D3 = 1;
  1933. pr_warn("Broken BIOS detected, avoid_D3 enabled\n");
  1934. }
  1935. else if (avoid_D3)
  1936. pr_info("avoid_D3 set\n");
  1937. return pci_register_driver(&rhine_driver);
  1938. }
  1939. static void __exit rhine_cleanup(void)
  1940. {
  1941. pci_unregister_driver(&rhine_driver);
  1942. }
  1943. module_init(rhine_init);
  1944. module_exit(rhine_cleanup);