r8169.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  1. /*
  2. =========================================================================
  3. r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver for Linux kernel 2.4.x.
  4. --------------------------------------------------------------------
  5. History:
  6. Feb 4 2002 - created initially by ShuChen <shuchen@realtek.com.tw>.
  7. May 20 2002 - Add link status force-mode and TBI mode support.
  8. 2004 - Massive updates. See kernel SCM system for details.
  9. =========================================================================
  10. 1. [DEPRECATED: use ethtool instead] The media can be forced in 5 modes.
  11. Command: 'insmod r8169 media = SET_MEDIA'
  12. Ex: 'insmod r8169 media = 0x04' will force PHY to operate in 100Mpbs Half-duplex.
  13. SET_MEDIA can be:
  14. _10_Half = 0x01
  15. _10_Full = 0x02
  16. _100_Half = 0x04
  17. _100_Full = 0x08
  18. _1000_Full = 0x10
  19. 2. Support TBI mode.
  20. =========================================================================
  21. VERSION 1.1 <2002/10/4>
  22. The bit4:0 of MII register 4 is called "selector field", and have to be
  23. 00001b to indicate support of IEEE std 802.3 during NWay process of
  24. exchanging Link Code Word (FLP).
  25. VERSION 1.2 <2002/11/30>
  26. - Large style cleanup
  27. - Use ether_crc in stock kernel (linux/crc32.h)
  28. - Copy mc_filter setup code from 8139cp
  29. (includes an optimization, and avoids set_bit use)
  30. VERSION 1.6LK <2004/04/14>
  31. - Merge of Realtek's version 1.6
  32. - Conversion to DMA API
  33. - Suspend/resume
  34. - Endianness
  35. - Misc Rx/Tx bugs
  36. VERSION 2.2LK <2005/01/25>
  37. - RX csum, TX csum/SG, TSO
  38. - VLAN
  39. - baby (< 7200) Jumbo frames support
  40. - Merge of Realtek's version 2.2 (new phy)
  41. */
  42. #include <linux/module.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/pci.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/delay.h>
  48. #include <linux/ethtool.h>
  49. #include <linux/mii.h>
  50. #include <linux/if_vlan.h>
  51. #include <linux/crc32.h>
  52. #include <linux/in.h>
  53. #include <linux/ip.h>
  54. #include <linux/tcp.h>
  55. #include <linux/init.h>
  56. #include <linux/dma-mapping.h>
  57. #include <asm/io.h>
  58. #include <asm/irq.h>
  59. #ifdef CONFIG_R8169_NAPI
  60. #define NAPI_SUFFIX "-NAPI"
  61. #else
  62. #define NAPI_SUFFIX ""
  63. #endif
  64. #define RTL8169_VERSION "2.2LK" NAPI_SUFFIX
  65. #define MODULENAME "r8169"
  66. #define PFX MODULENAME ": "
  67. #ifdef RTL8169_DEBUG
  68. #define assert(expr) \
  69. if(!(expr)) { \
  70. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  71. #expr,__FILE__,__FUNCTION__,__LINE__); \
  72. }
  73. #define dprintk(fmt, args...) do { printk(PFX fmt, ## args); } while (0)
  74. #else
  75. #define assert(expr) do {} while (0)
  76. #define dprintk(fmt, args...) do {} while (0)
  77. #endif /* RTL8169_DEBUG */
  78. #define R8169_MSG_DEFAULT \
  79. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  80. #define TX_BUFFS_AVAIL(tp) \
  81. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  82. #ifdef CONFIG_R8169_NAPI
  83. #define rtl8169_rx_skb netif_receive_skb
  84. #define rtl8169_rx_hwaccel_skb vlan_hwaccel_receive_skb
  85. #define rtl8169_rx_quota(count, quota) min(count, quota)
  86. #else
  87. #define rtl8169_rx_skb netif_rx
  88. #define rtl8169_rx_hwaccel_skb vlan_hwaccel_rx
  89. #define rtl8169_rx_quota(count, quota) count
  90. #endif
  91. /* media options */
  92. #define MAX_UNITS 8
  93. static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  94. static int num_media = 0;
  95. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  96. static int max_interrupt_work = 20;
  97. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  98. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  99. static int multicast_filter_limit = 32;
  100. /* MAC address length */
  101. #define MAC_ADDR_LEN 6
  102. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  103. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  104. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  105. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  106. #define RxPacketMaxSize 0x3FE8 /* 16K - 1 - ETH_HLEN - VLAN - CRC... */
  107. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  108. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  109. #define R8169_REGS_SIZE 256
  110. #define R8169_NAPI_WEIGHT 64
  111. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  112. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  113. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  114. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  115. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  116. #define RTL8169_TX_TIMEOUT (6*HZ)
  117. #define RTL8169_PHY_TIMEOUT (10*HZ)
  118. /* write/read MMIO register */
  119. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  120. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  121. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  122. #define RTL_R8(reg) readb (ioaddr + (reg))
  123. #define RTL_R16(reg) readw (ioaddr + (reg))
  124. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  125. enum mac_version {
  126. RTL_GIGA_MAC_VER_B = 0x00,
  127. /* RTL_GIGA_MAC_VER_C = 0x03, */
  128. RTL_GIGA_MAC_VER_D = 0x01,
  129. RTL_GIGA_MAC_VER_E = 0x02,
  130. RTL_GIGA_MAC_VER_X = 0x04 /* Greater than RTL_GIGA_MAC_VER_E */
  131. };
  132. enum phy_version {
  133. RTL_GIGA_PHY_VER_C = 0x03, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  134. RTL_GIGA_PHY_VER_D = 0x04, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  135. RTL_GIGA_PHY_VER_E = 0x05, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  136. RTL_GIGA_PHY_VER_F = 0x06, /* PHY Reg 0x03 bit0-3 == 0x0001 */
  137. RTL_GIGA_PHY_VER_G = 0x07, /* PHY Reg 0x03 bit0-3 == 0x0002 */
  138. RTL_GIGA_PHY_VER_H = 0x08, /* PHY Reg 0x03 bit0-3 == 0x0003 */
  139. };
  140. #define _R(NAME,MAC,MASK) \
  141. { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
  142. const static struct {
  143. const char *name;
  144. u8 mac_version;
  145. u32 RxConfigMask; /* Clears the bits supported by this chip */
  146. } rtl_chip_info[] = {
  147. _R("RTL8169", RTL_GIGA_MAC_VER_B, 0xff7e1880),
  148. _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_D, 0xff7e1880),
  149. _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_E, 0xff7e1880),
  150. _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_X, 0xff7e1880),
  151. };
  152. #undef _R
  153. static struct pci_device_id rtl8169_pci_tbl[] = {
  154. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), },
  155. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), },
  156. { PCI_DEVICE(0x16ec, 0x0116), },
  157. { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024, },
  158. {0,},
  159. };
  160. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  161. static int rx_copybreak = 200;
  162. static int use_dac;
  163. static struct {
  164. u32 msg_enable;
  165. } debug = { -1 };
  166. enum RTL8169_registers {
  167. MAC0 = 0, /* Ethernet hardware address. */
  168. MAR0 = 8, /* Multicast filter. */
  169. CounterAddrLow = 0x10,
  170. CounterAddrHigh = 0x14,
  171. TxDescStartAddrLow = 0x20,
  172. TxDescStartAddrHigh = 0x24,
  173. TxHDescStartAddrLow = 0x28,
  174. TxHDescStartAddrHigh = 0x2c,
  175. FLASH = 0x30,
  176. ERSR = 0x36,
  177. ChipCmd = 0x37,
  178. TxPoll = 0x38,
  179. IntrMask = 0x3C,
  180. IntrStatus = 0x3E,
  181. TxConfig = 0x40,
  182. RxConfig = 0x44,
  183. RxMissed = 0x4C,
  184. Cfg9346 = 0x50,
  185. Config0 = 0x51,
  186. Config1 = 0x52,
  187. Config2 = 0x53,
  188. Config3 = 0x54,
  189. Config4 = 0x55,
  190. Config5 = 0x56,
  191. MultiIntr = 0x5C,
  192. PHYAR = 0x60,
  193. TBICSR = 0x64,
  194. TBI_ANAR = 0x68,
  195. TBI_LPAR = 0x6A,
  196. PHYstatus = 0x6C,
  197. RxMaxSize = 0xDA,
  198. CPlusCmd = 0xE0,
  199. IntrMitigate = 0xE2,
  200. RxDescAddrLow = 0xE4,
  201. RxDescAddrHigh = 0xE8,
  202. EarlyTxThres = 0xEC,
  203. FuncEvent = 0xF0,
  204. FuncEventMask = 0xF4,
  205. FuncPresetState = 0xF8,
  206. FuncForceEvent = 0xFC,
  207. };
  208. enum RTL8169_register_content {
  209. /* InterruptStatusBits */
  210. SYSErr = 0x8000,
  211. PCSTimeout = 0x4000,
  212. SWInt = 0x0100,
  213. TxDescUnavail = 0x80,
  214. RxFIFOOver = 0x40,
  215. LinkChg = 0x20,
  216. RxOverflow = 0x10,
  217. TxErr = 0x08,
  218. TxOK = 0x04,
  219. RxErr = 0x02,
  220. RxOK = 0x01,
  221. /* RxStatusDesc */
  222. RxRES = 0x00200000,
  223. RxCRC = 0x00080000,
  224. RxRUNT = 0x00100000,
  225. RxRWT = 0x00400000,
  226. /* ChipCmdBits */
  227. CmdReset = 0x10,
  228. CmdRxEnb = 0x08,
  229. CmdTxEnb = 0x04,
  230. RxBufEmpty = 0x01,
  231. /* Cfg9346Bits */
  232. Cfg9346_Lock = 0x00,
  233. Cfg9346_Unlock = 0xC0,
  234. /* rx_mode_bits */
  235. AcceptErr = 0x20,
  236. AcceptRunt = 0x10,
  237. AcceptBroadcast = 0x08,
  238. AcceptMulticast = 0x04,
  239. AcceptMyPhys = 0x02,
  240. AcceptAllPhys = 0x01,
  241. /* RxConfigBits */
  242. RxCfgFIFOShift = 13,
  243. RxCfgDMAShift = 8,
  244. /* TxConfigBits */
  245. TxInterFrameGapShift = 24,
  246. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  247. /* TBICSR p.28 */
  248. TBIReset = 0x80000000,
  249. TBILoopback = 0x40000000,
  250. TBINwEnable = 0x20000000,
  251. TBINwRestart = 0x10000000,
  252. TBILinkOk = 0x02000000,
  253. TBINwComplete = 0x01000000,
  254. /* CPlusCmd p.31 */
  255. RxVlan = (1 << 6),
  256. RxChkSum = (1 << 5),
  257. PCIDAC = (1 << 4),
  258. PCIMulRW = (1 << 3),
  259. /* rtl8169_PHYstatus */
  260. TBI_Enable = 0x80,
  261. TxFlowCtrl = 0x40,
  262. RxFlowCtrl = 0x20,
  263. _1000bpsF = 0x10,
  264. _100bps = 0x08,
  265. _10bps = 0x04,
  266. LinkStatus = 0x02,
  267. FullDup = 0x01,
  268. /* GIGABIT_PHY_registers */
  269. PHY_CTRL_REG = 0,
  270. PHY_STAT_REG = 1,
  271. PHY_AUTO_NEGO_REG = 4,
  272. PHY_1000_CTRL_REG = 9,
  273. /* GIGABIT_PHY_REG_BIT */
  274. PHY_Restart_Auto_Nego = 0x0200,
  275. PHY_Enable_Auto_Nego = 0x1000,
  276. /* PHY_STAT_REG = 1 */
  277. PHY_Auto_Neco_Comp = 0x0020,
  278. /* PHY_AUTO_NEGO_REG = 4 */
  279. PHY_Cap_10_Half = 0x0020,
  280. PHY_Cap_10_Full = 0x0040,
  281. PHY_Cap_100_Half = 0x0080,
  282. PHY_Cap_100_Full = 0x0100,
  283. /* PHY_1000_CTRL_REG = 9 */
  284. PHY_Cap_1000_Full = 0x0200,
  285. PHY_Cap_Null = 0x0,
  286. /* _MediaType */
  287. _10_Half = 0x01,
  288. _10_Full = 0x02,
  289. _100_Half = 0x04,
  290. _100_Full = 0x08,
  291. _1000_Full = 0x10,
  292. /* _TBICSRBit */
  293. TBILinkOK = 0x02000000,
  294. /* DumpCounterCommand */
  295. CounterDump = 0x8,
  296. };
  297. enum _DescStatusBit {
  298. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  299. RingEnd = (1 << 30), /* End of descriptor ring */
  300. FirstFrag = (1 << 29), /* First segment of a packet */
  301. LastFrag = (1 << 28), /* Final segment of a packet */
  302. /* Tx private */
  303. LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
  304. MSSShift = 16, /* MSS value position */
  305. MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
  306. IPCS = (1 << 18), /* Calculate IP checksum */
  307. UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
  308. TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
  309. TxVlanTag = (1 << 17), /* Add VLAN tag */
  310. /* Rx private */
  311. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  312. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  313. #define RxProtoUDP (PID1)
  314. #define RxProtoTCP (PID0)
  315. #define RxProtoIP (PID1 | PID0)
  316. #define RxProtoMask RxProtoIP
  317. IPFail = (1 << 16), /* IP checksum failed */
  318. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  319. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  320. RxVlanTag = (1 << 16), /* VLAN tag available */
  321. };
  322. #define RsvdMask 0x3fffc000
  323. struct TxDesc {
  324. u32 opts1;
  325. u32 opts2;
  326. u64 addr;
  327. };
  328. struct RxDesc {
  329. u32 opts1;
  330. u32 opts2;
  331. u64 addr;
  332. };
  333. struct ring_info {
  334. struct sk_buff *skb;
  335. u32 len;
  336. u8 __pad[sizeof(void *) - sizeof(u32)];
  337. };
  338. struct rtl8169_private {
  339. void __iomem *mmio_addr; /* memory map physical address */
  340. struct pci_dev *pci_dev; /* Index of PCI device */
  341. struct net_device_stats stats; /* statistics of net device */
  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 rx_buf_sz;
  358. struct timer_list timer;
  359. u16 cp_cmd;
  360. u16 intr_mask;
  361. int phy_auto_nego_reg;
  362. int phy_1000_ctrl_reg;
  363. #ifdef CONFIG_R8169_VLAN
  364. struct vlan_group *vlgrp;
  365. #endif
  366. int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
  367. void (*get_settings)(struct net_device *, struct ethtool_cmd *);
  368. void (*phy_reset_enable)(void __iomem *);
  369. unsigned int (*phy_reset_pending)(void __iomem *);
  370. unsigned int (*link_ok)(void __iomem *);
  371. struct work_struct task;
  372. };
  373. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  374. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  375. module_param_array(media, int, &num_media, 0);
  376. MODULE_PARM_DESC(media, "force phy operation. Deprecated by ethtool (8).");
  377. module_param(rx_copybreak, int, 0);
  378. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  379. module_param(use_dac, int, 0);
  380. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  381. module_param_named(debug, debug.msg_enable, int, 0);
  382. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  383. MODULE_LICENSE("GPL");
  384. MODULE_VERSION(RTL8169_VERSION);
  385. static int rtl8169_open(struct net_device *dev);
  386. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev);
  387. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance,
  388. struct pt_regs *regs);
  389. static int rtl8169_init_ring(struct net_device *dev);
  390. static void rtl8169_hw_start(struct net_device *dev);
  391. static int rtl8169_close(struct net_device *dev);
  392. static void rtl8169_set_rx_mode(struct net_device *dev);
  393. static void rtl8169_tx_timeout(struct net_device *dev);
  394. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  395. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  396. void __iomem *);
  397. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  398. static void rtl8169_down(struct net_device *dev);
  399. #ifdef CONFIG_R8169_NAPI
  400. static int rtl8169_poll(struct net_device *dev, int *budget);
  401. #endif
  402. static const u16 rtl8169_intr_mask =
  403. SYSErr | LinkChg | RxOverflow | RxFIFOOver | TxErr | TxOK | RxErr | RxOK;
  404. static const u16 rtl8169_napi_event =
  405. RxOK | RxOverflow | RxFIFOOver | TxOK | TxErr;
  406. static const unsigned int rtl8169_rx_config =
  407. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  408. #define PHY_Cap_10_Half_Or_Less PHY_Cap_10_Half
  409. #define PHY_Cap_10_Full_Or_Less PHY_Cap_10_Full | PHY_Cap_10_Half_Or_Less
  410. #define PHY_Cap_100_Half_Or_Less PHY_Cap_100_Half | PHY_Cap_10_Full_Or_Less
  411. #define PHY_Cap_100_Full_Or_Less PHY_Cap_100_Full | PHY_Cap_100_Half_Or_Less
  412. static void mdio_write(void __iomem *ioaddr, int RegAddr, int value)
  413. {
  414. int i;
  415. RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
  416. udelay(1000);
  417. for (i = 2000; i > 0; i--) {
  418. /* Check if the RTL8169 has completed writing to the specified MII register */
  419. if (!(RTL_R32(PHYAR) & 0x80000000))
  420. break;
  421. udelay(100);
  422. }
  423. }
  424. static int mdio_read(void __iomem *ioaddr, int RegAddr)
  425. {
  426. int i, value = -1;
  427. RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
  428. udelay(1000);
  429. for (i = 2000; i > 0; i--) {
  430. /* Check if the RTL8169 has completed retrieving data from the specified MII register */
  431. if (RTL_R32(PHYAR) & 0x80000000) {
  432. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  433. break;
  434. }
  435. udelay(100);
  436. }
  437. return value;
  438. }
  439. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  440. {
  441. RTL_W16(IntrMask, 0x0000);
  442. RTL_W16(IntrStatus, 0xffff);
  443. }
  444. static void rtl8169_asic_down(void __iomem *ioaddr)
  445. {
  446. RTL_W8(ChipCmd, 0x00);
  447. rtl8169_irq_mask_and_ack(ioaddr);
  448. RTL_R16(CPlusCmd);
  449. }
  450. static unsigned int rtl8169_tbi_reset_pending(void __iomem *ioaddr)
  451. {
  452. return RTL_R32(TBICSR) & TBIReset;
  453. }
  454. static unsigned int rtl8169_xmii_reset_pending(void __iomem *ioaddr)
  455. {
  456. return mdio_read(ioaddr, 0) & 0x8000;
  457. }
  458. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  459. {
  460. return RTL_R32(TBICSR) & TBILinkOk;
  461. }
  462. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  463. {
  464. return RTL_R8(PHYstatus) & LinkStatus;
  465. }
  466. static void rtl8169_tbi_reset_enable(void __iomem *ioaddr)
  467. {
  468. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  469. }
  470. static void rtl8169_xmii_reset_enable(void __iomem *ioaddr)
  471. {
  472. unsigned int val;
  473. val = (mdio_read(ioaddr, PHY_CTRL_REG) | 0x8000) & 0xffff;
  474. mdio_write(ioaddr, PHY_CTRL_REG, val);
  475. }
  476. static void rtl8169_check_link_status(struct net_device *dev,
  477. struct rtl8169_private *tp, 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_link_option(int idx, u8 *autoneg, u16 *speed, u8 *duplex)
  493. {
  494. struct {
  495. u16 speed;
  496. u8 duplex;
  497. u8 autoneg;
  498. u8 media;
  499. } link_settings[] = {
  500. { SPEED_10, DUPLEX_HALF, AUTONEG_DISABLE, _10_Half },
  501. { SPEED_10, DUPLEX_FULL, AUTONEG_DISABLE, _10_Full },
  502. { SPEED_100, DUPLEX_HALF, AUTONEG_DISABLE, _100_Half },
  503. { SPEED_100, DUPLEX_FULL, AUTONEG_DISABLE, _100_Full },
  504. { SPEED_1000, DUPLEX_FULL, AUTONEG_DISABLE, _1000_Full },
  505. /* Make TBI happy */
  506. { SPEED_1000, DUPLEX_FULL, AUTONEG_ENABLE, 0xff }
  507. }, *p;
  508. unsigned char option;
  509. option = ((idx < MAX_UNITS) && (idx >= 0)) ? media[idx] : 0xff;
  510. if ((option != 0xff) && !idx && netif_msg_drv(&debug))
  511. printk(KERN_WARNING PFX "media option is deprecated.\n");
  512. for (p = link_settings; p->media != 0xff; p++) {
  513. if (p->media == option)
  514. break;
  515. }
  516. *autoneg = p->autoneg;
  517. *speed = p->speed;
  518. *duplex = p->duplex;
  519. }
  520. static void rtl8169_get_drvinfo(struct net_device *dev,
  521. struct ethtool_drvinfo *info)
  522. {
  523. struct rtl8169_private *tp = netdev_priv(dev);
  524. strcpy(info->driver, MODULENAME);
  525. strcpy(info->version, RTL8169_VERSION);
  526. strcpy(info->bus_info, pci_name(tp->pci_dev));
  527. }
  528. static int rtl8169_get_regs_len(struct net_device *dev)
  529. {
  530. return R8169_REGS_SIZE;
  531. }
  532. static int rtl8169_set_speed_tbi(struct net_device *dev,
  533. u8 autoneg, u16 speed, u8 duplex)
  534. {
  535. struct rtl8169_private *tp = netdev_priv(dev);
  536. void __iomem *ioaddr = tp->mmio_addr;
  537. int ret = 0;
  538. u32 reg;
  539. reg = RTL_R32(TBICSR);
  540. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  541. (duplex == DUPLEX_FULL)) {
  542. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  543. } else if (autoneg == AUTONEG_ENABLE)
  544. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  545. else {
  546. if (netif_msg_link(tp)) {
  547. printk(KERN_WARNING "%s: "
  548. "incorrect speed setting refused in TBI mode\n",
  549. dev->name);
  550. }
  551. ret = -EOPNOTSUPP;
  552. }
  553. return ret;
  554. }
  555. static int rtl8169_set_speed_xmii(struct net_device *dev,
  556. u8 autoneg, u16 speed, u8 duplex)
  557. {
  558. struct rtl8169_private *tp = netdev_priv(dev);
  559. void __iomem *ioaddr = tp->mmio_addr;
  560. int auto_nego, giga_ctrl;
  561. auto_nego = mdio_read(ioaddr, PHY_AUTO_NEGO_REG);
  562. auto_nego &= ~(PHY_Cap_10_Half | PHY_Cap_10_Full |
  563. PHY_Cap_100_Half | PHY_Cap_100_Full);
  564. giga_ctrl = mdio_read(ioaddr, PHY_1000_CTRL_REG);
  565. giga_ctrl &= ~(PHY_Cap_1000_Full | PHY_Cap_Null);
  566. if (autoneg == AUTONEG_ENABLE) {
  567. auto_nego |= (PHY_Cap_10_Half | PHY_Cap_10_Full |
  568. PHY_Cap_100_Half | PHY_Cap_100_Full);
  569. giga_ctrl |= PHY_Cap_1000_Full;
  570. } else {
  571. if (speed == SPEED_10)
  572. auto_nego |= PHY_Cap_10_Half | PHY_Cap_10_Full;
  573. else if (speed == SPEED_100)
  574. auto_nego |= PHY_Cap_100_Half | PHY_Cap_100_Full;
  575. else if (speed == SPEED_1000)
  576. giga_ctrl |= PHY_Cap_1000_Full;
  577. if (duplex == DUPLEX_HALF)
  578. auto_nego &= ~(PHY_Cap_10_Full | PHY_Cap_100_Full);
  579. }
  580. tp->phy_auto_nego_reg = auto_nego;
  581. tp->phy_1000_ctrl_reg = giga_ctrl;
  582. mdio_write(ioaddr, PHY_AUTO_NEGO_REG, auto_nego);
  583. mdio_write(ioaddr, PHY_1000_CTRL_REG, giga_ctrl);
  584. mdio_write(ioaddr, PHY_CTRL_REG, PHY_Enable_Auto_Nego |
  585. PHY_Restart_Auto_Nego);
  586. return 0;
  587. }
  588. static int rtl8169_set_speed(struct net_device *dev,
  589. u8 autoneg, u16 speed, u8 duplex)
  590. {
  591. struct rtl8169_private *tp = netdev_priv(dev);
  592. int ret;
  593. ret = tp->set_speed(dev, autoneg, speed, duplex);
  594. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & PHY_Cap_1000_Full))
  595. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  596. return ret;
  597. }
  598. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  599. {
  600. struct rtl8169_private *tp = netdev_priv(dev);
  601. unsigned long flags;
  602. int ret;
  603. spin_lock_irqsave(&tp->lock, flags);
  604. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  605. spin_unlock_irqrestore(&tp->lock, flags);
  606. return ret;
  607. }
  608. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  609. {
  610. struct rtl8169_private *tp = netdev_priv(dev);
  611. return tp->cp_cmd & RxChkSum;
  612. }
  613. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  614. {
  615. struct rtl8169_private *tp = netdev_priv(dev);
  616. void __iomem *ioaddr = tp->mmio_addr;
  617. unsigned long flags;
  618. spin_lock_irqsave(&tp->lock, flags);
  619. if (data)
  620. tp->cp_cmd |= RxChkSum;
  621. else
  622. tp->cp_cmd &= ~RxChkSum;
  623. RTL_W16(CPlusCmd, tp->cp_cmd);
  624. RTL_R16(CPlusCmd);
  625. spin_unlock_irqrestore(&tp->lock, flags);
  626. return 0;
  627. }
  628. #ifdef CONFIG_R8169_VLAN
  629. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  630. struct sk_buff *skb)
  631. {
  632. return (tp->vlgrp && vlan_tx_tag_present(skb)) ?
  633. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  634. }
  635. static void rtl8169_vlan_rx_register(struct net_device *dev,
  636. struct vlan_group *grp)
  637. {
  638. struct rtl8169_private *tp = netdev_priv(dev);
  639. void __iomem *ioaddr = tp->mmio_addr;
  640. unsigned long flags;
  641. spin_lock_irqsave(&tp->lock, flags);
  642. tp->vlgrp = grp;
  643. if (tp->vlgrp)
  644. tp->cp_cmd |= RxVlan;
  645. else
  646. tp->cp_cmd &= ~RxVlan;
  647. RTL_W16(CPlusCmd, tp->cp_cmd);
  648. RTL_R16(CPlusCmd);
  649. spin_unlock_irqrestore(&tp->lock, flags);
  650. }
  651. static void rtl8169_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
  652. {
  653. struct rtl8169_private *tp = netdev_priv(dev);
  654. unsigned long flags;
  655. spin_lock_irqsave(&tp->lock, flags);
  656. if (tp->vlgrp)
  657. tp->vlgrp->vlan_devices[vid] = NULL;
  658. spin_unlock_irqrestore(&tp->lock, flags);
  659. }
  660. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  661. struct sk_buff *skb)
  662. {
  663. u32 opts2 = le32_to_cpu(desc->opts2);
  664. int ret;
  665. if (tp->vlgrp && (opts2 & RxVlanTag)) {
  666. rtl8169_rx_hwaccel_skb(skb, tp->vlgrp,
  667. swab16(opts2 & 0xffff));
  668. ret = 0;
  669. } else
  670. ret = -1;
  671. desc->opts2 = 0;
  672. return ret;
  673. }
  674. #else /* !CONFIG_R8169_VLAN */
  675. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  676. struct sk_buff *skb)
  677. {
  678. return 0;
  679. }
  680. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  681. struct sk_buff *skb)
  682. {
  683. return -1;
  684. }
  685. #endif
  686. static void rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  687. {
  688. struct rtl8169_private *tp = netdev_priv(dev);
  689. void __iomem *ioaddr = tp->mmio_addr;
  690. u32 status;
  691. cmd->supported =
  692. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  693. cmd->port = PORT_FIBRE;
  694. cmd->transceiver = XCVR_INTERNAL;
  695. status = RTL_R32(TBICSR);
  696. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  697. cmd->autoneg = !!(status & TBINwEnable);
  698. cmd->speed = SPEED_1000;
  699. cmd->duplex = DUPLEX_FULL; /* Always set */
  700. }
  701. static void rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  702. {
  703. struct rtl8169_private *tp = netdev_priv(dev);
  704. void __iomem *ioaddr = tp->mmio_addr;
  705. u8 status;
  706. cmd->supported = SUPPORTED_10baseT_Half |
  707. SUPPORTED_10baseT_Full |
  708. SUPPORTED_100baseT_Half |
  709. SUPPORTED_100baseT_Full |
  710. SUPPORTED_1000baseT_Full |
  711. SUPPORTED_Autoneg |
  712. SUPPORTED_TP;
  713. cmd->autoneg = 1;
  714. cmd->advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
  715. if (tp->phy_auto_nego_reg & PHY_Cap_10_Half)
  716. cmd->advertising |= ADVERTISED_10baseT_Half;
  717. if (tp->phy_auto_nego_reg & PHY_Cap_10_Full)
  718. cmd->advertising |= ADVERTISED_10baseT_Full;
  719. if (tp->phy_auto_nego_reg & PHY_Cap_100_Half)
  720. cmd->advertising |= ADVERTISED_100baseT_Half;
  721. if (tp->phy_auto_nego_reg & PHY_Cap_100_Full)
  722. cmd->advertising |= ADVERTISED_100baseT_Full;
  723. if (tp->phy_1000_ctrl_reg & PHY_Cap_1000_Full)
  724. cmd->advertising |= ADVERTISED_1000baseT_Full;
  725. status = RTL_R8(PHYstatus);
  726. if (status & _1000bpsF)
  727. cmd->speed = SPEED_1000;
  728. else if (status & _100bps)
  729. cmd->speed = SPEED_100;
  730. else if (status & _10bps)
  731. cmd->speed = SPEED_10;
  732. cmd->duplex = ((status & _1000bpsF) || (status & FullDup)) ?
  733. DUPLEX_FULL : DUPLEX_HALF;
  734. }
  735. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  736. {
  737. struct rtl8169_private *tp = netdev_priv(dev);
  738. unsigned long flags;
  739. spin_lock_irqsave(&tp->lock, flags);
  740. tp->get_settings(dev, cmd);
  741. spin_unlock_irqrestore(&tp->lock, flags);
  742. return 0;
  743. }
  744. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  745. void *p)
  746. {
  747. struct rtl8169_private *tp = netdev_priv(dev);
  748. unsigned long flags;
  749. if (regs->len > R8169_REGS_SIZE)
  750. regs->len = R8169_REGS_SIZE;
  751. spin_lock_irqsave(&tp->lock, flags);
  752. memcpy_fromio(p, tp->mmio_addr, regs->len);
  753. spin_unlock_irqrestore(&tp->lock, flags);
  754. }
  755. static u32 rtl8169_get_msglevel(struct net_device *dev)
  756. {
  757. struct rtl8169_private *tp = netdev_priv(dev);
  758. return tp->msg_enable;
  759. }
  760. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  761. {
  762. struct rtl8169_private *tp = netdev_priv(dev);
  763. tp->msg_enable = value;
  764. }
  765. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  766. "tx_packets",
  767. "rx_packets",
  768. "tx_errors",
  769. "rx_errors",
  770. "rx_missed",
  771. "align_errors",
  772. "tx_single_collisions",
  773. "tx_multi_collisions",
  774. "unicast",
  775. "broadcast",
  776. "multicast",
  777. "tx_aborted",
  778. "tx_underrun",
  779. };
  780. struct rtl8169_counters {
  781. u64 tx_packets;
  782. u64 rx_packets;
  783. u64 tx_errors;
  784. u32 rx_errors;
  785. u16 rx_missed;
  786. u16 align_errors;
  787. u32 tx_one_collision;
  788. u32 tx_multi_collision;
  789. u64 rx_unicast;
  790. u64 rx_broadcast;
  791. u32 rx_multicast;
  792. u16 tx_aborted;
  793. u16 tx_underun;
  794. };
  795. static int rtl8169_get_stats_count(struct net_device *dev)
  796. {
  797. return ARRAY_SIZE(rtl8169_gstrings);
  798. }
  799. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  800. struct ethtool_stats *stats, u64 *data)
  801. {
  802. struct rtl8169_private *tp = netdev_priv(dev);
  803. void __iomem *ioaddr = tp->mmio_addr;
  804. struct rtl8169_counters *counters;
  805. dma_addr_t paddr;
  806. u32 cmd;
  807. ASSERT_RTNL();
  808. counters = pci_alloc_consistent(tp->pci_dev, sizeof(*counters), &paddr);
  809. if (!counters)
  810. return;
  811. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  812. cmd = (u64)paddr & DMA_32BIT_MASK;
  813. RTL_W32(CounterAddrLow, cmd);
  814. RTL_W32(CounterAddrLow, cmd | CounterDump);
  815. while (RTL_R32(CounterAddrLow) & CounterDump) {
  816. if (msleep_interruptible(1))
  817. break;
  818. }
  819. RTL_W32(CounterAddrLow, 0);
  820. RTL_W32(CounterAddrHigh, 0);
  821. data[0] = le64_to_cpu(counters->tx_packets);
  822. data[1] = le64_to_cpu(counters->rx_packets);
  823. data[2] = le64_to_cpu(counters->tx_errors);
  824. data[3] = le32_to_cpu(counters->rx_errors);
  825. data[4] = le16_to_cpu(counters->rx_missed);
  826. data[5] = le16_to_cpu(counters->align_errors);
  827. data[6] = le32_to_cpu(counters->tx_one_collision);
  828. data[7] = le32_to_cpu(counters->tx_multi_collision);
  829. data[8] = le64_to_cpu(counters->rx_unicast);
  830. data[9] = le64_to_cpu(counters->rx_broadcast);
  831. data[10] = le32_to_cpu(counters->rx_multicast);
  832. data[11] = le16_to_cpu(counters->tx_aborted);
  833. data[12] = le16_to_cpu(counters->tx_underun);
  834. pci_free_consistent(tp->pci_dev, sizeof(*counters), counters, paddr);
  835. }
  836. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  837. {
  838. switch(stringset) {
  839. case ETH_SS_STATS:
  840. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  841. break;
  842. }
  843. }
  844. static struct ethtool_ops rtl8169_ethtool_ops = {
  845. .get_drvinfo = rtl8169_get_drvinfo,
  846. .get_regs_len = rtl8169_get_regs_len,
  847. .get_link = ethtool_op_get_link,
  848. .get_settings = rtl8169_get_settings,
  849. .set_settings = rtl8169_set_settings,
  850. .get_msglevel = rtl8169_get_msglevel,
  851. .set_msglevel = rtl8169_set_msglevel,
  852. .get_rx_csum = rtl8169_get_rx_csum,
  853. .set_rx_csum = rtl8169_set_rx_csum,
  854. .get_tx_csum = ethtool_op_get_tx_csum,
  855. .set_tx_csum = ethtool_op_set_tx_csum,
  856. .get_sg = ethtool_op_get_sg,
  857. .set_sg = ethtool_op_set_sg,
  858. .get_tso = ethtool_op_get_tso,
  859. .set_tso = ethtool_op_set_tso,
  860. .get_regs = rtl8169_get_regs,
  861. .get_strings = rtl8169_get_strings,
  862. .get_stats_count = rtl8169_get_stats_count,
  863. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  864. .get_perm_addr = ethtool_op_get_perm_addr,
  865. };
  866. static void rtl8169_write_gmii_reg_bit(void __iomem *ioaddr, int reg, int bitnum,
  867. int bitval)
  868. {
  869. int val;
  870. val = mdio_read(ioaddr, reg);
  871. val = (bitval == 1) ?
  872. val | (bitval << bitnum) : val & ~(0x0001 << bitnum);
  873. mdio_write(ioaddr, reg, val & 0xffff);
  874. }
  875. static void rtl8169_get_mac_version(struct rtl8169_private *tp, void __iomem *ioaddr)
  876. {
  877. const struct {
  878. u32 mask;
  879. int mac_version;
  880. } mac_info[] = {
  881. { 0x1 << 28, RTL_GIGA_MAC_VER_X },
  882. { 0x1 << 26, RTL_GIGA_MAC_VER_E },
  883. { 0x1 << 23, RTL_GIGA_MAC_VER_D },
  884. { 0x00000000, RTL_GIGA_MAC_VER_B } /* Catch-all */
  885. }, *p = mac_info;
  886. u32 reg;
  887. reg = RTL_R32(TxConfig) & 0x7c800000;
  888. while ((reg & p->mask) != p->mask)
  889. p++;
  890. tp->mac_version = p->mac_version;
  891. }
  892. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  893. {
  894. struct {
  895. int version;
  896. char *msg;
  897. } mac_print[] = {
  898. { RTL_GIGA_MAC_VER_E, "RTL_GIGA_MAC_VER_E" },
  899. { RTL_GIGA_MAC_VER_D, "RTL_GIGA_MAC_VER_D" },
  900. { RTL_GIGA_MAC_VER_B, "RTL_GIGA_MAC_VER_B" },
  901. { 0, NULL }
  902. }, *p;
  903. for (p = mac_print; p->msg; p++) {
  904. if (tp->mac_version == p->version) {
  905. dprintk("mac_version == %s (%04d)\n", p->msg,
  906. p->version);
  907. return;
  908. }
  909. }
  910. dprintk("mac_version == Unknown\n");
  911. }
  912. static void rtl8169_get_phy_version(struct rtl8169_private *tp, void __iomem *ioaddr)
  913. {
  914. const struct {
  915. u16 mask;
  916. u16 set;
  917. int phy_version;
  918. } phy_info[] = {
  919. { 0x000f, 0x0002, RTL_GIGA_PHY_VER_G },
  920. { 0x000f, 0x0001, RTL_GIGA_PHY_VER_F },
  921. { 0x000f, 0x0000, RTL_GIGA_PHY_VER_E },
  922. { 0x0000, 0x0000, RTL_GIGA_PHY_VER_D } /* Catch-all */
  923. }, *p = phy_info;
  924. u16 reg;
  925. reg = mdio_read(ioaddr, 3) & 0xffff;
  926. while ((reg & p->mask) != p->set)
  927. p++;
  928. tp->phy_version = p->phy_version;
  929. }
  930. static void rtl8169_print_phy_version(struct rtl8169_private *tp)
  931. {
  932. struct {
  933. int version;
  934. char *msg;
  935. u32 reg;
  936. } phy_print[] = {
  937. { RTL_GIGA_PHY_VER_G, "RTL_GIGA_PHY_VER_G", 0x0002 },
  938. { RTL_GIGA_PHY_VER_F, "RTL_GIGA_PHY_VER_F", 0x0001 },
  939. { RTL_GIGA_PHY_VER_E, "RTL_GIGA_PHY_VER_E", 0x0000 },
  940. { RTL_GIGA_PHY_VER_D, "RTL_GIGA_PHY_VER_D", 0x0000 },
  941. { 0, NULL, 0x0000 }
  942. }, *p;
  943. for (p = phy_print; p->msg; p++) {
  944. if (tp->phy_version == p->version) {
  945. dprintk("phy_version == %s (%04x)\n", p->msg, p->reg);
  946. return;
  947. }
  948. }
  949. dprintk("phy_version == Unknown\n");
  950. }
  951. static void rtl8169_hw_phy_config(struct net_device *dev)
  952. {
  953. struct rtl8169_private *tp = netdev_priv(dev);
  954. void __iomem *ioaddr = tp->mmio_addr;
  955. struct {
  956. u16 regs[5]; /* Beware of bit-sign propagation */
  957. } phy_magic[5] = { {
  958. { 0x0000, //w 4 15 12 0
  959. 0x00a1, //w 3 15 0 00a1
  960. 0x0008, //w 2 15 0 0008
  961. 0x1020, //w 1 15 0 1020
  962. 0x1000 } },{ //w 0 15 0 1000
  963. { 0x7000, //w 4 15 12 7
  964. 0xff41, //w 3 15 0 ff41
  965. 0xde60, //w 2 15 0 de60
  966. 0x0140, //w 1 15 0 0140
  967. 0x0077 } },{ //w 0 15 0 0077
  968. { 0xa000, //w 4 15 12 a
  969. 0xdf01, //w 3 15 0 df01
  970. 0xdf20, //w 2 15 0 df20
  971. 0xff95, //w 1 15 0 ff95
  972. 0xfa00 } },{ //w 0 15 0 fa00
  973. { 0xb000, //w 4 15 12 b
  974. 0xff41, //w 3 15 0 ff41
  975. 0xde20, //w 2 15 0 de20
  976. 0x0140, //w 1 15 0 0140
  977. 0x00bb } },{ //w 0 15 0 00bb
  978. { 0xf000, //w 4 15 12 f
  979. 0xdf01, //w 3 15 0 df01
  980. 0xdf20, //w 2 15 0 df20
  981. 0xff95, //w 1 15 0 ff95
  982. 0xbf00 } //w 0 15 0 bf00
  983. }
  984. }, *p = phy_magic;
  985. int i;
  986. rtl8169_print_mac_version(tp);
  987. rtl8169_print_phy_version(tp);
  988. if (tp->mac_version <= RTL_GIGA_MAC_VER_B)
  989. return;
  990. if (tp->phy_version >= RTL_GIGA_PHY_VER_H)
  991. return;
  992. dprintk("MAC version != 0 && PHY version == 0 or 1\n");
  993. dprintk("Do final_reg2.cfg\n");
  994. /* Shazam ! */
  995. if (tp->mac_version == RTL_GIGA_MAC_VER_X) {
  996. mdio_write(ioaddr, 31, 0x0001);
  997. mdio_write(ioaddr, 9, 0x273a);
  998. mdio_write(ioaddr, 14, 0x7bfb);
  999. mdio_write(ioaddr, 27, 0x841e);
  1000. mdio_write(ioaddr, 31, 0x0002);
  1001. mdio_write(ioaddr, 1, 0x90d0);
  1002. mdio_write(ioaddr, 31, 0x0000);
  1003. return;
  1004. }
  1005. /* phy config for RTL8169s mac_version C chip */
  1006. mdio_write(ioaddr, 31, 0x0001); //w 31 2 0 1
  1007. mdio_write(ioaddr, 21, 0x1000); //w 21 15 0 1000
  1008. mdio_write(ioaddr, 24, 0x65c7); //w 24 15 0 65c7
  1009. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1010. for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
  1011. int val, pos = 4;
  1012. val = (mdio_read(ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
  1013. mdio_write(ioaddr, pos, val);
  1014. while (--pos >= 0)
  1015. mdio_write(ioaddr, pos, p->regs[4 - pos] & 0xffff);
  1016. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 1); //w 4 11 11 1
  1017. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1018. }
  1019. mdio_write(ioaddr, 31, 0x0000); //w 31 2 0 0
  1020. }
  1021. static void rtl8169_phy_timer(unsigned long __opaque)
  1022. {
  1023. struct net_device *dev = (struct net_device *)__opaque;
  1024. struct rtl8169_private *tp = netdev_priv(dev);
  1025. struct timer_list *timer = &tp->timer;
  1026. void __iomem *ioaddr = tp->mmio_addr;
  1027. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  1028. assert(tp->mac_version > RTL_GIGA_MAC_VER_B);
  1029. assert(tp->phy_version < RTL_GIGA_PHY_VER_H);
  1030. if (!(tp->phy_1000_ctrl_reg & PHY_Cap_1000_Full))
  1031. return;
  1032. spin_lock_irq(&tp->lock);
  1033. if (tp->phy_reset_pending(ioaddr)) {
  1034. /*
  1035. * A busy loop could burn quite a few cycles on nowadays CPU.
  1036. * Let's delay the execution of the timer for a few ticks.
  1037. */
  1038. timeout = HZ/10;
  1039. goto out_mod_timer;
  1040. }
  1041. if (tp->link_ok(ioaddr))
  1042. goto out_unlock;
  1043. if (netif_msg_link(tp))
  1044. printk(KERN_WARNING "%s: PHY reset until link up\n", dev->name);
  1045. tp->phy_reset_enable(ioaddr);
  1046. out_mod_timer:
  1047. mod_timer(timer, jiffies + timeout);
  1048. out_unlock:
  1049. spin_unlock_irq(&tp->lock);
  1050. }
  1051. static inline void rtl8169_delete_timer(struct net_device *dev)
  1052. {
  1053. struct rtl8169_private *tp = netdev_priv(dev);
  1054. struct timer_list *timer = &tp->timer;
  1055. if ((tp->mac_version <= RTL_GIGA_MAC_VER_B) ||
  1056. (tp->phy_version >= RTL_GIGA_PHY_VER_H))
  1057. return;
  1058. del_timer_sync(timer);
  1059. }
  1060. static inline void rtl8169_request_timer(struct net_device *dev)
  1061. {
  1062. struct rtl8169_private *tp = netdev_priv(dev);
  1063. struct timer_list *timer = &tp->timer;
  1064. if ((tp->mac_version <= RTL_GIGA_MAC_VER_B) ||
  1065. (tp->phy_version >= RTL_GIGA_PHY_VER_H))
  1066. return;
  1067. init_timer(timer);
  1068. timer->expires = jiffies + RTL8169_PHY_TIMEOUT;
  1069. timer->data = (unsigned long)(dev);
  1070. timer->function = rtl8169_phy_timer;
  1071. add_timer(timer);
  1072. }
  1073. #ifdef CONFIG_NET_POLL_CONTROLLER
  1074. /*
  1075. * Polling 'interrupt' - used by things like netconsole to send skbs
  1076. * without having to re-enable interrupts. It's not called while
  1077. * the interrupt routine is executing.
  1078. */
  1079. static void rtl8169_netpoll(struct net_device *dev)
  1080. {
  1081. struct rtl8169_private *tp = netdev_priv(dev);
  1082. struct pci_dev *pdev = tp->pci_dev;
  1083. disable_irq(pdev->irq);
  1084. rtl8169_interrupt(pdev->irq, dev, NULL);
  1085. enable_irq(pdev->irq);
  1086. }
  1087. #endif
  1088. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  1089. void __iomem *ioaddr)
  1090. {
  1091. iounmap(ioaddr);
  1092. pci_release_regions(pdev);
  1093. pci_disable_device(pdev);
  1094. free_netdev(dev);
  1095. }
  1096. static int __devinit
  1097. rtl8169_init_board(struct pci_dev *pdev, struct net_device **dev_out,
  1098. void __iomem **ioaddr_out)
  1099. {
  1100. void __iomem *ioaddr;
  1101. struct net_device *dev;
  1102. struct rtl8169_private *tp;
  1103. int rc = -ENOMEM, i, acpi_idle_state = 0, pm_cap;
  1104. assert(ioaddr_out != NULL);
  1105. /* dev zeroed in alloc_etherdev */
  1106. dev = alloc_etherdev(sizeof (*tp));
  1107. if (dev == NULL) {
  1108. if (netif_msg_drv(&debug))
  1109. printk(KERN_ERR PFX "unable to alloc new ethernet\n");
  1110. goto err_out;
  1111. }
  1112. SET_MODULE_OWNER(dev);
  1113. SET_NETDEV_DEV(dev, &pdev->dev);
  1114. tp = netdev_priv(dev);
  1115. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  1116. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  1117. rc = pci_enable_device(pdev);
  1118. if (rc < 0) {
  1119. if (netif_msg_probe(tp)) {
  1120. printk(KERN_ERR PFX "%s: enable failure\n",
  1121. pci_name(pdev));
  1122. }
  1123. goto err_out_free_dev;
  1124. }
  1125. rc = pci_set_mwi(pdev);
  1126. if (rc < 0)
  1127. goto err_out_disable;
  1128. /* save power state before pci_enable_device overwrites it */
  1129. pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  1130. if (pm_cap) {
  1131. u16 pwr_command;
  1132. pci_read_config_word(pdev, pm_cap + PCI_PM_CTRL, &pwr_command);
  1133. acpi_idle_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
  1134. } else {
  1135. if (netif_msg_probe(tp)) {
  1136. printk(KERN_ERR PFX
  1137. "Cannot find PowerManagement capability. "
  1138. "Aborting.\n");
  1139. }
  1140. goto err_out_mwi;
  1141. }
  1142. /* make sure PCI base addr 1 is MMIO */
  1143. if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  1144. if (netif_msg_probe(tp)) {
  1145. printk(KERN_ERR PFX
  1146. "region #1 not an MMIO resource, aborting\n");
  1147. }
  1148. rc = -ENODEV;
  1149. goto err_out_mwi;
  1150. }
  1151. /* check for weird/broken PCI region reporting */
  1152. if (pci_resource_len(pdev, 1) < R8169_REGS_SIZE) {
  1153. if (netif_msg_probe(tp)) {
  1154. printk(KERN_ERR PFX
  1155. "Invalid PCI region size(s), aborting\n");
  1156. }
  1157. rc = -ENODEV;
  1158. goto err_out_mwi;
  1159. }
  1160. rc = pci_request_regions(pdev, MODULENAME);
  1161. if (rc < 0) {
  1162. if (netif_msg_probe(tp)) {
  1163. printk(KERN_ERR PFX "%s: could not request regions.\n",
  1164. pci_name(pdev));
  1165. }
  1166. goto err_out_mwi;
  1167. }
  1168. tp->cp_cmd = PCIMulRW | RxChkSum;
  1169. if ((sizeof(dma_addr_t) > 4) &&
  1170. !pci_set_dma_mask(pdev, DMA_64BIT_MASK) && use_dac) {
  1171. tp->cp_cmd |= PCIDAC;
  1172. dev->features |= NETIF_F_HIGHDMA;
  1173. } else {
  1174. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1175. if (rc < 0) {
  1176. if (netif_msg_probe(tp)) {
  1177. printk(KERN_ERR PFX
  1178. "DMA configuration failed.\n");
  1179. }
  1180. goto err_out_free_res;
  1181. }
  1182. }
  1183. pci_set_master(pdev);
  1184. /* ioremap MMIO region */
  1185. ioaddr = ioremap(pci_resource_start(pdev, 1), R8169_REGS_SIZE);
  1186. if (ioaddr == NULL) {
  1187. if (netif_msg_probe(tp))
  1188. printk(KERN_ERR PFX "cannot remap MMIO, aborting\n");
  1189. rc = -EIO;
  1190. goto err_out_free_res;
  1191. }
  1192. /* Unneeded ? Don't mess with Mrs. Murphy. */
  1193. rtl8169_irq_mask_and_ack(ioaddr);
  1194. /* Soft reset the chip. */
  1195. RTL_W8(ChipCmd, CmdReset);
  1196. /* Check that the chip has finished the reset. */
  1197. for (i = 1000; i > 0; i--) {
  1198. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1199. break;
  1200. udelay(10);
  1201. }
  1202. /* Identify chip attached to board */
  1203. rtl8169_get_mac_version(tp, ioaddr);
  1204. rtl8169_get_phy_version(tp, ioaddr);
  1205. rtl8169_print_mac_version(tp);
  1206. rtl8169_print_phy_version(tp);
  1207. for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--) {
  1208. if (tp->mac_version == rtl_chip_info[i].mac_version)
  1209. break;
  1210. }
  1211. if (i < 0) {
  1212. /* Unknown chip: assume array element #0, original RTL-8169 */
  1213. if (netif_msg_probe(tp)) {
  1214. printk(KERN_DEBUG PFX "PCI device %s: "
  1215. "unknown chip version, assuming %s\n",
  1216. pci_name(pdev), rtl_chip_info[0].name);
  1217. }
  1218. i++;
  1219. }
  1220. tp->chipset = i;
  1221. *ioaddr_out = ioaddr;
  1222. *dev_out = dev;
  1223. out:
  1224. return rc;
  1225. err_out_free_res:
  1226. pci_release_regions(pdev);
  1227. err_out_mwi:
  1228. pci_clear_mwi(pdev);
  1229. err_out_disable:
  1230. pci_disable_device(pdev);
  1231. err_out_free_dev:
  1232. free_netdev(dev);
  1233. err_out:
  1234. *ioaddr_out = NULL;
  1235. *dev_out = NULL;
  1236. goto out;
  1237. }
  1238. static int __devinit
  1239. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1240. {
  1241. struct net_device *dev = NULL;
  1242. struct rtl8169_private *tp;
  1243. void __iomem *ioaddr = NULL;
  1244. static int board_idx = -1;
  1245. u8 autoneg, duplex;
  1246. u16 speed;
  1247. int i, rc;
  1248. assert(pdev != NULL);
  1249. assert(ent != NULL);
  1250. board_idx++;
  1251. if (netif_msg_drv(&debug)) {
  1252. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  1253. MODULENAME, RTL8169_VERSION);
  1254. }
  1255. rc = rtl8169_init_board(pdev, &dev, &ioaddr);
  1256. if (rc)
  1257. return rc;
  1258. tp = netdev_priv(dev);
  1259. assert(ioaddr != NULL);
  1260. if (RTL_R8(PHYstatus) & TBI_Enable) {
  1261. tp->set_speed = rtl8169_set_speed_tbi;
  1262. tp->get_settings = rtl8169_gset_tbi;
  1263. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  1264. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  1265. tp->link_ok = rtl8169_tbi_link_ok;
  1266. tp->phy_1000_ctrl_reg = PHY_Cap_1000_Full; /* Implied by TBI */
  1267. } else {
  1268. tp->set_speed = rtl8169_set_speed_xmii;
  1269. tp->get_settings = rtl8169_gset_xmii;
  1270. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  1271. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  1272. tp->link_ok = rtl8169_xmii_link_ok;
  1273. }
  1274. /* Get MAC address. FIXME: read EEPROM */
  1275. for (i = 0; i < MAC_ADDR_LEN; i++)
  1276. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  1277. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1278. dev->open = rtl8169_open;
  1279. dev->hard_start_xmit = rtl8169_start_xmit;
  1280. dev->get_stats = rtl8169_get_stats;
  1281. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  1282. dev->stop = rtl8169_close;
  1283. dev->tx_timeout = rtl8169_tx_timeout;
  1284. dev->set_multicast_list = rtl8169_set_rx_mode;
  1285. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  1286. dev->irq = pdev->irq;
  1287. dev->base_addr = (unsigned long) ioaddr;
  1288. dev->change_mtu = rtl8169_change_mtu;
  1289. #ifdef CONFIG_R8169_NAPI
  1290. dev->poll = rtl8169_poll;
  1291. dev->weight = R8169_NAPI_WEIGHT;
  1292. #endif
  1293. #ifdef CONFIG_R8169_VLAN
  1294. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1295. dev->vlan_rx_register = rtl8169_vlan_rx_register;
  1296. dev->vlan_rx_kill_vid = rtl8169_vlan_rx_kill_vid;
  1297. #endif
  1298. #ifdef CONFIG_NET_POLL_CONTROLLER
  1299. dev->poll_controller = rtl8169_netpoll;
  1300. #endif
  1301. tp->intr_mask = 0xffff;
  1302. tp->pci_dev = pdev;
  1303. tp->mmio_addr = ioaddr;
  1304. spin_lock_init(&tp->lock);
  1305. rc = register_netdev(dev);
  1306. if (rc) {
  1307. rtl8169_release_board(pdev, dev, ioaddr);
  1308. return rc;
  1309. }
  1310. if (netif_msg_probe(tp)) {
  1311. printk(KERN_DEBUG "%s: Identified chip type is '%s'.\n",
  1312. dev->name, rtl_chip_info[tp->chipset].name);
  1313. }
  1314. pci_set_drvdata(pdev, dev);
  1315. if (netif_msg_probe(tp)) {
  1316. printk(KERN_INFO "%s: %s at 0x%lx, "
  1317. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
  1318. "IRQ %d\n",
  1319. dev->name,
  1320. rtl_chip_info[ent->driver_data].name,
  1321. dev->base_addr,
  1322. dev->dev_addr[0], dev->dev_addr[1],
  1323. dev->dev_addr[2], dev->dev_addr[3],
  1324. dev->dev_addr[4], dev->dev_addr[5], dev->irq);
  1325. }
  1326. rtl8169_hw_phy_config(dev);
  1327. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1328. RTL_W8(0x82, 0x01);
  1329. if (tp->mac_version < RTL_GIGA_MAC_VER_E) {
  1330. dprintk("Set PCI Latency=0x40\n");
  1331. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x40);
  1332. }
  1333. if (tp->mac_version == RTL_GIGA_MAC_VER_D) {
  1334. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1335. RTL_W8(0x82, 0x01);
  1336. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  1337. mdio_write(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
  1338. }
  1339. rtl8169_link_option(board_idx, &autoneg, &speed, &duplex);
  1340. rtl8169_set_speed(dev, autoneg, speed, duplex);
  1341. if ((RTL_R8(PHYstatus) & TBI_Enable) && netif_msg_link(tp))
  1342. printk(KERN_INFO PFX "%s: TBI auto-negotiating\n", dev->name);
  1343. return 0;
  1344. }
  1345. static void __devexit
  1346. rtl8169_remove_one(struct pci_dev *pdev)
  1347. {
  1348. struct net_device *dev = pci_get_drvdata(pdev);
  1349. struct rtl8169_private *tp = netdev_priv(dev);
  1350. assert(dev != NULL);
  1351. assert(tp != NULL);
  1352. unregister_netdev(dev);
  1353. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  1354. pci_set_drvdata(pdev, NULL);
  1355. }
  1356. #ifdef CONFIG_PM
  1357. static int rtl8169_suspend(struct pci_dev *pdev, pm_message_t state)
  1358. {
  1359. struct net_device *dev = pci_get_drvdata(pdev);
  1360. struct rtl8169_private *tp = netdev_priv(dev);
  1361. void __iomem *ioaddr = tp->mmio_addr;
  1362. unsigned long flags;
  1363. if (!netif_running(dev))
  1364. return 0;
  1365. netif_device_detach(dev);
  1366. netif_stop_queue(dev);
  1367. spin_lock_irqsave(&tp->lock, flags);
  1368. /* Disable interrupts, stop Rx and Tx */
  1369. RTL_W16(IntrMask, 0);
  1370. RTL_W8(ChipCmd, 0);
  1371. /* Update the error counts. */
  1372. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  1373. RTL_W32(RxMissed, 0);
  1374. spin_unlock_irqrestore(&tp->lock, flags);
  1375. return 0;
  1376. }
  1377. static int rtl8169_resume(struct pci_dev *pdev)
  1378. {
  1379. struct net_device *dev = pci_get_drvdata(pdev);
  1380. if (!netif_running(dev))
  1381. return 0;
  1382. netif_device_attach(dev);
  1383. rtl8169_hw_start(dev);
  1384. return 0;
  1385. }
  1386. #endif /* CONFIG_PM */
  1387. static void rtl8169_set_rxbufsize(struct rtl8169_private *tp,
  1388. struct net_device *dev)
  1389. {
  1390. unsigned int mtu = dev->mtu;
  1391. tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
  1392. }
  1393. static int rtl8169_open(struct net_device *dev)
  1394. {
  1395. struct rtl8169_private *tp = netdev_priv(dev);
  1396. struct pci_dev *pdev = tp->pci_dev;
  1397. int retval;
  1398. rtl8169_set_rxbufsize(tp, dev);
  1399. retval =
  1400. request_irq(dev->irq, rtl8169_interrupt, SA_SHIRQ, dev->name, dev);
  1401. if (retval < 0)
  1402. goto out;
  1403. retval = -ENOMEM;
  1404. /*
  1405. * Rx and Tx desscriptors needs 256 bytes alignment.
  1406. * pci_alloc_consistent provides more.
  1407. */
  1408. tp->TxDescArray = pci_alloc_consistent(pdev, R8169_TX_RING_BYTES,
  1409. &tp->TxPhyAddr);
  1410. if (!tp->TxDescArray)
  1411. goto err_free_irq;
  1412. tp->RxDescArray = pci_alloc_consistent(pdev, R8169_RX_RING_BYTES,
  1413. &tp->RxPhyAddr);
  1414. if (!tp->RxDescArray)
  1415. goto err_free_tx;
  1416. retval = rtl8169_init_ring(dev);
  1417. if (retval < 0)
  1418. goto err_free_rx;
  1419. INIT_WORK(&tp->task, NULL, dev);
  1420. rtl8169_hw_start(dev);
  1421. rtl8169_request_timer(dev);
  1422. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  1423. out:
  1424. return retval;
  1425. err_free_rx:
  1426. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  1427. tp->RxPhyAddr);
  1428. err_free_tx:
  1429. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  1430. tp->TxPhyAddr);
  1431. err_free_irq:
  1432. free_irq(dev->irq, dev);
  1433. goto out;
  1434. }
  1435. static void rtl8169_hw_reset(void __iomem *ioaddr)
  1436. {
  1437. /* Disable interrupts */
  1438. rtl8169_irq_mask_and_ack(ioaddr);
  1439. /* Reset the chipset */
  1440. RTL_W8(ChipCmd, CmdReset);
  1441. /* PCI commit */
  1442. RTL_R8(ChipCmd);
  1443. }
  1444. static void
  1445. rtl8169_hw_start(struct net_device *dev)
  1446. {
  1447. struct rtl8169_private *tp = netdev_priv(dev);
  1448. void __iomem *ioaddr = tp->mmio_addr;
  1449. u32 i;
  1450. /* Soft reset the chip. */
  1451. RTL_W8(ChipCmd, CmdReset);
  1452. /* Check that the chip has finished the reset. */
  1453. for (i = 1000; i > 0; i--) {
  1454. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1455. break;
  1456. udelay(10);
  1457. }
  1458. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1459. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1460. RTL_W8(EarlyTxThres, EarlyTxThld);
  1461. /* Low hurts. Let's disable the filtering. */
  1462. RTL_W16(RxMaxSize, 16383);
  1463. /* Set Rx Config register */
  1464. i = rtl8169_rx_config |
  1465. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  1466. RTL_W32(RxConfig, i);
  1467. /* Set DMA burst size and Interframe Gap Time */
  1468. RTL_W32(TxConfig,
  1469. (TX_DMA_BURST << TxDMAShift) | (InterFrameGap <<
  1470. TxInterFrameGapShift));
  1471. tp->cp_cmd |= RTL_R16(CPlusCmd);
  1472. RTL_W16(CPlusCmd, tp->cp_cmd);
  1473. if ((tp->mac_version == RTL_GIGA_MAC_VER_D) ||
  1474. (tp->mac_version == RTL_GIGA_MAC_VER_E)) {
  1475. dprintk(KERN_INFO PFX "Set MAC Reg C+CR Offset 0xE0. "
  1476. "Bit-3 and bit-14 MUST be 1\n");
  1477. tp->cp_cmd |= (1 << 14) | PCIMulRW;
  1478. RTL_W16(CPlusCmd, tp->cp_cmd);
  1479. }
  1480. /*
  1481. * Undocumented corner. Supposedly:
  1482. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  1483. */
  1484. RTL_W16(IntrMitigate, 0x0000);
  1485. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr & DMA_32BIT_MASK));
  1486. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr >> 32));
  1487. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr & DMA_32BIT_MASK));
  1488. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr >> 32));
  1489. RTL_W8(Cfg9346, Cfg9346_Lock);
  1490. udelay(10);
  1491. RTL_W32(RxMissed, 0);
  1492. rtl8169_set_rx_mode(dev);
  1493. /* no early-rx interrupts */
  1494. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  1495. /* Enable all known interrupts by setting the interrupt mask. */
  1496. RTL_W16(IntrMask, rtl8169_intr_mask);
  1497. netif_start_queue(dev);
  1498. }
  1499. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  1500. {
  1501. struct rtl8169_private *tp = netdev_priv(dev);
  1502. int ret = 0;
  1503. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  1504. return -EINVAL;
  1505. dev->mtu = new_mtu;
  1506. if (!netif_running(dev))
  1507. goto out;
  1508. rtl8169_down(dev);
  1509. rtl8169_set_rxbufsize(tp, dev);
  1510. ret = rtl8169_init_ring(dev);
  1511. if (ret < 0)
  1512. goto out;
  1513. netif_poll_enable(dev);
  1514. rtl8169_hw_start(dev);
  1515. rtl8169_request_timer(dev);
  1516. out:
  1517. return ret;
  1518. }
  1519. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  1520. {
  1521. desc->addr = 0x0badbadbadbadbadull;
  1522. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  1523. }
  1524. static void rtl8169_free_rx_skb(struct rtl8169_private *tp,
  1525. struct sk_buff **sk_buff, struct RxDesc *desc)
  1526. {
  1527. struct pci_dev *pdev = tp->pci_dev;
  1528. pci_unmap_single(pdev, le64_to_cpu(desc->addr), tp->rx_buf_sz,
  1529. PCI_DMA_FROMDEVICE);
  1530. dev_kfree_skb(*sk_buff);
  1531. *sk_buff = NULL;
  1532. rtl8169_make_unusable_by_asic(desc);
  1533. }
  1534. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  1535. {
  1536. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  1537. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  1538. }
  1539. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  1540. u32 rx_buf_sz)
  1541. {
  1542. desc->addr = cpu_to_le64(mapping);
  1543. wmb();
  1544. rtl8169_mark_to_asic(desc, rx_buf_sz);
  1545. }
  1546. static int rtl8169_alloc_rx_skb(struct pci_dev *pdev, struct sk_buff **sk_buff,
  1547. struct RxDesc *desc, int rx_buf_sz)
  1548. {
  1549. struct sk_buff *skb;
  1550. dma_addr_t mapping;
  1551. int ret = 0;
  1552. skb = dev_alloc_skb(rx_buf_sz + NET_IP_ALIGN);
  1553. if (!skb)
  1554. goto err_out;
  1555. skb_reserve(skb, NET_IP_ALIGN);
  1556. *sk_buff = skb;
  1557. mapping = pci_map_single(pdev, skb->data, rx_buf_sz,
  1558. PCI_DMA_FROMDEVICE);
  1559. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  1560. out:
  1561. return ret;
  1562. err_out:
  1563. ret = -ENOMEM;
  1564. rtl8169_make_unusable_by_asic(desc);
  1565. goto out;
  1566. }
  1567. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  1568. {
  1569. int i;
  1570. for (i = 0; i < NUM_RX_DESC; i++) {
  1571. if (tp->Rx_skbuff[i]) {
  1572. rtl8169_free_rx_skb(tp, tp->Rx_skbuff + i,
  1573. tp->RxDescArray + i);
  1574. }
  1575. }
  1576. }
  1577. static u32 rtl8169_rx_fill(struct rtl8169_private *tp, struct net_device *dev,
  1578. u32 start, u32 end)
  1579. {
  1580. u32 cur;
  1581. for (cur = start; end - cur > 0; cur++) {
  1582. int ret, i = cur % NUM_RX_DESC;
  1583. if (tp->Rx_skbuff[i])
  1584. continue;
  1585. ret = rtl8169_alloc_rx_skb(tp->pci_dev, tp->Rx_skbuff + i,
  1586. tp->RxDescArray + i, tp->rx_buf_sz);
  1587. if (ret < 0)
  1588. break;
  1589. }
  1590. return cur - start;
  1591. }
  1592. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  1593. {
  1594. desc->opts1 |= cpu_to_le32(RingEnd);
  1595. }
  1596. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  1597. {
  1598. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  1599. }
  1600. static int rtl8169_init_ring(struct net_device *dev)
  1601. {
  1602. struct rtl8169_private *tp = netdev_priv(dev);
  1603. rtl8169_init_ring_indexes(tp);
  1604. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  1605. memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));
  1606. if (rtl8169_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
  1607. goto err_out;
  1608. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  1609. return 0;
  1610. err_out:
  1611. rtl8169_rx_clear(tp);
  1612. return -ENOMEM;
  1613. }
  1614. static void rtl8169_unmap_tx_skb(struct pci_dev *pdev, struct ring_info *tx_skb,
  1615. struct TxDesc *desc)
  1616. {
  1617. unsigned int len = tx_skb->len;
  1618. pci_unmap_single(pdev, le64_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);
  1619. desc->opts1 = 0x00;
  1620. desc->opts2 = 0x00;
  1621. desc->addr = 0x00;
  1622. tx_skb->len = 0;
  1623. }
  1624. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  1625. {
  1626. unsigned int i;
  1627. for (i = tp->dirty_tx; i < tp->dirty_tx + NUM_TX_DESC; i++) {
  1628. unsigned int entry = i % NUM_TX_DESC;
  1629. struct ring_info *tx_skb = tp->tx_skb + entry;
  1630. unsigned int len = tx_skb->len;
  1631. if (len) {
  1632. struct sk_buff *skb = tx_skb->skb;
  1633. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb,
  1634. tp->TxDescArray + entry);
  1635. if (skb) {
  1636. dev_kfree_skb(skb);
  1637. tx_skb->skb = NULL;
  1638. }
  1639. tp->stats.tx_dropped++;
  1640. }
  1641. }
  1642. tp->cur_tx = tp->dirty_tx = 0;
  1643. }
  1644. static void rtl8169_schedule_work(struct net_device *dev, void (*task)(void *))
  1645. {
  1646. struct rtl8169_private *tp = netdev_priv(dev);
  1647. PREPARE_WORK(&tp->task, task, dev);
  1648. schedule_delayed_work(&tp->task, 4);
  1649. }
  1650. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  1651. {
  1652. struct rtl8169_private *tp = netdev_priv(dev);
  1653. void __iomem *ioaddr = tp->mmio_addr;
  1654. synchronize_irq(dev->irq);
  1655. /* Wait for any pending NAPI task to complete */
  1656. netif_poll_disable(dev);
  1657. rtl8169_irq_mask_and_ack(ioaddr);
  1658. netif_poll_enable(dev);
  1659. }
  1660. static void rtl8169_reinit_task(void *_data)
  1661. {
  1662. struct net_device *dev = _data;
  1663. int ret;
  1664. if (netif_running(dev)) {
  1665. rtl8169_wait_for_quiescence(dev);
  1666. rtl8169_close(dev);
  1667. }
  1668. ret = rtl8169_open(dev);
  1669. if (unlikely(ret < 0)) {
  1670. if (net_ratelimit()) {
  1671. struct rtl8169_private *tp = netdev_priv(dev);
  1672. if (netif_msg_drv(tp)) {
  1673. printk(PFX KERN_ERR
  1674. "%s: reinit failure (status = %d)."
  1675. " Rescheduling.\n", dev->name, ret);
  1676. }
  1677. }
  1678. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  1679. }
  1680. }
  1681. static void rtl8169_reset_task(void *_data)
  1682. {
  1683. struct net_device *dev = _data;
  1684. struct rtl8169_private *tp = netdev_priv(dev);
  1685. if (!netif_running(dev))
  1686. return;
  1687. rtl8169_wait_for_quiescence(dev);
  1688. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr);
  1689. rtl8169_tx_clear(tp);
  1690. if (tp->dirty_rx == tp->cur_rx) {
  1691. rtl8169_init_ring_indexes(tp);
  1692. rtl8169_hw_start(dev);
  1693. netif_wake_queue(dev);
  1694. } else {
  1695. if (net_ratelimit()) {
  1696. struct rtl8169_private *tp = netdev_priv(dev);
  1697. if (netif_msg_intr(tp)) {
  1698. printk(PFX KERN_EMERG
  1699. "%s: Rx buffers shortage\n", dev->name);
  1700. }
  1701. }
  1702. rtl8169_schedule_work(dev, rtl8169_reset_task);
  1703. }
  1704. }
  1705. static void rtl8169_tx_timeout(struct net_device *dev)
  1706. {
  1707. struct rtl8169_private *tp = netdev_priv(dev);
  1708. rtl8169_hw_reset(tp->mmio_addr);
  1709. /* Let's wait a bit while any (async) irq lands on */
  1710. rtl8169_schedule_work(dev, rtl8169_reset_task);
  1711. }
  1712. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  1713. u32 opts1)
  1714. {
  1715. struct skb_shared_info *info = skb_shinfo(skb);
  1716. unsigned int cur_frag, entry;
  1717. struct TxDesc *txd;
  1718. entry = tp->cur_tx;
  1719. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  1720. skb_frag_t *frag = info->frags + cur_frag;
  1721. dma_addr_t mapping;
  1722. u32 status, len;
  1723. void *addr;
  1724. entry = (entry + 1) % NUM_TX_DESC;
  1725. txd = tp->TxDescArray + entry;
  1726. len = frag->size;
  1727. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  1728. mapping = pci_map_single(tp->pci_dev, addr, len, PCI_DMA_TODEVICE);
  1729. /* anti gcc 2.95.3 bugware (sic) */
  1730. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  1731. txd->opts1 = cpu_to_le32(status);
  1732. txd->addr = cpu_to_le64(mapping);
  1733. tp->tx_skb[entry].len = len;
  1734. }
  1735. if (cur_frag) {
  1736. tp->tx_skb[entry].skb = skb;
  1737. txd->opts1 |= cpu_to_le32(LastFrag);
  1738. }
  1739. return cur_frag;
  1740. }
  1741. static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
  1742. {
  1743. if (dev->features & NETIF_F_TSO) {
  1744. u32 mss = skb_shinfo(skb)->tso_size;
  1745. if (mss)
  1746. return LargeSend | ((mss & MSSMask) << MSSShift);
  1747. }
  1748. if (skb->ip_summed == CHECKSUM_HW) {
  1749. const struct iphdr *ip = skb->nh.iph;
  1750. if (ip->protocol == IPPROTO_TCP)
  1751. return IPCS | TCPCS;
  1752. else if (ip->protocol == IPPROTO_UDP)
  1753. return IPCS | UDPCS;
  1754. WARN_ON(1); /* we need a WARN() */
  1755. }
  1756. return 0;
  1757. }
  1758. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1759. {
  1760. struct rtl8169_private *tp = netdev_priv(dev);
  1761. unsigned int frags, entry = tp->cur_tx % NUM_TX_DESC;
  1762. struct TxDesc *txd = tp->TxDescArray + entry;
  1763. void __iomem *ioaddr = tp->mmio_addr;
  1764. dma_addr_t mapping;
  1765. u32 status, len;
  1766. u32 opts1;
  1767. int ret = 0;
  1768. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  1769. if (netif_msg_drv(tp)) {
  1770. printk(KERN_ERR
  1771. "%s: BUG! Tx Ring full when queue awake!\n",
  1772. dev->name);
  1773. }
  1774. goto err_stop;
  1775. }
  1776. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  1777. goto err_stop;
  1778. opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
  1779. frags = rtl8169_xmit_frags(tp, skb, opts1);
  1780. if (frags) {
  1781. len = skb_headlen(skb);
  1782. opts1 |= FirstFrag;
  1783. } else {
  1784. len = skb->len;
  1785. if (unlikely(len < ETH_ZLEN)) {
  1786. skb = skb_padto(skb, ETH_ZLEN);
  1787. if (!skb)
  1788. goto err_update_stats;
  1789. len = ETH_ZLEN;
  1790. }
  1791. opts1 |= FirstFrag | LastFrag;
  1792. tp->tx_skb[entry].skb = skb;
  1793. }
  1794. mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  1795. tp->tx_skb[entry].len = len;
  1796. txd->addr = cpu_to_le64(mapping);
  1797. txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  1798. wmb();
  1799. /* anti gcc 2.95.3 bugware (sic) */
  1800. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  1801. txd->opts1 = cpu_to_le32(status);
  1802. dev->trans_start = jiffies;
  1803. tp->cur_tx += frags + 1;
  1804. smp_wmb();
  1805. RTL_W8(TxPoll, 0x40); /* set polling bit */
  1806. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  1807. netif_stop_queue(dev);
  1808. smp_rmb();
  1809. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  1810. netif_wake_queue(dev);
  1811. }
  1812. out:
  1813. return ret;
  1814. err_stop:
  1815. netif_stop_queue(dev);
  1816. ret = 1;
  1817. err_update_stats:
  1818. tp->stats.tx_dropped++;
  1819. goto out;
  1820. }
  1821. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  1822. {
  1823. struct rtl8169_private *tp = netdev_priv(dev);
  1824. struct pci_dev *pdev = tp->pci_dev;
  1825. void __iomem *ioaddr = tp->mmio_addr;
  1826. u16 pci_status, pci_cmd;
  1827. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1828. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  1829. if (netif_msg_intr(tp)) {
  1830. printk(KERN_ERR
  1831. "%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
  1832. dev->name, pci_cmd, pci_status);
  1833. }
  1834. /*
  1835. * The recovery sequence below admits a very elaborated explanation:
  1836. * - it seems to work;
  1837. * - I did not see what else could be done.
  1838. *
  1839. * Feel free to adjust to your needs.
  1840. */
  1841. pci_write_config_word(pdev, PCI_COMMAND,
  1842. pci_cmd | PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
  1843. pci_write_config_word(pdev, PCI_STATUS,
  1844. pci_status & (PCI_STATUS_DETECTED_PARITY |
  1845. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  1846. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  1847. /* The infamous DAC f*ckup only happens at boot time */
  1848. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  1849. if (netif_msg_intr(tp))
  1850. printk(KERN_INFO "%s: disabling PCI DAC.\n", dev->name);
  1851. tp->cp_cmd &= ~PCIDAC;
  1852. RTL_W16(CPlusCmd, tp->cp_cmd);
  1853. dev->features &= ~NETIF_F_HIGHDMA;
  1854. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  1855. }
  1856. rtl8169_hw_reset(ioaddr);
  1857. }
  1858. static void
  1859. rtl8169_tx_interrupt(struct net_device *dev, struct rtl8169_private *tp,
  1860. void __iomem *ioaddr)
  1861. {
  1862. unsigned int dirty_tx, tx_left;
  1863. assert(dev != NULL);
  1864. assert(tp != NULL);
  1865. assert(ioaddr != NULL);
  1866. dirty_tx = tp->dirty_tx;
  1867. smp_rmb();
  1868. tx_left = tp->cur_tx - dirty_tx;
  1869. while (tx_left > 0) {
  1870. unsigned int entry = dirty_tx % NUM_TX_DESC;
  1871. struct ring_info *tx_skb = tp->tx_skb + entry;
  1872. u32 len = tx_skb->len;
  1873. u32 status;
  1874. rmb();
  1875. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  1876. if (status & DescOwn)
  1877. break;
  1878. tp->stats.tx_bytes += len;
  1879. tp->stats.tx_packets++;
  1880. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb, tp->TxDescArray + entry);
  1881. if (status & LastFrag) {
  1882. dev_kfree_skb_irq(tx_skb->skb);
  1883. tx_skb->skb = NULL;
  1884. }
  1885. dirty_tx++;
  1886. tx_left--;
  1887. }
  1888. if (tp->dirty_tx != dirty_tx) {
  1889. tp->dirty_tx = dirty_tx;
  1890. smp_wmb();
  1891. if (netif_queue_stopped(dev) &&
  1892. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  1893. netif_wake_queue(dev);
  1894. }
  1895. }
  1896. }
  1897. static inline int rtl8169_fragmented_frame(u32 status)
  1898. {
  1899. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  1900. }
  1901. static inline void rtl8169_rx_csum(struct sk_buff *skb, struct RxDesc *desc)
  1902. {
  1903. u32 opts1 = le32_to_cpu(desc->opts1);
  1904. u32 status = opts1 & RxProtoMask;
  1905. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  1906. ((status == RxProtoUDP) && !(opts1 & UDPFail)) ||
  1907. ((status == RxProtoIP) && !(opts1 & IPFail)))
  1908. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1909. else
  1910. skb->ip_summed = CHECKSUM_NONE;
  1911. }
  1912. static inline int rtl8169_try_rx_copy(struct sk_buff **sk_buff, int pkt_size,
  1913. struct RxDesc *desc, int rx_buf_sz)
  1914. {
  1915. int ret = -1;
  1916. if (pkt_size < rx_copybreak) {
  1917. struct sk_buff *skb;
  1918. skb = dev_alloc_skb(pkt_size + NET_IP_ALIGN);
  1919. if (skb) {
  1920. skb_reserve(skb, NET_IP_ALIGN);
  1921. eth_copy_and_sum(skb, sk_buff[0]->data, pkt_size, 0);
  1922. *sk_buff = skb;
  1923. rtl8169_mark_to_asic(desc, rx_buf_sz);
  1924. ret = 0;
  1925. }
  1926. }
  1927. return ret;
  1928. }
  1929. static int
  1930. rtl8169_rx_interrupt(struct net_device *dev, struct rtl8169_private *tp,
  1931. void __iomem *ioaddr)
  1932. {
  1933. unsigned int cur_rx, rx_left;
  1934. unsigned int delta, count;
  1935. assert(dev != NULL);
  1936. assert(tp != NULL);
  1937. assert(ioaddr != NULL);
  1938. cur_rx = tp->cur_rx;
  1939. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  1940. rx_left = rtl8169_rx_quota(rx_left, (u32) dev->quota);
  1941. for (; rx_left > 0; rx_left--, cur_rx++) {
  1942. unsigned int entry = cur_rx % NUM_RX_DESC;
  1943. struct RxDesc *desc = tp->RxDescArray + entry;
  1944. u32 status;
  1945. rmb();
  1946. status = le32_to_cpu(desc->opts1);
  1947. if (status & DescOwn)
  1948. break;
  1949. if (unlikely(status & RxRES)) {
  1950. if (netif_msg_rx_err(tp)) {
  1951. printk(KERN_INFO
  1952. "%s: Rx ERROR. status = %08x\n",
  1953. dev->name, status);
  1954. }
  1955. tp->stats.rx_errors++;
  1956. if (status & (RxRWT | RxRUNT))
  1957. tp->stats.rx_length_errors++;
  1958. if (status & RxCRC)
  1959. tp->stats.rx_crc_errors++;
  1960. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  1961. } else {
  1962. struct sk_buff *skb = tp->Rx_skbuff[entry];
  1963. int pkt_size = (status & 0x00001FFF) - 4;
  1964. void (*pci_action)(struct pci_dev *, dma_addr_t,
  1965. size_t, int) = pci_dma_sync_single_for_device;
  1966. /*
  1967. * The driver does not support incoming fragmented
  1968. * frames. They are seen as a symptom of over-mtu
  1969. * sized frames.
  1970. */
  1971. if (unlikely(rtl8169_fragmented_frame(status))) {
  1972. tp->stats.rx_dropped++;
  1973. tp->stats.rx_length_errors++;
  1974. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  1975. continue;
  1976. }
  1977. rtl8169_rx_csum(skb, desc);
  1978. pci_dma_sync_single_for_cpu(tp->pci_dev,
  1979. le64_to_cpu(desc->addr), tp->rx_buf_sz,
  1980. PCI_DMA_FROMDEVICE);
  1981. if (rtl8169_try_rx_copy(&skb, pkt_size, desc,
  1982. tp->rx_buf_sz)) {
  1983. pci_action = pci_unmap_single;
  1984. tp->Rx_skbuff[entry] = NULL;
  1985. }
  1986. pci_action(tp->pci_dev, le64_to_cpu(desc->addr),
  1987. tp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  1988. skb->dev = dev;
  1989. skb_put(skb, pkt_size);
  1990. skb->protocol = eth_type_trans(skb, dev);
  1991. if (rtl8169_rx_vlan_skb(tp, desc, skb) < 0)
  1992. rtl8169_rx_skb(skb);
  1993. dev->last_rx = jiffies;
  1994. tp->stats.rx_bytes += pkt_size;
  1995. tp->stats.rx_packets++;
  1996. }
  1997. }
  1998. count = cur_rx - tp->cur_rx;
  1999. tp->cur_rx = cur_rx;
  2000. delta = rtl8169_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
  2001. if (!delta && count && netif_msg_intr(tp))
  2002. printk(KERN_INFO "%s: no Rx buffer allocated\n", dev->name);
  2003. tp->dirty_rx += delta;
  2004. /*
  2005. * FIXME: until there is periodic timer to try and refill the ring,
  2006. * a temporary shortage may definitely kill the Rx process.
  2007. * - disable the asic to try and avoid an overflow and kick it again
  2008. * after refill ?
  2009. * - how do others driver handle this condition (Uh oh...).
  2010. */
  2011. if ((tp->dirty_rx + NUM_RX_DESC == tp->cur_rx) && netif_msg_intr(tp))
  2012. printk(KERN_EMERG "%s: Rx buffers exhausted\n", dev->name);
  2013. return count;
  2014. }
  2015. /* The interrupt handler does all of the Rx thread work and cleans up after the Tx thread. */
  2016. static irqreturn_t
  2017. rtl8169_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  2018. {
  2019. struct net_device *dev = (struct net_device *) dev_instance;
  2020. struct rtl8169_private *tp = netdev_priv(dev);
  2021. int boguscnt = max_interrupt_work;
  2022. void __iomem *ioaddr = tp->mmio_addr;
  2023. int status;
  2024. int handled = 0;
  2025. do {
  2026. status = RTL_R16(IntrStatus);
  2027. /* hotplug/major error/no more work/shared irq */
  2028. if ((status == 0xFFFF) || !status)
  2029. break;
  2030. handled = 1;
  2031. if (unlikely(!netif_running(dev))) {
  2032. rtl8169_asic_down(ioaddr);
  2033. goto out;
  2034. }
  2035. status &= tp->intr_mask;
  2036. RTL_W16(IntrStatus,
  2037. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  2038. if (!(status & rtl8169_intr_mask))
  2039. break;
  2040. if (unlikely(status & SYSErr)) {
  2041. rtl8169_pcierr_interrupt(dev);
  2042. break;
  2043. }
  2044. if (status & LinkChg)
  2045. rtl8169_check_link_status(dev, tp, ioaddr);
  2046. #ifdef CONFIG_R8169_NAPI
  2047. RTL_W16(IntrMask, rtl8169_intr_mask & ~rtl8169_napi_event);
  2048. tp->intr_mask = ~rtl8169_napi_event;
  2049. if (likely(netif_rx_schedule_prep(dev)))
  2050. __netif_rx_schedule(dev);
  2051. else if (netif_msg_intr(tp)) {
  2052. printk(KERN_INFO "%s: interrupt %04x taken in poll\n",
  2053. dev->name, status);
  2054. }
  2055. break;
  2056. #else
  2057. /* Rx interrupt */
  2058. if (status & (RxOK | RxOverflow | RxFIFOOver)) {
  2059. rtl8169_rx_interrupt(dev, tp, ioaddr);
  2060. }
  2061. /* Tx interrupt */
  2062. if (status & (TxOK | TxErr))
  2063. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2064. #endif
  2065. boguscnt--;
  2066. } while (boguscnt > 0);
  2067. if (boguscnt <= 0) {
  2068. if (net_ratelimit() && netif_msg_intr(tp)) {
  2069. printk(KERN_WARNING
  2070. "%s: Too much work at interrupt!\n", dev->name);
  2071. }
  2072. /* Clear all interrupt sources. */
  2073. RTL_W16(IntrStatus, 0xffff);
  2074. }
  2075. out:
  2076. return IRQ_RETVAL(handled);
  2077. }
  2078. #ifdef CONFIG_R8169_NAPI
  2079. static int rtl8169_poll(struct net_device *dev, int *budget)
  2080. {
  2081. unsigned int work_done, work_to_do = min(*budget, dev->quota);
  2082. struct rtl8169_private *tp = netdev_priv(dev);
  2083. void __iomem *ioaddr = tp->mmio_addr;
  2084. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr);
  2085. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2086. *budget -= work_done;
  2087. dev->quota -= work_done;
  2088. if (work_done < work_to_do) {
  2089. netif_rx_complete(dev);
  2090. tp->intr_mask = 0xffff;
  2091. /*
  2092. * 20040426: the barrier is not strictly required but the
  2093. * behavior of the irq handler could be less predictable
  2094. * without it. Btw, the lack of flush for the posted pci
  2095. * write is safe - FR
  2096. */
  2097. smp_wmb();
  2098. RTL_W16(IntrMask, rtl8169_intr_mask);
  2099. }
  2100. return (work_done >= work_to_do);
  2101. }
  2102. #endif
  2103. static void rtl8169_down(struct net_device *dev)
  2104. {
  2105. struct rtl8169_private *tp = netdev_priv(dev);
  2106. void __iomem *ioaddr = tp->mmio_addr;
  2107. unsigned int poll_locked = 0;
  2108. rtl8169_delete_timer(dev);
  2109. netif_stop_queue(dev);
  2110. flush_scheduled_work();
  2111. core_down:
  2112. spin_lock_irq(&tp->lock);
  2113. rtl8169_asic_down(ioaddr);
  2114. /* Update the error counts. */
  2115. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2116. RTL_W32(RxMissed, 0);
  2117. spin_unlock_irq(&tp->lock);
  2118. synchronize_irq(dev->irq);
  2119. if (!poll_locked) {
  2120. netif_poll_disable(dev);
  2121. poll_locked++;
  2122. }
  2123. /* Give a racing hard_start_xmit a few cycles to complete. */
  2124. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  2125. /*
  2126. * And now for the 50k$ question: are IRQ disabled or not ?
  2127. *
  2128. * Two paths lead here:
  2129. * 1) dev->close
  2130. * -> netif_running() is available to sync the current code and the
  2131. * IRQ handler. See rtl8169_interrupt for details.
  2132. * 2) dev->change_mtu
  2133. * -> rtl8169_poll can not be issued again and re-enable the
  2134. * interruptions. Let's simply issue the IRQ down sequence again.
  2135. */
  2136. if (RTL_R16(IntrMask))
  2137. goto core_down;
  2138. rtl8169_tx_clear(tp);
  2139. rtl8169_rx_clear(tp);
  2140. }
  2141. static int rtl8169_close(struct net_device *dev)
  2142. {
  2143. struct rtl8169_private *tp = netdev_priv(dev);
  2144. struct pci_dev *pdev = tp->pci_dev;
  2145. rtl8169_down(dev);
  2146. free_irq(dev->irq, dev);
  2147. netif_poll_enable(dev);
  2148. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  2149. tp->RxPhyAddr);
  2150. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  2151. tp->TxPhyAddr);
  2152. tp->TxDescArray = NULL;
  2153. tp->RxDescArray = NULL;
  2154. return 0;
  2155. }
  2156. static void
  2157. rtl8169_set_rx_mode(struct net_device *dev)
  2158. {
  2159. struct rtl8169_private *tp = netdev_priv(dev);
  2160. void __iomem *ioaddr = tp->mmio_addr;
  2161. unsigned long flags;
  2162. u32 mc_filter[2]; /* Multicast hash filter */
  2163. int i, rx_mode;
  2164. u32 tmp = 0;
  2165. if (dev->flags & IFF_PROMISC) {
  2166. /* Unconditionally log net taps. */
  2167. if (netif_msg_link(tp)) {
  2168. printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
  2169. dev->name);
  2170. }
  2171. rx_mode =
  2172. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  2173. AcceptAllPhys;
  2174. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2175. } else if ((dev->mc_count > multicast_filter_limit)
  2176. || (dev->flags & IFF_ALLMULTI)) {
  2177. /* Too many to filter perfectly -- accept all multicasts. */
  2178. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  2179. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2180. } else {
  2181. struct dev_mc_list *mclist;
  2182. rx_mode = AcceptBroadcast | AcceptMyPhys;
  2183. mc_filter[1] = mc_filter[0] = 0;
  2184. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  2185. i++, mclist = mclist->next) {
  2186. int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
  2187. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  2188. rx_mode |= AcceptMulticast;
  2189. }
  2190. }
  2191. spin_lock_irqsave(&tp->lock, flags);
  2192. tmp = rtl8169_rx_config | rx_mode |
  2193. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  2194. RTL_W32(RxConfig, tmp);
  2195. RTL_W32(MAR0 + 0, mc_filter[0]);
  2196. RTL_W32(MAR0 + 4, mc_filter[1]);
  2197. spin_unlock_irqrestore(&tp->lock, flags);
  2198. }
  2199. /**
  2200. * rtl8169_get_stats - Get rtl8169 read/write statistics
  2201. * @dev: The Ethernet Device to get statistics for
  2202. *
  2203. * Get TX/RX statistics for rtl8169
  2204. */
  2205. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  2206. {
  2207. struct rtl8169_private *tp = netdev_priv(dev);
  2208. void __iomem *ioaddr = tp->mmio_addr;
  2209. unsigned long flags;
  2210. if (netif_running(dev)) {
  2211. spin_lock_irqsave(&tp->lock, flags);
  2212. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2213. RTL_W32(RxMissed, 0);
  2214. spin_unlock_irqrestore(&tp->lock, flags);
  2215. }
  2216. return &tp->stats;
  2217. }
  2218. static struct pci_driver rtl8169_pci_driver = {
  2219. .name = MODULENAME,
  2220. .id_table = rtl8169_pci_tbl,
  2221. .probe = rtl8169_init_one,
  2222. .remove = __devexit_p(rtl8169_remove_one),
  2223. #ifdef CONFIG_PM
  2224. .suspend = rtl8169_suspend,
  2225. .resume = rtl8169_resume,
  2226. #endif
  2227. };
  2228. static int __init
  2229. rtl8169_init_module(void)
  2230. {
  2231. return pci_module_init(&rtl8169_pci_driver);
  2232. }
  2233. static void __exit
  2234. rtl8169_cleanup_module(void)
  2235. {
  2236. pci_unregister_driver(&rtl8169_pci_driver);
  2237. }
  2238. module_init(rtl8169_init_module);
  2239. module_exit(rtl8169_cleanup_module);