r8169.c 117 KB

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