r8169.c 209 KB

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