r8169.c 151 KB

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