r8169.c 161 KB

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