r8169.c 131 KB

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