r8169.c 205 KB

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