r8169.c 67 KB

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