r8169.c 174 KB

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