r8169.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907
  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 <asm/system.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #define RTL8169_VERSION "2.3LK-NAPI"
  29. #define MODULENAME "r8169"
  30. #define PFX MODULENAME ": "
  31. #ifdef RTL8169_DEBUG
  32. #define assert(expr) \
  33. if (!(expr)) { \
  34. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  35. #expr,__FILE__,__func__,__LINE__); \
  36. }
  37. #define dprintk(fmt, args...) \
  38. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  39. #else
  40. #define assert(expr) do {} while (0)
  41. #define dprintk(fmt, args...) do {} while (0)
  42. #endif /* RTL8169_DEBUG */
  43. #define R8169_MSG_DEFAULT \
  44. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  45. #define TX_BUFFS_AVAIL(tp) \
  46. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  47. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  48. static const int max_interrupt_work = 20;
  49. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  50. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  51. static const int multicast_filter_limit = 32;
  52. /* MAC address length */
  53. #define MAC_ADDR_LEN 6
  54. #define MAX_READ_REQUEST_SHIFT 12
  55. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  56. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  57. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  58. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  59. #define RxPacketMaxSize 0x3FE8 /* 16K - 1 - ETH_HLEN - VLAN - CRC... */
  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. /* write/read MMIO register */
  72. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  73. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  74. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  75. #define RTL_R8(reg) readb (ioaddr + (reg))
  76. #define RTL_R16(reg) readw (ioaddr + (reg))
  77. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  78. enum mac_version {
  79. RTL_GIGA_MAC_VER_01 = 0x01, // 8169
  80. RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
  81. RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
  82. RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
  83. RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
  84. RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
  85. RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
  86. RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
  87. RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
  88. RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
  89. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  90. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
  91. RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
  92. RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
  93. RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
  94. RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
  95. RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
  96. RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
  97. RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
  98. RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
  99. RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
  100. RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
  101. RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
  102. RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
  103. RTL_GIGA_MAC_VER_25 = 0x19 // 8168D
  104. };
  105. #define _R(NAME,MAC,MASK) \
  106. { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
  107. static const struct {
  108. const char *name;
  109. u8 mac_version;
  110. u32 RxConfigMask; /* Clears the bits supported by this chip */
  111. } rtl_chip_info[] = {
  112. _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
  113. _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
  114. _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
  115. _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
  116. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
  117. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
  118. _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
  119. _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
  120. _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
  121. _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
  122. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
  123. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
  124. _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
  125. _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
  126. _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
  127. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
  128. _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
  129. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
  130. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
  131. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
  132. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
  133. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
  134. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
  135. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
  136. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880) // PCI-E
  137. };
  138. #undef _R
  139. enum cfg_version {
  140. RTL_CFG_0 = 0x00,
  141. RTL_CFG_1,
  142. RTL_CFG_2
  143. };
  144. static void rtl_hw_start_8169(struct net_device *);
  145. static void rtl_hw_start_8168(struct net_device *);
  146. static void rtl_hw_start_8101(struct net_device *);
  147. static struct pci_device_id rtl8169_pci_tbl[] = {
  148. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  149. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  150. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  151. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  152. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  153. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  154. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  155. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  156. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  157. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  158. { 0x0001, 0x8168,
  159. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  160. {0,},
  161. };
  162. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  163. static int rx_copybreak = 200;
  164. static int use_dac;
  165. static struct {
  166. u32 msg_enable;
  167. } debug = { -1 };
  168. enum rtl_registers {
  169. MAC0 = 0, /* Ethernet hardware address. */
  170. MAC4 = 4,
  171. MAR0 = 8, /* Multicast filter. */
  172. CounterAddrLow = 0x10,
  173. CounterAddrHigh = 0x14,
  174. TxDescStartAddrLow = 0x20,
  175. TxDescStartAddrHigh = 0x24,
  176. TxHDescStartAddrLow = 0x28,
  177. TxHDescStartAddrHigh = 0x2c,
  178. FLASH = 0x30,
  179. ERSR = 0x36,
  180. ChipCmd = 0x37,
  181. TxPoll = 0x38,
  182. IntrMask = 0x3c,
  183. IntrStatus = 0x3e,
  184. TxConfig = 0x40,
  185. RxConfig = 0x44,
  186. RxMissed = 0x4c,
  187. Cfg9346 = 0x50,
  188. Config0 = 0x51,
  189. Config1 = 0x52,
  190. Config2 = 0x53,
  191. Config3 = 0x54,
  192. Config4 = 0x55,
  193. Config5 = 0x56,
  194. MultiIntr = 0x5c,
  195. PHYAR = 0x60,
  196. PHYstatus = 0x6c,
  197. RxMaxSize = 0xda,
  198. CPlusCmd = 0xe0,
  199. IntrMitigate = 0xe2,
  200. RxDescAddrLow = 0xe4,
  201. RxDescAddrHigh = 0xe8,
  202. EarlyTxThres = 0xec,
  203. FuncEvent = 0xf0,
  204. FuncEventMask = 0xf4,
  205. FuncPresetState = 0xf8,
  206. FuncForceEvent = 0xfc,
  207. };
  208. enum rtl8110_registers {
  209. TBICSR = 0x64,
  210. TBI_ANAR = 0x68,
  211. TBI_LPAR = 0x6a,
  212. };
  213. enum rtl8168_8101_registers {
  214. CSIDR = 0x64,
  215. CSIAR = 0x68,
  216. #define CSIAR_FLAG 0x80000000
  217. #define CSIAR_WRITE_CMD 0x80000000
  218. #define CSIAR_BYTE_ENABLE 0x0f
  219. #define CSIAR_BYTE_ENABLE_SHIFT 12
  220. #define CSIAR_ADDR_MASK 0x0fff
  221. EPHYAR = 0x80,
  222. #define EPHYAR_FLAG 0x80000000
  223. #define EPHYAR_WRITE_CMD 0x80000000
  224. #define EPHYAR_REG_MASK 0x1f
  225. #define EPHYAR_REG_SHIFT 16
  226. #define EPHYAR_DATA_MASK 0xffff
  227. DBG_REG = 0xd1,
  228. #define FIX_NAK_1 (1 << 4)
  229. #define FIX_NAK_2 (1 << 3)
  230. };
  231. enum rtl_register_content {
  232. /* InterruptStatusBits */
  233. SYSErr = 0x8000,
  234. PCSTimeout = 0x4000,
  235. SWInt = 0x0100,
  236. TxDescUnavail = 0x0080,
  237. RxFIFOOver = 0x0040,
  238. LinkChg = 0x0020,
  239. RxOverflow = 0x0010,
  240. TxErr = 0x0008,
  241. TxOK = 0x0004,
  242. RxErr = 0x0002,
  243. RxOK = 0x0001,
  244. /* RxStatusDesc */
  245. RxFOVF = (1 << 23),
  246. RxRWT = (1 << 22),
  247. RxRES = (1 << 21),
  248. RxRUNT = (1 << 20),
  249. RxCRC = (1 << 19),
  250. /* ChipCmdBits */
  251. CmdReset = 0x10,
  252. CmdRxEnb = 0x08,
  253. CmdTxEnb = 0x04,
  254. RxBufEmpty = 0x01,
  255. /* TXPoll register p.5 */
  256. HPQ = 0x80, /* Poll cmd on the high prio queue */
  257. NPQ = 0x40, /* Poll cmd on the low prio queue */
  258. FSWInt = 0x01, /* Forced software interrupt */
  259. /* Cfg9346Bits */
  260. Cfg9346_Lock = 0x00,
  261. Cfg9346_Unlock = 0xc0,
  262. /* rx_mode_bits */
  263. AcceptErr = 0x20,
  264. AcceptRunt = 0x10,
  265. AcceptBroadcast = 0x08,
  266. AcceptMulticast = 0x04,
  267. AcceptMyPhys = 0x02,
  268. AcceptAllPhys = 0x01,
  269. /* RxConfigBits */
  270. RxCfgFIFOShift = 13,
  271. RxCfgDMAShift = 8,
  272. /* TxConfigBits */
  273. TxInterFrameGapShift = 24,
  274. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  275. /* Config1 register p.24 */
  276. LEDS1 = (1 << 7),
  277. LEDS0 = (1 << 6),
  278. MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
  279. Speed_down = (1 << 4),
  280. MEMMAP = (1 << 3),
  281. IOMAP = (1 << 2),
  282. VPD = (1 << 1),
  283. PMEnable = (1 << 0), /* Power Management Enable */
  284. /* Config2 register p. 25 */
  285. PCI_Clock_66MHz = 0x01,
  286. PCI_Clock_33MHz = 0x00,
  287. /* Config3 register p.25 */
  288. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  289. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  290. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  291. /* Config5 register p.27 */
  292. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  293. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  294. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  295. LanWake = (1 << 1), /* LanWake enable/disable */
  296. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  297. /* TBICSR p.28 */
  298. TBIReset = 0x80000000,
  299. TBILoopback = 0x40000000,
  300. TBINwEnable = 0x20000000,
  301. TBINwRestart = 0x10000000,
  302. TBILinkOk = 0x02000000,
  303. TBINwComplete = 0x01000000,
  304. /* CPlusCmd p.31 */
  305. EnableBist = (1 << 15), // 8168 8101
  306. Mac_dbgo_oe = (1 << 14), // 8168 8101
  307. Normal_mode = (1 << 13), // unused
  308. Force_half_dup = (1 << 12), // 8168 8101
  309. Force_rxflow_en = (1 << 11), // 8168 8101
  310. Force_txflow_en = (1 << 10), // 8168 8101
  311. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  312. ASF = (1 << 8), // 8168 8101
  313. PktCntrDisable = (1 << 7), // 8168 8101
  314. Mac_dbgo_sel = 0x001c, // 8168
  315. RxVlan = (1 << 6),
  316. RxChkSum = (1 << 5),
  317. PCIDAC = (1 << 4),
  318. PCIMulRW = (1 << 3),
  319. INTT_0 = 0x0000, // 8168
  320. INTT_1 = 0x0001, // 8168
  321. INTT_2 = 0x0002, // 8168
  322. INTT_3 = 0x0003, // 8168
  323. /* rtl8169_PHYstatus */
  324. TBI_Enable = 0x80,
  325. TxFlowCtrl = 0x40,
  326. RxFlowCtrl = 0x20,
  327. _1000bpsF = 0x10,
  328. _100bps = 0x08,
  329. _10bps = 0x04,
  330. LinkStatus = 0x02,
  331. FullDup = 0x01,
  332. /* _TBICSRBit */
  333. TBILinkOK = 0x02000000,
  334. /* DumpCounterCommand */
  335. CounterDump = 0x8,
  336. };
  337. enum desc_status_bit {
  338. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  339. RingEnd = (1 << 30), /* End of descriptor ring */
  340. FirstFrag = (1 << 29), /* First segment of a packet */
  341. LastFrag = (1 << 28), /* Final segment of a packet */
  342. /* Tx private */
  343. LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
  344. MSSShift = 16, /* MSS value position */
  345. MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
  346. IPCS = (1 << 18), /* Calculate IP checksum */
  347. UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
  348. TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
  349. TxVlanTag = (1 << 17), /* Add VLAN tag */
  350. /* Rx private */
  351. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  352. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  353. #define RxProtoUDP (PID1)
  354. #define RxProtoTCP (PID0)
  355. #define RxProtoIP (PID1 | PID0)
  356. #define RxProtoMask RxProtoIP
  357. IPFail = (1 << 16), /* IP checksum failed */
  358. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  359. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  360. RxVlanTag = (1 << 16), /* VLAN tag available */
  361. };
  362. #define RsvdMask 0x3fffc000
  363. struct TxDesc {
  364. __le32 opts1;
  365. __le32 opts2;
  366. __le64 addr;
  367. };
  368. struct RxDesc {
  369. __le32 opts1;
  370. __le32 opts2;
  371. __le64 addr;
  372. };
  373. struct ring_info {
  374. struct sk_buff *skb;
  375. u32 len;
  376. u8 __pad[sizeof(void *) - sizeof(u32)];
  377. };
  378. enum features {
  379. RTL_FEATURE_WOL = (1 << 0),
  380. RTL_FEATURE_MSI = (1 << 1),
  381. RTL_FEATURE_GMII = (1 << 2),
  382. };
  383. struct rtl8169_private {
  384. void __iomem *mmio_addr; /* memory map physical address */
  385. struct pci_dev *pci_dev; /* Index of PCI device */
  386. struct net_device *dev;
  387. struct napi_struct napi;
  388. spinlock_t lock; /* spin lock flag */
  389. u32 msg_enable;
  390. int chipset;
  391. int mac_version;
  392. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  393. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  394. u32 dirty_rx;
  395. u32 dirty_tx;
  396. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  397. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  398. dma_addr_t TxPhyAddr;
  399. dma_addr_t RxPhyAddr;
  400. struct sk_buff *Rx_skbuff[NUM_RX_DESC]; /* Rx data buffers */
  401. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  402. unsigned align;
  403. unsigned rx_buf_sz;
  404. struct timer_list timer;
  405. u16 cp_cmd;
  406. u16 intr_event;
  407. u16 napi_event;
  408. u16 intr_mask;
  409. int phy_auto_nego_reg;
  410. int phy_1000_ctrl_reg;
  411. #ifdef CONFIG_R8169_VLAN
  412. struct vlan_group *vlgrp;
  413. #endif
  414. int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
  415. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  416. void (*phy_reset_enable)(void __iomem *);
  417. void (*hw_start)(struct net_device *);
  418. unsigned int (*phy_reset_pending)(void __iomem *);
  419. unsigned int (*link_ok)(void __iomem *);
  420. int pcie_cap;
  421. struct delayed_work task;
  422. unsigned features;
  423. struct mii_if_info mii;
  424. };
  425. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  426. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  427. module_param(rx_copybreak, int, 0);
  428. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  429. module_param(use_dac, int, 0);
  430. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  431. module_param_named(debug, debug.msg_enable, int, 0);
  432. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  433. MODULE_LICENSE("GPL");
  434. MODULE_VERSION(RTL8169_VERSION);
  435. static int rtl8169_open(struct net_device *dev);
  436. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev);
  437. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  438. static int rtl8169_init_ring(struct net_device *dev);
  439. static void rtl_hw_start(struct net_device *dev);
  440. static int rtl8169_close(struct net_device *dev);
  441. static void rtl_set_rx_mode(struct net_device *dev);
  442. static void rtl8169_tx_timeout(struct net_device *dev);
  443. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  444. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  445. void __iomem *, u32 budget);
  446. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  447. static void rtl8169_down(struct net_device *dev);
  448. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  449. static int rtl8169_poll(struct napi_struct *napi, int budget);
  450. static const unsigned int rtl8169_rx_config =
  451. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  452. static void mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  453. {
  454. int i;
  455. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  456. for (i = 20; i > 0; i--) {
  457. /*
  458. * Check if the RTL8169 has completed writing to the specified
  459. * MII register.
  460. */
  461. if (!(RTL_R32(PHYAR) & 0x80000000))
  462. break;
  463. udelay(25);
  464. }
  465. }
  466. static int mdio_read(void __iomem *ioaddr, int reg_addr)
  467. {
  468. int i, value = -1;
  469. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  470. for (i = 20; i > 0; i--) {
  471. /*
  472. * Check if the RTL8169 has completed retrieving data from
  473. * the specified MII register.
  474. */
  475. if (RTL_R32(PHYAR) & 0x80000000) {
  476. value = RTL_R32(PHYAR) & 0xffff;
  477. break;
  478. }
  479. udelay(25);
  480. }
  481. return value;
  482. }
  483. static void mdio_patch(void __iomem *ioaddr, int reg_addr, int value)
  484. {
  485. mdio_write(ioaddr, reg_addr, mdio_read(ioaddr, reg_addr) | value);
  486. }
  487. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  488. int val)
  489. {
  490. struct rtl8169_private *tp = netdev_priv(dev);
  491. void __iomem *ioaddr = tp->mmio_addr;
  492. mdio_write(ioaddr, location, val);
  493. }
  494. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  495. {
  496. struct rtl8169_private *tp = netdev_priv(dev);
  497. void __iomem *ioaddr = tp->mmio_addr;
  498. return mdio_read(ioaddr, location);
  499. }
  500. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  501. {
  502. unsigned int i;
  503. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  504. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  505. for (i = 0; i < 100; i++) {
  506. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  507. break;
  508. udelay(10);
  509. }
  510. }
  511. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  512. {
  513. u16 value = 0xffff;
  514. unsigned int i;
  515. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  516. for (i = 0; i < 100; i++) {
  517. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  518. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  519. break;
  520. }
  521. udelay(10);
  522. }
  523. return value;
  524. }
  525. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  526. {
  527. unsigned int i;
  528. RTL_W32(CSIDR, value);
  529. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  530. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  531. for (i = 0; i < 100; i++) {
  532. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  533. break;
  534. udelay(10);
  535. }
  536. }
  537. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  538. {
  539. u32 value = ~0x00;
  540. unsigned int i;
  541. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  542. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  543. for (i = 0; i < 100; i++) {
  544. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  545. value = RTL_R32(CSIDR);
  546. break;
  547. }
  548. udelay(10);
  549. }
  550. return value;
  551. }
  552. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  553. {
  554. RTL_W16(IntrMask, 0x0000);
  555. RTL_W16(IntrStatus, 0xffff);
  556. }
  557. static void rtl8169_asic_down(void __iomem *ioaddr)
  558. {
  559. RTL_W8(ChipCmd, 0x00);
  560. rtl8169_irq_mask_and_ack(ioaddr);
  561. RTL_R16(CPlusCmd);
  562. }
  563. static unsigned int rtl8169_tbi_reset_pending(void __iomem *ioaddr)
  564. {
  565. return RTL_R32(TBICSR) & TBIReset;
  566. }
  567. static unsigned int rtl8169_xmii_reset_pending(void __iomem *ioaddr)
  568. {
  569. return mdio_read(ioaddr, MII_BMCR) & BMCR_RESET;
  570. }
  571. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  572. {
  573. return RTL_R32(TBICSR) & TBILinkOk;
  574. }
  575. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  576. {
  577. return RTL_R8(PHYstatus) & LinkStatus;
  578. }
  579. static void rtl8169_tbi_reset_enable(void __iomem *ioaddr)
  580. {
  581. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  582. }
  583. static void rtl8169_xmii_reset_enable(void __iomem *ioaddr)
  584. {
  585. unsigned int val;
  586. val = mdio_read(ioaddr, MII_BMCR) | BMCR_RESET;
  587. mdio_write(ioaddr, MII_BMCR, val & 0xffff);
  588. }
  589. static void rtl8169_check_link_status(struct net_device *dev,
  590. struct rtl8169_private *tp,
  591. void __iomem *ioaddr)
  592. {
  593. unsigned long flags;
  594. spin_lock_irqsave(&tp->lock, flags);
  595. if (tp->link_ok(ioaddr)) {
  596. netif_carrier_on(dev);
  597. if (netif_msg_ifup(tp))
  598. printk(KERN_INFO PFX "%s: link up\n", dev->name);
  599. } else {
  600. if (netif_msg_ifdown(tp))
  601. printk(KERN_INFO PFX "%s: link down\n", dev->name);
  602. netif_carrier_off(dev);
  603. }
  604. spin_unlock_irqrestore(&tp->lock, flags);
  605. }
  606. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  607. {
  608. struct rtl8169_private *tp = netdev_priv(dev);
  609. void __iomem *ioaddr = tp->mmio_addr;
  610. u8 options;
  611. wol->wolopts = 0;
  612. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  613. wol->supported = WAKE_ANY;
  614. spin_lock_irq(&tp->lock);
  615. options = RTL_R8(Config1);
  616. if (!(options & PMEnable))
  617. goto out_unlock;
  618. options = RTL_R8(Config3);
  619. if (options & LinkUp)
  620. wol->wolopts |= WAKE_PHY;
  621. if (options & MagicPacket)
  622. wol->wolopts |= WAKE_MAGIC;
  623. options = RTL_R8(Config5);
  624. if (options & UWF)
  625. wol->wolopts |= WAKE_UCAST;
  626. if (options & BWF)
  627. wol->wolopts |= WAKE_BCAST;
  628. if (options & MWF)
  629. wol->wolopts |= WAKE_MCAST;
  630. out_unlock:
  631. spin_unlock_irq(&tp->lock);
  632. }
  633. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  634. {
  635. struct rtl8169_private *tp = netdev_priv(dev);
  636. void __iomem *ioaddr = tp->mmio_addr;
  637. unsigned int i;
  638. static struct {
  639. u32 opt;
  640. u16 reg;
  641. u8 mask;
  642. } cfg[] = {
  643. { WAKE_ANY, Config1, PMEnable },
  644. { WAKE_PHY, Config3, LinkUp },
  645. { WAKE_MAGIC, Config3, MagicPacket },
  646. { WAKE_UCAST, Config5, UWF },
  647. { WAKE_BCAST, Config5, BWF },
  648. { WAKE_MCAST, Config5, MWF },
  649. { WAKE_ANY, Config5, LanWake }
  650. };
  651. spin_lock_irq(&tp->lock);
  652. RTL_W8(Cfg9346, Cfg9346_Unlock);
  653. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  654. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  655. if (wol->wolopts & cfg[i].opt)
  656. options |= cfg[i].mask;
  657. RTL_W8(cfg[i].reg, options);
  658. }
  659. RTL_W8(Cfg9346, Cfg9346_Lock);
  660. if (wol->wolopts)
  661. tp->features |= RTL_FEATURE_WOL;
  662. else
  663. tp->features &= ~RTL_FEATURE_WOL;
  664. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  665. spin_unlock_irq(&tp->lock);
  666. return 0;
  667. }
  668. static void rtl8169_get_drvinfo(struct net_device *dev,
  669. struct ethtool_drvinfo *info)
  670. {
  671. struct rtl8169_private *tp = netdev_priv(dev);
  672. strcpy(info->driver, MODULENAME);
  673. strcpy(info->version, RTL8169_VERSION);
  674. strcpy(info->bus_info, pci_name(tp->pci_dev));
  675. }
  676. static int rtl8169_get_regs_len(struct net_device *dev)
  677. {
  678. return R8169_REGS_SIZE;
  679. }
  680. static int rtl8169_set_speed_tbi(struct net_device *dev,
  681. u8 autoneg, u16 speed, u8 duplex)
  682. {
  683. struct rtl8169_private *tp = netdev_priv(dev);
  684. void __iomem *ioaddr = tp->mmio_addr;
  685. int ret = 0;
  686. u32 reg;
  687. reg = RTL_R32(TBICSR);
  688. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  689. (duplex == DUPLEX_FULL)) {
  690. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  691. } else if (autoneg == AUTONEG_ENABLE)
  692. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  693. else {
  694. if (netif_msg_link(tp)) {
  695. printk(KERN_WARNING "%s: "
  696. "incorrect speed setting refused in TBI mode\n",
  697. dev->name);
  698. }
  699. ret = -EOPNOTSUPP;
  700. }
  701. return ret;
  702. }
  703. static int rtl8169_set_speed_xmii(struct net_device *dev,
  704. u8 autoneg, u16 speed, u8 duplex)
  705. {
  706. struct rtl8169_private *tp = netdev_priv(dev);
  707. void __iomem *ioaddr = tp->mmio_addr;
  708. int auto_nego, giga_ctrl;
  709. auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
  710. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  711. ADVERTISE_100HALF | ADVERTISE_100FULL);
  712. giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
  713. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  714. if (autoneg == AUTONEG_ENABLE) {
  715. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  716. ADVERTISE_100HALF | ADVERTISE_100FULL);
  717. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  718. } else {
  719. if (speed == SPEED_10)
  720. auto_nego |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  721. else if (speed == SPEED_100)
  722. auto_nego |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  723. else if (speed == SPEED_1000)
  724. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  725. if (duplex == DUPLEX_HALF)
  726. auto_nego &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
  727. if (duplex == DUPLEX_FULL)
  728. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
  729. /* This tweak comes straight from Realtek's driver. */
  730. if ((speed == SPEED_100) && (duplex == DUPLEX_HALF) &&
  731. ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  732. (tp->mac_version == RTL_GIGA_MAC_VER_16))) {
  733. auto_nego = ADVERTISE_100HALF | ADVERTISE_CSMA;
  734. }
  735. }
  736. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  737. if ((tp->mac_version == RTL_GIGA_MAC_VER_07) ||
  738. (tp->mac_version == RTL_GIGA_MAC_VER_08) ||
  739. (tp->mac_version == RTL_GIGA_MAC_VER_09) ||
  740. (tp->mac_version == RTL_GIGA_MAC_VER_10) ||
  741. (tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  742. (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
  743. (tp->mac_version == RTL_GIGA_MAC_VER_15) ||
  744. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  745. if ((giga_ctrl & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) &&
  746. netif_msg_link(tp)) {
  747. printk(KERN_INFO "%s: PHY does not support 1000Mbps.\n",
  748. dev->name);
  749. }
  750. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  751. }
  752. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  753. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  754. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  755. (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
  756. /*
  757. * Wake up the PHY.
  758. * Vendor specific (0x1f) and reserved (0x0e) MII registers.
  759. */
  760. mdio_write(ioaddr, 0x1f, 0x0000);
  761. mdio_write(ioaddr, 0x0e, 0x0000);
  762. }
  763. tp->phy_auto_nego_reg = auto_nego;
  764. tp->phy_1000_ctrl_reg = giga_ctrl;
  765. mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
  766. mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
  767. mdio_write(ioaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  768. return 0;
  769. }
  770. static int rtl8169_set_speed(struct net_device *dev,
  771. u8 autoneg, u16 speed, u8 duplex)
  772. {
  773. struct rtl8169_private *tp = netdev_priv(dev);
  774. int ret;
  775. ret = tp->set_speed(dev, autoneg, speed, duplex);
  776. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  777. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  778. return ret;
  779. }
  780. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  781. {
  782. struct rtl8169_private *tp = netdev_priv(dev);
  783. unsigned long flags;
  784. int ret;
  785. spin_lock_irqsave(&tp->lock, flags);
  786. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  787. spin_unlock_irqrestore(&tp->lock, flags);
  788. return ret;
  789. }
  790. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  791. {
  792. struct rtl8169_private *tp = netdev_priv(dev);
  793. return tp->cp_cmd & RxChkSum;
  794. }
  795. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  796. {
  797. struct rtl8169_private *tp = netdev_priv(dev);
  798. void __iomem *ioaddr = tp->mmio_addr;
  799. unsigned long flags;
  800. spin_lock_irqsave(&tp->lock, flags);
  801. if (data)
  802. tp->cp_cmd |= RxChkSum;
  803. else
  804. tp->cp_cmd &= ~RxChkSum;
  805. RTL_W16(CPlusCmd, tp->cp_cmd);
  806. RTL_R16(CPlusCmd);
  807. spin_unlock_irqrestore(&tp->lock, flags);
  808. return 0;
  809. }
  810. #ifdef CONFIG_R8169_VLAN
  811. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  812. struct sk_buff *skb)
  813. {
  814. return (tp->vlgrp && vlan_tx_tag_present(skb)) ?
  815. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  816. }
  817. static void rtl8169_vlan_rx_register(struct net_device *dev,
  818. struct vlan_group *grp)
  819. {
  820. struct rtl8169_private *tp = netdev_priv(dev);
  821. void __iomem *ioaddr = tp->mmio_addr;
  822. unsigned long flags;
  823. spin_lock_irqsave(&tp->lock, flags);
  824. tp->vlgrp = grp;
  825. if (tp->vlgrp)
  826. tp->cp_cmd |= RxVlan;
  827. else
  828. tp->cp_cmd &= ~RxVlan;
  829. RTL_W16(CPlusCmd, tp->cp_cmd);
  830. RTL_R16(CPlusCmd);
  831. spin_unlock_irqrestore(&tp->lock, flags);
  832. }
  833. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  834. struct sk_buff *skb)
  835. {
  836. u32 opts2 = le32_to_cpu(desc->opts2);
  837. struct vlan_group *vlgrp = tp->vlgrp;
  838. int ret;
  839. if (vlgrp && (opts2 & RxVlanTag)) {
  840. vlan_hwaccel_receive_skb(skb, vlgrp, swab16(opts2 & 0xffff));
  841. ret = 0;
  842. } else
  843. ret = -1;
  844. desc->opts2 = 0;
  845. return ret;
  846. }
  847. #else /* !CONFIG_R8169_VLAN */
  848. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  849. struct sk_buff *skb)
  850. {
  851. return 0;
  852. }
  853. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  854. struct sk_buff *skb)
  855. {
  856. return -1;
  857. }
  858. #endif
  859. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  860. {
  861. struct rtl8169_private *tp = netdev_priv(dev);
  862. void __iomem *ioaddr = tp->mmio_addr;
  863. u32 status;
  864. cmd->supported =
  865. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  866. cmd->port = PORT_FIBRE;
  867. cmd->transceiver = XCVR_INTERNAL;
  868. status = RTL_R32(TBICSR);
  869. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  870. cmd->autoneg = !!(status & TBINwEnable);
  871. cmd->speed = SPEED_1000;
  872. cmd->duplex = DUPLEX_FULL; /* Always set */
  873. return 0;
  874. }
  875. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  876. {
  877. struct rtl8169_private *tp = netdev_priv(dev);
  878. return mii_ethtool_gset(&tp->mii, cmd);
  879. }
  880. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  881. {
  882. struct rtl8169_private *tp = netdev_priv(dev);
  883. unsigned long flags;
  884. int rc;
  885. spin_lock_irqsave(&tp->lock, flags);
  886. rc = tp->get_settings(dev, cmd);
  887. spin_unlock_irqrestore(&tp->lock, flags);
  888. return rc;
  889. }
  890. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  891. void *p)
  892. {
  893. struct rtl8169_private *tp = netdev_priv(dev);
  894. unsigned long flags;
  895. if (regs->len > R8169_REGS_SIZE)
  896. regs->len = R8169_REGS_SIZE;
  897. spin_lock_irqsave(&tp->lock, flags);
  898. memcpy_fromio(p, tp->mmio_addr, regs->len);
  899. spin_unlock_irqrestore(&tp->lock, flags);
  900. }
  901. static u32 rtl8169_get_msglevel(struct net_device *dev)
  902. {
  903. struct rtl8169_private *tp = netdev_priv(dev);
  904. return tp->msg_enable;
  905. }
  906. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  907. {
  908. struct rtl8169_private *tp = netdev_priv(dev);
  909. tp->msg_enable = value;
  910. }
  911. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  912. "tx_packets",
  913. "rx_packets",
  914. "tx_errors",
  915. "rx_errors",
  916. "rx_missed",
  917. "align_errors",
  918. "tx_single_collisions",
  919. "tx_multi_collisions",
  920. "unicast",
  921. "broadcast",
  922. "multicast",
  923. "tx_aborted",
  924. "tx_underrun",
  925. };
  926. struct rtl8169_counters {
  927. __le64 tx_packets;
  928. __le64 rx_packets;
  929. __le64 tx_errors;
  930. __le32 rx_errors;
  931. __le16 rx_missed;
  932. __le16 align_errors;
  933. __le32 tx_one_collision;
  934. __le32 tx_multi_collision;
  935. __le64 rx_unicast;
  936. __le64 rx_broadcast;
  937. __le32 rx_multicast;
  938. __le16 tx_aborted;
  939. __le16 tx_underun;
  940. };
  941. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  942. {
  943. switch (sset) {
  944. case ETH_SS_STATS:
  945. return ARRAY_SIZE(rtl8169_gstrings);
  946. default:
  947. return -EOPNOTSUPP;
  948. }
  949. }
  950. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  951. struct ethtool_stats *stats, u64 *data)
  952. {
  953. struct rtl8169_private *tp = netdev_priv(dev);
  954. void __iomem *ioaddr = tp->mmio_addr;
  955. struct rtl8169_counters *counters;
  956. dma_addr_t paddr;
  957. u32 cmd;
  958. ASSERT_RTNL();
  959. counters = pci_alloc_consistent(tp->pci_dev, sizeof(*counters), &paddr);
  960. if (!counters)
  961. return;
  962. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  963. cmd = (u64)paddr & DMA_32BIT_MASK;
  964. RTL_W32(CounterAddrLow, cmd);
  965. RTL_W32(CounterAddrLow, cmd | CounterDump);
  966. while (RTL_R32(CounterAddrLow) & CounterDump) {
  967. if (msleep_interruptible(1))
  968. break;
  969. }
  970. RTL_W32(CounterAddrLow, 0);
  971. RTL_W32(CounterAddrHigh, 0);
  972. data[0] = le64_to_cpu(counters->tx_packets);
  973. data[1] = le64_to_cpu(counters->rx_packets);
  974. data[2] = le64_to_cpu(counters->tx_errors);
  975. data[3] = le32_to_cpu(counters->rx_errors);
  976. data[4] = le16_to_cpu(counters->rx_missed);
  977. data[5] = le16_to_cpu(counters->align_errors);
  978. data[6] = le32_to_cpu(counters->tx_one_collision);
  979. data[7] = le32_to_cpu(counters->tx_multi_collision);
  980. data[8] = le64_to_cpu(counters->rx_unicast);
  981. data[9] = le64_to_cpu(counters->rx_broadcast);
  982. data[10] = le32_to_cpu(counters->rx_multicast);
  983. data[11] = le16_to_cpu(counters->tx_aborted);
  984. data[12] = le16_to_cpu(counters->tx_underun);
  985. pci_free_consistent(tp->pci_dev, sizeof(*counters), counters, paddr);
  986. }
  987. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  988. {
  989. switch(stringset) {
  990. case ETH_SS_STATS:
  991. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  992. break;
  993. }
  994. }
  995. static const struct ethtool_ops rtl8169_ethtool_ops = {
  996. .get_drvinfo = rtl8169_get_drvinfo,
  997. .get_regs_len = rtl8169_get_regs_len,
  998. .get_link = ethtool_op_get_link,
  999. .get_settings = rtl8169_get_settings,
  1000. .set_settings = rtl8169_set_settings,
  1001. .get_msglevel = rtl8169_get_msglevel,
  1002. .set_msglevel = rtl8169_set_msglevel,
  1003. .get_rx_csum = rtl8169_get_rx_csum,
  1004. .set_rx_csum = rtl8169_set_rx_csum,
  1005. .set_tx_csum = ethtool_op_set_tx_csum,
  1006. .set_sg = ethtool_op_set_sg,
  1007. .set_tso = ethtool_op_set_tso,
  1008. .get_regs = rtl8169_get_regs,
  1009. .get_wol = rtl8169_get_wol,
  1010. .set_wol = rtl8169_set_wol,
  1011. .get_strings = rtl8169_get_strings,
  1012. .get_sset_count = rtl8169_get_sset_count,
  1013. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1014. };
  1015. static void rtl8169_write_gmii_reg_bit(void __iomem *ioaddr, int reg,
  1016. int bitnum, int bitval)
  1017. {
  1018. int val;
  1019. val = mdio_read(ioaddr, reg);
  1020. val = (bitval == 1) ?
  1021. val | (bitval << bitnum) : val & ~(0x0001 << bitnum);
  1022. mdio_write(ioaddr, reg, val & 0xffff);
  1023. }
  1024. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1025. void __iomem *ioaddr)
  1026. {
  1027. /*
  1028. * The driver currently handles the 8168Bf and the 8168Be identically
  1029. * but they can be identified more specifically through the test below
  1030. * if needed:
  1031. *
  1032. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1033. *
  1034. * Same thing for the 8101Eb and the 8101Ec:
  1035. *
  1036. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1037. */
  1038. const struct {
  1039. u32 mask;
  1040. u32 val;
  1041. int mac_version;
  1042. } mac_info[] = {
  1043. /* 8168D family. */
  1044. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_25 },
  1045. /* 8168C family. */
  1046. { 0x7cf00000, 0x3ca00000, RTL_GIGA_MAC_VER_24 },
  1047. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1048. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1049. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1050. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1051. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1052. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1053. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1054. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1055. /* 8168B family. */
  1056. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1057. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1058. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1059. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1060. /* 8101 family. */
  1061. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1062. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1063. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1064. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1065. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1066. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1067. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1068. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1069. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1070. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1071. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1072. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1073. /* FIXME: where did these entries come from ? -- FR */
  1074. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1075. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1076. /* 8110 family. */
  1077. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1078. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1079. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1080. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1081. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1082. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1083. { 0x00000000, 0x00000000, RTL_GIGA_MAC_VER_01 } /* Catch-all */
  1084. }, *p = mac_info;
  1085. u32 reg;
  1086. reg = RTL_R32(TxConfig);
  1087. while ((reg & p->mask) != p->val)
  1088. p++;
  1089. tp->mac_version = p->mac_version;
  1090. if (p->mask == 0x00000000) {
  1091. struct pci_dev *pdev = tp->pci_dev;
  1092. dev_info(&pdev->dev, "unknown MAC (%08x)\n", reg);
  1093. }
  1094. }
  1095. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1096. {
  1097. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1098. }
  1099. struct phy_reg {
  1100. u16 reg;
  1101. u16 val;
  1102. };
  1103. static void rtl_phy_write(void __iomem *ioaddr, struct phy_reg *regs, int len)
  1104. {
  1105. while (len-- > 0) {
  1106. mdio_write(ioaddr, regs->reg, regs->val);
  1107. regs++;
  1108. }
  1109. }
  1110. static void rtl8169s_hw_phy_config(void __iomem *ioaddr)
  1111. {
  1112. struct {
  1113. u16 regs[5]; /* Beware of bit-sign propagation */
  1114. } phy_magic[5] = { {
  1115. { 0x0000, //w 4 15 12 0
  1116. 0x00a1, //w 3 15 0 00a1
  1117. 0x0008, //w 2 15 0 0008
  1118. 0x1020, //w 1 15 0 1020
  1119. 0x1000 } },{ //w 0 15 0 1000
  1120. { 0x7000, //w 4 15 12 7
  1121. 0xff41, //w 3 15 0 ff41
  1122. 0xde60, //w 2 15 0 de60
  1123. 0x0140, //w 1 15 0 0140
  1124. 0x0077 } },{ //w 0 15 0 0077
  1125. { 0xa000, //w 4 15 12 a
  1126. 0xdf01, //w 3 15 0 df01
  1127. 0xdf20, //w 2 15 0 df20
  1128. 0xff95, //w 1 15 0 ff95
  1129. 0xfa00 } },{ //w 0 15 0 fa00
  1130. { 0xb000, //w 4 15 12 b
  1131. 0xff41, //w 3 15 0 ff41
  1132. 0xde20, //w 2 15 0 de20
  1133. 0x0140, //w 1 15 0 0140
  1134. 0x00bb } },{ //w 0 15 0 00bb
  1135. { 0xf000, //w 4 15 12 f
  1136. 0xdf01, //w 3 15 0 df01
  1137. 0xdf20, //w 2 15 0 df20
  1138. 0xff95, //w 1 15 0 ff95
  1139. 0xbf00 } //w 0 15 0 bf00
  1140. }
  1141. }, *p = phy_magic;
  1142. unsigned int i;
  1143. mdio_write(ioaddr, 0x1f, 0x0001); //w 31 2 0 1
  1144. mdio_write(ioaddr, 0x15, 0x1000); //w 21 15 0 1000
  1145. mdio_write(ioaddr, 0x18, 0x65c7); //w 24 15 0 65c7
  1146. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1147. for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
  1148. int val, pos = 4;
  1149. val = (mdio_read(ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
  1150. mdio_write(ioaddr, pos, val);
  1151. while (--pos >= 0)
  1152. mdio_write(ioaddr, pos, p->regs[4 - pos] & 0xffff);
  1153. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 1); //w 4 11 11 1
  1154. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1155. }
  1156. mdio_write(ioaddr, 0x1f, 0x0000); //w 31 2 0 0
  1157. }
  1158. static void rtl8169sb_hw_phy_config(void __iomem *ioaddr)
  1159. {
  1160. struct phy_reg phy_reg_init[] = {
  1161. { 0x1f, 0x0002 },
  1162. { 0x01, 0x90d0 },
  1163. { 0x1f, 0x0000 }
  1164. };
  1165. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1166. }
  1167. static void rtl8168bb_hw_phy_config(void __iomem *ioaddr)
  1168. {
  1169. struct phy_reg phy_reg_init[] = {
  1170. { 0x10, 0xf41b },
  1171. { 0x1f, 0x0000 }
  1172. };
  1173. mdio_write(ioaddr, 0x1f, 0x0001);
  1174. mdio_patch(ioaddr, 0x16, 1 << 0);
  1175. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1176. }
  1177. static void rtl8168bef_hw_phy_config(void __iomem *ioaddr)
  1178. {
  1179. struct phy_reg phy_reg_init[] = {
  1180. { 0x1f, 0x0001 },
  1181. { 0x10, 0xf41b },
  1182. { 0x1f, 0x0000 }
  1183. };
  1184. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1185. }
  1186. static void rtl8168cp_1_hw_phy_config(void __iomem *ioaddr)
  1187. {
  1188. struct phy_reg phy_reg_init[] = {
  1189. { 0x1f, 0x0000 },
  1190. { 0x1d, 0x0f00 },
  1191. { 0x1f, 0x0002 },
  1192. { 0x0c, 0x1ec8 },
  1193. { 0x1f, 0x0000 }
  1194. };
  1195. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1196. }
  1197. static void rtl8168cp_2_hw_phy_config(void __iomem *ioaddr)
  1198. {
  1199. struct phy_reg phy_reg_init[] = {
  1200. { 0x1f, 0x0001 },
  1201. { 0x1d, 0x3d98 },
  1202. { 0x1f, 0x0000 }
  1203. };
  1204. mdio_write(ioaddr, 0x1f, 0x0000);
  1205. mdio_patch(ioaddr, 0x14, 1 << 5);
  1206. mdio_patch(ioaddr, 0x0d, 1 << 5);
  1207. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1208. }
  1209. static void rtl8168c_1_hw_phy_config(void __iomem *ioaddr)
  1210. {
  1211. struct phy_reg phy_reg_init[] = {
  1212. { 0x1f, 0x0001 },
  1213. { 0x12, 0x2300 },
  1214. { 0x1f, 0x0002 },
  1215. { 0x00, 0x88d4 },
  1216. { 0x01, 0x82b1 },
  1217. { 0x03, 0x7002 },
  1218. { 0x08, 0x9e30 },
  1219. { 0x09, 0x01f0 },
  1220. { 0x0a, 0x5500 },
  1221. { 0x0c, 0x00c8 },
  1222. { 0x1f, 0x0003 },
  1223. { 0x12, 0xc096 },
  1224. { 0x16, 0x000a },
  1225. { 0x1f, 0x0000 },
  1226. { 0x1f, 0x0000 },
  1227. { 0x09, 0x2000 },
  1228. { 0x09, 0x0000 }
  1229. };
  1230. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1231. mdio_patch(ioaddr, 0x14, 1 << 5);
  1232. mdio_patch(ioaddr, 0x0d, 1 << 5);
  1233. mdio_write(ioaddr, 0x1f, 0x0000);
  1234. }
  1235. static void rtl8168c_2_hw_phy_config(void __iomem *ioaddr)
  1236. {
  1237. struct phy_reg phy_reg_init[] = {
  1238. { 0x1f, 0x0001 },
  1239. { 0x12, 0x2300 },
  1240. { 0x03, 0x802f },
  1241. { 0x02, 0x4f02 },
  1242. { 0x01, 0x0409 },
  1243. { 0x00, 0xf099 },
  1244. { 0x04, 0x9800 },
  1245. { 0x04, 0x9000 },
  1246. { 0x1d, 0x3d98 },
  1247. { 0x1f, 0x0002 },
  1248. { 0x0c, 0x7eb8 },
  1249. { 0x06, 0x0761 },
  1250. { 0x1f, 0x0003 },
  1251. { 0x16, 0x0f0a },
  1252. { 0x1f, 0x0000 }
  1253. };
  1254. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1255. mdio_patch(ioaddr, 0x16, 1 << 0);
  1256. mdio_patch(ioaddr, 0x14, 1 << 5);
  1257. mdio_patch(ioaddr, 0x0d, 1 << 5);
  1258. mdio_write(ioaddr, 0x1f, 0x0000);
  1259. }
  1260. static void rtl8168c_3_hw_phy_config(void __iomem *ioaddr)
  1261. {
  1262. struct phy_reg phy_reg_init[] = {
  1263. { 0x1f, 0x0001 },
  1264. { 0x12, 0x2300 },
  1265. { 0x1d, 0x3d98 },
  1266. { 0x1f, 0x0002 },
  1267. { 0x0c, 0x7eb8 },
  1268. { 0x06, 0x5461 },
  1269. { 0x1f, 0x0003 },
  1270. { 0x16, 0x0f0a },
  1271. { 0x1f, 0x0000 }
  1272. };
  1273. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1274. mdio_patch(ioaddr, 0x16, 1 << 0);
  1275. mdio_patch(ioaddr, 0x14, 1 << 5);
  1276. mdio_patch(ioaddr, 0x0d, 1 << 5);
  1277. mdio_write(ioaddr, 0x1f, 0x0000);
  1278. }
  1279. static void rtl8168c_4_hw_phy_config(void __iomem *ioaddr)
  1280. {
  1281. rtl8168c_3_hw_phy_config(ioaddr);
  1282. }
  1283. static void rtl8168d_hw_phy_config(void __iomem *ioaddr)
  1284. {
  1285. struct phy_reg phy_reg_init_0[] = {
  1286. { 0x1f, 0x0001 },
  1287. { 0x09, 0x2770 },
  1288. { 0x08, 0x04d0 },
  1289. { 0x0b, 0xad15 },
  1290. { 0x0c, 0x5bf0 },
  1291. { 0x1c, 0xf101 },
  1292. { 0x1f, 0x0003 },
  1293. { 0x14, 0x94d7 },
  1294. { 0x12, 0xf4d6 },
  1295. { 0x09, 0xca0f },
  1296. { 0x1f, 0x0002 },
  1297. { 0x0b, 0x0b10 },
  1298. { 0x0c, 0xd1f7 },
  1299. { 0x1f, 0x0002 },
  1300. { 0x06, 0x5461 },
  1301. { 0x1f, 0x0002 },
  1302. { 0x05, 0x6662 },
  1303. { 0x1f, 0x0000 },
  1304. { 0x14, 0x0060 },
  1305. { 0x1f, 0x0000 },
  1306. { 0x0d, 0xf8a0 },
  1307. { 0x1f, 0x0005 },
  1308. { 0x05, 0xffc2 }
  1309. };
  1310. rtl_phy_write(ioaddr, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1311. if (mdio_read(ioaddr, 0x06) == 0xc400) {
  1312. struct phy_reg phy_reg_init_1[] = {
  1313. { 0x1f, 0x0005 },
  1314. { 0x01, 0x0300 },
  1315. { 0x1f, 0x0000 },
  1316. { 0x11, 0x401c },
  1317. { 0x16, 0x4100 },
  1318. { 0x1f, 0x0005 },
  1319. { 0x07, 0x0010 },
  1320. { 0x05, 0x83dc },
  1321. { 0x06, 0x087d },
  1322. { 0x05, 0x8300 },
  1323. { 0x06, 0x0101 },
  1324. { 0x06, 0x05f8 },
  1325. { 0x06, 0xf9fa },
  1326. { 0x06, 0xfbef },
  1327. { 0x06, 0x79e2 },
  1328. { 0x06, 0x835f },
  1329. { 0x06, 0xe0f8 },
  1330. { 0x06, 0x9ae1 },
  1331. { 0x06, 0xf89b },
  1332. { 0x06, 0xef31 },
  1333. { 0x06, 0x3b65 },
  1334. { 0x06, 0xaa07 },
  1335. { 0x06, 0x81e4 },
  1336. { 0x06, 0xf89a },
  1337. { 0x06, 0xe5f8 },
  1338. { 0x06, 0x9baf },
  1339. { 0x06, 0x06ae },
  1340. { 0x05, 0x83dc },
  1341. { 0x06, 0x8300 },
  1342. };
  1343. rtl_phy_write(ioaddr, phy_reg_init_1,
  1344. ARRAY_SIZE(phy_reg_init_1));
  1345. }
  1346. mdio_write(ioaddr, 0x1f, 0x0000);
  1347. }
  1348. static void rtl8102e_hw_phy_config(void __iomem *ioaddr)
  1349. {
  1350. struct phy_reg phy_reg_init[] = {
  1351. { 0x1f, 0x0003 },
  1352. { 0x08, 0x441d },
  1353. { 0x01, 0x9100 },
  1354. { 0x1f, 0x0000 }
  1355. };
  1356. mdio_write(ioaddr, 0x1f, 0x0000);
  1357. mdio_patch(ioaddr, 0x11, 1 << 12);
  1358. mdio_patch(ioaddr, 0x19, 1 << 13);
  1359. rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1360. }
  1361. static void rtl_hw_phy_config(struct net_device *dev)
  1362. {
  1363. struct rtl8169_private *tp = netdev_priv(dev);
  1364. void __iomem *ioaddr = tp->mmio_addr;
  1365. rtl8169_print_mac_version(tp);
  1366. switch (tp->mac_version) {
  1367. case RTL_GIGA_MAC_VER_01:
  1368. break;
  1369. case RTL_GIGA_MAC_VER_02:
  1370. case RTL_GIGA_MAC_VER_03:
  1371. rtl8169s_hw_phy_config(ioaddr);
  1372. break;
  1373. case RTL_GIGA_MAC_VER_04:
  1374. rtl8169sb_hw_phy_config(ioaddr);
  1375. break;
  1376. case RTL_GIGA_MAC_VER_07:
  1377. case RTL_GIGA_MAC_VER_08:
  1378. case RTL_GIGA_MAC_VER_09:
  1379. rtl8102e_hw_phy_config(ioaddr);
  1380. break;
  1381. case RTL_GIGA_MAC_VER_11:
  1382. rtl8168bb_hw_phy_config(ioaddr);
  1383. break;
  1384. case RTL_GIGA_MAC_VER_12:
  1385. rtl8168bef_hw_phy_config(ioaddr);
  1386. break;
  1387. case RTL_GIGA_MAC_VER_17:
  1388. rtl8168bef_hw_phy_config(ioaddr);
  1389. break;
  1390. case RTL_GIGA_MAC_VER_18:
  1391. rtl8168cp_1_hw_phy_config(ioaddr);
  1392. break;
  1393. case RTL_GIGA_MAC_VER_19:
  1394. rtl8168c_1_hw_phy_config(ioaddr);
  1395. break;
  1396. case RTL_GIGA_MAC_VER_20:
  1397. rtl8168c_2_hw_phy_config(ioaddr);
  1398. break;
  1399. case RTL_GIGA_MAC_VER_21:
  1400. rtl8168c_3_hw_phy_config(ioaddr);
  1401. break;
  1402. case RTL_GIGA_MAC_VER_22:
  1403. rtl8168c_4_hw_phy_config(ioaddr);
  1404. break;
  1405. case RTL_GIGA_MAC_VER_23:
  1406. case RTL_GIGA_MAC_VER_24:
  1407. rtl8168cp_2_hw_phy_config(ioaddr);
  1408. break;
  1409. case RTL_GIGA_MAC_VER_25:
  1410. rtl8168d_hw_phy_config(ioaddr);
  1411. break;
  1412. default:
  1413. break;
  1414. }
  1415. }
  1416. static void rtl8169_phy_timer(unsigned long __opaque)
  1417. {
  1418. struct net_device *dev = (struct net_device *)__opaque;
  1419. struct rtl8169_private *tp = netdev_priv(dev);
  1420. struct timer_list *timer = &tp->timer;
  1421. void __iomem *ioaddr = tp->mmio_addr;
  1422. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  1423. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  1424. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1425. return;
  1426. spin_lock_irq(&tp->lock);
  1427. if (tp->phy_reset_pending(ioaddr)) {
  1428. /*
  1429. * A busy loop could burn quite a few cycles on nowadays CPU.
  1430. * Let's delay the execution of the timer for a few ticks.
  1431. */
  1432. timeout = HZ/10;
  1433. goto out_mod_timer;
  1434. }
  1435. if (tp->link_ok(ioaddr))
  1436. goto out_unlock;
  1437. if (netif_msg_link(tp))
  1438. printk(KERN_WARNING "%s: PHY reset until link up\n", dev->name);
  1439. tp->phy_reset_enable(ioaddr);
  1440. out_mod_timer:
  1441. mod_timer(timer, jiffies + timeout);
  1442. out_unlock:
  1443. spin_unlock_irq(&tp->lock);
  1444. }
  1445. static inline void rtl8169_delete_timer(struct net_device *dev)
  1446. {
  1447. struct rtl8169_private *tp = netdev_priv(dev);
  1448. struct timer_list *timer = &tp->timer;
  1449. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1450. return;
  1451. del_timer_sync(timer);
  1452. }
  1453. static inline void rtl8169_request_timer(struct net_device *dev)
  1454. {
  1455. struct rtl8169_private *tp = netdev_priv(dev);
  1456. struct timer_list *timer = &tp->timer;
  1457. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1458. return;
  1459. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  1460. }
  1461. #ifdef CONFIG_NET_POLL_CONTROLLER
  1462. /*
  1463. * Polling 'interrupt' - used by things like netconsole to send skbs
  1464. * without having to re-enable interrupts. It's not called while
  1465. * the interrupt routine is executing.
  1466. */
  1467. static void rtl8169_netpoll(struct net_device *dev)
  1468. {
  1469. struct rtl8169_private *tp = netdev_priv(dev);
  1470. struct pci_dev *pdev = tp->pci_dev;
  1471. disable_irq(pdev->irq);
  1472. rtl8169_interrupt(pdev->irq, dev);
  1473. enable_irq(pdev->irq);
  1474. }
  1475. #endif
  1476. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  1477. void __iomem *ioaddr)
  1478. {
  1479. iounmap(ioaddr);
  1480. pci_release_regions(pdev);
  1481. pci_disable_device(pdev);
  1482. free_netdev(dev);
  1483. }
  1484. static void rtl8169_phy_reset(struct net_device *dev,
  1485. struct rtl8169_private *tp)
  1486. {
  1487. void __iomem *ioaddr = tp->mmio_addr;
  1488. unsigned int i;
  1489. tp->phy_reset_enable(ioaddr);
  1490. for (i = 0; i < 100; i++) {
  1491. if (!tp->phy_reset_pending(ioaddr))
  1492. return;
  1493. msleep(1);
  1494. }
  1495. if (netif_msg_link(tp))
  1496. printk(KERN_ERR "%s: PHY reset failed.\n", dev->name);
  1497. }
  1498. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  1499. {
  1500. void __iomem *ioaddr = tp->mmio_addr;
  1501. rtl_hw_phy_config(dev);
  1502. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  1503. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1504. RTL_W8(0x82, 0x01);
  1505. }
  1506. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  1507. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  1508. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  1509. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  1510. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1511. RTL_W8(0x82, 0x01);
  1512. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  1513. mdio_write(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
  1514. }
  1515. rtl8169_phy_reset(dev, tp);
  1516. /*
  1517. * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
  1518. * only 8101. Don't panic.
  1519. */
  1520. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
  1521. if ((RTL_R8(PHYstatus) & TBI_Enable) && netif_msg_link(tp))
  1522. printk(KERN_INFO PFX "%s: TBI auto-negotiating\n", dev->name);
  1523. }
  1524. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  1525. {
  1526. void __iomem *ioaddr = tp->mmio_addr;
  1527. u32 high;
  1528. u32 low;
  1529. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  1530. high = addr[4] | (addr[5] << 8);
  1531. spin_lock_irq(&tp->lock);
  1532. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1533. RTL_W32(MAC0, low);
  1534. RTL_W32(MAC4, high);
  1535. RTL_W8(Cfg9346, Cfg9346_Lock);
  1536. spin_unlock_irq(&tp->lock);
  1537. }
  1538. static int rtl_set_mac_address(struct net_device *dev, void *p)
  1539. {
  1540. struct rtl8169_private *tp = netdev_priv(dev);
  1541. struct sockaddr *addr = p;
  1542. if (!is_valid_ether_addr(addr->sa_data))
  1543. return -EADDRNOTAVAIL;
  1544. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1545. rtl_rar_set(tp, dev->dev_addr);
  1546. return 0;
  1547. }
  1548. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1549. {
  1550. struct rtl8169_private *tp = netdev_priv(dev);
  1551. struct mii_ioctl_data *data = if_mii(ifr);
  1552. if (!netif_running(dev))
  1553. return -ENODEV;
  1554. switch (cmd) {
  1555. case SIOCGMIIPHY:
  1556. data->phy_id = 32; /* Internal PHY */
  1557. return 0;
  1558. case SIOCGMIIREG:
  1559. data->val_out = mdio_read(tp->mmio_addr, data->reg_num & 0x1f);
  1560. return 0;
  1561. case SIOCSMIIREG:
  1562. if (!capable(CAP_NET_ADMIN))
  1563. return -EPERM;
  1564. mdio_write(tp->mmio_addr, data->reg_num & 0x1f, data->val_in);
  1565. return 0;
  1566. }
  1567. return -EOPNOTSUPP;
  1568. }
  1569. static const struct rtl_cfg_info {
  1570. void (*hw_start)(struct net_device *);
  1571. unsigned int region;
  1572. unsigned int align;
  1573. u16 intr_event;
  1574. u16 napi_event;
  1575. unsigned features;
  1576. } rtl_cfg_infos [] = {
  1577. [RTL_CFG_0] = {
  1578. .hw_start = rtl_hw_start_8169,
  1579. .region = 1,
  1580. .align = 0,
  1581. .intr_event = SYSErr | LinkChg | RxOverflow |
  1582. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  1583. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  1584. .features = RTL_FEATURE_GMII
  1585. },
  1586. [RTL_CFG_1] = {
  1587. .hw_start = rtl_hw_start_8168,
  1588. .region = 2,
  1589. .align = 8,
  1590. .intr_event = SYSErr | LinkChg | RxOverflow |
  1591. TxErr | TxOK | RxOK | RxErr,
  1592. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  1593. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI
  1594. },
  1595. [RTL_CFG_2] = {
  1596. .hw_start = rtl_hw_start_8101,
  1597. .region = 2,
  1598. .align = 8,
  1599. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  1600. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  1601. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  1602. .features = RTL_FEATURE_MSI
  1603. }
  1604. };
  1605. /* Cfg9346_Unlock assumed. */
  1606. static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
  1607. const struct rtl_cfg_info *cfg)
  1608. {
  1609. unsigned msi = 0;
  1610. u8 cfg2;
  1611. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  1612. if (cfg->features & RTL_FEATURE_MSI) {
  1613. if (pci_enable_msi(pdev)) {
  1614. dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
  1615. } else {
  1616. cfg2 |= MSIEnable;
  1617. msi = RTL_FEATURE_MSI;
  1618. }
  1619. }
  1620. RTL_W8(Config2, cfg2);
  1621. return msi;
  1622. }
  1623. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  1624. {
  1625. if (tp->features & RTL_FEATURE_MSI) {
  1626. pci_disable_msi(pdev);
  1627. tp->features &= ~RTL_FEATURE_MSI;
  1628. }
  1629. }
  1630. static int rtl_eeprom_read(struct pci_dev *pdev, int cap, int addr, __le32 *val)
  1631. {
  1632. int ret, count = 100;
  1633. u16 status = 0;
  1634. u32 value;
  1635. ret = pci_write_config_word(pdev, cap + PCI_VPD_ADDR, addr);
  1636. if (ret < 0)
  1637. return ret;
  1638. do {
  1639. udelay(10);
  1640. ret = pci_read_config_word(pdev, cap + PCI_VPD_ADDR, &status);
  1641. if (ret < 0)
  1642. return ret;
  1643. } while (!(status & PCI_VPD_ADDR_F) && --count);
  1644. if (!(status & PCI_VPD_ADDR_F))
  1645. return -ETIMEDOUT;
  1646. ret = pci_read_config_dword(pdev, cap + PCI_VPD_DATA, &value);
  1647. if (ret < 0)
  1648. return ret;
  1649. *val = cpu_to_le32(value);
  1650. return 0;
  1651. }
  1652. static void rtl_init_mac_address(struct rtl8169_private *tp,
  1653. void __iomem *ioaddr)
  1654. {
  1655. struct pci_dev *pdev = tp->pci_dev;
  1656. u8 cfg1;
  1657. int vpd_cap;
  1658. u8 mac[8];
  1659. DECLARE_MAC_BUF(buf);
  1660. cfg1 = RTL_R8(Config1);
  1661. if (!(cfg1 & VPD)) {
  1662. dprintk("VPD access not enabled, enabling\n");
  1663. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1664. RTL_W8(Config1, cfg1 | VPD);
  1665. RTL_W8(Cfg9346, Cfg9346_Lock);
  1666. }
  1667. vpd_cap = pci_find_capability(pdev, PCI_CAP_ID_VPD);
  1668. if (!vpd_cap)
  1669. return;
  1670. /* MAC address is stored in EEPROM at offset 0x0e
  1671. * Realtek says: "The VPD address does not have to be a DWORD-aligned
  1672. * address as defined in the PCI 2.2 Specifications, but the VPD data
  1673. * is always consecutive 4-byte data starting from the VPD address
  1674. * specified."
  1675. */
  1676. if (rtl_eeprom_read(pdev, vpd_cap, 0x000e, (__le32*)&mac[0]) < 0 ||
  1677. rtl_eeprom_read(pdev, vpd_cap, 0x0012, (__le32*)&mac[4]) < 0) {
  1678. dprintk("Reading MAC address from EEPROM failed\n");
  1679. return;
  1680. }
  1681. dprintk("MAC address found in EEPROM: %s\n", print_mac(buf, mac));
  1682. /* Write MAC address */
  1683. rtl_rar_set(tp, mac);
  1684. }
  1685. static int __devinit
  1686. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1687. {
  1688. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  1689. const unsigned int region = cfg->region;
  1690. struct rtl8169_private *tp;
  1691. struct mii_if_info *mii;
  1692. struct net_device *dev;
  1693. void __iomem *ioaddr;
  1694. unsigned int i;
  1695. int rc;
  1696. if (netif_msg_drv(&debug)) {
  1697. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  1698. MODULENAME, RTL8169_VERSION);
  1699. }
  1700. dev = alloc_etherdev(sizeof (*tp));
  1701. if (!dev) {
  1702. if (netif_msg_drv(&debug))
  1703. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  1704. rc = -ENOMEM;
  1705. goto out;
  1706. }
  1707. SET_NETDEV_DEV(dev, &pdev->dev);
  1708. tp = netdev_priv(dev);
  1709. tp->dev = dev;
  1710. tp->pci_dev = pdev;
  1711. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  1712. mii = &tp->mii;
  1713. mii->dev = dev;
  1714. mii->mdio_read = rtl_mdio_read;
  1715. mii->mdio_write = rtl_mdio_write;
  1716. mii->phy_id_mask = 0x1f;
  1717. mii->reg_num_mask = 0x1f;
  1718. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  1719. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  1720. rc = pci_enable_device(pdev);
  1721. if (rc < 0) {
  1722. if (netif_msg_probe(tp))
  1723. dev_err(&pdev->dev, "enable failure\n");
  1724. goto err_out_free_dev_1;
  1725. }
  1726. rc = pci_set_mwi(pdev);
  1727. if (rc < 0)
  1728. goto err_out_disable_2;
  1729. /* make sure PCI base addr 1 is MMIO */
  1730. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  1731. if (netif_msg_probe(tp)) {
  1732. dev_err(&pdev->dev,
  1733. "region #%d not an MMIO resource, aborting\n",
  1734. region);
  1735. }
  1736. rc = -ENODEV;
  1737. goto err_out_mwi_3;
  1738. }
  1739. /* check for weird/broken PCI region reporting */
  1740. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  1741. if (netif_msg_probe(tp)) {
  1742. dev_err(&pdev->dev,
  1743. "Invalid PCI region size(s), aborting\n");
  1744. }
  1745. rc = -ENODEV;
  1746. goto err_out_mwi_3;
  1747. }
  1748. rc = pci_request_regions(pdev, MODULENAME);
  1749. if (rc < 0) {
  1750. if (netif_msg_probe(tp))
  1751. dev_err(&pdev->dev, "could not request regions.\n");
  1752. goto err_out_mwi_3;
  1753. }
  1754. tp->cp_cmd = PCIMulRW | RxChkSum;
  1755. if ((sizeof(dma_addr_t) > 4) &&
  1756. !pci_set_dma_mask(pdev, DMA_64BIT_MASK) && use_dac) {
  1757. tp->cp_cmd |= PCIDAC;
  1758. dev->features |= NETIF_F_HIGHDMA;
  1759. } else {
  1760. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1761. if (rc < 0) {
  1762. if (netif_msg_probe(tp)) {
  1763. dev_err(&pdev->dev,
  1764. "DMA configuration failed.\n");
  1765. }
  1766. goto err_out_free_res_4;
  1767. }
  1768. }
  1769. pci_set_master(pdev);
  1770. /* ioremap MMIO region */
  1771. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  1772. if (!ioaddr) {
  1773. if (netif_msg_probe(tp))
  1774. dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
  1775. rc = -EIO;
  1776. goto err_out_free_res_4;
  1777. }
  1778. tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  1779. if (!tp->pcie_cap && netif_msg_probe(tp))
  1780. dev_info(&pdev->dev, "no PCI Express capability\n");
  1781. /* Unneeded ? Don't mess with Mrs. Murphy. */
  1782. rtl8169_irq_mask_and_ack(ioaddr);
  1783. /* Soft reset the chip. */
  1784. RTL_W8(ChipCmd, CmdReset);
  1785. /* Check that the chip has finished the reset. */
  1786. for (i = 0; i < 100; i++) {
  1787. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1788. break;
  1789. msleep_interruptible(1);
  1790. }
  1791. /* Identify chip attached to board */
  1792. rtl8169_get_mac_version(tp, ioaddr);
  1793. rtl8169_print_mac_version(tp);
  1794. for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
  1795. if (tp->mac_version == rtl_chip_info[i].mac_version)
  1796. break;
  1797. }
  1798. if (i == ARRAY_SIZE(rtl_chip_info)) {
  1799. /* Unknown chip: assume array element #0, original RTL-8169 */
  1800. if (netif_msg_probe(tp)) {
  1801. dev_printk(KERN_DEBUG, &pdev->dev,
  1802. "unknown chip version, assuming %s\n",
  1803. rtl_chip_info[0].name);
  1804. }
  1805. i = 0;
  1806. }
  1807. tp->chipset = i;
  1808. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1809. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  1810. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  1811. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  1812. tp->features |= RTL_FEATURE_WOL;
  1813. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  1814. tp->features |= RTL_FEATURE_WOL;
  1815. tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
  1816. RTL_W8(Cfg9346, Cfg9346_Lock);
  1817. if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
  1818. (RTL_R8(PHYstatus) & TBI_Enable)) {
  1819. tp->set_speed = rtl8169_set_speed_tbi;
  1820. tp->get_settings = rtl8169_gset_tbi;
  1821. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  1822. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  1823. tp->link_ok = rtl8169_tbi_link_ok;
  1824. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  1825. } else {
  1826. tp->set_speed = rtl8169_set_speed_xmii;
  1827. tp->get_settings = rtl8169_gset_xmii;
  1828. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  1829. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  1830. tp->link_ok = rtl8169_xmii_link_ok;
  1831. dev->do_ioctl = rtl8169_ioctl;
  1832. }
  1833. spin_lock_init(&tp->lock);
  1834. rtl_init_mac_address(tp, ioaddr);
  1835. /* Get MAC address */
  1836. for (i = 0; i < MAC_ADDR_LEN; i++)
  1837. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  1838. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1839. dev->open = rtl8169_open;
  1840. dev->hard_start_xmit = rtl8169_start_xmit;
  1841. dev->get_stats = rtl8169_get_stats;
  1842. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  1843. dev->stop = rtl8169_close;
  1844. dev->tx_timeout = rtl8169_tx_timeout;
  1845. dev->set_multicast_list = rtl_set_rx_mode;
  1846. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  1847. dev->irq = pdev->irq;
  1848. dev->base_addr = (unsigned long) ioaddr;
  1849. dev->change_mtu = rtl8169_change_mtu;
  1850. dev->set_mac_address = rtl_set_mac_address;
  1851. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  1852. #ifdef CONFIG_R8169_VLAN
  1853. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1854. dev->vlan_rx_register = rtl8169_vlan_rx_register;
  1855. #endif
  1856. #ifdef CONFIG_NET_POLL_CONTROLLER
  1857. dev->poll_controller = rtl8169_netpoll;
  1858. #endif
  1859. tp->intr_mask = 0xffff;
  1860. tp->mmio_addr = ioaddr;
  1861. tp->align = cfg->align;
  1862. tp->hw_start = cfg->hw_start;
  1863. tp->intr_event = cfg->intr_event;
  1864. tp->napi_event = cfg->napi_event;
  1865. init_timer(&tp->timer);
  1866. tp->timer.data = (unsigned long) dev;
  1867. tp->timer.function = rtl8169_phy_timer;
  1868. rc = register_netdev(dev);
  1869. if (rc < 0)
  1870. goto err_out_msi_5;
  1871. pci_set_drvdata(pdev, dev);
  1872. if (netif_msg_probe(tp)) {
  1873. u32 xid = RTL_R32(TxConfig) & 0x7cf0f8ff;
  1874. printk(KERN_INFO "%s: %s at 0x%lx, "
  1875. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
  1876. "XID %08x IRQ %d\n",
  1877. dev->name,
  1878. rtl_chip_info[tp->chipset].name,
  1879. dev->base_addr,
  1880. dev->dev_addr[0], dev->dev_addr[1],
  1881. dev->dev_addr[2], dev->dev_addr[3],
  1882. dev->dev_addr[4], dev->dev_addr[5], xid, dev->irq);
  1883. }
  1884. rtl8169_init_phy(dev, tp);
  1885. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  1886. out:
  1887. return rc;
  1888. err_out_msi_5:
  1889. rtl_disable_msi(pdev, tp);
  1890. iounmap(ioaddr);
  1891. err_out_free_res_4:
  1892. pci_release_regions(pdev);
  1893. err_out_mwi_3:
  1894. pci_clear_mwi(pdev);
  1895. err_out_disable_2:
  1896. pci_disable_device(pdev);
  1897. err_out_free_dev_1:
  1898. free_netdev(dev);
  1899. goto out;
  1900. }
  1901. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  1902. {
  1903. struct net_device *dev = pci_get_drvdata(pdev);
  1904. struct rtl8169_private *tp = netdev_priv(dev);
  1905. flush_scheduled_work();
  1906. unregister_netdev(dev);
  1907. rtl_disable_msi(pdev, tp);
  1908. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  1909. pci_set_drvdata(pdev, NULL);
  1910. }
  1911. static void rtl8169_set_rxbufsize(struct rtl8169_private *tp,
  1912. struct net_device *dev)
  1913. {
  1914. unsigned int mtu = dev->mtu;
  1915. tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
  1916. }
  1917. static int rtl8169_open(struct net_device *dev)
  1918. {
  1919. struct rtl8169_private *tp = netdev_priv(dev);
  1920. struct pci_dev *pdev = tp->pci_dev;
  1921. int retval = -ENOMEM;
  1922. rtl8169_set_rxbufsize(tp, dev);
  1923. /*
  1924. * Rx and Tx desscriptors needs 256 bytes alignment.
  1925. * pci_alloc_consistent provides more.
  1926. */
  1927. tp->TxDescArray = pci_alloc_consistent(pdev, R8169_TX_RING_BYTES,
  1928. &tp->TxPhyAddr);
  1929. if (!tp->TxDescArray)
  1930. goto out;
  1931. tp->RxDescArray = pci_alloc_consistent(pdev, R8169_RX_RING_BYTES,
  1932. &tp->RxPhyAddr);
  1933. if (!tp->RxDescArray)
  1934. goto err_free_tx_0;
  1935. retval = rtl8169_init_ring(dev);
  1936. if (retval < 0)
  1937. goto err_free_rx_1;
  1938. INIT_DELAYED_WORK(&tp->task, NULL);
  1939. smp_mb();
  1940. retval = request_irq(dev->irq, rtl8169_interrupt,
  1941. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  1942. dev->name, dev);
  1943. if (retval < 0)
  1944. goto err_release_ring_2;
  1945. napi_enable(&tp->napi);
  1946. rtl_hw_start(dev);
  1947. rtl8169_request_timer(dev);
  1948. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  1949. out:
  1950. return retval;
  1951. err_release_ring_2:
  1952. rtl8169_rx_clear(tp);
  1953. err_free_rx_1:
  1954. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  1955. tp->RxPhyAddr);
  1956. err_free_tx_0:
  1957. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  1958. tp->TxPhyAddr);
  1959. goto out;
  1960. }
  1961. static void rtl8169_hw_reset(void __iomem *ioaddr)
  1962. {
  1963. /* Disable interrupts */
  1964. rtl8169_irq_mask_and_ack(ioaddr);
  1965. /* Reset the chipset */
  1966. RTL_W8(ChipCmd, CmdReset);
  1967. /* PCI commit */
  1968. RTL_R8(ChipCmd);
  1969. }
  1970. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  1971. {
  1972. void __iomem *ioaddr = tp->mmio_addr;
  1973. u32 cfg = rtl8169_rx_config;
  1974. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  1975. RTL_W32(RxConfig, cfg);
  1976. /* Set DMA burst size and Interframe Gap Time */
  1977. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  1978. (InterFrameGap << TxInterFrameGapShift));
  1979. }
  1980. static void rtl_hw_start(struct net_device *dev)
  1981. {
  1982. struct rtl8169_private *tp = netdev_priv(dev);
  1983. void __iomem *ioaddr = tp->mmio_addr;
  1984. unsigned int i;
  1985. /* Soft reset the chip. */
  1986. RTL_W8(ChipCmd, CmdReset);
  1987. /* Check that the chip has finished the reset. */
  1988. for (i = 0; i < 100; i++) {
  1989. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1990. break;
  1991. msleep_interruptible(1);
  1992. }
  1993. tp->hw_start(dev);
  1994. netif_start_queue(dev);
  1995. }
  1996. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  1997. void __iomem *ioaddr)
  1998. {
  1999. /*
  2000. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  2001. * register to be written before TxDescAddrLow to work.
  2002. * Switching from MMIO to I/O access fixes the issue as well.
  2003. */
  2004. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  2005. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_32BIT_MASK);
  2006. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  2007. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_32BIT_MASK);
  2008. }
  2009. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  2010. {
  2011. u16 cmd;
  2012. cmd = RTL_R16(CPlusCmd);
  2013. RTL_W16(CPlusCmd, cmd);
  2014. return cmd;
  2015. }
  2016. static void rtl_set_rx_max_size(void __iomem *ioaddr)
  2017. {
  2018. /* Low hurts. Let's disable the filtering. */
  2019. RTL_W16(RxMaxSize, 16383);
  2020. }
  2021. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  2022. {
  2023. struct {
  2024. u32 mac_version;
  2025. u32 clk;
  2026. u32 val;
  2027. } cfg2_info [] = {
  2028. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  2029. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  2030. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  2031. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  2032. }, *p = cfg2_info;
  2033. unsigned int i;
  2034. u32 clk;
  2035. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  2036. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  2037. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  2038. RTL_W32(0x7c, p->val);
  2039. break;
  2040. }
  2041. }
  2042. }
  2043. static void rtl_hw_start_8169(struct net_device *dev)
  2044. {
  2045. struct rtl8169_private *tp = netdev_priv(dev);
  2046. void __iomem *ioaddr = tp->mmio_addr;
  2047. struct pci_dev *pdev = tp->pci_dev;
  2048. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  2049. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  2050. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  2051. }
  2052. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2053. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2054. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2055. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2056. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2057. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2058. RTL_W8(EarlyTxThres, EarlyTxThld);
  2059. rtl_set_rx_max_size(ioaddr);
  2060. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2061. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2062. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2063. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2064. rtl_set_rx_tx_config_registers(tp);
  2065. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2066. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2067. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  2068. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  2069. "Bit-3 and bit-14 MUST be 1\n");
  2070. tp->cp_cmd |= (1 << 14);
  2071. }
  2072. RTL_W16(CPlusCmd, tp->cp_cmd);
  2073. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  2074. /*
  2075. * Undocumented corner. Supposedly:
  2076. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  2077. */
  2078. RTL_W16(IntrMitigate, 0x0000);
  2079. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2080. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  2081. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  2082. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  2083. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  2084. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2085. rtl_set_rx_tx_config_registers(tp);
  2086. }
  2087. RTL_W8(Cfg9346, Cfg9346_Lock);
  2088. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  2089. RTL_R8(IntrMask);
  2090. RTL_W32(RxMissed, 0);
  2091. rtl_set_rx_mode(dev);
  2092. /* no early-rx interrupts */
  2093. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2094. /* Enable all known interrupts by setting the interrupt mask. */
  2095. RTL_W16(IntrMask, tp->intr_event);
  2096. }
  2097. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  2098. {
  2099. struct net_device *dev = pci_get_drvdata(pdev);
  2100. struct rtl8169_private *tp = netdev_priv(dev);
  2101. int cap = tp->pcie_cap;
  2102. if (cap) {
  2103. u16 ctl;
  2104. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  2105. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  2106. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  2107. }
  2108. }
  2109. static void rtl_csi_access_enable(void __iomem *ioaddr)
  2110. {
  2111. u32 csi;
  2112. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  2113. rtl_csi_write(ioaddr, 0x070c, csi | 0x27000000);
  2114. }
  2115. struct ephy_info {
  2116. unsigned int offset;
  2117. u16 mask;
  2118. u16 bits;
  2119. };
  2120. static void rtl_ephy_init(void __iomem *ioaddr, struct ephy_info *e, int len)
  2121. {
  2122. u16 w;
  2123. while (len-- > 0) {
  2124. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  2125. rtl_ephy_write(ioaddr, e->offset, w);
  2126. e++;
  2127. }
  2128. }
  2129. static void rtl_disable_clock_request(struct pci_dev *pdev)
  2130. {
  2131. struct net_device *dev = pci_get_drvdata(pdev);
  2132. struct rtl8169_private *tp = netdev_priv(dev);
  2133. int cap = tp->pcie_cap;
  2134. if (cap) {
  2135. u16 ctl;
  2136. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  2137. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  2138. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  2139. }
  2140. }
  2141. #define R8168_CPCMD_QUIRK_MASK (\
  2142. EnableBist | \
  2143. Mac_dbgo_oe | \
  2144. Force_half_dup | \
  2145. Force_rxflow_en | \
  2146. Force_txflow_en | \
  2147. Cxpl_dbg_sel | \
  2148. ASF | \
  2149. PktCntrDisable | \
  2150. Mac_dbgo_sel)
  2151. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  2152. {
  2153. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2154. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2155. rtl_tx_performance_tweak(pdev,
  2156. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  2157. }
  2158. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  2159. {
  2160. rtl_hw_start_8168bb(ioaddr, pdev);
  2161. RTL_W8(EarlyTxThres, EarlyTxThld);
  2162. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  2163. }
  2164. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  2165. {
  2166. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  2167. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2168. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2169. rtl_disable_clock_request(pdev);
  2170. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2171. }
  2172. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2173. {
  2174. static struct ephy_info e_info_8168cp[] = {
  2175. { 0x01, 0, 0x0001 },
  2176. { 0x02, 0x0800, 0x1000 },
  2177. { 0x03, 0, 0x0042 },
  2178. { 0x06, 0x0080, 0x0000 },
  2179. { 0x07, 0, 0x2000 }
  2180. };
  2181. rtl_csi_access_enable(ioaddr);
  2182. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  2183. __rtl_hw_start_8168cp(ioaddr, pdev);
  2184. }
  2185. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2186. {
  2187. rtl_csi_access_enable(ioaddr);
  2188. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2189. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2190. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2191. }
  2192. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2193. {
  2194. rtl_csi_access_enable(ioaddr);
  2195. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2196. /* Magic. */
  2197. RTL_W8(DBG_REG, 0x20);
  2198. RTL_W8(EarlyTxThres, EarlyTxThld);
  2199. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2200. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2201. }
  2202. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2203. {
  2204. static struct ephy_info e_info_8168c_1[] = {
  2205. { 0x02, 0x0800, 0x1000 },
  2206. { 0x03, 0, 0x0002 },
  2207. { 0x06, 0x0080, 0x0000 }
  2208. };
  2209. rtl_csi_access_enable(ioaddr);
  2210. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  2211. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  2212. __rtl_hw_start_8168cp(ioaddr, pdev);
  2213. }
  2214. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2215. {
  2216. static struct ephy_info e_info_8168c_2[] = {
  2217. { 0x01, 0, 0x0001 },
  2218. { 0x03, 0x0400, 0x0220 }
  2219. };
  2220. rtl_csi_access_enable(ioaddr);
  2221. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  2222. __rtl_hw_start_8168cp(ioaddr, pdev);
  2223. }
  2224. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2225. {
  2226. rtl_hw_start_8168c_2(ioaddr, pdev);
  2227. }
  2228. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  2229. {
  2230. rtl_csi_access_enable(ioaddr);
  2231. __rtl_hw_start_8168cp(ioaddr, pdev);
  2232. }
  2233. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  2234. {
  2235. rtl_csi_access_enable(ioaddr);
  2236. rtl_disable_clock_request(pdev);
  2237. RTL_W8(EarlyTxThres, EarlyTxThld);
  2238. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2239. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2240. }
  2241. static void rtl_hw_start_8168(struct net_device *dev)
  2242. {
  2243. struct rtl8169_private *tp = netdev_priv(dev);
  2244. void __iomem *ioaddr = tp->mmio_addr;
  2245. struct pci_dev *pdev = tp->pci_dev;
  2246. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2247. RTL_W8(EarlyTxThres, EarlyTxThld);
  2248. rtl_set_rx_max_size(ioaddr);
  2249. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  2250. RTL_W16(CPlusCmd, tp->cp_cmd);
  2251. RTL_W16(IntrMitigate, 0x5151);
  2252. /* Work around for RxFIFO overflow. */
  2253. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  2254. tp->intr_event |= RxFIFOOver | PCSTimeout;
  2255. tp->intr_event &= ~RxOverflow;
  2256. }
  2257. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2258. rtl_set_rx_mode(dev);
  2259. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  2260. (InterFrameGap << TxInterFrameGapShift));
  2261. RTL_R8(IntrMask);
  2262. switch (tp->mac_version) {
  2263. case RTL_GIGA_MAC_VER_11:
  2264. rtl_hw_start_8168bb(ioaddr, pdev);
  2265. break;
  2266. case RTL_GIGA_MAC_VER_12:
  2267. case RTL_GIGA_MAC_VER_17:
  2268. rtl_hw_start_8168bef(ioaddr, pdev);
  2269. break;
  2270. case RTL_GIGA_MAC_VER_18:
  2271. rtl_hw_start_8168cp_1(ioaddr, pdev);
  2272. break;
  2273. case RTL_GIGA_MAC_VER_19:
  2274. rtl_hw_start_8168c_1(ioaddr, pdev);
  2275. break;
  2276. case RTL_GIGA_MAC_VER_20:
  2277. rtl_hw_start_8168c_2(ioaddr, pdev);
  2278. break;
  2279. case RTL_GIGA_MAC_VER_21:
  2280. rtl_hw_start_8168c_3(ioaddr, pdev);
  2281. break;
  2282. case RTL_GIGA_MAC_VER_22:
  2283. rtl_hw_start_8168c_4(ioaddr, pdev);
  2284. break;
  2285. case RTL_GIGA_MAC_VER_23:
  2286. rtl_hw_start_8168cp_2(ioaddr, pdev);
  2287. break;
  2288. case RTL_GIGA_MAC_VER_24:
  2289. rtl_hw_start_8168cp_3(ioaddr, pdev);
  2290. break;
  2291. case RTL_GIGA_MAC_VER_25:
  2292. rtl_hw_start_8168d(ioaddr, pdev);
  2293. break;
  2294. default:
  2295. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  2296. dev->name, tp->mac_version);
  2297. break;
  2298. }
  2299. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2300. RTL_W8(Cfg9346, Cfg9346_Lock);
  2301. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2302. RTL_W16(IntrMask, tp->intr_event);
  2303. }
  2304. #define R810X_CPCMD_QUIRK_MASK (\
  2305. EnableBist | \
  2306. Mac_dbgo_oe | \
  2307. Force_half_dup | \
  2308. Force_half_dup | \
  2309. Force_txflow_en | \
  2310. Cxpl_dbg_sel | \
  2311. ASF | \
  2312. PktCntrDisable | \
  2313. PCIDAC | \
  2314. PCIMulRW)
  2315. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2316. {
  2317. static struct ephy_info e_info_8102e_1[] = {
  2318. { 0x01, 0, 0x6e65 },
  2319. { 0x02, 0, 0x091f },
  2320. { 0x03, 0, 0xc2f9 },
  2321. { 0x06, 0, 0xafb5 },
  2322. { 0x07, 0, 0x0e00 },
  2323. { 0x19, 0, 0xec80 },
  2324. { 0x01, 0, 0x2e65 },
  2325. { 0x01, 0, 0x6e65 }
  2326. };
  2327. u8 cfg1;
  2328. rtl_csi_access_enable(ioaddr);
  2329. RTL_W8(DBG_REG, FIX_NAK_1);
  2330. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2331. RTL_W8(Config1,
  2332. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  2333. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2334. cfg1 = RTL_R8(Config1);
  2335. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  2336. RTL_W8(Config1, cfg1 & ~LEDS0);
  2337. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  2338. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  2339. }
  2340. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2341. {
  2342. rtl_csi_access_enable(ioaddr);
  2343. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2344. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  2345. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2346. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  2347. }
  2348. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2349. {
  2350. rtl_hw_start_8102e_2(ioaddr, pdev);
  2351. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  2352. }
  2353. static void rtl_hw_start_8101(struct net_device *dev)
  2354. {
  2355. struct rtl8169_private *tp = netdev_priv(dev);
  2356. void __iomem *ioaddr = tp->mmio_addr;
  2357. struct pci_dev *pdev = tp->pci_dev;
  2358. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  2359. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  2360. int cap = tp->pcie_cap;
  2361. if (cap) {
  2362. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  2363. PCI_EXP_DEVCTL_NOSNOOP_EN);
  2364. }
  2365. }
  2366. switch (tp->mac_version) {
  2367. case RTL_GIGA_MAC_VER_07:
  2368. rtl_hw_start_8102e_1(ioaddr, pdev);
  2369. break;
  2370. case RTL_GIGA_MAC_VER_08:
  2371. rtl_hw_start_8102e_3(ioaddr, pdev);
  2372. break;
  2373. case RTL_GIGA_MAC_VER_09:
  2374. rtl_hw_start_8102e_2(ioaddr, pdev);
  2375. break;
  2376. }
  2377. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2378. RTL_W8(EarlyTxThres, EarlyTxThld);
  2379. rtl_set_rx_max_size(ioaddr);
  2380. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2381. RTL_W16(CPlusCmd, tp->cp_cmd);
  2382. RTL_W16(IntrMitigate, 0x0000);
  2383. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2384. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2385. rtl_set_rx_tx_config_registers(tp);
  2386. RTL_W8(Cfg9346, Cfg9346_Lock);
  2387. RTL_R8(IntrMask);
  2388. rtl_set_rx_mode(dev);
  2389. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2390. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  2391. RTL_W16(IntrMask, tp->intr_event);
  2392. }
  2393. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  2394. {
  2395. struct rtl8169_private *tp = netdev_priv(dev);
  2396. int ret = 0;
  2397. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  2398. return -EINVAL;
  2399. dev->mtu = new_mtu;
  2400. if (!netif_running(dev))
  2401. goto out;
  2402. rtl8169_down(dev);
  2403. rtl8169_set_rxbufsize(tp, dev);
  2404. ret = rtl8169_init_ring(dev);
  2405. if (ret < 0)
  2406. goto out;
  2407. napi_enable(&tp->napi);
  2408. rtl_hw_start(dev);
  2409. rtl8169_request_timer(dev);
  2410. out:
  2411. return ret;
  2412. }
  2413. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  2414. {
  2415. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  2416. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  2417. }
  2418. static void rtl8169_free_rx_skb(struct rtl8169_private *tp,
  2419. struct sk_buff **sk_buff, struct RxDesc *desc)
  2420. {
  2421. struct pci_dev *pdev = tp->pci_dev;
  2422. pci_unmap_single(pdev, le64_to_cpu(desc->addr), tp->rx_buf_sz,
  2423. PCI_DMA_FROMDEVICE);
  2424. dev_kfree_skb(*sk_buff);
  2425. *sk_buff = NULL;
  2426. rtl8169_make_unusable_by_asic(desc);
  2427. }
  2428. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  2429. {
  2430. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  2431. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  2432. }
  2433. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  2434. u32 rx_buf_sz)
  2435. {
  2436. desc->addr = cpu_to_le64(mapping);
  2437. wmb();
  2438. rtl8169_mark_to_asic(desc, rx_buf_sz);
  2439. }
  2440. static struct sk_buff *rtl8169_alloc_rx_skb(struct pci_dev *pdev,
  2441. struct net_device *dev,
  2442. struct RxDesc *desc, int rx_buf_sz,
  2443. unsigned int align)
  2444. {
  2445. struct sk_buff *skb;
  2446. dma_addr_t mapping;
  2447. unsigned int pad;
  2448. pad = align ? align : NET_IP_ALIGN;
  2449. skb = netdev_alloc_skb(dev, rx_buf_sz + pad);
  2450. if (!skb)
  2451. goto err_out;
  2452. skb_reserve(skb, align ? ((pad - 1) & (unsigned long)skb->data) : pad);
  2453. mapping = pci_map_single(pdev, skb->data, rx_buf_sz,
  2454. PCI_DMA_FROMDEVICE);
  2455. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  2456. out:
  2457. return skb;
  2458. err_out:
  2459. rtl8169_make_unusable_by_asic(desc);
  2460. goto out;
  2461. }
  2462. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  2463. {
  2464. unsigned int i;
  2465. for (i = 0; i < NUM_RX_DESC; i++) {
  2466. if (tp->Rx_skbuff[i]) {
  2467. rtl8169_free_rx_skb(tp, tp->Rx_skbuff + i,
  2468. tp->RxDescArray + i);
  2469. }
  2470. }
  2471. }
  2472. static u32 rtl8169_rx_fill(struct rtl8169_private *tp, struct net_device *dev,
  2473. u32 start, u32 end)
  2474. {
  2475. u32 cur;
  2476. for (cur = start; end - cur != 0; cur++) {
  2477. struct sk_buff *skb;
  2478. unsigned int i = cur % NUM_RX_DESC;
  2479. WARN_ON((s32)(end - cur) < 0);
  2480. if (tp->Rx_skbuff[i])
  2481. continue;
  2482. skb = rtl8169_alloc_rx_skb(tp->pci_dev, dev,
  2483. tp->RxDescArray + i,
  2484. tp->rx_buf_sz, tp->align);
  2485. if (!skb)
  2486. break;
  2487. tp->Rx_skbuff[i] = skb;
  2488. }
  2489. return cur - start;
  2490. }
  2491. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  2492. {
  2493. desc->opts1 |= cpu_to_le32(RingEnd);
  2494. }
  2495. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  2496. {
  2497. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  2498. }
  2499. static int rtl8169_init_ring(struct net_device *dev)
  2500. {
  2501. struct rtl8169_private *tp = netdev_priv(dev);
  2502. rtl8169_init_ring_indexes(tp);
  2503. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  2504. memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));
  2505. if (rtl8169_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
  2506. goto err_out;
  2507. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  2508. return 0;
  2509. err_out:
  2510. rtl8169_rx_clear(tp);
  2511. return -ENOMEM;
  2512. }
  2513. static void rtl8169_unmap_tx_skb(struct pci_dev *pdev, struct ring_info *tx_skb,
  2514. struct TxDesc *desc)
  2515. {
  2516. unsigned int len = tx_skb->len;
  2517. pci_unmap_single(pdev, le64_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);
  2518. desc->opts1 = 0x00;
  2519. desc->opts2 = 0x00;
  2520. desc->addr = 0x00;
  2521. tx_skb->len = 0;
  2522. }
  2523. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  2524. {
  2525. unsigned int i;
  2526. for (i = tp->dirty_tx; i < tp->dirty_tx + NUM_TX_DESC; i++) {
  2527. unsigned int entry = i % NUM_TX_DESC;
  2528. struct ring_info *tx_skb = tp->tx_skb + entry;
  2529. unsigned int len = tx_skb->len;
  2530. if (len) {
  2531. struct sk_buff *skb = tx_skb->skb;
  2532. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb,
  2533. tp->TxDescArray + entry);
  2534. if (skb) {
  2535. dev_kfree_skb(skb);
  2536. tx_skb->skb = NULL;
  2537. }
  2538. tp->dev->stats.tx_dropped++;
  2539. }
  2540. }
  2541. tp->cur_tx = tp->dirty_tx = 0;
  2542. }
  2543. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  2544. {
  2545. struct rtl8169_private *tp = netdev_priv(dev);
  2546. PREPARE_DELAYED_WORK(&tp->task, task);
  2547. schedule_delayed_work(&tp->task, 4);
  2548. }
  2549. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  2550. {
  2551. struct rtl8169_private *tp = netdev_priv(dev);
  2552. void __iomem *ioaddr = tp->mmio_addr;
  2553. synchronize_irq(dev->irq);
  2554. /* Wait for any pending NAPI task to complete */
  2555. napi_disable(&tp->napi);
  2556. rtl8169_irq_mask_and_ack(ioaddr);
  2557. tp->intr_mask = 0xffff;
  2558. RTL_W16(IntrMask, tp->intr_event);
  2559. napi_enable(&tp->napi);
  2560. }
  2561. static void rtl8169_reinit_task(struct work_struct *work)
  2562. {
  2563. struct rtl8169_private *tp =
  2564. container_of(work, struct rtl8169_private, task.work);
  2565. struct net_device *dev = tp->dev;
  2566. int ret;
  2567. rtnl_lock();
  2568. if (!netif_running(dev))
  2569. goto out_unlock;
  2570. rtl8169_wait_for_quiescence(dev);
  2571. rtl8169_close(dev);
  2572. ret = rtl8169_open(dev);
  2573. if (unlikely(ret < 0)) {
  2574. if (net_ratelimit() && netif_msg_drv(tp)) {
  2575. printk(KERN_ERR PFX "%s: reinit failure (status = %d)."
  2576. " Rescheduling.\n", dev->name, ret);
  2577. }
  2578. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  2579. }
  2580. out_unlock:
  2581. rtnl_unlock();
  2582. }
  2583. static void rtl8169_reset_task(struct work_struct *work)
  2584. {
  2585. struct rtl8169_private *tp =
  2586. container_of(work, struct rtl8169_private, task.work);
  2587. struct net_device *dev = tp->dev;
  2588. rtnl_lock();
  2589. if (!netif_running(dev))
  2590. goto out_unlock;
  2591. rtl8169_wait_for_quiescence(dev);
  2592. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
  2593. rtl8169_tx_clear(tp);
  2594. if (tp->dirty_rx == tp->cur_rx) {
  2595. rtl8169_init_ring_indexes(tp);
  2596. rtl_hw_start(dev);
  2597. netif_wake_queue(dev);
  2598. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  2599. } else {
  2600. if (net_ratelimit() && netif_msg_intr(tp)) {
  2601. printk(KERN_EMERG PFX "%s: Rx buffers shortage\n",
  2602. dev->name);
  2603. }
  2604. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2605. }
  2606. out_unlock:
  2607. rtnl_unlock();
  2608. }
  2609. static void rtl8169_tx_timeout(struct net_device *dev)
  2610. {
  2611. struct rtl8169_private *tp = netdev_priv(dev);
  2612. rtl8169_hw_reset(tp->mmio_addr);
  2613. /* Let's wait a bit while any (async) irq lands on */
  2614. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2615. }
  2616. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  2617. u32 opts1)
  2618. {
  2619. struct skb_shared_info *info = skb_shinfo(skb);
  2620. unsigned int cur_frag, entry;
  2621. struct TxDesc * uninitialized_var(txd);
  2622. entry = tp->cur_tx;
  2623. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  2624. skb_frag_t *frag = info->frags + cur_frag;
  2625. dma_addr_t mapping;
  2626. u32 status, len;
  2627. void *addr;
  2628. entry = (entry + 1) % NUM_TX_DESC;
  2629. txd = tp->TxDescArray + entry;
  2630. len = frag->size;
  2631. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  2632. mapping = pci_map_single(tp->pci_dev, addr, len, PCI_DMA_TODEVICE);
  2633. /* anti gcc 2.95.3 bugware (sic) */
  2634. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  2635. txd->opts1 = cpu_to_le32(status);
  2636. txd->addr = cpu_to_le64(mapping);
  2637. tp->tx_skb[entry].len = len;
  2638. }
  2639. if (cur_frag) {
  2640. tp->tx_skb[entry].skb = skb;
  2641. txd->opts1 |= cpu_to_le32(LastFrag);
  2642. }
  2643. return cur_frag;
  2644. }
  2645. static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
  2646. {
  2647. if (dev->features & NETIF_F_TSO) {
  2648. u32 mss = skb_shinfo(skb)->gso_size;
  2649. if (mss)
  2650. return LargeSend | ((mss & MSSMask) << MSSShift);
  2651. }
  2652. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2653. const struct iphdr *ip = ip_hdr(skb);
  2654. if (ip->protocol == IPPROTO_TCP)
  2655. return IPCS | TCPCS;
  2656. else if (ip->protocol == IPPROTO_UDP)
  2657. return IPCS | UDPCS;
  2658. WARN_ON(1); /* we need a WARN() */
  2659. }
  2660. return 0;
  2661. }
  2662. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2663. {
  2664. struct rtl8169_private *tp = netdev_priv(dev);
  2665. unsigned int frags, entry = tp->cur_tx % NUM_TX_DESC;
  2666. struct TxDesc *txd = tp->TxDescArray + entry;
  2667. void __iomem *ioaddr = tp->mmio_addr;
  2668. dma_addr_t mapping;
  2669. u32 status, len;
  2670. u32 opts1;
  2671. int ret = NETDEV_TX_OK;
  2672. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  2673. if (netif_msg_drv(tp)) {
  2674. printk(KERN_ERR
  2675. "%s: BUG! Tx Ring full when queue awake!\n",
  2676. dev->name);
  2677. }
  2678. goto err_stop;
  2679. }
  2680. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  2681. goto err_stop;
  2682. opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
  2683. frags = rtl8169_xmit_frags(tp, skb, opts1);
  2684. if (frags) {
  2685. len = skb_headlen(skb);
  2686. opts1 |= FirstFrag;
  2687. } else {
  2688. len = skb->len;
  2689. if (unlikely(len < ETH_ZLEN)) {
  2690. if (skb_padto(skb, ETH_ZLEN))
  2691. goto err_update_stats;
  2692. len = ETH_ZLEN;
  2693. }
  2694. opts1 |= FirstFrag | LastFrag;
  2695. tp->tx_skb[entry].skb = skb;
  2696. }
  2697. mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  2698. tp->tx_skb[entry].len = len;
  2699. txd->addr = cpu_to_le64(mapping);
  2700. txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  2701. wmb();
  2702. /* anti gcc 2.95.3 bugware (sic) */
  2703. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  2704. txd->opts1 = cpu_to_le32(status);
  2705. dev->trans_start = jiffies;
  2706. tp->cur_tx += frags + 1;
  2707. smp_wmb();
  2708. RTL_W8(TxPoll, NPQ); /* set polling bit */
  2709. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  2710. netif_stop_queue(dev);
  2711. smp_rmb();
  2712. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  2713. netif_wake_queue(dev);
  2714. }
  2715. out:
  2716. return ret;
  2717. err_stop:
  2718. netif_stop_queue(dev);
  2719. ret = NETDEV_TX_BUSY;
  2720. err_update_stats:
  2721. dev->stats.tx_dropped++;
  2722. goto out;
  2723. }
  2724. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  2725. {
  2726. struct rtl8169_private *tp = netdev_priv(dev);
  2727. struct pci_dev *pdev = tp->pci_dev;
  2728. void __iomem *ioaddr = tp->mmio_addr;
  2729. u16 pci_status, pci_cmd;
  2730. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  2731. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  2732. if (netif_msg_intr(tp)) {
  2733. printk(KERN_ERR
  2734. "%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
  2735. dev->name, pci_cmd, pci_status);
  2736. }
  2737. /*
  2738. * The recovery sequence below admits a very elaborated explanation:
  2739. * - it seems to work;
  2740. * - I did not see what else could be done;
  2741. * - it makes iop3xx happy.
  2742. *
  2743. * Feel free to adjust to your needs.
  2744. */
  2745. if (pdev->broken_parity_status)
  2746. pci_cmd &= ~PCI_COMMAND_PARITY;
  2747. else
  2748. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  2749. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  2750. pci_write_config_word(pdev, PCI_STATUS,
  2751. pci_status & (PCI_STATUS_DETECTED_PARITY |
  2752. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  2753. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  2754. /* The infamous DAC f*ckup only happens at boot time */
  2755. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  2756. if (netif_msg_intr(tp))
  2757. printk(KERN_INFO "%s: disabling PCI DAC.\n", dev->name);
  2758. tp->cp_cmd &= ~PCIDAC;
  2759. RTL_W16(CPlusCmd, tp->cp_cmd);
  2760. dev->features &= ~NETIF_F_HIGHDMA;
  2761. }
  2762. rtl8169_hw_reset(ioaddr);
  2763. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  2764. }
  2765. static void rtl8169_tx_interrupt(struct net_device *dev,
  2766. struct rtl8169_private *tp,
  2767. void __iomem *ioaddr)
  2768. {
  2769. unsigned int dirty_tx, tx_left;
  2770. dirty_tx = tp->dirty_tx;
  2771. smp_rmb();
  2772. tx_left = tp->cur_tx - dirty_tx;
  2773. while (tx_left > 0) {
  2774. unsigned int entry = dirty_tx % NUM_TX_DESC;
  2775. struct ring_info *tx_skb = tp->tx_skb + entry;
  2776. u32 len = tx_skb->len;
  2777. u32 status;
  2778. rmb();
  2779. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  2780. if (status & DescOwn)
  2781. break;
  2782. dev->stats.tx_bytes += len;
  2783. dev->stats.tx_packets++;
  2784. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb, tp->TxDescArray + entry);
  2785. if (status & LastFrag) {
  2786. dev_kfree_skb_irq(tx_skb->skb);
  2787. tx_skb->skb = NULL;
  2788. }
  2789. dirty_tx++;
  2790. tx_left--;
  2791. }
  2792. if (tp->dirty_tx != dirty_tx) {
  2793. tp->dirty_tx = dirty_tx;
  2794. smp_wmb();
  2795. if (netif_queue_stopped(dev) &&
  2796. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  2797. netif_wake_queue(dev);
  2798. }
  2799. /*
  2800. * 8168 hack: TxPoll requests are lost when the Tx packets are
  2801. * too close. Let's kick an extra TxPoll request when a burst
  2802. * of start_xmit activity is detected (if it is not detected,
  2803. * it is slow enough). -- FR
  2804. */
  2805. smp_rmb();
  2806. if (tp->cur_tx != dirty_tx)
  2807. RTL_W8(TxPoll, NPQ);
  2808. }
  2809. }
  2810. static inline int rtl8169_fragmented_frame(u32 status)
  2811. {
  2812. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  2813. }
  2814. static inline void rtl8169_rx_csum(struct sk_buff *skb, struct RxDesc *desc)
  2815. {
  2816. u32 opts1 = le32_to_cpu(desc->opts1);
  2817. u32 status = opts1 & RxProtoMask;
  2818. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  2819. ((status == RxProtoUDP) && !(opts1 & UDPFail)) ||
  2820. ((status == RxProtoIP) && !(opts1 & IPFail)))
  2821. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2822. else
  2823. skb->ip_summed = CHECKSUM_NONE;
  2824. }
  2825. static inline bool rtl8169_try_rx_copy(struct sk_buff **sk_buff,
  2826. struct rtl8169_private *tp, int pkt_size,
  2827. dma_addr_t addr)
  2828. {
  2829. struct sk_buff *skb;
  2830. bool done = false;
  2831. if (pkt_size >= rx_copybreak)
  2832. goto out;
  2833. skb = netdev_alloc_skb(tp->dev, pkt_size + NET_IP_ALIGN);
  2834. if (!skb)
  2835. goto out;
  2836. pci_dma_sync_single_for_cpu(tp->pci_dev, addr, pkt_size,
  2837. PCI_DMA_FROMDEVICE);
  2838. skb_reserve(skb, NET_IP_ALIGN);
  2839. skb_copy_from_linear_data(*sk_buff, skb->data, pkt_size);
  2840. *sk_buff = skb;
  2841. done = true;
  2842. out:
  2843. return done;
  2844. }
  2845. static int rtl8169_rx_interrupt(struct net_device *dev,
  2846. struct rtl8169_private *tp,
  2847. void __iomem *ioaddr, u32 budget)
  2848. {
  2849. unsigned int cur_rx, rx_left;
  2850. unsigned int delta, count;
  2851. cur_rx = tp->cur_rx;
  2852. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  2853. rx_left = min(rx_left, budget);
  2854. for (; rx_left > 0; rx_left--, cur_rx++) {
  2855. unsigned int entry = cur_rx % NUM_RX_DESC;
  2856. struct RxDesc *desc = tp->RxDescArray + entry;
  2857. u32 status;
  2858. rmb();
  2859. status = le32_to_cpu(desc->opts1);
  2860. if (status & DescOwn)
  2861. break;
  2862. if (unlikely(status & RxRES)) {
  2863. if (netif_msg_rx_err(tp)) {
  2864. printk(KERN_INFO
  2865. "%s: Rx ERROR. status = %08x\n",
  2866. dev->name, status);
  2867. }
  2868. dev->stats.rx_errors++;
  2869. if (status & (RxRWT | RxRUNT))
  2870. dev->stats.rx_length_errors++;
  2871. if (status & RxCRC)
  2872. dev->stats.rx_crc_errors++;
  2873. if (status & RxFOVF) {
  2874. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2875. dev->stats.rx_fifo_errors++;
  2876. }
  2877. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2878. } else {
  2879. struct sk_buff *skb = tp->Rx_skbuff[entry];
  2880. dma_addr_t addr = le64_to_cpu(desc->addr);
  2881. int pkt_size = (status & 0x00001FFF) - 4;
  2882. struct pci_dev *pdev = tp->pci_dev;
  2883. /*
  2884. * The driver does not support incoming fragmented
  2885. * frames. They are seen as a symptom of over-mtu
  2886. * sized frames.
  2887. */
  2888. if (unlikely(rtl8169_fragmented_frame(status))) {
  2889. dev->stats.rx_dropped++;
  2890. dev->stats.rx_length_errors++;
  2891. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2892. continue;
  2893. }
  2894. rtl8169_rx_csum(skb, desc);
  2895. if (rtl8169_try_rx_copy(&skb, tp, pkt_size, addr)) {
  2896. pci_dma_sync_single_for_device(pdev, addr,
  2897. pkt_size, PCI_DMA_FROMDEVICE);
  2898. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2899. } else {
  2900. pci_unmap_single(pdev, addr, tp->rx_buf_sz,
  2901. PCI_DMA_FROMDEVICE);
  2902. tp->Rx_skbuff[entry] = NULL;
  2903. }
  2904. skb_put(skb, pkt_size);
  2905. skb->protocol = eth_type_trans(skb, dev);
  2906. if (rtl8169_rx_vlan_skb(tp, desc, skb) < 0)
  2907. netif_receive_skb(skb);
  2908. dev->last_rx = jiffies;
  2909. dev->stats.rx_bytes += pkt_size;
  2910. dev->stats.rx_packets++;
  2911. }
  2912. /* Work around for AMD plateform. */
  2913. if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
  2914. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  2915. desc->opts2 = 0;
  2916. cur_rx++;
  2917. }
  2918. }
  2919. count = cur_rx - tp->cur_rx;
  2920. tp->cur_rx = cur_rx;
  2921. delta = rtl8169_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
  2922. if (!delta && count && netif_msg_intr(tp))
  2923. printk(KERN_INFO "%s: no Rx buffer allocated\n", dev->name);
  2924. tp->dirty_rx += delta;
  2925. /*
  2926. * FIXME: until there is periodic timer to try and refill the ring,
  2927. * a temporary shortage may definitely kill the Rx process.
  2928. * - disable the asic to try and avoid an overflow and kick it again
  2929. * after refill ?
  2930. * - how do others driver handle this condition (Uh oh...).
  2931. */
  2932. if ((tp->dirty_rx + NUM_RX_DESC == tp->cur_rx) && netif_msg_intr(tp))
  2933. printk(KERN_EMERG "%s: Rx buffers exhausted\n", dev->name);
  2934. return count;
  2935. }
  2936. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  2937. {
  2938. struct net_device *dev = dev_instance;
  2939. struct rtl8169_private *tp = netdev_priv(dev);
  2940. void __iomem *ioaddr = tp->mmio_addr;
  2941. int handled = 0;
  2942. int status;
  2943. status = RTL_R16(IntrStatus);
  2944. /* hotplug/major error/no more work/shared irq */
  2945. if ((status == 0xffff) || !status)
  2946. goto out;
  2947. handled = 1;
  2948. if (unlikely(!netif_running(dev))) {
  2949. rtl8169_asic_down(ioaddr);
  2950. goto out;
  2951. }
  2952. status &= tp->intr_mask;
  2953. RTL_W16(IntrStatus,
  2954. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  2955. if (!(status & tp->intr_event))
  2956. goto out;
  2957. /* Work around for rx fifo overflow */
  2958. if (unlikely(status & RxFIFOOver) &&
  2959. (tp->mac_version == RTL_GIGA_MAC_VER_11)) {
  2960. netif_stop_queue(dev);
  2961. rtl8169_tx_timeout(dev);
  2962. goto out;
  2963. }
  2964. if (unlikely(status & SYSErr)) {
  2965. rtl8169_pcierr_interrupt(dev);
  2966. goto out;
  2967. }
  2968. if (status & LinkChg)
  2969. rtl8169_check_link_status(dev, tp, ioaddr);
  2970. if (status & tp->napi_event) {
  2971. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  2972. tp->intr_mask = ~tp->napi_event;
  2973. if (likely(netif_rx_schedule_prep(dev, &tp->napi)))
  2974. __netif_rx_schedule(dev, &tp->napi);
  2975. else if (netif_msg_intr(tp)) {
  2976. printk(KERN_INFO "%s: interrupt %04x in poll\n",
  2977. dev->name, status);
  2978. }
  2979. }
  2980. out:
  2981. return IRQ_RETVAL(handled);
  2982. }
  2983. static int rtl8169_poll(struct napi_struct *napi, int budget)
  2984. {
  2985. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  2986. struct net_device *dev = tp->dev;
  2987. void __iomem *ioaddr = tp->mmio_addr;
  2988. int work_done;
  2989. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  2990. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2991. if (work_done < budget) {
  2992. netif_rx_complete(dev, napi);
  2993. tp->intr_mask = 0xffff;
  2994. /*
  2995. * 20040426: the barrier is not strictly required but the
  2996. * behavior of the irq handler could be less predictable
  2997. * without it. Btw, the lack of flush for the posted pci
  2998. * write is safe - FR
  2999. */
  3000. smp_wmb();
  3001. RTL_W16(IntrMask, tp->intr_event);
  3002. }
  3003. return work_done;
  3004. }
  3005. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  3006. {
  3007. struct rtl8169_private *tp = netdev_priv(dev);
  3008. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  3009. return;
  3010. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  3011. RTL_W32(RxMissed, 0);
  3012. }
  3013. static void rtl8169_down(struct net_device *dev)
  3014. {
  3015. struct rtl8169_private *tp = netdev_priv(dev);
  3016. void __iomem *ioaddr = tp->mmio_addr;
  3017. unsigned int intrmask;
  3018. rtl8169_delete_timer(dev);
  3019. netif_stop_queue(dev);
  3020. napi_disable(&tp->napi);
  3021. core_down:
  3022. spin_lock_irq(&tp->lock);
  3023. rtl8169_asic_down(ioaddr);
  3024. rtl8169_rx_missed(dev, ioaddr);
  3025. spin_unlock_irq(&tp->lock);
  3026. synchronize_irq(dev->irq);
  3027. /* Give a racing hard_start_xmit a few cycles to complete. */
  3028. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  3029. /*
  3030. * And now for the 50k$ question: are IRQ disabled or not ?
  3031. *
  3032. * Two paths lead here:
  3033. * 1) dev->close
  3034. * -> netif_running() is available to sync the current code and the
  3035. * IRQ handler. See rtl8169_interrupt for details.
  3036. * 2) dev->change_mtu
  3037. * -> rtl8169_poll can not be issued again and re-enable the
  3038. * interruptions. Let's simply issue the IRQ down sequence again.
  3039. *
  3040. * No loop if hotpluged or major error (0xffff).
  3041. */
  3042. intrmask = RTL_R16(IntrMask);
  3043. if (intrmask && (intrmask != 0xffff))
  3044. goto core_down;
  3045. rtl8169_tx_clear(tp);
  3046. rtl8169_rx_clear(tp);
  3047. }
  3048. static int rtl8169_close(struct net_device *dev)
  3049. {
  3050. struct rtl8169_private *tp = netdev_priv(dev);
  3051. struct pci_dev *pdev = tp->pci_dev;
  3052. rtl8169_down(dev);
  3053. free_irq(dev->irq, dev);
  3054. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3055. tp->RxPhyAddr);
  3056. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3057. tp->TxPhyAddr);
  3058. tp->TxDescArray = NULL;
  3059. tp->RxDescArray = NULL;
  3060. return 0;
  3061. }
  3062. static void rtl_set_rx_mode(struct net_device *dev)
  3063. {
  3064. struct rtl8169_private *tp = netdev_priv(dev);
  3065. void __iomem *ioaddr = tp->mmio_addr;
  3066. unsigned long flags;
  3067. u32 mc_filter[2]; /* Multicast hash filter */
  3068. int rx_mode;
  3069. u32 tmp = 0;
  3070. if (dev->flags & IFF_PROMISC) {
  3071. /* Unconditionally log net taps. */
  3072. if (netif_msg_link(tp)) {
  3073. printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
  3074. dev->name);
  3075. }
  3076. rx_mode =
  3077. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  3078. AcceptAllPhys;
  3079. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3080. } else if ((dev->mc_count > multicast_filter_limit)
  3081. || (dev->flags & IFF_ALLMULTI)) {
  3082. /* Too many to filter perfectly -- accept all multicasts. */
  3083. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  3084. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3085. } else {
  3086. struct dev_mc_list *mclist;
  3087. unsigned int i;
  3088. rx_mode = AcceptBroadcast | AcceptMyPhys;
  3089. mc_filter[1] = mc_filter[0] = 0;
  3090. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  3091. i++, mclist = mclist->next) {
  3092. int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
  3093. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  3094. rx_mode |= AcceptMulticast;
  3095. }
  3096. }
  3097. spin_lock_irqsave(&tp->lock, flags);
  3098. tmp = rtl8169_rx_config | rx_mode |
  3099. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  3100. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  3101. u32 data = mc_filter[0];
  3102. mc_filter[0] = swab32(mc_filter[1]);
  3103. mc_filter[1] = swab32(data);
  3104. }
  3105. RTL_W32(MAR0 + 0, mc_filter[0]);
  3106. RTL_W32(MAR0 + 4, mc_filter[1]);
  3107. RTL_W32(RxConfig, tmp);
  3108. spin_unlock_irqrestore(&tp->lock, flags);
  3109. }
  3110. /**
  3111. * rtl8169_get_stats - Get rtl8169 read/write statistics
  3112. * @dev: The Ethernet Device to get statistics for
  3113. *
  3114. * Get TX/RX statistics for rtl8169
  3115. */
  3116. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  3117. {
  3118. struct rtl8169_private *tp = netdev_priv(dev);
  3119. void __iomem *ioaddr = tp->mmio_addr;
  3120. unsigned long flags;
  3121. if (netif_running(dev)) {
  3122. spin_lock_irqsave(&tp->lock, flags);
  3123. rtl8169_rx_missed(dev, ioaddr);
  3124. spin_unlock_irqrestore(&tp->lock, flags);
  3125. }
  3126. return &dev->stats;
  3127. }
  3128. #ifdef CONFIG_PM
  3129. static int rtl8169_suspend(struct pci_dev *pdev, pm_message_t state)
  3130. {
  3131. struct net_device *dev = pci_get_drvdata(pdev);
  3132. struct rtl8169_private *tp = netdev_priv(dev);
  3133. void __iomem *ioaddr = tp->mmio_addr;
  3134. if (!netif_running(dev))
  3135. goto out_pci_suspend;
  3136. netif_device_detach(dev);
  3137. netif_stop_queue(dev);
  3138. spin_lock_irq(&tp->lock);
  3139. rtl8169_asic_down(ioaddr);
  3140. rtl8169_rx_missed(dev, ioaddr);
  3141. spin_unlock_irq(&tp->lock);
  3142. out_pci_suspend:
  3143. pci_save_state(pdev);
  3144. pci_enable_wake(pdev, pci_choose_state(pdev, state),
  3145. (tp->features & RTL_FEATURE_WOL) ? 1 : 0);
  3146. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3147. return 0;
  3148. }
  3149. static int rtl8169_resume(struct pci_dev *pdev)
  3150. {
  3151. struct net_device *dev = pci_get_drvdata(pdev);
  3152. pci_set_power_state(pdev, PCI_D0);
  3153. pci_restore_state(pdev);
  3154. pci_enable_wake(pdev, PCI_D0, 0);
  3155. if (!netif_running(dev))
  3156. goto out;
  3157. netif_device_attach(dev);
  3158. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3159. out:
  3160. return 0;
  3161. }
  3162. static void rtl_shutdown(struct pci_dev *pdev)
  3163. {
  3164. rtl8169_suspend(pdev, PMSG_SUSPEND);
  3165. }
  3166. #endif /* CONFIG_PM */
  3167. static struct pci_driver rtl8169_pci_driver = {
  3168. .name = MODULENAME,
  3169. .id_table = rtl8169_pci_tbl,
  3170. .probe = rtl8169_init_one,
  3171. .remove = __devexit_p(rtl8169_remove_one),
  3172. #ifdef CONFIG_PM
  3173. .suspend = rtl8169_suspend,
  3174. .resume = rtl8169_resume,
  3175. .shutdown = rtl_shutdown,
  3176. #endif
  3177. };
  3178. static int __init rtl8169_init_module(void)
  3179. {
  3180. return pci_register_driver(&rtl8169_pci_driver);
  3181. }
  3182. static void __exit rtl8169_cleanup_module(void)
  3183. {
  3184. pci_unregister_driver(&rtl8169_pci_driver);
  3185. }
  3186. module_init(rtl8169_init_module);
  3187. module_exit(rtl8169_cleanup_module);