r8169.c 150 KB

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