r8169.c 204 KB

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