r8169.c 148 KB

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