r8169.c 80 KB

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