blob: 8e8d557901a970beea5287cf1ebfaa2ef1910f5b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00003 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004 *
5 * Copyright (C) 2004-2006 Atmel Corporation
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01006 */
7
Jamie Ilesc220f8c2011-03-08 20:27:08 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01009#include <linux/clk.h>
Yash Shahc218ad52019-06-18 13:26:08 +053010#include <linux/clk-provider.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030011#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000016#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010017#include <linux/slab.h>
18#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080019#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000020#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010021#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010031#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020032#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010033#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000034#include <linux/ip.h>
35#include <linux/udp.h>
36#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053037#include <linux/iopoll.h>
Harini Katakamd54f89a2019-03-01 16:20:34 +053038#include <linux/pm_runtime.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010039#include "macb.h"
40
Yash Shahc218ad52019-06-18 13:26:08 +053041/* This structure is only used for MACB on SiFive FU540 devices */
42struct sifive_fu540_macb_mgmt {
43 void __iomem *reg;
44 unsigned long rate;
45 struct clk_hw hw;
46};
47
Nicolas Ferre1b447912013-06-04 21:57:11 +000048#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000049#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050050
Zach Brownb410d132016-10-19 09:56:57 -050051#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050052#define MIN_RX_RING_SIZE 64
53#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000054#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050055 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010056
Zach Brownb410d132016-10-19 09:56:57 -050057#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050058#define MIN_TX_RING_SIZE 64
59#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000060#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050061 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010062
Nicolas Ferre909a8582012-11-19 06:00:21 +000063/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050064#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010065
Harini Katakame5010702019-01-29 15:20:03 +053066#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000067#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
68 | MACB_BIT(ISR_RLE) \
69 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000070#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
71 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000072
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000073/* Max length of transmit frame must be a multiple of 8 bytes */
74#define MACB_TX_LEN_ALIGN 8
75#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
76#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 +020077
Jarod Wilson44770e12016-10-17 15:54:17 -040078#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070079#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053080
Sergio Prado3e2a5e12016-02-09 12:07:16 -020081#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
82#define MACB_WOL_ENABLED (0x1 << 1)
83
Moritz Fischer64ec42f2016-03-29 19:11:12 -070084/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000085 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
86 */
87#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010088
Harini Katakamd54f89a2019-03-01 16:20:34 +053089#define MACB_PM_TIMEOUT 100 /* ms */
90
Harini Katakam8beb79b2019-03-01 16:20:32 +053091#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
92
Rafal Ozieblodc97a892017-01-27 15:08:20 +000093/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010094 * depends on hardware configuration:
95 *
96 * 1. dma address width 32 bits:
97 * word 1: 32 bit address of Data Buffer
98 * word 2: control
99 *
100 * 2. dma address width 64 bits:
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 *
106 * 3. dma address width 32 bits with hardware timestamping:
107 * word 1: 32 bit address of Data Buffer
108 * word 2: control
109 * word 3: timestamp word 1
110 * word 4: timestamp word 2
111 *
112 * 4. dma address width 64 bits with hardware timestamping:
113 * word 1: 32 bit address of Data Buffer
114 * word 2: control
115 * word 3: upper 32 bit address of Data Buffer
116 * word 4: unused
117 * word 5: timestamp word 1
118 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000119 */
120static unsigned int macb_dma_desc_get_size(struct macb *bp)
121{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100122#ifdef MACB_EXT_DESC
123 unsigned int desc_size;
124
125 switch (bp->hw_dma_cap) {
126 case HW_DMA_CAP_64B:
127 desc_size = sizeof(struct macb_dma_desc)
128 + sizeof(struct macb_dma_desc_64);
129 break;
130 case HW_DMA_CAP_PTP:
131 desc_size = sizeof(struct macb_dma_desc)
132 + sizeof(struct macb_dma_desc_ptp);
133 break;
134 case HW_DMA_CAP_64B_PTP:
135 desc_size = sizeof(struct macb_dma_desc)
136 + sizeof(struct macb_dma_desc_64)
137 + sizeof(struct macb_dma_desc_ptp);
138 break;
139 default:
140 desc_size = sizeof(struct macb_dma_desc);
141 }
142 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000143#endif
144 return sizeof(struct macb_dma_desc);
145}
146
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100147static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000148{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100149#ifdef MACB_EXT_DESC
150 switch (bp->hw_dma_cap) {
151 case HW_DMA_CAP_64B:
152 case HW_DMA_CAP_PTP:
153 desc_idx <<= 1;
154 break;
155 case HW_DMA_CAP_64B_PTP:
156 desc_idx *= 3;
157 break;
158 default:
159 break;
160 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000161#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100162 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000163}
164
165#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
166static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
167{
Shubhrajyoti Datta99dcb842019-09-23 14:03:51 +0530168 return (struct macb_dma_desc_64 *)((void *)desc
169 + sizeof(struct macb_dma_desc));
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000170}
171#endif
172
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000173/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500174static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000175{
Zach Brownb410d132016-10-19 09:56:57 -0500176 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000177}
178
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100179static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
180 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000182 index = macb_tx_ring_wrap(queue->bp, index);
183 index = macb_adj_dma_desc_idx(queue->bp, index);
184 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000185}
186
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100187static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
188 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000189{
Zach Brownb410d132016-10-19 09:56:57 -0500190 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000191}
192
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100193static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000194{
195 dma_addr_t offset;
196
Zach Brownb410d132016-10-19 09:56:57 -0500197 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000198 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000199
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100200 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000201}
202
Zach Brownb410d132016-10-19 09:56:57 -0500203static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000204{
Zach Brownb410d132016-10-19 09:56:57 -0500205 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000206}
207
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000208static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000209{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000210 index = macb_rx_ring_wrap(queue->bp, index);
211 index = macb_adj_dma_desc_idx(queue->bp, index);
212 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000213}
214
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000215static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000216{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000217 return queue->rx_buffers + queue->bp->rx_buffer_size *
218 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000219}
220
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300221/* I/O accessors */
222static u32 hw_readl_native(struct macb *bp, int offset)
223{
224 return __raw_readl(bp->regs + offset);
225}
226
227static void hw_writel_native(struct macb *bp, int offset, u32 value)
228{
229 __raw_writel(value, bp->regs + offset);
230}
231
232static u32 hw_readl(struct macb *bp, int offset)
233{
234 return readl_relaxed(bp->regs + offset);
235}
236
237static void hw_writel(struct macb *bp, int offset, u32 value)
238{
239 writel_relaxed(value, bp->regs + offset);
240}
241
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700242/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700243 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300244 * descriptor access.
245 */
246static bool hw_is_native_io(void __iomem *addr)
247{
248 u32 value = MACB_BIT(LLB);
249
250 __raw_writel(value, addr + MACB_NCR);
251 value = __raw_readl(addr + MACB_NCR);
252
253 /* Write 0 back to disable everything */
254 __raw_writel(0, addr + MACB_NCR);
255
256 return value == MACB_BIT(LLB);
257}
258
259static bool hw_is_gem(void __iomem *addr, bool native_io)
260{
261 u32 id;
262
263 if (native_io)
264 id = __raw_readl(addr + MACB_MID);
265 else
266 id = readl_relaxed(addr + MACB_MID);
267
268 return MACB_BFEXT(IDNUM, id) >= 0x2;
269}
270
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100271static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100272{
273 u32 bottom;
274 u16 top;
275
276 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000277 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100278 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000279 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000280
281 /* Clear unused address register sets */
282 macb_or_gem_writel(bp, SA2B, 0);
283 macb_or_gem_writel(bp, SA2T, 0);
284 macb_or_gem_writel(bp, SA3B, 0);
285 macb_or_gem_writel(bp, SA3T, 0);
286 macb_or_gem_writel(bp, SA4B, 0);
287 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100288}
289
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100290static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100291{
292 u32 bottom;
293 u16 top;
294 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000295 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100296
Moritz Fischeraa50b552016-03-29 19:11:13 -0700297 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000298 for (i = 0; i < 4; i++) {
299 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
300 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100301
Nicolas Ferre8b952742019-05-03 12:36:58 +0200302 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;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100308
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000309 if (is_valid_ether_addr(addr)) {
310 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
311 return;
312 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700313 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000314
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300315 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000316 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100317}
318
Harini Katakam8beb79b2019-03-01 16:20:32 +0530319static int macb_mdio_wait_for_idle(struct macb *bp)
320{
321 u32 val;
322
323 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
324 1, MACB_MDIO_TIMEOUT);
325}
326
frederic RODO6c36a702007-07-12 19:07:24 +0200327static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100328{
frederic RODO6c36a702007-07-12 19:07:24 +0200329 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530330 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530331
Harini Katakamd54f89a2019-03-01 16:20:34 +0530332 status = pm_runtime_get_sync(&bp->pdev->dev);
333 if (status < 0)
334 goto mdio_pm_exit;
335
336 status = macb_mdio_wait_for_idle(bp);
337 if (status < 0)
338 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100339
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100340 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
341 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200342 | MACB_BF(PHYA, mii_id)
343 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344 | MACB_BF(CODE, MACB_MAN_CODE)));
345
Harini Katakamd54f89a2019-03-01 16:20:34 +0530346 status = macb_mdio_wait_for_idle(bp);
347 if (status < 0)
348 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349
Harini Katakamd54f89a2019-03-01 16:20:34 +0530350 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
Harini Katakamd54f89a2019-03-01 16:20:34 +0530352mdio_read_exit:
353 pm_runtime_mark_last_busy(&bp->pdev->dev);
354 pm_runtime_put_autosuspend(&bp->pdev->dev);
355mdio_pm_exit:
356 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100357}
358
frederic RODO6c36a702007-07-12 19:07:24 +0200359static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
360 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100361{
frederic RODO6c36a702007-07-12 19:07:24 +0200362 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530363 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530364
Harini Katakamd54f89a2019-03-01 16:20:34 +0530365 status = pm_runtime_get_sync(&bp->pdev->dev);
366 if (status < 0)
367 goto mdio_pm_exit;
368
369 status = macb_mdio_wait_for_idle(bp);
370 if (status < 0)
371 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100372
373 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
374 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200375 | MACB_BF(PHYA, mii_id)
376 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100377 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200378 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100379
Harini Katakamd54f89a2019-03-01 16:20:34 +0530380 status = macb_mdio_wait_for_idle(bp);
381 if (status < 0)
382 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100383
Harini Katakamd54f89a2019-03-01 16:20:34 +0530384mdio_write_exit:
385 pm_runtime_mark_last_busy(&bp->pdev->dev);
386 pm_runtime_put_autosuspend(&bp->pdev->dev);
387mdio_pm_exit:
388 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100389}
390
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800391/**
392 * macb_set_tx_clk() - Set a clock to a new frequency
393 * @clk Pointer to the clock to change
394 * @rate New frequency in Hz
395 * @dev Pointer to the struct net_device
396 */
397static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
398{
399 long ferr, rate, rate_rounded;
400
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100401 if (!clk)
402 return;
403
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800404 switch (speed) {
405 case SPEED_10:
406 rate = 2500000;
407 break;
408 case SPEED_100:
409 rate = 25000000;
410 break;
411 case SPEED_1000:
412 rate = 125000000;
413 break;
414 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800415 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800416 }
417
418 rate_rounded = clk_round_rate(clk, rate);
419 if (rate_rounded < 0)
420 return;
421
422 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
423 * is not satisfied.
424 */
425 ferr = abs(rate_rounded - rate);
426 ferr = DIV_ROUND_UP(ferr, rate / 100000);
427 if (ferr > 5)
428 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700429 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800430
431 if (clk_set_rate(clk, rate_rounded))
432 netdev_err(dev, "adjusting tx_clk failed.\n");
433}
434
frederic RODO6c36a702007-07-12 19:07:24 +0200435static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436{
frederic RODO6c36a702007-07-12 19:07:24 +0200437 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200438 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200439 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200440 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100441
frederic RODO6c36a702007-07-12 19:07:24 +0200442 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100443
frederic RODO6c36a702007-07-12 19:07:24 +0200444 if (phydev->link) {
445 if ((bp->speed != phydev->speed) ||
446 (bp->duplex != phydev->duplex)) {
447 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100448
frederic RODO6c36a702007-07-12 19:07:24 +0200449 reg = macb_readl(bp, NCFGR);
450 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000451 if (macb_is_gem(bp))
452 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200453
454 if (phydev->duplex)
455 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900456 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200457 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200458 if (phydev->speed == SPEED_1000 &&
459 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000460 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200461
Patrice Vilchez140b7552012-10-31 06:04:50 +0000462 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200463
464 bp->speed = phydev->speed;
465 bp->duplex = phydev->duplex;
466 status_change = 1;
467 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100468 }
469
frederic RODO6c36a702007-07-12 19:07:24 +0200470 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700471 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200472 bp->speed = 0;
473 bp->duplex = -1;
474 }
475 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100476
frederic RODO6c36a702007-07-12 19:07:24 +0200477 status_change = 1;
478 }
479
480 spin_unlock_irqrestore(&bp->lock, flags);
481
482 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000483 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500484 /* Update the TX clock rate if and only if the link is
485 * up and there has been a link change.
486 */
487 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
488
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000489 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000490 netdev_info(dev, "link up (%d/%s)\n",
491 phydev->speed,
492 phydev->duplex == DUPLEX_FULL ?
493 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000494 } else {
495 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000496 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000497 }
frederic RODO6c36a702007-07-12 19:07:24 +0200498 }
499}
500
501/* based on au1000_eth. c*/
502static int macb_mii_probe(struct net_device *dev)
503{
504 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000505 struct phy_device *phydev;
Brad Mouring739de9a2018-03-13 16:32:13 -0500506 struct device_node *np;
Nicolas Ferre8b952742019-05-03 12:36:58 +0200507 int ret, i;
Brad Mouring739de9a2018-03-13 16:32:13 -0500508
Brad Mouring739de9a2018-03-13 16:32:13 -0500509 np = bp->pdev->dev.of_node;
510 ret = 0;
511
512 if (np) {
513 if (of_phy_is_fixed_link(np)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500514 bp->phy_node = of_node_get(np);
515 } else {
Brad Mouring2105a5d2018-03-13 16:32:15 -0500516 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
517 /* fallback to standard phy registration if no
518 * phy-handle was found nor any phy found during
519 * dt phy registration
Brad Mouring739de9a2018-03-13 16:32:13 -0500520 */
Brad Mouring2105a5d2018-03-13 16:32:15 -0500521 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500522 for (i = 0; i < PHY_MAX_ADDR; i++) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500523 phydev = mdiobus_scan(bp->mii_bus, i);
524 if (IS_ERR(phydev) &&
525 PTR_ERR(phydev) != -ENODEV) {
526 ret = PTR_ERR(phydev);
527 break;
528 }
529 }
530
531 if (ret)
532 return -ENODEV;
533 }
534 }
535 }
frederic RODO6c36a702007-07-12 19:07:24 +0200536
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200537 if (bp->phy_node) {
538 phydev = of_phy_connect(dev, bp->phy_node,
539 &macb_handle_link_change, 0,
540 bp->phy_interface);
541 if (!phydev)
542 return -ENODEV;
543 } else {
544 phydev = phy_find_first(bp->mii_bus);
545 if (!phydev) {
546 netdev_err(dev, "no PHY found\n");
547 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000548 }
frederic RODO6c36a702007-07-12 19:07:24 +0200549
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200550 /* attach the mac to the phy */
551 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
552 bp->phy_interface);
553 if (ret) {
554 netdev_err(dev, "Could not attach to PHY\n");
555 return ret;
556 }
frederic RODO6c36a702007-07-12 19:07:24 +0200557 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100558
frederic RODO6c36a702007-07-12 19:07:24 +0200559 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200560 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Andrew Lunn58056c12018-09-12 01:53:11 +0200561 phy_set_max_speed(phydev, SPEED_1000);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000562 else
Andrew Lunn58056c12018-09-12 01:53:11 +0200563 phy_set_max_speed(phydev, SPEED_100);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100564
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500565 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
Andrew Lunn41124fa2018-09-12 01:53:14 +0200566 phy_remove_link_mode(phydev,
567 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100568
frederic RODO6c36a702007-07-12 19:07:24 +0200569 bp->link = 0;
570 bp->speed = 0;
571 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200572
573 return 0;
574}
575
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100576static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200577{
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200578 struct device_node *np;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200579 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200580
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200581 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200582 macb_writel(bp, NCR, MACB_BIT(MPE));
583
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700584 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700585 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200586 err = -ENOMEM;
587 goto err_out;
588 }
589
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700590 bp->mii_bus->name = "MACB_mii_bus";
591 bp->mii_bus->read = &macb_mdio_read;
592 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000593 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700594 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700595 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700596 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700597
Jamie Iles91523942011-02-28 04:05:25 +0000598 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200599
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200600 np = bp->pdev->dev.of_node;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200601 if (np && of_phy_is_fixed_link(np)) {
602 if (of_phy_register_fixed_link(np) < 0) {
603 dev_err(&bp->pdev->dev,
604 "broken fixed-link specification %pOF\n", np);
605 goto err_out_free_mdiobus;
606 }
Brad Mouring739de9a2018-03-13 16:32:13 -0500607
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200608 err = mdiobus_register(bp->mii_bus);
609 } else {
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200610 err = of_mdiobus_register(bp->mii_bus, np);
611 }
612
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200613 if (err)
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200614 goto err_out_free_fixed_link;
frederic RODO6c36a702007-07-12 19:07:24 +0200615
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200616 err = macb_mii_probe(bp->dev);
617 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200618 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200619
620 return 0;
621
622err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700623 mdiobus_unregister(bp->mii_bus);
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200624err_out_free_fixed_link:
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100625 if (np && of_phy_is_fixed_link(np))
626 of_phy_deregister_fixed_link(np);
Brad Mouring739de9a2018-03-13 16:32:13 -0500627err_out_free_mdiobus:
628 of_node_put(bp->phy_node);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700629 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200630err_out:
631 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100632}
633
634static void macb_update_stats(struct macb *bp)
635{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000636 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
637 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300638 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100639
640 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
641
Moritz Fischer96ec6312016-03-29 19:11:11 -0700642 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700643 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100644}
645
Nicolas Ferree86cd532012-10-31 06:04:57 +0000646static int macb_halt_tx(struct macb *bp)
647{
648 unsigned long halt_time, timeout;
649 u32 status;
650
651 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
652
653 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
654 do {
655 halt_time = jiffies;
656 status = macb_readl(bp, TSR);
657 if (!(status & MACB_BIT(TGO)))
658 return 0;
659
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800660 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000661 } while (time_before(halt_time, timeout));
662
663 return -ETIMEDOUT;
664}
665
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200666static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
667{
668 if (tx_skb->mapping) {
669 if (tx_skb->mapped_as_page)
670 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
671 tx_skb->size, DMA_TO_DEVICE);
672 else
673 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
674 tx_skb->size, DMA_TO_DEVICE);
675 tx_skb->mapping = 0;
676 }
677
678 if (tx_skb->skb) {
679 dev_kfree_skb_any(tx_skb->skb);
680 tx_skb->skb = NULL;
681 }
682}
683
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000684static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530685{
Harini Katakamfff80192016-08-09 13:15:53 +0530686#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000687 struct macb_dma_desc_64 *desc_64;
688
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100689 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000690 desc_64 = macb_64b_desc(bp, desc);
691 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200692 /* The low bits of RX address contain the RX_USED bit, clearing
693 * of which allows packet RX. Make sure the high bits are also
694 * visible to HW at that point.
695 */
696 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000697 }
Harini Katakamfff80192016-08-09 13:15:53 +0530698#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000699 desc->addr = lower_32_bits(addr);
700}
701
702static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
703{
704 dma_addr_t addr = 0;
705#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
706 struct macb_dma_desc_64 *desc_64;
707
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100708 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000709 desc_64 = macb_64b_desc(bp, desc);
710 addr = ((u64)(desc_64->addrh) << 32);
711 }
712#endif
713 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
714 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530715}
716
Nicolas Ferree86cd532012-10-31 06:04:57 +0000717static void macb_tx_error_task(struct work_struct *work)
718{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100719 struct macb_queue *queue = container_of(work, struct macb_queue,
720 tx_error_task);
721 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000722 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100723 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000724 struct sk_buff *skb;
725 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100726 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000727
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100728 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
729 (unsigned int)(queue - bp->queues),
730 queue->tx_tail, queue->tx_head);
731
732 /* Prevent the queue IRQ handlers from running: each of them may call
733 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
734 * As explained below, we have to halt the transmission before updating
735 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
736 * network engine about the macb/gem being halted.
737 */
738 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000739
740 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100741 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000742
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700743 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000744 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100745 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000746 */
747 if (macb_halt_tx(bp))
748 /* Just complain for now, reinitializing TX path can be good */
749 netdev_err(bp->dev, "BUG: halt tx timed out\n");
750
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700751 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000752 * Free transmit buffers in upper layer.
753 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100754 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
755 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000756
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100757 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000758 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100759 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000760 skb = tx_skb->skb;
761
762 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200763 /* skb is set for the last buffer of the frame */
764 while (!skb) {
765 macb_tx_unmap(bp, tx_skb);
766 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100767 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200768 skb = tx_skb->skb;
769 }
770
771 /* ctrl still refers to the first buffer descriptor
772 * since it's the only one written back by the hardware
773 */
774 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
775 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500776 macb_tx_ring_wrap(bp, tail),
777 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200778 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000779 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200780 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000781 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200782 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000783 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700784 /* "Buffers exhausted mid-frame" errors may only happen
785 * if the driver is buggy, so complain loudly about
786 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000787 */
788 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
789 netdev_err(bp->dev,
790 "BUG: TX buffers exhausted mid-frame\n");
791
792 desc->ctrl = ctrl | MACB_BIT(TX_USED);
793 }
794
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200795 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000796 }
797
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100798 /* Set end of TX queue */
799 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000800 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100801 desc->ctrl = MACB_BIT(TX_USED);
802
Nicolas Ferree86cd532012-10-31 06:04:57 +0000803 /* Make descriptor updates visible to hardware */
804 wmb();
805
806 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000807 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530808#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100809 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000810 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530811#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000812 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100813 queue->tx_head = 0;
814 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000815
816 /* Housework before enabling TX IRQ */
817 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100818 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
819
820 /* Now we are ready to start transmission again */
821 netif_tx_start_all_queues(bp->dev);
822 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
823
824 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000825}
826
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100827static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100828{
829 unsigned int tail;
830 unsigned int head;
831 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100832 struct macb *bp = queue->bp;
833 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100834
835 status = macb_readl(bp, TSR);
836 macb_writel(bp, TSR, status);
837
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000838 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100839 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000840
Nicolas Ferree86cd532012-10-31 06:04:57 +0000841 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700842 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100844 head = queue->tx_head;
845 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000846 struct macb_tx_skb *tx_skb;
847 struct sk_buff *skb;
848 struct macb_dma_desc *desc;
849 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100850
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100851 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100852
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000853 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000855
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000856 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100857
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200858 /* TX_USED bit is only set by hardware on the very first buffer
859 * descriptor of the transmitted frame.
860 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000861 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100862 break;
863
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200864 /* Process all buffers of the current transmitted frame */
865 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100866 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200867 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000868
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200869 /* First, update TX stats if needed */
870 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -0400871 if (unlikely(skb_shinfo(skb)->tx_flags &
872 SKBTX_HW_TSTAMP) &&
873 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100874 /* skb now belongs to timestamp buffer
875 * and will be removed later
876 */
877 tx_skb->skb = NULL;
878 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200879 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500880 macb_tx_ring_wrap(bp, tail),
881 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200882 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000883 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200884 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000885 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200886 }
887
888 /* Now we can safely release resources */
889 macb_tx_unmap(bp, tx_skb);
890
891 /* skb is set only for the last buffer of the frame.
892 * WARNING: at this point skb has been freed by
893 * macb_tx_unmap().
894 */
895 if (skb)
896 break;
897 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100898 }
899
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100900 queue->tx_tail = tail;
901 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
902 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500903 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100904 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100905}
906
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000907static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000908{
909 unsigned int entry;
910 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000911 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000912 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000913 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000914
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000915 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
916 bp->rx_ring_size) > 0) {
917 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000918
919 /* Make hw descriptor updates visible to CPU */
920 rmb();
921
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000922 queue->rx_prepared_head++;
923 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000924
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000925 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000926 /* allocate sk_buff for this free entry in ring */
927 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700928 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000929 netdev_err(bp->dev,
930 "Unable to allocate sk_buff\n");
931 break;
932 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000933
934 /* now fill corresponding descriptor entry */
935 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700936 bp->rx_buffer_size,
937 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800938 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
939 dev_kfree_skb(skb);
940 break;
941 }
942
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000943 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000944
Zach Brownb410d132016-10-19 09:56:57 -0500945 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000946 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000947 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200948 /* Setting addr clears RX_USED and allows reception,
949 * make sure ctrl is cleared first to avoid a race.
950 */
951 dma_wmb();
952 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000953
954 /* properly align Ethernet header */
955 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530956 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000957 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200958 dma_wmb();
959 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000960 }
961 }
962
963 /* Make descriptor updates visible to hardware */
964 wmb();
965
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000966 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
967 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000968}
969
970/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000971static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000972 unsigned int end)
973{
974 unsigned int frag;
975
976 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000977 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700978
Nicolas Ferre4df95132013-06-04 21:57:12 +0000979 desc->addr &= ~MACB_BIT(RX_USED);
980 }
981
982 /* Make descriptor updates visible to hardware */
983 wmb();
984
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700985 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000986 * whatever caused this is updated, so we don't have to record
987 * anything.
988 */
989}
990
Antoine Tenart97236cd2019-06-21 17:30:02 +0200991static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
992 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000993{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000994 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000995 unsigned int len;
996 unsigned int entry;
997 struct sk_buff *skb;
998 struct macb_dma_desc *desc;
999 int count = 0;
1000
1001 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301002 u32 ctrl;
1003 dma_addr_t addr;
1004 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001005
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001006 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1007 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001008
1009 /* Make hw descriptor updates visible to CPU */
1010 rmb();
1011
Harini Katakamfff80192016-08-09 13:15:53 +05301012 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001013 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001014
Harini Katakamfff80192016-08-09 13:15:53 +05301015 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001016 break;
1017
Anssi Hannula6e0af292018-12-17 15:05:41 +02001018 /* Ensure ctrl is at least as up-to-date as rxused */
1019 dma_rmb();
1020
1021 ctrl = desc->ctrl;
1022
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001023 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001024 count++;
1025
1026 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1027 netdev_err(bp->dev,
1028 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001029 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001030 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001031 break;
1032 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001033 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001034 if (unlikely(!skb)) {
1035 netdev_err(bp->dev,
1036 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001037 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001038 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001039 break;
1040 }
1041 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001042 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301043 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001044
1045 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1046
1047 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001048 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001049 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001050
1051 skb->protocol = eth_type_trans(skb, bp->dev);
1052 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001053 if (bp->dev->features & NETIF_F_RXCSUM &&
1054 !(bp->dev->flags & IFF_PROMISC) &&
1055 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1056 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001057
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001058 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001059 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001060 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001061 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001062
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001063 gem_ptp_do_rxstamp(bp, skb, desc);
1064
Nicolas Ferre4df95132013-06-04 21:57:12 +00001065#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1066 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1067 skb->len, skb->csum);
1068 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001069 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001070 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1071 skb->data, 32, true);
1072#endif
1073
Antoine Tenart97236cd2019-06-21 17:30:02 +02001074 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001075 }
1076
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001077 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001078
1079 return count;
1080}
1081
Antoine Tenart97236cd2019-06-21 17:30:02 +02001082static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1083 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001084{
1085 unsigned int len;
1086 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001087 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001088 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001089 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001090 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001091
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001092 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301093 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001095 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001096 macb_rx_ring_wrap(bp, first_frag),
1097 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001098
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001099 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001100 * first buffer. Since the header is 14 bytes, this makes the
1101 * payload word-aligned.
1102 *
1103 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1104 * the two padding bytes into the skb so that we avoid hitting
1105 * the slowpath in memcpy(), and pull them off afterwards.
1106 */
1107 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001108 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001109 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001110 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001111 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001112 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001113 if (frag == last_frag)
1114 break;
1115 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001116
1117 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001118 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001119
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001120 return 1;
1121 }
1122
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001123 offset = 0;
1124 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001125 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001126 skb_put(skb, len);
1127
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001128 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001129 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001130
1131 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001132 if (unlikely(frag != last_frag)) {
1133 dev_kfree_skb_any(skb);
1134 return -1;
1135 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136 frag_len = len - offset;
1137 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001138 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001139 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001140 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001141 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001142 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001143 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001144
1145 if (frag == last_frag)
1146 break;
1147 }
1148
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001149 /* Make descriptor updates visible to hardware */
1150 wmb();
1151
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001152 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001153 skb->protocol = eth_type_trans(skb, bp->dev);
1154
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001155 bp->dev->stats.rx_packets++;
1156 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001157 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001158 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001159 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001160
1161 return 0;
1162}
1163
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001164static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001165{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001166 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001167 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001168 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001169 int i;
1170
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001171 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001172 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001173 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001174 macb_set_addr(bp, desc, addr);
1175 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001176 addr += bp->rx_buffer_size;
1177 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001178 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001179 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001180}
1181
Antoine Tenart97236cd2019-06-21 17:30:02 +02001182static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1183 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001184{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001185 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001186 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001187 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001188 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001189 int first_frag = -1;
1190
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001191 for (tail = queue->rx_tail; budget > 0; tail++) {
1192 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001193 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001194
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001195 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001197
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001198 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001199 break;
1200
Anssi Hannula6e0af292018-12-17 15:05:41 +02001201 /* Ensure ctrl is at least as up-to-date as addr */
1202 dma_rmb();
1203
1204 ctrl = desc->ctrl;
1205
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001206 if (ctrl & MACB_BIT(RX_SOF)) {
1207 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001208 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001209 first_frag = tail;
1210 }
1211
1212 if (ctrl & MACB_BIT(RX_EOF)) {
1213 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001214
1215 if (unlikely(first_frag == -1)) {
1216 reset_rx_queue = true;
1217 continue;
1218 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001219
Antoine Tenart97236cd2019-06-21 17:30:02 +02001220 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001221 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001222 if (unlikely(dropped < 0)) {
1223 reset_rx_queue = true;
1224 continue;
1225 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001226 if (!dropped) {
1227 received++;
1228 budget--;
1229 }
1230 }
1231 }
1232
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001233 if (unlikely(reset_rx_queue)) {
1234 unsigned long flags;
1235 u32 ctrl;
1236
1237 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1238
1239 spin_lock_irqsave(&bp->lock, flags);
1240
1241 ctrl = macb_readl(bp, NCR);
1242 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001244 macb_init_rx_ring(queue);
1245 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001246
1247 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1248
1249 spin_unlock_irqrestore(&bp->lock, flags);
1250 return received;
1251 }
1252
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001253 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001254 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001255 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001256 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001257
1258 return received;
1259}
1260
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001261static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001262{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001263 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1264 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001265 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001266 u32 status;
1267
1268 status = macb_readl(bp, RSR);
1269 macb_writel(bp, RSR, status);
1270
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001271 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001272 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273
Antoine Tenart97236cd2019-06-21 17:30:02 +02001274 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001275 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001276 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001277
Nicolas Ferre8770e912013-02-12 11:08:48 +01001278 /* Packets received while interrupts were disabled */
1279 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001280 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001281 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001282 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001283 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001284 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301285 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001286 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001287 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001288
1289 /* TODO: Handle errors */
1290
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001291 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001292}
1293
Harini Katakam032dc412018-01-27 12:09:01 +05301294static void macb_hresp_error_task(unsigned long data)
1295{
1296 struct macb *bp = (struct macb *)data;
1297 struct net_device *dev = bp->dev;
1298 struct macb_queue *queue = bp->queues;
1299 unsigned int q;
1300 u32 ctrl;
1301
1302 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301303 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301304 MACB_TX_INT_FLAGS |
1305 MACB_BIT(HRESP));
1306 }
1307 ctrl = macb_readl(bp, NCR);
1308 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1309 macb_writel(bp, NCR, ctrl);
1310
1311 netif_tx_stop_all_queues(dev);
1312 netif_carrier_off(dev);
1313
1314 bp->macbgem_ops.mog_init_rings(bp);
1315
1316 /* Initialize TX and RX buffers */
1317 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1318 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1319#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1320 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1321 queue_writel(queue, RBQPH,
1322 upper_32_bits(queue->rx_ring_dma));
1323#endif
1324 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1325#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1326 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1327 queue_writel(queue, TBQPH,
1328 upper_32_bits(queue->tx_ring_dma));
1329#endif
1330
1331 /* Enable interrupts */
1332 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301333 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301334 MACB_TX_INT_FLAGS |
1335 MACB_BIT(HRESP));
1336 }
1337
1338 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1339 macb_writel(bp, NCR, ctrl);
1340
1341 netif_carrier_on(dev);
1342 netif_tx_start_all_queues(dev);
1343}
1344
Claudiu Beznea42983882018-12-17 10:02:42 +00001345static void macb_tx_restart(struct macb_queue *queue)
1346{
1347 unsigned int head = queue->tx_head;
1348 unsigned int tail = queue->tx_tail;
1349 struct macb *bp = queue->bp;
1350
1351 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1352 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1353
1354 if (head == tail)
1355 return;
1356
1357 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1358}
1359
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001360static irqreturn_t macb_interrupt(int irq, void *dev_id)
1361{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001362 struct macb_queue *queue = dev_id;
1363 struct macb *bp = queue->bp;
1364 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001365 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001366
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001367 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001368
1369 if (unlikely(!status))
1370 return IRQ_NONE;
1371
1372 spin_lock(&bp->lock);
1373
1374 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001375 /* close possible race with dev_close */
1376 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001377 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001378 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1379 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001380 break;
1381 }
1382
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001383 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1384 (unsigned int)(queue - bp->queues),
1385 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001386
Harini Katakame5010702019-01-29 15:20:03 +05301387 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001388 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001389 * until we have processed the buffers. The
1390 * scheduling call may fail if the poll routine
1391 * is already scheduled, so disable interrupts
1392 * now.
1393 */
Harini Katakame5010702019-01-29 15:20:03 +05301394 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001395 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001396 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001397
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001398 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001399 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001400 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001401 }
1402 }
1403
Nicolas Ferree86cd532012-10-31 06:04:57 +00001404 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001405 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1406 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001407
1408 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001409 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001410
Nicolas Ferree86cd532012-10-31 06:04:57 +00001411 break;
1412 }
1413
1414 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001415 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001416
Claudiu Beznea42983882018-12-17 10:02:42 +00001417 if (status & MACB_BIT(TXUBR))
1418 macb_tx_restart(queue);
1419
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001420 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001421 * add that if/when we get our hands on a full-blown MII PHY.
1422 */
1423
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001424 /* There is a hardware issue under heavy load where DMA can
1425 * stop, this causes endless "used buffer descriptor read"
1426 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301427 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1428 * section 16.7.4 for details. RXUBR is only enabled for
1429 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001430 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001431 if (status & MACB_BIT(RXUBR)) {
1432 ctrl = macb_readl(bp, NCR);
1433 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001434 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001435 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1436
1437 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001438 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001439 }
1440
Alexander Steinb19f7f72011-04-13 05:03:24 +00001441 if (status & MACB_BIT(ISR_ROVR)) {
1442 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001443 if (macb_is_gem(bp))
1444 bp->hw_stats.gem.rx_overruns++;
1445 else
1446 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001447
1448 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001449 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001450 }
1451
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001452 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301453 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001454 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001455
1456 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001457 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001458 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001459 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001460 }
1461
1462 spin_unlock(&bp->lock);
1463
1464 return IRQ_HANDLED;
1465}
1466
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001467#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001468/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001469 * to allow network i/o with interrupts disabled.
1470 */
1471static void macb_poll_controller(struct net_device *dev)
1472{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001473 struct macb *bp = netdev_priv(dev);
1474 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001475 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001476 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001477
1478 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001479 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1480 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001481 local_irq_restore(flags);
1482}
1483#endif
1484
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001485static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001486 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001487 struct sk_buff *skb,
1488 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001489{
1490 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001491 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001492 struct macb_tx_skb *tx_skb = NULL;
1493 struct macb_dma_desc *desc;
1494 unsigned int offset, size, count = 0;
1495 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001496 unsigned int eof = 1, mss_mfs = 0;
1497 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1498
1499 /* LSO */
1500 if (skb_shinfo(skb)->gso_size != 0) {
1501 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1502 /* UDP - UFO */
1503 lso_ctrl = MACB_LSO_UFO_ENABLE;
1504 else
1505 /* TCP - TSO */
1506 lso_ctrl = MACB_LSO_TSO_ENABLE;
1507 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001508
1509 /* First, map non-paged data */
1510 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001511
1512 /* first buffer length */
1513 size = hdrlen;
1514
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001515 offset = 0;
1516 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001517 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001518 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001519
1520 mapping = dma_map_single(&bp->pdev->dev,
1521 skb->data + offset,
1522 size, DMA_TO_DEVICE);
1523 if (dma_mapping_error(&bp->pdev->dev, mapping))
1524 goto dma_error;
1525
1526 /* Save info to properly release resources */
1527 tx_skb->skb = NULL;
1528 tx_skb->mapping = mapping;
1529 tx_skb->size = size;
1530 tx_skb->mapped_as_page = false;
1531
1532 len -= size;
1533 offset += size;
1534 count++;
1535 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001536
1537 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001538 }
1539
1540 /* Then, map paged data from fragments */
1541 for (f = 0; f < nr_frags; f++) {
1542 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1543
1544 len = skb_frag_size(frag);
1545 offset = 0;
1546 while (len) {
1547 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001548 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001549 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001550
1551 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1552 offset, size, DMA_TO_DEVICE);
1553 if (dma_mapping_error(&bp->pdev->dev, mapping))
1554 goto dma_error;
1555
1556 /* Save info to properly release resources */
1557 tx_skb->skb = NULL;
1558 tx_skb->mapping = mapping;
1559 tx_skb->size = size;
1560 tx_skb->mapped_as_page = true;
1561
1562 len -= size;
1563 offset += size;
1564 count++;
1565 tx_head++;
1566 }
1567 }
1568
1569 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001570 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001571 netdev_err(bp->dev, "BUG! empty skb!\n");
1572 return 0;
1573 }
1574
1575 /* This is the last buffer of the frame: save socket buffer */
1576 tx_skb->skb = skb;
1577
1578 /* Update TX ring: update buffer descriptors in reverse order
1579 * to avoid race condition
1580 */
1581
1582 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1583 * to set the end of TX queue
1584 */
1585 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001586 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001587 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001588 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001589 desc->ctrl = ctrl;
1590
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001591 if (lso_ctrl) {
1592 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1593 /* include header and FCS in value given to h/w */
1594 mss_mfs = skb_shinfo(skb)->gso_size +
1595 skb_transport_offset(skb) +
1596 ETH_FCS_LEN;
1597 else /* TSO */ {
1598 mss_mfs = skb_shinfo(skb)->gso_size;
1599 /* TCP Sequence Number Source Select
1600 * can be set only for TSO
1601 */
1602 seq_ctrl = 0;
1603 }
1604 }
1605
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001606 do {
1607 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001608 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001609 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001610 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001611
1612 ctrl = (u32)tx_skb->size;
1613 if (eof) {
1614 ctrl |= MACB_BIT(TX_LAST);
1615 eof = 0;
1616 }
Zach Brownb410d132016-10-19 09:56:57 -05001617 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001618 ctrl |= MACB_BIT(TX_WRAP);
1619
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001620 /* First descriptor is header descriptor */
1621 if (i == queue->tx_head) {
1622 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1623 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001624 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1625 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1626 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001627 } else
1628 /* Only set MSS/MFS on payload descriptors
1629 * (second or later descriptor)
1630 */
1631 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1632
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001633 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001634 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001635 /* desc->addr must be visible to hardware before clearing
1636 * 'TX_USED' bit in desc->ctrl.
1637 */
1638 wmb();
1639 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001640 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001641
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001642 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001643
1644 return count;
1645
1646dma_error:
1647 netdev_err(bp->dev, "TX DMA map failed\n");
1648
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001649 for (i = queue->tx_head; i != tx_head; i++) {
1650 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001651
1652 macb_tx_unmap(bp, tx_skb);
1653 }
1654
1655 return 0;
1656}
1657
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001658static netdev_features_t macb_features_check(struct sk_buff *skb,
1659 struct net_device *dev,
1660 netdev_features_t features)
1661{
1662 unsigned int nr_frags, f;
1663 unsigned int hdrlen;
1664
1665 /* Validate LSO compatibility */
1666
1667 /* there is only one buffer */
1668 if (!skb_is_nonlinear(skb))
1669 return features;
1670
1671 /* length of header */
1672 hdrlen = skb_transport_offset(skb);
1673 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1674 hdrlen += tcp_hdrlen(skb);
1675
1676 /* For LSO:
1677 * When software supplies two or more payload buffers all payload buffers
1678 * apart from the last must be a multiple of 8 bytes in size.
1679 */
1680 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1681 return features & ~MACB_NETIF_LSO;
1682
1683 nr_frags = skb_shinfo(skb)->nr_frags;
1684 /* No need to check last fragment */
1685 nr_frags--;
1686 for (f = 0; f < nr_frags; f++) {
1687 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1688
1689 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1690 return features & ~MACB_NETIF_LSO;
1691 }
1692 return features;
1693}
1694
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001695static inline int macb_clear_csum(struct sk_buff *skb)
1696{
1697 /* no change for packets without checksum offloading */
1698 if (skb->ip_summed != CHECKSUM_PARTIAL)
1699 return 0;
1700
1701 /* make sure we can modify the header */
1702 if (unlikely(skb_cow_head(skb, 0)))
1703 return -1;
1704
1705 /* initialize checksum field
1706 * This is required - at least for Zynq, which otherwise calculates
1707 * wrong UDP header checksums for UDP packets with UDP data len <=2
1708 */
1709 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1710 return 0;
1711}
1712
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001713static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1714{
1715 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1716 int padlen = ETH_ZLEN - (*skb)->len;
1717 int headroom = skb_headroom(*skb);
1718 int tailroom = skb_tailroom(*skb);
1719 struct sk_buff *nskb;
1720 u32 fcs;
1721
1722 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1723 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1724 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1725 return 0;
1726
1727 if (padlen <= 0) {
1728 /* FCS could be appeded to tailroom. */
1729 if (tailroom >= ETH_FCS_LEN)
1730 goto add_fcs;
1731 /* FCS could be appeded by moving data to headroom. */
1732 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1733 padlen = 0;
1734 /* No room for FCS, need to reallocate skb. */
1735 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001736 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001737 } else {
1738 /* Add room for FCS. */
1739 padlen += ETH_FCS_LEN;
1740 }
1741
1742 if (!cloned && headroom + tailroom >= padlen) {
1743 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1744 skb_set_tail_pointer(*skb, (*skb)->len);
1745 } else {
1746 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1747 if (!nskb)
1748 return -ENOMEM;
1749
Huang Zijiangf3e5c072019-02-14 14:41:18 +08001750 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001751 *skb = nskb;
1752 }
1753
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001754 if (padlen > ETH_FCS_LEN)
1755 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001756
1757add_fcs:
1758 /* set FCS to packet */
1759 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1760 fcs = ~fcs;
1761
1762 skb_put_u8(*skb, fcs & 0xff);
1763 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1764 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1765 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1766
1767 return 0;
1768}
1769
Claudiu Beznead1c38952018-08-07 12:25:12 +03001770static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001771{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001772 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001773 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001774 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001775 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001776 unsigned int desc_cnt, nr_frags, frag_size, f;
1777 unsigned int hdrlen;
1778 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001779 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001780
Claudiu Beznea33729f22018-08-07 12:25:13 +03001781 if (macb_clear_csum(skb)) {
1782 dev_kfree_skb_any(skb);
1783 return ret;
1784 }
1785
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001786 if (macb_pad_and_fcs(&skb, dev)) {
1787 dev_kfree_skb_any(skb);
1788 return ret;
1789 }
1790
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001791 is_lso = (skb_shinfo(skb)->gso_size != 0);
1792
1793 if (is_lso) {
1794 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1795
1796 /* length of headers */
1797 if (is_udp)
1798 /* only queue eth + ip headers separately for UDP */
1799 hdrlen = skb_transport_offset(skb);
1800 else
1801 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1802 if (skb_headlen(skb) < hdrlen) {
1803 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1804 /* if this is required, would need to copy to single buffer */
1805 return NETDEV_TX_BUSY;
1806 }
1807 } else
1808 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001809
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001810#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1811 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001812 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1813 queue_index, skb->len, skb->head, skb->data,
1814 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001815 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1816 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001817#endif
1818
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001819 /* Count how many TX buffer descriptors are needed to send this
1820 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001821 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001822 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001823 if (is_lso && (skb_headlen(skb) > hdrlen))
1824 /* extra header descriptor if also payload in first buffer */
1825 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1826 else
1827 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001828 nr_frags = skb_shinfo(skb)->nr_frags;
1829 for (f = 0; f < nr_frags; f++) {
1830 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001831 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001832 }
1833
Dongdong Deng48719532009-08-23 19:49:07 -07001834 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001835
1836 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001837 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001838 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001839 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001840 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001841 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001842 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001843 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001844 }
1845
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001846 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001847 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001848 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001849 goto unlock;
1850 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001851
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001852 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001853 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001854 skb_tx_timestamp(skb);
1855
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001856 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1857
Zach Brownb410d132016-10-19 09:56:57 -05001858 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001859 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001860
Soren Brinkmann92030902014-03-04 08:46:39 -08001861unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001862 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001863
Claudiu Beznead1c38952018-08-07 12:25:12 +03001864 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001865}
1866
Nicolas Ferre4df95132013-06-04 21:57:12 +00001867static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001868{
1869 if (!macb_is_gem(bp)) {
1870 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1871 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001872 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001873
Nicolas Ferre1b447912013-06-04 21:57:11 +00001874 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001875 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001876 "RX buffer must be multiple of %d bytes, expanding\n",
1877 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001878 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001879 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001880 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001881 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001882
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001883 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001884 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001885}
1886
Nicolas Ferre4df95132013-06-04 21:57:12 +00001887static void gem_free_rx_buffers(struct macb *bp)
1888{
1889 struct sk_buff *skb;
1890 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001891 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001892 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001893 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001894 int i;
1895
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001896 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1897 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001898 continue;
1899
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001900 for (i = 0; i < bp->rx_ring_size; i++) {
1901 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001902
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001903 if (!skb)
1904 continue;
1905
1906 desc = macb_rx_desc(queue, i);
1907 addr = macb_get_addr(bp, desc);
1908
1909 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1910 DMA_FROM_DEVICE);
1911 dev_kfree_skb_any(skb);
1912 skb = NULL;
1913 }
1914
1915 kfree(queue->rx_skbuff);
1916 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001917 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001918}
1919
1920static void macb_free_rx_buffers(struct macb *bp)
1921{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001922 struct macb_queue *queue = &bp->queues[0];
1923
1924 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001925 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001926 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001927 queue->rx_buffers, queue->rx_buffers_dma);
1928 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001929 }
1930}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001931
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001932static void macb_free_consistent(struct macb *bp)
1933{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001934 struct macb_queue *queue;
1935 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301936 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001937
Nicolas Ferre4df95132013-06-04 21:57:12 +00001938 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001939
1940 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1941 kfree(queue->tx_skb);
1942 queue->tx_skb = NULL;
1943 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301944 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1945 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001946 queue->tx_ring, queue->tx_ring_dma);
1947 queue->tx_ring = NULL;
1948 }
Harini Katakame50b7702018-07-06 12:18:57 +05301949 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301950 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1951 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301952 queue->rx_ring, queue->rx_ring_dma);
1953 queue->rx_ring = NULL;
1954 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001955 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001956}
1957
1958static int gem_alloc_rx_buffers(struct macb *bp)
1959{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001960 struct macb_queue *queue;
1961 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001962 int size;
1963
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001964 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1965 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1966 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1967 if (!queue->rx_skbuff)
1968 return -ENOMEM;
1969 else
1970 netdev_dbg(bp->dev,
1971 "Allocated %d RX struct sk_buff entries at %p\n",
1972 bp->rx_ring_size, queue->rx_skbuff);
1973 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001974 return 0;
1975}
1976
1977static int macb_alloc_rx_buffers(struct macb *bp)
1978{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001979 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001980 int size;
1981
Zach Brownb410d132016-10-19 09:56:57 -05001982 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001983 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1984 &queue->rx_buffers_dma, GFP_KERNEL);
1985 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001986 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001987
1988 netdev_dbg(bp->dev,
1989 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001990 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001991 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992}
1993
1994static int macb_alloc_consistent(struct macb *bp)
1995{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001996 struct macb_queue *queue;
1997 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001998 int size;
1999
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002000 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302001 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002002 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2003 &queue->tx_ring_dma,
2004 GFP_KERNEL);
2005 if (!queue->tx_ring)
2006 goto out_err;
2007 netdev_dbg(bp->dev,
2008 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2009 q, size, (unsigned long)queue->tx_ring_dma,
2010 queue->tx_ring);
2011
Zach Brownb410d132016-10-19 09:56:57 -05002012 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002013 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2014 if (!queue->tx_skb)
2015 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002016
Harini Katakam404cd082018-07-06 12:18:58 +05302017 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002018 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2019 &queue->rx_ring_dma, GFP_KERNEL);
2020 if (!queue->rx_ring)
2021 goto out_err;
2022 netdev_dbg(bp->dev,
2023 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2024 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002025 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002026 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002027 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002028
2029 return 0;
2030
2031out_err:
2032 macb_free_consistent(bp);
2033 return -ENOMEM;
2034}
2035
Nicolas Ferre4df95132013-06-04 21:57:12 +00002036static void gem_init_rings(struct macb *bp)
2037{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002038 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002039 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002040 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002041 int i;
2042
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002043 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002044 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002045 desc = macb_tx_desc(queue, i);
2046 macb_set_addr(bp, desc, 0);
2047 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002048 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002049 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002050 queue->tx_head = 0;
2051 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002052
2053 queue->rx_tail = 0;
2054 queue->rx_prepared_head = 0;
2055
2056 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002057 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002058
Nicolas Ferre4df95132013-06-04 21:57:12 +00002059}
2060
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002061static void macb_init_rings(struct macb *bp)
2062{
2063 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002064 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002065
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002066 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002067
Zach Brownb410d132016-10-19 09:56:57 -05002068 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002069 desc = macb_tx_desc(&bp->queues[0], i);
2070 macb_set_addr(bp, desc, 0);
2071 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002072 }
Ben Shelton21d35152015-04-22 17:28:54 -05002073 bp->queues[0].tx_head = 0;
2074 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002075 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002076}
2077
2078static void macb_reset_hw(struct macb *bp)
2079{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002080 struct macb_queue *queue;
2081 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002082 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002083
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002084 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002085 * more gracefully?)
2086 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002087 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002088
2089 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002090 ctrl |= MACB_BIT(CLRSTAT);
2091
2092 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002093
2094 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002095 macb_writel(bp, TSR, -1);
2096 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002097
2098 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002099 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2100 queue_writel(queue, IDR, -1);
2101 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002102 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2103 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002104 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002105}
2106
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002107static u32 gem_mdc_clk_div(struct macb *bp)
2108{
2109 u32 config;
2110 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2111
2112 if (pclk_hz <= 20000000)
2113 config = GEM_BF(CLK, GEM_CLK_DIV8);
2114 else if (pclk_hz <= 40000000)
2115 config = GEM_BF(CLK, GEM_CLK_DIV16);
2116 else if (pclk_hz <= 80000000)
2117 config = GEM_BF(CLK, GEM_CLK_DIV32);
2118 else if (pclk_hz <= 120000000)
2119 config = GEM_BF(CLK, GEM_CLK_DIV48);
2120 else if (pclk_hz <= 160000000)
2121 config = GEM_BF(CLK, GEM_CLK_DIV64);
2122 else
2123 config = GEM_BF(CLK, GEM_CLK_DIV96);
2124
2125 return config;
2126}
2127
2128static u32 macb_mdc_clk_div(struct macb *bp)
2129{
2130 u32 config;
2131 unsigned long pclk_hz;
2132
2133 if (macb_is_gem(bp))
2134 return gem_mdc_clk_div(bp);
2135
2136 pclk_hz = clk_get_rate(bp->pclk);
2137 if (pclk_hz <= 20000000)
2138 config = MACB_BF(CLK, MACB_CLK_DIV8);
2139 else if (pclk_hz <= 40000000)
2140 config = MACB_BF(CLK, MACB_CLK_DIV16);
2141 else if (pclk_hz <= 80000000)
2142 config = MACB_BF(CLK, MACB_CLK_DIV32);
2143 else
2144 config = MACB_BF(CLK, MACB_CLK_DIV64);
2145
2146 return config;
2147}
2148
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002149/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002150 * should program. We find the width from decoding the design configuration
2151 * register to find the maximum supported data bus width.
2152 */
2153static u32 macb_dbw(struct macb *bp)
2154{
2155 if (!macb_is_gem(bp))
2156 return 0;
2157
2158 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2159 case 4:
2160 return GEM_BF(DBW, GEM_DBW128);
2161 case 2:
2162 return GEM_BF(DBW, GEM_DBW64);
2163 case 1:
2164 default:
2165 return GEM_BF(DBW, GEM_DBW32);
2166 }
2167}
2168
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002169/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002170 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002171 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002172 * (if not supported by FIFO, it will fallback to default)
2173 * - set both rx/tx packet buffers to full memory size
2174 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002175 */
2176static void macb_configure_dma(struct macb *bp)
2177{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002178 struct macb_queue *queue;
2179 u32 buffer_size;
2180 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002181 u32 dmacfg;
2182
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002183 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002184 if (macb_is_gem(bp)) {
2185 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002186 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2187 if (q)
2188 queue_writel(queue, RBQS, buffer_size);
2189 else
2190 dmacfg |= GEM_BF(RXBS, buffer_size);
2191 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002192 if (bp->dma_burst_length)
2193 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002194 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302195 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302196
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002197 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302198 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2199 else
2200 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2201
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002202 if (bp->dev->features & NETIF_F_HW_CSUM)
2203 dmacfg |= GEM_BIT(TXCOEN);
2204 else
2205 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302206
Michal Simekbd620722018-09-25 08:32:50 +02002207 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302208#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002209 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002210 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302211#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002212#ifdef CONFIG_MACB_USE_HWSTAMP
2213 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2214 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2215#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002216 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2217 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002218 gem_writel(bp, DMACFG, dmacfg);
2219 }
2220}
2221
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002222static void macb_init_hw(struct macb *bp)
2223{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002224 struct macb_queue *queue;
2225 unsigned int q;
2226
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002227 u32 config;
2228
2229 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002230 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002231
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002232 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302233 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2234 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002235 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236 config |= MACB_BIT(PAE); /* PAuse Enable */
2237 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002238 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302239 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2240 else
2241 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002242 if (bp->dev->flags & IFF_PROMISC)
2243 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002244 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2245 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002246 if (!(bp->dev->flags & IFF_BROADCAST))
2247 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002248 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002249 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002250 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302251 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002252 bp->speed = SPEED_10;
2253 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302254 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002255 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302256 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002257
Jamie Iles0116da42011-03-14 17:38:30 +00002258 macb_configure_dma(bp);
2259
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002260 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002261 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002262 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2263#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2264 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2265 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2266#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002267 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302268#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002269 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002270 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302271#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002272
2273 /* Enable interrupts */
2274 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05302275 bp->rx_intr_mask |
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002276 MACB_TX_INT_FLAGS |
2277 MACB_BIT(HRESP));
2278 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002279
2280 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002281 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002282}
2283
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002284/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002285 * locations in the memory map. The least significant bits are stored
2286 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2287 *
2288 * The unicast hash enable and the multicast hash enable bits in the
2289 * network configuration register enable the reception of hash matched
2290 * frames. The destination address is reduced to a 6 bit index into
2291 * the 64 bit hash register using the following hash function. The
2292 * hash function is an exclusive or of every sixth bit of the
2293 * destination address.
2294 *
2295 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2296 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2297 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2298 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2299 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2300 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2301 *
2302 * da[0] represents the least significant bit of the first byte
2303 * received, that is, the multicast/unicast indicator, and da[47]
2304 * represents the most significant bit of the last byte received. If
2305 * the hash index, hi[n], points to a bit that is set in the hash
2306 * register then the frame will be matched according to whether the
2307 * frame is multicast or unicast. A multicast match will be signalled
2308 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2309 * index points to a bit set in the hash register. A unicast match
2310 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2311 * and the hash index points to a bit set in the hash register. To
2312 * receive all multicast frames, the hash register should be set with
2313 * all ones and the multicast hash enable bit should be set in the
2314 * network configuration register.
2315 */
2316
2317static inline int hash_bit_value(int bitnr, __u8 *addr)
2318{
2319 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2320 return 1;
2321 return 0;
2322}
2323
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002324/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002325static int hash_get_index(__u8 *addr)
2326{
2327 int i, j, bitval;
2328 int hash_index = 0;
2329
2330 for (j = 0; j < 6; j++) {
2331 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002332 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002333
2334 hash_index |= (bitval << j);
2335 }
2336
2337 return hash_index;
2338}
2339
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002340/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002341static void macb_sethashtable(struct net_device *dev)
2342{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002343 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002344 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002345 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002346 struct macb *bp = netdev_priv(dev);
2347
Moritz Fischeraa50b552016-03-29 19:11:13 -07002348 mc_filter[0] = 0;
2349 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002350
Jiri Pirko22bedad32010-04-01 21:22:57 +00002351 netdev_for_each_mc_addr(ha, dev) {
2352 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002353 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2354 }
2355
Jamie Ilesf75ba502011-11-08 10:12:32 +00002356 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2357 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002358}
2359
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002360/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002361static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002362{
2363 unsigned long cfg;
2364 struct macb *bp = netdev_priv(dev);
2365
2366 cfg = macb_readl(bp, NCFGR);
2367
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002368 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002369 /* Enable promiscuous mode */
2370 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002371
2372 /* Disable RX checksum offload */
2373 if (macb_is_gem(bp))
2374 cfg &= ~GEM_BIT(RXCOEN);
2375 } else {
2376 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002377 cfg &= ~MACB_BIT(CAF);
2378
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002379 /* Enable RX checksum offload only if requested */
2380 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2381 cfg |= GEM_BIT(RXCOEN);
2382 }
2383
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002384 if (dev->flags & IFF_ALLMULTI) {
2385 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002386 macb_or_gem_writel(bp, HRB, -1);
2387 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002388 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002389 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002390 /* Enable specific multicasts */
2391 macb_sethashtable(dev);
2392 cfg |= MACB_BIT(NCFGR_MTI);
2393 } else if (dev->flags & (~IFF_ALLMULTI)) {
2394 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002395 macb_or_gem_writel(bp, HRB, 0);
2396 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002397 cfg &= ~MACB_BIT(NCFGR_MTI);
2398 }
2399
2400 macb_writel(bp, NCFGR, cfg);
2401}
2402
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002403static int macb_open(struct net_device *dev)
2404{
2405 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002406 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002407 struct macb_queue *queue;
2408 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002409 int err;
2410
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002411 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002412
Harini Katakamd54f89a2019-03-01 16:20:34 +05302413 err = pm_runtime_get_sync(&bp->pdev->dev);
2414 if (err < 0)
2415 goto pm_exit;
2416
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002417 /* carrier starts down */
2418 netif_carrier_off(dev);
2419
frederic RODO6c36a702007-07-12 19:07:24 +02002420 /* if the phy is not yet register, retry later*/
Harini Katakamd54f89a2019-03-01 16:20:34 +05302421 if (!dev->phydev) {
2422 err = -EAGAIN;
2423 goto pm_exit;
2424 }
frederic RODO6c36a702007-07-12 19:07:24 +02002425
Nicolas Ferre1b447912013-06-04 21:57:11 +00002426 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002427 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002428
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002429 err = macb_alloc_consistent(bp);
2430 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002431 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2432 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302433 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002434 }
2435
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002436 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2437 napi_enable(&queue->napi);
2438
Harini Katakam05044532019-05-07 19:59:10 +05302439 bp->macbgem_ops.mog_init_rings(bp);
2440 macb_init_hw(bp);
2441
frederic RODO6c36a702007-07-12 19:07:24 +02002442 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002443 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002444
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002445 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002446
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002447 if (bp->ptp_info)
2448 bp->ptp_info->ptp_init(dev);
2449
Harini Katakamd54f89a2019-03-01 16:20:34 +05302450pm_exit:
2451 if (err) {
2452 pm_runtime_put_sync(&bp->pdev->dev);
2453 return err;
2454 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002455 return 0;
2456}
2457
2458static int macb_close(struct net_device *dev)
2459{
2460 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002461 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002462 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002463 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002464
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002465 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002466
2467 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2468 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002469
Philippe Reynes0a912812016-06-22 00:32:35 +02002470 if (dev->phydev)
2471 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002472
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002473 spin_lock_irqsave(&bp->lock, flags);
2474 macb_reset_hw(bp);
2475 netif_carrier_off(dev);
2476 spin_unlock_irqrestore(&bp->lock, flags);
2477
2478 macb_free_consistent(bp);
2479
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002480 if (bp->ptp_info)
2481 bp->ptp_info->ptp_remove(dev);
2482
Harini Katakamd54f89a2019-03-01 16:20:34 +05302483 pm_runtime_put(&bp->pdev->dev);
2484
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002485 return 0;
2486}
2487
Harini Katakama5898ea2015-05-06 22:27:18 +05302488static int macb_change_mtu(struct net_device *dev, int new_mtu)
2489{
Harini Katakama5898ea2015-05-06 22:27:18 +05302490 if (netif_running(dev))
2491 return -EBUSY;
2492
Harini Katakama5898ea2015-05-06 22:27:18 +05302493 dev->mtu = new_mtu;
2494
2495 return 0;
2496}
2497
Jamie Ilesa494ed82011-03-09 16:26:35 +00002498static void gem_update_stats(struct macb *bp)
2499{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002500 struct macb_queue *queue;
2501 unsigned int i, q, idx;
2502 unsigned long *stat;
2503
Jamie Ilesa494ed82011-03-09 16:26:35 +00002504 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002505
Xander Huff3ff13f12015-01-13 16:15:51 -06002506 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2507 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002508 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002509
2510 bp->ethtool_stats[i] += val;
2511 *p += val;
2512
2513 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2514 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002515 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002516 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002517 *(++p) += val;
2518 }
2519 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002520
2521 idx = GEM_STATS_LEN;
2522 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2523 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2524 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002525}
2526
2527static struct net_device_stats *gem_get_stats(struct macb *bp)
2528{
2529 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002530 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002531
2532 gem_update_stats(bp);
2533
2534 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2535 hwstat->rx_alignment_errors +
2536 hwstat->rx_resource_errors +
2537 hwstat->rx_overruns +
2538 hwstat->rx_oversize_frames +
2539 hwstat->rx_jabbers +
2540 hwstat->rx_undersized_frames +
2541 hwstat->rx_length_field_frame_errors);
2542 nstat->tx_errors = (hwstat->tx_late_collisions +
2543 hwstat->tx_excessive_collisions +
2544 hwstat->tx_underrun +
2545 hwstat->tx_carrier_sense_errors);
2546 nstat->multicast = hwstat->rx_multicast_frames;
2547 nstat->collisions = (hwstat->tx_single_collision_frames +
2548 hwstat->tx_multiple_collision_frames +
2549 hwstat->tx_excessive_collisions);
2550 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2551 hwstat->rx_jabbers +
2552 hwstat->rx_undersized_frames +
2553 hwstat->rx_length_field_frame_errors);
2554 nstat->rx_over_errors = hwstat->rx_resource_errors;
2555 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2556 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2557 nstat->rx_fifo_errors = hwstat->rx_overruns;
2558 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2559 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2560 nstat->tx_fifo_errors = hwstat->tx_underrun;
2561
2562 return nstat;
2563}
2564
Xander Huff3ff13f12015-01-13 16:15:51 -06002565static void gem_get_ethtool_stats(struct net_device *dev,
2566 struct ethtool_stats *stats, u64 *data)
2567{
2568 struct macb *bp;
2569
2570 bp = netdev_priv(dev);
2571 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002572 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2573 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002574}
2575
2576static int gem_get_sset_count(struct net_device *dev, int sset)
2577{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002578 struct macb *bp = netdev_priv(dev);
2579
Xander Huff3ff13f12015-01-13 16:15:51 -06002580 switch (sset) {
2581 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002582 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002583 default:
2584 return -EOPNOTSUPP;
2585 }
2586}
2587
2588static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2589{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002590 char stat_string[ETH_GSTRING_LEN];
2591 struct macb *bp = netdev_priv(dev);
2592 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002593 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002594 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002595
2596 switch (sset) {
2597 case ETH_SS_STATS:
2598 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2599 memcpy(p, gem_statistics[i].stat_string,
2600 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002601
2602 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2603 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2604 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2605 q, queue_statistics[i].stat_string);
2606 memcpy(p, stat_string, ETH_GSTRING_LEN);
2607 }
2608 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002609 break;
2610 }
2611}
2612
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002613static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002614{
2615 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002616 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002617 struct macb_stats *hwstat = &bp->hw_stats.macb;
2618
2619 if (macb_is_gem(bp))
2620 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002621
frederic RODO6c36a702007-07-12 19:07:24 +02002622 /* read stats from hardware */
2623 macb_update_stats(bp);
2624
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002625 /* Convert HW stats into netdevice stats */
2626 nstat->rx_errors = (hwstat->rx_fcs_errors +
2627 hwstat->rx_align_errors +
2628 hwstat->rx_resource_errors +
2629 hwstat->rx_overruns +
2630 hwstat->rx_oversize_pkts +
2631 hwstat->rx_jabbers +
2632 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002633 hwstat->rx_length_mismatch);
2634 nstat->tx_errors = (hwstat->tx_late_cols +
2635 hwstat->tx_excessive_cols +
2636 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002637 hwstat->tx_carrier_errors +
2638 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002639 nstat->collisions = (hwstat->tx_single_cols +
2640 hwstat->tx_multiple_cols +
2641 hwstat->tx_excessive_cols);
2642 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2643 hwstat->rx_jabbers +
2644 hwstat->rx_undersize_pkts +
2645 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002646 nstat->rx_over_errors = hwstat->rx_resource_errors +
2647 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002648 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2649 nstat->rx_frame_errors = hwstat->rx_align_errors;
2650 nstat->rx_fifo_errors = hwstat->rx_overruns;
2651 /* XXX: What does "missed" mean? */
2652 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2653 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2654 nstat->tx_fifo_errors = hwstat->tx_underruns;
2655 /* Don't know about heartbeat or window errors... */
2656
2657 return nstat;
2658}
2659
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002660static int macb_get_regs_len(struct net_device *netdev)
2661{
2662 return MACB_GREGS_NBR * sizeof(u32);
2663}
2664
2665static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2666 void *p)
2667{
2668 struct macb *bp = netdev_priv(dev);
2669 unsigned int tail, head;
2670 u32 *regs_buff = p;
2671
2672 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2673 | MACB_GREGS_VERSION;
2674
Zach Brownb410d132016-10-19 09:56:57 -05002675 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2676 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002677
2678 regs_buff[0] = macb_readl(bp, NCR);
2679 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2680 regs_buff[2] = macb_readl(bp, NSR);
2681 regs_buff[3] = macb_readl(bp, TSR);
2682 regs_buff[4] = macb_readl(bp, RBQP);
2683 regs_buff[5] = macb_readl(bp, TBQP);
2684 regs_buff[6] = macb_readl(bp, RSR);
2685 regs_buff[7] = macb_readl(bp, IMR);
2686
2687 regs_buff[8] = tail;
2688 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002689 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2690 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002691
Neil Armstrongce721a72016-01-05 14:39:16 +01002692 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2693 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002694 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002695 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002696}
2697
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002698static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2699{
2700 struct macb *bp = netdev_priv(netdev);
2701
2702 wol->supported = 0;
2703 wol->wolopts = 0;
2704
2705 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2706 wol->supported = WAKE_MAGIC;
2707
2708 if (bp->wol & MACB_WOL_ENABLED)
2709 wol->wolopts |= WAKE_MAGIC;
2710 }
2711}
2712
2713static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2714{
2715 struct macb *bp = netdev_priv(netdev);
2716
2717 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2718 (wol->wolopts & ~WAKE_MAGIC))
2719 return -EOPNOTSUPP;
2720
2721 if (wol->wolopts & WAKE_MAGIC)
2722 bp->wol |= MACB_WOL_ENABLED;
2723 else
2724 bp->wol &= ~MACB_WOL_ENABLED;
2725
2726 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2727
2728 return 0;
2729}
2730
Zach Brown8441bb32016-10-19 09:56:58 -05002731static void macb_get_ringparam(struct net_device *netdev,
2732 struct ethtool_ringparam *ring)
2733{
2734 struct macb *bp = netdev_priv(netdev);
2735
2736 ring->rx_max_pending = MAX_RX_RING_SIZE;
2737 ring->tx_max_pending = MAX_TX_RING_SIZE;
2738
2739 ring->rx_pending = bp->rx_ring_size;
2740 ring->tx_pending = bp->tx_ring_size;
2741}
2742
2743static int macb_set_ringparam(struct net_device *netdev,
2744 struct ethtool_ringparam *ring)
2745{
2746 struct macb *bp = netdev_priv(netdev);
2747 u32 new_rx_size, new_tx_size;
2748 unsigned int reset = 0;
2749
2750 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2751 return -EINVAL;
2752
2753 new_rx_size = clamp_t(u32, ring->rx_pending,
2754 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2755 new_rx_size = roundup_pow_of_two(new_rx_size);
2756
2757 new_tx_size = clamp_t(u32, ring->tx_pending,
2758 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2759 new_tx_size = roundup_pow_of_two(new_tx_size);
2760
2761 if ((new_tx_size == bp->tx_ring_size) &&
2762 (new_rx_size == bp->rx_ring_size)) {
2763 /* nothing to do */
2764 return 0;
2765 }
2766
2767 if (netif_running(bp->dev)) {
2768 reset = 1;
2769 macb_close(bp->dev);
2770 }
2771
2772 bp->rx_ring_size = new_rx_size;
2773 bp->tx_ring_size = new_tx_size;
2774
2775 if (reset)
2776 macb_open(bp->dev);
2777
2778 return 0;
2779}
2780
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002781#ifdef CONFIG_MACB_USE_HWSTAMP
2782static unsigned int gem_get_tsu_rate(struct macb *bp)
2783{
2784 struct clk *tsu_clk;
2785 unsigned int tsu_rate;
2786
2787 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2788 if (!IS_ERR(tsu_clk))
2789 tsu_rate = clk_get_rate(tsu_clk);
2790 /* try pclk instead */
2791 else if (!IS_ERR(bp->pclk)) {
2792 tsu_clk = bp->pclk;
2793 tsu_rate = clk_get_rate(tsu_clk);
2794 } else
2795 return -ENOTSUPP;
2796 return tsu_rate;
2797}
2798
2799static s32 gem_get_ptp_max_adj(void)
2800{
2801 return 64000000;
2802}
2803
2804static int gem_get_ts_info(struct net_device *dev,
2805 struct ethtool_ts_info *info)
2806{
2807 struct macb *bp = netdev_priv(dev);
2808
2809 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2810 ethtool_op_get_ts_info(dev, info);
2811 return 0;
2812 }
2813
2814 info->so_timestamping =
2815 SOF_TIMESTAMPING_TX_SOFTWARE |
2816 SOF_TIMESTAMPING_RX_SOFTWARE |
2817 SOF_TIMESTAMPING_SOFTWARE |
2818 SOF_TIMESTAMPING_TX_HARDWARE |
2819 SOF_TIMESTAMPING_RX_HARDWARE |
2820 SOF_TIMESTAMPING_RAW_HARDWARE;
2821 info->tx_types =
2822 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2823 (1 << HWTSTAMP_TX_OFF) |
2824 (1 << HWTSTAMP_TX_ON);
2825 info->rx_filters =
2826 (1 << HWTSTAMP_FILTER_NONE) |
2827 (1 << HWTSTAMP_FILTER_ALL);
2828
2829 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2830
2831 return 0;
2832}
2833
2834static struct macb_ptp_info gem_ptp_info = {
2835 .ptp_init = gem_ptp_init,
2836 .ptp_remove = gem_ptp_remove,
2837 .get_ptp_max_adj = gem_get_ptp_max_adj,
2838 .get_tsu_rate = gem_get_tsu_rate,
2839 .get_ts_info = gem_get_ts_info,
2840 .get_hwtst = gem_get_hwtst,
2841 .set_hwtst = gem_set_hwtst,
2842};
2843#endif
2844
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002845static int macb_get_ts_info(struct net_device *netdev,
2846 struct ethtool_ts_info *info)
2847{
2848 struct macb *bp = netdev_priv(netdev);
2849
2850 if (bp->ptp_info)
2851 return bp->ptp_info->get_ts_info(netdev, info);
2852
2853 return ethtool_op_get_ts_info(netdev, info);
2854}
2855
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002856static void gem_enable_flow_filters(struct macb *bp, bool enable)
2857{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002858 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002859 struct ethtool_rx_fs_item *item;
2860 u32 t2_scr;
2861 int num_t2_scr;
2862
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002863 if (!(netdev->features & NETIF_F_NTUPLE))
2864 return;
2865
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002866 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2867
2868 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2869 struct ethtool_rx_flow_spec *fs = &item->fs;
2870 struct ethtool_tcpip4_spec *tp4sp_m;
2871
2872 if (fs->location >= num_t2_scr)
2873 continue;
2874
2875 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2876
2877 /* enable/disable screener regs for the flow entry */
2878 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2879
2880 /* only enable fields with no masking */
2881 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2882
2883 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2884 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2885 else
2886 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2887
2888 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2889 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2890 else
2891 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2892
2893 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2894 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2895 else
2896 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2897
2898 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2899 }
2900}
2901
2902static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2903{
2904 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2905 uint16_t index = fs->location;
2906 u32 w0, w1, t2_scr;
2907 bool cmp_a = false;
2908 bool cmp_b = false;
2909 bool cmp_c = false;
2910
2911 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2912 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2913
2914 /* ignore field if any masking set */
2915 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2916 /* 1st compare reg - IP source address */
2917 w0 = 0;
2918 w1 = 0;
2919 w0 = tp4sp_v->ip4src;
2920 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2921 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2922 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2923 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2924 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2925 cmp_a = true;
2926 }
2927
2928 /* ignore field if any masking set */
2929 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2930 /* 2nd compare reg - IP destination address */
2931 w0 = 0;
2932 w1 = 0;
2933 w0 = tp4sp_v->ip4dst;
2934 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2935 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2936 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2937 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2938 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2939 cmp_b = true;
2940 }
2941
2942 /* ignore both port fields if masking set in both */
2943 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2944 /* 3rd compare reg - source port, destination port */
2945 w0 = 0;
2946 w1 = 0;
2947 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2948 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2949 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2950 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2951 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2952 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2953 } else {
2954 /* only one port definition */
2955 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2956 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2957 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2958 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2959 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2960 } else { /* dst port */
2961 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2962 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2963 }
2964 }
2965 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2966 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2967 cmp_c = true;
2968 }
2969
2970 t2_scr = 0;
2971 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2972 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2973 if (cmp_a)
2974 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2975 if (cmp_b)
2976 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2977 if (cmp_c)
2978 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2979 gem_writel_n(bp, SCRT2, index, t2_scr);
2980}
2981
2982static int gem_add_flow_filter(struct net_device *netdev,
2983 struct ethtool_rxnfc *cmd)
2984{
2985 struct macb *bp = netdev_priv(netdev);
2986 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2987 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002988 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002989 int ret = -EINVAL;
2990 bool added = false;
2991
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002992 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002993 if (newfs == NULL)
2994 return -ENOMEM;
2995 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2996
2997 netdev_dbg(netdev,
2998 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2999 fs->flow_type, (int)fs->ring_cookie, fs->location,
3000 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3001 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3002 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3003
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003004 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3005
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003006 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003007 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3008 if (item->fs.location > newfs->fs.location) {
3009 list_add_tail(&newfs->list, &item->list);
3010 added = true;
3011 break;
3012 } else if (item->fs.location == fs->location) {
3013 netdev_err(netdev, "Rule not added: location %d not free!\n",
3014 fs->location);
3015 ret = -EBUSY;
3016 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003017 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003018 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003019 if (!added)
3020 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003021
3022 gem_prog_cmp_regs(bp, fs);
3023 bp->rx_fs_list.count++;
3024 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003025 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003026
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003027 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003028 return 0;
3029
3030err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003031 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003032 kfree(newfs);
3033 return ret;
3034}
3035
3036static int gem_del_flow_filter(struct net_device *netdev,
3037 struct ethtool_rxnfc *cmd)
3038{
3039 struct macb *bp = netdev_priv(netdev);
3040 struct ethtool_rx_fs_item *item;
3041 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003042 unsigned long flags;
3043
3044 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003045
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003046 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3047 if (item->fs.location == cmd->fs.location) {
3048 /* disable screener regs for the flow entry */
3049 fs = &(item->fs);
3050 netdev_dbg(netdev,
3051 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3052 fs->flow_type, (int)fs->ring_cookie, fs->location,
3053 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3054 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3055 htons(fs->h_u.tcp_ip4_spec.psrc),
3056 htons(fs->h_u.tcp_ip4_spec.pdst));
3057
3058 gem_writel_n(bp, SCRT2, fs->location, 0);
3059
3060 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003061 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003062 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3063 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003064 return 0;
3065 }
3066 }
3067
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003068 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003069 return -EINVAL;
3070}
3071
3072static int gem_get_flow_entry(struct net_device *netdev,
3073 struct ethtool_rxnfc *cmd)
3074{
3075 struct macb *bp = netdev_priv(netdev);
3076 struct ethtool_rx_fs_item *item;
3077
3078 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3079 if (item->fs.location == cmd->fs.location) {
3080 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3081 return 0;
3082 }
3083 }
3084 return -EINVAL;
3085}
3086
3087static int gem_get_all_flow_entries(struct net_device *netdev,
3088 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3089{
3090 struct macb *bp = netdev_priv(netdev);
3091 struct ethtool_rx_fs_item *item;
3092 uint32_t cnt = 0;
3093
3094 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3095 if (cnt == cmd->rule_cnt)
3096 return -EMSGSIZE;
3097 rule_locs[cnt] = item->fs.location;
3098 cnt++;
3099 }
3100 cmd->data = bp->max_tuples;
3101 cmd->rule_cnt = cnt;
3102
3103 return 0;
3104}
3105
3106static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3107 u32 *rule_locs)
3108{
3109 struct macb *bp = netdev_priv(netdev);
3110 int ret = 0;
3111
3112 switch (cmd->cmd) {
3113 case ETHTOOL_GRXRINGS:
3114 cmd->data = bp->num_queues;
3115 break;
3116 case ETHTOOL_GRXCLSRLCNT:
3117 cmd->rule_cnt = bp->rx_fs_list.count;
3118 break;
3119 case ETHTOOL_GRXCLSRULE:
3120 ret = gem_get_flow_entry(netdev, cmd);
3121 break;
3122 case ETHTOOL_GRXCLSRLALL:
3123 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3124 break;
3125 default:
3126 netdev_err(netdev,
3127 "Command parameter %d is not supported\n", cmd->cmd);
3128 ret = -EOPNOTSUPP;
3129 }
3130
3131 return ret;
3132}
3133
3134static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3135{
3136 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003137 int ret;
3138
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003139 switch (cmd->cmd) {
3140 case ETHTOOL_SRXCLSRLINS:
3141 if ((cmd->fs.location >= bp->max_tuples)
3142 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3143 ret = -EINVAL;
3144 break;
3145 }
3146 ret = gem_add_flow_filter(netdev, cmd);
3147 break;
3148 case ETHTOOL_SRXCLSRLDEL:
3149 ret = gem_del_flow_filter(netdev, cmd);
3150 break;
3151 default:
3152 netdev_err(netdev,
3153 "Command parameter %d is not supported\n", cmd->cmd);
3154 ret = -EOPNOTSUPP;
3155 }
3156
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003157 return ret;
3158}
3159
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003160static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003161 .get_regs_len = macb_get_regs_len,
3162 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003163 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003164 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003165 .get_wol = macb_get_wol,
3166 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003167 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3168 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003169 .get_ringparam = macb_get_ringparam,
3170 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003171};
Xander Huff8cd5a562015-01-15 15:55:20 -06003172
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003173static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003174 .get_regs_len = macb_get_regs_len,
3175 .get_regs = macb_get_regs,
3176 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003177 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003178 .get_ethtool_stats = gem_get_ethtool_stats,
3179 .get_strings = gem_get_ethtool_strings,
3180 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003181 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3182 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003183 .get_ringparam = macb_get_ringparam,
3184 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003185 .get_rxnfc = gem_get_rxnfc,
3186 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003187};
3188
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003189static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003190{
Philippe Reynes0a912812016-06-22 00:32:35 +02003191 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003192 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003193
3194 if (!netif_running(dev))
3195 return -EINVAL;
3196
frederic RODO6c36a702007-07-12 19:07:24 +02003197 if (!phydev)
3198 return -ENODEV;
3199
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003200 if (!bp->ptp_info)
3201 return phy_mii_ioctl(phydev, rq, cmd);
3202
3203 switch (cmd) {
3204 case SIOCSHWTSTAMP:
3205 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3206 case SIOCGHWTSTAMP:
3207 return bp->ptp_info->get_hwtst(dev, rq);
3208 default:
3209 return phy_mii_ioctl(phydev, rq, cmd);
3210 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003211}
3212
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003213static inline void macb_set_txcsum_feature(struct macb *bp,
3214 netdev_features_t features)
3215{
3216 u32 val;
3217
3218 if (!macb_is_gem(bp))
3219 return;
3220
3221 val = gem_readl(bp, DMACFG);
3222 if (features & NETIF_F_HW_CSUM)
3223 val |= GEM_BIT(TXCOEN);
3224 else
3225 val &= ~GEM_BIT(TXCOEN);
3226
3227 gem_writel(bp, DMACFG, val);
3228}
3229
3230static inline void macb_set_rxcsum_feature(struct macb *bp,
3231 netdev_features_t features)
3232{
3233 struct net_device *netdev = bp->dev;
3234 u32 val;
3235
3236 if (!macb_is_gem(bp))
3237 return;
3238
3239 val = gem_readl(bp, NCFGR);
3240 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3241 val |= GEM_BIT(RXCOEN);
3242 else
3243 val &= ~GEM_BIT(RXCOEN);
3244
3245 gem_writel(bp, NCFGR, val);
3246}
3247
3248static inline void macb_set_rxflow_feature(struct macb *bp,
3249 netdev_features_t features)
3250{
3251 if (!macb_is_gem(bp))
3252 return;
3253
3254 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3255}
3256
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003257static int macb_set_features(struct net_device *netdev,
3258 netdev_features_t features)
3259{
3260 struct macb *bp = netdev_priv(netdev);
3261 netdev_features_t changed = features ^ netdev->features;
3262
3263 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003264 if (changed & NETIF_F_HW_CSUM)
3265 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003266
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003267 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003268 if (changed & NETIF_F_RXCSUM)
3269 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003270
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003271 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003272 if (changed & NETIF_F_NTUPLE)
3273 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003274
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003275 return 0;
3276}
3277
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003278static void macb_restore_features(struct macb *bp)
3279{
3280 struct net_device *netdev = bp->dev;
3281 netdev_features_t features = netdev->features;
3282
3283 /* TX checksum offload */
3284 macb_set_txcsum_feature(bp, features);
3285
3286 /* RX checksum offload */
3287 macb_set_rxcsum_feature(bp, features);
3288
3289 /* RX Flow Filters */
3290 macb_set_rxflow_feature(bp, features);
3291}
3292
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003293static const struct net_device_ops macb_netdev_ops = {
3294 .ndo_open = macb_open,
3295 .ndo_stop = macb_close,
3296 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003297 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003298 .ndo_get_stats = macb_get_stats,
3299 .ndo_do_ioctl = macb_ioctl,
3300 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303301 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003302 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003303#ifdef CONFIG_NET_POLL_CONTROLLER
3304 .ndo_poll_controller = macb_poll_controller,
3305#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003306 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003307 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003308};
3309
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003310/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003311 * and integration options used
3312 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003313static void macb_configure_caps(struct macb *bp,
3314 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003315{
3316 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003317
Nicolas Ferref6970502015-03-31 15:02:01 +02003318 if (dt_conf)
3319 bp->caps = dt_conf->caps;
3320
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003321 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003322 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3323
Nicolas Ferree1755872014-07-24 13:50:58 +02003324 dcfg = gem_readl(bp, DCFG1);
3325 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3326 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3327 dcfg = gem_readl(bp, DCFG2);
3328 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3329 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003330#ifdef CONFIG_MACB_USE_HWSTAMP
3331 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003332 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3333 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003334 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003335 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003336 bp->ptp_info = &gem_ptp_info;
3337 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003338 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003339#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003340 }
3341
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003342 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003343}
3344
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003345static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003346 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003347 unsigned int *queue_mask,
3348 unsigned int *num_queues)
3349{
3350 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003351
3352 *queue_mask = 0x1;
3353 *num_queues = 1;
3354
Nicolas Ferreda120112015-03-31 15:02:00 +02003355 /* is it macb or gem ?
3356 *
3357 * We need to read directly from the hardware here because
3358 * we are early in the probe process and don't have the
3359 * MACB_CAPS_MACB_IS_GEM flag positioned
3360 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003361 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003362 return;
3363
3364 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303365 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3366
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003367 *queue_mask |= 0x1;
3368
3369 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3370 if (*queue_mask & (1 << hw_q))
3371 (*num_queues)++;
3372}
3373
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003374static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303375 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303376 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003377{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003378 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003379 int err;
3380
Bartosz Folta83a77e92016-12-14 06:39:15 +00003381 pdata = dev_get_platdata(&pdev->dev);
3382 if (pdata) {
3383 *pclk = pdata->pclk;
3384 *hclk = pdata->hclk;
3385 } else {
3386 *pclk = devm_clk_get(&pdev->dev, "pclk");
3387 *hclk = devm_clk_get(&pdev->dev, "hclk");
3388 }
3389
Harini Katakamcd5afa92019-03-20 19:12:22 +05303390 if (IS_ERR_OR_NULL(*pclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003391 err = PTR_ERR(*pclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303392 if (!err)
3393 err = -ENODEV;
3394
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003395 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003396 return err;
3397 }
3398
Harini Katakamcd5afa92019-03-20 19:12:22 +05303399 if (IS_ERR_OR_NULL(*hclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003400 err = PTR_ERR(*hclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303401 if (!err)
3402 err = -ENODEV;
3403
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003404 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003405 return err;
3406 }
3407
3408 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3409 if (IS_ERR(*tx_clk))
3410 *tx_clk = NULL;
3411
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303412 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3413 if (IS_ERR(*rx_clk))
3414 *rx_clk = NULL;
3415
Harini Katakamf5473d12019-03-01 16:20:33 +05303416 *tsu_clk = devm_clk_get(&pdev->dev, "tsu_clk");
3417 if (IS_ERR(*tsu_clk))
3418 *tsu_clk = NULL;
3419
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003420 err = clk_prepare_enable(*pclk);
3421 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003422 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003423 return err;
3424 }
3425
3426 err = clk_prepare_enable(*hclk);
3427 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003428 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003429 goto err_disable_pclk;
3430 }
3431
3432 err = clk_prepare_enable(*tx_clk);
3433 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003434 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003435 goto err_disable_hclk;
3436 }
3437
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303438 err = clk_prepare_enable(*rx_clk);
3439 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003440 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303441 goto err_disable_txclk;
3442 }
3443
Harini Katakamf5473d12019-03-01 16:20:33 +05303444 err = clk_prepare_enable(*tsu_clk);
3445 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003446 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303447 goto err_disable_rxclk;
3448 }
3449
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003450 return 0;
3451
Harini Katakamf5473d12019-03-01 16:20:33 +05303452err_disable_rxclk:
3453 clk_disable_unprepare(*rx_clk);
3454
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303455err_disable_txclk:
3456 clk_disable_unprepare(*tx_clk);
3457
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003458err_disable_hclk:
3459 clk_disable_unprepare(*hclk);
3460
3461err_disable_pclk:
3462 clk_disable_unprepare(*pclk);
3463
3464 return err;
3465}
3466
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003467static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003468{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003469 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003470 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003471 struct macb *bp = netdev_priv(dev);
3472 struct macb_queue *queue;
3473 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003474 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003475
Zach Brownb410d132016-10-19 09:56:57 -05003476 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3477 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3478
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003479 /* set the queue register mapping once for all: queue0 has a special
3480 * register mapping but we don't want to test the queue index then
3481 * compute the corresponding register offset at run time.
3482 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003483 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003484 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003485 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003486
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003487 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003488 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003489 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003490 if (hw_q) {
3491 queue->ISR = GEM_ISR(hw_q - 1);
3492 queue->IER = GEM_IER(hw_q - 1);
3493 queue->IDR = GEM_IDR(hw_q - 1);
3494 queue->IMR = GEM_IMR(hw_q - 1);
3495 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003496 queue->RBQP = GEM_RBQP(hw_q - 1);
3497 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303498#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003499 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003500 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003501 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3502 }
Harini Katakamfff80192016-08-09 13:15:53 +05303503#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003504 } else {
3505 /* queue0 uses legacy registers */
3506 queue->ISR = MACB_ISR;
3507 queue->IER = MACB_IER;
3508 queue->IDR = MACB_IDR;
3509 queue->IMR = MACB_IMR;
3510 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003511 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303512#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003513 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003514 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003515 queue->RBQPH = MACB_RBQPH;
3516 }
Harini Katakamfff80192016-08-09 13:15:53 +05303517#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003518 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003519
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003520 /* get irq: here we use the linux queue index, not the hardware
3521 * queue index. the queue irq definitions in the device tree
3522 * must remove the optional gaps that could exist in the
3523 * hardware queue mask.
3524 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003525 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003526 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003527 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003528 if (err) {
3529 dev_err(&pdev->dev,
3530 "Unable to request IRQ %d (error %d)\n",
3531 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003532 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003533 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003534
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003535 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003536 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003537 }
3538
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003539 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003540
Nicolas Ferre4df95132013-06-04 21:57:12 +00003541 /* setup appropriated routines according to adapter type */
3542 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003543 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003544 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3545 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3546 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3547 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003548 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003549 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003550 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003551 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3552 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3553 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3554 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003555 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003556 }
3557
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003558 /* Set features */
3559 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003560
3561 /* Check LSO capability */
3562 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3563 dev->hw_features |= MACB_NETIF_LSO;
3564
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003565 /* Checksum offload is only available on gem with packet buffer */
3566 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003567 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003568 if (bp->caps & MACB_CAPS_SG_DISABLED)
3569 dev->hw_features &= ~NETIF_F_SG;
3570 dev->features = dev->hw_features;
3571
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003572 /* Check RX Flow Filters support.
3573 * Max Rx flows set by availability of screeners & compare regs:
3574 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3575 */
3576 reg = gem_readl(bp, DCFG8);
3577 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3578 GEM_BFEXT(T2SCR, reg));
3579 if (bp->max_tuples > 0) {
3580 /* also needs one ethtype match to check IPv4 */
3581 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3582 /* program this reg now */
3583 reg = 0;
3584 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3585 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3586 /* Filtering is supported in hw but don't enable it in kernel now */
3587 dev->hw_features |= NETIF_F_NTUPLE;
3588 /* init Rx flow definitions */
3589 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3590 bp->rx_fs_list.count = 0;
3591 spin_lock_init(&bp->rx_fs_lock);
3592 } else
3593 bp->max_tuples = 0;
3594 }
3595
Neil Armstrongce721a72016-01-05 14:39:16 +01003596 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3597 val = 0;
3598 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3599 val = GEM_BIT(RGMII);
3600 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003601 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003602 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003603 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003604 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003605
Neil Armstrongce721a72016-01-05 14:39:16 +01003606 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3607 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003608
Neil Armstrongce721a72016-01-05 14:39:16 +01003609 macb_or_gem_writel(bp, USRIO, val);
3610 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003611
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003612 /* Set MII management clock divider */
3613 val = macb_mdc_clk_div(bp);
3614 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303615 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3616 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003617 macb_writel(bp, NCFGR, val);
3618
3619 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003620}
3621
3622#if defined(CONFIG_OF)
3623/* 1518 rounded up */
3624#define AT91ETHER_MAX_RBUFF_SZ 0x600
3625/* max number of receive buffers */
3626#define AT91ETHER_MAX_RX_DESCR 9
3627
Arnd Bergmann49db9222019-07-08 14:48:23 +02003628static struct sifive_fu540_macb_mgmt *mgmt;
3629
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003630/* Initialize and start the Receiver and Transmit subsystems */
3631static int at91ether_start(struct net_device *dev)
3632{
3633 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003634 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003635 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003636 dma_addr_t addr;
3637 u32 ctl;
3638 int i;
3639
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003640 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003641 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003642 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003643 &q->rx_ring_dma, GFP_KERNEL);
3644 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003645 return -ENOMEM;
3646
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003647 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003648 AT91ETHER_MAX_RX_DESCR *
3649 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003650 &q->rx_buffers_dma, GFP_KERNEL);
3651 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003652 dma_free_coherent(&lp->pdev->dev,
3653 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003654 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003655 q->rx_ring, q->rx_ring_dma);
3656 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003657 return -ENOMEM;
3658 }
3659
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003660 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003661 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003662 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003663 macb_set_addr(lp, desc, addr);
3664 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003665 addr += AT91ETHER_MAX_RBUFF_SZ;
3666 }
3667
3668 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003669 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003670
3671 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003672 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003673
3674 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003675 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003676
3677 /* Enable Receive and Transmit */
3678 ctl = macb_readl(lp, NCR);
3679 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3680
3681 return 0;
3682}
3683
3684/* Open the ethernet interface */
3685static int at91ether_open(struct net_device *dev)
3686{
3687 struct macb *lp = netdev_priv(dev);
3688 u32 ctl;
3689 int ret;
3690
3691 /* Clear internal statistics */
3692 ctl = macb_readl(lp, NCR);
3693 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3694
3695 macb_set_hwaddr(lp);
3696
3697 ret = at91ether_start(dev);
3698 if (ret)
3699 return ret;
3700
3701 /* Enable MAC interrupts */
3702 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3703 MACB_BIT(RXUBR) |
3704 MACB_BIT(ISR_TUND) |
3705 MACB_BIT(ISR_RLE) |
3706 MACB_BIT(TCOMP) |
3707 MACB_BIT(ISR_ROVR) |
3708 MACB_BIT(HRESP));
3709
3710 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003711 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003712
3713 netif_start_queue(dev);
3714
3715 return 0;
3716}
3717
3718/* Close the interface */
3719static int at91ether_close(struct net_device *dev)
3720{
3721 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003722 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003723 u32 ctl;
3724
3725 /* Disable Receiver and Transmitter */
3726 ctl = macb_readl(lp, NCR);
3727 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3728
3729 /* Disable MAC interrupts */
3730 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3731 MACB_BIT(RXUBR) |
3732 MACB_BIT(ISR_TUND) |
3733 MACB_BIT(ISR_RLE) |
3734 MACB_BIT(TCOMP) |
3735 MACB_BIT(ISR_ROVR) |
3736 MACB_BIT(HRESP));
3737
3738 netif_stop_queue(dev);
3739
3740 dma_free_coherent(&lp->pdev->dev,
3741 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003742 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003743 q->rx_ring, q->rx_ring_dma);
3744 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003745
3746 dma_free_coherent(&lp->pdev->dev,
3747 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003748 q->rx_buffers, q->rx_buffers_dma);
3749 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003750
3751 return 0;
3752}
3753
3754/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003755static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3756 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003757{
3758 struct macb *lp = netdev_priv(dev);
3759
3760 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3761 netif_stop_queue(dev);
3762
3763 /* Store packet information (to free when Tx completed) */
3764 lp->skb = skb;
3765 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003766 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3767 skb->len, DMA_TO_DEVICE);
3768 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003769 dev_kfree_skb_any(skb);
3770 dev->stats.tx_dropped++;
3771 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3772 return NETDEV_TX_OK;
3773 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003774
3775 /* Set address of the data in the Transmit Address register */
3776 macb_writel(lp, TAR, lp->skb_physaddr);
3777 /* Set length of the packet in the Transmit Control register */
3778 macb_writel(lp, TCR, skb->len);
3779
3780 } else {
3781 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3782 return NETDEV_TX_BUSY;
3783 }
3784
3785 return NETDEV_TX_OK;
3786}
3787
3788/* Extract received frame from buffer descriptors and sent to upper layers.
3789 * (Called from interrupt context)
3790 */
3791static void at91ether_rx(struct net_device *dev)
3792{
3793 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003794 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003795 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003796 unsigned char *p_recv;
3797 struct sk_buff *skb;
3798 unsigned int pktlen;
3799
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003800 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003801 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003802 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003803 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003804 skb = netdev_alloc_skb(dev, pktlen + 2);
3805 if (skb) {
3806 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003807 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003808
3809 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003810 dev->stats.rx_packets++;
3811 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003812 netif_rx(skb);
3813 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003814 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003815 }
3816
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003817 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003818 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003819
3820 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003821 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003822
3823 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003824 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3825 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003826 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003827 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003828
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003829 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003830 }
3831}
3832
3833/* MAC interrupt handler */
3834static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3835{
3836 struct net_device *dev = dev_id;
3837 struct macb *lp = netdev_priv(dev);
3838 u32 intstatus, ctl;
3839
3840 /* MAC Interrupt Status register indicates what interrupts are pending.
3841 * It is automatically cleared once read.
3842 */
3843 intstatus = macb_readl(lp, ISR);
3844
3845 /* Receive complete */
3846 if (intstatus & MACB_BIT(RCOMP))
3847 at91ether_rx(dev);
3848
3849 /* Transmit complete */
3850 if (intstatus & MACB_BIT(TCOMP)) {
3851 /* The TCOM bit is set even if the transmission failed */
3852 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003853 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003854
3855 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003856 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003857 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003858 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003859 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003860 dev->stats.tx_packets++;
3861 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003862 }
3863 netif_wake_queue(dev);
3864 }
3865
3866 /* Work-around for EMAC Errata section 41.3.1 */
3867 if (intstatus & MACB_BIT(RXUBR)) {
3868 ctl = macb_readl(lp, NCR);
3869 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003870 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003871 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3872 }
3873
3874 if (intstatus & MACB_BIT(ISR_ROVR))
3875 netdev_err(dev, "ROVR error\n");
3876
3877 return IRQ_HANDLED;
3878}
3879
3880#ifdef CONFIG_NET_POLL_CONTROLLER
3881static void at91ether_poll_controller(struct net_device *dev)
3882{
3883 unsigned long flags;
3884
3885 local_irq_save(flags);
3886 at91ether_interrupt(dev->irq, dev);
3887 local_irq_restore(flags);
3888}
3889#endif
3890
3891static const struct net_device_ops at91ether_netdev_ops = {
3892 .ndo_open = at91ether_open,
3893 .ndo_stop = at91ether_close,
3894 .ndo_start_xmit = at91ether_start_xmit,
3895 .ndo_get_stats = macb_get_stats,
3896 .ndo_set_rx_mode = macb_set_rx_mode,
3897 .ndo_set_mac_address = eth_mac_addr,
3898 .ndo_do_ioctl = macb_ioctl,
3899 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003900#ifdef CONFIG_NET_POLL_CONTROLLER
3901 .ndo_poll_controller = at91ether_poll_controller,
3902#endif
3903};
3904
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003905static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303906 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303907 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003908{
3909 int err;
3910
3911 *hclk = NULL;
3912 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303913 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05303914 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003915
3916 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3917 if (IS_ERR(*pclk))
3918 return PTR_ERR(*pclk);
3919
3920 err = clk_prepare_enable(*pclk);
3921 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003922 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003923 return err;
3924 }
3925
3926 return 0;
3927}
3928
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003929static int at91ether_init(struct platform_device *pdev)
3930{
3931 struct net_device *dev = platform_get_drvdata(pdev);
3932 struct macb *bp = netdev_priv(dev);
3933 int err;
3934 u32 reg;
3935
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003936 bp->queues[0].bp = bp;
3937
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003938 dev->netdev_ops = &at91ether_netdev_ops;
3939 dev->ethtool_ops = &macb_ethtool_ops;
3940
3941 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3942 0, dev->name, dev);
3943 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003944 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003945
3946 macb_writel(bp, NCR, 0);
3947
3948 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3949 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3950 reg |= MACB_BIT(RM9200_RMII);
3951
3952 macb_writel(bp, NCFGR, reg);
3953
3954 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003955}
3956
Yash Shahc218ad52019-06-18 13:26:08 +05303957static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
3958 unsigned long parent_rate)
3959{
3960 return mgmt->rate;
3961}
3962
3963static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
3964 unsigned long *parent_rate)
3965{
3966 if (WARN_ON(rate < 2500000))
3967 return 2500000;
3968 else if (rate == 2500000)
3969 return 2500000;
3970 else if (WARN_ON(rate < 13750000))
3971 return 2500000;
3972 else if (WARN_ON(rate < 25000000))
3973 return 25000000;
3974 else if (rate == 25000000)
3975 return 25000000;
3976 else if (WARN_ON(rate < 75000000))
3977 return 25000000;
3978 else if (WARN_ON(rate < 125000000))
3979 return 125000000;
3980 else if (rate == 125000000)
3981 return 125000000;
3982
3983 WARN_ON(rate > 125000000);
3984
3985 return 125000000;
3986}
3987
3988static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
3989 unsigned long parent_rate)
3990{
3991 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
3992 if (rate != 125000000)
3993 iowrite32(1, mgmt->reg);
3994 else
3995 iowrite32(0, mgmt->reg);
3996 mgmt->rate = rate;
3997
3998 return 0;
3999}
4000
4001static const struct clk_ops fu540_c000_ops = {
4002 .recalc_rate = fu540_macb_tx_recalc_rate,
4003 .round_rate = fu540_macb_tx_round_rate,
4004 .set_rate = fu540_macb_tx_set_rate,
4005};
4006
4007static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4008 struct clk **hclk, struct clk **tx_clk,
4009 struct clk **rx_clk, struct clk **tsu_clk)
4010{
4011 struct clk_init_data init;
4012 int err = 0;
4013
4014 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4015 if (err)
4016 return err;
4017
4018 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4019 if (!mgmt)
4020 return -ENOMEM;
4021
4022 init.name = "sifive-gemgxl-mgmt";
4023 init.ops = &fu540_c000_ops;
4024 init.flags = 0;
4025 init.num_parents = 0;
4026
4027 mgmt->rate = 0;
4028 mgmt->hw.init = &init;
4029
4030 *tx_clk = clk_register(NULL, &mgmt->hw);
4031 if (IS_ERR(*tx_clk))
4032 return PTR_ERR(*tx_clk);
4033
4034 err = clk_prepare_enable(*tx_clk);
4035 if (err)
4036 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4037 else
4038 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4039
4040 return 0;
4041}
4042
4043static int fu540_c000_init(struct platform_device *pdev)
4044{
4045 struct resource *res;
4046
4047 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
4048 if (!res)
4049 return -ENODEV;
4050
4051 mgmt->reg = ioremap(res->start, resource_size(res));
4052 if (!mgmt->reg)
4053 return -ENOMEM;
4054
4055 return macb_init(pdev);
4056}
4057
4058static const struct macb_config fu540_c000_config = {
4059 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4060 MACB_CAPS_GEM_HAS_PTP,
4061 .dma_burst_length = 16,
4062 .clk_init = fu540_c000_clk_init,
4063 .init = fu540_c000_init,
4064 .jumbo_max_len = 10240,
4065};
4066
David S. Miller3cef5c52015-03-09 23:38:02 -04004067static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004068 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004069 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004070 .init = macb_init,
4071};
4072
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004073static const struct macb_config sama5d3macb_config = {
4074 .caps = MACB_CAPS_SG_DISABLED
4075 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4076 .clk_init = macb_clk_init,
4077 .init = macb_init,
4078};
4079
David S. Miller3cef5c52015-03-09 23:38:02 -04004080static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004081 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4082 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004083 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004084 .init = macb_init,
4085};
4086
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004087static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004088 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004089 .dma_burst_length = 16,
4090 .clk_init = macb_clk_init,
4091 .init = macb_init,
4092};
4093
David S. Miller3cef5c52015-03-09 23:38:02 -04004094static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004095 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004096 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004097 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004098 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004099 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004100 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004101};
4102
David S. Miller3cef5c52015-03-09 23:38:02 -04004103static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004104 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004105 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004106 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004107 .init = macb_init,
4108};
4109
David S. Miller3cef5c52015-03-09 23:38:02 -04004110static const struct macb_config emac_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304111 .caps = MACB_CAPS_NEEDS_RSTONUBR,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004112 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004113 .init = at91ether_init,
4114};
4115
Neil Armstronge611b5b2016-01-05 14:39:17 +01004116static const struct macb_config np4_config = {
4117 .caps = MACB_CAPS_USRIO_DISABLED,
4118 .clk_init = macb_clk_init,
4119 .init = macb_init,
4120};
David S. Miller36583eb2015-05-23 01:22:35 -04004121
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304122static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004123 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4124 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304125 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304126 .dma_burst_length = 16,
4127 .clk_init = macb_clk_init,
4128 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304129 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304130};
4131
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004132static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304133 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4134 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004135 .dma_burst_length = 16,
4136 .clk_init = macb_clk_init,
4137 .init = macb_init,
4138};
4139
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004140static const struct of_device_id macb_dt_ids[] = {
4141 { .compatible = "cdns,at32ap7000-macb" },
4142 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4143 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004144 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004145 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4146 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004147 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004148 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004149 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004150 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004151 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4152 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4153 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304154 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004155 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304156 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004157 { /* sentinel */ }
4158};
4159MODULE_DEVICE_TABLE(of, macb_dt_ids);
4160#endif /* CONFIG_OF */
4161
Bartosz Folta83a77e92016-12-14 06:39:15 +00004162static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004163 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4164 MACB_CAPS_JUMBO |
4165 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004166 .dma_burst_length = 16,
4167 .clk_init = macb_clk_init,
4168 .init = macb_init,
4169 .jumbo_max_len = 10240,
4170};
4171
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004172static int macb_probe(struct platform_device *pdev)
4173{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004174 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004175 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304176 struct clk **, struct clk **, struct clk **,
4177 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004178 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004179 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304180 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304181 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004182 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004183 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004184 struct phy_device *phydev;
4185 struct net_device *dev;
4186 struct resource *regs;
4187 void __iomem *mem;
4188 const char *mac;
4189 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304190 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004191
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004192 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4193 mem = devm_ioremap_resource(&pdev->dev, regs);
4194 if (IS_ERR(mem))
4195 return PTR_ERR(mem);
4196
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004197 if (np) {
4198 const struct of_device_id *match;
4199
4200 match = of_match_node(macb_dt_ids, np);
4201 if (match && match->data) {
4202 macb_config = match->data;
4203 clk_init = macb_config->clk_init;
4204 init = macb_config->init;
4205 }
4206 }
4207
Harini Katakamf5473d12019-03-01 16:20:33 +05304208 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004209 if (err)
4210 return err;
4211
Harini Katakamd54f89a2019-03-01 16:20:34 +05304212 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4213 pm_runtime_use_autosuspend(&pdev->dev);
4214 pm_runtime_get_noresume(&pdev->dev);
4215 pm_runtime_set_active(&pdev->dev);
4216 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004217 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004218
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004219 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004220 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004221 if (!dev) {
4222 err = -ENOMEM;
4223 goto err_disable_clocks;
4224 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004225
4226 dev->base_addr = regs->start;
4227
4228 SET_NETDEV_DEV(dev, &pdev->dev);
4229
4230 bp = netdev_priv(dev);
4231 bp->pdev = pdev;
4232 bp->dev = dev;
4233 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004234 bp->native_io = native_io;
4235 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004236 bp->macb_reg_readl = hw_readl_native;
4237 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004238 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004239 bp->macb_reg_readl = hw_readl;
4240 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004241 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004242 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004243 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004244 if (macb_config)
4245 bp->dma_burst_length = macb_config->dma_burst_length;
4246 bp->pclk = pclk;
4247 bp->hclk = hclk;
4248 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304249 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304250 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004251 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304252 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304253
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004254 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004255 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004256 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4257 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4258
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004259 spin_lock_init(&bp->lock);
4260
Nicolas Ferread783472015-03-31 15:02:02 +02004261 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004262 macb_configure_caps(bp, macb_config);
4263
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004264#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4265 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4266 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4267 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4268 }
4269#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004270 platform_set_drvdata(pdev, dev);
4271
4272 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004273 if (dev->irq < 0) {
4274 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004275 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004276 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004277
Jarod Wilson44770e12016-10-17 15:54:17 -04004278 /* MTU range: 68 - 1500 or 10240 */
4279 dev->min_mtu = GEM_MTU_MIN_SIZE;
4280 if (bp->caps & MACB_CAPS_JUMBO)
4281 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4282 else
4283 dev->max_mtu = ETH_DATA_LEN;
4284
Harini Katakam404cd082018-07-06 12:18:58 +05304285 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4286 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4287 if (val)
4288 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4289 macb_dma_desc_get_size(bp);
4290
4291 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4292 if (val)
4293 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4294 macb_dma_desc_get_size(bp);
4295 }
4296
Harini Katakame5010702019-01-29 15:20:03 +05304297 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4298 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4299 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4300
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004301 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004302 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4303 err = -EPROBE_DEFER;
4304 goto err_out_free_netdev;
Antoine Tenart2bf4ecb2019-06-21 17:26:35 +02004305 } else if (!IS_ERR_OR_NULL(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004306 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004307 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004308 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004309 }
frederic RODO6c36a702007-07-12 19:07:24 +02004310
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004311 err = of_get_phy_mode(np);
Nicolas Ferre8b952742019-05-03 12:36:58 +02004312 if (err < 0)
4313 /* not found in DT, MII by default */
4314 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4315 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004316 bp->phy_interface = err;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004317
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004318 /* IP specific init */
4319 err = init(pdev);
4320 if (err)
4321 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004322
Florian Fainellicf669662016-05-02 18:38:45 -07004323 err = macb_mii_init(bp);
4324 if (err)
4325 goto err_out_free_netdev;
4326
Philippe Reynes0a912812016-06-22 00:32:35 +02004327 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004328
4329 netif_carrier_off(dev);
4330
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004331 err = register_netdev(dev);
4332 if (err) {
4333 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004334 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004335 }
4336
Harini Katakam032dc412018-01-27 12:09:01 +05304337 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4338 (unsigned long)bp);
4339
Florian Fainellicf669662016-05-02 18:38:45 -07004340 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004341
Bo Shen58798232014-09-13 01:57:49 +02004342 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4343 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4344 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004345
Harini Katakamd54f89a2019-03-01 16:20:34 +05304346 pm_runtime_mark_last_busy(&bp->pdev->dev);
4347 pm_runtime_put_autosuspend(&bp->pdev->dev);
4348
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004349 return 0;
4350
Florian Fainellicf669662016-05-02 18:38:45 -07004351err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004352 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004353 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004354 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004355 if (np && of_phy_is_fixed_link(np))
4356 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004357 mdiobus_free(bp->mii_bus);
4358
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004359err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004360 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004361
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004362err_disable_clocks:
4363 clk_disable_unprepare(tx_clk);
Yash Shahc218ad52019-06-18 13:26:08 +05304364 clk_unregister(tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004365 clk_disable_unprepare(hclk);
4366 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304367 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304368 clk_disable_unprepare(tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304369 pm_runtime_disable(&pdev->dev);
4370 pm_runtime_set_suspended(&pdev->dev);
4371 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004372
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004373 return err;
4374}
4375
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004376static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004377{
4378 struct net_device *dev;
4379 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004380 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004381
4382 dev = platform_get_drvdata(pdev);
4383
4384 if (dev) {
4385 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004386 if (dev->phydev)
4387 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004388 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004389 if (np && of_phy_is_fixed_link(np))
4390 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004391 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004392 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004393
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004394 unregister_netdev(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304395 pm_runtime_disable(&pdev->dev);
4396 pm_runtime_dont_use_autosuspend(&pdev->dev);
4397 if (!pm_runtime_suspended(&pdev->dev)) {
4398 clk_disable_unprepare(bp->tx_clk);
Yash Shahc218ad52019-06-18 13:26:08 +05304399 clk_unregister(bp->tx_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304400 clk_disable_unprepare(bp->hclk);
4401 clk_disable_unprepare(bp->pclk);
4402 clk_disable_unprepare(bp->rx_clk);
4403 clk_disable_unprepare(bp->tsu_clk);
4404 pm_runtime_set_suspended(&pdev->dev);
4405 }
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004406 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004407 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004408 }
4409
4410 return 0;
4411}
4412
Michal Simekd23823d2015-01-23 09:36:03 +01004413static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004414{
Wolfram Sangce886a42018-10-21 22:00:14 +02004415 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004416 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304417 struct macb_queue *queue = bp->queues;
4418 unsigned long flags;
4419 unsigned int q;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004420
Harini Katakamde991c52019-03-01 16:20:35 +05304421 if (!netif_running(netdev))
4422 return 0;
4423
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004424
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004425 if (bp->wol & MACB_WOL_ENABLED) {
4426 macb_writel(bp, IER, MACB_BIT(WOL));
4427 macb_writel(bp, WOL, MACB_BIT(MAG));
4428 enable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304429 netif_device_detach(netdev);
4430 } else {
4431 netif_device_detach(netdev);
4432 for (q = 0, queue = bp->queues; q < bp->num_queues;
4433 ++q, ++queue)
4434 napi_disable(&queue->napi);
4435 phy_stop(netdev->phydev);
4436 phy_suspend(netdev->phydev);
4437 spin_lock_irqsave(&bp->lock, flags);
4438 macb_reset_hw(bp);
4439 spin_unlock_irqrestore(&bp->lock, flags);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004440
4441 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4442 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4443
4444 if (netdev->hw_features & NETIF_F_NTUPLE)
4445 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304446 }
4447
Harini Katakamde991c52019-03-01 16:20:35 +05304448 netif_carrier_off(netdev);
4449 if (bp->ptp_info)
4450 bp->ptp_info->ptp_remove(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304451 pm_runtime_force_suspend(dev);
4452
4453 return 0;
4454}
4455
4456static int __maybe_unused macb_resume(struct device *dev)
4457{
4458 struct net_device *netdev = dev_get_drvdata(dev);
4459 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304460 struct macb_queue *queue = bp->queues;
4461 unsigned int q;
4462
4463 if (!netif_running(netdev))
4464 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304465
4466 pm_runtime_force_resume(dev);
4467
4468 if (bp->wol & MACB_WOL_ENABLED) {
4469 macb_writel(bp, IDR, MACB_BIT(WOL));
4470 macb_writel(bp, WOL, 0);
4471 disable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304472 } else {
4473 macb_writel(bp, NCR, MACB_BIT(MPE));
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004474
4475 if (netdev->hw_features & NETIF_F_NTUPLE)
4476 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4477
4478 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4479 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4480
Harini Katakamde991c52019-03-01 16:20:35 +05304481 for (q = 0, queue = bp->queues; q < bp->num_queues;
4482 ++q, ++queue)
4483 napi_enable(&queue->napi);
4484 phy_resume(netdev->phydev);
4485 phy_init_hw(netdev->phydev);
4486 phy_start(netdev->phydev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304487 }
4488
Harini Katakamde991c52019-03-01 16:20:35 +05304489 bp->macbgem_ops.mog_init_rings(bp);
4490 macb_init_hw(bp);
4491 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004492 macb_restore_features(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304493 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304494 if (bp->ptp_info)
4495 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304496
4497 return 0;
4498}
4499
4500static int __maybe_unused macb_runtime_suspend(struct device *dev)
4501{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004502 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304503 struct macb *bp = netdev_priv(netdev);
4504
4505 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004506 clk_disable_unprepare(bp->tx_clk);
4507 clk_disable_unprepare(bp->hclk);
4508 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304509 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004510 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304511 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004512
4513 return 0;
4514}
4515
Harini Katakamd54f89a2019-03-01 16:20:34 +05304516static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004517{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004518 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004519 struct macb *bp = netdev_priv(netdev);
4520
Harini Katakamd54f89a2019-03-01 16:20:34 +05304521 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004522 clk_prepare_enable(bp->pclk);
4523 clk_prepare_enable(bp->hclk);
4524 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304525 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004526 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304527 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004528
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004529 return 0;
4530}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004531
Harini Katakamd54f89a2019-03-01 16:20:34 +05304532static const struct dev_pm_ops macb_pm_ops = {
4533 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4534 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4535};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004536
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004537static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004538 .probe = macb_probe,
4539 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004540 .driver = {
4541 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004542 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004543 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004544 },
4545};
4546
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004547module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004548
4549MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004550MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004551MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004552MODULE_ALIAS("platform:macb");