r8169.c 129 KB

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