r8169.c 168 KB

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