r8169.c 159 KB

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