r8169.c 161 KB

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