r8169.c 92 KB

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