r8169.c 195 KB

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