r8169.c 119 KB

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