r8169.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524
  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 <linux/pm_runtime.h>
  26. #include <linux/firmware.h>
  27. #include <asm/system.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #define RTL8169_VERSION "2.3LK-NAPI"
  31. #define MODULENAME "r8169"
  32. #define PFX MODULENAME ": "
  33. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  34. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  35. #ifdef RTL8169_DEBUG
  36. #define assert(expr) \
  37. if (!(expr)) { \
  38. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  39. #expr,__FILE__,__func__,__LINE__); \
  40. }
  41. #define dprintk(fmt, args...) \
  42. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  43. #else
  44. #define assert(expr) do {} while (0)
  45. #define dprintk(fmt, args...) do {} while (0)
  46. #endif /* RTL8169_DEBUG */
  47. #define R8169_MSG_DEFAULT \
  48. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  49. #define TX_BUFFS_AVAIL(tp) \
  50. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  51. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  52. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  53. static const int multicast_filter_limit = 32;
  54. /* MAC address length */
  55. #define MAC_ADDR_LEN 6
  56. #define MAX_READ_REQUEST_SHIFT 12
  57. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  58. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  59. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  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. #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
  72. #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
  73. #define RTL_EEPROM_SIG_ADDR 0x0000
  74. /* write/read MMIO register */
  75. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  76. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  77. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  78. #define RTL_R8(reg) readb (ioaddr + (reg))
  79. #define RTL_R16(reg) readw (ioaddr + (reg))
  80. #define RTL_R32(reg) readl (ioaddr + (reg))
  81. enum mac_version {
  82. RTL_GIGA_MAC_NONE = 0x00,
  83. RTL_GIGA_MAC_VER_01 = 0x01, // 8169
  84. RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
  85. RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
  86. RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
  87. RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
  88. RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
  89. RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
  90. RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
  91. RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
  92. RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
  93. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  94. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
  95. RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
  96. RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
  97. RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
  98. RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
  99. RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
  100. RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
  101. RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
  102. RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
  103. RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
  104. RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
  105. RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
  106. RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
  107. RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
  108. RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
  109. RTL_GIGA_MAC_VER_27 = 0x1b // 8168DP
  110. };
  111. #define _R(NAME,MAC,MASK) \
  112. { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
  113. static const struct {
  114. const char *name;
  115. u8 mac_version;
  116. u32 RxConfigMask; /* Clears the bits supported by this chip */
  117. } rtl_chip_info[] = {
  118. _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
  119. _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
  120. _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
  121. _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
  122. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
  123. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
  124. _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
  125. _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
  126. _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
  127. _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
  128. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
  129. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
  130. _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
  131. _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
  132. _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
  133. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
  134. _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
  135. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
  136. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
  137. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
  138. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
  139. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
  140. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
  141. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
  142. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
  143. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
  144. _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880) // PCI-E
  145. };
  146. #undef _R
  147. enum cfg_version {
  148. RTL_CFG_0 = 0x00,
  149. RTL_CFG_1,
  150. RTL_CFG_2
  151. };
  152. static void rtl_hw_start_8169(struct net_device *);
  153. static void rtl_hw_start_8168(struct net_device *);
  154. static void rtl_hw_start_8101(struct net_device *);
  155. static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
  156. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  157. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  158. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  159. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  160. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  161. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  162. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  163. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  164. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  165. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  166. { 0x0001, 0x8168,
  167. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  168. {0,},
  169. };
  170. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  171. static int rx_buf_sz = 16383;
  172. static int use_dac;
  173. static struct {
  174. u32 msg_enable;
  175. } debug = { -1 };
  176. enum rtl_registers {
  177. MAC0 = 0, /* Ethernet hardware address. */
  178. MAC4 = 4,
  179. MAR0 = 8, /* Multicast filter. */
  180. CounterAddrLow = 0x10,
  181. CounterAddrHigh = 0x14,
  182. TxDescStartAddrLow = 0x20,
  183. TxDescStartAddrHigh = 0x24,
  184. TxHDescStartAddrLow = 0x28,
  185. TxHDescStartAddrHigh = 0x2c,
  186. FLASH = 0x30,
  187. ERSR = 0x36,
  188. ChipCmd = 0x37,
  189. TxPoll = 0x38,
  190. IntrMask = 0x3c,
  191. IntrStatus = 0x3e,
  192. TxConfig = 0x40,
  193. RxConfig = 0x44,
  194. RxMissed = 0x4c,
  195. Cfg9346 = 0x50,
  196. Config0 = 0x51,
  197. Config1 = 0x52,
  198. Config2 = 0x53,
  199. Config3 = 0x54,
  200. Config4 = 0x55,
  201. Config5 = 0x56,
  202. MultiIntr = 0x5c,
  203. PHYAR = 0x60,
  204. PHYstatus = 0x6c,
  205. RxMaxSize = 0xda,
  206. CPlusCmd = 0xe0,
  207. IntrMitigate = 0xe2,
  208. RxDescAddrLow = 0xe4,
  209. RxDescAddrHigh = 0xe8,
  210. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  211. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  212. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  213. #define TxPacketMax (8064 >> 7)
  214. FuncEvent = 0xf0,
  215. FuncEventMask = 0xf4,
  216. FuncPresetState = 0xf8,
  217. FuncForceEvent = 0xfc,
  218. };
  219. enum rtl8110_registers {
  220. TBICSR = 0x64,
  221. TBI_ANAR = 0x68,
  222. TBI_LPAR = 0x6a,
  223. };
  224. enum rtl8168_8101_registers {
  225. CSIDR = 0x64,
  226. CSIAR = 0x68,
  227. #define CSIAR_FLAG 0x80000000
  228. #define CSIAR_WRITE_CMD 0x80000000
  229. #define CSIAR_BYTE_ENABLE 0x0f
  230. #define CSIAR_BYTE_ENABLE_SHIFT 12
  231. #define CSIAR_ADDR_MASK 0x0fff
  232. EPHYAR = 0x80,
  233. #define EPHYAR_FLAG 0x80000000
  234. #define EPHYAR_WRITE_CMD 0x80000000
  235. #define EPHYAR_REG_MASK 0x1f
  236. #define EPHYAR_REG_SHIFT 16
  237. #define EPHYAR_DATA_MASK 0xffff
  238. DBG_REG = 0xd1,
  239. #define FIX_NAK_1 (1 << 4)
  240. #define FIX_NAK_2 (1 << 3)
  241. EFUSEAR = 0xdc,
  242. #define EFUSEAR_FLAG 0x80000000
  243. #define EFUSEAR_WRITE_CMD 0x80000000
  244. #define EFUSEAR_READ_CMD 0x00000000
  245. #define EFUSEAR_REG_MASK 0x03ff
  246. #define EFUSEAR_REG_SHIFT 8
  247. #define EFUSEAR_DATA_MASK 0xff
  248. };
  249. enum rtl8168_registers {
  250. EPHY_RXER_NUM = 0x7c,
  251. OCPDR = 0xb0, /* OCP GPHY access */
  252. #define OCPDR_WRITE_CMD 0x80000000
  253. #define OCPDR_READ_CMD 0x00000000
  254. #define OCPDR_REG_MASK 0x7f
  255. #define OCPDR_GPHY_REG_SHIFT 16
  256. #define OCPDR_DATA_MASK 0xffff
  257. OCPAR = 0xb4,
  258. #define OCPAR_FLAG 0x80000000
  259. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  260. #define OCPAR_GPHY_READ_CMD 0x0000f060
  261. };
  262. enum rtl_register_content {
  263. /* InterruptStatusBits */
  264. SYSErr = 0x8000,
  265. PCSTimeout = 0x4000,
  266. SWInt = 0x0100,
  267. TxDescUnavail = 0x0080,
  268. RxFIFOOver = 0x0040,
  269. LinkChg = 0x0020,
  270. RxOverflow = 0x0010,
  271. TxErr = 0x0008,
  272. TxOK = 0x0004,
  273. RxErr = 0x0002,
  274. RxOK = 0x0001,
  275. /* RxStatusDesc */
  276. RxFOVF = (1 << 23),
  277. RxRWT = (1 << 22),
  278. RxRES = (1 << 21),
  279. RxRUNT = (1 << 20),
  280. RxCRC = (1 << 19),
  281. /* ChipCmdBits */
  282. CmdReset = 0x10,
  283. CmdRxEnb = 0x08,
  284. CmdTxEnb = 0x04,
  285. RxBufEmpty = 0x01,
  286. /* TXPoll register p.5 */
  287. HPQ = 0x80, /* Poll cmd on the high prio queue */
  288. NPQ = 0x40, /* Poll cmd on the low prio queue */
  289. FSWInt = 0x01, /* Forced software interrupt */
  290. /* Cfg9346Bits */
  291. Cfg9346_Lock = 0x00,
  292. Cfg9346_Unlock = 0xc0,
  293. /* rx_mode_bits */
  294. AcceptErr = 0x20,
  295. AcceptRunt = 0x10,
  296. AcceptBroadcast = 0x08,
  297. AcceptMulticast = 0x04,
  298. AcceptMyPhys = 0x02,
  299. AcceptAllPhys = 0x01,
  300. /* RxConfigBits */
  301. RxCfgFIFOShift = 13,
  302. RxCfgDMAShift = 8,
  303. /* TxConfigBits */
  304. TxInterFrameGapShift = 24,
  305. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  306. /* Config1 register p.24 */
  307. LEDS1 = (1 << 7),
  308. LEDS0 = (1 << 6),
  309. MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
  310. Speed_down = (1 << 4),
  311. MEMMAP = (1 << 3),
  312. IOMAP = (1 << 2),
  313. VPD = (1 << 1),
  314. PMEnable = (1 << 0), /* Power Management Enable */
  315. /* Config2 register p. 25 */
  316. PCI_Clock_66MHz = 0x01,
  317. PCI_Clock_33MHz = 0x00,
  318. /* Config3 register p.25 */
  319. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  320. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  321. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  322. /* Config5 register p.27 */
  323. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  324. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  325. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  326. LanWake = (1 << 1), /* LanWake enable/disable */
  327. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  328. /* TBICSR p.28 */
  329. TBIReset = 0x80000000,
  330. TBILoopback = 0x40000000,
  331. TBINwEnable = 0x20000000,
  332. TBINwRestart = 0x10000000,
  333. TBILinkOk = 0x02000000,
  334. TBINwComplete = 0x01000000,
  335. /* CPlusCmd p.31 */
  336. EnableBist = (1 << 15), // 8168 8101
  337. Mac_dbgo_oe = (1 << 14), // 8168 8101
  338. Normal_mode = (1 << 13), // unused
  339. Force_half_dup = (1 << 12), // 8168 8101
  340. Force_rxflow_en = (1 << 11), // 8168 8101
  341. Force_txflow_en = (1 << 10), // 8168 8101
  342. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  343. ASF = (1 << 8), // 8168 8101
  344. PktCntrDisable = (1 << 7), // 8168 8101
  345. Mac_dbgo_sel = 0x001c, // 8168
  346. RxVlan = (1 << 6),
  347. RxChkSum = (1 << 5),
  348. PCIDAC = (1 << 4),
  349. PCIMulRW = (1 << 3),
  350. INTT_0 = 0x0000, // 8168
  351. INTT_1 = 0x0001, // 8168
  352. INTT_2 = 0x0002, // 8168
  353. INTT_3 = 0x0003, // 8168
  354. /* rtl8169_PHYstatus */
  355. TBI_Enable = 0x80,
  356. TxFlowCtrl = 0x40,
  357. RxFlowCtrl = 0x20,
  358. _1000bpsF = 0x10,
  359. _100bps = 0x08,
  360. _10bps = 0x04,
  361. LinkStatus = 0x02,
  362. FullDup = 0x01,
  363. /* _TBICSRBit */
  364. TBILinkOK = 0x02000000,
  365. /* DumpCounterCommand */
  366. CounterDump = 0x8,
  367. };
  368. enum desc_status_bit {
  369. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  370. RingEnd = (1 << 30), /* End of descriptor ring */
  371. FirstFrag = (1 << 29), /* First segment of a packet */
  372. LastFrag = (1 << 28), /* Final segment of a packet */
  373. /* Tx private */
  374. LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
  375. MSSShift = 16, /* MSS value position */
  376. MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
  377. IPCS = (1 << 18), /* Calculate IP checksum */
  378. UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
  379. TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
  380. TxVlanTag = (1 << 17), /* Add VLAN tag */
  381. /* Rx private */
  382. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  383. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  384. #define RxProtoUDP (PID1)
  385. #define RxProtoTCP (PID0)
  386. #define RxProtoIP (PID1 | PID0)
  387. #define RxProtoMask RxProtoIP
  388. IPFail = (1 << 16), /* IP checksum failed */
  389. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  390. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  391. RxVlanTag = (1 << 16), /* VLAN tag available */
  392. };
  393. #define RsvdMask 0x3fffc000
  394. struct TxDesc {
  395. __le32 opts1;
  396. __le32 opts2;
  397. __le64 addr;
  398. };
  399. struct RxDesc {
  400. __le32 opts1;
  401. __le32 opts2;
  402. __le64 addr;
  403. };
  404. struct ring_info {
  405. struct sk_buff *skb;
  406. u32 len;
  407. u8 __pad[sizeof(void *) - sizeof(u32)];
  408. };
  409. enum features {
  410. RTL_FEATURE_WOL = (1 << 0),
  411. RTL_FEATURE_MSI = (1 << 1),
  412. RTL_FEATURE_GMII = (1 << 2),
  413. };
  414. struct rtl8169_counters {
  415. __le64 tx_packets;
  416. __le64 rx_packets;
  417. __le64 tx_errors;
  418. __le32 rx_errors;
  419. __le16 rx_missed;
  420. __le16 align_errors;
  421. __le32 tx_one_collision;
  422. __le32 tx_multi_collision;
  423. __le64 rx_unicast;
  424. __le64 rx_broadcast;
  425. __le32 rx_multicast;
  426. __le16 tx_aborted;
  427. __le16 tx_underun;
  428. };
  429. struct rtl8169_private {
  430. void __iomem *mmio_addr; /* memory map physical address */
  431. struct pci_dev *pci_dev; /* Index of PCI device */
  432. struct net_device *dev;
  433. struct napi_struct napi;
  434. spinlock_t lock; /* spin lock flag */
  435. u32 msg_enable;
  436. int chipset;
  437. int mac_version;
  438. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  439. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  440. u32 dirty_rx;
  441. u32 dirty_tx;
  442. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  443. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  444. dma_addr_t TxPhyAddr;
  445. dma_addr_t RxPhyAddr;
  446. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  447. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  448. struct timer_list timer;
  449. u16 cp_cmd;
  450. u16 intr_event;
  451. u16 napi_event;
  452. u16 intr_mask;
  453. int phy_1000_ctrl_reg;
  454. #ifdef CONFIG_R8169_VLAN
  455. struct vlan_group *vlgrp;
  456. #endif
  457. struct mdio_ops {
  458. void (*write)(void __iomem *, int, int);
  459. int (*read)(void __iomem *, int);
  460. } mdio_ops;
  461. int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
  462. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  463. void (*phy_reset_enable)(struct rtl8169_private *tp);
  464. void (*hw_start)(struct net_device *);
  465. unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
  466. unsigned int (*link_ok)(void __iomem *);
  467. int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
  468. int pcie_cap;
  469. struct delayed_work task;
  470. unsigned features;
  471. struct mii_if_info mii;
  472. struct rtl8169_counters counters;
  473. u32 saved_wolopts;
  474. };
  475. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  476. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  477. module_param(use_dac, int, 0);
  478. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  479. module_param_named(debug, debug.msg_enable, int, 0);
  480. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  481. MODULE_LICENSE("GPL");
  482. MODULE_VERSION(RTL8169_VERSION);
  483. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  484. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  485. static int rtl8169_open(struct net_device *dev);
  486. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  487. struct net_device *dev);
  488. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  489. static int rtl8169_init_ring(struct net_device *dev);
  490. static void rtl_hw_start(struct net_device *dev);
  491. static int rtl8169_close(struct net_device *dev);
  492. static void rtl_set_rx_mode(struct net_device *dev);
  493. static void rtl8169_tx_timeout(struct net_device *dev);
  494. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  495. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  496. void __iomem *, u32 budget);
  497. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  498. static void rtl8169_down(struct net_device *dev);
  499. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  500. static int rtl8169_poll(struct napi_struct *napi, int budget);
  501. static const unsigned int rtl8169_rx_config =
  502. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  503. static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  504. {
  505. int i;
  506. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  507. for (i = 20; i > 0; i--) {
  508. /*
  509. * Check if the RTL8169 has completed writing to the specified
  510. * MII register.
  511. */
  512. if (!(RTL_R32(PHYAR) & 0x80000000))
  513. break;
  514. udelay(25);
  515. }
  516. /*
  517. * According to hardware specs a 20us delay is required after write
  518. * complete indication, but before sending next command.
  519. */
  520. udelay(20);
  521. }
  522. static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
  523. {
  524. int i, value = -1;
  525. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  526. for (i = 20; i > 0; i--) {
  527. /*
  528. * Check if the RTL8169 has completed retrieving data from
  529. * the specified MII register.
  530. */
  531. if (RTL_R32(PHYAR) & 0x80000000) {
  532. value = RTL_R32(PHYAR) & 0xffff;
  533. break;
  534. }
  535. udelay(25);
  536. }
  537. /*
  538. * According to hardware specs a 20us delay is required after read
  539. * complete indication, but before sending next command.
  540. */
  541. udelay(20);
  542. return value;
  543. }
  544. static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
  545. {
  546. int i;
  547. RTL_W32(OCPDR, data |
  548. ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  549. RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
  550. RTL_W32(EPHY_RXER_NUM, 0);
  551. for (i = 0; i < 100; i++) {
  552. mdelay(1);
  553. if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
  554. break;
  555. }
  556. }
  557. static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  558. {
  559. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
  560. (value & OCPDR_DATA_MASK));
  561. }
  562. static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
  563. {
  564. int i;
  565. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
  566. mdelay(1);
  567. RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
  568. RTL_W32(EPHY_RXER_NUM, 0);
  569. for (i = 0; i < 100; i++) {
  570. mdelay(1);
  571. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  572. break;
  573. }
  574. return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
  575. }
  576. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  577. {
  578. tp->mdio_ops.write(tp->mmio_addr, location, val);
  579. }
  580. static int rtl_readphy(struct rtl8169_private *tp, int location)
  581. {
  582. return tp->mdio_ops.read(tp->mmio_addr, location);
  583. }
  584. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  585. {
  586. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  587. }
  588. static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  589. {
  590. int val;
  591. val = rtl_readphy(tp, reg_addr);
  592. rtl_writephy(tp, reg_addr, (val | p) & ~m);
  593. }
  594. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  595. int val)
  596. {
  597. struct rtl8169_private *tp = netdev_priv(dev);
  598. rtl_writephy(tp, location, val);
  599. }
  600. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  601. {
  602. struct rtl8169_private *tp = netdev_priv(dev);
  603. return rtl_readphy(tp, location);
  604. }
  605. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  606. {
  607. unsigned int i;
  608. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  609. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  610. for (i = 0; i < 100; i++) {
  611. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  612. break;
  613. udelay(10);
  614. }
  615. }
  616. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  617. {
  618. u16 value = 0xffff;
  619. unsigned int i;
  620. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  621. for (i = 0; i < 100; i++) {
  622. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  623. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  624. break;
  625. }
  626. udelay(10);
  627. }
  628. return value;
  629. }
  630. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  631. {
  632. unsigned int i;
  633. RTL_W32(CSIDR, value);
  634. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  635. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  636. for (i = 0; i < 100; i++) {
  637. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  638. break;
  639. udelay(10);
  640. }
  641. }
  642. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  643. {
  644. u32 value = ~0x00;
  645. unsigned int i;
  646. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  647. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  648. for (i = 0; i < 100; i++) {
  649. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  650. value = RTL_R32(CSIDR);
  651. break;
  652. }
  653. udelay(10);
  654. }
  655. return value;
  656. }
  657. static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
  658. {
  659. u8 value = 0xff;
  660. unsigned int i;
  661. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  662. for (i = 0; i < 300; i++) {
  663. if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
  664. value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
  665. break;
  666. }
  667. udelay(100);
  668. }
  669. return value;
  670. }
  671. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  672. {
  673. RTL_W16(IntrMask, 0x0000);
  674. RTL_W16(IntrStatus, 0xffff);
  675. }
  676. static void rtl8169_asic_down(void __iomem *ioaddr)
  677. {
  678. RTL_W8(ChipCmd, 0x00);
  679. rtl8169_irq_mask_and_ack(ioaddr);
  680. RTL_R16(CPlusCmd);
  681. }
  682. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  683. {
  684. void __iomem *ioaddr = tp->mmio_addr;
  685. return RTL_R32(TBICSR) & TBIReset;
  686. }
  687. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  688. {
  689. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  690. }
  691. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  692. {
  693. return RTL_R32(TBICSR) & TBILinkOk;
  694. }
  695. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  696. {
  697. return RTL_R8(PHYstatus) & LinkStatus;
  698. }
  699. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  700. {
  701. void __iomem *ioaddr = tp->mmio_addr;
  702. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  703. }
  704. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  705. {
  706. unsigned int val;
  707. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  708. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  709. }
  710. static void __rtl8169_check_link_status(struct net_device *dev,
  711. struct rtl8169_private *tp,
  712. void __iomem *ioaddr,
  713. bool pm)
  714. {
  715. unsigned long flags;
  716. spin_lock_irqsave(&tp->lock, flags);
  717. if (tp->link_ok(ioaddr)) {
  718. /* This is to cancel a scheduled suspend if there's one. */
  719. if (pm)
  720. pm_request_resume(&tp->pci_dev->dev);
  721. netif_carrier_on(dev);
  722. netif_info(tp, ifup, dev, "link up\n");
  723. } else {
  724. netif_carrier_off(dev);
  725. netif_info(tp, ifdown, dev, "link down\n");
  726. if (pm)
  727. pm_schedule_suspend(&tp->pci_dev->dev, 100);
  728. }
  729. spin_unlock_irqrestore(&tp->lock, flags);
  730. }
  731. static void rtl8169_check_link_status(struct net_device *dev,
  732. struct rtl8169_private *tp,
  733. void __iomem *ioaddr)
  734. {
  735. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  736. }
  737. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  738. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  739. {
  740. void __iomem *ioaddr = tp->mmio_addr;
  741. u8 options;
  742. u32 wolopts = 0;
  743. options = RTL_R8(Config1);
  744. if (!(options & PMEnable))
  745. return 0;
  746. options = RTL_R8(Config3);
  747. if (options & LinkUp)
  748. wolopts |= WAKE_PHY;
  749. if (options & MagicPacket)
  750. wolopts |= WAKE_MAGIC;
  751. options = RTL_R8(Config5);
  752. if (options & UWF)
  753. wolopts |= WAKE_UCAST;
  754. if (options & BWF)
  755. wolopts |= WAKE_BCAST;
  756. if (options & MWF)
  757. wolopts |= WAKE_MCAST;
  758. return wolopts;
  759. }
  760. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  761. {
  762. struct rtl8169_private *tp = netdev_priv(dev);
  763. spin_lock_irq(&tp->lock);
  764. wol->supported = WAKE_ANY;
  765. wol->wolopts = __rtl8169_get_wol(tp);
  766. spin_unlock_irq(&tp->lock);
  767. }
  768. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  769. {
  770. void __iomem *ioaddr = tp->mmio_addr;
  771. unsigned int i;
  772. static const struct {
  773. u32 opt;
  774. u16 reg;
  775. u8 mask;
  776. } cfg[] = {
  777. { WAKE_ANY, Config1, PMEnable },
  778. { WAKE_PHY, Config3, LinkUp },
  779. { WAKE_MAGIC, Config3, MagicPacket },
  780. { WAKE_UCAST, Config5, UWF },
  781. { WAKE_BCAST, Config5, BWF },
  782. { WAKE_MCAST, Config5, MWF },
  783. { WAKE_ANY, Config5, LanWake }
  784. };
  785. RTL_W8(Cfg9346, Cfg9346_Unlock);
  786. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  787. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  788. if (wolopts & cfg[i].opt)
  789. options |= cfg[i].mask;
  790. RTL_W8(cfg[i].reg, options);
  791. }
  792. RTL_W8(Cfg9346, Cfg9346_Lock);
  793. }
  794. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  795. {
  796. struct rtl8169_private *tp = netdev_priv(dev);
  797. spin_lock_irq(&tp->lock);
  798. if (wol->wolopts)
  799. tp->features |= RTL_FEATURE_WOL;
  800. else
  801. tp->features &= ~RTL_FEATURE_WOL;
  802. __rtl8169_set_wol(tp, wol->wolopts);
  803. spin_unlock_irq(&tp->lock);
  804. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  805. return 0;
  806. }
  807. static void rtl8169_get_drvinfo(struct net_device *dev,
  808. struct ethtool_drvinfo *info)
  809. {
  810. struct rtl8169_private *tp = netdev_priv(dev);
  811. strcpy(info->driver, MODULENAME);
  812. strcpy(info->version, RTL8169_VERSION);
  813. strcpy(info->bus_info, pci_name(tp->pci_dev));
  814. }
  815. static int rtl8169_get_regs_len(struct net_device *dev)
  816. {
  817. return R8169_REGS_SIZE;
  818. }
  819. static int rtl8169_set_speed_tbi(struct net_device *dev,
  820. u8 autoneg, u16 speed, u8 duplex)
  821. {
  822. struct rtl8169_private *tp = netdev_priv(dev);
  823. void __iomem *ioaddr = tp->mmio_addr;
  824. int ret = 0;
  825. u32 reg;
  826. reg = RTL_R32(TBICSR);
  827. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  828. (duplex == DUPLEX_FULL)) {
  829. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  830. } else if (autoneg == AUTONEG_ENABLE)
  831. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  832. else {
  833. netif_warn(tp, link, dev,
  834. "incorrect speed setting refused in TBI mode\n");
  835. ret = -EOPNOTSUPP;
  836. }
  837. return ret;
  838. }
  839. static int rtl8169_set_speed_xmii(struct net_device *dev,
  840. u8 autoneg, u16 speed, u8 duplex)
  841. {
  842. struct rtl8169_private *tp = netdev_priv(dev);
  843. int giga_ctrl, bmcr;
  844. if (autoneg == AUTONEG_ENABLE) {
  845. int auto_nego;
  846. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  847. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  848. ADVERTISE_100HALF | ADVERTISE_100FULL);
  849. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  850. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  851. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  852. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  853. if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
  854. (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
  855. (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
  856. (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
  857. (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
  858. (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
  859. (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
  860. (tp->mac_version != RTL_GIGA_MAC_VER_16)) {
  861. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  862. } else {
  863. netif_info(tp, link, dev,
  864. "PHY does not support 1000Mbps\n");
  865. }
  866. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  867. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  868. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  869. (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
  870. /*
  871. * Wake up the PHY.
  872. * Vendor specific (0x1f) and reserved (0x0e) MII
  873. * registers.
  874. */
  875. rtl_writephy(tp, 0x1f, 0x0000);
  876. rtl_writephy(tp, 0x0e, 0x0000);
  877. }
  878. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  879. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  880. } else {
  881. giga_ctrl = 0;
  882. if (speed == SPEED_10)
  883. bmcr = 0;
  884. else if (speed == SPEED_100)
  885. bmcr = BMCR_SPEED100;
  886. else
  887. return -EINVAL;
  888. if (duplex == DUPLEX_FULL)
  889. bmcr |= BMCR_FULLDPLX;
  890. rtl_writephy(tp, 0x1f, 0x0000);
  891. }
  892. tp->phy_1000_ctrl_reg = giga_ctrl;
  893. rtl_writephy(tp, MII_BMCR, bmcr);
  894. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  895. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  896. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  897. rtl_writephy(tp, 0x17, 0x2138);
  898. rtl_writephy(tp, 0x0e, 0x0260);
  899. } else {
  900. rtl_writephy(tp, 0x17, 0x2108);
  901. rtl_writephy(tp, 0x0e, 0x0000);
  902. }
  903. }
  904. return 0;
  905. }
  906. static int rtl8169_set_speed(struct net_device *dev,
  907. u8 autoneg, u16 speed, u8 duplex)
  908. {
  909. struct rtl8169_private *tp = netdev_priv(dev);
  910. int ret;
  911. ret = tp->set_speed(dev, autoneg, speed, duplex);
  912. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  913. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  914. return ret;
  915. }
  916. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  917. {
  918. struct rtl8169_private *tp = netdev_priv(dev);
  919. unsigned long flags;
  920. int ret;
  921. spin_lock_irqsave(&tp->lock, flags);
  922. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  923. spin_unlock_irqrestore(&tp->lock, flags);
  924. return ret;
  925. }
  926. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  927. {
  928. struct rtl8169_private *tp = netdev_priv(dev);
  929. return tp->cp_cmd & RxChkSum;
  930. }
  931. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  932. {
  933. struct rtl8169_private *tp = netdev_priv(dev);
  934. void __iomem *ioaddr = tp->mmio_addr;
  935. unsigned long flags;
  936. spin_lock_irqsave(&tp->lock, flags);
  937. if (data)
  938. tp->cp_cmd |= RxChkSum;
  939. else
  940. tp->cp_cmd &= ~RxChkSum;
  941. RTL_W16(CPlusCmd, tp->cp_cmd);
  942. RTL_R16(CPlusCmd);
  943. spin_unlock_irqrestore(&tp->lock, flags);
  944. return 0;
  945. }
  946. #ifdef CONFIG_R8169_VLAN
  947. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  948. struct sk_buff *skb)
  949. {
  950. return (vlan_tx_tag_present(skb)) ?
  951. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  952. }
  953. static void rtl8169_vlan_rx_register(struct net_device *dev,
  954. struct vlan_group *grp)
  955. {
  956. struct rtl8169_private *tp = netdev_priv(dev);
  957. void __iomem *ioaddr = tp->mmio_addr;
  958. unsigned long flags;
  959. spin_lock_irqsave(&tp->lock, flags);
  960. tp->vlgrp = grp;
  961. /*
  962. * Do not disable RxVlan on 8110SCd.
  963. */
  964. if (tp->vlgrp || (tp->mac_version == RTL_GIGA_MAC_VER_05))
  965. tp->cp_cmd |= RxVlan;
  966. else
  967. tp->cp_cmd &= ~RxVlan;
  968. RTL_W16(CPlusCmd, tp->cp_cmd);
  969. RTL_R16(CPlusCmd);
  970. spin_unlock_irqrestore(&tp->lock, flags);
  971. }
  972. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  973. struct sk_buff *skb, int polling)
  974. {
  975. u32 opts2 = le32_to_cpu(desc->opts2);
  976. struct vlan_group *vlgrp = tp->vlgrp;
  977. int ret;
  978. if (vlgrp && (opts2 & RxVlanTag)) {
  979. u16 vtag = swab16(opts2 & 0xffff);
  980. if (likely(polling))
  981. vlan_gro_receive(&tp->napi, vlgrp, vtag, skb);
  982. else
  983. __vlan_hwaccel_rx(skb, vlgrp, vtag, polling);
  984. ret = 0;
  985. } else
  986. ret = -1;
  987. desc->opts2 = 0;
  988. return ret;
  989. }
  990. #else /* !CONFIG_R8169_VLAN */
  991. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  992. struct sk_buff *skb)
  993. {
  994. return 0;
  995. }
  996. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  997. struct sk_buff *skb, int polling)
  998. {
  999. return -1;
  1000. }
  1001. #endif
  1002. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1003. {
  1004. struct rtl8169_private *tp = netdev_priv(dev);
  1005. void __iomem *ioaddr = tp->mmio_addr;
  1006. u32 status;
  1007. cmd->supported =
  1008. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1009. cmd->port = PORT_FIBRE;
  1010. cmd->transceiver = XCVR_INTERNAL;
  1011. status = RTL_R32(TBICSR);
  1012. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1013. cmd->autoneg = !!(status & TBINwEnable);
  1014. cmd->speed = SPEED_1000;
  1015. cmd->duplex = DUPLEX_FULL; /* Always set */
  1016. return 0;
  1017. }
  1018. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1019. {
  1020. struct rtl8169_private *tp = netdev_priv(dev);
  1021. return mii_ethtool_gset(&tp->mii, cmd);
  1022. }
  1023. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1024. {
  1025. struct rtl8169_private *tp = netdev_priv(dev);
  1026. unsigned long flags;
  1027. int rc;
  1028. spin_lock_irqsave(&tp->lock, flags);
  1029. rc = tp->get_settings(dev, cmd);
  1030. spin_unlock_irqrestore(&tp->lock, flags);
  1031. return rc;
  1032. }
  1033. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1034. void *p)
  1035. {
  1036. struct rtl8169_private *tp = netdev_priv(dev);
  1037. unsigned long flags;
  1038. if (regs->len > R8169_REGS_SIZE)
  1039. regs->len = R8169_REGS_SIZE;
  1040. spin_lock_irqsave(&tp->lock, flags);
  1041. memcpy_fromio(p, tp->mmio_addr, regs->len);
  1042. spin_unlock_irqrestore(&tp->lock, flags);
  1043. }
  1044. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1045. {
  1046. struct rtl8169_private *tp = netdev_priv(dev);
  1047. return tp->msg_enable;
  1048. }
  1049. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1050. {
  1051. struct rtl8169_private *tp = netdev_priv(dev);
  1052. tp->msg_enable = value;
  1053. }
  1054. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1055. "tx_packets",
  1056. "rx_packets",
  1057. "tx_errors",
  1058. "rx_errors",
  1059. "rx_missed",
  1060. "align_errors",
  1061. "tx_single_collisions",
  1062. "tx_multi_collisions",
  1063. "unicast",
  1064. "broadcast",
  1065. "multicast",
  1066. "tx_aborted",
  1067. "tx_underrun",
  1068. };
  1069. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1070. {
  1071. switch (sset) {
  1072. case ETH_SS_STATS:
  1073. return ARRAY_SIZE(rtl8169_gstrings);
  1074. default:
  1075. return -EOPNOTSUPP;
  1076. }
  1077. }
  1078. static void rtl8169_update_counters(struct net_device *dev)
  1079. {
  1080. struct rtl8169_private *tp = netdev_priv(dev);
  1081. void __iomem *ioaddr = tp->mmio_addr;
  1082. struct rtl8169_counters *counters;
  1083. dma_addr_t paddr;
  1084. u32 cmd;
  1085. int wait = 1000;
  1086. struct device *d = &tp->pci_dev->dev;
  1087. /*
  1088. * Some chips are unable to dump tally counters when the receiver
  1089. * is disabled.
  1090. */
  1091. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1092. return;
  1093. counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
  1094. if (!counters)
  1095. return;
  1096. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1097. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1098. RTL_W32(CounterAddrLow, cmd);
  1099. RTL_W32(CounterAddrLow, cmd | CounterDump);
  1100. while (wait--) {
  1101. if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
  1102. /* copy updated counters */
  1103. memcpy(&tp->counters, counters, sizeof(*counters));
  1104. break;
  1105. }
  1106. udelay(10);
  1107. }
  1108. RTL_W32(CounterAddrLow, 0);
  1109. RTL_W32(CounterAddrHigh, 0);
  1110. dma_free_coherent(d, sizeof(*counters), counters, paddr);
  1111. }
  1112. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1113. struct ethtool_stats *stats, u64 *data)
  1114. {
  1115. struct rtl8169_private *tp = netdev_priv(dev);
  1116. ASSERT_RTNL();
  1117. rtl8169_update_counters(dev);
  1118. data[0] = le64_to_cpu(tp->counters.tx_packets);
  1119. data[1] = le64_to_cpu(tp->counters.rx_packets);
  1120. data[2] = le64_to_cpu(tp->counters.tx_errors);
  1121. data[3] = le32_to_cpu(tp->counters.rx_errors);
  1122. data[4] = le16_to_cpu(tp->counters.rx_missed);
  1123. data[5] = le16_to_cpu(tp->counters.align_errors);
  1124. data[6] = le32_to_cpu(tp->counters.tx_one_collision);
  1125. data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
  1126. data[8] = le64_to_cpu(tp->counters.rx_unicast);
  1127. data[9] = le64_to_cpu(tp->counters.rx_broadcast);
  1128. data[10] = le32_to_cpu(tp->counters.rx_multicast);
  1129. data[11] = le16_to_cpu(tp->counters.tx_aborted);
  1130. data[12] = le16_to_cpu(tp->counters.tx_underun);
  1131. }
  1132. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1133. {
  1134. switch(stringset) {
  1135. case ETH_SS_STATS:
  1136. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1137. break;
  1138. }
  1139. }
  1140. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1141. .get_drvinfo = rtl8169_get_drvinfo,
  1142. .get_regs_len = rtl8169_get_regs_len,
  1143. .get_link = ethtool_op_get_link,
  1144. .get_settings = rtl8169_get_settings,
  1145. .set_settings = rtl8169_set_settings,
  1146. .get_msglevel = rtl8169_get_msglevel,
  1147. .set_msglevel = rtl8169_set_msglevel,
  1148. .get_rx_csum = rtl8169_get_rx_csum,
  1149. .set_rx_csum = rtl8169_set_rx_csum,
  1150. .set_tx_csum = ethtool_op_set_tx_csum,
  1151. .set_sg = ethtool_op_set_sg,
  1152. .set_tso = ethtool_op_set_tso,
  1153. .get_regs = rtl8169_get_regs,
  1154. .get_wol = rtl8169_get_wol,
  1155. .set_wol = rtl8169_set_wol,
  1156. .get_strings = rtl8169_get_strings,
  1157. .get_sset_count = rtl8169_get_sset_count,
  1158. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1159. };
  1160. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1161. void __iomem *ioaddr)
  1162. {
  1163. /*
  1164. * The driver currently handles the 8168Bf and the 8168Be identically
  1165. * but they can be identified more specifically through the test below
  1166. * if needed:
  1167. *
  1168. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1169. *
  1170. * Same thing for the 8101Eb and the 8101Ec:
  1171. *
  1172. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1173. */
  1174. static const struct {
  1175. u32 mask;
  1176. u32 val;
  1177. int mac_version;
  1178. } mac_info[] = {
  1179. /* 8168D family. */
  1180. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  1181. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1182. { 0x7c800000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1183. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1184. /* 8168C family. */
  1185. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  1186. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1187. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1188. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1189. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1190. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1191. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1192. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1193. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1194. /* 8168B family. */
  1195. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1196. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1197. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1198. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1199. /* 8101 family. */
  1200. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1201. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1202. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1203. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1204. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1205. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1206. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1207. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1208. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1209. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1210. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1211. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1212. /* FIXME: where did these entries come from ? -- FR */
  1213. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1214. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1215. /* 8110 family. */
  1216. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1217. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1218. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1219. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1220. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1221. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1222. /* Catch-all */
  1223. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1224. }, *p = mac_info;
  1225. u32 reg;
  1226. reg = RTL_R32(TxConfig);
  1227. while ((reg & p->mask) != p->val)
  1228. p++;
  1229. tp->mac_version = p->mac_version;
  1230. }
  1231. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1232. {
  1233. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1234. }
  1235. struct phy_reg {
  1236. u16 reg;
  1237. u16 val;
  1238. };
  1239. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1240. const struct phy_reg *regs, int len)
  1241. {
  1242. while (len-- > 0) {
  1243. rtl_writephy(tp, regs->reg, regs->val);
  1244. regs++;
  1245. }
  1246. }
  1247. #define PHY_READ 0x00000000
  1248. #define PHY_DATA_OR 0x10000000
  1249. #define PHY_DATA_AND 0x20000000
  1250. #define PHY_BJMPN 0x30000000
  1251. #define PHY_READ_EFUSE 0x40000000
  1252. #define PHY_READ_MAC_BYTE 0x50000000
  1253. #define PHY_WRITE_MAC_BYTE 0x60000000
  1254. #define PHY_CLEAR_READCOUNT 0x70000000
  1255. #define PHY_WRITE 0x80000000
  1256. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1257. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1258. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1259. #define PHY_WRITE_PREVIOUS 0xc0000000
  1260. #define PHY_SKIPN 0xd0000000
  1261. #define PHY_DELAY_MS 0xe0000000
  1262. #define PHY_WRITE_ERI_WORD 0xf0000000
  1263. static void
  1264. rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
  1265. {
  1266. __le32 *phytable = (__le32 *)fw->data;
  1267. struct net_device *dev = tp->dev;
  1268. size_t i;
  1269. if (fw->size % sizeof(*phytable)) {
  1270. netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
  1271. return;
  1272. }
  1273. for (i = 0; i < fw->size / sizeof(*phytable); i++) {
  1274. u32 action = le32_to_cpu(phytable[i]);
  1275. if (!action)
  1276. break;
  1277. if ((action & 0xf0000000) != PHY_WRITE) {
  1278. netif_err(tp, probe, dev,
  1279. "unknown action 0x%08x\n", action);
  1280. return;
  1281. }
  1282. }
  1283. while (i-- != 0) {
  1284. u32 action = le32_to_cpu(*phytable);
  1285. u32 data = action & 0x0000ffff;
  1286. u32 reg = (action & 0x0fff0000) >> 16;
  1287. switch(action & 0xf0000000) {
  1288. case PHY_WRITE:
  1289. rtl_writephy(tp, reg, data);
  1290. phytable++;
  1291. break;
  1292. default:
  1293. BUG();
  1294. }
  1295. }
  1296. }
  1297. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  1298. {
  1299. static const struct phy_reg phy_reg_init[] = {
  1300. { 0x1f, 0x0001 },
  1301. { 0x06, 0x006e },
  1302. { 0x08, 0x0708 },
  1303. { 0x15, 0x4000 },
  1304. { 0x18, 0x65c7 },
  1305. { 0x1f, 0x0001 },
  1306. { 0x03, 0x00a1 },
  1307. { 0x02, 0x0008 },
  1308. { 0x01, 0x0120 },
  1309. { 0x00, 0x1000 },
  1310. { 0x04, 0x0800 },
  1311. { 0x04, 0x0000 },
  1312. { 0x03, 0xff41 },
  1313. { 0x02, 0xdf60 },
  1314. { 0x01, 0x0140 },
  1315. { 0x00, 0x0077 },
  1316. { 0x04, 0x7800 },
  1317. { 0x04, 0x7000 },
  1318. { 0x03, 0x802f },
  1319. { 0x02, 0x4f02 },
  1320. { 0x01, 0x0409 },
  1321. { 0x00, 0xf0f9 },
  1322. { 0x04, 0x9800 },
  1323. { 0x04, 0x9000 },
  1324. { 0x03, 0xdf01 },
  1325. { 0x02, 0xdf20 },
  1326. { 0x01, 0xff95 },
  1327. { 0x00, 0xba00 },
  1328. { 0x04, 0xa800 },
  1329. { 0x04, 0xa000 },
  1330. { 0x03, 0xff41 },
  1331. { 0x02, 0xdf20 },
  1332. { 0x01, 0x0140 },
  1333. { 0x00, 0x00bb },
  1334. { 0x04, 0xb800 },
  1335. { 0x04, 0xb000 },
  1336. { 0x03, 0xdf41 },
  1337. { 0x02, 0xdc60 },
  1338. { 0x01, 0x6340 },
  1339. { 0x00, 0x007d },
  1340. { 0x04, 0xd800 },
  1341. { 0x04, 0xd000 },
  1342. { 0x03, 0xdf01 },
  1343. { 0x02, 0xdf20 },
  1344. { 0x01, 0x100a },
  1345. { 0x00, 0xa0ff },
  1346. { 0x04, 0xf800 },
  1347. { 0x04, 0xf000 },
  1348. { 0x1f, 0x0000 },
  1349. { 0x0b, 0x0000 },
  1350. { 0x00, 0x9200 }
  1351. };
  1352. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1353. }
  1354. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  1355. {
  1356. static const struct phy_reg phy_reg_init[] = {
  1357. { 0x1f, 0x0002 },
  1358. { 0x01, 0x90d0 },
  1359. { 0x1f, 0x0000 }
  1360. };
  1361. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1362. }
  1363. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  1364. {
  1365. struct pci_dev *pdev = tp->pci_dev;
  1366. u16 vendor_id, device_id;
  1367. pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
  1368. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
  1369. if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
  1370. return;
  1371. rtl_writephy(tp, 0x1f, 0x0001);
  1372. rtl_writephy(tp, 0x10, 0xf01b);
  1373. rtl_writephy(tp, 0x1f, 0x0000);
  1374. }
  1375. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  1376. {
  1377. static const struct phy_reg phy_reg_init[] = {
  1378. { 0x1f, 0x0001 },
  1379. { 0x04, 0x0000 },
  1380. { 0x03, 0x00a1 },
  1381. { 0x02, 0x0008 },
  1382. { 0x01, 0x0120 },
  1383. { 0x00, 0x1000 },
  1384. { 0x04, 0x0800 },
  1385. { 0x04, 0x9000 },
  1386. { 0x03, 0x802f },
  1387. { 0x02, 0x4f02 },
  1388. { 0x01, 0x0409 },
  1389. { 0x00, 0xf099 },
  1390. { 0x04, 0x9800 },
  1391. { 0x04, 0xa000 },
  1392. { 0x03, 0xdf01 },
  1393. { 0x02, 0xdf20 },
  1394. { 0x01, 0xff95 },
  1395. { 0x00, 0xba00 },
  1396. { 0x04, 0xa800 },
  1397. { 0x04, 0xf000 },
  1398. { 0x03, 0xdf01 },
  1399. { 0x02, 0xdf20 },
  1400. { 0x01, 0x101a },
  1401. { 0x00, 0xa0ff },
  1402. { 0x04, 0xf800 },
  1403. { 0x04, 0x0000 },
  1404. { 0x1f, 0x0000 },
  1405. { 0x1f, 0x0001 },
  1406. { 0x10, 0xf41b },
  1407. { 0x14, 0xfb54 },
  1408. { 0x18, 0xf5c7 },
  1409. { 0x1f, 0x0000 },
  1410. { 0x1f, 0x0001 },
  1411. { 0x17, 0x0cc0 },
  1412. { 0x1f, 0x0000 }
  1413. };
  1414. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1415. rtl8169scd_hw_phy_config_quirk(tp);
  1416. }
  1417. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  1418. {
  1419. static const struct phy_reg phy_reg_init[] = {
  1420. { 0x1f, 0x0001 },
  1421. { 0x04, 0x0000 },
  1422. { 0x03, 0x00a1 },
  1423. { 0x02, 0x0008 },
  1424. { 0x01, 0x0120 },
  1425. { 0x00, 0x1000 },
  1426. { 0x04, 0x0800 },
  1427. { 0x04, 0x9000 },
  1428. { 0x03, 0x802f },
  1429. { 0x02, 0x4f02 },
  1430. { 0x01, 0x0409 },
  1431. { 0x00, 0xf099 },
  1432. { 0x04, 0x9800 },
  1433. { 0x04, 0xa000 },
  1434. { 0x03, 0xdf01 },
  1435. { 0x02, 0xdf20 },
  1436. { 0x01, 0xff95 },
  1437. { 0x00, 0xba00 },
  1438. { 0x04, 0xa800 },
  1439. { 0x04, 0xf000 },
  1440. { 0x03, 0xdf01 },
  1441. { 0x02, 0xdf20 },
  1442. { 0x01, 0x101a },
  1443. { 0x00, 0xa0ff },
  1444. { 0x04, 0xf800 },
  1445. { 0x04, 0x0000 },
  1446. { 0x1f, 0x0000 },
  1447. { 0x1f, 0x0001 },
  1448. { 0x0b, 0x8480 },
  1449. { 0x1f, 0x0000 },
  1450. { 0x1f, 0x0001 },
  1451. { 0x18, 0x67c7 },
  1452. { 0x04, 0x2000 },
  1453. { 0x03, 0x002f },
  1454. { 0x02, 0x4360 },
  1455. { 0x01, 0x0109 },
  1456. { 0x00, 0x3022 },
  1457. { 0x04, 0x2800 },
  1458. { 0x1f, 0x0000 },
  1459. { 0x1f, 0x0001 },
  1460. { 0x17, 0x0cc0 },
  1461. { 0x1f, 0x0000 }
  1462. };
  1463. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1464. }
  1465. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  1466. {
  1467. static const struct phy_reg phy_reg_init[] = {
  1468. { 0x10, 0xf41b },
  1469. { 0x1f, 0x0000 }
  1470. };
  1471. rtl_writephy(tp, 0x1f, 0x0001);
  1472. rtl_patchphy(tp, 0x16, 1 << 0);
  1473. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1474. }
  1475. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  1476. {
  1477. static const struct phy_reg phy_reg_init[] = {
  1478. { 0x1f, 0x0001 },
  1479. { 0x10, 0xf41b },
  1480. { 0x1f, 0x0000 }
  1481. };
  1482. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1483. }
  1484. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  1485. {
  1486. static const struct phy_reg phy_reg_init[] = {
  1487. { 0x1f, 0x0000 },
  1488. { 0x1d, 0x0f00 },
  1489. { 0x1f, 0x0002 },
  1490. { 0x0c, 0x1ec8 },
  1491. { 0x1f, 0x0000 }
  1492. };
  1493. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1494. }
  1495. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  1496. {
  1497. static const struct phy_reg phy_reg_init[] = {
  1498. { 0x1f, 0x0001 },
  1499. { 0x1d, 0x3d98 },
  1500. { 0x1f, 0x0000 }
  1501. };
  1502. rtl_writephy(tp, 0x1f, 0x0000);
  1503. rtl_patchphy(tp, 0x14, 1 << 5);
  1504. rtl_patchphy(tp, 0x0d, 1 << 5);
  1505. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1506. }
  1507. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  1508. {
  1509. static const struct phy_reg phy_reg_init[] = {
  1510. { 0x1f, 0x0001 },
  1511. { 0x12, 0x2300 },
  1512. { 0x1f, 0x0002 },
  1513. { 0x00, 0x88d4 },
  1514. { 0x01, 0x82b1 },
  1515. { 0x03, 0x7002 },
  1516. { 0x08, 0x9e30 },
  1517. { 0x09, 0x01f0 },
  1518. { 0x0a, 0x5500 },
  1519. { 0x0c, 0x00c8 },
  1520. { 0x1f, 0x0003 },
  1521. { 0x12, 0xc096 },
  1522. { 0x16, 0x000a },
  1523. { 0x1f, 0x0000 },
  1524. { 0x1f, 0x0000 },
  1525. { 0x09, 0x2000 },
  1526. { 0x09, 0x0000 }
  1527. };
  1528. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1529. rtl_patchphy(tp, 0x14, 1 << 5);
  1530. rtl_patchphy(tp, 0x0d, 1 << 5);
  1531. rtl_writephy(tp, 0x1f, 0x0000);
  1532. }
  1533. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  1534. {
  1535. static const struct phy_reg phy_reg_init[] = {
  1536. { 0x1f, 0x0001 },
  1537. { 0x12, 0x2300 },
  1538. { 0x03, 0x802f },
  1539. { 0x02, 0x4f02 },
  1540. { 0x01, 0x0409 },
  1541. { 0x00, 0xf099 },
  1542. { 0x04, 0x9800 },
  1543. { 0x04, 0x9000 },
  1544. { 0x1d, 0x3d98 },
  1545. { 0x1f, 0x0002 },
  1546. { 0x0c, 0x7eb8 },
  1547. { 0x06, 0x0761 },
  1548. { 0x1f, 0x0003 },
  1549. { 0x16, 0x0f0a },
  1550. { 0x1f, 0x0000 }
  1551. };
  1552. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1553. rtl_patchphy(tp, 0x16, 1 << 0);
  1554. rtl_patchphy(tp, 0x14, 1 << 5);
  1555. rtl_patchphy(tp, 0x0d, 1 << 5);
  1556. rtl_writephy(tp, 0x1f, 0x0000);
  1557. }
  1558. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  1559. {
  1560. static const struct phy_reg phy_reg_init[] = {
  1561. { 0x1f, 0x0001 },
  1562. { 0x12, 0x2300 },
  1563. { 0x1d, 0x3d98 },
  1564. { 0x1f, 0x0002 },
  1565. { 0x0c, 0x7eb8 },
  1566. { 0x06, 0x5461 },
  1567. { 0x1f, 0x0003 },
  1568. { 0x16, 0x0f0a },
  1569. { 0x1f, 0x0000 }
  1570. };
  1571. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1572. rtl_patchphy(tp, 0x16, 1 << 0);
  1573. rtl_patchphy(tp, 0x14, 1 << 5);
  1574. rtl_patchphy(tp, 0x0d, 1 << 5);
  1575. rtl_writephy(tp, 0x1f, 0x0000);
  1576. }
  1577. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  1578. {
  1579. rtl8168c_3_hw_phy_config(tp);
  1580. }
  1581. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  1582. {
  1583. static const struct phy_reg phy_reg_init_0[] = {
  1584. /* Channel Estimation */
  1585. { 0x1f, 0x0001 },
  1586. { 0x06, 0x4064 },
  1587. { 0x07, 0x2863 },
  1588. { 0x08, 0x059c },
  1589. { 0x09, 0x26b4 },
  1590. { 0x0a, 0x6a19 },
  1591. { 0x0b, 0xdcc8 },
  1592. { 0x10, 0xf06d },
  1593. { 0x14, 0x7f68 },
  1594. { 0x18, 0x7fd9 },
  1595. { 0x1c, 0xf0ff },
  1596. { 0x1d, 0x3d9c },
  1597. { 0x1f, 0x0003 },
  1598. { 0x12, 0xf49f },
  1599. { 0x13, 0x070b },
  1600. { 0x1a, 0x05ad },
  1601. { 0x14, 0x94c0 },
  1602. /*
  1603. * Tx Error Issue
  1604. * enhance line driver power
  1605. */
  1606. { 0x1f, 0x0002 },
  1607. { 0x06, 0x5561 },
  1608. { 0x1f, 0x0005 },
  1609. { 0x05, 0x8332 },
  1610. { 0x06, 0x5561 },
  1611. /*
  1612. * Can not link to 1Gbps with bad cable
  1613. * Decrease SNR threshold form 21.07dB to 19.04dB
  1614. */
  1615. { 0x1f, 0x0001 },
  1616. { 0x17, 0x0cc0 },
  1617. { 0x1f, 0x0000 },
  1618. { 0x0d, 0xf880 }
  1619. };
  1620. void __iomem *ioaddr = tp->mmio_addr;
  1621. const struct firmware *fw;
  1622. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1623. /*
  1624. * Rx Error Issue
  1625. * Fine Tune Switching regulator parameter
  1626. */
  1627. rtl_writephy(tp, 0x1f, 0x0002);
  1628. rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
  1629. rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
  1630. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1631. static const struct phy_reg phy_reg_init[] = {
  1632. { 0x1f, 0x0002 },
  1633. { 0x05, 0x669a },
  1634. { 0x1f, 0x0005 },
  1635. { 0x05, 0x8330 },
  1636. { 0x06, 0x669a },
  1637. { 0x1f, 0x0002 }
  1638. };
  1639. int val;
  1640. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1641. val = rtl_readphy(tp, 0x0d);
  1642. if ((val & 0x00ff) != 0x006c) {
  1643. static const u32 set[] = {
  1644. 0x0065, 0x0066, 0x0067, 0x0068,
  1645. 0x0069, 0x006a, 0x006b, 0x006c
  1646. };
  1647. int i;
  1648. rtl_writephy(tp, 0x1f, 0x0002);
  1649. val &= 0xff00;
  1650. for (i = 0; i < ARRAY_SIZE(set); i++)
  1651. rtl_writephy(tp, 0x0d, val | set[i]);
  1652. }
  1653. } else {
  1654. static const struct phy_reg phy_reg_init[] = {
  1655. { 0x1f, 0x0002 },
  1656. { 0x05, 0x6662 },
  1657. { 0x1f, 0x0005 },
  1658. { 0x05, 0x8330 },
  1659. { 0x06, 0x6662 }
  1660. };
  1661. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1662. }
  1663. /* RSET couple improve */
  1664. rtl_writephy(tp, 0x1f, 0x0002);
  1665. rtl_patchphy(tp, 0x0d, 0x0300);
  1666. rtl_patchphy(tp, 0x0f, 0x0010);
  1667. /* Fine tune PLL performance */
  1668. rtl_writephy(tp, 0x1f, 0x0002);
  1669. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1670. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1671. rtl_writephy(tp, 0x1f, 0x0005);
  1672. rtl_writephy(tp, 0x05, 0x001b);
  1673. if (rtl_readphy(tp, 0x06) == 0xbf00 &&
  1674. request_firmware(&fw, FIRMWARE_8168D_1, &tp->pci_dev->dev) == 0) {
  1675. rtl_phy_write_fw(tp, fw);
  1676. release_firmware(fw);
  1677. } else {
  1678. netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
  1679. }
  1680. rtl_writephy(tp, 0x1f, 0x0000);
  1681. }
  1682. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  1683. {
  1684. static const struct phy_reg phy_reg_init_0[] = {
  1685. /* Channel Estimation */
  1686. { 0x1f, 0x0001 },
  1687. { 0x06, 0x4064 },
  1688. { 0x07, 0x2863 },
  1689. { 0x08, 0x059c },
  1690. { 0x09, 0x26b4 },
  1691. { 0x0a, 0x6a19 },
  1692. { 0x0b, 0xdcc8 },
  1693. { 0x10, 0xf06d },
  1694. { 0x14, 0x7f68 },
  1695. { 0x18, 0x7fd9 },
  1696. { 0x1c, 0xf0ff },
  1697. { 0x1d, 0x3d9c },
  1698. { 0x1f, 0x0003 },
  1699. { 0x12, 0xf49f },
  1700. { 0x13, 0x070b },
  1701. { 0x1a, 0x05ad },
  1702. { 0x14, 0x94c0 },
  1703. /*
  1704. * Tx Error Issue
  1705. * enhance line driver power
  1706. */
  1707. { 0x1f, 0x0002 },
  1708. { 0x06, 0x5561 },
  1709. { 0x1f, 0x0005 },
  1710. { 0x05, 0x8332 },
  1711. { 0x06, 0x5561 },
  1712. /*
  1713. * Can not link to 1Gbps with bad cable
  1714. * Decrease SNR threshold form 21.07dB to 19.04dB
  1715. */
  1716. { 0x1f, 0x0001 },
  1717. { 0x17, 0x0cc0 },
  1718. { 0x1f, 0x0000 },
  1719. { 0x0d, 0xf880 }
  1720. };
  1721. void __iomem *ioaddr = tp->mmio_addr;
  1722. const struct firmware *fw;
  1723. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1724. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1725. static const struct phy_reg phy_reg_init[] = {
  1726. { 0x1f, 0x0002 },
  1727. { 0x05, 0x669a },
  1728. { 0x1f, 0x0005 },
  1729. { 0x05, 0x8330 },
  1730. { 0x06, 0x669a },
  1731. { 0x1f, 0x0002 }
  1732. };
  1733. int val;
  1734. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1735. val = rtl_readphy(tp, 0x0d);
  1736. if ((val & 0x00ff) != 0x006c) {
  1737. static const u32 set[] = {
  1738. 0x0065, 0x0066, 0x0067, 0x0068,
  1739. 0x0069, 0x006a, 0x006b, 0x006c
  1740. };
  1741. int i;
  1742. rtl_writephy(tp, 0x1f, 0x0002);
  1743. val &= 0xff00;
  1744. for (i = 0; i < ARRAY_SIZE(set); i++)
  1745. rtl_writephy(tp, 0x0d, val | set[i]);
  1746. }
  1747. } else {
  1748. static const struct phy_reg phy_reg_init[] = {
  1749. { 0x1f, 0x0002 },
  1750. { 0x05, 0x2642 },
  1751. { 0x1f, 0x0005 },
  1752. { 0x05, 0x8330 },
  1753. { 0x06, 0x2642 }
  1754. };
  1755. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1756. }
  1757. /* Fine tune PLL performance */
  1758. rtl_writephy(tp, 0x1f, 0x0002);
  1759. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1760. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1761. /* Switching regulator Slew rate */
  1762. rtl_writephy(tp, 0x1f, 0x0002);
  1763. rtl_patchphy(tp, 0x0f, 0x0017);
  1764. rtl_writephy(tp, 0x1f, 0x0005);
  1765. rtl_writephy(tp, 0x05, 0x001b);
  1766. if (rtl_readphy(tp, 0x06) == 0xb300 &&
  1767. request_firmware(&fw, FIRMWARE_8168D_2, &tp->pci_dev->dev) == 0) {
  1768. rtl_phy_write_fw(tp, fw);
  1769. release_firmware(fw);
  1770. } else {
  1771. netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
  1772. }
  1773. rtl_writephy(tp, 0x1f, 0x0000);
  1774. }
  1775. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  1776. {
  1777. static const struct phy_reg phy_reg_init[] = {
  1778. { 0x1f, 0x0002 },
  1779. { 0x10, 0x0008 },
  1780. { 0x0d, 0x006c },
  1781. { 0x1f, 0x0000 },
  1782. { 0x0d, 0xf880 },
  1783. { 0x1f, 0x0001 },
  1784. { 0x17, 0x0cc0 },
  1785. { 0x1f, 0x0001 },
  1786. { 0x0b, 0xa4d8 },
  1787. { 0x09, 0x281c },
  1788. { 0x07, 0x2883 },
  1789. { 0x0a, 0x6b35 },
  1790. { 0x1d, 0x3da4 },
  1791. { 0x1c, 0xeffd },
  1792. { 0x14, 0x7f52 },
  1793. { 0x18, 0x7fc6 },
  1794. { 0x08, 0x0601 },
  1795. { 0x06, 0x4063 },
  1796. { 0x10, 0xf074 },
  1797. { 0x1f, 0x0003 },
  1798. { 0x13, 0x0789 },
  1799. { 0x12, 0xf4bd },
  1800. { 0x1a, 0x04fd },
  1801. { 0x14, 0x84b0 },
  1802. { 0x1f, 0x0000 },
  1803. { 0x00, 0x9200 },
  1804. { 0x1f, 0x0005 },
  1805. { 0x01, 0x0340 },
  1806. { 0x1f, 0x0001 },
  1807. { 0x04, 0x4000 },
  1808. { 0x03, 0x1d21 },
  1809. { 0x02, 0x0c32 },
  1810. { 0x01, 0x0200 },
  1811. { 0x00, 0x5554 },
  1812. { 0x04, 0x4800 },
  1813. { 0x04, 0x4000 },
  1814. { 0x04, 0xf000 },
  1815. { 0x03, 0xdf01 },
  1816. { 0x02, 0xdf20 },
  1817. { 0x01, 0x101a },
  1818. { 0x00, 0xa0ff },
  1819. { 0x04, 0xf800 },
  1820. { 0x04, 0xf000 },
  1821. { 0x1f, 0x0000 },
  1822. { 0x1f, 0x0007 },
  1823. { 0x1e, 0x0023 },
  1824. { 0x16, 0x0000 },
  1825. { 0x1f, 0x0000 }
  1826. };
  1827. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1828. }
  1829. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  1830. {
  1831. static const struct phy_reg phy_reg_init[] = {
  1832. { 0x1f, 0x0003 },
  1833. { 0x08, 0x441d },
  1834. { 0x01, 0x9100 },
  1835. { 0x1f, 0x0000 }
  1836. };
  1837. rtl_writephy(tp, 0x1f, 0x0000);
  1838. rtl_patchphy(tp, 0x11, 1 << 12);
  1839. rtl_patchphy(tp, 0x19, 1 << 13);
  1840. rtl_patchphy(tp, 0x10, 1 << 15);
  1841. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1842. }
  1843. static void rtl_hw_phy_config(struct net_device *dev)
  1844. {
  1845. struct rtl8169_private *tp = netdev_priv(dev);
  1846. rtl8169_print_mac_version(tp);
  1847. switch (tp->mac_version) {
  1848. case RTL_GIGA_MAC_VER_01:
  1849. break;
  1850. case RTL_GIGA_MAC_VER_02:
  1851. case RTL_GIGA_MAC_VER_03:
  1852. rtl8169s_hw_phy_config(tp);
  1853. break;
  1854. case RTL_GIGA_MAC_VER_04:
  1855. rtl8169sb_hw_phy_config(tp);
  1856. break;
  1857. case RTL_GIGA_MAC_VER_05:
  1858. rtl8169scd_hw_phy_config(tp);
  1859. break;
  1860. case RTL_GIGA_MAC_VER_06:
  1861. rtl8169sce_hw_phy_config(tp);
  1862. break;
  1863. case RTL_GIGA_MAC_VER_07:
  1864. case RTL_GIGA_MAC_VER_08:
  1865. case RTL_GIGA_MAC_VER_09:
  1866. rtl8102e_hw_phy_config(tp);
  1867. break;
  1868. case RTL_GIGA_MAC_VER_11:
  1869. rtl8168bb_hw_phy_config(tp);
  1870. break;
  1871. case RTL_GIGA_MAC_VER_12:
  1872. rtl8168bef_hw_phy_config(tp);
  1873. break;
  1874. case RTL_GIGA_MAC_VER_17:
  1875. rtl8168bef_hw_phy_config(tp);
  1876. break;
  1877. case RTL_GIGA_MAC_VER_18:
  1878. rtl8168cp_1_hw_phy_config(tp);
  1879. break;
  1880. case RTL_GIGA_MAC_VER_19:
  1881. rtl8168c_1_hw_phy_config(tp);
  1882. break;
  1883. case RTL_GIGA_MAC_VER_20:
  1884. rtl8168c_2_hw_phy_config(tp);
  1885. break;
  1886. case RTL_GIGA_MAC_VER_21:
  1887. rtl8168c_3_hw_phy_config(tp);
  1888. break;
  1889. case RTL_GIGA_MAC_VER_22:
  1890. rtl8168c_4_hw_phy_config(tp);
  1891. break;
  1892. case RTL_GIGA_MAC_VER_23:
  1893. case RTL_GIGA_MAC_VER_24:
  1894. rtl8168cp_2_hw_phy_config(tp);
  1895. break;
  1896. case RTL_GIGA_MAC_VER_25:
  1897. rtl8168d_1_hw_phy_config(tp);
  1898. break;
  1899. case RTL_GIGA_MAC_VER_26:
  1900. rtl8168d_2_hw_phy_config(tp);
  1901. break;
  1902. case RTL_GIGA_MAC_VER_27:
  1903. rtl8168d_3_hw_phy_config(tp);
  1904. break;
  1905. default:
  1906. break;
  1907. }
  1908. }
  1909. static void rtl8169_phy_timer(unsigned long __opaque)
  1910. {
  1911. struct net_device *dev = (struct net_device *)__opaque;
  1912. struct rtl8169_private *tp = netdev_priv(dev);
  1913. struct timer_list *timer = &tp->timer;
  1914. void __iomem *ioaddr = tp->mmio_addr;
  1915. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  1916. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  1917. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1918. return;
  1919. spin_lock_irq(&tp->lock);
  1920. if (tp->phy_reset_pending(tp)) {
  1921. /*
  1922. * A busy loop could burn quite a few cycles on nowadays CPU.
  1923. * Let's delay the execution of the timer for a few ticks.
  1924. */
  1925. timeout = HZ/10;
  1926. goto out_mod_timer;
  1927. }
  1928. if (tp->link_ok(ioaddr))
  1929. goto out_unlock;
  1930. netif_warn(tp, link, dev, "PHY reset until link up\n");
  1931. tp->phy_reset_enable(tp);
  1932. out_mod_timer:
  1933. mod_timer(timer, jiffies + timeout);
  1934. out_unlock:
  1935. spin_unlock_irq(&tp->lock);
  1936. }
  1937. static inline void rtl8169_delete_timer(struct net_device *dev)
  1938. {
  1939. struct rtl8169_private *tp = netdev_priv(dev);
  1940. struct timer_list *timer = &tp->timer;
  1941. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1942. return;
  1943. del_timer_sync(timer);
  1944. }
  1945. static inline void rtl8169_request_timer(struct net_device *dev)
  1946. {
  1947. struct rtl8169_private *tp = netdev_priv(dev);
  1948. struct timer_list *timer = &tp->timer;
  1949. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1950. return;
  1951. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  1952. }
  1953. #ifdef CONFIG_NET_POLL_CONTROLLER
  1954. /*
  1955. * Polling 'interrupt' - used by things like netconsole to send skbs
  1956. * without having to re-enable interrupts. It's not called while
  1957. * the interrupt routine is executing.
  1958. */
  1959. static void rtl8169_netpoll(struct net_device *dev)
  1960. {
  1961. struct rtl8169_private *tp = netdev_priv(dev);
  1962. struct pci_dev *pdev = tp->pci_dev;
  1963. disable_irq(pdev->irq);
  1964. rtl8169_interrupt(pdev->irq, dev);
  1965. enable_irq(pdev->irq);
  1966. }
  1967. #endif
  1968. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  1969. void __iomem *ioaddr)
  1970. {
  1971. iounmap(ioaddr);
  1972. pci_release_regions(pdev);
  1973. pci_clear_mwi(pdev);
  1974. pci_disable_device(pdev);
  1975. free_netdev(dev);
  1976. }
  1977. static void rtl8169_phy_reset(struct net_device *dev,
  1978. struct rtl8169_private *tp)
  1979. {
  1980. unsigned int i;
  1981. tp->phy_reset_enable(tp);
  1982. for (i = 0; i < 100; i++) {
  1983. if (!tp->phy_reset_pending(tp))
  1984. return;
  1985. msleep(1);
  1986. }
  1987. netif_err(tp, link, dev, "PHY reset failed\n");
  1988. }
  1989. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  1990. {
  1991. void __iomem *ioaddr = tp->mmio_addr;
  1992. rtl_hw_phy_config(dev);
  1993. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  1994. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1995. RTL_W8(0x82, 0x01);
  1996. }
  1997. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  1998. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  1999. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  2000. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  2001. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2002. RTL_W8(0x82, 0x01);
  2003. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  2004. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  2005. }
  2006. rtl8169_phy_reset(dev, tp);
  2007. /*
  2008. * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
  2009. * only 8101. Don't panic.
  2010. */
  2011. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
  2012. if (RTL_R8(PHYstatus) & TBI_Enable)
  2013. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  2014. }
  2015. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  2016. {
  2017. void __iomem *ioaddr = tp->mmio_addr;
  2018. u32 high;
  2019. u32 low;
  2020. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  2021. high = addr[4] | (addr[5] << 8);
  2022. spin_lock_irq(&tp->lock);
  2023. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2024. RTL_W32(MAC4, high);
  2025. RTL_R32(MAC4);
  2026. RTL_W32(MAC0, low);
  2027. RTL_R32(MAC0);
  2028. RTL_W8(Cfg9346, Cfg9346_Lock);
  2029. spin_unlock_irq(&tp->lock);
  2030. }
  2031. static int rtl_set_mac_address(struct net_device *dev, void *p)
  2032. {
  2033. struct rtl8169_private *tp = netdev_priv(dev);
  2034. struct sockaddr *addr = p;
  2035. if (!is_valid_ether_addr(addr->sa_data))
  2036. return -EADDRNOTAVAIL;
  2037. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2038. rtl_rar_set(tp, dev->dev_addr);
  2039. return 0;
  2040. }
  2041. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2042. {
  2043. struct rtl8169_private *tp = netdev_priv(dev);
  2044. struct mii_ioctl_data *data = if_mii(ifr);
  2045. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  2046. }
  2047. static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2048. {
  2049. switch (cmd) {
  2050. case SIOCGMIIPHY:
  2051. data->phy_id = 32; /* Internal PHY */
  2052. return 0;
  2053. case SIOCGMIIREG:
  2054. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  2055. return 0;
  2056. case SIOCSMIIREG:
  2057. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  2058. return 0;
  2059. }
  2060. return -EOPNOTSUPP;
  2061. }
  2062. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2063. {
  2064. return -EOPNOTSUPP;
  2065. }
  2066. static const struct rtl_cfg_info {
  2067. void (*hw_start)(struct net_device *);
  2068. unsigned int region;
  2069. unsigned int align;
  2070. u16 intr_event;
  2071. u16 napi_event;
  2072. unsigned features;
  2073. u8 default_ver;
  2074. } rtl_cfg_infos [] = {
  2075. [RTL_CFG_0] = {
  2076. .hw_start = rtl_hw_start_8169,
  2077. .region = 1,
  2078. .align = 0,
  2079. .intr_event = SYSErr | LinkChg | RxOverflow |
  2080. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2081. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2082. .features = RTL_FEATURE_GMII,
  2083. .default_ver = RTL_GIGA_MAC_VER_01,
  2084. },
  2085. [RTL_CFG_1] = {
  2086. .hw_start = rtl_hw_start_8168,
  2087. .region = 2,
  2088. .align = 8,
  2089. .intr_event = SYSErr | LinkChg | RxOverflow |
  2090. TxErr | TxOK | RxOK | RxErr,
  2091. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  2092. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  2093. .default_ver = RTL_GIGA_MAC_VER_11,
  2094. },
  2095. [RTL_CFG_2] = {
  2096. .hw_start = rtl_hw_start_8101,
  2097. .region = 2,
  2098. .align = 8,
  2099. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  2100. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2101. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2102. .features = RTL_FEATURE_MSI,
  2103. .default_ver = RTL_GIGA_MAC_VER_13,
  2104. }
  2105. };
  2106. /* Cfg9346_Unlock assumed. */
  2107. static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
  2108. const struct rtl_cfg_info *cfg)
  2109. {
  2110. unsigned msi = 0;
  2111. u8 cfg2;
  2112. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  2113. if (cfg->features & RTL_FEATURE_MSI) {
  2114. if (pci_enable_msi(pdev)) {
  2115. dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
  2116. } else {
  2117. cfg2 |= MSIEnable;
  2118. msi = RTL_FEATURE_MSI;
  2119. }
  2120. }
  2121. RTL_W8(Config2, cfg2);
  2122. return msi;
  2123. }
  2124. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  2125. {
  2126. if (tp->features & RTL_FEATURE_MSI) {
  2127. pci_disable_msi(pdev);
  2128. tp->features &= ~RTL_FEATURE_MSI;
  2129. }
  2130. }
  2131. static const struct net_device_ops rtl8169_netdev_ops = {
  2132. .ndo_open = rtl8169_open,
  2133. .ndo_stop = rtl8169_close,
  2134. .ndo_get_stats = rtl8169_get_stats,
  2135. .ndo_start_xmit = rtl8169_start_xmit,
  2136. .ndo_tx_timeout = rtl8169_tx_timeout,
  2137. .ndo_validate_addr = eth_validate_addr,
  2138. .ndo_change_mtu = rtl8169_change_mtu,
  2139. .ndo_set_mac_address = rtl_set_mac_address,
  2140. .ndo_do_ioctl = rtl8169_ioctl,
  2141. .ndo_set_multicast_list = rtl_set_rx_mode,
  2142. #ifdef CONFIG_R8169_VLAN
  2143. .ndo_vlan_rx_register = rtl8169_vlan_rx_register,
  2144. #endif
  2145. #ifdef CONFIG_NET_POLL_CONTROLLER
  2146. .ndo_poll_controller = rtl8169_netpoll,
  2147. #endif
  2148. };
  2149. static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
  2150. {
  2151. struct mdio_ops *ops = &tp->mdio_ops;
  2152. switch (tp->mac_version) {
  2153. case RTL_GIGA_MAC_VER_27:
  2154. ops->write = r8168dp_1_mdio_write;
  2155. ops->read = r8168dp_1_mdio_read;
  2156. break;
  2157. default:
  2158. ops->write = r8169_mdio_write;
  2159. ops->read = r8169_mdio_read;
  2160. break;
  2161. }
  2162. }
  2163. static int __devinit
  2164. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2165. {
  2166. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  2167. const unsigned int region = cfg->region;
  2168. struct rtl8169_private *tp;
  2169. struct mii_if_info *mii;
  2170. struct net_device *dev;
  2171. void __iomem *ioaddr;
  2172. unsigned int i;
  2173. int rc;
  2174. if (netif_msg_drv(&debug)) {
  2175. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  2176. MODULENAME, RTL8169_VERSION);
  2177. }
  2178. dev = alloc_etherdev(sizeof (*tp));
  2179. if (!dev) {
  2180. if (netif_msg_drv(&debug))
  2181. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  2182. rc = -ENOMEM;
  2183. goto out;
  2184. }
  2185. SET_NETDEV_DEV(dev, &pdev->dev);
  2186. dev->netdev_ops = &rtl8169_netdev_ops;
  2187. tp = netdev_priv(dev);
  2188. tp->dev = dev;
  2189. tp->pci_dev = pdev;
  2190. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  2191. mii = &tp->mii;
  2192. mii->dev = dev;
  2193. mii->mdio_read = rtl_mdio_read;
  2194. mii->mdio_write = rtl_mdio_write;
  2195. mii->phy_id_mask = 0x1f;
  2196. mii->reg_num_mask = 0x1f;
  2197. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  2198. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  2199. rc = pci_enable_device(pdev);
  2200. if (rc < 0) {
  2201. netif_err(tp, probe, dev, "enable failure\n");
  2202. goto err_out_free_dev_1;
  2203. }
  2204. if (pci_set_mwi(pdev) < 0)
  2205. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  2206. /* make sure PCI base addr 1 is MMIO */
  2207. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  2208. netif_err(tp, probe, dev,
  2209. "region #%d not an MMIO resource, aborting\n",
  2210. region);
  2211. rc = -ENODEV;
  2212. goto err_out_mwi_2;
  2213. }
  2214. /* check for weird/broken PCI region reporting */
  2215. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  2216. netif_err(tp, probe, dev,
  2217. "Invalid PCI region size(s), aborting\n");
  2218. rc = -ENODEV;
  2219. goto err_out_mwi_2;
  2220. }
  2221. rc = pci_request_regions(pdev, MODULENAME);
  2222. if (rc < 0) {
  2223. netif_err(tp, probe, dev, "could not request regions\n");
  2224. goto err_out_mwi_2;
  2225. }
  2226. tp->cp_cmd = PCIMulRW | RxChkSum;
  2227. if ((sizeof(dma_addr_t) > 4) &&
  2228. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  2229. tp->cp_cmd |= PCIDAC;
  2230. dev->features |= NETIF_F_HIGHDMA;
  2231. } else {
  2232. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2233. if (rc < 0) {
  2234. netif_err(tp, probe, dev, "DMA configuration failed\n");
  2235. goto err_out_free_res_3;
  2236. }
  2237. }
  2238. /* ioremap MMIO region */
  2239. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  2240. if (!ioaddr) {
  2241. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  2242. rc = -EIO;
  2243. goto err_out_free_res_3;
  2244. }
  2245. tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  2246. if (!tp->pcie_cap)
  2247. netif_info(tp, probe, dev, "no PCI Express capability\n");
  2248. RTL_W16(IntrMask, 0x0000);
  2249. /* Soft reset the chip. */
  2250. RTL_W8(ChipCmd, CmdReset);
  2251. /* Check that the chip has finished the reset. */
  2252. for (i = 0; i < 100; i++) {
  2253. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2254. break;
  2255. msleep_interruptible(1);
  2256. }
  2257. RTL_W16(IntrStatus, 0xffff);
  2258. pci_set_master(pdev);
  2259. /* Identify chip attached to board */
  2260. rtl8169_get_mac_version(tp, ioaddr);
  2261. rtl_init_mdio_ops(tp);
  2262. /* Use appropriate default if unknown */
  2263. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  2264. netif_notice(tp, probe, dev,
  2265. "unknown MAC, using family default\n");
  2266. tp->mac_version = cfg->default_ver;
  2267. }
  2268. rtl8169_print_mac_version(tp);
  2269. for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
  2270. if (tp->mac_version == rtl_chip_info[i].mac_version)
  2271. break;
  2272. }
  2273. if (i == ARRAY_SIZE(rtl_chip_info)) {
  2274. dev_err(&pdev->dev,
  2275. "driver bug, MAC version not found in rtl_chip_info\n");
  2276. goto err_out_msi_4;
  2277. }
  2278. tp->chipset = i;
  2279. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2280. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  2281. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  2282. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  2283. tp->features |= RTL_FEATURE_WOL;
  2284. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  2285. tp->features |= RTL_FEATURE_WOL;
  2286. tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
  2287. RTL_W8(Cfg9346, Cfg9346_Lock);
  2288. if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
  2289. (RTL_R8(PHYstatus) & TBI_Enable)) {
  2290. tp->set_speed = rtl8169_set_speed_tbi;
  2291. tp->get_settings = rtl8169_gset_tbi;
  2292. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  2293. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  2294. tp->link_ok = rtl8169_tbi_link_ok;
  2295. tp->do_ioctl = rtl_tbi_ioctl;
  2296. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  2297. } else {
  2298. tp->set_speed = rtl8169_set_speed_xmii;
  2299. tp->get_settings = rtl8169_gset_xmii;
  2300. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  2301. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  2302. tp->link_ok = rtl8169_xmii_link_ok;
  2303. tp->do_ioctl = rtl_xmii_ioctl;
  2304. }
  2305. spin_lock_init(&tp->lock);
  2306. tp->mmio_addr = ioaddr;
  2307. /* Get MAC address */
  2308. for (i = 0; i < MAC_ADDR_LEN; i++)
  2309. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  2310. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  2311. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  2312. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  2313. dev->irq = pdev->irq;
  2314. dev->base_addr = (unsigned long) ioaddr;
  2315. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  2316. #ifdef CONFIG_R8169_VLAN
  2317. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  2318. #endif
  2319. dev->features |= NETIF_F_GRO;
  2320. tp->intr_mask = 0xffff;
  2321. tp->hw_start = cfg->hw_start;
  2322. tp->intr_event = cfg->intr_event;
  2323. tp->napi_event = cfg->napi_event;
  2324. init_timer(&tp->timer);
  2325. tp->timer.data = (unsigned long) dev;
  2326. tp->timer.function = rtl8169_phy_timer;
  2327. rc = register_netdev(dev);
  2328. if (rc < 0)
  2329. goto err_out_msi_4;
  2330. pci_set_drvdata(pdev, dev);
  2331. netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
  2332. rtl_chip_info[tp->chipset].name,
  2333. dev->base_addr, dev->dev_addr,
  2334. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
  2335. rtl8169_init_phy(dev, tp);
  2336. /*
  2337. * Pretend we are using VLANs; This bypasses a nasty bug where
  2338. * Interrupts stop flowing on high load on 8110SCd controllers.
  2339. */
  2340. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  2341. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | RxVlan);
  2342. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  2343. if (pci_dev_run_wake(pdev))
  2344. pm_runtime_put_noidle(&pdev->dev);
  2345. out:
  2346. return rc;
  2347. err_out_msi_4:
  2348. rtl_disable_msi(pdev, tp);
  2349. iounmap(ioaddr);
  2350. err_out_free_res_3:
  2351. pci_release_regions(pdev);
  2352. err_out_mwi_2:
  2353. pci_clear_mwi(pdev);
  2354. pci_disable_device(pdev);
  2355. err_out_free_dev_1:
  2356. free_netdev(dev);
  2357. goto out;
  2358. }
  2359. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  2360. {
  2361. struct net_device *dev = pci_get_drvdata(pdev);
  2362. struct rtl8169_private *tp = netdev_priv(dev);
  2363. cancel_delayed_work_sync(&tp->task);
  2364. unregister_netdev(dev);
  2365. if (pci_dev_run_wake(pdev))
  2366. pm_runtime_get_noresume(&pdev->dev);
  2367. /* restore original MAC address */
  2368. rtl_rar_set(tp, dev->perm_addr);
  2369. rtl_disable_msi(pdev, tp);
  2370. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  2371. pci_set_drvdata(pdev, NULL);
  2372. }
  2373. static int rtl8169_open(struct net_device *dev)
  2374. {
  2375. struct rtl8169_private *tp = netdev_priv(dev);
  2376. struct pci_dev *pdev = tp->pci_dev;
  2377. int retval = -ENOMEM;
  2378. pm_runtime_get_sync(&pdev->dev);
  2379. /*
  2380. * Rx and Tx desscriptors needs 256 bytes alignment.
  2381. * dma_alloc_coherent provides more.
  2382. */
  2383. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  2384. &tp->TxPhyAddr, GFP_KERNEL);
  2385. if (!tp->TxDescArray)
  2386. goto err_pm_runtime_put;
  2387. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  2388. &tp->RxPhyAddr, GFP_KERNEL);
  2389. if (!tp->RxDescArray)
  2390. goto err_free_tx_0;
  2391. retval = rtl8169_init_ring(dev);
  2392. if (retval < 0)
  2393. goto err_free_rx_1;
  2394. INIT_DELAYED_WORK(&tp->task, NULL);
  2395. smp_mb();
  2396. retval = request_irq(dev->irq, rtl8169_interrupt,
  2397. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  2398. dev->name, dev);
  2399. if (retval < 0)
  2400. goto err_release_ring_2;
  2401. napi_enable(&tp->napi);
  2402. rtl_hw_start(dev);
  2403. rtl8169_request_timer(dev);
  2404. tp->saved_wolopts = 0;
  2405. pm_runtime_put_noidle(&pdev->dev);
  2406. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  2407. out:
  2408. return retval;
  2409. err_release_ring_2:
  2410. rtl8169_rx_clear(tp);
  2411. err_free_rx_1:
  2412. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  2413. tp->RxPhyAddr);
  2414. tp->RxDescArray = NULL;
  2415. err_free_tx_0:
  2416. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  2417. tp->TxPhyAddr);
  2418. tp->TxDescArray = NULL;
  2419. err_pm_runtime_put:
  2420. pm_runtime_put_noidle(&pdev->dev);
  2421. goto out;
  2422. }
  2423. static void rtl8169_hw_reset(void __iomem *ioaddr)
  2424. {
  2425. /* Disable interrupts */
  2426. rtl8169_irq_mask_and_ack(ioaddr);
  2427. /* Reset the chipset */
  2428. RTL_W8(ChipCmd, CmdReset);
  2429. /* PCI commit */
  2430. RTL_R8(ChipCmd);
  2431. }
  2432. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  2433. {
  2434. void __iomem *ioaddr = tp->mmio_addr;
  2435. u32 cfg = rtl8169_rx_config;
  2436. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  2437. RTL_W32(RxConfig, cfg);
  2438. /* Set DMA burst size and Interframe Gap Time */
  2439. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  2440. (InterFrameGap << TxInterFrameGapShift));
  2441. }
  2442. static void rtl_hw_start(struct net_device *dev)
  2443. {
  2444. struct rtl8169_private *tp = netdev_priv(dev);
  2445. void __iomem *ioaddr = tp->mmio_addr;
  2446. unsigned int i;
  2447. /* Soft reset the chip. */
  2448. RTL_W8(ChipCmd, CmdReset);
  2449. /* Check that the chip has finished the reset. */
  2450. for (i = 0; i < 100; i++) {
  2451. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2452. break;
  2453. msleep_interruptible(1);
  2454. }
  2455. tp->hw_start(dev);
  2456. netif_start_queue(dev);
  2457. }
  2458. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  2459. void __iomem *ioaddr)
  2460. {
  2461. /*
  2462. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  2463. * register to be written before TxDescAddrLow to work.
  2464. * Switching from MMIO to I/O access fixes the issue as well.
  2465. */
  2466. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  2467. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  2468. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  2469. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  2470. }
  2471. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  2472. {
  2473. u16 cmd;
  2474. cmd = RTL_R16(CPlusCmd);
  2475. RTL_W16(CPlusCmd, cmd);
  2476. return cmd;
  2477. }
  2478. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  2479. {
  2480. /* Low hurts. Let's disable the filtering. */
  2481. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  2482. }
  2483. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  2484. {
  2485. static const struct {
  2486. u32 mac_version;
  2487. u32 clk;
  2488. u32 val;
  2489. } cfg2_info [] = {
  2490. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  2491. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  2492. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  2493. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  2494. }, *p = cfg2_info;
  2495. unsigned int i;
  2496. u32 clk;
  2497. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  2498. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  2499. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  2500. RTL_W32(0x7c, p->val);
  2501. break;
  2502. }
  2503. }
  2504. }
  2505. static void rtl_hw_start_8169(struct net_device *dev)
  2506. {
  2507. struct rtl8169_private *tp = netdev_priv(dev);
  2508. void __iomem *ioaddr = tp->mmio_addr;
  2509. struct pci_dev *pdev = tp->pci_dev;
  2510. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  2511. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  2512. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  2513. }
  2514. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2515. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2516. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2517. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2518. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2519. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2520. RTL_W8(EarlyTxThres, NoEarlyTx);
  2521. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2522. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2523. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2524. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2525. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2526. rtl_set_rx_tx_config_registers(tp);
  2527. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2528. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2529. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  2530. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  2531. "Bit-3 and bit-14 MUST be 1\n");
  2532. tp->cp_cmd |= (1 << 14);
  2533. }
  2534. RTL_W16(CPlusCmd, tp->cp_cmd);
  2535. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  2536. /*
  2537. * Undocumented corner. Supposedly:
  2538. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  2539. */
  2540. RTL_W16(IntrMitigate, 0x0000);
  2541. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2542. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  2543. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  2544. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  2545. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  2546. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2547. rtl_set_rx_tx_config_registers(tp);
  2548. }
  2549. RTL_W8(Cfg9346, Cfg9346_Lock);
  2550. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  2551. RTL_R8(IntrMask);
  2552. RTL_W32(RxMissed, 0);
  2553. rtl_set_rx_mode(dev);
  2554. /* no early-rx interrupts */
  2555. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2556. /* Enable all known interrupts by setting the interrupt mask. */
  2557. RTL_W16(IntrMask, tp->intr_event);
  2558. }
  2559. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  2560. {
  2561. struct net_device *dev = pci_get_drvdata(pdev);
  2562. struct rtl8169_private *tp = netdev_priv(dev);
  2563. int cap = tp->pcie_cap;
  2564. if (cap) {
  2565. u16 ctl;
  2566. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  2567. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  2568. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  2569. }
  2570. }
  2571. static void rtl_csi_access_enable(void __iomem *ioaddr)
  2572. {
  2573. u32 csi;
  2574. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  2575. rtl_csi_write(ioaddr, 0x070c, csi | 0x27000000);
  2576. }
  2577. struct ephy_info {
  2578. unsigned int offset;
  2579. u16 mask;
  2580. u16 bits;
  2581. };
  2582. static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
  2583. {
  2584. u16 w;
  2585. while (len-- > 0) {
  2586. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  2587. rtl_ephy_write(ioaddr, e->offset, w);
  2588. e++;
  2589. }
  2590. }
  2591. static void rtl_disable_clock_request(struct pci_dev *pdev)
  2592. {
  2593. struct net_device *dev = pci_get_drvdata(pdev);
  2594. struct rtl8169_private *tp = netdev_priv(dev);
  2595. int cap = tp->pcie_cap;
  2596. if (cap) {
  2597. u16 ctl;
  2598. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  2599. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  2600. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  2601. }
  2602. }
  2603. #define R8168_CPCMD_QUIRK_MASK (\
  2604. EnableBist | \
  2605. Mac_dbgo_oe | \
  2606. Force_half_dup | \
  2607. Force_rxflow_en | \
  2608. Force_txflow_en | \
  2609. Cxpl_dbg_sel | \
  2610. ASF | \
  2611. PktCntrDisable | \
  2612. Mac_dbgo_sel)
  2613. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  2614. {
  2615. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2616. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2617. rtl_tx_performance_tweak(pdev,
  2618. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  2619. }
  2620. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  2621. {
  2622. rtl_hw_start_8168bb(ioaddr, pdev);
  2623. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2624. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  2625. }
  2626. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  2627. {
  2628. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  2629. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2630. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2631. rtl_disable_clock_request(pdev);
  2632. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2633. }
  2634. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2635. {
  2636. static const struct ephy_info e_info_8168cp[] = {
  2637. { 0x01, 0, 0x0001 },
  2638. { 0x02, 0x0800, 0x1000 },
  2639. { 0x03, 0, 0x0042 },
  2640. { 0x06, 0x0080, 0x0000 },
  2641. { 0x07, 0, 0x2000 }
  2642. };
  2643. rtl_csi_access_enable(ioaddr);
  2644. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  2645. __rtl_hw_start_8168cp(ioaddr, pdev);
  2646. }
  2647. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2648. {
  2649. rtl_csi_access_enable(ioaddr);
  2650. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2651. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2652. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2653. }
  2654. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2655. {
  2656. rtl_csi_access_enable(ioaddr);
  2657. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2658. /* Magic. */
  2659. RTL_W8(DBG_REG, 0x20);
  2660. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2661. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2662. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2663. }
  2664. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2665. {
  2666. static const struct ephy_info e_info_8168c_1[] = {
  2667. { 0x02, 0x0800, 0x1000 },
  2668. { 0x03, 0, 0x0002 },
  2669. { 0x06, 0x0080, 0x0000 }
  2670. };
  2671. rtl_csi_access_enable(ioaddr);
  2672. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  2673. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  2674. __rtl_hw_start_8168cp(ioaddr, pdev);
  2675. }
  2676. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2677. {
  2678. static const struct ephy_info e_info_8168c_2[] = {
  2679. { 0x01, 0, 0x0001 },
  2680. { 0x03, 0x0400, 0x0220 }
  2681. };
  2682. rtl_csi_access_enable(ioaddr);
  2683. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  2684. __rtl_hw_start_8168cp(ioaddr, pdev);
  2685. }
  2686. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2687. {
  2688. rtl_hw_start_8168c_2(ioaddr, pdev);
  2689. }
  2690. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  2691. {
  2692. rtl_csi_access_enable(ioaddr);
  2693. __rtl_hw_start_8168cp(ioaddr, pdev);
  2694. }
  2695. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  2696. {
  2697. rtl_csi_access_enable(ioaddr);
  2698. rtl_disable_clock_request(pdev);
  2699. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2700. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2701. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2702. }
  2703. static void rtl_hw_start_8168(struct net_device *dev)
  2704. {
  2705. struct rtl8169_private *tp = netdev_priv(dev);
  2706. void __iomem *ioaddr = tp->mmio_addr;
  2707. struct pci_dev *pdev = tp->pci_dev;
  2708. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2709. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2710. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2711. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  2712. RTL_W16(CPlusCmd, tp->cp_cmd);
  2713. RTL_W16(IntrMitigate, 0x5151);
  2714. /* Work around for RxFIFO overflow. */
  2715. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  2716. tp->intr_event |= RxFIFOOver | PCSTimeout;
  2717. tp->intr_event &= ~RxOverflow;
  2718. }
  2719. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2720. rtl_set_rx_mode(dev);
  2721. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  2722. (InterFrameGap << TxInterFrameGapShift));
  2723. RTL_R8(IntrMask);
  2724. switch (tp->mac_version) {
  2725. case RTL_GIGA_MAC_VER_11:
  2726. rtl_hw_start_8168bb(ioaddr, pdev);
  2727. break;
  2728. case RTL_GIGA_MAC_VER_12:
  2729. case RTL_GIGA_MAC_VER_17:
  2730. rtl_hw_start_8168bef(ioaddr, pdev);
  2731. break;
  2732. case RTL_GIGA_MAC_VER_18:
  2733. rtl_hw_start_8168cp_1(ioaddr, pdev);
  2734. break;
  2735. case RTL_GIGA_MAC_VER_19:
  2736. rtl_hw_start_8168c_1(ioaddr, pdev);
  2737. break;
  2738. case RTL_GIGA_MAC_VER_20:
  2739. rtl_hw_start_8168c_2(ioaddr, pdev);
  2740. break;
  2741. case RTL_GIGA_MAC_VER_21:
  2742. rtl_hw_start_8168c_3(ioaddr, pdev);
  2743. break;
  2744. case RTL_GIGA_MAC_VER_22:
  2745. rtl_hw_start_8168c_4(ioaddr, pdev);
  2746. break;
  2747. case RTL_GIGA_MAC_VER_23:
  2748. rtl_hw_start_8168cp_2(ioaddr, pdev);
  2749. break;
  2750. case RTL_GIGA_MAC_VER_24:
  2751. rtl_hw_start_8168cp_3(ioaddr, pdev);
  2752. break;
  2753. case RTL_GIGA_MAC_VER_25:
  2754. case RTL_GIGA_MAC_VER_26:
  2755. case RTL_GIGA_MAC_VER_27:
  2756. rtl_hw_start_8168d(ioaddr, pdev);
  2757. break;
  2758. default:
  2759. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  2760. dev->name, tp->mac_version);
  2761. break;
  2762. }
  2763. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2764. RTL_W8(Cfg9346, Cfg9346_Lock);
  2765. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2766. RTL_W16(IntrMask, tp->intr_event);
  2767. }
  2768. #define R810X_CPCMD_QUIRK_MASK (\
  2769. EnableBist | \
  2770. Mac_dbgo_oe | \
  2771. Force_half_dup | \
  2772. Force_rxflow_en | \
  2773. Force_txflow_en | \
  2774. Cxpl_dbg_sel | \
  2775. ASF | \
  2776. PktCntrDisable | \
  2777. PCIDAC | \
  2778. PCIMulRW)
  2779. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2780. {
  2781. static const struct ephy_info e_info_8102e_1[] = {
  2782. { 0x01, 0, 0x6e65 },
  2783. { 0x02, 0, 0x091f },
  2784. { 0x03, 0, 0xc2f9 },
  2785. { 0x06, 0, 0xafb5 },
  2786. { 0x07, 0, 0x0e00 },
  2787. { 0x19, 0, 0xec80 },
  2788. { 0x01, 0, 0x2e65 },
  2789. { 0x01, 0, 0x6e65 }
  2790. };
  2791. u8 cfg1;
  2792. rtl_csi_access_enable(ioaddr);
  2793. RTL_W8(DBG_REG, FIX_NAK_1);
  2794. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2795. RTL_W8(Config1,
  2796. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  2797. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2798. cfg1 = RTL_R8(Config1);
  2799. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  2800. RTL_W8(Config1, cfg1 & ~LEDS0);
  2801. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  2802. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  2803. }
  2804. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2805. {
  2806. rtl_csi_access_enable(ioaddr);
  2807. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2808. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  2809. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2810. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  2811. }
  2812. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2813. {
  2814. rtl_hw_start_8102e_2(ioaddr, pdev);
  2815. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  2816. }
  2817. static void rtl_hw_start_8101(struct net_device *dev)
  2818. {
  2819. struct rtl8169_private *tp = netdev_priv(dev);
  2820. void __iomem *ioaddr = tp->mmio_addr;
  2821. struct pci_dev *pdev = tp->pci_dev;
  2822. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  2823. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  2824. int cap = tp->pcie_cap;
  2825. if (cap) {
  2826. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  2827. PCI_EXP_DEVCTL_NOSNOOP_EN);
  2828. }
  2829. }
  2830. switch (tp->mac_version) {
  2831. case RTL_GIGA_MAC_VER_07:
  2832. rtl_hw_start_8102e_1(ioaddr, pdev);
  2833. break;
  2834. case RTL_GIGA_MAC_VER_08:
  2835. rtl_hw_start_8102e_3(ioaddr, pdev);
  2836. break;
  2837. case RTL_GIGA_MAC_VER_09:
  2838. rtl_hw_start_8102e_2(ioaddr, pdev);
  2839. break;
  2840. }
  2841. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2842. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2843. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2844. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2845. RTL_W16(CPlusCmd, tp->cp_cmd);
  2846. RTL_W16(IntrMitigate, 0x0000);
  2847. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2848. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2849. rtl_set_rx_tx_config_registers(tp);
  2850. RTL_W8(Cfg9346, Cfg9346_Lock);
  2851. RTL_R8(IntrMask);
  2852. rtl_set_rx_mode(dev);
  2853. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2854. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  2855. RTL_W16(IntrMask, tp->intr_event);
  2856. }
  2857. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  2858. {
  2859. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  2860. return -EINVAL;
  2861. dev->mtu = new_mtu;
  2862. return 0;
  2863. }
  2864. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  2865. {
  2866. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  2867. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  2868. }
  2869. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  2870. void **data_buff, struct RxDesc *desc)
  2871. {
  2872. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  2873. DMA_FROM_DEVICE);
  2874. kfree(*data_buff);
  2875. *data_buff = NULL;
  2876. rtl8169_make_unusable_by_asic(desc);
  2877. }
  2878. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  2879. {
  2880. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  2881. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  2882. }
  2883. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  2884. u32 rx_buf_sz)
  2885. {
  2886. desc->addr = cpu_to_le64(mapping);
  2887. wmb();
  2888. rtl8169_mark_to_asic(desc, rx_buf_sz);
  2889. }
  2890. static inline void *rtl8169_align(void *data)
  2891. {
  2892. return (void *)ALIGN((long)data, 16);
  2893. }
  2894. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  2895. struct RxDesc *desc)
  2896. {
  2897. void *data;
  2898. dma_addr_t mapping;
  2899. struct device *d = &tp->pci_dev->dev;
  2900. struct net_device *dev = tp->dev;
  2901. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  2902. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  2903. if (!data)
  2904. return NULL;
  2905. if (rtl8169_align(data) != data) {
  2906. kfree(data);
  2907. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  2908. if (!data)
  2909. return NULL;
  2910. }
  2911. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  2912. DMA_FROM_DEVICE);
  2913. if (unlikely(dma_mapping_error(d, mapping))) {
  2914. if (net_ratelimit())
  2915. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  2916. goto err_out;
  2917. }
  2918. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  2919. return data;
  2920. err_out:
  2921. kfree(data);
  2922. return NULL;
  2923. }
  2924. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  2925. {
  2926. unsigned int i;
  2927. for (i = 0; i < NUM_RX_DESC; i++) {
  2928. if (tp->Rx_databuff[i]) {
  2929. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  2930. tp->RxDescArray + i);
  2931. }
  2932. }
  2933. }
  2934. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  2935. {
  2936. desc->opts1 |= cpu_to_le32(RingEnd);
  2937. }
  2938. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  2939. {
  2940. unsigned int i;
  2941. for (i = 0; i < NUM_RX_DESC; i++) {
  2942. void *data;
  2943. if (tp->Rx_databuff[i])
  2944. continue;
  2945. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  2946. if (!data) {
  2947. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  2948. goto err_out;
  2949. }
  2950. tp->Rx_databuff[i] = data;
  2951. }
  2952. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  2953. return 0;
  2954. err_out:
  2955. rtl8169_rx_clear(tp);
  2956. return -ENOMEM;
  2957. }
  2958. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  2959. {
  2960. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  2961. }
  2962. static int rtl8169_init_ring(struct net_device *dev)
  2963. {
  2964. struct rtl8169_private *tp = netdev_priv(dev);
  2965. rtl8169_init_ring_indexes(tp);
  2966. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  2967. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  2968. return rtl8169_rx_fill(tp);
  2969. }
  2970. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  2971. struct TxDesc *desc)
  2972. {
  2973. unsigned int len = tx_skb->len;
  2974. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  2975. desc->opts1 = 0x00;
  2976. desc->opts2 = 0x00;
  2977. desc->addr = 0x00;
  2978. tx_skb->len = 0;
  2979. }
  2980. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  2981. unsigned int n)
  2982. {
  2983. unsigned int i;
  2984. for (i = 0; i < n; i++) {
  2985. unsigned int entry = (start + i) % NUM_TX_DESC;
  2986. struct ring_info *tx_skb = tp->tx_skb + entry;
  2987. unsigned int len = tx_skb->len;
  2988. if (len) {
  2989. struct sk_buff *skb = tx_skb->skb;
  2990. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  2991. tp->TxDescArray + entry);
  2992. if (skb) {
  2993. tp->dev->stats.tx_dropped++;
  2994. dev_kfree_skb(skb);
  2995. tx_skb->skb = NULL;
  2996. }
  2997. }
  2998. }
  2999. }
  3000. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  3001. {
  3002. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  3003. tp->cur_tx = tp->dirty_tx = 0;
  3004. }
  3005. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  3006. {
  3007. struct rtl8169_private *tp = netdev_priv(dev);
  3008. PREPARE_DELAYED_WORK(&tp->task, task);
  3009. schedule_delayed_work(&tp->task, 4);
  3010. }
  3011. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  3012. {
  3013. struct rtl8169_private *tp = netdev_priv(dev);
  3014. void __iomem *ioaddr = tp->mmio_addr;
  3015. synchronize_irq(dev->irq);
  3016. /* Wait for any pending NAPI task to complete */
  3017. napi_disable(&tp->napi);
  3018. rtl8169_irq_mask_and_ack(ioaddr);
  3019. tp->intr_mask = 0xffff;
  3020. RTL_W16(IntrMask, tp->intr_event);
  3021. napi_enable(&tp->napi);
  3022. }
  3023. static void rtl8169_reinit_task(struct work_struct *work)
  3024. {
  3025. struct rtl8169_private *tp =
  3026. container_of(work, struct rtl8169_private, task.work);
  3027. struct net_device *dev = tp->dev;
  3028. int ret;
  3029. rtnl_lock();
  3030. if (!netif_running(dev))
  3031. goto out_unlock;
  3032. rtl8169_wait_for_quiescence(dev);
  3033. rtl8169_close(dev);
  3034. ret = rtl8169_open(dev);
  3035. if (unlikely(ret < 0)) {
  3036. if (net_ratelimit())
  3037. netif_err(tp, drv, dev,
  3038. "reinit failure (status = %d). Rescheduling\n",
  3039. ret);
  3040. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  3041. }
  3042. out_unlock:
  3043. rtnl_unlock();
  3044. }
  3045. static void rtl8169_reset_task(struct work_struct *work)
  3046. {
  3047. struct rtl8169_private *tp =
  3048. container_of(work, struct rtl8169_private, task.work);
  3049. struct net_device *dev = tp->dev;
  3050. rtnl_lock();
  3051. if (!netif_running(dev))
  3052. goto out_unlock;
  3053. rtl8169_wait_for_quiescence(dev);
  3054. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
  3055. rtl8169_tx_clear(tp);
  3056. if (tp->dirty_rx == tp->cur_rx) {
  3057. rtl8169_init_ring_indexes(tp);
  3058. rtl_hw_start(dev);
  3059. netif_wake_queue(dev);
  3060. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  3061. } else {
  3062. if (net_ratelimit())
  3063. netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
  3064. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3065. }
  3066. out_unlock:
  3067. rtnl_unlock();
  3068. }
  3069. static void rtl8169_tx_timeout(struct net_device *dev)
  3070. {
  3071. struct rtl8169_private *tp = netdev_priv(dev);
  3072. rtl8169_hw_reset(tp->mmio_addr);
  3073. /* Let's wait a bit while any (async) irq lands on */
  3074. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3075. }
  3076. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  3077. u32 opts1)
  3078. {
  3079. struct skb_shared_info *info = skb_shinfo(skb);
  3080. unsigned int cur_frag, entry;
  3081. struct TxDesc * uninitialized_var(txd);
  3082. struct device *d = &tp->pci_dev->dev;
  3083. entry = tp->cur_tx;
  3084. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  3085. skb_frag_t *frag = info->frags + cur_frag;
  3086. dma_addr_t mapping;
  3087. u32 status, len;
  3088. void *addr;
  3089. entry = (entry + 1) % NUM_TX_DESC;
  3090. txd = tp->TxDescArray + entry;
  3091. len = frag->size;
  3092. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  3093. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  3094. if (unlikely(dma_mapping_error(d, mapping))) {
  3095. if (net_ratelimit())
  3096. netif_err(tp, drv, tp->dev,
  3097. "Failed to map TX fragments DMA!\n");
  3098. goto err_out;
  3099. }
  3100. /* anti gcc 2.95.3 bugware (sic) */
  3101. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  3102. txd->opts1 = cpu_to_le32(status);
  3103. txd->addr = cpu_to_le64(mapping);
  3104. tp->tx_skb[entry].len = len;
  3105. }
  3106. if (cur_frag) {
  3107. tp->tx_skb[entry].skb = skb;
  3108. txd->opts1 |= cpu_to_le32(LastFrag);
  3109. }
  3110. return cur_frag;
  3111. err_out:
  3112. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  3113. return -EIO;
  3114. }
  3115. static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
  3116. {
  3117. if (dev->features & NETIF_F_TSO) {
  3118. u32 mss = skb_shinfo(skb)->gso_size;
  3119. if (mss)
  3120. return LargeSend | ((mss & MSSMask) << MSSShift);
  3121. }
  3122. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3123. const struct iphdr *ip = ip_hdr(skb);
  3124. if (ip->protocol == IPPROTO_TCP)
  3125. return IPCS | TCPCS;
  3126. else if (ip->protocol == IPPROTO_UDP)
  3127. return IPCS | UDPCS;
  3128. WARN_ON(1); /* we need a WARN() */
  3129. }
  3130. return 0;
  3131. }
  3132. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  3133. struct net_device *dev)
  3134. {
  3135. struct rtl8169_private *tp = netdev_priv(dev);
  3136. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  3137. struct TxDesc *txd = tp->TxDescArray + entry;
  3138. void __iomem *ioaddr = tp->mmio_addr;
  3139. struct device *d = &tp->pci_dev->dev;
  3140. dma_addr_t mapping;
  3141. u32 status, len;
  3142. u32 opts1;
  3143. int frags;
  3144. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  3145. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  3146. goto err_stop_0;
  3147. }
  3148. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  3149. goto err_stop_0;
  3150. len = skb_headlen(skb);
  3151. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  3152. if (unlikely(dma_mapping_error(d, mapping))) {
  3153. if (net_ratelimit())
  3154. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  3155. goto err_dma_0;
  3156. }
  3157. tp->tx_skb[entry].len = len;
  3158. txd->addr = cpu_to_le64(mapping);
  3159. txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  3160. opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
  3161. frags = rtl8169_xmit_frags(tp, skb, opts1);
  3162. if (frags < 0)
  3163. goto err_dma_1;
  3164. else if (frags)
  3165. opts1 |= FirstFrag;
  3166. else {
  3167. opts1 |= FirstFrag | LastFrag;
  3168. tp->tx_skb[entry].skb = skb;
  3169. }
  3170. wmb();
  3171. /* anti gcc 2.95.3 bugware (sic) */
  3172. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  3173. txd->opts1 = cpu_to_le32(status);
  3174. tp->cur_tx += frags + 1;
  3175. wmb();
  3176. RTL_W8(TxPoll, NPQ); /* set polling bit */
  3177. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  3178. netif_stop_queue(dev);
  3179. smp_rmb();
  3180. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  3181. netif_wake_queue(dev);
  3182. }
  3183. return NETDEV_TX_OK;
  3184. err_dma_1:
  3185. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  3186. err_dma_0:
  3187. dev_kfree_skb(skb);
  3188. dev->stats.tx_dropped++;
  3189. return NETDEV_TX_OK;
  3190. err_stop_0:
  3191. netif_stop_queue(dev);
  3192. dev->stats.tx_dropped++;
  3193. return NETDEV_TX_BUSY;
  3194. }
  3195. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  3196. {
  3197. struct rtl8169_private *tp = netdev_priv(dev);
  3198. struct pci_dev *pdev = tp->pci_dev;
  3199. void __iomem *ioaddr = tp->mmio_addr;
  3200. u16 pci_status, pci_cmd;
  3201. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  3202. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  3203. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  3204. pci_cmd, pci_status);
  3205. /*
  3206. * The recovery sequence below admits a very elaborated explanation:
  3207. * - it seems to work;
  3208. * - I did not see what else could be done;
  3209. * - it makes iop3xx happy.
  3210. *
  3211. * Feel free to adjust to your needs.
  3212. */
  3213. if (pdev->broken_parity_status)
  3214. pci_cmd &= ~PCI_COMMAND_PARITY;
  3215. else
  3216. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  3217. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  3218. pci_write_config_word(pdev, PCI_STATUS,
  3219. pci_status & (PCI_STATUS_DETECTED_PARITY |
  3220. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  3221. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  3222. /* The infamous DAC f*ckup only happens at boot time */
  3223. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  3224. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  3225. tp->cp_cmd &= ~PCIDAC;
  3226. RTL_W16(CPlusCmd, tp->cp_cmd);
  3227. dev->features &= ~NETIF_F_HIGHDMA;
  3228. }
  3229. rtl8169_hw_reset(ioaddr);
  3230. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  3231. }
  3232. static void rtl8169_tx_interrupt(struct net_device *dev,
  3233. struct rtl8169_private *tp,
  3234. void __iomem *ioaddr)
  3235. {
  3236. unsigned int dirty_tx, tx_left;
  3237. dirty_tx = tp->dirty_tx;
  3238. smp_rmb();
  3239. tx_left = tp->cur_tx - dirty_tx;
  3240. while (tx_left > 0) {
  3241. unsigned int entry = dirty_tx % NUM_TX_DESC;
  3242. struct ring_info *tx_skb = tp->tx_skb + entry;
  3243. u32 status;
  3244. rmb();
  3245. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  3246. if (status & DescOwn)
  3247. break;
  3248. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  3249. tp->TxDescArray + entry);
  3250. if (status & LastFrag) {
  3251. dev->stats.tx_packets++;
  3252. dev->stats.tx_bytes += tx_skb->skb->len;
  3253. dev_kfree_skb(tx_skb->skb);
  3254. tx_skb->skb = NULL;
  3255. }
  3256. dirty_tx++;
  3257. tx_left--;
  3258. }
  3259. if (tp->dirty_tx != dirty_tx) {
  3260. tp->dirty_tx = dirty_tx;
  3261. smp_wmb();
  3262. if (netif_queue_stopped(dev) &&
  3263. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  3264. netif_wake_queue(dev);
  3265. }
  3266. /*
  3267. * 8168 hack: TxPoll requests are lost when the Tx packets are
  3268. * too close. Let's kick an extra TxPoll request when a burst
  3269. * of start_xmit activity is detected (if it is not detected,
  3270. * it is slow enough). -- FR
  3271. */
  3272. smp_rmb();
  3273. if (tp->cur_tx != dirty_tx)
  3274. RTL_W8(TxPoll, NPQ);
  3275. }
  3276. }
  3277. static inline int rtl8169_fragmented_frame(u32 status)
  3278. {
  3279. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  3280. }
  3281. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  3282. {
  3283. u32 status = opts1 & RxProtoMask;
  3284. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  3285. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  3286. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3287. else
  3288. skb_checksum_none_assert(skb);
  3289. }
  3290. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  3291. struct rtl8169_private *tp,
  3292. int pkt_size,
  3293. dma_addr_t addr)
  3294. {
  3295. struct sk_buff *skb;
  3296. struct device *d = &tp->pci_dev->dev;
  3297. data = rtl8169_align(data);
  3298. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  3299. prefetch(data);
  3300. skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
  3301. if (skb)
  3302. memcpy(skb->data, data, pkt_size);
  3303. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  3304. return skb;
  3305. }
  3306. /*
  3307. * Warning : rtl8169_rx_interrupt() might be called :
  3308. * 1) from NAPI (softirq) context
  3309. * (polling = 1 : we should call netif_receive_skb())
  3310. * 2) from process context (rtl8169_reset_task())
  3311. * (polling = 0 : we must call netif_rx() instead)
  3312. */
  3313. static int rtl8169_rx_interrupt(struct net_device *dev,
  3314. struct rtl8169_private *tp,
  3315. void __iomem *ioaddr, u32 budget)
  3316. {
  3317. unsigned int cur_rx, rx_left;
  3318. unsigned int count;
  3319. int polling = (budget != ~(u32)0) ? 1 : 0;
  3320. cur_rx = tp->cur_rx;
  3321. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  3322. rx_left = min(rx_left, budget);
  3323. for (; rx_left > 0; rx_left--, cur_rx++) {
  3324. unsigned int entry = cur_rx % NUM_RX_DESC;
  3325. struct RxDesc *desc = tp->RxDescArray + entry;
  3326. u32 status;
  3327. rmb();
  3328. status = le32_to_cpu(desc->opts1);
  3329. if (status & DescOwn)
  3330. break;
  3331. if (unlikely(status & RxRES)) {
  3332. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  3333. status);
  3334. dev->stats.rx_errors++;
  3335. if (status & (RxRWT | RxRUNT))
  3336. dev->stats.rx_length_errors++;
  3337. if (status & RxCRC)
  3338. dev->stats.rx_crc_errors++;
  3339. if (status & RxFOVF) {
  3340. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3341. dev->stats.rx_fifo_errors++;
  3342. }
  3343. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3344. } else {
  3345. struct sk_buff *skb;
  3346. dma_addr_t addr = le64_to_cpu(desc->addr);
  3347. int pkt_size = (status & 0x00001FFF) - 4;
  3348. /*
  3349. * The driver does not support incoming fragmented
  3350. * frames. They are seen as a symptom of over-mtu
  3351. * sized frames.
  3352. */
  3353. if (unlikely(rtl8169_fragmented_frame(status))) {
  3354. dev->stats.rx_dropped++;
  3355. dev->stats.rx_length_errors++;
  3356. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3357. continue;
  3358. }
  3359. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  3360. tp, pkt_size, addr);
  3361. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3362. if (!skb) {
  3363. dev->stats.rx_dropped++;
  3364. continue;
  3365. }
  3366. rtl8169_rx_csum(skb, status);
  3367. skb_put(skb, pkt_size);
  3368. skb->protocol = eth_type_trans(skb, dev);
  3369. if (rtl8169_rx_vlan_skb(tp, desc, skb, polling) < 0) {
  3370. if (likely(polling))
  3371. napi_gro_receive(&tp->napi, skb);
  3372. else
  3373. netif_rx(skb);
  3374. }
  3375. dev->stats.rx_bytes += pkt_size;
  3376. dev->stats.rx_packets++;
  3377. }
  3378. /* Work around for AMD plateform. */
  3379. if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
  3380. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  3381. desc->opts2 = 0;
  3382. cur_rx++;
  3383. }
  3384. }
  3385. count = cur_rx - tp->cur_rx;
  3386. tp->cur_rx = cur_rx;
  3387. tp->dirty_rx += count;
  3388. return count;
  3389. }
  3390. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  3391. {
  3392. struct net_device *dev = dev_instance;
  3393. struct rtl8169_private *tp = netdev_priv(dev);
  3394. void __iomem *ioaddr = tp->mmio_addr;
  3395. int handled = 0;
  3396. int status;
  3397. /* loop handling interrupts until we have no new ones or
  3398. * we hit a invalid/hotplug case.
  3399. */
  3400. status = RTL_R16(IntrStatus);
  3401. while (status && status != 0xffff) {
  3402. handled = 1;
  3403. /* Handle all of the error cases first. These will reset
  3404. * the chip, so just exit the loop.
  3405. */
  3406. if (unlikely(!netif_running(dev))) {
  3407. rtl8169_asic_down(ioaddr);
  3408. break;
  3409. }
  3410. /* Work around for rx fifo overflow */
  3411. if (unlikely(status & RxFIFOOver) &&
  3412. (tp->mac_version == RTL_GIGA_MAC_VER_11)) {
  3413. netif_stop_queue(dev);
  3414. rtl8169_tx_timeout(dev);
  3415. break;
  3416. }
  3417. if (unlikely(status & SYSErr)) {
  3418. rtl8169_pcierr_interrupt(dev);
  3419. break;
  3420. }
  3421. if (status & LinkChg)
  3422. __rtl8169_check_link_status(dev, tp, ioaddr, true);
  3423. /* We need to see the lastest version of tp->intr_mask to
  3424. * avoid ignoring an MSI interrupt and having to wait for
  3425. * another event which may never come.
  3426. */
  3427. smp_rmb();
  3428. if (status & tp->intr_mask & tp->napi_event) {
  3429. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  3430. tp->intr_mask = ~tp->napi_event;
  3431. if (likely(napi_schedule_prep(&tp->napi)))
  3432. __napi_schedule(&tp->napi);
  3433. else
  3434. netif_info(tp, intr, dev,
  3435. "interrupt %04x in poll\n", status);
  3436. }
  3437. /* We only get a new MSI interrupt when all active irq
  3438. * sources on the chip have been acknowledged. So, ack
  3439. * everything we've seen and check if new sources have become
  3440. * active to avoid blocking all interrupts from the chip.
  3441. */
  3442. RTL_W16(IntrStatus,
  3443. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  3444. status = RTL_R16(IntrStatus);
  3445. }
  3446. return IRQ_RETVAL(handled);
  3447. }
  3448. static int rtl8169_poll(struct napi_struct *napi, int budget)
  3449. {
  3450. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  3451. struct net_device *dev = tp->dev;
  3452. void __iomem *ioaddr = tp->mmio_addr;
  3453. int work_done;
  3454. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  3455. rtl8169_tx_interrupt(dev, tp, ioaddr);
  3456. if (work_done < budget) {
  3457. napi_complete(napi);
  3458. /* We need for force the visibility of tp->intr_mask
  3459. * for other CPUs, as we can loose an MSI interrupt
  3460. * and potentially wait for a retransmit timeout if we don't.
  3461. * The posted write to IntrMask is safe, as it will
  3462. * eventually make it to the chip and we won't loose anything
  3463. * until it does.
  3464. */
  3465. tp->intr_mask = 0xffff;
  3466. wmb();
  3467. RTL_W16(IntrMask, tp->intr_event);
  3468. }
  3469. return work_done;
  3470. }
  3471. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  3472. {
  3473. struct rtl8169_private *tp = netdev_priv(dev);
  3474. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  3475. return;
  3476. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  3477. RTL_W32(RxMissed, 0);
  3478. }
  3479. static void rtl8169_down(struct net_device *dev)
  3480. {
  3481. struct rtl8169_private *tp = netdev_priv(dev);
  3482. void __iomem *ioaddr = tp->mmio_addr;
  3483. rtl8169_delete_timer(dev);
  3484. netif_stop_queue(dev);
  3485. napi_disable(&tp->napi);
  3486. spin_lock_irq(&tp->lock);
  3487. rtl8169_asic_down(ioaddr);
  3488. /*
  3489. * At this point device interrupts can not be enabled in any function,
  3490. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
  3491. * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
  3492. */
  3493. rtl8169_rx_missed(dev, ioaddr);
  3494. spin_unlock_irq(&tp->lock);
  3495. synchronize_irq(dev->irq);
  3496. /* Give a racing hard_start_xmit a few cycles to complete. */
  3497. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  3498. rtl8169_tx_clear(tp);
  3499. rtl8169_rx_clear(tp);
  3500. }
  3501. static int rtl8169_close(struct net_device *dev)
  3502. {
  3503. struct rtl8169_private *tp = netdev_priv(dev);
  3504. struct pci_dev *pdev = tp->pci_dev;
  3505. pm_runtime_get_sync(&pdev->dev);
  3506. /* update counters before going down */
  3507. rtl8169_update_counters(dev);
  3508. rtl8169_down(dev);
  3509. free_irq(dev->irq, dev);
  3510. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3511. tp->RxPhyAddr);
  3512. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3513. tp->TxPhyAddr);
  3514. tp->TxDescArray = NULL;
  3515. tp->RxDescArray = NULL;
  3516. pm_runtime_put_sync(&pdev->dev);
  3517. return 0;
  3518. }
  3519. static void rtl_set_rx_mode(struct net_device *dev)
  3520. {
  3521. struct rtl8169_private *tp = netdev_priv(dev);
  3522. void __iomem *ioaddr = tp->mmio_addr;
  3523. unsigned long flags;
  3524. u32 mc_filter[2]; /* Multicast hash filter */
  3525. int rx_mode;
  3526. u32 tmp = 0;
  3527. if (dev->flags & IFF_PROMISC) {
  3528. /* Unconditionally log net taps. */
  3529. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  3530. rx_mode =
  3531. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  3532. AcceptAllPhys;
  3533. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3534. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  3535. (dev->flags & IFF_ALLMULTI)) {
  3536. /* Too many to filter perfectly -- accept all multicasts. */
  3537. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  3538. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3539. } else {
  3540. struct netdev_hw_addr *ha;
  3541. rx_mode = AcceptBroadcast | AcceptMyPhys;
  3542. mc_filter[1] = mc_filter[0] = 0;
  3543. netdev_for_each_mc_addr(ha, dev) {
  3544. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  3545. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  3546. rx_mode |= AcceptMulticast;
  3547. }
  3548. }
  3549. spin_lock_irqsave(&tp->lock, flags);
  3550. tmp = rtl8169_rx_config | rx_mode |
  3551. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  3552. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  3553. u32 data = mc_filter[0];
  3554. mc_filter[0] = swab32(mc_filter[1]);
  3555. mc_filter[1] = swab32(data);
  3556. }
  3557. RTL_W32(MAR0 + 4, mc_filter[1]);
  3558. RTL_W32(MAR0 + 0, mc_filter[0]);
  3559. RTL_W32(RxConfig, tmp);
  3560. spin_unlock_irqrestore(&tp->lock, flags);
  3561. }
  3562. /**
  3563. * rtl8169_get_stats - Get rtl8169 read/write statistics
  3564. * @dev: The Ethernet Device to get statistics for
  3565. *
  3566. * Get TX/RX statistics for rtl8169
  3567. */
  3568. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  3569. {
  3570. struct rtl8169_private *tp = netdev_priv(dev);
  3571. void __iomem *ioaddr = tp->mmio_addr;
  3572. unsigned long flags;
  3573. if (netif_running(dev)) {
  3574. spin_lock_irqsave(&tp->lock, flags);
  3575. rtl8169_rx_missed(dev, ioaddr);
  3576. spin_unlock_irqrestore(&tp->lock, flags);
  3577. }
  3578. return &dev->stats;
  3579. }
  3580. static void rtl8169_net_suspend(struct net_device *dev)
  3581. {
  3582. if (!netif_running(dev))
  3583. return;
  3584. netif_device_detach(dev);
  3585. netif_stop_queue(dev);
  3586. }
  3587. #ifdef CONFIG_PM
  3588. static int rtl8169_suspend(struct device *device)
  3589. {
  3590. struct pci_dev *pdev = to_pci_dev(device);
  3591. struct net_device *dev = pci_get_drvdata(pdev);
  3592. rtl8169_net_suspend(dev);
  3593. return 0;
  3594. }
  3595. static void __rtl8169_resume(struct net_device *dev)
  3596. {
  3597. netif_device_attach(dev);
  3598. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3599. }
  3600. static int rtl8169_resume(struct device *device)
  3601. {
  3602. struct pci_dev *pdev = to_pci_dev(device);
  3603. struct net_device *dev = pci_get_drvdata(pdev);
  3604. struct rtl8169_private *tp = netdev_priv(dev);
  3605. rtl8169_init_phy(dev, tp);
  3606. if (netif_running(dev))
  3607. __rtl8169_resume(dev);
  3608. return 0;
  3609. }
  3610. static int rtl8169_runtime_suspend(struct device *device)
  3611. {
  3612. struct pci_dev *pdev = to_pci_dev(device);
  3613. struct net_device *dev = pci_get_drvdata(pdev);
  3614. struct rtl8169_private *tp = netdev_priv(dev);
  3615. if (!tp->TxDescArray)
  3616. return 0;
  3617. spin_lock_irq(&tp->lock);
  3618. tp->saved_wolopts = __rtl8169_get_wol(tp);
  3619. __rtl8169_set_wol(tp, WAKE_ANY);
  3620. spin_unlock_irq(&tp->lock);
  3621. rtl8169_net_suspend(dev);
  3622. return 0;
  3623. }
  3624. static int rtl8169_runtime_resume(struct device *device)
  3625. {
  3626. struct pci_dev *pdev = to_pci_dev(device);
  3627. struct net_device *dev = pci_get_drvdata(pdev);
  3628. struct rtl8169_private *tp = netdev_priv(dev);
  3629. if (!tp->TxDescArray)
  3630. return 0;
  3631. spin_lock_irq(&tp->lock);
  3632. __rtl8169_set_wol(tp, tp->saved_wolopts);
  3633. tp->saved_wolopts = 0;
  3634. spin_unlock_irq(&tp->lock);
  3635. rtl8169_init_phy(dev, tp);
  3636. __rtl8169_resume(dev);
  3637. return 0;
  3638. }
  3639. static int rtl8169_runtime_idle(struct device *device)
  3640. {
  3641. struct pci_dev *pdev = to_pci_dev(device);
  3642. struct net_device *dev = pci_get_drvdata(pdev);
  3643. struct rtl8169_private *tp = netdev_priv(dev);
  3644. return tp->TxDescArray ? -EBUSY : 0;
  3645. }
  3646. static const struct dev_pm_ops rtl8169_pm_ops = {
  3647. .suspend = rtl8169_suspend,
  3648. .resume = rtl8169_resume,
  3649. .freeze = rtl8169_suspend,
  3650. .thaw = rtl8169_resume,
  3651. .poweroff = rtl8169_suspend,
  3652. .restore = rtl8169_resume,
  3653. .runtime_suspend = rtl8169_runtime_suspend,
  3654. .runtime_resume = rtl8169_runtime_resume,
  3655. .runtime_idle = rtl8169_runtime_idle,
  3656. };
  3657. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  3658. #else /* !CONFIG_PM */
  3659. #define RTL8169_PM_OPS NULL
  3660. #endif /* !CONFIG_PM */
  3661. static void rtl_shutdown(struct pci_dev *pdev)
  3662. {
  3663. struct net_device *dev = pci_get_drvdata(pdev);
  3664. struct rtl8169_private *tp = netdev_priv(dev);
  3665. void __iomem *ioaddr = tp->mmio_addr;
  3666. rtl8169_net_suspend(dev);
  3667. /* restore original MAC address */
  3668. rtl_rar_set(tp, dev->perm_addr);
  3669. spin_lock_irq(&tp->lock);
  3670. rtl8169_asic_down(ioaddr);
  3671. spin_unlock_irq(&tp->lock);
  3672. if (system_state == SYSTEM_POWER_OFF) {
  3673. /* WoL fails with some 8168 when the receiver is disabled. */
  3674. if (tp->features & RTL_FEATURE_WOL) {
  3675. pci_clear_master(pdev);
  3676. RTL_W8(ChipCmd, CmdRxEnb);
  3677. /* PCI commit */
  3678. RTL_R8(ChipCmd);
  3679. }
  3680. pci_wake_from_d3(pdev, true);
  3681. pci_set_power_state(pdev, PCI_D3hot);
  3682. }
  3683. }
  3684. static struct pci_driver rtl8169_pci_driver = {
  3685. .name = MODULENAME,
  3686. .id_table = rtl8169_pci_tbl,
  3687. .probe = rtl8169_init_one,
  3688. .remove = __devexit_p(rtl8169_remove_one),
  3689. .shutdown = rtl_shutdown,
  3690. .driver.pm = RTL8169_PM_OPS,
  3691. };
  3692. static int __init rtl8169_init_module(void)
  3693. {
  3694. return pci_register_driver(&rtl8169_pci_driver);
  3695. }
  3696. static void __exit rtl8169_cleanup_module(void)
  3697. {
  3698. pci_unregister_driver(&rtl8169_pci_driver);
  3699. }
  3700. module_init(rtl8169_init_module);
  3701. module_exit(rtl8169_cleanup_module);