r8169.c 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690
  1. /*
  2. * r8169.c: RealTek 8169/8168/8101 ethernet driver.
  3. *
  4. * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
  5. * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
  6. * Copyright (c) a lot of people too. Please respect their work.
  7. *
  8. * See MAINTAINERS file for support contact information.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/pci.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/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/crc32.h>
  21. #include <linux/in.h>
  22. #include <linux/io.h>
  23. #include <linux/ip.h>
  24. #include <linux/tcp.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/firmware.h>
  29. #include <linux/prefetch.h>
  30. #include <linux/pci-aspm.h>
  31. #include <linux/ipv6.h>
  32. #include <net/ip6_checksum.h>
  33. #define MODULENAME "r8169"
  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_1 "rtl_nic/rtl8168h-1.fw"
  50. #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
  51. #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
  52. #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
  53. #define R8169_MSG_DEFAULT \
  54. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  55. #define TX_SLOTS_AVAIL(tp) \
  56. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
  57. /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
  58. #define TX_FRAGS_READY_FOR(tp,nr_frags) \
  59. (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
  60. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  61. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  62. static const int multicast_filter_limit = 32;
  63. #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
  64. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  65. #define R8169_REGS_SIZE 256
  66. #define R8169_RX_BUF_SIZE (SZ_16K - 1)
  67. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  68. #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
  69. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  70. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  71. #define RTL8169_TX_TIMEOUT (6*HZ)
  72. /* write/read MMIO register */
  73. #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
  74. #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
  75. #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
  76. #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
  77. #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
  78. #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
  79. enum mac_version {
  80. RTL_GIGA_MAC_VER_01 = 0,
  81. RTL_GIGA_MAC_VER_02,
  82. RTL_GIGA_MAC_VER_03,
  83. RTL_GIGA_MAC_VER_04,
  84. RTL_GIGA_MAC_VER_05,
  85. RTL_GIGA_MAC_VER_06,
  86. RTL_GIGA_MAC_VER_07,
  87. RTL_GIGA_MAC_VER_08,
  88. RTL_GIGA_MAC_VER_09,
  89. RTL_GIGA_MAC_VER_10,
  90. RTL_GIGA_MAC_VER_11,
  91. RTL_GIGA_MAC_VER_12,
  92. RTL_GIGA_MAC_VER_13,
  93. RTL_GIGA_MAC_VER_14,
  94. RTL_GIGA_MAC_VER_15,
  95. RTL_GIGA_MAC_VER_16,
  96. RTL_GIGA_MAC_VER_17,
  97. RTL_GIGA_MAC_VER_18,
  98. RTL_GIGA_MAC_VER_19,
  99. RTL_GIGA_MAC_VER_20,
  100. RTL_GIGA_MAC_VER_21,
  101. RTL_GIGA_MAC_VER_22,
  102. RTL_GIGA_MAC_VER_23,
  103. RTL_GIGA_MAC_VER_24,
  104. RTL_GIGA_MAC_VER_25,
  105. RTL_GIGA_MAC_VER_26,
  106. RTL_GIGA_MAC_VER_27,
  107. RTL_GIGA_MAC_VER_28,
  108. RTL_GIGA_MAC_VER_29,
  109. RTL_GIGA_MAC_VER_30,
  110. RTL_GIGA_MAC_VER_31,
  111. RTL_GIGA_MAC_VER_32,
  112. RTL_GIGA_MAC_VER_33,
  113. RTL_GIGA_MAC_VER_34,
  114. RTL_GIGA_MAC_VER_35,
  115. RTL_GIGA_MAC_VER_36,
  116. RTL_GIGA_MAC_VER_37,
  117. RTL_GIGA_MAC_VER_38,
  118. RTL_GIGA_MAC_VER_39,
  119. RTL_GIGA_MAC_VER_40,
  120. RTL_GIGA_MAC_VER_41,
  121. RTL_GIGA_MAC_VER_42,
  122. RTL_GIGA_MAC_VER_43,
  123. RTL_GIGA_MAC_VER_44,
  124. RTL_GIGA_MAC_VER_45,
  125. RTL_GIGA_MAC_VER_46,
  126. RTL_GIGA_MAC_VER_47,
  127. RTL_GIGA_MAC_VER_48,
  128. RTL_GIGA_MAC_VER_49,
  129. RTL_GIGA_MAC_VER_50,
  130. RTL_GIGA_MAC_VER_51,
  131. RTL_GIGA_MAC_NONE = 0xff,
  132. };
  133. #define JUMBO_1K ETH_DATA_LEN
  134. #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
  135. #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
  136. #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
  137. #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
  138. static const struct {
  139. const char *name;
  140. const char *fw_name;
  141. } rtl_chip_infos[] = {
  142. /* PCI devices. */
  143. [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
  144. [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
  145. [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
  146. [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
  147. [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
  148. [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
  149. /* PCI-E devices. */
  150. [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
  151. [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
  152. [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
  153. [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
  154. [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
  155. [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
  156. [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
  157. [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
  158. [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
  159. [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
  160. [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
  161. [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
  162. [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
  163. [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
  164. [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
  165. [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
  166. [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
  167. [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
  168. [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
  169. [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
  170. [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
  171. [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
  172. [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
  173. [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
  174. [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
  175. [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
  176. [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
  177. [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
  178. [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
  179. [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
  180. [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
  181. [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
  182. [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
  183. [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
  184. [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
  185. [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
  186. [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
  187. [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
  188. [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
  189. [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
  190. [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
  191. [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
  192. [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
  193. [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
  194. [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
  195. };
  196. enum cfg_version {
  197. RTL_CFG_0 = 0x00,
  198. RTL_CFG_1,
  199. RTL_CFG_2
  200. };
  201. static const struct pci_device_id rtl8169_pci_tbl[] = {
  202. { PCI_VDEVICE(REALTEK, 0x2502), RTL_CFG_1 },
  203. { PCI_VDEVICE(REALTEK, 0x2600), RTL_CFG_1 },
  204. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  205. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  206. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
  207. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  208. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  209. { PCI_DEVICE(PCI_VENDOR_ID_NCUBE, 0x8168), 0, 0, RTL_CFG_1 },
  210. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  211. { PCI_VENDOR_ID_DLINK, 0x4300,
  212. PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
  213. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  214. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
  215. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  216. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  217. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  218. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  219. { 0x0001, 0x8168,
  220. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  221. {0,},
  222. };
  223. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  224. static int use_dac = -1;
  225. static struct {
  226. u32 msg_enable;
  227. } debug = { -1 };
  228. enum rtl_registers {
  229. MAC0 = 0, /* Ethernet hardware address. */
  230. MAC4 = 4,
  231. MAR0 = 8, /* Multicast filter. */
  232. CounterAddrLow = 0x10,
  233. CounterAddrHigh = 0x14,
  234. TxDescStartAddrLow = 0x20,
  235. TxDescStartAddrHigh = 0x24,
  236. TxHDescStartAddrLow = 0x28,
  237. TxHDescStartAddrHigh = 0x2c,
  238. FLASH = 0x30,
  239. ERSR = 0x36,
  240. ChipCmd = 0x37,
  241. TxPoll = 0x38,
  242. IntrMask = 0x3c,
  243. IntrStatus = 0x3e,
  244. TxConfig = 0x40,
  245. #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
  246. #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
  247. RxConfig = 0x44,
  248. #define RX128_INT_EN (1 << 15) /* 8111c and later */
  249. #define RX_MULTI_EN (1 << 14) /* 8111c only */
  250. #define RXCFG_FIFO_SHIFT 13
  251. /* No threshold before first PCI xfer */
  252. #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
  253. #define RX_EARLY_OFF (1 << 11)
  254. #define RXCFG_DMA_SHIFT 8
  255. /* Unlimited maximum PCI burst. */
  256. #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
  257. RxMissed = 0x4c,
  258. Cfg9346 = 0x50,
  259. Config0 = 0x51,
  260. Config1 = 0x52,
  261. Config2 = 0x53,
  262. #define PME_SIGNAL (1 << 5) /* 8168c and later */
  263. Config3 = 0x54,
  264. Config4 = 0x55,
  265. Config5 = 0x56,
  266. MultiIntr = 0x5c,
  267. PHYAR = 0x60,
  268. PHYstatus = 0x6c,
  269. RxMaxSize = 0xda,
  270. CPlusCmd = 0xe0,
  271. IntrMitigate = 0xe2,
  272. #define RTL_COALESCE_MASK 0x0f
  273. #define RTL_COALESCE_SHIFT 4
  274. #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
  275. #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
  276. RxDescAddrLow = 0xe4,
  277. RxDescAddrHigh = 0xe8,
  278. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  279. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  280. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  281. #define TxPacketMax (8064 >> 7)
  282. #define EarlySize 0x27
  283. FuncEvent = 0xf0,
  284. FuncEventMask = 0xf4,
  285. FuncPresetState = 0xf8,
  286. IBCR0 = 0xf8,
  287. IBCR2 = 0xf9,
  288. IBIMR0 = 0xfa,
  289. IBISR0 = 0xfb,
  290. FuncForceEvent = 0xfc,
  291. };
  292. enum rtl8168_8101_registers {
  293. CSIDR = 0x64,
  294. CSIAR = 0x68,
  295. #define CSIAR_FLAG 0x80000000
  296. #define CSIAR_WRITE_CMD 0x80000000
  297. #define CSIAR_BYTE_ENABLE 0x0000f000
  298. #define CSIAR_ADDR_MASK 0x00000fff
  299. PMCH = 0x6f,
  300. EPHYAR = 0x80,
  301. #define EPHYAR_FLAG 0x80000000
  302. #define EPHYAR_WRITE_CMD 0x80000000
  303. #define EPHYAR_REG_MASK 0x1f
  304. #define EPHYAR_REG_SHIFT 16
  305. #define EPHYAR_DATA_MASK 0xffff
  306. DLLPR = 0xd0,
  307. #define PFM_EN (1 << 6)
  308. #define TX_10M_PS_EN (1 << 7)
  309. DBG_REG = 0xd1,
  310. #define FIX_NAK_1 (1 << 4)
  311. #define FIX_NAK_2 (1 << 3)
  312. TWSI = 0xd2,
  313. MCU = 0xd3,
  314. #define NOW_IS_OOB (1 << 7)
  315. #define TX_EMPTY (1 << 5)
  316. #define RX_EMPTY (1 << 4)
  317. #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
  318. #define EN_NDP (1 << 3)
  319. #define EN_OOB_RESET (1 << 2)
  320. #define LINK_LIST_RDY (1 << 1)
  321. EFUSEAR = 0xdc,
  322. #define EFUSEAR_FLAG 0x80000000
  323. #define EFUSEAR_WRITE_CMD 0x80000000
  324. #define EFUSEAR_READ_CMD 0x00000000
  325. #define EFUSEAR_REG_MASK 0x03ff
  326. #define EFUSEAR_REG_SHIFT 8
  327. #define EFUSEAR_DATA_MASK 0xff
  328. MISC_1 = 0xf2,
  329. #define PFM_D3COLD_EN (1 << 6)
  330. };
  331. enum rtl8168_registers {
  332. LED_FREQ = 0x1a,
  333. EEE_LED = 0x1b,
  334. ERIDR = 0x70,
  335. ERIAR = 0x74,
  336. #define ERIAR_FLAG 0x80000000
  337. #define ERIAR_WRITE_CMD 0x80000000
  338. #define ERIAR_READ_CMD 0x00000000
  339. #define ERIAR_ADDR_BYTE_ALIGN 4
  340. #define ERIAR_TYPE_SHIFT 16
  341. #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
  342. #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
  343. #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
  344. #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
  345. #define ERIAR_MASK_SHIFT 12
  346. #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
  347. #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
  348. #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
  349. #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
  350. #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
  351. EPHY_RXER_NUM = 0x7c,
  352. OCPDR = 0xb0, /* OCP GPHY access */
  353. #define OCPDR_WRITE_CMD 0x80000000
  354. #define OCPDR_READ_CMD 0x00000000
  355. #define OCPDR_REG_MASK 0x7f
  356. #define OCPDR_GPHY_REG_SHIFT 16
  357. #define OCPDR_DATA_MASK 0xffff
  358. OCPAR = 0xb4,
  359. #define OCPAR_FLAG 0x80000000
  360. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  361. #define OCPAR_GPHY_READ_CMD 0x0000f060
  362. GPHY_OCP = 0xb8,
  363. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  364. MISC = 0xf0, /* 8168e only. */
  365. #define TXPLA_RST (1 << 29)
  366. #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
  367. #define PWM_EN (1 << 22)
  368. #define RXDV_GATED_EN (1 << 19)
  369. #define EARLY_TALLY_EN (1 << 16)
  370. };
  371. enum rtl_register_content {
  372. /* InterruptStatusBits */
  373. SYSErr = 0x8000,
  374. PCSTimeout = 0x4000,
  375. SWInt = 0x0100,
  376. TxDescUnavail = 0x0080,
  377. RxFIFOOver = 0x0040,
  378. LinkChg = 0x0020,
  379. RxOverflow = 0x0010,
  380. TxErr = 0x0008,
  381. TxOK = 0x0004,
  382. RxErr = 0x0002,
  383. RxOK = 0x0001,
  384. /* RxStatusDesc */
  385. RxBOVF = (1 << 24),
  386. RxFOVF = (1 << 23),
  387. RxRWT = (1 << 22),
  388. RxRES = (1 << 21),
  389. RxRUNT = (1 << 20),
  390. RxCRC = (1 << 19),
  391. /* ChipCmdBits */
  392. StopReq = 0x80,
  393. CmdReset = 0x10,
  394. CmdRxEnb = 0x08,
  395. CmdTxEnb = 0x04,
  396. RxBufEmpty = 0x01,
  397. /* TXPoll register p.5 */
  398. HPQ = 0x80, /* Poll cmd on the high prio queue */
  399. NPQ = 0x40, /* Poll cmd on the low prio queue */
  400. FSWInt = 0x01, /* Forced software interrupt */
  401. /* Cfg9346Bits */
  402. Cfg9346_Lock = 0x00,
  403. Cfg9346_Unlock = 0xc0,
  404. /* rx_mode_bits */
  405. AcceptErr = 0x20,
  406. AcceptRunt = 0x10,
  407. AcceptBroadcast = 0x08,
  408. AcceptMulticast = 0x04,
  409. AcceptMyPhys = 0x02,
  410. AcceptAllPhys = 0x01,
  411. #define RX_CONFIG_ACCEPT_MASK 0x3f
  412. /* TxConfigBits */
  413. TxInterFrameGapShift = 24,
  414. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  415. /* Config1 register p.24 */
  416. LEDS1 = (1 << 7),
  417. LEDS0 = (1 << 6),
  418. Speed_down = (1 << 4),
  419. MEMMAP = (1 << 3),
  420. IOMAP = (1 << 2),
  421. VPD = (1 << 1),
  422. PMEnable = (1 << 0), /* Power Management Enable */
  423. /* Config2 register p. 25 */
  424. ClkReqEn = (1 << 7), /* Clock Request Enable */
  425. MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
  426. PCI_Clock_66MHz = 0x01,
  427. PCI_Clock_33MHz = 0x00,
  428. /* Config3 register p.25 */
  429. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  430. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  431. Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
  432. Rdy_to_L23 = (1 << 1), /* L23 Enable */
  433. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  434. /* Config4 register */
  435. Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
  436. /* Config5 register p.27 */
  437. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  438. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  439. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  440. Spi_en = (1 << 3),
  441. LanWake = (1 << 1), /* LanWake enable/disable */
  442. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  443. ASPM_en = (1 << 0), /* ASPM enable */
  444. /* CPlusCmd p.31 */
  445. EnableBist = (1 << 15), // 8168 8101
  446. Mac_dbgo_oe = (1 << 14), // 8168 8101
  447. Normal_mode = (1 << 13), // unused
  448. Force_half_dup = (1 << 12), // 8168 8101
  449. Force_rxflow_en = (1 << 11), // 8168 8101
  450. Force_txflow_en = (1 << 10), // 8168 8101
  451. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  452. ASF = (1 << 8), // 8168 8101
  453. PktCntrDisable = (1 << 7), // 8168 8101
  454. Mac_dbgo_sel = 0x001c, // 8168
  455. RxVlan = (1 << 6),
  456. RxChkSum = (1 << 5),
  457. PCIDAC = (1 << 4),
  458. PCIMulRW = (1 << 3),
  459. #define INTT_MASK GENMASK(1, 0)
  460. INTT_0 = 0x0000, // 8168
  461. INTT_1 = 0x0001, // 8168
  462. INTT_2 = 0x0002, // 8168
  463. INTT_3 = 0x0003, // 8168
  464. /* rtl8169_PHYstatus */
  465. TBI_Enable = 0x80,
  466. TxFlowCtrl = 0x40,
  467. RxFlowCtrl = 0x20,
  468. _1000bpsF = 0x10,
  469. _100bps = 0x08,
  470. _10bps = 0x04,
  471. LinkStatus = 0x02,
  472. FullDup = 0x01,
  473. /* _TBICSRBit */
  474. TBILinkOK = 0x02000000,
  475. /* ResetCounterCommand */
  476. CounterReset = 0x1,
  477. /* DumpCounterCommand */
  478. CounterDump = 0x8,
  479. /* magic enable v2 */
  480. MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
  481. };
  482. enum rtl_desc_bit {
  483. /* First doubleword. */
  484. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  485. RingEnd = (1 << 30), /* End of descriptor ring */
  486. FirstFrag = (1 << 29), /* First segment of a packet */
  487. LastFrag = (1 << 28), /* Final segment of a packet */
  488. };
  489. /* Generic case. */
  490. enum rtl_tx_desc_bit {
  491. /* First doubleword. */
  492. TD_LSO = (1 << 27), /* Large Send Offload */
  493. #define TD_MSS_MAX 0x07ffu /* MSS value */
  494. /* Second doubleword. */
  495. TxVlanTag = (1 << 17), /* Add VLAN tag */
  496. };
  497. /* 8169, 8168b and 810x except 8102e. */
  498. enum rtl_tx_desc_bit_0 {
  499. /* First doubleword. */
  500. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  501. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  502. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  503. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  504. };
  505. /* 8102e, 8168c and beyond. */
  506. enum rtl_tx_desc_bit_1 {
  507. /* First doubleword. */
  508. TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
  509. TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
  510. #define GTTCPHO_SHIFT 18
  511. #define GTTCPHO_MAX 0x7fU
  512. /* Second doubleword. */
  513. #define TCPHO_SHIFT 18
  514. #define TCPHO_MAX 0x3ffU
  515. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  516. TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
  517. TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
  518. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  519. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  520. };
  521. enum rtl_rx_desc_bit {
  522. /* Rx private */
  523. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  524. PID0 = (1 << 17), /* Protocol ID bit 0/2 */
  525. #define RxProtoUDP (PID1)
  526. #define RxProtoTCP (PID0)
  527. #define RxProtoIP (PID1 | PID0)
  528. #define RxProtoMask RxProtoIP
  529. IPFail = (1 << 16), /* IP checksum failed */
  530. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  531. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  532. RxVlanTag = (1 << 16), /* VLAN tag available */
  533. };
  534. #define RsvdMask 0x3fffc000
  535. #define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
  536. struct TxDesc {
  537. __le32 opts1;
  538. __le32 opts2;
  539. __le64 addr;
  540. };
  541. struct RxDesc {
  542. __le32 opts1;
  543. __le32 opts2;
  544. __le64 addr;
  545. };
  546. struct ring_info {
  547. struct sk_buff *skb;
  548. u32 len;
  549. u8 __pad[sizeof(void *) - sizeof(u32)];
  550. };
  551. struct rtl8169_counters {
  552. __le64 tx_packets;
  553. __le64 rx_packets;
  554. __le64 tx_errors;
  555. __le32 rx_errors;
  556. __le16 rx_missed;
  557. __le16 align_errors;
  558. __le32 tx_one_collision;
  559. __le32 tx_multi_collision;
  560. __le64 rx_unicast;
  561. __le64 rx_broadcast;
  562. __le32 rx_multicast;
  563. __le16 tx_aborted;
  564. __le16 tx_underun;
  565. };
  566. struct rtl8169_tc_offsets {
  567. bool inited;
  568. __le64 tx_errors;
  569. __le32 tx_multi_collision;
  570. __le16 tx_aborted;
  571. };
  572. enum rtl_flag {
  573. RTL_FLAG_TASK_ENABLED = 0,
  574. RTL_FLAG_TASK_SLOW_PENDING,
  575. RTL_FLAG_TASK_RESET_PENDING,
  576. RTL_FLAG_MAX
  577. };
  578. struct rtl8169_stats {
  579. u64 packets;
  580. u64 bytes;
  581. struct u64_stats_sync syncp;
  582. };
  583. struct rtl8169_private {
  584. void __iomem *mmio_addr; /* memory map physical address */
  585. struct pci_dev *pci_dev;
  586. struct net_device *dev;
  587. struct napi_struct napi;
  588. u32 msg_enable;
  589. u16 mac_version;
  590. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  591. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  592. u32 dirty_tx;
  593. struct rtl8169_stats rx_stats;
  594. struct rtl8169_stats tx_stats;
  595. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  596. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  597. dma_addr_t TxPhyAddr;
  598. dma_addr_t RxPhyAddr;
  599. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  600. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  601. u16 cp_cmd;
  602. u16 event_slow;
  603. const struct rtl_coalesce_info *coalesce_info;
  604. struct clk *clk;
  605. struct mdio_ops {
  606. void (*write)(struct rtl8169_private *, int, int);
  607. int (*read)(struct rtl8169_private *, int);
  608. } mdio_ops;
  609. struct jumbo_ops {
  610. void (*enable)(struct rtl8169_private *);
  611. void (*disable)(struct rtl8169_private *);
  612. } jumbo_ops;
  613. void (*hw_start)(struct rtl8169_private *tp);
  614. bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
  615. struct {
  616. DECLARE_BITMAP(flags, RTL_FLAG_MAX);
  617. struct mutex mutex;
  618. struct work_struct work;
  619. } wk;
  620. unsigned supports_gmii:1;
  621. struct mii_bus *mii_bus;
  622. dma_addr_t counters_phys_addr;
  623. struct rtl8169_counters *counters;
  624. struct rtl8169_tc_offsets tc_offset;
  625. u32 saved_wolopts;
  626. struct rtl_fw {
  627. const struct firmware *fw;
  628. #define RTL_VER_SIZE 32
  629. char version[RTL_VER_SIZE];
  630. struct rtl_fw_phy_action {
  631. __le32 *code;
  632. size_t size;
  633. } phy_action;
  634. } *rtl_fw;
  635. #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
  636. u32 ocp_base;
  637. };
  638. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  639. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  640. module_param(use_dac, int, 0);
  641. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  642. module_param_named(debug, debug.msg_enable, int, 0);
  643. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  644. MODULE_SOFTDEP("pre: realtek");
  645. MODULE_LICENSE("GPL");
  646. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  647. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  648. MODULE_FIRMWARE(FIRMWARE_8168E_1);
  649. MODULE_FIRMWARE(FIRMWARE_8168E_2);
  650. MODULE_FIRMWARE(FIRMWARE_8168E_3);
  651. MODULE_FIRMWARE(FIRMWARE_8105E_1);
  652. MODULE_FIRMWARE(FIRMWARE_8168F_1);
  653. MODULE_FIRMWARE(FIRMWARE_8168F_2);
  654. MODULE_FIRMWARE(FIRMWARE_8402_1);
  655. MODULE_FIRMWARE(FIRMWARE_8411_1);
  656. MODULE_FIRMWARE(FIRMWARE_8411_2);
  657. MODULE_FIRMWARE(FIRMWARE_8106E_1);
  658. MODULE_FIRMWARE(FIRMWARE_8106E_2);
  659. MODULE_FIRMWARE(FIRMWARE_8168G_2);
  660. MODULE_FIRMWARE(FIRMWARE_8168G_3);
  661. MODULE_FIRMWARE(FIRMWARE_8168H_1);
  662. MODULE_FIRMWARE(FIRMWARE_8168H_2);
  663. MODULE_FIRMWARE(FIRMWARE_8107E_1);
  664. MODULE_FIRMWARE(FIRMWARE_8107E_2);
  665. static inline struct device *tp_to_dev(struct rtl8169_private *tp)
  666. {
  667. return &tp->pci_dev->dev;
  668. }
  669. static void rtl_lock_work(struct rtl8169_private *tp)
  670. {
  671. mutex_lock(&tp->wk.mutex);
  672. }
  673. static void rtl_unlock_work(struct rtl8169_private *tp)
  674. {
  675. mutex_unlock(&tp->wk.mutex);
  676. }
  677. static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
  678. {
  679. pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
  680. PCI_EXP_DEVCTL_READRQ, force);
  681. }
  682. struct rtl_cond {
  683. bool (*check)(struct rtl8169_private *);
  684. const char *msg;
  685. };
  686. static void rtl_udelay(unsigned int d)
  687. {
  688. udelay(d);
  689. }
  690. static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
  691. void (*delay)(unsigned int), unsigned int d, int n,
  692. bool high)
  693. {
  694. int i;
  695. for (i = 0; i < n; i++) {
  696. delay(d);
  697. if (c->check(tp) == high)
  698. return true;
  699. }
  700. netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
  701. c->msg, !high, n, d);
  702. return false;
  703. }
  704. static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
  705. const struct rtl_cond *c,
  706. unsigned int d, int n)
  707. {
  708. return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
  709. }
  710. static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
  711. const struct rtl_cond *c,
  712. unsigned int d, int n)
  713. {
  714. return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
  715. }
  716. static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
  717. const struct rtl_cond *c,
  718. unsigned int d, int n)
  719. {
  720. return rtl_loop_wait(tp, c, msleep, d, n, true);
  721. }
  722. static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
  723. const struct rtl_cond *c,
  724. unsigned int d, int n)
  725. {
  726. return rtl_loop_wait(tp, c, msleep, d, n, false);
  727. }
  728. #define DECLARE_RTL_COND(name) \
  729. static bool name ## _check(struct rtl8169_private *); \
  730. \
  731. static const struct rtl_cond name = { \
  732. .check = name ## _check, \
  733. .msg = #name \
  734. }; \
  735. \
  736. static bool name ## _check(struct rtl8169_private *tp)
  737. static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
  738. {
  739. if (reg & 0xffff0001) {
  740. netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
  741. return true;
  742. }
  743. return false;
  744. }
  745. DECLARE_RTL_COND(rtl_ocp_gphy_cond)
  746. {
  747. return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
  748. }
  749. static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  750. {
  751. if (rtl_ocp_reg_failure(tp, reg))
  752. return;
  753. RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
  754. rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
  755. }
  756. static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
  757. {
  758. if (rtl_ocp_reg_failure(tp, reg))
  759. return 0;
  760. RTL_W32(tp, GPHY_OCP, reg << 15);
  761. return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
  762. (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
  763. }
  764. static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  765. {
  766. if (rtl_ocp_reg_failure(tp, reg))
  767. return;
  768. RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
  769. }
  770. static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
  771. {
  772. if (rtl_ocp_reg_failure(tp, reg))
  773. return 0;
  774. RTL_W32(tp, OCPDR, reg << 15);
  775. return RTL_R32(tp, OCPDR);
  776. }
  777. #define OCP_STD_PHY_BASE 0xa400
  778. static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
  779. {
  780. if (reg == 0x1f) {
  781. tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
  782. return;
  783. }
  784. if (tp->ocp_base != OCP_STD_PHY_BASE)
  785. reg -= 0x10;
  786. r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
  787. }
  788. static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
  789. {
  790. if (tp->ocp_base != OCP_STD_PHY_BASE)
  791. reg -= 0x10;
  792. return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
  793. }
  794. static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
  795. {
  796. if (reg == 0x1f) {
  797. tp->ocp_base = value << 4;
  798. return;
  799. }
  800. r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
  801. }
  802. static int mac_mcu_read(struct rtl8169_private *tp, int reg)
  803. {
  804. return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
  805. }
  806. DECLARE_RTL_COND(rtl_phyar_cond)
  807. {
  808. return RTL_R32(tp, PHYAR) & 0x80000000;
  809. }
  810. static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
  811. {
  812. RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
  813. rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
  814. /*
  815. * According to hardware specs a 20us delay is required after write
  816. * complete indication, but before sending next command.
  817. */
  818. udelay(20);
  819. }
  820. static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
  821. {
  822. int value;
  823. RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
  824. value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
  825. RTL_R32(tp, PHYAR) & 0xffff : ~0;
  826. /*
  827. * According to hardware specs a 20us delay is required after read
  828. * complete indication, but before sending next command.
  829. */
  830. udelay(20);
  831. return value;
  832. }
  833. DECLARE_RTL_COND(rtl_ocpar_cond)
  834. {
  835. return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
  836. }
  837. static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
  838. {
  839. RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  840. RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
  841. RTL_W32(tp, EPHY_RXER_NUM, 0);
  842. rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
  843. }
  844. static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
  845. {
  846. r8168dp_1_mdio_access(tp, reg,
  847. OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
  848. }
  849. static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
  850. {
  851. r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
  852. mdelay(1);
  853. RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
  854. RTL_W32(tp, EPHY_RXER_NUM, 0);
  855. return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
  856. RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
  857. }
  858. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  859. static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
  860. {
  861. RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  862. }
  863. static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
  864. {
  865. RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  866. }
  867. static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
  868. {
  869. r8168dp_2_mdio_start(tp);
  870. r8169_mdio_write(tp, reg, value);
  871. r8168dp_2_mdio_stop(tp);
  872. }
  873. static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
  874. {
  875. int value;
  876. /* Work around issue with chip reporting wrong PHY ID */
  877. if (reg == MII_PHYSID2)
  878. return 0xc912;
  879. r8168dp_2_mdio_start(tp);
  880. value = r8169_mdio_read(tp, reg);
  881. r8168dp_2_mdio_stop(tp);
  882. return value;
  883. }
  884. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  885. {
  886. tp->mdio_ops.write(tp, location, val);
  887. }
  888. static int rtl_readphy(struct rtl8169_private *tp, int location)
  889. {
  890. return tp->mdio_ops.read(tp, location);
  891. }
  892. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  893. {
  894. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  895. }
  896. static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  897. {
  898. int val;
  899. val = rtl_readphy(tp, reg_addr);
  900. rtl_writephy(tp, reg_addr, (val & ~m) | p);
  901. }
  902. DECLARE_RTL_COND(rtl_ephyar_cond)
  903. {
  904. return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
  905. }
  906. static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
  907. {
  908. RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  909. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  910. rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
  911. udelay(10);
  912. }
  913. static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
  914. {
  915. RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  916. return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
  917. RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
  918. }
  919. DECLARE_RTL_COND(rtl_eriar_cond)
  920. {
  921. return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
  922. }
  923. static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
  924. u32 val, int type)
  925. {
  926. BUG_ON((addr & 3) || (mask == 0));
  927. RTL_W32(tp, ERIDR, val);
  928. RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
  929. rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
  930. }
  931. static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
  932. {
  933. RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
  934. return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
  935. RTL_R32(tp, ERIDR) : ~0;
  936. }
  937. static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
  938. u32 m, int type)
  939. {
  940. u32 val;
  941. val = rtl_eri_read(tp, addr, type);
  942. rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
  943. }
  944. static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  945. {
  946. RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  947. return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
  948. RTL_R32(tp, OCPDR) : ~0;
  949. }
  950. static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  951. {
  952. return rtl_eri_read(tp, reg, ERIAR_OOB);
  953. }
  954. static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  955. {
  956. switch (tp->mac_version) {
  957. case RTL_GIGA_MAC_VER_27:
  958. case RTL_GIGA_MAC_VER_28:
  959. case RTL_GIGA_MAC_VER_31:
  960. return r8168dp_ocp_read(tp, mask, reg);
  961. case RTL_GIGA_MAC_VER_49:
  962. case RTL_GIGA_MAC_VER_50:
  963. case RTL_GIGA_MAC_VER_51:
  964. return r8168ep_ocp_read(tp, mask, reg);
  965. default:
  966. BUG();
  967. return ~0;
  968. }
  969. }
  970. static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  971. u32 data)
  972. {
  973. RTL_W32(tp, OCPDR, data);
  974. RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  975. rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
  976. }
  977. static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  978. u32 data)
  979. {
  980. rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
  981. data, ERIAR_OOB);
  982. }
  983. static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
  984. {
  985. switch (tp->mac_version) {
  986. case RTL_GIGA_MAC_VER_27:
  987. case RTL_GIGA_MAC_VER_28:
  988. case RTL_GIGA_MAC_VER_31:
  989. r8168dp_ocp_write(tp, mask, reg, data);
  990. break;
  991. case RTL_GIGA_MAC_VER_49:
  992. case RTL_GIGA_MAC_VER_50:
  993. case RTL_GIGA_MAC_VER_51:
  994. r8168ep_ocp_write(tp, mask, reg, data);
  995. break;
  996. default:
  997. BUG();
  998. break;
  999. }
  1000. }
  1001. static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
  1002. {
  1003. rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
  1004. ocp_write(tp, 0x1, 0x30, 0x00000001);
  1005. }
  1006. #define OOB_CMD_RESET 0x00
  1007. #define OOB_CMD_DRIVER_START 0x05
  1008. #define OOB_CMD_DRIVER_STOP 0x06
  1009. static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
  1010. {
  1011. return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
  1012. }
  1013. DECLARE_RTL_COND(rtl_ocp_read_cond)
  1014. {
  1015. u16 reg;
  1016. reg = rtl8168_get_ocp_reg(tp);
  1017. return ocp_read(tp, 0x0f, reg) & 0x00000800;
  1018. }
  1019. DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
  1020. {
  1021. return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
  1022. }
  1023. DECLARE_RTL_COND(rtl_ocp_tx_cond)
  1024. {
  1025. return RTL_R8(tp, IBISR0) & 0x20;
  1026. }
  1027. static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
  1028. {
  1029. RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
  1030. rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
  1031. RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
  1032. RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
  1033. }
  1034. static void rtl8168dp_driver_start(struct rtl8169_private *tp)
  1035. {
  1036. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  1037. rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
  1038. }
  1039. static void rtl8168ep_driver_start(struct rtl8169_private *tp)
  1040. {
  1041. ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
  1042. ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
  1043. rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
  1044. }
  1045. static void rtl8168_driver_start(struct rtl8169_private *tp)
  1046. {
  1047. switch (tp->mac_version) {
  1048. case RTL_GIGA_MAC_VER_27:
  1049. case RTL_GIGA_MAC_VER_28:
  1050. case RTL_GIGA_MAC_VER_31:
  1051. rtl8168dp_driver_start(tp);
  1052. break;
  1053. case RTL_GIGA_MAC_VER_49:
  1054. case RTL_GIGA_MAC_VER_50:
  1055. case RTL_GIGA_MAC_VER_51:
  1056. rtl8168ep_driver_start(tp);
  1057. break;
  1058. default:
  1059. BUG();
  1060. break;
  1061. }
  1062. }
  1063. static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
  1064. {
  1065. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  1066. rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
  1067. }
  1068. static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
  1069. {
  1070. rtl8168ep_stop_cmac(tp);
  1071. ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
  1072. ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
  1073. rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
  1074. }
  1075. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  1076. {
  1077. switch (tp->mac_version) {
  1078. case RTL_GIGA_MAC_VER_27:
  1079. case RTL_GIGA_MAC_VER_28:
  1080. case RTL_GIGA_MAC_VER_31:
  1081. rtl8168dp_driver_stop(tp);
  1082. break;
  1083. case RTL_GIGA_MAC_VER_49:
  1084. case RTL_GIGA_MAC_VER_50:
  1085. case RTL_GIGA_MAC_VER_51:
  1086. rtl8168ep_driver_stop(tp);
  1087. break;
  1088. default:
  1089. BUG();
  1090. break;
  1091. }
  1092. }
  1093. static bool r8168dp_check_dash(struct rtl8169_private *tp)
  1094. {
  1095. u16 reg = rtl8168_get_ocp_reg(tp);
  1096. return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
  1097. }
  1098. static bool r8168ep_check_dash(struct rtl8169_private *tp)
  1099. {
  1100. return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
  1101. }
  1102. static bool r8168_check_dash(struct rtl8169_private *tp)
  1103. {
  1104. switch (tp->mac_version) {
  1105. case RTL_GIGA_MAC_VER_27:
  1106. case RTL_GIGA_MAC_VER_28:
  1107. case RTL_GIGA_MAC_VER_31:
  1108. return r8168dp_check_dash(tp);
  1109. case RTL_GIGA_MAC_VER_49:
  1110. case RTL_GIGA_MAC_VER_50:
  1111. case RTL_GIGA_MAC_VER_51:
  1112. return r8168ep_check_dash(tp);
  1113. default:
  1114. return false;
  1115. }
  1116. }
  1117. struct exgmac_reg {
  1118. u16 addr;
  1119. u16 mask;
  1120. u32 val;
  1121. };
  1122. static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
  1123. const struct exgmac_reg *r, int len)
  1124. {
  1125. while (len-- > 0) {
  1126. rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
  1127. r++;
  1128. }
  1129. }
  1130. DECLARE_RTL_COND(rtl_efusear_cond)
  1131. {
  1132. return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
  1133. }
  1134. static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
  1135. {
  1136. RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  1137. return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
  1138. RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
  1139. }
  1140. static u16 rtl_get_events(struct rtl8169_private *tp)
  1141. {
  1142. return RTL_R16(tp, IntrStatus);
  1143. }
  1144. static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
  1145. {
  1146. RTL_W16(tp, IntrStatus, bits);
  1147. mmiowb();
  1148. }
  1149. static void rtl_irq_disable(struct rtl8169_private *tp)
  1150. {
  1151. RTL_W16(tp, IntrMask, 0);
  1152. mmiowb();
  1153. }
  1154. static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
  1155. {
  1156. RTL_W16(tp, IntrMask, bits);
  1157. }
  1158. #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
  1159. #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
  1160. #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
  1161. static void rtl_irq_enable_all(struct rtl8169_private *tp)
  1162. {
  1163. rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
  1164. }
  1165. static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
  1166. {
  1167. rtl_irq_disable(tp);
  1168. rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
  1169. RTL_R8(tp, ChipCmd);
  1170. }
  1171. static void rtl_link_chg_patch(struct rtl8169_private *tp)
  1172. {
  1173. struct net_device *dev = tp->dev;
  1174. struct phy_device *phydev = dev->phydev;
  1175. if (!netif_running(dev))
  1176. return;
  1177. if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  1178. tp->mac_version == RTL_GIGA_MAC_VER_38) {
  1179. if (phydev->speed == SPEED_1000) {
  1180. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
  1181. ERIAR_EXGMAC);
  1182. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1183. ERIAR_EXGMAC);
  1184. } else if (phydev->speed == SPEED_100) {
  1185. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1186. ERIAR_EXGMAC);
  1187. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1188. ERIAR_EXGMAC);
  1189. } else {
  1190. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1191. ERIAR_EXGMAC);
  1192. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
  1193. ERIAR_EXGMAC);
  1194. }
  1195. /* Reset packet filter */
  1196. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
  1197. ERIAR_EXGMAC);
  1198. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
  1199. ERIAR_EXGMAC);
  1200. } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  1201. tp->mac_version == RTL_GIGA_MAC_VER_36) {
  1202. if (phydev->speed == SPEED_1000) {
  1203. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
  1204. ERIAR_EXGMAC);
  1205. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1206. ERIAR_EXGMAC);
  1207. } else {
  1208. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1209. ERIAR_EXGMAC);
  1210. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
  1211. ERIAR_EXGMAC);
  1212. }
  1213. } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
  1214. if (phydev->speed == SPEED_10) {
  1215. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
  1216. ERIAR_EXGMAC);
  1217. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
  1218. ERIAR_EXGMAC);
  1219. } else {
  1220. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
  1221. ERIAR_EXGMAC);
  1222. }
  1223. }
  1224. }
  1225. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1226. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  1227. {
  1228. u8 options;
  1229. u32 wolopts = 0;
  1230. options = RTL_R8(tp, Config1);
  1231. if (!(options & PMEnable))
  1232. return 0;
  1233. options = RTL_R8(tp, Config3);
  1234. if (options & LinkUp)
  1235. wolopts |= WAKE_PHY;
  1236. switch (tp->mac_version) {
  1237. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  1238. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  1239. if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
  1240. wolopts |= WAKE_MAGIC;
  1241. break;
  1242. default:
  1243. if (options & MagicPacket)
  1244. wolopts |= WAKE_MAGIC;
  1245. break;
  1246. }
  1247. options = RTL_R8(tp, Config5);
  1248. if (options & UWF)
  1249. wolopts |= WAKE_UCAST;
  1250. if (options & BWF)
  1251. wolopts |= WAKE_BCAST;
  1252. if (options & MWF)
  1253. wolopts |= WAKE_MCAST;
  1254. return wolopts;
  1255. }
  1256. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1257. {
  1258. struct rtl8169_private *tp = netdev_priv(dev);
  1259. rtl_lock_work(tp);
  1260. wol->supported = WAKE_ANY;
  1261. wol->wolopts = tp->saved_wolopts;
  1262. rtl_unlock_work(tp);
  1263. }
  1264. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  1265. {
  1266. unsigned int i, tmp;
  1267. static const struct {
  1268. u32 opt;
  1269. u16 reg;
  1270. u8 mask;
  1271. } cfg[] = {
  1272. { WAKE_PHY, Config3, LinkUp },
  1273. { WAKE_UCAST, Config5, UWF },
  1274. { WAKE_BCAST, Config5, BWF },
  1275. { WAKE_MCAST, Config5, MWF },
  1276. { WAKE_ANY, Config5, LanWake },
  1277. { WAKE_MAGIC, Config3, MagicPacket }
  1278. };
  1279. u8 options;
  1280. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  1281. switch (tp->mac_version) {
  1282. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  1283. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  1284. tmp = ARRAY_SIZE(cfg) - 1;
  1285. if (wolopts & WAKE_MAGIC)
  1286. rtl_w0w1_eri(tp,
  1287. 0x0dc,
  1288. ERIAR_MASK_0100,
  1289. MagicPacket_v2,
  1290. 0x0000,
  1291. ERIAR_EXGMAC);
  1292. else
  1293. rtl_w0w1_eri(tp,
  1294. 0x0dc,
  1295. ERIAR_MASK_0100,
  1296. 0x0000,
  1297. MagicPacket_v2,
  1298. ERIAR_EXGMAC);
  1299. break;
  1300. default:
  1301. tmp = ARRAY_SIZE(cfg);
  1302. break;
  1303. }
  1304. for (i = 0; i < tmp; i++) {
  1305. options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
  1306. if (wolopts & cfg[i].opt)
  1307. options |= cfg[i].mask;
  1308. RTL_W8(tp, cfg[i].reg, options);
  1309. }
  1310. switch (tp->mac_version) {
  1311. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
  1312. options = RTL_R8(tp, Config1) & ~PMEnable;
  1313. if (wolopts)
  1314. options |= PMEnable;
  1315. RTL_W8(tp, Config1, options);
  1316. break;
  1317. default:
  1318. options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
  1319. if (wolopts)
  1320. options |= PME_SIGNAL;
  1321. RTL_W8(tp, Config2, options);
  1322. break;
  1323. }
  1324. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  1325. device_set_wakeup_enable(tp_to_dev(tp), wolopts);
  1326. }
  1327. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1328. {
  1329. struct rtl8169_private *tp = netdev_priv(dev);
  1330. struct device *d = tp_to_dev(tp);
  1331. if (wol->wolopts & ~WAKE_ANY)
  1332. return -EINVAL;
  1333. pm_runtime_get_noresume(d);
  1334. rtl_lock_work(tp);
  1335. tp->saved_wolopts = wol->wolopts;
  1336. if (pm_runtime_active(d))
  1337. __rtl8169_set_wol(tp, tp->saved_wolopts);
  1338. rtl_unlock_work(tp);
  1339. pm_runtime_put_noidle(d);
  1340. return 0;
  1341. }
  1342. static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
  1343. {
  1344. return rtl_chip_infos[tp->mac_version].fw_name;
  1345. }
  1346. static void rtl8169_get_drvinfo(struct net_device *dev,
  1347. struct ethtool_drvinfo *info)
  1348. {
  1349. struct rtl8169_private *tp = netdev_priv(dev);
  1350. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1351. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  1352. strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
  1353. BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
  1354. if (!IS_ERR_OR_NULL(rtl_fw))
  1355. strlcpy(info->fw_version, rtl_fw->version,
  1356. sizeof(info->fw_version));
  1357. }
  1358. static int rtl8169_get_regs_len(struct net_device *dev)
  1359. {
  1360. return R8169_REGS_SIZE;
  1361. }
  1362. static netdev_features_t rtl8169_fix_features(struct net_device *dev,
  1363. netdev_features_t features)
  1364. {
  1365. struct rtl8169_private *tp = netdev_priv(dev);
  1366. if (dev->mtu > TD_MSS_MAX)
  1367. features &= ~NETIF_F_ALL_TSO;
  1368. if (dev->mtu > JUMBO_1K &&
  1369. tp->mac_version > RTL_GIGA_MAC_VER_06)
  1370. features &= ~NETIF_F_IP_CSUM;
  1371. return features;
  1372. }
  1373. static int rtl8169_set_features(struct net_device *dev,
  1374. netdev_features_t features)
  1375. {
  1376. struct rtl8169_private *tp = netdev_priv(dev);
  1377. u32 rx_config;
  1378. rtl_lock_work(tp);
  1379. rx_config = RTL_R32(tp, RxConfig);
  1380. if (features & NETIF_F_RXALL)
  1381. rx_config |= (AcceptErr | AcceptRunt);
  1382. else
  1383. rx_config &= ~(AcceptErr | AcceptRunt);
  1384. RTL_W32(tp, RxConfig, rx_config);
  1385. if (features & NETIF_F_RXCSUM)
  1386. tp->cp_cmd |= RxChkSum;
  1387. else
  1388. tp->cp_cmd &= ~RxChkSum;
  1389. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1390. tp->cp_cmd |= RxVlan;
  1391. else
  1392. tp->cp_cmd &= ~RxVlan;
  1393. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  1394. RTL_R16(tp, CPlusCmd);
  1395. rtl_unlock_work(tp);
  1396. return 0;
  1397. }
  1398. static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
  1399. {
  1400. return (skb_vlan_tag_present(skb)) ?
  1401. TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
  1402. }
  1403. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1404. {
  1405. u32 opts2 = le32_to_cpu(desc->opts2);
  1406. if (opts2 & RxVlanTag)
  1407. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
  1408. }
  1409. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1410. void *p)
  1411. {
  1412. struct rtl8169_private *tp = netdev_priv(dev);
  1413. u32 __iomem *data = tp->mmio_addr;
  1414. u32 *dw = p;
  1415. int i;
  1416. rtl_lock_work(tp);
  1417. for (i = 0; i < R8169_REGS_SIZE; i += 4)
  1418. memcpy_fromio(dw++, data++, 4);
  1419. rtl_unlock_work(tp);
  1420. }
  1421. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1422. {
  1423. struct rtl8169_private *tp = netdev_priv(dev);
  1424. return tp->msg_enable;
  1425. }
  1426. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1427. {
  1428. struct rtl8169_private *tp = netdev_priv(dev);
  1429. tp->msg_enable = value;
  1430. }
  1431. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1432. "tx_packets",
  1433. "rx_packets",
  1434. "tx_errors",
  1435. "rx_errors",
  1436. "rx_missed",
  1437. "align_errors",
  1438. "tx_single_collisions",
  1439. "tx_multi_collisions",
  1440. "unicast",
  1441. "broadcast",
  1442. "multicast",
  1443. "tx_aborted",
  1444. "tx_underrun",
  1445. };
  1446. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1447. {
  1448. switch (sset) {
  1449. case ETH_SS_STATS:
  1450. return ARRAY_SIZE(rtl8169_gstrings);
  1451. default:
  1452. return -EOPNOTSUPP;
  1453. }
  1454. }
  1455. DECLARE_RTL_COND(rtl_counters_cond)
  1456. {
  1457. return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
  1458. }
  1459. static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
  1460. {
  1461. dma_addr_t paddr = tp->counters_phys_addr;
  1462. u32 cmd;
  1463. RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
  1464. RTL_R32(tp, CounterAddrHigh);
  1465. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1466. RTL_W32(tp, CounterAddrLow, cmd);
  1467. RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
  1468. return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
  1469. }
  1470. static bool rtl8169_reset_counters(struct rtl8169_private *tp)
  1471. {
  1472. /*
  1473. * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
  1474. * tally counters.
  1475. */
  1476. if (tp->mac_version < RTL_GIGA_MAC_VER_19)
  1477. return true;
  1478. return rtl8169_do_counters(tp, CounterReset);
  1479. }
  1480. static bool rtl8169_update_counters(struct rtl8169_private *tp)
  1481. {
  1482. u8 val = RTL_R8(tp, ChipCmd);
  1483. /*
  1484. * Some chips are unable to dump tally counters when the receiver
  1485. * is disabled. If 0xff chip may be in a PCI power-save state.
  1486. */
  1487. if (!(val & CmdRxEnb) || val == 0xff)
  1488. return true;
  1489. return rtl8169_do_counters(tp, CounterDump);
  1490. }
  1491. static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
  1492. {
  1493. struct rtl8169_counters *counters = tp->counters;
  1494. bool ret = false;
  1495. /*
  1496. * rtl8169_init_counter_offsets is called from rtl_open. On chip
  1497. * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
  1498. * reset by a power cycle, while the counter values collected by the
  1499. * driver are reset at every driver unload/load cycle.
  1500. *
  1501. * To make sure the HW values returned by @get_stats64 match the SW
  1502. * values, we collect the initial values at first open(*) and use them
  1503. * as offsets to normalize the values returned by @get_stats64.
  1504. *
  1505. * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
  1506. * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
  1507. * set at open time by rtl_hw_start.
  1508. */
  1509. if (tp->tc_offset.inited)
  1510. return true;
  1511. /* If both, reset and update fail, propagate to caller. */
  1512. if (rtl8169_reset_counters(tp))
  1513. ret = true;
  1514. if (rtl8169_update_counters(tp))
  1515. ret = true;
  1516. tp->tc_offset.tx_errors = counters->tx_errors;
  1517. tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
  1518. tp->tc_offset.tx_aborted = counters->tx_aborted;
  1519. tp->tc_offset.inited = true;
  1520. return ret;
  1521. }
  1522. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1523. struct ethtool_stats *stats, u64 *data)
  1524. {
  1525. struct rtl8169_private *tp = netdev_priv(dev);
  1526. struct device *d = tp_to_dev(tp);
  1527. struct rtl8169_counters *counters = tp->counters;
  1528. ASSERT_RTNL();
  1529. pm_runtime_get_noresume(d);
  1530. if (pm_runtime_active(d))
  1531. rtl8169_update_counters(tp);
  1532. pm_runtime_put_noidle(d);
  1533. data[0] = le64_to_cpu(counters->tx_packets);
  1534. data[1] = le64_to_cpu(counters->rx_packets);
  1535. data[2] = le64_to_cpu(counters->tx_errors);
  1536. data[3] = le32_to_cpu(counters->rx_errors);
  1537. data[4] = le16_to_cpu(counters->rx_missed);
  1538. data[5] = le16_to_cpu(counters->align_errors);
  1539. data[6] = le32_to_cpu(counters->tx_one_collision);
  1540. data[7] = le32_to_cpu(counters->tx_multi_collision);
  1541. data[8] = le64_to_cpu(counters->rx_unicast);
  1542. data[9] = le64_to_cpu(counters->rx_broadcast);
  1543. data[10] = le32_to_cpu(counters->rx_multicast);
  1544. data[11] = le16_to_cpu(counters->tx_aborted);
  1545. data[12] = le16_to_cpu(counters->tx_underun);
  1546. }
  1547. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1548. {
  1549. switch(stringset) {
  1550. case ETH_SS_STATS:
  1551. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1552. break;
  1553. }
  1554. }
  1555. /*
  1556. * Interrupt coalescing
  1557. *
  1558. * > 1 - the availability of the IntrMitigate (0xe2) register through the
  1559. * > 8169, 8168 and 810x line of chipsets
  1560. *
  1561. * 8169, 8168, and 8136(810x) serial chipsets support it.
  1562. *
  1563. * > 2 - the Tx timer unit at gigabit speed
  1564. *
  1565. * The unit of the timer depends on both the speed and the setting of CPlusCmd
  1566. * (0xe0) bit 1 and bit 0.
  1567. *
  1568. * For 8169
  1569. * bit[1:0] \ speed 1000M 100M 10M
  1570. * 0 0 320ns 2.56us 40.96us
  1571. * 0 1 2.56us 20.48us 327.7us
  1572. * 1 0 5.12us 40.96us 655.4us
  1573. * 1 1 10.24us 81.92us 1.31ms
  1574. *
  1575. * For the other
  1576. * bit[1:0] \ speed 1000M 100M 10M
  1577. * 0 0 5us 2.56us 40.96us
  1578. * 0 1 40us 20.48us 327.7us
  1579. * 1 0 80us 40.96us 655.4us
  1580. * 1 1 160us 81.92us 1.31ms
  1581. */
  1582. /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
  1583. struct rtl_coalesce_scale {
  1584. /* Rx / Tx */
  1585. u32 nsecs[2];
  1586. };
  1587. /* rx/tx scale factors for all CPlusCmd[0:1] cases */
  1588. struct rtl_coalesce_info {
  1589. u32 speed;
  1590. struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
  1591. };
  1592. /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
  1593. #define rxtx_x1822(r, t) { \
  1594. {{(r), (t)}}, \
  1595. {{(r)*8, (t)*8}}, \
  1596. {{(r)*8*2, (t)*8*2}}, \
  1597. {{(r)*8*2*2, (t)*8*2*2}}, \
  1598. }
  1599. static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
  1600. /* speed delays: rx00 tx00 */
  1601. { SPEED_10, rxtx_x1822(40960, 40960) },
  1602. { SPEED_100, rxtx_x1822( 2560, 2560) },
  1603. { SPEED_1000, rxtx_x1822( 320, 320) },
  1604. { 0 },
  1605. };
  1606. static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
  1607. /* speed delays: rx00 tx00 */
  1608. { SPEED_10, rxtx_x1822(40960, 40960) },
  1609. { SPEED_100, rxtx_x1822( 2560, 2560) },
  1610. { SPEED_1000, rxtx_x1822( 5000, 5000) },
  1611. { 0 },
  1612. };
  1613. #undef rxtx_x1822
  1614. /* get rx/tx scale vector corresponding to current speed */
  1615. static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
  1616. {
  1617. struct rtl8169_private *tp = netdev_priv(dev);
  1618. struct ethtool_link_ksettings ecmd;
  1619. const struct rtl_coalesce_info *ci;
  1620. int rc;
  1621. rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
  1622. if (rc < 0)
  1623. return ERR_PTR(rc);
  1624. for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
  1625. if (ecmd.base.speed == ci->speed) {
  1626. return ci;
  1627. }
  1628. }
  1629. return ERR_PTR(-ELNRNG);
  1630. }
  1631. static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1632. {
  1633. struct rtl8169_private *tp = netdev_priv(dev);
  1634. const struct rtl_coalesce_info *ci;
  1635. const struct rtl_coalesce_scale *scale;
  1636. struct {
  1637. u32 *max_frames;
  1638. u32 *usecs;
  1639. } coal_settings [] = {
  1640. { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
  1641. { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
  1642. }, *p = coal_settings;
  1643. int i;
  1644. u16 w;
  1645. memset(ec, 0, sizeof(*ec));
  1646. /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
  1647. ci = rtl_coalesce_info(dev);
  1648. if (IS_ERR(ci))
  1649. return PTR_ERR(ci);
  1650. scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
  1651. /* read IntrMitigate and adjust according to scale */
  1652. for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
  1653. *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
  1654. w >>= RTL_COALESCE_SHIFT;
  1655. *p->usecs = w & RTL_COALESCE_MASK;
  1656. }
  1657. for (i = 0; i < 2; i++) {
  1658. p = coal_settings + i;
  1659. *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
  1660. /*
  1661. * ethtool_coalesce says it is illegal to set both usecs and
  1662. * max_frames to 0.
  1663. */
  1664. if (!*p->usecs && !*p->max_frames)
  1665. *p->max_frames = 1;
  1666. }
  1667. return 0;
  1668. }
  1669. /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
  1670. static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
  1671. struct net_device *dev, u32 nsec, u16 *cp01)
  1672. {
  1673. const struct rtl_coalesce_info *ci;
  1674. u16 i;
  1675. ci = rtl_coalesce_info(dev);
  1676. if (IS_ERR(ci))
  1677. return ERR_CAST(ci);
  1678. for (i = 0; i < 4; i++) {
  1679. u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
  1680. ci->scalev[i].nsecs[1]);
  1681. if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
  1682. *cp01 = i;
  1683. return &ci->scalev[i];
  1684. }
  1685. }
  1686. return ERR_PTR(-EINVAL);
  1687. }
  1688. static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1689. {
  1690. struct rtl8169_private *tp = netdev_priv(dev);
  1691. const struct rtl_coalesce_scale *scale;
  1692. struct {
  1693. u32 frames;
  1694. u32 usecs;
  1695. } coal_settings [] = {
  1696. { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
  1697. { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
  1698. }, *p = coal_settings;
  1699. u16 w = 0, cp01;
  1700. int i;
  1701. scale = rtl_coalesce_choose_scale(dev,
  1702. max(p[0].usecs, p[1].usecs) * 1000, &cp01);
  1703. if (IS_ERR(scale))
  1704. return PTR_ERR(scale);
  1705. for (i = 0; i < 2; i++, p++) {
  1706. u32 units;
  1707. /*
  1708. * accept max_frames=1 we returned in rtl_get_coalesce.
  1709. * accept it not only when usecs=0 because of e.g. the following scenario:
  1710. *
  1711. * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
  1712. * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
  1713. * - then user does `ethtool -C eth0 rx-usecs 100`
  1714. *
  1715. * since ethtool sends to kernel whole ethtool_coalesce
  1716. * settings, if we do not handle rx_usecs=!0, rx_frames=1
  1717. * we'll reject it below in `frames % 4 != 0`.
  1718. */
  1719. if (p->frames == 1) {
  1720. p->frames = 0;
  1721. }
  1722. units = p->usecs * 1000 / scale->nsecs[i];
  1723. if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
  1724. return -EINVAL;
  1725. w <<= RTL_COALESCE_SHIFT;
  1726. w |= units;
  1727. w <<= RTL_COALESCE_SHIFT;
  1728. w |= p->frames >> 2;
  1729. }
  1730. rtl_lock_work(tp);
  1731. RTL_W16(tp, IntrMitigate, swab16(w));
  1732. tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
  1733. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  1734. RTL_R16(tp, CPlusCmd);
  1735. rtl_unlock_work(tp);
  1736. return 0;
  1737. }
  1738. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1739. .get_drvinfo = rtl8169_get_drvinfo,
  1740. .get_regs_len = rtl8169_get_regs_len,
  1741. .get_link = ethtool_op_get_link,
  1742. .get_coalesce = rtl_get_coalesce,
  1743. .set_coalesce = rtl_set_coalesce,
  1744. .get_msglevel = rtl8169_get_msglevel,
  1745. .set_msglevel = rtl8169_set_msglevel,
  1746. .get_regs = rtl8169_get_regs,
  1747. .get_wol = rtl8169_get_wol,
  1748. .set_wol = rtl8169_set_wol,
  1749. .get_strings = rtl8169_get_strings,
  1750. .get_sset_count = rtl8169_get_sset_count,
  1751. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1752. .get_ts_info = ethtool_op_get_ts_info,
  1753. .nway_reset = phy_ethtool_nway_reset,
  1754. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1755. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1756. };
  1757. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1758. u8 default_version)
  1759. {
  1760. /*
  1761. * The driver currently handles the 8168Bf and the 8168Be identically
  1762. * but they can be identified more specifically through the test below
  1763. * if needed:
  1764. *
  1765. * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1766. *
  1767. * Same thing for the 8101Eb and the 8101Ec:
  1768. *
  1769. * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1770. */
  1771. static const struct rtl_mac_info {
  1772. u32 mask;
  1773. u32 val;
  1774. int mac_version;
  1775. } mac_info[] = {
  1776. /* 8168EP family. */
  1777. { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
  1778. { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
  1779. { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
  1780. /* 8168H family. */
  1781. { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
  1782. { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
  1783. /* 8168G family. */
  1784. { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
  1785. { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
  1786. { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
  1787. { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
  1788. /* 8168F family. */
  1789. { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
  1790. { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
  1791. { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
  1792. /* 8168E family. */
  1793. { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
  1794. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  1795. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  1796. /* 8168D family. */
  1797. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1798. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1799. /* 8168DP family. */
  1800. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1801. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  1802. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  1803. /* 8168C family. */
  1804. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1805. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1806. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1807. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1808. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1809. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1810. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1811. /* 8168B family. */
  1812. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1813. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1814. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1815. /* 8101 family. */
  1816. { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
  1817. { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
  1818. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  1819. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  1820. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1821. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1822. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1823. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1824. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1825. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1826. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1827. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1828. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1829. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1830. /* FIXME: where did these entries come from ? -- FR */
  1831. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1832. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1833. /* 8110 family. */
  1834. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1835. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1836. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1837. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1838. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1839. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1840. /* Catch-all */
  1841. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1842. };
  1843. const struct rtl_mac_info *p = mac_info;
  1844. u32 reg;
  1845. reg = RTL_R32(tp, TxConfig);
  1846. while ((reg & p->mask) != p->val)
  1847. p++;
  1848. tp->mac_version = p->mac_version;
  1849. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  1850. dev_notice(tp_to_dev(tp),
  1851. "unknown MAC, using family default\n");
  1852. tp->mac_version = default_version;
  1853. } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
  1854. tp->mac_version = tp->supports_gmii ?
  1855. RTL_GIGA_MAC_VER_42 :
  1856. RTL_GIGA_MAC_VER_43;
  1857. } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
  1858. tp->mac_version = tp->supports_gmii ?
  1859. RTL_GIGA_MAC_VER_45 :
  1860. RTL_GIGA_MAC_VER_47;
  1861. } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
  1862. tp->mac_version = tp->supports_gmii ?
  1863. RTL_GIGA_MAC_VER_46 :
  1864. RTL_GIGA_MAC_VER_48;
  1865. }
  1866. }
  1867. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1868. {
  1869. netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
  1870. }
  1871. struct phy_reg {
  1872. u16 reg;
  1873. u16 val;
  1874. };
  1875. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1876. const struct phy_reg *regs, int len)
  1877. {
  1878. while (len-- > 0) {
  1879. rtl_writephy(tp, regs->reg, regs->val);
  1880. regs++;
  1881. }
  1882. }
  1883. #define PHY_READ 0x00000000
  1884. #define PHY_DATA_OR 0x10000000
  1885. #define PHY_DATA_AND 0x20000000
  1886. #define PHY_BJMPN 0x30000000
  1887. #define PHY_MDIO_CHG 0x40000000
  1888. #define PHY_CLEAR_READCOUNT 0x70000000
  1889. #define PHY_WRITE 0x80000000
  1890. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1891. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1892. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1893. #define PHY_WRITE_PREVIOUS 0xc0000000
  1894. #define PHY_SKIPN 0xd0000000
  1895. #define PHY_DELAY_MS 0xe0000000
  1896. struct fw_info {
  1897. u32 magic;
  1898. char version[RTL_VER_SIZE];
  1899. __le32 fw_start;
  1900. __le32 fw_len;
  1901. u8 chksum;
  1902. } __packed;
  1903. #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
  1904. static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1905. {
  1906. const struct firmware *fw = rtl_fw->fw;
  1907. struct fw_info *fw_info = (struct fw_info *)fw->data;
  1908. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  1909. char *version = rtl_fw->version;
  1910. bool rc = false;
  1911. if (fw->size < FW_OPCODE_SIZE)
  1912. goto out;
  1913. if (!fw_info->magic) {
  1914. size_t i, size, start;
  1915. u8 checksum = 0;
  1916. if (fw->size < sizeof(*fw_info))
  1917. goto out;
  1918. for (i = 0; i < fw->size; i++)
  1919. checksum += fw->data[i];
  1920. if (checksum != 0)
  1921. goto out;
  1922. start = le32_to_cpu(fw_info->fw_start);
  1923. if (start > fw->size)
  1924. goto out;
  1925. size = le32_to_cpu(fw_info->fw_len);
  1926. if (size > (fw->size - start) / FW_OPCODE_SIZE)
  1927. goto out;
  1928. memcpy(version, fw_info->version, RTL_VER_SIZE);
  1929. pa->code = (__le32 *)(fw->data + start);
  1930. pa->size = size;
  1931. } else {
  1932. if (fw->size % FW_OPCODE_SIZE)
  1933. goto out;
  1934. strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
  1935. pa->code = (__le32 *)fw->data;
  1936. pa->size = fw->size / FW_OPCODE_SIZE;
  1937. }
  1938. version[RTL_VER_SIZE - 1] = 0;
  1939. rc = true;
  1940. out:
  1941. return rc;
  1942. }
  1943. static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
  1944. struct rtl_fw_phy_action *pa)
  1945. {
  1946. bool rc = false;
  1947. size_t index;
  1948. for (index = 0; index < pa->size; index++) {
  1949. u32 action = le32_to_cpu(pa->code[index]);
  1950. u32 regno = (action & 0x0fff0000) >> 16;
  1951. switch(action & 0xf0000000) {
  1952. case PHY_READ:
  1953. case PHY_DATA_OR:
  1954. case PHY_DATA_AND:
  1955. case PHY_MDIO_CHG:
  1956. case PHY_CLEAR_READCOUNT:
  1957. case PHY_WRITE:
  1958. case PHY_WRITE_PREVIOUS:
  1959. case PHY_DELAY_MS:
  1960. break;
  1961. case PHY_BJMPN:
  1962. if (regno > index) {
  1963. netif_err(tp, ifup, tp->dev,
  1964. "Out of range of firmware\n");
  1965. goto out;
  1966. }
  1967. break;
  1968. case PHY_READCOUNT_EQ_SKIP:
  1969. if (index + 2 >= pa->size) {
  1970. netif_err(tp, ifup, tp->dev,
  1971. "Out of range of firmware\n");
  1972. goto out;
  1973. }
  1974. break;
  1975. case PHY_COMP_EQ_SKIPN:
  1976. case PHY_COMP_NEQ_SKIPN:
  1977. case PHY_SKIPN:
  1978. if (index + 1 + regno >= pa->size) {
  1979. netif_err(tp, ifup, tp->dev,
  1980. "Out of range of firmware\n");
  1981. goto out;
  1982. }
  1983. break;
  1984. default:
  1985. netif_err(tp, ifup, tp->dev,
  1986. "Invalid action 0x%08x\n", action);
  1987. goto out;
  1988. }
  1989. }
  1990. rc = true;
  1991. out:
  1992. return rc;
  1993. }
  1994. static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1995. {
  1996. struct net_device *dev = tp->dev;
  1997. int rc = -EINVAL;
  1998. if (!rtl_fw_format_ok(tp, rtl_fw)) {
  1999. netif_err(tp, ifup, dev, "invalid firmware\n");
  2000. goto out;
  2001. }
  2002. if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
  2003. rc = 0;
  2004. out:
  2005. return rc;
  2006. }
  2007. static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2008. {
  2009. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  2010. struct mdio_ops org, *ops = &tp->mdio_ops;
  2011. u32 predata, count;
  2012. size_t index;
  2013. predata = count = 0;
  2014. org.write = ops->write;
  2015. org.read = ops->read;
  2016. for (index = 0; index < pa->size; ) {
  2017. u32 action = le32_to_cpu(pa->code[index]);
  2018. u32 data = action & 0x0000ffff;
  2019. u32 regno = (action & 0x0fff0000) >> 16;
  2020. if (!action)
  2021. break;
  2022. switch(action & 0xf0000000) {
  2023. case PHY_READ:
  2024. predata = rtl_readphy(tp, regno);
  2025. count++;
  2026. index++;
  2027. break;
  2028. case PHY_DATA_OR:
  2029. predata |= data;
  2030. index++;
  2031. break;
  2032. case PHY_DATA_AND:
  2033. predata &= data;
  2034. index++;
  2035. break;
  2036. case PHY_BJMPN:
  2037. index -= regno;
  2038. break;
  2039. case PHY_MDIO_CHG:
  2040. if (data == 0) {
  2041. ops->write = org.write;
  2042. ops->read = org.read;
  2043. } else if (data == 1) {
  2044. ops->write = mac_mcu_write;
  2045. ops->read = mac_mcu_read;
  2046. }
  2047. index++;
  2048. break;
  2049. case PHY_CLEAR_READCOUNT:
  2050. count = 0;
  2051. index++;
  2052. break;
  2053. case PHY_WRITE:
  2054. rtl_writephy(tp, regno, data);
  2055. index++;
  2056. break;
  2057. case PHY_READCOUNT_EQ_SKIP:
  2058. index += (count == data) ? 2 : 1;
  2059. break;
  2060. case PHY_COMP_EQ_SKIPN:
  2061. if (predata == data)
  2062. index += regno;
  2063. index++;
  2064. break;
  2065. case PHY_COMP_NEQ_SKIPN:
  2066. if (predata != data)
  2067. index += regno;
  2068. index++;
  2069. break;
  2070. case PHY_WRITE_PREVIOUS:
  2071. rtl_writephy(tp, regno, predata);
  2072. index++;
  2073. break;
  2074. case PHY_SKIPN:
  2075. index += regno + 1;
  2076. break;
  2077. case PHY_DELAY_MS:
  2078. mdelay(data);
  2079. index++;
  2080. break;
  2081. default:
  2082. BUG();
  2083. }
  2084. }
  2085. ops->write = org.write;
  2086. ops->read = org.read;
  2087. }
  2088. static void rtl_release_firmware(struct rtl8169_private *tp)
  2089. {
  2090. if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
  2091. release_firmware(tp->rtl_fw->fw);
  2092. kfree(tp->rtl_fw);
  2093. }
  2094. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  2095. }
  2096. static void rtl_apply_firmware(struct rtl8169_private *tp)
  2097. {
  2098. struct rtl_fw *rtl_fw = tp->rtl_fw;
  2099. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  2100. if (!IS_ERR_OR_NULL(rtl_fw))
  2101. rtl_phy_write_fw(tp, rtl_fw);
  2102. }
  2103. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  2104. {
  2105. if (rtl_readphy(tp, reg) != val)
  2106. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  2107. else
  2108. rtl_apply_firmware(tp);
  2109. }
  2110. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  2111. {
  2112. static const struct phy_reg phy_reg_init[] = {
  2113. { 0x1f, 0x0001 },
  2114. { 0x06, 0x006e },
  2115. { 0x08, 0x0708 },
  2116. { 0x15, 0x4000 },
  2117. { 0x18, 0x65c7 },
  2118. { 0x1f, 0x0001 },
  2119. { 0x03, 0x00a1 },
  2120. { 0x02, 0x0008 },
  2121. { 0x01, 0x0120 },
  2122. { 0x00, 0x1000 },
  2123. { 0x04, 0x0800 },
  2124. { 0x04, 0x0000 },
  2125. { 0x03, 0xff41 },
  2126. { 0x02, 0xdf60 },
  2127. { 0x01, 0x0140 },
  2128. { 0x00, 0x0077 },
  2129. { 0x04, 0x7800 },
  2130. { 0x04, 0x7000 },
  2131. { 0x03, 0x802f },
  2132. { 0x02, 0x4f02 },
  2133. { 0x01, 0x0409 },
  2134. { 0x00, 0xf0f9 },
  2135. { 0x04, 0x9800 },
  2136. { 0x04, 0x9000 },
  2137. { 0x03, 0xdf01 },
  2138. { 0x02, 0xdf20 },
  2139. { 0x01, 0xff95 },
  2140. { 0x00, 0xba00 },
  2141. { 0x04, 0xa800 },
  2142. { 0x04, 0xa000 },
  2143. { 0x03, 0xff41 },
  2144. { 0x02, 0xdf20 },
  2145. { 0x01, 0x0140 },
  2146. { 0x00, 0x00bb },
  2147. { 0x04, 0xb800 },
  2148. { 0x04, 0xb000 },
  2149. { 0x03, 0xdf41 },
  2150. { 0x02, 0xdc60 },
  2151. { 0x01, 0x6340 },
  2152. { 0x00, 0x007d },
  2153. { 0x04, 0xd800 },
  2154. { 0x04, 0xd000 },
  2155. { 0x03, 0xdf01 },
  2156. { 0x02, 0xdf20 },
  2157. { 0x01, 0x100a },
  2158. { 0x00, 0xa0ff },
  2159. { 0x04, 0xf800 },
  2160. { 0x04, 0xf000 },
  2161. { 0x1f, 0x0000 },
  2162. { 0x0b, 0x0000 },
  2163. { 0x00, 0x9200 }
  2164. };
  2165. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2166. }
  2167. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  2168. {
  2169. static const struct phy_reg phy_reg_init[] = {
  2170. { 0x1f, 0x0002 },
  2171. { 0x01, 0x90d0 },
  2172. { 0x1f, 0x0000 }
  2173. };
  2174. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2175. }
  2176. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  2177. {
  2178. struct pci_dev *pdev = tp->pci_dev;
  2179. if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
  2180. (pdev->subsystem_device != 0xe000))
  2181. return;
  2182. rtl_writephy(tp, 0x1f, 0x0001);
  2183. rtl_writephy(tp, 0x10, 0xf01b);
  2184. rtl_writephy(tp, 0x1f, 0x0000);
  2185. }
  2186. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  2187. {
  2188. static const struct phy_reg phy_reg_init[] = {
  2189. { 0x1f, 0x0001 },
  2190. { 0x04, 0x0000 },
  2191. { 0x03, 0x00a1 },
  2192. { 0x02, 0x0008 },
  2193. { 0x01, 0x0120 },
  2194. { 0x00, 0x1000 },
  2195. { 0x04, 0x0800 },
  2196. { 0x04, 0x9000 },
  2197. { 0x03, 0x802f },
  2198. { 0x02, 0x4f02 },
  2199. { 0x01, 0x0409 },
  2200. { 0x00, 0xf099 },
  2201. { 0x04, 0x9800 },
  2202. { 0x04, 0xa000 },
  2203. { 0x03, 0xdf01 },
  2204. { 0x02, 0xdf20 },
  2205. { 0x01, 0xff95 },
  2206. { 0x00, 0xba00 },
  2207. { 0x04, 0xa800 },
  2208. { 0x04, 0xf000 },
  2209. { 0x03, 0xdf01 },
  2210. { 0x02, 0xdf20 },
  2211. { 0x01, 0x101a },
  2212. { 0x00, 0xa0ff },
  2213. { 0x04, 0xf800 },
  2214. { 0x04, 0x0000 },
  2215. { 0x1f, 0x0000 },
  2216. { 0x1f, 0x0001 },
  2217. { 0x10, 0xf41b },
  2218. { 0x14, 0xfb54 },
  2219. { 0x18, 0xf5c7 },
  2220. { 0x1f, 0x0000 },
  2221. { 0x1f, 0x0001 },
  2222. { 0x17, 0x0cc0 },
  2223. { 0x1f, 0x0000 }
  2224. };
  2225. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2226. rtl8169scd_hw_phy_config_quirk(tp);
  2227. }
  2228. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  2229. {
  2230. static const struct phy_reg phy_reg_init[] = {
  2231. { 0x1f, 0x0001 },
  2232. { 0x04, 0x0000 },
  2233. { 0x03, 0x00a1 },
  2234. { 0x02, 0x0008 },
  2235. { 0x01, 0x0120 },
  2236. { 0x00, 0x1000 },
  2237. { 0x04, 0x0800 },
  2238. { 0x04, 0x9000 },
  2239. { 0x03, 0x802f },
  2240. { 0x02, 0x4f02 },
  2241. { 0x01, 0x0409 },
  2242. { 0x00, 0xf099 },
  2243. { 0x04, 0x9800 },
  2244. { 0x04, 0xa000 },
  2245. { 0x03, 0xdf01 },
  2246. { 0x02, 0xdf20 },
  2247. { 0x01, 0xff95 },
  2248. { 0x00, 0xba00 },
  2249. { 0x04, 0xa800 },
  2250. { 0x04, 0xf000 },
  2251. { 0x03, 0xdf01 },
  2252. { 0x02, 0xdf20 },
  2253. { 0x01, 0x101a },
  2254. { 0x00, 0xa0ff },
  2255. { 0x04, 0xf800 },
  2256. { 0x04, 0x0000 },
  2257. { 0x1f, 0x0000 },
  2258. { 0x1f, 0x0001 },
  2259. { 0x0b, 0x8480 },
  2260. { 0x1f, 0x0000 },
  2261. { 0x1f, 0x0001 },
  2262. { 0x18, 0x67c7 },
  2263. { 0x04, 0x2000 },
  2264. { 0x03, 0x002f },
  2265. { 0x02, 0x4360 },
  2266. { 0x01, 0x0109 },
  2267. { 0x00, 0x3022 },
  2268. { 0x04, 0x2800 },
  2269. { 0x1f, 0x0000 },
  2270. { 0x1f, 0x0001 },
  2271. { 0x17, 0x0cc0 },
  2272. { 0x1f, 0x0000 }
  2273. };
  2274. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2275. }
  2276. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  2277. {
  2278. static const struct phy_reg phy_reg_init[] = {
  2279. { 0x10, 0xf41b },
  2280. { 0x1f, 0x0000 }
  2281. };
  2282. rtl_writephy(tp, 0x1f, 0x0001);
  2283. rtl_patchphy(tp, 0x16, 1 << 0);
  2284. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2285. }
  2286. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  2287. {
  2288. static const struct phy_reg phy_reg_init[] = {
  2289. { 0x1f, 0x0001 },
  2290. { 0x10, 0xf41b },
  2291. { 0x1f, 0x0000 }
  2292. };
  2293. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2294. }
  2295. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  2296. {
  2297. static const struct phy_reg phy_reg_init[] = {
  2298. { 0x1f, 0x0000 },
  2299. { 0x1d, 0x0f00 },
  2300. { 0x1f, 0x0002 },
  2301. { 0x0c, 0x1ec8 },
  2302. { 0x1f, 0x0000 }
  2303. };
  2304. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2305. }
  2306. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  2307. {
  2308. static const struct phy_reg phy_reg_init[] = {
  2309. { 0x1f, 0x0001 },
  2310. { 0x1d, 0x3d98 },
  2311. { 0x1f, 0x0000 }
  2312. };
  2313. rtl_writephy(tp, 0x1f, 0x0000);
  2314. rtl_patchphy(tp, 0x14, 1 << 5);
  2315. rtl_patchphy(tp, 0x0d, 1 << 5);
  2316. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2317. }
  2318. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  2319. {
  2320. static const struct phy_reg phy_reg_init[] = {
  2321. { 0x1f, 0x0001 },
  2322. { 0x12, 0x2300 },
  2323. { 0x1f, 0x0002 },
  2324. { 0x00, 0x88d4 },
  2325. { 0x01, 0x82b1 },
  2326. { 0x03, 0x7002 },
  2327. { 0x08, 0x9e30 },
  2328. { 0x09, 0x01f0 },
  2329. { 0x0a, 0x5500 },
  2330. { 0x0c, 0x00c8 },
  2331. { 0x1f, 0x0003 },
  2332. { 0x12, 0xc096 },
  2333. { 0x16, 0x000a },
  2334. { 0x1f, 0x0000 },
  2335. { 0x1f, 0x0000 },
  2336. { 0x09, 0x2000 },
  2337. { 0x09, 0x0000 }
  2338. };
  2339. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2340. rtl_patchphy(tp, 0x14, 1 << 5);
  2341. rtl_patchphy(tp, 0x0d, 1 << 5);
  2342. rtl_writephy(tp, 0x1f, 0x0000);
  2343. }
  2344. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  2345. {
  2346. static const struct phy_reg phy_reg_init[] = {
  2347. { 0x1f, 0x0001 },
  2348. { 0x12, 0x2300 },
  2349. { 0x03, 0x802f },
  2350. { 0x02, 0x4f02 },
  2351. { 0x01, 0x0409 },
  2352. { 0x00, 0xf099 },
  2353. { 0x04, 0x9800 },
  2354. { 0x04, 0x9000 },
  2355. { 0x1d, 0x3d98 },
  2356. { 0x1f, 0x0002 },
  2357. { 0x0c, 0x7eb8 },
  2358. { 0x06, 0x0761 },
  2359. { 0x1f, 0x0003 },
  2360. { 0x16, 0x0f0a },
  2361. { 0x1f, 0x0000 }
  2362. };
  2363. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2364. rtl_patchphy(tp, 0x16, 1 << 0);
  2365. rtl_patchphy(tp, 0x14, 1 << 5);
  2366. rtl_patchphy(tp, 0x0d, 1 << 5);
  2367. rtl_writephy(tp, 0x1f, 0x0000);
  2368. }
  2369. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  2370. {
  2371. static const struct phy_reg phy_reg_init[] = {
  2372. { 0x1f, 0x0001 },
  2373. { 0x12, 0x2300 },
  2374. { 0x1d, 0x3d98 },
  2375. { 0x1f, 0x0002 },
  2376. { 0x0c, 0x7eb8 },
  2377. { 0x06, 0x5461 },
  2378. { 0x1f, 0x0003 },
  2379. { 0x16, 0x0f0a },
  2380. { 0x1f, 0x0000 }
  2381. };
  2382. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2383. rtl_patchphy(tp, 0x16, 1 << 0);
  2384. rtl_patchphy(tp, 0x14, 1 << 5);
  2385. rtl_patchphy(tp, 0x0d, 1 << 5);
  2386. rtl_writephy(tp, 0x1f, 0x0000);
  2387. }
  2388. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  2389. {
  2390. rtl8168c_3_hw_phy_config(tp);
  2391. }
  2392. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  2393. {
  2394. static const struct phy_reg phy_reg_init_0[] = {
  2395. /* Channel Estimation */
  2396. { 0x1f, 0x0001 },
  2397. { 0x06, 0x4064 },
  2398. { 0x07, 0x2863 },
  2399. { 0x08, 0x059c },
  2400. { 0x09, 0x26b4 },
  2401. { 0x0a, 0x6a19 },
  2402. { 0x0b, 0xdcc8 },
  2403. { 0x10, 0xf06d },
  2404. { 0x14, 0x7f68 },
  2405. { 0x18, 0x7fd9 },
  2406. { 0x1c, 0xf0ff },
  2407. { 0x1d, 0x3d9c },
  2408. { 0x1f, 0x0003 },
  2409. { 0x12, 0xf49f },
  2410. { 0x13, 0x070b },
  2411. { 0x1a, 0x05ad },
  2412. { 0x14, 0x94c0 },
  2413. /*
  2414. * Tx Error Issue
  2415. * Enhance line driver power
  2416. */
  2417. { 0x1f, 0x0002 },
  2418. { 0x06, 0x5561 },
  2419. { 0x1f, 0x0005 },
  2420. { 0x05, 0x8332 },
  2421. { 0x06, 0x5561 },
  2422. /*
  2423. * Can not link to 1Gbps with bad cable
  2424. * Decrease SNR threshold form 21.07dB to 19.04dB
  2425. */
  2426. { 0x1f, 0x0001 },
  2427. { 0x17, 0x0cc0 },
  2428. { 0x1f, 0x0000 },
  2429. { 0x0d, 0xf880 }
  2430. };
  2431. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2432. /*
  2433. * Rx Error Issue
  2434. * Fine Tune Switching regulator parameter
  2435. */
  2436. rtl_writephy(tp, 0x1f, 0x0002);
  2437. rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
  2438. rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
  2439. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2440. static const struct phy_reg phy_reg_init[] = {
  2441. { 0x1f, 0x0002 },
  2442. { 0x05, 0x669a },
  2443. { 0x1f, 0x0005 },
  2444. { 0x05, 0x8330 },
  2445. { 0x06, 0x669a },
  2446. { 0x1f, 0x0002 }
  2447. };
  2448. int val;
  2449. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2450. val = rtl_readphy(tp, 0x0d);
  2451. if ((val & 0x00ff) != 0x006c) {
  2452. static const u32 set[] = {
  2453. 0x0065, 0x0066, 0x0067, 0x0068,
  2454. 0x0069, 0x006a, 0x006b, 0x006c
  2455. };
  2456. int i;
  2457. rtl_writephy(tp, 0x1f, 0x0002);
  2458. val &= 0xff00;
  2459. for (i = 0; i < ARRAY_SIZE(set); i++)
  2460. rtl_writephy(tp, 0x0d, val | set[i]);
  2461. }
  2462. } else {
  2463. static const struct phy_reg phy_reg_init[] = {
  2464. { 0x1f, 0x0002 },
  2465. { 0x05, 0x6662 },
  2466. { 0x1f, 0x0005 },
  2467. { 0x05, 0x8330 },
  2468. { 0x06, 0x6662 }
  2469. };
  2470. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2471. }
  2472. /* RSET couple improve */
  2473. rtl_writephy(tp, 0x1f, 0x0002);
  2474. rtl_patchphy(tp, 0x0d, 0x0300);
  2475. rtl_patchphy(tp, 0x0f, 0x0010);
  2476. /* Fine tune PLL performance */
  2477. rtl_writephy(tp, 0x1f, 0x0002);
  2478. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2479. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2480. rtl_writephy(tp, 0x1f, 0x0005);
  2481. rtl_writephy(tp, 0x05, 0x001b);
  2482. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  2483. rtl_writephy(tp, 0x1f, 0x0000);
  2484. }
  2485. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  2486. {
  2487. static const struct phy_reg phy_reg_init_0[] = {
  2488. /* Channel Estimation */
  2489. { 0x1f, 0x0001 },
  2490. { 0x06, 0x4064 },
  2491. { 0x07, 0x2863 },
  2492. { 0x08, 0x059c },
  2493. { 0x09, 0x26b4 },
  2494. { 0x0a, 0x6a19 },
  2495. { 0x0b, 0xdcc8 },
  2496. { 0x10, 0xf06d },
  2497. { 0x14, 0x7f68 },
  2498. { 0x18, 0x7fd9 },
  2499. { 0x1c, 0xf0ff },
  2500. { 0x1d, 0x3d9c },
  2501. { 0x1f, 0x0003 },
  2502. { 0x12, 0xf49f },
  2503. { 0x13, 0x070b },
  2504. { 0x1a, 0x05ad },
  2505. { 0x14, 0x94c0 },
  2506. /*
  2507. * Tx Error Issue
  2508. * Enhance line driver power
  2509. */
  2510. { 0x1f, 0x0002 },
  2511. { 0x06, 0x5561 },
  2512. { 0x1f, 0x0005 },
  2513. { 0x05, 0x8332 },
  2514. { 0x06, 0x5561 },
  2515. /*
  2516. * Can not link to 1Gbps with bad cable
  2517. * Decrease SNR threshold form 21.07dB to 19.04dB
  2518. */
  2519. { 0x1f, 0x0001 },
  2520. { 0x17, 0x0cc0 },
  2521. { 0x1f, 0x0000 },
  2522. { 0x0d, 0xf880 }
  2523. };
  2524. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2525. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2526. static const struct phy_reg phy_reg_init[] = {
  2527. { 0x1f, 0x0002 },
  2528. { 0x05, 0x669a },
  2529. { 0x1f, 0x0005 },
  2530. { 0x05, 0x8330 },
  2531. { 0x06, 0x669a },
  2532. { 0x1f, 0x0002 }
  2533. };
  2534. int val;
  2535. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2536. val = rtl_readphy(tp, 0x0d);
  2537. if ((val & 0x00ff) != 0x006c) {
  2538. static const u32 set[] = {
  2539. 0x0065, 0x0066, 0x0067, 0x0068,
  2540. 0x0069, 0x006a, 0x006b, 0x006c
  2541. };
  2542. int i;
  2543. rtl_writephy(tp, 0x1f, 0x0002);
  2544. val &= 0xff00;
  2545. for (i = 0; i < ARRAY_SIZE(set); i++)
  2546. rtl_writephy(tp, 0x0d, val | set[i]);
  2547. }
  2548. } else {
  2549. static const struct phy_reg phy_reg_init[] = {
  2550. { 0x1f, 0x0002 },
  2551. { 0x05, 0x2642 },
  2552. { 0x1f, 0x0005 },
  2553. { 0x05, 0x8330 },
  2554. { 0x06, 0x2642 }
  2555. };
  2556. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2557. }
  2558. /* Fine tune PLL performance */
  2559. rtl_writephy(tp, 0x1f, 0x0002);
  2560. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2561. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2562. /* Switching regulator Slew rate */
  2563. rtl_writephy(tp, 0x1f, 0x0002);
  2564. rtl_patchphy(tp, 0x0f, 0x0017);
  2565. rtl_writephy(tp, 0x1f, 0x0005);
  2566. rtl_writephy(tp, 0x05, 0x001b);
  2567. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2568. rtl_writephy(tp, 0x1f, 0x0000);
  2569. }
  2570. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2571. {
  2572. static const struct phy_reg phy_reg_init[] = {
  2573. { 0x1f, 0x0002 },
  2574. { 0x10, 0x0008 },
  2575. { 0x0d, 0x006c },
  2576. { 0x1f, 0x0000 },
  2577. { 0x0d, 0xf880 },
  2578. { 0x1f, 0x0001 },
  2579. { 0x17, 0x0cc0 },
  2580. { 0x1f, 0x0001 },
  2581. { 0x0b, 0xa4d8 },
  2582. { 0x09, 0x281c },
  2583. { 0x07, 0x2883 },
  2584. { 0x0a, 0x6b35 },
  2585. { 0x1d, 0x3da4 },
  2586. { 0x1c, 0xeffd },
  2587. { 0x14, 0x7f52 },
  2588. { 0x18, 0x7fc6 },
  2589. { 0x08, 0x0601 },
  2590. { 0x06, 0x4063 },
  2591. { 0x10, 0xf074 },
  2592. { 0x1f, 0x0003 },
  2593. { 0x13, 0x0789 },
  2594. { 0x12, 0xf4bd },
  2595. { 0x1a, 0x04fd },
  2596. { 0x14, 0x84b0 },
  2597. { 0x1f, 0x0000 },
  2598. { 0x00, 0x9200 },
  2599. { 0x1f, 0x0005 },
  2600. { 0x01, 0x0340 },
  2601. { 0x1f, 0x0001 },
  2602. { 0x04, 0x4000 },
  2603. { 0x03, 0x1d21 },
  2604. { 0x02, 0x0c32 },
  2605. { 0x01, 0x0200 },
  2606. { 0x00, 0x5554 },
  2607. { 0x04, 0x4800 },
  2608. { 0x04, 0x4000 },
  2609. { 0x04, 0xf000 },
  2610. { 0x03, 0xdf01 },
  2611. { 0x02, 0xdf20 },
  2612. { 0x01, 0x101a },
  2613. { 0x00, 0xa0ff },
  2614. { 0x04, 0xf800 },
  2615. { 0x04, 0xf000 },
  2616. { 0x1f, 0x0000 },
  2617. { 0x1f, 0x0007 },
  2618. { 0x1e, 0x0023 },
  2619. { 0x16, 0x0000 },
  2620. { 0x1f, 0x0000 }
  2621. };
  2622. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2623. }
  2624. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2625. {
  2626. static const struct phy_reg phy_reg_init[] = {
  2627. { 0x1f, 0x0001 },
  2628. { 0x17, 0x0cc0 },
  2629. { 0x1f, 0x0007 },
  2630. { 0x1e, 0x002d },
  2631. { 0x18, 0x0040 },
  2632. { 0x1f, 0x0000 }
  2633. };
  2634. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2635. rtl_patchphy(tp, 0x0d, 1 << 5);
  2636. }
  2637. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2638. {
  2639. static const struct phy_reg phy_reg_init[] = {
  2640. /* Enable Delay cap */
  2641. { 0x1f, 0x0005 },
  2642. { 0x05, 0x8b80 },
  2643. { 0x06, 0xc896 },
  2644. { 0x1f, 0x0000 },
  2645. /* Channel estimation fine tune */
  2646. { 0x1f, 0x0001 },
  2647. { 0x0b, 0x6c20 },
  2648. { 0x07, 0x2872 },
  2649. { 0x1c, 0xefff },
  2650. { 0x1f, 0x0003 },
  2651. { 0x14, 0x6420 },
  2652. { 0x1f, 0x0000 },
  2653. /* Update PFM & 10M TX idle timer */
  2654. { 0x1f, 0x0007 },
  2655. { 0x1e, 0x002f },
  2656. { 0x15, 0x1919 },
  2657. { 0x1f, 0x0000 },
  2658. { 0x1f, 0x0007 },
  2659. { 0x1e, 0x00ac },
  2660. { 0x18, 0x0006 },
  2661. { 0x1f, 0x0000 }
  2662. };
  2663. rtl_apply_firmware(tp);
  2664. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2665. /* DCO enable for 10M IDLE Power */
  2666. rtl_writephy(tp, 0x1f, 0x0007);
  2667. rtl_writephy(tp, 0x1e, 0x0023);
  2668. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2669. rtl_writephy(tp, 0x1f, 0x0000);
  2670. /* For impedance matching */
  2671. rtl_writephy(tp, 0x1f, 0x0002);
  2672. rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
  2673. rtl_writephy(tp, 0x1f, 0x0000);
  2674. /* PHY auto speed down */
  2675. rtl_writephy(tp, 0x1f, 0x0007);
  2676. rtl_writephy(tp, 0x1e, 0x002d);
  2677. rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
  2678. rtl_writephy(tp, 0x1f, 0x0000);
  2679. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2680. rtl_writephy(tp, 0x1f, 0x0005);
  2681. rtl_writephy(tp, 0x05, 0x8b86);
  2682. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2683. rtl_writephy(tp, 0x1f, 0x0000);
  2684. rtl_writephy(tp, 0x1f, 0x0005);
  2685. rtl_writephy(tp, 0x05, 0x8b85);
  2686. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2687. rtl_writephy(tp, 0x1f, 0x0007);
  2688. rtl_writephy(tp, 0x1e, 0x0020);
  2689. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
  2690. rtl_writephy(tp, 0x1f, 0x0006);
  2691. rtl_writephy(tp, 0x00, 0x5a00);
  2692. rtl_writephy(tp, 0x1f, 0x0000);
  2693. rtl_writephy(tp, 0x0d, 0x0007);
  2694. rtl_writephy(tp, 0x0e, 0x003c);
  2695. rtl_writephy(tp, 0x0d, 0x4007);
  2696. rtl_writephy(tp, 0x0e, 0x0000);
  2697. rtl_writephy(tp, 0x0d, 0x0000);
  2698. }
  2699. static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
  2700. {
  2701. const u16 w[] = {
  2702. addr[0] | (addr[1] << 8),
  2703. addr[2] | (addr[3] << 8),
  2704. addr[4] | (addr[5] << 8)
  2705. };
  2706. const struct exgmac_reg e[] = {
  2707. { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
  2708. { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
  2709. { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
  2710. { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
  2711. };
  2712. rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
  2713. }
  2714. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2715. {
  2716. static const struct phy_reg phy_reg_init[] = {
  2717. /* Enable Delay cap */
  2718. { 0x1f, 0x0004 },
  2719. { 0x1f, 0x0007 },
  2720. { 0x1e, 0x00ac },
  2721. { 0x18, 0x0006 },
  2722. { 0x1f, 0x0002 },
  2723. { 0x1f, 0x0000 },
  2724. { 0x1f, 0x0000 },
  2725. /* Channel estimation fine tune */
  2726. { 0x1f, 0x0003 },
  2727. { 0x09, 0xa20f },
  2728. { 0x1f, 0x0000 },
  2729. { 0x1f, 0x0000 },
  2730. /* Green Setting */
  2731. { 0x1f, 0x0005 },
  2732. { 0x05, 0x8b5b },
  2733. { 0x06, 0x9222 },
  2734. { 0x05, 0x8b6d },
  2735. { 0x06, 0x8000 },
  2736. { 0x05, 0x8b76 },
  2737. { 0x06, 0x8000 },
  2738. { 0x1f, 0x0000 }
  2739. };
  2740. rtl_apply_firmware(tp);
  2741. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2742. /* For 4-corner performance improve */
  2743. rtl_writephy(tp, 0x1f, 0x0005);
  2744. rtl_writephy(tp, 0x05, 0x8b80);
  2745. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2746. rtl_writephy(tp, 0x1f, 0x0000);
  2747. /* PHY auto speed down */
  2748. rtl_writephy(tp, 0x1f, 0x0004);
  2749. rtl_writephy(tp, 0x1f, 0x0007);
  2750. rtl_writephy(tp, 0x1e, 0x002d);
  2751. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  2752. rtl_writephy(tp, 0x1f, 0x0002);
  2753. rtl_writephy(tp, 0x1f, 0x0000);
  2754. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2755. /* improve 10M EEE waveform */
  2756. rtl_writephy(tp, 0x1f, 0x0005);
  2757. rtl_writephy(tp, 0x05, 0x8b86);
  2758. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2759. rtl_writephy(tp, 0x1f, 0x0000);
  2760. /* Improve 2-pair detection performance */
  2761. rtl_writephy(tp, 0x1f, 0x0005);
  2762. rtl_writephy(tp, 0x05, 0x8b85);
  2763. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2764. rtl_writephy(tp, 0x1f, 0x0000);
  2765. /* EEE setting */
  2766. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
  2767. rtl_writephy(tp, 0x1f, 0x0005);
  2768. rtl_writephy(tp, 0x05, 0x8b85);
  2769. rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
  2770. rtl_writephy(tp, 0x1f, 0x0004);
  2771. rtl_writephy(tp, 0x1f, 0x0007);
  2772. rtl_writephy(tp, 0x1e, 0x0020);
  2773. rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
  2774. rtl_writephy(tp, 0x1f, 0x0002);
  2775. rtl_writephy(tp, 0x1f, 0x0000);
  2776. rtl_writephy(tp, 0x0d, 0x0007);
  2777. rtl_writephy(tp, 0x0e, 0x003c);
  2778. rtl_writephy(tp, 0x0d, 0x4007);
  2779. rtl_writephy(tp, 0x0e, 0x0006);
  2780. rtl_writephy(tp, 0x0d, 0x0000);
  2781. /* Green feature */
  2782. rtl_writephy(tp, 0x1f, 0x0003);
  2783. rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
  2784. rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
  2785. rtl_writephy(tp, 0x1f, 0x0000);
  2786. rtl_writephy(tp, 0x1f, 0x0005);
  2787. rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
  2788. rtl_writephy(tp, 0x1f, 0x0000);
  2789. /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
  2790. rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
  2791. }
  2792. static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
  2793. {
  2794. /* For 4-corner performance improve */
  2795. rtl_writephy(tp, 0x1f, 0x0005);
  2796. rtl_writephy(tp, 0x05, 0x8b80);
  2797. rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
  2798. rtl_writephy(tp, 0x1f, 0x0000);
  2799. /* PHY auto speed down */
  2800. rtl_writephy(tp, 0x1f, 0x0007);
  2801. rtl_writephy(tp, 0x1e, 0x002d);
  2802. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  2803. rtl_writephy(tp, 0x1f, 0x0000);
  2804. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2805. /* Improve 10M EEE waveform */
  2806. rtl_writephy(tp, 0x1f, 0x0005);
  2807. rtl_writephy(tp, 0x05, 0x8b86);
  2808. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2809. rtl_writephy(tp, 0x1f, 0x0000);
  2810. }
  2811. static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
  2812. {
  2813. static const struct phy_reg phy_reg_init[] = {
  2814. /* Channel estimation fine tune */
  2815. { 0x1f, 0x0003 },
  2816. { 0x09, 0xa20f },
  2817. { 0x1f, 0x0000 },
  2818. /* Modify green table for giga & fnet */
  2819. { 0x1f, 0x0005 },
  2820. { 0x05, 0x8b55 },
  2821. { 0x06, 0x0000 },
  2822. { 0x05, 0x8b5e },
  2823. { 0x06, 0x0000 },
  2824. { 0x05, 0x8b67 },
  2825. { 0x06, 0x0000 },
  2826. { 0x05, 0x8b70 },
  2827. { 0x06, 0x0000 },
  2828. { 0x1f, 0x0000 },
  2829. { 0x1f, 0x0007 },
  2830. { 0x1e, 0x0078 },
  2831. { 0x17, 0x0000 },
  2832. { 0x19, 0x00fb },
  2833. { 0x1f, 0x0000 },
  2834. /* Modify green table for 10M */
  2835. { 0x1f, 0x0005 },
  2836. { 0x05, 0x8b79 },
  2837. { 0x06, 0xaa00 },
  2838. { 0x1f, 0x0000 },
  2839. /* Disable hiimpedance detection (RTCT) */
  2840. { 0x1f, 0x0003 },
  2841. { 0x01, 0x328a },
  2842. { 0x1f, 0x0000 }
  2843. };
  2844. rtl_apply_firmware(tp);
  2845. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2846. rtl8168f_hw_phy_config(tp);
  2847. /* Improve 2-pair detection performance */
  2848. rtl_writephy(tp, 0x1f, 0x0005);
  2849. rtl_writephy(tp, 0x05, 0x8b85);
  2850. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2851. rtl_writephy(tp, 0x1f, 0x0000);
  2852. }
  2853. static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
  2854. {
  2855. rtl_apply_firmware(tp);
  2856. rtl8168f_hw_phy_config(tp);
  2857. }
  2858. static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
  2859. {
  2860. static const struct phy_reg phy_reg_init[] = {
  2861. /* Channel estimation fine tune */
  2862. { 0x1f, 0x0003 },
  2863. { 0x09, 0xa20f },
  2864. { 0x1f, 0x0000 },
  2865. /* Modify green table for giga & fnet */
  2866. { 0x1f, 0x0005 },
  2867. { 0x05, 0x8b55 },
  2868. { 0x06, 0x0000 },
  2869. { 0x05, 0x8b5e },
  2870. { 0x06, 0x0000 },
  2871. { 0x05, 0x8b67 },
  2872. { 0x06, 0x0000 },
  2873. { 0x05, 0x8b70 },
  2874. { 0x06, 0x0000 },
  2875. { 0x1f, 0x0000 },
  2876. { 0x1f, 0x0007 },
  2877. { 0x1e, 0x0078 },
  2878. { 0x17, 0x0000 },
  2879. { 0x19, 0x00aa },
  2880. { 0x1f, 0x0000 },
  2881. /* Modify green table for 10M */
  2882. { 0x1f, 0x0005 },
  2883. { 0x05, 0x8b79 },
  2884. { 0x06, 0xaa00 },
  2885. { 0x1f, 0x0000 },
  2886. /* Disable hiimpedance detection (RTCT) */
  2887. { 0x1f, 0x0003 },
  2888. { 0x01, 0x328a },
  2889. { 0x1f, 0x0000 }
  2890. };
  2891. rtl_apply_firmware(tp);
  2892. rtl8168f_hw_phy_config(tp);
  2893. /* Improve 2-pair detection performance */
  2894. rtl_writephy(tp, 0x1f, 0x0005);
  2895. rtl_writephy(tp, 0x05, 0x8b85);
  2896. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2897. rtl_writephy(tp, 0x1f, 0x0000);
  2898. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2899. /* Modify green table for giga */
  2900. rtl_writephy(tp, 0x1f, 0x0005);
  2901. rtl_writephy(tp, 0x05, 0x8b54);
  2902. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  2903. rtl_writephy(tp, 0x05, 0x8b5d);
  2904. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  2905. rtl_writephy(tp, 0x05, 0x8a7c);
  2906. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2907. rtl_writephy(tp, 0x05, 0x8a7f);
  2908. rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
  2909. rtl_writephy(tp, 0x05, 0x8a82);
  2910. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2911. rtl_writephy(tp, 0x05, 0x8a85);
  2912. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2913. rtl_writephy(tp, 0x05, 0x8a88);
  2914. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  2915. rtl_writephy(tp, 0x1f, 0x0000);
  2916. /* uc same-seed solution */
  2917. rtl_writephy(tp, 0x1f, 0x0005);
  2918. rtl_writephy(tp, 0x05, 0x8b85);
  2919. rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
  2920. rtl_writephy(tp, 0x1f, 0x0000);
  2921. /* eee setting */
  2922. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
  2923. rtl_writephy(tp, 0x1f, 0x0005);
  2924. rtl_writephy(tp, 0x05, 0x8b85);
  2925. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2926. rtl_writephy(tp, 0x1f, 0x0004);
  2927. rtl_writephy(tp, 0x1f, 0x0007);
  2928. rtl_writephy(tp, 0x1e, 0x0020);
  2929. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
  2930. rtl_writephy(tp, 0x1f, 0x0000);
  2931. rtl_writephy(tp, 0x0d, 0x0007);
  2932. rtl_writephy(tp, 0x0e, 0x003c);
  2933. rtl_writephy(tp, 0x0d, 0x4007);
  2934. rtl_writephy(tp, 0x0e, 0x0000);
  2935. rtl_writephy(tp, 0x0d, 0x0000);
  2936. /* Green feature */
  2937. rtl_writephy(tp, 0x1f, 0x0003);
  2938. rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
  2939. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
  2940. rtl_writephy(tp, 0x1f, 0x0000);
  2941. }
  2942. static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
  2943. {
  2944. rtl_apply_firmware(tp);
  2945. rtl_writephy(tp, 0x1f, 0x0a46);
  2946. if (rtl_readphy(tp, 0x10) & 0x0100) {
  2947. rtl_writephy(tp, 0x1f, 0x0bcc);
  2948. rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
  2949. } else {
  2950. rtl_writephy(tp, 0x1f, 0x0bcc);
  2951. rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
  2952. }
  2953. rtl_writephy(tp, 0x1f, 0x0a46);
  2954. if (rtl_readphy(tp, 0x13) & 0x0100) {
  2955. rtl_writephy(tp, 0x1f, 0x0c41);
  2956. rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
  2957. } else {
  2958. rtl_writephy(tp, 0x1f, 0x0c41);
  2959. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
  2960. }
  2961. /* Enable PHY auto speed down */
  2962. rtl_writephy(tp, 0x1f, 0x0a44);
  2963. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  2964. rtl_writephy(tp, 0x1f, 0x0bcc);
  2965. rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
  2966. rtl_writephy(tp, 0x1f, 0x0a44);
  2967. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  2968. rtl_writephy(tp, 0x1f, 0x0a43);
  2969. rtl_writephy(tp, 0x13, 0x8084);
  2970. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  2971. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  2972. /* EEE auto-fallback function */
  2973. rtl_writephy(tp, 0x1f, 0x0a4b);
  2974. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  2975. /* Enable UC LPF tune function */
  2976. rtl_writephy(tp, 0x1f, 0x0a43);
  2977. rtl_writephy(tp, 0x13, 0x8012);
  2978. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2979. rtl_writephy(tp, 0x1f, 0x0c42);
  2980. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  2981. /* Improve SWR Efficiency */
  2982. rtl_writephy(tp, 0x1f, 0x0bcd);
  2983. rtl_writephy(tp, 0x14, 0x5065);
  2984. rtl_writephy(tp, 0x14, 0xd065);
  2985. rtl_writephy(tp, 0x1f, 0x0bc8);
  2986. rtl_writephy(tp, 0x11, 0x5655);
  2987. rtl_writephy(tp, 0x1f, 0x0bcd);
  2988. rtl_writephy(tp, 0x14, 0x1065);
  2989. rtl_writephy(tp, 0x14, 0x9065);
  2990. rtl_writephy(tp, 0x14, 0x1065);
  2991. /* Check ALDPS bit, disable it if enabled */
  2992. rtl_writephy(tp, 0x1f, 0x0a43);
  2993. if (rtl_readphy(tp, 0x10) & 0x0004)
  2994. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  2995. rtl_writephy(tp, 0x1f, 0x0000);
  2996. }
  2997. static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
  2998. {
  2999. rtl_apply_firmware(tp);
  3000. }
  3001. static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
  3002. {
  3003. u16 dout_tapbin;
  3004. u32 data;
  3005. rtl_apply_firmware(tp);
  3006. /* CHN EST parameters adjust - giga master */
  3007. rtl_writephy(tp, 0x1f, 0x0a43);
  3008. rtl_writephy(tp, 0x13, 0x809b);
  3009. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
  3010. rtl_writephy(tp, 0x13, 0x80a2);
  3011. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
  3012. rtl_writephy(tp, 0x13, 0x80a4);
  3013. rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
  3014. rtl_writephy(tp, 0x13, 0x809c);
  3015. rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
  3016. rtl_writephy(tp, 0x1f, 0x0000);
  3017. /* CHN EST parameters adjust - giga slave */
  3018. rtl_writephy(tp, 0x1f, 0x0a43);
  3019. rtl_writephy(tp, 0x13, 0x80ad);
  3020. rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
  3021. rtl_writephy(tp, 0x13, 0x80b4);
  3022. rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
  3023. rtl_writephy(tp, 0x13, 0x80ac);
  3024. rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
  3025. rtl_writephy(tp, 0x1f, 0x0000);
  3026. /* CHN EST parameters adjust - fnet */
  3027. rtl_writephy(tp, 0x1f, 0x0a43);
  3028. rtl_writephy(tp, 0x13, 0x808e);
  3029. rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
  3030. rtl_writephy(tp, 0x13, 0x8090);
  3031. rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
  3032. rtl_writephy(tp, 0x13, 0x8092);
  3033. rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
  3034. rtl_writephy(tp, 0x1f, 0x0000);
  3035. /* enable R-tune & PGA-retune function */
  3036. dout_tapbin = 0;
  3037. rtl_writephy(tp, 0x1f, 0x0a46);
  3038. data = rtl_readphy(tp, 0x13);
  3039. data &= 3;
  3040. data <<= 2;
  3041. dout_tapbin |= data;
  3042. data = rtl_readphy(tp, 0x12);
  3043. data &= 0xc000;
  3044. data >>= 14;
  3045. dout_tapbin |= data;
  3046. dout_tapbin = ~(dout_tapbin^0x08);
  3047. dout_tapbin <<= 12;
  3048. dout_tapbin &= 0xf000;
  3049. rtl_writephy(tp, 0x1f, 0x0a43);
  3050. rtl_writephy(tp, 0x13, 0x827a);
  3051. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3052. rtl_writephy(tp, 0x13, 0x827b);
  3053. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3054. rtl_writephy(tp, 0x13, 0x827c);
  3055. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3056. rtl_writephy(tp, 0x13, 0x827d);
  3057. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3058. rtl_writephy(tp, 0x1f, 0x0a43);
  3059. rtl_writephy(tp, 0x13, 0x0811);
  3060. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3061. rtl_writephy(tp, 0x1f, 0x0a42);
  3062. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3063. rtl_writephy(tp, 0x1f, 0x0000);
  3064. /* enable GPHY 10M */
  3065. rtl_writephy(tp, 0x1f, 0x0a44);
  3066. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3067. rtl_writephy(tp, 0x1f, 0x0000);
  3068. /* SAR ADC performance */
  3069. rtl_writephy(tp, 0x1f, 0x0bca);
  3070. rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
  3071. rtl_writephy(tp, 0x1f, 0x0000);
  3072. rtl_writephy(tp, 0x1f, 0x0a43);
  3073. rtl_writephy(tp, 0x13, 0x803f);
  3074. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3075. rtl_writephy(tp, 0x13, 0x8047);
  3076. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3077. rtl_writephy(tp, 0x13, 0x804f);
  3078. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3079. rtl_writephy(tp, 0x13, 0x8057);
  3080. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3081. rtl_writephy(tp, 0x13, 0x805f);
  3082. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3083. rtl_writephy(tp, 0x13, 0x8067);
  3084. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3085. rtl_writephy(tp, 0x13, 0x806f);
  3086. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3087. rtl_writephy(tp, 0x1f, 0x0000);
  3088. /* disable phy pfm mode */
  3089. rtl_writephy(tp, 0x1f, 0x0a44);
  3090. rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
  3091. rtl_writephy(tp, 0x1f, 0x0000);
  3092. /* Check ALDPS bit, disable it if enabled */
  3093. rtl_writephy(tp, 0x1f, 0x0a43);
  3094. if (rtl_readphy(tp, 0x10) & 0x0004)
  3095. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3096. rtl_writephy(tp, 0x1f, 0x0000);
  3097. }
  3098. static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
  3099. {
  3100. u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
  3101. u16 rlen;
  3102. u32 data;
  3103. rtl_apply_firmware(tp);
  3104. /* CHIN EST parameter update */
  3105. rtl_writephy(tp, 0x1f, 0x0a43);
  3106. rtl_writephy(tp, 0x13, 0x808a);
  3107. rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
  3108. rtl_writephy(tp, 0x1f, 0x0000);
  3109. /* enable R-tune & PGA-retune function */
  3110. rtl_writephy(tp, 0x1f, 0x0a43);
  3111. rtl_writephy(tp, 0x13, 0x0811);
  3112. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3113. rtl_writephy(tp, 0x1f, 0x0a42);
  3114. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3115. rtl_writephy(tp, 0x1f, 0x0000);
  3116. /* enable GPHY 10M */
  3117. rtl_writephy(tp, 0x1f, 0x0a44);
  3118. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3119. rtl_writephy(tp, 0x1f, 0x0000);
  3120. r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
  3121. data = r8168_mac_ocp_read(tp, 0xdd02);
  3122. ioffset_p3 = ((data & 0x80)>>7);
  3123. ioffset_p3 <<= 3;
  3124. data = r8168_mac_ocp_read(tp, 0xdd00);
  3125. ioffset_p3 |= ((data & (0xe000))>>13);
  3126. ioffset_p2 = ((data & (0x1e00))>>9);
  3127. ioffset_p1 = ((data & (0x01e0))>>5);
  3128. ioffset_p0 = ((data & 0x0010)>>4);
  3129. ioffset_p0 <<= 3;
  3130. ioffset_p0 |= (data & (0x07));
  3131. data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
  3132. if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
  3133. (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
  3134. rtl_writephy(tp, 0x1f, 0x0bcf);
  3135. rtl_writephy(tp, 0x16, data);
  3136. rtl_writephy(tp, 0x1f, 0x0000);
  3137. }
  3138. /* Modify rlen (TX LPF corner frequency) level */
  3139. rtl_writephy(tp, 0x1f, 0x0bcd);
  3140. data = rtl_readphy(tp, 0x16);
  3141. data &= 0x000f;
  3142. rlen = 0;
  3143. if (data > 3)
  3144. rlen = data - 3;
  3145. data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
  3146. rtl_writephy(tp, 0x17, data);
  3147. rtl_writephy(tp, 0x1f, 0x0bcd);
  3148. rtl_writephy(tp, 0x1f, 0x0000);
  3149. /* disable phy pfm mode */
  3150. rtl_writephy(tp, 0x1f, 0x0a44);
  3151. rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
  3152. rtl_writephy(tp, 0x1f, 0x0000);
  3153. /* Check ALDPS bit, disable it if enabled */
  3154. rtl_writephy(tp, 0x1f, 0x0a43);
  3155. if (rtl_readphy(tp, 0x10) & 0x0004)
  3156. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3157. rtl_writephy(tp, 0x1f, 0x0000);
  3158. }
  3159. static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
  3160. {
  3161. /* Enable PHY auto speed down */
  3162. rtl_writephy(tp, 0x1f, 0x0a44);
  3163. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  3164. rtl_writephy(tp, 0x1f, 0x0000);
  3165. /* patch 10M & ALDPS */
  3166. rtl_writephy(tp, 0x1f, 0x0bcc);
  3167. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3168. rtl_writephy(tp, 0x1f, 0x0a44);
  3169. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3170. rtl_writephy(tp, 0x1f, 0x0a43);
  3171. rtl_writephy(tp, 0x13, 0x8084);
  3172. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3173. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3174. rtl_writephy(tp, 0x1f, 0x0000);
  3175. /* Enable EEE auto-fallback function */
  3176. rtl_writephy(tp, 0x1f, 0x0a4b);
  3177. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  3178. rtl_writephy(tp, 0x1f, 0x0000);
  3179. /* Enable UC LPF tune function */
  3180. rtl_writephy(tp, 0x1f, 0x0a43);
  3181. rtl_writephy(tp, 0x13, 0x8012);
  3182. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3183. rtl_writephy(tp, 0x1f, 0x0000);
  3184. /* set rg_sel_sdm_rate */
  3185. rtl_writephy(tp, 0x1f, 0x0c42);
  3186. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3187. rtl_writephy(tp, 0x1f, 0x0000);
  3188. /* Check ALDPS bit, disable it if enabled */
  3189. rtl_writephy(tp, 0x1f, 0x0a43);
  3190. if (rtl_readphy(tp, 0x10) & 0x0004)
  3191. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3192. rtl_writephy(tp, 0x1f, 0x0000);
  3193. }
  3194. static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
  3195. {
  3196. /* patch 10M & ALDPS */
  3197. rtl_writephy(tp, 0x1f, 0x0bcc);
  3198. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3199. rtl_writephy(tp, 0x1f, 0x0a44);
  3200. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3201. rtl_writephy(tp, 0x1f, 0x0a43);
  3202. rtl_writephy(tp, 0x13, 0x8084);
  3203. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3204. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3205. rtl_writephy(tp, 0x1f, 0x0000);
  3206. /* Enable UC LPF tune function */
  3207. rtl_writephy(tp, 0x1f, 0x0a43);
  3208. rtl_writephy(tp, 0x13, 0x8012);
  3209. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3210. rtl_writephy(tp, 0x1f, 0x0000);
  3211. /* Set rg_sel_sdm_rate */
  3212. rtl_writephy(tp, 0x1f, 0x0c42);
  3213. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3214. rtl_writephy(tp, 0x1f, 0x0000);
  3215. /* Channel estimation parameters */
  3216. rtl_writephy(tp, 0x1f, 0x0a43);
  3217. rtl_writephy(tp, 0x13, 0x80f3);
  3218. rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
  3219. rtl_writephy(tp, 0x13, 0x80f0);
  3220. rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
  3221. rtl_writephy(tp, 0x13, 0x80ef);
  3222. rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
  3223. rtl_writephy(tp, 0x13, 0x80f6);
  3224. rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
  3225. rtl_writephy(tp, 0x13, 0x80ec);
  3226. rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
  3227. rtl_writephy(tp, 0x13, 0x80ed);
  3228. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3229. rtl_writephy(tp, 0x13, 0x80f2);
  3230. rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
  3231. rtl_writephy(tp, 0x13, 0x80f4);
  3232. rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
  3233. rtl_writephy(tp, 0x1f, 0x0a43);
  3234. rtl_writephy(tp, 0x13, 0x8110);
  3235. rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
  3236. rtl_writephy(tp, 0x13, 0x810f);
  3237. rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
  3238. rtl_writephy(tp, 0x13, 0x8111);
  3239. rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
  3240. rtl_writephy(tp, 0x13, 0x8113);
  3241. rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
  3242. rtl_writephy(tp, 0x13, 0x8115);
  3243. rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
  3244. rtl_writephy(tp, 0x13, 0x810e);
  3245. rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
  3246. rtl_writephy(tp, 0x13, 0x810c);
  3247. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3248. rtl_writephy(tp, 0x13, 0x810b);
  3249. rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
  3250. rtl_writephy(tp, 0x1f, 0x0a43);
  3251. rtl_writephy(tp, 0x13, 0x80d1);
  3252. rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
  3253. rtl_writephy(tp, 0x13, 0x80cd);
  3254. rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
  3255. rtl_writephy(tp, 0x13, 0x80d3);
  3256. rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
  3257. rtl_writephy(tp, 0x13, 0x80d5);
  3258. rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
  3259. rtl_writephy(tp, 0x13, 0x80d7);
  3260. rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
  3261. /* Force PWM-mode */
  3262. rtl_writephy(tp, 0x1f, 0x0bcd);
  3263. rtl_writephy(tp, 0x14, 0x5065);
  3264. rtl_writephy(tp, 0x14, 0xd065);
  3265. rtl_writephy(tp, 0x1f, 0x0bc8);
  3266. rtl_writephy(tp, 0x12, 0x00ed);
  3267. rtl_writephy(tp, 0x1f, 0x0bcd);
  3268. rtl_writephy(tp, 0x14, 0x1065);
  3269. rtl_writephy(tp, 0x14, 0x9065);
  3270. rtl_writephy(tp, 0x14, 0x1065);
  3271. rtl_writephy(tp, 0x1f, 0x0000);
  3272. /* Check ALDPS bit, disable it if enabled */
  3273. rtl_writephy(tp, 0x1f, 0x0a43);
  3274. if (rtl_readphy(tp, 0x10) & 0x0004)
  3275. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3276. rtl_writephy(tp, 0x1f, 0x0000);
  3277. }
  3278. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  3279. {
  3280. static const struct phy_reg phy_reg_init[] = {
  3281. { 0x1f, 0x0003 },
  3282. { 0x08, 0x441d },
  3283. { 0x01, 0x9100 },
  3284. { 0x1f, 0x0000 }
  3285. };
  3286. rtl_writephy(tp, 0x1f, 0x0000);
  3287. rtl_patchphy(tp, 0x11, 1 << 12);
  3288. rtl_patchphy(tp, 0x19, 1 << 13);
  3289. rtl_patchphy(tp, 0x10, 1 << 15);
  3290. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3291. }
  3292. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  3293. {
  3294. static const struct phy_reg phy_reg_init[] = {
  3295. { 0x1f, 0x0005 },
  3296. { 0x1a, 0x0000 },
  3297. { 0x1f, 0x0000 },
  3298. { 0x1f, 0x0004 },
  3299. { 0x1c, 0x0000 },
  3300. { 0x1f, 0x0000 },
  3301. { 0x1f, 0x0001 },
  3302. { 0x15, 0x7701 },
  3303. { 0x1f, 0x0000 }
  3304. };
  3305. /* Disable ALDPS before ram code */
  3306. rtl_writephy(tp, 0x1f, 0x0000);
  3307. rtl_writephy(tp, 0x18, 0x0310);
  3308. msleep(100);
  3309. rtl_apply_firmware(tp);
  3310. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3311. }
  3312. static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
  3313. {
  3314. /* Disable ALDPS before setting firmware */
  3315. rtl_writephy(tp, 0x1f, 0x0000);
  3316. rtl_writephy(tp, 0x18, 0x0310);
  3317. msleep(20);
  3318. rtl_apply_firmware(tp);
  3319. /* EEE setting */
  3320. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3321. rtl_writephy(tp, 0x1f, 0x0004);
  3322. rtl_writephy(tp, 0x10, 0x401f);
  3323. rtl_writephy(tp, 0x19, 0x7030);
  3324. rtl_writephy(tp, 0x1f, 0x0000);
  3325. }
  3326. static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
  3327. {
  3328. static const struct phy_reg phy_reg_init[] = {
  3329. { 0x1f, 0x0004 },
  3330. { 0x10, 0xc07f },
  3331. { 0x19, 0x7030 },
  3332. { 0x1f, 0x0000 }
  3333. };
  3334. /* Disable ALDPS before ram code */
  3335. rtl_writephy(tp, 0x1f, 0x0000);
  3336. rtl_writephy(tp, 0x18, 0x0310);
  3337. msleep(100);
  3338. rtl_apply_firmware(tp);
  3339. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3340. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3341. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3342. }
  3343. static void rtl_hw_phy_config(struct net_device *dev)
  3344. {
  3345. struct rtl8169_private *tp = netdev_priv(dev);
  3346. rtl8169_print_mac_version(tp);
  3347. switch (tp->mac_version) {
  3348. case RTL_GIGA_MAC_VER_01:
  3349. break;
  3350. case RTL_GIGA_MAC_VER_02:
  3351. case RTL_GIGA_MAC_VER_03:
  3352. rtl8169s_hw_phy_config(tp);
  3353. break;
  3354. case RTL_GIGA_MAC_VER_04:
  3355. rtl8169sb_hw_phy_config(tp);
  3356. break;
  3357. case RTL_GIGA_MAC_VER_05:
  3358. rtl8169scd_hw_phy_config(tp);
  3359. break;
  3360. case RTL_GIGA_MAC_VER_06:
  3361. rtl8169sce_hw_phy_config(tp);
  3362. break;
  3363. case RTL_GIGA_MAC_VER_07:
  3364. case RTL_GIGA_MAC_VER_08:
  3365. case RTL_GIGA_MAC_VER_09:
  3366. rtl8102e_hw_phy_config(tp);
  3367. break;
  3368. case RTL_GIGA_MAC_VER_11:
  3369. rtl8168bb_hw_phy_config(tp);
  3370. break;
  3371. case RTL_GIGA_MAC_VER_12:
  3372. rtl8168bef_hw_phy_config(tp);
  3373. break;
  3374. case RTL_GIGA_MAC_VER_17:
  3375. rtl8168bef_hw_phy_config(tp);
  3376. break;
  3377. case RTL_GIGA_MAC_VER_18:
  3378. rtl8168cp_1_hw_phy_config(tp);
  3379. break;
  3380. case RTL_GIGA_MAC_VER_19:
  3381. rtl8168c_1_hw_phy_config(tp);
  3382. break;
  3383. case RTL_GIGA_MAC_VER_20:
  3384. rtl8168c_2_hw_phy_config(tp);
  3385. break;
  3386. case RTL_GIGA_MAC_VER_21:
  3387. rtl8168c_3_hw_phy_config(tp);
  3388. break;
  3389. case RTL_GIGA_MAC_VER_22:
  3390. rtl8168c_4_hw_phy_config(tp);
  3391. break;
  3392. case RTL_GIGA_MAC_VER_23:
  3393. case RTL_GIGA_MAC_VER_24:
  3394. rtl8168cp_2_hw_phy_config(tp);
  3395. break;
  3396. case RTL_GIGA_MAC_VER_25:
  3397. rtl8168d_1_hw_phy_config(tp);
  3398. break;
  3399. case RTL_GIGA_MAC_VER_26:
  3400. rtl8168d_2_hw_phy_config(tp);
  3401. break;
  3402. case RTL_GIGA_MAC_VER_27:
  3403. rtl8168d_3_hw_phy_config(tp);
  3404. break;
  3405. case RTL_GIGA_MAC_VER_28:
  3406. rtl8168d_4_hw_phy_config(tp);
  3407. break;
  3408. case RTL_GIGA_MAC_VER_29:
  3409. case RTL_GIGA_MAC_VER_30:
  3410. rtl8105e_hw_phy_config(tp);
  3411. break;
  3412. case RTL_GIGA_MAC_VER_31:
  3413. /* None. */
  3414. break;
  3415. case RTL_GIGA_MAC_VER_32:
  3416. case RTL_GIGA_MAC_VER_33:
  3417. rtl8168e_1_hw_phy_config(tp);
  3418. break;
  3419. case RTL_GIGA_MAC_VER_34:
  3420. rtl8168e_2_hw_phy_config(tp);
  3421. break;
  3422. case RTL_GIGA_MAC_VER_35:
  3423. rtl8168f_1_hw_phy_config(tp);
  3424. break;
  3425. case RTL_GIGA_MAC_VER_36:
  3426. rtl8168f_2_hw_phy_config(tp);
  3427. break;
  3428. case RTL_GIGA_MAC_VER_37:
  3429. rtl8402_hw_phy_config(tp);
  3430. break;
  3431. case RTL_GIGA_MAC_VER_38:
  3432. rtl8411_hw_phy_config(tp);
  3433. break;
  3434. case RTL_GIGA_MAC_VER_39:
  3435. rtl8106e_hw_phy_config(tp);
  3436. break;
  3437. case RTL_GIGA_MAC_VER_40:
  3438. rtl8168g_1_hw_phy_config(tp);
  3439. break;
  3440. case RTL_GIGA_MAC_VER_42:
  3441. case RTL_GIGA_MAC_VER_43:
  3442. case RTL_GIGA_MAC_VER_44:
  3443. rtl8168g_2_hw_phy_config(tp);
  3444. break;
  3445. case RTL_GIGA_MAC_VER_45:
  3446. case RTL_GIGA_MAC_VER_47:
  3447. rtl8168h_1_hw_phy_config(tp);
  3448. break;
  3449. case RTL_GIGA_MAC_VER_46:
  3450. case RTL_GIGA_MAC_VER_48:
  3451. rtl8168h_2_hw_phy_config(tp);
  3452. break;
  3453. case RTL_GIGA_MAC_VER_49:
  3454. rtl8168ep_1_hw_phy_config(tp);
  3455. break;
  3456. case RTL_GIGA_MAC_VER_50:
  3457. case RTL_GIGA_MAC_VER_51:
  3458. rtl8168ep_2_hw_phy_config(tp);
  3459. break;
  3460. case RTL_GIGA_MAC_VER_41:
  3461. default:
  3462. break;
  3463. }
  3464. }
  3465. static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
  3466. {
  3467. if (!test_and_set_bit(flag, tp->wk.flags))
  3468. schedule_work(&tp->wk.work);
  3469. }
  3470. static bool rtl_tbi_enabled(struct rtl8169_private *tp)
  3471. {
  3472. return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
  3473. (RTL_R8(tp, PHYstatus) & TBI_Enable);
  3474. }
  3475. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  3476. {
  3477. rtl_hw_phy_config(dev);
  3478. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  3479. netif_dbg(tp, drv, dev,
  3480. "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3481. RTL_W8(tp, 0x82, 0x01);
  3482. }
  3483. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  3484. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  3485. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  3486. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  3487. netif_dbg(tp, drv, dev,
  3488. "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3489. RTL_W8(tp, 0x82, 0x01);
  3490. netif_dbg(tp, drv, dev,
  3491. "Set PHY Reg 0x0bh = 0x00h\n");
  3492. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  3493. }
  3494. /* We may have called phy_speed_down before */
  3495. phy_speed_up(dev->phydev);
  3496. genphy_soft_reset(dev->phydev);
  3497. /* It was reported that several chips end up with 10MBit/Half on a
  3498. * 1GBit link after resuming from S3. For whatever reason the PHY on
  3499. * these chips doesn't properly start a renegotiation when soft-reset.
  3500. * Explicitly requesting a renegotiation fixes this.
  3501. */
  3502. if (dev->phydev->autoneg == AUTONEG_ENABLE)
  3503. phy_restart_aneg(dev->phydev);
  3504. }
  3505. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  3506. {
  3507. rtl_lock_work(tp);
  3508. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3509. RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
  3510. RTL_R32(tp, MAC4);
  3511. RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
  3512. RTL_R32(tp, MAC0);
  3513. if (tp->mac_version == RTL_GIGA_MAC_VER_34)
  3514. rtl_rar_exgmac_set(tp, addr);
  3515. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3516. rtl_unlock_work(tp);
  3517. }
  3518. static int rtl_set_mac_address(struct net_device *dev, void *p)
  3519. {
  3520. struct rtl8169_private *tp = netdev_priv(dev);
  3521. struct device *d = tp_to_dev(tp);
  3522. int ret;
  3523. ret = eth_mac_addr(dev, p);
  3524. if (ret)
  3525. return ret;
  3526. pm_runtime_get_noresume(d);
  3527. if (pm_runtime_active(d))
  3528. rtl_rar_set(tp, dev->dev_addr);
  3529. pm_runtime_put_noidle(d);
  3530. return 0;
  3531. }
  3532. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3533. {
  3534. if (!netif_running(dev))
  3535. return -ENODEV;
  3536. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  3537. }
  3538. static void rtl_init_mdio_ops(struct rtl8169_private *tp)
  3539. {
  3540. struct mdio_ops *ops = &tp->mdio_ops;
  3541. switch (tp->mac_version) {
  3542. case RTL_GIGA_MAC_VER_27:
  3543. ops->write = r8168dp_1_mdio_write;
  3544. ops->read = r8168dp_1_mdio_read;
  3545. break;
  3546. case RTL_GIGA_MAC_VER_28:
  3547. case RTL_GIGA_MAC_VER_31:
  3548. ops->write = r8168dp_2_mdio_write;
  3549. ops->read = r8168dp_2_mdio_read;
  3550. break;
  3551. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3552. ops->write = r8168g_mdio_write;
  3553. ops->read = r8168g_mdio_read;
  3554. break;
  3555. default:
  3556. ops->write = r8169_mdio_write;
  3557. ops->read = r8169_mdio_read;
  3558. break;
  3559. }
  3560. }
  3561. static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
  3562. {
  3563. switch (tp->mac_version) {
  3564. case RTL_GIGA_MAC_VER_25:
  3565. case RTL_GIGA_MAC_VER_26:
  3566. case RTL_GIGA_MAC_VER_29:
  3567. case RTL_GIGA_MAC_VER_30:
  3568. case RTL_GIGA_MAC_VER_32:
  3569. case RTL_GIGA_MAC_VER_33:
  3570. case RTL_GIGA_MAC_VER_34:
  3571. case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
  3572. RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
  3573. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  3574. break;
  3575. default:
  3576. break;
  3577. }
  3578. }
  3579. static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
  3580. {
  3581. struct phy_device *phydev;
  3582. if (!__rtl8169_get_wol(tp))
  3583. return false;
  3584. /* phydev may not be attached to netdevice */
  3585. phydev = mdiobus_get_phy(tp->mii_bus, 0);
  3586. phy_speed_down(phydev, false);
  3587. rtl_wol_suspend_quirk(tp);
  3588. return true;
  3589. }
  3590. static void r8168_pll_power_down(struct rtl8169_private *tp)
  3591. {
  3592. if (r8168_check_dash(tp))
  3593. return;
  3594. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  3595. tp->mac_version == RTL_GIGA_MAC_VER_33)
  3596. rtl_ephy_write(tp, 0x19, 0xff64);
  3597. if (rtl_wol_pll_power_down(tp))
  3598. return;
  3599. switch (tp->mac_version) {
  3600. case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
  3601. case RTL_GIGA_MAC_VER_37:
  3602. case RTL_GIGA_MAC_VER_39:
  3603. case RTL_GIGA_MAC_VER_43:
  3604. case RTL_GIGA_MAC_VER_44:
  3605. case RTL_GIGA_MAC_VER_45:
  3606. case RTL_GIGA_MAC_VER_46:
  3607. case RTL_GIGA_MAC_VER_47:
  3608. case RTL_GIGA_MAC_VER_48:
  3609. case RTL_GIGA_MAC_VER_50:
  3610. case RTL_GIGA_MAC_VER_51:
  3611. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
  3612. break;
  3613. case RTL_GIGA_MAC_VER_40:
  3614. case RTL_GIGA_MAC_VER_41:
  3615. case RTL_GIGA_MAC_VER_49:
  3616. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
  3617. 0xfc000000, ERIAR_EXGMAC);
  3618. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
  3619. break;
  3620. }
  3621. }
  3622. static void r8168_pll_power_up(struct rtl8169_private *tp)
  3623. {
  3624. switch (tp->mac_version) {
  3625. case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
  3626. case RTL_GIGA_MAC_VER_37:
  3627. case RTL_GIGA_MAC_VER_39:
  3628. case RTL_GIGA_MAC_VER_43:
  3629. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
  3630. break;
  3631. case RTL_GIGA_MAC_VER_44:
  3632. case RTL_GIGA_MAC_VER_45:
  3633. case RTL_GIGA_MAC_VER_46:
  3634. case RTL_GIGA_MAC_VER_47:
  3635. case RTL_GIGA_MAC_VER_48:
  3636. case RTL_GIGA_MAC_VER_50:
  3637. case RTL_GIGA_MAC_VER_51:
  3638. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
  3639. break;
  3640. case RTL_GIGA_MAC_VER_40:
  3641. case RTL_GIGA_MAC_VER_41:
  3642. case RTL_GIGA_MAC_VER_49:
  3643. RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
  3644. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
  3645. 0x00000000, ERIAR_EXGMAC);
  3646. break;
  3647. }
  3648. phy_resume(tp->dev->phydev);
  3649. /* give MAC/PHY some time to resume */
  3650. msleep(20);
  3651. }
  3652. static void rtl_pll_power_down(struct rtl8169_private *tp)
  3653. {
  3654. switch (tp->mac_version) {
  3655. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  3656. case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
  3657. break;
  3658. default:
  3659. r8168_pll_power_down(tp);
  3660. }
  3661. }
  3662. static void rtl_pll_power_up(struct rtl8169_private *tp)
  3663. {
  3664. switch (tp->mac_version) {
  3665. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  3666. case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
  3667. break;
  3668. default:
  3669. r8168_pll_power_up(tp);
  3670. }
  3671. }
  3672. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  3673. {
  3674. switch (tp->mac_version) {
  3675. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  3676. case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
  3677. RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  3678. break;
  3679. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
  3680. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
  3681. case RTL_GIGA_MAC_VER_38:
  3682. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  3683. break;
  3684. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3685. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
  3686. break;
  3687. default:
  3688. RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
  3689. break;
  3690. }
  3691. }
  3692. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  3693. {
  3694. tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
  3695. }
  3696. static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
  3697. {
  3698. if (tp->jumbo_ops.enable) {
  3699. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3700. tp->jumbo_ops.enable(tp);
  3701. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3702. }
  3703. }
  3704. static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
  3705. {
  3706. if (tp->jumbo_ops.disable) {
  3707. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3708. tp->jumbo_ops.disable(tp);
  3709. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3710. }
  3711. }
  3712. static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
  3713. {
  3714. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  3715. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
  3716. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
  3717. }
  3718. static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
  3719. {
  3720. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  3721. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
  3722. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  3723. }
  3724. static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
  3725. {
  3726. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  3727. }
  3728. static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
  3729. {
  3730. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  3731. }
  3732. static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
  3733. {
  3734. RTL_W8(tp, MaxTxPacketSize, 0x3f);
  3735. RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
  3736. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
  3737. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
  3738. }
  3739. static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
  3740. {
  3741. RTL_W8(tp, MaxTxPacketSize, 0x0c);
  3742. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
  3743. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
  3744. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  3745. }
  3746. static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
  3747. {
  3748. rtl_tx_performance_tweak(tp,
  3749. PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3750. }
  3751. static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
  3752. {
  3753. rtl_tx_performance_tweak(tp,
  3754. PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3755. }
  3756. static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
  3757. {
  3758. r8168b_0_hw_jumbo_enable(tp);
  3759. RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
  3760. }
  3761. static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
  3762. {
  3763. r8168b_0_hw_jumbo_disable(tp);
  3764. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
  3765. }
  3766. static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
  3767. {
  3768. struct jumbo_ops *ops = &tp->jumbo_ops;
  3769. switch (tp->mac_version) {
  3770. case RTL_GIGA_MAC_VER_11:
  3771. ops->disable = r8168b_0_hw_jumbo_disable;
  3772. ops->enable = r8168b_0_hw_jumbo_enable;
  3773. break;
  3774. case RTL_GIGA_MAC_VER_12:
  3775. case RTL_GIGA_MAC_VER_17:
  3776. ops->disable = r8168b_1_hw_jumbo_disable;
  3777. ops->enable = r8168b_1_hw_jumbo_enable;
  3778. break;
  3779. case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
  3780. case RTL_GIGA_MAC_VER_19:
  3781. case RTL_GIGA_MAC_VER_20:
  3782. case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
  3783. case RTL_GIGA_MAC_VER_22:
  3784. case RTL_GIGA_MAC_VER_23:
  3785. case RTL_GIGA_MAC_VER_24:
  3786. case RTL_GIGA_MAC_VER_25:
  3787. case RTL_GIGA_MAC_VER_26:
  3788. ops->disable = r8168c_hw_jumbo_disable;
  3789. ops->enable = r8168c_hw_jumbo_enable;
  3790. break;
  3791. case RTL_GIGA_MAC_VER_27:
  3792. case RTL_GIGA_MAC_VER_28:
  3793. ops->disable = r8168dp_hw_jumbo_disable;
  3794. ops->enable = r8168dp_hw_jumbo_enable;
  3795. break;
  3796. case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
  3797. case RTL_GIGA_MAC_VER_32:
  3798. case RTL_GIGA_MAC_VER_33:
  3799. case RTL_GIGA_MAC_VER_34:
  3800. ops->disable = r8168e_hw_jumbo_disable;
  3801. ops->enable = r8168e_hw_jumbo_enable;
  3802. break;
  3803. /*
  3804. * No action needed for jumbo frames with 8169.
  3805. * No jumbo for 810x at all.
  3806. */
  3807. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3808. default:
  3809. ops->disable = NULL;
  3810. ops->enable = NULL;
  3811. break;
  3812. }
  3813. }
  3814. DECLARE_RTL_COND(rtl_chipcmd_cond)
  3815. {
  3816. return RTL_R8(tp, ChipCmd) & CmdReset;
  3817. }
  3818. static void rtl_hw_reset(struct rtl8169_private *tp)
  3819. {
  3820. RTL_W8(tp, ChipCmd, CmdReset);
  3821. rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
  3822. }
  3823. static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
  3824. {
  3825. struct rtl_fw *rtl_fw;
  3826. const char *name;
  3827. int rc = -ENOMEM;
  3828. name = rtl_lookup_firmware_name(tp);
  3829. if (!name)
  3830. goto out_no_firmware;
  3831. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  3832. if (!rtl_fw)
  3833. goto err_warn;
  3834. rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
  3835. if (rc < 0)
  3836. goto err_free;
  3837. rc = rtl_check_firmware(tp, rtl_fw);
  3838. if (rc < 0)
  3839. goto err_release_firmware;
  3840. tp->rtl_fw = rtl_fw;
  3841. out:
  3842. return;
  3843. err_release_firmware:
  3844. release_firmware(rtl_fw->fw);
  3845. err_free:
  3846. kfree(rtl_fw);
  3847. err_warn:
  3848. netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
  3849. name, rc);
  3850. out_no_firmware:
  3851. tp->rtl_fw = NULL;
  3852. goto out;
  3853. }
  3854. static void rtl_request_firmware(struct rtl8169_private *tp)
  3855. {
  3856. if (IS_ERR(tp->rtl_fw))
  3857. rtl_request_uncached_firmware(tp);
  3858. }
  3859. static void rtl_rx_close(struct rtl8169_private *tp)
  3860. {
  3861. RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  3862. }
  3863. DECLARE_RTL_COND(rtl_npq_cond)
  3864. {
  3865. return RTL_R8(tp, TxPoll) & NPQ;
  3866. }
  3867. DECLARE_RTL_COND(rtl_txcfg_empty_cond)
  3868. {
  3869. return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
  3870. }
  3871. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  3872. {
  3873. /* Disable interrupts */
  3874. rtl8169_irq_mask_and_ack(tp);
  3875. rtl_rx_close(tp);
  3876. switch (tp->mac_version) {
  3877. case RTL_GIGA_MAC_VER_27:
  3878. case RTL_GIGA_MAC_VER_28:
  3879. case RTL_GIGA_MAC_VER_31:
  3880. rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
  3881. break;
  3882. case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
  3883. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  3884. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  3885. rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
  3886. break;
  3887. default:
  3888. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
  3889. udelay(100);
  3890. break;
  3891. }
  3892. rtl_hw_reset(tp);
  3893. }
  3894. static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
  3895. {
  3896. u32 val = TX_DMA_BURST << TxDMAShift |
  3897. InterFrameGap << TxInterFrameGapShift;
  3898. if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
  3899. tp->mac_version != RTL_GIGA_MAC_VER_39)
  3900. val |= TXCFG_AUTO_FIFO;
  3901. RTL_W32(tp, TxConfig, val);
  3902. }
  3903. static void rtl_set_rx_max_size(struct rtl8169_private *tp)
  3904. {
  3905. /* Low hurts. Let's disable the filtering. */
  3906. RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
  3907. }
  3908. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
  3909. {
  3910. /*
  3911. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  3912. * register to be written before TxDescAddrLow to work.
  3913. * Switching from MMIO to I/O access fixes the issue as well.
  3914. */
  3915. RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  3916. RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  3917. RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  3918. RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  3919. }
  3920. static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
  3921. {
  3922. static const struct rtl_cfg2_info {
  3923. u32 mac_version;
  3924. u32 clk;
  3925. u32 val;
  3926. } cfg2_info [] = {
  3927. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  3928. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  3929. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  3930. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  3931. };
  3932. const struct rtl_cfg2_info *p = cfg2_info;
  3933. unsigned int i;
  3934. u32 clk;
  3935. clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
  3936. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  3937. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  3938. RTL_W32(tp, 0x7c, p->val);
  3939. break;
  3940. }
  3941. }
  3942. }
  3943. static void rtl_set_rx_mode(struct net_device *dev)
  3944. {
  3945. struct rtl8169_private *tp = netdev_priv(dev);
  3946. u32 mc_filter[2]; /* Multicast hash filter */
  3947. int rx_mode;
  3948. u32 tmp = 0;
  3949. if (dev->flags & IFF_PROMISC) {
  3950. /* Unconditionally log net taps. */
  3951. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  3952. rx_mode =
  3953. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  3954. AcceptAllPhys;
  3955. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3956. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  3957. (dev->flags & IFF_ALLMULTI)) {
  3958. /* Too many to filter perfectly -- accept all multicasts. */
  3959. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  3960. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3961. } else {
  3962. struct netdev_hw_addr *ha;
  3963. rx_mode = AcceptBroadcast | AcceptMyPhys;
  3964. mc_filter[1] = mc_filter[0] = 0;
  3965. netdev_for_each_mc_addr(ha, dev) {
  3966. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  3967. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  3968. rx_mode |= AcceptMulticast;
  3969. }
  3970. }
  3971. if (dev->features & NETIF_F_RXALL)
  3972. rx_mode |= (AcceptErr | AcceptRunt);
  3973. tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
  3974. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  3975. u32 data = mc_filter[0];
  3976. mc_filter[0] = swab32(mc_filter[1]);
  3977. mc_filter[1] = swab32(data);
  3978. }
  3979. if (tp->mac_version == RTL_GIGA_MAC_VER_35)
  3980. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3981. RTL_W32(tp, MAR0 + 4, mc_filter[1]);
  3982. RTL_W32(tp, MAR0 + 0, mc_filter[0]);
  3983. RTL_W32(tp, RxConfig, tmp);
  3984. }
  3985. static void rtl_hw_start(struct rtl8169_private *tp)
  3986. {
  3987. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  3988. tp->hw_start(tp);
  3989. rtl_set_rx_max_size(tp);
  3990. rtl_set_rx_tx_desc_registers(tp);
  3991. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  3992. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  3993. RTL_R8(tp, IntrMask);
  3994. RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
  3995. rtl_init_rxcfg(tp);
  3996. rtl_set_tx_config_registers(tp);
  3997. rtl_set_rx_mode(tp->dev);
  3998. /* no early-rx interrupts */
  3999. RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
  4000. rtl_irq_enable_all(tp);
  4001. }
  4002. static void rtl_hw_start_8169(struct rtl8169_private *tp)
  4003. {
  4004. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  4005. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  4006. RTL_W8(tp, EarlyTxThres, NoEarlyTx);
  4007. tp->cp_cmd |= PCIMulRW;
  4008. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4009. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  4010. netif_dbg(tp, drv, tp->dev,
  4011. "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
  4012. tp->cp_cmd |= (1 << 14);
  4013. }
  4014. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4015. rtl8169_set_magic_reg(tp, tp->mac_version);
  4016. /*
  4017. * Undocumented corner. Supposedly:
  4018. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  4019. */
  4020. RTL_W16(tp, IntrMitigate, 0x0000);
  4021. RTL_W32(tp, RxMissed, 0);
  4022. }
  4023. DECLARE_RTL_COND(rtl_csiar_cond)
  4024. {
  4025. return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
  4026. }
  4027. static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
  4028. {
  4029. u32 func = PCI_FUNC(tp->pci_dev->devfn);
  4030. RTL_W32(tp, CSIDR, value);
  4031. RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4032. CSIAR_BYTE_ENABLE | func << 16);
  4033. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4034. }
  4035. static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
  4036. {
  4037. u32 func = PCI_FUNC(tp->pci_dev->devfn);
  4038. RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
  4039. CSIAR_BYTE_ENABLE);
  4040. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4041. RTL_R32(tp, CSIDR) : ~0;
  4042. }
  4043. static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
  4044. {
  4045. struct pci_dev *pdev = tp->pci_dev;
  4046. u32 csi;
  4047. /* According to Realtek the value at config space address 0x070f
  4048. * controls the L0s/L1 entrance latency. We try standard ECAM access
  4049. * first and if it fails fall back to CSI.
  4050. */
  4051. if (pdev->cfg_size > 0x070f &&
  4052. pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
  4053. return;
  4054. netdev_notice_once(tp->dev,
  4055. "No native access to PCI extended config space, falling back to CSI\n");
  4056. csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
  4057. rtl_csi_write(tp, 0x070c, csi | val << 24);
  4058. }
  4059. static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
  4060. {
  4061. rtl_csi_access_enable(tp, 0x27);
  4062. }
  4063. struct ephy_info {
  4064. unsigned int offset;
  4065. u16 mask;
  4066. u16 bits;
  4067. };
  4068. static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
  4069. int len)
  4070. {
  4071. u16 w;
  4072. while (len-- > 0) {
  4073. w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
  4074. rtl_ephy_write(tp, e->offset, w);
  4075. e++;
  4076. }
  4077. }
  4078. static void rtl_disable_clock_request(struct rtl8169_private *tp)
  4079. {
  4080. pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
  4081. PCI_EXP_LNKCTL_CLKREQ_EN);
  4082. }
  4083. static void rtl_enable_clock_request(struct rtl8169_private *tp)
  4084. {
  4085. pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
  4086. PCI_EXP_LNKCTL_CLKREQ_EN);
  4087. }
  4088. static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
  4089. {
  4090. u8 data;
  4091. data = RTL_R8(tp, Config3);
  4092. if (enable)
  4093. data |= Rdy_to_L23;
  4094. else
  4095. data &= ~Rdy_to_L23;
  4096. RTL_W8(tp, Config3, data);
  4097. }
  4098. static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
  4099. {
  4100. if (enable) {
  4101. RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
  4102. RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
  4103. } else {
  4104. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
  4105. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
  4106. }
  4107. udelay(10);
  4108. }
  4109. static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
  4110. {
  4111. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4112. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4113. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4114. if (tp->dev->mtu <= ETH_DATA_LEN) {
  4115. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
  4116. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4117. }
  4118. }
  4119. static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
  4120. {
  4121. rtl_hw_start_8168bb(tp);
  4122. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4123. RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
  4124. }
  4125. static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
  4126. {
  4127. RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
  4128. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4129. if (tp->dev->mtu <= ETH_DATA_LEN)
  4130. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4131. rtl_disable_clock_request(tp);
  4132. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4133. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4134. }
  4135. static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
  4136. {
  4137. static const struct ephy_info e_info_8168cp[] = {
  4138. { 0x01, 0, 0x0001 },
  4139. { 0x02, 0x0800, 0x1000 },
  4140. { 0x03, 0, 0x0042 },
  4141. { 0x06, 0x0080, 0x0000 },
  4142. { 0x07, 0, 0x2000 }
  4143. };
  4144. rtl_set_def_aspm_entry_latency(tp);
  4145. rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  4146. __rtl_hw_start_8168cp(tp);
  4147. }
  4148. static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
  4149. {
  4150. rtl_set_def_aspm_entry_latency(tp);
  4151. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4152. if (tp->dev->mtu <= ETH_DATA_LEN)
  4153. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4154. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4155. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4156. }
  4157. static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
  4158. {
  4159. rtl_set_def_aspm_entry_latency(tp);
  4160. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4161. /* Magic. */
  4162. RTL_W8(tp, DBG_REG, 0x20);
  4163. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4164. if (tp->dev->mtu <= ETH_DATA_LEN)
  4165. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4166. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4167. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4168. }
  4169. static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
  4170. {
  4171. static const struct ephy_info e_info_8168c_1[] = {
  4172. { 0x02, 0x0800, 0x1000 },
  4173. { 0x03, 0, 0x0002 },
  4174. { 0x06, 0x0080, 0x0000 }
  4175. };
  4176. rtl_set_def_aspm_entry_latency(tp);
  4177. RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  4178. rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  4179. __rtl_hw_start_8168cp(tp);
  4180. }
  4181. static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
  4182. {
  4183. static const struct ephy_info e_info_8168c_2[] = {
  4184. { 0x01, 0, 0x0001 },
  4185. { 0x03, 0x0400, 0x0220 }
  4186. };
  4187. rtl_set_def_aspm_entry_latency(tp);
  4188. rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  4189. __rtl_hw_start_8168cp(tp);
  4190. }
  4191. static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
  4192. {
  4193. rtl_hw_start_8168c_2(tp);
  4194. }
  4195. static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
  4196. {
  4197. rtl_set_def_aspm_entry_latency(tp);
  4198. __rtl_hw_start_8168cp(tp);
  4199. }
  4200. static void rtl_hw_start_8168d(struct rtl8169_private *tp)
  4201. {
  4202. rtl_set_def_aspm_entry_latency(tp);
  4203. rtl_disable_clock_request(tp);
  4204. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4205. if (tp->dev->mtu <= ETH_DATA_LEN)
  4206. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4207. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4208. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4209. }
  4210. static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
  4211. {
  4212. rtl_set_def_aspm_entry_latency(tp);
  4213. if (tp->dev->mtu <= ETH_DATA_LEN)
  4214. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4215. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4216. rtl_disable_clock_request(tp);
  4217. }
  4218. static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
  4219. {
  4220. static const struct ephy_info e_info_8168d_4[] = {
  4221. { 0x0b, 0x0000, 0x0048 },
  4222. { 0x19, 0x0020, 0x0050 },
  4223. { 0x0c, 0x0100, 0x0020 }
  4224. };
  4225. rtl_set_def_aspm_entry_latency(tp);
  4226. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4227. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4228. rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
  4229. rtl_enable_clock_request(tp);
  4230. }
  4231. static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
  4232. {
  4233. static const struct ephy_info e_info_8168e_1[] = {
  4234. { 0x00, 0x0200, 0x0100 },
  4235. { 0x00, 0x0000, 0x0004 },
  4236. { 0x06, 0x0002, 0x0001 },
  4237. { 0x06, 0x0000, 0x0030 },
  4238. { 0x07, 0x0000, 0x2000 },
  4239. { 0x00, 0x0000, 0x0020 },
  4240. { 0x03, 0x5800, 0x2000 },
  4241. { 0x03, 0x0000, 0x0001 },
  4242. { 0x01, 0x0800, 0x1000 },
  4243. { 0x07, 0x0000, 0x4000 },
  4244. { 0x1e, 0x0000, 0x2000 },
  4245. { 0x19, 0xffff, 0xfe6c },
  4246. { 0x0a, 0x0000, 0x0040 }
  4247. };
  4248. rtl_set_def_aspm_entry_latency(tp);
  4249. rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
  4250. if (tp->dev->mtu <= ETH_DATA_LEN)
  4251. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4252. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4253. rtl_disable_clock_request(tp);
  4254. /* Reset tx FIFO pointer */
  4255. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
  4256. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
  4257. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  4258. }
  4259. static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
  4260. {
  4261. static const struct ephy_info e_info_8168e_2[] = {
  4262. { 0x09, 0x0000, 0x0080 },
  4263. { 0x19, 0x0000, 0x0224 }
  4264. };
  4265. rtl_set_def_aspm_entry_latency(tp);
  4266. rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
  4267. if (tp->dev->mtu <= ETH_DATA_LEN)
  4268. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4269. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4270. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4271. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4272. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4273. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4274. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
  4275. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4276. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  4277. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4278. rtl_disable_clock_request(tp);
  4279. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4280. /* Adjust EEE LED frequency */
  4281. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4282. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  4283. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
  4284. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  4285. rtl_hw_aspm_clkreq_enable(tp, true);
  4286. }
  4287. static void rtl_hw_start_8168f(struct rtl8169_private *tp)
  4288. {
  4289. rtl_set_def_aspm_entry_latency(tp);
  4290. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4291. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4292. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4293. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4294. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4295. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4296. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4297. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4298. rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4299. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4300. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
  4301. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4302. rtl_disable_clock_request(tp);
  4303. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4304. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  4305. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
  4306. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
  4307. }
  4308. static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
  4309. {
  4310. static const struct ephy_info e_info_8168f_1[] = {
  4311. { 0x06, 0x00c0, 0x0020 },
  4312. { 0x08, 0x0001, 0x0002 },
  4313. { 0x09, 0x0000, 0x0080 },
  4314. { 0x19, 0x0000, 0x0224 }
  4315. };
  4316. rtl_hw_start_8168f(tp);
  4317. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  4318. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  4319. /* Adjust EEE LED frequency */
  4320. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4321. }
  4322. static void rtl_hw_start_8411(struct rtl8169_private *tp)
  4323. {
  4324. static const struct ephy_info e_info_8168f_1[] = {
  4325. { 0x06, 0x00c0, 0x0020 },
  4326. { 0x0f, 0xffff, 0x5200 },
  4327. { 0x1e, 0x0000, 0x4000 },
  4328. { 0x19, 0x0000, 0x0224 }
  4329. };
  4330. rtl_hw_start_8168f(tp);
  4331. rtl_pcie_state_l2l3_enable(tp, false);
  4332. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  4333. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
  4334. }
  4335. static void rtl_hw_start_8168g(struct rtl8169_private *tp)
  4336. {
  4337. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
  4338. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  4339. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  4340. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4341. rtl_set_def_aspm_entry_latency(tp);
  4342. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4343. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4344. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4345. rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
  4346. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  4347. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4348. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4349. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4350. /* Adjust EEE LED frequency */
  4351. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4352. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  4353. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  4354. rtl_pcie_state_l2l3_enable(tp, false);
  4355. }
  4356. static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
  4357. {
  4358. static const struct ephy_info e_info_8168g_1[] = {
  4359. { 0x00, 0x0000, 0x0008 },
  4360. { 0x0c, 0x37d0, 0x0820 },
  4361. { 0x1e, 0x0000, 0x0001 },
  4362. { 0x19, 0x8000, 0x0000 }
  4363. };
  4364. rtl_hw_start_8168g(tp);
  4365. /* disable aspm and clock request before access ephy */
  4366. rtl_hw_aspm_clkreq_enable(tp, false);
  4367. rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
  4368. rtl_hw_aspm_clkreq_enable(tp, true);
  4369. }
  4370. static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
  4371. {
  4372. static const struct ephy_info e_info_8168g_2[] = {
  4373. { 0x00, 0x0000, 0x0008 },
  4374. { 0x0c, 0x3df0, 0x0200 },
  4375. { 0x19, 0xffff, 0xfc00 },
  4376. { 0x1e, 0xffff, 0x20eb }
  4377. };
  4378. rtl_hw_start_8168g(tp);
  4379. /* disable aspm and clock request before access ephy */
  4380. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
  4381. RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
  4382. rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
  4383. }
  4384. static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
  4385. {
  4386. static const struct ephy_info e_info_8411_2[] = {
  4387. { 0x00, 0x0000, 0x0008 },
  4388. { 0x0c, 0x3df0, 0x0200 },
  4389. { 0x0f, 0xffff, 0x5200 },
  4390. { 0x19, 0x0020, 0x0000 },
  4391. { 0x1e, 0x0000, 0x2000 }
  4392. };
  4393. rtl_hw_start_8168g(tp);
  4394. /* disable aspm and clock request before access ephy */
  4395. rtl_hw_aspm_clkreq_enable(tp, false);
  4396. rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
  4397. /* The following Realtek-provided magic fixes an issue with the RX unit
  4398. * getting confused after the PHY having been powered-down.
  4399. */
  4400. r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
  4401. r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
  4402. r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
  4403. r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
  4404. r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
  4405. r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
  4406. r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
  4407. r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
  4408. mdelay(3);
  4409. r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
  4410. r8168_mac_ocp_write(tp, 0xF800, 0xE008);
  4411. r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
  4412. r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
  4413. r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
  4414. r8168_mac_ocp_write(tp, 0xF808, 0xE027);
  4415. r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
  4416. r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
  4417. r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
  4418. r8168_mac_ocp_write(tp, 0xF810, 0xC602);
  4419. r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
  4420. r8168_mac_ocp_write(tp, 0xF814, 0x0000);
  4421. r8168_mac_ocp_write(tp, 0xF816, 0xC502);
  4422. r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
  4423. r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
  4424. r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
  4425. r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
  4426. r8168_mac_ocp_write(tp, 0xF820, 0x080A);
  4427. r8168_mac_ocp_write(tp, 0xF822, 0x6420);
  4428. r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
  4429. r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
  4430. r8168_mac_ocp_write(tp, 0xF828, 0xC516);
  4431. r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
  4432. r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
  4433. r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
  4434. r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
  4435. r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
  4436. r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
  4437. r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
  4438. r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
  4439. r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
  4440. r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
  4441. r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
  4442. r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
  4443. r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
  4444. r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
  4445. r8168_mac_ocp_write(tp, 0xF846, 0xC404);
  4446. r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
  4447. r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
  4448. r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
  4449. r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
  4450. r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
  4451. r8168_mac_ocp_write(tp, 0xF852, 0xE434);
  4452. r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
  4453. r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
  4454. r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
  4455. r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
  4456. r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
  4457. r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
  4458. r8168_mac_ocp_write(tp, 0xF860, 0xF007);
  4459. r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
  4460. r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
  4461. r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
  4462. r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
  4463. r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
  4464. r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
  4465. r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
  4466. r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
  4467. r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
  4468. r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
  4469. r8168_mac_ocp_write(tp, 0xF876, 0xC516);
  4470. r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
  4471. r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
  4472. r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
  4473. r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
  4474. r8168_mac_ocp_write(tp, 0xF880, 0xC512);
  4475. r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
  4476. r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
  4477. r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
  4478. r8168_mac_ocp_write(tp, 0xF888, 0x483F);
  4479. r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
  4480. r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
  4481. r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
  4482. r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
  4483. r8168_mac_ocp_write(tp, 0xF892, 0xC505);
  4484. r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
  4485. r8168_mac_ocp_write(tp, 0xF896, 0xC502);
  4486. r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
  4487. r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
  4488. r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
  4489. r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
  4490. r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
  4491. r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
  4492. r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
  4493. r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
  4494. r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
  4495. r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
  4496. r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
  4497. r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
  4498. r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
  4499. r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
  4500. r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
  4501. r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
  4502. r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
  4503. r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
  4504. r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
  4505. r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
  4506. r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
  4507. r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
  4508. r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
  4509. r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
  4510. r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
  4511. r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
  4512. r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
  4513. r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
  4514. r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
  4515. r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
  4516. r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
  4517. r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
  4518. r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
  4519. r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
  4520. r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
  4521. r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
  4522. r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
  4523. r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
  4524. r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
  4525. r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
  4526. r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
  4527. r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
  4528. r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
  4529. rtl_hw_aspm_clkreq_enable(tp, true);
  4530. }
  4531. static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
  4532. {
  4533. int rg_saw_cnt;
  4534. u32 data;
  4535. static const struct ephy_info e_info_8168h_1[] = {
  4536. { 0x1e, 0x0800, 0x0001 },
  4537. { 0x1d, 0x0000, 0x0800 },
  4538. { 0x05, 0xffff, 0x2089 },
  4539. { 0x06, 0xffff, 0x5881 },
  4540. { 0x04, 0xffff, 0x154a },
  4541. { 0x01, 0xffff, 0x068b }
  4542. };
  4543. /* disable aspm and clock request before access ephy */
  4544. rtl_hw_aspm_clkreq_enable(tp, false);
  4545. rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
  4546. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  4547. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  4548. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  4549. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4550. rtl_set_def_aspm_entry_latency(tp);
  4551. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4552. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4553. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4554. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
  4555. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
  4556. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  4557. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  4558. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4559. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4560. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4561. /* Adjust EEE LED frequency */
  4562. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4563. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4564. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  4565. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  4566. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  4567. rtl_pcie_state_l2l3_enable(tp, false);
  4568. rtl_writephy(tp, 0x1f, 0x0c42);
  4569. rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
  4570. rtl_writephy(tp, 0x1f, 0x0000);
  4571. if (rg_saw_cnt > 0) {
  4572. u16 sw_cnt_1ms_ini;
  4573. sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
  4574. sw_cnt_1ms_ini &= 0x0fff;
  4575. data = r8168_mac_ocp_read(tp, 0xd412);
  4576. data &= ~0x0fff;
  4577. data |= sw_cnt_1ms_ini;
  4578. r8168_mac_ocp_write(tp, 0xd412, data);
  4579. }
  4580. data = r8168_mac_ocp_read(tp, 0xe056);
  4581. data &= ~0xf0;
  4582. data |= 0x70;
  4583. r8168_mac_ocp_write(tp, 0xe056, data);
  4584. data = r8168_mac_ocp_read(tp, 0xe052);
  4585. data &= ~0x6000;
  4586. data |= 0x8008;
  4587. r8168_mac_ocp_write(tp, 0xe052, data);
  4588. data = r8168_mac_ocp_read(tp, 0xe0d6);
  4589. data &= ~0x01ff;
  4590. data |= 0x017f;
  4591. r8168_mac_ocp_write(tp, 0xe0d6, data);
  4592. data = r8168_mac_ocp_read(tp, 0xd420);
  4593. data &= ~0x0fff;
  4594. data |= 0x047f;
  4595. r8168_mac_ocp_write(tp, 0xd420, data);
  4596. r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
  4597. r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
  4598. r8168_mac_ocp_write(tp, 0xc094, 0x0000);
  4599. r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
  4600. rtl_hw_aspm_clkreq_enable(tp, true);
  4601. }
  4602. static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
  4603. {
  4604. rtl8168ep_stop_cmac(tp);
  4605. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  4606. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
  4607. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
  4608. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4609. rtl_set_def_aspm_entry_latency(tp);
  4610. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4611. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4612. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4613. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
  4614. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  4615. RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
  4616. RTL_W8(tp, MaxTxPacketSize, EarlySize);
  4617. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4618. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4619. /* Adjust EEE LED frequency */
  4620. RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
  4621. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  4622. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
  4623. rtl_pcie_state_l2l3_enable(tp, false);
  4624. }
  4625. static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
  4626. {
  4627. static const struct ephy_info e_info_8168ep_1[] = {
  4628. { 0x00, 0xffff, 0x10ab },
  4629. { 0x06, 0xffff, 0xf030 },
  4630. { 0x08, 0xffff, 0x2006 },
  4631. { 0x0d, 0xffff, 0x1666 },
  4632. { 0x0c, 0x3ff0, 0x0000 }
  4633. };
  4634. /* disable aspm and clock request before access ephy */
  4635. rtl_hw_aspm_clkreq_enable(tp, false);
  4636. rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
  4637. rtl_hw_start_8168ep(tp);
  4638. rtl_hw_aspm_clkreq_enable(tp, true);
  4639. }
  4640. static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
  4641. {
  4642. static const struct ephy_info e_info_8168ep_2[] = {
  4643. { 0x00, 0xffff, 0x10a3 },
  4644. { 0x19, 0xffff, 0xfc00 },
  4645. { 0x1e, 0xffff, 0x20ea }
  4646. };
  4647. /* disable aspm and clock request before access ephy */
  4648. rtl_hw_aspm_clkreq_enable(tp, false);
  4649. rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
  4650. rtl_hw_start_8168ep(tp);
  4651. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4652. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  4653. rtl_hw_aspm_clkreq_enable(tp, true);
  4654. }
  4655. static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
  4656. {
  4657. u32 data;
  4658. static const struct ephy_info e_info_8168ep_3[] = {
  4659. { 0x00, 0xffff, 0x10a3 },
  4660. { 0x19, 0xffff, 0x7c00 },
  4661. { 0x1e, 0xffff, 0x20eb },
  4662. { 0x0d, 0xffff, 0x1666 }
  4663. };
  4664. /* disable aspm and clock request before access ephy */
  4665. rtl_hw_aspm_clkreq_enable(tp, false);
  4666. rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
  4667. rtl_hw_start_8168ep(tp);
  4668. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4669. RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
  4670. data = r8168_mac_ocp_read(tp, 0xd3e2);
  4671. data &= 0xf000;
  4672. data |= 0x0271;
  4673. r8168_mac_ocp_write(tp, 0xd3e2, data);
  4674. data = r8168_mac_ocp_read(tp, 0xd3e4);
  4675. data &= 0xff00;
  4676. r8168_mac_ocp_write(tp, 0xd3e4, data);
  4677. data = r8168_mac_ocp_read(tp, 0xe860);
  4678. data |= 0x0080;
  4679. r8168_mac_ocp_write(tp, 0xe860, data);
  4680. rtl_hw_aspm_clkreq_enable(tp, true);
  4681. }
  4682. static void rtl_hw_start_8168(struct rtl8169_private *tp)
  4683. {
  4684. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4685. tp->cp_cmd &= ~INTT_MASK;
  4686. tp->cp_cmd |= PktCntrDisable | INTT_1;
  4687. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4688. RTL_W16(tp, IntrMitigate, 0x5100);
  4689. /* Work around for RxFIFO overflow. */
  4690. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  4691. tp->event_slow |= RxFIFOOver | PCSTimeout;
  4692. tp->event_slow &= ~RxOverflow;
  4693. }
  4694. switch (tp->mac_version) {
  4695. case RTL_GIGA_MAC_VER_11:
  4696. rtl_hw_start_8168bb(tp);
  4697. break;
  4698. case RTL_GIGA_MAC_VER_12:
  4699. case RTL_GIGA_MAC_VER_17:
  4700. rtl_hw_start_8168bef(tp);
  4701. break;
  4702. case RTL_GIGA_MAC_VER_18:
  4703. rtl_hw_start_8168cp_1(tp);
  4704. break;
  4705. case RTL_GIGA_MAC_VER_19:
  4706. rtl_hw_start_8168c_1(tp);
  4707. break;
  4708. case RTL_GIGA_MAC_VER_20:
  4709. rtl_hw_start_8168c_2(tp);
  4710. break;
  4711. case RTL_GIGA_MAC_VER_21:
  4712. rtl_hw_start_8168c_3(tp);
  4713. break;
  4714. case RTL_GIGA_MAC_VER_22:
  4715. rtl_hw_start_8168c_4(tp);
  4716. break;
  4717. case RTL_GIGA_MAC_VER_23:
  4718. rtl_hw_start_8168cp_2(tp);
  4719. break;
  4720. case RTL_GIGA_MAC_VER_24:
  4721. rtl_hw_start_8168cp_3(tp);
  4722. break;
  4723. case RTL_GIGA_MAC_VER_25:
  4724. case RTL_GIGA_MAC_VER_26:
  4725. case RTL_GIGA_MAC_VER_27:
  4726. rtl_hw_start_8168d(tp);
  4727. break;
  4728. case RTL_GIGA_MAC_VER_28:
  4729. rtl_hw_start_8168d_4(tp);
  4730. break;
  4731. case RTL_GIGA_MAC_VER_31:
  4732. rtl_hw_start_8168dp(tp);
  4733. break;
  4734. case RTL_GIGA_MAC_VER_32:
  4735. case RTL_GIGA_MAC_VER_33:
  4736. rtl_hw_start_8168e_1(tp);
  4737. break;
  4738. case RTL_GIGA_MAC_VER_34:
  4739. rtl_hw_start_8168e_2(tp);
  4740. break;
  4741. case RTL_GIGA_MAC_VER_35:
  4742. case RTL_GIGA_MAC_VER_36:
  4743. rtl_hw_start_8168f_1(tp);
  4744. break;
  4745. case RTL_GIGA_MAC_VER_38:
  4746. rtl_hw_start_8411(tp);
  4747. break;
  4748. case RTL_GIGA_MAC_VER_40:
  4749. case RTL_GIGA_MAC_VER_41:
  4750. rtl_hw_start_8168g_1(tp);
  4751. break;
  4752. case RTL_GIGA_MAC_VER_42:
  4753. rtl_hw_start_8168g_2(tp);
  4754. break;
  4755. case RTL_GIGA_MAC_VER_44:
  4756. rtl_hw_start_8411_2(tp);
  4757. break;
  4758. case RTL_GIGA_MAC_VER_45:
  4759. case RTL_GIGA_MAC_VER_46:
  4760. rtl_hw_start_8168h_1(tp);
  4761. break;
  4762. case RTL_GIGA_MAC_VER_49:
  4763. rtl_hw_start_8168ep_1(tp);
  4764. break;
  4765. case RTL_GIGA_MAC_VER_50:
  4766. rtl_hw_start_8168ep_2(tp);
  4767. break;
  4768. case RTL_GIGA_MAC_VER_51:
  4769. rtl_hw_start_8168ep_3(tp);
  4770. break;
  4771. default:
  4772. netif_err(tp, drv, tp->dev,
  4773. "unknown chipset (mac_version = %d)\n",
  4774. tp->mac_version);
  4775. break;
  4776. }
  4777. }
  4778. static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
  4779. {
  4780. static const struct ephy_info e_info_8102e_1[] = {
  4781. { 0x01, 0, 0x6e65 },
  4782. { 0x02, 0, 0x091f },
  4783. { 0x03, 0, 0xc2f9 },
  4784. { 0x06, 0, 0xafb5 },
  4785. { 0x07, 0, 0x0e00 },
  4786. { 0x19, 0, 0xec80 },
  4787. { 0x01, 0, 0x2e65 },
  4788. { 0x01, 0, 0x6e65 }
  4789. };
  4790. u8 cfg1;
  4791. rtl_set_def_aspm_entry_latency(tp);
  4792. RTL_W8(tp, DBG_REG, FIX_NAK_1);
  4793. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4794. RTL_W8(tp, Config1,
  4795. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  4796. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4797. cfg1 = RTL_R8(tp, Config1);
  4798. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  4799. RTL_W8(tp, Config1, cfg1 & ~LEDS0);
  4800. rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  4801. }
  4802. static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
  4803. {
  4804. rtl_set_def_aspm_entry_latency(tp);
  4805. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4806. RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
  4807. RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
  4808. }
  4809. static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
  4810. {
  4811. rtl_hw_start_8102e_2(tp);
  4812. rtl_ephy_write(tp, 0x03, 0xc2f9);
  4813. }
  4814. static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
  4815. {
  4816. static const struct ephy_info e_info_8105e_1[] = {
  4817. { 0x07, 0, 0x4000 },
  4818. { 0x19, 0, 0x0200 },
  4819. { 0x19, 0, 0x0020 },
  4820. { 0x1e, 0, 0x2000 },
  4821. { 0x03, 0, 0x0001 },
  4822. { 0x19, 0, 0x0100 },
  4823. { 0x19, 0, 0x0004 },
  4824. { 0x0a, 0, 0x0020 }
  4825. };
  4826. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4827. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  4828. /* Disable Early Tally Counter */
  4829. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
  4830. RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
  4831. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
  4832. rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  4833. rtl_pcie_state_l2l3_enable(tp, false);
  4834. }
  4835. static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
  4836. {
  4837. rtl_hw_start_8105e_1(tp);
  4838. rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
  4839. }
  4840. static void rtl_hw_start_8402(struct rtl8169_private *tp)
  4841. {
  4842. static const struct ephy_info e_info_8402[] = {
  4843. { 0x19, 0xffff, 0xff64 },
  4844. { 0x1e, 0, 0x4000 }
  4845. };
  4846. rtl_set_def_aspm_entry_latency(tp);
  4847. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4848. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  4849. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  4850. rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
  4851. rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
  4852. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
  4853. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
  4854. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4855. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4856. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4857. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4858. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
  4859. rtl_pcie_state_l2l3_enable(tp, false);
  4860. }
  4861. static void rtl_hw_start_8106(struct rtl8169_private *tp)
  4862. {
  4863. rtl_hw_aspm_clkreq_enable(tp, false);
  4864. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  4865. RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
  4866. RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
  4867. RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
  4868. RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
  4869. rtl_pcie_state_l2l3_enable(tp, false);
  4870. rtl_hw_aspm_clkreq_enable(tp, true);
  4871. }
  4872. static void rtl_hw_start_8101(struct rtl8169_private *tp)
  4873. {
  4874. if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
  4875. tp->event_slow &= ~RxFIFOOver;
  4876. if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
  4877. tp->mac_version == RTL_GIGA_MAC_VER_16)
  4878. pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
  4879. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4880. RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
  4881. tp->cp_cmd &= CPCMD_QUIRK_MASK;
  4882. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  4883. switch (tp->mac_version) {
  4884. case RTL_GIGA_MAC_VER_07:
  4885. rtl_hw_start_8102e_1(tp);
  4886. break;
  4887. case RTL_GIGA_MAC_VER_08:
  4888. rtl_hw_start_8102e_3(tp);
  4889. break;
  4890. case RTL_GIGA_MAC_VER_09:
  4891. rtl_hw_start_8102e_2(tp);
  4892. break;
  4893. case RTL_GIGA_MAC_VER_29:
  4894. rtl_hw_start_8105e_1(tp);
  4895. break;
  4896. case RTL_GIGA_MAC_VER_30:
  4897. rtl_hw_start_8105e_2(tp);
  4898. break;
  4899. case RTL_GIGA_MAC_VER_37:
  4900. rtl_hw_start_8402(tp);
  4901. break;
  4902. case RTL_GIGA_MAC_VER_39:
  4903. rtl_hw_start_8106(tp);
  4904. break;
  4905. case RTL_GIGA_MAC_VER_43:
  4906. rtl_hw_start_8168g_2(tp);
  4907. break;
  4908. case RTL_GIGA_MAC_VER_47:
  4909. case RTL_GIGA_MAC_VER_48:
  4910. rtl_hw_start_8168h_1(tp);
  4911. break;
  4912. }
  4913. RTL_W16(tp, IntrMitigate, 0x0000);
  4914. }
  4915. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  4916. {
  4917. struct rtl8169_private *tp = netdev_priv(dev);
  4918. if (new_mtu > ETH_DATA_LEN)
  4919. rtl_hw_jumbo_enable(tp);
  4920. else
  4921. rtl_hw_jumbo_disable(tp);
  4922. dev->mtu = new_mtu;
  4923. netdev_update_features(dev);
  4924. return 0;
  4925. }
  4926. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  4927. {
  4928. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  4929. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  4930. }
  4931. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  4932. void **data_buff, struct RxDesc *desc)
  4933. {
  4934. dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
  4935. R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
  4936. kfree(*data_buff);
  4937. *data_buff = NULL;
  4938. rtl8169_make_unusable_by_asic(desc);
  4939. }
  4940. static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
  4941. {
  4942. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  4943. /* Force memory writes to complete before releasing descriptor */
  4944. dma_wmb();
  4945. desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
  4946. }
  4947. static inline void *rtl8169_align(void *data)
  4948. {
  4949. return (void *)ALIGN((long)data, 16);
  4950. }
  4951. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  4952. struct RxDesc *desc)
  4953. {
  4954. void *data;
  4955. dma_addr_t mapping;
  4956. struct device *d = tp_to_dev(tp);
  4957. int node = dev_to_node(d);
  4958. data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
  4959. if (!data)
  4960. return NULL;
  4961. if (rtl8169_align(data) != data) {
  4962. kfree(data);
  4963. data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
  4964. if (!data)
  4965. return NULL;
  4966. }
  4967. mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
  4968. DMA_FROM_DEVICE);
  4969. if (unlikely(dma_mapping_error(d, mapping))) {
  4970. if (net_ratelimit())
  4971. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  4972. goto err_out;
  4973. }
  4974. desc->addr = cpu_to_le64(mapping);
  4975. rtl8169_mark_to_asic(desc);
  4976. return data;
  4977. err_out:
  4978. kfree(data);
  4979. return NULL;
  4980. }
  4981. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  4982. {
  4983. unsigned int i;
  4984. for (i = 0; i < NUM_RX_DESC; i++) {
  4985. if (tp->Rx_databuff[i]) {
  4986. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  4987. tp->RxDescArray + i);
  4988. }
  4989. }
  4990. }
  4991. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  4992. {
  4993. desc->opts1 |= cpu_to_le32(RingEnd);
  4994. }
  4995. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  4996. {
  4997. unsigned int i;
  4998. for (i = 0; i < NUM_RX_DESC; i++) {
  4999. void *data;
  5000. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  5001. if (!data) {
  5002. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  5003. goto err_out;
  5004. }
  5005. tp->Rx_databuff[i] = data;
  5006. }
  5007. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  5008. return 0;
  5009. err_out:
  5010. rtl8169_rx_clear(tp);
  5011. return -ENOMEM;
  5012. }
  5013. static int rtl8169_init_ring(struct rtl8169_private *tp)
  5014. {
  5015. rtl8169_init_ring_indexes(tp);
  5016. memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
  5017. memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
  5018. return rtl8169_rx_fill(tp);
  5019. }
  5020. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  5021. struct TxDesc *desc)
  5022. {
  5023. unsigned int len = tx_skb->len;
  5024. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  5025. desc->opts1 = 0x00;
  5026. desc->opts2 = 0x00;
  5027. desc->addr = 0x00;
  5028. tx_skb->len = 0;
  5029. }
  5030. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  5031. unsigned int n)
  5032. {
  5033. unsigned int i;
  5034. for (i = 0; i < n; i++) {
  5035. unsigned int entry = (start + i) % NUM_TX_DESC;
  5036. struct ring_info *tx_skb = tp->tx_skb + entry;
  5037. unsigned int len = tx_skb->len;
  5038. if (len) {
  5039. struct sk_buff *skb = tx_skb->skb;
  5040. rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
  5041. tp->TxDescArray + entry);
  5042. if (skb) {
  5043. dev_consume_skb_any(skb);
  5044. tx_skb->skb = NULL;
  5045. }
  5046. }
  5047. }
  5048. }
  5049. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  5050. {
  5051. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  5052. tp->cur_tx = tp->dirty_tx = 0;
  5053. }
  5054. static void rtl_reset_work(struct rtl8169_private *tp)
  5055. {
  5056. struct net_device *dev = tp->dev;
  5057. int i;
  5058. napi_disable(&tp->napi);
  5059. netif_stop_queue(dev);
  5060. synchronize_sched();
  5061. rtl8169_hw_reset(tp);
  5062. for (i = 0; i < NUM_RX_DESC; i++)
  5063. rtl8169_mark_to_asic(tp->RxDescArray + i);
  5064. rtl8169_tx_clear(tp);
  5065. rtl8169_init_ring_indexes(tp);
  5066. napi_enable(&tp->napi);
  5067. rtl_hw_start(tp);
  5068. netif_wake_queue(dev);
  5069. }
  5070. static void rtl8169_tx_timeout(struct net_device *dev)
  5071. {
  5072. struct rtl8169_private *tp = netdev_priv(dev);
  5073. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5074. }
  5075. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  5076. u32 *opts)
  5077. {
  5078. struct skb_shared_info *info = skb_shinfo(skb);
  5079. unsigned int cur_frag, entry;
  5080. struct TxDesc *uninitialized_var(txd);
  5081. struct device *d = tp_to_dev(tp);
  5082. entry = tp->cur_tx;
  5083. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  5084. const skb_frag_t *frag = info->frags + cur_frag;
  5085. dma_addr_t mapping;
  5086. u32 status, len;
  5087. void *addr;
  5088. entry = (entry + 1) % NUM_TX_DESC;
  5089. txd = tp->TxDescArray + entry;
  5090. len = skb_frag_size(frag);
  5091. addr = skb_frag_address(frag);
  5092. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  5093. if (unlikely(dma_mapping_error(d, mapping))) {
  5094. if (net_ratelimit())
  5095. netif_err(tp, drv, tp->dev,
  5096. "Failed to map TX fragments DMA!\n");
  5097. goto err_out;
  5098. }
  5099. /* Anti gcc 2.95.3 bugware (sic) */
  5100. status = opts[0] | len |
  5101. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5102. txd->opts1 = cpu_to_le32(status);
  5103. txd->opts2 = cpu_to_le32(opts[1]);
  5104. txd->addr = cpu_to_le64(mapping);
  5105. tp->tx_skb[entry].len = len;
  5106. }
  5107. if (cur_frag) {
  5108. tp->tx_skb[entry].skb = skb;
  5109. txd->opts1 |= cpu_to_le32(LastFrag);
  5110. }
  5111. return cur_frag;
  5112. err_out:
  5113. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  5114. return -EIO;
  5115. }
  5116. static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
  5117. {
  5118. return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
  5119. }
  5120. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5121. struct net_device *dev);
  5122. /* r8169_csum_workaround()
  5123. * The hw limites the value the transport offset. When the offset is out of the
  5124. * range, calculate the checksum by sw.
  5125. */
  5126. static void r8169_csum_workaround(struct rtl8169_private *tp,
  5127. struct sk_buff *skb)
  5128. {
  5129. if (skb_shinfo(skb)->gso_size) {
  5130. netdev_features_t features = tp->dev->features;
  5131. struct sk_buff *segs, *nskb;
  5132. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  5133. segs = skb_gso_segment(skb, features);
  5134. if (IS_ERR(segs) || !segs)
  5135. goto drop;
  5136. do {
  5137. nskb = segs;
  5138. segs = segs->next;
  5139. nskb->next = NULL;
  5140. rtl8169_start_xmit(nskb, tp->dev);
  5141. } while (segs);
  5142. dev_consume_skb_any(skb);
  5143. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5144. if (skb_checksum_help(skb) < 0)
  5145. goto drop;
  5146. rtl8169_start_xmit(skb, tp->dev);
  5147. } else {
  5148. struct net_device_stats *stats;
  5149. drop:
  5150. stats = &tp->dev->stats;
  5151. stats->tx_dropped++;
  5152. dev_kfree_skb_any(skb);
  5153. }
  5154. }
  5155. /* msdn_giant_send_check()
  5156. * According to the document of microsoft, the TCP Pseudo Header excludes the
  5157. * packet length for IPv6 TCP large packets.
  5158. */
  5159. static int msdn_giant_send_check(struct sk_buff *skb)
  5160. {
  5161. const struct ipv6hdr *ipv6h;
  5162. struct tcphdr *th;
  5163. int ret;
  5164. ret = skb_cow_head(skb, 0);
  5165. if (ret)
  5166. return ret;
  5167. ipv6h = ipv6_hdr(skb);
  5168. th = tcp_hdr(skb);
  5169. th->check = 0;
  5170. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  5171. return ret;
  5172. }
  5173. static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
  5174. struct sk_buff *skb, u32 *opts)
  5175. {
  5176. u32 mss = skb_shinfo(skb)->gso_size;
  5177. if (mss) {
  5178. opts[0] |= TD_LSO;
  5179. opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
  5180. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5181. const struct iphdr *ip = ip_hdr(skb);
  5182. if (ip->protocol == IPPROTO_TCP)
  5183. opts[0] |= TD0_IP_CS | TD0_TCP_CS;
  5184. else if (ip->protocol == IPPROTO_UDP)
  5185. opts[0] |= TD0_IP_CS | TD0_UDP_CS;
  5186. else
  5187. WARN_ON_ONCE(1);
  5188. }
  5189. return true;
  5190. }
  5191. static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
  5192. struct sk_buff *skb, u32 *opts)
  5193. {
  5194. u32 transport_offset = (u32)skb_transport_offset(skb);
  5195. u32 mss = skb_shinfo(skb)->gso_size;
  5196. if (mss) {
  5197. if (transport_offset > GTTCPHO_MAX) {
  5198. netif_warn(tp, tx_err, tp->dev,
  5199. "Invalid transport offset 0x%x for TSO\n",
  5200. transport_offset);
  5201. return false;
  5202. }
  5203. switch (vlan_get_protocol(skb)) {
  5204. case htons(ETH_P_IP):
  5205. opts[0] |= TD1_GTSENV4;
  5206. break;
  5207. case htons(ETH_P_IPV6):
  5208. if (msdn_giant_send_check(skb))
  5209. return false;
  5210. opts[0] |= TD1_GTSENV6;
  5211. break;
  5212. default:
  5213. WARN_ON_ONCE(1);
  5214. break;
  5215. }
  5216. opts[0] |= transport_offset << GTTCPHO_SHIFT;
  5217. opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
  5218. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5219. u8 ip_protocol;
  5220. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5221. return !(skb_checksum_help(skb) || eth_skb_pad(skb));
  5222. if (transport_offset > TCPHO_MAX) {
  5223. netif_warn(tp, tx_err, tp->dev,
  5224. "Invalid transport offset 0x%x\n",
  5225. transport_offset);
  5226. return false;
  5227. }
  5228. switch (vlan_get_protocol(skb)) {
  5229. case htons(ETH_P_IP):
  5230. opts[1] |= TD1_IPv4_CS;
  5231. ip_protocol = ip_hdr(skb)->protocol;
  5232. break;
  5233. case htons(ETH_P_IPV6):
  5234. opts[1] |= TD1_IPv6_CS;
  5235. ip_protocol = ipv6_hdr(skb)->nexthdr;
  5236. break;
  5237. default:
  5238. ip_protocol = IPPROTO_RAW;
  5239. break;
  5240. }
  5241. if (ip_protocol == IPPROTO_TCP)
  5242. opts[1] |= TD1_TCP_CS;
  5243. else if (ip_protocol == IPPROTO_UDP)
  5244. opts[1] |= TD1_UDP_CS;
  5245. else
  5246. WARN_ON_ONCE(1);
  5247. opts[1] |= transport_offset << TCPHO_SHIFT;
  5248. } else {
  5249. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5250. return !eth_skb_pad(skb);
  5251. }
  5252. return true;
  5253. }
  5254. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5255. struct net_device *dev)
  5256. {
  5257. struct rtl8169_private *tp = netdev_priv(dev);
  5258. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  5259. struct TxDesc *txd = tp->TxDescArray + entry;
  5260. struct device *d = tp_to_dev(tp);
  5261. dma_addr_t mapping;
  5262. u32 status, len;
  5263. u32 opts[2];
  5264. int frags;
  5265. if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
  5266. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  5267. goto err_stop_0;
  5268. }
  5269. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  5270. goto err_stop_0;
  5271. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
  5272. opts[0] = DescOwn;
  5273. if (!tp->tso_csum(tp, skb, opts)) {
  5274. r8169_csum_workaround(tp, skb);
  5275. return NETDEV_TX_OK;
  5276. }
  5277. len = skb_headlen(skb);
  5278. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  5279. if (unlikely(dma_mapping_error(d, mapping))) {
  5280. if (net_ratelimit())
  5281. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  5282. goto err_dma_0;
  5283. }
  5284. tp->tx_skb[entry].len = len;
  5285. txd->addr = cpu_to_le64(mapping);
  5286. frags = rtl8169_xmit_frags(tp, skb, opts);
  5287. if (frags < 0)
  5288. goto err_dma_1;
  5289. else if (frags)
  5290. opts[0] |= FirstFrag;
  5291. else {
  5292. opts[0] |= FirstFrag | LastFrag;
  5293. tp->tx_skb[entry].skb = skb;
  5294. }
  5295. txd->opts2 = cpu_to_le32(opts[1]);
  5296. skb_tx_timestamp(skb);
  5297. /* Force memory writes to complete before releasing descriptor */
  5298. dma_wmb();
  5299. /* Anti gcc 2.95.3 bugware (sic) */
  5300. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5301. txd->opts1 = cpu_to_le32(status);
  5302. /* Force all memory writes to complete before notifying device */
  5303. wmb();
  5304. tp->cur_tx += frags + 1;
  5305. RTL_W8(tp, TxPoll, NPQ);
  5306. mmiowb();
  5307. if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  5308. /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
  5309. * not miss a ring update when it notices a stopped queue.
  5310. */
  5311. smp_wmb();
  5312. netif_stop_queue(dev);
  5313. /* Sync with rtl_tx:
  5314. * - publish queue status and cur_tx ring index (write barrier)
  5315. * - refresh dirty_tx ring index (read barrier).
  5316. * May the current thread have a pessimistic view of the ring
  5317. * status and forget to wake up queue, a racing rtl_tx thread
  5318. * can't.
  5319. */
  5320. smp_mb();
  5321. if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
  5322. netif_wake_queue(dev);
  5323. }
  5324. return NETDEV_TX_OK;
  5325. err_dma_1:
  5326. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  5327. err_dma_0:
  5328. dev_kfree_skb_any(skb);
  5329. dev->stats.tx_dropped++;
  5330. return NETDEV_TX_OK;
  5331. err_stop_0:
  5332. netif_stop_queue(dev);
  5333. dev->stats.tx_dropped++;
  5334. return NETDEV_TX_BUSY;
  5335. }
  5336. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  5337. {
  5338. struct rtl8169_private *tp = netdev_priv(dev);
  5339. struct pci_dev *pdev = tp->pci_dev;
  5340. u16 pci_status, pci_cmd;
  5341. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  5342. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  5343. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  5344. pci_cmd, pci_status);
  5345. /*
  5346. * The recovery sequence below admits a very elaborated explanation:
  5347. * - it seems to work;
  5348. * - I did not see what else could be done;
  5349. * - it makes iop3xx happy.
  5350. *
  5351. * Feel free to adjust to your needs.
  5352. */
  5353. if (pdev->broken_parity_status)
  5354. pci_cmd &= ~PCI_COMMAND_PARITY;
  5355. else
  5356. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  5357. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  5358. pci_write_config_word(pdev, PCI_STATUS,
  5359. pci_status & (PCI_STATUS_DETECTED_PARITY |
  5360. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  5361. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  5362. /* The infamous DAC f*ckup only happens at boot time */
  5363. if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
  5364. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  5365. tp->cp_cmd &= ~PCIDAC;
  5366. RTL_W16(tp, CPlusCmd, tp->cp_cmd);
  5367. dev->features &= ~NETIF_F_HIGHDMA;
  5368. }
  5369. rtl8169_hw_reset(tp);
  5370. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5371. }
  5372. static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
  5373. {
  5374. unsigned int dirty_tx, tx_left;
  5375. dirty_tx = tp->dirty_tx;
  5376. smp_rmb();
  5377. tx_left = tp->cur_tx - dirty_tx;
  5378. while (tx_left > 0) {
  5379. unsigned int entry = dirty_tx % NUM_TX_DESC;
  5380. struct ring_info *tx_skb = tp->tx_skb + entry;
  5381. u32 status;
  5382. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  5383. if (status & DescOwn)
  5384. break;
  5385. /* This barrier is needed to keep us from reading
  5386. * any other fields out of the Tx descriptor until
  5387. * we know the status of DescOwn
  5388. */
  5389. dma_rmb();
  5390. rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
  5391. tp->TxDescArray + entry);
  5392. if (status & LastFrag) {
  5393. u64_stats_update_begin(&tp->tx_stats.syncp);
  5394. tp->tx_stats.packets++;
  5395. tp->tx_stats.bytes += tx_skb->skb->len;
  5396. u64_stats_update_end(&tp->tx_stats.syncp);
  5397. dev_consume_skb_any(tx_skb->skb);
  5398. tx_skb->skb = NULL;
  5399. }
  5400. dirty_tx++;
  5401. tx_left--;
  5402. }
  5403. if (tp->dirty_tx != dirty_tx) {
  5404. tp->dirty_tx = dirty_tx;
  5405. /* Sync with rtl8169_start_xmit:
  5406. * - publish dirty_tx ring index (write barrier)
  5407. * - refresh cur_tx ring index and queue status (read barrier)
  5408. * May the current thread miss the stopped queue condition,
  5409. * a racing xmit thread can only have a right view of the
  5410. * ring status.
  5411. */
  5412. smp_mb();
  5413. if (netif_queue_stopped(dev) &&
  5414. TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  5415. netif_wake_queue(dev);
  5416. }
  5417. /*
  5418. * 8168 hack: TxPoll requests are lost when the Tx packets are
  5419. * too close. Let's kick an extra TxPoll request when a burst
  5420. * of start_xmit activity is detected (if it is not detected,
  5421. * it is slow enough). -- FR
  5422. */
  5423. if (tp->cur_tx != dirty_tx)
  5424. RTL_W8(tp, TxPoll, NPQ);
  5425. }
  5426. }
  5427. static inline int rtl8169_fragmented_frame(u32 status)
  5428. {
  5429. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  5430. }
  5431. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  5432. {
  5433. u32 status = opts1 & RxProtoMask;
  5434. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  5435. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  5436. skb->ip_summed = CHECKSUM_UNNECESSARY;
  5437. else
  5438. skb_checksum_none_assert(skb);
  5439. }
  5440. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  5441. struct rtl8169_private *tp,
  5442. int pkt_size,
  5443. dma_addr_t addr)
  5444. {
  5445. struct sk_buff *skb;
  5446. struct device *d = tp_to_dev(tp);
  5447. data = rtl8169_align(data);
  5448. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  5449. prefetch(data);
  5450. skb = napi_alloc_skb(&tp->napi, pkt_size);
  5451. if (skb)
  5452. skb_copy_to_linear_data(skb, data, pkt_size);
  5453. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  5454. return skb;
  5455. }
  5456. static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
  5457. {
  5458. unsigned int cur_rx, rx_left;
  5459. unsigned int count;
  5460. cur_rx = tp->cur_rx;
  5461. for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
  5462. unsigned int entry = cur_rx % NUM_RX_DESC;
  5463. struct RxDesc *desc = tp->RxDescArray + entry;
  5464. u32 status;
  5465. status = le32_to_cpu(desc->opts1);
  5466. if (status & DescOwn)
  5467. break;
  5468. /* This barrier is needed to keep us from reading
  5469. * any other fields out of the Rx descriptor until
  5470. * we know the status of DescOwn
  5471. */
  5472. dma_rmb();
  5473. if (unlikely(status & RxRES)) {
  5474. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  5475. status);
  5476. dev->stats.rx_errors++;
  5477. if (status & (RxRWT | RxRUNT))
  5478. dev->stats.rx_length_errors++;
  5479. if (status & RxCRC)
  5480. dev->stats.rx_crc_errors++;
  5481. /* RxFOVF is a reserved bit on later chip versions */
  5482. if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
  5483. status & RxFOVF) {
  5484. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5485. dev->stats.rx_fifo_errors++;
  5486. } else if (status & (RxRUNT | RxCRC) &&
  5487. !(status & RxRWT) &&
  5488. dev->features & NETIF_F_RXALL) {
  5489. goto process_pkt;
  5490. }
  5491. } else {
  5492. struct sk_buff *skb;
  5493. dma_addr_t addr;
  5494. int pkt_size;
  5495. process_pkt:
  5496. addr = le64_to_cpu(desc->addr);
  5497. if (likely(!(dev->features & NETIF_F_RXFCS)))
  5498. pkt_size = (status & 0x00003fff) - 4;
  5499. else
  5500. pkt_size = status & 0x00003fff;
  5501. /*
  5502. * The driver does not support incoming fragmented
  5503. * frames. They are seen as a symptom of over-mtu
  5504. * sized frames.
  5505. */
  5506. if (unlikely(rtl8169_fragmented_frame(status))) {
  5507. dev->stats.rx_dropped++;
  5508. dev->stats.rx_length_errors++;
  5509. goto release_descriptor;
  5510. }
  5511. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  5512. tp, pkt_size, addr);
  5513. if (!skb) {
  5514. dev->stats.rx_dropped++;
  5515. goto release_descriptor;
  5516. }
  5517. rtl8169_rx_csum(skb, status);
  5518. skb_put(skb, pkt_size);
  5519. skb->protocol = eth_type_trans(skb, dev);
  5520. rtl8169_rx_vlan_tag(desc, skb);
  5521. if (skb->pkt_type == PACKET_MULTICAST)
  5522. dev->stats.multicast++;
  5523. napi_gro_receive(&tp->napi, skb);
  5524. u64_stats_update_begin(&tp->rx_stats.syncp);
  5525. tp->rx_stats.packets++;
  5526. tp->rx_stats.bytes += pkt_size;
  5527. u64_stats_update_end(&tp->rx_stats.syncp);
  5528. }
  5529. release_descriptor:
  5530. desc->opts2 = 0;
  5531. rtl8169_mark_to_asic(desc);
  5532. }
  5533. count = cur_rx - tp->cur_rx;
  5534. tp->cur_rx = cur_rx;
  5535. return count;
  5536. }
  5537. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  5538. {
  5539. struct rtl8169_private *tp = dev_instance;
  5540. u16 status = rtl_get_events(tp);
  5541. if (status == 0xffff || !(status & (RTL_EVENT_NAPI | tp->event_slow)))
  5542. return IRQ_NONE;
  5543. rtl_irq_disable(tp);
  5544. napi_schedule_irqoff(&tp->napi);
  5545. return IRQ_HANDLED;
  5546. }
  5547. /*
  5548. * Workqueue context.
  5549. */
  5550. static void rtl_slow_event_work(struct rtl8169_private *tp)
  5551. {
  5552. struct net_device *dev = tp->dev;
  5553. u16 status;
  5554. status = rtl_get_events(tp) & tp->event_slow;
  5555. rtl_ack_events(tp, status);
  5556. if (unlikely(status & RxFIFOOver)) {
  5557. switch (tp->mac_version) {
  5558. /* Work around for rx fifo overflow */
  5559. case RTL_GIGA_MAC_VER_11:
  5560. netif_stop_queue(dev);
  5561. /* XXX - Hack alert. See rtl_task(). */
  5562. set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
  5563. default:
  5564. break;
  5565. }
  5566. }
  5567. if (unlikely(status & SYSErr))
  5568. rtl8169_pcierr_interrupt(dev);
  5569. if (status & LinkChg)
  5570. phy_mac_interrupt(dev->phydev);
  5571. rtl_irq_enable_all(tp);
  5572. }
  5573. static void rtl_task(struct work_struct *work)
  5574. {
  5575. static const struct {
  5576. int bitnr;
  5577. void (*action)(struct rtl8169_private *);
  5578. } rtl_work[] = {
  5579. /* XXX - keep rtl_slow_event_work() as first element. */
  5580. { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
  5581. { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
  5582. };
  5583. struct rtl8169_private *tp =
  5584. container_of(work, struct rtl8169_private, wk.work);
  5585. struct net_device *dev = tp->dev;
  5586. int i;
  5587. rtl_lock_work(tp);
  5588. if (!netif_running(dev) ||
  5589. !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
  5590. goto out_unlock;
  5591. for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
  5592. bool pending;
  5593. pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
  5594. if (pending)
  5595. rtl_work[i].action(tp);
  5596. }
  5597. out_unlock:
  5598. rtl_unlock_work(tp);
  5599. }
  5600. static int rtl8169_poll(struct napi_struct *napi, int budget)
  5601. {
  5602. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  5603. struct net_device *dev = tp->dev;
  5604. u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
  5605. int work_done;
  5606. u16 status;
  5607. status = rtl_get_events(tp);
  5608. rtl_ack_events(tp, status & ~tp->event_slow);
  5609. work_done = rtl_rx(dev, tp, (u32) budget);
  5610. rtl_tx(dev, tp);
  5611. if (status & tp->event_slow) {
  5612. enable_mask &= ~tp->event_slow;
  5613. rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
  5614. }
  5615. if (work_done < budget) {
  5616. napi_complete_done(napi, work_done);
  5617. rtl_irq_enable(tp, enable_mask);
  5618. mmiowb();
  5619. }
  5620. return work_done;
  5621. }
  5622. static void rtl8169_rx_missed(struct net_device *dev)
  5623. {
  5624. struct rtl8169_private *tp = netdev_priv(dev);
  5625. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  5626. return;
  5627. dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
  5628. RTL_W32(tp, RxMissed, 0);
  5629. }
  5630. static void r8169_phylink_handler(struct net_device *ndev)
  5631. {
  5632. struct rtl8169_private *tp = netdev_priv(ndev);
  5633. if (netif_carrier_ok(ndev)) {
  5634. rtl_link_chg_patch(tp);
  5635. pm_request_resume(&tp->pci_dev->dev);
  5636. } else {
  5637. pm_runtime_idle(&tp->pci_dev->dev);
  5638. }
  5639. if (net_ratelimit())
  5640. phy_print_status(ndev->phydev);
  5641. }
  5642. static int r8169_phy_connect(struct rtl8169_private *tp)
  5643. {
  5644. struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
  5645. phy_interface_t phy_mode;
  5646. int ret;
  5647. phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
  5648. PHY_INTERFACE_MODE_MII;
  5649. ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
  5650. phy_mode);
  5651. if (ret)
  5652. return ret;
  5653. if (!tp->supports_gmii)
  5654. phy_set_max_speed(phydev, SPEED_100);
  5655. /* Ensure to advertise everything, incl. pause */
  5656. phydev->advertising = phydev->supported;
  5657. phy_attached_info(phydev);
  5658. return 0;
  5659. }
  5660. static void rtl8169_down(struct net_device *dev)
  5661. {
  5662. struct rtl8169_private *tp = netdev_priv(dev);
  5663. phy_stop(dev->phydev);
  5664. napi_disable(&tp->napi);
  5665. netif_stop_queue(dev);
  5666. rtl8169_hw_reset(tp);
  5667. /*
  5668. * At this point device interrupts can not be enabled in any function,
  5669. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
  5670. * and napi is disabled (rtl8169_poll).
  5671. */
  5672. rtl8169_rx_missed(dev);
  5673. /* Give a racing hard_start_xmit a few cycles to complete. */
  5674. synchronize_sched();
  5675. rtl8169_tx_clear(tp);
  5676. rtl8169_rx_clear(tp);
  5677. rtl_pll_power_down(tp);
  5678. }
  5679. static int rtl8169_close(struct net_device *dev)
  5680. {
  5681. struct rtl8169_private *tp = netdev_priv(dev);
  5682. struct pci_dev *pdev = tp->pci_dev;
  5683. pm_runtime_get_sync(&pdev->dev);
  5684. /* Update counters before going down */
  5685. rtl8169_update_counters(tp);
  5686. rtl_lock_work(tp);
  5687. /* Clear all task flags */
  5688. bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
  5689. rtl8169_down(dev);
  5690. rtl_unlock_work(tp);
  5691. cancel_work_sync(&tp->wk.work);
  5692. phy_disconnect(dev->phydev);
  5693. pci_free_irq(pdev, 0, tp);
  5694. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  5695. tp->RxPhyAddr);
  5696. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  5697. tp->TxPhyAddr);
  5698. tp->TxDescArray = NULL;
  5699. tp->RxDescArray = NULL;
  5700. pm_runtime_put_sync(&pdev->dev);
  5701. return 0;
  5702. }
  5703. #ifdef CONFIG_NET_POLL_CONTROLLER
  5704. static void rtl8169_netpoll(struct net_device *dev)
  5705. {
  5706. struct rtl8169_private *tp = netdev_priv(dev);
  5707. rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
  5708. }
  5709. #endif
  5710. static int rtl_open(struct net_device *dev)
  5711. {
  5712. struct rtl8169_private *tp = netdev_priv(dev);
  5713. struct pci_dev *pdev = tp->pci_dev;
  5714. int retval = -ENOMEM;
  5715. pm_runtime_get_sync(&pdev->dev);
  5716. /*
  5717. * Rx and Tx descriptors needs 256 bytes alignment.
  5718. * dma_alloc_coherent provides more.
  5719. */
  5720. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  5721. &tp->TxPhyAddr, GFP_KERNEL);
  5722. if (!tp->TxDescArray)
  5723. goto err_pm_runtime_put;
  5724. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  5725. &tp->RxPhyAddr, GFP_KERNEL);
  5726. if (!tp->RxDescArray)
  5727. goto err_free_tx_0;
  5728. retval = rtl8169_init_ring(tp);
  5729. if (retval < 0)
  5730. goto err_free_rx_1;
  5731. INIT_WORK(&tp->wk.work, rtl_task);
  5732. smp_mb();
  5733. rtl_request_firmware(tp);
  5734. retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
  5735. dev->name);
  5736. if (retval < 0)
  5737. goto err_release_fw_2;
  5738. retval = r8169_phy_connect(tp);
  5739. if (retval)
  5740. goto err_free_irq;
  5741. rtl_lock_work(tp);
  5742. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  5743. napi_enable(&tp->napi);
  5744. rtl8169_init_phy(dev, tp);
  5745. rtl_pll_power_up(tp);
  5746. rtl_hw_start(tp);
  5747. if (!rtl8169_init_counter_offsets(tp))
  5748. netif_warn(tp, hw, dev, "counter reset/update failed\n");
  5749. phy_start(dev->phydev);
  5750. netif_start_queue(dev);
  5751. rtl_unlock_work(tp);
  5752. pm_runtime_put_sync(&pdev->dev);
  5753. out:
  5754. return retval;
  5755. err_free_irq:
  5756. pci_free_irq(pdev, 0, tp);
  5757. err_release_fw_2:
  5758. rtl_release_firmware(tp);
  5759. rtl8169_rx_clear(tp);
  5760. err_free_rx_1:
  5761. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  5762. tp->RxPhyAddr);
  5763. tp->RxDescArray = NULL;
  5764. err_free_tx_0:
  5765. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  5766. tp->TxPhyAddr);
  5767. tp->TxDescArray = NULL;
  5768. err_pm_runtime_put:
  5769. pm_runtime_put_noidle(&pdev->dev);
  5770. goto out;
  5771. }
  5772. static void
  5773. rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  5774. {
  5775. struct rtl8169_private *tp = netdev_priv(dev);
  5776. struct pci_dev *pdev = tp->pci_dev;
  5777. struct rtl8169_counters *counters = tp->counters;
  5778. unsigned int start;
  5779. pm_runtime_get_noresume(&pdev->dev);
  5780. if (netif_running(dev) && pm_runtime_active(&pdev->dev))
  5781. rtl8169_rx_missed(dev);
  5782. do {
  5783. start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
  5784. stats->rx_packets = tp->rx_stats.packets;
  5785. stats->rx_bytes = tp->rx_stats.bytes;
  5786. } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
  5787. do {
  5788. start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
  5789. stats->tx_packets = tp->tx_stats.packets;
  5790. stats->tx_bytes = tp->tx_stats.bytes;
  5791. } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
  5792. stats->rx_dropped = dev->stats.rx_dropped;
  5793. stats->tx_dropped = dev->stats.tx_dropped;
  5794. stats->rx_length_errors = dev->stats.rx_length_errors;
  5795. stats->rx_errors = dev->stats.rx_errors;
  5796. stats->rx_crc_errors = dev->stats.rx_crc_errors;
  5797. stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
  5798. stats->rx_missed_errors = dev->stats.rx_missed_errors;
  5799. stats->multicast = dev->stats.multicast;
  5800. /*
  5801. * Fetch additonal counter values missing in stats collected by driver
  5802. * from tally counters.
  5803. */
  5804. if (pm_runtime_active(&pdev->dev))
  5805. rtl8169_update_counters(tp);
  5806. /*
  5807. * Subtract values fetched during initalization.
  5808. * See rtl8169_init_counter_offsets for a description why we do that.
  5809. */
  5810. stats->tx_errors = le64_to_cpu(counters->tx_errors) -
  5811. le64_to_cpu(tp->tc_offset.tx_errors);
  5812. stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
  5813. le32_to_cpu(tp->tc_offset.tx_multi_collision);
  5814. stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
  5815. le16_to_cpu(tp->tc_offset.tx_aborted);
  5816. pm_runtime_put_noidle(&pdev->dev);
  5817. }
  5818. static void rtl8169_net_suspend(struct net_device *dev)
  5819. {
  5820. struct rtl8169_private *tp = netdev_priv(dev);
  5821. if (!netif_running(dev))
  5822. return;
  5823. phy_stop(dev->phydev);
  5824. netif_device_detach(dev);
  5825. netif_stop_queue(dev);
  5826. rtl_lock_work(tp);
  5827. napi_disable(&tp->napi);
  5828. /* Clear all task flags */
  5829. bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
  5830. rtl_unlock_work(tp);
  5831. rtl_pll_power_down(tp);
  5832. }
  5833. #ifdef CONFIG_PM
  5834. static int rtl8169_suspend(struct device *device)
  5835. {
  5836. struct pci_dev *pdev = to_pci_dev(device);
  5837. struct net_device *dev = pci_get_drvdata(pdev);
  5838. struct rtl8169_private *tp = netdev_priv(dev);
  5839. rtl8169_net_suspend(dev);
  5840. clk_disable_unprepare(tp->clk);
  5841. return 0;
  5842. }
  5843. static void __rtl8169_resume(struct net_device *dev)
  5844. {
  5845. struct rtl8169_private *tp = netdev_priv(dev);
  5846. netif_device_attach(dev);
  5847. rtl_pll_power_up(tp);
  5848. rtl8169_init_phy(dev, tp);
  5849. phy_start(tp->dev->phydev);
  5850. rtl_lock_work(tp);
  5851. napi_enable(&tp->napi);
  5852. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  5853. rtl_unlock_work(tp);
  5854. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5855. }
  5856. static int rtl8169_resume(struct device *device)
  5857. {
  5858. struct pci_dev *pdev = to_pci_dev(device);
  5859. struct net_device *dev = pci_get_drvdata(pdev);
  5860. struct rtl8169_private *tp = netdev_priv(dev);
  5861. clk_prepare_enable(tp->clk);
  5862. if (netif_running(dev))
  5863. __rtl8169_resume(dev);
  5864. return 0;
  5865. }
  5866. static int rtl8169_runtime_suspend(struct device *device)
  5867. {
  5868. struct pci_dev *pdev = to_pci_dev(device);
  5869. struct net_device *dev = pci_get_drvdata(pdev);
  5870. struct rtl8169_private *tp = netdev_priv(dev);
  5871. if (!tp->TxDescArray)
  5872. return 0;
  5873. rtl_lock_work(tp);
  5874. __rtl8169_set_wol(tp, WAKE_ANY);
  5875. rtl_unlock_work(tp);
  5876. rtl8169_net_suspend(dev);
  5877. /* Update counters before going runtime suspend */
  5878. rtl8169_rx_missed(dev);
  5879. rtl8169_update_counters(tp);
  5880. return 0;
  5881. }
  5882. static int rtl8169_runtime_resume(struct device *device)
  5883. {
  5884. struct pci_dev *pdev = to_pci_dev(device);
  5885. struct net_device *dev = pci_get_drvdata(pdev);
  5886. struct rtl8169_private *tp = netdev_priv(dev);
  5887. rtl_rar_set(tp, dev->dev_addr);
  5888. if (!tp->TxDescArray)
  5889. return 0;
  5890. rtl_lock_work(tp);
  5891. __rtl8169_set_wol(tp, tp->saved_wolopts);
  5892. rtl_unlock_work(tp);
  5893. __rtl8169_resume(dev);
  5894. return 0;
  5895. }
  5896. static int rtl8169_runtime_idle(struct device *device)
  5897. {
  5898. struct pci_dev *pdev = to_pci_dev(device);
  5899. struct net_device *dev = pci_get_drvdata(pdev);
  5900. if (!netif_running(dev) || !netif_carrier_ok(dev))
  5901. pm_schedule_suspend(device, 10000);
  5902. return -EBUSY;
  5903. }
  5904. static const struct dev_pm_ops rtl8169_pm_ops = {
  5905. .suspend = rtl8169_suspend,
  5906. .resume = rtl8169_resume,
  5907. .freeze = rtl8169_suspend,
  5908. .thaw = rtl8169_resume,
  5909. .poweroff = rtl8169_suspend,
  5910. .restore = rtl8169_resume,
  5911. .runtime_suspend = rtl8169_runtime_suspend,
  5912. .runtime_resume = rtl8169_runtime_resume,
  5913. .runtime_idle = rtl8169_runtime_idle,
  5914. };
  5915. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  5916. #else /* !CONFIG_PM */
  5917. #define RTL8169_PM_OPS NULL
  5918. #endif /* !CONFIG_PM */
  5919. static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
  5920. {
  5921. /* WoL fails with 8168b when the receiver is disabled. */
  5922. switch (tp->mac_version) {
  5923. case RTL_GIGA_MAC_VER_11:
  5924. case RTL_GIGA_MAC_VER_12:
  5925. case RTL_GIGA_MAC_VER_17:
  5926. pci_clear_master(tp->pci_dev);
  5927. RTL_W8(tp, ChipCmd, CmdRxEnb);
  5928. /* PCI commit */
  5929. RTL_R8(tp, ChipCmd);
  5930. break;
  5931. default:
  5932. break;
  5933. }
  5934. }
  5935. static void rtl_shutdown(struct pci_dev *pdev)
  5936. {
  5937. struct net_device *dev = pci_get_drvdata(pdev);
  5938. struct rtl8169_private *tp = netdev_priv(dev);
  5939. rtl8169_net_suspend(dev);
  5940. /* Restore original MAC address */
  5941. rtl_rar_set(tp, dev->perm_addr);
  5942. rtl8169_hw_reset(tp);
  5943. if (system_state == SYSTEM_POWER_OFF) {
  5944. if (tp->saved_wolopts) {
  5945. rtl_wol_suspend_quirk(tp);
  5946. rtl_wol_shutdown_quirk(tp);
  5947. }
  5948. pci_wake_from_d3(pdev, true);
  5949. pci_set_power_state(pdev, PCI_D3hot);
  5950. }
  5951. }
  5952. static void rtl_remove_one(struct pci_dev *pdev)
  5953. {
  5954. struct net_device *dev = pci_get_drvdata(pdev);
  5955. struct rtl8169_private *tp = netdev_priv(dev);
  5956. if (r8168_check_dash(tp))
  5957. rtl8168_driver_stop(tp);
  5958. netif_napi_del(&tp->napi);
  5959. unregister_netdev(dev);
  5960. mdiobus_unregister(tp->mii_bus);
  5961. rtl_release_firmware(tp);
  5962. if (pci_dev_run_wake(pdev))
  5963. pm_runtime_get_noresume(&pdev->dev);
  5964. /* restore original MAC address */
  5965. rtl_rar_set(tp, dev->perm_addr);
  5966. }
  5967. static const struct net_device_ops rtl_netdev_ops = {
  5968. .ndo_open = rtl_open,
  5969. .ndo_stop = rtl8169_close,
  5970. .ndo_get_stats64 = rtl8169_get_stats64,
  5971. .ndo_start_xmit = rtl8169_start_xmit,
  5972. .ndo_tx_timeout = rtl8169_tx_timeout,
  5973. .ndo_validate_addr = eth_validate_addr,
  5974. .ndo_change_mtu = rtl8169_change_mtu,
  5975. .ndo_fix_features = rtl8169_fix_features,
  5976. .ndo_set_features = rtl8169_set_features,
  5977. .ndo_set_mac_address = rtl_set_mac_address,
  5978. .ndo_do_ioctl = rtl8169_ioctl,
  5979. .ndo_set_rx_mode = rtl_set_rx_mode,
  5980. #ifdef CONFIG_NET_POLL_CONTROLLER
  5981. .ndo_poll_controller = rtl8169_netpoll,
  5982. #endif
  5983. };
  5984. static const struct rtl_cfg_info {
  5985. void (*hw_start)(struct rtl8169_private *tp);
  5986. u16 event_slow;
  5987. unsigned int has_gmii:1;
  5988. const struct rtl_coalesce_info *coalesce_info;
  5989. u8 default_ver;
  5990. } rtl_cfg_infos [] = {
  5991. [RTL_CFG_0] = {
  5992. .hw_start = rtl_hw_start_8169,
  5993. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
  5994. .has_gmii = 1,
  5995. .coalesce_info = rtl_coalesce_info_8169,
  5996. .default_ver = RTL_GIGA_MAC_VER_01,
  5997. },
  5998. [RTL_CFG_1] = {
  5999. .hw_start = rtl_hw_start_8168,
  6000. .event_slow = SYSErr | LinkChg | RxOverflow,
  6001. .has_gmii = 1,
  6002. .coalesce_info = rtl_coalesce_info_8168_8136,
  6003. .default_ver = RTL_GIGA_MAC_VER_11,
  6004. },
  6005. [RTL_CFG_2] = {
  6006. .hw_start = rtl_hw_start_8101,
  6007. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
  6008. PCSTimeout,
  6009. .coalesce_info = rtl_coalesce_info_8168_8136,
  6010. .default_ver = RTL_GIGA_MAC_VER_13,
  6011. }
  6012. };
  6013. static int rtl_alloc_irq(struct rtl8169_private *tp)
  6014. {
  6015. unsigned int flags;
  6016. switch (tp->mac_version) {
  6017. case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
  6018. RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
  6019. RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
  6020. RTL_W8(tp, Cfg9346, Cfg9346_Lock);
  6021. /* fall through */
  6022. case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17:
  6023. flags = PCI_IRQ_LEGACY;
  6024. break;
  6025. default:
  6026. flags = PCI_IRQ_ALL_TYPES;
  6027. break;
  6028. }
  6029. return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
  6030. }
  6031. DECLARE_RTL_COND(rtl_link_list_ready_cond)
  6032. {
  6033. return RTL_R8(tp, MCU) & LINK_LIST_RDY;
  6034. }
  6035. DECLARE_RTL_COND(rtl_rxtx_empty_cond)
  6036. {
  6037. return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
  6038. }
  6039. static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
  6040. {
  6041. struct rtl8169_private *tp = mii_bus->priv;
  6042. if (phyaddr > 0)
  6043. return -ENODEV;
  6044. return rtl_readphy(tp, phyreg);
  6045. }
  6046. static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
  6047. int phyreg, u16 val)
  6048. {
  6049. struct rtl8169_private *tp = mii_bus->priv;
  6050. if (phyaddr > 0)
  6051. return -ENODEV;
  6052. rtl_writephy(tp, phyreg, val);
  6053. return 0;
  6054. }
  6055. static int r8169_mdio_register(struct rtl8169_private *tp)
  6056. {
  6057. struct pci_dev *pdev = tp->pci_dev;
  6058. struct phy_device *phydev;
  6059. struct mii_bus *new_bus;
  6060. int ret;
  6061. new_bus = devm_mdiobus_alloc(&pdev->dev);
  6062. if (!new_bus)
  6063. return -ENOMEM;
  6064. new_bus->name = "r8169";
  6065. new_bus->priv = tp;
  6066. new_bus->parent = &pdev->dev;
  6067. new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
  6068. snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
  6069. PCI_DEVID(pdev->bus->number, pdev->devfn));
  6070. new_bus->read = r8169_mdio_read_reg;
  6071. new_bus->write = r8169_mdio_write_reg;
  6072. ret = mdiobus_register(new_bus);
  6073. if (ret)
  6074. return ret;
  6075. phydev = mdiobus_get_phy(new_bus, 0);
  6076. if (!phydev) {
  6077. mdiobus_unregister(new_bus);
  6078. return -ENODEV;
  6079. }
  6080. /* PHY will be woken up in rtl_open() */
  6081. phy_suspend(phydev);
  6082. tp->mii_bus = new_bus;
  6083. return 0;
  6084. }
  6085. static void rtl_hw_init_8168g(struct rtl8169_private *tp)
  6086. {
  6087. u32 data;
  6088. tp->ocp_base = OCP_STD_PHY_BASE;
  6089. RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
  6090. if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
  6091. return;
  6092. if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
  6093. return;
  6094. RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
  6095. msleep(1);
  6096. RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
  6097. data = r8168_mac_ocp_read(tp, 0xe8de);
  6098. data &= ~(1 << 14);
  6099. r8168_mac_ocp_write(tp, 0xe8de, data);
  6100. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6101. return;
  6102. data = r8168_mac_ocp_read(tp, 0xe8de);
  6103. data |= (1 << 15);
  6104. r8168_mac_ocp_write(tp, 0xe8de, data);
  6105. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6106. return;
  6107. }
  6108. static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
  6109. {
  6110. rtl8168ep_stop_cmac(tp);
  6111. rtl_hw_init_8168g(tp);
  6112. }
  6113. static void rtl_hw_initialize(struct rtl8169_private *tp)
  6114. {
  6115. switch (tp->mac_version) {
  6116. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
  6117. rtl_hw_init_8168g(tp);
  6118. break;
  6119. case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
  6120. rtl_hw_init_8168ep(tp);
  6121. break;
  6122. default:
  6123. break;
  6124. }
  6125. }
  6126. /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
  6127. static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
  6128. {
  6129. switch (tp->mac_version) {
  6130. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  6131. case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
  6132. return false;
  6133. default:
  6134. return true;
  6135. }
  6136. }
  6137. static int rtl_jumbo_max(struct rtl8169_private *tp)
  6138. {
  6139. /* Non-GBit versions don't support jumbo frames */
  6140. if (!tp->supports_gmii)
  6141. return JUMBO_1K;
  6142. switch (tp->mac_version) {
  6143. /* RTL8169 */
  6144. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
  6145. return JUMBO_7K;
  6146. /* RTL8168b */
  6147. case RTL_GIGA_MAC_VER_11:
  6148. case RTL_GIGA_MAC_VER_12:
  6149. case RTL_GIGA_MAC_VER_17:
  6150. return JUMBO_4K;
  6151. /* RTL8168c */
  6152. case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
  6153. return JUMBO_6K;
  6154. default:
  6155. return JUMBO_9K;
  6156. }
  6157. }
  6158. static void rtl_disable_clk(void *data)
  6159. {
  6160. clk_disable_unprepare(data);
  6161. }
  6162. static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  6163. {
  6164. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  6165. struct rtl8169_private *tp;
  6166. struct net_device *dev;
  6167. int chipset, region, i;
  6168. int jumbo_max, rc;
  6169. dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
  6170. if (!dev)
  6171. return -ENOMEM;
  6172. SET_NETDEV_DEV(dev, &pdev->dev);
  6173. dev->netdev_ops = &rtl_netdev_ops;
  6174. tp = netdev_priv(dev);
  6175. tp->dev = dev;
  6176. tp->pci_dev = pdev;
  6177. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  6178. tp->supports_gmii = cfg->has_gmii;
  6179. /* Get the *optional* external "ether_clk" used on some boards */
  6180. tp->clk = devm_clk_get(&pdev->dev, "ether_clk");
  6181. if (IS_ERR(tp->clk)) {
  6182. rc = PTR_ERR(tp->clk);
  6183. if (rc == -ENOENT) {
  6184. /* clk-core allows NULL (for suspend / resume) */
  6185. tp->clk = NULL;
  6186. } else if (rc == -EPROBE_DEFER) {
  6187. return rc;
  6188. } else {
  6189. dev_err(&pdev->dev, "failed to get clk: %d\n", rc);
  6190. return rc;
  6191. }
  6192. } else {
  6193. rc = clk_prepare_enable(tp->clk);
  6194. if (rc) {
  6195. dev_err(&pdev->dev, "failed to enable clk: %d\n", rc);
  6196. return rc;
  6197. }
  6198. rc = devm_add_action_or_reset(&pdev->dev, rtl_disable_clk,
  6199. tp->clk);
  6200. if (rc)
  6201. return rc;
  6202. }
  6203. /* Disable ASPM completely as that cause random device stop working
  6204. * problems as well as full system hangs for some PCIe devices users.
  6205. */
  6206. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  6207. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  6208. rc = pcim_enable_device(pdev);
  6209. if (rc < 0) {
  6210. dev_err(&pdev->dev, "enable failure\n");
  6211. return rc;
  6212. }
  6213. if (pcim_set_mwi(pdev) < 0)
  6214. dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
  6215. /* use first MMIO region */
  6216. region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
  6217. if (region < 0) {
  6218. dev_err(&pdev->dev, "no MMIO resource found\n");
  6219. return -ENODEV;
  6220. }
  6221. /* check for weird/broken PCI region reporting */
  6222. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  6223. dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
  6224. return -ENODEV;
  6225. }
  6226. rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
  6227. if (rc < 0) {
  6228. dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
  6229. return rc;
  6230. }
  6231. tp->mmio_addr = pcim_iomap_table(pdev)[region];
  6232. if (!pci_is_pcie(pdev))
  6233. dev_info(&pdev->dev, "not PCI Express\n");
  6234. /* Identify chip attached to board */
  6235. rtl8169_get_mac_version(tp, cfg->default_ver);
  6236. if (rtl_tbi_enabled(tp)) {
  6237. dev_err(&pdev->dev, "TBI fiber mode not supported\n");
  6238. return -ENODEV;
  6239. }
  6240. tp->cp_cmd = RTL_R16(tp, CPlusCmd);
  6241. if ((sizeof(dma_addr_t) > 4) &&
  6242. (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
  6243. tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
  6244. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  6245. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  6246. /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
  6247. if (!pci_is_pcie(pdev))
  6248. tp->cp_cmd |= PCIDAC;
  6249. dev->features |= NETIF_F_HIGHDMA;
  6250. } else {
  6251. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  6252. if (rc < 0) {
  6253. dev_err(&pdev->dev, "DMA configuration failed\n");
  6254. return rc;
  6255. }
  6256. }
  6257. rtl_init_rxcfg(tp);
  6258. rtl_irq_disable(tp);
  6259. rtl_hw_initialize(tp);
  6260. rtl_hw_reset(tp);
  6261. rtl_ack_events(tp, 0xffff);
  6262. pci_set_master(pdev);
  6263. rtl_init_mdio_ops(tp);
  6264. rtl_init_jumbo_ops(tp);
  6265. rtl8169_print_mac_version(tp);
  6266. chipset = tp->mac_version;
  6267. rc = rtl_alloc_irq(tp);
  6268. if (rc < 0) {
  6269. dev_err(&pdev->dev, "Can't allocate interrupt\n");
  6270. return rc;
  6271. }
  6272. tp->saved_wolopts = __rtl8169_get_wol(tp);
  6273. mutex_init(&tp->wk.mutex);
  6274. u64_stats_init(&tp->rx_stats.syncp);
  6275. u64_stats_init(&tp->tx_stats.syncp);
  6276. /* Get MAC address */
  6277. switch (tp->mac_version) {
  6278. u8 mac_addr[ETH_ALEN] __aligned(4);
  6279. case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
  6280. case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
  6281. *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
  6282. *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
  6283. if (is_valid_ether_addr(mac_addr))
  6284. rtl_rar_set(tp, mac_addr);
  6285. break;
  6286. default:
  6287. break;
  6288. }
  6289. for (i = 0; i < ETH_ALEN; i++)
  6290. dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
  6291. dev->ethtool_ops = &rtl8169_ethtool_ops;
  6292. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  6293. netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
  6294. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  6295. * properly for all devices */
  6296. dev->features |= NETIF_F_RXCSUM |
  6297. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  6298. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6299. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
  6300. NETIF_F_HW_VLAN_CTAG_RX;
  6301. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6302. NETIF_F_HIGHDMA;
  6303. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  6304. tp->cp_cmd |= RxChkSum | RxVlan;
  6305. /*
  6306. * Pretend we are using VLANs; This bypasses a nasty bug where
  6307. * Interrupts stop flowing on high load on 8110SCd controllers.
  6308. */
  6309. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  6310. /* Disallow toggling */
  6311. dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  6312. if (rtl_chip_supports_csum_v2(tp)) {
  6313. tp->tso_csum = rtl8169_tso_csum_v2;
  6314. dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  6315. } else {
  6316. tp->tso_csum = rtl8169_tso_csum_v1;
  6317. }
  6318. dev->hw_features |= NETIF_F_RXALL;
  6319. dev->hw_features |= NETIF_F_RXFCS;
  6320. /* MTU range: 60 - hw-specific max */
  6321. dev->min_mtu = ETH_ZLEN;
  6322. jumbo_max = rtl_jumbo_max(tp);
  6323. dev->max_mtu = jumbo_max;
  6324. tp->hw_start = cfg->hw_start;
  6325. tp->event_slow = cfg->event_slow;
  6326. tp->coalesce_info = cfg->coalesce_info;
  6327. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  6328. tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
  6329. &tp->counters_phys_addr,
  6330. GFP_KERNEL);
  6331. if (!tp->counters)
  6332. return -ENOMEM;
  6333. pci_set_drvdata(pdev, dev);
  6334. rc = r8169_mdio_register(tp);
  6335. if (rc)
  6336. return rc;
  6337. /* chip gets powered up in rtl_open() */
  6338. rtl_pll_power_down(tp);
  6339. rc = register_netdev(dev);
  6340. if (rc)
  6341. goto err_mdio_unregister;
  6342. netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
  6343. rtl_chip_infos[chipset].name, dev->dev_addr,
  6344. (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
  6345. pci_irq_vector(pdev, 0));
  6346. if (jumbo_max > JUMBO_1K)
  6347. netif_info(tp, probe, dev,
  6348. "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
  6349. jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
  6350. "ok" : "ko");
  6351. if (r8168_check_dash(tp))
  6352. rtl8168_driver_start(tp);
  6353. if (pci_dev_run_wake(pdev))
  6354. pm_runtime_put_sync(&pdev->dev);
  6355. return 0;
  6356. err_mdio_unregister:
  6357. mdiobus_unregister(tp->mii_bus);
  6358. return rc;
  6359. }
  6360. static struct pci_driver rtl8169_pci_driver = {
  6361. .name = MODULENAME,
  6362. .id_table = rtl8169_pci_tbl,
  6363. .probe = rtl_init_one,
  6364. .remove = rtl_remove_one,
  6365. .shutdown = rtl_shutdown,
  6366. .driver.pm = RTL8169_PM_OPS,
  6367. };
  6368. module_pci_driver(rtl8169_pci_driver);