r8169.c 78 KB

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