r8169.c 159 KB

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