r8169.c 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698
  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/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/phy.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/crc32.h>
  21. #include <linux/in.h>
  22. #include <linux/io.h>
  23. #include <linux/ip.h>
  24. #include <linux/tcp.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/firmware.h>
  29. #include <linux/prefetch.h>
  30. #include <linux/pci-aspm.h>
  31. #include <linux/ipv6.h>
  32. #include <net/ip6_checksum.h>
  33. #define MODULENAME "r8169"
  34. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  35. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  36. #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
  37. #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
  38. #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
  39. #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
  40. #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
  41. #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
  42. #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
  43. #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
  44. #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
  45. #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
  46. #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
  47. #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
  48. #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
  49. #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
  50. #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
  51. #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
  52. #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
  53. #define R8169_MSG_DEFAULT \
  54. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  55. #define TX_SLOTS_AVAIL(tp) \
  56. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
  57. /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
  58. #define TX_FRAGS_READY_FOR(tp,nr_frags) \
  59. (TX_SLOTS_AVAIL(tp) >= (nr_frags + 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 TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
  64. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  65. #define R8169_REGS_SIZE 256
  66. #define R8169_RX_BUF_SIZE (SZ_16K - 1)
  67. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  68. #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
  69. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  70. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  71. #define RTL8169_TX_TIMEOUT (6*HZ)
  72. /* write/read MMIO register */
  73. #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
  74. #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
  75. #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
  76. #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
  77. #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
  78. #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
  79. enum mac_version {
  80. RTL_GIGA_MAC_VER_01 = 0,
  81. RTL_GIGA_MAC_VER_02,
  82. RTL_GIGA_MAC_VER_03,
  83. RTL_GIGA_MAC_VER_04,
  84. RTL_GIGA_MAC_VER_05,
  85. RTL_GIGA_MAC_VER_06,
  86. RTL_GIGA_MAC_VER_07,
  87. RTL_GIGA_MAC_VER_08,
  88. RTL_GIGA_MAC_VER_09,
  89. RTL_GIGA_MAC_VER_10,
  90. RTL_GIGA_MAC_VER_11,
  91. RTL_GIGA_MAC_VER_12,
  92. RTL_GIGA_MAC_VER_13,
  93. RTL_GIGA_MAC_VER_14,
  94. RTL_GIGA_MAC_VER_15,
  95. RTL_GIGA_MAC_VER_16,
  96. RTL_GIGA_MAC_VER_17,
  97. RTL_GIGA_MAC_VER_18,
  98. RTL_GIGA_MAC_VER_19,
  99. RTL_GIGA_MAC_VER_20,
  100. RTL_GIGA_MAC_VER_21,
  101. RTL_GIGA_MAC_VER_22,
  102. RTL_GIGA_MAC_VER_23,
  103. RTL_GIGA_MAC_VER_24,
  104. RTL_GIGA_MAC_VER_25,
  105. RTL_GIGA_MAC_VER_26,
  106. RTL_GIGA_MAC_VER_27,
  107. RTL_GIGA_MAC_VER_28,
  108. RTL_GIGA_MAC_VER_29,
  109. RTL_GIGA_MAC_VER_30,
  110. RTL_GIGA_MAC_VER_31,
  111. RTL_GIGA_MAC_VER_32,
  112. RTL_GIGA_MAC_VER_33,
  113. RTL_GIGA_MAC_VER_34,
  114. RTL_GIGA_MAC_VER_35,
  115. RTL_GIGA_MAC_VER_36,
  116. RTL_GIGA_MAC_VER_37,
  117. RTL_GIGA_MAC_VER_38,
  118. RTL_GIGA_MAC_VER_39,
  119. RTL_GIGA_MAC_VER_40,
  120. RTL_GIGA_MAC_VER_41,
  121. RTL_GIGA_MAC_VER_42,
  122. RTL_GIGA_MAC_VER_43,
  123. RTL_GIGA_MAC_VER_44,
  124. RTL_GIGA_MAC_VER_45,
  125. RTL_GIGA_MAC_VER_46,
  126. RTL_GIGA_MAC_VER_47,
  127. RTL_GIGA_MAC_VER_48,
  128. RTL_GIGA_MAC_VER_49,
  129. RTL_GIGA_MAC_VER_50,
  130. RTL_GIGA_MAC_VER_51,
  131. RTL_GIGA_MAC_NONE = 0xff,
  132. };
  133. #define JUMBO_1K ETH_DATA_LEN
  134. #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
  135. #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
  136. #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
  137. #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
  138. static const struct {
  139. const char *name;
  140. const char *fw_name;
  141. } rtl_chip_infos[] = {
  142. /* PCI devices. */
  143. [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
  144. [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
  145. [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
  146. [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
  147. [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
  148. [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
  149. /* PCI-E devices. */
  150. [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
  151. [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
  152. [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
  153. [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
  154. [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
  155. [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
  156. [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
  157. [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
  158. [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
  159. [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
  160. [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
  161. [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
  162. [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
  163. [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
  164. [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
  165. [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
  166. [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
  167. [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
  168. [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
  169. [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
  170. [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
  171. [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
  172. [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
  173. [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
  174. [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
  175. [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
  176. [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
  177. [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
  178. [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
  179. [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
  180. [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
  181. [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
  182. [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
  183. [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
  184. [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
  185. [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
  186. [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
  187. [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
  188. [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
  189. [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
  190. [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
  191. [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
  192. [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
  193. [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
  194. [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
  195. };
  196. enum cfg_version {
  197. RTL_CFG_0 = 0x00,
  198. RTL_CFG_1,
  199. RTL_CFG_2
  200. };
  201. static const struct pci_device_id rtl8169_pci_tbl[] = {
  202. { PCI_VDEVICE(REALTEK, 0x2502), RTL_CFG_1 },
  203. { PCI_VDEVICE(REALTEK, 0x2600), RTL_CFG_1 },
  204. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  205. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  206. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
  207. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  208. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  209. { PCI_DEVICE(PCI_VENDOR_ID_NCUBE, 0x8168), 0, 0, RTL_CFG_1 },
  210. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  211. { PCI_VENDOR_ID_DLINK, 0x4300,
  212. PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
  213. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  214. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
  215. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  216. { PCI_DEVICE(PCI_VENDOR_ID_USR, 0x0116), 0, 0, RTL_CFG_0 },
  217. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  218. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  219. { 0x0001, 0x8168,
  220. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  221. {0,},
  222. };
  223. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  224. static int use_dac = -1;
  225. static struct {
  226. u32 msg_enable;
  227. } debug = { -1 };
  228. enum rtl_registers {
  229. MAC0 = 0, /* Ethernet hardware address. */
  230. MAC4 = 4,
  231. MAR0 = 8, /* Multicast filter. */
  232. CounterAddrLow = 0x10,
  233. CounterAddrHigh = 0x14,
  234. TxDescStartAddrLow = 0x20,
  235. TxDescStartAddrHigh = 0x24,
  236. TxHDescStartAddrLow = 0x28,
  237. TxHDescStartAddrHigh = 0x2c,
  238. FLASH = 0x30,
  239. ERSR = 0x36,
  240. ChipCmd = 0x37,
  241. TxPoll = 0x38,
  242. IntrMask = 0x3c,
  243. IntrStatus = 0x3e,
  244. TxConfig = 0x40,
  245. #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
  246. #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
  247. RxConfig = 0x44,
  248. #define RX128_INT_EN (1 << 15) /* 8111c and later */
  249. #define RX_MULTI_EN (1 << 14) /* 8111c only */
  250. #define RXCFG_FIFO_SHIFT 13
  251. /* No threshold before first PCI xfer */
  252. #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
  253. #define RX_EARLY_OFF (1 << 11)
  254. #define RXCFG_DMA_SHIFT 8
  255. /* Unlimited maximum PCI burst. */
  256. #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
  257. RxMissed = 0x4c,
  258. Cfg9346 = 0x50,
  259. Config0 = 0x51,
  260. Config1 = 0x52,
  261. Config2 = 0x53,
  262. #define PME_SIGNAL (1 << 5) /* 8168c and later */
  263. Config3 = 0x54,
  264. Config4 = 0x55,
  265. Config5 = 0x56,
  266. MultiIntr = 0x5c,
  267. PHYAR = 0x60,
  268. PHYstatus = 0x6c,
  269. RxMaxSize = 0xda,
  270. CPlusCmd = 0xe0,
  271. IntrMitigate = 0xe2,
  272. #define RTL_COALESCE_MASK 0x0f
  273. #define RTL_COALESCE_SHIFT 4
  274. #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
  275. #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
  276. RxDescAddrLow = 0xe4,
  277. RxDescAddrHigh = 0xe8,
  278. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  279. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  280. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  281. #define TxPacketMax (8064 >> 7)
  282. #define EarlySize 0x27
  283. FuncEvent = 0xf0,
  284. FuncEventMask = 0xf4,
  285. FuncPresetState = 0xf8,
  286. IBCR0 = 0xf8,
  287. IBCR2 = 0xf9,
  288. IBIMR0 = 0xfa,
  289. IBISR0 = 0xfb,
  290. FuncForceEvent = 0xfc,
  291. };
  292. enum rtl8168_8101_registers {
  293. CSIDR = 0x64,
  294. CSIAR = 0x68,
  295. #define CSIAR_FLAG 0x80000000
  296. #define CSIAR_WRITE_CMD 0x80000000
  297. #define CSIAR_BYTE_ENABLE 0x0000f000
  298. #define CSIAR_ADDR_MASK 0x00000fff
  299. PMCH = 0x6f,
  300. EPHYAR = 0x80,
  301. #define EPHYAR_FLAG 0x80000000
  302. #define EPHYAR_WRITE_CMD 0x80000000
  303. #define EPHYAR_REG_MASK 0x1f
  304. #define EPHYAR_REG_SHIFT 16
  305. #define EPHYAR_DATA_MASK 0xffff
  306. DLLPR = 0xd0,
  307. #define PFM_EN (1 << 6)
  308. #define TX_10M_PS_EN (1 << 7)
  309. DBG_REG = 0xd1,
  310. #define FIX_NAK_1 (1 << 4)
  311. #define FIX_NAK_2 (1 << 3)
  312. TWSI = 0xd2,
  313. MCU = 0xd3,
  314. #define NOW_IS_OOB (1 << 7)
  315. #define TX_EMPTY (1 << 5)
  316. #define RX_EMPTY (1 << 4)
  317. #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
  318. #define EN_NDP (1 << 3)
  319. #define EN_OOB_RESET (1 << 2)
  320. #define LINK_LIST_RDY (1 << 1)
  321. EFUSEAR = 0xdc,
  322. #define EFUSEAR_FLAG 0x80000000
  323. #define EFUSEAR_WRITE_CMD 0x80000000
  324. #define EFUSEAR_READ_CMD 0x00000000
  325. #define EFUSEAR_REG_MASK 0x03ff
  326. #define EFUSEAR_REG_SHIFT 8
  327. #define EFUSEAR_DATA_MASK 0xff
  328. MISC_1 = 0xf2,
  329. #define PFM_D3COLD_EN (1 << 6)
  330. };
  331. enum rtl8168_registers {
  332. LED_FREQ = 0x1a,
  333. EEE_LED = 0x1b,
  334. ERIDR = 0x70,
  335. ERIAR = 0x74,
  336. #define ERIAR_FLAG 0x80000000
  337. #define ERIAR_WRITE_CMD 0x80000000
  338. #define ERIAR_READ_CMD 0x00000000
  339. #define ERIAR_ADDR_BYTE_ALIGN 4
  340. #define ERIAR_TYPE_SHIFT 16
  341. #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
  342. #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
  343. #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
  344. #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
  345. #define ERIAR_MASK_SHIFT 12
  346. #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
  347. #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
  348. #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
  349. #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
  350. #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
  351. EPHY_RXER_NUM = 0x7c,
  352. OCPDR = 0xb0, /* OCP GPHY access */
  353. #define OCPDR_WRITE_CMD 0x80000000
  354. #define OCPDR_READ_CMD 0x00000000
  355. #define OCPDR_REG_MASK 0x7f
  356. #define OCPDR_GPHY_REG_SHIFT 16
  357. #define OCPDR_DATA_MASK 0xffff
  358. OCPAR = 0xb4,
  359. #define OCPAR_FLAG 0x80000000
  360. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  361. #define OCPAR_GPHY_READ_CMD 0x0000f060
  362. GPHY_OCP = 0xb8,
  363. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  364. MISC = 0xf0, /* 8168e only. */
  365. #define TXPLA_RST (1 << 29)
  366. #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
  367. #define PWM_EN (1 << 22)
  368. #define RXDV_GATED_EN (1 << 19)
  369. #define EARLY_TALLY_EN (1 << 16)
  370. };
  371. enum rtl_register_content {
  372. /* InterruptStatusBits */
  373. SYSErr = 0x8000,
  374. PCSTimeout = 0x4000,
  375. SWInt = 0x0100,
  376. TxDescUnavail = 0x0080,
  377. RxFIFOOver = 0x0040,
  378. LinkChg = 0x0020,
  379. RxOverflow = 0x0010,
  380. TxErr = 0x0008,
  381. TxOK = 0x0004,
  382. RxErr = 0x0002,
  383. RxOK = 0x0001,
  384. /* RxStatusDesc */
  385. RxBOVF = (1 << 24),
  386. RxFOVF = (1 << 23),
  387. RxRWT = (1 << 22),
  388. RxRES = (1 << 21),
  389. RxRUNT = (1 << 20),
  390. RxCRC = (1 << 19),
  391. /* ChipCmdBits */
  392. StopReq = 0x80,
  393. CmdReset = 0x10,
  394. CmdRxEnb = 0x08,
  395. CmdTxEnb = 0x04,
  396. RxBufEmpty = 0x01,
  397. /* TXPoll register p.5 */
  398. HPQ = 0x80, /* Poll cmd on the high prio queue */
  399. NPQ = 0x40, /* Poll cmd on the low prio queue */
  400. FSWInt = 0x01, /* Forced software interrupt */
  401. /* Cfg9346Bits */
  402. Cfg9346_Lock = 0x00,
  403. Cfg9346_Unlock = 0xc0,
  404. /* rx_mode_bits */
  405. AcceptErr = 0x20,
  406. AcceptRunt = 0x10,
  407. AcceptBroadcast = 0x08,
  408. AcceptMulticast = 0x04,
  409. AcceptMyPhys = 0x02,
  410. AcceptAllPhys = 0x01,
  411. #define RX_CONFIG_ACCEPT_MASK 0x3f
  412. /* TxConfigBits */
  413. TxInterFrameGapShift = 24,
  414. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  415. /* Config1 register p.24 */
  416. LEDS1 = (1 << 7),
  417. LEDS0 = (1 << 6),
  418. Speed_down = (1 << 4),
  419. MEMMAP = (1 << 3),
  420. IOMAP = (1 << 2),
  421. VPD = (1 << 1),
  422. PMEnable = (1 << 0), /* Power Management Enable */
  423. /* Config2 register p. 25 */
  424. ClkReqEn = (1 << 7), /* Clock Request Enable */
  425. MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
  426. PCI_Clock_66MHz = 0x01,
  427. PCI_Clock_33MHz = 0x00,
  428. /* Config3 register p.25 */
  429. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  430. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  431. Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
  432. Rdy_to_L23 = (1 << 1), /* L23 Enable */
  433. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  434. /* Config4 register */
  435. Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
  436. /* Config5 register p.27 */
  437. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  438. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  439. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  440. Spi_en = (1 << 3),
  441. LanWake = (1 << 1), /* LanWake enable/disable */
  442. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  443. ASPM_en = (1 << 0), /* ASPM enable */
  444. /* CPlusCmd p.31 */
  445. EnableBist = (1 << 15), // 8168 8101
  446. Mac_dbgo_oe = (1 << 14), // 8168 8101
  447. Normal_mode = (1 << 13), // unused
  448. Force_half_dup = (1 << 12), // 8168 8101
  449. Force_rxflow_en = (1 << 11), // 8168 8101
  450. Force_txflow_en = (1 << 10), // 8168 8101
  451. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  452. ASF = (1 << 8), // 8168 8101
  453. PktCntrDisable = (1 << 7), // 8168 8101
  454. Mac_dbgo_sel = 0x001c, // 8168
  455. RxVlan = (1 << 6),
  456. RxChkSum = (1 << 5),
  457. PCIDAC = (1 << 4),
  458. PCIMulRW = (1 << 3),
  459. #define INTT_MASK GENMASK(1, 0)
  460. INTT_0 = 0x0000, // 8168
  461. INTT_1 = 0x0001, // 8168
  462. INTT_2 = 0x0002, // 8168
  463. INTT_3 = 0x0003, // 8168
  464. /* rtl8169_PHYstatus */
  465. TBI_Enable = 0x80,
  466. TxFlowCtrl = 0x40,
  467. RxFlowCtrl = 0x20,
  468. _1000bpsF = 0x10,
  469. _100bps = 0x08,
  470. _10bps = 0x04,
  471. LinkStatus = 0x02,
  472. FullDup = 0x01,
  473. /* _TBICSRBit */
  474. TBILinkOK = 0x02000000,
  475. /* ResetCounterCommand */
  476. CounterReset = 0x1,
  477. /* DumpCounterCommand */
  478. CounterDump = 0x8,
  479. /* magic enable v2 */
  480. MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
  481. };
  482. enum rtl_desc_bit {
  483. /* First doubleword. */
  484. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  485. RingEnd = (1 << 30), /* End of descriptor ring */
  486. FirstFrag = (1 << 29), /* First segment of a packet */
  487. LastFrag = (1 << 28), /* Final segment of a packet */
  488. };
  489. /* Generic case. */
  490. enum rtl_tx_desc_bit {
  491. /* First doubleword. */
  492. TD_LSO = (1 << 27), /* Large Send Offload */
  493. #define TD_MSS_MAX 0x07ffu /* MSS value */
  494. /* Second doubleword. */
  495. TxVlanTag = (1 << 17), /* Add VLAN tag */
  496. };
  497. /* 8169, 8168b and 810x except 8102e. */
  498. enum rtl_tx_desc_bit_0 {
  499. /* First doubleword. */
  500. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  501. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  502. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  503. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  504. };
  505. /* 8102e, 8168c and beyond. */
  506. enum rtl_tx_desc_bit_1 {
  507. /* First doubleword. */
  508. TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
  509. TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
  510. #define GTTCPHO_SHIFT 18
  511. #define GTTCPHO_MAX 0x7fU
  512. /* Second doubleword. */
  513. #define TCPHO_SHIFT 18
  514. #define TCPHO_MAX 0x3ffU
  515. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  516. TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
  517. TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
  518. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  519. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  520. };
  521. enum rtl_rx_desc_bit {
  522. /* Rx private */
  523. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  524. PID0 = (1 << 17), /* Protocol ID bit 0/2 */
  525. #define RxProtoUDP (PID1)
  526. #define RxProtoTCP (PID0)
  527. #define RxProtoIP (PID1 | PID0)
  528. #define RxProtoMask RxProtoIP
  529. IPFail = (1 << 16), /* IP checksum failed */
  530. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  531. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  532. RxVlanTag = (1 << 16), /* VLAN tag available */
  533. };
  534. #define RsvdMask 0x3fffc000
  535. #define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
  536. struct TxDesc {
  537. __le32 opts1;
  538. __le32 opts2;
  539. __le64 addr;
  540. };
  541. struct RxDesc {
  542. __le32 opts1;
  543. __le32 opts2;
  544. __le64 addr;
  545. };
  546. struct ring_info {
  547. struct sk_buff *skb;
  548. u32 len;
  549. u8 __pad[sizeof(void *) - sizeof(u32)];
  550. };
  551. struct rtl8169_counters {
  552. __le64 tx_packets;
  553. __le64 rx_packets;
  554. __le64 tx_errors;
  555. __le32 rx_errors;
  556. __le16 rx_missed;
  557. __le16 align_errors;
  558. __le32 tx_one_collision;
  559. __le32 tx_multi_collision;
  560. __le64 rx_unicast;
  561. __le64 rx_broadcast;
  562. __le32 rx_multicast;
  563. __le16 tx_aborted;
  564. __le16 tx_underun;
  565. };
  566. struct rtl8169_tc_offsets {
  567. bool inited;
  568. __le64 tx_errors;
  569. __le32 tx_multi_collision;
  570. __le16 tx_aborted;
  571. };
  572. enum rtl_flag {
  573. RTL_FLAG_TASK_ENABLED = 0,
  574. RTL_FLAG_TASK_SLOW_PENDING,
  575. RTL_FLAG_TASK_RESET_PENDING,
  576. RTL_FLAG_MAX
  577. };
  578. struct rtl8169_stats {
  579. u64 packets;
  580. u64 bytes;
  581. struct u64_stats_sync syncp;
  582. };
  583. struct rtl8169_private {
  584. void __iomem *mmio_addr; /* memory map physical address */
  585. struct pci_dev *pci_dev;
  586. struct net_device *dev;
  587. struct napi_struct napi;
  588. u32 msg_enable;
  589. u16 mac_version;
  590. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  591. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  592. u32 dirty_tx;
  593. struct rtl8169_stats rx_stats;
  594. struct rtl8169_stats tx_stats;
  595. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  596. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  597. dma_addr_t TxPhyAddr;
  598. dma_addr_t RxPhyAddr;
  599. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  600. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  601. u16 cp_cmd;
  602. u16 event_slow;
  603. const struct rtl_coalesce_info *coalesce_info;
  604. struct clk *clk;
  605. struct mdio_ops {
  606. void (*write)(struct rtl8169_private *, int, int);
  607. int (*read)(struct rtl8169_private *, int);
  608. } mdio_ops;
  609. struct jumbo_ops {
  610. void (*enable)(struct rtl8169_private *);
  611. void (*disable)(struct rtl8169_private *);
  612. } jumbo_ops;
  613. void (*hw_start)(struct rtl8169_private *tp);
  614. bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
  615. struct {
  616. DECLARE_BITMAP(flags, RTL_FLAG_MAX);
  617. struct mutex mutex;
  618. struct work_struct work;
  619. } wk;
  620. unsigned supports_gmii:1;
  621. struct mii_bus *mii_bus;
  622. dma_addr_t counters_phys_addr;
  623. struct rtl8169_counters *counters;
  624. struct rtl8169_tc_offsets tc_offset;
  625. u32 saved_wolopts;
  626. struct rtl_fw {
  627. const struct firmware *fw;
  628. #define RTL_VER_SIZE 32
  629. char version[RTL_VER_SIZE];
  630. struct rtl_fw_phy_action {
  631. __le32 *code;
  632. size_t size;
  633. } phy_action;
  634. } *rtl_fw;
  635. #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
  636. u32 ocp_base;
  637. };
  638. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  639. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  640. module_param(use_dac, int, 0);
  641. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  642. module_param_named(debug, debug.msg_enable, int, 0);
  643. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  644. MODULE_SOFTDEP("pre: realtek");
  645. MODULE_LICENSE("GPL");
  646. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  647. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  648. MODULE_FIRMWARE(FIRMWARE_8168E_1);
  649. MODULE_FIRMWARE(FIRMWARE_8168E_2);
  650. MODULE_FIRMWARE(FIRMWARE_8168E_3);
  651. MODULE_FIRMWARE(FIRMWARE_8105E_1);
  652. MODULE_FIRMWARE(FIRMWARE_8168F_1);
  653. MODULE_FIRMWARE(FIRMWARE_8168F_2);
  654. MODULE_FIRMWARE(FIRMWARE_8402_1);
  655. MODULE_FIRMWARE(FIRMWARE_8411_1);
  656. MODULE_FIRMWARE(FIRMWARE_8411_2);
  657. MODULE_FIRMWARE(FIRMWARE_8106E_1);
  658. MODULE_FIRMWARE(FIRMWARE_8106E_2);
  659. MODULE_FIRMWARE(FIRMWARE_8168G_2);
  660. MODULE_FIRMWARE(FIRMWARE_8168G_3);
  661. MODULE_FIRMWARE(FIRMWARE_8168H_1);
  662. MODULE_FIRMWARE(FIRMWARE_8168H_2);
  663. MODULE_FIRMWARE(FIRMWARE_8107E_1);
  664. MODULE_FIRMWARE(FIRMWARE_8107E_2);
  665. static inline struct device *tp_to_dev(struct rtl8169_private *tp)
  666. {
  667. return &tp->pci_dev->dev;
  668. }
  669. static void rtl_lock_work(struct rtl8169_private *tp)
  670. {
  671. mutex_lock(&tp->wk.mutex);
  672. }
  673. static void rtl_unlock_work(struct rtl8169_private *tp)
  674. {
  675. mutex_unlock(&tp->wk.mutex);
  676. }
  677. static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
  678. {
  679. pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
  680. PCI_EXP_DEVCTL_READRQ, force);
  681. }
  682. struct rtl_cond {
  683. bool (*check)(struct rtl8169_private *);
  684. const char *msg;
  685. };
  686. static void rtl_udelay(unsigned int d)
  687. {
  688. udelay(d);
  689. }
  690. static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
  691. void (*delay)(unsigned int), unsigned int d, int n,
  692. bool high)
  693. {
  694. int i;
  695. for (i = 0; i < n; i++) {
  696. delay(d);
  697. if (c->check(tp) == high)
  698. return true;
  699. }
  700. netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
  701. c->msg, !high, n, d);
  702. return false;
  703. }
  704. static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
  705. const struct rtl_cond *c,
  706. unsigned int d, int n)
  707. {
  708. return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
  709. }
  710. static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
  711. const struct rtl_cond *c,
  712. unsigned int d, int n)
  713. {
  714. return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
  715. }
  716. static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
  717. const struct rtl_cond *c,
  718. unsigned int d, int n)
  719. {
  720. return rtl_loop_wait(tp, c, msleep, d, n, true);
  721. }
  722. static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
  723. const struct rtl_cond *c,
  724. unsigned int d, int n)
  725. {
  726. return rtl_loop_wait(tp, c, msleep, d, n, false);
  727. }
  728. #define DECLARE_RTL_COND(name) \
  729. static bool name ## _check(struct rtl8169_private *); \
  730. \
  731. static const struct rtl_cond name = { \
  732. .check = name ## _check, \
  733. .msg = #name \
  734. }; \
  735. \
  736. static bool name ## _check(struct rtl8169_private *tp)
  737. static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
  738. {
  739. if (reg & 0xffff0001) {
  740. netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
  741. return true;
  742. }
  743. return false;
  744. }
  745. DECLARE_RTL_COND(rtl_ocp_gphy_cond)
  746. {
  747. return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
  748. }
  749. static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  750. {
  751. if (rtl_ocp_reg_failure(tp, reg))
  752. return;
  753. RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
  754. rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
  755. }
  756. static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
  757. {
  758. if (rtl_ocp_reg_failure(tp, reg))
  759. return 0;
  760. RTL_W32(tp, GPHY_OCP, reg << 15);
  761. return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
  762. (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
  763. }
  764. static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  765. {
  766. if (rtl_ocp_reg_failure(tp, reg))
  767. return;
  768. RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
  769. }
  770. static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
  771. {
  772. if (rtl_ocp_reg_failure(tp, reg))
  773. return 0;
  774. RTL_W32(tp, OCPDR, reg << 15);
  775. return RTL_R32(tp, OCPDR);
  776. }
  777. #define OCP_STD_PHY_BASE 0xa400
  778. static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
  779. {
  780. if (reg == 0x1f) {
  781. tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
  782. return;
  783. }
  784. if (tp->ocp_base != OCP_STD_PHY_BASE)
  785. reg -= 0x10;
  786. r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
  787. }
  788. static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
  789. {
  790. if (tp->ocp_base != OCP_STD_PHY_BASE)
  791. reg -= 0x10;
  792. return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
  793. }
  794. static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
  795. {
  796. if (reg == 0x1f) {
  797. tp->ocp_base = value << 4;
  798. return;
  799. }
  800. r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
  801. }
  802. static int mac_mcu_read(struct rtl8169_private *tp, int reg)
  803. {
  804. return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
  805. }
  806. DECLARE_RTL_COND(rtl_phyar_cond)
  807. {
  808. return RTL_R32(tp, PHYAR) & 0x80000000;
  809. }
  810. static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
  811. {
  812. RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
  813. rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
  814. /*
  815. * According to hardware specs a 20us delay is required after write
  816. * complete indication, but before sending next command.
  817. */
  818. udelay(20);
  819. }
  820. static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
  821. {
  822. int value;
  823. RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
  824. value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
  825. RTL_R32(tp, PHYAR) & 0xffff : ~0;
  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. DECLARE_RTL_COND(rtl_ocpar_cond)
  834. {
  835. return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
  836. }
  837. static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
  838. {
  839. RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  840. RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
  841. RTL_W32(tp, EPHY_RXER_NUM, 0);
  842. rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
  843. }
  844. static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
  845. {
  846. r8168dp_1_mdio_access(tp, reg,
  847. OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
  848. }
  849. static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
  850. {
  851. r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
  852. mdelay(1);
  853. RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
  854. RTL_W32(tp, EPHY_RXER_NUM, 0);
  855. return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
  856. RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
  857. }
  858. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  859. static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
  860. {
  861. RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  862. }
  863. static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
  864. {
  865. RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  866. }
  867. static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
  868. {
  869. r8168dp_2_mdio_start(tp);
  870. r8169_mdio_write(tp, reg, value);
  871. r8168dp_2_mdio_stop(tp);
  872. }
  873. static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
  874. {
  875. int value;
  876. /* Work around issue with chip reporting wrong PHY ID */
  877. if (reg == MII_PHYSID2)
  878. return 0xc912;
  879. r8168dp_2_mdio_start(tp);
  880. value = r8169_mdio_read(tp, reg);
  881. r8168dp_2_mdio_stop(tp);
  882. return value;
  883. }
  884. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  885. {
  886. tp->mdio_ops.write(tp, location, val);
  887. }
  888. static int rtl_readphy(struct rtl8169_private *tp, int location)
  889. {
  890. return tp->mdio_ops.read(tp, location);
  891. }
  892. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  893. {
  894. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  895. }
  896. static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  897. {
  898. int val;
  899. val = rtl_readphy(tp, reg_addr);
  900. rtl_writephy(tp, reg_addr, (val & ~m) | p);
  901. }
  902. DECLARE_RTL_COND(rtl_ephyar_cond)
  903. {
  904. return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
  905. }
  906. static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
  907. {
  908. RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  909. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  910. rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
  911. udelay(10);
  912. }
  913. static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
  914. {
  915. RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  916. return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
  917. RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
  918. }
  919. DECLARE_RTL_COND(rtl_eriar_cond)
  920. {
  921. return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
  922. }
  923. static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
  924. u32 val, int type)
  925. {
  926. BUG_ON((addr & 3) || (mask == 0));
  927. RTL_W32(tp, ERIDR, val);
  928. RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
  929. rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
  930. }
  931. static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
  932. {
  933. RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
  934. return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
  935. RTL_R32(tp, ERIDR) : ~0;
  936. }
  937. static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
  938. u32 m, int type)
  939. {
  940. u32 val;
  941. val = rtl_eri_read(tp, addr, type);
  942. rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
  943. }
  944. static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  945. {
  946. RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  947. return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
  948. RTL_R32(tp, OCPDR) : ~0;
  949. }
  950. static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  951. {
  952. return rtl_eri_read(tp, reg, ERIAR_OOB);
  953. }
  954. static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  955. {
  956. switch (tp->mac_version) {
  957. case RTL_GIGA_MAC_VER_27:
  958. case RTL_GIGA_MAC_VER_28:
  959. case RTL_GIGA_MAC_VER_31:
  960. return r8168dp_ocp_read(tp, mask, reg);
  961. case RTL_GIGA_MAC_VER_49:
  962. case RTL_GIGA_MAC_VER_50:
  963. case RTL_GIGA_MAC_VER_51:
  964. return r8168ep_ocp_read(tp, mask, reg);
  965. default:
  966. BUG();
  967. return ~0;
  968. }
  969. }
  970. static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  971. u32 data)
  972. {
  973. RTL_W32(tp, OCPDR, data);
  974. RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  975. rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
  976. }
  977. static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  978. u32 data)
  979. {
  980. rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
  981. data, ERIAR_OOB);
  982. }
  983. static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
  984. {
  985. switch (tp->mac_version) {
  986. case RTL_GIGA_MAC_VER_27:
  987. case RTL_GIGA_MAC_VER_28:
  988. case RTL_GIGA_MAC_VER_31:
  989. r8168dp_ocp_write(tp, mask, reg, data);
  990. break;
  991. case RTL_GIGA_MAC_VER_49:
  992. case RTL_GIGA_MAC_VER_50:
  993. case RTL_GIGA_MAC_VER_51:
  994. r8168ep_ocp_write(tp, mask, reg, data);
  995. break;
  996. default:
  997. BUG();
  998. break;
  999. }
  1000. }
  1001. static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
  1002. {
  1003. rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
  1004. ocp_write(tp, 0x1, 0x30, 0x00000001);
  1005. }
  1006. #define OOB_CMD_RESET 0x00
  1007. #define OOB_CMD_DRIVER_START 0x05
  1008. #define OOB_CMD_DRIVER_STOP 0x06
  1009. static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
  1010. {
  1011. return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
  1012. }
  1013. DECLARE_RTL_COND(rtl_ocp_read_cond)
  1014. {
  1015. u16 reg;
  1016. reg = rtl8168_get_ocp_reg(tp);
  1017. return ocp_read(tp, 0x0f, reg) & 0x00000800;
  1018. }
  1019. DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
  1020. {
  1021. return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
  1022. }
  1023. DECLARE_RTL_COND(rtl_ocp_tx_cond)
  1024. {
  1025. return RTL_R8(tp, IBISR0) & 0x20;
  1026. }
  1027. static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
  1028. {
  1029. RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
  1030. rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
  1031. RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
  1032. RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
  1033. }
  1034. static void rtl8168dp_driver_start(struct rtl8169_private *tp)
  1035. {
  1036. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  1037. rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
  1038. }
  1039. static void rtl8168ep_driver_start(struct rtl8169_private *tp)
  1040. {
  1041. ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
  1042. ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
  1043. rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
  1044. }
  1045. static void rtl8168_driver_start(struct rtl8169_private *tp)
  1046. {
  1047. switch (tp->mac_version) {
  1048. case RTL_GIGA_MAC_VER_27:
  1049. case RTL_GIGA_MAC_VER_28:
  1050. case RTL_GIGA_MAC_VER_31:
  1051. rtl8168dp_driver_start(tp);
  1052. break;
  1053. case RTL_GIGA_MAC_VER_49:
  1054. case RTL_GIGA_MAC_VER_50:
  1055. case RTL_GIGA_MAC_VER_51:
  1056. rtl8168ep_driver_start(tp);
  1057. break;
  1058. default:
  1059. BUG();
  1060. break;
  1061. }
  1062. }
  1063. static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
  1064. {
  1065. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  1066. rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
  1067. }
  1068. static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
  1069. {
  1070. rtl8168ep_stop_cmac(tp);
  1071. ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
  1072. ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
  1073. rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
  1074. }
  1075. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  1076. {
  1077. switch (tp->mac_version) {
  1078. case RTL_GIGA_MAC_VER_27:
  1079. case RTL_GIGA_MAC_VER_28:
  1080. case RTL_GIGA_MAC_VER_31:
  1081. rtl8168dp_driver_stop(tp);
  1082. break;
  1083. case RTL_GIGA_MAC_VER_49:
  1084. case RTL_GIGA_MAC_VER_50:
  1085. case RTL_GIGA_MAC_VER_51:
  1086. rtl8168ep_driver_stop(tp);
  1087. break;
  1088. default:
  1089. BUG();
  1090. break;
  1091. }
  1092. }
  1093. static bool r8168dp_check_dash(struct rtl8169_private *tp)
  1094. {
  1095. u16 reg = rtl8168_get_ocp_reg(tp);
  1096. return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
  1097. }
  1098. static bool r8168ep_check_dash(struct rtl8169_private *tp)
  1099. {
  1100. return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
  1101. }
  1102. static bool r8168_check_dash(struct rtl8169_private *tp)
  1103. {
  1104. switch (tp->mac_version) {
  1105. case RTL_GIGA_MAC_VER_27:
  1106. case RTL_GIGA_MAC_VER_28:
  1107. case RTL_GIGA_MAC_VER_31:
  1108. return r8168dp_check_dash(tp);
  1109. case RTL_GIGA_MAC_VER_49:
  1110. case RTL_GIGA_MAC_VER_50:
  1111. case RTL_GIGA_MAC_VER_51:
  1112. return r8168ep_check_dash(tp);
  1113. default:
  1114. return false;
  1115. }
  1116. }
  1117. struct exgmac_reg {
  1118. u16 addr;
  1119. u16 mask;
  1120. u32 val;
  1121. };
  1122. static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
  1123. const struct exgmac_reg *r, int len)
  1124. {
  1125. while (len-- > 0) {
  1126. rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
  1127. r++;
  1128. }
  1129. }
  1130. DECLARE_RTL_COND(rtl_efusear_cond)
  1131. {
  1132. return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
  1133. }
  1134. static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
  1135. {
  1136. RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  1137. return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
  1138. RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
  1139. }
  1140. static u16 rtl_get_events(struct rtl8169_private *tp)
  1141. {
  1142. return RTL_R16(tp, IntrStatus);
  1143. }
  1144. static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
  1145. {
  1146. RTL_W16(tp, IntrStatus, bits);
  1147. mmiowb();
  1148. }
  1149. static void rtl_irq_disable(struct rtl8169_private *tp)
  1150. {
  1151. RTL_W16(tp, IntrMask, 0);
  1152. mmiowb();
  1153. }
  1154. static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
  1155. {
  1156. RTL_W16(tp, IntrMask, bits);
  1157. }
  1158. #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
  1159. #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
  1160. #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
  1161. static void rtl_irq_enable_all(struct rtl8169_private *tp)
  1162. {
  1163. rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
  1164. }
  1165. static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
  1166. {
  1167. rtl_irq_disable(tp);
  1168. rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
  1169. RTL_R8(tp, ChipCmd);
  1170. }
  1171. static void rtl_link_chg_patch(struct rtl8169_private *tp)
  1172. {
  1173. struct net_device *dev = tp->dev;
  1174. struct phy_device *phydev = dev->phydev;
  1175. if (!netif_running(dev))
  1176. return;
  1177. if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  1178. tp->mac_version == RTL_GIGA_MAC_VER_38) {
  1179. if (phydev->speed == SPEED_1000) {
  1180. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
  1181. ERIAR_EXGMAC);
  1182. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1183. ERIAR_EXGMAC);
  1184. } else if (phydev->speed == SPEED_100) {
  1185. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1186. ERIAR_EXGMAC);
  1187. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1188. ERIAR_EXGMAC);
  1189. } else {
  1190. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1191. ERIAR_EXGMAC);
  1192. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
  1193. ERIAR_EXGMAC);
  1194. }
  1195. /* Reset packet filter */
  1196. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
  1197. ERIAR_EXGMAC);
  1198. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
  1199. ERIAR_EXGMAC);
  1200. } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  1201. tp->mac_version == RTL_GIGA_MAC_VER_36) {
  1202. if (phydev->speed == SPEED_1000) {
  1203. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
  1204. ERIAR_EXGMAC);
  1205. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1206. ERIAR_EXGMAC);
  1207. } else {
  1208. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1209. ERIAR_EXGMAC);
  1210. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
  1211. ERIAR_EXGMAC);
  1212. }
  1213. } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
  1214. if (phydev->speed == SPEED_10) {
  1215. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
  1216. ERIAR_EXGMAC);
  1217. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
  1218. ERIAR_EXGMAC);
  1219. } else {
  1220. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
  1221. ERIAR_EXGMAC);
  1222. }
  1223. }
  1224. }
  1225. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1226. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  1227. {
  1228. u8 options;
  1229. u32 wolopts = 0;
  1230. options = RTL_R8(tp, Config1);
  1231. if (!(options & PMEnable))
  1232. return 0;
  1233. options = RTL_R8(tp, Config3);
  1234. if (options & LinkUp)
  1235. wolopts |= WAKE_PHY;
  1236. switch (tp->mac_version) {
  1237. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  1238. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  1239. if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
  1240. wolopts |= WAKE_MAGIC;
  1241. break;
  1242. default:
  1243. if (options & MagicPacket)
  1244. wolopts |= WAKE_MAGIC;
  1245. break;
  1246. }
  1247. options = RTL_R8(tp, Config5);
  1248. if (options & UWF)
  1249. wolopts |= WAKE_UCAST;
  1250. if (options & BWF)
  1251. wolopts |= WAKE_BCAST;
  1252. if (options & MWF)
  1253. wolopts |= WAKE_MCAST;
  1254. return wolopts;
  1255. }
  1256. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1257. {
  1258. struct rtl8169_private *tp = netdev_priv(dev);
  1259. rtl_lock_work(tp);
  1260. wol->supported = WAKE_ANY;
  1261. wol->wolopts = tp->saved_wolopts;
  1262. rtl_unlock_work(tp);
  1263. }
  1264. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  1265. {
  1266. unsigned int i, tmp;
  1267. static const struct {
  1268. u32 opt;
  1269. u16 reg;
  1270. u8 mask;
  1271. } cfg[] = {
  1272. { WAKE_PHY, Config3, LinkUp },
  1273. { WAKE_UCAST, Config5, UWF },
  1274. { WAKE_BCAST, Config5, BWF },
  1275. { WAKE_MCAST, Config5, MWF },
  1276. { WAKE_ANY, Config5, LanWake },
  1277. { WAKE_MAGIC, Config3, MagicPacket }
  1278. };
  1279. u8 options;
  1280. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  1281. switch (tp->mac_version) {
  1282. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  1283. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  1284. tmp = ARRAY_SIZE(cfg) - 1;
  1285. if (wolopts & WAKE_MAGIC)
  1286. rtl_w0w1_eri(tp,
  1287. 0x0dc,
  1288. ERIAR_MASK_0100,
  1289. MagicPacket_v2,
  1290. 0x0000,
  1291. ERIAR_EXGMAC);
  1292. else
  1293. rtl_w0w1_eri(tp,
  1294. 0x0dc,
  1295. ERIAR_MASK_0100,
  1296. 0x0000,
  1297. MagicPacket_v2,
  1298. ERIAR_EXGMAC);
  1299. break;
  1300. default:
  1301. tmp = ARRAY_SIZE(cfg);
  1302. break;
  1303. }
  1304. for (i = 0; i < tmp; i++) {
  1305. options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
  1306. if (wolopts & cfg[i].opt)
  1307. options |= cfg[i].mask;
  1308. RTL_W8(tp, cfg[i].reg, options);
  1309. }
  1310. switch (tp->mac_version) {
  1311. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
  1312. options = RTL_R8(tp, Config1) & ~PMEnable;
  1313. if (wolopts)
  1314. options |= PMEnable;
  1315. RTL_W8(tp, Config1, options);
  1316. break;
  1317. default:
  1318. options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
  1319. if (wolopts)
  1320. options |= PME_SIGNAL;
  1321. RTL_W8(tp, Config2, options);
  1322. break;
  1323. }
  1324. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  1325. device_set_wakeup_enable(tp_to_dev(tp), wolopts);
  1326. }
  1327. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1328. {
  1329. struct rtl8169_private *tp = netdev_priv(dev);
  1330. struct device *d = tp_to_dev(tp);
  1331. if (wol->wolopts & ~WAKE_ANY)
  1332. return -EINVAL;
  1333. pm_runtime_get_noresume(d);
  1334. rtl_lock_work(tp);
  1335. tp->saved_wolopts = wol->wolopts;
  1336. if (pm_runtime_active(d))
  1337. __rtl8169_set_wol(tp, tp->saved_wolopts);
  1338. rtl_unlock_work(tp);
  1339. pm_runtime_put_noidle(d);
  1340. return 0;
  1341. }
  1342. static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
  1343. {
  1344. return rtl_chip_infos[tp->mac_version].fw_name;
  1345. }
  1346. static void rtl8169_get_drvinfo(struct net_device *dev,
  1347. struct ethtool_drvinfo *info)
  1348. {
  1349. struct rtl8169_private *tp = netdev_priv(dev);
  1350. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1351. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  1352. strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
  1353. BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
  1354. if (!IS_ERR_OR_NULL(rtl_fw))
  1355. strlcpy(info->fw_version, rtl_fw->version,
  1356. sizeof(info->fw_version));
  1357. }
  1358. static int rtl8169_get_regs_len(struct net_device *dev)
  1359. {
  1360. return R8169_REGS_SIZE;
  1361. }
  1362. static netdev_features_t rtl8169_fix_features(struct net_device *dev,
  1363. netdev_features_t features)
  1364. {
  1365. struct rtl8169_private *tp = netdev_priv(dev);
  1366. if (dev->mtu > TD_MSS_MAX)
  1367. features &= ~NETIF_F_ALL_TSO;
  1368. if (dev->mtu > JUMBO_1K &&
  1369. tp->mac_version > RTL_GIGA_MAC_VER_06)
  1370. features &= ~NETIF_F_IP_CSUM;
  1371. return features;
  1372. }
  1373. static int rtl8169_set_features(struct net_device *dev,
  1374. netdev_features_t features)
  1375. {
  1376. struct rtl8169_private *tp = netdev_priv(dev);
  1377. u32 rx_config;
  1378. rtl_lock_work(tp);
  1379. rx_config = RTL_R32(tp, RxConfig);
  1380. if (features & NETIF_F_RXALL)
  1381. rx_config |= (AcceptErr | AcceptRunt);
  1382. else
  1383. rx_config &= ~(AcceptErr | AcceptRunt);
  1384. RTL_W32(tp, RxConfig, rx_config);
  1385. if (features & NETIF_F_RXCSUM)
  1386. tp->cp_cmd |= RxChkSum;
  1387. else
  1388. tp->cp_cmd &= ~RxChkSum;
  1389. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1390. tp->cp_cmd |= RxVlan;
  1391. else
  1392. tp->cp_cmd &= ~RxVlan;
  1393. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  1394. RTL_R16(tp, CPlusCmd);
  1395. rtl_unlock_work(tp);
  1396. return 0;
  1397. }
  1398. static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
  1399. {
  1400. return (skb_vlan_tag_present(skb)) ?
  1401. TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
  1402. }
  1403. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1404. {
  1405. u32 opts2 = le32_to_cpu(desc->opts2);
  1406. if (opts2 & RxVlanTag)
  1407. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
  1408. }
  1409. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1410. void *p)
  1411. {
  1412. struct rtl8169_private *tp = netdev_priv(dev);
  1413. u32 __iomem *data = tp->mmio_addr;
  1414. u32 *dw = p;
  1415. int i;
  1416. rtl_lock_work(tp);
  1417. for (i = 0; i < R8169_REGS_SIZE; i += 4)
  1418. memcpy_fromio(dw++, data++, 4);
  1419. rtl_unlock_work(tp);
  1420. }
  1421. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1422. {
  1423. struct rtl8169_private *tp = netdev_priv(dev);
  1424. return tp->msg_enable;
  1425. }
  1426. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1427. {
  1428. struct rtl8169_private *tp = netdev_priv(dev);
  1429. tp->msg_enable = value;
  1430. }
  1431. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1432. "tx_packets",
  1433. "rx_packets",
  1434. "tx_errors",
  1435. "rx_errors",
  1436. "rx_missed",
  1437. "align_errors",
  1438. "tx_single_collisions",
  1439. "tx_multi_collisions",
  1440. "unicast",
  1441. "broadcast",
  1442. "multicast",
  1443. "tx_aborted",
  1444. "tx_underrun",
  1445. };
  1446. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1447. {
  1448. switch (sset) {
  1449. case ETH_SS_STATS:
  1450. return ARRAY_SIZE(rtl8169_gstrings);
  1451. default:
  1452. return -EOPNOTSUPP;
  1453. }
  1454. }
  1455. DECLARE_RTL_COND(rtl_counters_cond)
  1456. {
  1457. return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
  1458. }
  1459. static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
  1460. {
  1461. dma_addr_t paddr = tp->counters_phys_addr;
  1462. u32 cmd;
  1463. RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
  1464. RTL_R32(tp, CounterAddrHigh);
  1465. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1466. RTL_W32(tp, CounterAddrLow, cmd);
  1467. RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
  1468. return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
  1469. }
  1470. static bool rtl8169_reset_counters(struct rtl8169_private *tp)
  1471. {
  1472. /*
  1473. * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
  1474. * tally counters.
  1475. */
  1476. if (tp->mac_version < RTL_GIGA_MAC_VER_19)
  1477. return true;
  1478. return rtl8169_do_counters(tp, CounterReset);
  1479. }
  1480. static bool rtl8169_update_counters(struct rtl8169_private *tp)
  1481. {
  1482. u8 val = RTL_R8(tp, ChipCmd);
  1483. /*
  1484. * Some chips are unable to dump tally counters when the receiver
  1485. * is disabled. If 0xff chip may be in a PCI power-save state.
  1486. */
  1487. if (!(val & CmdRxEnb) || val == 0xff)
  1488. return true;
  1489. return rtl8169_do_counters(tp, CounterDump);
  1490. }
  1491. static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
  1492. {
  1493. struct rtl8169_counters *counters = tp->counters;
  1494. bool ret = false;
  1495. /*
  1496. * rtl8169_init_counter_offsets is called from rtl_open. On chip
  1497. * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
  1498. * reset by a power cycle, while the counter values collected by the
  1499. * driver are reset at every driver unload/load cycle.
  1500. *
  1501. * To make sure the HW values returned by @get_stats64 match the SW
  1502. * values, we collect the initial values at first open(*) and use them
  1503. * as offsets to normalize the values returned by @get_stats64.
  1504. *
  1505. * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
  1506. * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
  1507. * set at open time by rtl_hw_start.
  1508. */
  1509. if (tp->tc_offset.inited)
  1510. return true;
  1511. /* If both, reset and update fail, propagate to caller. */
  1512. if (rtl8169_reset_counters(tp))
  1513. ret = true;
  1514. if (rtl8169_update_counters(tp))
  1515. ret = true;
  1516. tp->tc_offset.tx_errors = counters->tx_errors;
  1517. tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
  1518. tp->tc_offset.tx_aborted = counters->tx_aborted;
  1519. tp->tc_offset.inited = true;
  1520. return ret;
  1521. }
  1522. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1523. struct ethtool_stats *stats, u64 *data)
  1524. {
  1525. struct rtl8169_private *tp = netdev_priv(dev);
  1526. struct device *d = tp_to_dev(tp);
  1527. struct rtl8169_counters *counters = tp->counters;
  1528. ASSERT_RTNL();
  1529. pm_runtime_get_noresume(d);
  1530. if (pm_runtime_active(d))
  1531. rtl8169_update_counters(tp);
  1532. pm_runtime_put_noidle(d);
  1533. data[0] = le64_to_cpu(counters->tx_packets);
  1534. data[1] = le64_to_cpu(counters->rx_packets);
  1535. data[2] = le64_to_cpu(counters->tx_errors);
  1536. data[3] = le32_to_cpu(counters->rx_errors);
  1537. data[4] = le16_to_cpu(counters->rx_missed);
  1538. data[5] = le16_to_cpu(counters->align_errors);
  1539. data[6] = le32_to_cpu(counters->tx_one_collision);
  1540. data[7] = le32_to_cpu(counters->tx_multi_collision);
  1541. data[8] = le64_to_cpu(counters->rx_unicast);
  1542. data[9] = le64_to_cpu(counters->rx_broadcast);
  1543. data[10] = le32_to_cpu(counters->rx_multicast);
  1544. data[11] = le16_to_cpu(counters->tx_aborted);
  1545. data[12] = le16_to_cpu(counters->tx_underun);
  1546. }
  1547. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1548. {
  1549. switch(stringset) {
  1550. case ETH_SS_STATS:
  1551. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1552. break;
  1553. }
  1554. }
  1555. /*
  1556. * Interrupt coalescing
  1557. *
  1558. * > 1 - the availability of the IntrMitigate (0xe2) register through the
  1559. * > 8169, 8168 and 810x line of chipsets
  1560. *
  1561. * 8169, 8168, and 8136(810x) serial chipsets support it.
  1562. *
  1563. * > 2 - the Tx timer unit at gigabit speed
  1564. *
  1565. * The unit of the timer depends on both the speed and the setting of CPlusCmd
  1566. * (0xe0) bit 1 and bit 0.
  1567. *
  1568. * For 8169
  1569. * bit[1:0] \ speed 1000M 100M 10M
  1570. * 0 0 320ns 2.56us 40.96us
  1571. * 0 1 2.56us 20.48us 327.7us
  1572. * 1 0 5.12us 40.96us 655.4us
  1573. * 1 1 10.24us 81.92us 1.31ms
  1574. *
  1575. * For the other
  1576. * bit[1:0] \ speed 1000M 100M 10M
  1577. * 0 0 5us 2.56us 40.96us
  1578. * 0 1 40us 20.48us 327.7us
  1579. * 1 0 80us 40.96us 655.4us
  1580. * 1 1 160us 81.92us 1.31ms
  1581. */
  1582. /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
  1583. struct rtl_coalesce_scale {
  1584. /* Rx / Tx */
  1585. u32 nsecs[2];
  1586. };
  1587. /* rx/tx scale factors for all CPlusCmd[0:1] cases */
  1588. struct rtl_coalesce_info {
  1589. u32 speed;
  1590. struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
  1591. };
  1592. /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
  1593. #define rxtx_x1822(r, t) { \
  1594. {{(r), (t)}}, \
  1595. {{(r)*8, (t)*8}}, \
  1596. {{(r)*8*2, (t)*8*2}}, \
  1597. {{(r)*8*2*2, (t)*8*2*2}}, \
  1598. }
  1599. static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
  1600. /* speed delays: rx00 tx00 */
  1601. { SPEED_10, rxtx_x1822(40960, 40960) },
  1602. { SPEED_100, rxtx_x1822( 2560, 2560) },
  1603. { SPEED_1000, rxtx_x1822( 320, 320) },
  1604. { 0 },
  1605. };
  1606. static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
  1607. /* speed delays: rx00 tx00 */
  1608. { SPEED_10, rxtx_x1822(40960, 40960) },
  1609. { SPEED_100, rxtx_x1822( 2560, 2560) },
  1610. { SPEED_1000, rxtx_x1822( 5000, 5000) },
  1611. { 0 },
  1612. };
  1613. #undef rxtx_x1822
  1614. /* get rx/tx scale vector corresponding to current speed */
  1615. static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
  1616. {
  1617. struct rtl8169_private *tp = netdev_priv(dev);
  1618. struct ethtool_link_ksettings ecmd;
  1619. const struct rtl_coalesce_info *ci;
  1620. int rc;
  1621. rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
  1622. if (rc < 0)
  1623. return ERR_PTR(rc);
  1624. for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
  1625. if (ecmd.base.speed == ci->speed) {
  1626. return ci;
  1627. }
  1628. }
  1629. return ERR_PTR(-ELNRNG);
  1630. }
  1631. static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1632. {
  1633. struct rtl8169_private *tp = netdev_priv(dev);
  1634. const struct rtl_coalesce_info *ci;
  1635. const struct rtl_coalesce_scale *scale;
  1636. struct {
  1637. u32 *max_frames;
  1638. u32 *usecs;
  1639. } coal_settings [] = {
  1640. { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
  1641. { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
  1642. }, *p = coal_settings;
  1643. int i;
  1644. u16 w;
  1645. memset(ec, 0, sizeof(*ec));
  1646. /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
  1647. ci = rtl_coalesce_info(dev);
  1648. if (IS_ERR(ci))
  1649. return PTR_ERR(ci);
  1650. scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
  1651. /* read IntrMitigate and adjust according to scale */
  1652. for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
  1653. *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
  1654. w >>= RTL_COALESCE_SHIFT;
  1655. *p->usecs = w & RTL_COALESCE_MASK;
  1656. }
  1657. for (i = 0; i < 2; i++) {
  1658. p = coal_settings + i;
  1659. *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
  1660. /*
  1661. * ethtool_coalesce says it is illegal to set both usecs and
  1662. * max_frames to 0.
  1663. */
  1664. if (!*p->usecs && !*p->max_frames)
  1665. *p->max_frames = 1;
  1666. }
  1667. return 0;
  1668. }
  1669. /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
  1670. static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
  1671. struct net_device *dev, u32 nsec, u16 *cp01)
  1672. {
  1673. const struct rtl_coalesce_info *ci;
  1674. u16 i;
  1675. ci = rtl_coalesce_info(dev);
  1676. if (IS_ERR(ci))
  1677. return ERR_CAST(ci);
  1678. for (i = 0; i < 4; i++) {
  1679. u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
  1680. ci->scalev[i].nsecs[1]);
  1681. if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
  1682. *cp01 = i;
  1683. return &ci->scalev[i];
  1684. }
  1685. }
  1686. return ERR_PTR(-EINVAL);
  1687. }
  1688. static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1689. {
  1690. struct rtl8169_private *tp = netdev_priv(dev);
  1691. const struct rtl_coalesce_scale *scale;
  1692. struct {
  1693. u32 frames;
  1694. u32 usecs;
  1695. } coal_settings [] = {
  1696. { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
  1697. { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
  1698. }, *p = coal_settings;
  1699. u16 w = 0, cp01;
  1700. int i;
  1701. scale = rtl_coalesce_choose_scale(dev,
  1702. max(p[0].usecs, p[1].usecs) * 1000, &cp01);
  1703. if (IS_ERR(scale))
  1704. return PTR_ERR(scale);
  1705. for (i = 0; i < 2; i++, p++) {
  1706. u32 units;
  1707. /*
  1708. * accept max_frames=1 we returned in rtl_get_coalesce.
  1709. * accept it not only when usecs=0 because of e.g. the following scenario:
  1710. *
  1711. * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
  1712. * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
  1713. * - then user does `ethtool -C eth0 rx-usecs 100`
  1714. *
  1715. * since ethtool sends to kernel whole ethtool_coalesce
  1716. * settings, if we do not handle rx_usecs=!0, rx_frames=1
  1717. * we'll reject it below in `frames % 4 != 0`.
  1718. */
  1719. if (p->frames == 1) {
  1720. p->frames = 0;
  1721. }
  1722. units = p->usecs * 1000 / scale->nsecs[i];
  1723. if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
  1724. return -EINVAL;
  1725. w <<= RTL_COALESCE_SHIFT;
  1726. w |= units;
  1727. w <<= RTL_COALESCE_SHIFT;
  1728. w |= p->frames >> 2;
  1729. }
  1730. rtl_lock_work(tp);
  1731. RTL_W16(tp, IntrMitigate, swab16(w));
  1732. tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
  1733. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  1734. RTL_R16(tp, CPlusCmd);
  1735. rtl_unlock_work(tp);
  1736. return 0;
  1737. }
  1738. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1739. .get_drvinfo = rtl8169_get_drvinfo,
  1740. .get_regs_len = rtl8169_get_regs_len,
  1741. .get_link = ethtool_op_get_link,
  1742. .get_coalesce = rtl_get_coalesce,
  1743. .set_coalesce = rtl_set_coalesce,
  1744. .get_msglevel = rtl8169_get_msglevel,
  1745. .set_msglevel = rtl8169_set_msglevel,
  1746. .get_regs = rtl8169_get_regs,
  1747. .get_wol = rtl8169_get_wol,
  1748. .set_wol = rtl8169_set_wol,
  1749. .get_strings = rtl8169_get_strings,
  1750. .get_sset_count = rtl8169_get_sset_count,
  1751. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1752. .get_ts_info = ethtool_op_get_ts_info,
  1753. .nway_reset = phy_ethtool_nway_reset,
  1754. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1755. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1756. };
  1757. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1758. u8 default_version)
  1759. {
  1760. /*
  1761. * The driver currently handles the 8168Bf and the 8168Be identically
  1762. * but they can be identified more specifically through the test below
  1763. * if needed:
  1764. *
  1765. * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1766. *
  1767. * Same thing for the 8101Eb and the 8101Ec:
  1768. *
  1769. * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1770. */
  1771. static const struct rtl_mac_info {
  1772. u32 mask;
  1773. u32 val;
  1774. int mac_version;
  1775. } mac_info[] = {
  1776. /* 8168EP family. */
  1777. { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
  1778. { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
  1779. { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
  1780. /* 8168H family. */
  1781. { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
  1782. { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
  1783. /* 8168G family. */
  1784. { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
  1785. { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
  1786. { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
  1787. { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
  1788. /* 8168F family. */
  1789. { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
  1790. { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
  1791. { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
  1792. /* 8168E family. */
  1793. { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
  1794. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  1795. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  1796. /* 8168D family. */
  1797. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1798. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1799. /* 8168DP family. */
  1800. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1801. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  1802. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  1803. /* 8168C family. */
  1804. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1805. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1806. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1807. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1808. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1809. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1810. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1811. /* 8168B family. */
  1812. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1813. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1814. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1815. /* 8101 family. */
  1816. { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
  1817. { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
  1818. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  1819. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  1820. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1821. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1822. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1823. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1824. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1825. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1826. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1827. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1828. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1829. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1830. /* FIXME: where did these entries come from ? -- FR */
  1831. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1832. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1833. /* 8110 family. */
  1834. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1835. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1836. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1837. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1838. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1839. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1840. /* Catch-all */
  1841. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1842. };
  1843. const struct rtl_mac_info *p = mac_info;
  1844. u32 reg;
  1845. reg = RTL_R32(tp, TxConfig);
  1846. while ((reg & p->mask) != p->val)
  1847. p++;
  1848. tp->mac_version = p->mac_version;
  1849. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  1850. dev_notice(tp_to_dev(tp),
  1851. "unknown MAC, using family default\n");
  1852. tp->mac_version = default_version;
  1853. } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
  1854. tp->mac_version = tp->supports_gmii ?
  1855. RTL_GIGA_MAC_VER_42 :
  1856. RTL_GIGA_MAC_VER_43;
  1857. } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
  1858. tp->mac_version = tp->supports_gmii ?
  1859. RTL_GIGA_MAC_VER_45 :
  1860. RTL_GIGA_MAC_VER_47;
  1861. } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
  1862. tp->mac_version = tp->supports_gmii ?
  1863. RTL_GIGA_MAC_VER_46 :
  1864. RTL_GIGA_MAC_VER_48;
  1865. }
  1866. }
  1867. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1868. {
  1869. netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
  1870. }
  1871. struct phy_reg {
  1872. u16 reg;
  1873. u16 val;
  1874. };
  1875. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1876. const struct phy_reg *regs, int len)
  1877. {
  1878. while (len-- > 0) {
  1879. rtl_writephy(tp, regs->reg, regs->val);
  1880. regs++;
  1881. }
  1882. }
  1883. #define PHY_READ 0x00000000
  1884. #define PHY_DATA_OR 0x10000000
  1885. #define PHY_DATA_AND 0x20000000
  1886. #define PHY_BJMPN 0x30000000
  1887. #define PHY_MDIO_CHG 0x40000000
  1888. #define PHY_CLEAR_READCOUNT 0x70000000
  1889. #define PHY_WRITE 0x80000000
  1890. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1891. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1892. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1893. #define PHY_WRITE_PREVIOUS 0xc0000000
  1894. #define PHY_SKIPN 0xd0000000
  1895. #define PHY_DELAY_MS 0xe0000000
  1896. struct fw_info {
  1897. u32 magic;
  1898. char version[RTL_VER_SIZE];
  1899. __le32 fw_start;
  1900. __le32 fw_len;
  1901. u8 chksum;
  1902. } __packed;
  1903. #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
  1904. static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1905. {
  1906. const struct firmware *fw = rtl_fw->fw;
  1907. struct fw_info *fw_info = (struct fw_info *)fw->data;
  1908. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  1909. char *version = rtl_fw->version;
  1910. bool rc = false;
  1911. if (fw->size < FW_OPCODE_SIZE)
  1912. goto out;
  1913. if (!fw_info->magic) {
  1914. size_t i, size, start;
  1915. u8 checksum = 0;
  1916. if (fw->size < sizeof(*fw_info))
  1917. goto out;
  1918. for (i = 0; i < fw->size; i++)
  1919. checksum += fw->data[i];
  1920. if (checksum != 0)
  1921. goto out;
  1922. start = le32_to_cpu(fw_info->fw_start);
  1923. if (start > fw->size)
  1924. goto out;
  1925. size = le32_to_cpu(fw_info->fw_len);
  1926. if (size > (fw->size - start) / FW_OPCODE_SIZE)
  1927. goto out;
  1928. memcpy(version, fw_info->version, RTL_VER_SIZE);
  1929. pa->code = (__le32 *)(fw->data + start);
  1930. pa->size = size;
  1931. } else {
  1932. if (fw->size % FW_OPCODE_SIZE)
  1933. goto out;
  1934. strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
  1935. pa->code = (__le32 *)fw->data;
  1936. pa->size = fw->size / FW_OPCODE_SIZE;
  1937. }
  1938. version[RTL_VER_SIZE - 1] = 0;
  1939. rc = true;
  1940. out:
  1941. return rc;
  1942. }
  1943. static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
  1944. struct rtl_fw_phy_action *pa)
  1945. {
  1946. bool rc = false;
  1947. size_t index;
  1948. for (index = 0; index < pa->size; index++) {
  1949. u32 action = le32_to_cpu(pa->code[index]);
  1950. u32 regno = (action & 0x0fff0000) >> 16;
  1951. switch(action & 0xf0000000) {
  1952. case PHY_READ:
  1953. case PHY_DATA_OR:
  1954. case PHY_DATA_AND:
  1955. case PHY_MDIO_CHG:
  1956. case PHY_CLEAR_READCOUNT:
  1957. case PHY_WRITE:
  1958. case PHY_WRITE_PREVIOUS:
  1959. case PHY_DELAY_MS:
  1960. break;
  1961. case PHY_BJMPN:
  1962. if (regno > index) {
  1963. netif_err(tp, ifup, tp->dev,
  1964. "Out of range of firmware\n");
  1965. goto out;
  1966. }
  1967. break;
  1968. case PHY_READCOUNT_EQ_SKIP:
  1969. if (index + 2 >= pa->size) {
  1970. netif_err(tp, ifup, tp->dev,
  1971. "Out of range of firmware\n");
  1972. goto out;
  1973. }
  1974. break;
  1975. case PHY_COMP_EQ_SKIPN:
  1976. case PHY_COMP_NEQ_SKIPN:
  1977. case PHY_SKIPN:
  1978. if (index + 1 + regno >= pa->size) {
  1979. netif_err(tp, ifup, tp->dev,
  1980. "Out of range of firmware\n");
  1981. goto out;
  1982. }
  1983. break;
  1984. default:
  1985. netif_err(tp, ifup, tp->dev,
  1986. "Invalid action 0x%08x\n", action);
  1987. goto out;
  1988. }
  1989. }
  1990. rc = true;
  1991. out:
  1992. return rc;
  1993. }
  1994. static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1995. {
  1996. struct net_device *dev = tp->dev;
  1997. int rc = -EINVAL;
  1998. if (!rtl_fw_format_ok(tp, rtl_fw)) {
  1999. netif_err(tp, ifup, dev, "invalid firmware\n");
  2000. goto out;
  2001. }
  2002. if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
  2003. rc = 0;
  2004. out:
  2005. return rc;
  2006. }
  2007. static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2008. {
  2009. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  2010. struct mdio_ops org, *ops = &tp->mdio_ops;
  2011. u32 predata, count;
  2012. size_t index;
  2013. predata = count = 0;
  2014. org.write = ops->write;
  2015. org.read = ops->read;
  2016. for (index = 0; index < pa->size; ) {
  2017. u32 action = le32_to_cpu(pa->code[index]);
  2018. u32 data = action & 0x0000ffff;
  2019. u32 regno = (action & 0x0fff0000) >> 16;
  2020. if (!action)
  2021. break;
  2022. switch(action & 0xf0000000) {
  2023. case PHY_READ:
  2024. predata = rtl_readphy(tp, regno);
  2025. count++;
  2026. index++;
  2027. break;
  2028. case PHY_DATA_OR:
  2029. predata |= data;
  2030. index++;
  2031. break;
  2032. case PHY_DATA_AND:
  2033. predata &= data;
  2034. index++;
  2035. break;
  2036. case PHY_BJMPN:
  2037. index -= regno;
  2038. break;
  2039. case PHY_MDIO_CHG:
  2040. if (data == 0) {
  2041. ops->write = org.write;
  2042. ops->read = org.read;
  2043. } else if (data == 1) {
  2044. ops->write = mac_mcu_write;
  2045. ops->read = mac_mcu_read;
  2046. }
  2047. index++;
  2048. break;
  2049. case PHY_CLEAR_READCOUNT:
  2050. count = 0;
  2051. index++;
  2052. break;
  2053. case PHY_WRITE:
  2054. rtl_writephy(tp, regno, data);
  2055. index++;
  2056. break;
  2057. case PHY_READCOUNT_EQ_SKIP:
  2058. index += (count == data) ? 2 : 1;
  2059. break;
  2060. case PHY_COMP_EQ_SKIPN:
  2061. if (predata == data)
  2062. index += regno;
  2063. index++;
  2064. break;
  2065. case PHY_COMP_NEQ_SKIPN:
  2066. if (predata != data)
  2067. index += regno;
  2068. index++;
  2069. break;
  2070. case PHY_WRITE_PREVIOUS:
  2071. rtl_writephy(tp, regno, predata);
  2072. index++;
  2073. break;
  2074. case PHY_SKIPN:
  2075. index += regno + 1;
  2076. break;
  2077. case PHY_DELAY_MS:
  2078. mdelay(data);
  2079. index++;
  2080. break;
  2081. default:
  2082. BUG();
  2083. }
  2084. }
  2085. ops->write = org.write;
  2086. ops->read = org.read;
  2087. }
  2088. static void rtl_release_firmware(struct rtl8169_private *tp)
  2089. {
  2090. if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
  2091. release_firmware(tp->rtl_fw->fw);
  2092. kfree(tp->rtl_fw);
  2093. }
  2094. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  2095. }
  2096. static void rtl_apply_firmware(struct rtl8169_private *tp)
  2097. {
  2098. struct rtl_fw *rtl_fw = tp->rtl_fw;
  2099. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  2100. if (!IS_ERR_OR_NULL(rtl_fw))
  2101. rtl_phy_write_fw(tp, rtl_fw);
  2102. }
  2103. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  2104. {
  2105. if (rtl_readphy(tp, reg) != val)
  2106. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  2107. else
  2108. rtl_apply_firmware(tp);
  2109. }
  2110. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  2111. {
  2112. static const struct phy_reg phy_reg_init[] = {
  2113. { 0x1f, 0x0001 },
  2114. { 0x06, 0x006e },
  2115. { 0x08, 0x0708 },
  2116. { 0x15, 0x4000 },
  2117. { 0x18, 0x65c7 },
  2118. { 0x1f, 0x0001 },
  2119. { 0x03, 0x00a1 },
  2120. { 0x02, 0x0008 },
  2121. { 0x01, 0x0120 },
  2122. { 0x00, 0x1000 },
  2123. { 0x04, 0x0800 },
  2124. { 0x04, 0x0000 },
  2125. { 0x03, 0xff41 },
  2126. { 0x02, 0xdf60 },
  2127. { 0x01, 0x0140 },
  2128. { 0x00, 0x0077 },
  2129. { 0x04, 0x7800 },
  2130. { 0x04, 0x7000 },
  2131. { 0x03, 0x802f },
  2132. { 0x02, 0x4f02 },
  2133. { 0x01, 0x0409 },
  2134. { 0x00, 0xf0f9 },
  2135. { 0x04, 0x9800 },
  2136. { 0x04, 0x9000 },
  2137. { 0x03, 0xdf01 },
  2138. { 0x02, 0xdf20 },
  2139. { 0x01, 0xff95 },
  2140. { 0x00, 0xba00 },
  2141. { 0x04, 0xa800 },
  2142. { 0x04, 0xa000 },
  2143. { 0x03, 0xff41 },
  2144. { 0x02, 0xdf20 },
  2145. { 0x01, 0x0140 },
  2146. { 0x00, 0x00bb },
  2147. { 0x04, 0xb800 },
  2148. { 0x04, 0xb000 },
  2149. { 0x03, 0xdf41 },
  2150. { 0x02, 0xdc60 },
  2151. { 0x01, 0x6340 },
  2152. { 0x00, 0x007d },
  2153. { 0x04, 0xd800 },
  2154. { 0x04, 0xd000 },
  2155. { 0x03, 0xdf01 },
  2156. { 0x02, 0xdf20 },
  2157. { 0x01, 0x100a },
  2158. { 0x00, 0xa0ff },
  2159. { 0x04, 0xf800 },
  2160. { 0x04, 0xf000 },
  2161. { 0x1f, 0x0000 },
  2162. { 0x0b, 0x0000 },
  2163. { 0x00, 0x9200 }
  2164. };
  2165. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2166. }
  2167. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  2168. {
  2169. static const struct phy_reg phy_reg_init[] = {
  2170. { 0x1f, 0x0002 },
  2171. { 0x01, 0x90d0 },
  2172. { 0x1f, 0x0000 }
  2173. };
  2174. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2175. }
  2176. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  2177. {
  2178. struct pci_dev *pdev = tp->pci_dev;
  2179. if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
  2180. (pdev->subsystem_device != 0xe000))
  2181. return;
  2182. rtl_writephy(tp, 0x1f, 0x0001);
  2183. rtl_writephy(tp, 0x10, 0xf01b);
  2184. rtl_writephy(tp, 0x1f, 0x0000);
  2185. }
  2186. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  2187. {
  2188. static const struct phy_reg phy_reg_init[] = {
  2189. { 0x1f, 0x0001 },
  2190. { 0x04, 0x0000 },
  2191. { 0x03, 0x00a1 },
  2192. { 0x02, 0x0008 },
  2193. { 0x01, 0x0120 },
  2194. { 0x00, 0x1000 },
  2195. { 0x04, 0x0800 },
  2196. { 0x04, 0x9000 },
  2197. { 0x03, 0x802f },
  2198. { 0x02, 0x4f02 },
  2199. { 0x01, 0x0409 },
  2200. { 0x00, 0xf099 },
  2201. { 0x04, 0x9800 },
  2202. { 0x04, 0xa000 },
  2203. { 0x03, 0xdf01 },
  2204. { 0x02, 0xdf20 },
  2205. { 0x01, 0xff95 },
  2206. { 0x00, 0xba00 },
  2207. { 0x04, 0xa800 },
  2208. { 0x04, 0xf000 },
  2209. { 0x03, 0xdf01 },
  2210. { 0x02, 0xdf20 },
  2211. { 0x01, 0x101a },
  2212. { 0x00, 0xa0ff },
  2213. { 0x04, 0xf800 },
  2214. { 0x04, 0x0000 },
  2215. { 0x1f, 0x0000 },
  2216. { 0x1f, 0x0001 },
  2217. { 0x10, 0xf41b },
  2218. { 0x14, 0xfb54 },
  2219. { 0x18, 0xf5c7 },
  2220. { 0x1f, 0x0000 },
  2221. { 0x1f, 0x0001 },
  2222. { 0x17, 0x0cc0 },
  2223. { 0x1f, 0x0000 }
  2224. };
  2225. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2226. rtl8169scd_hw_phy_config_quirk(tp);
  2227. }
  2228. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  2229. {
  2230. static const struct phy_reg phy_reg_init[] = {
  2231. { 0x1f, 0x0001 },
  2232. { 0x04, 0x0000 },
  2233. { 0x03, 0x00a1 },
  2234. { 0x02, 0x0008 },
  2235. { 0x01, 0x0120 },
  2236. { 0x00, 0x1000 },
  2237. { 0x04, 0x0800 },
  2238. { 0x04, 0x9000 },
  2239. { 0x03, 0x802f },
  2240. { 0x02, 0x4f02 },
  2241. { 0x01, 0x0409 },
  2242. { 0x00, 0xf099 },
  2243. { 0x04, 0x9800 },
  2244. { 0x04, 0xa000 },
  2245. { 0x03, 0xdf01 },
  2246. { 0x02, 0xdf20 },
  2247. { 0x01, 0xff95 },
  2248. { 0x00, 0xba00 },
  2249. { 0x04, 0xa800 },
  2250. { 0x04, 0xf000 },
  2251. { 0x03, 0xdf01 },
  2252. { 0x02, 0xdf20 },
  2253. { 0x01, 0x101a },
  2254. { 0x00, 0xa0ff },
  2255. { 0x04, 0xf800 },
  2256. { 0x04, 0x0000 },
  2257. { 0x1f, 0x0000 },
  2258. { 0x1f, 0x0001 },
  2259. { 0x0b, 0x8480 },
  2260. { 0x1f, 0x0000 },
  2261. { 0x1f, 0x0001 },
  2262. { 0x18, 0x67c7 },
  2263. { 0x04, 0x2000 },
  2264. { 0x03, 0x002f },
  2265. { 0x02, 0x4360 },
  2266. { 0x01, 0x0109 },
  2267. { 0x00, 0x3022 },
  2268. { 0x04, 0x2800 },
  2269. { 0x1f, 0x0000 },
  2270. { 0x1f, 0x0001 },
  2271. { 0x17, 0x0cc0 },
  2272. { 0x1f, 0x0000 }
  2273. };
  2274. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2275. }
  2276. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  2277. {
  2278. static const struct phy_reg phy_reg_init[] = {
  2279. { 0x10, 0xf41b },
  2280. { 0x1f, 0x0000 }
  2281. };
  2282. rtl_writephy(tp, 0x1f, 0x0001);
  2283. rtl_patchphy(tp, 0x16, 1 << 0);
  2284. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2285. }
  2286. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  2287. {
  2288. static const struct phy_reg phy_reg_init[] = {
  2289. { 0x1f, 0x0001 },
  2290. { 0x10, 0xf41b },
  2291. { 0x1f, 0x0000 }
  2292. };
  2293. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2294. }
  2295. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  2296. {
  2297. static const struct phy_reg phy_reg_init[] = {
  2298. { 0x1f, 0x0000 },
  2299. { 0x1d, 0x0f00 },
  2300. { 0x1f, 0x0002 },
  2301. { 0x0c, 0x1ec8 },
  2302. { 0x1f, 0x0000 }
  2303. };
  2304. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2305. }
  2306. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  2307. {
  2308. static const struct phy_reg phy_reg_init[] = {
  2309. { 0x1f, 0x0001 },
  2310. { 0x1d, 0x3d98 },
  2311. { 0x1f, 0x0000 }
  2312. };
  2313. rtl_writephy(tp, 0x1f, 0x0000);
  2314. rtl_patchphy(tp, 0x14, 1 << 5);
  2315. rtl_patchphy(tp, 0x0d, 1 << 5);
  2316. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2317. }
  2318. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  2319. {
  2320. static const struct phy_reg phy_reg_init[] = {
  2321. { 0x1f, 0x0001 },
  2322. { 0x12, 0x2300 },
  2323. { 0x1f, 0x0002 },
  2324. { 0x00, 0x88d4 },
  2325. { 0x01, 0x82b1 },
  2326. { 0x03, 0x7002 },
  2327. { 0x08, 0x9e30 },
  2328. { 0x09, 0x01f0 },
  2329. { 0x0a, 0x5500 },
  2330. { 0x0c, 0x00c8 },
  2331. { 0x1f, 0x0003 },
  2332. { 0x12, 0xc096 },
  2333. { 0x16, 0x000a },
  2334. { 0x1f, 0x0000 },
  2335. { 0x1f, 0x0000 },
  2336. { 0x09, 0x2000 },
  2337. { 0x09, 0x0000 }
  2338. };
  2339. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2340. rtl_patchphy(tp, 0x14, 1 << 5);
  2341. rtl_patchphy(tp, 0x0d, 1 << 5);
  2342. rtl_writephy(tp, 0x1f, 0x0000);
  2343. }
  2344. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  2345. {
  2346. static const struct phy_reg phy_reg_init[] = {
  2347. { 0x1f, 0x0001 },
  2348. { 0x12, 0x2300 },
  2349. { 0x03, 0x802f },
  2350. { 0x02, 0x4f02 },
  2351. { 0x01, 0x0409 },
  2352. { 0x00, 0xf099 },
  2353. { 0x04, 0x9800 },
  2354. { 0x04, 0x9000 },
  2355. { 0x1d, 0x3d98 },
  2356. { 0x1f, 0x0002 },
  2357. { 0x0c, 0x7eb8 },
  2358. { 0x06, 0x0761 },
  2359. { 0x1f, 0x0003 },
  2360. { 0x16, 0x0f0a },
  2361. { 0x1f, 0x0000 }
  2362. };
  2363. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2364. rtl_patchphy(tp, 0x16, 1 << 0);
  2365. rtl_patchphy(tp, 0x14, 1 << 5);
  2366. rtl_patchphy(tp, 0x0d, 1 << 5);
  2367. rtl_writephy(tp, 0x1f, 0x0000);
  2368. }
  2369. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  2370. {
  2371. static const struct phy_reg phy_reg_init[] = {
  2372. { 0x1f, 0x0001 },
  2373. { 0x12, 0x2300 },
  2374. { 0x1d, 0x3d98 },
  2375. { 0x1f, 0x0002 },
  2376. { 0x0c, 0x7eb8 },
  2377. { 0x06, 0x5461 },
  2378. { 0x1f, 0x0003 },
  2379. { 0x16, 0x0f0a },
  2380. { 0x1f, 0x0000 }
  2381. };
  2382. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2383. rtl_patchphy(tp, 0x16, 1 << 0);
  2384. rtl_patchphy(tp, 0x14, 1 << 5);
  2385. rtl_patchphy(tp, 0x0d, 1 << 5);
  2386. rtl_writephy(tp, 0x1f, 0x0000);
  2387. }
  2388. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  2389. {
  2390. rtl8168c_3_hw_phy_config(tp);
  2391. }
  2392. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  2393. {
  2394. static const struct phy_reg phy_reg_init_0[] = {
  2395. /* Channel Estimation */
  2396. { 0x1f, 0x0001 },
  2397. { 0x06, 0x4064 },
  2398. { 0x07, 0x2863 },
  2399. { 0x08, 0x059c },
  2400. { 0x09, 0x26b4 },
  2401. { 0x0a, 0x6a19 },
  2402. { 0x0b, 0xdcc8 },
  2403. { 0x10, 0xf06d },
  2404. { 0x14, 0x7f68 },
  2405. { 0x18, 0x7fd9 },
  2406. { 0x1c, 0xf0ff },
  2407. { 0x1d, 0x3d9c },
  2408. { 0x1f, 0x0003 },
  2409. { 0x12, 0xf49f },
  2410. { 0x13, 0x070b },
  2411. { 0x1a, 0x05ad },
  2412. { 0x14, 0x94c0 },
  2413. /*
  2414. * Tx Error Issue
  2415. * Enhance line driver power
  2416. */
  2417. { 0x1f, 0x0002 },
  2418. { 0x06, 0x5561 },
  2419. { 0x1f, 0x0005 },
  2420. { 0x05, 0x8332 },
  2421. { 0x06, 0x5561 },
  2422. /*
  2423. * Can not link to 1Gbps with bad cable
  2424. * Decrease SNR threshold form 21.07dB to 19.04dB
  2425. */
  2426. { 0x1f, 0x0001 },
  2427. { 0x17, 0x0cc0 },
  2428. { 0x1f, 0x0000 },
  2429. { 0x0d, 0xf880 }
  2430. };
  2431. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2432. /*
  2433. * Rx Error Issue
  2434. * Fine Tune Switching regulator parameter
  2435. */
  2436. rtl_writephy(tp, 0x1f, 0x0002);
  2437. rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
  2438. rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
  2439. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2440. static const struct phy_reg phy_reg_init[] = {
  2441. { 0x1f, 0x0002 },
  2442. { 0x05, 0x669a },
  2443. { 0x1f, 0x0005 },
  2444. { 0x05, 0x8330 },
  2445. { 0x06, 0x669a },
  2446. { 0x1f, 0x0002 }
  2447. };
  2448. int val;
  2449. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2450. val = rtl_readphy(tp, 0x0d);
  2451. if ((val & 0x00ff) != 0x006c) {
  2452. static const u32 set[] = {
  2453. 0x0065, 0x0066, 0x0067, 0x0068,
  2454. 0x0069, 0x006a, 0x006b, 0x006c
  2455. };
  2456. int i;
  2457. rtl_writephy(tp, 0x1f, 0x0002);
  2458. val &= 0xff00;
  2459. for (i = 0; i < ARRAY_SIZE(set); i++)
  2460. rtl_writephy(tp, 0x0d, val | set[i]);
  2461. }
  2462. } else {
  2463. static const struct phy_reg phy_reg_init[] = {
  2464. { 0x1f, 0x0002 },
  2465. { 0x05, 0x6662 },
  2466. { 0x1f, 0x0005 },
  2467. { 0x05, 0x8330 },
  2468. { 0x06, 0x6662 }
  2469. };
  2470. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2471. }
  2472. /* RSET couple improve */
  2473. rtl_writephy(tp, 0x1f, 0x0002);
  2474. rtl_patchphy(tp, 0x0d, 0x0300);
  2475. rtl_patchphy(tp, 0x0f, 0x0010);
  2476. /* Fine tune PLL performance */
  2477. rtl_writephy(tp, 0x1f, 0x0002);
  2478. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2479. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2480. rtl_writephy(tp, 0x1f, 0x0005);
  2481. rtl_writephy(tp, 0x05, 0x001b);
  2482. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  2483. rtl_writephy(tp, 0x1f, 0x0000);
  2484. }
  2485. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  2486. {
  2487. static const struct phy_reg phy_reg_init_0[] = {
  2488. /* Channel Estimation */
  2489. { 0x1f, 0x0001 },
  2490. { 0x06, 0x4064 },
  2491. { 0x07, 0x2863 },
  2492. { 0x08, 0x059c },
  2493. { 0x09, 0x26b4 },
  2494. { 0x0a, 0x6a19 },
  2495. { 0x0b, 0xdcc8 },
  2496. { 0x10, 0xf06d },
  2497. { 0x14, 0x7f68 },
  2498. { 0x18, 0x7fd9 },
  2499. { 0x1c, 0xf0ff },
  2500. { 0x1d, 0x3d9c },
  2501. { 0x1f, 0x0003 },
  2502. { 0x12, 0xf49f },
  2503. { 0x13, 0x070b },
  2504. { 0x1a, 0x05ad },
  2505. { 0x14, 0x94c0 },
  2506. /*
  2507. * Tx Error Issue
  2508. * Enhance line driver power
  2509. */
  2510. { 0x1f, 0x0002 },
  2511. { 0x06, 0x5561 },
  2512. { 0x1f, 0x0005 },
  2513. { 0x05, 0x8332 },
  2514. { 0x06, 0x5561 },
  2515. /*
  2516. * Can not link to 1Gbps with bad cable
  2517. * Decrease SNR threshold form 21.07dB to 19.04dB
  2518. */
  2519. { 0x1f, 0x0001 },
  2520. { 0x17, 0x0cc0 },
  2521. { 0x1f, 0x0000 },
  2522. { 0x0d, 0xf880 }
  2523. };
  2524. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2525. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2526. static const struct phy_reg phy_reg_init[] = {
  2527. { 0x1f, 0x0002 },
  2528. { 0x05, 0x669a },
  2529. { 0x1f, 0x0005 },
  2530. { 0x05, 0x8330 },
  2531. { 0x06, 0x669a },
  2532. { 0x1f, 0x0002 }
  2533. };
  2534. int val;
  2535. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2536. val = rtl_readphy(tp, 0x0d);
  2537. if ((val & 0x00ff) != 0x006c) {
  2538. static const u32 set[] = {
  2539. 0x0065, 0x0066, 0x0067, 0x0068,
  2540. 0x0069, 0x006a, 0x006b, 0x006c
  2541. };
  2542. int i;
  2543. rtl_writephy(tp, 0x1f, 0x0002);
  2544. val &= 0xff00;
  2545. for (i = 0; i < ARRAY_SIZE(set); i++)
  2546. rtl_writephy(tp, 0x0d, val | set[i]);
  2547. }
  2548. } else {
  2549. static const struct phy_reg phy_reg_init[] = {
  2550. { 0x1f, 0x0002 },
  2551. { 0x05, 0x2642 },
  2552. { 0x1f, 0x0005 },
  2553. { 0x05, 0x8330 },
  2554. { 0x06, 0x2642 }
  2555. };
  2556. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2557. }
  2558. /* Fine tune PLL performance */
  2559. rtl_writephy(tp, 0x1f, 0x0002);
  2560. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2561. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2562. /* Switching regulator Slew rate */
  2563. rtl_writephy(tp, 0x1f, 0x0002);
  2564. rtl_patchphy(tp, 0x0f, 0x0017);
  2565. rtl_writephy(tp, 0x1f, 0x0005);
  2566. rtl_writephy(tp, 0x05, 0x001b);
  2567. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2568. rtl_writephy(tp, 0x1f, 0x0000);
  2569. }
  2570. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2571. {
  2572. static const struct phy_reg phy_reg_init[] = {
  2573. { 0x1f, 0x0002 },
  2574. { 0x10, 0x0008 },
  2575. { 0x0d, 0x006c },
  2576. { 0x1f, 0x0000 },
  2577. { 0x0d, 0xf880 },
  2578. { 0x1f, 0x0001 },
  2579. { 0x17, 0x0cc0 },
  2580. { 0x1f, 0x0001 },
  2581. { 0x0b, 0xa4d8 },
  2582. { 0x09, 0x281c },
  2583. { 0x07, 0x2883 },
  2584. { 0x0a, 0x6b35 },
  2585. { 0x1d, 0x3da4 },
  2586. { 0x1c, 0xeffd },
  2587. { 0x14, 0x7f52 },
  2588. { 0x18, 0x7fc6 },
  2589. { 0x08, 0x0601 },
  2590. { 0x06, 0x4063 },
  2591. { 0x10, 0xf074 },
  2592. { 0x1f, 0x0003 },
  2593. { 0x13, 0x0789 },
  2594. { 0x12, 0xf4bd },
  2595. { 0x1a, 0x04fd },
  2596. { 0x14, 0x84b0 },
  2597. { 0x1f, 0x0000 },
  2598. { 0x00, 0x9200 },
  2599. { 0x1f, 0x0005 },
  2600. { 0x01, 0x0340 },
  2601. { 0x1f, 0x0001 },
  2602. { 0x04, 0x4000 },
  2603. { 0x03, 0x1d21 },
  2604. { 0x02, 0x0c32 },
  2605. { 0x01, 0x0200 },
  2606. { 0x00, 0x5554 },
  2607. { 0x04, 0x4800 },
  2608. { 0x04, 0x4000 },
  2609. { 0x04, 0xf000 },
  2610. { 0x03, 0xdf01 },
  2611. { 0x02, 0xdf20 },
  2612. { 0x01, 0x101a },
  2613. { 0x00, 0xa0ff },
  2614. { 0x04, 0xf800 },
  2615. { 0x04, 0xf000 },
  2616. { 0x1f, 0x0000 },
  2617. { 0x1f, 0x0007 },
  2618. { 0x1e, 0x0023 },
  2619. { 0x16, 0x0000 },
  2620. { 0x1f, 0x0000 }
  2621. };
  2622. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2623. }
  2624. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2625. {
  2626. static const struct phy_reg phy_reg_init[] = {
  2627. { 0x1f, 0x0001 },
  2628. { 0x17, 0x0cc0 },
  2629. { 0x1f, 0x0007 },
  2630. { 0x1e, 0x002d },
  2631. { 0x18, 0x0040 },
  2632. { 0x1f, 0x0000 }
  2633. };
  2634. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2635. rtl_patchphy(tp, 0x0d, 1 << 5);
  2636. }
  2637. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2638. {
  2639. static const struct phy_reg phy_reg_init[] = {
  2640. /* Enable Delay cap */
  2641. { 0x1f, 0x0005 },
  2642. { 0x05, 0x8b80 },
  2643. { 0x06, 0xc896 },
  2644. { 0x1f, 0x0000 },
  2645. /* Channel estimation fine tune */
  2646. { 0x1f, 0x0001 },
  2647. { 0x0b, 0x6c20 },
  2648. { 0x07, 0x2872 },
  2649. { 0x1c, 0xefff },
  2650. { 0x1f, 0x0003 },
  2651. { 0x14, 0x6420 },
  2652. { 0x1f, 0x0000 },
  2653. /* Update PFM & 10M TX idle timer */
  2654. { 0x1f, 0x0007 },
  2655. { 0x1e, 0x002f },
  2656. { 0x15, 0x1919 },
  2657. { 0x1f, 0x0000 },
  2658. { 0x1f, 0x0007 },
  2659. { 0x1e, 0x00ac },
  2660. { 0x18, 0x0006 },
  2661. { 0x1f, 0x0000 }
  2662. };
  2663. rtl_apply_firmware(tp);
  2664. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2665. /* DCO enable for 10M IDLE Power */
  2666. rtl_writephy(tp, 0x1f, 0x0007);
  2667. rtl_writephy(tp, 0x1e, 0x0023);
  2668. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2669. rtl_writephy(tp, 0x1f, 0x0000);
  2670. /* For impedance matching */
  2671. rtl_writephy(tp, 0x1f, 0x0002);
  2672. rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
  2673. rtl_writephy(tp, 0x1f, 0x0000);
  2674. /* PHY auto speed down */
  2675. rtl_writephy(tp, 0x1f, 0x0007);
  2676. rtl_writephy(tp, 0x1e, 0x002d);
  2677. rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
  2678. rtl_writephy(tp, 0x1f, 0x0000);
  2679. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2680. rtl_writephy(tp, 0x1f, 0x0005);
  2681. rtl_writephy(tp, 0x05, 0x8b86);
  2682. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2683. rtl_writephy(tp, 0x1f, 0x0000);
  2684. rtl_writephy(tp, 0x1f, 0x0005);
  2685. rtl_writephy(tp, 0x05, 0x8b85);
  2686. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2687. rtl_writephy(tp, 0x1f, 0x0007);
  2688. rtl_writephy(tp, 0x1e, 0x0020);
  2689. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
  2690. rtl_writephy(tp, 0x1f, 0x0006);
  2691. rtl_writephy(tp, 0x00, 0x5a00);
  2692. rtl_writephy(tp, 0x1f, 0x0000);
  2693. rtl_writephy(tp, 0x0d, 0x0007);
  2694. rtl_writephy(tp, 0x0e, 0x003c);
  2695. rtl_writephy(tp, 0x0d, 0x4007);
  2696. rtl_writephy(tp, 0x0e, 0x0000);
  2697. rtl_writephy(tp, 0x0d, 0x0000);
  2698. }
  2699. static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
  2700. {
  2701. const u16 w[] = {
  2702. addr[0] | (addr[1] << 8),
  2703. addr[2] | (addr[3] << 8),
  2704. addr[4] | (addr[5] << 8)
  2705. };
  2706. const struct exgmac_reg e[] = {
  2707. { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
  2708. { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
  2709. { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
  2710. { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
  2711. };
  2712. rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
  2713. }
  2714. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2715. {
  2716. static const struct phy_reg phy_reg_init[] = {
  2717. /* Enable Delay cap */
  2718. { 0x1f, 0x0004 },
  2719. { 0x1f, 0x0007 },
  2720. { 0x1e, 0x00ac },
  2721. { 0x18, 0x0006 },
  2722. { 0x1f, 0x0002 },
  2723. { 0x1f, 0x0000 },
  2724. { 0x1f, 0x0000 },
  2725. /* Channel estimation fine tune */
  2726. { 0x1f, 0x0003 },
  2727. { 0x09, 0xa20f },
  2728. { 0x1f, 0x0000 },
  2729. { 0x1f, 0x0000 },
  2730. /* Green Setting */
  2731. { 0x1f, 0x0005 },
  2732. { 0x05, 0x8b5b },
  2733. { 0x06, 0x9222 },
  2734. { 0x05, 0x8b6d },
  2735. { 0x06, 0x8000 },
  2736. { 0x05, 0x8b76 },
  2737. { 0x06, 0x8000 },
  2738. { 0x1f, 0x0000 }
  2739. };
  2740. rtl_apply_firmware(tp);
  2741. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2742. /* For 4-corner performance improve */
  2743. rtl_writephy(tp, 0x1f, 0x0005);
  2744. rtl_writephy(tp, 0x05, 0x8b80);
  2745. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2746. rtl_writephy(tp, 0x1f, 0x0000);
  2747. /* PHY auto speed down */
  2748. rtl_writephy(tp, 0x1f, 0x0004);
  2749. rtl_writephy(tp, 0x1f, 0x0007);
  2750. rtl_writephy(tp, 0x1e, 0x002d);
  2751. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  2752. rtl_writephy(tp, 0x1f, 0x0002);
  2753. rtl_writephy(tp, 0x1f, 0x0000);
  2754. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2755. /* improve 10M EEE waveform */
  2756. rtl_writephy(tp, 0x1f, 0x0005);
  2757. rtl_writephy(tp, 0x05, 0x8b86);
  2758. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2759. rtl_writephy(tp, 0x1f, 0x0000);
  2760. /* Improve 2-pair detection performance */
  2761. rtl_writephy(tp, 0x1f, 0x0005);
  2762. rtl_writephy(tp, 0x05, 0x8b85);
  2763. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2764. rtl_writephy(tp, 0x1f, 0x0000);
  2765. /* EEE setting */
  2766. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
  2767. rtl_writephy(tp, 0x1f, 0x0005);
  2768. rtl_writephy(tp, 0x05, 0x8b85);
  2769. rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
  2770. rtl_writephy(tp, 0x1f, 0x0004);
  2771. rtl_writephy(tp, 0x1f, 0x0007);
  2772. rtl_writephy(tp, 0x1e, 0x0020);
  2773. rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
  2774. rtl_writephy(tp, 0x1f, 0x0002);
  2775. rtl_writephy(tp, 0x1f, 0x0000);
  2776. rtl_writephy(tp, 0x0d, 0x0007);
  2777. rtl_writephy(tp, 0x0e, 0x003c);
  2778. rtl_writephy(tp, 0x0d, 0x4007);
  2779. rtl_writephy(tp, 0x0e, 0x0006);
  2780. rtl_writephy(tp, 0x0d, 0x0000);
  2781. /* Green feature */
  2782. rtl_writephy(tp, 0x1f, 0x0003);
  2783. rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
  2784. rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
  2785. rtl_writephy(tp, 0x1f, 0x0000);
  2786. rtl_writephy(tp, 0x1f, 0x0005);
  2787. rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
  2788. rtl_writephy(tp, 0x1f, 0x0000);
  2789. /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
  2790. rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
  2791. }
  2792. static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
  2793. {
  2794. /* For 4-corner performance improve */
  2795. rtl_writephy(tp, 0x1f, 0x0005);
  2796. rtl_writephy(tp, 0x05, 0x8b80);
  2797. rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
  2798. rtl_writephy(tp, 0x1f, 0x0000);
  2799. /* PHY auto speed down */
  2800. rtl_writephy(tp, 0x1f, 0x0007);
  2801. rtl_writephy(tp, 0x1e, 0x002d);
  2802. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  2803. rtl_writephy(tp, 0x1f, 0x0000);
  2804. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2805. /* Improve 10M EEE waveform */
  2806. rtl_writephy(tp, 0x1f, 0x0005);
  2807. rtl_writephy(tp, 0x05, 0x8b86);
  2808. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2809. rtl_writephy(tp, 0x1f, 0x0000);
  2810. }
  2811. static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
  2812. {
  2813. static const struct phy_reg phy_reg_init[] = {
  2814. /* Channel estimation fine tune */
  2815. { 0x1f, 0x0003 },
  2816. { 0x09, 0xa20f },
  2817. { 0x1f, 0x0000 },
  2818. /* Modify green table for giga & fnet */
  2819. { 0x1f, 0x0005 },
  2820. { 0x05, 0x8b55 },
  2821. { 0x06, 0x0000 },
  2822. { 0x05, 0x8b5e },
  2823. { 0x06, 0x0000 },
  2824. { 0x05, 0x8b67 },
  2825. { 0x06, 0x0000 },
  2826. { 0x05, 0x8b70 },
  2827. { 0x06, 0x0000 },
  2828. { 0x1f, 0x0000 },
  2829. { 0x1f, 0x0007 },
  2830. { 0x1e, 0x0078 },
  2831. { 0x17, 0x0000 },
  2832. { 0x19, 0x00fb },
  2833. { 0x1f, 0x0000 },
  2834. /* Modify green table for 10M */
  2835. { 0x1f, 0x0005 },
  2836. { 0x05, 0x8b79 },
  2837. { 0x06, 0xaa00 },
  2838. { 0x1f, 0x0000 },
  2839. /* Disable hiimpedance detection (RTCT) */
  2840. { 0x1f, 0x0003 },
  2841. { 0x01, 0x328a },
  2842. { 0x1f, 0x0000 }
  2843. };
  2844. rtl_apply_firmware(tp);
  2845. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2846. rtl8168f_hw_phy_config(tp);
  2847. /* Improve 2-pair detection performance */
  2848. rtl_writephy(tp, 0x1f, 0x0005);
  2849. rtl_writephy(tp, 0x05, 0x8b85);
  2850. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2851. rtl_writephy(tp, 0x1f, 0x0000);
  2852. }
  2853. static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
  2854. {
  2855. rtl_apply_firmware(tp);
  2856. rtl8168f_hw_phy_config(tp);
  2857. }
  2858. static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
  2859. {
  2860. static const struct phy_reg phy_reg_init[] = {
  2861. /* Channel estimation fine tune */
  2862. { 0x1f, 0x0003 },
  2863. { 0x09, 0xa20f },
  2864. { 0x1f, 0x0000 },
  2865. /* Modify green table for giga & fnet */
  2866. { 0x1f, 0x0005 },
  2867. { 0x05, 0x8b55 },
  2868. { 0x06, 0x0000 },
  2869. { 0x05, 0x8b5e },
  2870. { 0x06, 0x0000 },
  2871. { 0x05, 0x8b67 },
  2872. { 0x06, 0x0000 },
  2873. { 0x05, 0x8b70 },
  2874. { 0x06, 0x0000 },
  2875. { 0x1f, 0x0000 },
  2876. { 0x1f, 0x0007 },
  2877. { 0x1e, 0x0078 },
  2878. { 0x17, 0x0000 },
  2879. { 0x19, 0x00aa },
  2880. { 0x1f, 0x0000 },
  2881. /* Modify green table for 10M */
  2882. { 0x1f, 0x0005 },
  2883. { 0x05, 0x8b79 },
  2884. { 0x06, 0xaa00 },
  2885. { 0x1f, 0x0000 },
  2886. /* Disable hiimpedance detection (RTCT) */
  2887. { 0x1f, 0x0003 },
  2888. { 0x01, 0x328a },
  2889. { 0x1f, 0x0000 }
  2890. };
  2891. rtl_apply_firmware(tp);
  2892. rtl8168f_hw_phy_config(tp);
  2893. /* Improve 2-pair detection performance */
  2894. rtl_writephy(tp, 0x1f, 0x0005);
  2895. rtl_writephy(tp, 0x05, 0x8b85);
  2896. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2897. rtl_writephy(tp, 0x1f, 0x0000);
  2898. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2899. /* Modify green table for giga */
  2900. rtl_writephy(tp, 0x1f, 0x0005);
  2901. rtl_writephy(tp, 0x05, 0x8b54);
  2902. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  2903. rtl_writephy(tp, 0x05, 0x8b5d);
  2904. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  2905. rtl_writephy(tp, 0x05, 0x8a7c);
  2906. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2907. rtl_writephy(tp, 0x05, 0x8a7f);
  2908. rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
  2909. rtl_writephy(tp, 0x05, 0x8a82);
  2910. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2911. rtl_writephy(tp, 0x05, 0x8a85);
  2912. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2913. rtl_writephy(tp, 0x05, 0x8a88);
  2914. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2915. rtl_writephy(tp, 0x1f, 0x0000);
  2916. /* uc same-seed solution */
  2917. rtl_writephy(tp, 0x1f, 0x0005);
  2918. rtl_writephy(tp, 0x05, 0x8b85);
  2919. rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
  2920. rtl_writephy(tp, 0x1f, 0x0000);
  2921. /* eee setting */
  2922. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
  2923. rtl_writephy(tp, 0x1f, 0x0005);
  2924. rtl_writephy(tp, 0x05, 0x8b85);
  2925. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2926. rtl_writephy(tp, 0x1f, 0x0004);
  2927. rtl_writephy(tp, 0x1f, 0x0007);
  2928. rtl_writephy(tp, 0x1e, 0x0020);
  2929. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
  2930. rtl_writephy(tp, 0x1f, 0x0000);
  2931. rtl_writephy(tp, 0x0d, 0x0007);
  2932. rtl_writephy(tp, 0x0e, 0x003c);
  2933. rtl_writephy(tp, 0x0d, 0x4007);
  2934. rtl_writephy(tp, 0x0e, 0x0000);
  2935. rtl_writephy(tp, 0x0d, 0x0000);
  2936. /* Green feature */
  2937. rtl_writephy(tp, 0x1f, 0x0003);
  2938. rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
  2939. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
  2940. rtl_writephy(tp, 0x1f, 0x0000);
  2941. }
  2942. static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
  2943. {
  2944. rtl_apply_firmware(tp);
  2945. rtl_writephy(tp, 0x1f, 0x0a46);
  2946. if (rtl_readphy(tp, 0x10) & 0x0100) {
  2947. rtl_writephy(tp, 0x1f, 0x0bcc);
  2948. rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
  2949. } else {
  2950. rtl_writephy(tp, 0x1f, 0x0bcc);
  2951. rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
  2952. }
  2953. rtl_writephy(tp, 0x1f, 0x0a46);
  2954. if (rtl_readphy(tp, 0x13) & 0x0100) {
  2955. rtl_writephy(tp, 0x1f, 0x0c41);
  2956. rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
  2957. } else {
  2958. rtl_writephy(tp, 0x1f, 0x0c41);
  2959. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
  2960. }
  2961. /* Enable PHY auto speed down */
  2962. rtl_writephy(tp, 0x1f, 0x0a44);
  2963. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  2964. rtl_writephy(tp, 0x1f, 0x0bcc);
  2965. rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
  2966. rtl_writephy(tp, 0x1f, 0x0a44);
  2967. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  2968. rtl_writephy(tp, 0x1f, 0x0a43);
  2969. rtl_writephy(tp, 0x13, 0x8084);
  2970. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  2971. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  2972. /* EEE auto-fallback function */
  2973. rtl_writephy(tp, 0x1f, 0x0a4b);
  2974. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  2975. /* Enable UC LPF tune function */
  2976. rtl_writephy(tp, 0x1f, 0x0a43);
  2977. rtl_writephy(tp, 0x13, 0x8012);
  2978. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2979. rtl_writephy(tp, 0x1f, 0x0c42);
  2980. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  2981. /* Improve SWR Efficiency */
  2982. rtl_writephy(tp, 0x1f, 0x0bcd);
  2983. rtl_writephy(tp, 0x14, 0x5065);
  2984. rtl_writephy(tp, 0x14, 0xd065);
  2985. rtl_writephy(tp, 0x1f, 0x0bc8);
  2986. rtl_writephy(tp, 0x11, 0x5655);
  2987. rtl_writephy(tp, 0x1f, 0x0bcd);
  2988. rtl_writephy(tp, 0x14, 0x1065);
  2989. rtl_writephy(tp, 0x14, 0x9065);
  2990. rtl_writephy(tp, 0x14, 0x1065);
  2991. /* Check ALDPS bit, disable it if enabled */
  2992. rtl_writephy(tp, 0x1f, 0x0a43);
  2993. if (rtl_readphy(tp, 0x10) & 0x0004)
  2994. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  2995. rtl_writephy(tp, 0x1f, 0x0000);
  2996. }
  2997. static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
  2998. {
  2999. rtl_apply_firmware(tp);
  3000. }
  3001. static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
  3002. {
  3003. u16 dout_tapbin;
  3004. u32 data;
  3005. rtl_apply_firmware(tp);
  3006. /* CHN EST parameters adjust - giga master */
  3007. rtl_writephy(tp, 0x1f, 0x0a43);
  3008. rtl_writephy(tp, 0x13, 0x809b);
  3009. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
  3010. rtl_writephy(tp, 0x13, 0x80a2);
  3011. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
  3012. rtl_writephy(tp, 0x13, 0x80a4);
  3013. rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
  3014. rtl_writephy(tp, 0x13, 0x809c);
  3015. rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
  3016. rtl_writephy(tp, 0x1f, 0x0000);
  3017. /* CHN EST parameters adjust - giga slave */
  3018. rtl_writephy(tp, 0x1f, 0x0a43);
  3019. rtl_writephy(tp, 0x13, 0x80ad);
  3020. rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
  3021. rtl_writephy(tp, 0x13, 0x80b4);
  3022. rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
  3023. rtl_writephy(tp, 0x13, 0x80ac);
  3024. rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
  3025. rtl_writephy(tp, 0x1f, 0x0000);
  3026. /* CHN EST parameters adjust - fnet */
  3027. rtl_writephy(tp, 0x1f, 0x0a43);
  3028. rtl_writephy(tp, 0x13, 0x808e);
  3029. rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
  3030. rtl_writephy(tp, 0x13, 0x8090);
  3031. rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
  3032. rtl_writephy(tp, 0x13, 0x8092);
  3033. rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
  3034. rtl_writephy(tp, 0x1f, 0x0000);
  3035. /* enable R-tune & PGA-retune function */
  3036. dout_tapbin = 0;
  3037. rtl_writephy(tp, 0x1f, 0x0a46);
  3038. data = rtl_readphy(tp, 0x13);
  3039. data &= 3;
  3040. data <<= 2;
  3041. dout_tapbin |= data;
  3042. data = rtl_readphy(tp, 0x12);
  3043. data &= 0xc000;
  3044. data >>= 14;
  3045. dout_tapbin |= data;
  3046. dout_tapbin = ~(dout_tapbin^0x08);
  3047. dout_tapbin <<= 12;
  3048. dout_tapbin &= 0xf000;
  3049. rtl_writephy(tp, 0x1f, 0x0a43);
  3050. rtl_writephy(tp, 0x13, 0x827a);
  3051. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3052. rtl_writephy(tp, 0x13, 0x827b);
  3053. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3054. rtl_writephy(tp, 0x13, 0x827c);
  3055. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3056. rtl_writephy(tp, 0x13, 0x827d);
  3057. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3058. rtl_writephy(tp, 0x1f, 0x0a43);
  3059. rtl_writephy(tp, 0x13, 0x0811);
  3060. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3061. rtl_writephy(tp, 0x1f, 0x0a42);
  3062. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3063. rtl_writephy(tp, 0x1f, 0x0000);
  3064. /* enable GPHY 10M */
  3065. rtl_writephy(tp, 0x1f, 0x0a44);
  3066. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3067. rtl_writephy(tp, 0x1f, 0x0000);
  3068. /* SAR ADC performance */
  3069. rtl_writephy(tp, 0x1f, 0x0bca);
  3070. rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
  3071. rtl_writephy(tp, 0x1f, 0x0000);
  3072. rtl_writephy(tp, 0x1f, 0x0a43);
  3073. rtl_writephy(tp, 0x13, 0x803f);
  3074. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3075. rtl_writephy(tp, 0x13, 0x8047);
  3076. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3077. rtl_writephy(tp, 0x13, 0x804f);
  3078. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3079. rtl_writephy(tp, 0x13, 0x8057);
  3080. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3081. rtl_writephy(tp, 0x13, 0x805f);
  3082. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3083. rtl_writephy(tp, 0x13, 0x8067);
  3084. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3085. rtl_writephy(tp, 0x13, 0x806f);
  3086. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3087. rtl_writephy(tp, 0x1f, 0x0000);
  3088. /* disable phy pfm mode */
  3089. rtl_writephy(tp, 0x1f, 0x0a44);
  3090. rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
  3091. rtl_writephy(tp, 0x1f, 0x0000);
  3092. /* Check ALDPS bit, disable it if enabled */
  3093. rtl_writephy(tp, 0x1f, 0x0a43);
  3094. if (rtl_readphy(tp, 0x10) & 0x0004)
  3095. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3096. rtl_writephy(tp, 0x1f, 0x0000);
  3097. }
  3098. static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
  3099. {
  3100. u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
  3101. u16 rlen;
  3102. u32 data;
  3103. rtl_apply_firmware(tp);
  3104. /* CHIN EST parameter update */
  3105. rtl_writephy(tp, 0x1f, 0x0a43);
  3106. rtl_writephy(tp, 0x13, 0x808a);
  3107. rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
  3108. rtl_writephy(tp, 0x1f, 0x0000);
  3109. /* enable R-tune & PGA-retune function */
  3110. rtl_writephy(tp, 0x1f, 0x0a43);
  3111. rtl_writephy(tp, 0x13, 0x0811);
  3112. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3113. rtl_writephy(tp, 0x1f, 0x0a42);
  3114. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3115. rtl_writephy(tp, 0x1f, 0x0000);
  3116. /* enable GPHY 10M */
  3117. rtl_writephy(tp, 0x1f, 0x0a44);
  3118. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3119. rtl_writephy(tp, 0x1f, 0x0000);
  3120. r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
  3121. data = r8168_mac_ocp_read(tp, 0xdd02);
  3122. ioffset_p3 = ((data & 0x80)>>7);
  3123. ioffset_p3 <<= 3;
  3124. data = r8168_mac_ocp_read(tp, 0xdd00);
  3125. ioffset_p3 |= ((data & (0xe000))>>13);
  3126. ioffset_p2 = ((data & (0x1e00))>>9);
  3127. ioffset_p1 = ((data & (0x01e0))>>5);
  3128. ioffset_p0 = ((data & 0x0010)>>4);
  3129. ioffset_p0 <<= 3;
  3130. ioffset_p0 |= (data & (0x07));
  3131. data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
  3132. if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
  3133. (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
  3134. rtl_writephy(tp, 0x1f, 0x0bcf);
  3135. rtl_writephy(tp, 0x16, data);
  3136. rtl_writephy(tp, 0x1f, 0x0000);
  3137. }
  3138. /* Modify rlen (TX LPF corner frequency) level */
  3139. rtl_writephy(tp, 0x1f, 0x0bcd);
  3140. data = rtl_readphy(tp, 0x16);
  3141. data &= 0x000f;
  3142. rlen = 0;
  3143. if (data > 3)
  3144. rlen = data - 3;
  3145. data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
  3146. rtl_writephy(tp, 0x17, data);
  3147. rtl_writephy(tp, 0x1f, 0x0bcd);
  3148. rtl_writephy(tp, 0x1f, 0x0000);
  3149. /* disable phy pfm mode */
  3150. rtl_writephy(tp, 0x1f, 0x0a44);
  3151. rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
  3152. rtl_writephy(tp, 0x1f, 0x0000);
  3153. /* Check ALDPS bit, disable it if enabled */
  3154. rtl_writephy(tp, 0x1f, 0x0a43);
  3155. if (rtl_readphy(tp, 0x10) & 0x0004)
  3156. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3157. rtl_writephy(tp, 0x1f, 0x0000);
  3158. }
  3159. static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
  3160. {
  3161. /* Enable PHY auto speed down */
  3162. rtl_writephy(tp, 0x1f, 0x0a44);
  3163. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  3164. rtl_writephy(tp, 0x1f, 0x0000);
  3165. /* patch 10M & ALDPS */
  3166. rtl_writephy(tp, 0x1f, 0x0bcc);
  3167. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3168. rtl_writephy(tp, 0x1f, 0x0a44);
  3169. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3170. rtl_writephy(tp, 0x1f, 0x0a43);
  3171. rtl_writephy(tp, 0x13, 0x8084);
  3172. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3173. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3174. rtl_writephy(tp, 0x1f, 0x0000);
  3175. /* Enable EEE auto-fallback function */
  3176. rtl_writephy(tp, 0x1f, 0x0a4b);
  3177. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  3178. rtl_writephy(tp, 0x1f, 0x0000);
  3179. /* Enable UC LPF tune function */
  3180. rtl_writephy(tp, 0x1f, 0x0a43);
  3181. rtl_writephy(tp, 0x13, 0x8012);
  3182. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3183. rtl_writephy(tp, 0x1f, 0x0000);
  3184. /* set rg_sel_sdm_rate */
  3185. rtl_writephy(tp, 0x1f, 0x0c42);
  3186. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3187. rtl_writephy(tp, 0x1f, 0x0000);
  3188. /* Check ALDPS bit, disable it if enabled */
  3189. rtl_writephy(tp, 0x1f, 0x0a43);
  3190. if (rtl_readphy(tp, 0x10) & 0x0004)
  3191. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3192. rtl_writephy(tp, 0x1f, 0x0000);
  3193. }
  3194. static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
  3195. {
  3196. /* patch 10M & ALDPS */
  3197. rtl_writephy(tp, 0x1f, 0x0bcc);
  3198. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3199. rtl_writephy(tp, 0x1f, 0x0a44);
  3200. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3201. rtl_writephy(tp, 0x1f, 0x0a43);
  3202. rtl_writephy(tp, 0x13, 0x8084);
  3203. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3204. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3205. rtl_writephy(tp, 0x1f, 0x0000);
  3206. /* Enable UC LPF tune function */
  3207. rtl_writephy(tp, 0x1f, 0x0a43);
  3208. rtl_writephy(tp, 0x13, 0x8012);
  3209. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3210. rtl_writephy(tp, 0x1f, 0x0000);
  3211. /* Set rg_sel_sdm_rate */
  3212. rtl_writephy(tp, 0x1f, 0x0c42);
  3213. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3214. rtl_writephy(tp, 0x1f, 0x0000);
  3215. /* Channel estimation parameters */
  3216. rtl_writephy(tp, 0x1f, 0x0a43);
  3217. rtl_writephy(tp, 0x13, 0x80f3);
  3218. rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
  3219. rtl_writephy(tp, 0x13, 0x80f0);
  3220. rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
  3221. rtl_writephy(tp, 0x13, 0x80ef);
  3222. rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
  3223. rtl_writephy(tp, 0x13, 0x80f6);
  3224. rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
  3225. rtl_writephy(tp, 0x13, 0x80ec);
  3226. rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
  3227. rtl_writephy(tp, 0x13, 0x80ed);
  3228. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3229. rtl_writephy(tp, 0x13, 0x80f2);
  3230. rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
  3231. rtl_writephy(tp, 0x13, 0x80f4);
  3232. rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
  3233. rtl_writephy(tp, 0x1f, 0x0a43);
  3234. rtl_writephy(tp, 0x13, 0x8110);
  3235. rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
  3236. rtl_writephy(tp, 0x13, 0x810f);
  3237. rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
  3238. rtl_writephy(tp, 0x13, 0x8111);
  3239. rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
  3240. rtl_writephy(tp, 0x13, 0x8113);
  3241. rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
  3242. rtl_writephy(tp, 0x13, 0x8115);
  3243. rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
  3244. rtl_writephy(tp, 0x13, 0x810e);
  3245. rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
  3246. rtl_writephy(tp, 0x13, 0x810c);
  3247. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3248. rtl_writephy(tp, 0x13, 0x810b);
  3249. rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
  3250. rtl_writephy(tp, 0x1f, 0x0a43);
  3251. rtl_writephy(tp, 0x13, 0x80d1);
  3252. rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
  3253. rtl_writephy(tp, 0x13, 0x80cd);
  3254. rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
  3255. rtl_writephy(tp, 0x13, 0x80d3);
  3256. rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
  3257. rtl_writephy(tp, 0x13, 0x80d5);
  3258. rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
  3259. rtl_writephy(tp, 0x13, 0x80d7);
  3260. rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
  3261. /* Force PWM-mode */
  3262. rtl_writephy(tp, 0x1f, 0x0bcd);
  3263. rtl_writephy(tp, 0x14, 0x5065);
  3264. rtl_writephy(tp, 0x14, 0xd065);
  3265. rtl_writephy(tp, 0x1f, 0x0bc8);
  3266. rtl_writephy(tp, 0x12, 0x00ed);
  3267. rtl_writephy(tp, 0x1f, 0x0bcd);
  3268. rtl_writephy(tp, 0x14, 0x1065);
  3269. rtl_writephy(tp, 0x14, 0x9065);
  3270. rtl_writephy(tp, 0x14, 0x1065);
  3271. rtl_writephy(tp, 0x1f, 0x0000);
  3272. /* Check ALDPS bit, disable it if enabled */
  3273. rtl_writephy(tp, 0x1f, 0x0a43);
  3274. if (rtl_readphy(tp, 0x10) & 0x0004)
  3275. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3276. rtl_writephy(tp, 0x1f, 0x0000);
  3277. }
  3278. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  3279. {
  3280. static const struct phy_reg phy_reg_init[] = {
  3281. { 0x1f, 0x0003 },
  3282. { 0x08, 0x441d },
  3283. { 0x01, 0x9100 },
  3284. { 0x1f, 0x0000 }
  3285. };
  3286. rtl_writephy(tp, 0x1f, 0x0000);
  3287. rtl_patchphy(tp, 0x11, 1 << 12);
  3288. rtl_patchphy(tp, 0x19, 1 << 13);
  3289. rtl_patchphy(tp, 0x10, 1 << 15);
  3290. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3291. }
  3292. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  3293. {
  3294. static const struct phy_reg phy_reg_init[] = {
  3295. { 0x1f, 0x0005 },
  3296. { 0x1a, 0x0000 },
  3297. { 0x1f, 0x0000 },
  3298. { 0x1f, 0x0004 },
  3299. { 0x1c, 0x0000 },
  3300. { 0x1f, 0x0000 },
  3301. { 0x1f, 0x0001 },
  3302. { 0x15, 0x7701 },
  3303. { 0x1f, 0x0000 }
  3304. };
  3305. /* Disable ALDPS before ram code */
  3306. rtl_writephy(tp, 0x1f, 0x0000);
  3307. rtl_writephy(tp, 0x18, 0x0310);
  3308. msleep(100);
  3309. rtl_apply_firmware(tp);
  3310. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3311. }
  3312. static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
  3313. {
  3314. /* Disable ALDPS before setting firmware */
  3315. rtl_writephy(tp, 0x1f, 0x0000);
  3316. rtl_writephy(tp, 0x18, 0x0310);
  3317. msleep(20);
  3318. rtl_apply_firmware(tp);
  3319. /* EEE setting */
  3320. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3321. rtl_writephy(tp, 0x1f, 0x0004);
  3322. rtl_writephy(tp, 0x10, 0x401f);
  3323. rtl_writephy(tp, 0x19, 0x7030);
  3324. rtl_writephy(tp, 0x1f, 0x0000);
  3325. }
  3326. static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
  3327. {
  3328. static const struct phy_reg phy_reg_init[] = {
  3329. { 0x1f, 0x0004 },
  3330. { 0x10, 0xc07f },
  3331. { 0x19, 0x7030 },
  3332. { 0x1f, 0x0000 }
  3333. };
  3334. /* Disable ALDPS before ram code */
  3335. rtl_writephy(tp, 0x1f, 0x0000);
  3336. rtl_writephy(tp, 0x18, 0x0310);
  3337. msleep(100);
  3338. rtl_apply_firmware(tp);
  3339. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3340. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3341. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3342. }
  3343. static void rtl_hw_phy_config(struct net_device *dev)
  3344. {
  3345. struct rtl8169_private *tp = netdev_priv(dev);
  3346. rtl8169_print_mac_version(tp);
  3347. switch (tp->mac_version) {
  3348. case RTL_GIGA_MAC_VER_01:
  3349. break;
  3350. case RTL_GIGA_MAC_VER_02:
  3351. case RTL_GIGA_MAC_VER_03:
  3352. rtl8169s_hw_phy_config(tp);
  3353. break;
  3354. case RTL_GIGA_MAC_VER_04:
  3355. rtl8169sb_hw_phy_config(tp);
  3356. break;
  3357. case RTL_GIGA_MAC_VER_05:
  3358. rtl8169scd_hw_phy_config(tp);
  3359. break;
  3360. case RTL_GIGA_MAC_VER_06:
  3361. rtl8169sce_hw_phy_config(tp);
  3362. break;
  3363. case RTL_GIGA_MAC_VER_07:
  3364. case RTL_GIGA_MAC_VER_08:
  3365. case RTL_GIGA_MAC_VER_09:
  3366. rtl8102e_hw_phy_config(tp);
  3367. break;
  3368. case RTL_GIGA_MAC_VER_11:
  3369. rtl8168bb_hw_phy_config(tp);
  3370. break;
  3371. case RTL_GIGA_MAC_VER_12:
  3372. rtl8168bef_hw_phy_config(tp);
  3373. break;
  3374. case RTL_GIGA_MAC_VER_17:
  3375. rtl8168bef_hw_phy_config(tp);
  3376. break;
  3377. case RTL_GIGA_MAC_VER_18:
  3378. rtl8168cp_1_hw_phy_config(tp);
  3379. break;
  3380. case RTL_GIGA_MAC_VER_19:
  3381. rtl8168c_1_hw_phy_config(tp);
  3382. break;
  3383. case RTL_GIGA_MAC_VER_20:
  3384. rtl8168c_2_hw_phy_config(tp);
  3385. break;
  3386. case RTL_GIGA_MAC_VER_21:
  3387. rtl8168c_3_hw_phy_config(tp);
  3388. break;
  3389. case RTL_GIGA_MAC_VER_22:
  3390. rtl8168c_4_hw_phy_config(tp);
  3391. break;
  3392. case RTL_GIGA_MAC_VER_23:
  3393. case RTL_GIGA_MAC_VER_24:
  3394. rtl8168cp_2_hw_phy_config(tp);
  3395. break;
  3396. case RTL_GIGA_MAC_VER_25:
  3397. rtl8168d_1_hw_phy_config(tp);
  3398. break;
  3399. case RTL_GIGA_MAC_VER_26:
  3400. rtl8168d_2_hw_phy_config(tp);
  3401. break;
  3402. case RTL_GIGA_MAC_VER_27:
  3403. rtl8168d_3_hw_phy_config(tp);
  3404. break;
  3405. case RTL_GIGA_MAC_VER_28:
  3406. rtl8168d_4_hw_phy_config(tp);
  3407. break;
  3408. case RTL_GIGA_MAC_VER_29:
  3409. case RTL_GIGA_MAC_VER_30:
  3410. rtl8105e_hw_phy_config(tp);
  3411. break;
  3412. case RTL_GIGA_MAC_VER_31:
  3413. /* None. */
  3414. break;
  3415. case RTL_GIGA_MAC_VER_32:
  3416. case RTL_GIGA_MAC_VER_33:
  3417. rtl8168e_1_hw_phy_config(tp);
  3418. break;
  3419. case RTL_GIGA_MAC_VER_34:
  3420. rtl8168e_2_hw_phy_config(tp);
  3421. break;
  3422. case RTL_GIGA_MAC_VER_35:
  3423. rtl8168f_1_hw_phy_config(tp);
  3424. break;
  3425. case RTL_GIGA_MAC_VER_36:
  3426. rtl8168f_2_hw_phy_config(tp);
  3427. break;
  3428. case RTL_GIGA_MAC_VER_37:
  3429. rtl8402_hw_phy_config(tp);
  3430. break;
  3431. case RTL_GIGA_MAC_VER_38:
  3432. rtl8411_hw_phy_config(tp);
  3433. break;
  3434. case RTL_GIGA_MAC_VER_39:
  3435. rtl8106e_hw_phy_config(tp);
  3436. break;
  3437. case RTL_GIGA_MAC_VER_40:
  3438. rtl8168g_1_hw_phy_config(tp);
  3439. break;
  3440. case RTL_GIGA_MAC_VER_42:
  3441. case RTL_GIGA_MAC_VER_43:
  3442. case RTL_GIGA_MAC_VER_44:
  3443. rtl8168g_2_hw_phy_config(tp);
  3444. break;
  3445. case RTL_GIGA_MAC_VER_45:
  3446. case RTL_GIGA_MAC_VER_47:
  3447. rtl8168h_1_hw_phy_config(tp);
  3448. break;
  3449. case RTL_GIGA_MAC_VER_46:
  3450. case RTL_GIGA_MAC_VER_48:
  3451. rtl8168h_2_hw_phy_config(tp);
  3452. break;
  3453. case RTL_GIGA_MAC_VER_49:
  3454. rtl8168ep_1_hw_phy_config(tp);
  3455. break;
  3456. case RTL_GIGA_MAC_VER_50:
  3457. case RTL_GIGA_MAC_VER_51:
  3458. rtl8168ep_2_hw_phy_config(tp);
  3459. break;
  3460. case RTL_GIGA_MAC_VER_41:
  3461. default:
  3462. break;
  3463. }
  3464. }
  3465. static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
  3466. {
  3467. if (!test_and_set_bit(flag, tp->wk.flags))
  3468. schedule_work(&tp->wk.work);
  3469. }
  3470. static bool rtl_tbi_enabled(struct rtl8169_private *tp)
  3471. {
  3472. return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
  3473. (RTL_R8(tp, PHYstatus) & TBI_Enable);
  3474. }
  3475. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  3476. {
  3477. rtl_hw_phy_config(dev);
  3478. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  3479. netif_dbg(tp, drv, dev,
  3480. "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3481. RTL_W8(tp, 0x82, 0x01);
  3482. }
  3483. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  3484. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  3485. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  3486. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  3487. netif_dbg(tp, drv, dev,
  3488. "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3489. RTL_W8(tp, 0x82, 0x01);
  3490. netif_dbg(tp, drv, dev,
  3491. "Set PHY Reg 0x0bh = 0x00h\n");
  3492. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  3493. }
  3494. /* We may have called phy_speed_down before */
  3495. phy_speed_up(dev->phydev);
  3496. genphy_soft_reset(dev->phydev);
  3497. /* It was reported that several chips end up with 10MBit/Half on a
  3498. * 1GBit link after resuming from S3. For whatever reason the PHY on
  3499. * these chips doesn't properly start a renegotiation when soft-reset.
  3500. * Explicitly requesting a renegotiation fixes this.
  3501. */
  3502. if (dev->phydev->autoneg == AUTONEG_ENABLE)
  3503. phy_restart_aneg(dev->phydev);
  3504. }
  3505. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  3506. {
  3507. rtl_lock_work(tp);
  3508. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3509. RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
  3510. RTL_R32(tp, MAC4);
  3511. RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
  3512. RTL_R32(tp, MAC0);
  3513. if (tp->mac_version == RTL_GIGA_MAC_VER_34)
  3514. rtl_rar_exgmac_set(tp, addr);
  3515. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3516. rtl_unlock_work(tp);
  3517. }
  3518. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  3519. {
  3520. switch (tp->mac_version) {
  3521. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  3522. case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
  3523. RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  3524. break;
  3525. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
  3526. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
  3527. case RTL_GIGA_MAC_VER_38:
  3528. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  3529. break;
  3530. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3531. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
  3532. break;
  3533. default:
  3534. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
  3535. break;
  3536. }
  3537. }
  3538. static int rtl_set_mac_address(struct net_device *dev, void *p)
  3539. {
  3540. struct rtl8169_private *tp = netdev_priv(dev);
  3541. struct device *d = tp_to_dev(tp);
  3542. int ret;
  3543. ret = eth_mac_addr(dev, p);
  3544. if (ret)
  3545. return ret;
  3546. pm_runtime_get_noresume(d);
  3547. if (pm_runtime_active(d))
  3548. rtl_rar_set(tp, dev->dev_addr);
  3549. pm_runtime_put_noidle(d);
  3550. /* Reportedly at least Asus X453MA truncates packets otherwise */
  3551. if (tp->mac_version == RTL_GIGA_MAC_VER_37)
  3552. rtl_init_rxcfg(tp);
  3553. return 0;
  3554. }
  3555. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3556. {
  3557. if (!netif_running(dev))
  3558. return -ENODEV;
  3559. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  3560. }
  3561. static void rtl_init_mdio_ops(struct rtl8169_private *tp)
  3562. {
  3563. struct mdio_ops *ops = &tp->mdio_ops;
  3564. switch (tp->mac_version) {
  3565. case RTL_GIGA_MAC_VER_27:
  3566. ops->write = r8168dp_1_mdio_write;
  3567. ops->read = r8168dp_1_mdio_read;
  3568. break;
  3569. case RTL_GIGA_MAC_VER_28:
  3570. case RTL_GIGA_MAC_VER_31:
  3571. ops->write = r8168dp_2_mdio_write;
  3572. ops->read = r8168dp_2_mdio_read;
  3573. break;
  3574. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3575. ops->write = r8168g_mdio_write;
  3576. ops->read = r8168g_mdio_read;
  3577. break;
  3578. default:
  3579. ops->write = r8169_mdio_write;
  3580. ops->read = r8169_mdio_read;
  3581. break;
  3582. }
  3583. }
  3584. static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
  3585. {
  3586. switch (tp->mac_version) {
  3587. case RTL_GIGA_MAC_VER_25:
  3588. case RTL_GIGA_MAC_VER_26:
  3589. case RTL_GIGA_MAC_VER_29:
  3590. case RTL_GIGA_MAC_VER_30:
  3591. case RTL_GIGA_MAC_VER_32:
  3592. case RTL_GIGA_MAC_VER_33:
  3593. case RTL_GIGA_MAC_VER_34:
  3594. case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
  3595. RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
  3596. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  3597. break;
  3598. default:
  3599. break;
  3600. }
  3601. }
  3602. static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
  3603. {
  3604. struct phy_device *phydev;
  3605. if (!__rtl8169_get_wol(tp))
  3606. return false;
  3607. /* phydev may not be attached to netdevice */
  3608. phydev = mdiobus_get_phy(tp->mii_bus, 0);
  3609. phy_speed_down(phydev, false);
  3610. rtl_wol_suspend_quirk(tp);
  3611. return true;
  3612. }
  3613. static void r8168_pll_power_down(struct rtl8169_private *tp)
  3614. {
  3615. if (r8168_check_dash(tp))
  3616. return;
  3617. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  3618. tp->mac_version == RTL_GIGA_MAC_VER_33)
  3619. rtl_ephy_write(tp, 0x19, 0xff64);
  3620. if (rtl_wol_pll_power_down(tp))
  3621. return;
  3622. switch (tp->mac_version) {
  3623. case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
  3624. case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
  3625. case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_33:
  3626. case RTL_GIGA_MAC_VER_37:
  3627. case RTL_GIGA_MAC_VER_39:
  3628. case RTL_GIGA_MAC_VER_43:
  3629. case RTL_GIGA_MAC_VER_44:
  3630. case RTL_GIGA_MAC_VER_45:
  3631. case RTL_GIGA_MAC_VER_46:
  3632. case RTL_GIGA_MAC_VER_47:
  3633. case RTL_GIGA_MAC_VER_48:
  3634. case RTL_GIGA_MAC_VER_50:
  3635. case RTL_GIGA_MAC_VER_51:
  3636. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
  3637. break;
  3638. case RTL_GIGA_MAC_VER_40:
  3639. case RTL_GIGA_MAC_VER_41:
  3640. case RTL_GIGA_MAC_VER_49:
  3641. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
  3642. 0xfc000000, ERIAR_EXGMAC);
  3643. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
  3644. break;
  3645. }
  3646. }
  3647. static void r8168_pll_power_up(struct rtl8169_private *tp)
  3648. {
  3649. switch (tp->mac_version) {
  3650. case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
  3651. case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
  3652. case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_33:
  3653. case RTL_GIGA_MAC_VER_37:
  3654. case RTL_GIGA_MAC_VER_39:
  3655. case RTL_GIGA_MAC_VER_43:
  3656. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
  3657. break;
  3658. case RTL_GIGA_MAC_VER_44:
  3659. case RTL_GIGA_MAC_VER_45:
  3660. case RTL_GIGA_MAC_VER_46:
  3661. case RTL_GIGA_MAC_VER_47:
  3662. case RTL_GIGA_MAC_VER_48:
  3663. case RTL_GIGA_MAC_VER_50:
  3664. case RTL_GIGA_MAC_VER_51:
  3665. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
  3666. break;
  3667. case RTL_GIGA_MAC_VER_40:
  3668. case RTL_GIGA_MAC_VER_41:
  3669. case RTL_GIGA_MAC_VER_49:
  3670. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
  3671. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
  3672. 0x00000000, ERIAR_EXGMAC);
  3673. break;
  3674. }
  3675. phy_resume(tp->dev->phydev);
  3676. /* give MAC/PHY some time to resume */
  3677. msleep(20);
  3678. }
  3679. static void rtl_pll_power_down(struct rtl8169_private *tp)
  3680. {
  3681. switch (tp->mac_version) {
  3682. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  3683. case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
  3684. break;
  3685. default:
  3686. r8168_pll_power_down(tp);
  3687. }
  3688. }
  3689. static void rtl_pll_power_up(struct rtl8169_private *tp)
  3690. {
  3691. switch (tp->mac_version) {
  3692. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  3693. case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
  3694. break;
  3695. default:
  3696. r8168_pll_power_up(tp);
  3697. }
  3698. }
  3699. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  3700. {
  3701. tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
  3702. }
  3703. static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
  3704. {
  3705. if (tp->jumbo_ops.enable) {
  3706. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3707. tp->jumbo_ops.enable(tp);
  3708. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3709. }
  3710. }
  3711. static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
  3712. {
  3713. if (tp->jumbo_ops.disable) {
  3714. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3715. tp->jumbo_ops.disable(tp);
  3716. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3717. }
  3718. }
  3719. static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
  3720. {
  3721. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  3722. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
  3723. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
  3724. }
  3725. static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
  3726. {
  3727. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  3728. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
  3729. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  3730. }
  3731. static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
  3732. {
  3733. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  3734. }
  3735. static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
  3736. {
  3737. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  3738. }
  3739. static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
  3740. {
  3741. RTL_W8(tp, MaxTxPacketSize, 0x24);
  3742. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  3743. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
  3744. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
  3745. }
  3746. static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
  3747. {
  3748. RTL_W8(tp, MaxTxPacketSize, 0x3f);
  3749. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  3750. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
  3751. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  3752. }
  3753. static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
  3754. {
  3755. rtl_tx_performance_tweak(tp,
  3756. PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3757. }
  3758. static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
  3759. {
  3760. rtl_tx_performance_tweak(tp,
  3761. PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3762. }
  3763. static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
  3764. {
  3765. r8168b_0_hw_jumbo_enable(tp);
  3766. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
  3767. }
  3768. static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
  3769. {
  3770. r8168b_0_hw_jumbo_disable(tp);
  3771. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
  3772. }
  3773. static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
  3774. {
  3775. struct jumbo_ops *ops = &tp->jumbo_ops;
  3776. switch (tp->mac_version) {
  3777. case RTL_GIGA_MAC_VER_11:
  3778. ops->disable = r8168b_0_hw_jumbo_disable;
  3779. ops->enable = r8168b_0_hw_jumbo_enable;
  3780. break;
  3781. case RTL_GIGA_MAC_VER_12:
  3782. case RTL_GIGA_MAC_VER_17:
  3783. ops->disable = r8168b_1_hw_jumbo_disable;
  3784. ops->enable = r8168b_1_hw_jumbo_enable;
  3785. break;
  3786. case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
  3787. case RTL_GIGA_MAC_VER_19:
  3788. case RTL_GIGA_MAC_VER_20:
  3789. case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
  3790. case RTL_GIGA_MAC_VER_22:
  3791. case RTL_GIGA_MAC_VER_23:
  3792. case RTL_GIGA_MAC_VER_24:
  3793. case RTL_GIGA_MAC_VER_25:
  3794. case RTL_GIGA_MAC_VER_26:
  3795. ops->disable = r8168c_hw_jumbo_disable;
  3796. ops->enable = r8168c_hw_jumbo_enable;
  3797. break;
  3798. case RTL_GIGA_MAC_VER_27:
  3799. case RTL_GIGA_MAC_VER_28:
  3800. ops->disable = r8168dp_hw_jumbo_disable;
  3801. ops->enable = r8168dp_hw_jumbo_enable;
  3802. break;
  3803. case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
  3804. case RTL_GIGA_MAC_VER_32:
  3805. case RTL_GIGA_MAC_VER_33:
  3806. case RTL_GIGA_MAC_VER_34:
  3807. ops->disable = r8168e_hw_jumbo_disable;
  3808. ops->enable = r8168e_hw_jumbo_enable;
  3809. break;
  3810. /*
  3811. * No action needed for jumbo frames with 8169.
  3812. * No jumbo for 810x at all.
  3813. */
  3814. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3815. default:
  3816. ops->disable = NULL;
  3817. ops->enable = NULL;
  3818. break;
  3819. }
  3820. }
  3821. DECLARE_RTL_COND(rtl_chipcmd_cond)
  3822. {
  3823. return RTL_R8(tp, ChipCmd) & CmdReset;
  3824. }
  3825. static void rtl_hw_reset(struct rtl8169_private *tp)
  3826. {
  3827. RTL_W8(tp, ChipCmd, CmdReset);
  3828. rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
  3829. }
  3830. static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
  3831. {
  3832. struct rtl_fw *rtl_fw;
  3833. const char *name;
  3834. int rc = -ENOMEM;
  3835. name = rtl_lookup_firmware_name(tp);
  3836. if (!name)
  3837. goto out_no_firmware;
  3838. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  3839. if (!rtl_fw)
  3840. goto err_warn;
  3841. rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
  3842. if (rc < 0)
  3843. goto err_free;
  3844. rc = rtl_check_firmware(tp, rtl_fw);
  3845. if (rc < 0)
  3846. goto err_release_firmware;
  3847. tp->rtl_fw = rtl_fw;
  3848. out:
  3849. return;
  3850. err_release_firmware:
  3851. release_firmware(rtl_fw->fw);
  3852. err_free:
  3853. kfree(rtl_fw);
  3854. err_warn:
  3855. netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
  3856. name, rc);
  3857. out_no_firmware:
  3858. tp->rtl_fw = NULL;
  3859. goto out;
  3860. }
  3861. static void rtl_request_firmware(struct rtl8169_private *tp)
  3862. {
  3863. if (IS_ERR(tp->rtl_fw))
  3864. rtl_request_uncached_firmware(tp);
  3865. }
  3866. static void rtl_rx_close(struct rtl8169_private *tp)
  3867. {
  3868. RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  3869. }
  3870. DECLARE_RTL_COND(rtl_npq_cond)
  3871. {
  3872. return RTL_R8(tp, TxPoll) & NPQ;
  3873. }
  3874. DECLARE_RTL_COND(rtl_txcfg_empty_cond)
  3875. {
  3876. return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
  3877. }
  3878. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  3879. {
  3880. /* Disable interrupts */
  3881. rtl8169_irq_mask_and_ack(tp);
  3882. rtl_rx_close(tp);
  3883. switch (tp->mac_version) {
  3884. case RTL_GIGA_MAC_VER_27:
  3885. case RTL_GIGA_MAC_VER_28:
  3886. case RTL_GIGA_MAC_VER_31:
  3887. rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
  3888. break;
  3889. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  3890. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3891. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  3892. rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
  3893. break;
  3894. default:
  3895. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  3896. udelay(100);
  3897. break;
  3898. }
  3899. rtl_hw_reset(tp);
  3900. }
  3901. static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
  3902. {
  3903. u32 val = TX_DMA_BURST << TxDMAShift |
  3904. InterFrameGap << TxInterFrameGapShift;
  3905. if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
  3906. tp->mac_version != RTL_GIGA_MAC_VER_39)
  3907. val |= TXCFG_AUTO_FIFO;
  3908. RTL_W32(tp, TxConfig, val);
  3909. }
  3910. static void rtl_set_rx_max_size(struct rtl8169_private *tp)
  3911. {
  3912. /* Low hurts. Let's disable the filtering. */
  3913. RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
  3914. }
  3915. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
  3916. {
  3917. /*
  3918. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  3919. * register to be written before TxDescAddrLow to work.
  3920. * Switching from MMIO to I/O access fixes the issue as well.
  3921. */
  3922. RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  3923. RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  3924. RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  3925. RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  3926. }
  3927. static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
  3928. {
  3929. static const struct rtl_cfg2_info {
  3930. u32 mac_version;
  3931. u32 clk;
  3932. u32 val;
  3933. } cfg2_info [] = {
  3934. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  3935. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  3936. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  3937. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  3938. };
  3939. const struct rtl_cfg2_info *p = cfg2_info;
  3940. unsigned int i;
  3941. u32 clk;
  3942. clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
  3943. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  3944. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  3945. RTL_W32(tp, 0x7c, p->val);
  3946. break;
  3947. }
  3948. }
  3949. }
  3950. static void rtl_set_rx_mode(struct net_device *dev)
  3951. {
  3952. struct rtl8169_private *tp = netdev_priv(dev);
  3953. u32 mc_filter[2]; /* Multicast hash filter */
  3954. int rx_mode;
  3955. u32 tmp = 0;
  3956. if (dev->flags & IFF_PROMISC) {
  3957. /* Unconditionally log net taps. */
  3958. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  3959. rx_mode =
  3960. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  3961. AcceptAllPhys;
  3962. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3963. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  3964. (dev->flags & IFF_ALLMULTI)) {
  3965. /* Too many to filter perfectly -- accept all multicasts. */
  3966. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  3967. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3968. } else {
  3969. struct netdev_hw_addr *ha;
  3970. rx_mode = AcceptBroadcast | AcceptMyPhys;
  3971. mc_filter[1] = mc_filter[0] = 0;
  3972. netdev_for_each_mc_addr(ha, dev) {
  3973. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  3974. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  3975. rx_mode |= AcceptMulticast;
  3976. }
  3977. }
  3978. if (dev->features & NETIF_F_RXALL)
  3979. rx_mode |= (AcceptErr | AcceptRunt);
  3980. tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
  3981. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  3982. u32 data = mc_filter[0];
  3983. mc_filter[0] = swab32(mc_filter[1]);
  3984. mc_filter[1] = swab32(data);
  3985. }
  3986. if (tp->mac_version == RTL_GIGA_MAC_VER_35)
  3987. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3988. RTL_W32(tp, MAR0 + 4, mc_filter[1]);
  3989. RTL_W32(tp, MAR0 + 0, mc_filter[0]);
  3990. RTL_W32(tp, RxConfig, tmp);
  3991. }
  3992. static void rtl_hw_start(struct rtl8169_private *tp)
  3993. {
  3994. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3995. tp->hw_start(tp);
  3996. rtl_set_rx_max_size(tp);
  3997. rtl_set_rx_tx_desc_registers(tp);
  3998. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3999. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  4000. RTL_R8(tp, IntrMask);
  4001. RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
  4002. rtl_init_rxcfg(tp);
  4003. rtl_set_tx_config_registers(tp);
  4004. rtl_set_rx_mode(tp->dev);
  4005. /* no early-rx interrupts */
  4006. RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
  4007. rtl_irq_enable_all(tp);
  4008. }
  4009. static void rtl_hw_start_8169(struct rtl8169_private *tp)
  4010. {
  4011. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  4012. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  4013. RTL_W8(tp, EarlyTxThres, NoEarlyTx);
  4014. tp->cp_cmd |= PCIMulRW;
  4015. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4016. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  4017. netif_dbg(tp, drv, tp->dev,
  4018. "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
  4019. tp->cp_cmd |= (1 << 14);
  4020. }
  4021. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4022. rtl8169_set_magic_reg(tp, tp->mac_version);
  4023. /*
  4024. * Undocumented corner. Supposedly:
  4025. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  4026. */
  4027. RTL_W16(tp, IntrMitigate, 0x0000);
  4028. RTL_W32(tp, RxMissed, 0);
  4029. }
  4030. DECLARE_RTL_COND(rtl_csiar_cond)
  4031. {
  4032. return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
  4033. }
  4034. static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
  4035. {
  4036. u32 func = PCI_FUNC(tp->pci_dev->devfn);
  4037. RTL_W32(tp, CSIDR, value);
  4038. RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4039. CSIAR_BYTE_ENABLE | func << 16);
  4040. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4041. }
  4042. static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
  4043. {
  4044. u32 func = PCI_FUNC(tp->pci_dev->devfn);
  4045. RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
  4046. CSIAR_BYTE_ENABLE);
  4047. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4048. RTL_R32(tp, CSIDR) : ~0;
  4049. }
  4050. static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
  4051. {
  4052. struct pci_dev *pdev = tp->pci_dev;
  4053. u32 csi;
  4054. /* According to Realtek the value at config space address 0x070f
  4055. * controls the L0s/L1 entrance latency. We try standard ECAM access
  4056. * first and if it fails fall back to CSI.
  4057. */
  4058. if (pdev->cfg_size > 0x070f &&
  4059. pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
  4060. return;
  4061. netdev_notice_once(tp->dev,
  4062. "No native access to PCI extended config space, falling back to CSI\n");
  4063. csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
  4064. rtl_csi_write(tp, 0x070c, csi | val << 24);
  4065. }
  4066. static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
  4067. {
  4068. rtl_csi_access_enable(tp, 0x27);
  4069. }
  4070. struct ephy_info {
  4071. unsigned int offset;
  4072. u16 mask;
  4073. u16 bits;
  4074. };
  4075. static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
  4076. int len)
  4077. {
  4078. u16 w;
  4079. while (len-- > 0) {
  4080. w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
  4081. rtl_ephy_write(tp, e->offset, w);
  4082. e++;
  4083. }
  4084. }
  4085. static void rtl_disable_clock_request(struct rtl8169_private *tp)
  4086. {
  4087. pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
  4088. PCI_EXP_LNKCTL_CLKREQ_EN);
  4089. }
  4090. static void rtl_enable_clock_request(struct rtl8169_private *tp)
  4091. {
  4092. pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
  4093. PCI_EXP_LNKCTL_CLKREQ_EN);
  4094. }
  4095. static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
  4096. {
  4097. u8 data;
  4098. data = RTL_R8(tp, Config3);
  4099. if (enable)
  4100. data |= Rdy_to_L23;
  4101. else
  4102. data &= ~Rdy_to_L23;
  4103. RTL_W8(tp, Config3, data);
  4104. }
  4105. static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
  4106. {
  4107. if (enable) {
  4108. RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
  4109. RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
  4110. } else {
  4111. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
  4112. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
  4113. }
  4114. udelay(10);
  4115. }
  4116. static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
  4117. {
  4118. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4119. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4120. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4121. if (tp->dev->mtu <= ETH_DATA_LEN) {
  4122. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
  4123. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4124. }
  4125. }
  4126. static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
  4127. {
  4128. rtl_hw_start_8168bb(tp);
  4129. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4130. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
  4131. }
  4132. static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
  4133. {
  4134. RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
  4135. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4136. if (tp->dev->mtu <= ETH_DATA_LEN)
  4137. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4138. rtl_disable_clock_request(tp);
  4139. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4140. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4141. }
  4142. static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
  4143. {
  4144. static const struct ephy_info e_info_8168cp[] = {
  4145. { 0x01, 0, 0x0001 },
  4146. { 0x02, 0x0800, 0x1000 },
  4147. { 0x03, 0, 0x0042 },
  4148. { 0x06, 0x0080, 0x0000 },
  4149. { 0x07, 0, 0x2000 }
  4150. };
  4151. rtl_set_def_aspm_entry_latency(tp);
  4152. rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  4153. __rtl_hw_start_8168cp(tp);
  4154. }
  4155. static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
  4156. {
  4157. rtl_set_def_aspm_entry_latency(tp);
  4158. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4159. if (tp->dev->mtu <= ETH_DATA_LEN)
  4160. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4161. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4162. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4163. }
  4164. static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
  4165. {
  4166. rtl_set_def_aspm_entry_latency(tp);
  4167. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4168. /* Magic. */
  4169. RTL_W8(tp, DBG_REG, 0x20);
  4170. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4171. if (tp->dev->mtu <= ETH_DATA_LEN)
  4172. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4173. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4174. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4175. }
  4176. static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
  4177. {
  4178. static const struct ephy_info e_info_8168c_1[] = {
  4179. { 0x02, 0x0800, 0x1000 },
  4180. { 0x03, 0, 0x0002 },
  4181. { 0x06, 0x0080, 0x0000 }
  4182. };
  4183. rtl_set_def_aspm_entry_latency(tp);
  4184. RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  4185. rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  4186. __rtl_hw_start_8168cp(tp);
  4187. }
  4188. static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
  4189. {
  4190. static const struct ephy_info e_info_8168c_2[] = {
  4191. { 0x01, 0, 0x0001 },
  4192. { 0x03, 0x0400, 0x0220 }
  4193. };
  4194. rtl_set_def_aspm_entry_latency(tp);
  4195. rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  4196. __rtl_hw_start_8168cp(tp);
  4197. }
  4198. static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
  4199. {
  4200. rtl_hw_start_8168c_2(tp);
  4201. }
  4202. static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
  4203. {
  4204. rtl_set_def_aspm_entry_latency(tp);
  4205. __rtl_hw_start_8168cp(tp);
  4206. }
  4207. static void rtl_hw_start_8168d(struct rtl8169_private *tp)
  4208. {
  4209. rtl_set_def_aspm_entry_latency(tp);
  4210. rtl_disable_clock_request(tp);
  4211. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4212. if (tp->dev->mtu <= ETH_DATA_LEN)
  4213. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4214. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4215. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4216. }
  4217. static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
  4218. {
  4219. rtl_set_def_aspm_entry_latency(tp);
  4220. if (tp->dev->mtu <= ETH_DATA_LEN)
  4221. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4222. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4223. rtl_disable_clock_request(tp);
  4224. }
  4225. static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
  4226. {
  4227. static const struct ephy_info e_info_8168d_4[] = {
  4228. { 0x0b, 0x0000, 0x0048 },
  4229. { 0x19, 0x0020, 0x0050 },
  4230. { 0x0c, 0x0100, 0x0020 }
  4231. };
  4232. rtl_set_def_aspm_entry_latency(tp);
  4233. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4234. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4235. rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
  4236. rtl_enable_clock_request(tp);
  4237. }
  4238. static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
  4239. {
  4240. static const struct ephy_info e_info_8168e_1[] = {
  4241. { 0x00, 0x0200, 0x0100 },
  4242. { 0x00, 0x0000, 0x0004 },
  4243. { 0x06, 0x0002, 0x0001 },
  4244. { 0x06, 0x0000, 0x0030 },
  4245. { 0x07, 0x0000, 0x2000 },
  4246. { 0x00, 0x0000, 0x0020 },
  4247. { 0x03, 0x5800, 0x2000 },
  4248. { 0x03, 0x0000, 0x0001 },
  4249. { 0x01, 0x0800, 0x1000 },
  4250. { 0x07, 0x0000, 0x4000 },
  4251. { 0x1e, 0x0000, 0x2000 },
  4252. { 0x19, 0xffff, 0xfe6c },
  4253. { 0x0a, 0x0000, 0x0040 }
  4254. };
  4255. rtl_set_def_aspm_entry_latency(tp);
  4256. rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
  4257. if (tp->dev->mtu <= ETH_DATA_LEN)
  4258. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4259. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4260. rtl_disable_clock_request(tp);
  4261. /* Reset tx FIFO pointer */
  4262. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
  4263. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
  4264. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  4265. }
  4266. static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
  4267. {
  4268. static const struct ephy_info e_info_8168e_2[] = {
  4269. { 0x09, 0x0000, 0x0080 },
  4270. { 0x19, 0x0000, 0x0224 }
  4271. };
  4272. rtl_set_def_aspm_entry_latency(tp);
  4273. rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
  4274. if (tp->dev->mtu <= ETH_DATA_LEN)
  4275. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4276. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4277. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4278. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4279. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4280. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4281. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
  4282. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4283. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  4284. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4285. rtl_disable_clock_request(tp);
  4286. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4287. /* Adjust EEE LED frequency */
  4288. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4289. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  4290. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
  4291. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  4292. rtl_hw_aspm_clkreq_enable(tp, true);
  4293. }
  4294. static void rtl_hw_start_8168f(struct rtl8169_private *tp)
  4295. {
  4296. rtl_set_def_aspm_entry_latency(tp);
  4297. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4298. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4299. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4300. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4301. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4302. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4303. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4304. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4305. rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4306. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4307. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
  4308. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4309. rtl_disable_clock_request(tp);
  4310. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4311. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  4312. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
  4313. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  4314. }
  4315. static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
  4316. {
  4317. static const struct ephy_info e_info_8168f_1[] = {
  4318. { 0x06, 0x00c0, 0x0020 },
  4319. { 0x08, 0x0001, 0x0002 },
  4320. { 0x09, 0x0000, 0x0080 },
  4321. { 0x19, 0x0000, 0x0224 }
  4322. };
  4323. rtl_hw_start_8168f(tp);
  4324. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  4325. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  4326. /* Adjust EEE LED frequency */
  4327. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4328. }
  4329. static void rtl_hw_start_8411(struct rtl8169_private *tp)
  4330. {
  4331. static const struct ephy_info e_info_8168f_1[] = {
  4332. { 0x06, 0x00c0, 0x0020 },
  4333. { 0x0f, 0xffff, 0x5200 },
  4334. { 0x1e, 0x0000, 0x4000 },
  4335. { 0x19, 0x0000, 0x0224 }
  4336. };
  4337. rtl_hw_start_8168f(tp);
  4338. rtl_pcie_state_l2l3_enable(tp, false);
  4339. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  4340. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
  4341. }
  4342. static void rtl_hw_start_8168g(struct rtl8169_private *tp)
  4343. {
  4344. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
  4345. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  4346. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  4347. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4348. rtl_set_def_aspm_entry_latency(tp);
  4349. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4350. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4351. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4352. rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
  4353. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  4354. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4355. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4356. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4357. /* Adjust EEE LED frequency */
  4358. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4359. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  4360. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  4361. rtl_pcie_state_l2l3_enable(tp, false);
  4362. }
  4363. static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
  4364. {
  4365. static const struct ephy_info e_info_8168g_1[] = {
  4366. { 0x00, 0x0000, 0x0008 },
  4367. { 0x0c, 0x37d0, 0x0820 },
  4368. { 0x1e, 0x0000, 0x0001 },
  4369. { 0x19, 0x8000, 0x0000 }
  4370. };
  4371. rtl_hw_start_8168g(tp);
  4372. /* disable aspm and clock request before access ephy */
  4373. rtl_hw_aspm_clkreq_enable(tp, false);
  4374. rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
  4375. rtl_hw_aspm_clkreq_enable(tp, true);
  4376. }
  4377. static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
  4378. {
  4379. static const struct ephy_info e_info_8168g_2[] = {
  4380. { 0x00, 0x0000, 0x0008 },
  4381. { 0x0c, 0x3df0, 0x0200 },
  4382. { 0x19, 0xffff, 0xfc00 },
  4383. { 0x1e, 0xffff, 0x20eb }
  4384. };
  4385. rtl_hw_start_8168g(tp);
  4386. /* disable aspm and clock request before access ephy */
  4387. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
  4388. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
  4389. rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
  4390. }
  4391. static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
  4392. {
  4393. static const struct ephy_info e_info_8411_2[] = {
  4394. { 0x00, 0x0000, 0x0008 },
  4395. { 0x0c, 0x3df0, 0x0200 },
  4396. { 0x0f, 0xffff, 0x5200 },
  4397. { 0x19, 0x0020, 0x0000 },
  4398. { 0x1e, 0x0000, 0x2000 }
  4399. };
  4400. rtl_hw_start_8168g(tp);
  4401. /* disable aspm and clock request before access ephy */
  4402. rtl_hw_aspm_clkreq_enable(tp, false);
  4403. rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
  4404. /* The following Realtek-provided magic fixes an issue with the RX unit
  4405. * getting confused after the PHY having been powered-down.
  4406. */
  4407. r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
  4408. r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
  4409. r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
  4410. r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
  4411. r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
  4412. r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
  4413. r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
  4414. r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
  4415. mdelay(3);
  4416. r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
  4417. r8168_mac_ocp_write(tp, 0xF800, 0xE008);
  4418. r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
  4419. r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
  4420. r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
  4421. r8168_mac_ocp_write(tp, 0xF808, 0xE027);
  4422. r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
  4423. r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
  4424. r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
  4425. r8168_mac_ocp_write(tp, 0xF810, 0xC602);
  4426. r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
  4427. r8168_mac_ocp_write(tp, 0xF814, 0x0000);
  4428. r8168_mac_ocp_write(tp, 0xF816, 0xC502);
  4429. r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
  4430. r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
  4431. r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
  4432. r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
  4433. r8168_mac_ocp_write(tp, 0xF820, 0x080A);
  4434. r8168_mac_ocp_write(tp, 0xF822, 0x6420);
  4435. r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
  4436. r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
  4437. r8168_mac_ocp_write(tp, 0xF828, 0xC516);
  4438. r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
  4439. r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
  4440. r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
  4441. r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
  4442. r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
  4443. r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
  4444. r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
  4445. r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
  4446. r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
  4447. r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
  4448. r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
  4449. r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
  4450. r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
  4451. r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
  4452. r8168_mac_ocp_write(tp, 0xF846, 0xC404);
  4453. r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
  4454. r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
  4455. r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
  4456. r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
  4457. r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
  4458. r8168_mac_ocp_write(tp, 0xF852, 0xE434);
  4459. r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
  4460. r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
  4461. r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
  4462. r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
  4463. r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
  4464. r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
  4465. r8168_mac_ocp_write(tp, 0xF860, 0xF007);
  4466. r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
  4467. r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
  4468. r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
  4469. r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
  4470. r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
  4471. r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
  4472. r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
  4473. r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
  4474. r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
  4475. r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
  4476. r8168_mac_ocp_write(tp, 0xF876, 0xC516);
  4477. r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
  4478. r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
  4479. r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
  4480. r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
  4481. r8168_mac_ocp_write(tp, 0xF880, 0xC512);
  4482. r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
  4483. r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
  4484. r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
  4485. r8168_mac_ocp_write(tp, 0xF888, 0x483F);
  4486. r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
  4487. r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
  4488. r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
  4489. r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
  4490. r8168_mac_ocp_write(tp, 0xF892, 0xC505);
  4491. r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
  4492. r8168_mac_ocp_write(tp, 0xF896, 0xC502);
  4493. r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
  4494. r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
  4495. r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
  4496. r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
  4497. r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
  4498. r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
  4499. r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
  4500. r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
  4501. r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
  4502. r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
  4503. r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
  4504. r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
  4505. r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
  4506. r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
  4507. r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
  4508. r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
  4509. r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
  4510. r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
  4511. r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
  4512. r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
  4513. r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
  4514. r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
  4515. r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
  4516. r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
  4517. r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
  4518. r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
  4519. r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
  4520. r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
  4521. r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
  4522. r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
  4523. r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
  4524. r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
  4525. r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
  4526. r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
  4527. r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
  4528. r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
  4529. r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
  4530. r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
  4531. r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
  4532. r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
  4533. r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
  4534. r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
  4535. r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
  4536. rtl_hw_aspm_clkreq_enable(tp, true);
  4537. }
  4538. static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
  4539. {
  4540. int rg_saw_cnt;
  4541. u32 data;
  4542. static const struct ephy_info e_info_8168h_1[] = {
  4543. { 0x1e, 0x0800, 0x0001 },
  4544. { 0x1d, 0x0000, 0x0800 },
  4545. { 0x05, 0xffff, 0x2089 },
  4546. { 0x06, 0xffff, 0x5881 },
  4547. { 0x04, 0xffff, 0x154a },
  4548. { 0x01, 0xffff, 0x068b }
  4549. };
  4550. /* disable aspm and clock request before access ephy */
  4551. rtl_hw_aspm_clkreq_enable(tp, false);
  4552. rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
  4553. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  4554. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  4555. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  4556. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4557. rtl_set_def_aspm_entry_latency(tp);
  4558. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4559. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4560. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4561. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
  4562. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
  4563. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  4564. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  4565. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4566. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4567. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4568. /* Adjust EEE LED frequency */
  4569. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4570. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4571. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  4572. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  4573. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  4574. rtl_pcie_state_l2l3_enable(tp, false);
  4575. rtl_writephy(tp, 0x1f, 0x0c42);
  4576. rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
  4577. rtl_writephy(tp, 0x1f, 0x0000);
  4578. if (rg_saw_cnt > 0) {
  4579. u16 sw_cnt_1ms_ini;
  4580. sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
  4581. sw_cnt_1ms_ini &= 0x0fff;
  4582. data = r8168_mac_ocp_read(tp, 0xd412);
  4583. data &= ~0x0fff;
  4584. data |= sw_cnt_1ms_ini;
  4585. r8168_mac_ocp_write(tp, 0xd412, data);
  4586. }
  4587. data = r8168_mac_ocp_read(tp, 0xe056);
  4588. data &= ~0xf0;
  4589. data |= 0x70;
  4590. r8168_mac_ocp_write(tp, 0xe056, data);
  4591. data = r8168_mac_ocp_read(tp, 0xe052);
  4592. data &= ~0x6000;
  4593. data |= 0x8008;
  4594. r8168_mac_ocp_write(tp, 0xe052, data);
  4595. data = r8168_mac_ocp_read(tp, 0xe0d6);
  4596. data &= ~0x01ff;
  4597. data |= 0x017f;
  4598. r8168_mac_ocp_write(tp, 0xe0d6, data);
  4599. data = r8168_mac_ocp_read(tp, 0xd420);
  4600. data &= ~0x0fff;
  4601. data |= 0x047f;
  4602. r8168_mac_ocp_write(tp, 0xd420, data);
  4603. r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
  4604. r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
  4605. r8168_mac_ocp_write(tp, 0xc094, 0x0000);
  4606. r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
  4607. rtl_hw_aspm_clkreq_enable(tp, true);
  4608. }
  4609. static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
  4610. {
  4611. rtl8168ep_stop_cmac(tp);
  4612. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  4613. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
  4614. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
  4615. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4616. rtl_set_def_aspm_entry_latency(tp);
  4617. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4618. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4619. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4620. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
  4621. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  4622. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  4623. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4624. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4625. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4626. /* Adjust EEE LED frequency */
  4627. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4628. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  4629. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  4630. rtl_pcie_state_l2l3_enable(tp, false);
  4631. }
  4632. static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
  4633. {
  4634. static const struct ephy_info e_info_8168ep_1[] = {
  4635. { 0x00, 0xffff, 0x10ab },
  4636. { 0x06, 0xffff, 0xf030 },
  4637. { 0x08, 0xffff, 0x2006 },
  4638. { 0x0d, 0xffff, 0x1666 },
  4639. { 0x0c, 0x3ff0, 0x0000 }
  4640. };
  4641. /* disable aspm and clock request before access ephy */
  4642. rtl_hw_aspm_clkreq_enable(tp, false);
  4643. rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
  4644. rtl_hw_start_8168ep(tp);
  4645. rtl_hw_aspm_clkreq_enable(tp, true);
  4646. }
  4647. static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
  4648. {
  4649. static const struct ephy_info e_info_8168ep_2[] = {
  4650. { 0x00, 0xffff, 0x10a3 },
  4651. { 0x19, 0xffff, 0xfc00 },
  4652. { 0x1e, 0xffff, 0x20ea }
  4653. };
  4654. /* disable aspm and clock request before access ephy */
  4655. rtl_hw_aspm_clkreq_enable(tp, false);
  4656. rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
  4657. rtl_hw_start_8168ep(tp);
  4658. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4659. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  4660. rtl_hw_aspm_clkreq_enable(tp, true);
  4661. }
  4662. static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
  4663. {
  4664. u32 data;
  4665. static const struct ephy_info e_info_8168ep_3[] = {
  4666. { 0x00, 0xffff, 0x10a3 },
  4667. { 0x19, 0xffff, 0x7c00 },
  4668. { 0x1e, 0xffff, 0x20eb },
  4669. { 0x0d, 0xffff, 0x1666 }
  4670. };
  4671. /* disable aspm and clock request before access ephy */
  4672. rtl_hw_aspm_clkreq_enable(tp, false);
  4673. rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
  4674. rtl_hw_start_8168ep(tp);
  4675. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4676. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  4677. data = r8168_mac_ocp_read(tp, 0xd3e2);
  4678. data &= 0xf000;
  4679. data |= 0x0271;
  4680. r8168_mac_ocp_write(tp, 0xd3e2, data);
  4681. data = r8168_mac_ocp_read(tp, 0xd3e4);
  4682. data &= 0xff00;
  4683. r8168_mac_ocp_write(tp, 0xd3e4, data);
  4684. data = r8168_mac_ocp_read(tp, 0xe860);
  4685. data |= 0x0080;
  4686. r8168_mac_ocp_write(tp, 0xe860, data);
  4687. rtl_hw_aspm_clkreq_enable(tp, true);
  4688. }
  4689. static void rtl_hw_start_8168(struct rtl8169_private *tp)
  4690. {
  4691. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4692. tp->cp_cmd &= ~INTT_MASK;
  4693. tp->cp_cmd |= PktCntrDisable | INTT_1;
  4694. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4695. RTL_W16(tp, IntrMitigate, 0x5100);
  4696. /* Work around for RxFIFO overflow. */
  4697. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  4698. tp->event_slow |= RxFIFOOver | PCSTimeout;
  4699. tp->event_slow &= ~RxOverflow;
  4700. }
  4701. switch (tp->mac_version) {
  4702. case RTL_GIGA_MAC_VER_11:
  4703. rtl_hw_start_8168bb(tp);
  4704. break;
  4705. case RTL_GIGA_MAC_VER_12:
  4706. case RTL_GIGA_MAC_VER_17:
  4707. rtl_hw_start_8168bef(tp);
  4708. break;
  4709. case RTL_GIGA_MAC_VER_18:
  4710. rtl_hw_start_8168cp_1(tp);
  4711. break;
  4712. case RTL_GIGA_MAC_VER_19:
  4713. rtl_hw_start_8168c_1(tp);
  4714. break;
  4715. case RTL_GIGA_MAC_VER_20:
  4716. rtl_hw_start_8168c_2(tp);
  4717. break;
  4718. case RTL_GIGA_MAC_VER_21:
  4719. rtl_hw_start_8168c_3(tp);
  4720. break;
  4721. case RTL_GIGA_MAC_VER_22:
  4722. rtl_hw_start_8168c_4(tp);
  4723. break;
  4724. case RTL_GIGA_MAC_VER_23:
  4725. rtl_hw_start_8168cp_2(tp);
  4726. break;
  4727. case RTL_GIGA_MAC_VER_24:
  4728. rtl_hw_start_8168cp_3(tp);
  4729. break;
  4730. case RTL_GIGA_MAC_VER_25:
  4731. case RTL_GIGA_MAC_VER_26:
  4732. case RTL_GIGA_MAC_VER_27:
  4733. rtl_hw_start_8168d(tp);
  4734. break;
  4735. case RTL_GIGA_MAC_VER_28:
  4736. rtl_hw_start_8168d_4(tp);
  4737. break;
  4738. case RTL_GIGA_MAC_VER_31:
  4739. rtl_hw_start_8168dp(tp);
  4740. break;
  4741. case RTL_GIGA_MAC_VER_32:
  4742. case RTL_GIGA_MAC_VER_33:
  4743. rtl_hw_start_8168e_1(tp);
  4744. break;
  4745. case RTL_GIGA_MAC_VER_34:
  4746. rtl_hw_start_8168e_2(tp);
  4747. break;
  4748. case RTL_GIGA_MAC_VER_35:
  4749. case RTL_GIGA_MAC_VER_36:
  4750. rtl_hw_start_8168f_1(tp);
  4751. break;
  4752. case RTL_GIGA_MAC_VER_38:
  4753. rtl_hw_start_8411(tp);
  4754. break;
  4755. case RTL_GIGA_MAC_VER_40:
  4756. case RTL_GIGA_MAC_VER_41:
  4757. rtl_hw_start_8168g_1(tp);
  4758. break;
  4759. case RTL_GIGA_MAC_VER_42:
  4760. rtl_hw_start_8168g_2(tp);
  4761. break;
  4762. case RTL_GIGA_MAC_VER_44:
  4763. rtl_hw_start_8411_2(tp);
  4764. break;
  4765. case RTL_GIGA_MAC_VER_45:
  4766. case RTL_GIGA_MAC_VER_46:
  4767. rtl_hw_start_8168h_1(tp);
  4768. break;
  4769. case RTL_GIGA_MAC_VER_49:
  4770. rtl_hw_start_8168ep_1(tp);
  4771. break;
  4772. case RTL_GIGA_MAC_VER_50:
  4773. rtl_hw_start_8168ep_2(tp);
  4774. break;
  4775. case RTL_GIGA_MAC_VER_51:
  4776. rtl_hw_start_8168ep_3(tp);
  4777. break;
  4778. default:
  4779. netif_err(tp, drv, tp->dev,
  4780. "unknown chipset (mac_version = %d)\n",
  4781. tp->mac_version);
  4782. break;
  4783. }
  4784. }
  4785. static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
  4786. {
  4787. static const struct ephy_info e_info_8102e_1[] = {
  4788. { 0x01, 0, 0x6e65 },
  4789. { 0x02, 0, 0x091f },
  4790. { 0x03, 0, 0xc2f9 },
  4791. { 0x06, 0, 0xafb5 },
  4792. { 0x07, 0, 0x0e00 },
  4793. { 0x19, 0, 0xec80 },
  4794. { 0x01, 0, 0x2e65 },
  4795. { 0x01, 0, 0x6e65 }
  4796. };
  4797. u8 cfg1;
  4798. rtl_set_def_aspm_entry_latency(tp);
  4799. RTL_W8(tp, DBG_REG, FIX_NAK_1);
  4800. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4801. RTL_W8(tp, Config1,
  4802. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  4803. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4804. cfg1 = RTL_R8(tp, Config1);
  4805. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  4806. RTL_W8(tp, Config1, cfg1 & ~LEDS0);
  4807. rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  4808. }
  4809. static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
  4810. {
  4811. rtl_set_def_aspm_entry_latency(tp);
  4812. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4813. RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
  4814. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4815. }
  4816. static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
  4817. {
  4818. rtl_hw_start_8102e_2(tp);
  4819. rtl_ephy_write(tp, 0x03, 0xc2f9);
  4820. }
  4821. static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
  4822. {
  4823. static const struct ephy_info e_info_8105e_1[] = {
  4824. { 0x07, 0, 0x4000 },
  4825. { 0x19, 0, 0x0200 },
  4826. { 0x19, 0, 0x0020 },
  4827. { 0x1e, 0, 0x2000 },
  4828. { 0x03, 0, 0x0001 },
  4829. { 0x19, 0, 0x0100 },
  4830. { 0x19, 0, 0x0004 },
  4831. { 0x0a, 0, 0x0020 }
  4832. };
  4833. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4834. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  4835. /* Disable Early Tally Counter */
  4836. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
  4837. RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
  4838. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  4839. rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  4840. rtl_pcie_state_l2l3_enable(tp, false);
  4841. }
  4842. static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
  4843. {
  4844. rtl_hw_start_8105e_1(tp);
  4845. rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
  4846. }
  4847. static void rtl_hw_start_8402(struct rtl8169_private *tp)
  4848. {
  4849. static const struct ephy_info e_info_8402[] = {
  4850. { 0x19, 0xffff, 0xff64 },
  4851. { 0x1e, 0, 0x4000 }
  4852. };
  4853. rtl_set_def_aspm_entry_latency(tp);
  4854. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4855. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  4856. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4857. rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
  4858. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4859. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
  4860. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
  4861. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4862. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4863. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4864. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4865. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
  4866. rtl_pcie_state_l2l3_enable(tp, false);
  4867. }
  4868. static void rtl_hw_start_8106(struct rtl8169_private *tp)
  4869. {
  4870. rtl_hw_aspm_clkreq_enable(tp, false);
  4871. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4872. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  4873. RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
  4874. RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
  4875. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4876. rtl_pcie_state_l2l3_enable(tp, false);
  4877. rtl_hw_aspm_clkreq_enable(tp, true);
  4878. }
  4879. static void rtl_hw_start_8101(struct rtl8169_private *tp)
  4880. {
  4881. if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
  4882. tp->event_slow &= ~RxFIFOOver;
  4883. if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
  4884. tp->mac_version == RTL_GIGA_MAC_VER_16)
  4885. pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
  4886. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4887. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4888. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4889. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4890. switch (tp->mac_version) {
  4891. case RTL_GIGA_MAC_VER_07:
  4892. rtl_hw_start_8102e_1(tp);
  4893. break;
  4894. case RTL_GIGA_MAC_VER_08:
  4895. rtl_hw_start_8102e_3(tp);
  4896. break;
  4897. case RTL_GIGA_MAC_VER_09:
  4898. rtl_hw_start_8102e_2(tp);
  4899. break;
  4900. case RTL_GIGA_MAC_VER_29:
  4901. rtl_hw_start_8105e_1(tp);
  4902. break;
  4903. case RTL_GIGA_MAC_VER_30:
  4904. rtl_hw_start_8105e_2(tp);
  4905. break;
  4906. case RTL_GIGA_MAC_VER_37:
  4907. rtl_hw_start_8402(tp);
  4908. break;
  4909. case RTL_GIGA_MAC_VER_39:
  4910. rtl_hw_start_8106(tp);
  4911. break;
  4912. case RTL_GIGA_MAC_VER_43:
  4913. rtl_hw_start_8168g_2(tp);
  4914. break;
  4915. case RTL_GIGA_MAC_VER_47:
  4916. case RTL_GIGA_MAC_VER_48:
  4917. rtl_hw_start_8168h_1(tp);
  4918. break;
  4919. }
  4920. RTL_W16(tp, IntrMitigate, 0x0000);
  4921. }
  4922. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  4923. {
  4924. struct rtl8169_private *tp = netdev_priv(dev);
  4925. if (new_mtu > ETH_DATA_LEN)
  4926. rtl_hw_jumbo_enable(tp);
  4927. else
  4928. rtl_hw_jumbo_disable(tp);
  4929. dev->mtu = new_mtu;
  4930. netdev_update_features(dev);
  4931. return 0;
  4932. }
  4933. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  4934. {
  4935. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  4936. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  4937. }
  4938. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  4939. void **data_buff, struct RxDesc *desc)
  4940. {
  4941. dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
  4942. R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
  4943. kfree(*data_buff);
  4944. *data_buff = NULL;
  4945. rtl8169_make_unusable_by_asic(desc);
  4946. }
  4947. static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
  4948. {
  4949. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  4950. /* Force memory writes to complete before releasing descriptor */
  4951. dma_wmb();
  4952. desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
  4953. }
  4954. static inline void *rtl8169_align(void *data)
  4955. {
  4956. return (void *)ALIGN((long)data, 16);
  4957. }
  4958. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  4959. struct RxDesc *desc)
  4960. {
  4961. void *data;
  4962. dma_addr_t mapping;
  4963. struct device *d = tp_to_dev(tp);
  4964. int node = dev_to_node(d);
  4965. data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
  4966. if (!data)
  4967. return NULL;
  4968. if (rtl8169_align(data) != data) {
  4969. kfree(data);
  4970. data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
  4971. if (!data)
  4972. return NULL;
  4973. }
  4974. mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
  4975. DMA_FROM_DEVICE);
  4976. if (unlikely(dma_mapping_error(d, mapping))) {
  4977. if (net_ratelimit())
  4978. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  4979. goto err_out;
  4980. }
  4981. desc->addr = cpu_to_le64(mapping);
  4982. rtl8169_mark_to_asic(desc);
  4983. return data;
  4984. err_out:
  4985. kfree(data);
  4986. return NULL;
  4987. }
  4988. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  4989. {
  4990. unsigned int i;
  4991. for (i = 0; i < NUM_RX_DESC; i++) {
  4992. if (tp->Rx_databuff[i]) {
  4993. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  4994. tp->RxDescArray + i);
  4995. }
  4996. }
  4997. }
  4998. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  4999. {
  5000. desc->opts1 |= cpu_to_le32(RingEnd);
  5001. }
  5002. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  5003. {
  5004. unsigned int i;
  5005. for (i = 0; i < NUM_RX_DESC; i++) {
  5006. void *data;
  5007. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  5008. if (!data) {
  5009. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  5010. goto err_out;
  5011. }
  5012. tp->Rx_databuff[i] = data;
  5013. }
  5014. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  5015. return 0;
  5016. err_out:
  5017. rtl8169_rx_clear(tp);
  5018. return -ENOMEM;
  5019. }
  5020. static int rtl8169_init_ring(struct rtl8169_private *tp)
  5021. {
  5022. rtl8169_init_ring_indexes(tp);
  5023. memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
  5024. memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
  5025. return rtl8169_rx_fill(tp);
  5026. }
  5027. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  5028. struct TxDesc *desc)
  5029. {
  5030. unsigned int len = tx_skb->len;
  5031. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  5032. desc->opts1 = 0x00;
  5033. desc->opts2 = 0x00;
  5034. desc->addr = 0x00;
  5035. tx_skb->len = 0;
  5036. }
  5037. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  5038. unsigned int n)
  5039. {
  5040. unsigned int i;
  5041. for (i = 0; i < n; i++) {
  5042. unsigned int entry = (start + i) % NUM_TX_DESC;
  5043. struct ring_info *tx_skb = tp->tx_skb + entry;
  5044. unsigned int len = tx_skb->len;
  5045. if (len) {
  5046. struct sk_buff *skb = tx_skb->skb;
  5047. rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
  5048. tp->TxDescArray + entry);
  5049. if (skb) {
  5050. dev_consume_skb_any(skb);
  5051. tx_skb->skb = NULL;
  5052. }
  5053. }
  5054. }
  5055. }
  5056. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  5057. {
  5058. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  5059. tp->cur_tx = tp->dirty_tx = 0;
  5060. }
  5061. static void rtl_reset_work(struct rtl8169_private *tp)
  5062. {
  5063. struct net_device *dev = tp->dev;
  5064. int i;
  5065. napi_disable(&tp->napi);
  5066. netif_stop_queue(dev);
  5067. synchronize_sched();
  5068. rtl8169_hw_reset(tp);
  5069. for (i = 0; i < NUM_RX_DESC; i++)
  5070. rtl8169_mark_to_asic(tp->RxDescArray + i);
  5071. rtl8169_tx_clear(tp);
  5072. rtl8169_init_ring_indexes(tp);
  5073. napi_enable(&tp->napi);
  5074. rtl_hw_start(tp);
  5075. netif_wake_queue(dev);
  5076. }
  5077. static void rtl8169_tx_timeout(struct net_device *dev)
  5078. {
  5079. struct rtl8169_private *tp = netdev_priv(dev);
  5080. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5081. }
  5082. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  5083. u32 *opts)
  5084. {
  5085. struct skb_shared_info *info = skb_shinfo(skb);
  5086. unsigned int cur_frag, entry;
  5087. struct TxDesc *uninitialized_var(txd);
  5088. struct device *d = tp_to_dev(tp);
  5089. entry = tp->cur_tx;
  5090. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  5091. const skb_frag_t *frag = info->frags + cur_frag;
  5092. dma_addr_t mapping;
  5093. u32 status, len;
  5094. void *addr;
  5095. entry = (entry + 1) % NUM_TX_DESC;
  5096. txd = tp->TxDescArray + entry;
  5097. len = skb_frag_size(frag);
  5098. addr = skb_frag_address(frag);
  5099. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  5100. if (unlikely(dma_mapping_error(d, mapping))) {
  5101. if (net_ratelimit())
  5102. netif_err(tp, drv, tp->dev,
  5103. "Failed to map TX fragments DMA!\n");
  5104. goto err_out;
  5105. }
  5106. /* Anti gcc 2.95.3 bugware (sic) */
  5107. status = opts[0] | len |
  5108. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5109. txd->opts1 = cpu_to_le32(status);
  5110. txd->opts2 = cpu_to_le32(opts[1]);
  5111. txd->addr = cpu_to_le64(mapping);
  5112. tp->tx_skb[entry].len = len;
  5113. }
  5114. if (cur_frag) {
  5115. tp->tx_skb[entry].skb = skb;
  5116. txd->opts1 |= cpu_to_le32(LastFrag);
  5117. }
  5118. return cur_frag;
  5119. err_out:
  5120. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  5121. return -EIO;
  5122. }
  5123. static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
  5124. {
  5125. return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
  5126. }
  5127. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5128. struct net_device *dev);
  5129. /* r8169_csum_workaround()
  5130. * The hw limites the value the transport offset. When the offset is out of the
  5131. * range, calculate the checksum by sw.
  5132. */
  5133. static void r8169_csum_workaround(struct rtl8169_private *tp,
  5134. struct sk_buff *skb)
  5135. {
  5136. if (skb_shinfo(skb)->gso_size) {
  5137. netdev_features_t features = tp->dev->features;
  5138. struct sk_buff *segs, *nskb;
  5139. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  5140. segs = skb_gso_segment(skb, features);
  5141. if (IS_ERR(segs) || !segs)
  5142. goto drop;
  5143. do {
  5144. nskb = segs;
  5145. segs = segs->next;
  5146. nskb->next = NULL;
  5147. rtl8169_start_xmit(nskb, tp->dev);
  5148. } while (segs);
  5149. dev_consume_skb_any(skb);
  5150. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5151. if (skb_checksum_help(skb) < 0)
  5152. goto drop;
  5153. rtl8169_start_xmit(skb, tp->dev);
  5154. } else {
  5155. struct net_device_stats *stats;
  5156. drop:
  5157. stats = &tp->dev->stats;
  5158. stats->tx_dropped++;
  5159. dev_kfree_skb_any(skb);
  5160. }
  5161. }
  5162. /* msdn_giant_send_check()
  5163. * According to the document of microsoft, the TCP Pseudo Header excludes the
  5164. * packet length for IPv6 TCP large packets.
  5165. */
  5166. static int msdn_giant_send_check(struct sk_buff *skb)
  5167. {
  5168. const struct ipv6hdr *ipv6h;
  5169. struct tcphdr *th;
  5170. int ret;
  5171. ret = skb_cow_head(skb, 0);
  5172. if (ret)
  5173. return ret;
  5174. ipv6h = ipv6_hdr(skb);
  5175. th = tcp_hdr(skb);
  5176. th->check = 0;
  5177. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  5178. return ret;
  5179. }
  5180. static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
  5181. struct sk_buff *skb, u32 *opts)
  5182. {
  5183. u32 mss = skb_shinfo(skb)->gso_size;
  5184. if (mss) {
  5185. opts[0] |= TD_LSO;
  5186. opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
  5187. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5188. const struct iphdr *ip = ip_hdr(skb);
  5189. if (ip->protocol == IPPROTO_TCP)
  5190. opts[0] |= TD0_IP_CS | TD0_TCP_CS;
  5191. else if (ip->protocol == IPPROTO_UDP)
  5192. opts[0] |= TD0_IP_CS | TD0_UDP_CS;
  5193. else
  5194. WARN_ON_ONCE(1);
  5195. }
  5196. return true;
  5197. }
  5198. static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
  5199. struct sk_buff *skb, u32 *opts)
  5200. {
  5201. u32 transport_offset = (u32)skb_transport_offset(skb);
  5202. u32 mss = skb_shinfo(skb)->gso_size;
  5203. if (mss) {
  5204. if (transport_offset > GTTCPHO_MAX) {
  5205. netif_warn(tp, tx_err, tp->dev,
  5206. "Invalid transport offset 0x%x for TSO\n",
  5207. transport_offset);
  5208. return false;
  5209. }
  5210. switch (vlan_get_protocol(skb)) {
  5211. case htons(ETH_P_IP):
  5212. opts[0] |= TD1_GTSENV4;
  5213. break;
  5214. case htons(ETH_P_IPV6):
  5215. if (msdn_giant_send_check(skb))
  5216. return false;
  5217. opts[0] |= TD1_GTSENV6;
  5218. break;
  5219. default:
  5220. WARN_ON_ONCE(1);
  5221. break;
  5222. }
  5223. opts[0] |= transport_offset << GTTCPHO_SHIFT;
  5224. opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
  5225. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5226. u8 ip_protocol;
  5227. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5228. return !(skb_checksum_help(skb) || eth_skb_pad(skb));
  5229. if (transport_offset > TCPHO_MAX) {
  5230. netif_warn(tp, tx_err, tp->dev,
  5231. "Invalid transport offset 0x%x\n",
  5232. transport_offset);
  5233. return false;
  5234. }
  5235. switch (vlan_get_protocol(skb)) {
  5236. case htons(ETH_P_IP):
  5237. opts[1] |= TD1_IPv4_CS;
  5238. ip_protocol = ip_hdr(skb)->protocol;
  5239. break;
  5240. case htons(ETH_P_IPV6):
  5241. opts[1] |= TD1_IPv6_CS;
  5242. ip_protocol = ipv6_hdr(skb)->nexthdr;
  5243. break;
  5244. default:
  5245. ip_protocol = IPPROTO_RAW;
  5246. break;
  5247. }
  5248. if (ip_protocol == IPPROTO_TCP)
  5249. opts[1] |= TD1_TCP_CS;
  5250. else if (ip_protocol == IPPROTO_UDP)
  5251. opts[1] |= TD1_UDP_CS;
  5252. else
  5253. WARN_ON_ONCE(1);
  5254. opts[1] |= transport_offset << TCPHO_SHIFT;
  5255. } else {
  5256. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5257. /* eth_skb_pad would free the skb on error */
  5258. return !__skb_put_padto(skb, ETH_ZLEN, false);
  5259. }
  5260. return true;
  5261. }
  5262. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5263. struct net_device *dev)
  5264. {
  5265. struct rtl8169_private *tp = netdev_priv(dev);
  5266. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  5267. struct TxDesc *txd = tp->TxDescArray + entry;
  5268. struct device *d = tp_to_dev(tp);
  5269. dma_addr_t mapping;
  5270. u32 status, len;
  5271. u32 opts[2];
  5272. int frags;
  5273. if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
  5274. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  5275. goto err_stop_0;
  5276. }
  5277. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  5278. goto err_stop_0;
  5279. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
  5280. opts[0] = DescOwn;
  5281. if (!tp->tso_csum(tp, skb, opts)) {
  5282. r8169_csum_workaround(tp, skb);
  5283. return NETDEV_TX_OK;
  5284. }
  5285. len = skb_headlen(skb);
  5286. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  5287. if (unlikely(dma_mapping_error(d, mapping))) {
  5288. if (net_ratelimit())
  5289. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  5290. goto err_dma_0;
  5291. }
  5292. tp->tx_skb[entry].len = len;
  5293. txd->addr = cpu_to_le64(mapping);
  5294. frags = rtl8169_xmit_frags(tp, skb, opts);
  5295. if (frags < 0)
  5296. goto err_dma_1;
  5297. else if (frags)
  5298. opts[0] |= FirstFrag;
  5299. else {
  5300. opts[0] |= FirstFrag | LastFrag;
  5301. tp->tx_skb[entry].skb = skb;
  5302. }
  5303. txd->opts2 = cpu_to_le32(opts[1]);
  5304. skb_tx_timestamp(skb);
  5305. /* Force memory writes to complete before releasing descriptor */
  5306. dma_wmb();
  5307. /* Anti gcc 2.95.3 bugware (sic) */
  5308. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5309. txd->opts1 = cpu_to_le32(status);
  5310. /* Force all memory writes to complete before notifying device */
  5311. wmb();
  5312. tp->cur_tx += frags + 1;
  5313. RTL_W8(tp, TxPoll, NPQ);
  5314. mmiowb();
  5315. if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  5316. /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
  5317. * not miss a ring update when it notices a stopped queue.
  5318. */
  5319. smp_wmb();
  5320. netif_stop_queue(dev);
  5321. /* Sync with rtl_tx:
  5322. * - publish queue status and cur_tx ring index (write barrier)
  5323. * - refresh dirty_tx ring index (read barrier).
  5324. * May the current thread have a pessimistic view of the ring
  5325. * status and forget to wake up queue, a racing rtl_tx thread
  5326. * can't.
  5327. */
  5328. smp_mb();
  5329. if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
  5330. netif_wake_queue(dev);
  5331. }
  5332. return NETDEV_TX_OK;
  5333. err_dma_1:
  5334. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  5335. err_dma_0:
  5336. dev_kfree_skb_any(skb);
  5337. dev->stats.tx_dropped++;
  5338. return NETDEV_TX_OK;
  5339. err_stop_0:
  5340. netif_stop_queue(dev);
  5341. dev->stats.tx_dropped++;
  5342. return NETDEV_TX_BUSY;
  5343. }
  5344. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  5345. {
  5346. struct rtl8169_private *tp = netdev_priv(dev);
  5347. struct pci_dev *pdev = tp->pci_dev;
  5348. u16 pci_status, pci_cmd;
  5349. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  5350. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  5351. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  5352. pci_cmd, pci_status);
  5353. /*
  5354. * The recovery sequence below admits a very elaborated explanation:
  5355. * - it seems to work;
  5356. * - I did not see what else could be done;
  5357. * - it makes iop3xx happy.
  5358. *
  5359. * Feel free to adjust to your needs.
  5360. */
  5361. if (pdev->broken_parity_status)
  5362. pci_cmd &= ~PCI_COMMAND_PARITY;
  5363. else
  5364. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  5365. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  5366. pci_write_config_word(pdev, PCI_STATUS,
  5367. pci_status & (PCI_STATUS_DETECTED_PARITY |
  5368. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  5369. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  5370. /* The infamous DAC f*ckup only happens at boot time */
  5371. if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
  5372. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  5373. tp->cp_cmd &= ~PCIDAC;
  5374. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  5375. dev->features &= ~NETIF_F_HIGHDMA;
  5376. }
  5377. rtl8169_hw_reset(tp);
  5378. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5379. }
  5380. static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
  5381. {
  5382. unsigned int dirty_tx, tx_left;
  5383. dirty_tx = tp->dirty_tx;
  5384. smp_rmb();
  5385. tx_left = tp->cur_tx - dirty_tx;
  5386. while (tx_left > 0) {
  5387. unsigned int entry = dirty_tx % NUM_TX_DESC;
  5388. struct ring_info *tx_skb = tp->tx_skb + entry;
  5389. u32 status;
  5390. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  5391. if (status & DescOwn)
  5392. break;
  5393. /* This barrier is needed to keep us from reading
  5394. * any other fields out of the Tx descriptor until
  5395. * we know the status of DescOwn
  5396. */
  5397. dma_rmb();
  5398. rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
  5399. tp->TxDescArray + entry);
  5400. if (status & LastFrag) {
  5401. u64_stats_update_begin(&tp->tx_stats.syncp);
  5402. tp->tx_stats.packets++;
  5403. tp->tx_stats.bytes += tx_skb->skb->len;
  5404. u64_stats_update_end(&tp->tx_stats.syncp);
  5405. dev_consume_skb_any(tx_skb->skb);
  5406. tx_skb->skb = NULL;
  5407. }
  5408. dirty_tx++;
  5409. tx_left--;
  5410. }
  5411. if (tp->dirty_tx != dirty_tx) {
  5412. tp->dirty_tx = dirty_tx;
  5413. /* Sync with rtl8169_start_xmit:
  5414. * - publish dirty_tx ring index (write barrier)
  5415. * - refresh cur_tx ring index and queue status (read barrier)
  5416. * May the current thread miss the stopped queue condition,
  5417. * a racing xmit thread can only have a right view of the
  5418. * ring status.
  5419. */
  5420. smp_mb();
  5421. if (netif_queue_stopped(dev) &&
  5422. TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  5423. netif_wake_queue(dev);
  5424. }
  5425. /*
  5426. * 8168 hack: TxPoll requests are lost when the Tx packets are
  5427. * too close. Let's kick an extra TxPoll request when a burst
  5428. * of start_xmit activity is detected (if it is not detected,
  5429. * it is slow enough). -- FR
  5430. */
  5431. if (tp->cur_tx != dirty_tx)
  5432. RTL_W8(tp, TxPoll, NPQ);
  5433. }
  5434. }
  5435. static inline int rtl8169_fragmented_frame(u32 status)
  5436. {
  5437. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  5438. }
  5439. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  5440. {
  5441. u32 status = opts1 & RxProtoMask;
  5442. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  5443. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  5444. skb->ip_summed = CHECKSUM_UNNECESSARY;
  5445. else
  5446. skb_checksum_none_assert(skb);
  5447. }
  5448. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  5449. struct rtl8169_private *tp,
  5450. int pkt_size,
  5451. dma_addr_t addr)
  5452. {
  5453. struct sk_buff *skb;
  5454. struct device *d = tp_to_dev(tp);
  5455. data = rtl8169_align(data);
  5456. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  5457. prefetch(data);
  5458. skb = napi_alloc_skb(&tp->napi, pkt_size);
  5459. if (skb)
  5460. skb_copy_to_linear_data(skb, data, pkt_size);
  5461. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  5462. return skb;
  5463. }
  5464. static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
  5465. {
  5466. unsigned int cur_rx, rx_left;
  5467. unsigned int count;
  5468. cur_rx = tp->cur_rx;
  5469. for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
  5470. unsigned int entry = cur_rx % NUM_RX_DESC;
  5471. struct RxDesc *desc = tp->RxDescArray + entry;
  5472. u32 status;
  5473. status = le32_to_cpu(desc->opts1);
  5474. if (status & DescOwn)
  5475. break;
  5476. /* This barrier is needed to keep us from reading
  5477. * any other fields out of the Rx descriptor until
  5478. * we know the status of DescOwn
  5479. */
  5480. dma_rmb();
  5481. if (unlikely(status & RxRES)) {
  5482. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  5483. status);
  5484. dev->stats.rx_errors++;
  5485. if (status & (RxRWT | RxRUNT))
  5486. dev->stats.rx_length_errors++;
  5487. if (status & RxCRC)
  5488. dev->stats.rx_crc_errors++;
  5489. /* RxFOVF is a reserved bit on later chip versions */
  5490. if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
  5491. status & RxFOVF) {
  5492. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5493. dev->stats.rx_fifo_errors++;
  5494. } else if (status & (RxRUNT | RxCRC) &&
  5495. !(status & RxRWT) &&
  5496. dev->features & NETIF_F_RXALL) {
  5497. goto process_pkt;
  5498. }
  5499. } else {
  5500. struct sk_buff *skb;
  5501. dma_addr_t addr;
  5502. int pkt_size;
  5503. process_pkt:
  5504. addr = le64_to_cpu(desc->addr);
  5505. if (likely(!(dev->features & NETIF_F_RXFCS)))
  5506. pkt_size = (status & 0x00003fff) - 4;
  5507. else
  5508. pkt_size = status & 0x00003fff;
  5509. /*
  5510. * The driver does not support incoming fragmented
  5511. * frames. They are seen as a symptom of over-mtu
  5512. * sized frames.
  5513. */
  5514. if (unlikely(rtl8169_fragmented_frame(status))) {
  5515. dev->stats.rx_dropped++;
  5516. dev->stats.rx_length_errors++;
  5517. goto release_descriptor;
  5518. }
  5519. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  5520. tp, pkt_size, addr);
  5521. if (!skb) {
  5522. dev->stats.rx_dropped++;
  5523. goto release_descriptor;
  5524. }
  5525. rtl8169_rx_csum(skb, status);
  5526. skb_put(skb, pkt_size);
  5527. skb->protocol = eth_type_trans(skb, dev);
  5528. rtl8169_rx_vlan_tag(desc, skb);
  5529. if (skb->pkt_type == PACKET_MULTICAST)
  5530. dev->stats.multicast++;
  5531. napi_gro_receive(&tp->napi, skb);
  5532. u64_stats_update_begin(&tp->rx_stats.syncp);
  5533. tp->rx_stats.packets++;
  5534. tp->rx_stats.bytes += pkt_size;
  5535. u64_stats_update_end(&tp->rx_stats.syncp);
  5536. }
  5537. release_descriptor:
  5538. desc->opts2 = 0;
  5539. rtl8169_mark_to_asic(desc);
  5540. }
  5541. count = cur_rx - tp->cur_rx;
  5542. tp->cur_rx = cur_rx;
  5543. return count;
  5544. }
  5545. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  5546. {
  5547. struct rtl8169_private *tp = dev_instance;
  5548. u16 status = rtl_get_events(tp);
  5549. if (status == 0xffff || !(status & (RTL_EVENT_NAPI | tp->event_slow)))
  5550. return IRQ_NONE;
  5551. rtl_irq_disable(tp);
  5552. napi_schedule(&tp->napi);
  5553. return IRQ_HANDLED;
  5554. }
  5555. /*
  5556. * Workqueue context.
  5557. */
  5558. static void rtl_slow_event_work(struct rtl8169_private *tp)
  5559. {
  5560. struct net_device *dev = tp->dev;
  5561. u16 status;
  5562. status = rtl_get_events(tp) & tp->event_slow;
  5563. rtl_ack_events(tp, status);
  5564. if (unlikely(status & RxFIFOOver)) {
  5565. switch (tp->mac_version) {
  5566. /* Work around for rx fifo overflow */
  5567. case RTL_GIGA_MAC_VER_11:
  5568. netif_stop_queue(dev);
  5569. /* XXX - Hack alert. See rtl_task(). */
  5570. set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
  5571. default:
  5572. break;
  5573. }
  5574. }
  5575. if (unlikely(status & SYSErr))
  5576. rtl8169_pcierr_interrupt(dev);
  5577. if (status & LinkChg)
  5578. phy_mac_interrupt(dev->phydev);
  5579. rtl_irq_enable_all(tp);
  5580. }
  5581. static void rtl_task(struct work_struct *work)
  5582. {
  5583. static const struct {
  5584. int bitnr;
  5585. void (*action)(struct rtl8169_private *);
  5586. } rtl_work[] = {
  5587. /* XXX - keep rtl_slow_event_work() as first element. */
  5588. { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
  5589. { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
  5590. };
  5591. struct rtl8169_private *tp =
  5592. container_of(work, struct rtl8169_private, wk.work);
  5593. struct net_device *dev = tp->dev;
  5594. int i;
  5595. rtl_lock_work(tp);
  5596. if (!netif_running(dev) ||
  5597. !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
  5598. goto out_unlock;
  5599. for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
  5600. bool pending;
  5601. pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
  5602. if (pending)
  5603. rtl_work[i].action(tp);
  5604. }
  5605. out_unlock:
  5606. rtl_unlock_work(tp);
  5607. }
  5608. static int rtl8169_poll(struct napi_struct *napi, int budget)
  5609. {
  5610. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  5611. struct net_device *dev = tp->dev;
  5612. u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
  5613. int work_done;
  5614. u16 status;
  5615. status = rtl_get_events(tp);
  5616. rtl_ack_events(tp, status & ~tp->event_slow);
  5617. work_done = rtl_rx(dev, tp, (u32) budget);
  5618. rtl_tx(dev, tp);
  5619. if (status & tp->event_slow) {
  5620. enable_mask &= ~tp->event_slow;
  5621. rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
  5622. }
  5623. if (work_done < budget) {
  5624. napi_complete_done(napi, work_done);
  5625. rtl_irq_enable(tp, enable_mask);
  5626. mmiowb();
  5627. }
  5628. return work_done;
  5629. }
  5630. static void rtl8169_rx_missed(struct net_device *dev)
  5631. {
  5632. struct rtl8169_private *tp = netdev_priv(dev);
  5633. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  5634. return;
  5635. dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
  5636. RTL_W32(tp, RxMissed, 0);
  5637. }
  5638. static void r8169_phylink_handler(struct net_device *ndev)
  5639. {
  5640. struct rtl8169_private *tp = netdev_priv(ndev);
  5641. if (netif_carrier_ok(ndev)) {
  5642. rtl_link_chg_patch(tp);
  5643. pm_request_resume(&tp->pci_dev->dev);
  5644. } else {
  5645. pm_runtime_idle(&tp->pci_dev->dev);
  5646. }
  5647. if (net_ratelimit())
  5648. phy_print_status(ndev->phydev);
  5649. }
  5650. static int r8169_phy_connect(struct rtl8169_private *tp)
  5651. {
  5652. struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
  5653. phy_interface_t phy_mode;
  5654. int ret;
  5655. phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
  5656. PHY_INTERFACE_MODE_MII;
  5657. ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
  5658. phy_mode);
  5659. if (ret)
  5660. return ret;
  5661. if (!tp->supports_gmii)
  5662. phy_set_max_speed(phydev, SPEED_100);
  5663. /* Ensure to advertise everything, incl. pause */
  5664. phydev->advertising = phydev->supported;
  5665. phy_attached_info(phydev);
  5666. return 0;
  5667. }
  5668. static void rtl8169_down(struct net_device *dev)
  5669. {
  5670. struct rtl8169_private *tp = netdev_priv(dev);
  5671. phy_stop(dev->phydev);
  5672. napi_disable(&tp->napi);
  5673. netif_stop_queue(dev);
  5674. rtl8169_hw_reset(tp);
  5675. /*
  5676. * At this point device interrupts can not be enabled in any function,
  5677. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
  5678. * and napi is disabled (rtl8169_poll).
  5679. */
  5680. rtl8169_rx_missed(dev);
  5681. /* Give a racing hard_start_xmit a few cycles to complete. */
  5682. synchronize_sched();
  5683. rtl8169_tx_clear(tp);
  5684. rtl8169_rx_clear(tp);
  5685. rtl_pll_power_down(tp);
  5686. }
  5687. static int rtl8169_close(struct net_device *dev)
  5688. {
  5689. struct rtl8169_private *tp = netdev_priv(dev);
  5690. struct pci_dev *pdev = tp->pci_dev;
  5691. pm_runtime_get_sync(&pdev->dev);
  5692. /* Update counters before going down */
  5693. rtl8169_update_counters(tp);
  5694. rtl_lock_work(tp);
  5695. /* Clear all task flags */
  5696. bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
  5697. rtl8169_down(dev);
  5698. rtl_unlock_work(tp);
  5699. cancel_work_sync(&tp->wk.work);
  5700. phy_disconnect(dev->phydev);
  5701. free_irq(pci_irq_vector(pdev, 0), tp);
  5702. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  5703. tp->RxPhyAddr);
  5704. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  5705. tp->TxPhyAddr);
  5706. tp->TxDescArray = NULL;
  5707. tp->RxDescArray = NULL;
  5708. pm_runtime_put_sync(&pdev->dev);
  5709. return 0;
  5710. }
  5711. #ifdef CONFIG_NET_POLL_CONTROLLER
  5712. static void rtl8169_netpoll(struct net_device *dev)
  5713. {
  5714. struct rtl8169_private *tp = netdev_priv(dev);
  5715. rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
  5716. }
  5717. #endif
  5718. static int rtl_open(struct net_device *dev)
  5719. {
  5720. struct rtl8169_private *tp = netdev_priv(dev);
  5721. struct pci_dev *pdev = tp->pci_dev;
  5722. int retval = -ENOMEM;
  5723. pm_runtime_get_sync(&pdev->dev);
  5724. /*
  5725. * Rx and Tx descriptors needs 256 bytes alignment.
  5726. * dma_alloc_coherent provides more.
  5727. */
  5728. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  5729. &tp->TxPhyAddr, GFP_KERNEL);
  5730. if (!tp->TxDescArray)
  5731. goto err_pm_runtime_put;
  5732. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  5733. &tp->RxPhyAddr, GFP_KERNEL);
  5734. if (!tp->RxDescArray)
  5735. goto err_free_tx_0;
  5736. retval = rtl8169_init_ring(tp);
  5737. if (retval < 0)
  5738. goto err_free_rx_1;
  5739. INIT_WORK(&tp->wk.work, rtl_task);
  5740. smp_mb();
  5741. rtl_request_firmware(tp);
  5742. retval = request_irq(pci_irq_vector(pdev, 0), rtl8169_interrupt,
  5743. IRQF_SHARED, dev->name, tp);
  5744. if (retval < 0)
  5745. goto err_release_fw_2;
  5746. retval = r8169_phy_connect(tp);
  5747. if (retval)
  5748. goto err_free_irq;
  5749. rtl_lock_work(tp);
  5750. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  5751. napi_enable(&tp->napi);
  5752. rtl8169_init_phy(dev, tp);
  5753. rtl_pll_power_up(tp);
  5754. rtl_hw_start(tp);
  5755. if (!rtl8169_init_counter_offsets(tp))
  5756. netif_warn(tp, hw, dev, "counter reset/update failed\n");
  5757. phy_start(dev->phydev);
  5758. netif_start_queue(dev);
  5759. rtl_unlock_work(tp);
  5760. pm_runtime_put_sync(&pdev->dev);
  5761. out:
  5762. return retval;
  5763. err_free_irq:
  5764. free_irq(pci_irq_vector(pdev, 0), tp);
  5765. err_release_fw_2:
  5766. rtl_release_firmware(tp);
  5767. rtl8169_rx_clear(tp);
  5768. err_free_rx_1:
  5769. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  5770. tp->RxPhyAddr);
  5771. tp->RxDescArray = NULL;
  5772. err_free_tx_0:
  5773. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  5774. tp->TxPhyAddr);
  5775. tp->TxDescArray = NULL;
  5776. err_pm_runtime_put:
  5777. pm_runtime_put_noidle(&pdev->dev);
  5778. goto out;
  5779. }
  5780. static void
  5781. rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  5782. {
  5783. struct rtl8169_private *tp = netdev_priv(dev);
  5784. struct pci_dev *pdev = tp->pci_dev;
  5785. struct rtl8169_counters *counters = tp->counters;
  5786. unsigned int start;
  5787. pm_runtime_get_noresume(&pdev->dev);
  5788. if (netif_running(dev) && pm_runtime_active(&pdev->dev))
  5789. rtl8169_rx_missed(dev);
  5790. do {
  5791. start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
  5792. stats->rx_packets = tp->rx_stats.packets;
  5793. stats->rx_bytes = tp->rx_stats.bytes;
  5794. } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
  5795. do {
  5796. start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
  5797. stats->tx_packets = tp->tx_stats.packets;
  5798. stats->tx_bytes = tp->tx_stats.bytes;
  5799. } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
  5800. stats->rx_dropped = dev->stats.rx_dropped;
  5801. stats->tx_dropped = dev->stats.tx_dropped;
  5802. stats->rx_length_errors = dev->stats.rx_length_errors;
  5803. stats->rx_errors = dev->stats.rx_errors;
  5804. stats->rx_crc_errors = dev->stats.rx_crc_errors;
  5805. stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
  5806. stats->rx_missed_errors = dev->stats.rx_missed_errors;
  5807. stats->multicast = dev->stats.multicast;
  5808. /*
  5809. * Fetch additonal counter values missing in stats collected by driver
  5810. * from tally counters.
  5811. */
  5812. if (pm_runtime_active(&pdev->dev))
  5813. rtl8169_update_counters(tp);
  5814. /*
  5815. * Subtract values fetched during initalization.
  5816. * See rtl8169_init_counter_offsets for a description why we do that.
  5817. */
  5818. stats->tx_errors = le64_to_cpu(counters->tx_errors) -
  5819. le64_to_cpu(tp->tc_offset.tx_errors);
  5820. stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
  5821. le32_to_cpu(tp->tc_offset.tx_multi_collision);
  5822. stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
  5823. le16_to_cpu(tp->tc_offset.tx_aborted);
  5824. pm_runtime_put_noidle(&pdev->dev);
  5825. }
  5826. static void rtl8169_net_suspend(struct net_device *dev)
  5827. {
  5828. struct rtl8169_private *tp = netdev_priv(dev);
  5829. if (!netif_running(dev))
  5830. return;
  5831. phy_stop(dev->phydev);
  5832. netif_device_detach(dev);
  5833. netif_stop_queue(dev);
  5834. rtl_lock_work(tp);
  5835. napi_disable(&tp->napi);
  5836. /* Clear all task flags */
  5837. bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
  5838. rtl_unlock_work(tp);
  5839. rtl_pll_power_down(tp);
  5840. }
  5841. #ifdef CONFIG_PM
  5842. static int rtl8169_suspend(struct device *device)
  5843. {
  5844. struct pci_dev *pdev = to_pci_dev(device);
  5845. struct net_device *dev = pci_get_drvdata(pdev);
  5846. struct rtl8169_private *tp = netdev_priv(dev);
  5847. rtl8169_net_suspend(dev);
  5848. clk_disable_unprepare(tp->clk);
  5849. return 0;
  5850. }
  5851. static void __rtl8169_resume(struct net_device *dev)
  5852. {
  5853. struct rtl8169_private *tp = netdev_priv(dev);
  5854. netif_device_attach(dev);
  5855. rtl_pll_power_up(tp);
  5856. rtl8169_init_phy(dev, tp);
  5857. phy_start(tp->dev->phydev);
  5858. rtl_lock_work(tp);
  5859. napi_enable(&tp->napi);
  5860. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  5861. rtl_unlock_work(tp);
  5862. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5863. }
  5864. static int rtl8169_resume(struct device *device)
  5865. {
  5866. struct pci_dev *pdev = to_pci_dev(device);
  5867. struct net_device *dev = pci_get_drvdata(pdev);
  5868. struct rtl8169_private *tp = netdev_priv(dev);
  5869. clk_prepare_enable(tp->clk);
  5870. if (netif_running(dev))
  5871. __rtl8169_resume(dev);
  5872. return 0;
  5873. }
  5874. static int rtl8169_runtime_suspend(struct device *device)
  5875. {
  5876. struct pci_dev *pdev = to_pci_dev(device);
  5877. struct net_device *dev = pci_get_drvdata(pdev);
  5878. struct rtl8169_private *tp = netdev_priv(dev);
  5879. if (!tp->TxDescArray)
  5880. return 0;
  5881. rtl_lock_work(tp);
  5882. __rtl8169_set_wol(tp, WAKE_ANY);
  5883. rtl_unlock_work(tp);
  5884. rtl8169_net_suspend(dev);
  5885. /* Update counters before going runtime suspend */
  5886. rtl8169_rx_missed(dev);
  5887. rtl8169_update_counters(tp);
  5888. return 0;
  5889. }
  5890. static int rtl8169_runtime_resume(struct device *device)
  5891. {
  5892. struct pci_dev *pdev = to_pci_dev(device);
  5893. struct net_device *dev = pci_get_drvdata(pdev);
  5894. struct rtl8169_private *tp = netdev_priv(dev);
  5895. rtl_rar_set(tp, dev->dev_addr);
  5896. if (!tp->TxDescArray)
  5897. return 0;
  5898. rtl_lock_work(tp);
  5899. __rtl8169_set_wol(tp, tp->saved_wolopts);
  5900. rtl_unlock_work(tp);
  5901. __rtl8169_resume(dev);
  5902. return 0;
  5903. }
  5904. static int rtl8169_runtime_idle(struct device *device)
  5905. {
  5906. struct pci_dev *pdev = to_pci_dev(device);
  5907. struct net_device *dev = pci_get_drvdata(pdev);
  5908. if (!netif_running(dev) || !netif_carrier_ok(dev))
  5909. pm_schedule_suspend(device, 10000);
  5910. return -EBUSY;
  5911. }
  5912. static const struct dev_pm_ops rtl8169_pm_ops = {
  5913. .suspend = rtl8169_suspend,
  5914. .resume = rtl8169_resume,
  5915. .freeze = rtl8169_suspend,
  5916. .thaw = rtl8169_resume,
  5917. .poweroff = rtl8169_suspend,
  5918. .restore = rtl8169_resume,
  5919. .runtime_suspend = rtl8169_runtime_suspend,
  5920. .runtime_resume = rtl8169_runtime_resume,
  5921. .runtime_idle = rtl8169_runtime_idle,
  5922. };
  5923. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  5924. #else /* !CONFIG_PM */
  5925. #define RTL8169_PM_OPS NULL
  5926. #endif /* !CONFIG_PM */
  5927. static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
  5928. {
  5929. /* WoL fails with 8168b when the receiver is disabled. */
  5930. switch (tp->mac_version) {
  5931. case RTL_GIGA_MAC_VER_11:
  5932. case RTL_GIGA_MAC_VER_12:
  5933. case RTL_GIGA_MAC_VER_17:
  5934. pci_clear_master(tp->pci_dev);
  5935. RTL_W8(tp, ChipCmd, CmdRxEnb);
  5936. /* PCI commit */
  5937. RTL_R8(tp, ChipCmd);
  5938. break;
  5939. default:
  5940. break;
  5941. }
  5942. }
  5943. static void rtl_shutdown(struct pci_dev *pdev)
  5944. {
  5945. struct net_device *dev = pci_get_drvdata(pdev);
  5946. struct rtl8169_private *tp = netdev_priv(dev);
  5947. rtl8169_net_suspend(dev);
  5948. /* Restore original MAC address */
  5949. rtl_rar_set(tp, dev->perm_addr);
  5950. rtl8169_hw_reset(tp);
  5951. if (system_state == SYSTEM_POWER_OFF) {
  5952. if (tp->saved_wolopts) {
  5953. rtl_wol_suspend_quirk(tp);
  5954. rtl_wol_shutdown_quirk(tp);
  5955. }
  5956. pci_wake_from_d3(pdev, true);
  5957. pci_set_power_state(pdev, PCI_D3hot);
  5958. }
  5959. }
  5960. static void rtl_remove_one(struct pci_dev *pdev)
  5961. {
  5962. struct net_device *dev = pci_get_drvdata(pdev);
  5963. struct rtl8169_private *tp = netdev_priv(dev);
  5964. if (r8168_check_dash(tp))
  5965. rtl8168_driver_stop(tp);
  5966. netif_napi_del(&tp->napi);
  5967. unregister_netdev(dev);
  5968. mdiobus_unregister(tp->mii_bus);
  5969. rtl_release_firmware(tp);
  5970. if (pci_dev_run_wake(pdev))
  5971. pm_runtime_get_noresume(&pdev->dev);
  5972. /* restore original MAC address */
  5973. rtl_rar_set(tp, dev->perm_addr);
  5974. }
  5975. static const struct net_device_ops rtl_netdev_ops = {
  5976. .ndo_open = rtl_open,
  5977. .ndo_stop = rtl8169_close,
  5978. .ndo_get_stats64 = rtl8169_get_stats64,
  5979. .ndo_start_xmit = rtl8169_start_xmit,
  5980. .ndo_tx_timeout = rtl8169_tx_timeout,
  5981. .ndo_validate_addr = eth_validate_addr,
  5982. .ndo_change_mtu = rtl8169_change_mtu,
  5983. .ndo_fix_features = rtl8169_fix_features,
  5984. .ndo_set_features = rtl8169_set_features,
  5985. .ndo_set_mac_address = rtl_set_mac_address,
  5986. .ndo_do_ioctl = rtl8169_ioctl,
  5987. .ndo_set_rx_mode = rtl_set_rx_mode,
  5988. #ifdef CONFIG_NET_POLL_CONTROLLER
  5989. .ndo_poll_controller = rtl8169_netpoll,
  5990. #endif
  5991. };
  5992. static const struct rtl_cfg_info {
  5993. void (*hw_start)(struct rtl8169_private *tp);
  5994. u16 event_slow;
  5995. unsigned int has_gmii:1;
  5996. const struct rtl_coalesce_info *coalesce_info;
  5997. u8 default_ver;
  5998. } rtl_cfg_infos [] = {
  5999. [RTL_CFG_0] = {
  6000. .hw_start = rtl_hw_start_8169,
  6001. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
  6002. .has_gmii = 1,
  6003. .coalesce_info = rtl_coalesce_info_8169,
  6004. .default_ver = RTL_GIGA_MAC_VER_01,
  6005. },
  6006. [RTL_CFG_1] = {
  6007. .hw_start = rtl_hw_start_8168,
  6008. .event_slow = SYSErr | LinkChg | RxOverflow,
  6009. .has_gmii = 1,
  6010. .coalesce_info = rtl_coalesce_info_8168_8136,
  6011. .default_ver = RTL_GIGA_MAC_VER_11,
  6012. },
  6013. [RTL_CFG_2] = {
  6014. .hw_start = rtl_hw_start_8101,
  6015. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
  6016. PCSTimeout,
  6017. .coalesce_info = rtl_coalesce_info_8168_8136,
  6018. .default_ver = RTL_GIGA_MAC_VER_13,
  6019. }
  6020. };
  6021. static int rtl_alloc_irq(struct rtl8169_private *tp)
  6022. {
  6023. unsigned int flags;
  6024. switch (tp->mac_version) {
  6025. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  6026. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  6027. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
  6028. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  6029. /* fall through */
  6030. case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17:
  6031. flags = PCI_IRQ_LEGACY;
  6032. break;
  6033. default:
  6034. flags = PCI_IRQ_ALL_TYPES;
  6035. break;
  6036. }
  6037. return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
  6038. }
  6039. DECLARE_RTL_COND(rtl_link_list_ready_cond)
  6040. {
  6041. return RTL_R8(tp, MCU) & LINK_LIST_RDY;
  6042. }
  6043. DECLARE_RTL_COND(rtl_rxtx_empty_cond)
  6044. {
  6045. return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
  6046. }
  6047. static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
  6048. {
  6049. struct rtl8169_private *tp = mii_bus->priv;
  6050. if (phyaddr > 0)
  6051. return -ENODEV;
  6052. return rtl_readphy(tp, phyreg);
  6053. }
  6054. static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
  6055. int phyreg, u16 val)
  6056. {
  6057. struct rtl8169_private *tp = mii_bus->priv;
  6058. if (phyaddr > 0)
  6059. return -ENODEV;
  6060. rtl_writephy(tp, phyreg, val);
  6061. return 0;
  6062. }
  6063. static int r8169_mdio_register(struct rtl8169_private *tp)
  6064. {
  6065. struct pci_dev *pdev = tp->pci_dev;
  6066. struct phy_device *phydev;
  6067. struct mii_bus *new_bus;
  6068. int ret;
  6069. new_bus = devm_mdiobus_alloc(&pdev->dev);
  6070. if (!new_bus)
  6071. return -ENOMEM;
  6072. new_bus->name = "r8169";
  6073. new_bus->priv = tp;
  6074. new_bus->parent = &pdev->dev;
  6075. new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
  6076. snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
  6077. PCI_DEVID(pdev->bus->number, pdev->devfn));
  6078. new_bus->read = r8169_mdio_read_reg;
  6079. new_bus->write = r8169_mdio_write_reg;
  6080. ret = mdiobus_register(new_bus);
  6081. if (ret)
  6082. return ret;
  6083. phydev = mdiobus_get_phy(new_bus, 0);
  6084. if (!phydev) {
  6085. mdiobus_unregister(new_bus);
  6086. return -ENODEV;
  6087. }
  6088. /* PHY will be woken up in rtl_open() */
  6089. phy_suspend(phydev);
  6090. tp->mii_bus = new_bus;
  6091. return 0;
  6092. }
  6093. static void rtl_hw_init_8168g(struct rtl8169_private *tp)
  6094. {
  6095. u32 data;
  6096. tp->ocp_base = OCP_STD_PHY_BASE;
  6097. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
  6098. if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
  6099. return;
  6100. if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
  6101. return;
  6102. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
  6103. msleep(1);
  6104. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  6105. data = r8168_mac_ocp_read(tp, 0xe8de);
  6106. data &= ~(1 << 14);
  6107. r8168_mac_ocp_write(tp, 0xe8de, data);
  6108. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6109. return;
  6110. data = r8168_mac_ocp_read(tp, 0xe8de);
  6111. data |= (1 << 15);
  6112. r8168_mac_ocp_write(tp, 0xe8de, data);
  6113. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6114. return;
  6115. }
  6116. static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
  6117. {
  6118. rtl8168ep_stop_cmac(tp);
  6119. rtl_hw_init_8168g(tp);
  6120. }
  6121. static void rtl_hw_initialize(struct rtl8169_private *tp)
  6122. {
  6123. switch (tp->mac_version) {
  6124. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
  6125. rtl_hw_init_8168g(tp);
  6126. break;
  6127. case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
  6128. rtl_hw_init_8168ep(tp);
  6129. break;
  6130. default:
  6131. break;
  6132. }
  6133. }
  6134. /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
  6135. static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
  6136. {
  6137. switch (tp->mac_version) {
  6138. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  6139. case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
  6140. return false;
  6141. default:
  6142. return true;
  6143. }
  6144. }
  6145. static int rtl_jumbo_max(struct rtl8169_private *tp)
  6146. {
  6147. /* Non-GBit versions don't support jumbo frames */
  6148. if (!tp->supports_gmii)
  6149. return JUMBO_1K;
  6150. switch (tp->mac_version) {
  6151. /* RTL8169 */
  6152. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  6153. return JUMBO_7K;
  6154. /* RTL8168b */
  6155. case RTL_GIGA_MAC_VER_11:
  6156. case RTL_GIGA_MAC_VER_12:
  6157. case RTL_GIGA_MAC_VER_17:
  6158. return JUMBO_4K;
  6159. /* RTL8168c */
  6160. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
  6161. return JUMBO_6K;
  6162. default:
  6163. return JUMBO_9K;
  6164. }
  6165. }
  6166. static void rtl_disable_clk(void *data)
  6167. {
  6168. clk_disable_unprepare(data);
  6169. }
  6170. static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  6171. {
  6172. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  6173. struct rtl8169_private *tp;
  6174. struct net_device *dev;
  6175. int chipset, region, i;
  6176. int jumbo_max, rc;
  6177. dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
  6178. if (!dev)
  6179. return -ENOMEM;
  6180. SET_NETDEV_DEV(dev, &pdev->dev);
  6181. dev->netdev_ops = &rtl_netdev_ops;
  6182. tp = netdev_priv(dev);
  6183. tp->dev = dev;
  6184. tp->pci_dev = pdev;
  6185. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  6186. tp->supports_gmii = cfg->has_gmii;
  6187. /* Get the *optional* external "ether_clk" used on some boards */
  6188. tp->clk = devm_clk_get(&pdev->dev, "ether_clk");
  6189. if (IS_ERR(tp->clk)) {
  6190. rc = PTR_ERR(tp->clk);
  6191. if (rc == -ENOENT) {
  6192. /* clk-core allows NULL (for suspend / resume) */
  6193. tp->clk = NULL;
  6194. } else if (rc == -EPROBE_DEFER) {
  6195. return rc;
  6196. } else {
  6197. dev_err(&pdev->dev, "failed to get clk: %d\n", rc);
  6198. return rc;
  6199. }
  6200. } else {
  6201. rc = clk_prepare_enable(tp->clk);
  6202. if (rc) {
  6203. dev_err(&pdev->dev, "failed to enable clk: %d\n", rc);
  6204. return rc;
  6205. }
  6206. rc = devm_add_action_or_reset(&pdev->dev, rtl_disable_clk,
  6207. tp->clk);
  6208. if (rc)
  6209. return rc;
  6210. }
  6211. /* Disable ASPM completely as that cause random device stop working
  6212. * problems as well as full system hangs for some PCIe devices users.
  6213. */
  6214. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  6215. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  6216. rc = pcim_enable_device(pdev);
  6217. if (rc < 0) {
  6218. dev_err(&pdev->dev, "enable failure\n");
  6219. return rc;
  6220. }
  6221. if (pcim_set_mwi(pdev) < 0)
  6222. dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
  6223. /* use first MMIO region */
  6224. region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
  6225. if (region < 0) {
  6226. dev_err(&pdev->dev, "no MMIO resource found\n");
  6227. return -ENODEV;
  6228. }
  6229. /* check for weird/broken PCI region reporting */
  6230. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  6231. dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
  6232. return -ENODEV;
  6233. }
  6234. rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
  6235. if (rc < 0) {
  6236. dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
  6237. return rc;
  6238. }
  6239. tp->mmio_addr = pcim_iomap_table(pdev)[region];
  6240. if (!pci_is_pcie(pdev))
  6241. dev_info(&pdev->dev, "not PCI Express\n");
  6242. /* Identify chip attached to board */
  6243. rtl8169_get_mac_version(tp, cfg->default_ver);
  6244. if (rtl_tbi_enabled(tp)) {
  6245. dev_err(&pdev->dev, "TBI fiber mode not supported\n");
  6246. return -ENODEV;
  6247. }
  6248. tp->cp_cmd = RTL_R16(tp, CPlusCmd);
  6249. if ((sizeof(dma_addr_t) > 4) &&
  6250. (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
  6251. tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
  6252. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  6253. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  6254. /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
  6255. if (!pci_is_pcie(pdev))
  6256. tp->cp_cmd |= PCIDAC;
  6257. dev->features |= NETIF_F_HIGHDMA;
  6258. } else {
  6259. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  6260. if (rc < 0) {
  6261. dev_err(&pdev->dev, "DMA configuration failed\n");
  6262. return rc;
  6263. }
  6264. }
  6265. rtl_init_rxcfg(tp);
  6266. rtl_irq_disable(tp);
  6267. rtl_hw_initialize(tp);
  6268. rtl_hw_reset(tp);
  6269. rtl_ack_events(tp, 0xffff);
  6270. pci_set_master(pdev);
  6271. rtl_init_mdio_ops(tp);
  6272. rtl_init_jumbo_ops(tp);
  6273. rtl8169_print_mac_version(tp);
  6274. chipset = tp->mac_version;
  6275. rc = rtl_alloc_irq(tp);
  6276. if (rc < 0) {
  6277. dev_err(&pdev->dev, "Can't allocate interrupt\n");
  6278. return rc;
  6279. }
  6280. tp->saved_wolopts = __rtl8169_get_wol(tp);
  6281. mutex_init(&tp->wk.mutex);
  6282. u64_stats_init(&tp->rx_stats.syncp);
  6283. u64_stats_init(&tp->tx_stats.syncp);
  6284. /* Get MAC address */
  6285. switch (tp->mac_version) {
  6286. u8 mac_addr[ETH_ALEN] __aligned(4);
  6287. case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
  6288. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  6289. *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
  6290. *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
  6291. if (is_valid_ether_addr(mac_addr))
  6292. rtl_rar_set(tp, mac_addr);
  6293. break;
  6294. default:
  6295. break;
  6296. }
  6297. for (i = 0; i < ETH_ALEN; i++)
  6298. dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
  6299. dev->ethtool_ops = &rtl8169_ethtool_ops;
  6300. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  6301. netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
  6302. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  6303. * properly for all devices */
  6304. dev->features |= NETIF_F_RXCSUM |
  6305. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  6306. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6307. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
  6308. NETIF_F_HW_VLAN_CTAG_RX;
  6309. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6310. NETIF_F_HIGHDMA;
  6311. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  6312. tp->cp_cmd |= RxChkSum | RxVlan;
  6313. /*
  6314. * Pretend we are using VLANs; This bypasses a nasty bug where
  6315. * Interrupts stop flowing on high load on 8110SCd controllers.
  6316. */
  6317. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  6318. /* Disallow toggling */
  6319. dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  6320. if (rtl_chip_supports_csum_v2(tp)) {
  6321. tp->tso_csum = rtl8169_tso_csum_v2;
  6322. dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  6323. } else {
  6324. tp->tso_csum = rtl8169_tso_csum_v1;
  6325. }
  6326. dev->hw_features |= NETIF_F_RXALL;
  6327. dev->hw_features |= NETIF_F_RXFCS;
  6328. /* MTU range: 60 - hw-specific max */
  6329. dev->min_mtu = ETH_ZLEN;
  6330. jumbo_max = rtl_jumbo_max(tp);
  6331. dev->max_mtu = jumbo_max;
  6332. tp->hw_start = cfg->hw_start;
  6333. tp->event_slow = cfg->event_slow;
  6334. tp->coalesce_info = cfg->coalesce_info;
  6335. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  6336. tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
  6337. &tp->counters_phys_addr,
  6338. GFP_KERNEL);
  6339. if (!tp->counters)
  6340. return -ENOMEM;
  6341. pci_set_drvdata(pdev, dev);
  6342. rc = r8169_mdio_register(tp);
  6343. if (rc)
  6344. return rc;
  6345. /* chip gets powered up in rtl_open() */
  6346. rtl_pll_power_down(tp);
  6347. rc = register_netdev(dev);
  6348. if (rc)
  6349. goto err_mdio_unregister;
  6350. netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
  6351. rtl_chip_infos[chipset].name, dev->dev_addr,
  6352. (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
  6353. pci_irq_vector(pdev, 0));
  6354. if (jumbo_max > JUMBO_1K)
  6355. netif_info(tp, probe, dev,
  6356. "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
  6357. jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
  6358. "ok" : "ko");
  6359. if (r8168_check_dash(tp))
  6360. rtl8168_driver_start(tp);
  6361. if (pci_dev_run_wake(pdev))
  6362. pm_runtime_put_sync(&pdev->dev);
  6363. return 0;
  6364. err_mdio_unregister:
  6365. mdiobus_unregister(tp->mii_bus);
  6366. return rc;
  6367. }
  6368. static struct pci_driver rtl8169_pci_driver = {
  6369. .name = MODULENAME,
  6370. .id_table = rtl8169_pci_tbl,
  6371. .probe = rtl_init_one,
  6372. .remove = rtl_remove_one,
  6373. .shutdown = rtl_shutdown,
  6374. .driver.pm = RTL8169_PM_OPS,
  6375. };
  6376. module_pci_driver(rtl8169_pci_driver);