r8169.c 85 KB

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