r8169.c 77 KB

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