r8169.c 91 KB

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