r8169.c 116 KB

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