r8169.c 76 KB

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