r8169.c 205 KB

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