r8169.c 149 KB

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