r8169.c 167 KB

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