r8169.c 117 KB

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