r8169_main.c 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * r8169.c: RealTek 8169/8168/8101 ethernet driver.
  4. *
  5. * Copyright (c) 2002 ShuChen <[email protected]>
  6. * Copyright (c) 2003 - 2007 Francois Romieu <[email protected]>
  7. * Copyright (c) a lot of people too. Please respect their work.
  8. *
  9. * See MAINTAINERS file for support contact information.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/phy.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/in.h>
  21. #include <linux/io.h>
  22. #include <linux/ip.h>
  23. #include <linux/tcp.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/bitfield.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/ipv6.h>
  30. #include <asm/unaligned.h>
  31. #include <net/ip6_checksum.h>
  32. #include "r8169.h"
  33. #include "r8169_firmware.h"
  34. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  35. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  36. #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
  37. #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
  38. #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
  39. #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
  40. #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
  41. #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
  42. #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
  43. #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
  44. #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
  45. #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
  46. #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
  47. #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
  48. #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
  49. #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
  50. #define FIRMWARE_8168FP_3 "rtl_nic/rtl8168fp-3.fw"
  51. #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
  52. #define FIRMWARE_8125A_3 "rtl_nic/rtl8125a-3.fw"
  53. #define FIRMWARE_8125B_2 "rtl_nic/rtl8125b-2.fw"
  54. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  55. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  56. #define MC_FILTER_LIMIT 32
  57. #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
  58. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  59. #define R8169_REGS_SIZE 256
  60. #define R8169_RX_BUF_SIZE (SZ_16K - 1)
  61. #define NUM_TX_DESC 256 /* Number of Tx descriptor registers */
  62. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  63. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  64. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  65. #define OCP_STD_PHY_BASE 0xa400
  66. #define RTL_CFG_NO_GBIT 1
  67. /* write/read MMIO register */
  68. #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
  69. #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
  70. #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
  71. #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
  72. #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
  73. #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
  74. #define JUMBO_4K (4 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
  75. #define JUMBO_6K (6 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
  76. #define JUMBO_7K (7 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
  77. #define JUMBO_9K (9 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
  78. static const struct {
  79. const char *name;
  80. const char *fw_name;
  81. } rtl_chip_infos[] = {
  82. /* PCI devices. */
  83. [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
  84. [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
  85. [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
  86. [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
  87. [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
  88. /* PCI-E devices. */
  89. [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
  90. [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
  91. [RTL_GIGA_MAC_VER_09] = {"RTL8102e/RTL8103e" },
  92. [RTL_GIGA_MAC_VER_10] = {"RTL8101e/RTL8100e" },
  93. [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
  94. [RTL_GIGA_MAC_VER_14] = {"RTL8401" },
  95. [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
  96. [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
  97. [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
  98. [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
  99. [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
  100. [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
  101. [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
  102. [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
  103. [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
  104. [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
  105. [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
  106. [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
  107. [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
  108. [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
  109. [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
  110. [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
  111. [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
  112. [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
  113. [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
  114. [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
  115. [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
  116. [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
  117. [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
  118. [RTL_GIGA_MAC_VER_42] = {"RTL8168gu/8111gu", FIRMWARE_8168G_3},
  119. [RTL_GIGA_MAC_VER_43] = {"RTL8106eus", FIRMWARE_8106E_2},
  120. [RTL_GIGA_MAC_VER_44] = {"RTL8411b", FIRMWARE_8411_2 },
  121. [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
  122. [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
  123. [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
  124. [RTL_GIGA_MAC_VER_52] = {"RTL8168fp/RTL8117", FIRMWARE_8168FP_3},
  125. [RTL_GIGA_MAC_VER_53] = {"RTL8168fp/RTL8117", },
  126. [RTL_GIGA_MAC_VER_61] = {"RTL8125A", FIRMWARE_8125A_3},
  127. /* reserve 62 for CFG_METHOD_4 in the vendor driver */
  128. [RTL_GIGA_MAC_VER_63] = {"RTL8125B", FIRMWARE_8125B_2},
  129. };
  130. static const struct pci_device_id rtl8169_pci_tbl[] = {
  131. { PCI_VDEVICE(REALTEK, 0x2502) },
  132. { PCI_VDEVICE(REALTEK, 0x2600) },
  133. { PCI_VDEVICE(REALTEK, 0x8129) },
  134. { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT },
  135. { PCI_VDEVICE(REALTEK, 0x8161) },
  136. { PCI_VDEVICE(REALTEK, 0x8162) },
  137. { PCI_VDEVICE(REALTEK, 0x8167) },
  138. { PCI_VDEVICE(REALTEK, 0x8168) },
  139. { PCI_VDEVICE(NCUBE, 0x8168) },
  140. { PCI_VDEVICE(REALTEK, 0x8169) },
  141. { PCI_VENDOR_ID_DLINK, 0x4300,
  142. PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
  143. { PCI_VDEVICE(DLINK, 0x4300) },
  144. { PCI_VDEVICE(DLINK, 0x4302) },
  145. { PCI_VDEVICE(AT, 0xc107) },
  146. { PCI_VDEVICE(USR, 0x0116) },
  147. { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 },
  148. { 0x0001, 0x8168, PCI_ANY_ID, 0x2410 },
  149. { PCI_VDEVICE(REALTEK, 0x8125) },
  150. { PCI_VDEVICE(REALTEK, 0x3000) },
  151. {}
  152. };
  153. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  154. enum rtl_registers {
  155. MAC0 = 0, /* Ethernet hardware address. */
  156. MAC4 = 4,
  157. MAR0 = 8, /* Multicast filter. */
  158. CounterAddrLow = 0x10,
  159. CounterAddrHigh = 0x14,
  160. TxDescStartAddrLow = 0x20,
  161. TxDescStartAddrHigh = 0x24,
  162. TxHDescStartAddrLow = 0x28,
  163. TxHDescStartAddrHigh = 0x2c,
  164. FLASH = 0x30,
  165. ERSR = 0x36,
  166. ChipCmd = 0x37,
  167. TxPoll = 0x38,
  168. IntrMask = 0x3c,
  169. IntrStatus = 0x3e,
  170. TxConfig = 0x40,
  171. #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
  172. #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
  173. RxConfig = 0x44,
  174. #define RX128_INT_EN (1 << 15) /* 8111c and later */
  175. #define RX_MULTI_EN (1 << 14) /* 8111c only */
  176. #define RXCFG_FIFO_SHIFT 13
  177. /* No threshold before first PCI xfer */
  178. #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
  179. #define RX_EARLY_OFF (1 << 11)
  180. #define RX_PAUSE_SLOT_ON (1 << 11) /* 8125b and later */
  181. #define RXCFG_DMA_SHIFT 8
  182. /* Unlimited maximum PCI burst. */
  183. #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
  184. Cfg9346 = 0x50,
  185. Config0 = 0x51,
  186. Config1 = 0x52,
  187. Config2 = 0x53,
  188. #define PME_SIGNAL (1 << 5) /* 8168c and later */
  189. Config3 = 0x54,
  190. Config4 = 0x55,
  191. Config5 = 0x56,
  192. PHYAR = 0x60,
  193. PHYstatus = 0x6c,
  194. RxMaxSize = 0xda,
  195. CPlusCmd = 0xe0,
  196. IntrMitigate = 0xe2,
  197. #define RTL_COALESCE_TX_USECS GENMASK(15, 12)
  198. #define RTL_COALESCE_TX_FRAMES GENMASK(11, 8)
  199. #define RTL_COALESCE_RX_USECS GENMASK(7, 4)
  200. #define RTL_COALESCE_RX_FRAMES GENMASK(3, 0)
  201. #define RTL_COALESCE_T_MAX 0x0fU
  202. #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_T_MAX * 4)
  203. RxDescAddrLow = 0xe4,
  204. RxDescAddrHigh = 0xe8,
  205. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  206. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  207. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  208. #define TxPacketMax (8064 >> 7)
  209. #define EarlySize 0x27
  210. FuncEvent = 0xf0,
  211. FuncEventMask = 0xf4,
  212. FuncPresetState = 0xf8,
  213. IBCR0 = 0xf8,
  214. IBCR2 = 0xf9,
  215. IBIMR0 = 0xfa,
  216. IBISR0 = 0xfb,
  217. FuncForceEvent = 0xfc,
  218. };
  219. enum rtl8168_8101_registers {
  220. CSIDR = 0x64,
  221. CSIAR = 0x68,
  222. #define CSIAR_FLAG 0x80000000
  223. #define CSIAR_WRITE_CMD 0x80000000
  224. #define CSIAR_BYTE_ENABLE 0x0000f000
  225. #define CSIAR_ADDR_MASK 0x00000fff
  226. PMCH = 0x6f,
  227. #define D3COLD_NO_PLL_DOWN BIT(7)
  228. #define D3HOT_NO_PLL_DOWN BIT(6)
  229. #define D3_NO_PLL_DOWN (BIT(7) | BIT(6))
  230. EPHYAR = 0x80,
  231. #define EPHYAR_FLAG 0x80000000
  232. #define EPHYAR_WRITE_CMD 0x80000000
  233. #define EPHYAR_REG_MASK 0x1f
  234. #define EPHYAR_REG_SHIFT 16
  235. #define EPHYAR_DATA_MASK 0xffff
  236. DLLPR = 0xd0,
  237. #define PFM_EN (1 << 6)
  238. #define TX_10M_PS_EN (1 << 7)
  239. DBG_REG = 0xd1,
  240. #define FIX_NAK_1 (1 << 4)
  241. #define FIX_NAK_2 (1 << 3)
  242. TWSI = 0xd2,
  243. MCU = 0xd3,
  244. #define NOW_IS_OOB (1 << 7)
  245. #define TX_EMPTY (1 << 5)
  246. #define RX_EMPTY (1 << 4)
  247. #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
  248. #define EN_NDP (1 << 3)
  249. #define EN_OOB_RESET (1 << 2)
  250. #define LINK_LIST_RDY (1 << 1)
  251. EFUSEAR = 0xdc,
  252. #define EFUSEAR_FLAG 0x80000000
  253. #define EFUSEAR_WRITE_CMD 0x80000000
  254. #define EFUSEAR_READ_CMD 0x00000000
  255. #define EFUSEAR_REG_MASK 0x03ff
  256. #define EFUSEAR_REG_SHIFT 8
  257. #define EFUSEAR_DATA_MASK 0xff
  258. MISC_1 = 0xf2,
  259. #define PFM_D3COLD_EN (1 << 6)
  260. };
  261. enum rtl8168_registers {
  262. LED_FREQ = 0x1a,
  263. EEE_LED = 0x1b,
  264. ERIDR = 0x70,
  265. ERIAR = 0x74,
  266. #define ERIAR_FLAG 0x80000000
  267. #define ERIAR_WRITE_CMD 0x80000000
  268. #define ERIAR_READ_CMD 0x00000000
  269. #define ERIAR_ADDR_BYTE_ALIGN 4
  270. #define ERIAR_TYPE_SHIFT 16
  271. #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
  272. #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
  273. #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
  274. #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
  275. #define ERIAR_MASK_SHIFT 12
  276. #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
  277. #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
  278. #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
  279. #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
  280. #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
  281. EPHY_RXER_NUM = 0x7c,
  282. OCPDR = 0xb0, /* OCP GPHY access */
  283. #define OCPDR_WRITE_CMD 0x80000000
  284. #define OCPDR_READ_CMD 0x00000000
  285. #define OCPDR_REG_MASK 0x7f
  286. #define OCPDR_GPHY_REG_SHIFT 16
  287. #define OCPDR_DATA_MASK 0xffff
  288. OCPAR = 0xb4,
  289. #define OCPAR_FLAG 0x80000000
  290. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  291. #define OCPAR_GPHY_READ_CMD 0x0000f060
  292. GPHY_OCP = 0xb8,
  293. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  294. MISC = 0xf0, /* 8168e only. */
  295. #define TXPLA_RST (1 << 29)
  296. #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
  297. #define PWM_EN (1 << 22)
  298. #define RXDV_GATED_EN (1 << 19)
  299. #define EARLY_TALLY_EN (1 << 16)
  300. };
  301. enum rtl8125_registers {
  302. IntrMask_8125 = 0x38,
  303. IntrStatus_8125 = 0x3c,
  304. TxPoll_8125 = 0x90,
  305. MAC0_BKP = 0x19e0,
  306. EEE_TXIDLE_TIMER_8125 = 0x6048,
  307. };
  308. #define RX_VLAN_INNER_8125 BIT(22)
  309. #define RX_VLAN_OUTER_8125 BIT(23)
  310. #define RX_VLAN_8125 (RX_VLAN_INNER_8125 | RX_VLAN_OUTER_8125)
  311. #define RX_FETCH_DFLT_8125 (8 << 27)
  312. enum rtl_register_content {
  313. /* InterruptStatusBits */
  314. SYSErr = 0x8000,
  315. PCSTimeout = 0x4000,
  316. SWInt = 0x0100,
  317. TxDescUnavail = 0x0080,
  318. RxFIFOOver = 0x0040,
  319. LinkChg = 0x0020,
  320. RxOverflow = 0x0010,
  321. TxErr = 0x0008,
  322. TxOK = 0x0004,
  323. RxErr = 0x0002,
  324. RxOK = 0x0001,
  325. /* RxStatusDesc */
  326. RxRWT = (1 << 22),
  327. RxRES = (1 << 21),
  328. RxRUNT = (1 << 20),
  329. RxCRC = (1 << 19),
  330. /* ChipCmdBits */
  331. StopReq = 0x80,
  332. CmdReset = 0x10,
  333. CmdRxEnb = 0x08,
  334. CmdTxEnb = 0x04,
  335. RxBufEmpty = 0x01,
  336. /* TXPoll register p.5 */
  337. HPQ = 0x80, /* Poll cmd on the high prio queue */
  338. NPQ = 0x40, /* Poll cmd on the low prio queue */
  339. FSWInt = 0x01, /* Forced software interrupt */
  340. /* Cfg9346Bits */
  341. Cfg9346_Lock = 0x00,
  342. Cfg9346_Unlock = 0xc0,
  343. /* rx_mode_bits */
  344. AcceptErr = 0x20,
  345. AcceptRunt = 0x10,
  346. #define RX_CONFIG_ACCEPT_ERR_MASK 0x30
  347. AcceptBroadcast = 0x08,
  348. AcceptMulticast = 0x04,
  349. AcceptMyPhys = 0x02,
  350. AcceptAllPhys = 0x01,
  351. #define RX_CONFIG_ACCEPT_OK_MASK 0x0f
  352. #define RX_CONFIG_ACCEPT_MASK 0x3f
  353. /* TxConfigBits */
  354. TxInterFrameGapShift = 24,
  355. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  356. /* Config1 register p.24 */
  357. LEDS1 = (1 << 7),
  358. LEDS0 = (1 << 6),
  359. Speed_down = (1 << 4),
  360. MEMMAP = (1 << 3),
  361. IOMAP = (1 << 2),
  362. VPD = (1 << 1),
  363. PMEnable = (1 << 0), /* Power Management Enable */
  364. /* Config2 register p. 25 */
  365. ClkReqEn = (1 << 7), /* Clock Request Enable */
  366. MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
  367. PCI_Clock_66MHz = 0x01,
  368. PCI_Clock_33MHz = 0x00,
  369. /* Config3 register p.25 */
  370. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  371. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  372. Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
  373. Rdy_to_L23 = (1 << 1), /* L23 Enable */
  374. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  375. /* Config4 register */
  376. Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
  377. /* Config5 register p.27 */
  378. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  379. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  380. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  381. Spi_en = (1 << 3),
  382. LanWake = (1 << 1), /* LanWake enable/disable */
  383. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  384. ASPM_en = (1 << 0), /* ASPM enable */
  385. /* CPlusCmd p.31 */
  386. EnableBist = (1 << 15), // 8168 8101
  387. Mac_dbgo_oe = (1 << 14), // 8168 8101
  388. EnAnaPLL = (1 << 14), // 8169
  389. Normal_mode = (1 << 13), // unused
  390. Force_half_dup = (1 << 12), // 8168 8101
  391. Force_rxflow_en = (1 << 11), // 8168 8101
  392. Force_txflow_en = (1 << 10), // 8168 8101
  393. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  394. ASF = (1 << 8), // 8168 8101
  395. PktCntrDisable = (1 << 7), // 8168 8101
  396. Mac_dbgo_sel = 0x001c, // 8168
  397. RxVlan = (1 << 6),
  398. RxChkSum = (1 << 5),
  399. PCIDAC = (1 << 4),
  400. PCIMulRW = (1 << 3),
  401. #define INTT_MASK GENMASK(1, 0)
  402. #define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
  403. /* rtl8169_PHYstatus */
  404. TBI_Enable = 0x80,
  405. TxFlowCtrl = 0x40,
  406. RxFlowCtrl = 0x20,
  407. _1000bpsF = 0x10,
  408. _100bps = 0x08,
  409. _10bps = 0x04,
  410. LinkStatus = 0x02,
  411. FullDup = 0x01,
  412. /* ResetCounterCommand */
  413. CounterReset = 0x1,
  414. /* DumpCounterCommand */
  415. CounterDump = 0x8,
  416. /* magic enable v2 */
  417. MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
  418. };
  419. enum rtl_desc_bit {
  420. /* First doubleword. */
  421. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  422. RingEnd = (1 << 30), /* End of descriptor ring */
  423. FirstFrag = (1 << 29), /* First segment of a packet */
  424. LastFrag = (1 << 28), /* Final segment of a packet */
  425. };
  426. /* Generic case. */
  427. enum rtl_tx_desc_bit {
  428. /* First doubleword. */
  429. TD_LSO = (1 << 27), /* Large Send Offload */
  430. #define TD_MSS_MAX 0x07ffu /* MSS value */
  431. /* Second doubleword. */
  432. TxVlanTag = (1 << 17), /* Add VLAN tag */
  433. };
  434. /* 8169, 8168b and 810x except 8102e. */
  435. enum rtl_tx_desc_bit_0 {
  436. /* First doubleword. */
  437. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  438. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  439. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  440. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  441. };
  442. /* 8102e, 8168c and beyond. */
  443. enum rtl_tx_desc_bit_1 {
  444. /* First doubleword. */
  445. TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
  446. TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
  447. #define GTTCPHO_SHIFT 18
  448. #define GTTCPHO_MAX 0x7f
  449. /* Second doubleword. */
  450. #define TCPHO_SHIFT 18
  451. #define TCPHO_MAX 0x3ff
  452. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  453. TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
  454. TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
  455. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  456. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  457. };
  458. enum rtl_rx_desc_bit {
  459. /* Rx private */
  460. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  461. PID0 = (1 << 17), /* Protocol ID bit 0/2 */
  462. #define RxProtoUDP (PID1)
  463. #define RxProtoTCP (PID0)
  464. #define RxProtoIP (PID1 | PID0)
  465. #define RxProtoMask RxProtoIP
  466. IPFail = (1 << 16), /* IP checksum failed */
  467. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  468. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  469. #define RxCSFailMask (IPFail | UDPFail | TCPFail)
  470. RxVlanTag = (1 << 16), /* VLAN tag available */
  471. };
  472. #define RTL_GSO_MAX_SIZE_V1 32000
  473. #define RTL_GSO_MAX_SEGS_V1 24
  474. #define RTL_GSO_MAX_SIZE_V2 64000
  475. #define RTL_GSO_MAX_SEGS_V2 64
  476. struct TxDesc {
  477. __le32 opts1;
  478. __le32 opts2;
  479. __le64 addr;
  480. };
  481. struct RxDesc {
  482. __le32 opts1;
  483. __le32 opts2;
  484. __le64 addr;
  485. };
  486. struct ring_info {
  487. struct sk_buff *skb;
  488. u32 len;
  489. };
  490. struct rtl8169_counters {
  491. __le64 tx_packets;
  492. __le64 rx_packets;
  493. __le64 tx_errors;
  494. __le32 rx_errors;
  495. __le16 rx_missed;
  496. __le16 align_errors;
  497. __le32 tx_one_collision;
  498. __le32 tx_multi_collision;
  499. __le64 rx_unicast;
  500. __le64 rx_broadcast;
  501. __le32 rx_multicast;
  502. __le16 tx_aborted;
  503. __le16 tx_underun;
  504. };
  505. struct rtl8169_tc_offsets {
  506. bool inited;
  507. __le64 tx_errors;
  508. __le32 tx_multi_collision;
  509. __le16 tx_aborted;
  510. __le16 rx_missed;
  511. };
  512. enum rtl_flag {
  513. RTL_FLAG_TASK_ENABLED = 0,
  514. RTL_FLAG_TASK_RESET_PENDING,
  515. RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE,
  516. RTL_FLAG_TASK_TX_TIMEOUT,
  517. RTL_FLAG_MAX
  518. };
  519. enum rtl_dash_type {
  520. RTL_DASH_NONE,
  521. RTL_DASH_DP,
  522. RTL_DASH_EP,
  523. };
  524. struct rtl8169_private {
  525. void __iomem *mmio_addr; /* memory map physical address */
  526. struct pci_dev *pci_dev;
  527. struct net_device *dev;
  528. struct phy_device *phydev;
  529. struct napi_struct napi;
  530. enum mac_version mac_version;
  531. enum rtl_dash_type dash_type;
  532. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  533. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  534. u32 dirty_tx;
  535. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  536. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  537. dma_addr_t TxPhyAddr;
  538. dma_addr_t RxPhyAddr;
  539. struct page *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  540. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  541. u16 cp_cmd;
  542. u32 irq_mask;
  543. int irq;
  544. struct clk *clk;
  545. struct {
  546. DECLARE_BITMAP(flags, RTL_FLAG_MAX);
  547. struct work_struct work;
  548. } wk;
  549. unsigned supports_gmii:1;
  550. unsigned aspm_manageable:1;
  551. unsigned dash_enabled:1;
  552. dma_addr_t counters_phys_addr;
  553. struct rtl8169_counters *counters;
  554. struct rtl8169_tc_offsets tc_offset;
  555. u32 saved_wolopts;
  556. int eee_adv;
  557. const char *fw_name;
  558. struct rtl_fw *rtl_fw;
  559. u32 ocp_base;
  560. };
  561. typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
  562. MODULE_AUTHOR("Realtek and the Linux r8169 crew <[email protected]>");
  563. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  564. MODULE_SOFTDEP("pre: realtek");
  565. MODULE_LICENSE("GPL");
  566. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  567. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  568. MODULE_FIRMWARE(FIRMWARE_8168E_1);
  569. MODULE_FIRMWARE(FIRMWARE_8168E_2);
  570. MODULE_FIRMWARE(FIRMWARE_8168E_3);
  571. MODULE_FIRMWARE(FIRMWARE_8105E_1);
  572. MODULE_FIRMWARE(FIRMWARE_8168F_1);
  573. MODULE_FIRMWARE(FIRMWARE_8168F_2);
  574. MODULE_FIRMWARE(FIRMWARE_8402_1);
  575. MODULE_FIRMWARE(FIRMWARE_8411_1);
  576. MODULE_FIRMWARE(FIRMWARE_8411_2);
  577. MODULE_FIRMWARE(FIRMWARE_8106E_1);
  578. MODULE_FIRMWARE(FIRMWARE_8106E_2);
  579. MODULE_FIRMWARE(FIRMWARE_8168G_2);
  580. MODULE_FIRMWARE(FIRMWARE_8168G_3);
  581. MODULE_FIRMWARE(FIRMWARE_8168H_2);
  582. MODULE_FIRMWARE(FIRMWARE_8168FP_3);
  583. MODULE_FIRMWARE(FIRMWARE_8107E_2);
  584. MODULE_FIRMWARE(FIRMWARE_8125A_3);
  585. MODULE_FIRMWARE(FIRMWARE_8125B_2);
  586. static inline struct device *tp_to_dev(struct rtl8169_private *tp)
  587. {
  588. return &tp->pci_dev->dev;
  589. }
  590. static void rtl_lock_config_regs(struct rtl8169_private *tp)
  591. {
  592. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  593. }
  594. static void rtl_unlock_config_regs(struct rtl8169_private *tp)
  595. {
  596. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  597. }
  598. static void rtl_pci_commit(struct rtl8169_private *tp)
  599. {
  600. /* Read an arbitrary register to commit a preceding PCI write */
  601. RTL_R8(tp, ChipCmd);
  602. }
  603. static bool rtl_is_8125(struct rtl8169_private *tp)
  604. {
  605. return tp->mac_version >= RTL_GIGA_MAC_VER_61;
  606. }
  607. static bool rtl_is_8168evl_up(struct rtl8169_private *tp)
  608. {
  609. return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
  610. tp->mac_version != RTL_GIGA_MAC_VER_39 &&
  611. tp->mac_version <= RTL_GIGA_MAC_VER_53;
  612. }
  613. static bool rtl_supports_eee(struct rtl8169_private *tp)
  614. {
  615. return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
  616. tp->mac_version != RTL_GIGA_MAC_VER_37 &&
  617. tp->mac_version != RTL_GIGA_MAC_VER_39;
  618. }
  619. static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg)
  620. {
  621. int i;
  622. for (i = 0; i < ETH_ALEN; i++)
  623. mac[i] = RTL_R8(tp, reg + i);
  624. }
  625. struct rtl_cond {
  626. bool (*check)(struct rtl8169_private *);
  627. const char *msg;
  628. };
  629. static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
  630. unsigned long usecs, int n, bool high)
  631. {
  632. int i;
  633. for (i = 0; i < n; i++) {
  634. if (c->check(tp) == high)
  635. return true;
  636. fsleep(usecs);
  637. }
  638. if (net_ratelimit())
  639. netdev_err(tp->dev, "%s == %d (loop: %d, delay: %lu).\n",
  640. c->msg, !high, n, usecs);
  641. return false;
  642. }
  643. static bool rtl_loop_wait_high(struct rtl8169_private *tp,
  644. const struct rtl_cond *c,
  645. unsigned long d, int n)
  646. {
  647. return rtl_loop_wait(tp, c, d, n, true);
  648. }
  649. static bool rtl_loop_wait_low(struct rtl8169_private *tp,
  650. const struct rtl_cond *c,
  651. unsigned long d, int n)
  652. {
  653. return rtl_loop_wait(tp, c, d, n, false);
  654. }
  655. #define DECLARE_RTL_COND(name) \
  656. static bool name ## _check(struct rtl8169_private *); \
  657. \
  658. static const struct rtl_cond name = { \
  659. .check = name ## _check, \
  660. .msg = #name \
  661. }; \
  662. \
  663. static bool name ## _check(struct rtl8169_private *tp)
  664. static void r8168fp_adjust_ocp_cmd(struct rtl8169_private *tp, u32 *cmd, int type)
  665. {
  666. /* based on RTL8168FP_OOBMAC_BASE in vendor driver */
  667. if (type == ERIAR_OOB &&
  668. (tp->mac_version == RTL_GIGA_MAC_VER_52 ||
  669. tp->mac_version == RTL_GIGA_MAC_VER_53))
  670. *cmd |= 0xf70 << 18;
  671. }
  672. DECLARE_RTL_COND(rtl_eriar_cond)
  673. {
  674. return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
  675. }
  676. static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
  677. u32 val, int type)
  678. {
  679. u32 cmd = ERIAR_WRITE_CMD | type | mask | addr;
  680. if (WARN(addr & 3 || !mask, "addr: 0x%x, mask: 0x%08x\n", addr, mask))
  681. return;
  682. RTL_W32(tp, ERIDR, val);
  683. r8168fp_adjust_ocp_cmd(tp, &cmd, type);
  684. RTL_W32(tp, ERIAR, cmd);
  685. rtl_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
  686. }
  687. static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
  688. u32 val)
  689. {
  690. _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
  691. }
  692. static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
  693. {
  694. u32 cmd = ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr;
  695. r8168fp_adjust_ocp_cmd(tp, &cmd, type);
  696. RTL_W32(tp, ERIAR, cmd);
  697. return rtl_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
  698. RTL_R32(tp, ERIDR) : ~0;
  699. }
  700. static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
  701. {
  702. return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
  703. }
  704. static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 p, u32 m)
  705. {
  706. u32 val = rtl_eri_read(tp, addr);
  707. rtl_eri_write(tp, addr, ERIAR_MASK_1111, (val & ~m) | p);
  708. }
  709. static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 p)
  710. {
  711. rtl_w0w1_eri(tp, addr, p, 0);
  712. }
  713. static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 m)
  714. {
  715. rtl_w0w1_eri(tp, addr, 0, m);
  716. }
  717. static bool rtl_ocp_reg_failure(u32 reg)
  718. {
  719. return WARN_ONCE(reg & 0xffff0001, "Invalid ocp reg %x!\n", reg);
  720. }
  721. DECLARE_RTL_COND(rtl_ocp_gphy_cond)
  722. {
  723. return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
  724. }
  725. static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  726. {
  727. if (rtl_ocp_reg_failure(reg))
  728. return;
  729. RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
  730. rtl_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
  731. }
  732. static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
  733. {
  734. if (rtl_ocp_reg_failure(reg))
  735. return 0;
  736. RTL_W32(tp, GPHY_OCP, reg << 15);
  737. return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
  738. (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
  739. }
  740. static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  741. {
  742. if (rtl_ocp_reg_failure(reg))
  743. return;
  744. RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
  745. }
  746. static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
  747. {
  748. if (rtl_ocp_reg_failure(reg))
  749. return 0;
  750. RTL_W32(tp, OCPDR, reg << 15);
  751. return RTL_R32(tp, OCPDR);
  752. }
  753. static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
  754. u16 set)
  755. {
  756. u16 data = r8168_mac_ocp_read(tp, reg);
  757. r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
  758. }
  759. /* Work around a hw issue with RTL8168g PHY, the quirk disables
  760. * PHY MCU interrupts before PHY power-down.
  761. */
  762. static void rtl8168g_phy_suspend_quirk(struct rtl8169_private *tp, int value)
  763. {
  764. switch (tp->mac_version) {
  765. case RTL_GIGA_MAC_VER_40:
  766. if (value & BMCR_RESET || !(value & BMCR_PDOWN))
  767. rtl_eri_set_bits(tp, 0x1a8, 0xfc000000);
  768. else
  769. rtl_eri_clear_bits(tp, 0x1a8, 0xfc000000);
  770. break;
  771. default:
  772. break;
  773. }
  774. };
  775. static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
  776. {
  777. if (reg == 0x1f) {
  778. tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
  779. return;
  780. }
  781. if (tp->ocp_base != OCP_STD_PHY_BASE)
  782. reg -= 0x10;
  783. if (tp->ocp_base == OCP_STD_PHY_BASE && reg == MII_BMCR)
  784. rtl8168g_phy_suspend_quirk(tp, value);
  785. r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
  786. }
  787. static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
  788. {
  789. if (reg == 0x1f)
  790. return tp->ocp_base == OCP_STD_PHY_BASE ? 0 : tp->ocp_base >> 4;
  791. if (tp->ocp_base != OCP_STD_PHY_BASE)
  792. reg -= 0x10;
  793. return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
  794. }
  795. static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
  796. {
  797. if (reg == 0x1f) {
  798. tp->ocp_base = value << 4;
  799. return;
  800. }
  801. r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
  802. }
  803. static int mac_mcu_read(struct rtl8169_private *tp, int reg)
  804. {
  805. return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
  806. }
  807. DECLARE_RTL_COND(rtl_phyar_cond)
  808. {
  809. return RTL_R32(tp, PHYAR) & 0x80000000;
  810. }
  811. static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
  812. {
  813. RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
  814. rtl_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
  815. /*
  816. * According to hardware specs a 20us delay is required after write
  817. * complete indication, but before sending next command.
  818. */
  819. udelay(20);
  820. }
  821. static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
  822. {
  823. int value;
  824. RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
  825. value = rtl_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
  826. RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
  827. /*
  828. * According to hardware specs a 20us delay is required after read
  829. * complete indication, but before sending next command.
  830. */
  831. udelay(20);
  832. return value;
  833. }
  834. DECLARE_RTL_COND(rtl_ocpar_cond)
  835. {
  836. return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
  837. }
  838. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  839. static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
  840. {
  841. RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  842. }
  843. static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
  844. {
  845. RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  846. }
  847. static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
  848. {
  849. r8168dp_2_mdio_start(tp);
  850. r8169_mdio_write(tp, reg, value);
  851. r8168dp_2_mdio_stop(tp);
  852. }
  853. static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
  854. {
  855. int value;
  856. /* Work around issue with chip reporting wrong PHY ID */
  857. if (reg == MII_PHYSID2)
  858. return 0xc912;
  859. r8168dp_2_mdio_start(tp);
  860. value = r8169_mdio_read(tp, reg);
  861. r8168dp_2_mdio_stop(tp);
  862. return value;
  863. }
  864. static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
  865. {
  866. switch (tp->mac_version) {
  867. case RTL_GIGA_MAC_VER_28:
  868. case RTL_GIGA_MAC_VER_31:
  869. r8168dp_2_mdio_write(tp, location, val);
  870. break;
  871. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_63:
  872. r8168g_mdio_write(tp, location, val);
  873. break;
  874. default:
  875. r8169_mdio_write(tp, location, val);
  876. break;
  877. }
  878. }
  879. static int rtl_readphy(struct rtl8169_private *tp, int location)
  880. {
  881. switch (tp->mac_version) {
  882. case RTL_GIGA_MAC_VER_28:
  883. case RTL_GIGA_MAC_VER_31:
  884. return r8168dp_2_mdio_read(tp, location);
  885. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_63:
  886. return r8168g_mdio_read(tp, location);
  887. default:
  888. return r8169_mdio_read(tp, location);
  889. }
  890. }
  891. DECLARE_RTL_COND(rtl_ephyar_cond)
  892. {
  893. return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
  894. }
  895. static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
  896. {
  897. RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  898. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  899. rtl_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
  900. udelay(10);
  901. }
  902. static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
  903. {
  904. RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  905. return rtl_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
  906. RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
  907. }
  908. static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u16 reg)
  909. {
  910. RTL_W32(tp, OCPAR, 0x0fu << 12 | (reg & 0x0fff));
  911. return rtl_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
  912. RTL_R32(tp, OCPDR) : ~0;
  913. }
  914. static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u16 reg)
  915. {
  916. return _rtl_eri_read(tp, reg, ERIAR_OOB);
  917. }
  918. static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  919. u32 data)
  920. {
  921. RTL_W32(tp, OCPDR, data);
  922. RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  923. rtl_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
  924. }
  925. static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  926. u32 data)
  927. {
  928. _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
  929. data, ERIAR_OOB);
  930. }
  931. static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
  932. {
  933. rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
  934. r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
  935. }
  936. #define OOB_CMD_RESET 0x00
  937. #define OOB_CMD_DRIVER_START 0x05
  938. #define OOB_CMD_DRIVER_STOP 0x06
  939. static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
  940. {
  941. return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
  942. }
  943. DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
  944. {
  945. u16 reg;
  946. reg = rtl8168_get_ocp_reg(tp);
  947. return r8168dp_ocp_read(tp, reg) & 0x00000800;
  948. }
  949. DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
  950. {
  951. return r8168ep_ocp_read(tp, 0x124) & 0x00000001;
  952. }
  953. DECLARE_RTL_COND(rtl_ocp_tx_cond)
  954. {
  955. return RTL_R8(tp, IBISR0) & 0x20;
  956. }
  957. static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
  958. {
  959. RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
  960. rtl_loop_wait_high(tp, &rtl_ocp_tx_cond, 50000, 2000);
  961. RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
  962. RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
  963. }
  964. static void rtl8168dp_driver_start(struct rtl8169_private *tp)
  965. {
  966. r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
  967. rtl_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10000, 10);
  968. }
  969. static void rtl8168ep_driver_start(struct rtl8169_private *tp)
  970. {
  971. r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
  972. r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01);
  973. rtl_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10000, 10);
  974. }
  975. static void rtl8168_driver_start(struct rtl8169_private *tp)
  976. {
  977. if (tp->dash_type == RTL_DASH_DP)
  978. rtl8168dp_driver_start(tp);
  979. else
  980. rtl8168ep_driver_start(tp);
  981. }
  982. static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
  983. {
  984. r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  985. rtl_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10000, 10);
  986. }
  987. static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
  988. {
  989. rtl8168ep_stop_cmac(tp);
  990. r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
  991. r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01);
  992. rtl_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10000, 10);
  993. }
  994. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  995. {
  996. if (tp->dash_type == RTL_DASH_DP)
  997. rtl8168dp_driver_stop(tp);
  998. else
  999. rtl8168ep_driver_stop(tp);
  1000. }
  1001. static bool r8168dp_check_dash(struct rtl8169_private *tp)
  1002. {
  1003. u16 reg = rtl8168_get_ocp_reg(tp);
  1004. return r8168dp_ocp_read(tp, reg) & BIT(15);
  1005. }
  1006. static bool r8168ep_check_dash(struct rtl8169_private *tp)
  1007. {
  1008. return r8168ep_ocp_read(tp, 0x128) & BIT(0);
  1009. }
  1010. static bool rtl_dash_is_enabled(struct rtl8169_private *tp)
  1011. {
  1012. switch (tp->dash_type) {
  1013. case RTL_DASH_DP:
  1014. return r8168dp_check_dash(tp);
  1015. case RTL_DASH_EP:
  1016. return r8168ep_check_dash(tp);
  1017. default:
  1018. return false;
  1019. }
  1020. }
  1021. static enum rtl_dash_type rtl_get_dash_type(struct rtl8169_private *tp)
  1022. {
  1023. switch (tp->mac_version) {
  1024. case RTL_GIGA_MAC_VER_28:
  1025. case RTL_GIGA_MAC_VER_31:
  1026. return RTL_DASH_DP;
  1027. case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_53:
  1028. return RTL_DASH_EP;
  1029. default:
  1030. return RTL_DASH_NONE;
  1031. }
  1032. }
  1033. static void rtl_set_d3_pll_down(struct rtl8169_private *tp, bool enable)
  1034. {
  1035. switch (tp->mac_version) {
  1036. case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_26:
  1037. case RTL_GIGA_MAC_VER_29 ... RTL_GIGA_MAC_VER_30:
  1038. case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_37:
  1039. case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_63:
  1040. if (enable)
  1041. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~D3_NO_PLL_DOWN);
  1042. else
  1043. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | D3_NO_PLL_DOWN);
  1044. break;
  1045. default:
  1046. break;
  1047. }
  1048. }
  1049. static void rtl_reset_packet_filter(struct rtl8169_private *tp)
  1050. {
  1051. rtl_eri_clear_bits(tp, 0xdc, BIT(0));
  1052. rtl_eri_set_bits(tp, 0xdc, BIT(0));
  1053. }
  1054. DECLARE_RTL_COND(rtl_efusear_cond)
  1055. {
  1056. return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
  1057. }
  1058. u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
  1059. {
  1060. RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  1061. return rtl_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
  1062. RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
  1063. }
  1064. static u32 rtl_get_events(struct rtl8169_private *tp)
  1065. {
  1066. if (rtl_is_8125(tp))
  1067. return RTL_R32(tp, IntrStatus_8125);
  1068. else
  1069. return RTL_R16(tp, IntrStatus);
  1070. }
  1071. static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
  1072. {
  1073. if (rtl_is_8125(tp))
  1074. RTL_W32(tp, IntrStatus_8125, bits);
  1075. else
  1076. RTL_W16(tp, IntrStatus, bits);
  1077. }
  1078. static void rtl_irq_disable(struct rtl8169_private *tp)
  1079. {
  1080. if (rtl_is_8125(tp))
  1081. RTL_W32(tp, IntrMask_8125, 0);
  1082. else
  1083. RTL_W16(tp, IntrMask, 0);
  1084. }
  1085. static void rtl_irq_enable(struct rtl8169_private *tp)
  1086. {
  1087. if (rtl_is_8125(tp))
  1088. RTL_W32(tp, IntrMask_8125, tp->irq_mask);
  1089. else
  1090. RTL_W16(tp, IntrMask, tp->irq_mask);
  1091. }
  1092. static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
  1093. {
  1094. rtl_irq_disable(tp);
  1095. rtl_ack_events(tp, 0xffffffff);
  1096. rtl_pci_commit(tp);
  1097. }
  1098. static void rtl_link_chg_patch(struct rtl8169_private *tp)
  1099. {
  1100. struct phy_device *phydev = tp->phydev;
  1101. if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  1102. tp->mac_version == RTL_GIGA_MAC_VER_38) {
  1103. if (phydev->speed == SPEED_1000) {
  1104. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
  1105. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
  1106. } else if (phydev->speed == SPEED_100) {
  1107. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
  1108. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
  1109. } else {
  1110. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
  1111. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
  1112. }
  1113. rtl_reset_packet_filter(tp);
  1114. } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  1115. tp->mac_version == RTL_GIGA_MAC_VER_36) {
  1116. if (phydev->speed == SPEED_1000) {
  1117. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
  1118. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
  1119. } else {
  1120. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
  1121. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
  1122. }
  1123. } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
  1124. if (phydev->speed == SPEED_10) {
  1125. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
  1126. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
  1127. } else {
  1128. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
  1129. }
  1130. }
  1131. }
  1132. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1133. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1134. {
  1135. struct rtl8169_private *tp = netdev_priv(dev);
  1136. wol->supported = WAKE_ANY;
  1137. wol->wolopts = tp->saved_wolopts;
  1138. }
  1139. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  1140. {
  1141. static const struct {
  1142. u32 opt;
  1143. u16 reg;
  1144. u8 mask;
  1145. } cfg[] = {
  1146. { WAKE_PHY, Config3, LinkUp },
  1147. { WAKE_UCAST, Config5, UWF },
  1148. { WAKE_BCAST, Config5, BWF },
  1149. { WAKE_MCAST, Config5, MWF },
  1150. { WAKE_ANY, Config5, LanWake },
  1151. { WAKE_MAGIC, Config3, MagicPacket }
  1152. };
  1153. unsigned int i, tmp = ARRAY_SIZE(cfg);
  1154. u8 options;
  1155. rtl_unlock_config_regs(tp);
  1156. if (rtl_is_8168evl_up(tp)) {
  1157. tmp--;
  1158. if (wolopts & WAKE_MAGIC)
  1159. rtl_eri_set_bits(tp, 0x0dc, MagicPacket_v2);
  1160. else
  1161. rtl_eri_clear_bits(tp, 0x0dc, MagicPacket_v2);
  1162. } else if (rtl_is_8125(tp)) {
  1163. tmp--;
  1164. if (wolopts & WAKE_MAGIC)
  1165. r8168_mac_ocp_modify(tp, 0xc0b6, 0, BIT(0));
  1166. else
  1167. r8168_mac_ocp_modify(tp, 0xc0b6, BIT(0), 0);
  1168. }
  1169. for (i = 0; i < tmp; i++) {
  1170. options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
  1171. if (wolopts & cfg[i].opt)
  1172. options |= cfg[i].mask;
  1173. RTL_W8(tp, cfg[i].reg, options);
  1174. }
  1175. switch (tp->mac_version) {
  1176. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  1177. options = RTL_R8(tp, Config1) & ~PMEnable;
  1178. if (wolopts)
  1179. options |= PMEnable;
  1180. RTL_W8(tp, Config1, options);
  1181. break;
  1182. case RTL_GIGA_MAC_VER_34:
  1183. case RTL_GIGA_MAC_VER_37:
  1184. case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_63:
  1185. options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
  1186. if (wolopts)
  1187. options |= PME_SIGNAL;
  1188. RTL_W8(tp, Config2, options);
  1189. break;
  1190. default:
  1191. break;
  1192. }
  1193. rtl_lock_config_regs(tp);
  1194. device_set_wakeup_enable(tp_to_dev(tp), wolopts);
  1195. if (!tp->dash_enabled) {
  1196. rtl_set_d3_pll_down(tp, !wolopts);
  1197. tp->dev->wol_enabled = wolopts ? 1 : 0;
  1198. }
  1199. }
  1200. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1201. {
  1202. struct rtl8169_private *tp = netdev_priv(dev);
  1203. if (wol->wolopts & ~WAKE_ANY)
  1204. return -EINVAL;
  1205. tp->saved_wolopts = wol->wolopts;
  1206. __rtl8169_set_wol(tp, tp->saved_wolopts);
  1207. return 0;
  1208. }
  1209. static void rtl8169_get_drvinfo(struct net_device *dev,
  1210. struct ethtool_drvinfo *info)
  1211. {
  1212. struct rtl8169_private *tp = netdev_priv(dev);
  1213. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1214. strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1215. strscpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
  1216. BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
  1217. if (rtl_fw)
  1218. strscpy(info->fw_version, rtl_fw->version,
  1219. sizeof(info->fw_version));
  1220. }
  1221. static int rtl8169_get_regs_len(struct net_device *dev)
  1222. {
  1223. return R8169_REGS_SIZE;
  1224. }
  1225. static netdev_features_t rtl8169_fix_features(struct net_device *dev,
  1226. netdev_features_t features)
  1227. {
  1228. struct rtl8169_private *tp = netdev_priv(dev);
  1229. if (dev->mtu > TD_MSS_MAX)
  1230. features &= ~NETIF_F_ALL_TSO;
  1231. if (dev->mtu > ETH_DATA_LEN &&
  1232. tp->mac_version > RTL_GIGA_MAC_VER_06)
  1233. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO);
  1234. return features;
  1235. }
  1236. static void rtl_set_rx_config_features(struct rtl8169_private *tp,
  1237. netdev_features_t features)
  1238. {
  1239. u32 rx_config = RTL_R32(tp, RxConfig);
  1240. if (features & NETIF_F_RXALL)
  1241. rx_config |= RX_CONFIG_ACCEPT_ERR_MASK;
  1242. else
  1243. rx_config &= ~RX_CONFIG_ACCEPT_ERR_MASK;
  1244. if (rtl_is_8125(tp)) {
  1245. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1246. rx_config |= RX_VLAN_8125;
  1247. else
  1248. rx_config &= ~RX_VLAN_8125;
  1249. }
  1250. RTL_W32(tp, RxConfig, rx_config);
  1251. }
  1252. static int rtl8169_set_features(struct net_device *dev,
  1253. netdev_features_t features)
  1254. {
  1255. struct rtl8169_private *tp = netdev_priv(dev);
  1256. rtl_set_rx_config_features(tp, features);
  1257. if (features & NETIF_F_RXCSUM)
  1258. tp->cp_cmd |= RxChkSum;
  1259. else
  1260. tp->cp_cmd &= ~RxChkSum;
  1261. if (!rtl_is_8125(tp)) {
  1262. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1263. tp->cp_cmd |= RxVlan;
  1264. else
  1265. tp->cp_cmd &= ~RxVlan;
  1266. }
  1267. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  1268. rtl_pci_commit(tp);
  1269. return 0;
  1270. }
  1271. static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
  1272. {
  1273. return (skb_vlan_tag_present(skb)) ?
  1274. TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
  1275. }
  1276. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1277. {
  1278. u32 opts2 = le32_to_cpu(desc->opts2);
  1279. if (opts2 & RxVlanTag)
  1280. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
  1281. }
  1282. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1283. void *p)
  1284. {
  1285. struct rtl8169_private *tp = netdev_priv(dev);
  1286. u32 __iomem *data = tp->mmio_addr;
  1287. u32 *dw = p;
  1288. int i;
  1289. for (i = 0; i < R8169_REGS_SIZE; i += 4)
  1290. memcpy_fromio(dw++, data++, 4);
  1291. }
  1292. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1293. "tx_packets",
  1294. "rx_packets",
  1295. "tx_errors",
  1296. "rx_errors",
  1297. "rx_missed",
  1298. "align_errors",
  1299. "tx_single_collisions",
  1300. "tx_multi_collisions",
  1301. "unicast",
  1302. "broadcast",
  1303. "multicast",
  1304. "tx_aborted",
  1305. "tx_underrun",
  1306. };
  1307. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1308. {
  1309. switch (sset) {
  1310. case ETH_SS_STATS:
  1311. return ARRAY_SIZE(rtl8169_gstrings);
  1312. default:
  1313. return -EOPNOTSUPP;
  1314. }
  1315. }
  1316. DECLARE_RTL_COND(rtl_counters_cond)
  1317. {
  1318. return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
  1319. }
  1320. static void rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
  1321. {
  1322. u32 cmd = lower_32_bits(tp->counters_phys_addr);
  1323. RTL_W32(tp, CounterAddrHigh, upper_32_bits(tp->counters_phys_addr));
  1324. rtl_pci_commit(tp);
  1325. RTL_W32(tp, CounterAddrLow, cmd);
  1326. RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
  1327. rtl_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
  1328. }
  1329. static void rtl8169_update_counters(struct rtl8169_private *tp)
  1330. {
  1331. u8 val = RTL_R8(tp, ChipCmd);
  1332. /*
  1333. * Some chips are unable to dump tally counters when the receiver
  1334. * is disabled. If 0xff chip may be in a PCI power-save state.
  1335. */
  1336. if (val & CmdRxEnb && val != 0xff)
  1337. rtl8169_do_counters(tp, CounterDump);
  1338. }
  1339. static void rtl8169_init_counter_offsets(struct rtl8169_private *tp)
  1340. {
  1341. struct rtl8169_counters *counters = tp->counters;
  1342. /*
  1343. * rtl8169_init_counter_offsets is called from rtl_open. On chip
  1344. * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
  1345. * reset by a power cycle, while the counter values collected by the
  1346. * driver are reset at every driver unload/load cycle.
  1347. *
  1348. * To make sure the HW values returned by @get_stats64 match the SW
  1349. * values, we collect the initial values at first open(*) and use them
  1350. * as offsets to normalize the values returned by @get_stats64.
  1351. *
  1352. * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
  1353. * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
  1354. * set at open time by rtl_hw_start.
  1355. */
  1356. if (tp->tc_offset.inited)
  1357. return;
  1358. if (tp->mac_version >= RTL_GIGA_MAC_VER_19) {
  1359. rtl8169_do_counters(tp, CounterReset);
  1360. } else {
  1361. rtl8169_update_counters(tp);
  1362. tp->tc_offset.tx_errors = counters->tx_errors;
  1363. tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
  1364. tp->tc_offset.tx_aborted = counters->tx_aborted;
  1365. tp->tc_offset.rx_missed = counters->rx_missed;
  1366. }
  1367. tp->tc_offset.inited = true;
  1368. }
  1369. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1370. struct ethtool_stats *stats, u64 *data)
  1371. {
  1372. struct rtl8169_private *tp = netdev_priv(dev);
  1373. struct rtl8169_counters *counters;
  1374. counters = tp->counters;
  1375. rtl8169_update_counters(tp);
  1376. data[0] = le64_to_cpu(counters->tx_packets);
  1377. data[1] = le64_to_cpu(counters->rx_packets);
  1378. data[2] = le64_to_cpu(counters->tx_errors);
  1379. data[3] = le32_to_cpu(counters->rx_errors);
  1380. data[4] = le16_to_cpu(counters->rx_missed);
  1381. data[5] = le16_to_cpu(counters->align_errors);
  1382. data[6] = le32_to_cpu(counters->tx_one_collision);
  1383. data[7] = le32_to_cpu(counters->tx_multi_collision);
  1384. data[8] = le64_to_cpu(counters->rx_unicast);
  1385. data[9] = le64_to_cpu(counters->rx_broadcast);
  1386. data[10] = le32_to_cpu(counters->rx_multicast);
  1387. data[11] = le16_to_cpu(counters->tx_aborted);
  1388. data[12] = le16_to_cpu(counters->tx_underun);
  1389. }
  1390. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1391. {
  1392. switch(stringset) {
  1393. case ETH_SS_STATS:
  1394. memcpy(data, rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1395. break;
  1396. }
  1397. }
  1398. /*
  1399. * Interrupt coalescing
  1400. *
  1401. * > 1 - the availability of the IntrMitigate (0xe2) register through the
  1402. * > 8169, 8168 and 810x line of chipsets
  1403. *
  1404. * 8169, 8168, and 8136(810x) serial chipsets support it.
  1405. *
  1406. * > 2 - the Tx timer unit at gigabit speed
  1407. *
  1408. * The unit of the timer depends on both the speed and the setting of CPlusCmd
  1409. * (0xe0) bit 1 and bit 0.
  1410. *
  1411. * For 8169
  1412. * bit[1:0] \ speed 1000M 100M 10M
  1413. * 0 0 320ns 2.56us 40.96us
  1414. * 0 1 2.56us 20.48us 327.7us
  1415. * 1 0 5.12us 40.96us 655.4us
  1416. * 1 1 10.24us 81.92us 1.31ms
  1417. *
  1418. * For the other
  1419. * bit[1:0] \ speed 1000M 100M 10M
  1420. * 0 0 5us 2.56us 40.96us
  1421. * 0 1 40us 20.48us 327.7us
  1422. * 1 0 80us 40.96us 655.4us
  1423. * 1 1 160us 81.92us 1.31ms
  1424. */
  1425. /* rx/tx scale factors for all CPlusCmd[0:1] cases */
  1426. struct rtl_coalesce_info {
  1427. u32 speed;
  1428. u32 scale_nsecs[4];
  1429. };
  1430. /* produce array with base delay *1, *8, *8*2, *8*2*2 */
  1431. #define COALESCE_DELAY(d) { (d), 8 * (d), 16 * (d), 32 * (d) }
  1432. static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
  1433. { SPEED_1000, COALESCE_DELAY(320) },
  1434. { SPEED_100, COALESCE_DELAY(2560) },
  1435. { SPEED_10, COALESCE_DELAY(40960) },
  1436. { 0 },
  1437. };
  1438. static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
  1439. { SPEED_1000, COALESCE_DELAY(5000) },
  1440. { SPEED_100, COALESCE_DELAY(2560) },
  1441. { SPEED_10, COALESCE_DELAY(40960) },
  1442. { 0 },
  1443. };
  1444. #undef COALESCE_DELAY
  1445. /* get rx/tx scale vector corresponding to current speed */
  1446. static const struct rtl_coalesce_info *
  1447. rtl_coalesce_info(struct rtl8169_private *tp)
  1448. {
  1449. const struct rtl_coalesce_info *ci;
  1450. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  1451. ci = rtl_coalesce_info_8169;
  1452. else
  1453. ci = rtl_coalesce_info_8168_8136;
  1454. /* if speed is unknown assume highest one */
  1455. if (tp->phydev->speed == SPEED_UNKNOWN)
  1456. return ci;
  1457. for (; ci->speed; ci++) {
  1458. if (tp->phydev->speed == ci->speed)
  1459. return ci;
  1460. }
  1461. return ERR_PTR(-ELNRNG);
  1462. }
  1463. static int rtl_get_coalesce(struct net_device *dev,
  1464. struct ethtool_coalesce *ec,
  1465. struct kernel_ethtool_coalesce *kernel_coal,
  1466. struct netlink_ext_ack *extack)
  1467. {
  1468. struct rtl8169_private *tp = netdev_priv(dev);
  1469. const struct rtl_coalesce_info *ci;
  1470. u32 scale, c_us, c_fr;
  1471. u16 intrmit;
  1472. if (rtl_is_8125(tp))
  1473. return -EOPNOTSUPP;
  1474. memset(ec, 0, sizeof(*ec));
  1475. /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
  1476. ci = rtl_coalesce_info(tp);
  1477. if (IS_ERR(ci))
  1478. return PTR_ERR(ci);
  1479. scale = ci->scale_nsecs[tp->cp_cmd & INTT_MASK];
  1480. intrmit = RTL_R16(tp, IntrMitigate);
  1481. c_us = FIELD_GET(RTL_COALESCE_TX_USECS, intrmit);
  1482. ec->tx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000);
  1483. c_fr = FIELD_GET(RTL_COALESCE_TX_FRAMES, intrmit);
  1484. /* ethtool_coalesce states usecs and max_frames must not both be 0 */
  1485. ec->tx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1;
  1486. c_us = FIELD_GET(RTL_COALESCE_RX_USECS, intrmit);
  1487. ec->rx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000);
  1488. c_fr = FIELD_GET(RTL_COALESCE_RX_FRAMES, intrmit);
  1489. ec->rx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1;
  1490. return 0;
  1491. }
  1492. /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, usec) */
  1493. static int rtl_coalesce_choose_scale(struct rtl8169_private *tp, u32 usec,
  1494. u16 *cp01)
  1495. {
  1496. const struct rtl_coalesce_info *ci;
  1497. u16 i;
  1498. ci = rtl_coalesce_info(tp);
  1499. if (IS_ERR(ci))
  1500. return PTR_ERR(ci);
  1501. for (i = 0; i < 4; i++) {
  1502. if (usec <= ci->scale_nsecs[i] * RTL_COALESCE_T_MAX / 1000U) {
  1503. *cp01 = i;
  1504. return ci->scale_nsecs[i];
  1505. }
  1506. }
  1507. return -ERANGE;
  1508. }
  1509. static int rtl_set_coalesce(struct net_device *dev,
  1510. struct ethtool_coalesce *ec,
  1511. struct kernel_ethtool_coalesce *kernel_coal,
  1512. struct netlink_ext_ack *extack)
  1513. {
  1514. struct rtl8169_private *tp = netdev_priv(dev);
  1515. u32 tx_fr = ec->tx_max_coalesced_frames;
  1516. u32 rx_fr = ec->rx_max_coalesced_frames;
  1517. u32 coal_usec_max, units;
  1518. u16 w = 0, cp01 = 0;
  1519. int scale;
  1520. if (rtl_is_8125(tp))
  1521. return -EOPNOTSUPP;
  1522. if (rx_fr > RTL_COALESCE_FRAME_MAX || tx_fr > RTL_COALESCE_FRAME_MAX)
  1523. return -ERANGE;
  1524. coal_usec_max = max(ec->rx_coalesce_usecs, ec->tx_coalesce_usecs);
  1525. scale = rtl_coalesce_choose_scale(tp, coal_usec_max, &cp01);
  1526. if (scale < 0)
  1527. return scale;
  1528. /* Accept max_frames=1 we returned in rtl_get_coalesce. Accept it
  1529. * not only when usecs=0 because of e.g. the following scenario:
  1530. *
  1531. * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
  1532. * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
  1533. * - then user does `ethtool -C eth0 rx-usecs 100`
  1534. *
  1535. * Since ethtool sends to kernel whole ethtool_coalesce settings,
  1536. * if we want to ignore rx_frames then it has to be set to 0.
  1537. */
  1538. if (rx_fr == 1)
  1539. rx_fr = 0;
  1540. if (tx_fr == 1)
  1541. tx_fr = 0;
  1542. /* HW requires time limit to be set if frame limit is set */
  1543. if ((tx_fr && !ec->tx_coalesce_usecs) ||
  1544. (rx_fr && !ec->rx_coalesce_usecs))
  1545. return -EINVAL;
  1546. w |= FIELD_PREP(RTL_COALESCE_TX_FRAMES, DIV_ROUND_UP(tx_fr, 4));
  1547. w |= FIELD_PREP(RTL_COALESCE_RX_FRAMES, DIV_ROUND_UP(rx_fr, 4));
  1548. units = DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000U, scale);
  1549. w |= FIELD_PREP(RTL_COALESCE_TX_USECS, units);
  1550. units = DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000U, scale);
  1551. w |= FIELD_PREP(RTL_COALESCE_RX_USECS, units);
  1552. RTL_W16(tp, IntrMitigate, w);
  1553. /* Meaning of PktCntrDisable bit changed from RTL8168e-vl */
  1554. if (rtl_is_8168evl_up(tp)) {
  1555. if (!rx_fr && !tx_fr)
  1556. /* disable packet counter */
  1557. tp->cp_cmd |= PktCntrDisable;
  1558. else
  1559. tp->cp_cmd &= ~PktCntrDisable;
  1560. }
  1561. tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
  1562. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  1563. rtl_pci_commit(tp);
  1564. return 0;
  1565. }
  1566. static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
  1567. {
  1568. struct rtl8169_private *tp = netdev_priv(dev);
  1569. if (!rtl_supports_eee(tp))
  1570. return -EOPNOTSUPP;
  1571. return phy_ethtool_get_eee(tp->phydev, data);
  1572. }
  1573. static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
  1574. {
  1575. struct rtl8169_private *tp = netdev_priv(dev);
  1576. int ret;
  1577. if (!rtl_supports_eee(tp))
  1578. return -EOPNOTSUPP;
  1579. ret = phy_ethtool_set_eee(tp->phydev, data);
  1580. if (!ret)
  1581. tp->eee_adv = phy_read_mmd(dev->phydev, MDIO_MMD_AN,
  1582. MDIO_AN_EEE_ADV);
  1583. return ret;
  1584. }
  1585. static void rtl8169_get_ringparam(struct net_device *dev,
  1586. struct ethtool_ringparam *data,
  1587. struct kernel_ethtool_ringparam *kernel_data,
  1588. struct netlink_ext_ack *extack)
  1589. {
  1590. data->rx_max_pending = NUM_RX_DESC;
  1591. data->rx_pending = NUM_RX_DESC;
  1592. data->tx_max_pending = NUM_TX_DESC;
  1593. data->tx_pending = NUM_TX_DESC;
  1594. }
  1595. static void rtl8169_get_pauseparam(struct net_device *dev,
  1596. struct ethtool_pauseparam *data)
  1597. {
  1598. struct rtl8169_private *tp = netdev_priv(dev);
  1599. bool tx_pause, rx_pause;
  1600. phy_get_pause(tp->phydev, &tx_pause, &rx_pause);
  1601. data->autoneg = tp->phydev->autoneg;
  1602. data->tx_pause = tx_pause ? 1 : 0;
  1603. data->rx_pause = rx_pause ? 1 : 0;
  1604. }
  1605. static int rtl8169_set_pauseparam(struct net_device *dev,
  1606. struct ethtool_pauseparam *data)
  1607. {
  1608. struct rtl8169_private *tp = netdev_priv(dev);
  1609. if (dev->mtu > ETH_DATA_LEN)
  1610. return -EOPNOTSUPP;
  1611. phy_set_asym_pause(tp->phydev, data->rx_pause, data->tx_pause);
  1612. return 0;
  1613. }
  1614. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1615. .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
  1616. ETHTOOL_COALESCE_MAX_FRAMES,
  1617. .get_drvinfo = rtl8169_get_drvinfo,
  1618. .get_regs_len = rtl8169_get_regs_len,
  1619. .get_link = ethtool_op_get_link,
  1620. .get_coalesce = rtl_get_coalesce,
  1621. .set_coalesce = rtl_set_coalesce,
  1622. .get_regs = rtl8169_get_regs,
  1623. .get_wol = rtl8169_get_wol,
  1624. .set_wol = rtl8169_set_wol,
  1625. .get_strings = rtl8169_get_strings,
  1626. .get_sset_count = rtl8169_get_sset_count,
  1627. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1628. .get_ts_info = ethtool_op_get_ts_info,
  1629. .nway_reset = phy_ethtool_nway_reset,
  1630. .get_eee = rtl8169_get_eee,
  1631. .set_eee = rtl8169_set_eee,
  1632. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1633. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1634. .get_ringparam = rtl8169_get_ringparam,
  1635. .get_pauseparam = rtl8169_get_pauseparam,
  1636. .set_pauseparam = rtl8169_set_pauseparam,
  1637. };
  1638. static void rtl_enable_eee(struct rtl8169_private *tp)
  1639. {
  1640. struct phy_device *phydev = tp->phydev;
  1641. int adv;
  1642. /* respect EEE advertisement the user may have set */
  1643. if (tp->eee_adv >= 0)
  1644. adv = tp->eee_adv;
  1645. else
  1646. adv = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  1647. if (adv >= 0)
  1648. phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1649. }
  1650. static enum mac_version rtl8169_get_mac_version(u16 xid, bool gmii)
  1651. {
  1652. /*
  1653. * The driver currently handles the 8168Bf and the 8168Be identically
  1654. * but they can be identified more specifically through the test below
  1655. * if needed:
  1656. *
  1657. * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1658. *
  1659. * Same thing for the 8101Eb and the 8101Ec:
  1660. *
  1661. * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1662. */
  1663. static const struct rtl_mac_info {
  1664. u16 mask;
  1665. u16 val;
  1666. enum mac_version ver;
  1667. } mac_info[] = {
  1668. /* 8125B family. */
  1669. { 0x7cf, 0x641, RTL_GIGA_MAC_VER_63 },
  1670. /* 8125A family. */
  1671. { 0x7cf, 0x609, RTL_GIGA_MAC_VER_61 },
  1672. /* It seems only XID 609 made it to the mass market.
  1673. * { 0x7cf, 0x608, RTL_GIGA_MAC_VER_60 },
  1674. * { 0x7c8, 0x608, RTL_GIGA_MAC_VER_61 },
  1675. */
  1676. /* RTL8117 */
  1677. { 0x7cf, 0x54b, RTL_GIGA_MAC_VER_53 },
  1678. { 0x7cf, 0x54a, RTL_GIGA_MAC_VER_52 },
  1679. /* 8168EP family. */
  1680. { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
  1681. /* It seems this chip version never made it to
  1682. * the wild. Let's disable detection.
  1683. * { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
  1684. * { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
  1685. */
  1686. /* 8168H family. */
  1687. { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
  1688. /* It seems this chip version never made it to
  1689. * the wild. Let's disable detection.
  1690. * { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
  1691. */
  1692. /* 8168G family. */
  1693. { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
  1694. { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
  1695. /* It seems this chip version never made it to
  1696. * the wild. Let's disable detection.
  1697. * { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
  1698. */
  1699. { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
  1700. /* 8168F family. */
  1701. { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
  1702. { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
  1703. { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
  1704. /* 8168E family. */
  1705. { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
  1706. { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
  1707. { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
  1708. /* 8168D family. */
  1709. { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
  1710. { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
  1711. /* 8168DP family. */
  1712. /* It seems this early RTL8168dp version never made it to
  1713. * the wild. Support has been removed.
  1714. * { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
  1715. */
  1716. { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
  1717. { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
  1718. /* 8168C family. */
  1719. { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
  1720. { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
  1721. { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
  1722. { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
  1723. { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
  1724. { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
  1725. { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
  1726. /* 8168B family. */
  1727. { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
  1728. { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
  1729. /* 8101 family. */
  1730. { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
  1731. { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
  1732. { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
  1733. { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
  1734. { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
  1735. { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
  1736. { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
  1737. { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
  1738. { 0x7cf, 0x240, RTL_GIGA_MAC_VER_14 },
  1739. { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
  1740. { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
  1741. { 0x7c8, 0x340, RTL_GIGA_MAC_VER_10 },
  1742. /* 8110 family. */
  1743. { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
  1744. { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
  1745. { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
  1746. { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
  1747. { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
  1748. /* Catch-all */
  1749. { 0x000, 0x000, RTL_GIGA_MAC_NONE }
  1750. };
  1751. const struct rtl_mac_info *p = mac_info;
  1752. enum mac_version ver;
  1753. while ((xid & p->mask) != p->val)
  1754. p++;
  1755. ver = p->ver;
  1756. if (ver != RTL_GIGA_MAC_NONE && !gmii) {
  1757. if (ver == RTL_GIGA_MAC_VER_42)
  1758. ver = RTL_GIGA_MAC_VER_43;
  1759. else if (ver == RTL_GIGA_MAC_VER_46)
  1760. ver = RTL_GIGA_MAC_VER_48;
  1761. }
  1762. return ver;
  1763. }
  1764. static void rtl_release_firmware(struct rtl8169_private *tp)
  1765. {
  1766. if (tp->rtl_fw) {
  1767. rtl_fw_release_firmware(tp->rtl_fw);
  1768. kfree(tp->rtl_fw);
  1769. tp->rtl_fw = NULL;
  1770. }
  1771. }
  1772. void r8169_apply_firmware(struct rtl8169_private *tp)
  1773. {
  1774. int val;
  1775. /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
  1776. if (tp->rtl_fw) {
  1777. rtl_fw_write_firmware(tp, tp->rtl_fw);
  1778. /* At least one firmware doesn't reset tp->ocp_base. */
  1779. tp->ocp_base = OCP_STD_PHY_BASE;
  1780. /* PHY soft reset may still be in progress */
  1781. phy_read_poll_timeout(tp->phydev, MII_BMCR, val,
  1782. !(val & BMCR_RESET),
  1783. 50000, 600000, true);
  1784. }
  1785. }
  1786. static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
  1787. {
  1788. /* Adjust EEE LED frequency */
  1789. if (tp->mac_version != RTL_GIGA_MAC_VER_38)
  1790. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  1791. rtl_eri_set_bits(tp, 0x1b0, 0x0003);
  1792. }
  1793. static void rtl8125a_config_eee_mac(struct rtl8169_private *tp)
  1794. {
  1795. r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
  1796. r8168_mac_ocp_modify(tp, 0xeb62, 0, BIT(2) | BIT(1));
  1797. }
  1798. static void rtl8125_set_eee_txidle_timer(struct rtl8169_private *tp)
  1799. {
  1800. RTL_W16(tp, EEE_TXIDLE_TIMER_8125, tp->dev->mtu + ETH_HLEN + 0x20);
  1801. }
  1802. static void rtl8125b_config_eee_mac(struct rtl8169_private *tp)
  1803. {
  1804. rtl8125_set_eee_txidle_timer(tp);
  1805. r8168_mac_ocp_modify(tp, 0xe040, 0, BIT(1) | BIT(0));
  1806. }
  1807. static void rtl_rar_exgmac_set(struct rtl8169_private *tp, const u8 *addr)
  1808. {
  1809. rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, get_unaligned_le32(addr));
  1810. rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, get_unaligned_le16(addr + 4));
  1811. rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, get_unaligned_le16(addr) << 16);
  1812. rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, get_unaligned_le32(addr + 2));
  1813. }
  1814. u16 rtl8168h_2_get_adc_bias_ioffset(struct rtl8169_private *tp)
  1815. {
  1816. u16 data1, data2, ioffset;
  1817. r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
  1818. data1 = r8168_mac_ocp_read(tp, 0xdd02);
  1819. data2 = r8168_mac_ocp_read(tp, 0xdd00);
  1820. ioffset = (data2 >> 1) & 0x7ff8;
  1821. ioffset |= data2 & 0x0007;
  1822. if (data1 & BIT(7))
  1823. ioffset |= BIT(15);
  1824. return ioffset;
  1825. }
  1826. static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
  1827. {
  1828. set_bit(flag, tp->wk.flags);
  1829. schedule_work(&tp->wk.work);
  1830. }
  1831. static void rtl8169_init_phy(struct rtl8169_private *tp)
  1832. {
  1833. r8169_hw_phy_config(tp, tp->phydev, tp->mac_version);
  1834. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  1835. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  1836. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  1837. /* set undocumented MAC Reg C+CR Offset 0x82h */
  1838. RTL_W8(tp, 0x82, 0x01);
  1839. }
  1840. if (tp->mac_version == RTL_GIGA_MAC_VER_05 &&
  1841. tp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_GIGABYTE &&
  1842. tp->pci_dev->subsystem_device == 0xe000)
  1843. phy_write_paged(tp->phydev, 0x0001, 0x10, 0xf01b);
  1844. /* We may have called phy_speed_down before */
  1845. phy_speed_up(tp->phydev);
  1846. if (rtl_supports_eee(tp))
  1847. rtl_enable_eee(tp);
  1848. genphy_soft_reset(tp->phydev);
  1849. }
  1850. static void rtl_rar_set(struct rtl8169_private *tp, const u8 *addr)
  1851. {
  1852. rtl_unlock_config_regs(tp);
  1853. RTL_W32(tp, MAC4, get_unaligned_le16(addr + 4));
  1854. rtl_pci_commit(tp);
  1855. RTL_W32(tp, MAC0, get_unaligned_le32(addr));
  1856. rtl_pci_commit(tp);
  1857. if (tp->mac_version == RTL_GIGA_MAC_VER_34)
  1858. rtl_rar_exgmac_set(tp, addr);
  1859. rtl_lock_config_regs(tp);
  1860. }
  1861. static int rtl_set_mac_address(struct net_device *dev, void *p)
  1862. {
  1863. struct rtl8169_private *tp = netdev_priv(dev);
  1864. int ret;
  1865. ret = eth_mac_addr(dev, p);
  1866. if (ret)
  1867. return ret;
  1868. rtl_rar_set(tp, dev->dev_addr);
  1869. return 0;
  1870. }
  1871. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  1872. {
  1873. switch (tp->mac_version) {
  1874. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  1875. case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
  1876. RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  1877. break;
  1878. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
  1879. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
  1880. case RTL_GIGA_MAC_VER_38:
  1881. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  1882. break;
  1883. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_53:
  1884. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
  1885. break;
  1886. case RTL_GIGA_MAC_VER_61:
  1887. RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST);
  1888. break;
  1889. case RTL_GIGA_MAC_VER_63:
  1890. RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST |
  1891. RX_PAUSE_SLOT_ON);
  1892. break;
  1893. default:
  1894. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
  1895. break;
  1896. }
  1897. }
  1898. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  1899. {
  1900. tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
  1901. }
  1902. static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
  1903. {
  1904. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  1905. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
  1906. }
  1907. static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
  1908. {
  1909. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  1910. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
  1911. }
  1912. static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
  1913. {
  1914. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  1915. }
  1916. static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
  1917. {
  1918. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  1919. }
  1920. static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
  1921. {
  1922. RTL_W8(tp, MaxTxPacketSize, 0x24);
  1923. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  1924. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
  1925. }
  1926. static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
  1927. {
  1928. RTL_W8(tp, MaxTxPacketSize, 0x3f);
  1929. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  1930. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
  1931. }
  1932. static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
  1933. {
  1934. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
  1935. }
  1936. static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
  1937. {
  1938. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
  1939. }
  1940. static void rtl_jumbo_config(struct rtl8169_private *tp)
  1941. {
  1942. bool jumbo = tp->dev->mtu > ETH_DATA_LEN;
  1943. int readrq = 4096;
  1944. rtl_unlock_config_regs(tp);
  1945. switch (tp->mac_version) {
  1946. case RTL_GIGA_MAC_VER_17:
  1947. if (jumbo) {
  1948. readrq = 512;
  1949. r8168b_1_hw_jumbo_enable(tp);
  1950. } else {
  1951. r8168b_1_hw_jumbo_disable(tp);
  1952. }
  1953. break;
  1954. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
  1955. if (jumbo) {
  1956. readrq = 512;
  1957. r8168c_hw_jumbo_enable(tp);
  1958. } else {
  1959. r8168c_hw_jumbo_disable(tp);
  1960. }
  1961. break;
  1962. case RTL_GIGA_MAC_VER_28:
  1963. if (jumbo)
  1964. r8168dp_hw_jumbo_enable(tp);
  1965. else
  1966. r8168dp_hw_jumbo_disable(tp);
  1967. break;
  1968. case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_33:
  1969. if (jumbo)
  1970. r8168e_hw_jumbo_enable(tp);
  1971. else
  1972. r8168e_hw_jumbo_disable(tp);
  1973. break;
  1974. default:
  1975. break;
  1976. }
  1977. rtl_lock_config_regs(tp);
  1978. if (pci_is_pcie(tp->pci_dev) && tp->supports_gmii)
  1979. pcie_set_readrq(tp->pci_dev, readrq);
  1980. /* Chip doesn't support pause in jumbo mode */
  1981. if (jumbo) {
  1982. linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  1983. tp->phydev->advertising);
  1984. linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  1985. tp->phydev->advertising);
  1986. phy_start_aneg(tp->phydev);
  1987. }
  1988. }
  1989. DECLARE_RTL_COND(rtl_chipcmd_cond)
  1990. {
  1991. return RTL_R8(tp, ChipCmd) & CmdReset;
  1992. }
  1993. static void rtl_hw_reset(struct rtl8169_private *tp)
  1994. {
  1995. RTL_W8(tp, ChipCmd, CmdReset);
  1996. rtl_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
  1997. }
  1998. static void rtl_request_firmware(struct rtl8169_private *tp)
  1999. {
  2000. struct rtl_fw *rtl_fw;
  2001. /* firmware loaded already or no firmware available */
  2002. if (tp->rtl_fw || !tp->fw_name)
  2003. return;
  2004. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  2005. if (!rtl_fw)
  2006. return;
  2007. rtl_fw->phy_write = rtl_writephy;
  2008. rtl_fw->phy_read = rtl_readphy;
  2009. rtl_fw->mac_mcu_write = mac_mcu_write;
  2010. rtl_fw->mac_mcu_read = mac_mcu_read;
  2011. rtl_fw->fw_name = tp->fw_name;
  2012. rtl_fw->dev = tp_to_dev(tp);
  2013. if (rtl_fw_request_firmware(rtl_fw))
  2014. kfree(rtl_fw);
  2015. else
  2016. tp->rtl_fw = rtl_fw;
  2017. }
  2018. static void rtl_rx_close(struct rtl8169_private *tp)
  2019. {
  2020. RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  2021. }
  2022. DECLARE_RTL_COND(rtl_npq_cond)
  2023. {
  2024. return RTL_R8(tp, TxPoll) & NPQ;
  2025. }
  2026. DECLARE_RTL_COND(rtl_txcfg_empty_cond)
  2027. {
  2028. return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
  2029. }
  2030. DECLARE_RTL_COND(rtl_rxtx_empty_cond)
  2031. {
  2032. return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
  2033. }
  2034. DECLARE_RTL_COND(rtl_rxtx_empty_cond_2)
  2035. {
  2036. /* IntrMitigate has new functionality on RTL8125 */
  2037. return (RTL_R16(tp, IntrMitigate) & 0x0103) == 0x0103;
  2038. }
  2039. static void rtl_wait_txrx_fifo_empty(struct rtl8169_private *tp)
  2040. {
  2041. switch (tp->mac_version) {
  2042. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_53:
  2043. rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42);
  2044. rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
  2045. break;
  2046. case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_61:
  2047. rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
  2048. break;
  2049. case RTL_GIGA_MAC_VER_63:
  2050. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  2051. rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42);
  2052. rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond_2, 100, 42);
  2053. break;
  2054. default:
  2055. break;
  2056. }
  2057. }
  2058. static void rtl_disable_rxdvgate(struct rtl8169_private *tp)
  2059. {
  2060. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  2061. }
  2062. static void rtl_enable_rxdvgate(struct rtl8169_private *tp)
  2063. {
  2064. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
  2065. fsleep(2000);
  2066. rtl_wait_txrx_fifo_empty(tp);
  2067. }
  2068. static void rtl_wol_enable_rx(struct rtl8169_private *tp)
  2069. {
  2070. if (tp->mac_version >= RTL_GIGA_MAC_VER_25)
  2071. RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
  2072. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  2073. if (tp->mac_version >= RTL_GIGA_MAC_VER_40)
  2074. rtl_disable_rxdvgate(tp);
  2075. }
  2076. static void rtl_prepare_power_down(struct rtl8169_private *tp)
  2077. {
  2078. if (tp->dash_enabled)
  2079. return;
  2080. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  2081. tp->mac_version == RTL_GIGA_MAC_VER_33)
  2082. rtl_ephy_write(tp, 0x19, 0xff64);
  2083. if (device_may_wakeup(tp_to_dev(tp))) {
  2084. phy_speed_down(tp->phydev, false);
  2085. rtl_wol_enable_rx(tp);
  2086. }
  2087. }
  2088. static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
  2089. {
  2090. u32 val = TX_DMA_BURST << TxDMAShift |
  2091. InterFrameGap << TxInterFrameGapShift;
  2092. if (rtl_is_8168evl_up(tp))
  2093. val |= TXCFG_AUTO_FIFO;
  2094. RTL_W32(tp, TxConfig, val);
  2095. }
  2096. static void rtl_set_rx_max_size(struct rtl8169_private *tp)
  2097. {
  2098. /* Low hurts. Let's disable the filtering. */
  2099. RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
  2100. }
  2101. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
  2102. {
  2103. /*
  2104. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  2105. * register to be written before TxDescAddrLow to work.
  2106. * Switching from MMIO to I/O access fixes the issue as well.
  2107. */
  2108. RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  2109. RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  2110. RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  2111. RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  2112. }
  2113. static void rtl8169_set_magic_reg(struct rtl8169_private *tp)
  2114. {
  2115. u32 val;
  2116. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  2117. val = 0x000fff00;
  2118. else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
  2119. val = 0x00ffff00;
  2120. else
  2121. return;
  2122. if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
  2123. val |= 0xff;
  2124. RTL_W32(tp, 0x7c, val);
  2125. }
  2126. static void rtl_set_rx_mode(struct net_device *dev)
  2127. {
  2128. u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
  2129. /* Multicast hash filter */
  2130. u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
  2131. struct rtl8169_private *tp = netdev_priv(dev);
  2132. u32 tmp;
  2133. if (dev->flags & IFF_PROMISC) {
  2134. rx_mode |= AcceptAllPhys;
  2135. } else if (!(dev->flags & IFF_MULTICAST)) {
  2136. rx_mode &= ~AcceptMulticast;
  2137. } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT ||
  2138. dev->flags & IFF_ALLMULTI ||
  2139. tp->mac_version == RTL_GIGA_MAC_VER_35) {
  2140. /* accept all multicasts */
  2141. } else if (netdev_mc_empty(dev)) {
  2142. rx_mode &= ~AcceptMulticast;
  2143. } else {
  2144. struct netdev_hw_addr *ha;
  2145. mc_filter[1] = mc_filter[0] = 0;
  2146. netdev_for_each_mc_addr(ha, dev) {
  2147. u32 bit_nr = eth_hw_addr_crc(ha) >> 26;
  2148. mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
  2149. }
  2150. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  2151. tmp = mc_filter[0];
  2152. mc_filter[0] = swab32(mc_filter[1]);
  2153. mc_filter[1] = swab32(tmp);
  2154. }
  2155. }
  2156. RTL_W32(tp, MAR0 + 4, mc_filter[1]);
  2157. RTL_W32(tp, MAR0 + 0, mc_filter[0]);
  2158. tmp = RTL_R32(tp, RxConfig);
  2159. RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_OK_MASK) | rx_mode);
  2160. }
  2161. DECLARE_RTL_COND(rtl_csiar_cond)
  2162. {
  2163. return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
  2164. }
  2165. static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
  2166. {
  2167. u32 func = PCI_FUNC(tp->pci_dev->devfn);
  2168. RTL_W32(tp, CSIDR, value);
  2169. RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  2170. CSIAR_BYTE_ENABLE | func << 16);
  2171. rtl_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  2172. }
  2173. static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
  2174. {
  2175. u32 func = PCI_FUNC(tp->pci_dev->devfn);
  2176. RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
  2177. CSIAR_BYTE_ENABLE);
  2178. return rtl_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  2179. RTL_R32(tp, CSIDR) : ~0;
  2180. }
  2181. static void rtl_set_aspm_entry_latency(struct rtl8169_private *tp, u8 val)
  2182. {
  2183. struct pci_dev *pdev = tp->pci_dev;
  2184. u32 csi;
  2185. /* According to Realtek the value at config space address 0x070f
  2186. * controls the L0s/L1 entrance latency. We try standard ECAM access
  2187. * first and if it fails fall back to CSI.
  2188. * bit 0..2: L0: 0 = 1us, 1 = 2us .. 6 = 7us, 7 = 7us (no typo)
  2189. * bit 3..5: L1: 0 = 1us, 1 = 2us .. 6 = 64us, 7 = 64us
  2190. */
  2191. if (pdev->cfg_size > 0x070f &&
  2192. pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
  2193. return;
  2194. netdev_notice_once(tp->dev,
  2195. "No native access to PCI extended config space, falling back to CSI\n");
  2196. csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
  2197. rtl_csi_write(tp, 0x070c, csi | val << 24);
  2198. }
  2199. static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
  2200. {
  2201. /* L0 7us, L1 16us */
  2202. rtl_set_aspm_entry_latency(tp, 0x27);
  2203. }
  2204. struct ephy_info {
  2205. unsigned int offset;
  2206. u16 mask;
  2207. u16 bits;
  2208. };
  2209. static void __rtl_ephy_init(struct rtl8169_private *tp,
  2210. const struct ephy_info *e, int len)
  2211. {
  2212. u16 w;
  2213. while (len-- > 0) {
  2214. w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
  2215. rtl_ephy_write(tp, e->offset, w);
  2216. e++;
  2217. }
  2218. }
  2219. #define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
  2220. static void rtl_disable_clock_request(struct rtl8169_private *tp)
  2221. {
  2222. pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
  2223. PCI_EXP_LNKCTL_CLKREQ_EN);
  2224. }
  2225. static void rtl_enable_clock_request(struct rtl8169_private *tp)
  2226. {
  2227. pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
  2228. PCI_EXP_LNKCTL_CLKREQ_EN);
  2229. }
  2230. static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
  2231. {
  2232. /* work around an issue when PCI reset occurs during L2/L3 state */
  2233. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
  2234. }
  2235. static void rtl_enable_exit_l1(struct rtl8169_private *tp)
  2236. {
  2237. /* Bits control which events trigger ASPM L1 exit:
  2238. * Bit 12: rxdv
  2239. * Bit 11: ltr_msg
  2240. * Bit 10: txdma_poll
  2241. * Bit 9: xadm
  2242. * Bit 8: pktavi
  2243. * Bit 7: txpla
  2244. */
  2245. switch (tp->mac_version) {
  2246. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
  2247. rtl_eri_set_bits(tp, 0xd4, 0x1f00);
  2248. break;
  2249. case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_38:
  2250. rtl_eri_set_bits(tp, 0xd4, 0x0c00);
  2251. break;
  2252. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_63:
  2253. r8168_mac_ocp_modify(tp, 0xc0ac, 0, 0x1f80);
  2254. break;
  2255. default:
  2256. break;
  2257. }
  2258. }
  2259. static void rtl_disable_exit_l1(struct rtl8169_private *tp)
  2260. {
  2261. switch (tp->mac_version) {
  2262. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  2263. rtl_eri_clear_bits(tp, 0xd4, 0x1f00);
  2264. break;
  2265. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_63:
  2266. r8168_mac_ocp_modify(tp, 0xc0ac, 0x1f80, 0);
  2267. break;
  2268. default:
  2269. break;
  2270. }
  2271. }
  2272. static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
  2273. {
  2274. /* Don't enable ASPM in the chip if OS can't control ASPM */
  2275. if (enable && tp->aspm_manageable) {
  2276. RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
  2277. RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
  2278. switch (tp->mac_version) {
  2279. case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48:
  2280. case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_63:
  2281. /* reset ephy tx/rx disable timer */
  2282. r8168_mac_ocp_modify(tp, 0xe094, 0xff00, 0);
  2283. /* chip can trigger L1.2 */
  2284. r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, BIT(2));
  2285. break;
  2286. default:
  2287. break;
  2288. }
  2289. } else {
  2290. switch (tp->mac_version) {
  2291. case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48:
  2292. case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_63:
  2293. r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, 0);
  2294. break;
  2295. default:
  2296. break;
  2297. }
  2298. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
  2299. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
  2300. }
  2301. udelay(10);
  2302. }
  2303. static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
  2304. u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
  2305. {
  2306. /* Usage of dynamic vs. static FIFO is controlled by bit
  2307. * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
  2308. */
  2309. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
  2310. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
  2311. }
  2312. static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
  2313. u8 low, u8 high)
  2314. {
  2315. /* FIFO thresholds for pause flow control */
  2316. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
  2317. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
  2318. }
  2319. static void rtl_hw_start_8168b(struct rtl8169_private *tp)
  2320. {
  2321. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2322. }
  2323. static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
  2324. {
  2325. RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
  2326. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2327. rtl_disable_clock_request(tp);
  2328. }
  2329. static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
  2330. {
  2331. static const struct ephy_info e_info_8168cp[] = {
  2332. { 0x01, 0, 0x0001 },
  2333. { 0x02, 0x0800, 0x1000 },
  2334. { 0x03, 0, 0x0042 },
  2335. { 0x06, 0x0080, 0x0000 },
  2336. { 0x07, 0, 0x2000 }
  2337. };
  2338. rtl_set_def_aspm_entry_latency(tp);
  2339. rtl_ephy_init(tp, e_info_8168cp);
  2340. __rtl_hw_start_8168cp(tp);
  2341. }
  2342. static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
  2343. {
  2344. rtl_set_def_aspm_entry_latency(tp);
  2345. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2346. }
  2347. static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
  2348. {
  2349. rtl_set_def_aspm_entry_latency(tp);
  2350. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2351. /* Magic. */
  2352. RTL_W8(tp, DBG_REG, 0x20);
  2353. }
  2354. static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
  2355. {
  2356. static const struct ephy_info e_info_8168c_1[] = {
  2357. { 0x02, 0x0800, 0x1000 },
  2358. { 0x03, 0, 0x0002 },
  2359. { 0x06, 0x0080, 0x0000 }
  2360. };
  2361. rtl_set_def_aspm_entry_latency(tp);
  2362. RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  2363. rtl_ephy_init(tp, e_info_8168c_1);
  2364. __rtl_hw_start_8168cp(tp);
  2365. }
  2366. static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
  2367. {
  2368. static const struct ephy_info e_info_8168c_2[] = {
  2369. { 0x01, 0, 0x0001 },
  2370. { 0x03, 0x0400, 0x0020 }
  2371. };
  2372. rtl_set_def_aspm_entry_latency(tp);
  2373. rtl_ephy_init(tp, e_info_8168c_2);
  2374. __rtl_hw_start_8168cp(tp);
  2375. }
  2376. static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
  2377. {
  2378. rtl_set_def_aspm_entry_latency(tp);
  2379. __rtl_hw_start_8168cp(tp);
  2380. }
  2381. static void rtl_hw_start_8168d(struct rtl8169_private *tp)
  2382. {
  2383. rtl_set_def_aspm_entry_latency(tp);
  2384. rtl_disable_clock_request(tp);
  2385. }
  2386. static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
  2387. {
  2388. static const struct ephy_info e_info_8168d_4[] = {
  2389. { 0x0b, 0x0000, 0x0048 },
  2390. { 0x19, 0x0020, 0x0050 },
  2391. { 0x0c, 0x0100, 0x0020 },
  2392. { 0x10, 0x0004, 0x0000 },
  2393. };
  2394. rtl_set_def_aspm_entry_latency(tp);
  2395. rtl_ephy_init(tp, e_info_8168d_4);
  2396. rtl_enable_clock_request(tp);
  2397. }
  2398. static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
  2399. {
  2400. static const struct ephy_info e_info_8168e_1[] = {
  2401. { 0x00, 0x0200, 0x0100 },
  2402. { 0x00, 0x0000, 0x0004 },
  2403. { 0x06, 0x0002, 0x0001 },
  2404. { 0x06, 0x0000, 0x0030 },
  2405. { 0x07, 0x0000, 0x2000 },
  2406. { 0x00, 0x0000, 0x0020 },
  2407. { 0x03, 0x5800, 0x2000 },
  2408. { 0x03, 0x0000, 0x0001 },
  2409. { 0x01, 0x0800, 0x1000 },
  2410. { 0x07, 0x0000, 0x4000 },
  2411. { 0x1e, 0x0000, 0x2000 },
  2412. { 0x19, 0xffff, 0xfe6c },
  2413. { 0x0a, 0x0000, 0x0040 }
  2414. };
  2415. rtl_set_def_aspm_entry_latency(tp);
  2416. rtl_ephy_init(tp, e_info_8168e_1);
  2417. rtl_disable_clock_request(tp);
  2418. /* Reset tx FIFO pointer */
  2419. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
  2420. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
  2421. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  2422. }
  2423. static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
  2424. {
  2425. static const struct ephy_info e_info_8168e_2[] = {
  2426. { 0x09, 0x0000, 0x0080 },
  2427. { 0x19, 0x0000, 0x0224 },
  2428. { 0x00, 0x0000, 0x0004 },
  2429. { 0x0c, 0x3df0, 0x0200 },
  2430. };
  2431. rtl_set_def_aspm_entry_latency(tp);
  2432. rtl_ephy_init(tp, e_info_8168e_2);
  2433. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2434. rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000);
  2435. rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
  2436. rtl_eri_set_bits(tp, 0x1d0, BIT(1));
  2437. rtl_reset_packet_filter(tp);
  2438. rtl_eri_set_bits(tp, 0x1b0, BIT(4));
  2439. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
  2440. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
  2441. rtl_disable_clock_request(tp);
  2442. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  2443. rtl8168_config_eee_mac(tp);
  2444. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  2445. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
  2446. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  2447. rtl_hw_aspm_clkreq_enable(tp, true);
  2448. }
  2449. static void rtl_hw_start_8168f(struct rtl8169_private *tp)
  2450. {
  2451. rtl_set_def_aspm_entry_latency(tp);
  2452. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2453. rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000);
  2454. rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
  2455. rtl_reset_packet_filter(tp);
  2456. rtl_eri_set_bits(tp, 0x1b0, BIT(4));
  2457. rtl_eri_set_bits(tp, 0x1d0, BIT(4) | BIT(1));
  2458. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
  2459. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
  2460. rtl_disable_clock_request(tp);
  2461. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  2462. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  2463. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
  2464. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  2465. rtl8168_config_eee_mac(tp);
  2466. }
  2467. static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
  2468. {
  2469. static const struct ephy_info e_info_8168f_1[] = {
  2470. { 0x06, 0x00c0, 0x0020 },
  2471. { 0x08, 0x0001, 0x0002 },
  2472. { 0x09, 0x0000, 0x0080 },
  2473. { 0x19, 0x0000, 0x0224 },
  2474. { 0x00, 0x0000, 0x0008 },
  2475. { 0x0c, 0x3df0, 0x0200 },
  2476. };
  2477. rtl_hw_start_8168f(tp);
  2478. rtl_ephy_init(tp, e_info_8168f_1);
  2479. }
  2480. static void rtl_hw_start_8411(struct rtl8169_private *tp)
  2481. {
  2482. static const struct ephy_info e_info_8168f_1[] = {
  2483. { 0x06, 0x00c0, 0x0020 },
  2484. { 0x0f, 0xffff, 0x5200 },
  2485. { 0x19, 0x0000, 0x0224 },
  2486. { 0x00, 0x0000, 0x0008 },
  2487. { 0x0c, 0x3df0, 0x0200 },
  2488. };
  2489. rtl_hw_start_8168f(tp);
  2490. rtl_pcie_state_l2l3_disable(tp);
  2491. rtl_ephy_init(tp, e_info_8168f_1);
  2492. }
  2493. static void rtl_hw_start_8168g(struct rtl8169_private *tp)
  2494. {
  2495. rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
  2496. rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
  2497. rtl_set_def_aspm_entry_latency(tp);
  2498. rtl_reset_packet_filter(tp);
  2499. rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
  2500. rtl_disable_rxdvgate(tp);
  2501. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2502. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
  2503. rtl8168_config_eee_mac(tp);
  2504. rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06);
  2505. rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
  2506. rtl_pcie_state_l2l3_disable(tp);
  2507. }
  2508. static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
  2509. {
  2510. static const struct ephy_info e_info_8168g_1[] = {
  2511. { 0x00, 0x0008, 0x0000 },
  2512. { 0x0c, 0x3ff0, 0x0820 },
  2513. { 0x1e, 0x0000, 0x0001 },
  2514. { 0x19, 0x8000, 0x0000 }
  2515. };
  2516. rtl_hw_start_8168g(tp);
  2517. /* disable aspm and clock request before access ephy */
  2518. rtl_hw_aspm_clkreq_enable(tp, false);
  2519. rtl_ephy_init(tp, e_info_8168g_1);
  2520. rtl_hw_aspm_clkreq_enable(tp, true);
  2521. }
  2522. static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
  2523. {
  2524. static const struct ephy_info e_info_8168g_2[] = {
  2525. { 0x00, 0x0008, 0x0000 },
  2526. { 0x0c, 0x3ff0, 0x0820 },
  2527. { 0x19, 0xffff, 0x7c00 },
  2528. { 0x1e, 0xffff, 0x20eb },
  2529. { 0x0d, 0xffff, 0x1666 },
  2530. { 0x00, 0xffff, 0x10a3 },
  2531. { 0x06, 0xffff, 0xf050 },
  2532. { 0x04, 0x0000, 0x0010 },
  2533. { 0x1d, 0x4000, 0x0000 },
  2534. };
  2535. rtl_hw_start_8168g(tp);
  2536. /* disable aspm and clock request before access ephy */
  2537. rtl_hw_aspm_clkreq_enable(tp, false);
  2538. rtl_ephy_init(tp, e_info_8168g_2);
  2539. }
  2540. static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
  2541. {
  2542. static const struct ephy_info e_info_8411_2[] = {
  2543. { 0x00, 0x0008, 0x0000 },
  2544. { 0x0c, 0x37d0, 0x0820 },
  2545. { 0x1e, 0x0000, 0x0001 },
  2546. { 0x19, 0x8021, 0x0000 },
  2547. { 0x1e, 0x0000, 0x2000 },
  2548. { 0x0d, 0x0100, 0x0200 },
  2549. { 0x00, 0x0000, 0x0080 },
  2550. { 0x06, 0x0000, 0x0010 },
  2551. { 0x04, 0x0000, 0x0010 },
  2552. { 0x1d, 0x0000, 0x4000 },
  2553. };
  2554. rtl_hw_start_8168g(tp);
  2555. /* disable aspm and clock request before access ephy */
  2556. rtl_hw_aspm_clkreq_enable(tp, false);
  2557. rtl_ephy_init(tp, e_info_8411_2);
  2558. /* The following Realtek-provided magic fixes an issue with the RX unit
  2559. * getting confused after the PHY having been powered-down.
  2560. */
  2561. r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
  2562. r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
  2563. r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
  2564. r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
  2565. r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
  2566. r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
  2567. r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
  2568. r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
  2569. mdelay(3);
  2570. r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
  2571. r8168_mac_ocp_write(tp, 0xF800, 0xE008);
  2572. r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
  2573. r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
  2574. r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
  2575. r8168_mac_ocp_write(tp, 0xF808, 0xE027);
  2576. r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
  2577. r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
  2578. r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
  2579. r8168_mac_ocp_write(tp, 0xF810, 0xC602);
  2580. r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
  2581. r8168_mac_ocp_write(tp, 0xF814, 0x0000);
  2582. r8168_mac_ocp_write(tp, 0xF816, 0xC502);
  2583. r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
  2584. r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
  2585. r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
  2586. r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
  2587. r8168_mac_ocp_write(tp, 0xF820, 0x080A);
  2588. r8168_mac_ocp_write(tp, 0xF822, 0x6420);
  2589. r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
  2590. r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
  2591. r8168_mac_ocp_write(tp, 0xF828, 0xC516);
  2592. r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
  2593. r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
  2594. r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
  2595. r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
  2596. r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
  2597. r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
  2598. r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
  2599. r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
  2600. r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
  2601. r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
  2602. r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
  2603. r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
  2604. r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
  2605. r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
  2606. r8168_mac_ocp_write(tp, 0xF846, 0xC404);
  2607. r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
  2608. r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
  2609. r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
  2610. r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
  2611. r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
  2612. r8168_mac_ocp_write(tp, 0xF852, 0xE434);
  2613. r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
  2614. r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
  2615. r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
  2616. r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
  2617. r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
  2618. r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
  2619. r8168_mac_ocp_write(tp, 0xF860, 0xF007);
  2620. r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
  2621. r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
  2622. r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
  2623. r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
  2624. r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
  2625. r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
  2626. r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
  2627. r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
  2628. r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
  2629. r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
  2630. r8168_mac_ocp_write(tp, 0xF876, 0xC516);
  2631. r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
  2632. r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
  2633. r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
  2634. r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
  2635. r8168_mac_ocp_write(tp, 0xF880, 0xC512);
  2636. r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
  2637. r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
  2638. r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
  2639. r8168_mac_ocp_write(tp, 0xF888, 0x483F);
  2640. r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
  2641. r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
  2642. r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
  2643. r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
  2644. r8168_mac_ocp_write(tp, 0xF892, 0xC505);
  2645. r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
  2646. r8168_mac_ocp_write(tp, 0xF896, 0xC502);
  2647. r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
  2648. r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
  2649. r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
  2650. r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
  2651. r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
  2652. r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
  2653. r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
  2654. r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
  2655. r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
  2656. r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
  2657. r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
  2658. r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
  2659. r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
  2660. r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
  2661. r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
  2662. r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
  2663. r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
  2664. r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
  2665. r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
  2666. r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
  2667. r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
  2668. r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
  2669. r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
  2670. r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
  2671. r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
  2672. r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
  2673. r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
  2674. r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
  2675. r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
  2676. r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
  2677. r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
  2678. r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
  2679. r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
  2680. r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
  2681. r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
  2682. r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
  2683. r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
  2684. r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
  2685. r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
  2686. r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
  2687. r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
  2688. r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
  2689. r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
  2690. rtl_hw_aspm_clkreq_enable(tp, true);
  2691. }
  2692. static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
  2693. {
  2694. static const struct ephy_info e_info_8168h_1[] = {
  2695. { 0x1e, 0x0800, 0x0001 },
  2696. { 0x1d, 0x0000, 0x0800 },
  2697. { 0x05, 0xffff, 0x2089 },
  2698. { 0x06, 0xffff, 0x5881 },
  2699. { 0x04, 0xffff, 0x854a },
  2700. { 0x01, 0xffff, 0x068b }
  2701. };
  2702. int rg_saw_cnt;
  2703. /* disable aspm and clock request before access ephy */
  2704. rtl_hw_aspm_clkreq_enable(tp, false);
  2705. rtl_ephy_init(tp, e_info_8168h_1);
  2706. rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
  2707. rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
  2708. rtl_set_def_aspm_entry_latency(tp);
  2709. rtl_reset_packet_filter(tp);
  2710. rtl_eri_set_bits(tp, 0xdc, 0x001c);
  2711. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
  2712. rtl_disable_rxdvgate(tp);
  2713. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2714. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
  2715. rtl8168_config_eee_mac(tp);
  2716. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  2717. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  2718. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  2719. rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
  2720. rtl_pcie_state_l2l3_disable(tp);
  2721. rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
  2722. if (rg_saw_cnt > 0) {
  2723. u16 sw_cnt_1ms_ini;
  2724. sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
  2725. sw_cnt_1ms_ini &= 0x0fff;
  2726. r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
  2727. }
  2728. r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
  2729. r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
  2730. r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
  2731. r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
  2732. r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
  2733. r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
  2734. r8168_mac_ocp_write(tp, 0xc094, 0x0000);
  2735. r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
  2736. rtl_hw_aspm_clkreq_enable(tp, true);
  2737. }
  2738. static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
  2739. {
  2740. rtl8168ep_stop_cmac(tp);
  2741. rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
  2742. rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
  2743. rtl_set_def_aspm_entry_latency(tp);
  2744. rtl_reset_packet_filter(tp);
  2745. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
  2746. rtl_disable_rxdvgate(tp);
  2747. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2748. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
  2749. rtl8168_config_eee_mac(tp);
  2750. rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06);
  2751. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  2752. rtl_pcie_state_l2l3_disable(tp);
  2753. }
  2754. static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
  2755. {
  2756. static const struct ephy_info e_info_8168ep_3[] = {
  2757. { 0x00, 0x0000, 0x0080 },
  2758. { 0x0d, 0x0100, 0x0200 },
  2759. { 0x19, 0x8021, 0x0000 },
  2760. { 0x1e, 0x0000, 0x2000 },
  2761. };
  2762. /* disable aspm and clock request before access ephy */
  2763. rtl_hw_aspm_clkreq_enable(tp, false);
  2764. rtl_ephy_init(tp, e_info_8168ep_3);
  2765. rtl_hw_start_8168ep(tp);
  2766. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  2767. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  2768. r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
  2769. r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
  2770. r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
  2771. rtl_hw_aspm_clkreq_enable(tp, true);
  2772. }
  2773. static void rtl_hw_start_8117(struct rtl8169_private *tp)
  2774. {
  2775. static const struct ephy_info e_info_8117[] = {
  2776. { 0x19, 0x0040, 0x1100 },
  2777. { 0x59, 0x0040, 0x1100 },
  2778. };
  2779. int rg_saw_cnt;
  2780. rtl8168ep_stop_cmac(tp);
  2781. /* disable aspm and clock request before access ephy */
  2782. rtl_hw_aspm_clkreq_enable(tp, false);
  2783. rtl_ephy_init(tp, e_info_8117);
  2784. rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
  2785. rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
  2786. rtl_set_def_aspm_entry_latency(tp);
  2787. rtl_reset_packet_filter(tp);
  2788. rtl_eri_set_bits(tp, 0xd4, 0x0010);
  2789. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
  2790. rtl_disable_rxdvgate(tp);
  2791. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2792. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
  2793. rtl8168_config_eee_mac(tp);
  2794. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  2795. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  2796. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  2797. rtl_eri_clear_bits(tp, 0x1b0, BIT(12));
  2798. rtl_pcie_state_l2l3_disable(tp);
  2799. rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff;
  2800. if (rg_saw_cnt > 0) {
  2801. u16 sw_cnt_1ms_ini;
  2802. sw_cnt_1ms_ini = (16000000 / rg_saw_cnt) & 0x0fff;
  2803. r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
  2804. }
  2805. r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
  2806. r8168_mac_ocp_write(tp, 0xea80, 0x0003);
  2807. r8168_mac_ocp_modify(tp, 0xe052, 0x0000, 0x0009);
  2808. r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
  2809. r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
  2810. r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
  2811. r8168_mac_ocp_write(tp, 0xc094, 0x0000);
  2812. r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
  2813. /* firmware is for MAC only */
  2814. r8169_apply_firmware(tp);
  2815. rtl_hw_aspm_clkreq_enable(tp, true);
  2816. }
  2817. static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
  2818. {
  2819. static const struct ephy_info e_info_8102e_1[] = {
  2820. { 0x01, 0, 0x6e65 },
  2821. { 0x02, 0, 0x091f },
  2822. { 0x03, 0, 0xc2f9 },
  2823. { 0x06, 0, 0xafb5 },
  2824. { 0x07, 0, 0x0e00 },
  2825. { 0x19, 0, 0xec80 },
  2826. { 0x01, 0, 0x2e65 },
  2827. { 0x01, 0, 0x6e65 }
  2828. };
  2829. u8 cfg1;
  2830. rtl_set_def_aspm_entry_latency(tp);
  2831. RTL_W8(tp, DBG_REG, FIX_NAK_1);
  2832. RTL_W8(tp, Config1,
  2833. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  2834. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2835. cfg1 = RTL_R8(tp, Config1);
  2836. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  2837. RTL_W8(tp, Config1, cfg1 & ~LEDS0);
  2838. rtl_ephy_init(tp, e_info_8102e_1);
  2839. }
  2840. static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
  2841. {
  2842. rtl_set_def_aspm_entry_latency(tp);
  2843. RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
  2844. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2845. }
  2846. static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
  2847. {
  2848. rtl_hw_start_8102e_2(tp);
  2849. rtl_ephy_write(tp, 0x03, 0xc2f9);
  2850. }
  2851. static void rtl_hw_start_8401(struct rtl8169_private *tp)
  2852. {
  2853. static const struct ephy_info e_info_8401[] = {
  2854. { 0x01, 0xffff, 0x6fe5 },
  2855. { 0x03, 0xffff, 0x0599 },
  2856. { 0x06, 0xffff, 0xaf25 },
  2857. { 0x07, 0xffff, 0x8e68 },
  2858. };
  2859. rtl_ephy_init(tp, e_info_8401);
  2860. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  2861. }
  2862. static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
  2863. {
  2864. static const struct ephy_info e_info_8105e_1[] = {
  2865. { 0x07, 0, 0x4000 },
  2866. { 0x19, 0, 0x0200 },
  2867. { 0x19, 0, 0x0020 },
  2868. { 0x1e, 0, 0x2000 },
  2869. { 0x03, 0, 0x0001 },
  2870. { 0x19, 0, 0x0100 },
  2871. { 0x19, 0, 0x0004 },
  2872. { 0x0a, 0, 0x0020 }
  2873. };
  2874. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  2875. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  2876. /* Disable Early Tally Counter */
  2877. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
  2878. RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
  2879. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  2880. rtl_ephy_init(tp, e_info_8105e_1);
  2881. rtl_pcie_state_l2l3_disable(tp);
  2882. }
  2883. static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
  2884. {
  2885. rtl_hw_start_8105e_1(tp);
  2886. rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
  2887. }
  2888. static void rtl_hw_start_8402(struct rtl8169_private *tp)
  2889. {
  2890. static const struct ephy_info e_info_8402[] = {
  2891. { 0x19, 0xffff, 0xff64 },
  2892. { 0x1e, 0, 0x4000 }
  2893. };
  2894. rtl_set_def_aspm_entry_latency(tp);
  2895. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  2896. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  2897. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  2898. rtl_ephy_init(tp, e_info_8402);
  2899. rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
  2900. rtl_reset_packet_filter(tp);
  2901. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
  2902. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
  2903. rtl_w0w1_eri(tp, 0x0d4, 0x0e00, 0xff00);
  2904. /* disable EEE */
  2905. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
  2906. rtl_pcie_state_l2l3_disable(tp);
  2907. }
  2908. static void rtl_hw_start_8106(struct rtl8169_private *tp)
  2909. {
  2910. rtl_hw_aspm_clkreq_enable(tp, false);
  2911. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  2912. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  2913. RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
  2914. RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
  2915. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  2916. /* L0 7us, L1 32us - needed to avoid issues with link-up detection */
  2917. rtl_set_aspm_entry_latency(tp, 0x2f);
  2918. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
  2919. /* disable EEE */
  2920. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
  2921. rtl_pcie_state_l2l3_disable(tp);
  2922. rtl_hw_aspm_clkreq_enable(tp, true);
  2923. }
  2924. DECLARE_RTL_COND(rtl_mac_ocp_e00e_cond)
  2925. {
  2926. return r8168_mac_ocp_read(tp, 0xe00e) & BIT(13);
  2927. }
  2928. static void rtl_hw_start_8125_common(struct rtl8169_private *tp)
  2929. {
  2930. rtl_pcie_state_l2l3_disable(tp);
  2931. RTL_W16(tp, 0x382, 0x221b);
  2932. RTL_W8(tp, 0x4500, 0);
  2933. RTL_W16(tp, 0x4800, 0);
  2934. /* disable UPS */
  2935. r8168_mac_ocp_modify(tp, 0xd40a, 0x0010, 0x0000);
  2936. RTL_W8(tp, Config1, RTL_R8(tp, Config1) & ~0x10);
  2937. r8168_mac_ocp_write(tp, 0xc140, 0xffff);
  2938. r8168_mac_ocp_write(tp, 0xc142, 0xffff);
  2939. r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x03a9);
  2940. r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
  2941. r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
  2942. /* disable new tx descriptor format */
  2943. r8168_mac_ocp_modify(tp, 0xeb58, 0x0001, 0x0000);
  2944. if (tp->mac_version == RTL_GIGA_MAC_VER_63)
  2945. r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0200);
  2946. else
  2947. r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0400);
  2948. if (tp->mac_version == RTL_GIGA_MAC_VER_63)
  2949. r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0000);
  2950. else
  2951. r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0020);
  2952. r8168_mac_ocp_modify(tp, 0xc0b4, 0x0000, 0x000c);
  2953. r8168_mac_ocp_modify(tp, 0xeb6a, 0x00ff, 0x0033);
  2954. r8168_mac_ocp_modify(tp, 0xeb50, 0x03e0, 0x0040);
  2955. r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0030);
  2956. r8168_mac_ocp_modify(tp, 0xe040, 0x1000, 0x0000);
  2957. r8168_mac_ocp_modify(tp, 0xea1c, 0x0003, 0x0001);
  2958. r8168_mac_ocp_modify(tp, 0xe0c0, 0x4f0f, 0x4403);
  2959. r8168_mac_ocp_modify(tp, 0xe052, 0x0080, 0x0068);
  2960. r8168_mac_ocp_modify(tp, 0xd430, 0x0fff, 0x047f);
  2961. r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000);
  2962. r8168_mac_ocp_modify(tp, 0xeb54, 0x0000, 0x0001);
  2963. udelay(1);
  2964. r8168_mac_ocp_modify(tp, 0xeb54, 0x0001, 0x0000);
  2965. RTL_W16(tp, 0x1880, RTL_R16(tp, 0x1880) & ~0x0030);
  2966. r8168_mac_ocp_write(tp, 0xe098, 0xc302);
  2967. rtl_loop_wait_low(tp, &rtl_mac_ocp_e00e_cond, 1000, 10);
  2968. if (tp->mac_version == RTL_GIGA_MAC_VER_63)
  2969. rtl8125b_config_eee_mac(tp);
  2970. else
  2971. rtl8125a_config_eee_mac(tp);
  2972. rtl_disable_rxdvgate(tp);
  2973. }
  2974. static void rtl_hw_start_8125a_2(struct rtl8169_private *tp)
  2975. {
  2976. static const struct ephy_info e_info_8125a_2[] = {
  2977. { 0x04, 0xffff, 0xd000 },
  2978. { 0x0a, 0xffff, 0x8653 },
  2979. { 0x23, 0xffff, 0xab66 },
  2980. { 0x20, 0xffff, 0x9455 },
  2981. { 0x21, 0xffff, 0x99ff },
  2982. { 0x29, 0xffff, 0xfe04 },
  2983. { 0x44, 0xffff, 0xd000 },
  2984. { 0x4a, 0xffff, 0x8653 },
  2985. { 0x63, 0xffff, 0xab66 },
  2986. { 0x60, 0xffff, 0x9455 },
  2987. { 0x61, 0xffff, 0x99ff },
  2988. { 0x69, 0xffff, 0xfe04 },
  2989. };
  2990. rtl_set_def_aspm_entry_latency(tp);
  2991. /* disable aspm and clock request before access ephy */
  2992. rtl_hw_aspm_clkreq_enable(tp, false);
  2993. rtl_ephy_init(tp, e_info_8125a_2);
  2994. rtl_hw_start_8125_common(tp);
  2995. rtl_hw_aspm_clkreq_enable(tp, true);
  2996. }
  2997. static void rtl_hw_start_8125b(struct rtl8169_private *tp)
  2998. {
  2999. static const struct ephy_info e_info_8125b[] = {
  3000. { 0x0b, 0xffff, 0xa908 },
  3001. { 0x1e, 0xffff, 0x20eb },
  3002. { 0x4b, 0xffff, 0xa908 },
  3003. { 0x5e, 0xffff, 0x20eb },
  3004. { 0x22, 0x0030, 0x0020 },
  3005. { 0x62, 0x0030, 0x0020 },
  3006. };
  3007. rtl_set_def_aspm_entry_latency(tp);
  3008. rtl_hw_aspm_clkreq_enable(tp, false);
  3009. rtl_ephy_init(tp, e_info_8125b);
  3010. rtl_hw_start_8125_common(tp);
  3011. rtl_hw_aspm_clkreq_enable(tp, true);
  3012. }
  3013. static void rtl_hw_config(struct rtl8169_private *tp)
  3014. {
  3015. static const rtl_generic_fct hw_configs[] = {
  3016. [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
  3017. [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
  3018. [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
  3019. [RTL_GIGA_MAC_VER_10] = NULL,
  3020. [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168b,
  3021. [RTL_GIGA_MAC_VER_14] = rtl_hw_start_8401,
  3022. [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168b,
  3023. [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
  3024. [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
  3025. [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
  3026. [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_2,
  3027. [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
  3028. [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
  3029. [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
  3030. [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
  3031. [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
  3032. [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
  3033. [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
  3034. [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
  3035. [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168d,
  3036. [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
  3037. [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
  3038. [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
  3039. [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
  3040. [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
  3041. [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
  3042. [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
  3043. [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
  3044. [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
  3045. [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
  3046. [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
  3047. [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
  3048. [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
  3049. [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
  3050. [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
  3051. [RTL_GIGA_MAC_VER_52] = rtl_hw_start_8117,
  3052. [RTL_GIGA_MAC_VER_53] = rtl_hw_start_8117,
  3053. [RTL_GIGA_MAC_VER_61] = rtl_hw_start_8125a_2,
  3054. [RTL_GIGA_MAC_VER_63] = rtl_hw_start_8125b,
  3055. };
  3056. if (hw_configs[tp->mac_version])
  3057. hw_configs[tp->mac_version](tp);
  3058. }
  3059. static void rtl_hw_start_8125(struct rtl8169_private *tp)
  3060. {
  3061. int i;
  3062. /* disable interrupt coalescing */
  3063. for (i = 0xa00; i < 0xb00; i += 4)
  3064. RTL_W32(tp, i, 0);
  3065. rtl_hw_config(tp);
  3066. }
  3067. static void rtl_hw_start_8168(struct rtl8169_private *tp)
  3068. {
  3069. if (rtl_is_8168evl_up(tp))
  3070. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  3071. else
  3072. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  3073. rtl_hw_config(tp);
  3074. /* disable interrupt coalescing */
  3075. RTL_W16(tp, IntrMitigate, 0x0000);
  3076. }
  3077. static void rtl_hw_start_8169(struct rtl8169_private *tp)
  3078. {
  3079. RTL_W8(tp, EarlyTxThres, NoEarlyTx);
  3080. tp->cp_cmd |= PCIMulRW;
  3081. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3082. tp->mac_version == RTL_GIGA_MAC_VER_03)
  3083. tp->cp_cmd |= EnAnaPLL;
  3084. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  3085. rtl8169_set_magic_reg(tp);
  3086. /* disable interrupt coalescing */
  3087. RTL_W16(tp, IntrMitigate, 0x0000);
  3088. }
  3089. static void rtl_hw_start(struct rtl8169_private *tp)
  3090. {
  3091. rtl_unlock_config_regs(tp);
  3092. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  3093. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  3094. rtl_hw_start_8169(tp);
  3095. else if (rtl_is_8125(tp))
  3096. rtl_hw_start_8125(tp);
  3097. else
  3098. rtl_hw_start_8168(tp);
  3099. rtl_enable_exit_l1(tp);
  3100. rtl_set_rx_max_size(tp);
  3101. rtl_set_rx_tx_desc_registers(tp);
  3102. rtl_lock_config_regs(tp);
  3103. rtl_jumbo_config(tp);
  3104. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  3105. rtl_pci_commit(tp);
  3106. RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
  3107. rtl_init_rxcfg(tp);
  3108. rtl_set_tx_config_registers(tp);
  3109. rtl_set_rx_config_features(tp, tp->dev->features);
  3110. rtl_set_rx_mode(tp->dev);
  3111. rtl_irq_enable(tp);
  3112. }
  3113. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  3114. {
  3115. struct rtl8169_private *tp = netdev_priv(dev);
  3116. dev->mtu = new_mtu;
  3117. netdev_update_features(dev);
  3118. rtl_jumbo_config(tp);
  3119. switch (tp->mac_version) {
  3120. case RTL_GIGA_MAC_VER_61:
  3121. case RTL_GIGA_MAC_VER_63:
  3122. rtl8125_set_eee_txidle_timer(tp);
  3123. break;
  3124. default:
  3125. break;
  3126. }
  3127. return 0;
  3128. }
  3129. static void rtl8169_mark_to_asic(struct RxDesc *desc)
  3130. {
  3131. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  3132. desc->opts2 = 0;
  3133. /* Force memory writes to complete before releasing descriptor */
  3134. dma_wmb();
  3135. WRITE_ONCE(desc->opts1, cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE));
  3136. }
  3137. static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  3138. struct RxDesc *desc)
  3139. {
  3140. struct device *d = tp_to_dev(tp);
  3141. int node = dev_to_node(d);
  3142. dma_addr_t mapping;
  3143. struct page *data;
  3144. data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
  3145. if (!data)
  3146. return NULL;
  3147. mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
  3148. if (unlikely(dma_mapping_error(d, mapping))) {
  3149. netdev_err(tp->dev, "Failed to map RX DMA!\n");
  3150. __free_pages(data, get_order(R8169_RX_BUF_SIZE));
  3151. return NULL;
  3152. }
  3153. desc->addr = cpu_to_le64(mapping);
  3154. rtl8169_mark_to_asic(desc);
  3155. return data;
  3156. }
  3157. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  3158. {
  3159. int i;
  3160. for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
  3161. dma_unmap_page(tp_to_dev(tp),
  3162. le64_to_cpu(tp->RxDescArray[i].addr),
  3163. R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
  3164. __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
  3165. tp->Rx_databuff[i] = NULL;
  3166. tp->RxDescArray[i].addr = 0;
  3167. tp->RxDescArray[i].opts1 = 0;
  3168. }
  3169. }
  3170. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  3171. {
  3172. int i;
  3173. for (i = 0; i < NUM_RX_DESC; i++) {
  3174. struct page *data;
  3175. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  3176. if (!data) {
  3177. rtl8169_rx_clear(tp);
  3178. return -ENOMEM;
  3179. }
  3180. tp->Rx_databuff[i] = data;
  3181. }
  3182. /* mark as last descriptor in the ring */
  3183. tp->RxDescArray[NUM_RX_DESC - 1].opts1 |= cpu_to_le32(RingEnd);
  3184. return 0;
  3185. }
  3186. static int rtl8169_init_ring(struct rtl8169_private *tp)
  3187. {
  3188. rtl8169_init_ring_indexes(tp);
  3189. memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
  3190. memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
  3191. return rtl8169_rx_fill(tp);
  3192. }
  3193. static void rtl8169_unmap_tx_skb(struct rtl8169_private *tp, unsigned int entry)
  3194. {
  3195. struct ring_info *tx_skb = tp->tx_skb + entry;
  3196. struct TxDesc *desc = tp->TxDescArray + entry;
  3197. dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), tx_skb->len,
  3198. DMA_TO_DEVICE);
  3199. memset(desc, 0, sizeof(*desc));
  3200. memset(tx_skb, 0, sizeof(*tx_skb));
  3201. }
  3202. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  3203. unsigned int n)
  3204. {
  3205. unsigned int i;
  3206. for (i = 0; i < n; i++) {
  3207. unsigned int entry = (start + i) % NUM_TX_DESC;
  3208. struct ring_info *tx_skb = tp->tx_skb + entry;
  3209. unsigned int len = tx_skb->len;
  3210. if (len) {
  3211. struct sk_buff *skb = tx_skb->skb;
  3212. rtl8169_unmap_tx_skb(tp, entry);
  3213. if (skb)
  3214. dev_consume_skb_any(skb);
  3215. }
  3216. }
  3217. }
  3218. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  3219. {
  3220. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  3221. netdev_reset_queue(tp->dev);
  3222. }
  3223. static void rtl8169_cleanup(struct rtl8169_private *tp)
  3224. {
  3225. napi_disable(&tp->napi);
  3226. /* Give a racing hard_start_xmit a few cycles to complete. */
  3227. synchronize_net();
  3228. /* Disable interrupts */
  3229. rtl8169_irq_mask_and_ack(tp);
  3230. rtl_rx_close(tp);
  3231. switch (tp->mac_version) {
  3232. case RTL_GIGA_MAC_VER_28:
  3233. case RTL_GIGA_MAC_VER_31:
  3234. rtl_loop_wait_low(tp, &rtl_npq_cond, 20, 2000);
  3235. break;
  3236. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  3237. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  3238. rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
  3239. break;
  3240. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_63:
  3241. rtl_enable_rxdvgate(tp);
  3242. fsleep(2000);
  3243. break;
  3244. default:
  3245. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  3246. fsleep(100);
  3247. break;
  3248. }
  3249. rtl_hw_reset(tp);
  3250. rtl8169_tx_clear(tp);
  3251. rtl8169_init_ring_indexes(tp);
  3252. }
  3253. static void rtl_reset_work(struct rtl8169_private *tp)
  3254. {
  3255. int i;
  3256. netif_stop_queue(tp->dev);
  3257. rtl8169_cleanup(tp);
  3258. for (i = 0; i < NUM_RX_DESC; i++)
  3259. rtl8169_mark_to_asic(tp->RxDescArray + i);
  3260. napi_enable(&tp->napi);
  3261. rtl_hw_start(tp);
  3262. }
  3263. static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue)
  3264. {
  3265. struct rtl8169_private *tp = netdev_priv(dev);
  3266. rtl_schedule_task(tp, RTL_FLAG_TASK_TX_TIMEOUT);
  3267. }
  3268. static int rtl8169_tx_map(struct rtl8169_private *tp, const u32 *opts, u32 len,
  3269. void *addr, unsigned int entry, bool desc_own)
  3270. {
  3271. struct TxDesc *txd = tp->TxDescArray + entry;
  3272. struct device *d = tp_to_dev(tp);
  3273. dma_addr_t mapping;
  3274. u32 opts1;
  3275. int ret;
  3276. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  3277. ret = dma_mapping_error(d, mapping);
  3278. if (unlikely(ret)) {
  3279. if (net_ratelimit())
  3280. netdev_err(tp->dev, "Failed to map TX data!\n");
  3281. return ret;
  3282. }
  3283. txd->addr = cpu_to_le64(mapping);
  3284. txd->opts2 = cpu_to_le32(opts[1]);
  3285. opts1 = opts[0] | len;
  3286. if (entry == NUM_TX_DESC - 1)
  3287. opts1 |= RingEnd;
  3288. if (desc_own)
  3289. opts1 |= DescOwn;
  3290. txd->opts1 = cpu_to_le32(opts1);
  3291. tp->tx_skb[entry].len = len;
  3292. return 0;
  3293. }
  3294. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  3295. const u32 *opts, unsigned int entry)
  3296. {
  3297. struct skb_shared_info *info = skb_shinfo(skb);
  3298. unsigned int cur_frag;
  3299. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  3300. const skb_frag_t *frag = info->frags + cur_frag;
  3301. void *addr = skb_frag_address(frag);
  3302. u32 len = skb_frag_size(frag);
  3303. entry = (entry + 1) % NUM_TX_DESC;
  3304. if (unlikely(rtl8169_tx_map(tp, opts, len, addr, entry, true)))
  3305. goto err_out;
  3306. }
  3307. return 0;
  3308. err_out:
  3309. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  3310. return -EIO;
  3311. }
  3312. static bool rtl_skb_is_udp(struct sk_buff *skb)
  3313. {
  3314. int no = skb_network_offset(skb);
  3315. struct ipv6hdr *i6h, _i6h;
  3316. struct iphdr *ih, _ih;
  3317. switch (vlan_get_protocol(skb)) {
  3318. case htons(ETH_P_IP):
  3319. ih = skb_header_pointer(skb, no, sizeof(_ih), &_ih);
  3320. return ih && ih->protocol == IPPROTO_UDP;
  3321. case htons(ETH_P_IPV6):
  3322. i6h = skb_header_pointer(skb, no, sizeof(_i6h), &_i6h);
  3323. return i6h && i6h->nexthdr == IPPROTO_UDP;
  3324. default:
  3325. return false;
  3326. }
  3327. }
  3328. #define RTL_MIN_PATCH_LEN 47
  3329. /* see rtl8125_get_patch_pad_len() in r8125 vendor driver */
  3330. static unsigned int rtl8125_quirk_udp_padto(struct rtl8169_private *tp,
  3331. struct sk_buff *skb)
  3332. {
  3333. unsigned int padto = 0, len = skb->len;
  3334. if (rtl_is_8125(tp) && len < 128 + RTL_MIN_PATCH_LEN &&
  3335. rtl_skb_is_udp(skb) && skb_transport_header_was_set(skb)) {
  3336. unsigned int trans_data_len = skb_tail_pointer(skb) -
  3337. skb_transport_header(skb);
  3338. if (trans_data_len >= offsetof(struct udphdr, len) &&
  3339. trans_data_len < RTL_MIN_PATCH_LEN) {
  3340. u16 dest = ntohs(udp_hdr(skb)->dest);
  3341. /* dest is a standard PTP port */
  3342. if (dest == 319 || dest == 320)
  3343. padto = len + RTL_MIN_PATCH_LEN - trans_data_len;
  3344. }
  3345. if (trans_data_len < sizeof(struct udphdr))
  3346. padto = max_t(unsigned int, padto,
  3347. len + sizeof(struct udphdr) - trans_data_len);
  3348. }
  3349. return padto;
  3350. }
  3351. static unsigned int rtl_quirk_packet_padto(struct rtl8169_private *tp,
  3352. struct sk_buff *skb)
  3353. {
  3354. unsigned int padto;
  3355. padto = rtl8125_quirk_udp_padto(tp, skb);
  3356. switch (tp->mac_version) {
  3357. case RTL_GIGA_MAC_VER_34:
  3358. case RTL_GIGA_MAC_VER_61:
  3359. case RTL_GIGA_MAC_VER_63:
  3360. padto = max_t(unsigned int, padto, ETH_ZLEN);
  3361. break;
  3362. default:
  3363. break;
  3364. }
  3365. return padto;
  3366. }
  3367. static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
  3368. {
  3369. u32 mss = skb_shinfo(skb)->gso_size;
  3370. if (mss) {
  3371. opts[0] |= TD_LSO;
  3372. opts[0] |= mss << TD0_MSS_SHIFT;
  3373. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3374. const struct iphdr *ip = ip_hdr(skb);
  3375. if (ip->protocol == IPPROTO_TCP)
  3376. opts[0] |= TD0_IP_CS | TD0_TCP_CS;
  3377. else if (ip->protocol == IPPROTO_UDP)
  3378. opts[0] |= TD0_IP_CS | TD0_UDP_CS;
  3379. else
  3380. WARN_ON_ONCE(1);
  3381. }
  3382. }
  3383. static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
  3384. struct sk_buff *skb, u32 *opts)
  3385. {
  3386. struct skb_shared_info *shinfo = skb_shinfo(skb);
  3387. u32 mss = shinfo->gso_size;
  3388. if (mss) {
  3389. if (shinfo->gso_type & SKB_GSO_TCPV4) {
  3390. opts[0] |= TD1_GTSENV4;
  3391. } else if (shinfo->gso_type & SKB_GSO_TCPV6) {
  3392. if (skb_cow_head(skb, 0))
  3393. return false;
  3394. tcp_v6_gso_csum_prep(skb);
  3395. opts[0] |= TD1_GTSENV6;
  3396. } else {
  3397. WARN_ON_ONCE(1);
  3398. }
  3399. opts[0] |= skb_transport_offset(skb) << GTTCPHO_SHIFT;
  3400. opts[1] |= mss << TD1_MSS_SHIFT;
  3401. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3402. u8 ip_protocol;
  3403. switch (vlan_get_protocol(skb)) {
  3404. case htons(ETH_P_IP):
  3405. opts[1] |= TD1_IPv4_CS;
  3406. ip_protocol = ip_hdr(skb)->protocol;
  3407. break;
  3408. case htons(ETH_P_IPV6):
  3409. opts[1] |= TD1_IPv6_CS;
  3410. ip_protocol = ipv6_hdr(skb)->nexthdr;
  3411. break;
  3412. default:
  3413. ip_protocol = IPPROTO_RAW;
  3414. break;
  3415. }
  3416. if (ip_protocol == IPPROTO_TCP)
  3417. opts[1] |= TD1_TCP_CS;
  3418. else if (ip_protocol == IPPROTO_UDP)
  3419. opts[1] |= TD1_UDP_CS;
  3420. else
  3421. WARN_ON_ONCE(1);
  3422. opts[1] |= skb_transport_offset(skb) << TCPHO_SHIFT;
  3423. } else {
  3424. unsigned int padto = rtl_quirk_packet_padto(tp, skb);
  3425. /* skb_padto would free the skb on error */
  3426. return !__skb_put_padto(skb, padto, false);
  3427. }
  3428. return true;
  3429. }
  3430. static bool rtl_tx_slots_avail(struct rtl8169_private *tp)
  3431. {
  3432. unsigned int slots_avail = READ_ONCE(tp->dirty_tx) + NUM_TX_DESC
  3433. - READ_ONCE(tp->cur_tx);
  3434. /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
  3435. return slots_avail > MAX_SKB_FRAGS;
  3436. }
  3437. /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
  3438. static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
  3439. {
  3440. switch (tp->mac_version) {
  3441. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  3442. case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
  3443. return false;
  3444. default:
  3445. return true;
  3446. }
  3447. }
  3448. static void rtl8169_doorbell(struct rtl8169_private *tp)
  3449. {
  3450. if (rtl_is_8125(tp))
  3451. RTL_W16(tp, TxPoll_8125, BIT(0));
  3452. else
  3453. RTL_W8(tp, TxPoll, NPQ);
  3454. }
  3455. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  3456. struct net_device *dev)
  3457. {
  3458. unsigned int frags = skb_shinfo(skb)->nr_frags;
  3459. struct rtl8169_private *tp = netdev_priv(dev);
  3460. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  3461. struct TxDesc *txd_first, *txd_last;
  3462. bool stop_queue, door_bell;
  3463. u32 opts[2];
  3464. if (unlikely(!rtl_tx_slots_avail(tp))) {
  3465. if (net_ratelimit())
  3466. netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
  3467. goto err_stop_0;
  3468. }
  3469. opts[1] = rtl8169_tx_vlan_tag(skb);
  3470. opts[0] = 0;
  3471. if (!rtl_chip_supports_csum_v2(tp))
  3472. rtl8169_tso_csum_v1(skb, opts);
  3473. else if (!rtl8169_tso_csum_v2(tp, skb, opts))
  3474. goto err_dma_0;
  3475. if (unlikely(rtl8169_tx_map(tp, opts, skb_headlen(skb), skb->data,
  3476. entry, false)))
  3477. goto err_dma_0;
  3478. txd_first = tp->TxDescArray + entry;
  3479. if (frags) {
  3480. if (rtl8169_xmit_frags(tp, skb, opts, entry))
  3481. goto err_dma_1;
  3482. entry = (entry + frags) % NUM_TX_DESC;
  3483. }
  3484. txd_last = tp->TxDescArray + entry;
  3485. txd_last->opts1 |= cpu_to_le32(LastFrag);
  3486. tp->tx_skb[entry].skb = skb;
  3487. skb_tx_timestamp(skb);
  3488. /* Force memory writes to complete before releasing descriptor */
  3489. dma_wmb();
  3490. door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
  3491. txd_first->opts1 |= cpu_to_le32(DescOwn | FirstFrag);
  3492. /* rtl_tx needs to see descriptor changes before updated tp->cur_tx */
  3493. smp_wmb();
  3494. WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1);
  3495. stop_queue = !rtl_tx_slots_avail(tp);
  3496. if (unlikely(stop_queue)) {
  3497. /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
  3498. * not miss a ring update when it notices a stopped queue.
  3499. */
  3500. smp_wmb();
  3501. netif_stop_queue(dev);
  3502. /* Sync with rtl_tx:
  3503. * - publish queue status and cur_tx ring index (write barrier)
  3504. * - refresh dirty_tx ring index (read barrier).
  3505. * May the current thread have a pessimistic view of the ring
  3506. * status and forget to wake up queue, a racing rtl_tx thread
  3507. * can't.
  3508. */
  3509. smp_mb__after_atomic();
  3510. if (rtl_tx_slots_avail(tp))
  3511. netif_start_queue(dev);
  3512. door_bell = true;
  3513. }
  3514. if (door_bell)
  3515. rtl8169_doorbell(tp);
  3516. return NETDEV_TX_OK;
  3517. err_dma_1:
  3518. rtl8169_unmap_tx_skb(tp, entry);
  3519. err_dma_0:
  3520. dev_kfree_skb_any(skb);
  3521. dev->stats.tx_dropped++;
  3522. return NETDEV_TX_OK;
  3523. err_stop_0:
  3524. netif_stop_queue(dev);
  3525. dev->stats.tx_dropped++;
  3526. return NETDEV_TX_BUSY;
  3527. }
  3528. static unsigned int rtl_last_frag_len(struct sk_buff *skb)
  3529. {
  3530. struct skb_shared_info *info = skb_shinfo(skb);
  3531. unsigned int nr_frags = info->nr_frags;
  3532. if (!nr_frags)
  3533. return UINT_MAX;
  3534. return skb_frag_size(info->frags + nr_frags - 1);
  3535. }
  3536. /* Workaround for hw issues with TSO on RTL8168evl */
  3537. static netdev_features_t rtl8168evl_fix_tso(struct sk_buff *skb,
  3538. netdev_features_t features)
  3539. {
  3540. /* IPv4 header has options field */
  3541. if (vlan_get_protocol(skb) == htons(ETH_P_IP) &&
  3542. ip_hdrlen(skb) > sizeof(struct iphdr))
  3543. features &= ~NETIF_F_ALL_TSO;
  3544. /* IPv4 TCP header has options field */
  3545. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 &&
  3546. tcp_hdrlen(skb) > sizeof(struct tcphdr))
  3547. features &= ~NETIF_F_ALL_TSO;
  3548. else if (rtl_last_frag_len(skb) <= 6)
  3549. features &= ~NETIF_F_ALL_TSO;
  3550. return features;
  3551. }
  3552. static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
  3553. struct net_device *dev,
  3554. netdev_features_t features)
  3555. {
  3556. struct rtl8169_private *tp = netdev_priv(dev);
  3557. if (skb_is_gso(skb)) {
  3558. if (tp->mac_version == RTL_GIGA_MAC_VER_34)
  3559. features = rtl8168evl_fix_tso(skb, features);
  3560. if (skb_transport_offset(skb) > GTTCPHO_MAX &&
  3561. rtl_chip_supports_csum_v2(tp))
  3562. features &= ~NETIF_F_ALL_TSO;
  3563. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3564. /* work around hw bug on some chip versions */
  3565. if (skb->len < ETH_ZLEN)
  3566. features &= ~NETIF_F_CSUM_MASK;
  3567. if (rtl_quirk_packet_padto(tp, skb))
  3568. features &= ~NETIF_F_CSUM_MASK;
  3569. if (skb_transport_offset(skb) > TCPHO_MAX &&
  3570. rtl_chip_supports_csum_v2(tp))
  3571. features &= ~NETIF_F_CSUM_MASK;
  3572. }
  3573. return vlan_features_check(skb, features);
  3574. }
  3575. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  3576. {
  3577. struct rtl8169_private *tp = netdev_priv(dev);
  3578. struct pci_dev *pdev = tp->pci_dev;
  3579. int pci_status_errs;
  3580. u16 pci_cmd;
  3581. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  3582. pci_status_errs = pci_status_get_and_clear_errors(pdev);
  3583. if (net_ratelimit())
  3584. netdev_err(dev, "PCI error (cmd = 0x%04x, status_errs = 0x%04x)\n",
  3585. pci_cmd, pci_status_errs);
  3586. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  3587. }
  3588. static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
  3589. int budget)
  3590. {
  3591. unsigned int dirty_tx, bytes_compl = 0, pkts_compl = 0;
  3592. struct sk_buff *skb;
  3593. dirty_tx = tp->dirty_tx;
  3594. while (READ_ONCE(tp->cur_tx) != dirty_tx) {
  3595. unsigned int entry = dirty_tx % NUM_TX_DESC;
  3596. u32 status;
  3597. status = le32_to_cpu(READ_ONCE(tp->TxDescArray[entry].opts1));
  3598. if (status & DescOwn)
  3599. break;
  3600. skb = tp->tx_skb[entry].skb;
  3601. rtl8169_unmap_tx_skb(tp, entry);
  3602. if (skb) {
  3603. pkts_compl++;
  3604. bytes_compl += skb->len;
  3605. napi_consume_skb(skb, budget);
  3606. }
  3607. dirty_tx++;
  3608. }
  3609. if (tp->dirty_tx != dirty_tx) {
  3610. netdev_completed_queue(dev, pkts_compl, bytes_compl);
  3611. dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl);
  3612. /* Sync with rtl8169_start_xmit:
  3613. * - publish dirty_tx ring index (write barrier)
  3614. * - refresh cur_tx ring index and queue status (read barrier)
  3615. * May the current thread miss the stopped queue condition,
  3616. * a racing xmit thread can only have a right view of the
  3617. * ring status.
  3618. */
  3619. smp_store_mb(tp->dirty_tx, dirty_tx);
  3620. if (netif_queue_stopped(dev) && rtl_tx_slots_avail(tp))
  3621. netif_wake_queue(dev);
  3622. /*
  3623. * 8168 hack: TxPoll requests are lost when the Tx packets are
  3624. * too close. Let's kick an extra TxPoll request when a burst
  3625. * of start_xmit activity is detected (if it is not detected,
  3626. * it is slow enough). -- FR
  3627. * If skb is NULL then we come here again once a tx irq is
  3628. * triggered after the last fragment is marked transmitted.
  3629. */
  3630. if (READ_ONCE(tp->cur_tx) != dirty_tx && skb)
  3631. rtl8169_doorbell(tp);
  3632. }
  3633. }
  3634. static inline int rtl8169_fragmented_frame(u32 status)
  3635. {
  3636. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  3637. }
  3638. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  3639. {
  3640. u32 status = opts1 & (RxProtoMask | RxCSFailMask);
  3641. if (status == RxProtoTCP || status == RxProtoUDP)
  3642. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3643. else
  3644. skb_checksum_none_assert(skb);
  3645. }
  3646. static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget)
  3647. {
  3648. struct device *d = tp_to_dev(tp);
  3649. int count;
  3650. for (count = 0; count < budget; count++, tp->cur_rx++) {
  3651. unsigned int pkt_size, entry = tp->cur_rx % NUM_RX_DESC;
  3652. struct RxDesc *desc = tp->RxDescArray + entry;
  3653. struct sk_buff *skb;
  3654. const void *rx_buf;
  3655. dma_addr_t addr;
  3656. u32 status;
  3657. status = le32_to_cpu(READ_ONCE(desc->opts1));
  3658. if (status & DescOwn)
  3659. break;
  3660. /* This barrier is needed to keep us from reading
  3661. * any other fields out of the Rx descriptor until
  3662. * we know the status of DescOwn
  3663. */
  3664. dma_rmb();
  3665. if (unlikely(status & RxRES)) {
  3666. if (net_ratelimit())
  3667. netdev_warn(dev, "Rx ERROR. status = %08x\n",
  3668. status);
  3669. dev->stats.rx_errors++;
  3670. if (status & (RxRWT | RxRUNT))
  3671. dev->stats.rx_length_errors++;
  3672. if (status & RxCRC)
  3673. dev->stats.rx_crc_errors++;
  3674. if (!(dev->features & NETIF_F_RXALL))
  3675. goto release_descriptor;
  3676. else if (status & RxRWT || !(status & (RxRUNT | RxCRC)))
  3677. goto release_descriptor;
  3678. }
  3679. pkt_size = status & GENMASK(13, 0);
  3680. if (likely(!(dev->features & NETIF_F_RXFCS)))
  3681. pkt_size -= ETH_FCS_LEN;
  3682. /* The driver does not support incoming fragmented frames.
  3683. * They are seen as a symptom of over-mtu sized frames.
  3684. */
  3685. if (unlikely(rtl8169_fragmented_frame(status))) {
  3686. dev->stats.rx_dropped++;
  3687. dev->stats.rx_length_errors++;
  3688. goto release_descriptor;
  3689. }
  3690. skb = napi_alloc_skb(&tp->napi, pkt_size);
  3691. if (unlikely(!skb)) {
  3692. dev->stats.rx_dropped++;
  3693. goto release_descriptor;
  3694. }
  3695. addr = le64_to_cpu(desc->addr);
  3696. rx_buf = page_address(tp->Rx_databuff[entry]);
  3697. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  3698. prefetch(rx_buf);
  3699. skb_copy_to_linear_data(skb, rx_buf, pkt_size);
  3700. skb->tail += pkt_size;
  3701. skb->len = pkt_size;
  3702. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  3703. rtl8169_rx_csum(skb, status);
  3704. skb->protocol = eth_type_trans(skb, dev);
  3705. rtl8169_rx_vlan_tag(desc, skb);
  3706. if (skb->pkt_type == PACKET_MULTICAST)
  3707. dev->stats.multicast++;
  3708. napi_gro_receive(&tp->napi, skb);
  3709. dev_sw_netstats_rx_add(dev, pkt_size);
  3710. release_descriptor:
  3711. rtl8169_mark_to_asic(desc);
  3712. }
  3713. return count;
  3714. }
  3715. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  3716. {
  3717. struct rtl8169_private *tp = dev_instance;
  3718. u32 status = rtl_get_events(tp);
  3719. if ((status & 0xffff) == 0xffff || !(status & tp->irq_mask))
  3720. return IRQ_NONE;
  3721. if (unlikely(status & SYSErr)) {
  3722. rtl8169_pcierr_interrupt(tp->dev);
  3723. goto out;
  3724. }
  3725. if (status & LinkChg)
  3726. phy_mac_interrupt(tp->phydev);
  3727. if (unlikely(status & RxFIFOOver &&
  3728. tp->mac_version == RTL_GIGA_MAC_VER_11)) {
  3729. netif_stop_queue(tp->dev);
  3730. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  3731. }
  3732. if (napi_schedule_prep(&tp->napi)) {
  3733. rtl_irq_disable(tp);
  3734. __napi_schedule(&tp->napi);
  3735. }
  3736. out:
  3737. rtl_ack_events(tp, status);
  3738. return IRQ_HANDLED;
  3739. }
  3740. static void rtl_task(struct work_struct *work)
  3741. {
  3742. struct rtl8169_private *tp =
  3743. container_of(work, struct rtl8169_private, wk.work);
  3744. int ret;
  3745. rtnl_lock();
  3746. if (!netif_running(tp->dev) ||
  3747. !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
  3748. goto out_unlock;
  3749. if (test_and_clear_bit(RTL_FLAG_TASK_TX_TIMEOUT, tp->wk.flags)) {
  3750. /* ASPM compatibility issues are a typical reason for tx timeouts */
  3751. ret = pci_disable_link_state(tp->pci_dev, PCIE_LINK_STATE_L1 |
  3752. PCIE_LINK_STATE_L0S);
  3753. if (!ret)
  3754. netdev_warn_once(tp->dev, "ASPM disabled on Tx timeout\n");
  3755. goto reset;
  3756. }
  3757. if (test_and_clear_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags)) {
  3758. reset:
  3759. rtl_reset_work(tp);
  3760. netif_wake_queue(tp->dev);
  3761. } else if (test_and_clear_bit(RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE, tp->wk.flags)) {
  3762. rtl_reset_work(tp);
  3763. }
  3764. out_unlock:
  3765. rtnl_unlock();
  3766. }
  3767. static int rtl8169_poll(struct napi_struct *napi, int budget)
  3768. {
  3769. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  3770. struct net_device *dev = tp->dev;
  3771. int work_done;
  3772. rtl_tx(dev, tp, budget);
  3773. work_done = rtl_rx(dev, tp, budget);
  3774. if (work_done < budget && napi_complete_done(napi, work_done))
  3775. rtl_irq_enable(tp);
  3776. return work_done;
  3777. }
  3778. static void r8169_phylink_handler(struct net_device *ndev)
  3779. {
  3780. struct rtl8169_private *tp = netdev_priv(ndev);
  3781. struct device *d = tp_to_dev(tp);
  3782. if (netif_carrier_ok(ndev)) {
  3783. rtl_link_chg_patch(tp);
  3784. pm_request_resume(d);
  3785. netif_wake_queue(tp->dev);
  3786. } else {
  3787. /* In few cases rx is broken after link-down otherwise */
  3788. if (rtl_is_8125(tp))
  3789. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_NO_QUEUE_WAKE);
  3790. pm_runtime_idle(d);
  3791. }
  3792. phy_print_status(tp->phydev);
  3793. }
  3794. static int r8169_phy_connect(struct rtl8169_private *tp)
  3795. {
  3796. struct phy_device *phydev = tp->phydev;
  3797. phy_interface_t phy_mode;
  3798. int ret;
  3799. phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
  3800. PHY_INTERFACE_MODE_MII;
  3801. ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
  3802. phy_mode);
  3803. if (ret)
  3804. return ret;
  3805. if (!tp->supports_gmii)
  3806. phy_set_max_speed(phydev, SPEED_100);
  3807. phy_attached_info(phydev);
  3808. return 0;
  3809. }
  3810. static void rtl8169_down(struct rtl8169_private *tp)
  3811. {
  3812. /* Clear all task flags */
  3813. bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
  3814. phy_stop(tp->phydev);
  3815. rtl8169_update_counters(tp);
  3816. pci_clear_master(tp->pci_dev);
  3817. rtl_pci_commit(tp);
  3818. rtl8169_cleanup(tp);
  3819. rtl_disable_exit_l1(tp);
  3820. rtl_prepare_power_down(tp);
  3821. if (tp->dash_type != RTL_DASH_NONE)
  3822. rtl8168_driver_stop(tp);
  3823. }
  3824. static void rtl8169_up(struct rtl8169_private *tp)
  3825. {
  3826. if (tp->dash_type != RTL_DASH_NONE)
  3827. rtl8168_driver_start(tp);
  3828. pci_set_master(tp->pci_dev);
  3829. phy_init_hw(tp->phydev);
  3830. phy_resume(tp->phydev);
  3831. rtl8169_init_phy(tp);
  3832. napi_enable(&tp->napi);
  3833. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  3834. rtl_reset_work(tp);
  3835. phy_start(tp->phydev);
  3836. }
  3837. static int rtl8169_close(struct net_device *dev)
  3838. {
  3839. struct rtl8169_private *tp = netdev_priv(dev);
  3840. struct pci_dev *pdev = tp->pci_dev;
  3841. pm_runtime_get_sync(&pdev->dev);
  3842. netif_stop_queue(dev);
  3843. rtl8169_down(tp);
  3844. rtl8169_rx_clear(tp);
  3845. cancel_work(&tp->wk.work);
  3846. free_irq(tp->irq, tp);
  3847. phy_disconnect(tp->phydev);
  3848. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3849. tp->RxPhyAddr);
  3850. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3851. tp->TxPhyAddr);
  3852. tp->TxDescArray = NULL;
  3853. tp->RxDescArray = NULL;
  3854. pm_runtime_put_sync(&pdev->dev);
  3855. return 0;
  3856. }
  3857. #ifdef CONFIG_NET_POLL_CONTROLLER
  3858. static void rtl8169_netpoll(struct net_device *dev)
  3859. {
  3860. struct rtl8169_private *tp = netdev_priv(dev);
  3861. rtl8169_interrupt(tp->irq, tp);
  3862. }
  3863. #endif
  3864. static int rtl_open(struct net_device *dev)
  3865. {
  3866. struct rtl8169_private *tp = netdev_priv(dev);
  3867. struct pci_dev *pdev = tp->pci_dev;
  3868. unsigned long irqflags;
  3869. int retval = -ENOMEM;
  3870. pm_runtime_get_sync(&pdev->dev);
  3871. /*
  3872. * Rx and Tx descriptors needs 256 bytes alignment.
  3873. * dma_alloc_coherent provides more.
  3874. */
  3875. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  3876. &tp->TxPhyAddr, GFP_KERNEL);
  3877. if (!tp->TxDescArray)
  3878. goto out;
  3879. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  3880. &tp->RxPhyAddr, GFP_KERNEL);
  3881. if (!tp->RxDescArray)
  3882. goto err_free_tx_0;
  3883. retval = rtl8169_init_ring(tp);
  3884. if (retval < 0)
  3885. goto err_free_rx_1;
  3886. rtl_request_firmware(tp);
  3887. irqflags = pci_dev_msi_enabled(pdev) ? IRQF_NO_THREAD : IRQF_SHARED;
  3888. retval = request_irq(tp->irq, rtl8169_interrupt, irqflags, dev->name, tp);
  3889. if (retval < 0)
  3890. goto err_release_fw_2;
  3891. retval = r8169_phy_connect(tp);
  3892. if (retval)
  3893. goto err_free_irq;
  3894. rtl8169_up(tp);
  3895. rtl8169_init_counter_offsets(tp);
  3896. netif_start_queue(dev);
  3897. out:
  3898. pm_runtime_put_sync(&pdev->dev);
  3899. return retval;
  3900. err_free_irq:
  3901. free_irq(tp->irq, tp);
  3902. err_release_fw_2:
  3903. rtl_release_firmware(tp);
  3904. rtl8169_rx_clear(tp);
  3905. err_free_rx_1:
  3906. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3907. tp->RxPhyAddr);
  3908. tp->RxDescArray = NULL;
  3909. err_free_tx_0:
  3910. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3911. tp->TxPhyAddr);
  3912. tp->TxDescArray = NULL;
  3913. goto out;
  3914. }
  3915. static void
  3916. rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  3917. {
  3918. struct rtl8169_private *tp = netdev_priv(dev);
  3919. struct pci_dev *pdev = tp->pci_dev;
  3920. struct rtl8169_counters *counters = tp->counters;
  3921. pm_runtime_get_noresume(&pdev->dev);
  3922. netdev_stats_to_stats64(stats, &dev->stats);
  3923. dev_fetch_sw_netstats(stats, dev->tstats);
  3924. /*
  3925. * Fetch additional counter values missing in stats collected by driver
  3926. * from tally counters.
  3927. */
  3928. if (pm_runtime_active(&pdev->dev))
  3929. rtl8169_update_counters(tp);
  3930. /*
  3931. * Subtract values fetched during initalization.
  3932. * See rtl8169_init_counter_offsets for a description why we do that.
  3933. */
  3934. stats->tx_errors = le64_to_cpu(counters->tx_errors) -
  3935. le64_to_cpu(tp->tc_offset.tx_errors);
  3936. stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
  3937. le32_to_cpu(tp->tc_offset.tx_multi_collision);
  3938. stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
  3939. le16_to_cpu(tp->tc_offset.tx_aborted);
  3940. stats->rx_missed_errors = le16_to_cpu(counters->rx_missed) -
  3941. le16_to_cpu(tp->tc_offset.rx_missed);
  3942. pm_runtime_put_noidle(&pdev->dev);
  3943. }
  3944. static void rtl8169_net_suspend(struct rtl8169_private *tp)
  3945. {
  3946. netif_device_detach(tp->dev);
  3947. if (netif_running(tp->dev))
  3948. rtl8169_down(tp);
  3949. }
  3950. static int rtl8169_runtime_resume(struct device *dev)
  3951. {
  3952. struct rtl8169_private *tp = dev_get_drvdata(dev);
  3953. rtl_rar_set(tp, tp->dev->dev_addr);
  3954. __rtl8169_set_wol(tp, tp->saved_wolopts);
  3955. if (tp->TxDescArray)
  3956. rtl8169_up(tp);
  3957. netif_device_attach(tp->dev);
  3958. return 0;
  3959. }
  3960. static int rtl8169_suspend(struct device *device)
  3961. {
  3962. struct rtl8169_private *tp = dev_get_drvdata(device);
  3963. rtnl_lock();
  3964. rtl8169_net_suspend(tp);
  3965. if (!device_may_wakeup(tp_to_dev(tp)))
  3966. clk_disable_unprepare(tp->clk);
  3967. rtnl_unlock();
  3968. return 0;
  3969. }
  3970. static int rtl8169_resume(struct device *device)
  3971. {
  3972. struct rtl8169_private *tp = dev_get_drvdata(device);
  3973. if (!device_may_wakeup(tp_to_dev(tp)))
  3974. clk_prepare_enable(tp->clk);
  3975. /* Reportedly at least Asus X453MA truncates packets otherwise */
  3976. if (tp->mac_version == RTL_GIGA_MAC_VER_37)
  3977. rtl_init_rxcfg(tp);
  3978. return rtl8169_runtime_resume(device);
  3979. }
  3980. static int rtl8169_runtime_suspend(struct device *device)
  3981. {
  3982. struct rtl8169_private *tp = dev_get_drvdata(device);
  3983. if (!tp->TxDescArray) {
  3984. netif_device_detach(tp->dev);
  3985. return 0;
  3986. }
  3987. rtnl_lock();
  3988. __rtl8169_set_wol(tp, WAKE_PHY);
  3989. rtl8169_net_suspend(tp);
  3990. rtnl_unlock();
  3991. return 0;
  3992. }
  3993. static int rtl8169_runtime_idle(struct device *device)
  3994. {
  3995. struct rtl8169_private *tp = dev_get_drvdata(device);
  3996. if (tp->dash_enabled)
  3997. return -EBUSY;
  3998. if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
  3999. pm_schedule_suspend(device, 10000);
  4000. return -EBUSY;
  4001. }
  4002. static const struct dev_pm_ops rtl8169_pm_ops = {
  4003. SYSTEM_SLEEP_PM_OPS(rtl8169_suspend, rtl8169_resume)
  4004. RUNTIME_PM_OPS(rtl8169_runtime_suspend, rtl8169_runtime_resume,
  4005. rtl8169_runtime_idle)
  4006. };
  4007. static void rtl_shutdown(struct pci_dev *pdev)
  4008. {
  4009. struct rtl8169_private *tp = pci_get_drvdata(pdev);
  4010. rtnl_lock();
  4011. rtl8169_net_suspend(tp);
  4012. rtnl_unlock();
  4013. /* Restore original MAC address */
  4014. rtl_rar_set(tp, tp->dev->perm_addr);
  4015. if (system_state == SYSTEM_POWER_OFF && !tp->dash_enabled) {
  4016. pci_wake_from_d3(pdev, tp->saved_wolopts);
  4017. pci_set_power_state(pdev, PCI_D3hot);
  4018. }
  4019. }
  4020. static void rtl_remove_one(struct pci_dev *pdev)
  4021. {
  4022. struct rtl8169_private *tp = pci_get_drvdata(pdev);
  4023. if (pci_dev_run_wake(pdev))
  4024. pm_runtime_get_noresume(&pdev->dev);
  4025. cancel_work_sync(&tp->wk.work);
  4026. unregister_netdev(tp->dev);
  4027. if (tp->dash_type != RTL_DASH_NONE)
  4028. rtl8168_driver_stop(tp);
  4029. rtl_release_firmware(tp);
  4030. /* restore original MAC address */
  4031. rtl_rar_set(tp, tp->dev->perm_addr);
  4032. }
  4033. static const struct net_device_ops rtl_netdev_ops = {
  4034. .ndo_open = rtl_open,
  4035. .ndo_stop = rtl8169_close,
  4036. .ndo_get_stats64 = rtl8169_get_stats64,
  4037. .ndo_start_xmit = rtl8169_start_xmit,
  4038. .ndo_features_check = rtl8169_features_check,
  4039. .ndo_tx_timeout = rtl8169_tx_timeout,
  4040. .ndo_validate_addr = eth_validate_addr,
  4041. .ndo_change_mtu = rtl8169_change_mtu,
  4042. .ndo_fix_features = rtl8169_fix_features,
  4043. .ndo_set_features = rtl8169_set_features,
  4044. .ndo_set_mac_address = rtl_set_mac_address,
  4045. .ndo_eth_ioctl = phy_do_ioctl_running,
  4046. .ndo_set_rx_mode = rtl_set_rx_mode,
  4047. #ifdef CONFIG_NET_POLL_CONTROLLER
  4048. .ndo_poll_controller = rtl8169_netpoll,
  4049. #endif
  4050. };
  4051. static void rtl_set_irq_mask(struct rtl8169_private *tp)
  4052. {
  4053. tp->irq_mask = RxOK | RxErr | TxOK | TxErr | LinkChg;
  4054. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  4055. tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
  4056. else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
  4057. /* special workaround needed */
  4058. tp->irq_mask |= RxFIFOOver;
  4059. else
  4060. tp->irq_mask |= RxOverflow;
  4061. }
  4062. static int rtl_alloc_irq(struct rtl8169_private *tp)
  4063. {
  4064. unsigned int flags;
  4065. switch (tp->mac_version) {
  4066. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  4067. rtl_unlock_config_regs(tp);
  4068. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
  4069. rtl_lock_config_regs(tp);
  4070. fallthrough;
  4071. case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17:
  4072. flags = PCI_IRQ_LEGACY;
  4073. break;
  4074. default:
  4075. flags = PCI_IRQ_ALL_TYPES;
  4076. break;
  4077. }
  4078. return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
  4079. }
  4080. static void rtl_read_mac_address(struct rtl8169_private *tp,
  4081. u8 mac_addr[ETH_ALEN])
  4082. {
  4083. /* Get MAC address */
  4084. if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
  4085. u32 value;
  4086. value = rtl_eri_read(tp, 0xe0);
  4087. put_unaligned_le32(value, mac_addr);
  4088. value = rtl_eri_read(tp, 0xe4);
  4089. put_unaligned_le16(value, mac_addr + 4);
  4090. } else if (rtl_is_8125(tp)) {
  4091. rtl_read_mac_from_reg(tp, mac_addr, MAC0_BKP);
  4092. }
  4093. }
  4094. DECLARE_RTL_COND(rtl_link_list_ready_cond)
  4095. {
  4096. return RTL_R8(tp, MCU) & LINK_LIST_RDY;
  4097. }
  4098. static void r8168g_wait_ll_share_fifo_ready(struct rtl8169_private *tp)
  4099. {
  4100. rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
  4101. }
  4102. static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
  4103. {
  4104. struct rtl8169_private *tp = mii_bus->priv;
  4105. if (phyaddr > 0)
  4106. return -ENODEV;
  4107. return rtl_readphy(tp, phyreg);
  4108. }
  4109. static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
  4110. int phyreg, u16 val)
  4111. {
  4112. struct rtl8169_private *tp = mii_bus->priv;
  4113. if (phyaddr > 0)
  4114. return -ENODEV;
  4115. rtl_writephy(tp, phyreg, val);
  4116. return 0;
  4117. }
  4118. static int r8169_mdio_register(struct rtl8169_private *tp)
  4119. {
  4120. struct pci_dev *pdev = tp->pci_dev;
  4121. struct mii_bus *new_bus;
  4122. int ret;
  4123. new_bus = devm_mdiobus_alloc(&pdev->dev);
  4124. if (!new_bus)
  4125. return -ENOMEM;
  4126. new_bus->name = "r8169";
  4127. new_bus->priv = tp;
  4128. new_bus->parent = &pdev->dev;
  4129. new_bus->irq[0] = PHY_MAC_INTERRUPT;
  4130. snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x-%x",
  4131. pci_domain_nr(pdev->bus), pci_dev_id(pdev));
  4132. new_bus->read = r8169_mdio_read_reg;
  4133. new_bus->write = r8169_mdio_write_reg;
  4134. ret = devm_mdiobus_register(&pdev->dev, new_bus);
  4135. if (ret)
  4136. return ret;
  4137. tp->phydev = mdiobus_get_phy(new_bus, 0);
  4138. if (!tp->phydev) {
  4139. return -ENODEV;
  4140. } else if (!tp->phydev->drv) {
  4141. /* Most chip versions fail with the genphy driver.
  4142. * Therefore ensure that the dedicated PHY driver is loaded.
  4143. */
  4144. dev_err(&pdev->dev, "no dedicated PHY driver found for PHY ID 0x%08x, maybe realtek.ko needs to be added to initramfs?\n",
  4145. tp->phydev->phy_id);
  4146. return -EUNATCH;
  4147. }
  4148. tp->phydev->mac_managed_pm = 1;
  4149. phy_support_asym_pause(tp->phydev);
  4150. /* PHY will be woken up in rtl_open() */
  4151. phy_suspend(tp->phydev);
  4152. return 0;
  4153. }
  4154. static void rtl_hw_init_8168g(struct rtl8169_private *tp)
  4155. {
  4156. rtl_enable_rxdvgate(tp);
  4157. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
  4158. msleep(1);
  4159. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4160. r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
  4161. r8168g_wait_ll_share_fifo_ready(tp);
  4162. r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
  4163. r8168g_wait_ll_share_fifo_ready(tp);
  4164. }
  4165. static void rtl_hw_init_8125(struct rtl8169_private *tp)
  4166. {
  4167. rtl_enable_rxdvgate(tp);
  4168. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
  4169. msleep(1);
  4170. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4171. r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
  4172. r8168g_wait_ll_share_fifo_ready(tp);
  4173. r8168_mac_ocp_write(tp, 0xc0aa, 0x07d0);
  4174. r8168_mac_ocp_write(tp, 0xc0a6, 0x0150);
  4175. r8168_mac_ocp_write(tp, 0xc01e, 0x5555);
  4176. r8168g_wait_ll_share_fifo_ready(tp);
  4177. }
  4178. static void rtl_hw_initialize(struct rtl8169_private *tp)
  4179. {
  4180. switch (tp->mac_version) {
  4181. case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_53:
  4182. rtl8168ep_stop_cmac(tp);
  4183. fallthrough;
  4184. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
  4185. rtl_hw_init_8168g(tp);
  4186. break;
  4187. case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_63:
  4188. rtl_hw_init_8125(tp);
  4189. break;
  4190. default:
  4191. break;
  4192. }
  4193. }
  4194. static int rtl_jumbo_max(struct rtl8169_private *tp)
  4195. {
  4196. /* Non-GBit versions don't support jumbo frames */
  4197. if (!tp->supports_gmii)
  4198. return 0;
  4199. switch (tp->mac_version) {
  4200. /* RTL8169 */
  4201. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  4202. return JUMBO_7K;
  4203. /* RTL8168b */
  4204. case RTL_GIGA_MAC_VER_11:
  4205. case RTL_GIGA_MAC_VER_17:
  4206. return JUMBO_4K;
  4207. /* RTL8168c */
  4208. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
  4209. return JUMBO_6K;
  4210. default:
  4211. return JUMBO_9K;
  4212. }
  4213. }
  4214. static void rtl_init_mac_address(struct rtl8169_private *tp)
  4215. {
  4216. u8 mac_addr[ETH_ALEN] __aligned(2) = {};
  4217. struct net_device *dev = tp->dev;
  4218. int rc;
  4219. rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
  4220. if (!rc)
  4221. goto done;
  4222. rtl_read_mac_address(tp, mac_addr);
  4223. if (is_valid_ether_addr(mac_addr))
  4224. goto done;
  4225. rtl_read_mac_from_reg(tp, mac_addr, MAC0);
  4226. if (is_valid_ether_addr(mac_addr))
  4227. goto done;
  4228. eth_random_addr(mac_addr);
  4229. dev->addr_assign_type = NET_ADDR_RANDOM;
  4230. dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
  4231. done:
  4232. eth_hw_addr_set(dev, mac_addr);
  4233. rtl_rar_set(tp, mac_addr);
  4234. }
  4235. /* register is set if system vendor successfully tested ASPM 1.2 */
  4236. static bool rtl_aspm_is_safe(struct rtl8169_private *tp)
  4237. {
  4238. if (tp->mac_version >= RTL_GIGA_MAC_VER_61 &&
  4239. r8168_mac_ocp_read(tp, 0xc0b2) & 0xf)
  4240. return true;
  4241. return false;
  4242. }
  4243. static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  4244. {
  4245. struct rtl8169_private *tp;
  4246. int jumbo_max, region, rc;
  4247. enum mac_version chipset;
  4248. struct net_device *dev;
  4249. u16 xid;
  4250. dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
  4251. if (!dev)
  4252. return -ENOMEM;
  4253. SET_NETDEV_DEV(dev, &pdev->dev);
  4254. dev->netdev_ops = &rtl_netdev_ops;
  4255. tp = netdev_priv(dev);
  4256. tp->dev = dev;
  4257. tp->pci_dev = pdev;
  4258. tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
  4259. tp->eee_adv = -1;
  4260. tp->ocp_base = OCP_STD_PHY_BASE;
  4261. dev->tstats = devm_netdev_alloc_pcpu_stats(&pdev->dev,
  4262. struct pcpu_sw_netstats);
  4263. if (!dev->tstats)
  4264. return -ENOMEM;
  4265. /* Get the *optional* external "ether_clk" used on some boards */
  4266. tp->clk = devm_clk_get_optional_enabled(&pdev->dev, "ether_clk");
  4267. if (IS_ERR(tp->clk))
  4268. return dev_err_probe(&pdev->dev, PTR_ERR(tp->clk), "failed to get ether_clk\n");
  4269. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  4270. rc = pcim_enable_device(pdev);
  4271. if (rc < 0) {
  4272. dev_err(&pdev->dev, "enable failure\n");
  4273. return rc;
  4274. }
  4275. if (pcim_set_mwi(pdev) < 0)
  4276. dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
  4277. /* use first MMIO region */
  4278. region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
  4279. if (region < 0) {
  4280. dev_err(&pdev->dev, "no MMIO resource found\n");
  4281. return -ENODEV;
  4282. }
  4283. rc = pcim_iomap_regions(pdev, BIT(region), KBUILD_MODNAME);
  4284. if (rc < 0) {
  4285. dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
  4286. return rc;
  4287. }
  4288. tp->mmio_addr = pcim_iomap_table(pdev)[region];
  4289. xid = (RTL_R32(tp, TxConfig) >> 20) & 0xfcf;
  4290. /* Identify chip attached to board */
  4291. chipset = rtl8169_get_mac_version(xid, tp->supports_gmii);
  4292. if (chipset == RTL_GIGA_MAC_NONE) {
  4293. dev_err(&pdev->dev, "unknown chip XID %03x, contact r8169 maintainers (see MAINTAINERS file)\n", xid);
  4294. return -ENODEV;
  4295. }
  4296. tp->mac_version = chipset;
  4297. /* Disable ASPM L1 as that cause random device stop working
  4298. * problems as well as full system hangs for some PCIe devices users.
  4299. */
  4300. if (rtl_aspm_is_safe(tp))
  4301. rc = 0;
  4302. else
  4303. rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L1);
  4304. tp->aspm_manageable = !rc;
  4305. tp->dash_type = rtl_get_dash_type(tp);
  4306. tp->dash_enabled = rtl_dash_is_enabled(tp);
  4307. tp->cp_cmd = RTL_R16(tp, CPlusCmd) & CPCMD_MASK;
  4308. if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
  4309. !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
  4310. dev->features |= NETIF_F_HIGHDMA;
  4311. rtl_init_rxcfg(tp);
  4312. rtl8169_irq_mask_and_ack(tp);
  4313. rtl_hw_initialize(tp);
  4314. rtl_hw_reset(tp);
  4315. rc = rtl_alloc_irq(tp);
  4316. if (rc < 0) {
  4317. dev_err(&pdev->dev, "Can't allocate interrupt\n");
  4318. return rc;
  4319. }
  4320. tp->irq = pci_irq_vector(pdev, 0);
  4321. INIT_WORK(&tp->wk.work, rtl_task);
  4322. rtl_init_mac_address(tp);
  4323. dev->ethtool_ops = &rtl8169_ethtool_ops;
  4324. netif_napi_add(dev, &tp->napi, rtl8169_poll);
  4325. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  4326. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  4327. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  4328. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  4329. /*
  4330. * Pretend we are using VLANs; This bypasses a nasty bug where
  4331. * Interrupts stop flowing on high load on 8110SCd controllers.
  4332. */
  4333. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  4334. /* Disallow toggling */
  4335. dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  4336. if (rtl_chip_supports_csum_v2(tp))
  4337. dev->hw_features |= NETIF_F_IPV6_CSUM;
  4338. dev->features |= dev->hw_features;
  4339. /* There has been a number of reports that using SG/TSO results in
  4340. * tx timeouts. However for a lot of people SG/TSO works fine.
  4341. * Therefore disable both features by default, but allow users to
  4342. * enable them. Use at own risk!
  4343. */
  4344. if (rtl_chip_supports_csum_v2(tp)) {
  4345. dev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6;
  4346. netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V2);
  4347. netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V2);
  4348. } else {
  4349. dev->hw_features |= NETIF_F_SG | NETIF_F_TSO;
  4350. netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V1);
  4351. netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V1);
  4352. }
  4353. dev->hw_features |= NETIF_F_RXALL;
  4354. dev->hw_features |= NETIF_F_RXFCS;
  4355. /* configure chip for default features */
  4356. rtl8169_set_features(dev, dev->features);
  4357. if (!tp->dash_enabled) {
  4358. rtl_set_d3_pll_down(tp, true);
  4359. } else {
  4360. rtl_set_d3_pll_down(tp, false);
  4361. dev->wol_enabled = 1;
  4362. }
  4363. jumbo_max = rtl_jumbo_max(tp);
  4364. if (jumbo_max)
  4365. dev->max_mtu = jumbo_max;
  4366. rtl_set_irq_mask(tp);
  4367. tp->fw_name = rtl_chip_infos[chipset].fw_name;
  4368. tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
  4369. &tp->counters_phys_addr,
  4370. GFP_KERNEL);
  4371. if (!tp->counters)
  4372. return -ENOMEM;
  4373. pci_set_drvdata(pdev, tp);
  4374. rc = r8169_mdio_register(tp);
  4375. if (rc)
  4376. return rc;
  4377. rc = register_netdev(dev);
  4378. if (rc)
  4379. return rc;
  4380. netdev_info(dev, "%s, %pM, XID %03x, IRQ %d\n",
  4381. rtl_chip_infos[chipset].name, dev->dev_addr, xid, tp->irq);
  4382. if (jumbo_max)
  4383. netdev_info(dev, "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
  4384. jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
  4385. "ok" : "ko");
  4386. if (tp->dash_type != RTL_DASH_NONE) {
  4387. netdev_info(dev, "DASH %s\n",
  4388. tp->dash_enabled ? "enabled" : "disabled");
  4389. rtl8168_driver_start(tp);
  4390. }
  4391. if (pci_dev_run_wake(pdev))
  4392. pm_runtime_put_sync(&pdev->dev);
  4393. return 0;
  4394. }
  4395. static struct pci_driver rtl8169_pci_driver = {
  4396. .name = KBUILD_MODNAME,
  4397. .id_table = rtl8169_pci_tbl,
  4398. .probe = rtl_init_one,
  4399. .remove = rtl_remove_one,
  4400. .shutdown = rtl_shutdown,
  4401. .driver.pm = pm_ptr(&rtl8169_pm_ops),
  4402. };
  4403. module_pci_driver(rtl8169_pci_driver);