r8169.c 149 KB

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