r8169.c 130 KB

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