r8169.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  1. /*
  2. * r8169.c: RealTek 8169/8168/8101 ethernet driver.
  3. *
  4. * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
  5. * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
  6. * Copyright (c) a lot of people too. Please respect their work.
  7. *
  8. * See MAINTAINERS file for support contact information.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/pci.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/delay.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/mii.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/crc32.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/tcp.h>
  23. #include <linux/init.h>
  24. #include <linux/dma-mapping.h>
  25. #include <asm/system.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #ifdef CONFIG_R8169_NAPI
  29. #define NAPI_SUFFIX "-NAPI"
  30. #else
  31. #define NAPI_SUFFIX ""
  32. #endif
  33. #define RTL8169_VERSION "2.2LK" NAPI_SUFFIX
  34. #define MODULENAME "r8169"
  35. #define PFX MODULENAME ": "
  36. #ifdef RTL8169_DEBUG
  37. #define assert(expr) \
  38. if (!(expr)) { \
  39. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  40. #expr,__FILE__,__FUNCTION__,__LINE__); \
  41. }
  42. #define dprintk(fmt, args...) do { printk(PFX fmt, ## args); } while (0)
  43. #else
  44. #define assert(expr) do {} while (0)
  45. #define dprintk(fmt, args...) do {} while (0)
  46. #endif /* RTL8169_DEBUG */
  47. #define R8169_MSG_DEFAULT \
  48. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  49. #define TX_BUFFS_AVAIL(tp) \
  50. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  51. #ifdef CONFIG_R8169_NAPI
  52. #define rtl8169_rx_skb netif_receive_skb
  53. #define rtl8169_rx_hwaccel_skb vlan_hwaccel_receive_skb
  54. #define rtl8169_rx_quota(count, quota) min(count, quota)
  55. #else
  56. #define rtl8169_rx_skb netif_rx
  57. #define rtl8169_rx_hwaccel_skb vlan_hwaccel_rx
  58. #define rtl8169_rx_quota(count, quota) count
  59. #endif
  60. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  61. static const int max_interrupt_work = 20;
  62. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  63. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  64. static const int multicast_filter_limit = 32;
  65. /* MAC address length */
  66. #define MAC_ADDR_LEN 6
  67. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  68. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  69. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  70. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  71. #define RxPacketMaxSize 0x3FE8 /* 16K - 1 - ETH_HLEN - VLAN - CRC... */
  72. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  73. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  74. #define R8169_REGS_SIZE 256
  75. #define R8169_NAPI_WEIGHT 64
  76. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  77. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  78. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  79. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  80. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  81. #define RTL8169_TX_TIMEOUT (6*HZ)
  82. #define RTL8169_PHY_TIMEOUT (10*HZ)
  83. /* write/read MMIO register */
  84. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  85. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  86. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  87. #define RTL_R8(reg) readb (ioaddr + (reg))
  88. #define RTL_R16(reg) readw (ioaddr + (reg))
  89. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  90. enum mac_version {
  91. RTL_GIGA_MAC_VER_01 = 0x01, // 8169
  92. RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
  93. RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
  94. RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
  95. RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
  96. RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
  97. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  98. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be 8168Bf
  99. RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb 8101Ec
  100. RTL_GIGA_MAC_VER_14 = 0x0e, // 8101
  101. RTL_GIGA_MAC_VER_15 = 0x0f // 8101
  102. };
  103. enum phy_version {
  104. RTL_GIGA_PHY_VER_C = 0x03, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  105. RTL_GIGA_PHY_VER_D = 0x04, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  106. RTL_GIGA_PHY_VER_E = 0x05, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  107. RTL_GIGA_PHY_VER_F = 0x06, /* PHY Reg 0x03 bit0-3 == 0x0001 */
  108. RTL_GIGA_PHY_VER_G = 0x07, /* PHY Reg 0x03 bit0-3 == 0x0002 */
  109. RTL_GIGA_PHY_VER_H = 0x08, /* PHY Reg 0x03 bit0-3 == 0x0003 */
  110. };
  111. #define _R(NAME,MAC,MASK) \
  112. { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
  113. static const struct {
  114. const char *name;
  115. u8 mac_version;
  116. u32 RxConfigMask; /* Clears the bits supported by this chip */
  117. } rtl_chip_info[] = {
  118. _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
  119. _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
  120. _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
  121. _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
  122. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
  123. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
  124. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
  125. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
  126. _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
  127. _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
  128. _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880) // PCI-E 8139
  129. };
  130. #undef _R
  131. enum cfg_version {
  132. RTL_CFG_0 = 0x00,
  133. RTL_CFG_1,
  134. RTL_CFG_2
  135. };
  136. static void rtl_hw_start_8169(struct net_device *);
  137. static void rtl_hw_start_8168(struct net_device *);
  138. static void rtl_hw_start_8101(struct net_device *);
  139. static struct pci_device_id rtl8169_pci_tbl[] = {
  140. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  141. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  142. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  143. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  144. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  145. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  146. { PCI_DEVICE(0x1259, 0xc107), 0, 0, RTL_CFG_0 },
  147. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  148. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  149. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  150. {0,},
  151. };
  152. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  153. static int rx_copybreak = 200;
  154. static int use_dac;
  155. static struct {
  156. u32 msg_enable;
  157. } debug = { -1 };
  158. enum rtl_registers {
  159. MAC0 = 0, /* Ethernet hardware address. */
  160. MAC4 = 4,
  161. MAR0 = 8, /* Multicast filter. */
  162. CounterAddrLow = 0x10,
  163. CounterAddrHigh = 0x14,
  164. TxDescStartAddrLow = 0x20,
  165. TxDescStartAddrHigh = 0x24,
  166. TxHDescStartAddrLow = 0x28,
  167. TxHDescStartAddrHigh = 0x2c,
  168. FLASH = 0x30,
  169. ERSR = 0x36,
  170. ChipCmd = 0x37,
  171. TxPoll = 0x38,
  172. IntrMask = 0x3c,
  173. IntrStatus = 0x3e,
  174. TxConfig = 0x40,
  175. RxConfig = 0x44,
  176. RxMissed = 0x4c,
  177. Cfg9346 = 0x50,
  178. Config0 = 0x51,
  179. Config1 = 0x52,
  180. Config2 = 0x53,
  181. Config3 = 0x54,
  182. Config4 = 0x55,
  183. Config5 = 0x56,
  184. MultiIntr = 0x5c,
  185. PHYAR = 0x60,
  186. TBICSR = 0x64,
  187. TBI_ANAR = 0x68,
  188. TBI_LPAR = 0x6a,
  189. PHYstatus = 0x6c,
  190. RxMaxSize = 0xda,
  191. CPlusCmd = 0xe0,
  192. IntrMitigate = 0xe2,
  193. RxDescAddrLow = 0xe4,
  194. RxDescAddrHigh = 0xe8,
  195. EarlyTxThres = 0xec,
  196. FuncEvent = 0xf0,
  197. FuncEventMask = 0xf4,
  198. FuncPresetState = 0xf8,
  199. FuncForceEvent = 0xfc,
  200. };
  201. enum rtl_register_content {
  202. /* InterruptStatusBits */
  203. SYSErr = 0x8000,
  204. PCSTimeout = 0x4000,
  205. SWInt = 0x0100,
  206. TxDescUnavail = 0x0080,
  207. RxFIFOOver = 0x0040,
  208. LinkChg = 0x0020,
  209. RxOverflow = 0x0010,
  210. TxErr = 0x0008,
  211. TxOK = 0x0004,
  212. RxErr = 0x0002,
  213. RxOK = 0x0001,
  214. /* RxStatusDesc */
  215. RxFOVF = (1 << 23),
  216. RxRWT = (1 << 22),
  217. RxRES = (1 << 21),
  218. RxRUNT = (1 << 20),
  219. RxCRC = (1 << 19),
  220. /* ChipCmdBits */
  221. CmdReset = 0x10,
  222. CmdRxEnb = 0x08,
  223. CmdTxEnb = 0x04,
  224. RxBufEmpty = 0x01,
  225. /* TXPoll register p.5 */
  226. HPQ = 0x80, /* Poll cmd on the high prio queue */
  227. NPQ = 0x40, /* Poll cmd on the low prio queue */
  228. FSWInt = 0x01, /* Forced software interrupt */
  229. /* Cfg9346Bits */
  230. Cfg9346_Lock = 0x00,
  231. Cfg9346_Unlock = 0xc0,
  232. /* rx_mode_bits */
  233. AcceptErr = 0x20,
  234. AcceptRunt = 0x10,
  235. AcceptBroadcast = 0x08,
  236. AcceptMulticast = 0x04,
  237. AcceptMyPhys = 0x02,
  238. AcceptAllPhys = 0x01,
  239. /* RxConfigBits */
  240. RxCfgFIFOShift = 13,
  241. RxCfgDMAShift = 8,
  242. /* TxConfigBits */
  243. TxInterFrameGapShift = 24,
  244. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  245. /* Config1 register p.24 */
  246. PMEnable = (1 << 0), /* Power Management Enable */
  247. /* Config2 register p. 25 */
  248. PCI_Clock_66MHz = 0x01,
  249. PCI_Clock_33MHz = 0x00,
  250. /* Config3 register p.25 */
  251. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  252. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  253. /* Config5 register p.27 */
  254. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  255. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  256. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  257. LanWake = (1 << 1), /* LanWake enable/disable */
  258. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  259. /* TBICSR p.28 */
  260. TBIReset = 0x80000000,
  261. TBILoopback = 0x40000000,
  262. TBINwEnable = 0x20000000,
  263. TBINwRestart = 0x10000000,
  264. TBILinkOk = 0x02000000,
  265. TBINwComplete = 0x01000000,
  266. /* CPlusCmd p.31 */
  267. PktCntrDisable = (1 << 7), // 8168
  268. RxVlan = (1 << 6),
  269. RxChkSum = (1 << 5),
  270. PCIDAC = (1 << 4),
  271. PCIMulRW = (1 << 3),
  272. INTT_0 = 0x0000, // 8168
  273. INTT_1 = 0x0001, // 8168
  274. INTT_2 = 0x0002, // 8168
  275. INTT_3 = 0x0003, // 8168
  276. /* rtl8169_PHYstatus */
  277. TBI_Enable = 0x80,
  278. TxFlowCtrl = 0x40,
  279. RxFlowCtrl = 0x20,
  280. _1000bpsF = 0x10,
  281. _100bps = 0x08,
  282. _10bps = 0x04,
  283. LinkStatus = 0x02,
  284. FullDup = 0x01,
  285. /* _TBICSRBit */
  286. TBILinkOK = 0x02000000,
  287. /* DumpCounterCommand */
  288. CounterDump = 0x8,
  289. };
  290. enum desc_status_bit {
  291. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  292. RingEnd = (1 << 30), /* End of descriptor ring */
  293. FirstFrag = (1 << 29), /* First segment of a packet */
  294. LastFrag = (1 << 28), /* Final segment of a packet */
  295. /* Tx private */
  296. LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
  297. MSSShift = 16, /* MSS value position */
  298. MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
  299. IPCS = (1 << 18), /* Calculate IP checksum */
  300. UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
  301. TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
  302. TxVlanTag = (1 << 17), /* Add VLAN tag */
  303. /* Rx private */
  304. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  305. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  306. #define RxProtoUDP (PID1)
  307. #define RxProtoTCP (PID0)
  308. #define RxProtoIP (PID1 | PID0)
  309. #define RxProtoMask RxProtoIP
  310. IPFail = (1 << 16), /* IP checksum failed */
  311. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  312. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  313. RxVlanTag = (1 << 16), /* VLAN tag available */
  314. };
  315. #define RsvdMask 0x3fffc000
  316. struct TxDesc {
  317. __le32 opts1;
  318. __le32 opts2;
  319. __le64 addr;
  320. };
  321. struct RxDesc {
  322. __le32 opts1;
  323. __le32 opts2;
  324. __le64 addr;
  325. };
  326. struct ring_info {
  327. struct sk_buff *skb;
  328. u32 len;
  329. u8 __pad[sizeof(void *) - sizeof(u32)];
  330. };
  331. struct rtl8169_private {
  332. void __iomem *mmio_addr; /* memory map physical address */
  333. struct pci_dev *pci_dev; /* Index of PCI device */
  334. struct net_device *dev;
  335. struct net_device_stats stats; /* statistics of net device */
  336. spinlock_t lock; /* spin lock flag */
  337. u32 msg_enable;
  338. int chipset;
  339. int mac_version;
  340. int phy_version;
  341. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  342. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  343. u32 dirty_rx;
  344. u32 dirty_tx;
  345. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  346. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  347. dma_addr_t TxPhyAddr;
  348. dma_addr_t RxPhyAddr;
  349. struct sk_buff *Rx_skbuff[NUM_RX_DESC]; /* Rx data buffers */
  350. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  351. unsigned align;
  352. unsigned rx_buf_sz;
  353. struct timer_list timer;
  354. u16 cp_cmd;
  355. u16 intr_event;
  356. u16 napi_event;
  357. u16 intr_mask;
  358. int phy_auto_nego_reg;
  359. int phy_1000_ctrl_reg;
  360. #ifdef CONFIG_R8169_VLAN
  361. struct vlan_group *vlgrp;
  362. #endif
  363. int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
  364. void (*get_settings)(struct net_device *, struct ethtool_cmd *);
  365. void (*phy_reset_enable)(void __iomem *);
  366. void (*hw_start)(struct net_device *);
  367. unsigned int (*phy_reset_pending)(void __iomem *);
  368. unsigned int (*link_ok)(void __iomem *);
  369. struct delayed_work task;
  370. unsigned wol_enabled : 1;
  371. };
  372. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  373. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  374. module_param(rx_copybreak, int, 0);
  375. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  376. module_param(use_dac, int, 0);
  377. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  378. module_param_named(debug, debug.msg_enable, int, 0);
  379. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  380. MODULE_LICENSE("GPL");
  381. MODULE_VERSION(RTL8169_VERSION);
  382. static int rtl8169_open(struct net_device *dev);
  383. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev);
  384. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  385. static int rtl8169_init_ring(struct net_device *dev);
  386. static void rtl_hw_start(struct net_device *dev);
  387. static int rtl8169_close(struct net_device *dev);
  388. static void rtl_set_rx_mode(struct net_device *dev);
  389. static void rtl8169_tx_timeout(struct net_device *dev);
  390. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  391. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  392. void __iomem *);
  393. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  394. static void rtl8169_down(struct net_device *dev);
  395. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  396. #ifdef CONFIG_R8169_NAPI
  397. static int rtl8169_poll(struct net_device *dev, int *budget);
  398. #endif
  399. static const unsigned int rtl8169_rx_config =
  400. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  401. static void mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  402. {
  403. int i;
  404. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0xFF) << 16 | value);
  405. for (i = 20; i > 0; i--) {
  406. /*
  407. * Check if the RTL8169 has completed writing to the specified
  408. * MII register.
  409. */
  410. if (!(RTL_R32(PHYAR) & 0x80000000))
  411. break;
  412. udelay(25);
  413. }
  414. }
  415. static int mdio_read(void __iomem *ioaddr, int reg_addr)
  416. {
  417. int i, value = -1;
  418. RTL_W32(PHYAR, 0x0 | (reg_addr & 0xFF) << 16);
  419. for (i = 20; i > 0; i--) {
  420. /*
  421. * Check if the RTL8169 has completed retrieving data from
  422. * the specified MII register.
  423. */
  424. if (RTL_R32(PHYAR) & 0x80000000) {
  425. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  426. break;
  427. }
  428. udelay(25);
  429. }
  430. return value;
  431. }
  432. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  433. {
  434. RTL_W16(IntrMask, 0x0000);
  435. RTL_W16(IntrStatus, 0xffff);
  436. }
  437. static void rtl8169_asic_down(void __iomem *ioaddr)
  438. {
  439. RTL_W8(ChipCmd, 0x00);
  440. rtl8169_irq_mask_and_ack(ioaddr);
  441. RTL_R16(CPlusCmd);
  442. }
  443. static unsigned int rtl8169_tbi_reset_pending(void __iomem *ioaddr)
  444. {
  445. return RTL_R32(TBICSR) & TBIReset;
  446. }
  447. static unsigned int rtl8169_xmii_reset_pending(void __iomem *ioaddr)
  448. {
  449. return mdio_read(ioaddr, MII_BMCR) & BMCR_RESET;
  450. }
  451. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  452. {
  453. return RTL_R32(TBICSR) & TBILinkOk;
  454. }
  455. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  456. {
  457. return RTL_R8(PHYstatus) & LinkStatus;
  458. }
  459. static void rtl8169_tbi_reset_enable(void __iomem *ioaddr)
  460. {
  461. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  462. }
  463. static void rtl8169_xmii_reset_enable(void __iomem *ioaddr)
  464. {
  465. unsigned int val;
  466. val = mdio_read(ioaddr, MII_BMCR) | BMCR_RESET;
  467. mdio_write(ioaddr, MII_BMCR, val & 0xffff);
  468. }
  469. static void rtl8169_check_link_status(struct net_device *dev,
  470. struct rtl8169_private *tp,
  471. void __iomem *ioaddr)
  472. {
  473. unsigned long flags;
  474. spin_lock_irqsave(&tp->lock, flags);
  475. if (tp->link_ok(ioaddr)) {
  476. netif_carrier_on(dev);
  477. if (netif_msg_ifup(tp))
  478. printk(KERN_INFO PFX "%s: link up\n", dev->name);
  479. } else {
  480. if (netif_msg_ifdown(tp))
  481. printk(KERN_INFO PFX "%s: link down\n", dev->name);
  482. netif_carrier_off(dev);
  483. }
  484. spin_unlock_irqrestore(&tp->lock, flags);
  485. }
  486. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  487. {
  488. struct rtl8169_private *tp = netdev_priv(dev);
  489. void __iomem *ioaddr = tp->mmio_addr;
  490. u8 options;
  491. wol->wolopts = 0;
  492. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  493. wol->supported = WAKE_ANY;
  494. spin_lock_irq(&tp->lock);
  495. options = RTL_R8(Config1);
  496. if (!(options & PMEnable))
  497. goto out_unlock;
  498. options = RTL_R8(Config3);
  499. if (options & LinkUp)
  500. wol->wolopts |= WAKE_PHY;
  501. if (options & MagicPacket)
  502. wol->wolopts |= WAKE_MAGIC;
  503. options = RTL_R8(Config5);
  504. if (options & UWF)
  505. wol->wolopts |= WAKE_UCAST;
  506. if (options & BWF)
  507. wol->wolopts |= WAKE_BCAST;
  508. if (options & MWF)
  509. wol->wolopts |= WAKE_MCAST;
  510. out_unlock:
  511. spin_unlock_irq(&tp->lock);
  512. }
  513. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  514. {
  515. struct rtl8169_private *tp = netdev_priv(dev);
  516. void __iomem *ioaddr = tp->mmio_addr;
  517. unsigned int i;
  518. static struct {
  519. u32 opt;
  520. u16 reg;
  521. u8 mask;
  522. } cfg[] = {
  523. { WAKE_ANY, Config1, PMEnable },
  524. { WAKE_PHY, Config3, LinkUp },
  525. { WAKE_MAGIC, Config3, MagicPacket },
  526. { WAKE_UCAST, Config5, UWF },
  527. { WAKE_BCAST, Config5, BWF },
  528. { WAKE_MCAST, Config5, MWF },
  529. { WAKE_ANY, Config5, LanWake }
  530. };
  531. spin_lock_irq(&tp->lock);
  532. RTL_W8(Cfg9346, Cfg9346_Unlock);
  533. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  534. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  535. if (wol->wolopts & cfg[i].opt)
  536. options |= cfg[i].mask;
  537. RTL_W8(cfg[i].reg, options);
  538. }
  539. RTL_W8(Cfg9346, Cfg9346_Lock);
  540. tp->wol_enabled = (wol->wolopts) ? 1 : 0;
  541. spin_unlock_irq(&tp->lock);
  542. return 0;
  543. }
  544. static void rtl8169_get_drvinfo(struct net_device *dev,
  545. struct ethtool_drvinfo *info)
  546. {
  547. struct rtl8169_private *tp = netdev_priv(dev);
  548. strcpy(info->driver, MODULENAME);
  549. strcpy(info->version, RTL8169_VERSION);
  550. strcpy(info->bus_info, pci_name(tp->pci_dev));
  551. }
  552. static int rtl8169_get_regs_len(struct net_device *dev)
  553. {
  554. return R8169_REGS_SIZE;
  555. }
  556. static int rtl8169_set_speed_tbi(struct net_device *dev,
  557. u8 autoneg, u16 speed, u8 duplex)
  558. {
  559. struct rtl8169_private *tp = netdev_priv(dev);
  560. void __iomem *ioaddr = tp->mmio_addr;
  561. int ret = 0;
  562. u32 reg;
  563. reg = RTL_R32(TBICSR);
  564. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  565. (duplex == DUPLEX_FULL)) {
  566. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  567. } else if (autoneg == AUTONEG_ENABLE)
  568. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  569. else {
  570. if (netif_msg_link(tp)) {
  571. printk(KERN_WARNING "%s: "
  572. "incorrect speed setting refused in TBI mode\n",
  573. dev->name);
  574. }
  575. ret = -EOPNOTSUPP;
  576. }
  577. return ret;
  578. }
  579. static int rtl8169_set_speed_xmii(struct net_device *dev,
  580. u8 autoneg, u16 speed, u8 duplex)
  581. {
  582. struct rtl8169_private *tp = netdev_priv(dev);
  583. void __iomem *ioaddr = tp->mmio_addr;
  584. int auto_nego, giga_ctrl;
  585. auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
  586. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  587. ADVERTISE_100HALF | ADVERTISE_100FULL);
  588. giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
  589. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  590. if (autoneg == AUTONEG_ENABLE) {
  591. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  592. ADVERTISE_100HALF | ADVERTISE_100FULL);
  593. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  594. } else {
  595. if (speed == SPEED_10)
  596. auto_nego |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  597. else if (speed == SPEED_100)
  598. auto_nego |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  599. else if (speed == SPEED_1000)
  600. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  601. if (duplex == DUPLEX_HALF)
  602. auto_nego &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
  603. if (duplex == DUPLEX_FULL)
  604. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
  605. /* This tweak comes straight from Realtek's driver. */
  606. if ((speed == SPEED_100) && (duplex == DUPLEX_HALF) &&
  607. (tp->mac_version == RTL_GIGA_MAC_VER_13)) {
  608. auto_nego = ADVERTISE_100HALF | ADVERTISE_CSMA;
  609. }
  610. }
  611. /* The 8100e/8101e do Fast Ethernet only. */
  612. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  613. (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
  614. (tp->mac_version == RTL_GIGA_MAC_VER_15)) {
  615. if ((giga_ctrl & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) &&
  616. netif_msg_link(tp)) {
  617. printk(KERN_INFO "%s: PHY does not support 1000Mbps.\n",
  618. dev->name);
  619. }
  620. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  621. }
  622. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  623. tp->phy_auto_nego_reg = auto_nego;
  624. tp->phy_1000_ctrl_reg = giga_ctrl;
  625. mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
  626. mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
  627. mdio_write(ioaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  628. return 0;
  629. }
  630. static int rtl8169_set_speed(struct net_device *dev,
  631. u8 autoneg, u16 speed, u8 duplex)
  632. {
  633. struct rtl8169_private *tp = netdev_priv(dev);
  634. int ret;
  635. ret = tp->set_speed(dev, autoneg, speed, duplex);
  636. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  637. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  638. return ret;
  639. }
  640. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  641. {
  642. struct rtl8169_private *tp = netdev_priv(dev);
  643. unsigned long flags;
  644. int ret;
  645. spin_lock_irqsave(&tp->lock, flags);
  646. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  647. spin_unlock_irqrestore(&tp->lock, flags);
  648. return ret;
  649. }
  650. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  651. {
  652. struct rtl8169_private *tp = netdev_priv(dev);
  653. return tp->cp_cmd & RxChkSum;
  654. }
  655. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  656. {
  657. struct rtl8169_private *tp = netdev_priv(dev);
  658. void __iomem *ioaddr = tp->mmio_addr;
  659. unsigned long flags;
  660. spin_lock_irqsave(&tp->lock, flags);
  661. if (data)
  662. tp->cp_cmd |= RxChkSum;
  663. else
  664. tp->cp_cmd &= ~RxChkSum;
  665. RTL_W16(CPlusCmd, tp->cp_cmd);
  666. RTL_R16(CPlusCmd);
  667. spin_unlock_irqrestore(&tp->lock, flags);
  668. return 0;
  669. }
  670. #ifdef CONFIG_R8169_VLAN
  671. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  672. struct sk_buff *skb)
  673. {
  674. return (tp->vlgrp && vlan_tx_tag_present(skb)) ?
  675. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  676. }
  677. static void rtl8169_vlan_rx_register(struct net_device *dev,
  678. struct vlan_group *grp)
  679. {
  680. struct rtl8169_private *tp = netdev_priv(dev);
  681. void __iomem *ioaddr = tp->mmio_addr;
  682. unsigned long flags;
  683. spin_lock_irqsave(&tp->lock, flags);
  684. tp->vlgrp = grp;
  685. if (tp->vlgrp)
  686. tp->cp_cmd |= RxVlan;
  687. else
  688. tp->cp_cmd &= ~RxVlan;
  689. RTL_W16(CPlusCmd, tp->cp_cmd);
  690. RTL_R16(CPlusCmd);
  691. spin_unlock_irqrestore(&tp->lock, flags);
  692. }
  693. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  694. struct sk_buff *skb)
  695. {
  696. u32 opts2 = le32_to_cpu(desc->opts2);
  697. int ret;
  698. if (tp->vlgrp && (opts2 & RxVlanTag)) {
  699. rtl8169_rx_hwaccel_skb(skb, tp->vlgrp, swab16(opts2 & 0xffff));
  700. ret = 0;
  701. } else
  702. ret = -1;
  703. desc->opts2 = 0;
  704. return ret;
  705. }
  706. #else /* !CONFIG_R8169_VLAN */
  707. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  708. struct sk_buff *skb)
  709. {
  710. return 0;
  711. }
  712. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  713. struct sk_buff *skb)
  714. {
  715. return -1;
  716. }
  717. #endif
  718. static void rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  719. {
  720. struct rtl8169_private *tp = netdev_priv(dev);
  721. void __iomem *ioaddr = tp->mmio_addr;
  722. u32 status;
  723. cmd->supported =
  724. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  725. cmd->port = PORT_FIBRE;
  726. cmd->transceiver = XCVR_INTERNAL;
  727. status = RTL_R32(TBICSR);
  728. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  729. cmd->autoneg = !!(status & TBINwEnable);
  730. cmd->speed = SPEED_1000;
  731. cmd->duplex = DUPLEX_FULL; /* Always set */
  732. }
  733. static void rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  734. {
  735. struct rtl8169_private *tp = netdev_priv(dev);
  736. void __iomem *ioaddr = tp->mmio_addr;
  737. u8 status;
  738. cmd->supported = SUPPORTED_10baseT_Half |
  739. SUPPORTED_10baseT_Full |
  740. SUPPORTED_100baseT_Half |
  741. SUPPORTED_100baseT_Full |
  742. SUPPORTED_1000baseT_Full |
  743. SUPPORTED_Autoneg |
  744. SUPPORTED_TP;
  745. cmd->autoneg = 1;
  746. cmd->advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
  747. if (tp->phy_auto_nego_reg & ADVERTISE_10HALF)
  748. cmd->advertising |= ADVERTISED_10baseT_Half;
  749. if (tp->phy_auto_nego_reg & ADVERTISE_10FULL)
  750. cmd->advertising |= ADVERTISED_10baseT_Full;
  751. if (tp->phy_auto_nego_reg & ADVERTISE_100HALF)
  752. cmd->advertising |= ADVERTISED_100baseT_Half;
  753. if (tp->phy_auto_nego_reg & ADVERTISE_100FULL)
  754. cmd->advertising |= ADVERTISED_100baseT_Full;
  755. if (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL)
  756. cmd->advertising |= ADVERTISED_1000baseT_Full;
  757. status = RTL_R8(PHYstatus);
  758. if (status & _1000bpsF)
  759. cmd->speed = SPEED_1000;
  760. else if (status & _100bps)
  761. cmd->speed = SPEED_100;
  762. else if (status & _10bps)
  763. cmd->speed = SPEED_10;
  764. if (status & TxFlowCtrl)
  765. cmd->advertising |= ADVERTISED_Asym_Pause;
  766. if (status & RxFlowCtrl)
  767. cmd->advertising |= ADVERTISED_Pause;
  768. cmd->duplex = ((status & _1000bpsF) || (status & FullDup)) ?
  769. DUPLEX_FULL : DUPLEX_HALF;
  770. }
  771. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  772. {
  773. struct rtl8169_private *tp = netdev_priv(dev);
  774. unsigned long flags;
  775. spin_lock_irqsave(&tp->lock, flags);
  776. tp->get_settings(dev, cmd);
  777. spin_unlock_irqrestore(&tp->lock, flags);
  778. return 0;
  779. }
  780. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  781. void *p)
  782. {
  783. struct rtl8169_private *tp = netdev_priv(dev);
  784. unsigned long flags;
  785. if (regs->len > R8169_REGS_SIZE)
  786. regs->len = R8169_REGS_SIZE;
  787. spin_lock_irqsave(&tp->lock, flags);
  788. memcpy_fromio(p, tp->mmio_addr, regs->len);
  789. spin_unlock_irqrestore(&tp->lock, flags);
  790. }
  791. static u32 rtl8169_get_msglevel(struct net_device *dev)
  792. {
  793. struct rtl8169_private *tp = netdev_priv(dev);
  794. return tp->msg_enable;
  795. }
  796. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  797. {
  798. struct rtl8169_private *tp = netdev_priv(dev);
  799. tp->msg_enable = value;
  800. }
  801. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  802. "tx_packets",
  803. "rx_packets",
  804. "tx_errors",
  805. "rx_errors",
  806. "rx_missed",
  807. "align_errors",
  808. "tx_single_collisions",
  809. "tx_multi_collisions",
  810. "unicast",
  811. "broadcast",
  812. "multicast",
  813. "tx_aborted",
  814. "tx_underrun",
  815. };
  816. struct rtl8169_counters {
  817. u64 tx_packets;
  818. u64 rx_packets;
  819. u64 tx_errors;
  820. u32 rx_errors;
  821. u16 rx_missed;
  822. u16 align_errors;
  823. u32 tx_one_collision;
  824. u32 tx_multi_collision;
  825. u64 rx_unicast;
  826. u64 rx_broadcast;
  827. u32 rx_multicast;
  828. u16 tx_aborted;
  829. u16 tx_underun;
  830. };
  831. static int rtl8169_get_stats_count(struct net_device *dev)
  832. {
  833. return ARRAY_SIZE(rtl8169_gstrings);
  834. }
  835. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  836. struct ethtool_stats *stats, u64 *data)
  837. {
  838. struct rtl8169_private *tp = netdev_priv(dev);
  839. void __iomem *ioaddr = tp->mmio_addr;
  840. struct rtl8169_counters *counters;
  841. dma_addr_t paddr;
  842. u32 cmd;
  843. ASSERT_RTNL();
  844. counters = pci_alloc_consistent(tp->pci_dev, sizeof(*counters), &paddr);
  845. if (!counters)
  846. return;
  847. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  848. cmd = (u64)paddr & DMA_32BIT_MASK;
  849. RTL_W32(CounterAddrLow, cmd);
  850. RTL_W32(CounterAddrLow, cmd | CounterDump);
  851. while (RTL_R32(CounterAddrLow) & CounterDump) {
  852. if (msleep_interruptible(1))
  853. break;
  854. }
  855. RTL_W32(CounterAddrLow, 0);
  856. RTL_W32(CounterAddrHigh, 0);
  857. data[0] = le64_to_cpu(counters->tx_packets);
  858. data[1] = le64_to_cpu(counters->rx_packets);
  859. data[2] = le64_to_cpu(counters->tx_errors);
  860. data[3] = le32_to_cpu(counters->rx_errors);
  861. data[4] = le16_to_cpu(counters->rx_missed);
  862. data[5] = le16_to_cpu(counters->align_errors);
  863. data[6] = le32_to_cpu(counters->tx_one_collision);
  864. data[7] = le32_to_cpu(counters->tx_multi_collision);
  865. data[8] = le64_to_cpu(counters->rx_unicast);
  866. data[9] = le64_to_cpu(counters->rx_broadcast);
  867. data[10] = le32_to_cpu(counters->rx_multicast);
  868. data[11] = le16_to_cpu(counters->tx_aborted);
  869. data[12] = le16_to_cpu(counters->tx_underun);
  870. pci_free_consistent(tp->pci_dev, sizeof(*counters), counters, paddr);
  871. }
  872. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  873. {
  874. switch(stringset) {
  875. case ETH_SS_STATS:
  876. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  877. break;
  878. }
  879. }
  880. static const struct ethtool_ops rtl8169_ethtool_ops = {
  881. .get_drvinfo = rtl8169_get_drvinfo,
  882. .get_regs_len = rtl8169_get_regs_len,
  883. .get_link = ethtool_op_get_link,
  884. .get_settings = rtl8169_get_settings,
  885. .set_settings = rtl8169_set_settings,
  886. .get_msglevel = rtl8169_get_msglevel,
  887. .set_msglevel = rtl8169_set_msglevel,
  888. .get_rx_csum = rtl8169_get_rx_csum,
  889. .set_rx_csum = rtl8169_set_rx_csum,
  890. .get_tx_csum = ethtool_op_get_tx_csum,
  891. .set_tx_csum = ethtool_op_set_tx_csum,
  892. .get_sg = ethtool_op_get_sg,
  893. .set_sg = ethtool_op_set_sg,
  894. .get_tso = ethtool_op_get_tso,
  895. .set_tso = ethtool_op_set_tso,
  896. .get_regs = rtl8169_get_regs,
  897. .get_wol = rtl8169_get_wol,
  898. .set_wol = rtl8169_set_wol,
  899. .get_strings = rtl8169_get_strings,
  900. .get_stats_count = rtl8169_get_stats_count,
  901. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  902. .get_perm_addr = ethtool_op_get_perm_addr,
  903. };
  904. static void rtl8169_write_gmii_reg_bit(void __iomem *ioaddr, int reg,
  905. int bitnum, int bitval)
  906. {
  907. int val;
  908. val = mdio_read(ioaddr, reg);
  909. val = (bitval == 1) ?
  910. val | (bitval << bitnum) : val & ~(0x0001 << bitnum);
  911. mdio_write(ioaddr, reg, val & 0xffff);
  912. }
  913. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  914. void __iomem *ioaddr)
  915. {
  916. /*
  917. * The driver currently handles the 8168Bf and the 8168Be identically
  918. * but they can be identified more specifically through the test below
  919. * if needed:
  920. *
  921. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  922. *
  923. * Same thing for the 8101Eb and the 8101Ec:
  924. *
  925. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  926. */
  927. const struct {
  928. u32 mask;
  929. int mac_version;
  930. } mac_info[] = {
  931. { 0x38800000, RTL_GIGA_MAC_VER_15 },
  932. { 0x38000000, RTL_GIGA_MAC_VER_12 },
  933. { 0x34000000, RTL_GIGA_MAC_VER_13 },
  934. { 0x30800000, RTL_GIGA_MAC_VER_14 },
  935. { 0x30000000, RTL_GIGA_MAC_VER_11 },
  936. { 0x98000000, RTL_GIGA_MAC_VER_06 },
  937. { 0x18000000, RTL_GIGA_MAC_VER_05 },
  938. { 0x10000000, RTL_GIGA_MAC_VER_04 },
  939. { 0x04000000, RTL_GIGA_MAC_VER_03 },
  940. { 0x00800000, RTL_GIGA_MAC_VER_02 },
  941. { 0x00000000, RTL_GIGA_MAC_VER_01 } /* Catch-all */
  942. }, *p = mac_info;
  943. u32 reg;
  944. reg = RTL_R32(TxConfig) & 0xfc800000;
  945. while ((reg & p->mask) != p->mask)
  946. p++;
  947. tp->mac_version = p->mac_version;
  948. }
  949. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  950. {
  951. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  952. }
  953. static void rtl8169_get_phy_version(struct rtl8169_private *tp,
  954. void __iomem *ioaddr)
  955. {
  956. const struct {
  957. u16 mask;
  958. u16 set;
  959. int phy_version;
  960. } phy_info[] = {
  961. { 0x000f, 0x0002, RTL_GIGA_PHY_VER_G },
  962. { 0x000f, 0x0001, RTL_GIGA_PHY_VER_F },
  963. { 0x000f, 0x0000, RTL_GIGA_PHY_VER_E },
  964. { 0x0000, 0x0000, RTL_GIGA_PHY_VER_D } /* Catch-all */
  965. }, *p = phy_info;
  966. u16 reg;
  967. reg = mdio_read(ioaddr, MII_PHYSID2) & 0xffff;
  968. while ((reg & p->mask) != p->set)
  969. p++;
  970. tp->phy_version = p->phy_version;
  971. }
  972. static void rtl8169_print_phy_version(struct rtl8169_private *tp)
  973. {
  974. struct {
  975. int version;
  976. char *msg;
  977. u32 reg;
  978. } phy_print[] = {
  979. { RTL_GIGA_PHY_VER_G, "RTL_GIGA_PHY_VER_G", 0x0002 },
  980. { RTL_GIGA_PHY_VER_F, "RTL_GIGA_PHY_VER_F", 0x0001 },
  981. { RTL_GIGA_PHY_VER_E, "RTL_GIGA_PHY_VER_E", 0x0000 },
  982. { RTL_GIGA_PHY_VER_D, "RTL_GIGA_PHY_VER_D", 0x0000 },
  983. { 0, NULL, 0x0000 }
  984. }, *p;
  985. for (p = phy_print; p->msg; p++) {
  986. if (tp->phy_version == p->version) {
  987. dprintk("phy_version == %s (%04x)\n", p->msg, p->reg);
  988. return;
  989. }
  990. }
  991. dprintk("phy_version == Unknown\n");
  992. }
  993. static void rtl8169_hw_phy_config(struct net_device *dev)
  994. {
  995. struct rtl8169_private *tp = netdev_priv(dev);
  996. void __iomem *ioaddr = tp->mmio_addr;
  997. struct {
  998. u16 regs[5]; /* Beware of bit-sign propagation */
  999. } phy_magic[5] = { {
  1000. { 0x0000, //w 4 15 12 0
  1001. 0x00a1, //w 3 15 0 00a1
  1002. 0x0008, //w 2 15 0 0008
  1003. 0x1020, //w 1 15 0 1020
  1004. 0x1000 } },{ //w 0 15 0 1000
  1005. { 0x7000, //w 4 15 12 7
  1006. 0xff41, //w 3 15 0 ff41
  1007. 0xde60, //w 2 15 0 de60
  1008. 0x0140, //w 1 15 0 0140
  1009. 0x0077 } },{ //w 0 15 0 0077
  1010. { 0xa000, //w 4 15 12 a
  1011. 0xdf01, //w 3 15 0 df01
  1012. 0xdf20, //w 2 15 0 df20
  1013. 0xff95, //w 1 15 0 ff95
  1014. 0xfa00 } },{ //w 0 15 0 fa00
  1015. { 0xb000, //w 4 15 12 b
  1016. 0xff41, //w 3 15 0 ff41
  1017. 0xde20, //w 2 15 0 de20
  1018. 0x0140, //w 1 15 0 0140
  1019. 0x00bb } },{ //w 0 15 0 00bb
  1020. { 0xf000, //w 4 15 12 f
  1021. 0xdf01, //w 3 15 0 df01
  1022. 0xdf20, //w 2 15 0 df20
  1023. 0xff95, //w 1 15 0 ff95
  1024. 0xbf00 } //w 0 15 0 bf00
  1025. }
  1026. }, *p = phy_magic;
  1027. unsigned int i;
  1028. rtl8169_print_mac_version(tp);
  1029. rtl8169_print_phy_version(tp);
  1030. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1031. return;
  1032. if (tp->phy_version >= RTL_GIGA_PHY_VER_H)
  1033. return;
  1034. dprintk("MAC version != 0 && PHY version == 0 or 1\n");
  1035. dprintk("Do final_reg2.cfg\n");
  1036. /* Shazam ! */
  1037. if (tp->mac_version == RTL_GIGA_MAC_VER_04) {
  1038. mdio_write(ioaddr, 31, 0x0002);
  1039. mdio_write(ioaddr, 1, 0x90d0);
  1040. mdio_write(ioaddr, 31, 0x0000);
  1041. return;
  1042. }
  1043. /* phy config for RTL8169s mac_version C chip */
  1044. mdio_write(ioaddr, 31, 0x0001); //w 31 2 0 1
  1045. mdio_write(ioaddr, 21, 0x1000); //w 21 15 0 1000
  1046. mdio_write(ioaddr, 24, 0x65c7); //w 24 15 0 65c7
  1047. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1048. for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
  1049. int val, pos = 4;
  1050. val = (mdio_read(ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
  1051. mdio_write(ioaddr, pos, val);
  1052. while (--pos >= 0)
  1053. mdio_write(ioaddr, pos, p->regs[4 - pos] & 0xffff);
  1054. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 1); //w 4 11 11 1
  1055. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1056. }
  1057. mdio_write(ioaddr, 31, 0x0000); //w 31 2 0 0
  1058. }
  1059. static void rtl8169_phy_timer(unsigned long __opaque)
  1060. {
  1061. struct net_device *dev = (struct net_device *)__opaque;
  1062. struct rtl8169_private *tp = netdev_priv(dev);
  1063. struct timer_list *timer = &tp->timer;
  1064. void __iomem *ioaddr = tp->mmio_addr;
  1065. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  1066. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  1067. assert(tp->phy_version < RTL_GIGA_PHY_VER_H);
  1068. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1069. return;
  1070. spin_lock_irq(&tp->lock);
  1071. if (tp->phy_reset_pending(ioaddr)) {
  1072. /*
  1073. * A busy loop could burn quite a few cycles on nowadays CPU.
  1074. * Let's delay the execution of the timer for a few ticks.
  1075. */
  1076. timeout = HZ/10;
  1077. goto out_mod_timer;
  1078. }
  1079. if (tp->link_ok(ioaddr))
  1080. goto out_unlock;
  1081. if (netif_msg_link(tp))
  1082. printk(KERN_WARNING "%s: PHY reset until link up\n", dev->name);
  1083. tp->phy_reset_enable(ioaddr);
  1084. out_mod_timer:
  1085. mod_timer(timer, jiffies + timeout);
  1086. out_unlock:
  1087. spin_unlock_irq(&tp->lock);
  1088. }
  1089. static inline void rtl8169_delete_timer(struct net_device *dev)
  1090. {
  1091. struct rtl8169_private *tp = netdev_priv(dev);
  1092. struct timer_list *timer = &tp->timer;
  1093. if ((tp->mac_version <= RTL_GIGA_MAC_VER_01) ||
  1094. (tp->phy_version >= RTL_GIGA_PHY_VER_H))
  1095. return;
  1096. del_timer_sync(timer);
  1097. }
  1098. static inline void rtl8169_request_timer(struct net_device *dev)
  1099. {
  1100. struct rtl8169_private *tp = netdev_priv(dev);
  1101. struct timer_list *timer = &tp->timer;
  1102. if ((tp->mac_version <= RTL_GIGA_MAC_VER_01) ||
  1103. (tp->phy_version >= RTL_GIGA_PHY_VER_H))
  1104. return;
  1105. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  1106. }
  1107. #ifdef CONFIG_NET_POLL_CONTROLLER
  1108. /*
  1109. * Polling 'interrupt' - used by things like netconsole to send skbs
  1110. * without having to re-enable interrupts. It's not called while
  1111. * the interrupt routine is executing.
  1112. */
  1113. static void rtl8169_netpoll(struct net_device *dev)
  1114. {
  1115. struct rtl8169_private *tp = netdev_priv(dev);
  1116. struct pci_dev *pdev = tp->pci_dev;
  1117. disable_irq(pdev->irq);
  1118. rtl8169_interrupt(pdev->irq, dev);
  1119. enable_irq(pdev->irq);
  1120. }
  1121. #endif
  1122. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  1123. void __iomem *ioaddr)
  1124. {
  1125. iounmap(ioaddr);
  1126. pci_release_regions(pdev);
  1127. pci_disable_device(pdev);
  1128. free_netdev(dev);
  1129. }
  1130. static void rtl8169_phy_reset(struct net_device *dev,
  1131. struct rtl8169_private *tp)
  1132. {
  1133. void __iomem *ioaddr = tp->mmio_addr;
  1134. unsigned int i;
  1135. tp->phy_reset_enable(ioaddr);
  1136. for (i = 0; i < 100; i++) {
  1137. if (!tp->phy_reset_pending(ioaddr))
  1138. return;
  1139. msleep(1);
  1140. }
  1141. if (netif_msg_link(tp))
  1142. printk(KERN_ERR "%s: PHY reset failed.\n", dev->name);
  1143. }
  1144. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  1145. {
  1146. void __iomem *ioaddr = tp->mmio_addr;
  1147. rtl8169_hw_phy_config(dev);
  1148. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1149. RTL_W8(0x82, 0x01);
  1150. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  1151. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  1152. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  1153. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  1154. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1155. RTL_W8(0x82, 0x01);
  1156. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  1157. mdio_write(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
  1158. }
  1159. rtl8169_phy_reset(dev, tp);
  1160. /*
  1161. * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
  1162. * only 8101. Don't panic.
  1163. */
  1164. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
  1165. if ((RTL_R8(PHYstatus) & TBI_Enable) && netif_msg_link(tp))
  1166. printk(KERN_INFO PFX "%s: TBI auto-negotiating\n", dev->name);
  1167. }
  1168. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  1169. {
  1170. void __iomem *ioaddr = tp->mmio_addr;
  1171. u32 high;
  1172. u32 low;
  1173. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  1174. high = addr[4] | (addr[5] << 8);
  1175. spin_lock_irq(&tp->lock);
  1176. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1177. RTL_W32(MAC0, low);
  1178. RTL_W32(MAC4, high);
  1179. RTL_W8(Cfg9346, Cfg9346_Lock);
  1180. spin_unlock_irq(&tp->lock);
  1181. }
  1182. static int rtl_set_mac_address(struct net_device *dev, void *p)
  1183. {
  1184. struct rtl8169_private *tp = netdev_priv(dev);
  1185. struct sockaddr *addr = p;
  1186. if (!is_valid_ether_addr(addr->sa_data))
  1187. return -EADDRNOTAVAIL;
  1188. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1189. rtl_rar_set(tp, dev->dev_addr);
  1190. return 0;
  1191. }
  1192. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1193. {
  1194. struct rtl8169_private *tp = netdev_priv(dev);
  1195. struct mii_ioctl_data *data = if_mii(ifr);
  1196. if (!netif_running(dev))
  1197. return -ENODEV;
  1198. switch (cmd) {
  1199. case SIOCGMIIPHY:
  1200. data->phy_id = 32; /* Internal PHY */
  1201. return 0;
  1202. case SIOCGMIIREG:
  1203. data->val_out = mdio_read(tp->mmio_addr, data->reg_num & 0x1f);
  1204. return 0;
  1205. case SIOCSMIIREG:
  1206. if (!capable(CAP_NET_ADMIN))
  1207. return -EPERM;
  1208. mdio_write(tp->mmio_addr, data->reg_num & 0x1f, data->val_in);
  1209. return 0;
  1210. }
  1211. return -EOPNOTSUPP;
  1212. }
  1213. static const struct rtl_cfg_info {
  1214. void (*hw_start)(struct net_device *);
  1215. unsigned int region;
  1216. unsigned int align;
  1217. u16 intr_event;
  1218. u16 napi_event;
  1219. } rtl_cfg_infos [] = {
  1220. [RTL_CFG_0] = {
  1221. .hw_start = rtl_hw_start_8169,
  1222. .region = 1,
  1223. .align = 0,
  1224. .intr_event = SYSErr | LinkChg | RxOverflow |
  1225. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  1226. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow
  1227. },
  1228. [RTL_CFG_1] = {
  1229. .hw_start = rtl_hw_start_8168,
  1230. .region = 2,
  1231. .align = 8,
  1232. .intr_event = SYSErr | LinkChg | RxOverflow |
  1233. TxErr | TxOK | RxOK | RxErr,
  1234. .napi_event = TxErr | TxOK | RxOK | RxOverflow
  1235. },
  1236. [RTL_CFG_2] = {
  1237. .hw_start = rtl_hw_start_8101,
  1238. .region = 2,
  1239. .align = 8,
  1240. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  1241. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  1242. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow
  1243. }
  1244. };
  1245. static int __devinit
  1246. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1247. {
  1248. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  1249. const unsigned int region = cfg->region;
  1250. struct rtl8169_private *tp;
  1251. struct net_device *dev;
  1252. void __iomem *ioaddr;
  1253. unsigned int i;
  1254. int rc;
  1255. if (netif_msg_drv(&debug)) {
  1256. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  1257. MODULENAME, RTL8169_VERSION);
  1258. }
  1259. dev = alloc_etherdev(sizeof (*tp));
  1260. if (!dev) {
  1261. if (netif_msg_drv(&debug))
  1262. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  1263. rc = -ENOMEM;
  1264. goto out;
  1265. }
  1266. SET_MODULE_OWNER(dev);
  1267. SET_NETDEV_DEV(dev, &pdev->dev);
  1268. tp = netdev_priv(dev);
  1269. tp->dev = dev;
  1270. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  1271. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  1272. rc = pci_enable_device(pdev);
  1273. if (rc < 0) {
  1274. if (netif_msg_probe(tp))
  1275. dev_err(&pdev->dev, "enable failure\n");
  1276. goto err_out_free_dev_1;
  1277. }
  1278. rc = pci_set_mwi(pdev);
  1279. if (rc < 0)
  1280. goto err_out_disable_2;
  1281. /* make sure PCI base addr 1 is MMIO */
  1282. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  1283. if (netif_msg_probe(tp)) {
  1284. dev_err(&pdev->dev,
  1285. "region #%d not an MMIO resource, aborting\n",
  1286. region);
  1287. }
  1288. rc = -ENODEV;
  1289. goto err_out_mwi_3;
  1290. }
  1291. /* check for weird/broken PCI region reporting */
  1292. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  1293. if (netif_msg_probe(tp)) {
  1294. dev_err(&pdev->dev,
  1295. "Invalid PCI region size(s), aborting\n");
  1296. }
  1297. rc = -ENODEV;
  1298. goto err_out_mwi_3;
  1299. }
  1300. rc = pci_request_regions(pdev, MODULENAME);
  1301. if (rc < 0) {
  1302. if (netif_msg_probe(tp))
  1303. dev_err(&pdev->dev, "could not request regions.\n");
  1304. goto err_out_mwi_3;
  1305. }
  1306. tp->cp_cmd = PCIMulRW | RxChkSum;
  1307. if ((sizeof(dma_addr_t) > 4) &&
  1308. !pci_set_dma_mask(pdev, DMA_64BIT_MASK) && use_dac) {
  1309. tp->cp_cmd |= PCIDAC;
  1310. dev->features |= NETIF_F_HIGHDMA;
  1311. } else {
  1312. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1313. if (rc < 0) {
  1314. if (netif_msg_probe(tp)) {
  1315. dev_err(&pdev->dev,
  1316. "DMA configuration failed.\n");
  1317. }
  1318. goto err_out_free_res_4;
  1319. }
  1320. }
  1321. pci_set_master(pdev);
  1322. /* ioremap MMIO region */
  1323. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  1324. if (!ioaddr) {
  1325. if (netif_msg_probe(tp))
  1326. dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
  1327. rc = -EIO;
  1328. goto err_out_free_res_4;
  1329. }
  1330. /* Unneeded ? Don't mess with Mrs. Murphy. */
  1331. rtl8169_irq_mask_and_ack(ioaddr);
  1332. /* Soft reset the chip. */
  1333. RTL_W8(ChipCmd, CmdReset);
  1334. /* Check that the chip has finished the reset. */
  1335. for (i = 0; i < 100; i++) {
  1336. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1337. break;
  1338. msleep_interruptible(1);
  1339. }
  1340. /* Identify chip attached to board */
  1341. rtl8169_get_mac_version(tp, ioaddr);
  1342. rtl8169_get_phy_version(tp, ioaddr);
  1343. rtl8169_print_mac_version(tp);
  1344. rtl8169_print_phy_version(tp);
  1345. for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--) {
  1346. if (tp->mac_version == rtl_chip_info[i].mac_version)
  1347. break;
  1348. }
  1349. if (i < 0) {
  1350. /* Unknown chip: assume array element #0, original RTL-8169 */
  1351. if (netif_msg_probe(tp)) {
  1352. dev_printk(KERN_DEBUG, &pdev->dev,
  1353. "unknown chip version, assuming %s\n",
  1354. rtl_chip_info[0].name);
  1355. }
  1356. i++;
  1357. }
  1358. tp->chipset = i;
  1359. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1360. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  1361. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  1362. RTL_W8(Cfg9346, Cfg9346_Lock);
  1363. if (RTL_R8(PHYstatus) & TBI_Enable) {
  1364. tp->set_speed = rtl8169_set_speed_tbi;
  1365. tp->get_settings = rtl8169_gset_tbi;
  1366. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  1367. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  1368. tp->link_ok = rtl8169_tbi_link_ok;
  1369. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  1370. } else {
  1371. tp->set_speed = rtl8169_set_speed_xmii;
  1372. tp->get_settings = rtl8169_gset_xmii;
  1373. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  1374. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  1375. tp->link_ok = rtl8169_xmii_link_ok;
  1376. dev->do_ioctl = rtl8169_ioctl;
  1377. }
  1378. /* Get MAC address. FIXME: read EEPROM */
  1379. for (i = 0; i < MAC_ADDR_LEN; i++)
  1380. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  1381. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1382. dev->open = rtl8169_open;
  1383. dev->hard_start_xmit = rtl8169_start_xmit;
  1384. dev->get_stats = rtl8169_get_stats;
  1385. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  1386. dev->stop = rtl8169_close;
  1387. dev->tx_timeout = rtl8169_tx_timeout;
  1388. dev->set_multicast_list = rtl_set_rx_mode;
  1389. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  1390. dev->irq = pdev->irq;
  1391. dev->base_addr = (unsigned long) ioaddr;
  1392. dev->change_mtu = rtl8169_change_mtu;
  1393. dev->set_mac_address = rtl_set_mac_address;
  1394. #ifdef CONFIG_R8169_NAPI
  1395. dev->poll = rtl8169_poll;
  1396. dev->weight = R8169_NAPI_WEIGHT;
  1397. #endif
  1398. #ifdef CONFIG_R8169_VLAN
  1399. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1400. dev->vlan_rx_register = rtl8169_vlan_rx_register;
  1401. #endif
  1402. #ifdef CONFIG_NET_POLL_CONTROLLER
  1403. dev->poll_controller = rtl8169_netpoll;
  1404. #endif
  1405. tp->intr_mask = 0xffff;
  1406. tp->pci_dev = pdev;
  1407. tp->mmio_addr = ioaddr;
  1408. tp->align = cfg->align;
  1409. tp->hw_start = cfg->hw_start;
  1410. tp->intr_event = cfg->intr_event;
  1411. tp->napi_event = cfg->napi_event;
  1412. init_timer(&tp->timer);
  1413. tp->timer.data = (unsigned long) dev;
  1414. tp->timer.function = rtl8169_phy_timer;
  1415. spin_lock_init(&tp->lock);
  1416. rc = register_netdev(dev);
  1417. if (rc < 0)
  1418. goto err_out_unmap_5;
  1419. pci_set_drvdata(pdev, dev);
  1420. if (netif_msg_probe(tp)) {
  1421. u32 xid = RTL_R32(TxConfig) & 0x7cf0f8ff;
  1422. printk(KERN_INFO "%s: %s at 0x%lx, "
  1423. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
  1424. "XID %08x IRQ %d\n",
  1425. dev->name,
  1426. rtl_chip_info[tp->chipset].name,
  1427. dev->base_addr,
  1428. dev->dev_addr[0], dev->dev_addr[1],
  1429. dev->dev_addr[2], dev->dev_addr[3],
  1430. dev->dev_addr[4], dev->dev_addr[5], xid, dev->irq);
  1431. }
  1432. rtl8169_init_phy(dev, tp);
  1433. out:
  1434. return rc;
  1435. err_out_unmap_5:
  1436. iounmap(ioaddr);
  1437. err_out_free_res_4:
  1438. pci_release_regions(pdev);
  1439. err_out_mwi_3:
  1440. pci_clear_mwi(pdev);
  1441. err_out_disable_2:
  1442. pci_disable_device(pdev);
  1443. err_out_free_dev_1:
  1444. free_netdev(dev);
  1445. goto out;
  1446. }
  1447. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  1448. {
  1449. struct net_device *dev = pci_get_drvdata(pdev);
  1450. struct rtl8169_private *tp = netdev_priv(dev);
  1451. flush_scheduled_work();
  1452. unregister_netdev(dev);
  1453. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  1454. pci_set_drvdata(pdev, NULL);
  1455. }
  1456. static void rtl8169_set_rxbufsize(struct rtl8169_private *tp,
  1457. struct net_device *dev)
  1458. {
  1459. unsigned int mtu = dev->mtu;
  1460. tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
  1461. }
  1462. static int rtl8169_open(struct net_device *dev)
  1463. {
  1464. struct rtl8169_private *tp = netdev_priv(dev);
  1465. struct pci_dev *pdev = tp->pci_dev;
  1466. int retval = -ENOMEM;
  1467. rtl8169_set_rxbufsize(tp, dev);
  1468. /*
  1469. * Rx and Tx desscriptors needs 256 bytes alignment.
  1470. * pci_alloc_consistent provides more.
  1471. */
  1472. tp->TxDescArray = pci_alloc_consistent(pdev, R8169_TX_RING_BYTES,
  1473. &tp->TxPhyAddr);
  1474. if (!tp->TxDescArray)
  1475. goto out;
  1476. tp->RxDescArray = pci_alloc_consistent(pdev, R8169_RX_RING_BYTES,
  1477. &tp->RxPhyAddr);
  1478. if (!tp->RxDescArray)
  1479. goto err_free_tx_0;
  1480. retval = rtl8169_init_ring(dev);
  1481. if (retval < 0)
  1482. goto err_free_rx_1;
  1483. INIT_DELAYED_WORK(&tp->task, NULL);
  1484. smp_mb();
  1485. retval = request_irq(dev->irq, rtl8169_interrupt, IRQF_SHARED,
  1486. dev->name, dev);
  1487. if (retval < 0)
  1488. goto err_release_ring_2;
  1489. rtl_hw_start(dev);
  1490. rtl8169_request_timer(dev);
  1491. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  1492. out:
  1493. return retval;
  1494. err_release_ring_2:
  1495. rtl8169_rx_clear(tp);
  1496. err_free_rx_1:
  1497. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  1498. tp->RxPhyAddr);
  1499. err_free_tx_0:
  1500. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  1501. tp->TxPhyAddr);
  1502. goto out;
  1503. }
  1504. static void rtl8169_hw_reset(void __iomem *ioaddr)
  1505. {
  1506. /* Disable interrupts */
  1507. rtl8169_irq_mask_and_ack(ioaddr);
  1508. /* Reset the chipset */
  1509. RTL_W8(ChipCmd, CmdReset);
  1510. /* PCI commit */
  1511. RTL_R8(ChipCmd);
  1512. }
  1513. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  1514. {
  1515. void __iomem *ioaddr = tp->mmio_addr;
  1516. u32 cfg = rtl8169_rx_config;
  1517. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  1518. RTL_W32(RxConfig, cfg);
  1519. /* Set DMA burst size and Interframe Gap Time */
  1520. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  1521. (InterFrameGap << TxInterFrameGapShift));
  1522. }
  1523. static void rtl_hw_start(struct net_device *dev)
  1524. {
  1525. struct rtl8169_private *tp = netdev_priv(dev);
  1526. void __iomem *ioaddr = tp->mmio_addr;
  1527. unsigned int i;
  1528. /* Soft reset the chip. */
  1529. RTL_W8(ChipCmd, CmdReset);
  1530. /* Check that the chip has finished the reset. */
  1531. for (i = 0; i < 100; i++) {
  1532. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1533. break;
  1534. msleep_interruptible(1);
  1535. }
  1536. tp->hw_start(dev);
  1537. netif_start_queue(dev);
  1538. }
  1539. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  1540. void __iomem *ioaddr)
  1541. {
  1542. /*
  1543. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  1544. * register to be written before TxDescAddrLow to work.
  1545. * Switching from MMIO to I/O access fixes the issue as well.
  1546. */
  1547. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  1548. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_32BIT_MASK);
  1549. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  1550. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_32BIT_MASK);
  1551. }
  1552. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  1553. {
  1554. u16 cmd;
  1555. cmd = RTL_R16(CPlusCmd);
  1556. RTL_W16(CPlusCmd, cmd);
  1557. return cmd;
  1558. }
  1559. static void rtl_set_rx_max_size(void __iomem *ioaddr)
  1560. {
  1561. /* Low hurts. Let's disable the filtering. */
  1562. RTL_W16(RxMaxSize, 16383);
  1563. }
  1564. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  1565. {
  1566. struct {
  1567. u32 mac_version;
  1568. u32 clk;
  1569. u32 val;
  1570. } cfg2_info [] = {
  1571. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  1572. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  1573. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  1574. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  1575. }, *p = cfg2_info;
  1576. unsigned int i;
  1577. u32 clk;
  1578. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  1579. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++) {
  1580. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  1581. RTL_W32(0x7c, p->val);
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. static void rtl_hw_start_8169(struct net_device *dev)
  1587. {
  1588. struct rtl8169_private *tp = netdev_priv(dev);
  1589. void __iomem *ioaddr = tp->mmio_addr;
  1590. struct pci_dev *pdev = tp->pci_dev;
  1591. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  1592. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  1593. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  1594. }
  1595. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1596. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  1597. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1598. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  1599. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  1600. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1601. RTL_W8(EarlyTxThres, EarlyTxThld);
  1602. rtl_set_rx_max_size(ioaddr);
  1603. rtl_set_rx_tx_config_registers(tp);
  1604. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  1605. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1606. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  1607. dprintk(KERN_INFO PFX "Set MAC Reg C+CR Offset 0xE0. "
  1608. "Bit-3 and bit-14 MUST be 1\n");
  1609. tp->cp_cmd |= (1 << 14);
  1610. }
  1611. RTL_W16(CPlusCmd, tp->cp_cmd);
  1612. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  1613. /*
  1614. * Undocumented corner. Supposedly:
  1615. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  1616. */
  1617. RTL_W16(IntrMitigate, 0x0000);
  1618. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1619. RTL_W8(Cfg9346, Cfg9346_Lock);
  1620. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  1621. RTL_R8(IntrMask);
  1622. RTL_W32(RxMissed, 0);
  1623. rtl_set_rx_mode(dev);
  1624. /* no early-rx interrupts */
  1625. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  1626. /* Enable all known interrupts by setting the interrupt mask. */
  1627. RTL_W16(IntrMask, tp->intr_event);
  1628. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1629. }
  1630. static void rtl_hw_start_8168(struct net_device *dev)
  1631. {
  1632. struct rtl8169_private *tp = netdev_priv(dev);
  1633. void __iomem *ioaddr = tp->mmio_addr;
  1634. struct pci_dev *pdev = tp->pci_dev;
  1635. u8 ctl;
  1636. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1637. RTL_W8(EarlyTxThres, EarlyTxThld);
  1638. rtl_set_rx_max_size(ioaddr);
  1639. rtl_set_rx_tx_config_registers(tp);
  1640. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  1641. RTL_W16(CPlusCmd, tp->cp_cmd);
  1642. /* Tx performance tweak. */
  1643. pci_read_config_byte(pdev, 0x69, &ctl);
  1644. ctl = (ctl & ~0x70) | 0x50;
  1645. pci_write_config_byte(pdev, 0x69, ctl);
  1646. RTL_W16(IntrMitigate, 0x5151);
  1647. /* Work around for RxFIFO overflow. */
  1648. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  1649. tp->intr_event |= RxFIFOOver | PCSTimeout;
  1650. tp->intr_event &= ~RxOverflow;
  1651. }
  1652. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1653. RTL_W8(Cfg9346, Cfg9346_Lock);
  1654. RTL_R8(IntrMask);
  1655. RTL_W32(RxMissed, 0);
  1656. rtl_set_rx_mode(dev);
  1657. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1658. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  1659. RTL_W16(IntrMask, tp->intr_event);
  1660. }
  1661. static void rtl_hw_start_8101(struct net_device *dev)
  1662. {
  1663. struct rtl8169_private *tp = netdev_priv(dev);
  1664. void __iomem *ioaddr = tp->mmio_addr;
  1665. struct pci_dev *pdev = tp->pci_dev;
  1666. if (tp->mac_version == RTL_GIGA_MAC_VER_13) {
  1667. pci_write_config_word(pdev, 0x68, 0x00);
  1668. pci_write_config_word(pdev, 0x69, 0x08);
  1669. }
  1670. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1671. RTL_W8(EarlyTxThres, EarlyTxThld);
  1672. rtl_set_rx_max_size(ioaddr);
  1673. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  1674. RTL_W16(CPlusCmd, tp->cp_cmd);
  1675. RTL_W16(IntrMitigate, 0x0000);
  1676. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1677. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1678. rtl_set_rx_tx_config_registers(tp);
  1679. RTL_W8(Cfg9346, Cfg9346_Lock);
  1680. RTL_R8(IntrMask);
  1681. RTL_W32(RxMissed, 0);
  1682. rtl_set_rx_mode(dev);
  1683. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1684. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  1685. RTL_W16(IntrMask, tp->intr_event);
  1686. }
  1687. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  1688. {
  1689. struct rtl8169_private *tp = netdev_priv(dev);
  1690. int ret = 0;
  1691. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  1692. return -EINVAL;
  1693. dev->mtu = new_mtu;
  1694. if (!netif_running(dev))
  1695. goto out;
  1696. rtl8169_down(dev);
  1697. rtl8169_set_rxbufsize(tp, dev);
  1698. ret = rtl8169_init_ring(dev);
  1699. if (ret < 0)
  1700. goto out;
  1701. netif_poll_enable(dev);
  1702. rtl_hw_start(dev);
  1703. rtl8169_request_timer(dev);
  1704. out:
  1705. return ret;
  1706. }
  1707. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  1708. {
  1709. desc->addr = 0x0badbadbadbadbadull;
  1710. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  1711. }
  1712. static void rtl8169_free_rx_skb(struct rtl8169_private *tp,
  1713. struct sk_buff **sk_buff, struct RxDesc *desc)
  1714. {
  1715. struct pci_dev *pdev = tp->pci_dev;
  1716. pci_unmap_single(pdev, le64_to_cpu(desc->addr), tp->rx_buf_sz,
  1717. PCI_DMA_FROMDEVICE);
  1718. dev_kfree_skb(*sk_buff);
  1719. *sk_buff = NULL;
  1720. rtl8169_make_unusable_by_asic(desc);
  1721. }
  1722. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  1723. {
  1724. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  1725. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  1726. }
  1727. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  1728. u32 rx_buf_sz)
  1729. {
  1730. desc->addr = cpu_to_le64(mapping);
  1731. wmb();
  1732. rtl8169_mark_to_asic(desc, rx_buf_sz);
  1733. }
  1734. static struct sk_buff *rtl8169_alloc_rx_skb(struct pci_dev *pdev,
  1735. struct net_device *dev,
  1736. struct RxDesc *desc, int rx_buf_sz,
  1737. unsigned int align)
  1738. {
  1739. struct sk_buff *skb;
  1740. dma_addr_t mapping;
  1741. unsigned int pad;
  1742. pad = align ? align : NET_IP_ALIGN;
  1743. skb = netdev_alloc_skb(dev, rx_buf_sz + pad);
  1744. if (!skb)
  1745. goto err_out;
  1746. skb_reserve(skb, align ? ((pad - 1) & (unsigned long)skb->data) : pad);
  1747. mapping = pci_map_single(pdev, skb->data, rx_buf_sz,
  1748. PCI_DMA_FROMDEVICE);
  1749. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  1750. out:
  1751. return skb;
  1752. err_out:
  1753. rtl8169_make_unusable_by_asic(desc);
  1754. goto out;
  1755. }
  1756. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  1757. {
  1758. unsigned int i;
  1759. for (i = 0; i < NUM_RX_DESC; i++) {
  1760. if (tp->Rx_skbuff[i]) {
  1761. rtl8169_free_rx_skb(tp, tp->Rx_skbuff + i,
  1762. tp->RxDescArray + i);
  1763. }
  1764. }
  1765. }
  1766. static u32 rtl8169_rx_fill(struct rtl8169_private *tp, struct net_device *dev,
  1767. u32 start, u32 end)
  1768. {
  1769. u32 cur;
  1770. for (cur = start; end - cur != 0; cur++) {
  1771. struct sk_buff *skb;
  1772. unsigned int i = cur % NUM_RX_DESC;
  1773. WARN_ON((s32)(end - cur) < 0);
  1774. if (tp->Rx_skbuff[i])
  1775. continue;
  1776. skb = rtl8169_alloc_rx_skb(tp->pci_dev, dev,
  1777. tp->RxDescArray + i,
  1778. tp->rx_buf_sz, tp->align);
  1779. if (!skb)
  1780. break;
  1781. tp->Rx_skbuff[i] = skb;
  1782. }
  1783. return cur - start;
  1784. }
  1785. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  1786. {
  1787. desc->opts1 |= cpu_to_le32(RingEnd);
  1788. }
  1789. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  1790. {
  1791. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  1792. }
  1793. static int rtl8169_init_ring(struct net_device *dev)
  1794. {
  1795. struct rtl8169_private *tp = netdev_priv(dev);
  1796. rtl8169_init_ring_indexes(tp);
  1797. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  1798. memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));
  1799. if (rtl8169_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
  1800. goto err_out;
  1801. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  1802. return 0;
  1803. err_out:
  1804. rtl8169_rx_clear(tp);
  1805. return -ENOMEM;
  1806. }
  1807. static void rtl8169_unmap_tx_skb(struct pci_dev *pdev, struct ring_info *tx_skb,
  1808. struct TxDesc *desc)
  1809. {
  1810. unsigned int len = tx_skb->len;
  1811. pci_unmap_single(pdev, le64_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);
  1812. desc->opts1 = 0x00;
  1813. desc->opts2 = 0x00;
  1814. desc->addr = 0x00;
  1815. tx_skb->len = 0;
  1816. }
  1817. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  1818. {
  1819. unsigned int i;
  1820. for (i = tp->dirty_tx; i < tp->dirty_tx + NUM_TX_DESC; i++) {
  1821. unsigned int entry = i % NUM_TX_DESC;
  1822. struct ring_info *tx_skb = tp->tx_skb + entry;
  1823. unsigned int len = tx_skb->len;
  1824. if (len) {
  1825. struct sk_buff *skb = tx_skb->skb;
  1826. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb,
  1827. tp->TxDescArray + entry);
  1828. if (skb) {
  1829. dev_kfree_skb(skb);
  1830. tx_skb->skb = NULL;
  1831. }
  1832. tp->stats.tx_dropped++;
  1833. }
  1834. }
  1835. tp->cur_tx = tp->dirty_tx = 0;
  1836. }
  1837. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  1838. {
  1839. struct rtl8169_private *tp = netdev_priv(dev);
  1840. PREPARE_DELAYED_WORK(&tp->task, task);
  1841. schedule_delayed_work(&tp->task, 4);
  1842. }
  1843. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  1844. {
  1845. struct rtl8169_private *tp = netdev_priv(dev);
  1846. void __iomem *ioaddr = tp->mmio_addr;
  1847. synchronize_irq(dev->irq);
  1848. /* Wait for any pending NAPI task to complete */
  1849. netif_poll_disable(dev);
  1850. rtl8169_irq_mask_and_ack(ioaddr);
  1851. netif_poll_enable(dev);
  1852. }
  1853. static void rtl8169_reinit_task(struct work_struct *work)
  1854. {
  1855. struct rtl8169_private *tp =
  1856. container_of(work, struct rtl8169_private, task.work);
  1857. struct net_device *dev = tp->dev;
  1858. int ret;
  1859. rtnl_lock();
  1860. if (!netif_running(dev))
  1861. goto out_unlock;
  1862. rtl8169_wait_for_quiescence(dev);
  1863. rtl8169_close(dev);
  1864. ret = rtl8169_open(dev);
  1865. if (unlikely(ret < 0)) {
  1866. if (net_ratelimit() && netif_msg_drv(tp)) {
  1867. printk(PFX KERN_ERR "%s: reinit failure (status = %d)."
  1868. " Rescheduling.\n", dev->name, ret);
  1869. }
  1870. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  1871. }
  1872. out_unlock:
  1873. rtnl_unlock();
  1874. }
  1875. static void rtl8169_reset_task(struct work_struct *work)
  1876. {
  1877. struct rtl8169_private *tp =
  1878. container_of(work, struct rtl8169_private, task.work);
  1879. struct net_device *dev = tp->dev;
  1880. rtnl_lock();
  1881. if (!netif_running(dev))
  1882. goto out_unlock;
  1883. rtl8169_wait_for_quiescence(dev);
  1884. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr);
  1885. rtl8169_tx_clear(tp);
  1886. if (tp->dirty_rx == tp->cur_rx) {
  1887. rtl8169_init_ring_indexes(tp);
  1888. rtl_hw_start(dev);
  1889. netif_wake_queue(dev);
  1890. } else {
  1891. if (net_ratelimit() && netif_msg_intr(tp)) {
  1892. printk(PFX KERN_EMERG "%s: Rx buffers shortage\n",
  1893. dev->name);
  1894. }
  1895. rtl8169_schedule_work(dev, rtl8169_reset_task);
  1896. }
  1897. out_unlock:
  1898. rtnl_unlock();
  1899. }
  1900. static void rtl8169_tx_timeout(struct net_device *dev)
  1901. {
  1902. struct rtl8169_private *tp = netdev_priv(dev);
  1903. rtl8169_hw_reset(tp->mmio_addr);
  1904. /* Let's wait a bit while any (async) irq lands on */
  1905. rtl8169_schedule_work(dev, rtl8169_reset_task);
  1906. }
  1907. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  1908. u32 opts1)
  1909. {
  1910. struct skb_shared_info *info = skb_shinfo(skb);
  1911. unsigned int cur_frag, entry;
  1912. struct TxDesc *txd;
  1913. entry = tp->cur_tx;
  1914. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  1915. skb_frag_t *frag = info->frags + cur_frag;
  1916. dma_addr_t mapping;
  1917. u32 status, len;
  1918. void *addr;
  1919. entry = (entry + 1) % NUM_TX_DESC;
  1920. txd = tp->TxDescArray + entry;
  1921. len = frag->size;
  1922. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  1923. mapping = pci_map_single(tp->pci_dev, addr, len, PCI_DMA_TODEVICE);
  1924. /* anti gcc 2.95.3 bugware (sic) */
  1925. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  1926. txd->opts1 = cpu_to_le32(status);
  1927. txd->addr = cpu_to_le64(mapping);
  1928. tp->tx_skb[entry].len = len;
  1929. }
  1930. if (cur_frag) {
  1931. tp->tx_skb[entry].skb = skb;
  1932. txd->opts1 |= cpu_to_le32(LastFrag);
  1933. }
  1934. return cur_frag;
  1935. }
  1936. static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
  1937. {
  1938. if (dev->features & NETIF_F_TSO) {
  1939. u32 mss = skb_shinfo(skb)->gso_size;
  1940. if (mss)
  1941. return LargeSend | ((mss & MSSMask) << MSSShift);
  1942. }
  1943. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1944. const struct iphdr *ip = ip_hdr(skb);
  1945. if (ip->protocol == IPPROTO_TCP)
  1946. return IPCS | TCPCS;
  1947. else if (ip->protocol == IPPROTO_UDP)
  1948. return IPCS | UDPCS;
  1949. WARN_ON(1); /* we need a WARN() */
  1950. }
  1951. return 0;
  1952. }
  1953. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1954. {
  1955. struct rtl8169_private *tp = netdev_priv(dev);
  1956. unsigned int frags, entry = tp->cur_tx % NUM_TX_DESC;
  1957. struct TxDesc *txd = tp->TxDescArray + entry;
  1958. void __iomem *ioaddr = tp->mmio_addr;
  1959. dma_addr_t mapping;
  1960. u32 status, len;
  1961. u32 opts1;
  1962. int ret = NETDEV_TX_OK;
  1963. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  1964. if (netif_msg_drv(tp)) {
  1965. printk(KERN_ERR
  1966. "%s: BUG! Tx Ring full when queue awake!\n",
  1967. dev->name);
  1968. }
  1969. goto err_stop;
  1970. }
  1971. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  1972. goto err_stop;
  1973. opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
  1974. frags = rtl8169_xmit_frags(tp, skb, opts1);
  1975. if (frags) {
  1976. len = skb_headlen(skb);
  1977. opts1 |= FirstFrag;
  1978. } else {
  1979. len = skb->len;
  1980. if (unlikely(len < ETH_ZLEN)) {
  1981. if (skb_padto(skb, ETH_ZLEN))
  1982. goto err_update_stats;
  1983. len = ETH_ZLEN;
  1984. }
  1985. opts1 |= FirstFrag | LastFrag;
  1986. tp->tx_skb[entry].skb = skb;
  1987. }
  1988. mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  1989. tp->tx_skb[entry].len = len;
  1990. txd->addr = cpu_to_le64(mapping);
  1991. txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  1992. wmb();
  1993. /* anti gcc 2.95.3 bugware (sic) */
  1994. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  1995. txd->opts1 = cpu_to_le32(status);
  1996. dev->trans_start = jiffies;
  1997. tp->cur_tx += frags + 1;
  1998. smp_wmb();
  1999. RTL_W8(TxPoll, NPQ); /* set polling bit */
  2000. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  2001. netif_stop_queue(dev);
  2002. smp_rmb();
  2003. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  2004. netif_wake_queue(dev);
  2005. }
  2006. out:
  2007. return ret;
  2008. err_stop:
  2009. netif_stop_queue(dev);
  2010. ret = NETDEV_TX_BUSY;
  2011. err_update_stats:
  2012. tp->stats.tx_dropped++;
  2013. goto out;
  2014. }
  2015. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  2016. {
  2017. struct rtl8169_private *tp = netdev_priv(dev);
  2018. struct pci_dev *pdev = tp->pci_dev;
  2019. void __iomem *ioaddr = tp->mmio_addr;
  2020. u16 pci_status, pci_cmd;
  2021. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  2022. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  2023. if (netif_msg_intr(tp)) {
  2024. printk(KERN_ERR
  2025. "%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
  2026. dev->name, pci_cmd, pci_status);
  2027. }
  2028. /*
  2029. * The recovery sequence below admits a very elaborated explanation:
  2030. * - it seems to work;
  2031. * - I did not see what else could be done;
  2032. * - it makes iop3xx happy.
  2033. *
  2034. * Feel free to adjust to your needs.
  2035. */
  2036. if (pdev->broken_parity_status)
  2037. pci_cmd &= ~PCI_COMMAND_PARITY;
  2038. else
  2039. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  2040. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  2041. pci_write_config_word(pdev, PCI_STATUS,
  2042. pci_status & (PCI_STATUS_DETECTED_PARITY |
  2043. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  2044. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  2045. /* The infamous DAC f*ckup only happens at boot time */
  2046. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  2047. if (netif_msg_intr(tp))
  2048. printk(KERN_INFO "%s: disabling PCI DAC.\n", dev->name);
  2049. tp->cp_cmd &= ~PCIDAC;
  2050. RTL_W16(CPlusCmd, tp->cp_cmd);
  2051. dev->features &= ~NETIF_F_HIGHDMA;
  2052. }
  2053. rtl8169_hw_reset(ioaddr);
  2054. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  2055. }
  2056. static void rtl8169_tx_interrupt(struct net_device *dev,
  2057. struct rtl8169_private *tp,
  2058. void __iomem *ioaddr)
  2059. {
  2060. unsigned int dirty_tx, tx_left;
  2061. dirty_tx = tp->dirty_tx;
  2062. smp_rmb();
  2063. tx_left = tp->cur_tx - dirty_tx;
  2064. while (tx_left > 0) {
  2065. unsigned int entry = dirty_tx % NUM_TX_DESC;
  2066. struct ring_info *tx_skb = tp->tx_skb + entry;
  2067. u32 len = tx_skb->len;
  2068. u32 status;
  2069. rmb();
  2070. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  2071. if (status & DescOwn)
  2072. break;
  2073. tp->stats.tx_bytes += len;
  2074. tp->stats.tx_packets++;
  2075. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb, tp->TxDescArray + entry);
  2076. if (status & LastFrag) {
  2077. dev_kfree_skb_irq(tx_skb->skb);
  2078. tx_skb->skb = NULL;
  2079. }
  2080. dirty_tx++;
  2081. tx_left--;
  2082. }
  2083. if (tp->dirty_tx != dirty_tx) {
  2084. tp->dirty_tx = dirty_tx;
  2085. smp_wmb();
  2086. if (netif_queue_stopped(dev) &&
  2087. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  2088. netif_wake_queue(dev);
  2089. }
  2090. }
  2091. }
  2092. static inline int rtl8169_fragmented_frame(u32 status)
  2093. {
  2094. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  2095. }
  2096. static inline void rtl8169_rx_csum(struct sk_buff *skb, struct RxDesc *desc)
  2097. {
  2098. u32 opts1 = le32_to_cpu(desc->opts1);
  2099. u32 status = opts1 & RxProtoMask;
  2100. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  2101. ((status == RxProtoUDP) && !(opts1 & UDPFail)) ||
  2102. ((status == RxProtoIP) && !(opts1 & IPFail)))
  2103. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2104. else
  2105. skb->ip_summed = CHECKSUM_NONE;
  2106. }
  2107. static inline bool rtl8169_try_rx_copy(struct sk_buff **sk_buff,
  2108. struct rtl8169_private *tp, int pkt_size,
  2109. dma_addr_t addr)
  2110. {
  2111. struct sk_buff *skb;
  2112. bool done = false;
  2113. if (pkt_size >= rx_copybreak)
  2114. goto out;
  2115. skb = netdev_alloc_skb(tp->dev, pkt_size + NET_IP_ALIGN);
  2116. if (!skb)
  2117. goto out;
  2118. pci_dma_sync_single_for_cpu(tp->pci_dev, addr, pkt_size,
  2119. PCI_DMA_FROMDEVICE);
  2120. skb_reserve(skb, NET_IP_ALIGN);
  2121. skb_copy_from_linear_data(*sk_buff, skb->data, pkt_size);
  2122. *sk_buff = skb;
  2123. done = true;
  2124. out:
  2125. return done;
  2126. }
  2127. static int rtl8169_rx_interrupt(struct net_device *dev,
  2128. struct rtl8169_private *tp,
  2129. void __iomem *ioaddr)
  2130. {
  2131. unsigned int cur_rx, rx_left;
  2132. unsigned int delta, count;
  2133. cur_rx = tp->cur_rx;
  2134. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  2135. rx_left = rtl8169_rx_quota(rx_left, (u32) dev->quota);
  2136. for (; rx_left > 0; rx_left--, cur_rx++) {
  2137. unsigned int entry = cur_rx % NUM_RX_DESC;
  2138. struct RxDesc *desc = tp->RxDescArray + entry;
  2139. u32 status;
  2140. rmb();
  2141. status = le32_to_cpu(desc->opts1);
  2142. if (status & DescOwn)
  2143. break;
  2144. if (unlikely(status & RxRES)) {
  2145. if (netif_msg_rx_err(tp)) {
  2146. printk(KERN_INFO
  2147. "%s: Rx ERROR. status = %08x\n",
  2148. dev->name, status);
  2149. }
  2150. tp->stats.rx_errors++;
  2151. if (status & (RxRWT | RxRUNT))
  2152. tp->stats.rx_length_errors++;
  2153. if (status & RxCRC)
  2154. tp->stats.rx_crc_errors++;
  2155. if (status & RxFOVF) {
  2156. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2157. tp->stats.rx_fifo_errors++;
  2158. }
  2159. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2160. } else {
  2161. struct sk_buff *skb = tp->Rx_skbuff[entry];
  2162. dma_addr_t addr = le64_to_cpu(desc->addr);
  2163. int pkt_size = (status & 0x00001FFF) - 4;
  2164. struct pci_dev *pdev = tp->pci_dev;
  2165. /*
  2166. * The driver does not support incoming fragmented
  2167. * frames. They are seen as a symptom of over-mtu
  2168. * sized frames.
  2169. */
  2170. if (unlikely(rtl8169_fragmented_frame(status))) {
  2171. tp->stats.rx_dropped++;
  2172. tp->stats.rx_length_errors++;
  2173. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2174. continue;
  2175. }
  2176. rtl8169_rx_csum(skb, desc);
  2177. if (rtl8169_try_rx_copy(&skb, tp, pkt_size, addr)) {
  2178. pci_dma_sync_single_for_device(pdev, addr,
  2179. pkt_size, PCI_DMA_FROMDEVICE);
  2180. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2181. } else {
  2182. pci_unmap_single(pdev, addr, pkt_size,
  2183. PCI_DMA_FROMDEVICE);
  2184. tp->Rx_skbuff[entry] = NULL;
  2185. }
  2186. skb_put(skb, pkt_size);
  2187. skb->protocol = eth_type_trans(skb, dev);
  2188. if (rtl8169_rx_vlan_skb(tp, desc, skb) < 0)
  2189. rtl8169_rx_skb(skb);
  2190. dev->last_rx = jiffies;
  2191. tp->stats.rx_bytes += pkt_size;
  2192. tp->stats.rx_packets++;
  2193. }
  2194. /* Work around for AMD plateform. */
  2195. if ((desc->opts2 & 0xfffe000) &&
  2196. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  2197. desc->opts2 = 0;
  2198. cur_rx++;
  2199. }
  2200. }
  2201. count = cur_rx - tp->cur_rx;
  2202. tp->cur_rx = cur_rx;
  2203. delta = rtl8169_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
  2204. if (!delta && count && netif_msg_intr(tp))
  2205. printk(KERN_INFO "%s: no Rx buffer allocated\n", dev->name);
  2206. tp->dirty_rx += delta;
  2207. /*
  2208. * FIXME: until there is periodic timer to try and refill the ring,
  2209. * a temporary shortage may definitely kill the Rx process.
  2210. * - disable the asic to try and avoid an overflow and kick it again
  2211. * after refill ?
  2212. * - how do others driver handle this condition (Uh oh...).
  2213. */
  2214. if ((tp->dirty_rx + NUM_RX_DESC == tp->cur_rx) && netif_msg_intr(tp))
  2215. printk(KERN_EMERG "%s: Rx buffers exhausted\n", dev->name);
  2216. return count;
  2217. }
  2218. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  2219. {
  2220. struct net_device *dev = dev_instance;
  2221. struct rtl8169_private *tp = netdev_priv(dev);
  2222. int boguscnt = max_interrupt_work;
  2223. void __iomem *ioaddr = tp->mmio_addr;
  2224. int status;
  2225. int handled = 0;
  2226. do {
  2227. status = RTL_R16(IntrStatus);
  2228. /* hotplug/major error/no more work/shared irq */
  2229. if ((status == 0xFFFF) || !status)
  2230. break;
  2231. handled = 1;
  2232. if (unlikely(!netif_running(dev))) {
  2233. rtl8169_asic_down(ioaddr);
  2234. goto out;
  2235. }
  2236. status &= tp->intr_mask;
  2237. RTL_W16(IntrStatus,
  2238. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  2239. if (!(status & tp->intr_event))
  2240. break;
  2241. /* Work around for rx fifo overflow */
  2242. if (unlikely(status & RxFIFOOver) &&
  2243. (tp->mac_version == RTL_GIGA_MAC_VER_11)) {
  2244. netif_stop_queue(dev);
  2245. rtl8169_tx_timeout(dev);
  2246. break;
  2247. }
  2248. if (unlikely(status & SYSErr)) {
  2249. rtl8169_pcierr_interrupt(dev);
  2250. break;
  2251. }
  2252. if (status & LinkChg)
  2253. rtl8169_check_link_status(dev, tp, ioaddr);
  2254. #ifdef CONFIG_R8169_NAPI
  2255. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  2256. tp->intr_mask = ~tp->napi_event;
  2257. if (likely(netif_rx_schedule_prep(dev)))
  2258. __netif_rx_schedule(dev);
  2259. else if (netif_msg_intr(tp)) {
  2260. printk(KERN_INFO "%s: interrupt %04x taken in poll\n",
  2261. dev->name, status);
  2262. }
  2263. break;
  2264. #else
  2265. /* Rx interrupt */
  2266. if (status & (RxOK | RxOverflow | RxFIFOOver))
  2267. rtl8169_rx_interrupt(dev, tp, ioaddr);
  2268. /* Tx interrupt */
  2269. if (status & (TxOK | TxErr))
  2270. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2271. #endif
  2272. boguscnt--;
  2273. } while (boguscnt > 0);
  2274. if (boguscnt <= 0) {
  2275. if (netif_msg_intr(tp) && net_ratelimit() ) {
  2276. printk(KERN_WARNING
  2277. "%s: Too much work at interrupt!\n", dev->name);
  2278. }
  2279. /* Clear all interrupt sources. */
  2280. RTL_W16(IntrStatus, 0xffff);
  2281. }
  2282. out:
  2283. return IRQ_RETVAL(handled);
  2284. }
  2285. #ifdef CONFIG_R8169_NAPI
  2286. static int rtl8169_poll(struct net_device *dev, int *budget)
  2287. {
  2288. unsigned int work_done, work_to_do = min(*budget, dev->quota);
  2289. struct rtl8169_private *tp = netdev_priv(dev);
  2290. void __iomem *ioaddr = tp->mmio_addr;
  2291. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr);
  2292. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2293. *budget -= work_done;
  2294. dev->quota -= work_done;
  2295. if (work_done < work_to_do) {
  2296. netif_rx_complete(dev);
  2297. tp->intr_mask = 0xffff;
  2298. /*
  2299. * 20040426: the barrier is not strictly required but the
  2300. * behavior of the irq handler could be less predictable
  2301. * without it. Btw, the lack of flush for the posted pci
  2302. * write is safe - FR
  2303. */
  2304. smp_wmb();
  2305. RTL_W16(IntrMask, tp->intr_event);
  2306. }
  2307. return (work_done >= work_to_do);
  2308. }
  2309. #endif
  2310. static void rtl8169_down(struct net_device *dev)
  2311. {
  2312. struct rtl8169_private *tp = netdev_priv(dev);
  2313. void __iomem *ioaddr = tp->mmio_addr;
  2314. unsigned int poll_locked = 0;
  2315. unsigned int intrmask;
  2316. rtl8169_delete_timer(dev);
  2317. netif_stop_queue(dev);
  2318. core_down:
  2319. spin_lock_irq(&tp->lock);
  2320. rtl8169_asic_down(ioaddr);
  2321. /* Update the error counts. */
  2322. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2323. RTL_W32(RxMissed, 0);
  2324. spin_unlock_irq(&tp->lock);
  2325. synchronize_irq(dev->irq);
  2326. if (!poll_locked) {
  2327. netif_poll_disable(dev);
  2328. poll_locked++;
  2329. }
  2330. /* Give a racing hard_start_xmit a few cycles to complete. */
  2331. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  2332. /*
  2333. * And now for the 50k$ question: are IRQ disabled or not ?
  2334. *
  2335. * Two paths lead here:
  2336. * 1) dev->close
  2337. * -> netif_running() is available to sync the current code and the
  2338. * IRQ handler. See rtl8169_interrupt for details.
  2339. * 2) dev->change_mtu
  2340. * -> rtl8169_poll can not be issued again and re-enable the
  2341. * interruptions. Let's simply issue the IRQ down sequence again.
  2342. *
  2343. * No loop if hotpluged or major error (0xffff).
  2344. */
  2345. intrmask = RTL_R16(IntrMask);
  2346. if (intrmask && (intrmask != 0xffff))
  2347. goto core_down;
  2348. rtl8169_tx_clear(tp);
  2349. rtl8169_rx_clear(tp);
  2350. }
  2351. static int rtl8169_close(struct net_device *dev)
  2352. {
  2353. struct rtl8169_private *tp = netdev_priv(dev);
  2354. struct pci_dev *pdev = tp->pci_dev;
  2355. rtl8169_down(dev);
  2356. free_irq(dev->irq, dev);
  2357. netif_poll_enable(dev);
  2358. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  2359. tp->RxPhyAddr);
  2360. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  2361. tp->TxPhyAddr);
  2362. tp->TxDescArray = NULL;
  2363. tp->RxDescArray = NULL;
  2364. return 0;
  2365. }
  2366. static void rtl_set_rx_mode(struct net_device *dev)
  2367. {
  2368. struct rtl8169_private *tp = netdev_priv(dev);
  2369. void __iomem *ioaddr = tp->mmio_addr;
  2370. unsigned long flags;
  2371. u32 mc_filter[2]; /* Multicast hash filter */
  2372. int rx_mode;
  2373. u32 tmp = 0;
  2374. if (dev->flags & IFF_PROMISC) {
  2375. /* Unconditionally log net taps. */
  2376. if (netif_msg_link(tp)) {
  2377. printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
  2378. dev->name);
  2379. }
  2380. rx_mode =
  2381. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  2382. AcceptAllPhys;
  2383. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2384. } else if ((dev->mc_count > multicast_filter_limit)
  2385. || (dev->flags & IFF_ALLMULTI)) {
  2386. /* Too many to filter perfectly -- accept all multicasts. */
  2387. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  2388. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2389. } else {
  2390. struct dev_mc_list *mclist;
  2391. unsigned int i;
  2392. rx_mode = AcceptBroadcast | AcceptMyPhys;
  2393. mc_filter[1] = mc_filter[0] = 0;
  2394. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  2395. i++, mclist = mclist->next) {
  2396. int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
  2397. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  2398. rx_mode |= AcceptMulticast;
  2399. }
  2400. }
  2401. spin_lock_irqsave(&tp->lock, flags);
  2402. tmp = rtl8169_rx_config | rx_mode |
  2403. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  2404. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  2405. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  2406. (tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  2407. (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
  2408. (tp->mac_version == RTL_GIGA_MAC_VER_15)) {
  2409. mc_filter[0] = 0xffffffff;
  2410. mc_filter[1] = 0xffffffff;
  2411. }
  2412. RTL_W32(MAR0 + 0, mc_filter[0]);
  2413. RTL_W32(MAR0 + 4, mc_filter[1]);
  2414. RTL_W32(RxConfig, tmp);
  2415. spin_unlock_irqrestore(&tp->lock, flags);
  2416. }
  2417. /**
  2418. * rtl8169_get_stats - Get rtl8169 read/write statistics
  2419. * @dev: The Ethernet Device to get statistics for
  2420. *
  2421. * Get TX/RX statistics for rtl8169
  2422. */
  2423. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  2424. {
  2425. struct rtl8169_private *tp = netdev_priv(dev);
  2426. void __iomem *ioaddr = tp->mmio_addr;
  2427. unsigned long flags;
  2428. if (netif_running(dev)) {
  2429. spin_lock_irqsave(&tp->lock, flags);
  2430. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2431. RTL_W32(RxMissed, 0);
  2432. spin_unlock_irqrestore(&tp->lock, flags);
  2433. }
  2434. return &tp->stats;
  2435. }
  2436. #ifdef CONFIG_PM
  2437. static int rtl8169_suspend(struct pci_dev *pdev, pm_message_t state)
  2438. {
  2439. struct net_device *dev = pci_get_drvdata(pdev);
  2440. struct rtl8169_private *tp = netdev_priv(dev);
  2441. void __iomem *ioaddr = tp->mmio_addr;
  2442. if (!netif_running(dev))
  2443. goto out_pci_suspend;
  2444. netif_device_detach(dev);
  2445. netif_stop_queue(dev);
  2446. spin_lock_irq(&tp->lock);
  2447. rtl8169_asic_down(ioaddr);
  2448. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2449. RTL_W32(RxMissed, 0);
  2450. spin_unlock_irq(&tp->lock);
  2451. out_pci_suspend:
  2452. pci_save_state(pdev);
  2453. pci_enable_wake(pdev, pci_choose_state(pdev, state), tp->wol_enabled);
  2454. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2455. return 0;
  2456. }
  2457. static int rtl8169_resume(struct pci_dev *pdev)
  2458. {
  2459. struct net_device *dev = pci_get_drvdata(pdev);
  2460. pci_set_power_state(pdev, PCI_D0);
  2461. pci_restore_state(pdev);
  2462. pci_enable_wake(pdev, PCI_D0, 0);
  2463. if (!netif_running(dev))
  2464. goto out;
  2465. netif_device_attach(dev);
  2466. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2467. out:
  2468. return 0;
  2469. }
  2470. #endif /* CONFIG_PM */
  2471. static struct pci_driver rtl8169_pci_driver = {
  2472. .name = MODULENAME,
  2473. .id_table = rtl8169_pci_tbl,
  2474. .probe = rtl8169_init_one,
  2475. .remove = __devexit_p(rtl8169_remove_one),
  2476. #ifdef CONFIG_PM
  2477. .suspend = rtl8169_suspend,
  2478. .resume = rtl8169_resume,
  2479. #endif
  2480. };
  2481. static int __init rtl8169_init_module(void)
  2482. {
  2483. return pci_register_driver(&rtl8169_pci_driver);
  2484. }
  2485. static void __exit rtl8169_cleanup_module(void)
  2486. {
  2487. pci_unregister_driver(&rtl8169_pci_driver);
  2488. }
  2489. module_init(rtl8169_init_module);
  2490. module_exit(rtl8169_cleanup_module);