r8169.c 171 KB

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