r8169.c 77 KB

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