nixge.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2016-2017, National Instruments Corp.
  3. *
  4. * Author: Moritz Fischer <mdf@kernel.org>
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/of_net.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/phy.h>
  16. #include <linux/mii.h>
  17. #include <linux/nvmem-consumer.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/iopoll.h>
  20. #define TX_BD_NUM 64
  21. #define RX_BD_NUM 128
  22. /* Axi DMA Register definitions */
  23. #define XAXIDMA_TX_CR_OFFSET 0x00 /* Channel control */
  24. #define XAXIDMA_TX_SR_OFFSET 0x04 /* Status */
  25. #define XAXIDMA_TX_CDESC_OFFSET 0x08 /* Current descriptor pointer */
  26. #define XAXIDMA_TX_TDESC_OFFSET 0x10 /* Tail descriptor pointer */
  27. #define XAXIDMA_RX_CR_OFFSET 0x30 /* Channel control */
  28. #define XAXIDMA_RX_SR_OFFSET 0x34 /* Status */
  29. #define XAXIDMA_RX_CDESC_OFFSET 0x38 /* Current descriptor pointer */
  30. #define XAXIDMA_RX_TDESC_OFFSET 0x40 /* Tail descriptor pointer */
  31. #define XAXIDMA_CR_RUNSTOP_MASK 0x1 /* Start/stop DMA channel */
  32. #define XAXIDMA_CR_RESET_MASK 0x4 /* Reset DMA engine */
  33. #define XAXIDMA_BD_CTRL_LENGTH_MASK 0x007FFFFF /* Requested len */
  34. #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
  35. #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
  36. #define XAXIDMA_BD_CTRL_ALL_MASK 0x0C000000 /* All control bits */
  37. #define XAXIDMA_DELAY_MASK 0xFF000000 /* Delay timeout counter */
  38. #define XAXIDMA_COALESCE_MASK 0x00FF0000 /* Coalesce counter */
  39. #define XAXIDMA_DELAY_SHIFT 24
  40. #define XAXIDMA_COALESCE_SHIFT 16
  41. #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
  42. #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
  43. #define XAXIDMA_IRQ_ERROR_MASK 0x00004000 /* Error interrupt */
  44. #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
  45. /* Default TX/RX Threshold and waitbound values for SGDMA mode */
  46. #define XAXIDMA_DFT_TX_THRESHOLD 24
  47. #define XAXIDMA_DFT_TX_WAITBOUND 254
  48. #define XAXIDMA_DFT_RX_THRESHOLD 24
  49. #define XAXIDMA_DFT_RX_WAITBOUND 254
  50. #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
  51. #define XAXIDMA_BD_STS_COMPLETE_MASK 0x80000000 /* Completed */
  52. #define XAXIDMA_BD_STS_DEC_ERR_MASK 0x40000000 /* Decode error */
  53. #define XAXIDMA_BD_STS_SLV_ERR_MASK 0x20000000 /* Slave error */
  54. #define XAXIDMA_BD_STS_INT_ERR_MASK 0x10000000 /* Internal err */
  55. #define XAXIDMA_BD_STS_ALL_ERR_MASK 0x70000000 /* All errors */
  56. #define XAXIDMA_BD_STS_RXSOF_MASK 0x08000000 /* First rx pkt */
  57. #define XAXIDMA_BD_STS_RXEOF_MASK 0x04000000 /* Last rx pkt */
  58. #define XAXIDMA_BD_STS_ALL_MASK 0xFC000000 /* All status bits */
  59. #define NIXGE_REG_CTRL_OFFSET 0x4000
  60. #define NIXGE_REG_INFO 0x00
  61. #define NIXGE_REG_MAC_CTL 0x04
  62. #define NIXGE_REG_PHY_CTL 0x08
  63. #define NIXGE_REG_LED_CTL 0x0c
  64. #define NIXGE_REG_MDIO_DATA 0x10
  65. #define NIXGE_REG_MDIO_ADDR 0x14
  66. #define NIXGE_REG_MDIO_OP 0x18
  67. #define NIXGE_REG_MDIO_CTRL 0x1c
  68. #define NIXGE_ID_LED_CTL_EN BIT(0)
  69. #define NIXGE_ID_LED_CTL_VAL BIT(1)
  70. #define NIXGE_MDIO_CLAUSE45 BIT(12)
  71. #define NIXGE_MDIO_CLAUSE22 0
  72. #define NIXGE_MDIO_OP(n) (((n) & 0x3) << 10)
  73. #define NIXGE_MDIO_OP_ADDRESS 0
  74. #define NIXGE_MDIO_C45_WRITE BIT(0)
  75. #define NIXGE_MDIO_C45_READ (BIT(1) | BIT(0))
  76. #define NIXGE_MDIO_C22_WRITE BIT(0)
  77. #define NIXGE_MDIO_C22_READ BIT(1)
  78. #define NIXGE_MDIO_ADDR(n) (((n) & 0x1f) << 5)
  79. #define NIXGE_MDIO_MMD(n) (((n) & 0x1f) << 0)
  80. #define NIXGE_REG_MAC_LSB 0x1000
  81. #define NIXGE_REG_MAC_MSB 0x1004
  82. /* Packet size info */
  83. #define NIXGE_HDR_SIZE 14 /* Size of Ethernet header */
  84. #define NIXGE_TRL_SIZE 4 /* Size of Ethernet trailer (FCS) */
  85. #define NIXGE_MTU 1500 /* Max MTU of an Ethernet frame */
  86. #define NIXGE_JUMBO_MTU 9000 /* Max MTU of a jumbo Eth. frame */
  87. #define NIXGE_MAX_FRAME_SIZE (NIXGE_MTU + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE)
  88. #define NIXGE_MAX_JUMBO_FRAME_SIZE \
  89. (NIXGE_JUMBO_MTU + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE)
  90. enum nixge_version {
  91. NIXGE_V2,
  92. NIXGE_V3,
  93. NIXGE_VERSION_COUNT
  94. };
  95. struct nixge_hw_dma_bd {
  96. u32 next_lo;
  97. u32 next_hi;
  98. u32 phys_lo;
  99. u32 phys_hi;
  100. u32 reserved3;
  101. u32 reserved4;
  102. u32 cntrl;
  103. u32 status;
  104. u32 app0;
  105. u32 app1;
  106. u32 app2;
  107. u32 app3;
  108. u32 app4;
  109. u32 sw_id_offset_lo;
  110. u32 sw_id_offset_hi;
  111. u32 reserved6;
  112. };
  113. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  114. #define nixge_hw_dma_bd_set_addr(bd, field, addr) \
  115. do { \
  116. (bd)->field##_lo = lower_32_bits((addr)); \
  117. (bd)->field##_hi = upper_32_bits((addr)); \
  118. } while (0)
  119. #else
  120. #define nixge_hw_dma_bd_set_addr(bd, field, addr) \
  121. ((bd)->field##_lo = lower_32_bits((addr)))
  122. #endif
  123. #define nixge_hw_dma_bd_set_phys(bd, addr) \
  124. nixge_hw_dma_bd_set_addr((bd), phys, (addr))
  125. #define nixge_hw_dma_bd_set_next(bd, addr) \
  126. nixge_hw_dma_bd_set_addr((bd), next, (addr))
  127. #define nixge_hw_dma_bd_set_offset(bd, addr) \
  128. nixge_hw_dma_bd_set_addr((bd), sw_id_offset, (addr))
  129. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  130. #define nixge_hw_dma_bd_get_addr(bd, field) \
  131. (dma_addr_t)((((u64)(bd)->field##_hi) << 32) | ((bd)->field##_lo))
  132. #else
  133. #define nixge_hw_dma_bd_get_addr(bd, field) \
  134. (dma_addr_t)((bd)->field##_lo)
  135. #endif
  136. struct nixge_tx_skb {
  137. struct sk_buff *skb;
  138. dma_addr_t mapping;
  139. size_t size;
  140. bool mapped_as_page;
  141. };
  142. struct nixge_priv {
  143. struct net_device *ndev;
  144. struct napi_struct napi;
  145. struct device *dev;
  146. /* Connection to PHY device */
  147. struct device_node *phy_node;
  148. phy_interface_t phy_mode;
  149. int link;
  150. unsigned int speed;
  151. unsigned int duplex;
  152. /* MDIO bus data */
  153. struct mii_bus *mii_bus; /* MII bus reference */
  154. /* IO registers, dma functions and IRQs */
  155. void __iomem *ctrl_regs;
  156. void __iomem *dma_regs;
  157. struct tasklet_struct dma_err_tasklet;
  158. int tx_irq;
  159. int rx_irq;
  160. /* Buffer descriptors */
  161. struct nixge_hw_dma_bd *tx_bd_v;
  162. struct nixge_tx_skb *tx_skb;
  163. dma_addr_t tx_bd_p;
  164. struct nixge_hw_dma_bd *rx_bd_v;
  165. dma_addr_t rx_bd_p;
  166. u32 tx_bd_ci;
  167. u32 tx_bd_tail;
  168. u32 rx_bd_ci;
  169. u32 coalesce_count_rx;
  170. u32 coalesce_count_tx;
  171. };
  172. static void nixge_dma_write_reg(struct nixge_priv *priv, off_t offset, u32 val)
  173. {
  174. writel(val, priv->dma_regs + offset);
  175. }
  176. static void nixge_dma_write_desc_reg(struct nixge_priv *priv, off_t offset,
  177. dma_addr_t addr)
  178. {
  179. writel(lower_32_bits(addr), priv->dma_regs + offset);
  180. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  181. writel(upper_32_bits(addr), priv->dma_regs + offset + 4);
  182. #endif
  183. }
  184. static u32 nixge_dma_read_reg(const struct nixge_priv *priv, off_t offset)
  185. {
  186. return readl(priv->dma_regs + offset);
  187. }
  188. static void nixge_ctrl_write_reg(struct nixge_priv *priv, off_t offset, u32 val)
  189. {
  190. writel(val, priv->ctrl_regs + offset);
  191. }
  192. static u32 nixge_ctrl_read_reg(struct nixge_priv *priv, off_t offset)
  193. {
  194. return readl(priv->ctrl_regs + offset);
  195. }
  196. #define nixge_ctrl_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
  197. readl_poll_timeout((priv)->ctrl_regs + (addr), (val), (cond), \
  198. (sleep_us), (timeout_us))
  199. #define nixge_dma_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
  200. readl_poll_timeout((priv)->dma_regs + (addr), (val), (cond), \
  201. (sleep_us), (timeout_us))
  202. static void nixge_hw_dma_bd_release(struct net_device *ndev)
  203. {
  204. struct nixge_priv *priv = netdev_priv(ndev);
  205. dma_addr_t phys_addr;
  206. struct sk_buff *skb;
  207. int i;
  208. for (i = 0; i < RX_BD_NUM; i++) {
  209. phys_addr = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
  210. phys);
  211. dma_unmap_single(ndev->dev.parent, phys_addr,
  212. NIXGE_MAX_JUMBO_FRAME_SIZE,
  213. DMA_FROM_DEVICE);
  214. skb = (struct sk_buff *)(uintptr_t)
  215. nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
  216. sw_id_offset);
  217. dev_kfree_skb(skb);
  218. }
  219. if (priv->rx_bd_v)
  220. dma_free_coherent(ndev->dev.parent,
  221. sizeof(*priv->rx_bd_v) * RX_BD_NUM,
  222. priv->rx_bd_v,
  223. priv->rx_bd_p);
  224. if (priv->tx_skb)
  225. devm_kfree(ndev->dev.parent, priv->tx_skb);
  226. if (priv->tx_bd_v)
  227. dma_free_coherent(ndev->dev.parent,
  228. sizeof(*priv->tx_bd_v) * TX_BD_NUM,
  229. priv->tx_bd_v,
  230. priv->tx_bd_p);
  231. }
  232. static int nixge_hw_dma_bd_init(struct net_device *ndev)
  233. {
  234. struct nixge_priv *priv = netdev_priv(ndev);
  235. struct sk_buff *skb;
  236. dma_addr_t phys;
  237. u32 cr;
  238. int i;
  239. /* Reset the indexes which are used for accessing the BDs */
  240. priv->tx_bd_ci = 0;
  241. priv->tx_bd_tail = 0;
  242. priv->rx_bd_ci = 0;
  243. /* Allocate the Tx and Rx buffer descriptors. */
  244. priv->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  245. sizeof(*priv->tx_bd_v) * TX_BD_NUM,
  246. &priv->tx_bd_p, GFP_KERNEL);
  247. if (!priv->tx_bd_v)
  248. goto out;
  249. priv->tx_skb = devm_kcalloc(ndev->dev.parent,
  250. TX_BD_NUM, sizeof(*priv->tx_skb),
  251. GFP_KERNEL);
  252. if (!priv->tx_skb)
  253. goto out;
  254. priv->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  255. sizeof(*priv->rx_bd_v) * RX_BD_NUM,
  256. &priv->rx_bd_p, GFP_KERNEL);
  257. if (!priv->rx_bd_v)
  258. goto out;
  259. for (i = 0; i < TX_BD_NUM; i++) {
  260. nixge_hw_dma_bd_set_next(&priv->tx_bd_v[i],
  261. priv->tx_bd_p +
  262. sizeof(*priv->tx_bd_v) *
  263. ((i + 1) % TX_BD_NUM));
  264. }
  265. for (i = 0; i < RX_BD_NUM; i++) {
  266. nixge_hw_dma_bd_set_next(&priv->rx_bd_v[i],
  267. priv->rx_bd_p
  268. + sizeof(*priv->rx_bd_v) *
  269. ((i + 1) % RX_BD_NUM));
  270. skb = netdev_alloc_skb_ip_align(ndev,
  271. NIXGE_MAX_JUMBO_FRAME_SIZE);
  272. if (!skb)
  273. goto out;
  274. nixge_hw_dma_bd_set_offset(&priv->rx_bd_v[i], (uintptr_t)skb);
  275. phys = dma_map_single(ndev->dev.parent, skb->data,
  276. NIXGE_MAX_JUMBO_FRAME_SIZE,
  277. DMA_FROM_DEVICE);
  278. nixge_hw_dma_bd_set_phys(&priv->rx_bd_v[i], phys);
  279. priv->rx_bd_v[i].cntrl = NIXGE_MAX_JUMBO_FRAME_SIZE;
  280. }
  281. /* Start updating the Rx channel control register */
  282. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  283. /* Update the interrupt coalesce count */
  284. cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
  285. ((priv->coalesce_count_rx) << XAXIDMA_COALESCE_SHIFT));
  286. /* Update the delay timer count */
  287. cr = ((cr & ~XAXIDMA_DELAY_MASK) |
  288. (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  289. /* Enable coalesce, delay timer and error interrupts */
  290. cr |= XAXIDMA_IRQ_ALL_MASK;
  291. /* Write to the Rx channel control register */
  292. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
  293. /* Start updating the Tx channel control register */
  294. cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
  295. /* Update the interrupt coalesce count */
  296. cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
  297. ((priv->coalesce_count_tx) << XAXIDMA_COALESCE_SHIFT));
  298. /* Update the delay timer count */
  299. cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
  300. (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  301. /* Enable coalesce, delay timer and error interrupts */
  302. cr |= XAXIDMA_IRQ_ALL_MASK;
  303. /* Write to the Tx channel control register */
  304. nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
  305. /* Populate the tail pointer and bring the Rx Axi DMA engine out of
  306. * halted state. This will make the Rx side ready for reception.
  307. */
  308. nixge_dma_write_desc_reg(priv, XAXIDMA_RX_CDESC_OFFSET, priv->rx_bd_p);
  309. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  310. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET,
  311. cr | XAXIDMA_CR_RUNSTOP_MASK);
  312. nixge_dma_write_desc_reg(priv, XAXIDMA_RX_TDESC_OFFSET, priv->rx_bd_p +
  313. (sizeof(*priv->rx_bd_v) * (RX_BD_NUM - 1)));
  314. /* Write to the RS (Run-stop) bit in the Tx channel control register.
  315. * Tx channel is now ready to run. But only after we write to the
  316. * tail pointer register that the Tx channel will start transmitting.
  317. */
  318. nixge_dma_write_desc_reg(priv, XAXIDMA_TX_CDESC_OFFSET, priv->tx_bd_p);
  319. cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
  320. nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET,
  321. cr | XAXIDMA_CR_RUNSTOP_MASK);
  322. return 0;
  323. out:
  324. nixge_hw_dma_bd_release(ndev);
  325. return -ENOMEM;
  326. }
  327. static void __nixge_device_reset(struct nixge_priv *priv, off_t offset)
  328. {
  329. u32 status;
  330. int err;
  331. /* Reset Axi DMA. This would reset NIXGE Ethernet core as well.
  332. * The reset process of Axi DMA takes a while to complete as all
  333. * pending commands/transfers will be flushed or completed during
  334. * this reset process.
  335. */
  336. nixge_dma_write_reg(priv, offset, XAXIDMA_CR_RESET_MASK);
  337. err = nixge_dma_poll_timeout(priv, offset, status,
  338. !(status & XAXIDMA_CR_RESET_MASK), 10,
  339. 1000);
  340. if (err)
  341. netdev_err(priv->ndev, "%s: DMA reset timeout!\n", __func__);
  342. }
  343. static void nixge_device_reset(struct net_device *ndev)
  344. {
  345. struct nixge_priv *priv = netdev_priv(ndev);
  346. __nixge_device_reset(priv, XAXIDMA_TX_CR_OFFSET);
  347. __nixge_device_reset(priv, XAXIDMA_RX_CR_OFFSET);
  348. if (nixge_hw_dma_bd_init(ndev))
  349. netdev_err(ndev, "%s: descriptor allocation failed\n",
  350. __func__);
  351. netif_trans_update(ndev);
  352. }
  353. static void nixge_handle_link_change(struct net_device *ndev)
  354. {
  355. struct nixge_priv *priv = netdev_priv(ndev);
  356. struct phy_device *phydev = ndev->phydev;
  357. if (phydev->link != priv->link || phydev->speed != priv->speed ||
  358. phydev->duplex != priv->duplex) {
  359. priv->link = phydev->link;
  360. priv->speed = phydev->speed;
  361. priv->duplex = phydev->duplex;
  362. phy_print_status(phydev);
  363. }
  364. }
  365. static void nixge_tx_skb_unmap(struct nixge_priv *priv,
  366. struct nixge_tx_skb *tx_skb)
  367. {
  368. if (tx_skb->mapping) {
  369. if (tx_skb->mapped_as_page)
  370. dma_unmap_page(priv->ndev->dev.parent, tx_skb->mapping,
  371. tx_skb->size, DMA_TO_DEVICE);
  372. else
  373. dma_unmap_single(priv->ndev->dev.parent,
  374. tx_skb->mapping,
  375. tx_skb->size, DMA_TO_DEVICE);
  376. tx_skb->mapping = 0;
  377. }
  378. if (tx_skb->skb) {
  379. dev_kfree_skb_any(tx_skb->skb);
  380. tx_skb->skb = NULL;
  381. }
  382. }
  383. static void nixge_start_xmit_done(struct net_device *ndev)
  384. {
  385. struct nixge_priv *priv = netdev_priv(ndev);
  386. struct nixge_hw_dma_bd *cur_p;
  387. struct nixge_tx_skb *tx_skb;
  388. unsigned int status = 0;
  389. u32 packets = 0;
  390. u32 size = 0;
  391. cur_p = &priv->tx_bd_v[priv->tx_bd_ci];
  392. tx_skb = &priv->tx_skb[priv->tx_bd_ci];
  393. status = cur_p->status;
  394. while (status & XAXIDMA_BD_STS_COMPLETE_MASK) {
  395. nixge_tx_skb_unmap(priv, tx_skb);
  396. cur_p->status = 0;
  397. size += status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
  398. packets++;
  399. ++priv->tx_bd_ci;
  400. priv->tx_bd_ci %= TX_BD_NUM;
  401. cur_p = &priv->tx_bd_v[priv->tx_bd_ci];
  402. tx_skb = &priv->tx_skb[priv->tx_bd_ci];
  403. status = cur_p->status;
  404. }
  405. ndev->stats.tx_packets += packets;
  406. ndev->stats.tx_bytes += size;
  407. if (packets)
  408. netif_wake_queue(ndev);
  409. }
  410. static int nixge_check_tx_bd_space(struct nixge_priv *priv,
  411. int num_frag)
  412. {
  413. struct nixge_hw_dma_bd *cur_p;
  414. cur_p = &priv->tx_bd_v[(priv->tx_bd_tail + num_frag) % TX_BD_NUM];
  415. if (cur_p->status & XAXIDMA_BD_STS_ALL_MASK)
  416. return NETDEV_TX_BUSY;
  417. return 0;
  418. }
  419. static int nixge_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  420. {
  421. struct nixge_priv *priv = netdev_priv(ndev);
  422. struct nixge_hw_dma_bd *cur_p;
  423. struct nixge_tx_skb *tx_skb;
  424. dma_addr_t tail_p, cur_phys;
  425. skb_frag_t *frag;
  426. u32 num_frag;
  427. u32 ii;
  428. num_frag = skb_shinfo(skb)->nr_frags;
  429. cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
  430. tx_skb = &priv->tx_skb[priv->tx_bd_tail];
  431. if (nixge_check_tx_bd_space(priv, num_frag)) {
  432. if (!netif_queue_stopped(ndev))
  433. netif_stop_queue(ndev);
  434. return NETDEV_TX_OK;
  435. }
  436. cur_phys = dma_map_single(ndev->dev.parent, skb->data,
  437. skb_headlen(skb), DMA_TO_DEVICE);
  438. if (dma_mapping_error(ndev->dev.parent, cur_phys))
  439. goto drop;
  440. nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
  441. cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
  442. tx_skb->skb = NULL;
  443. tx_skb->mapping = cur_phys;
  444. tx_skb->size = skb_headlen(skb);
  445. tx_skb->mapped_as_page = false;
  446. for (ii = 0; ii < num_frag; ii++) {
  447. ++priv->tx_bd_tail;
  448. priv->tx_bd_tail %= TX_BD_NUM;
  449. cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
  450. tx_skb = &priv->tx_skb[priv->tx_bd_tail];
  451. frag = &skb_shinfo(skb)->frags[ii];
  452. cur_phys = skb_frag_dma_map(ndev->dev.parent, frag, 0,
  453. skb_frag_size(frag),
  454. DMA_TO_DEVICE);
  455. if (dma_mapping_error(ndev->dev.parent, cur_phys))
  456. goto frag_err;
  457. nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
  458. cur_p->cntrl = skb_frag_size(frag);
  459. tx_skb->skb = NULL;
  460. tx_skb->mapping = cur_phys;
  461. tx_skb->size = skb_frag_size(frag);
  462. tx_skb->mapped_as_page = true;
  463. }
  464. /* last buffer of the frame */
  465. tx_skb->skb = skb;
  466. cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
  467. tail_p = priv->tx_bd_p + sizeof(*priv->tx_bd_v) * priv->tx_bd_tail;
  468. /* Start the transfer */
  469. nixge_dma_write_desc_reg(priv, XAXIDMA_TX_TDESC_OFFSET, tail_p);
  470. ++priv->tx_bd_tail;
  471. priv->tx_bd_tail %= TX_BD_NUM;
  472. return NETDEV_TX_OK;
  473. frag_err:
  474. for (; ii > 0; ii--) {
  475. if (priv->tx_bd_tail)
  476. priv->tx_bd_tail--;
  477. else
  478. priv->tx_bd_tail = TX_BD_NUM - 1;
  479. tx_skb = &priv->tx_skb[priv->tx_bd_tail];
  480. nixge_tx_skb_unmap(priv, tx_skb);
  481. cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
  482. cur_p->status = 0;
  483. }
  484. dma_unmap_single(priv->ndev->dev.parent,
  485. tx_skb->mapping,
  486. tx_skb->size, DMA_TO_DEVICE);
  487. drop:
  488. ndev->stats.tx_dropped++;
  489. return NETDEV_TX_OK;
  490. }
  491. static int nixge_recv(struct net_device *ndev, int budget)
  492. {
  493. struct nixge_priv *priv = netdev_priv(ndev);
  494. struct sk_buff *skb, *new_skb;
  495. struct nixge_hw_dma_bd *cur_p;
  496. dma_addr_t tail_p = 0, cur_phys = 0;
  497. u32 packets = 0;
  498. u32 length = 0;
  499. u32 size = 0;
  500. cur_p = &priv->rx_bd_v[priv->rx_bd_ci];
  501. while ((cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK &&
  502. budget > packets)) {
  503. tail_p = priv->rx_bd_p + sizeof(*priv->rx_bd_v) *
  504. priv->rx_bd_ci;
  505. skb = (struct sk_buff *)(uintptr_t)
  506. nixge_hw_dma_bd_get_addr(cur_p, sw_id_offset);
  507. length = cur_p->status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
  508. if (length > NIXGE_MAX_JUMBO_FRAME_SIZE)
  509. length = NIXGE_MAX_JUMBO_FRAME_SIZE;
  510. dma_unmap_single(ndev->dev.parent,
  511. nixge_hw_dma_bd_get_addr(cur_p, phys),
  512. NIXGE_MAX_JUMBO_FRAME_SIZE,
  513. DMA_FROM_DEVICE);
  514. skb_put(skb, length);
  515. skb->protocol = eth_type_trans(skb, ndev);
  516. skb_checksum_none_assert(skb);
  517. /* For now mark them as CHECKSUM_NONE since
  518. * we don't have offload capabilities
  519. */
  520. skb->ip_summed = CHECKSUM_NONE;
  521. napi_gro_receive(&priv->napi, skb);
  522. size += length;
  523. packets++;
  524. new_skb = netdev_alloc_skb_ip_align(ndev,
  525. NIXGE_MAX_JUMBO_FRAME_SIZE);
  526. if (!new_skb)
  527. return packets;
  528. cur_phys = dma_map_single(ndev->dev.parent, new_skb->data,
  529. NIXGE_MAX_JUMBO_FRAME_SIZE,
  530. DMA_FROM_DEVICE);
  531. if (dma_mapping_error(ndev->dev.parent, cur_phys)) {
  532. /* FIXME: bail out and clean up */
  533. netdev_err(ndev, "Failed to map ...\n");
  534. }
  535. nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
  536. cur_p->cntrl = NIXGE_MAX_JUMBO_FRAME_SIZE;
  537. cur_p->status = 0;
  538. nixge_hw_dma_bd_set_offset(cur_p, (uintptr_t)new_skb);
  539. ++priv->rx_bd_ci;
  540. priv->rx_bd_ci %= RX_BD_NUM;
  541. cur_p = &priv->rx_bd_v[priv->rx_bd_ci];
  542. }
  543. ndev->stats.rx_packets += packets;
  544. ndev->stats.rx_bytes += size;
  545. if (tail_p)
  546. nixge_dma_write_desc_reg(priv, XAXIDMA_RX_TDESC_OFFSET, tail_p);
  547. return packets;
  548. }
  549. static int nixge_poll(struct napi_struct *napi, int budget)
  550. {
  551. struct nixge_priv *priv = container_of(napi, struct nixge_priv, napi);
  552. int work_done;
  553. u32 status, cr;
  554. work_done = 0;
  555. work_done = nixge_recv(priv->ndev, budget);
  556. if (work_done < budget) {
  557. napi_complete_done(napi, work_done);
  558. status = nixge_dma_read_reg(priv, XAXIDMA_RX_SR_OFFSET);
  559. if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
  560. /* If there's more, reschedule, but clear */
  561. nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
  562. napi_reschedule(napi);
  563. } else {
  564. /* if not, turn on RX IRQs again ... */
  565. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  566. cr |= (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
  567. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
  568. }
  569. }
  570. return work_done;
  571. }
  572. static irqreturn_t nixge_tx_irq(int irq, void *_ndev)
  573. {
  574. struct nixge_priv *priv = netdev_priv(_ndev);
  575. struct net_device *ndev = _ndev;
  576. unsigned int status;
  577. dma_addr_t phys;
  578. u32 cr;
  579. status = nixge_dma_read_reg(priv, XAXIDMA_TX_SR_OFFSET);
  580. if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
  581. nixge_dma_write_reg(priv, XAXIDMA_TX_SR_OFFSET, status);
  582. nixge_start_xmit_done(priv->ndev);
  583. goto out;
  584. }
  585. if (!(status & XAXIDMA_IRQ_ALL_MASK)) {
  586. netdev_err(ndev, "No interrupts asserted in Tx path\n");
  587. return IRQ_NONE;
  588. }
  589. if (status & XAXIDMA_IRQ_ERROR_MASK) {
  590. phys = nixge_hw_dma_bd_get_addr(&priv->tx_bd_v[priv->tx_bd_ci],
  591. phys);
  592. netdev_err(ndev, "DMA Tx error 0x%x\n", status);
  593. netdev_err(ndev, "Current BD is at: 0x%llx\n", (u64)phys);
  594. cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
  595. /* Disable coalesce, delay timer and error interrupts */
  596. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  597. /* Write to the Tx channel control register */
  598. nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
  599. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  600. /* Disable coalesce, delay timer and error interrupts */
  601. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  602. /* Write to the Rx channel control register */
  603. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
  604. tasklet_schedule(&priv->dma_err_tasklet);
  605. nixge_dma_write_reg(priv, XAXIDMA_TX_SR_OFFSET, status);
  606. }
  607. out:
  608. return IRQ_HANDLED;
  609. }
  610. static irqreturn_t nixge_rx_irq(int irq, void *_ndev)
  611. {
  612. struct nixge_priv *priv = netdev_priv(_ndev);
  613. struct net_device *ndev = _ndev;
  614. unsigned int status;
  615. dma_addr_t phys;
  616. u32 cr;
  617. status = nixge_dma_read_reg(priv, XAXIDMA_RX_SR_OFFSET);
  618. if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
  619. /* Turn of IRQs because NAPI */
  620. nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
  621. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  622. cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
  623. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
  624. if (napi_schedule_prep(&priv->napi))
  625. __napi_schedule(&priv->napi);
  626. goto out;
  627. }
  628. if (!(status & XAXIDMA_IRQ_ALL_MASK)) {
  629. netdev_err(ndev, "No interrupts asserted in Rx path\n");
  630. return IRQ_NONE;
  631. }
  632. if (status & XAXIDMA_IRQ_ERROR_MASK) {
  633. phys = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[priv->rx_bd_ci],
  634. phys);
  635. netdev_err(ndev, "DMA Rx error 0x%x\n", status);
  636. netdev_err(ndev, "Current BD is at: 0x%llx\n", (u64)phys);
  637. cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
  638. /* Disable coalesce, delay timer and error interrupts */
  639. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  640. /* Finally write to the Tx channel control register */
  641. nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
  642. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  643. /* Disable coalesce, delay timer and error interrupts */
  644. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  645. /* write to the Rx channel control register */
  646. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
  647. tasklet_schedule(&priv->dma_err_tasklet);
  648. nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
  649. }
  650. out:
  651. return IRQ_HANDLED;
  652. }
  653. static void nixge_dma_err_handler(unsigned long data)
  654. {
  655. struct nixge_priv *lp = (struct nixge_priv *)data;
  656. struct nixge_hw_dma_bd *cur_p;
  657. struct nixge_tx_skb *tx_skb;
  658. u32 cr, i;
  659. __nixge_device_reset(lp, XAXIDMA_TX_CR_OFFSET);
  660. __nixge_device_reset(lp, XAXIDMA_RX_CR_OFFSET);
  661. for (i = 0; i < TX_BD_NUM; i++) {
  662. cur_p = &lp->tx_bd_v[i];
  663. tx_skb = &lp->tx_skb[i];
  664. nixge_tx_skb_unmap(lp, tx_skb);
  665. nixge_hw_dma_bd_set_phys(cur_p, 0);
  666. cur_p->cntrl = 0;
  667. cur_p->status = 0;
  668. nixge_hw_dma_bd_set_offset(cur_p, 0);
  669. }
  670. for (i = 0; i < RX_BD_NUM; i++) {
  671. cur_p = &lp->rx_bd_v[i];
  672. cur_p->status = 0;
  673. }
  674. lp->tx_bd_ci = 0;
  675. lp->tx_bd_tail = 0;
  676. lp->rx_bd_ci = 0;
  677. /* Start updating the Rx channel control register */
  678. cr = nixge_dma_read_reg(lp, XAXIDMA_RX_CR_OFFSET);
  679. /* Update the interrupt coalesce count */
  680. cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
  681. (XAXIDMA_DFT_RX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
  682. /* Update the delay timer count */
  683. cr = ((cr & ~XAXIDMA_DELAY_MASK) |
  684. (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  685. /* Enable coalesce, delay timer and error interrupts */
  686. cr |= XAXIDMA_IRQ_ALL_MASK;
  687. /* Finally write to the Rx channel control register */
  688. nixge_dma_write_reg(lp, XAXIDMA_RX_CR_OFFSET, cr);
  689. /* Start updating the Tx channel control register */
  690. cr = nixge_dma_read_reg(lp, XAXIDMA_TX_CR_OFFSET);
  691. /* Update the interrupt coalesce count */
  692. cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
  693. (XAXIDMA_DFT_TX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
  694. /* Update the delay timer count */
  695. cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
  696. (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  697. /* Enable coalesce, delay timer and error interrupts */
  698. cr |= XAXIDMA_IRQ_ALL_MASK;
  699. /* Finally write to the Tx channel control register */
  700. nixge_dma_write_reg(lp, XAXIDMA_TX_CR_OFFSET, cr);
  701. /* Populate the tail pointer and bring the Rx Axi DMA engine out of
  702. * halted state. This will make the Rx side ready for reception.
  703. */
  704. nixge_dma_write_desc_reg(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
  705. cr = nixge_dma_read_reg(lp, XAXIDMA_RX_CR_OFFSET);
  706. nixge_dma_write_reg(lp, XAXIDMA_RX_CR_OFFSET,
  707. cr | XAXIDMA_CR_RUNSTOP_MASK);
  708. nixge_dma_write_desc_reg(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
  709. (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
  710. /* Write to the RS (Run-stop) bit in the Tx channel control register.
  711. * Tx channel is now ready to run. But only after we write to the
  712. * tail pointer register that the Tx channel will start transmitting
  713. */
  714. nixge_dma_write_desc_reg(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
  715. cr = nixge_dma_read_reg(lp, XAXIDMA_TX_CR_OFFSET);
  716. nixge_dma_write_reg(lp, XAXIDMA_TX_CR_OFFSET,
  717. cr | XAXIDMA_CR_RUNSTOP_MASK);
  718. }
  719. static int nixge_open(struct net_device *ndev)
  720. {
  721. struct nixge_priv *priv = netdev_priv(ndev);
  722. struct phy_device *phy;
  723. int ret;
  724. nixge_device_reset(ndev);
  725. phy = of_phy_connect(ndev, priv->phy_node,
  726. &nixge_handle_link_change, 0, priv->phy_mode);
  727. if (!phy)
  728. return -ENODEV;
  729. phy_start(phy);
  730. /* Enable tasklets for Axi DMA error handling */
  731. tasklet_init(&priv->dma_err_tasklet, nixge_dma_err_handler,
  732. (unsigned long)priv);
  733. napi_enable(&priv->napi);
  734. /* Enable interrupts for Axi DMA Tx */
  735. ret = request_irq(priv->tx_irq, nixge_tx_irq, 0, ndev->name, ndev);
  736. if (ret)
  737. goto err_tx_irq;
  738. /* Enable interrupts for Axi DMA Rx */
  739. ret = request_irq(priv->rx_irq, nixge_rx_irq, 0, ndev->name, ndev);
  740. if (ret)
  741. goto err_rx_irq;
  742. netif_start_queue(ndev);
  743. return 0;
  744. err_rx_irq:
  745. free_irq(priv->tx_irq, ndev);
  746. err_tx_irq:
  747. phy_stop(phy);
  748. phy_disconnect(phy);
  749. tasklet_kill(&priv->dma_err_tasklet);
  750. netdev_err(ndev, "request_irq() failed\n");
  751. return ret;
  752. }
  753. static int nixge_stop(struct net_device *ndev)
  754. {
  755. struct nixge_priv *priv = netdev_priv(ndev);
  756. u32 cr;
  757. netif_stop_queue(ndev);
  758. napi_disable(&priv->napi);
  759. if (ndev->phydev) {
  760. phy_stop(ndev->phydev);
  761. phy_disconnect(ndev->phydev);
  762. }
  763. cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  764. nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET,
  765. cr & (~XAXIDMA_CR_RUNSTOP_MASK));
  766. cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
  767. nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET,
  768. cr & (~XAXIDMA_CR_RUNSTOP_MASK));
  769. tasklet_kill(&priv->dma_err_tasklet);
  770. free_irq(priv->tx_irq, ndev);
  771. free_irq(priv->rx_irq, ndev);
  772. nixge_hw_dma_bd_release(ndev);
  773. return 0;
  774. }
  775. static int nixge_change_mtu(struct net_device *ndev, int new_mtu)
  776. {
  777. if (netif_running(ndev))
  778. return -EBUSY;
  779. if ((new_mtu + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE) >
  780. NIXGE_MAX_JUMBO_FRAME_SIZE)
  781. return -EINVAL;
  782. ndev->mtu = new_mtu;
  783. return 0;
  784. }
  785. static s32 __nixge_hw_set_mac_address(struct net_device *ndev)
  786. {
  787. struct nixge_priv *priv = netdev_priv(ndev);
  788. nixge_ctrl_write_reg(priv, NIXGE_REG_MAC_LSB,
  789. (ndev->dev_addr[2]) << 24 |
  790. (ndev->dev_addr[3] << 16) |
  791. (ndev->dev_addr[4] << 8) |
  792. (ndev->dev_addr[5] << 0));
  793. nixge_ctrl_write_reg(priv, NIXGE_REG_MAC_MSB,
  794. (ndev->dev_addr[1] | (ndev->dev_addr[0] << 8)));
  795. return 0;
  796. }
  797. static int nixge_net_set_mac_address(struct net_device *ndev, void *p)
  798. {
  799. int err;
  800. err = eth_mac_addr(ndev, p);
  801. if (!err)
  802. __nixge_hw_set_mac_address(ndev);
  803. return err;
  804. }
  805. static const struct net_device_ops nixge_netdev_ops = {
  806. .ndo_open = nixge_open,
  807. .ndo_stop = nixge_stop,
  808. .ndo_start_xmit = nixge_start_xmit,
  809. .ndo_change_mtu = nixge_change_mtu,
  810. .ndo_set_mac_address = nixge_net_set_mac_address,
  811. .ndo_validate_addr = eth_validate_addr,
  812. };
  813. static void nixge_ethtools_get_drvinfo(struct net_device *ndev,
  814. struct ethtool_drvinfo *ed)
  815. {
  816. strlcpy(ed->driver, "nixge", sizeof(ed->driver));
  817. strlcpy(ed->bus_info, "platform", sizeof(ed->bus_info));
  818. }
  819. static int nixge_ethtools_get_coalesce(struct net_device *ndev,
  820. struct ethtool_coalesce *ecoalesce)
  821. {
  822. struct nixge_priv *priv = netdev_priv(ndev);
  823. u32 regval = 0;
  824. regval = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
  825. ecoalesce->rx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
  826. >> XAXIDMA_COALESCE_SHIFT;
  827. regval = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
  828. ecoalesce->tx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
  829. >> XAXIDMA_COALESCE_SHIFT;
  830. return 0;
  831. }
  832. static int nixge_ethtools_set_coalesce(struct net_device *ndev,
  833. struct ethtool_coalesce *ecoalesce)
  834. {
  835. struct nixge_priv *priv = netdev_priv(ndev);
  836. if (netif_running(ndev)) {
  837. netdev_err(ndev,
  838. "Please stop netif before applying configuration\n");
  839. return -EBUSY;
  840. }
  841. if (ecoalesce->rx_coalesce_usecs ||
  842. ecoalesce->rx_coalesce_usecs_irq ||
  843. ecoalesce->rx_max_coalesced_frames_irq ||
  844. ecoalesce->tx_coalesce_usecs ||
  845. ecoalesce->tx_coalesce_usecs_irq ||
  846. ecoalesce->tx_max_coalesced_frames_irq ||
  847. ecoalesce->stats_block_coalesce_usecs ||
  848. ecoalesce->use_adaptive_rx_coalesce ||
  849. ecoalesce->use_adaptive_tx_coalesce ||
  850. ecoalesce->pkt_rate_low ||
  851. ecoalesce->rx_coalesce_usecs_low ||
  852. ecoalesce->rx_max_coalesced_frames_low ||
  853. ecoalesce->tx_coalesce_usecs_low ||
  854. ecoalesce->tx_max_coalesced_frames_low ||
  855. ecoalesce->pkt_rate_high ||
  856. ecoalesce->rx_coalesce_usecs_high ||
  857. ecoalesce->rx_max_coalesced_frames_high ||
  858. ecoalesce->tx_coalesce_usecs_high ||
  859. ecoalesce->tx_max_coalesced_frames_high ||
  860. ecoalesce->rate_sample_interval)
  861. return -EOPNOTSUPP;
  862. if (ecoalesce->rx_max_coalesced_frames)
  863. priv->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
  864. if (ecoalesce->tx_max_coalesced_frames)
  865. priv->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
  866. return 0;
  867. }
  868. static int nixge_ethtools_set_phys_id(struct net_device *ndev,
  869. enum ethtool_phys_id_state state)
  870. {
  871. struct nixge_priv *priv = netdev_priv(ndev);
  872. u32 ctrl;
  873. ctrl = nixge_ctrl_read_reg(priv, NIXGE_REG_LED_CTL);
  874. switch (state) {
  875. case ETHTOOL_ID_ACTIVE:
  876. ctrl |= NIXGE_ID_LED_CTL_EN;
  877. /* Enable identification LED override*/
  878. nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
  879. return 2;
  880. case ETHTOOL_ID_ON:
  881. ctrl |= NIXGE_ID_LED_CTL_VAL;
  882. nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
  883. break;
  884. case ETHTOOL_ID_OFF:
  885. ctrl &= ~NIXGE_ID_LED_CTL_VAL;
  886. nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
  887. break;
  888. case ETHTOOL_ID_INACTIVE:
  889. /* Restore LED settings */
  890. ctrl &= ~NIXGE_ID_LED_CTL_EN;
  891. nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
  892. break;
  893. }
  894. return 0;
  895. }
  896. static const struct ethtool_ops nixge_ethtool_ops = {
  897. .get_drvinfo = nixge_ethtools_get_drvinfo,
  898. .get_coalesce = nixge_ethtools_get_coalesce,
  899. .set_coalesce = nixge_ethtools_set_coalesce,
  900. .set_phys_id = nixge_ethtools_set_phys_id,
  901. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  902. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  903. .get_link = ethtool_op_get_link,
  904. };
  905. static int nixge_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  906. {
  907. struct nixge_priv *priv = bus->priv;
  908. u32 status, tmp;
  909. int err;
  910. u16 device;
  911. if (reg & MII_ADDR_C45) {
  912. device = (reg >> 16) & 0x1f;
  913. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_ADDR, reg & 0xffff);
  914. tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_OP_ADDRESS)
  915. | NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
  916. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
  917. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
  918. err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
  919. !status, 10, 1000);
  920. if (err) {
  921. dev_err(priv->dev, "timeout setting address");
  922. return err;
  923. }
  924. tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_C45_READ) |
  925. NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
  926. } else {
  927. device = reg & 0x1f;
  928. tmp = NIXGE_MDIO_CLAUSE22 | NIXGE_MDIO_OP(NIXGE_MDIO_C22_READ) |
  929. NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
  930. }
  931. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
  932. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
  933. err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
  934. !status, 10, 1000);
  935. if (err) {
  936. dev_err(priv->dev, "timeout setting read command");
  937. return err;
  938. }
  939. status = nixge_ctrl_read_reg(priv, NIXGE_REG_MDIO_DATA);
  940. return status;
  941. }
  942. static int nixge_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
  943. {
  944. struct nixge_priv *priv = bus->priv;
  945. u32 status, tmp;
  946. u16 device;
  947. int err;
  948. if (reg & MII_ADDR_C45) {
  949. device = (reg >> 16) & 0x1f;
  950. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_ADDR, reg & 0xffff);
  951. tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_OP_ADDRESS)
  952. | NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
  953. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
  954. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
  955. err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
  956. !status, 10, 1000);
  957. if (err) {
  958. dev_err(priv->dev, "timeout setting address");
  959. return err;
  960. }
  961. tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_C45_WRITE)
  962. | NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
  963. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_DATA, val);
  964. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
  965. err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
  966. !status, 10, 1000);
  967. if (err)
  968. dev_err(priv->dev, "timeout setting write command");
  969. } else {
  970. device = reg & 0x1f;
  971. tmp = NIXGE_MDIO_CLAUSE22 |
  972. NIXGE_MDIO_OP(NIXGE_MDIO_C22_WRITE) |
  973. NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
  974. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_DATA, val);
  975. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
  976. nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
  977. err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
  978. !status, 10, 1000);
  979. if (err)
  980. dev_err(priv->dev, "timeout setting write command");
  981. }
  982. return err;
  983. }
  984. static int nixge_mdio_setup(struct nixge_priv *priv, struct device_node *np)
  985. {
  986. struct mii_bus *bus;
  987. bus = devm_mdiobus_alloc(priv->dev);
  988. if (!bus)
  989. return -ENOMEM;
  990. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(priv->dev));
  991. bus->priv = priv;
  992. bus->name = "nixge_mii_bus";
  993. bus->read = nixge_mdio_read;
  994. bus->write = nixge_mdio_write;
  995. bus->parent = priv->dev;
  996. priv->mii_bus = bus;
  997. return of_mdiobus_register(bus, np);
  998. }
  999. static void *nixge_get_nvmem_address(struct device *dev)
  1000. {
  1001. struct nvmem_cell *cell;
  1002. size_t cell_size;
  1003. char *mac;
  1004. cell = nvmem_cell_get(dev, "address");
  1005. if (IS_ERR(cell))
  1006. return NULL;
  1007. mac = nvmem_cell_read(cell, &cell_size);
  1008. nvmem_cell_put(cell);
  1009. return mac;
  1010. }
  1011. /* Match table for of_platform binding */
  1012. static const struct of_device_id nixge_dt_ids[] = {
  1013. { .compatible = "ni,xge-enet-2.00", .data = (void *)NIXGE_V2 },
  1014. { .compatible = "ni,xge-enet-3.00", .data = (void *)NIXGE_V3 },
  1015. {},
  1016. };
  1017. MODULE_DEVICE_TABLE(of, nixge_dt_ids);
  1018. static int nixge_of_get_resources(struct platform_device *pdev)
  1019. {
  1020. const struct of_device_id *of_id;
  1021. enum nixge_version version;
  1022. struct resource *ctrlres;
  1023. struct resource *dmares;
  1024. struct net_device *ndev;
  1025. struct nixge_priv *priv;
  1026. ndev = platform_get_drvdata(pdev);
  1027. priv = netdev_priv(ndev);
  1028. of_id = of_match_node(nixge_dt_ids, pdev->dev.of_node);
  1029. if (!of_id)
  1030. return -ENODEV;
  1031. version = (enum nixge_version)of_id->data;
  1032. if (version <= NIXGE_V2)
  1033. dmares = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1034. else
  1035. dmares = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1036. "dma");
  1037. priv->dma_regs = devm_ioremap_resource(&pdev->dev, dmares);
  1038. if (IS_ERR(priv->dma_regs)) {
  1039. netdev_err(ndev, "failed to map dma regs\n");
  1040. return PTR_ERR(priv->dma_regs);
  1041. }
  1042. if (version <= NIXGE_V2) {
  1043. priv->ctrl_regs = priv->dma_regs + NIXGE_REG_CTRL_OFFSET;
  1044. } else {
  1045. ctrlres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1046. "ctrl");
  1047. priv->ctrl_regs = devm_ioremap_resource(&pdev->dev, ctrlres);
  1048. }
  1049. if (IS_ERR(priv->ctrl_regs)) {
  1050. netdev_err(ndev, "failed to map ctrl regs\n");
  1051. return PTR_ERR(priv->ctrl_regs);
  1052. }
  1053. return 0;
  1054. }
  1055. static int nixge_probe(struct platform_device *pdev)
  1056. {
  1057. struct device_node *mn, *phy_node;
  1058. struct nixge_priv *priv;
  1059. struct net_device *ndev;
  1060. const u8 *mac_addr;
  1061. int err;
  1062. ndev = alloc_etherdev(sizeof(*priv));
  1063. if (!ndev)
  1064. return -ENOMEM;
  1065. platform_set_drvdata(pdev, ndev);
  1066. SET_NETDEV_DEV(ndev, &pdev->dev);
  1067. ndev->features = NETIF_F_SG;
  1068. ndev->netdev_ops = &nixge_netdev_ops;
  1069. ndev->ethtool_ops = &nixge_ethtool_ops;
  1070. /* MTU range: 64 - 9000 */
  1071. ndev->min_mtu = 64;
  1072. ndev->max_mtu = NIXGE_JUMBO_MTU;
  1073. mac_addr = nixge_get_nvmem_address(&pdev->dev);
  1074. if (mac_addr && is_valid_ether_addr(mac_addr)) {
  1075. ether_addr_copy(ndev->dev_addr, mac_addr);
  1076. kfree(mac_addr);
  1077. } else {
  1078. eth_hw_addr_random(ndev);
  1079. }
  1080. priv = netdev_priv(ndev);
  1081. priv->ndev = ndev;
  1082. priv->dev = &pdev->dev;
  1083. netif_napi_add(ndev, &priv->napi, nixge_poll, NAPI_POLL_WEIGHT);
  1084. err = nixge_of_get_resources(pdev);
  1085. if (err)
  1086. goto free_netdev;
  1087. __nixge_hw_set_mac_address(ndev);
  1088. priv->tx_irq = platform_get_irq_byname(pdev, "tx");
  1089. if (priv->tx_irq < 0) {
  1090. netdev_err(ndev, "could not find 'tx' irq");
  1091. err = priv->tx_irq;
  1092. goto free_netdev;
  1093. }
  1094. priv->rx_irq = platform_get_irq_byname(pdev, "rx");
  1095. if (priv->rx_irq < 0) {
  1096. netdev_err(ndev, "could not find 'rx' irq");
  1097. err = priv->rx_irq;
  1098. goto free_netdev;
  1099. }
  1100. priv->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
  1101. priv->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
  1102. mn = of_get_child_by_name(pdev->dev.of_node, "mdio");
  1103. if (mn) {
  1104. err = nixge_mdio_setup(priv, mn);
  1105. of_node_put(mn);
  1106. if (err) {
  1107. netdev_err(ndev, "error registering mdio bus");
  1108. goto free_netdev;
  1109. }
  1110. }
  1111. priv->phy_mode = of_get_phy_mode(pdev->dev.of_node);
  1112. if ((int)priv->phy_mode < 0) {
  1113. netdev_err(ndev, "not find \"phy-mode\" property\n");
  1114. err = -EINVAL;
  1115. goto unregister_mdio;
  1116. }
  1117. phy_node = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  1118. if (!phy_node && of_phy_is_fixed_link(pdev->dev.of_node)) {
  1119. err = of_phy_register_fixed_link(pdev->dev.of_node);
  1120. if (err < 0) {
  1121. netdev_err(ndev, "broken fixed-link specification\n");
  1122. goto unregister_mdio;
  1123. }
  1124. phy_node = of_node_get(pdev->dev.of_node);
  1125. }
  1126. priv->phy_node = phy_node;
  1127. err = register_netdev(priv->ndev);
  1128. if (err) {
  1129. netdev_err(ndev, "register_netdev() error (%i)\n", err);
  1130. goto free_phy;
  1131. }
  1132. return 0;
  1133. free_phy:
  1134. if (of_phy_is_fixed_link(pdev->dev.of_node))
  1135. of_phy_deregister_fixed_link(pdev->dev.of_node);
  1136. of_node_put(phy_node);
  1137. unregister_mdio:
  1138. if (priv->mii_bus)
  1139. mdiobus_unregister(priv->mii_bus);
  1140. free_netdev:
  1141. free_netdev(ndev);
  1142. return err;
  1143. }
  1144. static int nixge_remove(struct platform_device *pdev)
  1145. {
  1146. struct net_device *ndev = platform_get_drvdata(pdev);
  1147. struct nixge_priv *priv = netdev_priv(ndev);
  1148. unregister_netdev(ndev);
  1149. if (of_phy_is_fixed_link(pdev->dev.of_node))
  1150. of_phy_deregister_fixed_link(pdev->dev.of_node);
  1151. of_node_put(priv->phy_node);
  1152. if (priv->mii_bus)
  1153. mdiobus_unregister(priv->mii_bus);
  1154. free_netdev(ndev);
  1155. return 0;
  1156. }
  1157. static struct platform_driver nixge_driver = {
  1158. .probe = nixge_probe,
  1159. .remove = nixge_remove,
  1160. .driver = {
  1161. .name = "nixge",
  1162. .of_match_table = of_match_ptr(nixge_dt_ids),
  1163. },
  1164. };
  1165. module_platform_driver(nixge_driver);
  1166. MODULE_LICENSE("GPL v2");
  1167. MODULE_DESCRIPTION("National Instruments XGE Management MAC");
  1168. MODULE_AUTHOR("Moritz Fischer <mdf@kernel.org>");