r8169.c 168 KB

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