r8169.c 178 KB

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