r8169.c 168 KB

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