r8169.c 160 KB

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