blob: 4b514c705e57198e7f42107eb941bdc98672ceef [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000035#include <linux/ip.h>
36#include <linux/udp.h>
37#include <linux/tcp.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Nicolas Ferre1b447912013-06-04 21:57:11 +000040#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050042
Zach Brownb410d132016-10-19 09:56:57 -050043#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050044#define MIN_RX_RING_SIZE 64
45#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000046#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050047 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
Zach Brownb410d132016-10-19 09:56:57 -050049#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050050#define MIN_TX_RING_SIZE 64
51#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000052#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050053 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010054
Nicolas Ferre909a8582012-11-19 06:00:21 +000055/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050056#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
58#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
59 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000060#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
61 | MACB_BIT(ISR_RLE) \
62 | MACB_BIT(TXERR))
63#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
64
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000065/* Max length of transmit frame must be a multiple of 8 bytes */
66#define MACB_TX_LEN_ALIGN 8
67#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
68#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020069
Jarod Wilson44770e12016-10-17 15:54:17 -040070#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070071#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053072
Sergio Prado3e2a5e12016-02-09 12:07:16 -020073#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
74#define MACB_WOL_ENABLED (0x1 << 1)
75
Moritz Fischer64ec42f2016-03-29 19:11:12 -070076/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000077 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
78 */
79#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010080
Rafal Ozieblodc97a892017-01-27 15:08:20 +000081/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010082 * depends on hardware configuration:
83 *
84 * 1. dma address width 32 bits:
85 * word 1: 32 bit address of Data Buffer
86 * word 2: control
87 *
88 * 2. dma address width 64 bits:
89 * word 1: 32 bit address of Data Buffer
90 * word 2: control
91 * word 3: upper 32 bit address of Data Buffer
92 * word 4: unused
93 *
94 * 3. dma address width 32 bits with hardware timestamping:
95 * word 1: 32 bit address of Data Buffer
96 * word 2: control
97 * word 3: timestamp word 1
98 * word 4: timestamp word 2
99 *
100 * 4. dma address width 64 bits with hardware timestamping:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 * word 3: upper 32 bit address of Data Buffer
104 * word 4: unused
105 * word 5: timestamp word 1
106 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000107 */
108static unsigned int macb_dma_desc_get_size(struct macb *bp)
109{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100110#ifdef MACB_EXT_DESC
111 unsigned int desc_size;
112
113 switch (bp->hw_dma_cap) {
114 case HW_DMA_CAP_64B:
115 desc_size = sizeof(struct macb_dma_desc)
116 + sizeof(struct macb_dma_desc_64);
117 break;
118 case HW_DMA_CAP_PTP:
119 desc_size = sizeof(struct macb_dma_desc)
120 + sizeof(struct macb_dma_desc_ptp);
121 break;
122 case HW_DMA_CAP_64B_PTP:
123 desc_size = sizeof(struct macb_dma_desc)
124 + sizeof(struct macb_dma_desc_64)
125 + sizeof(struct macb_dma_desc_ptp);
126 break;
127 default:
128 desc_size = sizeof(struct macb_dma_desc);
129 }
130 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000131#endif
132 return sizeof(struct macb_dma_desc);
133}
134
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100135static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000136{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100137#ifdef MACB_EXT_DESC
138 switch (bp->hw_dma_cap) {
139 case HW_DMA_CAP_64B:
140 case HW_DMA_CAP_PTP:
141 desc_idx <<= 1;
142 break;
143 case HW_DMA_CAP_64B_PTP:
144 desc_idx *= 3;
145 break;
146 default:
147 break;
148 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100150 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000151}
152
153#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
154static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
155{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100156 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
157 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
158 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000159}
160#endif
161
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000162/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500163static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000164{
Zach Brownb410d132016-10-19 09:56:57 -0500165 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000166}
167
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100168static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
169 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000170{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000171 index = macb_tx_ring_wrap(queue->bp, index);
172 index = macb_adj_dma_desc_idx(queue->bp, index);
173 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000174}
175
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100176static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
177 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000178{
Zach Brownb410d132016-10-19 09:56:57 -0500179 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000180}
181
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100182static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183{
184 dma_addr_t offset;
185
Zach Brownb410d132016-10-19 09:56:57 -0500186 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000187 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100189 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000190}
191
Zach Brownb410d132016-10-19 09:56:57 -0500192static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000193{
Zach Brownb410d132016-10-19 09:56:57 -0500194 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195}
196
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000197static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000198{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000199 index = macb_rx_ring_wrap(queue->bp, index);
200 index = macb_adj_dma_desc_idx(queue->bp, index);
201 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000202}
203
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000204static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000206 return queue->rx_buffers + queue->bp->rx_buffer_size *
207 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000208}
209
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300210/* I/O accessors */
211static u32 hw_readl_native(struct macb *bp, int offset)
212{
213 return __raw_readl(bp->regs + offset);
214}
215
216static void hw_writel_native(struct macb *bp, int offset, u32 value)
217{
218 __raw_writel(value, bp->regs + offset);
219}
220
221static u32 hw_readl(struct macb *bp, int offset)
222{
223 return readl_relaxed(bp->regs + offset);
224}
225
226static void hw_writel(struct macb *bp, int offset, u32 value)
227{
228 writel_relaxed(value, bp->regs + offset);
229}
230
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700231/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700232 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300233 * descriptor access.
234 */
235static bool hw_is_native_io(void __iomem *addr)
236{
237 u32 value = MACB_BIT(LLB);
238
239 __raw_writel(value, addr + MACB_NCR);
240 value = __raw_readl(addr + MACB_NCR);
241
242 /* Write 0 back to disable everything */
243 __raw_writel(0, addr + MACB_NCR);
244
245 return value == MACB_BIT(LLB);
246}
247
248static bool hw_is_gem(void __iomem *addr, bool native_io)
249{
250 u32 id;
251
252 if (native_io)
253 id = __raw_readl(addr + MACB_MID);
254 else
255 id = readl_relaxed(addr + MACB_MID);
256
257 return MACB_BFEXT(IDNUM, id) >= 0x2;
258}
259
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100260static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261{
262 u32 bottom;
263 u16 top;
264
265 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000266 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100267 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000268 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000269
270 /* Clear unused address register sets */
271 macb_or_gem_writel(bp, SA2B, 0);
272 macb_or_gem_writel(bp, SA2T, 0);
273 macb_or_gem_writel(bp, SA3B, 0);
274 macb_or_gem_writel(bp, SA3T, 0);
275 macb_or_gem_writel(bp, SA4B, 0);
276 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100277}
278
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100279static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100280{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000281 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 u32 bottom;
283 u16 top;
284 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000285 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100286
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900287 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000288
Moritz Fischeraa50b552016-03-29 19:11:13 -0700289 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000290 for (i = 0; i < 4; i++) {
291 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
292 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100293
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000294 if (pdata && pdata->rev_eth_addr) {
295 addr[5] = bottom & 0xff;
296 addr[4] = (bottom >> 8) & 0xff;
297 addr[3] = (bottom >> 16) & 0xff;
298 addr[2] = (bottom >> 24) & 0xff;
299 addr[1] = top & 0xff;
300 addr[0] = (top & 0xff00) >> 8;
301 } else {
302 addr[0] = bottom & 0xff;
303 addr[1] = (bottom >> 8) & 0xff;
304 addr[2] = (bottom >> 16) & 0xff;
305 addr[3] = (bottom >> 24) & 0xff;
306 addr[4] = top & 0xff;
307 addr[5] = (top >> 8) & 0xff;
308 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000310 if (is_valid_ether_addr(addr)) {
311 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
312 return;
313 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700314 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300316 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000317 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100318}
319
frederic RODO6c36a702007-07-12 19:07:24 +0200320static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321{
frederic RODO6c36a702007-07-12 19:07:24 +0200322 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323 int value;
324
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100325 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
326 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200327 | MACB_BF(PHYA, mii_id)
328 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100329 | MACB_BF(CODE, MACB_MAN_CODE)));
330
frederic RODO6c36a702007-07-12 19:07:24 +0200331 /* wait for end of transfer */
332 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
333 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334
335 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336
337 return value;
338}
339
frederic RODO6c36a702007-07-12 19:07:24 +0200340static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
341 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342{
frederic RODO6c36a702007-07-12 19:07:24 +0200343 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344
345 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
346 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200347 | MACB_BF(PHYA, mii_id)
348 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200350 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
frederic RODO6c36a702007-07-12 19:07:24 +0200352 /* wait for end of transfer */
353 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
354 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100355
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356 return 0;
357}
358
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800359/**
360 * macb_set_tx_clk() - Set a clock to a new frequency
361 * @clk Pointer to the clock to change
362 * @rate New frequency in Hz
363 * @dev Pointer to the struct net_device
364 */
365static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
366{
367 long ferr, rate, rate_rounded;
368
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100369 if (!clk)
370 return;
371
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800372 switch (speed) {
373 case SPEED_10:
374 rate = 2500000;
375 break;
376 case SPEED_100:
377 rate = 25000000;
378 break;
379 case SPEED_1000:
380 rate = 125000000;
381 break;
382 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800383 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800384 }
385
386 rate_rounded = clk_round_rate(clk, rate);
387 if (rate_rounded < 0)
388 return;
389
390 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
391 * is not satisfied.
392 */
393 ferr = abs(rate_rounded - rate);
394 ferr = DIV_ROUND_UP(ferr, rate / 100000);
395 if (ferr > 5)
396 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700397 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800398
399 if (clk_set_rate(clk, rate_rounded))
400 netdev_err(dev, "adjusting tx_clk failed.\n");
401}
402
frederic RODO6c36a702007-07-12 19:07:24 +0200403static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100404{
frederic RODO6c36a702007-07-12 19:07:24 +0200405 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200406 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200407 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200408 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100409
frederic RODO6c36a702007-07-12 19:07:24 +0200410 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 if (phydev->link) {
413 if ((bp->speed != phydev->speed) ||
414 (bp->duplex != phydev->duplex)) {
415 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100416
frederic RODO6c36a702007-07-12 19:07:24 +0200417 reg = macb_readl(bp, NCFGR);
418 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000419 if (macb_is_gem(bp))
420 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200421
422 if (phydev->duplex)
423 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900424 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200425 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200426 if (phydev->speed == SPEED_1000 &&
427 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000428 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200429
Patrice Vilchez140b7552012-10-31 06:04:50 +0000430 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200431
432 bp->speed = phydev->speed;
433 bp->duplex = phydev->duplex;
434 status_change = 1;
435 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436 }
437
frederic RODO6c36a702007-07-12 19:07:24 +0200438 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700439 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200440 bp->speed = 0;
441 bp->duplex = -1;
442 }
443 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
frederic RODO6c36a702007-07-12 19:07:24 +0200445 status_change = 1;
446 }
447
448 spin_unlock_irqrestore(&bp->lock, flags);
449
450 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000451 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500452 /* Update the TX clock rate if and only if the link is
453 * up and there has been a link change.
454 */
455 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
456
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000457 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000458 netdev_info(dev, "link up (%d/%s)\n",
459 phydev->speed,
460 phydev->duplex == DUPLEX_FULL ?
461 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000462 } else {
463 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000464 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000465 }
frederic RODO6c36a702007-07-12 19:07:24 +0200466 }
467}
468
469/* based on au1000_eth. c*/
470static int macb_mii_probe(struct net_device *dev)
471{
472 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000473 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000474 struct phy_device *phydev;
Brad Mouring739de9a2018-03-13 16:32:13 -0500475 struct device_node *np;
476 int phy_irq, ret, i;
477
478 pdata = dev_get_platdata(&bp->pdev->dev);
479 np = bp->pdev->dev.of_node;
480 ret = 0;
481
482 if (np) {
483 if (of_phy_is_fixed_link(np)) {
484 if (of_phy_register_fixed_link(np) < 0) {
485 dev_err(&bp->pdev->dev,
486 "broken fixed-link specification\n");
487 return -ENODEV;
488 }
489 bp->phy_node = of_node_get(np);
490 } else {
491 /* fallback to standard phy registration if no phy were
492 * found during dt phy registration
493 */
494 if (!phy_find_first(bp->mii_bus)) {
495 for (i = 0; i < PHY_MAX_ADDR; i++) {
496 struct phy_device *phydev;
497
498 phydev = mdiobus_scan(bp->mii_bus, i);
499 if (IS_ERR(phydev) &&
500 PTR_ERR(phydev) != -ENODEV) {
501 ret = PTR_ERR(phydev);
502 break;
503 }
504 }
505
506 if (ret)
507 return -ENODEV;
508 }
509 }
510 }
frederic RODO6c36a702007-07-12 19:07:24 +0200511
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200512 if (bp->phy_node) {
513 phydev = of_phy_connect(dev, bp->phy_node,
514 &macb_handle_link_change, 0,
515 bp->phy_interface);
516 if (!phydev)
517 return -ENODEV;
518 } else {
519 phydev = phy_find_first(bp->mii_bus);
520 if (!phydev) {
521 netdev_err(dev, "no PHY found\n");
522 return -ENXIO;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000523 }
frederic RODO6c36a702007-07-12 19:07:24 +0200524
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200525 if (pdata) {
526 if (gpio_is_valid(pdata->phy_irq_pin)) {
527 ret = devm_gpio_request(&bp->pdev->dev,
528 pdata->phy_irq_pin, "phy int");
529 if (!ret) {
530 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
531 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
532 }
533 } else {
534 phydev->irq = PHY_POLL;
535 }
536 }
537
538 /* attach the mac to the phy */
539 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
540 bp->phy_interface);
541 if (ret) {
542 netdev_err(dev, "Could not attach to PHY\n");
543 return ret;
544 }
frederic RODO6c36a702007-07-12 19:07:24 +0200545 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100546
frederic RODO6c36a702007-07-12 19:07:24 +0200547 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200548 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000549 phydev->supported &= PHY_GBIT_FEATURES;
550 else
551 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100552
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500553 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
554 phydev->supported &= ~SUPPORTED_1000baseT_Half;
555
frederic RODO6c36a702007-07-12 19:07:24 +0200556 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100557
frederic RODO6c36a702007-07-12 19:07:24 +0200558 bp->link = 0;
559 bp->speed = 0;
560 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200561
562 return 0;
563}
564
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100565static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200566{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000567 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200568 struct device_node *np;
Brad Mouring739de9a2018-03-13 16:32:13 -0500569 int err, i;
frederic RODO6c36a702007-07-12 19:07:24 +0200570
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200571 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200572 macb_writel(bp, NCR, MACB_BIT(MPE));
573
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700574 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700575 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200576 err = -ENOMEM;
577 goto err_out;
578 }
579
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700580 bp->mii_bus->name = "MACB_mii_bus";
581 bp->mii_bus->read = &macb_mdio_read;
582 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000583 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700584 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700585 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700586 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900587 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700588
Jamie Iles91523942011-02-28 04:05:25 +0000589 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200590
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200591 np = bp->pdev->dev.of_node;
Brad Mouring739de9a2018-03-13 16:32:13 -0500592
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200593 if (np) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500594 err = of_mdiobus_register(bp->mii_bus, np);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200595 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000596 for (i = 0; i < PHY_MAX_ADDR; i++)
597 bp->mii_bus->irq[i] = PHY_POLL;
598
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200599 if (pdata)
600 bp->mii_bus->phy_mask = pdata->phy_mask;
601
602 err = mdiobus_register(bp->mii_bus);
603 }
604
605 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100606 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200607
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200608 err = macb_mii_probe(bp->dev);
609 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200610 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200611
612 return 0;
613
614err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700615 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100616 if (np && of_phy_is_fixed_link(np))
617 of_phy_deregister_fixed_link(np);
Brad Mouring739de9a2018-03-13 16:32:13 -0500618err_out_free_mdiobus:
619 of_node_put(bp->phy_node);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700620 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200621err_out:
622 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100623}
624
625static void macb_update_stats(struct macb *bp)
626{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000627 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
628 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300629 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100630
631 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
632
Moritz Fischer96ec6312016-03-29 19:11:11 -0700633 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700634 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100635}
636
Nicolas Ferree86cd532012-10-31 06:04:57 +0000637static int macb_halt_tx(struct macb *bp)
638{
639 unsigned long halt_time, timeout;
640 u32 status;
641
642 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
643
644 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
645 do {
646 halt_time = jiffies;
647 status = macb_readl(bp, TSR);
648 if (!(status & MACB_BIT(TGO)))
649 return 0;
650
651 usleep_range(10, 250);
652 } while (time_before(halt_time, timeout));
653
654 return -ETIMEDOUT;
655}
656
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200657static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
658{
659 if (tx_skb->mapping) {
660 if (tx_skb->mapped_as_page)
661 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
662 tx_skb->size, DMA_TO_DEVICE);
663 else
664 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
665 tx_skb->size, DMA_TO_DEVICE);
666 tx_skb->mapping = 0;
667 }
668
669 if (tx_skb->skb) {
670 dev_kfree_skb_any(tx_skb->skb);
671 tx_skb->skb = NULL;
672 }
673}
674
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000675static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530676{
Harini Katakamfff80192016-08-09 13:15:53 +0530677#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000678 struct macb_dma_desc_64 *desc_64;
679
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100680 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000681 desc_64 = macb_64b_desc(bp, desc);
682 desc_64->addrh = upper_32_bits(addr);
683 }
Harini Katakamfff80192016-08-09 13:15:53 +0530684#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000685 desc->addr = lower_32_bits(addr);
686}
687
688static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
689{
690 dma_addr_t addr = 0;
691#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
692 struct macb_dma_desc_64 *desc_64;
693
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100694 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000695 desc_64 = macb_64b_desc(bp, desc);
696 addr = ((u64)(desc_64->addrh) << 32);
697 }
698#endif
699 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
700 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530701}
702
Nicolas Ferree86cd532012-10-31 06:04:57 +0000703static void macb_tx_error_task(struct work_struct *work)
704{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100705 struct macb_queue *queue = container_of(work, struct macb_queue,
706 tx_error_task);
707 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000708 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100709 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000710 struct sk_buff *skb;
711 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100712 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000713
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100714 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
715 (unsigned int)(queue - bp->queues),
716 queue->tx_tail, queue->tx_head);
717
718 /* Prevent the queue IRQ handlers from running: each of them may call
719 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
720 * As explained below, we have to halt the transmission before updating
721 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
722 * network engine about the macb/gem being halted.
723 */
724 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000725
726 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100727 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000728
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700729 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000730 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100731 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000732 */
733 if (macb_halt_tx(bp))
734 /* Just complain for now, reinitializing TX path can be good */
735 netdev_err(bp->dev, "BUG: halt tx timed out\n");
736
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700737 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000738 * Free transmit buffers in upper layer.
739 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
741 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000742
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100743 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000744 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100745 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000746 skb = tx_skb->skb;
747
748 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200749 /* skb is set for the last buffer of the frame */
750 while (!skb) {
751 macb_tx_unmap(bp, tx_skb);
752 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100753 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200754 skb = tx_skb->skb;
755 }
756
757 /* ctrl still refers to the first buffer descriptor
758 * since it's the only one written back by the hardware
759 */
760 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
761 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500762 macb_tx_ring_wrap(bp, tail),
763 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200764 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000765 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200766 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000767 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200768 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000769 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700770 /* "Buffers exhausted mid-frame" errors may only happen
771 * if the driver is buggy, so complain loudly about
772 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000773 */
774 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
775 netdev_err(bp->dev,
776 "BUG: TX buffers exhausted mid-frame\n");
777
778 desc->ctrl = ctrl | MACB_BIT(TX_USED);
779 }
780
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200781 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000782 }
783
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100784 /* Set end of TX queue */
785 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000786 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100787 desc->ctrl = MACB_BIT(TX_USED);
788
Nicolas Ferree86cd532012-10-31 06:04:57 +0000789 /* Make descriptor updates visible to hardware */
790 wmb();
791
792 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000793 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530794#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100795 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000796 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530797#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000798 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100799 queue->tx_head = 0;
800 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000801
802 /* Housework before enabling TX IRQ */
803 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100804 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
805
806 /* Now we are ready to start transmission again */
807 netif_tx_start_all_queues(bp->dev);
808 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
809
810 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000811}
812
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100813static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100814{
815 unsigned int tail;
816 unsigned int head;
817 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100818 struct macb *bp = queue->bp;
819 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100820
821 status = macb_readl(bp, TSR);
822 macb_writel(bp, TSR, status);
823
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000824 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100825 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000826
Nicolas Ferree86cd532012-10-31 06:04:57 +0000827 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700828 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100830 head = queue->tx_head;
831 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000832 struct macb_tx_skb *tx_skb;
833 struct sk_buff *skb;
834 struct macb_dma_desc *desc;
835 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100836
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100837 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100838
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000839 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000841
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000842 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200844 /* TX_USED bit is only set by hardware on the very first buffer
845 * descriptor of the transmitted frame.
846 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000847 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100848 break;
849
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200850 /* Process all buffers of the current transmitted frame */
851 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100852 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200853 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000854
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200855 /* First, update TX stats if needed */
856 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100857 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
858 /* skb now belongs to timestamp buffer
859 * and will be removed later
860 */
861 tx_skb->skb = NULL;
862 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200863 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500864 macb_tx_ring_wrap(bp, tail),
865 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200866 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000867 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200868 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000869 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200870 }
871
872 /* Now we can safely release resources */
873 macb_tx_unmap(bp, tx_skb);
874
875 /* skb is set only for the last buffer of the frame.
876 * WARNING: at this point skb has been freed by
877 * macb_tx_unmap().
878 */
879 if (skb)
880 break;
881 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100882 }
883
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100884 queue->tx_tail = tail;
885 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
886 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500887 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100888 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100889}
890
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000891static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000892{
893 unsigned int entry;
894 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000895 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000896 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000897 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000898
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000899 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
900 bp->rx_ring_size) > 0) {
901 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000902
903 /* Make hw descriptor updates visible to CPU */
904 rmb();
905
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000906 queue->rx_prepared_head++;
907 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000908
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000909 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000910 /* allocate sk_buff for this free entry in ring */
911 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700912 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000913 netdev_err(bp->dev,
914 "Unable to allocate sk_buff\n");
915 break;
916 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000917
918 /* now fill corresponding descriptor entry */
919 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700920 bp->rx_buffer_size,
921 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800922 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
923 dev_kfree_skb(skb);
924 break;
925 }
926
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000927 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000928
Zach Brownb410d132016-10-19 09:56:57 -0500929 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000930 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000931 macb_set_addr(bp, desc, paddr);
932 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000933
934 /* properly align Ethernet header */
935 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530936 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000937 desc->addr &= ~MACB_BIT(RX_USED);
938 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000939 }
940 }
941
942 /* Make descriptor updates visible to hardware */
943 wmb();
944
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000945 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
946 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000947}
948
949/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000950static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000951 unsigned int end)
952{
953 unsigned int frag;
954
955 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000956 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700957
Nicolas Ferre4df95132013-06-04 21:57:12 +0000958 desc->addr &= ~MACB_BIT(RX_USED);
959 }
960
961 /* Make descriptor updates visible to hardware */
962 wmb();
963
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700964 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000965 * whatever caused this is updated, so we don't have to record
966 * anything.
967 */
968}
969
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000970static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000971{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000972 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000973 unsigned int len;
974 unsigned int entry;
975 struct sk_buff *skb;
976 struct macb_dma_desc *desc;
977 int count = 0;
978
979 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530980 u32 ctrl;
981 dma_addr_t addr;
982 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000983
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000984 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
985 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000986
987 /* Make hw descriptor updates visible to CPU */
988 rmb();
989
Harini Katakamfff80192016-08-09 13:15:53 +0530990 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000991 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000992 ctrl = desc->ctrl;
993
Harini Katakamfff80192016-08-09 13:15:53 +0530994 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000995 break;
996
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000997 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000998 count++;
999
1000 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1001 netdev_err(bp->dev,
1002 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001003 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001004 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001005 break;
1006 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001007 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001008 if (unlikely(!skb)) {
1009 netdev_err(bp->dev,
1010 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001011 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001012 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001013 break;
1014 }
1015 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001016 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301017 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001018
1019 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1020
1021 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001022 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001023 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001024
1025 skb->protocol = eth_type_trans(skb, bp->dev);
1026 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001027 if (bp->dev->features & NETIF_F_RXCSUM &&
1028 !(bp->dev->flags & IFF_PROMISC) &&
1029 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1030 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001031
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001032 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001033 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001034 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001035 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001036
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001037 gem_ptp_do_rxstamp(bp, skb, desc);
1038
Nicolas Ferre4df95132013-06-04 21:57:12 +00001039#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1040 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1041 skb->len, skb->csum);
1042 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001043 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001044 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1045 skb->data, 32, true);
1046#endif
1047
1048 netif_receive_skb(skb);
1049 }
1050
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001051 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001052
1053 return count;
1054}
1055
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001056static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001057 unsigned int last_frag)
1058{
1059 unsigned int len;
1060 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001061 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001062 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001063 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001064 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001066 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301067 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001068
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001069 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001070 macb_rx_ring_wrap(bp, first_frag),
1071 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001072
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001073 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001074 * first buffer. Since the header is 14 bytes, this makes the
1075 * payload word-aligned.
1076 *
1077 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1078 * the two padding bytes into the skb so that we avoid hitting
1079 * the slowpath in memcpy(), and pull them off afterwards.
1080 */
1081 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001082 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001083 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001084 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001085 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001086 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001087 if (frag == last_frag)
1088 break;
1089 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001090
1091 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001092 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001093
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094 return 1;
1095 }
1096
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001097 offset = 0;
1098 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001099 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001100 skb_put(skb, len);
1101
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001102 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001103 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001104
1105 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001106 if (unlikely(frag != last_frag)) {
1107 dev_kfree_skb_any(skb);
1108 return -1;
1109 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001110 frag_len = len - offset;
1111 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001112 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001113 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001114 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001115 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001116 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001117 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001118
1119 if (frag == last_frag)
1120 break;
1121 }
1122
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001123 /* Make descriptor updates visible to hardware */
1124 wmb();
1125
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001126 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001127 skb->protocol = eth_type_trans(skb, bp->dev);
1128
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001129 bp->dev->stats.rx_packets++;
1130 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001131 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001132 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001133 netif_receive_skb(skb);
1134
1135 return 0;
1136}
1137
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001138static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001139{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001140 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001141 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001142 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001143 int i;
1144
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001145 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001146 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001147 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001148 macb_set_addr(bp, desc, addr);
1149 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001150 addr += bp->rx_buffer_size;
1151 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001152 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001153 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001154}
1155
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001156static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001157{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001158 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001159 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001160 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001161 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001162 int first_frag = -1;
1163
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001164 for (tail = queue->rx_tail; budget > 0; tail++) {
1165 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001166 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001167
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001168 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001169 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001170
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001171 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001172
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001173 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001174 break;
1175
1176 if (ctrl & MACB_BIT(RX_SOF)) {
1177 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001178 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001179 first_frag = tail;
1180 }
1181
1182 if (ctrl & MACB_BIT(RX_EOF)) {
1183 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001184
1185 if (unlikely(first_frag == -1)) {
1186 reset_rx_queue = true;
1187 continue;
1188 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001189
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001190 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001192 if (unlikely(dropped < 0)) {
1193 reset_rx_queue = true;
1194 continue;
1195 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 if (!dropped) {
1197 received++;
1198 budget--;
1199 }
1200 }
1201 }
1202
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001203 if (unlikely(reset_rx_queue)) {
1204 unsigned long flags;
1205 u32 ctrl;
1206
1207 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1208
1209 spin_lock_irqsave(&bp->lock, flags);
1210
1211 ctrl = macb_readl(bp, NCR);
1212 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1213
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001214 macb_init_rx_ring(queue);
1215 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001216
1217 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1218
1219 spin_unlock_irqrestore(&bp->lock, flags);
1220 return received;
1221 }
1222
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001223 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001224 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001226 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227
1228 return received;
1229}
1230
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001231static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001232{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001233 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1234 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001235 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236 u32 status;
1237
1238 status = macb_readl(bp, RSR);
1239 macb_writel(bp, RSR, status);
1240
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001241 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001242 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001244 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001245 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001246 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247
Nicolas Ferre8770e912013-02-12 11:08:48 +01001248 /* Packets received while interrupts were disabled */
1249 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001250 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001251 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001252 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001253 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001254 } else {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001255 queue_writel(queue, IER, MACB_RX_INT_FLAGS);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001256 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001257 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001258
1259 /* TODO: Handle errors */
1260
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001261 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001262}
1263
Harini Katakam032dc412018-01-27 12:09:01 +05301264static void macb_hresp_error_task(unsigned long data)
1265{
1266 struct macb *bp = (struct macb *)data;
1267 struct net_device *dev = bp->dev;
1268 struct macb_queue *queue = bp->queues;
1269 unsigned int q;
1270 u32 ctrl;
1271
1272 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1273 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
1274 MACB_TX_INT_FLAGS |
1275 MACB_BIT(HRESP));
1276 }
1277 ctrl = macb_readl(bp, NCR);
1278 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1279 macb_writel(bp, NCR, ctrl);
1280
1281 netif_tx_stop_all_queues(dev);
1282 netif_carrier_off(dev);
1283
1284 bp->macbgem_ops.mog_init_rings(bp);
1285
1286 /* Initialize TX and RX buffers */
1287 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1288 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1289#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1290 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1291 queue_writel(queue, RBQPH,
1292 upper_32_bits(queue->rx_ring_dma));
1293#endif
1294 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1295#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1296 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1297 queue_writel(queue, TBQPH,
1298 upper_32_bits(queue->tx_ring_dma));
1299#endif
1300
1301 /* Enable interrupts */
1302 queue_writel(queue, IER,
1303 MACB_RX_INT_FLAGS |
1304 MACB_TX_INT_FLAGS |
1305 MACB_BIT(HRESP));
1306 }
1307
1308 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1309 macb_writel(bp, NCR, ctrl);
1310
1311 netif_carrier_on(dev);
1312 netif_tx_start_all_queues(dev);
1313}
1314
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001315static irqreturn_t macb_interrupt(int irq, void *dev_id)
1316{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001317 struct macb_queue *queue = dev_id;
1318 struct macb *bp = queue->bp;
1319 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001320 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001321
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001322 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001323
1324 if (unlikely(!status))
1325 return IRQ_NONE;
1326
1327 spin_lock(&bp->lock);
1328
1329 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001330 /* close possible race with dev_close */
1331 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001332 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001333 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1334 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001335 break;
1336 }
1337
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001338 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1339 (unsigned int)(queue - bp->queues),
1340 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001341
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001342 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001343 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001344 * until we have processed the buffers. The
1345 * scheduling call may fail if the poll routine
1346 * is already scheduled, so disable interrupts
1347 * now.
1348 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001349 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001350 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001351 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001352
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001353 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001354 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001355 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001356 }
1357 }
1358
Nicolas Ferree86cd532012-10-31 06:04:57 +00001359 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001360 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1361 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001362
1363 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001364 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001365
Nicolas Ferree86cd532012-10-31 06:04:57 +00001366 break;
1367 }
1368
1369 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001372 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001373 * add that if/when we get our hands on a full-blown MII PHY.
1374 */
1375
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001376 /* There is a hardware issue under heavy load where DMA can
1377 * stop, this causes endless "used buffer descriptor read"
1378 * interrupts but it can be cleared by re-enabling RX. See
1379 * the at91 manual, section 41.3.1 or the Zynq manual
1380 * section 16.7.4 for details.
1381 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001382 if (status & MACB_BIT(RXUBR)) {
1383 ctrl = macb_readl(bp, NCR);
1384 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001385 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001386 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1387
1388 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001389 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001390 }
1391
Alexander Steinb19f7f72011-04-13 05:03:24 +00001392 if (status & MACB_BIT(ISR_ROVR)) {
1393 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001394 if (macb_is_gem(bp))
1395 bp->hw_stats.gem.rx_overruns++;
1396 else
1397 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001398
1399 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001400 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001401 }
1402
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001403 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301404 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001405 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001406
1407 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001408 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001410 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001411 }
1412
1413 spin_unlock(&bp->lock);
1414
1415 return IRQ_HANDLED;
1416}
1417
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001418#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001419/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001420 * to allow network i/o with interrupts disabled.
1421 */
1422static void macb_poll_controller(struct net_device *dev)
1423{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001424 struct macb *bp = netdev_priv(dev);
1425 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001426 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001427 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001428
1429 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001430 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1431 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001432 local_irq_restore(flags);
1433}
1434#endif
1435
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001436static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001437 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001438 struct sk_buff *skb,
1439 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001440{
1441 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001442 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001443 struct macb_tx_skb *tx_skb = NULL;
1444 struct macb_dma_desc *desc;
1445 unsigned int offset, size, count = 0;
1446 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001447 unsigned int eof = 1, mss_mfs = 0;
1448 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1449
1450 /* LSO */
1451 if (skb_shinfo(skb)->gso_size != 0) {
1452 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1453 /* UDP - UFO */
1454 lso_ctrl = MACB_LSO_UFO_ENABLE;
1455 else
1456 /* TCP - TSO */
1457 lso_ctrl = MACB_LSO_TSO_ENABLE;
1458 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001459
1460 /* First, map non-paged data */
1461 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001462
1463 /* first buffer length */
1464 size = hdrlen;
1465
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001466 offset = 0;
1467 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001468 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001469 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001470
1471 mapping = dma_map_single(&bp->pdev->dev,
1472 skb->data + offset,
1473 size, DMA_TO_DEVICE);
1474 if (dma_mapping_error(&bp->pdev->dev, mapping))
1475 goto dma_error;
1476
1477 /* Save info to properly release resources */
1478 tx_skb->skb = NULL;
1479 tx_skb->mapping = mapping;
1480 tx_skb->size = size;
1481 tx_skb->mapped_as_page = false;
1482
1483 len -= size;
1484 offset += size;
1485 count++;
1486 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001487
1488 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001489 }
1490
1491 /* Then, map paged data from fragments */
1492 for (f = 0; f < nr_frags; f++) {
1493 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1494
1495 len = skb_frag_size(frag);
1496 offset = 0;
1497 while (len) {
1498 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001499 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001500 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001501
1502 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1503 offset, size, DMA_TO_DEVICE);
1504 if (dma_mapping_error(&bp->pdev->dev, mapping))
1505 goto dma_error;
1506
1507 /* Save info to properly release resources */
1508 tx_skb->skb = NULL;
1509 tx_skb->mapping = mapping;
1510 tx_skb->size = size;
1511 tx_skb->mapped_as_page = true;
1512
1513 len -= size;
1514 offset += size;
1515 count++;
1516 tx_head++;
1517 }
1518 }
1519
1520 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001521 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001522 netdev_err(bp->dev, "BUG! empty skb!\n");
1523 return 0;
1524 }
1525
1526 /* This is the last buffer of the frame: save socket buffer */
1527 tx_skb->skb = skb;
1528
1529 /* Update TX ring: update buffer descriptors in reverse order
1530 * to avoid race condition
1531 */
1532
1533 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1534 * to set the end of TX queue
1535 */
1536 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001537 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001538 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001539 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001540 desc->ctrl = ctrl;
1541
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001542 if (lso_ctrl) {
1543 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1544 /* include header and FCS in value given to h/w */
1545 mss_mfs = skb_shinfo(skb)->gso_size +
1546 skb_transport_offset(skb) +
1547 ETH_FCS_LEN;
1548 else /* TSO */ {
1549 mss_mfs = skb_shinfo(skb)->gso_size;
1550 /* TCP Sequence Number Source Select
1551 * can be set only for TSO
1552 */
1553 seq_ctrl = 0;
1554 }
1555 }
1556
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001557 do {
1558 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001559 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001560 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001561 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001562
1563 ctrl = (u32)tx_skb->size;
1564 if (eof) {
1565 ctrl |= MACB_BIT(TX_LAST);
1566 eof = 0;
1567 }
Zach Brownb410d132016-10-19 09:56:57 -05001568 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001569 ctrl |= MACB_BIT(TX_WRAP);
1570
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001571 /* First descriptor is header descriptor */
1572 if (i == queue->tx_head) {
1573 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1574 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1575 } else
1576 /* Only set MSS/MFS on payload descriptors
1577 * (second or later descriptor)
1578 */
1579 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1580
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001581 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001582 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001583 /* desc->addr must be visible to hardware before clearing
1584 * 'TX_USED' bit in desc->ctrl.
1585 */
1586 wmb();
1587 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001588 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001589
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001590 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001591
1592 return count;
1593
1594dma_error:
1595 netdev_err(bp->dev, "TX DMA map failed\n");
1596
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001597 for (i = queue->tx_head; i != tx_head; i++) {
1598 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001599
1600 macb_tx_unmap(bp, tx_skb);
1601 }
1602
1603 return 0;
1604}
1605
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001606static netdev_features_t macb_features_check(struct sk_buff *skb,
1607 struct net_device *dev,
1608 netdev_features_t features)
1609{
1610 unsigned int nr_frags, f;
1611 unsigned int hdrlen;
1612
1613 /* Validate LSO compatibility */
1614
1615 /* there is only one buffer */
1616 if (!skb_is_nonlinear(skb))
1617 return features;
1618
1619 /* length of header */
1620 hdrlen = skb_transport_offset(skb);
1621 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1622 hdrlen += tcp_hdrlen(skb);
1623
1624 /* For LSO:
1625 * When software supplies two or more payload buffers all payload buffers
1626 * apart from the last must be a multiple of 8 bytes in size.
1627 */
1628 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1629 return features & ~MACB_NETIF_LSO;
1630
1631 nr_frags = skb_shinfo(skb)->nr_frags;
1632 /* No need to check last fragment */
1633 nr_frags--;
1634 for (f = 0; f < nr_frags; f++) {
1635 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1636
1637 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1638 return features & ~MACB_NETIF_LSO;
1639 }
1640 return features;
1641}
1642
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001643static inline int macb_clear_csum(struct sk_buff *skb)
1644{
1645 /* no change for packets without checksum offloading */
1646 if (skb->ip_summed != CHECKSUM_PARTIAL)
1647 return 0;
1648
1649 /* make sure we can modify the header */
1650 if (unlikely(skb_cow_head(skb, 0)))
1651 return -1;
1652
1653 /* initialize checksum field
1654 * This is required - at least for Zynq, which otherwise calculates
1655 * wrong UDP header checksums for UDP packets with UDP data len <=2
1656 */
1657 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1658 return 0;
1659}
1660
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001661static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1662{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001663 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001664 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001665 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001666 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001667 unsigned int desc_cnt, nr_frags, frag_size, f;
1668 unsigned int hdrlen;
1669 bool is_lso, is_udp = 0;
1670
1671 is_lso = (skb_shinfo(skb)->gso_size != 0);
1672
1673 if (is_lso) {
1674 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1675
1676 /* length of headers */
1677 if (is_udp)
1678 /* only queue eth + ip headers separately for UDP */
1679 hdrlen = skb_transport_offset(skb);
1680 else
1681 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1682 if (skb_headlen(skb) < hdrlen) {
1683 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1684 /* if this is required, would need to copy to single buffer */
1685 return NETDEV_TX_BUSY;
1686 }
1687 } else
1688 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001689
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001690#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1691 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001692 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1693 queue_index, skb->len, skb->head, skb->data,
1694 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001695 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1696 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001697#endif
1698
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001699 /* Count how many TX buffer descriptors are needed to send this
1700 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001701 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001702 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001703 if (is_lso && (skb_headlen(skb) > hdrlen))
1704 /* extra header descriptor if also payload in first buffer */
1705 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1706 else
1707 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001708 nr_frags = skb_shinfo(skb)->nr_frags;
1709 for (f = 0; f < nr_frags; f++) {
1710 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001711 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001712 }
1713
Dongdong Deng48719532009-08-23 19:49:07 -07001714 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001715
1716 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001717 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001718 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001719 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001720 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001721 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001722 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001723 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001724 }
1725
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001726 if (macb_clear_csum(skb)) {
1727 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001728 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001729 }
1730
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001731 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001732 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001733 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001734 goto unlock;
1735 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001736
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001737 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001738 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001739 skb_tx_timestamp(skb);
1740
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001741 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1742
Zach Brownb410d132016-10-19 09:56:57 -05001743 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001744 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745
Soren Brinkmann92030902014-03-04 08:46:39 -08001746unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001747 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001748
Patrick McHardy6ed10652009-06-23 06:03:08 +00001749 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001750}
1751
Nicolas Ferre4df95132013-06-04 21:57:12 +00001752static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001753{
1754 if (!macb_is_gem(bp)) {
1755 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1756 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001757 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001758
Nicolas Ferre1b447912013-06-04 21:57:11 +00001759 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001760 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001761 "RX buffer must be multiple of %d bytes, expanding\n",
1762 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001763 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001764 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001765 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001766 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001767
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001768 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001769 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001770}
1771
Nicolas Ferre4df95132013-06-04 21:57:12 +00001772static void gem_free_rx_buffers(struct macb *bp)
1773{
1774 struct sk_buff *skb;
1775 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001776 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001777 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001778 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001779 int i;
1780
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001781 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1782 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001783 continue;
1784
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001785 for (i = 0; i < bp->rx_ring_size; i++) {
1786 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001787
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001788 if (!skb)
1789 continue;
1790
1791 desc = macb_rx_desc(queue, i);
1792 addr = macb_get_addr(bp, desc);
1793
1794 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1795 DMA_FROM_DEVICE);
1796 dev_kfree_skb_any(skb);
1797 skb = NULL;
1798 }
1799
1800 kfree(queue->rx_skbuff);
1801 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001802 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001803}
1804
1805static void macb_free_rx_buffers(struct macb *bp)
1806{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001807 struct macb_queue *queue = &bp->queues[0];
1808
1809 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001810 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001811 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001812 queue->rx_buffers, queue->rx_buffers_dma);
1813 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001814 }
1815}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001816
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001817static void macb_free_consistent(struct macb *bp)
1818{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001819 struct macb_queue *queue;
1820 unsigned int q;
1821
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001822 queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001823 bp->macbgem_ops.mog_free_rx_buffers(bp);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001824 if (queue->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001825 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001826 queue->rx_ring, queue->rx_ring_dma);
1827 queue->rx_ring = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001828 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001829
1830 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1831 kfree(queue->tx_skb);
1832 queue->tx_skb = NULL;
1833 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001834 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001835 queue->tx_ring, queue->tx_ring_dma);
1836 queue->tx_ring = NULL;
1837 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001838 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001839}
1840
1841static int gem_alloc_rx_buffers(struct macb *bp)
1842{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001843 struct macb_queue *queue;
1844 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001845 int size;
1846
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001847 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1848 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1849 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1850 if (!queue->rx_skbuff)
1851 return -ENOMEM;
1852 else
1853 netdev_dbg(bp->dev,
1854 "Allocated %d RX struct sk_buff entries at %p\n",
1855 bp->rx_ring_size, queue->rx_skbuff);
1856 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001857 return 0;
1858}
1859
1860static int macb_alloc_rx_buffers(struct macb *bp)
1861{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001862 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001863 int size;
1864
Zach Brownb410d132016-10-19 09:56:57 -05001865 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001866 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1867 &queue->rx_buffers_dma, GFP_KERNEL);
1868 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001869 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001870
1871 netdev_dbg(bp->dev,
1872 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001873 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001874 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001875}
1876
1877static int macb_alloc_consistent(struct macb *bp)
1878{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001879 struct macb_queue *queue;
1880 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001881 int size;
1882
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001883 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001884 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001885 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1886 &queue->tx_ring_dma,
1887 GFP_KERNEL);
1888 if (!queue->tx_ring)
1889 goto out_err;
1890 netdev_dbg(bp->dev,
1891 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1892 q, size, (unsigned long)queue->tx_ring_dma,
1893 queue->tx_ring);
1894
Zach Brownb410d132016-10-19 09:56:57 -05001895 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001896 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1897 if (!queue->tx_skb)
1898 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001899
1900 size = RX_RING_BYTES(bp);
1901 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1902 &queue->rx_ring_dma, GFP_KERNEL);
1903 if (!queue->rx_ring)
1904 goto out_err;
1905 netdev_dbg(bp->dev,
1906 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1907 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001908 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001909 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001910 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001911
1912 return 0;
1913
1914out_err:
1915 macb_free_consistent(bp);
1916 return -ENOMEM;
1917}
1918
Nicolas Ferre4df95132013-06-04 21:57:12 +00001919static void gem_init_rings(struct macb *bp)
1920{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001921 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001922 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001923 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001924 int i;
1925
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001926 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001927 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001928 desc = macb_tx_desc(queue, i);
1929 macb_set_addr(bp, desc, 0);
1930 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001931 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001932 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001933 queue->tx_head = 0;
1934 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001935
1936 queue->rx_tail = 0;
1937 queue->rx_prepared_head = 0;
1938
1939 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001940 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001941
Nicolas Ferre4df95132013-06-04 21:57:12 +00001942}
1943
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001944static void macb_init_rings(struct macb *bp)
1945{
1946 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001947 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001948
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001949 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001950
Zach Brownb410d132016-10-19 09:56:57 -05001951 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001952 desc = macb_tx_desc(&bp->queues[0], i);
1953 macb_set_addr(bp, desc, 0);
1954 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001955 }
Ben Shelton21d35152015-04-22 17:28:54 -05001956 bp->queues[0].tx_head = 0;
1957 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001958 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001959}
1960
1961static void macb_reset_hw(struct macb *bp)
1962{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001963 struct macb_queue *queue;
1964 unsigned int q;
1965
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001966 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001967 * more gracefully?)
1968 */
1969 macb_writel(bp, NCR, 0);
1970
1971 /* Clear the stats registers (XXX: Update stats first?) */
1972 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1973
1974 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001975 macb_writel(bp, TSR, -1);
1976 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001977
1978 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001979 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1980 queue_writel(queue, IDR, -1);
1981 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001982 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1983 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001984 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001985}
1986
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001987static u32 gem_mdc_clk_div(struct macb *bp)
1988{
1989 u32 config;
1990 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1991
1992 if (pclk_hz <= 20000000)
1993 config = GEM_BF(CLK, GEM_CLK_DIV8);
1994 else if (pclk_hz <= 40000000)
1995 config = GEM_BF(CLK, GEM_CLK_DIV16);
1996 else if (pclk_hz <= 80000000)
1997 config = GEM_BF(CLK, GEM_CLK_DIV32);
1998 else if (pclk_hz <= 120000000)
1999 config = GEM_BF(CLK, GEM_CLK_DIV48);
2000 else if (pclk_hz <= 160000000)
2001 config = GEM_BF(CLK, GEM_CLK_DIV64);
2002 else
2003 config = GEM_BF(CLK, GEM_CLK_DIV96);
2004
2005 return config;
2006}
2007
2008static u32 macb_mdc_clk_div(struct macb *bp)
2009{
2010 u32 config;
2011 unsigned long pclk_hz;
2012
2013 if (macb_is_gem(bp))
2014 return gem_mdc_clk_div(bp);
2015
2016 pclk_hz = clk_get_rate(bp->pclk);
2017 if (pclk_hz <= 20000000)
2018 config = MACB_BF(CLK, MACB_CLK_DIV8);
2019 else if (pclk_hz <= 40000000)
2020 config = MACB_BF(CLK, MACB_CLK_DIV16);
2021 else if (pclk_hz <= 80000000)
2022 config = MACB_BF(CLK, MACB_CLK_DIV32);
2023 else
2024 config = MACB_BF(CLK, MACB_CLK_DIV64);
2025
2026 return config;
2027}
2028
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002029/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002030 * should program. We find the width from decoding the design configuration
2031 * register to find the maximum supported data bus width.
2032 */
2033static u32 macb_dbw(struct macb *bp)
2034{
2035 if (!macb_is_gem(bp))
2036 return 0;
2037
2038 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2039 case 4:
2040 return GEM_BF(DBW, GEM_DBW128);
2041 case 2:
2042 return GEM_BF(DBW, GEM_DBW64);
2043 case 1:
2044 default:
2045 return GEM_BF(DBW, GEM_DBW32);
2046 }
2047}
2048
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002049/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002050 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002051 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002052 * (if not supported by FIFO, it will fallback to default)
2053 * - set both rx/tx packet buffers to full memory size
2054 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002055 */
2056static void macb_configure_dma(struct macb *bp)
2057{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002058 struct macb_queue *queue;
2059 u32 buffer_size;
2060 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002061 u32 dmacfg;
2062
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002063 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002064 if (macb_is_gem(bp)) {
2065 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002066 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2067 if (q)
2068 queue_writel(queue, RBQS, buffer_size);
2069 else
2070 dmacfg |= GEM_BF(RXBS, buffer_size);
2071 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002072 if (bp->dma_burst_length)
2073 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002074 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302075 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302076
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002077 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302078 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2079 else
2080 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2081
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002082 if (bp->dev->features & NETIF_F_HW_CSUM)
2083 dmacfg |= GEM_BIT(TXCOEN);
2084 else
2085 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302086
2087#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002088 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002089 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302090#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002091#ifdef CONFIG_MACB_USE_HWSTAMP
2092 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2093 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2094#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002095 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2096 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002097 gem_writel(bp, DMACFG, dmacfg);
2098 }
2099}
2100
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002101static void macb_init_hw(struct macb *bp)
2102{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002103 struct macb_queue *queue;
2104 unsigned int q;
2105
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002106 u32 config;
2107
2108 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002109 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002110
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002111 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302112 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2113 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002114 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002115 config |= MACB_BIT(PAE); /* PAuse Enable */
2116 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002117 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302118 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2119 else
2120 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002121 if (bp->dev->flags & IFF_PROMISC)
2122 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002123 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2124 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002125 if (!(bp->dev->flags & IFF_BROADCAST))
2126 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002127 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002128 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002129 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302130 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002131 bp->speed = SPEED_10;
2132 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302133 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002134 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302135 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002136
Jamie Iles0116da42011-03-14 17:38:30 +00002137 macb_configure_dma(bp);
2138
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002139 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002140 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002141 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2142#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2143 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2144 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2145#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002146 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302147#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002148 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002149 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302150#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002151
2152 /* Enable interrupts */
2153 queue_writel(queue, IER,
2154 MACB_RX_INT_FLAGS |
2155 MACB_TX_INT_FLAGS |
2156 MACB_BIT(HRESP));
2157 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002158
2159 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02002160 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002161}
2162
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002163/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002164 * locations in the memory map. The least significant bits are stored
2165 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2166 *
2167 * The unicast hash enable and the multicast hash enable bits in the
2168 * network configuration register enable the reception of hash matched
2169 * frames. The destination address is reduced to a 6 bit index into
2170 * the 64 bit hash register using the following hash function. The
2171 * hash function is an exclusive or of every sixth bit of the
2172 * destination address.
2173 *
2174 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2175 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2176 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2177 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2178 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2179 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2180 *
2181 * da[0] represents the least significant bit of the first byte
2182 * received, that is, the multicast/unicast indicator, and da[47]
2183 * represents the most significant bit of the last byte received. If
2184 * the hash index, hi[n], points to a bit that is set in the hash
2185 * register then the frame will be matched according to whether the
2186 * frame is multicast or unicast. A multicast match will be signalled
2187 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2188 * index points to a bit set in the hash register. A unicast match
2189 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2190 * and the hash index points to a bit set in the hash register. To
2191 * receive all multicast frames, the hash register should be set with
2192 * all ones and the multicast hash enable bit should be set in the
2193 * network configuration register.
2194 */
2195
2196static inline int hash_bit_value(int bitnr, __u8 *addr)
2197{
2198 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2199 return 1;
2200 return 0;
2201}
2202
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002203/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002204static int hash_get_index(__u8 *addr)
2205{
2206 int i, j, bitval;
2207 int hash_index = 0;
2208
2209 for (j = 0; j < 6; j++) {
2210 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002211 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002212
2213 hash_index |= (bitval << j);
2214 }
2215
2216 return hash_index;
2217}
2218
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002219/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002220static void macb_sethashtable(struct net_device *dev)
2221{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002222 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002223 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002224 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002225 struct macb *bp = netdev_priv(dev);
2226
Moritz Fischeraa50b552016-03-29 19:11:13 -07002227 mc_filter[0] = 0;
2228 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002229
Jiri Pirko22bedad32010-04-01 21:22:57 +00002230 netdev_for_each_mc_addr(ha, dev) {
2231 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002232 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2233 }
2234
Jamie Ilesf75ba502011-11-08 10:12:32 +00002235 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2236 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002237}
2238
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002239/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002240static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002241{
2242 unsigned long cfg;
2243 struct macb *bp = netdev_priv(dev);
2244
2245 cfg = macb_readl(bp, NCFGR);
2246
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002247 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002248 /* Enable promiscuous mode */
2249 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002250
2251 /* Disable RX checksum offload */
2252 if (macb_is_gem(bp))
2253 cfg &= ~GEM_BIT(RXCOEN);
2254 } else {
2255 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002256 cfg &= ~MACB_BIT(CAF);
2257
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002258 /* Enable RX checksum offload only if requested */
2259 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2260 cfg |= GEM_BIT(RXCOEN);
2261 }
2262
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002263 if (dev->flags & IFF_ALLMULTI) {
2264 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002265 macb_or_gem_writel(bp, HRB, -1);
2266 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002267 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002268 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002269 /* Enable specific multicasts */
2270 macb_sethashtable(dev);
2271 cfg |= MACB_BIT(NCFGR_MTI);
2272 } else if (dev->flags & (~IFF_ALLMULTI)) {
2273 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002274 macb_or_gem_writel(bp, HRB, 0);
2275 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002276 cfg &= ~MACB_BIT(NCFGR_MTI);
2277 }
2278
2279 macb_writel(bp, NCFGR, cfg);
2280}
2281
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002282static int macb_open(struct net_device *dev)
2283{
2284 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002285 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002286 struct macb_queue *queue;
2287 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002288 int err;
2289
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002290 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002291
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002292 /* carrier starts down */
2293 netif_carrier_off(dev);
2294
frederic RODO6c36a702007-07-12 19:07:24 +02002295 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002296 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002297 return -EAGAIN;
2298
Nicolas Ferre1b447912013-06-04 21:57:11 +00002299 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002300 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002301
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002302 err = macb_alloc_consistent(bp);
2303 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002304 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2305 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002306 return err;
2307 }
2308
Nicolas Ferre4df95132013-06-04 21:57:12 +00002309 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002310 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002311
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002312 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2313 napi_enable(&queue->napi);
2314
frederic RODO6c36a702007-07-12 19:07:24 +02002315 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002316 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002317
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002318 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002319
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002320 if (bp->ptp_info)
2321 bp->ptp_info->ptp_init(dev);
2322
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002323 return 0;
2324}
2325
2326static int macb_close(struct net_device *dev)
2327{
2328 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002329 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002330 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002331 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002332
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002333 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002334
2335 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2336 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002337
Philippe Reynes0a912812016-06-22 00:32:35 +02002338 if (dev->phydev)
2339 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002340
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002341 spin_lock_irqsave(&bp->lock, flags);
2342 macb_reset_hw(bp);
2343 netif_carrier_off(dev);
2344 spin_unlock_irqrestore(&bp->lock, flags);
2345
2346 macb_free_consistent(bp);
2347
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002348 if (bp->ptp_info)
2349 bp->ptp_info->ptp_remove(dev);
2350
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002351 return 0;
2352}
2353
Harini Katakama5898ea2015-05-06 22:27:18 +05302354static int macb_change_mtu(struct net_device *dev, int new_mtu)
2355{
Harini Katakama5898ea2015-05-06 22:27:18 +05302356 if (netif_running(dev))
2357 return -EBUSY;
2358
Harini Katakama5898ea2015-05-06 22:27:18 +05302359 dev->mtu = new_mtu;
2360
2361 return 0;
2362}
2363
Jamie Ilesa494ed82011-03-09 16:26:35 +00002364static void gem_update_stats(struct macb *bp)
2365{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002366 struct macb_queue *queue;
2367 unsigned int i, q, idx;
2368 unsigned long *stat;
2369
Jamie Ilesa494ed82011-03-09 16:26:35 +00002370 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002371
Xander Huff3ff13f12015-01-13 16:15:51 -06002372 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2373 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002374 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002375
2376 bp->ethtool_stats[i] += val;
2377 *p += val;
2378
2379 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2380 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002381 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002382 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002383 *(++p) += val;
2384 }
2385 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002386
2387 idx = GEM_STATS_LEN;
2388 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2389 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2390 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002391}
2392
2393static struct net_device_stats *gem_get_stats(struct macb *bp)
2394{
2395 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002396 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002397
2398 gem_update_stats(bp);
2399
2400 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2401 hwstat->rx_alignment_errors +
2402 hwstat->rx_resource_errors +
2403 hwstat->rx_overruns +
2404 hwstat->rx_oversize_frames +
2405 hwstat->rx_jabbers +
2406 hwstat->rx_undersized_frames +
2407 hwstat->rx_length_field_frame_errors);
2408 nstat->tx_errors = (hwstat->tx_late_collisions +
2409 hwstat->tx_excessive_collisions +
2410 hwstat->tx_underrun +
2411 hwstat->tx_carrier_sense_errors);
2412 nstat->multicast = hwstat->rx_multicast_frames;
2413 nstat->collisions = (hwstat->tx_single_collision_frames +
2414 hwstat->tx_multiple_collision_frames +
2415 hwstat->tx_excessive_collisions);
2416 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2417 hwstat->rx_jabbers +
2418 hwstat->rx_undersized_frames +
2419 hwstat->rx_length_field_frame_errors);
2420 nstat->rx_over_errors = hwstat->rx_resource_errors;
2421 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2422 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2423 nstat->rx_fifo_errors = hwstat->rx_overruns;
2424 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2425 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2426 nstat->tx_fifo_errors = hwstat->tx_underrun;
2427
2428 return nstat;
2429}
2430
Xander Huff3ff13f12015-01-13 16:15:51 -06002431static void gem_get_ethtool_stats(struct net_device *dev,
2432 struct ethtool_stats *stats, u64 *data)
2433{
2434 struct macb *bp;
2435
2436 bp = netdev_priv(dev);
2437 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002438 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2439 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002440}
2441
2442static int gem_get_sset_count(struct net_device *dev, int sset)
2443{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002444 struct macb *bp = netdev_priv(dev);
2445
Xander Huff3ff13f12015-01-13 16:15:51 -06002446 switch (sset) {
2447 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002448 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002449 default:
2450 return -EOPNOTSUPP;
2451 }
2452}
2453
2454static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2455{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002456 char stat_string[ETH_GSTRING_LEN];
2457 struct macb *bp = netdev_priv(dev);
2458 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002459 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002460 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002461
2462 switch (sset) {
2463 case ETH_SS_STATS:
2464 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2465 memcpy(p, gem_statistics[i].stat_string,
2466 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002467
2468 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2469 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2470 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2471 q, queue_statistics[i].stat_string);
2472 memcpy(p, stat_string, ETH_GSTRING_LEN);
2473 }
2474 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002475 break;
2476 }
2477}
2478
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002479static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002480{
2481 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002482 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002483 struct macb_stats *hwstat = &bp->hw_stats.macb;
2484
2485 if (macb_is_gem(bp))
2486 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002487
frederic RODO6c36a702007-07-12 19:07:24 +02002488 /* read stats from hardware */
2489 macb_update_stats(bp);
2490
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002491 /* Convert HW stats into netdevice stats */
2492 nstat->rx_errors = (hwstat->rx_fcs_errors +
2493 hwstat->rx_align_errors +
2494 hwstat->rx_resource_errors +
2495 hwstat->rx_overruns +
2496 hwstat->rx_oversize_pkts +
2497 hwstat->rx_jabbers +
2498 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002499 hwstat->rx_length_mismatch);
2500 nstat->tx_errors = (hwstat->tx_late_cols +
2501 hwstat->tx_excessive_cols +
2502 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002503 hwstat->tx_carrier_errors +
2504 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002505 nstat->collisions = (hwstat->tx_single_cols +
2506 hwstat->tx_multiple_cols +
2507 hwstat->tx_excessive_cols);
2508 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2509 hwstat->rx_jabbers +
2510 hwstat->rx_undersize_pkts +
2511 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002512 nstat->rx_over_errors = hwstat->rx_resource_errors +
2513 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002514 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2515 nstat->rx_frame_errors = hwstat->rx_align_errors;
2516 nstat->rx_fifo_errors = hwstat->rx_overruns;
2517 /* XXX: What does "missed" mean? */
2518 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2519 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2520 nstat->tx_fifo_errors = hwstat->tx_underruns;
2521 /* Don't know about heartbeat or window errors... */
2522
2523 return nstat;
2524}
2525
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002526static int macb_get_regs_len(struct net_device *netdev)
2527{
2528 return MACB_GREGS_NBR * sizeof(u32);
2529}
2530
2531static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2532 void *p)
2533{
2534 struct macb *bp = netdev_priv(dev);
2535 unsigned int tail, head;
2536 u32 *regs_buff = p;
2537
2538 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2539 | MACB_GREGS_VERSION;
2540
Zach Brownb410d132016-10-19 09:56:57 -05002541 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2542 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002543
2544 regs_buff[0] = macb_readl(bp, NCR);
2545 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2546 regs_buff[2] = macb_readl(bp, NSR);
2547 regs_buff[3] = macb_readl(bp, TSR);
2548 regs_buff[4] = macb_readl(bp, RBQP);
2549 regs_buff[5] = macb_readl(bp, TBQP);
2550 regs_buff[6] = macb_readl(bp, RSR);
2551 regs_buff[7] = macb_readl(bp, IMR);
2552
2553 regs_buff[8] = tail;
2554 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002555 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2556 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002557
Neil Armstrongce721a72016-01-05 14:39:16 +01002558 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2559 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002560 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002561 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002562}
2563
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002564static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2565{
2566 struct macb *bp = netdev_priv(netdev);
2567
2568 wol->supported = 0;
2569 wol->wolopts = 0;
2570
2571 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2572 wol->supported = WAKE_MAGIC;
2573
2574 if (bp->wol & MACB_WOL_ENABLED)
2575 wol->wolopts |= WAKE_MAGIC;
2576 }
2577}
2578
2579static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2580{
2581 struct macb *bp = netdev_priv(netdev);
2582
2583 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2584 (wol->wolopts & ~WAKE_MAGIC))
2585 return -EOPNOTSUPP;
2586
2587 if (wol->wolopts & WAKE_MAGIC)
2588 bp->wol |= MACB_WOL_ENABLED;
2589 else
2590 bp->wol &= ~MACB_WOL_ENABLED;
2591
2592 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2593
2594 return 0;
2595}
2596
Zach Brown8441bb32016-10-19 09:56:58 -05002597static void macb_get_ringparam(struct net_device *netdev,
2598 struct ethtool_ringparam *ring)
2599{
2600 struct macb *bp = netdev_priv(netdev);
2601
2602 ring->rx_max_pending = MAX_RX_RING_SIZE;
2603 ring->tx_max_pending = MAX_TX_RING_SIZE;
2604
2605 ring->rx_pending = bp->rx_ring_size;
2606 ring->tx_pending = bp->tx_ring_size;
2607}
2608
2609static int macb_set_ringparam(struct net_device *netdev,
2610 struct ethtool_ringparam *ring)
2611{
2612 struct macb *bp = netdev_priv(netdev);
2613 u32 new_rx_size, new_tx_size;
2614 unsigned int reset = 0;
2615
2616 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2617 return -EINVAL;
2618
2619 new_rx_size = clamp_t(u32, ring->rx_pending,
2620 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2621 new_rx_size = roundup_pow_of_two(new_rx_size);
2622
2623 new_tx_size = clamp_t(u32, ring->tx_pending,
2624 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2625 new_tx_size = roundup_pow_of_two(new_tx_size);
2626
2627 if ((new_tx_size == bp->tx_ring_size) &&
2628 (new_rx_size == bp->rx_ring_size)) {
2629 /* nothing to do */
2630 return 0;
2631 }
2632
2633 if (netif_running(bp->dev)) {
2634 reset = 1;
2635 macb_close(bp->dev);
2636 }
2637
2638 bp->rx_ring_size = new_rx_size;
2639 bp->tx_ring_size = new_tx_size;
2640
2641 if (reset)
2642 macb_open(bp->dev);
2643
2644 return 0;
2645}
2646
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002647#ifdef CONFIG_MACB_USE_HWSTAMP
2648static unsigned int gem_get_tsu_rate(struct macb *bp)
2649{
2650 struct clk *tsu_clk;
2651 unsigned int tsu_rate;
2652
2653 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2654 if (!IS_ERR(tsu_clk))
2655 tsu_rate = clk_get_rate(tsu_clk);
2656 /* try pclk instead */
2657 else if (!IS_ERR(bp->pclk)) {
2658 tsu_clk = bp->pclk;
2659 tsu_rate = clk_get_rate(tsu_clk);
2660 } else
2661 return -ENOTSUPP;
2662 return tsu_rate;
2663}
2664
2665static s32 gem_get_ptp_max_adj(void)
2666{
2667 return 64000000;
2668}
2669
2670static int gem_get_ts_info(struct net_device *dev,
2671 struct ethtool_ts_info *info)
2672{
2673 struct macb *bp = netdev_priv(dev);
2674
2675 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2676 ethtool_op_get_ts_info(dev, info);
2677 return 0;
2678 }
2679
2680 info->so_timestamping =
2681 SOF_TIMESTAMPING_TX_SOFTWARE |
2682 SOF_TIMESTAMPING_RX_SOFTWARE |
2683 SOF_TIMESTAMPING_SOFTWARE |
2684 SOF_TIMESTAMPING_TX_HARDWARE |
2685 SOF_TIMESTAMPING_RX_HARDWARE |
2686 SOF_TIMESTAMPING_RAW_HARDWARE;
2687 info->tx_types =
2688 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2689 (1 << HWTSTAMP_TX_OFF) |
2690 (1 << HWTSTAMP_TX_ON);
2691 info->rx_filters =
2692 (1 << HWTSTAMP_FILTER_NONE) |
2693 (1 << HWTSTAMP_FILTER_ALL);
2694
2695 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2696
2697 return 0;
2698}
2699
2700static struct macb_ptp_info gem_ptp_info = {
2701 .ptp_init = gem_ptp_init,
2702 .ptp_remove = gem_ptp_remove,
2703 .get_ptp_max_adj = gem_get_ptp_max_adj,
2704 .get_tsu_rate = gem_get_tsu_rate,
2705 .get_ts_info = gem_get_ts_info,
2706 .get_hwtst = gem_get_hwtst,
2707 .set_hwtst = gem_set_hwtst,
2708};
2709#endif
2710
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002711static int macb_get_ts_info(struct net_device *netdev,
2712 struct ethtool_ts_info *info)
2713{
2714 struct macb *bp = netdev_priv(netdev);
2715
2716 if (bp->ptp_info)
2717 return bp->ptp_info->get_ts_info(netdev, info);
2718
2719 return ethtool_op_get_ts_info(netdev, info);
2720}
2721
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002722static void gem_enable_flow_filters(struct macb *bp, bool enable)
2723{
2724 struct ethtool_rx_fs_item *item;
2725 u32 t2_scr;
2726 int num_t2_scr;
2727
2728 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2729
2730 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2731 struct ethtool_rx_flow_spec *fs = &item->fs;
2732 struct ethtool_tcpip4_spec *tp4sp_m;
2733
2734 if (fs->location >= num_t2_scr)
2735 continue;
2736
2737 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2738
2739 /* enable/disable screener regs for the flow entry */
2740 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2741
2742 /* only enable fields with no masking */
2743 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2744
2745 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2746 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2747 else
2748 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2749
2750 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2751 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2752 else
2753 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2754
2755 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2756 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2757 else
2758 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2759
2760 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2761 }
2762}
2763
2764static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2765{
2766 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2767 uint16_t index = fs->location;
2768 u32 w0, w1, t2_scr;
2769 bool cmp_a = false;
2770 bool cmp_b = false;
2771 bool cmp_c = false;
2772
2773 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2774 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2775
2776 /* ignore field if any masking set */
2777 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2778 /* 1st compare reg - IP source address */
2779 w0 = 0;
2780 w1 = 0;
2781 w0 = tp4sp_v->ip4src;
2782 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2783 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2784 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2785 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2786 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2787 cmp_a = true;
2788 }
2789
2790 /* ignore field if any masking set */
2791 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2792 /* 2nd compare reg - IP destination address */
2793 w0 = 0;
2794 w1 = 0;
2795 w0 = tp4sp_v->ip4dst;
2796 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2797 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2798 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2799 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2800 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2801 cmp_b = true;
2802 }
2803
2804 /* ignore both port fields if masking set in both */
2805 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2806 /* 3rd compare reg - source port, destination port */
2807 w0 = 0;
2808 w1 = 0;
2809 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2810 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2811 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2812 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2813 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2814 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2815 } else {
2816 /* only one port definition */
2817 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2818 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2819 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2820 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2821 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2822 } else { /* dst port */
2823 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2824 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2825 }
2826 }
2827 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2828 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2829 cmp_c = true;
2830 }
2831
2832 t2_scr = 0;
2833 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2834 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2835 if (cmp_a)
2836 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2837 if (cmp_b)
2838 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2839 if (cmp_c)
2840 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2841 gem_writel_n(bp, SCRT2, index, t2_scr);
2842}
2843
2844static int gem_add_flow_filter(struct net_device *netdev,
2845 struct ethtool_rxnfc *cmd)
2846{
2847 struct macb *bp = netdev_priv(netdev);
2848 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2849 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002850 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002851 int ret = -EINVAL;
2852 bool added = false;
2853
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002854 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002855 if (newfs == NULL)
2856 return -ENOMEM;
2857 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2858
2859 netdev_dbg(netdev,
2860 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2861 fs->flow_type, (int)fs->ring_cookie, fs->location,
2862 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2863 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2864 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2865
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002866 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2867
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002868 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002869 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2870 if (item->fs.location > newfs->fs.location) {
2871 list_add_tail(&newfs->list, &item->list);
2872 added = true;
2873 break;
2874 } else if (item->fs.location == fs->location) {
2875 netdev_err(netdev, "Rule not added: location %d not free!\n",
2876 fs->location);
2877 ret = -EBUSY;
2878 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002879 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002880 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002881 if (!added)
2882 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002883
2884 gem_prog_cmp_regs(bp, fs);
2885 bp->rx_fs_list.count++;
2886 /* enable filtering if NTUPLE on */
2887 if (netdev->features & NETIF_F_NTUPLE)
2888 gem_enable_flow_filters(bp, 1);
2889
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002890 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002891 return 0;
2892
2893err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002894 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002895 kfree(newfs);
2896 return ret;
2897}
2898
2899static int gem_del_flow_filter(struct net_device *netdev,
2900 struct ethtool_rxnfc *cmd)
2901{
2902 struct macb *bp = netdev_priv(netdev);
2903 struct ethtool_rx_fs_item *item;
2904 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002905 unsigned long flags;
2906
2907 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002908
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002909 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2910 if (item->fs.location == cmd->fs.location) {
2911 /* disable screener regs for the flow entry */
2912 fs = &(item->fs);
2913 netdev_dbg(netdev,
2914 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2915 fs->flow_type, (int)fs->ring_cookie, fs->location,
2916 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2917 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2918 htons(fs->h_u.tcp_ip4_spec.psrc),
2919 htons(fs->h_u.tcp_ip4_spec.pdst));
2920
2921 gem_writel_n(bp, SCRT2, fs->location, 0);
2922
2923 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002924 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002925 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
2926 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002927 return 0;
2928 }
2929 }
2930
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002931 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002932 return -EINVAL;
2933}
2934
2935static int gem_get_flow_entry(struct net_device *netdev,
2936 struct ethtool_rxnfc *cmd)
2937{
2938 struct macb *bp = netdev_priv(netdev);
2939 struct ethtool_rx_fs_item *item;
2940
2941 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2942 if (item->fs.location == cmd->fs.location) {
2943 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
2944 return 0;
2945 }
2946 }
2947 return -EINVAL;
2948}
2949
2950static int gem_get_all_flow_entries(struct net_device *netdev,
2951 struct ethtool_rxnfc *cmd, u32 *rule_locs)
2952{
2953 struct macb *bp = netdev_priv(netdev);
2954 struct ethtool_rx_fs_item *item;
2955 uint32_t cnt = 0;
2956
2957 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2958 if (cnt == cmd->rule_cnt)
2959 return -EMSGSIZE;
2960 rule_locs[cnt] = item->fs.location;
2961 cnt++;
2962 }
2963 cmd->data = bp->max_tuples;
2964 cmd->rule_cnt = cnt;
2965
2966 return 0;
2967}
2968
2969static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2970 u32 *rule_locs)
2971{
2972 struct macb *bp = netdev_priv(netdev);
2973 int ret = 0;
2974
2975 switch (cmd->cmd) {
2976 case ETHTOOL_GRXRINGS:
2977 cmd->data = bp->num_queues;
2978 break;
2979 case ETHTOOL_GRXCLSRLCNT:
2980 cmd->rule_cnt = bp->rx_fs_list.count;
2981 break;
2982 case ETHTOOL_GRXCLSRULE:
2983 ret = gem_get_flow_entry(netdev, cmd);
2984 break;
2985 case ETHTOOL_GRXCLSRLALL:
2986 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
2987 break;
2988 default:
2989 netdev_err(netdev,
2990 "Command parameter %d is not supported\n", cmd->cmd);
2991 ret = -EOPNOTSUPP;
2992 }
2993
2994 return ret;
2995}
2996
2997static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2998{
2999 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003000 int ret;
3001
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003002 switch (cmd->cmd) {
3003 case ETHTOOL_SRXCLSRLINS:
3004 if ((cmd->fs.location >= bp->max_tuples)
3005 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3006 ret = -EINVAL;
3007 break;
3008 }
3009 ret = gem_add_flow_filter(netdev, cmd);
3010 break;
3011 case ETHTOOL_SRXCLSRLDEL:
3012 ret = gem_del_flow_filter(netdev, cmd);
3013 break;
3014 default:
3015 netdev_err(netdev,
3016 "Command parameter %d is not supported\n", cmd->cmd);
3017 ret = -EOPNOTSUPP;
3018 }
3019
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003020 return ret;
3021}
3022
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003023static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003024 .get_regs_len = macb_get_regs_len,
3025 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003026 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003027 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003028 .get_wol = macb_get_wol,
3029 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003030 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3031 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003032 .get_ringparam = macb_get_ringparam,
3033 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003034};
Xander Huff8cd5a562015-01-15 15:55:20 -06003035
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003036static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003037 .get_regs_len = macb_get_regs_len,
3038 .get_regs = macb_get_regs,
3039 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003040 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003041 .get_ethtool_stats = gem_get_ethtool_stats,
3042 .get_strings = gem_get_ethtool_strings,
3043 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003044 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3045 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003046 .get_ringparam = macb_get_ringparam,
3047 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003048 .get_rxnfc = gem_get_rxnfc,
3049 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003050};
3051
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003052static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003053{
Philippe Reynes0a912812016-06-22 00:32:35 +02003054 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003055 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003056
3057 if (!netif_running(dev))
3058 return -EINVAL;
3059
frederic RODO6c36a702007-07-12 19:07:24 +02003060 if (!phydev)
3061 return -ENODEV;
3062
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003063 if (!bp->ptp_info)
3064 return phy_mii_ioctl(phydev, rq, cmd);
3065
3066 switch (cmd) {
3067 case SIOCSHWTSTAMP:
3068 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3069 case SIOCGHWTSTAMP:
3070 return bp->ptp_info->get_hwtst(dev, rq);
3071 default:
3072 return phy_mii_ioctl(phydev, rq, cmd);
3073 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003074}
3075
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003076static int macb_set_features(struct net_device *netdev,
3077 netdev_features_t features)
3078{
3079 struct macb *bp = netdev_priv(netdev);
3080 netdev_features_t changed = features ^ netdev->features;
3081
3082 /* TX checksum offload */
3083 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3084 u32 dmacfg;
3085
3086 dmacfg = gem_readl(bp, DMACFG);
3087 if (features & NETIF_F_HW_CSUM)
3088 dmacfg |= GEM_BIT(TXCOEN);
3089 else
3090 dmacfg &= ~GEM_BIT(TXCOEN);
3091 gem_writel(bp, DMACFG, dmacfg);
3092 }
3093
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003094 /* RX checksum offload */
3095 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3096 u32 netcfg;
3097
3098 netcfg = gem_readl(bp, NCFGR);
3099 if (features & NETIF_F_RXCSUM &&
3100 !(netdev->flags & IFF_PROMISC))
3101 netcfg |= GEM_BIT(RXCOEN);
3102 else
3103 netcfg &= ~GEM_BIT(RXCOEN);
3104 gem_writel(bp, NCFGR, netcfg);
3105 }
3106
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003107 /* RX Flow Filters */
3108 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3109 bool turn_on = features & NETIF_F_NTUPLE;
3110
3111 gem_enable_flow_filters(bp, turn_on);
3112 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003113 return 0;
3114}
3115
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003116static const struct net_device_ops macb_netdev_ops = {
3117 .ndo_open = macb_open,
3118 .ndo_stop = macb_close,
3119 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003120 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003121 .ndo_get_stats = macb_get_stats,
3122 .ndo_do_ioctl = macb_ioctl,
3123 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303124 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003125 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003126#ifdef CONFIG_NET_POLL_CONTROLLER
3127 .ndo_poll_controller = macb_poll_controller,
3128#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003129 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003130 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003131};
3132
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003133/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003134 * and integration options used
3135 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003136static void macb_configure_caps(struct macb *bp,
3137 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003138{
3139 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003140
Nicolas Ferref6970502015-03-31 15:02:01 +02003141 if (dt_conf)
3142 bp->caps = dt_conf->caps;
3143
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003144 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003145 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3146
Nicolas Ferree1755872014-07-24 13:50:58 +02003147 dcfg = gem_readl(bp, DCFG1);
3148 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3149 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3150 dcfg = gem_readl(bp, DCFG2);
3151 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3152 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003153#ifdef CONFIG_MACB_USE_HWSTAMP
3154 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003155 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3156 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003157 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003158 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003159 bp->ptp_info = &gem_ptp_info;
3160 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003161 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003162#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003163 }
3164
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003165 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003166}
3167
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003168static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003169 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003170 unsigned int *queue_mask,
3171 unsigned int *num_queues)
3172{
3173 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003174
3175 *queue_mask = 0x1;
3176 *num_queues = 1;
3177
Nicolas Ferreda120112015-03-31 15:02:00 +02003178 /* is it macb or gem ?
3179 *
3180 * We need to read directly from the hardware here because
3181 * we are early in the probe process and don't have the
3182 * MACB_CAPS_MACB_IS_GEM flag positioned
3183 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003184 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003185 return;
3186
3187 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303188 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3189
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003190 *queue_mask |= 0x1;
3191
3192 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3193 if (*queue_mask & (1 << hw_q))
3194 (*num_queues)++;
3195}
3196
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003197static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303198 struct clk **hclk, struct clk **tx_clk,
3199 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003200{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003201 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003202 int err;
3203
Bartosz Folta83a77e92016-12-14 06:39:15 +00003204 pdata = dev_get_platdata(&pdev->dev);
3205 if (pdata) {
3206 *pclk = pdata->pclk;
3207 *hclk = pdata->hclk;
3208 } else {
3209 *pclk = devm_clk_get(&pdev->dev, "pclk");
3210 *hclk = devm_clk_get(&pdev->dev, "hclk");
3211 }
3212
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003213 if (IS_ERR(*pclk)) {
3214 err = PTR_ERR(*pclk);
3215 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3216 return err;
3217 }
3218
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003219 if (IS_ERR(*hclk)) {
3220 err = PTR_ERR(*hclk);
3221 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3222 return err;
3223 }
3224
3225 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3226 if (IS_ERR(*tx_clk))
3227 *tx_clk = NULL;
3228
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303229 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3230 if (IS_ERR(*rx_clk))
3231 *rx_clk = NULL;
3232
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003233 err = clk_prepare_enable(*pclk);
3234 if (err) {
3235 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3236 return err;
3237 }
3238
3239 err = clk_prepare_enable(*hclk);
3240 if (err) {
3241 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3242 goto err_disable_pclk;
3243 }
3244
3245 err = clk_prepare_enable(*tx_clk);
3246 if (err) {
3247 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3248 goto err_disable_hclk;
3249 }
3250
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303251 err = clk_prepare_enable(*rx_clk);
3252 if (err) {
3253 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3254 goto err_disable_txclk;
3255 }
3256
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003257 return 0;
3258
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303259err_disable_txclk:
3260 clk_disable_unprepare(*tx_clk);
3261
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003262err_disable_hclk:
3263 clk_disable_unprepare(*hclk);
3264
3265err_disable_pclk:
3266 clk_disable_unprepare(*pclk);
3267
3268 return err;
3269}
3270
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003271static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003272{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003273 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003274 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003275 struct macb *bp = netdev_priv(dev);
3276 struct macb_queue *queue;
3277 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003278 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003279
Zach Brownb410d132016-10-19 09:56:57 -05003280 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3281 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3282
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003283 /* set the queue register mapping once for all: queue0 has a special
3284 * register mapping but we don't want to test the queue index then
3285 * compute the corresponding register offset at run time.
3286 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003287 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003288 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003289 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003290
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003291 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003292 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003293 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003294 if (hw_q) {
3295 queue->ISR = GEM_ISR(hw_q - 1);
3296 queue->IER = GEM_IER(hw_q - 1);
3297 queue->IDR = GEM_IDR(hw_q - 1);
3298 queue->IMR = GEM_IMR(hw_q - 1);
3299 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003300 queue->RBQP = GEM_RBQP(hw_q - 1);
3301 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303302#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003303 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003304 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003305 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3306 }
Harini Katakamfff80192016-08-09 13:15:53 +05303307#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003308 } else {
3309 /* queue0 uses legacy registers */
3310 queue->ISR = MACB_ISR;
3311 queue->IER = MACB_IER;
3312 queue->IDR = MACB_IDR;
3313 queue->IMR = MACB_IMR;
3314 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003315 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303316#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003317 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003318 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003319 queue->RBQPH = MACB_RBQPH;
3320 }
Harini Katakamfff80192016-08-09 13:15:53 +05303321#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003322 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003323
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003324 /* get irq: here we use the linux queue index, not the hardware
3325 * queue index. the queue irq definitions in the device tree
3326 * must remove the optional gaps that could exist in the
3327 * hardware queue mask.
3328 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003329 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003330 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003331 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003332 if (err) {
3333 dev_err(&pdev->dev,
3334 "Unable to request IRQ %d (error %d)\n",
3335 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003336 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003337 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003338
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003339 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003340 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003341 }
3342
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003343 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003344
Nicolas Ferre4df95132013-06-04 21:57:12 +00003345 /* setup appropriated routines according to adapter type */
3346 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003347 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003348 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3349 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3350 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3351 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003352 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003353 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003354 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003355 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3356 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3357 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3358 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003359 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003360 }
3361
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003362 /* Set features */
3363 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003364
3365 /* Check LSO capability */
3366 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3367 dev->hw_features |= MACB_NETIF_LSO;
3368
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003369 /* Checksum offload is only available on gem with packet buffer */
3370 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003371 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003372 if (bp->caps & MACB_CAPS_SG_DISABLED)
3373 dev->hw_features &= ~NETIF_F_SG;
3374 dev->features = dev->hw_features;
3375
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003376 /* Check RX Flow Filters support.
3377 * Max Rx flows set by availability of screeners & compare regs:
3378 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3379 */
3380 reg = gem_readl(bp, DCFG8);
3381 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3382 GEM_BFEXT(T2SCR, reg));
3383 if (bp->max_tuples > 0) {
3384 /* also needs one ethtype match to check IPv4 */
3385 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3386 /* program this reg now */
3387 reg = 0;
3388 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3389 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3390 /* Filtering is supported in hw but don't enable it in kernel now */
3391 dev->hw_features |= NETIF_F_NTUPLE;
3392 /* init Rx flow definitions */
3393 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3394 bp->rx_fs_list.count = 0;
3395 spin_lock_init(&bp->rx_fs_lock);
3396 } else
3397 bp->max_tuples = 0;
3398 }
3399
Neil Armstrongce721a72016-01-05 14:39:16 +01003400 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3401 val = 0;
3402 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3403 val = GEM_BIT(RGMII);
3404 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003405 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003406 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003407 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003408 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003409
Neil Armstrongce721a72016-01-05 14:39:16 +01003410 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3411 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003412
Neil Armstrongce721a72016-01-05 14:39:16 +01003413 macb_or_gem_writel(bp, USRIO, val);
3414 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003415
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003416 /* Set MII management clock divider */
3417 val = macb_mdc_clk_div(bp);
3418 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303419 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3420 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003421 macb_writel(bp, NCFGR, val);
3422
3423 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003424}
3425
3426#if defined(CONFIG_OF)
3427/* 1518 rounded up */
3428#define AT91ETHER_MAX_RBUFF_SZ 0x600
3429/* max number of receive buffers */
3430#define AT91ETHER_MAX_RX_DESCR 9
3431
3432/* Initialize and start the Receiver and Transmit subsystems */
3433static int at91ether_start(struct net_device *dev)
3434{
3435 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003436 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003437 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003438 dma_addr_t addr;
3439 u32 ctl;
3440 int i;
3441
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003442 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003443 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003444 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003445 &q->rx_ring_dma, GFP_KERNEL);
3446 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003447 return -ENOMEM;
3448
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003449 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003450 AT91ETHER_MAX_RX_DESCR *
3451 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003452 &q->rx_buffers_dma, GFP_KERNEL);
3453 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003454 dma_free_coherent(&lp->pdev->dev,
3455 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003456 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003457 q->rx_ring, q->rx_ring_dma);
3458 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003459 return -ENOMEM;
3460 }
3461
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003462 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003463 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003464 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003465 macb_set_addr(lp, desc, addr);
3466 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003467 addr += AT91ETHER_MAX_RBUFF_SZ;
3468 }
3469
3470 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003471 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003472
3473 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003474 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003475
3476 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003477 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003478
3479 /* Enable Receive and Transmit */
3480 ctl = macb_readl(lp, NCR);
3481 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3482
3483 return 0;
3484}
3485
3486/* Open the ethernet interface */
3487static int at91ether_open(struct net_device *dev)
3488{
3489 struct macb *lp = netdev_priv(dev);
3490 u32 ctl;
3491 int ret;
3492
3493 /* Clear internal statistics */
3494 ctl = macb_readl(lp, NCR);
3495 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3496
3497 macb_set_hwaddr(lp);
3498
3499 ret = at91ether_start(dev);
3500 if (ret)
3501 return ret;
3502
3503 /* Enable MAC interrupts */
3504 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3505 MACB_BIT(RXUBR) |
3506 MACB_BIT(ISR_TUND) |
3507 MACB_BIT(ISR_RLE) |
3508 MACB_BIT(TCOMP) |
3509 MACB_BIT(ISR_ROVR) |
3510 MACB_BIT(HRESP));
3511
3512 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003513 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003514
3515 netif_start_queue(dev);
3516
3517 return 0;
3518}
3519
3520/* Close the interface */
3521static int at91ether_close(struct net_device *dev)
3522{
3523 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003524 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003525 u32 ctl;
3526
3527 /* Disable Receiver and Transmitter */
3528 ctl = macb_readl(lp, NCR);
3529 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3530
3531 /* Disable MAC interrupts */
3532 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3533 MACB_BIT(RXUBR) |
3534 MACB_BIT(ISR_TUND) |
3535 MACB_BIT(ISR_RLE) |
3536 MACB_BIT(TCOMP) |
3537 MACB_BIT(ISR_ROVR) |
3538 MACB_BIT(HRESP));
3539
3540 netif_stop_queue(dev);
3541
3542 dma_free_coherent(&lp->pdev->dev,
3543 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003544 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003545 q->rx_ring, q->rx_ring_dma);
3546 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003547
3548 dma_free_coherent(&lp->pdev->dev,
3549 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003550 q->rx_buffers, q->rx_buffers_dma);
3551 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003552
3553 return 0;
3554}
3555
3556/* Transmit packet */
3557static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
3558{
3559 struct macb *lp = netdev_priv(dev);
3560
3561 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3562 netif_stop_queue(dev);
3563
3564 /* Store packet information (to free when Tx completed) */
3565 lp->skb = skb;
3566 lp->skb_length = skb->len;
3567 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3568 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003569 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3570 dev_kfree_skb_any(skb);
3571 dev->stats.tx_dropped++;
3572 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3573 return NETDEV_TX_OK;
3574 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003575
3576 /* Set address of the data in the Transmit Address register */
3577 macb_writel(lp, TAR, lp->skb_physaddr);
3578 /* Set length of the packet in the Transmit Control register */
3579 macb_writel(lp, TCR, skb->len);
3580
3581 } else {
3582 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3583 return NETDEV_TX_BUSY;
3584 }
3585
3586 return NETDEV_TX_OK;
3587}
3588
3589/* Extract received frame from buffer descriptors and sent to upper layers.
3590 * (Called from interrupt context)
3591 */
3592static void at91ether_rx(struct net_device *dev)
3593{
3594 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003595 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003596 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003597 unsigned char *p_recv;
3598 struct sk_buff *skb;
3599 unsigned int pktlen;
3600
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003601 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003602 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003603 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003604 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003605 skb = netdev_alloc_skb(dev, pktlen + 2);
3606 if (skb) {
3607 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003608 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003609
3610 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003611 dev->stats.rx_packets++;
3612 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003613 netif_rx(skb);
3614 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003615 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003616 }
3617
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003618 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003619 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003620
3621 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003622 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003623
3624 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003625 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3626 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003627 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003628 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003629
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003630 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003631 }
3632}
3633
3634/* MAC interrupt handler */
3635static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3636{
3637 struct net_device *dev = dev_id;
3638 struct macb *lp = netdev_priv(dev);
3639 u32 intstatus, ctl;
3640
3641 /* MAC Interrupt Status register indicates what interrupts are pending.
3642 * It is automatically cleared once read.
3643 */
3644 intstatus = macb_readl(lp, ISR);
3645
3646 /* Receive complete */
3647 if (intstatus & MACB_BIT(RCOMP))
3648 at91ether_rx(dev);
3649
3650 /* Transmit complete */
3651 if (intstatus & MACB_BIT(TCOMP)) {
3652 /* The TCOM bit is set even if the transmission failed */
3653 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003654 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003655
3656 if (lp->skb) {
3657 dev_kfree_skb_irq(lp->skb);
3658 lp->skb = NULL;
3659 dma_unmap_single(NULL, lp->skb_physaddr,
3660 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003661 dev->stats.tx_packets++;
3662 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003663 }
3664 netif_wake_queue(dev);
3665 }
3666
3667 /* Work-around for EMAC Errata section 41.3.1 */
3668 if (intstatus & MACB_BIT(RXUBR)) {
3669 ctl = macb_readl(lp, NCR);
3670 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003671 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003672 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3673 }
3674
3675 if (intstatus & MACB_BIT(ISR_ROVR))
3676 netdev_err(dev, "ROVR error\n");
3677
3678 return IRQ_HANDLED;
3679}
3680
3681#ifdef CONFIG_NET_POLL_CONTROLLER
3682static void at91ether_poll_controller(struct net_device *dev)
3683{
3684 unsigned long flags;
3685
3686 local_irq_save(flags);
3687 at91ether_interrupt(dev->irq, dev);
3688 local_irq_restore(flags);
3689}
3690#endif
3691
3692static const struct net_device_ops at91ether_netdev_ops = {
3693 .ndo_open = at91ether_open,
3694 .ndo_stop = at91ether_close,
3695 .ndo_start_xmit = at91ether_start_xmit,
3696 .ndo_get_stats = macb_get_stats,
3697 .ndo_set_rx_mode = macb_set_rx_mode,
3698 .ndo_set_mac_address = eth_mac_addr,
3699 .ndo_do_ioctl = macb_ioctl,
3700 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003701#ifdef CONFIG_NET_POLL_CONTROLLER
3702 .ndo_poll_controller = at91ether_poll_controller,
3703#endif
3704};
3705
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003706static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303707 struct clk **hclk, struct clk **tx_clk,
3708 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003709{
3710 int err;
3711
3712 *hclk = NULL;
3713 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303714 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003715
3716 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3717 if (IS_ERR(*pclk))
3718 return PTR_ERR(*pclk);
3719
3720 err = clk_prepare_enable(*pclk);
3721 if (err) {
3722 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3723 return err;
3724 }
3725
3726 return 0;
3727}
3728
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003729static int at91ether_init(struct platform_device *pdev)
3730{
3731 struct net_device *dev = platform_get_drvdata(pdev);
3732 struct macb *bp = netdev_priv(dev);
3733 int err;
3734 u32 reg;
3735
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003736 dev->netdev_ops = &at91ether_netdev_ops;
3737 dev->ethtool_ops = &macb_ethtool_ops;
3738
3739 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3740 0, dev->name, dev);
3741 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003742 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003743
3744 macb_writel(bp, NCR, 0);
3745
3746 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3747 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3748 reg |= MACB_BIT(RM9200_RMII);
3749
3750 macb_writel(bp, NCFGR, reg);
3751
3752 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003753}
3754
David S. Miller3cef5c52015-03-09 23:38:02 -04003755static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003756 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003757 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003758 .init = macb_init,
3759};
3760
David S. Miller3cef5c52015-03-09 23:38:02 -04003761static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003762 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3763 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003764 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003765 .init = macb_init,
3766};
3767
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003768static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003769 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003770 .dma_burst_length = 16,
3771 .clk_init = macb_clk_init,
3772 .init = macb_init,
3773};
3774
David S. Miller3cef5c52015-03-09 23:38:02 -04003775static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003776 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003777 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003778 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003779 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003780 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003781 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003782};
3783
David S. Miller3cef5c52015-03-09 23:38:02 -04003784static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003785 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003786 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003787 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003788 .init = macb_init,
3789};
3790
David S. Miller3cef5c52015-03-09 23:38:02 -04003791static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003792 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003793 .init = at91ether_init,
3794};
3795
Neil Armstronge611b5b2016-01-05 14:39:17 +01003796static const struct macb_config np4_config = {
3797 .caps = MACB_CAPS_USRIO_DISABLED,
3798 .clk_init = macb_clk_init,
3799 .init = macb_init,
3800};
David S. Miller36583eb2015-05-23 01:22:35 -04003801
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303802static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003803 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3804 MACB_CAPS_JUMBO |
3805 MACB_CAPS_GEM_HAS_PTP,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303806 .dma_burst_length = 16,
3807 .clk_init = macb_clk_init,
3808 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303809 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303810};
3811
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003812static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303813 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003814 .dma_burst_length = 16,
3815 .clk_init = macb_clk_init,
3816 .init = macb_init,
3817};
3818
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003819static const struct of_device_id macb_dt_ids[] = {
3820 { .compatible = "cdns,at32ap7000-macb" },
3821 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3822 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003823 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003824 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3825 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003826 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003827 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3828 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3829 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3830 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303831 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003832 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003833 { /* sentinel */ }
3834};
3835MODULE_DEVICE_TABLE(of, macb_dt_ids);
3836#endif /* CONFIG_OF */
3837
Bartosz Folta83a77e92016-12-14 06:39:15 +00003838static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003839 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3840 MACB_CAPS_JUMBO |
3841 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003842 .dma_burst_length = 16,
3843 .clk_init = macb_clk_init,
3844 .init = macb_init,
3845 .jumbo_max_len = 10240,
3846};
3847
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003848static int macb_probe(struct platform_device *pdev)
3849{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003850 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003851 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303852 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003853 = macb_config->clk_init;
3854 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003855 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303856 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003857 unsigned int queue_mask, num_queues;
3858 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003859 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003860 struct phy_device *phydev;
3861 struct net_device *dev;
3862 struct resource *regs;
3863 void __iomem *mem;
3864 const char *mac;
3865 struct macb *bp;
3866 int err;
3867
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003868 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3869 mem = devm_ioremap_resource(&pdev->dev, regs);
3870 if (IS_ERR(mem))
3871 return PTR_ERR(mem);
3872
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003873 if (np) {
3874 const struct of_device_id *match;
3875
3876 match = of_match_node(macb_dt_ids, np);
3877 if (match && match->data) {
3878 macb_config = match->data;
3879 clk_init = macb_config->clk_init;
3880 init = macb_config->init;
3881 }
3882 }
3883
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303884 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003885 if (err)
3886 return err;
3887
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003888 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003889
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003890 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003891 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003892 if (!dev) {
3893 err = -ENOMEM;
3894 goto err_disable_clocks;
3895 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003896
3897 dev->base_addr = regs->start;
3898
3899 SET_NETDEV_DEV(dev, &pdev->dev);
3900
3901 bp = netdev_priv(dev);
3902 bp->pdev = pdev;
3903 bp->dev = dev;
3904 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003905 bp->native_io = native_io;
3906 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003907 bp->macb_reg_readl = hw_readl_native;
3908 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003909 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003910 bp->macb_reg_readl = hw_readl;
3911 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003912 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003913 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003914 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003915 if (macb_config)
3916 bp->dma_burst_length = macb_config->dma_burst_length;
3917 bp->pclk = pclk;
3918 bp->hclk = hclk;
3919 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303920 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003921 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303922 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303923
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003924 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003925 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003926 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3927 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3928
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003929 spin_lock_init(&bp->lock);
3930
Nicolas Ferread783472015-03-31 15:02:02 +02003931 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003932 macb_configure_caps(bp, macb_config);
3933
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003934#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3935 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3936 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3937 bp->hw_dma_cap |= HW_DMA_CAP_64B;
3938 }
3939#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003940 platform_set_drvdata(pdev, dev);
3941
3942 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003943 if (dev->irq < 0) {
3944 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003945 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003946 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003947
Jarod Wilson44770e12016-10-17 15:54:17 -04003948 /* MTU range: 68 - 1500 or 10240 */
3949 dev->min_mtu = GEM_MTU_MIN_SIZE;
3950 if (bp->caps & MACB_CAPS_JUMBO)
3951 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3952 else
3953 dev->max_mtu = ETH_DATA_LEN;
3954
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003955 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003956 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003957 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003958 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003959 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003960
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003961 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003962 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003963 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003964 if (pdata && pdata->is_rmii)
3965 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3966 else
3967 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3968 } else {
3969 bp->phy_interface = err;
3970 }
3971
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003972 /* IP specific init */
3973 err = init(pdev);
3974 if (err)
3975 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003976
Florian Fainellicf669662016-05-02 18:38:45 -07003977 err = macb_mii_init(bp);
3978 if (err)
3979 goto err_out_free_netdev;
3980
Philippe Reynes0a912812016-06-22 00:32:35 +02003981 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003982
3983 netif_carrier_off(dev);
3984
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003985 err = register_netdev(dev);
3986 if (err) {
3987 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003988 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003989 }
3990
Harini Katakam032dc412018-01-27 12:09:01 +05303991 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
3992 (unsigned long)bp);
3993
Florian Fainellicf669662016-05-02 18:38:45 -07003994 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003995
Bo Shen58798232014-09-13 01:57:49 +02003996 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3997 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3998 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003999
4000 return 0;
4001
Florian Fainellicf669662016-05-02 18:38:45 -07004002err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004003 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004004 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004005 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004006 if (np && of_phy_is_fixed_link(np))
4007 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004008 mdiobus_free(bp->mii_bus);
4009
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004010err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004011 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004012
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004013err_disable_clocks:
4014 clk_disable_unprepare(tx_clk);
4015 clk_disable_unprepare(hclk);
4016 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304017 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004018
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004019 return err;
4020}
4021
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004022static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004023{
4024 struct net_device *dev;
4025 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004026 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004027
4028 dev = platform_get_drvdata(pdev);
4029
4030 if (dev) {
4031 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004032 if (dev->phydev)
4033 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004034 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004035 if (np && of_phy_is_fixed_link(np))
4036 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004037 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004038 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004039
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004040 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004041 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004042 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004043 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304044 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004045 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004046 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004047 }
4048
4049 return 0;
4050}
4051
Michal Simekd23823d2015-01-23 09:36:03 +01004052static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004053{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004054 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004055 struct net_device *netdev = platform_get_drvdata(pdev);
4056 struct macb *bp = netdev_priv(netdev);
4057
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004058 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004059 netif_device_detach(netdev);
4060
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004061 if (bp->wol & MACB_WOL_ENABLED) {
4062 macb_writel(bp, IER, MACB_BIT(WOL));
4063 macb_writel(bp, WOL, MACB_BIT(MAG));
4064 enable_irq_wake(bp->queues[0].irq);
4065 } else {
4066 clk_disable_unprepare(bp->tx_clk);
4067 clk_disable_unprepare(bp->hclk);
4068 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304069 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004070 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004071
4072 return 0;
4073}
4074
Michal Simekd23823d2015-01-23 09:36:03 +01004075static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004076{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004077 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004078 struct net_device *netdev = platform_get_drvdata(pdev);
4079 struct macb *bp = netdev_priv(netdev);
4080
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004081 if (bp->wol & MACB_WOL_ENABLED) {
4082 macb_writel(bp, IDR, MACB_BIT(WOL));
4083 macb_writel(bp, WOL, 0);
4084 disable_irq_wake(bp->queues[0].irq);
4085 } else {
4086 clk_prepare_enable(bp->pclk);
4087 clk_prepare_enable(bp->hclk);
4088 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304089 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004090 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004091
4092 netif_device_attach(netdev);
4093
4094 return 0;
4095}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004096
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004097static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4098
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004099static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004100 .probe = macb_probe,
4101 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004102 .driver = {
4103 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004104 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004105 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004106 },
4107};
4108
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004109module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004110
4111MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004112MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004113MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004114MODULE_ALIAS("platform:macb");