r8169.c 149 KB

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