r8169.c 168 KB

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