r8169.c 138 KB

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