r8169.c 137 KB

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