r8169.c 120 KB

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