r8169.c 151 KB

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