r8169.c 75 KB

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