r8169.c 150 KB

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