r8169.c 120 KB

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