r8169.c 128 KB

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