r8169.c 150 KB

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