via-rhine.c 56 KB

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