r8169.c 137 KB

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