r8169.c 159 KB

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