via-rhine.c 70 KB

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