r8169.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213
  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/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/firmware.h>
  28. #include <linux/pci-aspm.h>
  29. #include <linux/prefetch.h>
  30. #include <asm/system.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #define RTL8169_VERSION "2.3LK-NAPI"
  34. #define MODULENAME "r8169"
  35. #define PFX MODULENAME ": "
  36. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  37. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  38. #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
  39. #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
  40. #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
  41. #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
  42. #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
  43. #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
  44. #ifdef RTL8169_DEBUG
  45. #define assert(expr) \
  46. if (!(expr)) { \
  47. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  48. #expr,__FILE__,__func__,__LINE__); \
  49. }
  50. #define dprintk(fmt, args...) \
  51. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  52. #else
  53. #define assert(expr) do {} while (0)
  54. #define dprintk(fmt, args...) do {} while (0)
  55. #endif /* RTL8169_DEBUG */
  56. #define R8169_MSG_DEFAULT \
  57. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  58. #define TX_BUFFS_AVAIL(tp) \
  59. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  60. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  61. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  62. static const int multicast_filter_limit = 32;
  63. #define MAX_READ_REQUEST_SHIFT 12
  64. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  65. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  66. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  67. #define R8169_REGS_SIZE 256
  68. #define R8169_NAPI_WEIGHT 64
  69. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  70. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  71. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  72. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  73. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  74. #define RTL8169_TX_TIMEOUT (6*HZ)
  75. #define RTL8169_PHY_TIMEOUT (10*HZ)
  76. #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
  77. #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
  78. #define RTL_EEPROM_SIG_ADDR 0x0000
  79. /* write/read MMIO register */
  80. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  81. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  82. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  83. #define RTL_R8(reg) readb (ioaddr + (reg))
  84. #define RTL_R16(reg) readw (ioaddr + (reg))
  85. #define RTL_R32(reg) readl (ioaddr + (reg))
  86. enum mac_version {
  87. RTL_GIGA_MAC_VER_01 = 0,
  88. RTL_GIGA_MAC_VER_02,
  89. RTL_GIGA_MAC_VER_03,
  90. RTL_GIGA_MAC_VER_04,
  91. RTL_GIGA_MAC_VER_05,
  92. RTL_GIGA_MAC_VER_06,
  93. RTL_GIGA_MAC_VER_07,
  94. RTL_GIGA_MAC_VER_08,
  95. RTL_GIGA_MAC_VER_09,
  96. RTL_GIGA_MAC_VER_10,
  97. RTL_GIGA_MAC_VER_11,
  98. RTL_GIGA_MAC_VER_12,
  99. RTL_GIGA_MAC_VER_13,
  100. RTL_GIGA_MAC_VER_14,
  101. RTL_GIGA_MAC_VER_15,
  102. RTL_GIGA_MAC_VER_16,
  103. RTL_GIGA_MAC_VER_17,
  104. RTL_GIGA_MAC_VER_18,
  105. RTL_GIGA_MAC_VER_19,
  106. RTL_GIGA_MAC_VER_20,
  107. RTL_GIGA_MAC_VER_21,
  108. RTL_GIGA_MAC_VER_22,
  109. RTL_GIGA_MAC_VER_23,
  110. RTL_GIGA_MAC_VER_24,
  111. RTL_GIGA_MAC_VER_25,
  112. RTL_GIGA_MAC_VER_26,
  113. RTL_GIGA_MAC_VER_27,
  114. RTL_GIGA_MAC_VER_28,
  115. RTL_GIGA_MAC_VER_29,
  116. RTL_GIGA_MAC_VER_30,
  117. RTL_GIGA_MAC_VER_31,
  118. RTL_GIGA_MAC_VER_32,
  119. RTL_GIGA_MAC_VER_33,
  120. RTL_GIGA_MAC_VER_34,
  121. RTL_GIGA_MAC_VER_35,
  122. RTL_GIGA_MAC_VER_36,
  123. RTL_GIGA_MAC_NONE = 0xff,
  124. };
  125. enum rtl_tx_desc_version {
  126. RTL_TD_0 = 0,
  127. RTL_TD_1 = 1,
  128. };
  129. #define JUMBO_1K ETH_DATA_LEN
  130. #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
  131. #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
  132. #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
  133. #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
  134. #define _R(NAME,TD,FW,SZ,B) { \
  135. .name = NAME, \
  136. .txd_version = TD, \
  137. .fw_name = FW, \
  138. .jumbo_max = SZ, \
  139. .jumbo_tx_csum = B \
  140. }
  141. static const struct {
  142. const char *name;
  143. enum rtl_tx_desc_version txd_version;
  144. const char *fw_name;
  145. u16 jumbo_max;
  146. bool jumbo_tx_csum;
  147. } rtl_chip_infos[] = {
  148. /* PCI devices. */
  149. [RTL_GIGA_MAC_VER_01] =
  150. _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
  151. [RTL_GIGA_MAC_VER_02] =
  152. _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
  153. [RTL_GIGA_MAC_VER_03] =
  154. _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
  155. [RTL_GIGA_MAC_VER_04] =
  156. _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
  157. [RTL_GIGA_MAC_VER_05] =
  158. _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
  159. [RTL_GIGA_MAC_VER_06] =
  160. _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
  161. /* PCI-E devices. */
  162. [RTL_GIGA_MAC_VER_07] =
  163. _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
  164. [RTL_GIGA_MAC_VER_08] =
  165. _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
  166. [RTL_GIGA_MAC_VER_09] =
  167. _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
  168. [RTL_GIGA_MAC_VER_10] =
  169. _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
  170. [RTL_GIGA_MAC_VER_11] =
  171. _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
  172. [RTL_GIGA_MAC_VER_12] =
  173. _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
  174. [RTL_GIGA_MAC_VER_13] =
  175. _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
  176. [RTL_GIGA_MAC_VER_14] =
  177. _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
  178. [RTL_GIGA_MAC_VER_15] =
  179. _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
  180. [RTL_GIGA_MAC_VER_16] =
  181. _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
  182. [RTL_GIGA_MAC_VER_17] =
  183. _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
  184. [RTL_GIGA_MAC_VER_18] =
  185. _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
  186. [RTL_GIGA_MAC_VER_19] =
  187. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  188. [RTL_GIGA_MAC_VER_20] =
  189. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  190. [RTL_GIGA_MAC_VER_21] =
  191. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  192. [RTL_GIGA_MAC_VER_22] =
  193. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  194. [RTL_GIGA_MAC_VER_23] =
  195. _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
  196. [RTL_GIGA_MAC_VER_24] =
  197. _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
  198. [RTL_GIGA_MAC_VER_25] =
  199. _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
  200. JUMBO_9K, false),
  201. [RTL_GIGA_MAC_VER_26] =
  202. _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
  203. JUMBO_9K, false),
  204. [RTL_GIGA_MAC_VER_27] =
  205. _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
  206. [RTL_GIGA_MAC_VER_28] =
  207. _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
  208. [RTL_GIGA_MAC_VER_29] =
  209. _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
  210. JUMBO_1K, true),
  211. [RTL_GIGA_MAC_VER_30] =
  212. _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
  213. JUMBO_1K, true),
  214. [RTL_GIGA_MAC_VER_31] =
  215. _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
  216. [RTL_GIGA_MAC_VER_32] =
  217. _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
  218. JUMBO_9K, false),
  219. [RTL_GIGA_MAC_VER_33] =
  220. _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
  221. JUMBO_9K, false),
  222. [RTL_GIGA_MAC_VER_34] =
  223. _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
  224. JUMBO_9K, false),
  225. [RTL_GIGA_MAC_VER_35] =
  226. _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
  227. JUMBO_9K, false),
  228. [RTL_GIGA_MAC_VER_36] =
  229. _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
  230. JUMBO_9K, false),
  231. };
  232. #undef _R
  233. enum cfg_version {
  234. RTL_CFG_0 = 0x00,
  235. RTL_CFG_1,
  236. RTL_CFG_2
  237. };
  238. static void rtl_hw_start_8169(struct net_device *);
  239. static void rtl_hw_start_8168(struct net_device *);
  240. static void rtl_hw_start_8101(struct net_device *);
  241. static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
  242. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  243. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  244. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  245. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  246. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  247. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  248. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
  249. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  250. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  251. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  252. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  253. { 0x0001, 0x8168,
  254. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  255. {0,},
  256. };
  257. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  258. static int rx_buf_sz = 16383;
  259. static int use_dac;
  260. static struct {
  261. u32 msg_enable;
  262. } debug = { -1 };
  263. enum rtl_registers {
  264. MAC0 = 0, /* Ethernet hardware address. */
  265. MAC4 = 4,
  266. MAR0 = 8, /* Multicast filter. */
  267. CounterAddrLow = 0x10,
  268. CounterAddrHigh = 0x14,
  269. TxDescStartAddrLow = 0x20,
  270. TxDescStartAddrHigh = 0x24,
  271. TxHDescStartAddrLow = 0x28,
  272. TxHDescStartAddrHigh = 0x2c,
  273. FLASH = 0x30,
  274. ERSR = 0x36,
  275. ChipCmd = 0x37,
  276. TxPoll = 0x38,
  277. IntrMask = 0x3c,
  278. IntrStatus = 0x3e,
  279. TxConfig = 0x40,
  280. #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
  281. #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
  282. RxConfig = 0x44,
  283. #define RX128_INT_EN (1 << 15) /* 8111c and later */
  284. #define RX_MULTI_EN (1 << 14) /* 8111c only */
  285. #define RXCFG_FIFO_SHIFT 13
  286. /* No threshold before first PCI xfer */
  287. #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
  288. #define RXCFG_DMA_SHIFT 8
  289. /* Unlimited maximum PCI burst. */
  290. #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
  291. RxMissed = 0x4c,
  292. Cfg9346 = 0x50,
  293. Config0 = 0x51,
  294. Config1 = 0x52,
  295. Config2 = 0x53,
  296. Config3 = 0x54,
  297. Config4 = 0x55,
  298. Config5 = 0x56,
  299. MultiIntr = 0x5c,
  300. PHYAR = 0x60,
  301. PHYstatus = 0x6c,
  302. RxMaxSize = 0xda,
  303. CPlusCmd = 0xe0,
  304. IntrMitigate = 0xe2,
  305. RxDescAddrLow = 0xe4,
  306. RxDescAddrHigh = 0xe8,
  307. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  308. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  309. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  310. #define TxPacketMax (8064 >> 7)
  311. #define EarlySize 0x27
  312. FuncEvent = 0xf0,
  313. FuncEventMask = 0xf4,
  314. FuncPresetState = 0xf8,
  315. FuncForceEvent = 0xfc,
  316. };
  317. enum rtl8110_registers {
  318. TBICSR = 0x64,
  319. TBI_ANAR = 0x68,
  320. TBI_LPAR = 0x6a,
  321. };
  322. enum rtl8168_8101_registers {
  323. CSIDR = 0x64,
  324. CSIAR = 0x68,
  325. #define CSIAR_FLAG 0x80000000
  326. #define CSIAR_WRITE_CMD 0x80000000
  327. #define CSIAR_BYTE_ENABLE 0x0f
  328. #define CSIAR_BYTE_ENABLE_SHIFT 12
  329. #define CSIAR_ADDR_MASK 0x0fff
  330. PMCH = 0x6f,
  331. EPHYAR = 0x80,
  332. #define EPHYAR_FLAG 0x80000000
  333. #define EPHYAR_WRITE_CMD 0x80000000
  334. #define EPHYAR_REG_MASK 0x1f
  335. #define EPHYAR_REG_SHIFT 16
  336. #define EPHYAR_DATA_MASK 0xffff
  337. DLLPR = 0xd0,
  338. #define PFM_EN (1 << 6)
  339. DBG_REG = 0xd1,
  340. #define FIX_NAK_1 (1 << 4)
  341. #define FIX_NAK_2 (1 << 3)
  342. TWSI = 0xd2,
  343. MCU = 0xd3,
  344. #define NOW_IS_OOB (1 << 7)
  345. #define EN_NDP (1 << 3)
  346. #define EN_OOB_RESET (1 << 2)
  347. EFUSEAR = 0xdc,
  348. #define EFUSEAR_FLAG 0x80000000
  349. #define EFUSEAR_WRITE_CMD 0x80000000
  350. #define EFUSEAR_READ_CMD 0x00000000
  351. #define EFUSEAR_REG_MASK 0x03ff
  352. #define EFUSEAR_REG_SHIFT 8
  353. #define EFUSEAR_DATA_MASK 0xff
  354. };
  355. enum rtl8168_registers {
  356. LED_FREQ = 0x1a,
  357. EEE_LED = 0x1b,
  358. ERIDR = 0x70,
  359. ERIAR = 0x74,
  360. #define ERIAR_FLAG 0x80000000
  361. #define ERIAR_WRITE_CMD 0x80000000
  362. #define ERIAR_READ_CMD 0x00000000
  363. #define ERIAR_ADDR_BYTE_ALIGN 4
  364. #define ERIAR_TYPE_SHIFT 16
  365. #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
  366. #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
  367. #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
  368. #define ERIAR_MASK_SHIFT 12
  369. #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
  370. #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
  371. #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
  372. EPHY_RXER_NUM = 0x7c,
  373. OCPDR = 0xb0, /* OCP GPHY access */
  374. #define OCPDR_WRITE_CMD 0x80000000
  375. #define OCPDR_READ_CMD 0x00000000
  376. #define OCPDR_REG_MASK 0x7f
  377. #define OCPDR_GPHY_REG_SHIFT 16
  378. #define OCPDR_DATA_MASK 0xffff
  379. OCPAR = 0xb4,
  380. #define OCPAR_FLAG 0x80000000
  381. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  382. #define OCPAR_GPHY_READ_CMD 0x0000f060
  383. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  384. MISC = 0xf0, /* 8168e only. */
  385. #define TXPLA_RST (1 << 29)
  386. #define PWM_EN (1 << 22)
  387. };
  388. enum rtl_register_content {
  389. /* InterruptStatusBits */
  390. SYSErr = 0x8000,
  391. PCSTimeout = 0x4000,
  392. SWInt = 0x0100,
  393. TxDescUnavail = 0x0080,
  394. RxFIFOOver = 0x0040,
  395. LinkChg = 0x0020,
  396. RxOverflow = 0x0010,
  397. TxErr = 0x0008,
  398. TxOK = 0x0004,
  399. RxErr = 0x0002,
  400. RxOK = 0x0001,
  401. /* RxStatusDesc */
  402. RxBOVF = (1 << 24),
  403. RxFOVF = (1 << 23),
  404. RxRWT = (1 << 22),
  405. RxRES = (1 << 21),
  406. RxRUNT = (1 << 20),
  407. RxCRC = (1 << 19),
  408. /* ChipCmdBits */
  409. StopReq = 0x80,
  410. CmdReset = 0x10,
  411. CmdRxEnb = 0x08,
  412. CmdTxEnb = 0x04,
  413. RxBufEmpty = 0x01,
  414. /* TXPoll register p.5 */
  415. HPQ = 0x80, /* Poll cmd on the high prio queue */
  416. NPQ = 0x40, /* Poll cmd on the low prio queue */
  417. FSWInt = 0x01, /* Forced software interrupt */
  418. /* Cfg9346Bits */
  419. Cfg9346_Lock = 0x00,
  420. Cfg9346_Unlock = 0xc0,
  421. /* rx_mode_bits */
  422. AcceptErr = 0x20,
  423. AcceptRunt = 0x10,
  424. AcceptBroadcast = 0x08,
  425. AcceptMulticast = 0x04,
  426. AcceptMyPhys = 0x02,
  427. AcceptAllPhys = 0x01,
  428. #define RX_CONFIG_ACCEPT_MASK 0x3f
  429. /* TxConfigBits */
  430. TxInterFrameGapShift = 24,
  431. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  432. /* Config1 register p.24 */
  433. LEDS1 = (1 << 7),
  434. LEDS0 = (1 << 6),
  435. Speed_down = (1 << 4),
  436. MEMMAP = (1 << 3),
  437. IOMAP = (1 << 2),
  438. VPD = (1 << 1),
  439. PMEnable = (1 << 0), /* Power Management Enable */
  440. /* Config2 register p. 25 */
  441. MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
  442. PCI_Clock_66MHz = 0x01,
  443. PCI_Clock_33MHz = 0x00,
  444. /* Config3 register p.25 */
  445. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  446. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  447. Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
  448. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  449. /* Config4 register */
  450. Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
  451. /* Config5 register p.27 */
  452. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  453. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  454. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  455. Spi_en = (1 << 3),
  456. LanWake = (1 << 1), /* LanWake enable/disable */
  457. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  458. /* TBICSR p.28 */
  459. TBIReset = 0x80000000,
  460. TBILoopback = 0x40000000,
  461. TBINwEnable = 0x20000000,
  462. TBINwRestart = 0x10000000,
  463. TBILinkOk = 0x02000000,
  464. TBINwComplete = 0x01000000,
  465. /* CPlusCmd p.31 */
  466. EnableBist = (1 << 15), // 8168 8101
  467. Mac_dbgo_oe = (1 << 14), // 8168 8101
  468. Normal_mode = (1 << 13), // unused
  469. Force_half_dup = (1 << 12), // 8168 8101
  470. Force_rxflow_en = (1 << 11), // 8168 8101
  471. Force_txflow_en = (1 << 10), // 8168 8101
  472. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  473. ASF = (1 << 8), // 8168 8101
  474. PktCntrDisable = (1 << 7), // 8168 8101
  475. Mac_dbgo_sel = 0x001c, // 8168
  476. RxVlan = (1 << 6),
  477. RxChkSum = (1 << 5),
  478. PCIDAC = (1 << 4),
  479. PCIMulRW = (1 << 3),
  480. INTT_0 = 0x0000, // 8168
  481. INTT_1 = 0x0001, // 8168
  482. INTT_2 = 0x0002, // 8168
  483. INTT_3 = 0x0003, // 8168
  484. /* rtl8169_PHYstatus */
  485. TBI_Enable = 0x80,
  486. TxFlowCtrl = 0x40,
  487. RxFlowCtrl = 0x20,
  488. _1000bpsF = 0x10,
  489. _100bps = 0x08,
  490. _10bps = 0x04,
  491. LinkStatus = 0x02,
  492. FullDup = 0x01,
  493. /* _TBICSRBit */
  494. TBILinkOK = 0x02000000,
  495. /* DumpCounterCommand */
  496. CounterDump = 0x8,
  497. };
  498. enum rtl_desc_bit {
  499. /* First doubleword. */
  500. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  501. RingEnd = (1 << 30), /* End of descriptor ring */
  502. FirstFrag = (1 << 29), /* First segment of a packet */
  503. LastFrag = (1 << 28), /* Final segment of a packet */
  504. };
  505. /* Generic case. */
  506. enum rtl_tx_desc_bit {
  507. /* First doubleword. */
  508. TD_LSO = (1 << 27), /* Large Send Offload */
  509. #define TD_MSS_MAX 0x07ffu /* MSS value */
  510. /* Second doubleword. */
  511. TxVlanTag = (1 << 17), /* Add VLAN tag */
  512. };
  513. /* 8169, 8168b and 810x except 8102e. */
  514. enum rtl_tx_desc_bit_0 {
  515. /* First doubleword. */
  516. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  517. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  518. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  519. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  520. };
  521. /* 8102e, 8168c and beyond. */
  522. enum rtl_tx_desc_bit_1 {
  523. /* Second doubleword. */
  524. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  525. TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
  526. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  527. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  528. };
  529. static const struct rtl_tx_desc_info {
  530. struct {
  531. u32 udp;
  532. u32 tcp;
  533. } checksum;
  534. u16 mss_shift;
  535. u16 opts_offset;
  536. } tx_desc_info [] = {
  537. [RTL_TD_0] = {
  538. .checksum = {
  539. .udp = TD0_IP_CS | TD0_UDP_CS,
  540. .tcp = TD0_IP_CS | TD0_TCP_CS
  541. },
  542. .mss_shift = TD0_MSS_SHIFT,
  543. .opts_offset = 0
  544. },
  545. [RTL_TD_1] = {
  546. .checksum = {
  547. .udp = TD1_IP_CS | TD1_UDP_CS,
  548. .tcp = TD1_IP_CS | TD1_TCP_CS
  549. },
  550. .mss_shift = TD1_MSS_SHIFT,
  551. .opts_offset = 1
  552. }
  553. };
  554. enum rtl_rx_desc_bit {
  555. /* Rx private */
  556. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  557. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  558. #define RxProtoUDP (PID1)
  559. #define RxProtoTCP (PID0)
  560. #define RxProtoIP (PID1 | PID0)
  561. #define RxProtoMask RxProtoIP
  562. IPFail = (1 << 16), /* IP checksum failed */
  563. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  564. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  565. RxVlanTag = (1 << 16), /* VLAN tag available */
  566. };
  567. #define RsvdMask 0x3fffc000
  568. struct TxDesc {
  569. __le32 opts1;
  570. __le32 opts2;
  571. __le64 addr;
  572. };
  573. struct RxDesc {
  574. __le32 opts1;
  575. __le32 opts2;
  576. __le64 addr;
  577. };
  578. struct ring_info {
  579. struct sk_buff *skb;
  580. u32 len;
  581. u8 __pad[sizeof(void *) - sizeof(u32)];
  582. };
  583. enum features {
  584. RTL_FEATURE_WOL = (1 << 0),
  585. RTL_FEATURE_MSI = (1 << 1),
  586. RTL_FEATURE_GMII = (1 << 2),
  587. };
  588. struct rtl8169_counters {
  589. __le64 tx_packets;
  590. __le64 rx_packets;
  591. __le64 tx_errors;
  592. __le32 rx_errors;
  593. __le16 rx_missed;
  594. __le16 align_errors;
  595. __le32 tx_one_collision;
  596. __le32 tx_multi_collision;
  597. __le64 rx_unicast;
  598. __le64 rx_broadcast;
  599. __le32 rx_multicast;
  600. __le16 tx_aborted;
  601. __le16 tx_underun;
  602. };
  603. struct rtl8169_private {
  604. void __iomem *mmio_addr; /* memory map physical address */
  605. struct pci_dev *pci_dev;
  606. struct net_device *dev;
  607. struct napi_struct napi;
  608. spinlock_t lock;
  609. u32 msg_enable;
  610. u16 txd_version;
  611. u16 mac_version;
  612. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  613. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  614. u32 dirty_rx;
  615. u32 dirty_tx;
  616. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  617. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  618. dma_addr_t TxPhyAddr;
  619. dma_addr_t RxPhyAddr;
  620. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  621. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  622. struct timer_list timer;
  623. u16 cp_cmd;
  624. u16 intr_event;
  625. u16 napi_event;
  626. u16 intr_mask;
  627. struct mdio_ops {
  628. void (*write)(void __iomem *, int, int);
  629. int (*read)(void __iomem *, int);
  630. } mdio_ops;
  631. struct pll_power_ops {
  632. void (*down)(struct rtl8169_private *);
  633. void (*up)(struct rtl8169_private *);
  634. } pll_power_ops;
  635. struct jumbo_ops {
  636. void (*enable)(struct rtl8169_private *);
  637. void (*disable)(struct rtl8169_private *);
  638. } jumbo_ops;
  639. int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
  640. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  641. void (*phy_reset_enable)(struct rtl8169_private *tp);
  642. void (*hw_start)(struct net_device *);
  643. unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
  644. unsigned int (*link_ok)(void __iomem *);
  645. int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
  646. struct {
  647. struct work_struct work;
  648. } wk;
  649. unsigned features;
  650. struct mii_if_info mii;
  651. struct rtl8169_counters counters;
  652. u32 saved_wolopts;
  653. u32 opts1_mask;
  654. struct rtl_fw {
  655. const struct firmware *fw;
  656. #define RTL_VER_SIZE 32
  657. char version[RTL_VER_SIZE];
  658. struct rtl_fw_phy_action {
  659. __le32 *code;
  660. size_t size;
  661. } phy_action;
  662. } *rtl_fw;
  663. #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
  664. };
  665. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  666. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  667. module_param(use_dac, int, 0);
  668. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  669. module_param_named(debug, debug.msg_enable, int, 0);
  670. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  671. MODULE_LICENSE("GPL");
  672. MODULE_VERSION(RTL8169_VERSION);
  673. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  674. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  675. MODULE_FIRMWARE(FIRMWARE_8168E_1);
  676. MODULE_FIRMWARE(FIRMWARE_8168E_2);
  677. MODULE_FIRMWARE(FIRMWARE_8168E_3);
  678. MODULE_FIRMWARE(FIRMWARE_8105E_1);
  679. MODULE_FIRMWARE(FIRMWARE_8168F_1);
  680. MODULE_FIRMWARE(FIRMWARE_8168F_2);
  681. static int rtl8169_open(struct net_device *dev);
  682. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  683. struct net_device *dev);
  684. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  685. static int rtl8169_init_ring(struct net_device *dev);
  686. static void rtl_hw_start(struct net_device *dev);
  687. static int rtl8169_close(struct net_device *dev);
  688. static void rtl_set_rx_mode(struct net_device *dev);
  689. static void rtl8169_tx_timeout(struct net_device *dev);
  690. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  691. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  692. void __iomem *, u32 budget);
  693. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  694. static void rtl8169_down(struct net_device *dev);
  695. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  696. static int rtl8169_poll(struct napi_struct *napi, int budget);
  697. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  698. {
  699. int cap = pci_pcie_cap(pdev);
  700. if (cap) {
  701. u16 ctl;
  702. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  703. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  704. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  705. }
  706. }
  707. static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  708. {
  709. void __iomem *ioaddr = tp->mmio_addr;
  710. int i;
  711. RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  712. for (i = 0; i < 20; i++) {
  713. udelay(100);
  714. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  715. break;
  716. }
  717. return RTL_R32(OCPDR);
  718. }
  719. static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
  720. {
  721. void __iomem *ioaddr = tp->mmio_addr;
  722. int i;
  723. RTL_W32(OCPDR, data);
  724. RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  725. for (i = 0; i < 20; i++) {
  726. udelay(100);
  727. if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
  728. break;
  729. }
  730. }
  731. static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
  732. {
  733. void __iomem *ioaddr = tp->mmio_addr;
  734. int i;
  735. RTL_W8(ERIDR, cmd);
  736. RTL_W32(ERIAR, 0x800010e8);
  737. msleep(2);
  738. for (i = 0; i < 5; i++) {
  739. udelay(100);
  740. if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
  741. break;
  742. }
  743. ocp_write(tp, 0x1, 0x30, 0x00000001);
  744. }
  745. #define OOB_CMD_RESET 0x00
  746. #define OOB_CMD_DRIVER_START 0x05
  747. #define OOB_CMD_DRIVER_STOP 0x06
  748. static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
  749. {
  750. return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
  751. }
  752. static void rtl8168_driver_start(struct rtl8169_private *tp)
  753. {
  754. u16 reg;
  755. int i;
  756. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  757. reg = rtl8168_get_ocp_reg(tp);
  758. for (i = 0; i < 10; i++) {
  759. msleep(10);
  760. if (ocp_read(tp, 0x0f, reg) & 0x00000800)
  761. break;
  762. }
  763. }
  764. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  765. {
  766. u16 reg;
  767. int i;
  768. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  769. reg = rtl8168_get_ocp_reg(tp);
  770. for (i = 0; i < 10; i++) {
  771. msleep(10);
  772. if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
  773. break;
  774. }
  775. }
  776. static int r8168dp_check_dash(struct rtl8169_private *tp)
  777. {
  778. u16 reg = rtl8168_get_ocp_reg(tp);
  779. return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
  780. }
  781. static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  782. {
  783. int i;
  784. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  785. for (i = 20; i > 0; i--) {
  786. /*
  787. * Check if the RTL8169 has completed writing to the specified
  788. * MII register.
  789. */
  790. if (!(RTL_R32(PHYAR) & 0x80000000))
  791. break;
  792. udelay(25);
  793. }
  794. /*
  795. * According to hardware specs a 20us delay is required after write
  796. * complete indication, but before sending next command.
  797. */
  798. udelay(20);
  799. }
  800. static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
  801. {
  802. int i, value = -1;
  803. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  804. for (i = 20; i > 0; i--) {
  805. /*
  806. * Check if the RTL8169 has completed retrieving data from
  807. * the specified MII register.
  808. */
  809. if (RTL_R32(PHYAR) & 0x80000000) {
  810. value = RTL_R32(PHYAR) & 0xffff;
  811. break;
  812. }
  813. udelay(25);
  814. }
  815. /*
  816. * According to hardware specs a 20us delay is required after read
  817. * complete indication, but before sending next command.
  818. */
  819. udelay(20);
  820. return value;
  821. }
  822. static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
  823. {
  824. int i;
  825. RTL_W32(OCPDR, data |
  826. ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  827. RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
  828. RTL_W32(EPHY_RXER_NUM, 0);
  829. for (i = 0; i < 100; i++) {
  830. mdelay(1);
  831. if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
  832. break;
  833. }
  834. }
  835. static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  836. {
  837. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
  838. (value & OCPDR_DATA_MASK));
  839. }
  840. static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
  841. {
  842. int i;
  843. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
  844. mdelay(1);
  845. RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
  846. RTL_W32(EPHY_RXER_NUM, 0);
  847. for (i = 0; i < 100; i++) {
  848. mdelay(1);
  849. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  850. break;
  851. }
  852. return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
  853. }
  854. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  855. static void r8168dp_2_mdio_start(void __iomem *ioaddr)
  856. {
  857. RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  858. }
  859. static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
  860. {
  861. RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  862. }
  863. static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  864. {
  865. r8168dp_2_mdio_start(ioaddr);
  866. r8169_mdio_write(ioaddr, reg_addr, value);
  867. r8168dp_2_mdio_stop(ioaddr);
  868. }
  869. static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
  870. {
  871. int value;
  872. r8168dp_2_mdio_start(ioaddr);
  873. value = r8169_mdio_read(ioaddr, reg_addr);
  874. r8168dp_2_mdio_stop(ioaddr);
  875. return value;
  876. }
  877. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  878. {
  879. tp->mdio_ops.write(tp->mmio_addr, location, val);
  880. }
  881. static int rtl_readphy(struct rtl8169_private *tp, int location)
  882. {
  883. return tp->mdio_ops.read(tp->mmio_addr, location);
  884. }
  885. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  886. {
  887. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  888. }
  889. static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  890. {
  891. int val;
  892. val = rtl_readphy(tp, reg_addr);
  893. rtl_writephy(tp, reg_addr, (val | p) & ~m);
  894. }
  895. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  896. int val)
  897. {
  898. struct rtl8169_private *tp = netdev_priv(dev);
  899. rtl_writephy(tp, location, val);
  900. }
  901. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  902. {
  903. struct rtl8169_private *tp = netdev_priv(dev);
  904. return rtl_readphy(tp, location);
  905. }
  906. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  907. {
  908. unsigned int i;
  909. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  910. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  911. for (i = 0; i < 100; i++) {
  912. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  913. break;
  914. udelay(10);
  915. }
  916. }
  917. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  918. {
  919. u16 value = 0xffff;
  920. unsigned int i;
  921. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  922. for (i = 0; i < 100; i++) {
  923. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  924. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  925. break;
  926. }
  927. udelay(10);
  928. }
  929. return value;
  930. }
  931. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  932. {
  933. unsigned int i;
  934. RTL_W32(CSIDR, value);
  935. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  936. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  937. for (i = 0; i < 100; i++) {
  938. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  939. break;
  940. udelay(10);
  941. }
  942. }
  943. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  944. {
  945. u32 value = ~0x00;
  946. unsigned int i;
  947. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  948. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  949. for (i = 0; i < 100; i++) {
  950. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  951. value = RTL_R32(CSIDR);
  952. break;
  953. }
  954. udelay(10);
  955. }
  956. return value;
  957. }
  958. static
  959. void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
  960. {
  961. unsigned int i;
  962. BUG_ON((addr & 3) || (mask == 0));
  963. RTL_W32(ERIDR, val);
  964. RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
  965. for (i = 0; i < 100; i++) {
  966. if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
  967. break;
  968. udelay(100);
  969. }
  970. }
  971. static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
  972. {
  973. u32 value = ~0x00;
  974. unsigned int i;
  975. RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
  976. for (i = 0; i < 100; i++) {
  977. if (RTL_R32(ERIAR) & ERIAR_FLAG) {
  978. value = RTL_R32(ERIDR);
  979. break;
  980. }
  981. udelay(100);
  982. }
  983. return value;
  984. }
  985. static void
  986. rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
  987. {
  988. u32 val;
  989. val = rtl_eri_read(ioaddr, addr, type);
  990. rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
  991. }
  992. struct exgmac_reg {
  993. u16 addr;
  994. u16 mask;
  995. u32 val;
  996. };
  997. static void rtl_write_exgmac_batch(void __iomem *ioaddr,
  998. const struct exgmac_reg *r, int len)
  999. {
  1000. while (len-- > 0) {
  1001. rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
  1002. r++;
  1003. }
  1004. }
  1005. static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
  1006. {
  1007. u8 value = 0xff;
  1008. unsigned int i;
  1009. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  1010. for (i = 0; i < 300; i++) {
  1011. if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
  1012. value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
  1013. break;
  1014. }
  1015. udelay(100);
  1016. }
  1017. return value;
  1018. }
  1019. static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
  1020. {
  1021. void __iomem *ioaddr = tp->mmio_addr;
  1022. RTL_W16(IntrMask, 0x0000);
  1023. RTL_W16(IntrStatus, tp->intr_event);
  1024. RTL_R8(ChipCmd);
  1025. }
  1026. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  1027. {
  1028. void __iomem *ioaddr = tp->mmio_addr;
  1029. return RTL_R32(TBICSR) & TBIReset;
  1030. }
  1031. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  1032. {
  1033. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  1034. }
  1035. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  1036. {
  1037. return RTL_R32(TBICSR) & TBILinkOk;
  1038. }
  1039. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  1040. {
  1041. return RTL_R8(PHYstatus) & LinkStatus;
  1042. }
  1043. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  1044. {
  1045. void __iomem *ioaddr = tp->mmio_addr;
  1046. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  1047. }
  1048. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  1049. {
  1050. unsigned int val;
  1051. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  1052. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  1053. }
  1054. static void rtl_link_chg_patch(struct rtl8169_private *tp)
  1055. {
  1056. void __iomem *ioaddr = tp->mmio_addr;
  1057. struct net_device *dev = tp->dev;
  1058. if (!netif_running(dev))
  1059. return;
  1060. if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
  1061. if (RTL_R8(PHYstatus) & _1000bpsF) {
  1062. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1063. 0x00000011, ERIAR_EXGMAC);
  1064. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1065. 0x00000005, ERIAR_EXGMAC);
  1066. } else if (RTL_R8(PHYstatus) & _100bps) {
  1067. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1068. 0x0000001f, ERIAR_EXGMAC);
  1069. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1070. 0x00000005, ERIAR_EXGMAC);
  1071. } else {
  1072. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1073. 0x0000001f, ERIAR_EXGMAC);
  1074. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1075. 0x0000003f, ERIAR_EXGMAC);
  1076. }
  1077. /* Reset packet filter */
  1078. rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
  1079. ERIAR_EXGMAC);
  1080. rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
  1081. ERIAR_EXGMAC);
  1082. } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  1083. tp->mac_version == RTL_GIGA_MAC_VER_36) {
  1084. if (RTL_R8(PHYstatus) & _1000bpsF) {
  1085. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1086. 0x00000011, ERIAR_EXGMAC);
  1087. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1088. 0x00000005, ERIAR_EXGMAC);
  1089. } else {
  1090. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1091. 0x0000001f, ERIAR_EXGMAC);
  1092. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1093. 0x0000003f, ERIAR_EXGMAC);
  1094. }
  1095. }
  1096. }
  1097. static void __rtl8169_check_link_status(struct net_device *dev,
  1098. struct rtl8169_private *tp,
  1099. void __iomem *ioaddr, bool pm)
  1100. {
  1101. unsigned long flags;
  1102. spin_lock_irqsave(&tp->lock, flags);
  1103. if (tp->link_ok(ioaddr)) {
  1104. rtl_link_chg_patch(tp);
  1105. /* This is to cancel a scheduled suspend if there's one. */
  1106. if (pm)
  1107. pm_request_resume(&tp->pci_dev->dev);
  1108. netif_carrier_on(dev);
  1109. if (net_ratelimit())
  1110. netif_info(tp, ifup, dev, "link up\n");
  1111. } else {
  1112. netif_carrier_off(dev);
  1113. netif_info(tp, ifdown, dev, "link down\n");
  1114. if (pm)
  1115. pm_schedule_suspend(&tp->pci_dev->dev, 5000);
  1116. }
  1117. spin_unlock_irqrestore(&tp->lock, flags);
  1118. }
  1119. static void rtl8169_check_link_status(struct net_device *dev,
  1120. struct rtl8169_private *tp,
  1121. void __iomem *ioaddr)
  1122. {
  1123. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  1124. }
  1125. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1126. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  1127. {
  1128. void __iomem *ioaddr = tp->mmio_addr;
  1129. u8 options;
  1130. u32 wolopts = 0;
  1131. options = RTL_R8(Config1);
  1132. if (!(options & PMEnable))
  1133. return 0;
  1134. options = RTL_R8(Config3);
  1135. if (options & LinkUp)
  1136. wolopts |= WAKE_PHY;
  1137. if (options & MagicPacket)
  1138. wolopts |= WAKE_MAGIC;
  1139. options = RTL_R8(Config5);
  1140. if (options & UWF)
  1141. wolopts |= WAKE_UCAST;
  1142. if (options & BWF)
  1143. wolopts |= WAKE_BCAST;
  1144. if (options & MWF)
  1145. wolopts |= WAKE_MCAST;
  1146. return wolopts;
  1147. }
  1148. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1149. {
  1150. struct rtl8169_private *tp = netdev_priv(dev);
  1151. spin_lock_irq(&tp->lock);
  1152. wol->supported = WAKE_ANY;
  1153. wol->wolopts = __rtl8169_get_wol(tp);
  1154. spin_unlock_irq(&tp->lock);
  1155. }
  1156. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  1157. {
  1158. void __iomem *ioaddr = tp->mmio_addr;
  1159. unsigned int i;
  1160. static const struct {
  1161. u32 opt;
  1162. u16 reg;
  1163. u8 mask;
  1164. } cfg[] = {
  1165. { WAKE_ANY, Config1, PMEnable },
  1166. { WAKE_PHY, Config3, LinkUp },
  1167. { WAKE_MAGIC, Config3, MagicPacket },
  1168. { WAKE_UCAST, Config5, UWF },
  1169. { WAKE_BCAST, Config5, BWF },
  1170. { WAKE_MCAST, Config5, MWF },
  1171. { WAKE_ANY, Config5, LanWake }
  1172. };
  1173. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1174. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  1175. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  1176. if (wolopts & cfg[i].opt)
  1177. options |= cfg[i].mask;
  1178. RTL_W8(cfg[i].reg, options);
  1179. }
  1180. RTL_W8(Cfg9346, Cfg9346_Lock);
  1181. }
  1182. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1183. {
  1184. struct rtl8169_private *tp = netdev_priv(dev);
  1185. spin_lock_irq(&tp->lock);
  1186. if (wol->wolopts)
  1187. tp->features |= RTL_FEATURE_WOL;
  1188. else
  1189. tp->features &= ~RTL_FEATURE_WOL;
  1190. __rtl8169_set_wol(tp, wol->wolopts);
  1191. spin_unlock_irq(&tp->lock);
  1192. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  1193. return 0;
  1194. }
  1195. static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
  1196. {
  1197. return rtl_chip_infos[tp->mac_version].fw_name;
  1198. }
  1199. static void rtl8169_get_drvinfo(struct net_device *dev,
  1200. struct ethtool_drvinfo *info)
  1201. {
  1202. struct rtl8169_private *tp = netdev_priv(dev);
  1203. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1204. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  1205. strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
  1206. strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
  1207. BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
  1208. if (!IS_ERR_OR_NULL(rtl_fw))
  1209. strlcpy(info->fw_version, rtl_fw->version,
  1210. sizeof(info->fw_version));
  1211. }
  1212. static int rtl8169_get_regs_len(struct net_device *dev)
  1213. {
  1214. return R8169_REGS_SIZE;
  1215. }
  1216. static int rtl8169_set_speed_tbi(struct net_device *dev,
  1217. u8 autoneg, u16 speed, u8 duplex, u32 ignored)
  1218. {
  1219. struct rtl8169_private *tp = netdev_priv(dev);
  1220. void __iomem *ioaddr = tp->mmio_addr;
  1221. int ret = 0;
  1222. u32 reg;
  1223. reg = RTL_R32(TBICSR);
  1224. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  1225. (duplex == DUPLEX_FULL)) {
  1226. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  1227. } else if (autoneg == AUTONEG_ENABLE)
  1228. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  1229. else {
  1230. netif_warn(tp, link, dev,
  1231. "incorrect speed setting refused in TBI mode\n");
  1232. ret = -EOPNOTSUPP;
  1233. }
  1234. return ret;
  1235. }
  1236. static int rtl8169_set_speed_xmii(struct net_device *dev,
  1237. u8 autoneg, u16 speed, u8 duplex, u32 adv)
  1238. {
  1239. struct rtl8169_private *tp = netdev_priv(dev);
  1240. int giga_ctrl, bmcr;
  1241. int rc = -EINVAL;
  1242. rtl_writephy(tp, 0x1f, 0x0000);
  1243. if (autoneg == AUTONEG_ENABLE) {
  1244. int auto_nego;
  1245. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  1246. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  1247. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1248. if (adv & ADVERTISED_10baseT_Half)
  1249. auto_nego |= ADVERTISE_10HALF;
  1250. if (adv & ADVERTISED_10baseT_Full)
  1251. auto_nego |= ADVERTISE_10FULL;
  1252. if (adv & ADVERTISED_100baseT_Half)
  1253. auto_nego |= ADVERTISE_100HALF;
  1254. if (adv & ADVERTISED_100baseT_Full)
  1255. auto_nego |= ADVERTISE_100FULL;
  1256. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1257. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  1258. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  1259. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  1260. if (tp->mii.supports_gmii) {
  1261. if (adv & ADVERTISED_1000baseT_Half)
  1262. giga_ctrl |= ADVERTISE_1000HALF;
  1263. if (adv & ADVERTISED_1000baseT_Full)
  1264. giga_ctrl |= ADVERTISE_1000FULL;
  1265. } else if (adv & (ADVERTISED_1000baseT_Half |
  1266. ADVERTISED_1000baseT_Full)) {
  1267. netif_info(tp, link, dev,
  1268. "PHY does not support 1000Mbps\n");
  1269. goto out;
  1270. }
  1271. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1272. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  1273. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  1274. } else {
  1275. giga_ctrl = 0;
  1276. if (speed == SPEED_10)
  1277. bmcr = 0;
  1278. else if (speed == SPEED_100)
  1279. bmcr = BMCR_SPEED100;
  1280. else
  1281. goto out;
  1282. if (duplex == DUPLEX_FULL)
  1283. bmcr |= BMCR_FULLDPLX;
  1284. }
  1285. rtl_writephy(tp, MII_BMCR, bmcr);
  1286. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  1287. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  1288. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  1289. rtl_writephy(tp, 0x17, 0x2138);
  1290. rtl_writephy(tp, 0x0e, 0x0260);
  1291. } else {
  1292. rtl_writephy(tp, 0x17, 0x2108);
  1293. rtl_writephy(tp, 0x0e, 0x0000);
  1294. }
  1295. }
  1296. rc = 0;
  1297. out:
  1298. return rc;
  1299. }
  1300. static int rtl8169_set_speed(struct net_device *dev,
  1301. u8 autoneg, u16 speed, u8 duplex, u32 advertising)
  1302. {
  1303. struct rtl8169_private *tp = netdev_priv(dev);
  1304. int ret;
  1305. ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
  1306. if (ret < 0)
  1307. goto out;
  1308. if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
  1309. (advertising & ADVERTISED_1000baseT_Full)) {
  1310. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  1311. }
  1312. out:
  1313. return ret;
  1314. }
  1315. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1316. {
  1317. struct rtl8169_private *tp = netdev_priv(dev);
  1318. unsigned long flags;
  1319. int ret;
  1320. del_timer_sync(&tp->timer);
  1321. spin_lock_irqsave(&tp->lock, flags);
  1322. ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
  1323. cmd->duplex, cmd->advertising);
  1324. spin_unlock_irqrestore(&tp->lock, flags);
  1325. return ret;
  1326. }
  1327. static netdev_features_t rtl8169_fix_features(struct net_device *dev,
  1328. netdev_features_t features)
  1329. {
  1330. struct rtl8169_private *tp = netdev_priv(dev);
  1331. if (dev->mtu > TD_MSS_MAX)
  1332. features &= ~NETIF_F_ALL_TSO;
  1333. if (dev->mtu > JUMBO_1K &&
  1334. !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
  1335. features &= ~NETIF_F_IP_CSUM;
  1336. return features;
  1337. }
  1338. static int rtl8169_set_features(struct net_device *dev,
  1339. netdev_features_t features)
  1340. {
  1341. struct rtl8169_private *tp = netdev_priv(dev);
  1342. void __iomem *ioaddr = tp->mmio_addr;
  1343. unsigned long flags;
  1344. spin_lock_irqsave(&tp->lock, flags);
  1345. if (features & NETIF_F_RXCSUM)
  1346. tp->cp_cmd |= RxChkSum;
  1347. else
  1348. tp->cp_cmd &= ~RxChkSum;
  1349. if (dev->features & NETIF_F_HW_VLAN_RX)
  1350. tp->cp_cmd |= RxVlan;
  1351. else
  1352. tp->cp_cmd &= ~RxVlan;
  1353. RTL_W16(CPlusCmd, tp->cp_cmd);
  1354. RTL_R16(CPlusCmd);
  1355. spin_unlock_irqrestore(&tp->lock, flags);
  1356. return 0;
  1357. }
  1358. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  1359. struct sk_buff *skb)
  1360. {
  1361. return (vlan_tx_tag_present(skb)) ?
  1362. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  1363. }
  1364. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1365. {
  1366. u32 opts2 = le32_to_cpu(desc->opts2);
  1367. if (opts2 & RxVlanTag)
  1368. __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
  1369. desc->opts2 = 0;
  1370. }
  1371. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1372. {
  1373. struct rtl8169_private *tp = netdev_priv(dev);
  1374. void __iomem *ioaddr = tp->mmio_addr;
  1375. u32 status;
  1376. cmd->supported =
  1377. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1378. cmd->port = PORT_FIBRE;
  1379. cmd->transceiver = XCVR_INTERNAL;
  1380. status = RTL_R32(TBICSR);
  1381. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1382. cmd->autoneg = !!(status & TBINwEnable);
  1383. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1384. cmd->duplex = DUPLEX_FULL; /* Always set */
  1385. return 0;
  1386. }
  1387. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1388. {
  1389. struct rtl8169_private *tp = netdev_priv(dev);
  1390. return mii_ethtool_gset(&tp->mii, cmd);
  1391. }
  1392. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1393. {
  1394. struct rtl8169_private *tp = netdev_priv(dev);
  1395. unsigned long flags;
  1396. int rc;
  1397. spin_lock_irqsave(&tp->lock, flags);
  1398. rc = tp->get_settings(dev, cmd);
  1399. spin_unlock_irqrestore(&tp->lock, flags);
  1400. return rc;
  1401. }
  1402. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1403. void *p)
  1404. {
  1405. struct rtl8169_private *tp = netdev_priv(dev);
  1406. unsigned long flags;
  1407. if (regs->len > R8169_REGS_SIZE)
  1408. regs->len = R8169_REGS_SIZE;
  1409. spin_lock_irqsave(&tp->lock, flags);
  1410. memcpy_fromio(p, tp->mmio_addr, regs->len);
  1411. spin_unlock_irqrestore(&tp->lock, flags);
  1412. }
  1413. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1414. {
  1415. struct rtl8169_private *tp = netdev_priv(dev);
  1416. return tp->msg_enable;
  1417. }
  1418. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1419. {
  1420. struct rtl8169_private *tp = netdev_priv(dev);
  1421. tp->msg_enable = value;
  1422. }
  1423. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1424. "tx_packets",
  1425. "rx_packets",
  1426. "tx_errors",
  1427. "rx_errors",
  1428. "rx_missed",
  1429. "align_errors",
  1430. "tx_single_collisions",
  1431. "tx_multi_collisions",
  1432. "unicast",
  1433. "broadcast",
  1434. "multicast",
  1435. "tx_aborted",
  1436. "tx_underrun",
  1437. };
  1438. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1439. {
  1440. switch (sset) {
  1441. case ETH_SS_STATS:
  1442. return ARRAY_SIZE(rtl8169_gstrings);
  1443. default:
  1444. return -EOPNOTSUPP;
  1445. }
  1446. }
  1447. static void rtl8169_update_counters(struct net_device *dev)
  1448. {
  1449. struct rtl8169_private *tp = netdev_priv(dev);
  1450. void __iomem *ioaddr = tp->mmio_addr;
  1451. struct device *d = &tp->pci_dev->dev;
  1452. struct rtl8169_counters *counters;
  1453. dma_addr_t paddr;
  1454. u32 cmd;
  1455. int wait = 1000;
  1456. /*
  1457. * Some chips are unable to dump tally counters when the receiver
  1458. * is disabled.
  1459. */
  1460. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1461. return;
  1462. counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
  1463. if (!counters)
  1464. return;
  1465. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1466. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1467. RTL_W32(CounterAddrLow, cmd);
  1468. RTL_W32(CounterAddrLow, cmd | CounterDump);
  1469. while (wait--) {
  1470. if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
  1471. memcpy(&tp->counters, counters, sizeof(*counters));
  1472. break;
  1473. }
  1474. udelay(10);
  1475. }
  1476. RTL_W32(CounterAddrLow, 0);
  1477. RTL_W32(CounterAddrHigh, 0);
  1478. dma_free_coherent(d, sizeof(*counters), counters, paddr);
  1479. }
  1480. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1481. struct ethtool_stats *stats, u64 *data)
  1482. {
  1483. struct rtl8169_private *tp = netdev_priv(dev);
  1484. ASSERT_RTNL();
  1485. rtl8169_update_counters(dev);
  1486. data[0] = le64_to_cpu(tp->counters.tx_packets);
  1487. data[1] = le64_to_cpu(tp->counters.rx_packets);
  1488. data[2] = le64_to_cpu(tp->counters.tx_errors);
  1489. data[3] = le32_to_cpu(tp->counters.rx_errors);
  1490. data[4] = le16_to_cpu(tp->counters.rx_missed);
  1491. data[5] = le16_to_cpu(tp->counters.align_errors);
  1492. data[6] = le32_to_cpu(tp->counters.tx_one_collision);
  1493. data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
  1494. data[8] = le64_to_cpu(tp->counters.rx_unicast);
  1495. data[9] = le64_to_cpu(tp->counters.rx_broadcast);
  1496. data[10] = le32_to_cpu(tp->counters.rx_multicast);
  1497. data[11] = le16_to_cpu(tp->counters.tx_aborted);
  1498. data[12] = le16_to_cpu(tp->counters.tx_underun);
  1499. }
  1500. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1501. {
  1502. switch(stringset) {
  1503. case ETH_SS_STATS:
  1504. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1505. break;
  1506. }
  1507. }
  1508. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1509. .get_drvinfo = rtl8169_get_drvinfo,
  1510. .get_regs_len = rtl8169_get_regs_len,
  1511. .get_link = ethtool_op_get_link,
  1512. .get_settings = rtl8169_get_settings,
  1513. .set_settings = rtl8169_set_settings,
  1514. .get_msglevel = rtl8169_get_msglevel,
  1515. .set_msglevel = rtl8169_set_msglevel,
  1516. .get_regs = rtl8169_get_regs,
  1517. .get_wol = rtl8169_get_wol,
  1518. .set_wol = rtl8169_set_wol,
  1519. .get_strings = rtl8169_get_strings,
  1520. .get_sset_count = rtl8169_get_sset_count,
  1521. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1522. };
  1523. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1524. struct net_device *dev, u8 default_version)
  1525. {
  1526. void __iomem *ioaddr = tp->mmio_addr;
  1527. /*
  1528. * The driver currently handles the 8168Bf and the 8168Be identically
  1529. * but they can be identified more specifically through the test below
  1530. * if needed:
  1531. *
  1532. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1533. *
  1534. * Same thing for the 8101Eb and the 8101Ec:
  1535. *
  1536. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1537. */
  1538. static const struct rtl_mac_info {
  1539. u32 mask;
  1540. u32 val;
  1541. int mac_version;
  1542. } mac_info[] = {
  1543. /* 8168F family. */
  1544. { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
  1545. { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
  1546. /* 8168E family. */
  1547. { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
  1548. { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
  1549. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  1550. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  1551. /* 8168D family. */
  1552. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  1553. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1554. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1555. /* 8168DP family. */
  1556. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1557. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  1558. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  1559. /* 8168C family. */
  1560. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  1561. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1562. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1563. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1564. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1565. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1566. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1567. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1568. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1569. /* 8168B family. */
  1570. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1571. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1572. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1573. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1574. /* 8101 family. */
  1575. { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
  1576. { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
  1577. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  1578. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  1579. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1580. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1581. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1582. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1583. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1584. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1585. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1586. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1587. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1588. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1589. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1590. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1591. /* FIXME: where did these entries come from ? -- FR */
  1592. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1593. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1594. /* 8110 family. */
  1595. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1596. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1597. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1598. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1599. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1600. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1601. /* Catch-all */
  1602. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1603. };
  1604. const struct rtl_mac_info *p = mac_info;
  1605. u32 reg;
  1606. reg = RTL_R32(TxConfig);
  1607. while ((reg & p->mask) != p->val)
  1608. p++;
  1609. tp->mac_version = p->mac_version;
  1610. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  1611. netif_notice(tp, probe, dev,
  1612. "unknown MAC, using family default\n");
  1613. tp->mac_version = default_version;
  1614. }
  1615. }
  1616. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1617. {
  1618. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1619. }
  1620. struct phy_reg {
  1621. u16 reg;
  1622. u16 val;
  1623. };
  1624. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1625. const struct phy_reg *regs, int len)
  1626. {
  1627. while (len-- > 0) {
  1628. rtl_writephy(tp, regs->reg, regs->val);
  1629. regs++;
  1630. }
  1631. }
  1632. #define PHY_READ 0x00000000
  1633. #define PHY_DATA_OR 0x10000000
  1634. #define PHY_DATA_AND 0x20000000
  1635. #define PHY_BJMPN 0x30000000
  1636. #define PHY_READ_EFUSE 0x40000000
  1637. #define PHY_READ_MAC_BYTE 0x50000000
  1638. #define PHY_WRITE_MAC_BYTE 0x60000000
  1639. #define PHY_CLEAR_READCOUNT 0x70000000
  1640. #define PHY_WRITE 0x80000000
  1641. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1642. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1643. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1644. #define PHY_WRITE_PREVIOUS 0xc0000000
  1645. #define PHY_SKIPN 0xd0000000
  1646. #define PHY_DELAY_MS 0xe0000000
  1647. #define PHY_WRITE_ERI_WORD 0xf0000000
  1648. struct fw_info {
  1649. u32 magic;
  1650. char version[RTL_VER_SIZE];
  1651. __le32 fw_start;
  1652. __le32 fw_len;
  1653. u8 chksum;
  1654. } __packed;
  1655. #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
  1656. static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1657. {
  1658. const struct firmware *fw = rtl_fw->fw;
  1659. struct fw_info *fw_info = (struct fw_info *)fw->data;
  1660. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  1661. char *version = rtl_fw->version;
  1662. bool rc = false;
  1663. if (fw->size < FW_OPCODE_SIZE)
  1664. goto out;
  1665. if (!fw_info->magic) {
  1666. size_t i, size, start;
  1667. u8 checksum = 0;
  1668. if (fw->size < sizeof(*fw_info))
  1669. goto out;
  1670. for (i = 0; i < fw->size; i++)
  1671. checksum += fw->data[i];
  1672. if (checksum != 0)
  1673. goto out;
  1674. start = le32_to_cpu(fw_info->fw_start);
  1675. if (start > fw->size)
  1676. goto out;
  1677. size = le32_to_cpu(fw_info->fw_len);
  1678. if (size > (fw->size - start) / FW_OPCODE_SIZE)
  1679. goto out;
  1680. memcpy(version, fw_info->version, RTL_VER_SIZE);
  1681. pa->code = (__le32 *)(fw->data + start);
  1682. pa->size = size;
  1683. } else {
  1684. if (fw->size % FW_OPCODE_SIZE)
  1685. goto out;
  1686. strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
  1687. pa->code = (__le32 *)fw->data;
  1688. pa->size = fw->size / FW_OPCODE_SIZE;
  1689. }
  1690. version[RTL_VER_SIZE - 1] = 0;
  1691. rc = true;
  1692. out:
  1693. return rc;
  1694. }
  1695. static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
  1696. struct rtl_fw_phy_action *pa)
  1697. {
  1698. bool rc = false;
  1699. size_t index;
  1700. for (index = 0; index < pa->size; index++) {
  1701. u32 action = le32_to_cpu(pa->code[index]);
  1702. u32 regno = (action & 0x0fff0000) >> 16;
  1703. switch(action & 0xf0000000) {
  1704. case PHY_READ:
  1705. case PHY_DATA_OR:
  1706. case PHY_DATA_AND:
  1707. case PHY_READ_EFUSE:
  1708. case PHY_CLEAR_READCOUNT:
  1709. case PHY_WRITE:
  1710. case PHY_WRITE_PREVIOUS:
  1711. case PHY_DELAY_MS:
  1712. break;
  1713. case PHY_BJMPN:
  1714. if (regno > index) {
  1715. netif_err(tp, ifup, tp->dev,
  1716. "Out of range of firmware\n");
  1717. goto out;
  1718. }
  1719. break;
  1720. case PHY_READCOUNT_EQ_SKIP:
  1721. if (index + 2 >= pa->size) {
  1722. netif_err(tp, ifup, tp->dev,
  1723. "Out of range of firmware\n");
  1724. goto out;
  1725. }
  1726. break;
  1727. case PHY_COMP_EQ_SKIPN:
  1728. case PHY_COMP_NEQ_SKIPN:
  1729. case PHY_SKIPN:
  1730. if (index + 1 + regno >= pa->size) {
  1731. netif_err(tp, ifup, tp->dev,
  1732. "Out of range of firmware\n");
  1733. goto out;
  1734. }
  1735. break;
  1736. case PHY_READ_MAC_BYTE:
  1737. case PHY_WRITE_MAC_BYTE:
  1738. case PHY_WRITE_ERI_WORD:
  1739. default:
  1740. netif_err(tp, ifup, tp->dev,
  1741. "Invalid action 0x%08x\n", action);
  1742. goto out;
  1743. }
  1744. }
  1745. rc = true;
  1746. out:
  1747. return rc;
  1748. }
  1749. static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1750. {
  1751. struct net_device *dev = tp->dev;
  1752. int rc = -EINVAL;
  1753. if (!rtl_fw_format_ok(tp, rtl_fw)) {
  1754. netif_err(tp, ifup, dev, "invalid firwmare\n");
  1755. goto out;
  1756. }
  1757. if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
  1758. rc = 0;
  1759. out:
  1760. return rc;
  1761. }
  1762. static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1763. {
  1764. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  1765. u32 predata, count;
  1766. size_t index;
  1767. predata = count = 0;
  1768. for (index = 0; index < pa->size; ) {
  1769. u32 action = le32_to_cpu(pa->code[index]);
  1770. u32 data = action & 0x0000ffff;
  1771. u32 regno = (action & 0x0fff0000) >> 16;
  1772. if (!action)
  1773. break;
  1774. switch(action & 0xf0000000) {
  1775. case PHY_READ:
  1776. predata = rtl_readphy(tp, regno);
  1777. count++;
  1778. index++;
  1779. break;
  1780. case PHY_DATA_OR:
  1781. predata |= data;
  1782. index++;
  1783. break;
  1784. case PHY_DATA_AND:
  1785. predata &= data;
  1786. index++;
  1787. break;
  1788. case PHY_BJMPN:
  1789. index -= regno;
  1790. break;
  1791. case PHY_READ_EFUSE:
  1792. predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
  1793. index++;
  1794. break;
  1795. case PHY_CLEAR_READCOUNT:
  1796. count = 0;
  1797. index++;
  1798. break;
  1799. case PHY_WRITE:
  1800. rtl_writephy(tp, regno, data);
  1801. index++;
  1802. break;
  1803. case PHY_READCOUNT_EQ_SKIP:
  1804. index += (count == data) ? 2 : 1;
  1805. break;
  1806. case PHY_COMP_EQ_SKIPN:
  1807. if (predata == data)
  1808. index += regno;
  1809. index++;
  1810. break;
  1811. case PHY_COMP_NEQ_SKIPN:
  1812. if (predata != data)
  1813. index += regno;
  1814. index++;
  1815. break;
  1816. case PHY_WRITE_PREVIOUS:
  1817. rtl_writephy(tp, regno, predata);
  1818. index++;
  1819. break;
  1820. case PHY_SKIPN:
  1821. index += regno + 1;
  1822. break;
  1823. case PHY_DELAY_MS:
  1824. mdelay(data);
  1825. index++;
  1826. break;
  1827. case PHY_READ_MAC_BYTE:
  1828. case PHY_WRITE_MAC_BYTE:
  1829. case PHY_WRITE_ERI_WORD:
  1830. default:
  1831. BUG();
  1832. }
  1833. }
  1834. }
  1835. static void rtl_release_firmware(struct rtl8169_private *tp)
  1836. {
  1837. if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
  1838. release_firmware(tp->rtl_fw->fw);
  1839. kfree(tp->rtl_fw);
  1840. }
  1841. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  1842. }
  1843. static void rtl_apply_firmware(struct rtl8169_private *tp)
  1844. {
  1845. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1846. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  1847. if (!IS_ERR_OR_NULL(rtl_fw))
  1848. rtl_phy_write_fw(tp, rtl_fw);
  1849. }
  1850. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  1851. {
  1852. if (rtl_readphy(tp, reg) != val)
  1853. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  1854. else
  1855. rtl_apply_firmware(tp);
  1856. }
  1857. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  1858. {
  1859. static const struct phy_reg phy_reg_init[] = {
  1860. { 0x1f, 0x0001 },
  1861. { 0x06, 0x006e },
  1862. { 0x08, 0x0708 },
  1863. { 0x15, 0x4000 },
  1864. { 0x18, 0x65c7 },
  1865. { 0x1f, 0x0001 },
  1866. { 0x03, 0x00a1 },
  1867. { 0x02, 0x0008 },
  1868. { 0x01, 0x0120 },
  1869. { 0x00, 0x1000 },
  1870. { 0x04, 0x0800 },
  1871. { 0x04, 0x0000 },
  1872. { 0x03, 0xff41 },
  1873. { 0x02, 0xdf60 },
  1874. { 0x01, 0x0140 },
  1875. { 0x00, 0x0077 },
  1876. { 0x04, 0x7800 },
  1877. { 0x04, 0x7000 },
  1878. { 0x03, 0x802f },
  1879. { 0x02, 0x4f02 },
  1880. { 0x01, 0x0409 },
  1881. { 0x00, 0xf0f9 },
  1882. { 0x04, 0x9800 },
  1883. { 0x04, 0x9000 },
  1884. { 0x03, 0xdf01 },
  1885. { 0x02, 0xdf20 },
  1886. { 0x01, 0xff95 },
  1887. { 0x00, 0xba00 },
  1888. { 0x04, 0xa800 },
  1889. { 0x04, 0xa000 },
  1890. { 0x03, 0xff41 },
  1891. { 0x02, 0xdf20 },
  1892. { 0x01, 0x0140 },
  1893. { 0x00, 0x00bb },
  1894. { 0x04, 0xb800 },
  1895. { 0x04, 0xb000 },
  1896. { 0x03, 0xdf41 },
  1897. { 0x02, 0xdc60 },
  1898. { 0x01, 0x6340 },
  1899. { 0x00, 0x007d },
  1900. { 0x04, 0xd800 },
  1901. { 0x04, 0xd000 },
  1902. { 0x03, 0xdf01 },
  1903. { 0x02, 0xdf20 },
  1904. { 0x01, 0x100a },
  1905. { 0x00, 0xa0ff },
  1906. { 0x04, 0xf800 },
  1907. { 0x04, 0xf000 },
  1908. { 0x1f, 0x0000 },
  1909. { 0x0b, 0x0000 },
  1910. { 0x00, 0x9200 }
  1911. };
  1912. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1913. }
  1914. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  1915. {
  1916. static const struct phy_reg phy_reg_init[] = {
  1917. { 0x1f, 0x0002 },
  1918. { 0x01, 0x90d0 },
  1919. { 0x1f, 0x0000 }
  1920. };
  1921. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1922. }
  1923. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  1924. {
  1925. struct pci_dev *pdev = tp->pci_dev;
  1926. if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
  1927. (pdev->subsystem_device != 0xe000))
  1928. return;
  1929. rtl_writephy(tp, 0x1f, 0x0001);
  1930. rtl_writephy(tp, 0x10, 0xf01b);
  1931. rtl_writephy(tp, 0x1f, 0x0000);
  1932. }
  1933. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  1934. {
  1935. static const struct phy_reg phy_reg_init[] = {
  1936. { 0x1f, 0x0001 },
  1937. { 0x04, 0x0000 },
  1938. { 0x03, 0x00a1 },
  1939. { 0x02, 0x0008 },
  1940. { 0x01, 0x0120 },
  1941. { 0x00, 0x1000 },
  1942. { 0x04, 0x0800 },
  1943. { 0x04, 0x9000 },
  1944. { 0x03, 0x802f },
  1945. { 0x02, 0x4f02 },
  1946. { 0x01, 0x0409 },
  1947. { 0x00, 0xf099 },
  1948. { 0x04, 0x9800 },
  1949. { 0x04, 0xa000 },
  1950. { 0x03, 0xdf01 },
  1951. { 0x02, 0xdf20 },
  1952. { 0x01, 0xff95 },
  1953. { 0x00, 0xba00 },
  1954. { 0x04, 0xa800 },
  1955. { 0x04, 0xf000 },
  1956. { 0x03, 0xdf01 },
  1957. { 0x02, 0xdf20 },
  1958. { 0x01, 0x101a },
  1959. { 0x00, 0xa0ff },
  1960. { 0x04, 0xf800 },
  1961. { 0x04, 0x0000 },
  1962. { 0x1f, 0x0000 },
  1963. { 0x1f, 0x0001 },
  1964. { 0x10, 0xf41b },
  1965. { 0x14, 0xfb54 },
  1966. { 0x18, 0xf5c7 },
  1967. { 0x1f, 0x0000 },
  1968. { 0x1f, 0x0001 },
  1969. { 0x17, 0x0cc0 },
  1970. { 0x1f, 0x0000 }
  1971. };
  1972. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1973. rtl8169scd_hw_phy_config_quirk(tp);
  1974. }
  1975. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  1976. {
  1977. static const struct phy_reg phy_reg_init[] = {
  1978. { 0x1f, 0x0001 },
  1979. { 0x04, 0x0000 },
  1980. { 0x03, 0x00a1 },
  1981. { 0x02, 0x0008 },
  1982. { 0x01, 0x0120 },
  1983. { 0x00, 0x1000 },
  1984. { 0x04, 0x0800 },
  1985. { 0x04, 0x9000 },
  1986. { 0x03, 0x802f },
  1987. { 0x02, 0x4f02 },
  1988. { 0x01, 0x0409 },
  1989. { 0x00, 0xf099 },
  1990. { 0x04, 0x9800 },
  1991. { 0x04, 0xa000 },
  1992. { 0x03, 0xdf01 },
  1993. { 0x02, 0xdf20 },
  1994. { 0x01, 0xff95 },
  1995. { 0x00, 0xba00 },
  1996. { 0x04, 0xa800 },
  1997. { 0x04, 0xf000 },
  1998. { 0x03, 0xdf01 },
  1999. { 0x02, 0xdf20 },
  2000. { 0x01, 0x101a },
  2001. { 0x00, 0xa0ff },
  2002. { 0x04, 0xf800 },
  2003. { 0x04, 0x0000 },
  2004. { 0x1f, 0x0000 },
  2005. { 0x1f, 0x0001 },
  2006. { 0x0b, 0x8480 },
  2007. { 0x1f, 0x0000 },
  2008. { 0x1f, 0x0001 },
  2009. { 0x18, 0x67c7 },
  2010. { 0x04, 0x2000 },
  2011. { 0x03, 0x002f },
  2012. { 0x02, 0x4360 },
  2013. { 0x01, 0x0109 },
  2014. { 0x00, 0x3022 },
  2015. { 0x04, 0x2800 },
  2016. { 0x1f, 0x0000 },
  2017. { 0x1f, 0x0001 },
  2018. { 0x17, 0x0cc0 },
  2019. { 0x1f, 0x0000 }
  2020. };
  2021. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2022. }
  2023. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  2024. {
  2025. static const struct phy_reg phy_reg_init[] = {
  2026. { 0x10, 0xf41b },
  2027. { 0x1f, 0x0000 }
  2028. };
  2029. rtl_writephy(tp, 0x1f, 0x0001);
  2030. rtl_patchphy(tp, 0x16, 1 << 0);
  2031. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2032. }
  2033. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  2034. {
  2035. static const struct phy_reg phy_reg_init[] = {
  2036. { 0x1f, 0x0001 },
  2037. { 0x10, 0xf41b },
  2038. { 0x1f, 0x0000 }
  2039. };
  2040. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2041. }
  2042. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  2043. {
  2044. static const struct phy_reg phy_reg_init[] = {
  2045. { 0x1f, 0x0000 },
  2046. { 0x1d, 0x0f00 },
  2047. { 0x1f, 0x0002 },
  2048. { 0x0c, 0x1ec8 },
  2049. { 0x1f, 0x0000 }
  2050. };
  2051. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2052. }
  2053. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  2054. {
  2055. static const struct phy_reg phy_reg_init[] = {
  2056. { 0x1f, 0x0001 },
  2057. { 0x1d, 0x3d98 },
  2058. { 0x1f, 0x0000 }
  2059. };
  2060. rtl_writephy(tp, 0x1f, 0x0000);
  2061. rtl_patchphy(tp, 0x14, 1 << 5);
  2062. rtl_patchphy(tp, 0x0d, 1 << 5);
  2063. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2064. }
  2065. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  2066. {
  2067. static const struct phy_reg phy_reg_init[] = {
  2068. { 0x1f, 0x0001 },
  2069. { 0x12, 0x2300 },
  2070. { 0x1f, 0x0002 },
  2071. { 0x00, 0x88d4 },
  2072. { 0x01, 0x82b1 },
  2073. { 0x03, 0x7002 },
  2074. { 0x08, 0x9e30 },
  2075. { 0x09, 0x01f0 },
  2076. { 0x0a, 0x5500 },
  2077. { 0x0c, 0x00c8 },
  2078. { 0x1f, 0x0003 },
  2079. { 0x12, 0xc096 },
  2080. { 0x16, 0x000a },
  2081. { 0x1f, 0x0000 },
  2082. { 0x1f, 0x0000 },
  2083. { 0x09, 0x2000 },
  2084. { 0x09, 0x0000 }
  2085. };
  2086. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2087. rtl_patchphy(tp, 0x14, 1 << 5);
  2088. rtl_patchphy(tp, 0x0d, 1 << 5);
  2089. rtl_writephy(tp, 0x1f, 0x0000);
  2090. }
  2091. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  2092. {
  2093. static const struct phy_reg phy_reg_init[] = {
  2094. { 0x1f, 0x0001 },
  2095. { 0x12, 0x2300 },
  2096. { 0x03, 0x802f },
  2097. { 0x02, 0x4f02 },
  2098. { 0x01, 0x0409 },
  2099. { 0x00, 0xf099 },
  2100. { 0x04, 0x9800 },
  2101. { 0x04, 0x9000 },
  2102. { 0x1d, 0x3d98 },
  2103. { 0x1f, 0x0002 },
  2104. { 0x0c, 0x7eb8 },
  2105. { 0x06, 0x0761 },
  2106. { 0x1f, 0x0003 },
  2107. { 0x16, 0x0f0a },
  2108. { 0x1f, 0x0000 }
  2109. };
  2110. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2111. rtl_patchphy(tp, 0x16, 1 << 0);
  2112. rtl_patchphy(tp, 0x14, 1 << 5);
  2113. rtl_patchphy(tp, 0x0d, 1 << 5);
  2114. rtl_writephy(tp, 0x1f, 0x0000);
  2115. }
  2116. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  2117. {
  2118. static const struct phy_reg phy_reg_init[] = {
  2119. { 0x1f, 0x0001 },
  2120. { 0x12, 0x2300 },
  2121. { 0x1d, 0x3d98 },
  2122. { 0x1f, 0x0002 },
  2123. { 0x0c, 0x7eb8 },
  2124. { 0x06, 0x5461 },
  2125. { 0x1f, 0x0003 },
  2126. { 0x16, 0x0f0a },
  2127. { 0x1f, 0x0000 }
  2128. };
  2129. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2130. rtl_patchphy(tp, 0x16, 1 << 0);
  2131. rtl_patchphy(tp, 0x14, 1 << 5);
  2132. rtl_patchphy(tp, 0x0d, 1 << 5);
  2133. rtl_writephy(tp, 0x1f, 0x0000);
  2134. }
  2135. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  2136. {
  2137. rtl8168c_3_hw_phy_config(tp);
  2138. }
  2139. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  2140. {
  2141. static const struct phy_reg phy_reg_init_0[] = {
  2142. /* Channel Estimation */
  2143. { 0x1f, 0x0001 },
  2144. { 0x06, 0x4064 },
  2145. { 0x07, 0x2863 },
  2146. { 0x08, 0x059c },
  2147. { 0x09, 0x26b4 },
  2148. { 0x0a, 0x6a19 },
  2149. { 0x0b, 0xdcc8 },
  2150. { 0x10, 0xf06d },
  2151. { 0x14, 0x7f68 },
  2152. { 0x18, 0x7fd9 },
  2153. { 0x1c, 0xf0ff },
  2154. { 0x1d, 0x3d9c },
  2155. { 0x1f, 0x0003 },
  2156. { 0x12, 0xf49f },
  2157. { 0x13, 0x070b },
  2158. { 0x1a, 0x05ad },
  2159. { 0x14, 0x94c0 },
  2160. /*
  2161. * Tx Error Issue
  2162. * Enhance line driver power
  2163. */
  2164. { 0x1f, 0x0002 },
  2165. { 0x06, 0x5561 },
  2166. { 0x1f, 0x0005 },
  2167. { 0x05, 0x8332 },
  2168. { 0x06, 0x5561 },
  2169. /*
  2170. * Can not link to 1Gbps with bad cable
  2171. * Decrease SNR threshold form 21.07dB to 19.04dB
  2172. */
  2173. { 0x1f, 0x0001 },
  2174. { 0x17, 0x0cc0 },
  2175. { 0x1f, 0x0000 },
  2176. { 0x0d, 0xf880 }
  2177. };
  2178. void __iomem *ioaddr = tp->mmio_addr;
  2179. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2180. /*
  2181. * Rx Error Issue
  2182. * Fine Tune Switching regulator parameter
  2183. */
  2184. rtl_writephy(tp, 0x1f, 0x0002);
  2185. rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
  2186. rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
  2187. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  2188. static const struct phy_reg phy_reg_init[] = {
  2189. { 0x1f, 0x0002 },
  2190. { 0x05, 0x669a },
  2191. { 0x1f, 0x0005 },
  2192. { 0x05, 0x8330 },
  2193. { 0x06, 0x669a },
  2194. { 0x1f, 0x0002 }
  2195. };
  2196. int val;
  2197. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2198. val = rtl_readphy(tp, 0x0d);
  2199. if ((val & 0x00ff) != 0x006c) {
  2200. static const u32 set[] = {
  2201. 0x0065, 0x0066, 0x0067, 0x0068,
  2202. 0x0069, 0x006a, 0x006b, 0x006c
  2203. };
  2204. int i;
  2205. rtl_writephy(tp, 0x1f, 0x0002);
  2206. val &= 0xff00;
  2207. for (i = 0; i < ARRAY_SIZE(set); i++)
  2208. rtl_writephy(tp, 0x0d, val | set[i]);
  2209. }
  2210. } else {
  2211. static const struct phy_reg phy_reg_init[] = {
  2212. { 0x1f, 0x0002 },
  2213. { 0x05, 0x6662 },
  2214. { 0x1f, 0x0005 },
  2215. { 0x05, 0x8330 },
  2216. { 0x06, 0x6662 }
  2217. };
  2218. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2219. }
  2220. /* RSET couple improve */
  2221. rtl_writephy(tp, 0x1f, 0x0002);
  2222. rtl_patchphy(tp, 0x0d, 0x0300);
  2223. rtl_patchphy(tp, 0x0f, 0x0010);
  2224. /* Fine tune PLL performance */
  2225. rtl_writephy(tp, 0x1f, 0x0002);
  2226. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  2227. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  2228. rtl_writephy(tp, 0x1f, 0x0005);
  2229. rtl_writephy(tp, 0x05, 0x001b);
  2230. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  2231. rtl_writephy(tp, 0x1f, 0x0000);
  2232. }
  2233. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  2234. {
  2235. static const struct phy_reg phy_reg_init_0[] = {
  2236. /* Channel Estimation */
  2237. { 0x1f, 0x0001 },
  2238. { 0x06, 0x4064 },
  2239. { 0x07, 0x2863 },
  2240. { 0x08, 0x059c },
  2241. { 0x09, 0x26b4 },
  2242. { 0x0a, 0x6a19 },
  2243. { 0x0b, 0xdcc8 },
  2244. { 0x10, 0xf06d },
  2245. { 0x14, 0x7f68 },
  2246. { 0x18, 0x7fd9 },
  2247. { 0x1c, 0xf0ff },
  2248. { 0x1d, 0x3d9c },
  2249. { 0x1f, 0x0003 },
  2250. { 0x12, 0xf49f },
  2251. { 0x13, 0x070b },
  2252. { 0x1a, 0x05ad },
  2253. { 0x14, 0x94c0 },
  2254. /*
  2255. * Tx Error Issue
  2256. * Enhance line driver power
  2257. */
  2258. { 0x1f, 0x0002 },
  2259. { 0x06, 0x5561 },
  2260. { 0x1f, 0x0005 },
  2261. { 0x05, 0x8332 },
  2262. { 0x06, 0x5561 },
  2263. /*
  2264. * Can not link to 1Gbps with bad cable
  2265. * Decrease SNR threshold form 21.07dB to 19.04dB
  2266. */
  2267. { 0x1f, 0x0001 },
  2268. { 0x17, 0x0cc0 },
  2269. { 0x1f, 0x0000 },
  2270. { 0x0d, 0xf880 }
  2271. };
  2272. void __iomem *ioaddr = tp->mmio_addr;
  2273. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2274. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  2275. static const struct phy_reg phy_reg_init[] = {
  2276. { 0x1f, 0x0002 },
  2277. { 0x05, 0x669a },
  2278. { 0x1f, 0x0005 },
  2279. { 0x05, 0x8330 },
  2280. { 0x06, 0x669a },
  2281. { 0x1f, 0x0002 }
  2282. };
  2283. int val;
  2284. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2285. val = rtl_readphy(tp, 0x0d);
  2286. if ((val & 0x00ff) != 0x006c) {
  2287. static const u32 set[] = {
  2288. 0x0065, 0x0066, 0x0067, 0x0068,
  2289. 0x0069, 0x006a, 0x006b, 0x006c
  2290. };
  2291. int i;
  2292. rtl_writephy(tp, 0x1f, 0x0002);
  2293. val &= 0xff00;
  2294. for (i = 0; i < ARRAY_SIZE(set); i++)
  2295. rtl_writephy(tp, 0x0d, val | set[i]);
  2296. }
  2297. } else {
  2298. static const struct phy_reg phy_reg_init[] = {
  2299. { 0x1f, 0x0002 },
  2300. { 0x05, 0x2642 },
  2301. { 0x1f, 0x0005 },
  2302. { 0x05, 0x8330 },
  2303. { 0x06, 0x2642 }
  2304. };
  2305. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2306. }
  2307. /* Fine tune PLL performance */
  2308. rtl_writephy(tp, 0x1f, 0x0002);
  2309. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  2310. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  2311. /* Switching regulator Slew rate */
  2312. rtl_writephy(tp, 0x1f, 0x0002);
  2313. rtl_patchphy(tp, 0x0f, 0x0017);
  2314. rtl_writephy(tp, 0x1f, 0x0005);
  2315. rtl_writephy(tp, 0x05, 0x001b);
  2316. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2317. rtl_writephy(tp, 0x1f, 0x0000);
  2318. }
  2319. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2320. {
  2321. static const struct phy_reg phy_reg_init[] = {
  2322. { 0x1f, 0x0002 },
  2323. { 0x10, 0x0008 },
  2324. { 0x0d, 0x006c },
  2325. { 0x1f, 0x0000 },
  2326. { 0x0d, 0xf880 },
  2327. { 0x1f, 0x0001 },
  2328. { 0x17, 0x0cc0 },
  2329. { 0x1f, 0x0001 },
  2330. { 0x0b, 0xa4d8 },
  2331. { 0x09, 0x281c },
  2332. { 0x07, 0x2883 },
  2333. { 0x0a, 0x6b35 },
  2334. { 0x1d, 0x3da4 },
  2335. { 0x1c, 0xeffd },
  2336. { 0x14, 0x7f52 },
  2337. { 0x18, 0x7fc6 },
  2338. { 0x08, 0x0601 },
  2339. { 0x06, 0x4063 },
  2340. { 0x10, 0xf074 },
  2341. { 0x1f, 0x0003 },
  2342. { 0x13, 0x0789 },
  2343. { 0x12, 0xf4bd },
  2344. { 0x1a, 0x04fd },
  2345. { 0x14, 0x84b0 },
  2346. { 0x1f, 0x0000 },
  2347. { 0x00, 0x9200 },
  2348. { 0x1f, 0x0005 },
  2349. { 0x01, 0x0340 },
  2350. { 0x1f, 0x0001 },
  2351. { 0x04, 0x4000 },
  2352. { 0x03, 0x1d21 },
  2353. { 0x02, 0x0c32 },
  2354. { 0x01, 0x0200 },
  2355. { 0x00, 0x5554 },
  2356. { 0x04, 0x4800 },
  2357. { 0x04, 0x4000 },
  2358. { 0x04, 0xf000 },
  2359. { 0x03, 0xdf01 },
  2360. { 0x02, 0xdf20 },
  2361. { 0x01, 0x101a },
  2362. { 0x00, 0xa0ff },
  2363. { 0x04, 0xf800 },
  2364. { 0x04, 0xf000 },
  2365. { 0x1f, 0x0000 },
  2366. { 0x1f, 0x0007 },
  2367. { 0x1e, 0x0023 },
  2368. { 0x16, 0x0000 },
  2369. { 0x1f, 0x0000 }
  2370. };
  2371. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2372. }
  2373. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2374. {
  2375. static const struct phy_reg phy_reg_init[] = {
  2376. { 0x1f, 0x0001 },
  2377. { 0x17, 0x0cc0 },
  2378. { 0x1f, 0x0007 },
  2379. { 0x1e, 0x002d },
  2380. { 0x18, 0x0040 },
  2381. { 0x1f, 0x0000 }
  2382. };
  2383. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2384. rtl_patchphy(tp, 0x0d, 1 << 5);
  2385. }
  2386. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2387. {
  2388. static const struct phy_reg phy_reg_init[] = {
  2389. /* Enable Delay cap */
  2390. { 0x1f, 0x0005 },
  2391. { 0x05, 0x8b80 },
  2392. { 0x06, 0xc896 },
  2393. { 0x1f, 0x0000 },
  2394. /* Channel estimation fine tune */
  2395. { 0x1f, 0x0001 },
  2396. { 0x0b, 0x6c20 },
  2397. { 0x07, 0x2872 },
  2398. { 0x1c, 0xefff },
  2399. { 0x1f, 0x0003 },
  2400. { 0x14, 0x6420 },
  2401. { 0x1f, 0x0000 },
  2402. /* Update PFM & 10M TX idle timer */
  2403. { 0x1f, 0x0007 },
  2404. { 0x1e, 0x002f },
  2405. { 0x15, 0x1919 },
  2406. { 0x1f, 0x0000 },
  2407. { 0x1f, 0x0007 },
  2408. { 0x1e, 0x00ac },
  2409. { 0x18, 0x0006 },
  2410. { 0x1f, 0x0000 }
  2411. };
  2412. rtl_apply_firmware(tp);
  2413. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2414. /* DCO enable for 10M IDLE Power */
  2415. rtl_writephy(tp, 0x1f, 0x0007);
  2416. rtl_writephy(tp, 0x1e, 0x0023);
  2417. rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
  2418. rtl_writephy(tp, 0x1f, 0x0000);
  2419. /* For impedance matching */
  2420. rtl_writephy(tp, 0x1f, 0x0002);
  2421. rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
  2422. rtl_writephy(tp, 0x1f, 0x0000);
  2423. /* PHY auto speed down */
  2424. rtl_writephy(tp, 0x1f, 0x0007);
  2425. rtl_writephy(tp, 0x1e, 0x002d);
  2426. rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
  2427. rtl_writephy(tp, 0x1f, 0x0000);
  2428. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2429. rtl_writephy(tp, 0x1f, 0x0005);
  2430. rtl_writephy(tp, 0x05, 0x8b86);
  2431. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2432. rtl_writephy(tp, 0x1f, 0x0000);
  2433. rtl_writephy(tp, 0x1f, 0x0005);
  2434. rtl_writephy(tp, 0x05, 0x8b85);
  2435. rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
  2436. rtl_writephy(tp, 0x1f, 0x0007);
  2437. rtl_writephy(tp, 0x1e, 0x0020);
  2438. rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
  2439. rtl_writephy(tp, 0x1f, 0x0006);
  2440. rtl_writephy(tp, 0x00, 0x5a00);
  2441. rtl_writephy(tp, 0x1f, 0x0000);
  2442. rtl_writephy(tp, 0x0d, 0x0007);
  2443. rtl_writephy(tp, 0x0e, 0x003c);
  2444. rtl_writephy(tp, 0x0d, 0x4007);
  2445. rtl_writephy(tp, 0x0e, 0x0000);
  2446. rtl_writephy(tp, 0x0d, 0x0000);
  2447. }
  2448. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2449. {
  2450. static const struct phy_reg phy_reg_init[] = {
  2451. /* Enable Delay cap */
  2452. { 0x1f, 0x0004 },
  2453. { 0x1f, 0x0007 },
  2454. { 0x1e, 0x00ac },
  2455. { 0x18, 0x0006 },
  2456. { 0x1f, 0x0002 },
  2457. { 0x1f, 0x0000 },
  2458. { 0x1f, 0x0000 },
  2459. /* Channel estimation fine tune */
  2460. { 0x1f, 0x0003 },
  2461. { 0x09, 0xa20f },
  2462. { 0x1f, 0x0000 },
  2463. { 0x1f, 0x0000 },
  2464. /* Green Setting */
  2465. { 0x1f, 0x0005 },
  2466. { 0x05, 0x8b5b },
  2467. { 0x06, 0x9222 },
  2468. { 0x05, 0x8b6d },
  2469. { 0x06, 0x8000 },
  2470. { 0x05, 0x8b76 },
  2471. { 0x06, 0x8000 },
  2472. { 0x1f, 0x0000 }
  2473. };
  2474. rtl_apply_firmware(tp);
  2475. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2476. /* For 4-corner performance improve */
  2477. rtl_writephy(tp, 0x1f, 0x0005);
  2478. rtl_writephy(tp, 0x05, 0x8b80);
  2479. rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
  2480. rtl_writephy(tp, 0x1f, 0x0000);
  2481. /* PHY auto speed down */
  2482. rtl_writephy(tp, 0x1f, 0x0004);
  2483. rtl_writephy(tp, 0x1f, 0x0007);
  2484. rtl_writephy(tp, 0x1e, 0x002d);
  2485. rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
  2486. rtl_writephy(tp, 0x1f, 0x0002);
  2487. rtl_writephy(tp, 0x1f, 0x0000);
  2488. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2489. /* improve 10M EEE waveform */
  2490. rtl_writephy(tp, 0x1f, 0x0005);
  2491. rtl_writephy(tp, 0x05, 0x8b86);
  2492. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2493. rtl_writephy(tp, 0x1f, 0x0000);
  2494. /* Improve 2-pair detection performance */
  2495. rtl_writephy(tp, 0x1f, 0x0005);
  2496. rtl_writephy(tp, 0x05, 0x8b85);
  2497. rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
  2498. rtl_writephy(tp, 0x1f, 0x0000);
  2499. /* EEE setting */
  2500. rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
  2501. ERIAR_EXGMAC);
  2502. rtl_writephy(tp, 0x1f, 0x0005);
  2503. rtl_writephy(tp, 0x05, 0x8b85);
  2504. rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
  2505. rtl_writephy(tp, 0x1f, 0x0004);
  2506. rtl_writephy(tp, 0x1f, 0x0007);
  2507. rtl_writephy(tp, 0x1e, 0x0020);
  2508. rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
  2509. rtl_writephy(tp, 0x1f, 0x0002);
  2510. rtl_writephy(tp, 0x1f, 0x0000);
  2511. rtl_writephy(tp, 0x0d, 0x0007);
  2512. rtl_writephy(tp, 0x0e, 0x003c);
  2513. rtl_writephy(tp, 0x0d, 0x4007);
  2514. rtl_writephy(tp, 0x0e, 0x0000);
  2515. rtl_writephy(tp, 0x0d, 0x0000);
  2516. /* Green feature */
  2517. rtl_writephy(tp, 0x1f, 0x0003);
  2518. rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
  2519. rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
  2520. rtl_writephy(tp, 0x1f, 0x0000);
  2521. }
  2522. static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
  2523. {
  2524. static const struct phy_reg phy_reg_init[] = {
  2525. /* Channel estimation fine tune */
  2526. { 0x1f, 0x0003 },
  2527. { 0x09, 0xa20f },
  2528. { 0x1f, 0x0000 },
  2529. /* Modify green table for giga & fnet */
  2530. { 0x1f, 0x0005 },
  2531. { 0x05, 0x8b55 },
  2532. { 0x06, 0x0000 },
  2533. { 0x05, 0x8b5e },
  2534. { 0x06, 0x0000 },
  2535. { 0x05, 0x8b67 },
  2536. { 0x06, 0x0000 },
  2537. { 0x05, 0x8b70 },
  2538. { 0x06, 0x0000 },
  2539. { 0x1f, 0x0000 },
  2540. { 0x1f, 0x0007 },
  2541. { 0x1e, 0x0078 },
  2542. { 0x17, 0x0000 },
  2543. { 0x19, 0x00fb },
  2544. { 0x1f, 0x0000 },
  2545. /* Modify green table for 10M */
  2546. { 0x1f, 0x0005 },
  2547. { 0x05, 0x8b79 },
  2548. { 0x06, 0xaa00 },
  2549. { 0x1f, 0x0000 },
  2550. /* Disable hiimpedance detection (RTCT) */
  2551. { 0x1f, 0x0003 },
  2552. { 0x01, 0x328a },
  2553. { 0x1f, 0x0000 }
  2554. };
  2555. rtl_apply_firmware(tp);
  2556. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2557. /* For 4-corner performance improve */
  2558. rtl_writephy(tp, 0x1f, 0x0005);
  2559. rtl_writephy(tp, 0x05, 0x8b80);
  2560. rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
  2561. rtl_writephy(tp, 0x1f, 0x0000);
  2562. /* PHY auto speed down */
  2563. rtl_writephy(tp, 0x1f, 0x0007);
  2564. rtl_writephy(tp, 0x1e, 0x002d);
  2565. rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
  2566. rtl_writephy(tp, 0x1f, 0x0000);
  2567. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2568. /* Improve 10M EEE waveform */
  2569. rtl_writephy(tp, 0x1f, 0x0005);
  2570. rtl_writephy(tp, 0x05, 0x8b86);
  2571. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2572. rtl_writephy(tp, 0x1f, 0x0000);
  2573. /* Improve 2-pair detection performance */
  2574. rtl_writephy(tp, 0x1f, 0x0005);
  2575. rtl_writephy(tp, 0x05, 0x8b85);
  2576. rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
  2577. rtl_writephy(tp, 0x1f, 0x0000);
  2578. }
  2579. static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
  2580. {
  2581. rtl_apply_firmware(tp);
  2582. /* For 4-corner performance improve */
  2583. rtl_writephy(tp, 0x1f, 0x0005);
  2584. rtl_writephy(tp, 0x05, 0x8b80);
  2585. rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
  2586. rtl_writephy(tp, 0x1f, 0x0000);
  2587. /* PHY auto speed down */
  2588. rtl_writephy(tp, 0x1f, 0x0007);
  2589. rtl_writephy(tp, 0x1e, 0x002d);
  2590. rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
  2591. rtl_writephy(tp, 0x1f, 0x0000);
  2592. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2593. /* Improve 10M EEE waveform */
  2594. rtl_writephy(tp, 0x1f, 0x0005);
  2595. rtl_writephy(tp, 0x05, 0x8b86);
  2596. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2597. rtl_writephy(tp, 0x1f, 0x0000);
  2598. }
  2599. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  2600. {
  2601. static const struct phy_reg phy_reg_init[] = {
  2602. { 0x1f, 0x0003 },
  2603. { 0x08, 0x441d },
  2604. { 0x01, 0x9100 },
  2605. { 0x1f, 0x0000 }
  2606. };
  2607. rtl_writephy(tp, 0x1f, 0x0000);
  2608. rtl_patchphy(tp, 0x11, 1 << 12);
  2609. rtl_patchphy(tp, 0x19, 1 << 13);
  2610. rtl_patchphy(tp, 0x10, 1 << 15);
  2611. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2612. }
  2613. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  2614. {
  2615. static const struct phy_reg phy_reg_init[] = {
  2616. { 0x1f, 0x0005 },
  2617. { 0x1a, 0x0000 },
  2618. { 0x1f, 0x0000 },
  2619. { 0x1f, 0x0004 },
  2620. { 0x1c, 0x0000 },
  2621. { 0x1f, 0x0000 },
  2622. { 0x1f, 0x0001 },
  2623. { 0x15, 0x7701 },
  2624. { 0x1f, 0x0000 }
  2625. };
  2626. /* Disable ALDPS before ram code */
  2627. rtl_writephy(tp, 0x1f, 0x0000);
  2628. rtl_writephy(tp, 0x18, 0x0310);
  2629. msleep(100);
  2630. rtl_apply_firmware(tp);
  2631. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2632. }
  2633. static void rtl_hw_phy_config(struct net_device *dev)
  2634. {
  2635. struct rtl8169_private *tp = netdev_priv(dev);
  2636. rtl8169_print_mac_version(tp);
  2637. switch (tp->mac_version) {
  2638. case RTL_GIGA_MAC_VER_01:
  2639. break;
  2640. case RTL_GIGA_MAC_VER_02:
  2641. case RTL_GIGA_MAC_VER_03:
  2642. rtl8169s_hw_phy_config(tp);
  2643. break;
  2644. case RTL_GIGA_MAC_VER_04:
  2645. rtl8169sb_hw_phy_config(tp);
  2646. break;
  2647. case RTL_GIGA_MAC_VER_05:
  2648. rtl8169scd_hw_phy_config(tp);
  2649. break;
  2650. case RTL_GIGA_MAC_VER_06:
  2651. rtl8169sce_hw_phy_config(tp);
  2652. break;
  2653. case RTL_GIGA_MAC_VER_07:
  2654. case RTL_GIGA_MAC_VER_08:
  2655. case RTL_GIGA_MAC_VER_09:
  2656. rtl8102e_hw_phy_config(tp);
  2657. break;
  2658. case RTL_GIGA_MAC_VER_11:
  2659. rtl8168bb_hw_phy_config(tp);
  2660. break;
  2661. case RTL_GIGA_MAC_VER_12:
  2662. rtl8168bef_hw_phy_config(tp);
  2663. break;
  2664. case RTL_GIGA_MAC_VER_17:
  2665. rtl8168bef_hw_phy_config(tp);
  2666. break;
  2667. case RTL_GIGA_MAC_VER_18:
  2668. rtl8168cp_1_hw_phy_config(tp);
  2669. break;
  2670. case RTL_GIGA_MAC_VER_19:
  2671. rtl8168c_1_hw_phy_config(tp);
  2672. break;
  2673. case RTL_GIGA_MAC_VER_20:
  2674. rtl8168c_2_hw_phy_config(tp);
  2675. break;
  2676. case RTL_GIGA_MAC_VER_21:
  2677. rtl8168c_3_hw_phy_config(tp);
  2678. break;
  2679. case RTL_GIGA_MAC_VER_22:
  2680. rtl8168c_4_hw_phy_config(tp);
  2681. break;
  2682. case RTL_GIGA_MAC_VER_23:
  2683. case RTL_GIGA_MAC_VER_24:
  2684. rtl8168cp_2_hw_phy_config(tp);
  2685. break;
  2686. case RTL_GIGA_MAC_VER_25:
  2687. rtl8168d_1_hw_phy_config(tp);
  2688. break;
  2689. case RTL_GIGA_MAC_VER_26:
  2690. rtl8168d_2_hw_phy_config(tp);
  2691. break;
  2692. case RTL_GIGA_MAC_VER_27:
  2693. rtl8168d_3_hw_phy_config(tp);
  2694. break;
  2695. case RTL_GIGA_MAC_VER_28:
  2696. rtl8168d_4_hw_phy_config(tp);
  2697. break;
  2698. case RTL_GIGA_MAC_VER_29:
  2699. case RTL_GIGA_MAC_VER_30:
  2700. rtl8105e_hw_phy_config(tp);
  2701. break;
  2702. case RTL_GIGA_MAC_VER_31:
  2703. /* None. */
  2704. break;
  2705. case RTL_GIGA_MAC_VER_32:
  2706. case RTL_GIGA_MAC_VER_33:
  2707. rtl8168e_1_hw_phy_config(tp);
  2708. break;
  2709. case RTL_GIGA_MAC_VER_34:
  2710. rtl8168e_2_hw_phy_config(tp);
  2711. break;
  2712. case RTL_GIGA_MAC_VER_35:
  2713. rtl8168f_1_hw_phy_config(tp);
  2714. break;
  2715. case RTL_GIGA_MAC_VER_36:
  2716. rtl8168f_2_hw_phy_config(tp);
  2717. break;
  2718. default:
  2719. break;
  2720. }
  2721. }
  2722. static void rtl8169_phy_timer(unsigned long __opaque)
  2723. {
  2724. struct net_device *dev = (struct net_device *)__opaque;
  2725. struct rtl8169_private *tp = netdev_priv(dev);
  2726. struct timer_list *timer = &tp->timer;
  2727. void __iomem *ioaddr = tp->mmio_addr;
  2728. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  2729. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  2730. spin_lock_irq(&tp->lock);
  2731. if (tp->phy_reset_pending(tp)) {
  2732. /*
  2733. * A busy loop could burn quite a few cycles on nowadays CPU.
  2734. * Let's delay the execution of the timer for a few ticks.
  2735. */
  2736. timeout = HZ/10;
  2737. goto out_mod_timer;
  2738. }
  2739. if (tp->link_ok(ioaddr))
  2740. goto out_unlock;
  2741. netif_warn(tp, link, dev, "PHY reset until link up\n");
  2742. tp->phy_reset_enable(tp);
  2743. out_mod_timer:
  2744. mod_timer(timer, jiffies + timeout);
  2745. out_unlock:
  2746. spin_unlock_irq(&tp->lock);
  2747. }
  2748. #ifdef CONFIG_NET_POLL_CONTROLLER
  2749. /*
  2750. * Polling 'interrupt' - used by things like netconsole to send skbs
  2751. * without having to re-enable interrupts. It's not called while
  2752. * the interrupt routine is executing.
  2753. */
  2754. static void rtl8169_netpoll(struct net_device *dev)
  2755. {
  2756. struct rtl8169_private *tp = netdev_priv(dev);
  2757. struct pci_dev *pdev = tp->pci_dev;
  2758. disable_irq(pdev->irq);
  2759. rtl8169_interrupt(pdev->irq, dev);
  2760. enable_irq(pdev->irq);
  2761. }
  2762. #endif
  2763. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  2764. void __iomem *ioaddr)
  2765. {
  2766. iounmap(ioaddr);
  2767. pci_release_regions(pdev);
  2768. pci_clear_mwi(pdev);
  2769. pci_disable_device(pdev);
  2770. free_netdev(dev);
  2771. }
  2772. static void rtl8169_phy_reset(struct net_device *dev,
  2773. struct rtl8169_private *tp)
  2774. {
  2775. unsigned int i;
  2776. tp->phy_reset_enable(tp);
  2777. for (i = 0; i < 100; i++) {
  2778. if (!tp->phy_reset_pending(tp))
  2779. return;
  2780. msleep(1);
  2781. }
  2782. netif_err(tp, link, dev, "PHY reset failed\n");
  2783. }
  2784. static bool rtl_tbi_enabled(struct rtl8169_private *tp)
  2785. {
  2786. void __iomem *ioaddr = tp->mmio_addr;
  2787. return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
  2788. (RTL_R8(PHYstatus) & TBI_Enable);
  2789. }
  2790. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  2791. {
  2792. void __iomem *ioaddr = tp->mmio_addr;
  2793. rtl_hw_phy_config(dev);
  2794. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  2795. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2796. RTL_W8(0x82, 0x01);
  2797. }
  2798. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  2799. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  2800. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  2801. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  2802. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2803. RTL_W8(0x82, 0x01);
  2804. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  2805. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  2806. }
  2807. rtl8169_phy_reset(dev, tp);
  2808. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  2809. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  2810. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  2811. (tp->mii.supports_gmii ?
  2812. ADVERTISED_1000baseT_Half |
  2813. ADVERTISED_1000baseT_Full : 0));
  2814. if (rtl_tbi_enabled(tp))
  2815. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  2816. }
  2817. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  2818. {
  2819. void __iomem *ioaddr = tp->mmio_addr;
  2820. u32 high;
  2821. u32 low;
  2822. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  2823. high = addr[4] | (addr[5] << 8);
  2824. spin_lock_irq(&tp->lock);
  2825. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2826. RTL_W32(MAC4, high);
  2827. RTL_R32(MAC4);
  2828. RTL_W32(MAC0, low);
  2829. RTL_R32(MAC0);
  2830. if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
  2831. const struct exgmac_reg e[] = {
  2832. { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
  2833. { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
  2834. { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
  2835. { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
  2836. low >> 16 },
  2837. };
  2838. rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
  2839. }
  2840. RTL_W8(Cfg9346, Cfg9346_Lock);
  2841. spin_unlock_irq(&tp->lock);
  2842. }
  2843. static int rtl_set_mac_address(struct net_device *dev, void *p)
  2844. {
  2845. struct rtl8169_private *tp = netdev_priv(dev);
  2846. struct sockaddr *addr = p;
  2847. if (!is_valid_ether_addr(addr->sa_data))
  2848. return -EADDRNOTAVAIL;
  2849. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2850. rtl_rar_set(tp, dev->dev_addr);
  2851. return 0;
  2852. }
  2853. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2854. {
  2855. struct rtl8169_private *tp = netdev_priv(dev);
  2856. struct mii_ioctl_data *data = if_mii(ifr);
  2857. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  2858. }
  2859. static int rtl_xmii_ioctl(struct rtl8169_private *tp,
  2860. struct mii_ioctl_data *data, int cmd)
  2861. {
  2862. switch (cmd) {
  2863. case SIOCGMIIPHY:
  2864. data->phy_id = 32; /* Internal PHY */
  2865. return 0;
  2866. case SIOCGMIIREG:
  2867. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  2868. return 0;
  2869. case SIOCSMIIREG:
  2870. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  2871. return 0;
  2872. }
  2873. return -EOPNOTSUPP;
  2874. }
  2875. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2876. {
  2877. return -EOPNOTSUPP;
  2878. }
  2879. static const struct rtl_cfg_info {
  2880. void (*hw_start)(struct net_device *);
  2881. unsigned int region;
  2882. unsigned int align;
  2883. u16 intr_event;
  2884. u16 napi_event;
  2885. unsigned features;
  2886. u8 default_ver;
  2887. } rtl_cfg_infos [] = {
  2888. [RTL_CFG_0] = {
  2889. .hw_start = rtl_hw_start_8169,
  2890. .region = 1,
  2891. .align = 0,
  2892. .intr_event = SYSErr | LinkChg | RxOverflow |
  2893. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2894. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2895. .features = RTL_FEATURE_GMII,
  2896. .default_ver = RTL_GIGA_MAC_VER_01,
  2897. },
  2898. [RTL_CFG_1] = {
  2899. .hw_start = rtl_hw_start_8168,
  2900. .region = 2,
  2901. .align = 8,
  2902. .intr_event = SYSErr | LinkChg | RxOverflow |
  2903. TxErr | TxOK | RxOK | RxErr,
  2904. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  2905. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  2906. .default_ver = RTL_GIGA_MAC_VER_11,
  2907. },
  2908. [RTL_CFG_2] = {
  2909. .hw_start = rtl_hw_start_8101,
  2910. .region = 2,
  2911. .align = 8,
  2912. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  2913. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2914. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2915. .features = RTL_FEATURE_MSI,
  2916. .default_ver = RTL_GIGA_MAC_VER_13,
  2917. }
  2918. };
  2919. /* Cfg9346_Unlock assumed. */
  2920. static unsigned rtl_try_msi(struct rtl8169_private *tp,
  2921. const struct rtl_cfg_info *cfg)
  2922. {
  2923. void __iomem *ioaddr = tp->mmio_addr;
  2924. unsigned msi = 0;
  2925. u8 cfg2;
  2926. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  2927. if (cfg->features & RTL_FEATURE_MSI) {
  2928. if (pci_enable_msi(tp->pci_dev)) {
  2929. netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
  2930. } else {
  2931. cfg2 |= MSIEnable;
  2932. msi = RTL_FEATURE_MSI;
  2933. }
  2934. }
  2935. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  2936. RTL_W8(Config2, cfg2);
  2937. return msi;
  2938. }
  2939. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  2940. {
  2941. if (tp->features & RTL_FEATURE_MSI) {
  2942. pci_disable_msi(pdev);
  2943. tp->features &= ~RTL_FEATURE_MSI;
  2944. }
  2945. }
  2946. static const struct net_device_ops rtl8169_netdev_ops = {
  2947. .ndo_open = rtl8169_open,
  2948. .ndo_stop = rtl8169_close,
  2949. .ndo_get_stats = rtl8169_get_stats,
  2950. .ndo_start_xmit = rtl8169_start_xmit,
  2951. .ndo_tx_timeout = rtl8169_tx_timeout,
  2952. .ndo_validate_addr = eth_validate_addr,
  2953. .ndo_change_mtu = rtl8169_change_mtu,
  2954. .ndo_fix_features = rtl8169_fix_features,
  2955. .ndo_set_features = rtl8169_set_features,
  2956. .ndo_set_mac_address = rtl_set_mac_address,
  2957. .ndo_do_ioctl = rtl8169_ioctl,
  2958. .ndo_set_rx_mode = rtl_set_rx_mode,
  2959. #ifdef CONFIG_NET_POLL_CONTROLLER
  2960. .ndo_poll_controller = rtl8169_netpoll,
  2961. #endif
  2962. };
  2963. static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
  2964. {
  2965. struct mdio_ops *ops = &tp->mdio_ops;
  2966. switch (tp->mac_version) {
  2967. case RTL_GIGA_MAC_VER_27:
  2968. ops->write = r8168dp_1_mdio_write;
  2969. ops->read = r8168dp_1_mdio_read;
  2970. break;
  2971. case RTL_GIGA_MAC_VER_28:
  2972. case RTL_GIGA_MAC_VER_31:
  2973. ops->write = r8168dp_2_mdio_write;
  2974. ops->read = r8168dp_2_mdio_read;
  2975. break;
  2976. default:
  2977. ops->write = r8169_mdio_write;
  2978. ops->read = r8169_mdio_read;
  2979. break;
  2980. }
  2981. }
  2982. static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
  2983. {
  2984. void __iomem *ioaddr = tp->mmio_addr;
  2985. switch (tp->mac_version) {
  2986. case RTL_GIGA_MAC_VER_29:
  2987. case RTL_GIGA_MAC_VER_30:
  2988. case RTL_GIGA_MAC_VER_32:
  2989. case RTL_GIGA_MAC_VER_33:
  2990. case RTL_GIGA_MAC_VER_34:
  2991. RTL_W32(RxConfig, RTL_R32(RxConfig) |
  2992. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  2993. break;
  2994. default:
  2995. break;
  2996. }
  2997. }
  2998. static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
  2999. {
  3000. if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
  3001. return false;
  3002. rtl_writephy(tp, 0x1f, 0x0000);
  3003. rtl_writephy(tp, MII_BMCR, 0x0000);
  3004. rtl_wol_suspend_quirk(tp);
  3005. return true;
  3006. }
  3007. static void r810x_phy_power_down(struct rtl8169_private *tp)
  3008. {
  3009. rtl_writephy(tp, 0x1f, 0x0000);
  3010. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  3011. }
  3012. static void r810x_phy_power_up(struct rtl8169_private *tp)
  3013. {
  3014. rtl_writephy(tp, 0x1f, 0x0000);
  3015. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  3016. }
  3017. static void r810x_pll_power_down(struct rtl8169_private *tp)
  3018. {
  3019. if (rtl_wol_pll_power_down(tp))
  3020. return;
  3021. r810x_phy_power_down(tp);
  3022. }
  3023. static void r810x_pll_power_up(struct rtl8169_private *tp)
  3024. {
  3025. r810x_phy_power_up(tp);
  3026. }
  3027. static void r8168_phy_power_up(struct rtl8169_private *tp)
  3028. {
  3029. rtl_writephy(tp, 0x1f, 0x0000);
  3030. switch (tp->mac_version) {
  3031. case RTL_GIGA_MAC_VER_11:
  3032. case RTL_GIGA_MAC_VER_12:
  3033. case RTL_GIGA_MAC_VER_17:
  3034. case RTL_GIGA_MAC_VER_18:
  3035. case RTL_GIGA_MAC_VER_19:
  3036. case RTL_GIGA_MAC_VER_20:
  3037. case RTL_GIGA_MAC_VER_21:
  3038. case RTL_GIGA_MAC_VER_22:
  3039. case RTL_GIGA_MAC_VER_23:
  3040. case RTL_GIGA_MAC_VER_24:
  3041. case RTL_GIGA_MAC_VER_25:
  3042. case RTL_GIGA_MAC_VER_26:
  3043. case RTL_GIGA_MAC_VER_27:
  3044. case RTL_GIGA_MAC_VER_28:
  3045. case RTL_GIGA_MAC_VER_31:
  3046. rtl_writephy(tp, 0x0e, 0x0000);
  3047. break;
  3048. default:
  3049. break;
  3050. }
  3051. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  3052. }
  3053. static void r8168_phy_power_down(struct rtl8169_private *tp)
  3054. {
  3055. rtl_writephy(tp, 0x1f, 0x0000);
  3056. switch (tp->mac_version) {
  3057. case RTL_GIGA_MAC_VER_32:
  3058. case RTL_GIGA_MAC_VER_33:
  3059. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
  3060. break;
  3061. case RTL_GIGA_MAC_VER_11:
  3062. case RTL_GIGA_MAC_VER_12:
  3063. case RTL_GIGA_MAC_VER_17:
  3064. case RTL_GIGA_MAC_VER_18:
  3065. case RTL_GIGA_MAC_VER_19:
  3066. case RTL_GIGA_MAC_VER_20:
  3067. case RTL_GIGA_MAC_VER_21:
  3068. case RTL_GIGA_MAC_VER_22:
  3069. case RTL_GIGA_MAC_VER_23:
  3070. case RTL_GIGA_MAC_VER_24:
  3071. case RTL_GIGA_MAC_VER_25:
  3072. case RTL_GIGA_MAC_VER_26:
  3073. case RTL_GIGA_MAC_VER_27:
  3074. case RTL_GIGA_MAC_VER_28:
  3075. case RTL_GIGA_MAC_VER_31:
  3076. rtl_writephy(tp, 0x0e, 0x0200);
  3077. default:
  3078. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  3079. break;
  3080. }
  3081. }
  3082. static void r8168_pll_power_down(struct rtl8169_private *tp)
  3083. {
  3084. void __iomem *ioaddr = tp->mmio_addr;
  3085. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3086. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3087. tp->mac_version == RTL_GIGA_MAC_VER_31) &&
  3088. r8168dp_check_dash(tp)) {
  3089. return;
  3090. }
  3091. if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
  3092. tp->mac_version == RTL_GIGA_MAC_VER_24) &&
  3093. (RTL_R16(CPlusCmd) & ASF)) {
  3094. return;
  3095. }
  3096. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  3097. tp->mac_version == RTL_GIGA_MAC_VER_33)
  3098. rtl_ephy_write(ioaddr, 0x19, 0xff64);
  3099. if (rtl_wol_pll_power_down(tp))
  3100. return;
  3101. r8168_phy_power_down(tp);
  3102. switch (tp->mac_version) {
  3103. case RTL_GIGA_MAC_VER_25:
  3104. case RTL_GIGA_MAC_VER_26:
  3105. case RTL_GIGA_MAC_VER_27:
  3106. case RTL_GIGA_MAC_VER_28:
  3107. case RTL_GIGA_MAC_VER_31:
  3108. case RTL_GIGA_MAC_VER_32:
  3109. case RTL_GIGA_MAC_VER_33:
  3110. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  3111. break;
  3112. }
  3113. }
  3114. static void r8168_pll_power_up(struct rtl8169_private *tp)
  3115. {
  3116. void __iomem *ioaddr = tp->mmio_addr;
  3117. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3118. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3119. tp->mac_version == RTL_GIGA_MAC_VER_31) &&
  3120. r8168dp_check_dash(tp)) {
  3121. return;
  3122. }
  3123. switch (tp->mac_version) {
  3124. case RTL_GIGA_MAC_VER_25:
  3125. case RTL_GIGA_MAC_VER_26:
  3126. case RTL_GIGA_MAC_VER_27:
  3127. case RTL_GIGA_MAC_VER_28:
  3128. case RTL_GIGA_MAC_VER_31:
  3129. case RTL_GIGA_MAC_VER_32:
  3130. case RTL_GIGA_MAC_VER_33:
  3131. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  3132. break;
  3133. }
  3134. r8168_phy_power_up(tp);
  3135. }
  3136. static void rtl_generic_op(struct rtl8169_private *tp,
  3137. void (*op)(struct rtl8169_private *))
  3138. {
  3139. if (op)
  3140. op(tp);
  3141. }
  3142. static void rtl_pll_power_down(struct rtl8169_private *tp)
  3143. {
  3144. rtl_generic_op(tp, tp->pll_power_ops.down);
  3145. }
  3146. static void rtl_pll_power_up(struct rtl8169_private *tp)
  3147. {
  3148. rtl_generic_op(tp, tp->pll_power_ops.up);
  3149. }
  3150. static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
  3151. {
  3152. struct pll_power_ops *ops = &tp->pll_power_ops;
  3153. switch (tp->mac_version) {
  3154. case RTL_GIGA_MAC_VER_07:
  3155. case RTL_GIGA_MAC_VER_08:
  3156. case RTL_GIGA_MAC_VER_09:
  3157. case RTL_GIGA_MAC_VER_10:
  3158. case RTL_GIGA_MAC_VER_16:
  3159. case RTL_GIGA_MAC_VER_29:
  3160. case RTL_GIGA_MAC_VER_30:
  3161. ops->down = r810x_pll_power_down;
  3162. ops->up = r810x_pll_power_up;
  3163. break;
  3164. case RTL_GIGA_MAC_VER_11:
  3165. case RTL_GIGA_MAC_VER_12:
  3166. case RTL_GIGA_MAC_VER_17:
  3167. case RTL_GIGA_MAC_VER_18:
  3168. case RTL_GIGA_MAC_VER_19:
  3169. case RTL_GIGA_MAC_VER_20:
  3170. case RTL_GIGA_MAC_VER_21:
  3171. case RTL_GIGA_MAC_VER_22:
  3172. case RTL_GIGA_MAC_VER_23:
  3173. case RTL_GIGA_MAC_VER_24:
  3174. case RTL_GIGA_MAC_VER_25:
  3175. case RTL_GIGA_MAC_VER_26:
  3176. case RTL_GIGA_MAC_VER_27:
  3177. case RTL_GIGA_MAC_VER_28:
  3178. case RTL_GIGA_MAC_VER_31:
  3179. case RTL_GIGA_MAC_VER_32:
  3180. case RTL_GIGA_MAC_VER_33:
  3181. case RTL_GIGA_MAC_VER_34:
  3182. case RTL_GIGA_MAC_VER_35:
  3183. case RTL_GIGA_MAC_VER_36:
  3184. ops->down = r8168_pll_power_down;
  3185. ops->up = r8168_pll_power_up;
  3186. break;
  3187. default:
  3188. ops->down = NULL;
  3189. ops->up = NULL;
  3190. break;
  3191. }
  3192. }
  3193. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  3194. {
  3195. void __iomem *ioaddr = tp->mmio_addr;
  3196. switch (tp->mac_version) {
  3197. case RTL_GIGA_MAC_VER_01:
  3198. case RTL_GIGA_MAC_VER_02:
  3199. case RTL_GIGA_MAC_VER_03:
  3200. case RTL_GIGA_MAC_VER_04:
  3201. case RTL_GIGA_MAC_VER_05:
  3202. case RTL_GIGA_MAC_VER_06:
  3203. case RTL_GIGA_MAC_VER_10:
  3204. case RTL_GIGA_MAC_VER_11:
  3205. case RTL_GIGA_MAC_VER_12:
  3206. case RTL_GIGA_MAC_VER_13:
  3207. case RTL_GIGA_MAC_VER_14:
  3208. case RTL_GIGA_MAC_VER_15:
  3209. case RTL_GIGA_MAC_VER_16:
  3210. case RTL_GIGA_MAC_VER_17:
  3211. RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  3212. break;
  3213. case RTL_GIGA_MAC_VER_18:
  3214. case RTL_GIGA_MAC_VER_19:
  3215. case RTL_GIGA_MAC_VER_20:
  3216. case RTL_GIGA_MAC_VER_21:
  3217. case RTL_GIGA_MAC_VER_22:
  3218. case RTL_GIGA_MAC_VER_23:
  3219. case RTL_GIGA_MAC_VER_24:
  3220. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  3221. break;
  3222. default:
  3223. RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
  3224. break;
  3225. }
  3226. }
  3227. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  3228. {
  3229. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  3230. }
  3231. static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
  3232. {
  3233. rtl_generic_op(tp, tp->jumbo_ops.enable);
  3234. }
  3235. static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
  3236. {
  3237. rtl_generic_op(tp, tp->jumbo_ops.disable);
  3238. }
  3239. static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
  3240. {
  3241. void __iomem *ioaddr = tp->mmio_addr;
  3242. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  3243. RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
  3244. rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
  3245. }
  3246. static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
  3247. {
  3248. void __iomem *ioaddr = tp->mmio_addr;
  3249. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  3250. RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
  3251. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3252. }
  3253. static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
  3254. {
  3255. void __iomem *ioaddr = tp->mmio_addr;
  3256. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  3257. }
  3258. static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
  3259. {
  3260. void __iomem *ioaddr = tp->mmio_addr;
  3261. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  3262. }
  3263. static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
  3264. {
  3265. void __iomem *ioaddr = tp->mmio_addr;
  3266. RTL_W8(MaxTxPacketSize, 0x3f);
  3267. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  3268. RTL_W8(Config4, RTL_R8(Config4) | 0x01);
  3269. rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
  3270. }
  3271. static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
  3272. {
  3273. void __iomem *ioaddr = tp->mmio_addr;
  3274. RTL_W8(MaxTxPacketSize, 0x0c);
  3275. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  3276. RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
  3277. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3278. }
  3279. static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
  3280. {
  3281. rtl_tx_performance_tweak(tp->pci_dev,
  3282. (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3283. }
  3284. static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
  3285. {
  3286. rtl_tx_performance_tweak(tp->pci_dev,
  3287. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3288. }
  3289. static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
  3290. {
  3291. void __iomem *ioaddr = tp->mmio_addr;
  3292. r8168b_0_hw_jumbo_enable(tp);
  3293. RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
  3294. }
  3295. static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
  3296. {
  3297. void __iomem *ioaddr = tp->mmio_addr;
  3298. r8168b_0_hw_jumbo_disable(tp);
  3299. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  3300. }
  3301. static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
  3302. {
  3303. struct jumbo_ops *ops = &tp->jumbo_ops;
  3304. switch (tp->mac_version) {
  3305. case RTL_GIGA_MAC_VER_11:
  3306. ops->disable = r8168b_0_hw_jumbo_disable;
  3307. ops->enable = r8168b_0_hw_jumbo_enable;
  3308. break;
  3309. case RTL_GIGA_MAC_VER_12:
  3310. case RTL_GIGA_MAC_VER_17:
  3311. ops->disable = r8168b_1_hw_jumbo_disable;
  3312. ops->enable = r8168b_1_hw_jumbo_enable;
  3313. break;
  3314. case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
  3315. case RTL_GIGA_MAC_VER_19:
  3316. case RTL_GIGA_MAC_VER_20:
  3317. case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
  3318. case RTL_GIGA_MAC_VER_22:
  3319. case RTL_GIGA_MAC_VER_23:
  3320. case RTL_GIGA_MAC_VER_24:
  3321. case RTL_GIGA_MAC_VER_25:
  3322. case RTL_GIGA_MAC_VER_26:
  3323. ops->disable = r8168c_hw_jumbo_disable;
  3324. ops->enable = r8168c_hw_jumbo_enable;
  3325. break;
  3326. case RTL_GIGA_MAC_VER_27:
  3327. case RTL_GIGA_MAC_VER_28:
  3328. ops->disable = r8168dp_hw_jumbo_disable;
  3329. ops->enable = r8168dp_hw_jumbo_enable;
  3330. break;
  3331. case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
  3332. case RTL_GIGA_MAC_VER_32:
  3333. case RTL_GIGA_MAC_VER_33:
  3334. case RTL_GIGA_MAC_VER_34:
  3335. ops->disable = r8168e_hw_jumbo_disable;
  3336. ops->enable = r8168e_hw_jumbo_enable;
  3337. break;
  3338. /*
  3339. * No action needed for jumbo frames with 8169.
  3340. * No jumbo for 810x at all.
  3341. */
  3342. default:
  3343. ops->disable = NULL;
  3344. ops->enable = NULL;
  3345. break;
  3346. }
  3347. }
  3348. static void rtl_hw_reset(struct rtl8169_private *tp)
  3349. {
  3350. void __iomem *ioaddr = tp->mmio_addr;
  3351. int i;
  3352. /* Soft reset the chip. */
  3353. RTL_W8(ChipCmd, CmdReset);
  3354. /* Check that the chip has finished the reset. */
  3355. for (i = 0; i < 100; i++) {
  3356. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  3357. break;
  3358. udelay(100);
  3359. }
  3360. }
  3361. static int __devinit
  3362. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  3363. {
  3364. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  3365. const unsigned int region = cfg->region;
  3366. struct rtl8169_private *tp;
  3367. struct mii_if_info *mii;
  3368. struct net_device *dev;
  3369. void __iomem *ioaddr;
  3370. int chipset, i;
  3371. int rc;
  3372. if (netif_msg_drv(&debug)) {
  3373. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  3374. MODULENAME, RTL8169_VERSION);
  3375. }
  3376. dev = alloc_etherdev(sizeof (*tp));
  3377. if (!dev) {
  3378. if (netif_msg_drv(&debug))
  3379. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  3380. rc = -ENOMEM;
  3381. goto out;
  3382. }
  3383. SET_NETDEV_DEV(dev, &pdev->dev);
  3384. dev->netdev_ops = &rtl8169_netdev_ops;
  3385. tp = netdev_priv(dev);
  3386. tp->dev = dev;
  3387. tp->pci_dev = pdev;
  3388. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  3389. mii = &tp->mii;
  3390. mii->dev = dev;
  3391. mii->mdio_read = rtl_mdio_read;
  3392. mii->mdio_write = rtl_mdio_write;
  3393. mii->phy_id_mask = 0x1f;
  3394. mii->reg_num_mask = 0x1f;
  3395. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  3396. /* disable ASPM completely as that cause random device stop working
  3397. * problems as well as full system hangs for some PCIe devices users */
  3398. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  3399. PCIE_LINK_STATE_CLKPM);
  3400. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  3401. rc = pci_enable_device(pdev);
  3402. if (rc < 0) {
  3403. netif_err(tp, probe, dev, "enable failure\n");
  3404. goto err_out_free_dev_1;
  3405. }
  3406. if (pci_set_mwi(pdev) < 0)
  3407. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  3408. /* make sure PCI base addr 1 is MMIO */
  3409. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  3410. netif_err(tp, probe, dev,
  3411. "region #%d not an MMIO resource, aborting\n",
  3412. region);
  3413. rc = -ENODEV;
  3414. goto err_out_mwi_2;
  3415. }
  3416. /* check for weird/broken PCI region reporting */
  3417. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  3418. netif_err(tp, probe, dev,
  3419. "Invalid PCI region size(s), aborting\n");
  3420. rc = -ENODEV;
  3421. goto err_out_mwi_2;
  3422. }
  3423. rc = pci_request_regions(pdev, MODULENAME);
  3424. if (rc < 0) {
  3425. netif_err(tp, probe, dev, "could not request regions\n");
  3426. goto err_out_mwi_2;
  3427. }
  3428. tp->cp_cmd = RxChkSum;
  3429. if ((sizeof(dma_addr_t) > 4) &&
  3430. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  3431. tp->cp_cmd |= PCIDAC;
  3432. dev->features |= NETIF_F_HIGHDMA;
  3433. } else {
  3434. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3435. if (rc < 0) {
  3436. netif_err(tp, probe, dev, "DMA configuration failed\n");
  3437. goto err_out_free_res_3;
  3438. }
  3439. }
  3440. /* ioremap MMIO region */
  3441. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  3442. if (!ioaddr) {
  3443. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  3444. rc = -EIO;
  3445. goto err_out_free_res_3;
  3446. }
  3447. tp->mmio_addr = ioaddr;
  3448. if (!pci_is_pcie(pdev))
  3449. netif_info(tp, probe, dev, "not PCI Express\n");
  3450. /* Identify chip attached to board */
  3451. rtl8169_get_mac_version(tp, dev, cfg->default_ver);
  3452. rtl_init_rxcfg(tp);
  3453. RTL_W16(IntrMask, 0x0000);
  3454. rtl_hw_reset(tp);
  3455. RTL_W16(IntrStatus, 0xffff);
  3456. pci_set_master(pdev);
  3457. /*
  3458. * Pretend we are using VLANs; This bypasses a nasty bug where
  3459. * Interrupts stop flowing on high load on 8110SCd controllers.
  3460. */
  3461. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  3462. tp->cp_cmd |= RxVlan;
  3463. rtl_init_mdio_ops(tp);
  3464. rtl_init_pll_power_ops(tp);
  3465. rtl_init_jumbo_ops(tp);
  3466. rtl8169_print_mac_version(tp);
  3467. chipset = tp->mac_version;
  3468. tp->txd_version = rtl_chip_infos[chipset].txd_version;
  3469. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3470. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  3471. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  3472. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  3473. tp->features |= RTL_FEATURE_WOL;
  3474. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  3475. tp->features |= RTL_FEATURE_WOL;
  3476. tp->features |= rtl_try_msi(tp, cfg);
  3477. RTL_W8(Cfg9346, Cfg9346_Lock);
  3478. if (rtl_tbi_enabled(tp)) {
  3479. tp->set_speed = rtl8169_set_speed_tbi;
  3480. tp->get_settings = rtl8169_gset_tbi;
  3481. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  3482. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  3483. tp->link_ok = rtl8169_tbi_link_ok;
  3484. tp->do_ioctl = rtl_tbi_ioctl;
  3485. } else {
  3486. tp->set_speed = rtl8169_set_speed_xmii;
  3487. tp->get_settings = rtl8169_gset_xmii;
  3488. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  3489. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  3490. tp->link_ok = rtl8169_xmii_link_ok;
  3491. tp->do_ioctl = rtl_xmii_ioctl;
  3492. }
  3493. spin_lock_init(&tp->lock);
  3494. /* Get MAC address */
  3495. for (i = 0; i < ETH_ALEN; i++)
  3496. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  3497. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  3498. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  3499. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  3500. dev->irq = pdev->irq;
  3501. dev->base_addr = (unsigned long) ioaddr;
  3502. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  3503. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  3504. * properly for all devices */
  3505. dev->features |= NETIF_F_RXCSUM |
  3506. NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  3507. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  3508. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  3509. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  3510. NETIF_F_HIGHDMA;
  3511. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  3512. /* 8110SCd requires hardware Rx VLAN - disallow toggling */
  3513. dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
  3514. tp->intr_mask = 0xffff;
  3515. tp->hw_start = cfg->hw_start;
  3516. tp->intr_event = cfg->intr_event;
  3517. tp->napi_event = cfg->napi_event;
  3518. tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
  3519. ~(RxBOVF | RxFOVF) : ~0;
  3520. init_timer(&tp->timer);
  3521. tp->timer.data = (unsigned long) dev;
  3522. tp->timer.function = rtl8169_phy_timer;
  3523. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  3524. rc = register_netdev(dev);
  3525. if (rc < 0)
  3526. goto err_out_msi_4;
  3527. pci_set_drvdata(pdev, dev);
  3528. netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
  3529. rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
  3530. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
  3531. if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
  3532. netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
  3533. "tx checksumming: %s]\n",
  3534. rtl_chip_infos[chipset].jumbo_max,
  3535. rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
  3536. }
  3537. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3538. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3539. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3540. rtl8168_driver_start(tp);
  3541. }
  3542. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  3543. if (pci_dev_run_wake(pdev))
  3544. pm_runtime_put_noidle(&pdev->dev);
  3545. netif_carrier_off(dev);
  3546. out:
  3547. return rc;
  3548. err_out_msi_4:
  3549. rtl_disable_msi(pdev, tp);
  3550. iounmap(ioaddr);
  3551. err_out_free_res_3:
  3552. pci_release_regions(pdev);
  3553. err_out_mwi_2:
  3554. pci_clear_mwi(pdev);
  3555. pci_disable_device(pdev);
  3556. err_out_free_dev_1:
  3557. free_netdev(dev);
  3558. goto out;
  3559. }
  3560. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  3561. {
  3562. struct net_device *dev = pci_get_drvdata(pdev);
  3563. struct rtl8169_private *tp = netdev_priv(dev);
  3564. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3565. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3566. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3567. rtl8168_driver_stop(tp);
  3568. }
  3569. cancel_work_sync(&tp->wk.work);
  3570. unregister_netdev(dev);
  3571. rtl_release_firmware(tp);
  3572. if (pci_dev_run_wake(pdev))
  3573. pm_runtime_get_noresume(&pdev->dev);
  3574. /* restore original MAC address */
  3575. rtl_rar_set(tp, dev->perm_addr);
  3576. rtl_disable_msi(pdev, tp);
  3577. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  3578. pci_set_drvdata(pdev, NULL);
  3579. }
  3580. static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
  3581. {
  3582. struct rtl_fw *rtl_fw;
  3583. const char *name;
  3584. int rc = -ENOMEM;
  3585. name = rtl_lookup_firmware_name(tp);
  3586. if (!name)
  3587. goto out_no_firmware;
  3588. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  3589. if (!rtl_fw)
  3590. goto err_warn;
  3591. rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
  3592. if (rc < 0)
  3593. goto err_free;
  3594. rc = rtl_check_firmware(tp, rtl_fw);
  3595. if (rc < 0)
  3596. goto err_release_firmware;
  3597. tp->rtl_fw = rtl_fw;
  3598. out:
  3599. return;
  3600. err_release_firmware:
  3601. release_firmware(rtl_fw->fw);
  3602. err_free:
  3603. kfree(rtl_fw);
  3604. err_warn:
  3605. netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
  3606. name, rc);
  3607. out_no_firmware:
  3608. tp->rtl_fw = NULL;
  3609. goto out;
  3610. }
  3611. static void rtl_request_firmware(struct rtl8169_private *tp)
  3612. {
  3613. if (IS_ERR(tp->rtl_fw))
  3614. rtl_request_uncached_firmware(tp);
  3615. }
  3616. static void rtl_task(struct work_struct *);
  3617. static int rtl8169_open(struct net_device *dev)
  3618. {
  3619. struct rtl8169_private *tp = netdev_priv(dev);
  3620. void __iomem *ioaddr = tp->mmio_addr;
  3621. struct pci_dev *pdev = tp->pci_dev;
  3622. int retval = -ENOMEM;
  3623. pm_runtime_get_sync(&pdev->dev);
  3624. /*
  3625. * Rx and Tx desscriptors needs 256 bytes alignment.
  3626. * dma_alloc_coherent provides more.
  3627. */
  3628. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  3629. &tp->TxPhyAddr, GFP_KERNEL);
  3630. if (!tp->TxDescArray)
  3631. goto err_pm_runtime_put;
  3632. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  3633. &tp->RxPhyAddr, GFP_KERNEL);
  3634. if (!tp->RxDescArray)
  3635. goto err_free_tx_0;
  3636. retval = rtl8169_init_ring(dev);
  3637. if (retval < 0)
  3638. goto err_free_rx_1;
  3639. INIT_WORK(&tp->wk.work, rtl_task);
  3640. smp_mb();
  3641. rtl_request_firmware(tp);
  3642. retval = request_irq(dev->irq, rtl8169_interrupt,
  3643. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  3644. dev->name, dev);
  3645. if (retval < 0)
  3646. goto err_release_fw_2;
  3647. napi_enable(&tp->napi);
  3648. rtl8169_init_phy(dev, tp);
  3649. rtl8169_set_features(dev, dev->features);
  3650. rtl_pll_power_up(tp);
  3651. rtl_hw_start(dev);
  3652. tp->saved_wolopts = 0;
  3653. pm_runtime_put_noidle(&pdev->dev);
  3654. rtl8169_check_link_status(dev, tp, ioaddr);
  3655. out:
  3656. return retval;
  3657. err_release_fw_2:
  3658. rtl_release_firmware(tp);
  3659. rtl8169_rx_clear(tp);
  3660. err_free_rx_1:
  3661. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3662. tp->RxPhyAddr);
  3663. tp->RxDescArray = NULL;
  3664. err_free_tx_0:
  3665. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3666. tp->TxPhyAddr);
  3667. tp->TxDescArray = NULL;
  3668. err_pm_runtime_put:
  3669. pm_runtime_put_noidle(&pdev->dev);
  3670. goto out;
  3671. }
  3672. static void rtl_rx_close(struct rtl8169_private *tp)
  3673. {
  3674. void __iomem *ioaddr = tp->mmio_addr;
  3675. RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  3676. }
  3677. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  3678. {
  3679. void __iomem *ioaddr = tp->mmio_addr;
  3680. /* Disable interrupts */
  3681. rtl8169_irq_mask_and_ack(tp);
  3682. rtl_rx_close(tp);
  3683. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3684. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3685. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3686. while (RTL_R8(TxPoll) & NPQ)
  3687. udelay(20);
  3688. } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  3689. tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  3690. tp->mac_version == RTL_GIGA_MAC_VER_36) {
  3691. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  3692. while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
  3693. udelay(100);
  3694. } else {
  3695. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  3696. udelay(100);
  3697. }
  3698. rtl_hw_reset(tp);
  3699. }
  3700. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  3701. {
  3702. void __iomem *ioaddr = tp->mmio_addr;
  3703. /* Set DMA burst size and Interframe Gap Time */
  3704. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  3705. (InterFrameGap << TxInterFrameGapShift));
  3706. }
  3707. static void rtl_hw_start(struct net_device *dev)
  3708. {
  3709. struct rtl8169_private *tp = netdev_priv(dev);
  3710. tp->hw_start(dev);
  3711. netif_start_queue(dev);
  3712. }
  3713. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  3714. void __iomem *ioaddr)
  3715. {
  3716. /*
  3717. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  3718. * register to be written before TxDescAddrLow to work.
  3719. * Switching from MMIO to I/O access fixes the issue as well.
  3720. */
  3721. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  3722. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  3723. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  3724. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  3725. }
  3726. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  3727. {
  3728. u16 cmd;
  3729. cmd = RTL_R16(CPlusCmd);
  3730. RTL_W16(CPlusCmd, cmd);
  3731. return cmd;
  3732. }
  3733. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  3734. {
  3735. /* Low hurts. Let's disable the filtering. */
  3736. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  3737. }
  3738. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  3739. {
  3740. static const struct rtl_cfg2_info {
  3741. u32 mac_version;
  3742. u32 clk;
  3743. u32 val;
  3744. } cfg2_info [] = {
  3745. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  3746. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  3747. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  3748. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  3749. };
  3750. const struct rtl_cfg2_info *p = cfg2_info;
  3751. unsigned int i;
  3752. u32 clk;
  3753. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  3754. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  3755. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  3756. RTL_W32(0x7c, p->val);
  3757. break;
  3758. }
  3759. }
  3760. }
  3761. static void rtl_hw_start_8169(struct net_device *dev)
  3762. {
  3763. struct rtl8169_private *tp = netdev_priv(dev);
  3764. void __iomem *ioaddr = tp->mmio_addr;
  3765. struct pci_dev *pdev = tp->pci_dev;
  3766. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  3767. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  3768. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  3769. }
  3770. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3771. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  3772. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3773. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  3774. tp->mac_version == RTL_GIGA_MAC_VER_04)
  3775. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3776. rtl_init_rxcfg(tp);
  3777. RTL_W8(EarlyTxThres, NoEarlyTx);
  3778. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3779. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  3780. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3781. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  3782. tp->mac_version == RTL_GIGA_MAC_VER_04)
  3783. rtl_set_rx_tx_config_registers(tp);
  3784. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  3785. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3786. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  3787. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  3788. "Bit-3 and bit-14 MUST be 1\n");
  3789. tp->cp_cmd |= (1 << 14);
  3790. }
  3791. RTL_W16(CPlusCmd, tp->cp_cmd);
  3792. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  3793. /*
  3794. * Undocumented corner. Supposedly:
  3795. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  3796. */
  3797. RTL_W16(IntrMitigate, 0x0000);
  3798. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3799. if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
  3800. tp->mac_version != RTL_GIGA_MAC_VER_02 &&
  3801. tp->mac_version != RTL_GIGA_MAC_VER_03 &&
  3802. tp->mac_version != RTL_GIGA_MAC_VER_04) {
  3803. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3804. rtl_set_rx_tx_config_registers(tp);
  3805. }
  3806. RTL_W8(Cfg9346, Cfg9346_Lock);
  3807. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  3808. RTL_R8(IntrMask);
  3809. RTL_W32(RxMissed, 0);
  3810. rtl_set_rx_mode(dev);
  3811. /* no early-rx interrupts */
  3812. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  3813. /* Enable all known interrupts by setting the interrupt mask. */
  3814. RTL_W16(IntrMask, tp->intr_event);
  3815. }
  3816. static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
  3817. {
  3818. u32 csi;
  3819. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  3820. rtl_csi_write(ioaddr, 0x070c, csi | bits);
  3821. }
  3822. static void rtl_csi_access_enable_1(void __iomem *ioaddr)
  3823. {
  3824. rtl_csi_access_enable(ioaddr, 0x17000000);
  3825. }
  3826. static void rtl_csi_access_enable_2(void __iomem *ioaddr)
  3827. {
  3828. rtl_csi_access_enable(ioaddr, 0x27000000);
  3829. }
  3830. struct ephy_info {
  3831. unsigned int offset;
  3832. u16 mask;
  3833. u16 bits;
  3834. };
  3835. static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
  3836. {
  3837. u16 w;
  3838. while (len-- > 0) {
  3839. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  3840. rtl_ephy_write(ioaddr, e->offset, w);
  3841. e++;
  3842. }
  3843. }
  3844. static void rtl_disable_clock_request(struct pci_dev *pdev)
  3845. {
  3846. int cap = pci_pcie_cap(pdev);
  3847. if (cap) {
  3848. u16 ctl;
  3849. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  3850. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  3851. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  3852. }
  3853. }
  3854. static void rtl_enable_clock_request(struct pci_dev *pdev)
  3855. {
  3856. int cap = pci_pcie_cap(pdev);
  3857. if (cap) {
  3858. u16 ctl;
  3859. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  3860. ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  3861. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  3862. }
  3863. }
  3864. #define R8168_CPCMD_QUIRK_MASK (\
  3865. EnableBist | \
  3866. Mac_dbgo_oe | \
  3867. Force_half_dup | \
  3868. Force_rxflow_en | \
  3869. Force_txflow_en | \
  3870. Cxpl_dbg_sel | \
  3871. ASF | \
  3872. PktCntrDisable | \
  3873. Mac_dbgo_sel)
  3874. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  3875. {
  3876. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3877. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3878. rtl_tx_performance_tweak(pdev,
  3879. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3880. }
  3881. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  3882. {
  3883. rtl_hw_start_8168bb(ioaddr, pdev);
  3884. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3885. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  3886. }
  3887. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  3888. {
  3889. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  3890. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3891. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3892. rtl_disable_clock_request(pdev);
  3893. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3894. }
  3895. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3896. {
  3897. static const struct ephy_info e_info_8168cp[] = {
  3898. { 0x01, 0, 0x0001 },
  3899. { 0x02, 0x0800, 0x1000 },
  3900. { 0x03, 0, 0x0042 },
  3901. { 0x06, 0x0080, 0x0000 },
  3902. { 0x07, 0, 0x2000 }
  3903. };
  3904. rtl_csi_access_enable_2(ioaddr);
  3905. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  3906. __rtl_hw_start_8168cp(ioaddr, pdev);
  3907. }
  3908. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3909. {
  3910. rtl_csi_access_enable_2(ioaddr);
  3911. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3912. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3913. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3914. }
  3915. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3916. {
  3917. rtl_csi_access_enable_2(ioaddr);
  3918. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3919. /* Magic. */
  3920. RTL_W8(DBG_REG, 0x20);
  3921. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3922. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3923. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3924. }
  3925. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3926. {
  3927. static const struct ephy_info e_info_8168c_1[] = {
  3928. { 0x02, 0x0800, 0x1000 },
  3929. { 0x03, 0, 0x0002 },
  3930. { 0x06, 0x0080, 0x0000 }
  3931. };
  3932. rtl_csi_access_enable_2(ioaddr);
  3933. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  3934. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  3935. __rtl_hw_start_8168cp(ioaddr, pdev);
  3936. }
  3937. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3938. {
  3939. static const struct ephy_info e_info_8168c_2[] = {
  3940. { 0x01, 0, 0x0001 },
  3941. { 0x03, 0x0400, 0x0220 }
  3942. };
  3943. rtl_csi_access_enable_2(ioaddr);
  3944. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  3945. __rtl_hw_start_8168cp(ioaddr, pdev);
  3946. }
  3947. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3948. {
  3949. rtl_hw_start_8168c_2(ioaddr, pdev);
  3950. }
  3951. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3952. {
  3953. rtl_csi_access_enable_2(ioaddr);
  3954. __rtl_hw_start_8168cp(ioaddr, pdev);
  3955. }
  3956. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  3957. {
  3958. rtl_csi_access_enable_2(ioaddr);
  3959. rtl_disable_clock_request(pdev);
  3960. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3961. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3962. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3963. }
  3964. static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
  3965. {
  3966. rtl_csi_access_enable_1(ioaddr);
  3967. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3968. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3969. rtl_disable_clock_request(pdev);
  3970. }
  3971. static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3972. {
  3973. static const struct ephy_info e_info_8168d_4[] = {
  3974. { 0x0b, ~0, 0x48 },
  3975. { 0x19, 0x20, 0x50 },
  3976. { 0x0c, ~0, 0x20 }
  3977. };
  3978. int i;
  3979. rtl_csi_access_enable_1(ioaddr);
  3980. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3981. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3982. for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
  3983. const struct ephy_info *e = e_info_8168d_4 + i;
  3984. u16 w;
  3985. w = rtl_ephy_read(ioaddr, e->offset);
  3986. rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
  3987. }
  3988. rtl_enable_clock_request(pdev);
  3989. }
  3990. static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3991. {
  3992. static const struct ephy_info e_info_8168e_1[] = {
  3993. { 0x00, 0x0200, 0x0100 },
  3994. { 0x00, 0x0000, 0x0004 },
  3995. { 0x06, 0x0002, 0x0001 },
  3996. { 0x06, 0x0000, 0x0030 },
  3997. { 0x07, 0x0000, 0x2000 },
  3998. { 0x00, 0x0000, 0x0020 },
  3999. { 0x03, 0x5800, 0x2000 },
  4000. { 0x03, 0x0000, 0x0001 },
  4001. { 0x01, 0x0800, 0x1000 },
  4002. { 0x07, 0x0000, 0x4000 },
  4003. { 0x1e, 0x0000, 0x2000 },
  4004. { 0x19, 0xffff, 0xfe6c },
  4005. { 0x0a, 0x0000, 0x0040 }
  4006. };
  4007. rtl_csi_access_enable_2(ioaddr);
  4008. rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
  4009. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4010. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4011. rtl_disable_clock_request(pdev);
  4012. /* Reset tx FIFO pointer */
  4013. RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
  4014. RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
  4015. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4016. }
  4017. static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  4018. {
  4019. static const struct ephy_info e_info_8168e_2[] = {
  4020. { 0x09, 0x0000, 0x0080 },
  4021. { 0x19, 0x0000, 0x0224 }
  4022. };
  4023. rtl_csi_access_enable_1(ioaddr);
  4024. rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
  4025. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4026. rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4027. rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4028. rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4029. rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4030. rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4031. rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
  4032. rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4033. rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
  4034. ERIAR_EXGMAC);
  4035. RTL_W8(MaxTxPacketSize, EarlySize);
  4036. rtl_disable_clock_request(pdev);
  4037. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  4038. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  4039. /* Adjust EEE LED frequency */
  4040. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  4041. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  4042. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  4043. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4044. }
  4045. static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
  4046. {
  4047. static const struct ephy_info e_info_8168f_1[] = {
  4048. { 0x06, 0x00c0, 0x0020 },
  4049. { 0x08, 0x0001, 0x0002 },
  4050. { 0x09, 0x0000, 0x0080 },
  4051. { 0x19, 0x0000, 0x0224 }
  4052. };
  4053. rtl_csi_access_enable_1(ioaddr);
  4054. rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  4055. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4056. rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4057. rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4058. rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4059. rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4060. rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4061. rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4062. rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4063. rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4064. rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4065. rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
  4066. rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
  4067. ERIAR_EXGMAC);
  4068. RTL_W8(MaxTxPacketSize, EarlySize);
  4069. rtl_disable_clock_request(pdev);
  4070. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  4071. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  4072. /* Adjust EEE LED frequency */
  4073. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  4074. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  4075. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  4076. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4077. }
  4078. static void rtl_hw_start_8168(struct net_device *dev)
  4079. {
  4080. struct rtl8169_private *tp = netdev_priv(dev);
  4081. void __iomem *ioaddr = tp->mmio_addr;
  4082. struct pci_dev *pdev = tp->pci_dev;
  4083. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4084. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4085. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  4086. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  4087. RTL_W16(CPlusCmd, tp->cp_cmd);
  4088. RTL_W16(IntrMitigate, 0x5151);
  4089. /* Work around for RxFIFO overflow. */
  4090. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  4091. tp->intr_event |= RxFIFOOver | PCSTimeout;
  4092. tp->intr_event &= ~RxOverflow;
  4093. }
  4094. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  4095. rtl_set_rx_mode(dev);
  4096. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  4097. (InterFrameGap << TxInterFrameGapShift));
  4098. RTL_R8(IntrMask);
  4099. switch (tp->mac_version) {
  4100. case RTL_GIGA_MAC_VER_11:
  4101. rtl_hw_start_8168bb(ioaddr, pdev);
  4102. break;
  4103. case RTL_GIGA_MAC_VER_12:
  4104. case RTL_GIGA_MAC_VER_17:
  4105. rtl_hw_start_8168bef(ioaddr, pdev);
  4106. break;
  4107. case RTL_GIGA_MAC_VER_18:
  4108. rtl_hw_start_8168cp_1(ioaddr, pdev);
  4109. break;
  4110. case RTL_GIGA_MAC_VER_19:
  4111. rtl_hw_start_8168c_1(ioaddr, pdev);
  4112. break;
  4113. case RTL_GIGA_MAC_VER_20:
  4114. rtl_hw_start_8168c_2(ioaddr, pdev);
  4115. break;
  4116. case RTL_GIGA_MAC_VER_21:
  4117. rtl_hw_start_8168c_3(ioaddr, pdev);
  4118. break;
  4119. case RTL_GIGA_MAC_VER_22:
  4120. rtl_hw_start_8168c_4(ioaddr, pdev);
  4121. break;
  4122. case RTL_GIGA_MAC_VER_23:
  4123. rtl_hw_start_8168cp_2(ioaddr, pdev);
  4124. break;
  4125. case RTL_GIGA_MAC_VER_24:
  4126. rtl_hw_start_8168cp_3(ioaddr, pdev);
  4127. break;
  4128. case RTL_GIGA_MAC_VER_25:
  4129. case RTL_GIGA_MAC_VER_26:
  4130. case RTL_GIGA_MAC_VER_27:
  4131. rtl_hw_start_8168d(ioaddr, pdev);
  4132. break;
  4133. case RTL_GIGA_MAC_VER_28:
  4134. rtl_hw_start_8168d_4(ioaddr, pdev);
  4135. break;
  4136. case RTL_GIGA_MAC_VER_31:
  4137. rtl_hw_start_8168dp(ioaddr, pdev);
  4138. break;
  4139. case RTL_GIGA_MAC_VER_32:
  4140. case RTL_GIGA_MAC_VER_33:
  4141. rtl_hw_start_8168e_1(ioaddr, pdev);
  4142. break;
  4143. case RTL_GIGA_MAC_VER_34:
  4144. rtl_hw_start_8168e_2(ioaddr, pdev);
  4145. break;
  4146. case RTL_GIGA_MAC_VER_35:
  4147. case RTL_GIGA_MAC_VER_36:
  4148. rtl_hw_start_8168f_1(ioaddr, pdev);
  4149. break;
  4150. default:
  4151. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  4152. dev->name, tp->mac_version);
  4153. break;
  4154. }
  4155. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  4156. RTL_W8(Cfg9346, Cfg9346_Lock);
  4157. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  4158. RTL_W16(IntrMask, tp->intr_event);
  4159. }
  4160. #define R810X_CPCMD_QUIRK_MASK (\
  4161. EnableBist | \
  4162. Mac_dbgo_oe | \
  4163. Force_half_dup | \
  4164. Force_rxflow_en | \
  4165. Force_txflow_en | \
  4166. Cxpl_dbg_sel | \
  4167. ASF | \
  4168. PktCntrDisable | \
  4169. Mac_dbgo_sel)
  4170. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  4171. {
  4172. static const struct ephy_info e_info_8102e_1[] = {
  4173. { 0x01, 0, 0x6e65 },
  4174. { 0x02, 0, 0x091f },
  4175. { 0x03, 0, 0xc2f9 },
  4176. { 0x06, 0, 0xafb5 },
  4177. { 0x07, 0, 0x0e00 },
  4178. { 0x19, 0, 0xec80 },
  4179. { 0x01, 0, 0x2e65 },
  4180. { 0x01, 0, 0x6e65 }
  4181. };
  4182. u8 cfg1;
  4183. rtl_csi_access_enable_2(ioaddr);
  4184. RTL_W8(DBG_REG, FIX_NAK_1);
  4185. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4186. RTL_W8(Config1,
  4187. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  4188. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4189. cfg1 = RTL_R8(Config1);
  4190. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  4191. RTL_W8(Config1, cfg1 & ~LEDS0);
  4192. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  4193. }
  4194. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  4195. {
  4196. rtl_csi_access_enable_2(ioaddr);
  4197. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4198. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  4199. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4200. }
  4201. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  4202. {
  4203. rtl_hw_start_8102e_2(ioaddr, pdev);
  4204. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  4205. }
  4206. static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  4207. {
  4208. static const struct ephy_info e_info_8105e_1[] = {
  4209. { 0x07, 0, 0x4000 },
  4210. { 0x19, 0, 0x0200 },
  4211. { 0x19, 0, 0x0020 },
  4212. { 0x1e, 0, 0x2000 },
  4213. { 0x03, 0, 0x0001 },
  4214. { 0x19, 0, 0x0100 },
  4215. { 0x19, 0, 0x0004 },
  4216. { 0x0a, 0, 0x0020 }
  4217. };
  4218. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4219. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  4220. /* Disable Early Tally Counter */
  4221. RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
  4222. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  4223. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  4224. rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  4225. }
  4226. static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  4227. {
  4228. rtl_hw_start_8105e_1(ioaddr, pdev);
  4229. rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
  4230. }
  4231. static void rtl_hw_start_8101(struct net_device *dev)
  4232. {
  4233. struct rtl8169_private *tp = netdev_priv(dev);
  4234. void __iomem *ioaddr = tp->mmio_addr;
  4235. struct pci_dev *pdev = tp->pci_dev;
  4236. if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
  4237. tp->intr_event &= ~RxFIFOOver;
  4238. tp->napi_event &= ~RxFIFOOver;
  4239. }
  4240. if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
  4241. tp->mac_version == RTL_GIGA_MAC_VER_16) {
  4242. int cap = pci_pcie_cap(pdev);
  4243. if (cap) {
  4244. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  4245. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4246. }
  4247. }
  4248. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4249. switch (tp->mac_version) {
  4250. case RTL_GIGA_MAC_VER_07:
  4251. rtl_hw_start_8102e_1(ioaddr, pdev);
  4252. break;
  4253. case RTL_GIGA_MAC_VER_08:
  4254. rtl_hw_start_8102e_3(ioaddr, pdev);
  4255. break;
  4256. case RTL_GIGA_MAC_VER_09:
  4257. rtl_hw_start_8102e_2(ioaddr, pdev);
  4258. break;
  4259. case RTL_GIGA_MAC_VER_29:
  4260. rtl_hw_start_8105e_1(ioaddr, pdev);
  4261. break;
  4262. case RTL_GIGA_MAC_VER_30:
  4263. rtl_hw_start_8105e_2(ioaddr, pdev);
  4264. break;
  4265. }
  4266. RTL_W8(Cfg9346, Cfg9346_Lock);
  4267. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4268. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  4269. tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
  4270. RTL_W16(CPlusCmd, tp->cp_cmd);
  4271. RTL_W16(IntrMitigate, 0x0000);
  4272. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  4273. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  4274. rtl_set_rx_tx_config_registers(tp);
  4275. RTL_R8(IntrMask);
  4276. rtl_set_rx_mode(dev);
  4277. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  4278. RTL_W16(IntrMask, tp->intr_event);
  4279. }
  4280. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  4281. {
  4282. struct rtl8169_private *tp = netdev_priv(dev);
  4283. if (new_mtu < ETH_ZLEN ||
  4284. new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
  4285. return -EINVAL;
  4286. if (new_mtu > ETH_DATA_LEN)
  4287. rtl_hw_jumbo_enable(tp);
  4288. else
  4289. rtl_hw_jumbo_disable(tp);
  4290. dev->mtu = new_mtu;
  4291. netdev_update_features(dev);
  4292. return 0;
  4293. }
  4294. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  4295. {
  4296. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  4297. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  4298. }
  4299. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  4300. void **data_buff, struct RxDesc *desc)
  4301. {
  4302. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  4303. DMA_FROM_DEVICE);
  4304. kfree(*data_buff);
  4305. *data_buff = NULL;
  4306. rtl8169_make_unusable_by_asic(desc);
  4307. }
  4308. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  4309. {
  4310. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  4311. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  4312. }
  4313. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  4314. u32 rx_buf_sz)
  4315. {
  4316. desc->addr = cpu_to_le64(mapping);
  4317. wmb();
  4318. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4319. }
  4320. static inline void *rtl8169_align(void *data)
  4321. {
  4322. return (void *)ALIGN((long)data, 16);
  4323. }
  4324. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  4325. struct RxDesc *desc)
  4326. {
  4327. void *data;
  4328. dma_addr_t mapping;
  4329. struct device *d = &tp->pci_dev->dev;
  4330. struct net_device *dev = tp->dev;
  4331. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  4332. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  4333. if (!data)
  4334. return NULL;
  4335. if (rtl8169_align(data) != data) {
  4336. kfree(data);
  4337. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  4338. if (!data)
  4339. return NULL;
  4340. }
  4341. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  4342. DMA_FROM_DEVICE);
  4343. if (unlikely(dma_mapping_error(d, mapping))) {
  4344. if (net_ratelimit())
  4345. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  4346. goto err_out;
  4347. }
  4348. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  4349. return data;
  4350. err_out:
  4351. kfree(data);
  4352. return NULL;
  4353. }
  4354. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  4355. {
  4356. unsigned int i;
  4357. for (i = 0; i < NUM_RX_DESC; i++) {
  4358. if (tp->Rx_databuff[i]) {
  4359. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  4360. tp->RxDescArray + i);
  4361. }
  4362. }
  4363. }
  4364. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  4365. {
  4366. desc->opts1 |= cpu_to_le32(RingEnd);
  4367. }
  4368. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  4369. {
  4370. unsigned int i;
  4371. for (i = 0; i < NUM_RX_DESC; i++) {
  4372. void *data;
  4373. if (tp->Rx_databuff[i])
  4374. continue;
  4375. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  4376. if (!data) {
  4377. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  4378. goto err_out;
  4379. }
  4380. tp->Rx_databuff[i] = data;
  4381. }
  4382. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  4383. return 0;
  4384. err_out:
  4385. rtl8169_rx_clear(tp);
  4386. return -ENOMEM;
  4387. }
  4388. static int rtl8169_init_ring(struct net_device *dev)
  4389. {
  4390. struct rtl8169_private *tp = netdev_priv(dev);
  4391. rtl8169_init_ring_indexes(tp);
  4392. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  4393. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  4394. return rtl8169_rx_fill(tp);
  4395. }
  4396. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  4397. struct TxDesc *desc)
  4398. {
  4399. unsigned int len = tx_skb->len;
  4400. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  4401. desc->opts1 = 0x00;
  4402. desc->opts2 = 0x00;
  4403. desc->addr = 0x00;
  4404. tx_skb->len = 0;
  4405. }
  4406. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  4407. unsigned int n)
  4408. {
  4409. unsigned int i;
  4410. for (i = 0; i < n; i++) {
  4411. unsigned int entry = (start + i) % NUM_TX_DESC;
  4412. struct ring_info *tx_skb = tp->tx_skb + entry;
  4413. unsigned int len = tx_skb->len;
  4414. if (len) {
  4415. struct sk_buff *skb = tx_skb->skb;
  4416. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  4417. tp->TxDescArray + entry);
  4418. if (skb) {
  4419. tp->dev->stats.tx_dropped++;
  4420. dev_kfree_skb(skb);
  4421. tx_skb->skb = NULL;
  4422. }
  4423. }
  4424. }
  4425. }
  4426. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  4427. {
  4428. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  4429. tp->cur_tx = tp->dirty_tx = 0;
  4430. }
  4431. static void rtl8169_schedule_work(struct net_device *dev)
  4432. {
  4433. struct rtl8169_private *tp = netdev_priv(dev);
  4434. schedule_work(&tp->wk.work);
  4435. }
  4436. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  4437. {
  4438. struct rtl8169_private *tp = netdev_priv(dev);
  4439. void __iomem *ioaddr = tp->mmio_addr;
  4440. synchronize_irq(dev->irq);
  4441. /* Wait for any pending NAPI task to complete */
  4442. napi_disable(&tp->napi);
  4443. rtl8169_irq_mask_and_ack(tp);
  4444. tp->intr_mask = 0xffff;
  4445. RTL_W16(IntrMask, tp->intr_event);
  4446. napi_enable(&tp->napi);
  4447. }
  4448. static void rtl_reset_work(struct rtl8169_private *tp)
  4449. {
  4450. struct net_device *dev = tp->dev;
  4451. int i;
  4452. rtnl_lock();
  4453. if (!netif_running(dev))
  4454. goto out_unlock;
  4455. rtl8169_hw_reset(tp);
  4456. rtl8169_wait_for_quiescence(dev);
  4457. for (i = 0; i < NUM_RX_DESC; i++)
  4458. rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
  4459. rtl8169_tx_clear(tp);
  4460. rtl8169_init_ring_indexes(tp);
  4461. rtl_hw_start(dev);
  4462. netif_wake_queue(dev);
  4463. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  4464. out_unlock:
  4465. rtnl_unlock();
  4466. }
  4467. static void rtl8169_tx_timeout(struct net_device *dev)
  4468. {
  4469. rtl8169_schedule_work(dev);
  4470. }
  4471. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  4472. u32 *opts)
  4473. {
  4474. struct skb_shared_info *info = skb_shinfo(skb);
  4475. unsigned int cur_frag, entry;
  4476. struct TxDesc * uninitialized_var(txd);
  4477. struct device *d = &tp->pci_dev->dev;
  4478. entry = tp->cur_tx;
  4479. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  4480. const skb_frag_t *frag = info->frags + cur_frag;
  4481. dma_addr_t mapping;
  4482. u32 status, len;
  4483. void *addr;
  4484. entry = (entry + 1) % NUM_TX_DESC;
  4485. txd = tp->TxDescArray + entry;
  4486. len = skb_frag_size(frag);
  4487. addr = skb_frag_address(frag);
  4488. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  4489. if (unlikely(dma_mapping_error(d, mapping))) {
  4490. if (net_ratelimit())
  4491. netif_err(tp, drv, tp->dev,
  4492. "Failed to map TX fragments DMA!\n");
  4493. goto err_out;
  4494. }
  4495. /* Anti gcc 2.95.3 bugware (sic) */
  4496. status = opts[0] | len |
  4497. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  4498. txd->opts1 = cpu_to_le32(status);
  4499. txd->opts2 = cpu_to_le32(opts[1]);
  4500. txd->addr = cpu_to_le64(mapping);
  4501. tp->tx_skb[entry].len = len;
  4502. }
  4503. if (cur_frag) {
  4504. tp->tx_skb[entry].skb = skb;
  4505. txd->opts1 |= cpu_to_le32(LastFrag);
  4506. }
  4507. return cur_frag;
  4508. err_out:
  4509. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  4510. return -EIO;
  4511. }
  4512. static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
  4513. struct sk_buff *skb, u32 *opts)
  4514. {
  4515. const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
  4516. u32 mss = skb_shinfo(skb)->gso_size;
  4517. int offset = info->opts_offset;
  4518. if (mss) {
  4519. opts[0] |= TD_LSO;
  4520. opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
  4521. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  4522. const struct iphdr *ip = ip_hdr(skb);
  4523. if (ip->protocol == IPPROTO_TCP)
  4524. opts[offset] |= info->checksum.tcp;
  4525. else if (ip->protocol == IPPROTO_UDP)
  4526. opts[offset] |= info->checksum.udp;
  4527. else
  4528. WARN_ON_ONCE(1);
  4529. }
  4530. }
  4531. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  4532. struct net_device *dev)
  4533. {
  4534. struct rtl8169_private *tp = netdev_priv(dev);
  4535. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  4536. struct TxDesc *txd = tp->TxDescArray + entry;
  4537. void __iomem *ioaddr = tp->mmio_addr;
  4538. struct device *d = &tp->pci_dev->dev;
  4539. dma_addr_t mapping;
  4540. u32 status, len;
  4541. u32 opts[2];
  4542. int frags;
  4543. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  4544. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  4545. goto err_stop_0;
  4546. }
  4547. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  4548. goto err_stop_0;
  4549. len = skb_headlen(skb);
  4550. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  4551. if (unlikely(dma_mapping_error(d, mapping))) {
  4552. if (net_ratelimit())
  4553. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  4554. goto err_dma_0;
  4555. }
  4556. tp->tx_skb[entry].len = len;
  4557. txd->addr = cpu_to_le64(mapping);
  4558. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  4559. opts[0] = DescOwn;
  4560. rtl8169_tso_csum(tp, skb, opts);
  4561. frags = rtl8169_xmit_frags(tp, skb, opts);
  4562. if (frags < 0)
  4563. goto err_dma_1;
  4564. else if (frags)
  4565. opts[0] |= FirstFrag;
  4566. else {
  4567. opts[0] |= FirstFrag | LastFrag;
  4568. tp->tx_skb[entry].skb = skb;
  4569. }
  4570. txd->opts2 = cpu_to_le32(opts[1]);
  4571. wmb();
  4572. /* Anti gcc 2.95.3 bugware (sic) */
  4573. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  4574. txd->opts1 = cpu_to_le32(status);
  4575. tp->cur_tx += frags + 1;
  4576. wmb();
  4577. RTL_W8(TxPoll, NPQ);
  4578. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  4579. netif_stop_queue(dev);
  4580. smp_rmb();
  4581. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  4582. netif_wake_queue(dev);
  4583. }
  4584. return NETDEV_TX_OK;
  4585. err_dma_1:
  4586. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  4587. err_dma_0:
  4588. dev_kfree_skb(skb);
  4589. dev->stats.tx_dropped++;
  4590. return NETDEV_TX_OK;
  4591. err_stop_0:
  4592. netif_stop_queue(dev);
  4593. dev->stats.tx_dropped++;
  4594. return NETDEV_TX_BUSY;
  4595. }
  4596. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  4597. {
  4598. struct rtl8169_private *tp = netdev_priv(dev);
  4599. struct pci_dev *pdev = tp->pci_dev;
  4600. u16 pci_status, pci_cmd;
  4601. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  4602. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  4603. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  4604. pci_cmd, pci_status);
  4605. /*
  4606. * The recovery sequence below admits a very elaborated explanation:
  4607. * - it seems to work;
  4608. * - I did not see what else could be done;
  4609. * - it makes iop3xx happy.
  4610. *
  4611. * Feel free to adjust to your needs.
  4612. */
  4613. if (pdev->broken_parity_status)
  4614. pci_cmd &= ~PCI_COMMAND_PARITY;
  4615. else
  4616. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  4617. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  4618. pci_write_config_word(pdev, PCI_STATUS,
  4619. pci_status & (PCI_STATUS_DETECTED_PARITY |
  4620. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  4621. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  4622. /* The infamous DAC f*ckup only happens at boot time */
  4623. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  4624. void __iomem *ioaddr = tp->mmio_addr;
  4625. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  4626. tp->cp_cmd &= ~PCIDAC;
  4627. RTL_W16(CPlusCmd, tp->cp_cmd);
  4628. dev->features &= ~NETIF_F_HIGHDMA;
  4629. }
  4630. rtl8169_hw_reset(tp);
  4631. rtl8169_schedule_work(dev);
  4632. }
  4633. static void rtl8169_tx_interrupt(struct net_device *dev,
  4634. struct rtl8169_private *tp,
  4635. void __iomem *ioaddr)
  4636. {
  4637. unsigned int dirty_tx, tx_left;
  4638. dirty_tx = tp->dirty_tx;
  4639. smp_rmb();
  4640. tx_left = tp->cur_tx - dirty_tx;
  4641. while (tx_left > 0) {
  4642. unsigned int entry = dirty_tx % NUM_TX_DESC;
  4643. struct ring_info *tx_skb = tp->tx_skb + entry;
  4644. u32 status;
  4645. rmb();
  4646. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  4647. if (status & DescOwn)
  4648. break;
  4649. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  4650. tp->TxDescArray + entry);
  4651. if (status & LastFrag) {
  4652. dev->stats.tx_packets++;
  4653. dev->stats.tx_bytes += tx_skb->skb->len;
  4654. dev_kfree_skb(tx_skb->skb);
  4655. tx_skb->skb = NULL;
  4656. }
  4657. dirty_tx++;
  4658. tx_left--;
  4659. }
  4660. if (tp->dirty_tx != dirty_tx) {
  4661. tp->dirty_tx = dirty_tx;
  4662. smp_wmb();
  4663. if (netif_queue_stopped(dev) &&
  4664. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  4665. netif_wake_queue(dev);
  4666. }
  4667. /*
  4668. * 8168 hack: TxPoll requests are lost when the Tx packets are
  4669. * too close. Let's kick an extra TxPoll request when a burst
  4670. * of start_xmit activity is detected (if it is not detected,
  4671. * it is slow enough). -- FR
  4672. */
  4673. smp_rmb();
  4674. if (tp->cur_tx != dirty_tx)
  4675. RTL_W8(TxPoll, NPQ);
  4676. }
  4677. }
  4678. static inline int rtl8169_fragmented_frame(u32 status)
  4679. {
  4680. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  4681. }
  4682. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  4683. {
  4684. u32 status = opts1 & RxProtoMask;
  4685. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  4686. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  4687. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4688. else
  4689. skb_checksum_none_assert(skb);
  4690. }
  4691. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  4692. struct rtl8169_private *tp,
  4693. int pkt_size,
  4694. dma_addr_t addr)
  4695. {
  4696. struct sk_buff *skb;
  4697. struct device *d = &tp->pci_dev->dev;
  4698. data = rtl8169_align(data);
  4699. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  4700. prefetch(data);
  4701. skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
  4702. if (skb)
  4703. memcpy(skb->data, data, pkt_size);
  4704. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  4705. return skb;
  4706. }
  4707. static int rtl8169_rx_interrupt(struct net_device *dev,
  4708. struct rtl8169_private *tp,
  4709. void __iomem *ioaddr, u32 budget)
  4710. {
  4711. unsigned int cur_rx, rx_left;
  4712. unsigned int count;
  4713. cur_rx = tp->cur_rx;
  4714. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  4715. rx_left = min(rx_left, budget);
  4716. for (; rx_left > 0; rx_left--, cur_rx++) {
  4717. unsigned int entry = cur_rx % NUM_RX_DESC;
  4718. struct RxDesc *desc = tp->RxDescArray + entry;
  4719. u32 status;
  4720. rmb();
  4721. status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
  4722. if (status & DescOwn)
  4723. break;
  4724. if (unlikely(status & RxRES)) {
  4725. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  4726. status);
  4727. dev->stats.rx_errors++;
  4728. if (status & (RxRWT | RxRUNT))
  4729. dev->stats.rx_length_errors++;
  4730. if (status & RxCRC)
  4731. dev->stats.rx_crc_errors++;
  4732. if (status & RxFOVF) {
  4733. rtl8169_schedule_work(dev);
  4734. dev->stats.rx_fifo_errors++;
  4735. }
  4736. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4737. } else {
  4738. struct sk_buff *skb;
  4739. dma_addr_t addr = le64_to_cpu(desc->addr);
  4740. int pkt_size = (status & 0x00003fff) - 4;
  4741. /*
  4742. * The driver does not support incoming fragmented
  4743. * frames. They are seen as a symptom of over-mtu
  4744. * sized frames.
  4745. */
  4746. if (unlikely(rtl8169_fragmented_frame(status))) {
  4747. dev->stats.rx_dropped++;
  4748. dev->stats.rx_length_errors++;
  4749. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4750. continue;
  4751. }
  4752. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  4753. tp, pkt_size, addr);
  4754. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4755. if (!skb) {
  4756. dev->stats.rx_dropped++;
  4757. continue;
  4758. }
  4759. rtl8169_rx_csum(skb, status);
  4760. skb_put(skb, pkt_size);
  4761. skb->protocol = eth_type_trans(skb, dev);
  4762. rtl8169_rx_vlan_tag(desc, skb);
  4763. napi_gro_receive(&tp->napi, skb);
  4764. dev->stats.rx_bytes += pkt_size;
  4765. dev->stats.rx_packets++;
  4766. }
  4767. /* Work around for AMD plateform. */
  4768. if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
  4769. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  4770. desc->opts2 = 0;
  4771. cur_rx++;
  4772. }
  4773. }
  4774. count = cur_rx - tp->cur_rx;
  4775. tp->cur_rx = cur_rx;
  4776. tp->dirty_rx += count;
  4777. return count;
  4778. }
  4779. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  4780. {
  4781. struct net_device *dev = dev_instance;
  4782. struct rtl8169_private *tp = netdev_priv(dev);
  4783. void __iomem *ioaddr = tp->mmio_addr;
  4784. int handled = 0;
  4785. int status;
  4786. /* loop handling interrupts until we have no new ones or
  4787. * we hit a invalid/hotplug case.
  4788. */
  4789. status = RTL_R16(IntrStatus);
  4790. while (status && status != 0xffff) {
  4791. status &= tp->intr_event;
  4792. if (!status)
  4793. break;
  4794. handled = 1;
  4795. /* Handle all of the error cases first. These will reset
  4796. * the chip, so just exit the loop.
  4797. */
  4798. if (unlikely(!netif_running(dev))) {
  4799. rtl8169_hw_reset(tp);
  4800. break;
  4801. }
  4802. if (unlikely(status & RxFIFOOver)) {
  4803. switch (tp->mac_version) {
  4804. /* Work around for rx fifo overflow */
  4805. case RTL_GIGA_MAC_VER_11:
  4806. netif_stop_queue(dev);
  4807. rtl8169_tx_timeout(dev);
  4808. goto done;
  4809. default:
  4810. break;
  4811. }
  4812. }
  4813. if (unlikely(status & SYSErr)) {
  4814. rtl8169_pcierr_interrupt(dev);
  4815. break;
  4816. }
  4817. if (status & LinkChg)
  4818. __rtl8169_check_link_status(dev, tp, ioaddr, true);
  4819. /* We need to see the lastest version of tp->intr_mask to
  4820. * avoid ignoring an MSI interrupt and having to wait for
  4821. * another event which may never come.
  4822. */
  4823. smp_rmb();
  4824. if (status & tp->intr_mask & tp->napi_event) {
  4825. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  4826. tp->intr_mask = ~tp->napi_event;
  4827. if (likely(napi_schedule_prep(&tp->napi)))
  4828. __napi_schedule(&tp->napi);
  4829. else
  4830. netif_info(tp, intr, dev,
  4831. "interrupt %04x in poll\n", status);
  4832. }
  4833. /* We only get a new MSI interrupt when all active irq
  4834. * sources on the chip have been acknowledged. So, ack
  4835. * everything we've seen and check if new sources have become
  4836. * active to avoid blocking all interrupts from the chip.
  4837. */
  4838. RTL_W16(IntrStatus,
  4839. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  4840. status = RTL_R16(IntrStatus);
  4841. }
  4842. done:
  4843. return IRQ_RETVAL(handled);
  4844. }
  4845. static void rtl_task(struct work_struct *work)
  4846. {
  4847. struct rtl8169_private *tp =
  4848. container_of(work, struct rtl8169_private, wk.work);
  4849. rtl_reset_work(tp);
  4850. }
  4851. static int rtl8169_poll(struct napi_struct *napi, int budget)
  4852. {
  4853. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  4854. struct net_device *dev = tp->dev;
  4855. void __iomem *ioaddr = tp->mmio_addr;
  4856. int work_done;
  4857. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  4858. rtl8169_tx_interrupt(dev, tp, ioaddr);
  4859. if (work_done < budget) {
  4860. napi_complete(napi);
  4861. /* We need for force the visibility of tp->intr_mask
  4862. * for other CPUs, as we can loose an MSI interrupt
  4863. * and potentially wait for a retransmit timeout if we don't.
  4864. * The posted write to IntrMask is safe, as it will
  4865. * eventually make it to the chip and we won't loose anything
  4866. * until it does.
  4867. */
  4868. tp->intr_mask = 0xffff;
  4869. wmb();
  4870. RTL_W16(IntrMask, tp->intr_event);
  4871. }
  4872. return work_done;
  4873. }
  4874. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  4875. {
  4876. struct rtl8169_private *tp = netdev_priv(dev);
  4877. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  4878. return;
  4879. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  4880. RTL_W32(RxMissed, 0);
  4881. }
  4882. static void rtl8169_down(struct net_device *dev)
  4883. {
  4884. struct rtl8169_private *tp = netdev_priv(dev);
  4885. void __iomem *ioaddr = tp->mmio_addr;
  4886. del_timer_sync(&tp->timer);
  4887. netif_stop_queue(dev);
  4888. napi_disable(&tp->napi);
  4889. spin_lock_irq(&tp->lock);
  4890. rtl8169_hw_reset(tp);
  4891. /*
  4892. * At this point device interrupts can not be enabled in any function,
  4893. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
  4894. * and napi is disabled (rtl8169_poll).
  4895. */
  4896. rtl8169_rx_missed(dev, ioaddr);
  4897. spin_unlock_irq(&tp->lock);
  4898. synchronize_irq(dev->irq);
  4899. /* Give a racing hard_start_xmit a few cycles to complete. */
  4900. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  4901. rtl8169_tx_clear(tp);
  4902. rtl8169_rx_clear(tp);
  4903. rtl_pll_power_down(tp);
  4904. }
  4905. static int rtl8169_close(struct net_device *dev)
  4906. {
  4907. struct rtl8169_private *tp = netdev_priv(dev);
  4908. struct pci_dev *pdev = tp->pci_dev;
  4909. pm_runtime_get_sync(&pdev->dev);
  4910. /* Update counters before going down */
  4911. rtl8169_update_counters(dev);
  4912. rtl8169_down(dev);
  4913. free_irq(dev->irq, dev);
  4914. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  4915. tp->RxPhyAddr);
  4916. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  4917. tp->TxPhyAddr);
  4918. tp->TxDescArray = NULL;
  4919. tp->RxDescArray = NULL;
  4920. pm_runtime_put_sync(&pdev->dev);
  4921. return 0;
  4922. }
  4923. static void rtl_set_rx_mode(struct net_device *dev)
  4924. {
  4925. struct rtl8169_private *tp = netdev_priv(dev);
  4926. void __iomem *ioaddr = tp->mmio_addr;
  4927. unsigned long flags;
  4928. u32 mc_filter[2]; /* Multicast hash filter */
  4929. int rx_mode;
  4930. u32 tmp = 0;
  4931. if (dev->flags & IFF_PROMISC) {
  4932. /* Unconditionally log net taps. */
  4933. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  4934. rx_mode =
  4935. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  4936. AcceptAllPhys;
  4937. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4938. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  4939. (dev->flags & IFF_ALLMULTI)) {
  4940. /* Too many to filter perfectly -- accept all multicasts. */
  4941. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  4942. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4943. } else {
  4944. struct netdev_hw_addr *ha;
  4945. rx_mode = AcceptBroadcast | AcceptMyPhys;
  4946. mc_filter[1] = mc_filter[0] = 0;
  4947. netdev_for_each_mc_addr(ha, dev) {
  4948. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  4949. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  4950. rx_mode |= AcceptMulticast;
  4951. }
  4952. }
  4953. spin_lock_irqsave(&tp->lock, flags);
  4954. tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
  4955. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  4956. u32 data = mc_filter[0];
  4957. mc_filter[0] = swab32(mc_filter[1]);
  4958. mc_filter[1] = swab32(data);
  4959. }
  4960. RTL_W32(MAR0 + 4, mc_filter[1]);
  4961. RTL_W32(MAR0 + 0, mc_filter[0]);
  4962. RTL_W32(RxConfig, tmp);
  4963. spin_unlock_irqrestore(&tp->lock, flags);
  4964. }
  4965. /**
  4966. * rtl8169_get_stats - Get rtl8169 read/write statistics
  4967. * @dev: The Ethernet Device to get statistics for
  4968. *
  4969. * Get TX/RX statistics for rtl8169
  4970. */
  4971. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  4972. {
  4973. struct rtl8169_private *tp = netdev_priv(dev);
  4974. void __iomem *ioaddr = tp->mmio_addr;
  4975. unsigned long flags;
  4976. if (netif_running(dev)) {
  4977. spin_lock_irqsave(&tp->lock, flags);
  4978. rtl8169_rx_missed(dev, ioaddr);
  4979. spin_unlock_irqrestore(&tp->lock, flags);
  4980. }
  4981. return &dev->stats;
  4982. }
  4983. static void rtl8169_net_suspend(struct net_device *dev)
  4984. {
  4985. struct rtl8169_private *tp = netdev_priv(dev);
  4986. if (!netif_running(dev))
  4987. return;
  4988. rtl_pll_power_down(tp);
  4989. netif_device_detach(dev);
  4990. netif_stop_queue(dev);
  4991. }
  4992. #ifdef CONFIG_PM
  4993. static int rtl8169_suspend(struct device *device)
  4994. {
  4995. struct pci_dev *pdev = to_pci_dev(device);
  4996. struct net_device *dev = pci_get_drvdata(pdev);
  4997. rtl8169_net_suspend(dev);
  4998. return 0;
  4999. }
  5000. static void __rtl8169_resume(struct net_device *dev)
  5001. {
  5002. struct rtl8169_private *tp = netdev_priv(dev);
  5003. netif_device_attach(dev);
  5004. rtl_pll_power_up(tp);
  5005. rtl8169_schedule_work(dev);
  5006. }
  5007. static int rtl8169_resume(struct device *device)
  5008. {
  5009. struct pci_dev *pdev = to_pci_dev(device);
  5010. struct net_device *dev = pci_get_drvdata(pdev);
  5011. struct rtl8169_private *tp = netdev_priv(dev);
  5012. rtl8169_init_phy(dev, tp);
  5013. if (netif_running(dev))
  5014. __rtl8169_resume(dev);
  5015. return 0;
  5016. }
  5017. static int rtl8169_runtime_suspend(struct device *device)
  5018. {
  5019. struct pci_dev *pdev = to_pci_dev(device);
  5020. struct net_device *dev = pci_get_drvdata(pdev);
  5021. struct rtl8169_private *tp = netdev_priv(dev);
  5022. if (!tp->TxDescArray)
  5023. return 0;
  5024. spin_lock_irq(&tp->lock);
  5025. tp->saved_wolopts = __rtl8169_get_wol(tp);
  5026. __rtl8169_set_wol(tp, WAKE_ANY);
  5027. spin_unlock_irq(&tp->lock);
  5028. rtl8169_net_suspend(dev);
  5029. return 0;
  5030. }
  5031. static int rtl8169_runtime_resume(struct device *device)
  5032. {
  5033. struct pci_dev *pdev = to_pci_dev(device);
  5034. struct net_device *dev = pci_get_drvdata(pdev);
  5035. struct rtl8169_private *tp = netdev_priv(dev);
  5036. if (!tp->TxDescArray)
  5037. return 0;
  5038. spin_lock_irq(&tp->lock);
  5039. __rtl8169_set_wol(tp, tp->saved_wolopts);
  5040. tp->saved_wolopts = 0;
  5041. spin_unlock_irq(&tp->lock);
  5042. rtl8169_init_phy(dev, tp);
  5043. __rtl8169_resume(dev);
  5044. return 0;
  5045. }
  5046. static int rtl8169_runtime_idle(struct device *device)
  5047. {
  5048. struct pci_dev *pdev = to_pci_dev(device);
  5049. struct net_device *dev = pci_get_drvdata(pdev);
  5050. struct rtl8169_private *tp = netdev_priv(dev);
  5051. return tp->TxDescArray ? -EBUSY : 0;
  5052. }
  5053. static const struct dev_pm_ops rtl8169_pm_ops = {
  5054. .suspend = rtl8169_suspend,
  5055. .resume = rtl8169_resume,
  5056. .freeze = rtl8169_suspend,
  5057. .thaw = rtl8169_resume,
  5058. .poweroff = rtl8169_suspend,
  5059. .restore = rtl8169_resume,
  5060. .runtime_suspend = rtl8169_runtime_suspend,
  5061. .runtime_resume = rtl8169_runtime_resume,
  5062. .runtime_idle = rtl8169_runtime_idle,
  5063. };
  5064. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  5065. #else /* !CONFIG_PM */
  5066. #define RTL8169_PM_OPS NULL
  5067. #endif /* !CONFIG_PM */
  5068. static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
  5069. {
  5070. void __iomem *ioaddr = tp->mmio_addr;
  5071. /* WoL fails with 8168b when the receiver is disabled. */
  5072. switch (tp->mac_version) {
  5073. case RTL_GIGA_MAC_VER_11:
  5074. case RTL_GIGA_MAC_VER_12:
  5075. case RTL_GIGA_MAC_VER_17:
  5076. pci_clear_master(tp->pci_dev);
  5077. RTL_W8(ChipCmd, CmdRxEnb);
  5078. /* PCI commit */
  5079. RTL_R8(ChipCmd);
  5080. break;
  5081. default:
  5082. break;
  5083. }
  5084. }
  5085. static void rtl_shutdown(struct pci_dev *pdev)
  5086. {
  5087. struct net_device *dev = pci_get_drvdata(pdev);
  5088. struct rtl8169_private *tp = netdev_priv(dev);
  5089. rtl8169_net_suspend(dev);
  5090. /* Restore original MAC address */
  5091. rtl_rar_set(tp, dev->perm_addr);
  5092. spin_lock_irq(&tp->lock);
  5093. rtl8169_hw_reset(tp);
  5094. spin_unlock_irq(&tp->lock);
  5095. if (system_state == SYSTEM_POWER_OFF) {
  5096. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  5097. rtl_wol_suspend_quirk(tp);
  5098. rtl_wol_shutdown_quirk(tp);
  5099. }
  5100. pci_wake_from_d3(pdev, true);
  5101. pci_set_power_state(pdev, PCI_D3hot);
  5102. }
  5103. }
  5104. static struct pci_driver rtl8169_pci_driver = {
  5105. .name = MODULENAME,
  5106. .id_table = rtl8169_pci_tbl,
  5107. .probe = rtl8169_init_one,
  5108. .remove = __devexit_p(rtl8169_remove_one),
  5109. .shutdown = rtl_shutdown,
  5110. .driver.pm = RTL8169_PM_OPS,
  5111. };
  5112. static int __init rtl8169_init_module(void)
  5113. {
  5114. return pci_register_driver(&rtl8169_pci_driver);
  5115. }
  5116. static void __exit rtl8169_cleanup_module(void)
  5117. {
  5118. pci_unregister_driver(&rtl8169_pci_driver);
  5119. }
  5120. module_init(rtl8169_init_module);
  5121. module_exit(rtl8169_cleanup_module);