r8169.c 128 KB

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