r8169.c 168 KB

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