r8169.c 117 KB

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