r8169.c 208 KB

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