r8169.c 209 KB

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