r8169.c 171 KB

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