r8169.c 123 KB

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