r8169.c 81 KB

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