r8169.c 119 KB

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