r8169.c 168 KB

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