blob: 2c28da1737fe4ec2c0d1579d71bb454a3cabe289 [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>
Antoine Tenart7897b072019-11-13 10:00:06 +010028#include <linux/phylink.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)))
Harini Katakamf822e9c2020-02-05 18:08:12 +053076/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
77 * false amba_error in TX path from the DMA assuming there is not enough
78 * space in the SRAM (16KB) even when there is.
79 */
80#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020081
Jarod Wilson44770e12016-10-17 15:54:17 -040082#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070083#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053084
Sergio Prado3e2a5e12016-02-09 12:07:16 -020085#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
86#define MACB_WOL_ENABLED (0x1 << 1)
87
Moritz Fischer64ec42f2016-03-29 19:11:12 -070088/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000089 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
90 */
91#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010092
Harini Katakamd54f89a2019-03-01 16:20:34 +053093#define MACB_PM_TIMEOUT 100 /* ms */
94
Harini Katakam8beb79b2019-03-01 16:20:32 +053095#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
96
Rafal Ozieblodc97a892017-01-27 15:08:20 +000097/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010098 * depends on hardware configuration:
99 *
100 * 1. dma address width 32 bits:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 *
104 * 2. dma address width 64 bits:
105 * word 1: 32 bit address of Data Buffer
106 * word 2: control
107 * word 3: upper 32 bit address of Data Buffer
108 * word 4: unused
109 *
110 * 3. dma address width 32 bits with hardware timestamping:
111 * word 1: 32 bit address of Data Buffer
112 * word 2: control
113 * word 3: timestamp word 1
114 * word 4: timestamp word 2
115 *
116 * 4. dma address width 64 bits with hardware timestamping:
117 * word 1: 32 bit address of Data Buffer
118 * word 2: control
119 * word 3: upper 32 bit address of Data Buffer
120 * word 4: unused
121 * word 5: timestamp word 1
122 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000123 */
124static unsigned int macb_dma_desc_get_size(struct macb *bp)
125{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100126#ifdef MACB_EXT_DESC
127 unsigned int desc_size;
128
129 switch (bp->hw_dma_cap) {
130 case HW_DMA_CAP_64B:
131 desc_size = sizeof(struct macb_dma_desc)
132 + sizeof(struct macb_dma_desc_64);
133 break;
134 case HW_DMA_CAP_PTP:
135 desc_size = sizeof(struct macb_dma_desc)
136 + sizeof(struct macb_dma_desc_ptp);
137 break;
138 case HW_DMA_CAP_64B_PTP:
139 desc_size = sizeof(struct macb_dma_desc)
140 + sizeof(struct macb_dma_desc_64)
141 + sizeof(struct macb_dma_desc_ptp);
142 break;
143 default:
144 desc_size = sizeof(struct macb_dma_desc);
145 }
146 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000147#endif
148 return sizeof(struct macb_dma_desc);
149}
150
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100151static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000152{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100153#ifdef MACB_EXT_DESC
154 switch (bp->hw_dma_cap) {
155 case HW_DMA_CAP_64B:
156 case HW_DMA_CAP_PTP:
157 desc_idx <<= 1;
158 break;
159 case HW_DMA_CAP_64B_PTP:
160 desc_idx *= 3;
161 break;
162 default:
163 break;
164 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000165#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100166 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000167}
168
169#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
170static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
171{
Shubhrajyoti Datta99dcb842019-09-23 14:03:51 +0530172 return (struct macb_dma_desc_64 *)((void *)desc
173 + sizeof(struct macb_dma_desc));
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000174}
175#endif
176
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000177/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500178static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179{
Zach Brownb410d132016-10-19 09:56:57 -0500180 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181}
182
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100183static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
184 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000185{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000186 index = macb_tx_ring_wrap(queue->bp, index);
187 index = macb_adj_dma_desc_idx(queue->bp, index);
188 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000189}
190
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100191static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
192 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000193{
Zach Brownb410d132016-10-19 09:56:57 -0500194 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195}
196
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100197static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000198{
199 dma_addr_t offset;
200
Zach Brownb410d132016-10-19 09:56:57 -0500201 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000202 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000203
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100204 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205}
206
Zach Brownb410d132016-10-19 09:56:57 -0500207static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000208{
Zach Brownb410d132016-10-19 09:56:57 -0500209 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000210}
211
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000212static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000213{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000214 index = macb_rx_ring_wrap(queue->bp, index);
215 index = macb_adj_dma_desc_idx(queue->bp, index);
216 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000217}
218
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000219static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000220{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000221 return queue->rx_buffers + queue->bp->rx_buffer_size *
222 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000223}
224
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300225/* I/O accessors */
226static u32 hw_readl_native(struct macb *bp, int offset)
227{
228 return __raw_readl(bp->regs + offset);
229}
230
231static void hw_writel_native(struct macb *bp, int offset, u32 value)
232{
233 __raw_writel(value, bp->regs + offset);
234}
235
236static u32 hw_readl(struct macb *bp, int offset)
237{
238 return readl_relaxed(bp->regs + offset);
239}
240
241static void hw_writel(struct macb *bp, int offset, u32 value)
242{
243 writel_relaxed(value, bp->regs + offset);
244}
245
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700246/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700247 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300248 * descriptor access.
249 */
250static bool hw_is_native_io(void __iomem *addr)
251{
252 u32 value = MACB_BIT(LLB);
253
254 __raw_writel(value, addr + MACB_NCR);
255 value = __raw_readl(addr + MACB_NCR);
256
257 /* Write 0 back to disable everything */
258 __raw_writel(0, addr + MACB_NCR);
259
260 return value == MACB_BIT(LLB);
261}
262
263static bool hw_is_gem(void __iomem *addr, bool native_io)
264{
265 u32 id;
266
267 if (native_io)
268 id = __raw_readl(addr + MACB_MID);
269 else
270 id = readl_relaxed(addr + MACB_MID);
271
272 return MACB_BFEXT(IDNUM, id) >= 0x2;
273}
274
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100275static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100276{
277 u32 bottom;
278 u16 top;
279
280 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000281 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000283 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000284
285 /* Clear unused address register sets */
286 macb_or_gem_writel(bp, SA2B, 0);
287 macb_or_gem_writel(bp, SA2T, 0);
288 macb_or_gem_writel(bp, SA3B, 0);
289 macb_or_gem_writel(bp, SA3T, 0);
290 macb_or_gem_writel(bp, SA4B, 0);
291 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100292}
293
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100294static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100295{
296 u32 bottom;
297 u16 top;
298 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000299 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100300
Moritz Fischeraa50b552016-03-29 19:11:13 -0700301 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000302 for (i = 0; i < 4; i++) {
303 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
304 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100305
Nicolas Ferre8b952742019-05-03 12:36:58 +0200306 addr[0] = bottom & 0xff;
307 addr[1] = (bottom >> 8) & 0xff;
308 addr[2] = (bottom >> 16) & 0xff;
309 addr[3] = (bottom >> 24) & 0xff;
310 addr[4] = top & 0xff;
311 addr[5] = (top >> 8) & 0xff;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100312
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000313 if (is_valid_ether_addr(addr)) {
314 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
315 return;
316 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700317 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000318
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300319 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000320 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321}
322
Harini Katakam8beb79b2019-03-01 16:20:32 +0530323static int macb_mdio_wait_for_idle(struct macb *bp)
324{
325 u32 val;
326
327 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
328 1, MACB_MDIO_TIMEOUT);
329}
330
frederic RODO6c36a702007-07-12 19:07:24 +0200331static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100332{
frederic RODO6c36a702007-07-12 19:07:24 +0200333 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530334 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530335
Harini Katakamd54f89a2019-03-01 16:20:34 +0530336 status = pm_runtime_get_sync(&bp->pdev->dev);
337 if (status < 0)
338 goto mdio_pm_exit;
339
340 status = macb_mdio_wait_for_idle(bp);
341 if (status < 0)
342 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100343
Milind Parab43ad3522020-01-09 08:36:46 +0000344 if (regnum & MII_ADDR_C45) {
345 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
346 | MACB_BF(RW, MACB_MAN_C45_ADDR)
347 | MACB_BF(PHYA, mii_id)
348 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
349 | MACB_BF(DATA, regnum & 0xFFFF)
350 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
351
352 status = macb_mdio_wait_for_idle(bp);
353 if (status < 0)
354 goto mdio_read_exit;
355
356 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
357 | MACB_BF(RW, MACB_MAN_C45_READ)
358 | MACB_BF(PHYA, mii_id)
359 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
360 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
361 } else {
362 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
363 | MACB_BF(RW, MACB_MAN_C22_READ)
364 | MACB_BF(PHYA, mii_id)
365 | MACB_BF(REGA, regnum)
366 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
367 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100368
Harini Katakamd54f89a2019-03-01 16:20:34 +0530369 status = macb_mdio_wait_for_idle(bp);
370 if (status < 0)
371 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100372
Harini Katakamd54f89a2019-03-01 16:20:34 +0530373 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100374
Harini Katakamd54f89a2019-03-01 16:20:34 +0530375mdio_read_exit:
376 pm_runtime_mark_last_busy(&bp->pdev->dev);
377 pm_runtime_put_autosuspend(&bp->pdev->dev);
378mdio_pm_exit:
379 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100380}
381
frederic RODO6c36a702007-07-12 19:07:24 +0200382static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
383 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100384{
frederic RODO6c36a702007-07-12 19:07:24 +0200385 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530386 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530387
Harini Katakamd54f89a2019-03-01 16:20:34 +0530388 status = pm_runtime_get_sync(&bp->pdev->dev);
389 if (status < 0)
390 goto mdio_pm_exit;
391
392 status = macb_mdio_wait_for_idle(bp);
393 if (status < 0)
394 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100395
Milind Parab43ad3522020-01-09 08:36:46 +0000396 if (regnum & MII_ADDR_C45) {
397 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
398 | MACB_BF(RW, MACB_MAN_C45_ADDR)
399 | MACB_BF(PHYA, mii_id)
400 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
401 | MACB_BF(DATA, regnum & 0xFFFF)
402 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
403
404 status = macb_mdio_wait_for_idle(bp);
405 if (status < 0)
406 goto mdio_write_exit;
407
408 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
409 | MACB_BF(RW, MACB_MAN_C45_WRITE)
410 | MACB_BF(PHYA, mii_id)
411 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
412 | MACB_BF(CODE, MACB_MAN_C45_CODE)
413 | MACB_BF(DATA, value)));
414 } else {
415 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
416 | MACB_BF(RW, MACB_MAN_C22_WRITE)
417 | MACB_BF(PHYA, mii_id)
418 | MACB_BF(REGA, regnum)
419 | MACB_BF(CODE, MACB_MAN_C22_CODE)
420 | MACB_BF(DATA, value)));
421 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100422
Harini Katakamd54f89a2019-03-01 16:20:34 +0530423 status = macb_mdio_wait_for_idle(bp);
424 if (status < 0)
425 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100426
Harini Katakamd54f89a2019-03-01 16:20:34 +0530427mdio_write_exit:
428 pm_runtime_mark_last_busy(&bp->pdev->dev);
429 pm_runtime_put_autosuspend(&bp->pdev->dev);
430mdio_pm_exit:
431 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100432}
433
Antoine Tenart6e952d92019-11-13 10:00:05 +0100434static void macb_init_buffers(struct macb *bp)
435{
436 struct macb_queue *queue;
437 unsigned int q;
438
439 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
440 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
441#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
442 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
443 queue_writel(queue, RBQPH,
444 upper_32_bits(queue->rx_ring_dma));
445#endif
446 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
447#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
448 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
449 queue_writel(queue, TBQPH,
450 upper_32_bits(queue->tx_ring_dma));
451#endif
452 }
453}
454
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800455/**
456 * macb_set_tx_clk() - Set a clock to a new frequency
457 * @clk Pointer to the clock to change
458 * @rate New frequency in Hz
459 * @dev Pointer to the struct net_device
460 */
461static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
462{
463 long ferr, rate, rate_rounded;
464
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100465 if (!clk)
466 return;
467
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800468 switch (speed) {
469 case SPEED_10:
470 rate = 2500000;
471 break;
472 case SPEED_100:
473 rate = 25000000;
474 break;
475 case SPEED_1000:
476 rate = 125000000;
477 break;
478 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800479 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800480 }
481
482 rate_rounded = clk_round_rate(clk, rate);
483 if (rate_rounded < 0)
484 return;
485
486 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
487 * is not satisfied.
488 */
489 ferr = abs(rate_rounded - rate);
490 ferr = DIV_ROUND_UP(ferr, rate / 100000);
491 if (ferr > 5)
492 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700493 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800494
495 if (clk_set_rate(clk, rate_rounded))
496 netdev_err(dev, "adjusting tx_clk failed.\n");
497}
498
Antoine Tenart7897b072019-11-13 10:00:06 +0100499static void macb_validate(struct phylink_config *config,
500 unsigned long *supported,
501 struct phylink_link_state *state)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100502{
Antoine Tenart7897b072019-11-13 10:00:06 +0100503 struct net_device *ndev = to_net_dev(config->dev);
504 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
505 struct macb *bp = netdev_priv(ndev);
506
507 /* We only support MII, RMII, GMII, RGMII & SGMII. */
508 if (state->interface != PHY_INTERFACE_MODE_NA &&
509 state->interface != PHY_INTERFACE_MODE_MII &&
510 state->interface != PHY_INTERFACE_MODE_RMII &&
511 state->interface != PHY_INTERFACE_MODE_GMII &&
512 state->interface != PHY_INTERFACE_MODE_SGMII &&
513 !phy_interface_mode_is_rgmii(state->interface)) {
514 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
515 return;
516 }
517
518 if (!macb_is_gem(bp) &&
519 (state->interface == PHY_INTERFACE_MODE_GMII ||
520 phy_interface_mode_is_rgmii(state->interface))) {
521 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
522 return;
523 }
524
525 phylink_set_port_modes(mask);
526 phylink_set(mask, Autoneg);
527 phylink_set(mask, Asym_Pause);
528
529 phylink_set(mask, 10baseT_Half);
530 phylink_set(mask, 10baseT_Full);
531 phylink_set(mask, 100baseT_Half);
532 phylink_set(mask, 100baseT_Full);
533
534 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
535 (state->interface == PHY_INTERFACE_MODE_NA ||
536 state->interface == PHY_INTERFACE_MODE_GMII ||
537 state->interface == PHY_INTERFACE_MODE_SGMII ||
538 phy_interface_mode_is_rgmii(state->interface))) {
539 phylink_set(mask, 1000baseT_Full);
540 phylink_set(mask, 1000baseX_Full);
541
542 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
543 phylink_set(mask, 1000baseT_Half);
544 }
545
546 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
547 bitmap_and(state->advertising, state->advertising, mask,
548 __ETHTOOL_LINK_MODE_MASK_NBITS);
549}
550
Russell Kingd46b7e42019-11-21 00:36:22 +0000551static void macb_mac_pcs_get_state(struct phylink_config *config,
552 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100553{
Russell Kingd46b7e42019-11-21 00:36:22 +0000554 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100555}
556
557static void macb_mac_an_restart(struct phylink_config *config)
558{
559 /* Not supported */
560}
561
562static void macb_mac_config(struct phylink_config *config, unsigned int mode,
563 const struct phylink_link_state *state)
564{
565 struct net_device *ndev = to_net_dev(config->dev);
566 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200567 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100568 u32 old_ctrl, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100569
frederic RODO6c36a702007-07-12 19:07:24 +0200570 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100571
Antoine Tenart7897b072019-11-13 10:00:06 +0100572 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100573
Antoine Tenart7897b072019-11-13 10:00:06 +0100574 /* Clear all the bits we might set later */
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100575 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | MACB_BIT(PAE));
576
577 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
578 if (state->interface == PHY_INTERFACE_MODE_RMII)
579 ctrl |= MACB_BIT(RM9200_RMII);
580 } else {
581 ctrl &= ~(GEM_BIT(GBE) | GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
582
583 /* We do not support MLO_PAUSE_RX yet */
584 if (state->pause & MLO_PAUSE_TX)
585 ctrl |= MACB_BIT(PAE);
586
587 if (state->interface == PHY_INTERFACE_MODE_SGMII)
588 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
589 }
frederic RODO6c36a702007-07-12 19:07:24 +0200590
Antoine Tenart7897b072019-11-13 10:00:06 +0100591 if (state->speed == SPEED_1000)
592 ctrl |= GEM_BIT(GBE);
593 else if (state->speed == SPEED_100)
594 ctrl |= MACB_BIT(SPD);
frederic RODO6c36a702007-07-12 19:07:24 +0200595
Antoine Tenart7897b072019-11-13 10:00:06 +0100596 if (state->duplex)
597 ctrl |= MACB_BIT(FD);
frederic RODO6c36a702007-07-12 19:07:24 +0200598
Antoine Tenart7897b072019-11-13 10:00:06 +0100599 /* Apply the new configuration, if any */
600 if (old_ctrl ^ ctrl)
601 macb_or_gem_writel(bp, NCFGR, ctrl);
602
603 bp->speed = state->speed;
frederic RODO6c36a702007-07-12 19:07:24 +0200604
605 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200606}
607
Antoine Tenart7897b072019-11-13 10:00:06 +0100608static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
609 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200610{
Antoine Tenart7897b072019-11-13 10:00:06 +0100611 struct net_device *ndev = to_net_dev(config->dev);
612 struct macb *bp = netdev_priv(ndev);
613 struct macb_queue *queue;
614 unsigned int q;
615 u32 ctrl;
616
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100617 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
618 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
619 queue_writel(queue, IDR,
620 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100621
622 /* Disable Rx and Tx */
623 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
624 macb_writel(bp, NCR, ctrl);
625
626 netif_tx_stop_all_queues(ndev);
627}
628
629static void macb_mac_link_up(struct phylink_config *config, unsigned int mode,
630 phy_interface_t interface, struct phy_device *phy)
631{
632 struct net_device *ndev = to_net_dev(config->dev);
633 struct macb *bp = netdev_priv(ndev);
634 struct macb_queue *queue;
635 unsigned int q;
636
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100637 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
638 macb_set_tx_clk(bp->tx_clk, bp->speed, ndev);
Antoine Tenart7897b072019-11-13 10:00:06 +0100639
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100640 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
641 * cleared the pipeline and control registers.
642 */
643 bp->macbgem_ops.mog_init_rings(bp);
644 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100645
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100646 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
647 queue_writel(queue, IER,
648 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
649 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100650
651 /* Enable Rx and Tx */
652 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
653
654 netif_tx_wake_all_queues(ndev);
655}
656
657static const struct phylink_mac_ops macb_phylink_ops = {
658 .validate = macb_validate,
Russell Kingd46b7e42019-11-21 00:36:22 +0000659 .mac_pcs_get_state = macb_mac_pcs_get_state,
Antoine Tenart7897b072019-11-13 10:00:06 +0100660 .mac_an_restart = macb_mac_an_restart,
661 .mac_config = macb_mac_config,
662 .mac_link_down = macb_mac_link_down,
663 .mac_link_up = macb_mac_link_up,
664};
665
Milind Parabfd2a8912020-01-13 03:30:43 +0000666static bool macb_phy_handle_exists(struct device_node *dn)
667{
668 dn = of_parse_phandle(dn, "phy-handle", 0);
669 of_node_put(dn);
670 return dn != NULL;
671}
672
Antoine Tenart7897b072019-11-13 10:00:06 +0100673static int macb_phylink_connect(struct macb *bp)
674{
Milind Parabfd2a8912020-01-13 03:30:43 +0000675 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100676 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000677 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100678 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500679
Milind Parabfd2a8912020-01-13 03:30:43 +0000680 if (dn)
681 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
682
683 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200684 phydev = phy_find_first(bp->mii_bus);
685 if (!phydev) {
686 netdev_err(dev, "no PHY found\n");
687 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000688 }
frederic RODO6c36a702007-07-12 19:07:24 +0200689
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200690 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100691 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000692 }
693
694 if (ret) {
695 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
696 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200697 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100698
Antoine Tenart7897b072019-11-13 10:00:06 +0100699 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100700
Antoine Tenart7897b072019-11-13 10:00:06 +0100701 return 0;
702}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100703
Antoine Tenart7897b072019-11-13 10:00:06 +0100704/* based on au1000_eth. c*/
705static int macb_mii_probe(struct net_device *dev)
706{
707 struct macb *bp = netdev_priv(dev);
708
709 bp->phylink_config.dev = &dev->dev;
710 bp->phylink_config.type = PHYLINK_NETDEV;
711
712 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
713 bp->phy_interface, &macb_phylink_ops);
714 if (IS_ERR(bp->phylink)) {
715 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
716 PTR_ERR(bp->phylink));
717 return PTR_ERR(bp->phylink);
718 }
frederic RODO6c36a702007-07-12 19:07:24 +0200719
720 return 0;
721}
722
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100723static int macb_mdiobus_register(struct macb *bp)
724{
725 struct device_node *child, *np = bp->pdev->dev.of_node;
726
727 /* Only create the PHY from the device tree if at least one PHY is
728 * described. Otherwise scan the entire MDIO bus. We do this to support
729 * old device tree that did not follow the best practices and did not
730 * describe their network PHYs.
731 */
732 for_each_available_child_of_node(np, child)
733 if (of_mdiobus_child_is_phy(child)) {
734 /* The loop increments the child refcount,
735 * decrement it before returning.
736 */
737 of_node_put(child);
738
739 return of_mdiobus_register(bp->mii_bus, np);
740 }
741
742 return mdiobus_register(bp->mii_bus);
743}
744
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100745static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200746{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200747 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200748
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200749 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200750 macb_writel(bp, NCR, MACB_BIT(MPE));
751
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700752 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700753 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200754 err = -ENOMEM;
755 goto err_out;
756 }
757
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700758 bp->mii_bus->name = "MACB_mii_bus";
759 bp->mii_bus->read = &macb_mdio_read;
760 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000761 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700762 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700763 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700764 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700765
Jamie Iles91523942011-02-28 04:05:25 +0000766 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200767
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100768 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200769 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100770 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200771
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200772 err = macb_mii_probe(bp->dev);
773 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200774 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200775
776 return 0;
777
778err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700779 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500780err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700781 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200782err_out:
783 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100784}
785
786static void macb_update_stats(struct macb *bp)
787{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000788 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
789 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300790 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100791
792 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
793
Moritz Fischer96ec6312016-03-29 19:11:11 -0700794 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700795 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100796}
797
Nicolas Ferree86cd532012-10-31 06:04:57 +0000798static int macb_halt_tx(struct macb *bp)
799{
800 unsigned long halt_time, timeout;
801 u32 status;
802
803 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
804
805 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
806 do {
807 halt_time = jiffies;
808 status = macb_readl(bp, TSR);
809 if (!(status & MACB_BIT(TGO)))
810 return 0;
811
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800812 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000813 } while (time_before(halt_time, timeout));
814
815 return -ETIMEDOUT;
816}
817
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200818static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
819{
820 if (tx_skb->mapping) {
821 if (tx_skb->mapped_as_page)
822 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
823 tx_skb->size, DMA_TO_DEVICE);
824 else
825 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
826 tx_skb->size, DMA_TO_DEVICE);
827 tx_skb->mapping = 0;
828 }
829
830 if (tx_skb->skb) {
831 dev_kfree_skb_any(tx_skb->skb);
832 tx_skb->skb = NULL;
833 }
834}
835
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000836static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530837{
Harini Katakamfff80192016-08-09 13:15:53 +0530838#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000839 struct macb_dma_desc_64 *desc_64;
840
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100841 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000842 desc_64 = macb_64b_desc(bp, desc);
843 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200844 /* The low bits of RX address contain the RX_USED bit, clearing
845 * of which allows packet RX. Make sure the high bits are also
846 * visible to HW at that point.
847 */
848 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000849 }
Harini Katakamfff80192016-08-09 13:15:53 +0530850#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000851 desc->addr = lower_32_bits(addr);
852}
853
854static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
855{
856 dma_addr_t addr = 0;
857#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
858 struct macb_dma_desc_64 *desc_64;
859
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100860 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000861 desc_64 = macb_64b_desc(bp, desc);
862 addr = ((u64)(desc_64->addrh) << 32);
863 }
864#endif
865 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
866 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530867}
868
Nicolas Ferree86cd532012-10-31 06:04:57 +0000869static void macb_tx_error_task(struct work_struct *work)
870{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100871 struct macb_queue *queue = container_of(work, struct macb_queue,
872 tx_error_task);
873 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000874 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100875 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000876 struct sk_buff *skb;
877 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100878 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000879
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100880 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
881 (unsigned int)(queue - bp->queues),
882 queue->tx_tail, queue->tx_head);
883
884 /* Prevent the queue IRQ handlers from running: each of them may call
885 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
886 * As explained below, we have to halt the transmission before updating
887 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
888 * network engine about the macb/gem being halted.
889 */
890 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000891
892 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100893 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000894
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700895 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000896 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100897 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000898 */
899 if (macb_halt_tx(bp))
900 /* Just complain for now, reinitializing TX path can be good */
901 netdev_err(bp->dev, "BUG: halt tx timed out\n");
902
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700903 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000904 * Free transmit buffers in upper layer.
905 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100906 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
907 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000908
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100909 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000910 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100911 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000912 skb = tx_skb->skb;
913
914 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200915 /* skb is set for the last buffer of the frame */
916 while (!skb) {
917 macb_tx_unmap(bp, tx_skb);
918 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100919 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200920 skb = tx_skb->skb;
921 }
922
923 /* ctrl still refers to the first buffer descriptor
924 * since it's the only one written back by the hardware
925 */
926 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
927 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500928 macb_tx_ring_wrap(bp, tail),
929 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200930 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000931 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200932 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000933 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200934 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000935 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700936 /* "Buffers exhausted mid-frame" errors may only happen
937 * if the driver is buggy, so complain loudly about
938 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000939 */
940 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
941 netdev_err(bp->dev,
942 "BUG: TX buffers exhausted mid-frame\n");
943
944 desc->ctrl = ctrl | MACB_BIT(TX_USED);
945 }
946
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200947 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000948 }
949
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100950 /* Set end of TX queue */
951 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000952 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100953 desc->ctrl = MACB_BIT(TX_USED);
954
Nicolas Ferree86cd532012-10-31 06:04:57 +0000955 /* Make descriptor updates visible to hardware */
956 wmb();
957
958 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000959 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530960#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100961 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000962 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530963#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000964 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100965 queue->tx_head = 0;
966 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000967
968 /* Housework before enabling TX IRQ */
969 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100970 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
971
972 /* Now we are ready to start transmission again */
973 netif_tx_start_all_queues(bp->dev);
974 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
975
976 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000977}
978
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100979static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100980{
981 unsigned int tail;
982 unsigned int head;
983 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100984 struct macb *bp = queue->bp;
985 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986
987 status = macb_readl(bp, TSR);
988 macb_writel(bp, TSR, status);
989
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000990 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100991 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000992
Nicolas Ferree86cd532012-10-31 06:04:57 +0000993 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700994 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100995
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100996 head = queue->tx_head;
997 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000998 struct macb_tx_skb *tx_skb;
999 struct sk_buff *skb;
1000 struct macb_dma_desc *desc;
1001 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001002
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001003 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001004
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001005 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001006 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001007
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001008 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001009
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001010 /* TX_USED bit is only set by hardware on the very first buffer
1011 * descriptor of the transmitted frame.
1012 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001013 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001014 break;
1015
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001016 /* Process all buffers of the current transmitted frame */
1017 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001018 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001019 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001020
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001021 /* First, update TX stats if needed */
1022 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001023 if (unlikely(skb_shinfo(skb)->tx_flags &
1024 SKBTX_HW_TSTAMP) &&
1025 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001026 /* skb now belongs to timestamp buffer
1027 * and will be removed later
1028 */
1029 tx_skb->skb = NULL;
1030 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001031 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001032 macb_tx_ring_wrap(bp, tail),
1033 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001034 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001035 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001036 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001037 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001038 }
1039
1040 /* Now we can safely release resources */
1041 macb_tx_unmap(bp, tx_skb);
1042
1043 /* skb is set only for the last buffer of the frame.
1044 * WARNING: at this point skb has been freed by
1045 * macb_tx_unmap().
1046 */
1047 if (skb)
1048 break;
1049 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001050 }
1051
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001052 queue->tx_tail = tail;
1053 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1054 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001055 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001056 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001057}
1058
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001059static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001060{
1061 unsigned int entry;
1062 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001063 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001064 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001065 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001066
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001067 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1068 bp->rx_ring_size) > 0) {
1069 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001070
1071 /* Make hw descriptor updates visible to CPU */
1072 rmb();
1073
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001074 queue->rx_prepared_head++;
1075 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001076
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001077 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001078 /* allocate sk_buff for this free entry in ring */
1079 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001080 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001081 netdev_err(bp->dev,
1082 "Unable to allocate sk_buff\n");
1083 break;
1084 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001085
1086 /* now fill corresponding descriptor entry */
1087 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001088 bp->rx_buffer_size,
1089 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001090 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1091 dev_kfree_skb(skb);
1092 break;
1093 }
1094
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001095 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001096
Zach Brownb410d132016-10-19 09:56:57 -05001097 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001098 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001099 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001100 /* Setting addr clears RX_USED and allows reception,
1101 * make sure ctrl is cleared first to avoid a race.
1102 */
1103 dma_wmb();
1104 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001105
1106 /* properly align Ethernet header */
1107 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301108 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001109 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001110 dma_wmb();
1111 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001112 }
1113 }
1114
1115 /* Make descriptor updates visible to hardware */
1116 wmb();
1117
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001118 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1119 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001120}
1121
1122/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001123static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001124 unsigned int end)
1125{
1126 unsigned int frag;
1127
1128 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001129 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001130
Nicolas Ferre4df95132013-06-04 21:57:12 +00001131 desc->addr &= ~MACB_BIT(RX_USED);
1132 }
1133
1134 /* Make descriptor updates visible to hardware */
1135 wmb();
1136
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001137 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001138 * whatever caused this is updated, so we don't have to record
1139 * anything.
1140 */
1141}
1142
Antoine Tenart97236cd2019-06-21 17:30:02 +02001143static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1144 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001145{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001146 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001147 unsigned int len;
1148 unsigned int entry;
1149 struct sk_buff *skb;
1150 struct macb_dma_desc *desc;
1151 int count = 0;
1152
1153 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301154 u32 ctrl;
1155 dma_addr_t addr;
1156 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001157
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001158 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1159 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001160
1161 /* Make hw descriptor updates visible to CPU */
1162 rmb();
1163
Harini Katakamfff80192016-08-09 13:15:53 +05301164 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001165 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001166
Harini Katakamfff80192016-08-09 13:15:53 +05301167 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001168 break;
1169
Anssi Hannula6e0af292018-12-17 15:05:41 +02001170 /* Ensure ctrl is at least as up-to-date as rxused */
1171 dma_rmb();
1172
1173 ctrl = desc->ctrl;
1174
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001175 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001176 count++;
1177
1178 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1179 netdev_err(bp->dev,
1180 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001181 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001182 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001183 break;
1184 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001185 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001186 if (unlikely(!skb)) {
1187 netdev_err(bp->dev,
1188 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001189 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001190 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001191 break;
1192 }
1193 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001194 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301195 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001196
1197 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1198
1199 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001200 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001201 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001202
1203 skb->protocol = eth_type_trans(skb, bp->dev);
1204 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001205 if (bp->dev->features & NETIF_F_RXCSUM &&
1206 !(bp->dev->flags & IFF_PROMISC) &&
1207 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1208 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001209
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001210 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001211 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001212 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001213 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001214
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001215 gem_ptp_do_rxstamp(bp, skb, desc);
1216
Nicolas Ferre4df95132013-06-04 21:57:12 +00001217#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1218 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1219 skb->len, skb->csum);
1220 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001221 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001222 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1223 skb->data, 32, true);
1224#endif
1225
Antoine Tenart97236cd2019-06-21 17:30:02 +02001226 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001227 }
1228
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001229 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001230
1231 return count;
1232}
1233
Antoine Tenart97236cd2019-06-21 17:30:02 +02001234static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1235 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236{
1237 unsigned int len;
1238 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001239 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001241 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001242 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001244 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301245 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001246
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001247 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001248 macb_rx_ring_wrap(bp, first_frag),
1249 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001250
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001251 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001252 * first buffer. Since the header is 14 bytes, this makes the
1253 * payload word-aligned.
1254 *
1255 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1256 * the two padding bytes into the skb so that we avoid hitting
1257 * the slowpath in memcpy(), and pull them off afterwards.
1258 */
1259 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001260 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001261 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001262 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001263 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001264 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001265 if (frag == last_frag)
1266 break;
1267 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001268
1269 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001270 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001271
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001272 return 1;
1273 }
1274
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001275 offset = 0;
1276 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001277 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001278 skb_put(skb, len);
1279
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001280 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001281 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001282
1283 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001284 if (unlikely(frag != last_frag)) {
1285 dev_kfree_skb_any(skb);
1286 return -1;
1287 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001288 frag_len = len - offset;
1289 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001290 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001291 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001292 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001293 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001294 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001295 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001296
1297 if (frag == last_frag)
1298 break;
1299 }
1300
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001301 /* Make descriptor updates visible to hardware */
1302 wmb();
1303
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001304 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001305 skb->protocol = eth_type_trans(skb, bp->dev);
1306
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001307 bp->dev->stats.rx_packets++;
1308 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001309 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001310 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001311 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001312
1313 return 0;
1314}
1315
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001316static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001317{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001318 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001319 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001320 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001321 int i;
1322
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001323 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001324 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001325 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001326 macb_set_addr(bp, desc, addr);
1327 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001328 addr += bp->rx_buffer_size;
1329 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001330 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001331 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001332}
1333
Antoine Tenart97236cd2019-06-21 17:30:02 +02001334static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1335 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001336{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001337 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001338 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001339 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001340 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001341 int first_frag = -1;
1342
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001343 for (tail = queue->rx_tail; budget > 0; tail++) {
1344 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001345 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001346
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001347 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001348 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001349
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001350 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001351 break;
1352
Anssi Hannula6e0af292018-12-17 15:05:41 +02001353 /* Ensure ctrl is at least as up-to-date as addr */
1354 dma_rmb();
1355
1356 ctrl = desc->ctrl;
1357
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001358 if (ctrl & MACB_BIT(RX_SOF)) {
1359 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001360 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001361 first_frag = tail;
1362 }
1363
1364 if (ctrl & MACB_BIT(RX_EOF)) {
1365 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001366
1367 if (unlikely(first_frag == -1)) {
1368 reset_rx_queue = true;
1369 continue;
1370 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371
Antoine Tenart97236cd2019-06-21 17:30:02 +02001372 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001373 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001374 if (unlikely(dropped < 0)) {
1375 reset_rx_queue = true;
1376 continue;
1377 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001378 if (!dropped) {
1379 received++;
1380 budget--;
1381 }
1382 }
1383 }
1384
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001385 if (unlikely(reset_rx_queue)) {
1386 unsigned long flags;
1387 u32 ctrl;
1388
1389 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1390
1391 spin_lock_irqsave(&bp->lock, flags);
1392
1393 ctrl = macb_readl(bp, NCR);
1394 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1395
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001396 macb_init_rx_ring(queue);
1397 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001398
1399 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1400
1401 spin_unlock_irqrestore(&bp->lock, flags);
1402 return received;
1403 }
1404
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001405 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001406 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001407 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001408 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409
1410 return received;
1411}
1412
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001413static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001414{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001415 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1416 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001417 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001418 u32 status;
1419
1420 status = macb_readl(bp, RSR);
1421 macb_writel(bp, RSR, status);
1422
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001423 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001424 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001425
Antoine Tenart97236cd2019-06-21 17:30:02 +02001426 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001427 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001428 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429
Nicolas Ferre8770e912013-02-12 11:08:48 +01001430 /* Packets received while interrupts were disabled */
1431 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001432 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001433 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001434 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001435 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001436 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301437 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001438 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001439 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001440
1441 /* TODO: Handle errors */
1442
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001443 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001444}
1445
Harini Katakam032dc412018-01-27 12:09:01 +05301446static void macb_hresp_error_task(unsigned long data)
1447{
1448 struct macb *bp = (struct macb *)data;
1449 struct net_device *dev = bp->dev;
1450 struct macb_queue *queue = bp->queues;
1451 unsigned int q;
1452 u32 ctrl;
1453
1454 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301455 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301456 MACB_TX_INT_FLAGS |
1457 MACB_BIT(HRESP));
1458 }
1459 ctrl = macb_readl(bp, NCR);
1460 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1461 macb_writel(bp, NCR, ctrl);
1462
1463 netif_tx_stop_all_queues(dev);
1464 netif_carrier_off(dev);
1465
1466 bp->macbgem_ops.mog_init_rings(bp);
1467
1468 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001469 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301470
Antoine Tenart6e952d92019-11-13 10:00:05 +01001471 /* Enable interrupts */
1472 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301473 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301474 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301475 MACB_TX_INT_FLAGS |
1476 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301477
1478 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1479 macb_writel(bp, NCR, ctrl);
1480
1481 netif_carrier_on(dev);
1482 netif_tx_start_all_queues(dev);
1483}
1484
Claudiu Beznea42983882018-12-17 10:02:42 +00001485static void macb_tx_restart(struct macb_queue *queue)
1486{
1487 unsigned int head = queue->tx_head;
1488 unsigned int tail = queue->tx_tail;
1489 struct macb *bp = queue->bp;
1490
1491 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1492 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1493
1494 if (head == tail)
1495 return;
1496
1497 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1498}
1499
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500static irqreturn_t macb_interrupt(int irq, void *dev_id)
1501{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001502 struct macb_queue *queue = dev_id;
1503 struct macb *bp = queue->bp;
1504 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001505 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001506
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001507 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001508
1509 if (unlikely(!status))
1510 return IRQ_NONE;
1511
1512 spin_lock(&bp->lock);
1513
1514 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001515 /* close possible race with dev_close */
1516 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001517 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001518 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1519 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001520 break;
1521 }
1522
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001523 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1524 (unsigned int)(queue - bp->queues),
1525 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001526
Harini Katakame5010702019-01-29 15:20:03 +05301527 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001528 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001529 * until we have processed the buffers. The
1530 * scheduling call may fail if the poll routine
1531 * is already scheduled, so disable interrupts
1532 * now.
1533 */
Harini Katakame5010702019-01-29 15:20:03 +05301534 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001535 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001536 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001537
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001538 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001539 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001540 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001541 }
1542 }
1543
Nicolas Ferree86cd532012-10-31 06:04:57 +00001544 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001545 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1546 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001547
1548 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001549 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001550
Nicolas Ferree86cd532012-10-31 06:04:57 +00001551 break;
1552 }
1553
1554 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001555 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001556
Claudiu Beznea42983882018-12-17 10:02:42 +00001557 if (status & MACB_BIT(TXUBR))
1558 macb_tx_restart(queue);
1559
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001560 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001561 * add that if/when we get our hands on a full-blown MII PHY.
1562 */
1563
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001564 /* There is a hardware issue under heavy load where DMA can
1565 * stop, this causes endless "used buffer descriptor read"
1566 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301567 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1568 * section 16.7.4 for details. RXUBR is only enabled for
1569 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001570 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001571 if (status & MACB_BIT(RXUBR)) {
1572 ctrl = macb_readl(bp, NCR);
1573 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001574 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001575 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1576
1577 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001578 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001579 }
1580
Alexander Steinb19f7f72011-04-13 05:03:24 +00001581 if (status & MACB_BIT(ISR_ROVR)) {
1582 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001583 if (macb_is_gem(bp))
1584 bp->hw_stats.gem.rx_overruns++;
1585 else
1586 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001587
1588 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001589 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001590 }
1591
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001592 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301593 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001594 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001595
1596 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001597 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001598 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001599 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001600 }
1601
1602 spin_unlock(&bp->lock);
1603
1604 return IRQ_HANDLED;
1605}
1606
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001607#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001608/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001609 * to allow network i/o with interrupts disabled.
1610 */
1611static void macb_poll_controller(struct net_device *dev)
1612{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001613 struct macb *bp = netdev_priv(dev);
1614 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001615 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001616 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001617
1618 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001619 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1620 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001621 local_irq_restore(flags);
1622}
1623#endif
1624
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001625static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001626 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001627 struct sk_buff *skb,
1628 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001629{
1630 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001631 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001632 struct macb_tx_skb *tx_skb = NULL;
1633 struct macb_dma_desc *desc;
1634 unsigned int offset, size, count = 0;
1635 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001636 unsigned int eof = 1, mss_mfs = 0;
1637 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1638
1639 /* LSO */
1640 if (skb_shinfo(skb)->gso_size != 0) {
1641 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1642 /* UDP - UFO */
1643 lso_ctrl = MACB_LSO_UFO_ENABLE;
1644 else
1645 /* TCP - TSO */
1646 lso_ctrl = MACB_LSO_TSO_ENABLE;
1647 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001648
1649 /* First, map non-paged data */
1650 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001651
1652 /* first buffer length */
1653 size = hdrlen;
1654
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001655 offset = 0;
1656 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001657 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001658 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001659
1660 mapping = dma_map_single(&bp->pdev->dev,
1661 skb->data + offset,
1662 size, DMA_TO_DEVICE);
1663 if (dma_mapping_error(&bp->pdev->dev, mapping))
1664 goto dma_error;
1665
1666 /* Save info to properly release resources */
1667 tx_skb->skb = NULL;
1668 tx_skb->mapping = mapping;
1669 tx_skb->size = size;
1670 tx_skb->mapped_as_page = false;
1671
1672 len -= size;
1673 offset += size;
1674 count++;
1675 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001676
1677 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001678 }
1679
1680 /* Then, map paged data from fragments */
1681 for (f = 0; f < nr_frags; f++) {
1682 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1683
1684 len = skb_frag_size(frag);
1685 offset = 0;
1686 while (len) {
1687 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001688 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001689 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001690
1691 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1692 offset, size, DMA_TO_DEVICE);
1693 if (dma_mapping_error(&bp->pdev->dev, mapping))
1694 goto dma_error;
1695
1696 /* Save info to properly release resources */
1697 tx_skb->skb = NULL;
1698 tx_skb->mapping = mapping;
1699 tx_skb->size = size;
1700 tx_skb->mapped_as_page = true;
1701
1702 len -= size;
1703 offset += size;
1704 count++;
1705 tx_head++;
1706 }
1707 }
1708
1709 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001710 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001711 netdev_err(bp->dev, "BUG! empty skb!\n");
1712 return 0;
1713 }
1714
1715 /* This is the last buffer of the frame: save socket buffer */
1716 tx_skb->skb = skb;
1717
1718 /* Update TX ring: update buffer descriptors in reverse order
1719 * to avoid race condition
1720 */
1721
1722 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1723 * to set the end of TX queue
1724 */
1725 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001726 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001727 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001728 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001729 desc->ctrl = ctrl;
1730
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001731 if (lso_ctrl) {
1732 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1733 /* include header and FCS in value given to h/w */
1734 mss_mfs = skb_shinfo(skb)->gso_size +
1735 skb_transport_offset(skb) +
1736 ETH_FCS_LEN;
1737 else /* TSO */ {
1738 mss_mfs = skb_shinfo(skb)->gso_size;
1739 /* TCP Sequence Number Source Select
1740 * can be set only for TSO
1741 */
1742 seq_ctrl = 0;
1743 }
1744 }
1745
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001746 do {
1747 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001748 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001749 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001750 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001751
1752 ctrl = (u32)tx_skb->size;
1753 if (eof) {
1754 ctrl |= MACB_BIT(TX_LAST);
1755 eof = 0;
1756 }
Zach Brownb410d132016-10-19 09:56:57 -05001757 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001758 ctrl |= MACB_BIT(TX_WRAP);
1759
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001760 /* First descriptor is header descriptor */
1761 if (i == queue->tx_head) {
1762 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1763 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001764 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1765 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1766 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001767 } else
1768 /* Only set MSS/MFS on payload descriptors
1769 * (second or later descriptor)
1770 */
1771 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1772
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001773 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001774 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001775 /* desc->addr must be visible to hardware before clearing
1776 * 'TX_USED' bit in desc->ctrl.
1777 */
1778 wmb();
1779 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001780 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001781
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001782 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001783
1784 return count;
1785
1786dma_error:
1787 netdev_err(bp->dev, "TX DMA map failed\n");
1788
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001789 for (i = queue->tx_head; i != tx_head; i++) {
1790 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001791
1792 macb_tx_unmap(bp, tx_skb);
1793 }
1794
1795 return 0;
1796}
1797
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001798static netdev_features_t macb_features_check(struct sk_buff *skb,
1799 struct net_device *dev,
1800 netdev_features_t features)
1801{
1802 unsigned int nr_frags, f;
1803 unsigned int hdrlen;
1804
1805 /* Validate LSO compatibility */
1806
Harini Katakam41c1ef92020-02-05 18:08:11 +05301807 /* there is only one buffer or protocol is not UDP */
1808 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001809 return features;
1810
1811 /* length of header */
1812 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001813
Harini Katakam41c1ef92020-02-05 18:08:11 +05301814 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001815 * When software supplies two or more payload buffers all payload buffers
1816 * apart from the last must be a multiple of 8 bytes in size.
1817 */
1818 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1819 return features & ~MACB_NETIF_LSO;
1820
1821 nr_frags = skb_shinfo(skb)->nr_frags;
1822 /* No need to check last fragment */
1823 nr_frags--;
1824 for (f = 0; f < nr_frags; f++) {
1825 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1826
1827 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1828 return features & ~MACB_NETIF_LSO;
1829 }
1830 return features;
1831}
1832
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001833static inline int macb_clear_csum(struct sk_buff *skb)
1834{
1835 /* no change for packets without checksum offloading */
1836 if (skb->ip_summed != CHECKSUM_PARTIAL)
1837 return 0;
1838
1839 /* make sure we can modify the header */
1840 if (unlikely(skb_cow_head(skb, 0)))
1841 return -1;
1842
1843 /* initialize checksum field
1844 * This is required - at least for Zynq, which otherwise calculates
1845 * wrong UDP header checksums for UDP packets with UDP data len <=2
1846 */
1847 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1848 return 0;
1849}
1850
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001851static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1852{
1853 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1854 int padlen = ETH_ZLEN - (*skb)->len;
1855 int headroom = skb_headroom(*skb);
1856 int tailroom = skb_tailroom(*skb);
1857 struct sk_buff *nskb;
1858 u32 fcs;
1859
1860 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1861 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1862 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1863 return 0;
1864
1865 if (padlen <= 0) {
1866 /* FCS could be appeded to tailroom. */
1867 if (tailroom >= ETH_FCS_LEN)
1868 goto add_fcs;
1869 /* FCS could be appeded by moving data to headroom. */
1870 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1871 padlen = 0;
1872 /* No room for FCS, need to reallocate skb. */
1873 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001874 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001875 } else {
1876 /* Add room for FCS. */
1877 padlen += ETH_FCS_LEN;
1878 }
1879
1880 if (!cloned && headroom + tailroom >= padlen) {
1881 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1882 skb_set_tail_pointer(*skb, (*skb)->len);
1883 } else {
1884 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1885 if (!nskb)
1886 return -ENOMEM;
1887
Huang Zijiangf3e5c072019-02-14 14:41:18 +08001888 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001889 *skb = nskb;
1890 }
1891
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001892 if (padlen > ETH_FCS_LEN)
1893 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001894
1895add_fcs:
1896 /* set FCS to packet */
1897 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1898 fcs = ~fcs;
1899
1900 skb_put_u8(*skb, fcs & 0xff);
1901 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1902 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1903 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1904
1905 return 0;
1906}
1907
Claudiu Beznead1c38952018-08-07 12:25:12 +03001908static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001909{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001910 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001911 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001912 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001913 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001914 unsigned int desc_cnt, nr_frags, frag_size, f;
1915 unsigned int hdrlen;
1916 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001917 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001918
Claudiu Beznea33729f22018-08-07 12:25:13 +03001919 if (macb_clear_csum(skb)) {
1920 dev_kfree_skb_any(skb);
1921 return ret;
1922 }
1923
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001924 if (macb_pad_and_fcs(&skb, dev)) {
1925 dev_kfree_skb_any(skb);
1926 return ret;
1927 }
1928
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001929 is_lso = (skb_shinfo(skb)->gso_size != 0);
1930
1931 if (is_lso) {
1932 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1933
1934 /* length of headers */
1935 if (is_udp)
1936 /* only queue eth + ip headers separately for UDP */
1937 hdrlen = skb_transport_offset(skb);
1938 else
1939 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1940 if (skb_headlen(skb) < hdrlen) {
1941 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1942 /* if this is required, would need to copy to single buffer */
1943 return NETDEV_TX_BUSY;
1944 }
1945 } else
1946 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001947
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001948#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1949 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001950 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1951 queue_index, skb->len, skb->head, skb->data,
1952 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001953 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1954 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001955#endif
1956
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001957 /* Count how many TX buffer descriptors are needed to send this
1958 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001959 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001960 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001961 if (is_lso && (skb_headlen(skb) > hdrlen))
1962 /* extra header descriptor if also payload in first buffer */
1963 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1964 else
1965 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001966 nr_frags = skb_shinfo(skb)->nr_frags;
1967 for (f = 0; f < nr_frags; f++) {
1968 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001969 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001970 }
1971
Dongdong Deng48719532009-08-23 19:49:07 -07001972 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001973
1974 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001975 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001976 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001977 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001978 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001979 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001980 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001981 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001982 }
1983
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001984 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001985 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001986 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001987 goto unlock;
1988 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001989
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001990 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001991 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001992 skb_tx_timestamp(skb);
1993
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001994 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1995
Zach Brownb410d132016-10-19 09:56:57 -05001996 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001997 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001998
Soren Brinkmann92030902014-03-04 08:46:39 -08001999unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002000 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002001
Claudiu Beznead1c38952018-08-07 12:25:12 +03002002 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002003}
2004
Nicolas Ferre4df95132013-06-04 21:57:12 +00002005static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002006{
2007 if (!macb_is_gem(bp)) {
2008 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2009 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002010 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002011
Nicolas Ferre1b447912013-06-04 21:57:11 +00002012 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002013 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002014 "RX buffer must be multiple of %d bytes, expanding\n",
2015 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002016 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002017 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002018 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002019 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002020
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002021 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002022 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002023}
2024
Nicolas Ferre4df95132013-06-04 21:57:12 +00002025static void gem_free_rx_buffers(struct macb *bp)
2026{
2027 struct sk_buff *skb;
2028 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002029 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002030 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002031 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002032 int i;
2033
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002034 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2035 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002036 continue;
2037
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002038 for (i = 0; i < bp->rx_ring_size; i++) {
2039 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002040
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002041 if (!skb)
2042 continue;
2043
2044 desc = macb_rx_desc(queue, i);
2045 addr = macb_get_addr(bp, desc);
2046
2047 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2048 DMA_FROM_DEVICE);
2049 dev_kfree_skb_any(skb);
2050 skb = NULL;
2051 }
2052
2053 kfree(queue->rx_skbuff);
2054 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002055 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002056}
2057
2058static void macb_free_rx_buffers(struct macb *bp)
2059{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002060 struct macb_queue *queue = &bp->queues[0];
2061
2062 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002063 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002064 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002065 queue->rx_buffers, queue->rx_buffers_dma);
2066 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002067 }
2068}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002069
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002070static void macb_free_consistent(struct macb *bp)
2071{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002072 struct macb_queue *queue;
2073 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302074 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002075
Nicolas Ferre4df95132013-06-04 21:57:12 +00002076 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002077
2078 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2079 kfree(queue->tx_skb);
2080 queue->tx_skb = NULL;
2081 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302082 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2083 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002084 queue->tx_ring, queue->tx_ring_dma);
2085 queue->tx_ring = NULL;
2086 }
Harini Katakame50b7702018-07-06 12:18:57 +05302087 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302088 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2089 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302090 queue->rx_ring, queue->rx_ring_dma);
2091 queue->rx_ring = NULL;
2092 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002093 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002094}
2095
2096static int gem_alloc_rx_buffers(struct macb *bp)
2097{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002098 struct macb_queue *queue;
2099 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002100 int size;
2101
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002102 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2103 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2104 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2105 if (!queue->rx_skbuff)
2106 return -ENOMEM;
2107 else
2108 netdev_dbg(bp->dev,
2109 "Allocated %d RX struct sk_buff entries at %p\n",
2110 bp->rx_ring_size, queue->rx_skbuff);
2111 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002112 return 0;
2113}
2114
2115static int macb_alloc_rx_buffers(struct macb *bp)
2116{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002117 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002118 int size;
2119
Zach Brownb410d132016-10-19 09:56:57 -05002120 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002121 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2122 &queue->rx_buffers_dma, GFP_KERNEL);
2123 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002124 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002125
2126 netdev_dbg(bp->dev,
2127 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002128 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002129 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002130}
2131
2132static int macb_alloc_consistent(struct macb *bp)
2133{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002134 struct macb_queue *queue;
2135 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002136 int size;
2137
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002138 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302139 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002140 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2141 &queue->tx_ring_dma,
2142 GFP_KERNEL);
2143 if (!queue->tx_ring)
2144 goto out_err;
2145 netdev_dbg(bp->dev,
2146 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2147 q, size, (unsigned long)queue->tx_ring_dma,
2148 queue->tx_ring);
2149
Zach Brownb410d132016-10-19 09:56:57 -05002150 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002151 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2152 if (!queue->tx_skb)
2153 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002154
Harini Katakam404cd082018-07-06 12:18:58 +05302155 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002156 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2157 &queue->rx_ring_dma, GFP_KERNEL);
2158 if (!queue->rx_ring)
2159 goto out_err;
2160 netdev_dbg(bp->dev,
2161 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2162 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002163 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002164 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002165 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002166
2167 return 0;
2168
2169out_err:
2170 macb_free_consistent(bp);
2171 return -ENOMEM;
2172}
2173
Nicolas Ferre4df95132013-06-04 21:57:12 +00002174static void gem_init_rings(struct macb *bp)
2175{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002176 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002177 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002178 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002179 int i;
2180
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002181 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002182 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002183 desc = macb_tx_desc(queue, i);
2184 macb_set_addr(bp, desc, 0);
2185 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002186 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002187 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002188 queue->tx_head = 0;
2189 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002190
2191 queue->rx_tail = 0;
2192 queue->rx_prepared_head = 0;
2193
2194 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002195 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002196
Nicolas Ferre4df95132013-06-04 21:57:12 +00002197}
2198
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002199static void macb_init_rings(struct macb *bp)
2200{
2201 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002202 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002203
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002204 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002205
Zach Brownb410d132016-10-19 09:56:57 -05002206 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002207 desc = macb_tx_desc(&bp->queues[0], i);
2208 macb_set_addr(bp, desc, 0);
2209 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002210 }
Ben Shelton21d35152015-04-22 17:28:54 -05002211 bp->queues[0].tx_head = 0;
2212 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002213 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002214}
2215
2216static void macb_reset_hw(struct macb *bp)
2217{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002218 struct macb_queue *queue;
2219 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002220 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002221
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002222 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223 * more gracefully?)
2224 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002225 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002226
2227 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002228 ctrl |= MACB_BIT(CLRSTAT);
2229
2230 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002231
2232 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002233 macb_writel(bp, TSR, -1);
2234 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002235
2236 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002237 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2238 queue_writel(queue, IDR, -1);
2239 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002240 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2241 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002242 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002243}
2244
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002245static u32 gem_mdc_clk_div(struct macb *bp)
2246{
2247 u32 config;
2248 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2249
2250 if (pclk_hz <= 20000000)
2251 config = GEM_BF(CLK, GEM_CLK_DIV8);
2252 else if (pclk_hz <= 40000000)
2253 config = GEM_BF(CLK, GEM_CLK_DIV16);
2254 else if (pclk_hz <= 80000000)
2255 config = GEM_BF(CLK, GEM_CLK_DIV32);
2256 else if (pclk_hz <= 120000000)
2257 config = GEM_BF(CLK, GEM_CLK_DIV48);
2258 else if (pclk_hz <= 160000000)
2259 config = GEM_BF(CLK, GEM_CLK_DIV64);
2260 else
2261 config = GEM_BF(CLK, GEM_CLK_DIV96);
2262
2263 return config;
2264}
2265
2266static u32 macb_mdc_clk_div(struct macb *bp)
2267{
2268 u32 config;
2269 unsigned long pclk_hz;
2270
2271 if (macb_is_gem(bp))
2272 return gem_mdc_clk_div(bp);
2273
2274 pclk_hz = clk_get_rate(bp->pclk);
2275 if (pclk_hz <= 20000000)
2276 config = MACB_BF(CLK, MACB_CLK_DIV8);
2277 else if (pclk_hz <= 40000000)
2278 config = MACB_BF(CLK, MACB_CLK_DIV16);
2279 else if (pclk_hz <= 80000000)
2280 config = MACB_BF(CLK, MACB_CLK_DIV32);
2281 else
2282 config = MACB_BF(CLK, MACB_CLK_DIV64);
2283
2284 return config;
2285}
2286
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002287/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002288 * should program. We find the width from decoding the design configuration
2289 * register to find the maximum supported data bus width.
2290 */
2291static u32 macb_dbw(struct macb *bp)
2292{
2293 if (!macb_is_gem(bp))
2294 return 0;
2295
2296 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2297 case 4:
2298 return GEM_BF(DBW, GEM_DBW128);
2299 case 2:
2300 return GEM_BF(DBW, GEM_DBW64);
2301 case 1:
2302 default:
2303 return GEM_BF(DBW, GEM_DBW32);
2304 }
2305}
2306
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002307/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002308 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002309 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002310 * (if not supported by FIFO, it will fallback to default)
2311 * - set both rx/tx packet buffers to full memory size
2312 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002313 */
2314static void macb_configure_dma(struct macb *bp)
2315{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002316 struct macb_queue *queue;
2317 u32 buffer_size;
2318 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002319 u32 dmacfg;
2320
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002321 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002322 if (macb_is_gem(bp)) {
2323 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002324 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2325 if (q)
2326 queue_writel(queue, RBQS, buffer_size);
2327 else
2328 dmacfg |= GEM_BF(RXBS, buffer_size);
2329 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002330 if (bp->dma_burst_length)
2331 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002332 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302333 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302334
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002335 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302336 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2337 else
2338 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2339
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002340 if (bp->dev->features & NETIF_F_HW_CSUM)
2341 dmacfg |= GEM_BIT(TXCOEN);
2342 else
2343 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302344
Michal Simekbd620722018-09-25 08:32:50 +02002345 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302346#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002347 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002348 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302349#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002350#ifdef CONFIG_MACB_USE_HWSTAMP
2351 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2352 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2353#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002354 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2355 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002356 gem_writel(bp, DMACFG, dmacfg);
2357 }
2358}
2359
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002360static void macb_init_hw(struct macb *bp)
2361{
2362 u32 config;
2363
2364 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002365 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002366
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002367 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002368 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002369 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002370 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302371 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2372 else
2373 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002374 if (bp->dev->flags & IFF_PROMISC)
2375 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002376 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2377 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002378 if (!(bp->dev->flags & IFF_BROADCAST))
2379 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002380 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002381 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002382 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302383 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302384 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002385 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302386 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002387
Jamie Iles0116da42011-03-14 17:38:30 +00002388 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002389}
2390
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002391/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002392 * locations in the memory map. The least significant bits are stored
2393 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2394 *
2395 * The unicast hash enable and the multicast hash enable bits in the
2396 * network configuration register enable the reception of hash matched
2397 * frames. The destination address is reduced to a 6 bit index into
2398 * the 64 bit hash register using the following hash function. The
2399 * hash function is an exclusive or of every sixth bit of the
2400 * destination address.
2401 *
2402 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2403 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2404 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2405 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2406 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2407 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2408 *
2409 * da[0] represents the least significant bit of the first byte
2410 * received, that is, the multicast/unicast indicator, and da[47]
2411 * represents the most significant bit of the last byte received. If
2412 * the hash index, hi[n], points to a bit that is set in the hash
2413 * register then the frame will be matched according to whether the
2414 * frame is multicast or unicast. A multicast match will be signalled
2415 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2416 * index points to a bit set in the hash register. A unicast match
2417 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2418 * and the hash index points to a bit set in the hash register. To
2419 * receive all multicast frames, the hash register should be set with
2420 * all ones and the multicast hash enable bit should be set in the
2421 * network configuration register.
2422 */
2423
2424static inline int hash_bit_value(int bitnr, __u8 *addr)
2425{
2426 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2427 return 1;
2428 return 0;
2429}
2430
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002431/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002432static int hash_get_index(__u8 *addr)
2433{
2434 int i, j, bitval;
2435 int hash_index = 0;
2436
2437 for (j = 0; j < 6; j++) {
2438 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002439 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002440
2441 hash_index |= (bitval << j);
2442 }
2443
2444 return hash_index;
2445}
2446
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002447/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002448static void macb_sethashtable(struct net_device *dev)
2449{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002450 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002451 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002452 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002453 struct macb *bp = netdev_priv(dev);
2454
Moritz Fischeraa50b552016-03-29 19:11:13 -07002455 mc_filter[0] = 0;
2456 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002457
Jiri Pirko22bedad32010-04-01 21:22:57 +00002458 netdev_for_each_mc_addr(ha, dev) {
2459 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002460 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2461 }
2462
Jamie Ilesf75ba502011-11-08 10:12:32 +00002463 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2464 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002465}
2466
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002467/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002468static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002469{
2470 unsigned long cfg;
2471 struct macb *bp = netdev_priv(dev);
2472
2473 cfg = macb_readl(bp, NCFGR);
2474
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002475 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002476 /* Enable promiscuous mode */
2477 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002478
2479 /* Disable RX checksum offload */
2480 if (macb_is_gem(bp))
2481 cfg &= ~GEM_BIT(RXCOEN);
2482 } else {
2483 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002484 cfg &= ~MACB_BIT(CAF);
2485
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002486 /* Enable RX checksum offload only if requested */
2487 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2488 cfg |= GEM_BIT(RXCOEN);
2489 }
2490
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002491 if (dev->flags & IFF_ALLMULTI) {
2492 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002493 macb_or_gem_writel(bp, HRB, -1);
2494 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002495 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002496 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002497 /* Enable specific multicasts */
2498 macb_sethashtable(dev);
2499 cfg |= MACB_BIT(NCFGR_MTI);
2500 } else if (dev->flags & (~IFF_ALLMULTI)) {
2501 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002502 macb_or_gem_writel(bp, HRB, 0);
2503 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002504 cfg &= ~MACB_BIT(NCFGR_MTI);
2505 }
2506
2507 macb_writel(bp, NCFGR, cfg);
2508}
2509
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002510static int macb_open(struct net_device *dev)
2511{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002512 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002513 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002514 struct macb_queue *queue;
2515 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002516 int err;
2517
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002518 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002519
Harini Katakamd54f89a2019-03-01 16:20:34 +05302520 err = pm_runtime_get_sync(&bp->pdev->dev);
2521 if (err < 0)
2522 goto pm_exit;
2523
Nicolas Ferre1b447912013-06-04 21:57:11 +00002524 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002525 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002526
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002527 err = macb_alloc_consistent(bp);
2528 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002529 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2530 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302531 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002532 }
2533
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002534 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2535 napi_enable(&queue->napi);
2536
Harini Katakam05044532019-05-07 19:59:10 +05302537 macb_init_hw(bp);
2538
Antoine Tenart7897b072019-11-13 10:00:06 +01002539 err = macb_phylink_connect(bp);
2540 if (err)
2541 goto pm_exit;
frederic RODO6c36a702007-07-12 19:07:24 +02002542
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002543 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002544
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002545 if (bp->ptp_info)
2546 bp->ptp_info->ptp_init(dev);
2547
Harini Katakamd54f89a2019-03-01 16:20:34 +05302548pm_exit:
2549 if (err) {
2550 pm_runtime_put_sync(&bp->pdev->dev);
2551 return err;
2552 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002553 return 0;
2554}
2555
2556static int macb_close(struct net_device *dev)
2557{
2558 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002559 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002560 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002561 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002562
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002563 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002564
2565 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2566 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002567
Antoine Tenart7897b072019-11-13 10:00:06 +01002568 phylink_stop(bp->phylink);
2569 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002570
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002571 spin_lock_irqsave(&bp->lock, flags);
2572 macb_reset_hw(bp);
2573 netif_carrier_off(dev);
2574 spin_unlock_irqrestore(&bp->lock, flags);
2575
2576 macb_free_consistent(bp);
2577
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002578 if (bp->ptp_info)
2579 bp->ptp_info->ptp_remove(dev);
2580
Harini Katakamd54f89a2019-03-01 16:20:34 +05302581 pm_runtime_put(&bp->pdev->dev);
2582
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002583 return 0;
2584}
2585
Harini Katakama5898ea2015-05-06 22:27:18 +05302586static int macb_change_mtu(struct net_device *dev, int new_mtu)
2587{
Harini Katakama5898ea2015-05-06 22:27:18 +05302588 if (netif_running(dev))
2589 return -EBUSY;
2590
Harini Katakama5898ea2015-05-06 22:27:18 +05302591 dev->mtu = new_mtu;
2592
2593 return 0;
2594}
2595
Jamie Ilesa494ed82011-03-09 16:26:35 +00002596static void gem_update_stats(struct macb *bp)
2597{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002598 struct macb_queue *queue;
2599 unsigned int i, q, idx;
2600 unsigned long *stat;
2601
Jamie Ilesa494ed82011-03-09 16:26:35 +00002602 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002603
Xander Huff3ff13f12015-01-13 16:15:51 -06002604 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2605 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002606 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002607
2608 bp->ethtool_stats[i] += val;
2609 *p += val;
2610
2611 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2612 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002613 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002614 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002615 *(++p) += val;
2616 }
2617 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002618
2619 idx = GEM_STATS_LEN;
2620 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2621 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2622 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002623}
2624
2625static struct net_device_stats *gem_get_stats(struct macb *bp)
2626{
2627 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002628 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002629
2630 gem_update_stats(bp);
2631
2632 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2633 hwstat->rx_alignment_errors +
2634 hwstat->rx_resource_errors +
2635 hwstat->rx_overruns +
2636 hwstat->rx_oversize_frames +
2637 hwstat->rx_jabbers +
2638 hwstat->rx_undersized_frames +
2639 hwstat->rx_length_field_frame_errors);
2640 nstat->tx_errors = (hwstat->tx_late_collisions +
2641 hwstat->tx_excessive_collisions +
2642 hwstat->tx_underrun +
2643 hwstat->tx_carrier_sense_errors);
2644 nstat->multicast = hwstat->rx_multicast_frames;
2645 nstat->collisions = (hwstat->tx_single_collision_frames +
2646 hwstat->tx_multiple_collision_frames +
2647 hwstat->tx_excessive_collisions);
2648 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2649 hwstat->rx_jabbers +
2650 hwstat->rx_undersized_frames +
2651 hwstat->rx_length_field_frame_errors);
2652 nstat->rx_over_errors = hwstat->rx_resource_errors;
2653 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2654 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2655 nstat->rx_fifo_errors = hwstat->rx_overruns;
2656 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2657 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2658 nstat->tx_fifo_errors = hwstat->tx_underrun;
2659
2660 return nstat;
2661}
2662
Xander Huff3ff13f12015-01-13 16:15:51 -06002663static void gem_get_ethtool_stats(struct net_device *dev,
2664 struct ethtool_stats *stats, u64 *data)
2665{
2666 struct macb *bp;
2667
2668 bp = netdev_priv(dev);
2669 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002670 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2671 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002672}
2673
2674static int gem_get_sset_count(struct net_device *dev, int sset)
2675{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002676 struct macb *bp = netdev_priv(dev);
2677
Xander Huff3ff13f12015-01-13 16:15:51 -06002678 switch (sset) {
2679 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002680 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002681 default:
2682 return -EOPNOTSUPP;
2683 }
2684}
2685
2686static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2687{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002688 char stat_string[ETH_GSTRING_LEN];
2689 struct macb *bp = netdev_priv(dev);
2690 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002691 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002692 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002693
2694 switch (sset) {
2695 case ETH_SS_STATS:
2696 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2697 memcpy(p, gem_statistics[i].stat_string,
2698 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002699
2700 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2701 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2702 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2703 q, queue_statistics[i].stat_string);
2704 memcpy(p, stat_string, ETH_GSTRING_LEN);
2705 }
2706 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002707 break;
2708 }
2709}
2710
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002711static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002712{
2713 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002714 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002715 struct macb_stats *hwstat = &bp->hw_stats.macb;
2716
2717 if (macb_is_gem(bp))
2718 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002719
frederic RODO6c36a702007-07-12 19:07:24 +02002720 /* read stats from hardware */
2721 macb_update_stats(bp);
2722
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002723 /* Convert HW stats into netdevice stats */
2724 nstat->rx_errors = (hwstat->rx_fcs_errors +
2725 hwstat->rx_align_errors +
2726 hwstat->rx_resource_errors +
2727 hwstat->rx_overruns +
2728 hwstat->rx_oversize_pkts +
2729 hwstat->rx_jabbers +
2730 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002731 hwstat->rx_length_mismatch);
2732 nstat->tx_errors = (hwstat->tx_late_cols +
2733 hwstat->tx_excessive_cols +
2734 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002735 hwstat->tx_carrier_errors +
2736 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002737 nstat->collisions = (hwstat->tx_single_cols +
2738 hwstat->tx_multiple_cols +
2739 hwstat->tx_excessive_cols);
2740 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2741 hwstat->rx_jabbers +
2742 hwstat->rx_undersize_pkts +
2743 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002744 nstat->rx_over_errors = hwstat->rx_resource_errors +
2745 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002746 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2747 nstat->rx_frame_errors = hwstat->rx_align_errors;
2748 nstat->rx_fifo_errors = hwstat->rx_overruns;
2749 /* XXX: What does "missed" mean? */
2750 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2751 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2752 nstat->tx_fifo_errors = hwstat->tx_underruns;
2753 /* Don't know about heartbeat or window errors... */
2754
2755 return nstat;
2756}
2757
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002758static int macb_get_regs_len(struct net_device *netdev)
2759{
2760 return MACB_GREGS_NBR * sizeof(u32);
2761}
2762
2763static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2764 void *p)
2765{
2766 struct macb *bp = netdev_priv(dev);
2767 unsigned int tail, head;
2768 u32 *regs_buff = p;
2769
2770 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2771 | MACB_GREGS_VERSION;
2772
Zach Brownb410d132016-10-19 09:56:57 -05002773 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2774 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002775
2776 regs_buff[0] = macb_readl(bp, NCR);
2777 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2778 regs_buff[2] = macb_readl(bp, NSR);
2779 regs_buff[3] = macb_readl(bp, TSR);
2780 regs_buff[4] = macb_readl(bp, RBQP);
2781 regs_buff[5] = macb_readl(bp, TBQP);
2782 regs_buff[6] = macb_readl(bp, RSR);
2783 regs_buff[7] = macb_readl(bp, IMR);
2784
2785 regs_buff[8] = tail;
2786 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002787 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2788 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002789
Neil Armstrongce721a72016-01-05 14:39:16 +01002790 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2791 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002792 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002793 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002794}
2795
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002796static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2797{
2798 struct macb *bp = netdev_priv(netdev);
2799
2800 wol->supported = 0;
2801 wol->wolopts = 0;
2802
Antoine Tenart7897b072019-11-13 10:00:06 +01002803 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET)
2804 phylink_ethtool_get_wol(bp->phylink, wol);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002805}
2806
2807static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2808{
2809 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01002810 int ret;
2811
2812 ret = phylink_ethtool_set_wol(bp->phylink, wol);
2813 if (!ret)
2814 return 0;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002815
2816 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2817 (wol->wolopts & ~WAKE_MAGIC))
2818 return -EOPNOTSUPP;
2819
2820 if (wol->wolopts & WAKE_MAGIC)
2821 bp->wol |= MACB_WOL_ENABLED;
2822 else
2823 bp->wol &= ~MACB_WOL_ENABLED;
2824
2825 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2826
2827 return 0;
2828}
2829
Antoine Tenart7897b072019-11-13 10:00:06 +01002830static int macb_get_link_ksettings(struct net_device *netdev,
2831 struct ethtool_link_ksettings *kset)
2832{
2833 struct macb *bp = netdev_priv(netdev);
2834
2835 return phylink_ethtool_ksettings_get(bp->phylink, kset);
2836}
2837
2838static int macb_set_link_ksettings(struct net_device *netdev,
2839 const struct ethtool_link_ksettings *kset)
2840{
2841 struct macb *bp = netdev_priv(netdev);
2842
2843 return phylink_ethtool_ksettings_set(bp->phylink, kset);
2844}
2845
Zach Brown8441bb32016-10-19 09:56:58 -05002846static void macb_get_ringparam(struct net_device *netdev,
2847 struct ethtool_ringparam *ring)
2848{
2849 struct macb *bp = netdev_priv(netdev);
2850
2851 ring->rx_max_pending = MAX_RX_RING_SIZE;
2852 ring->tx_max_pending = MAX_TX_RING_SIZE;
2853
2854 ring->rx_pending = bp->rx_ring_size;
2855 ring->tx_pending = bp->tx_ring_size;
2856}
2857
2858static int macb_set_ringparam(struct net_device *netdev,
2859 struct ethtool_ringparam *ring)
2860{
2861 struct macb *bp = netdev_priv(netdev);
2862 u32 new_rx_size, new_tx_size;
2863 unsigned int reset = 0;
2864
2865 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2866 return -EINVAL;
2867
2868 new_rx_size = clamp_t(u32, ring->rx_pending,
2869 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2870 new_rx_size = roundup_pow_of_two(new_rx_size);
2871
2872 new_tx_size = clamp_t(u32, ring->tx_pending,
2873 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2874 new_tx_size = roundup_pow_of_two(new_tx_size);
2875
2876 if ((new_tx_size == bp->tx_ring_size) &&
2877 (new_rx_size == bp->rx_ring_size)) {
2878 /* nothing to do */
2879 return 0;
2880 }
2881
2882 if (netif_running(bp->dev)) {
2883 reset = 1;
2884 macb_close(bp->dev);
2885 }
2886
2887 bp->rx_ring_size = new_rx_size;
2888 bp->tx_ring_size = new_tx_size;
2889
2890 if (reset)
2891 macb_open(bp->dev);
2892
2893 return 0;
2894}
2895
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002896#ifdef CONFIG_MACB_USE_HWSTAMP
2897static unsigned int gem_get_tsu_rate(struct macb *bp)
2898{
2899 struct clk *tsu_clk;
2900 unsigned int tsu_rate;
2901
2902 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2903 if (!IS_ERR(tsu_clk))
2904 tsu_rate = clk_get_rate(tsu_clk);
2905 /* try pclk instead */
2906 else if (!IS_ERR(bp->pclk)) {
2907 tsu_clk = bp->pclk;
2908 tsu_rate = clk_get_rate(tsu_clk);
2909 } else
2910 return -ENOTSUPP;
2911 return tsu_rate;
2912}
2913
2914static s32 gem_get_ptp_max_adj(void)
2915{
2916 return 64000000;
2917}
2918
2919static int gem_get_ts_info(struct net_device *dev,
2920 struct ethtool_ts_info *info)
2921{
2922 struct macb *bp = netdev_priv(dev);
2923
2924 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2925 ethtool_op_get_ts_info(dev, info);
2926 return 0;
2927 }
2928
2929 info->so_timestamping =
2930 SOF_TIMESTAMPING_TX_SOFTWARE |
2931 SOF_TIMESTAMPING_RX_SOFTWARE |
2932 SOF_TIMESTAMPING_SOFTWARE |
2933 SOF_TIMESTAMPING_TX_HARDWARE |
2934 SOF_TIMESTAMPING_RX_HARDWARE |
2935 SOF_TIMESTAMPING_RAW_HARDWARE;
2936 info->tx_types =
2937 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2938 (1 << HWTSTAMP_TX_OFF) |
2939 (1 << HWTSTAMP_TX_ON);
2940 info->rx_filters =
2941 (1 << HWTSTAMP_FILTER_NONE) |
2942 (1 << HWTSTAMP_FILTER_ALL);
2943
2944 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2945
2946 return 0;
2947}
2948
2949static struct macb_ptp_info gem_ptp_info = {
2950 .ptp_init = gem_ptp_init,
2951 .ptp_remove = gem_ptp_remove,
2952 .get_ptp_max_adj = gem_get_ptp_max_adj,
2953 .get_tsu_rate = gem_get_tsu_rate,
2954 .get_ts_info = gem_get_ts_info,
2955 .get_hwtst = gem_get_hwtst,
2956 .set_hwtst = gem_set_hwtst,
2957};
2958#endif
2959
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002960static int macb_get_ts_info(struct net_device *netdev,
2961 struct ethtool_ts_info *info)
2962{
2963 struct macb *bp = netdev_priv(netdev);
2964
2965 if (bp->ptp_info)
2966 return bp->ptp_info->get_ts_info(netdev, info);
2967
2968 return ethtool_op_get_ts_info(netdev, info);
2969}
2970
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002971static void gem_enable_flow_filters(struct macb *bp, bool enable)
2972{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002973 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002974 struct ethtool_rx_fs_item *item;
2975 u32 t2_scr;
2976 int num_t2_scr;
2977
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002978 if (!(netdev->features & NETIF_F_NTUPLE))
2979 return;
2980
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002981 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2982
2983 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2984 struct ethtool_rx_flow_spec *fs = &item->fs;
2985 struct ethtool_tcpip4_spec *tp4sp_m;
2986
2987 if (fs->location >= num_t2_scr)
2988 continue;
2989
2990 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2991
2992 /* enable/disable screener regs for the flow entry */
2993 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2994
2995 /* only enable fields with no masking */
2996 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2997
2998 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2999 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3000 else
3001 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3002
3003 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3004 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3005 else
3006 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3007
3008 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3009 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3010 else
3011 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3012
3013 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3014 }
3015}
3016
3017static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3018{
3019 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3020 uint16_t index = fs->location;
3021 u32 w0, w1, t2_scr;
3022 bool cmp_a = false;
3023 bool cmp_b = false;
3024 bool cmp_c = false;
3025
3026 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3027 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3028
3029 /* ignore field if any masking set */
3030 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3031 /* 1st compare reg - IP source address */
3032 w0 = 0;
3033 w1 = 0;
3034 w0 = tp4sp_v->ip4src;
3035 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3036 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3037 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3038 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3039 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3040 cmp_a = true;
3041 }
3042
3043 /* ignore field if any masking set */
3044 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3045 /* 2nd compare reg - IP destination address */
3046 w0 = 0;
3047 w1 = 0;
3048 w0 = tp4sp_v->ip4dst;
3049 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3050 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3051 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3052 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3053 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3054 cmp_b = true;
3055 }
3056
3057 /* ignore both port fields if masking set in both */
3058 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3059 /* 3rd compare reg - source port, destination port */
3060 w0 = 0;
3061 w1 = 0;
3062 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3063 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3064 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3065 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3066 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3067 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3068 } else {
3069 /* only one port definition */
3070 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3071 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3072 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3073 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3074 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3075 } else { /* dst port */
3076 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3077 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3078 }
3079 }
3080 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3081 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3082 cmp_c = true;
3083 }
3084
3085 t2_scr = 0;
3086 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3087 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3088 if (cmp_a)
3089 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3090 if (cmp_b)
3091 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3092 if (cmp_c)
3093 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3094 gem_writel_n(bp, SCRT2, index, t2_scr);
3095}
3096
3097static int gem_add_flow_filter(struct net_device *netdev,
3098 struct ethtool_rxnfc *cmd)
3099{
3100 struct macb *bp = netdev_priv(netdev);
3101 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3102 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003103 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003104 int ret = -EINVAL;
3105 bool added = false;
3106
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003107 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003108 if (newfs == NULL)
3109 return -ENOMEM;
3110 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3111
3112 netdev_dbg(netdev,
3113 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3114 fs->flow_type, (int)fs->ring_cookie, fs->location,
3115 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3116 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3117 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3118
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003119 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3120
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003121 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003122 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3123 if (item->fs.location > newfs->fs.location) {
3124 list_add_tail(&newfs->list, &item->list);
3125 added = true;
3126 break;
3127 } else if (item->fs.location == fs->location) {
3128 netdev_err(netdev, "Rule not added: location %d not free!\n",
3129 fs->location);
3130 ret = -EBUSY;
3131 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003132 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003133 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003134 if (!added)
3135 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003136
3137 gem_prog_cmp_regs(bp, fs);
3138 bp->rx_fs_list.count++;
3139 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003140 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003141
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003142 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003143 return 0;
3144
3145err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003146 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003147 kfree(newfs);
3148 return ret;
3149}
3150
3151static int gem_del_flow_filter(struct net_device *netdev,
3152 struct ethtool_rxnfc *cmd)
3153{
3154 struct macb *bp = netdev_priv(netdev);
3155 struct ethtool_rx_fs_item *item;
3156 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003157 unsigned long flags;
3158
3159 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003160
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003161 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3162 if (item->fs.location == cmd->fs.location) {
3163 /* disable screener regs for the flow entry */
3164 fs = &(item->fs);
3165 netdev_dbg(netdev,
3166 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3167 fs->flow_type, (int)fs->ring_cookie, fs->location,
3168 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3169 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3170 htons(fs->h_u.tcp_ip4_spec.psrc),
3171 htons(fs->h_u.tcp_ip4_spec.pdst));
3172
3173 gem_writel_n(bp, SCRT2, fs->location, 0);
3174
3175 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003176 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003177 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3178 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003179 return 0;
3180 }
3181 }
3182
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003183 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003184 return -EINVAL;
3185}
3186
3187static int gem_get_flow_entry(struct net_device *netdev,
3188 struct ethtool_rxnfc *cmd)
3189{
3190 struct macb *bp = netdev_priv(netdev);
3191 struct ethtool_rx_fs_item *item;
3192
3193 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3194 if (item->fs.location == cmd->fs.location) {
3195 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3196 return 0;
3197 }
3198 }
3199 return -EINVAL;
3200}
3201
3202static int gem_get_all_flow_entries(struct net_device *netdev,
3203 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3204{
3205 struct macb *bp = netdev_priv(netdev);
3206 struct ethtool_rx_fs_item *item;
3207 uint32_t cnt = 0;
3208
3209 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3210 if (cnt == cmd->rule_cnt)
3211 return -EMSGSIZE;
3212 rule_locs[cnt] = item->fs.location;
3213 cnt++;
3214 }
3215 cmd->data = bp->max_tuples;
3216 cmd->rule_cnt = cnt;
3217
3218 return 0;
3219}
3220
3221static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3222 u32 *rule_locs)
3223{
3224 struct macb *bp = netdev_priv(netdev);
3225 int ret = 0;
3226
3227 switch (cmd->cmd) {
3228 case ETHTOOL_GRXRINGS:
3229 cmd->data = bp->num_queues;
3230 break;
3231 case ETHTOOL_GRXCLSRLCNT:
3232 cmd->rule_cnt = bp->rx_fs_list.count;
3233 break;
3234 case ETHTOOL_GRXCLSRULE:
3235 ret = gem_get_flow_entry(netdev, cmd);
3236 break;
3237 case ETHTOOL_GRXCLSRLALL:
3238 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3239 break;
3240 default:
3241 netdev_err(netdev,
3242 "Command parameter %d is not supported\n", cmd->cmd);
3243 ret = -EOPNOTSUPP;
3244 }
3245
3246 return ret;
3247}
3248
3249static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3250{
3251 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003252 int ret;
3253
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003254 switch (cmd->cmd) {
3255 case ETHTOOL_SRXCLSRLINS:
3256 if ((cmd->fs.location >= bp->max_tuples)
3257 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3258 ret = -EINVAL;
3259 break;
3260 }
3261 ret = gem_add_flow_filter(netdev, cmd);
3262 break;
3263 case ETHTOOL_SRXCLSRLDEL:
3264 ret = gem_del_flow_filter(netdev, cmd);
3265 break;
3266 default:
3267 netdev_err(netdev,
3268 "Command parameter %d is not supported\n", cmd->cmd);
3269 ret = -EOPNOTSUPP;
3270 }
3271
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003272 return ret;
3273}
3274
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003275static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003276 .get_regs_len = macb_get_regs_len,
3277 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003278 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003279 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003280 .get_wol = macb_get_wol,
3281 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003282 .get_link_ksettings = macb_get_link_ksettings,
3283 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003284 .get_ringparam = macb_get_ringparam,
3285 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003286};
Xander Huff8cd5a562015-01-15 15:55:20 -06003287
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003288static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003289 .get_regs_len = macb_get_regs_len,
3290 .get_regs = macb_get_regs,
3291 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003292 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003293 .get_ethtool_stats = gem_get_ethtool_stats,
3294 .get_strings = gem_get_ethtool_strings,
3295 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003296 .get_link_ksettings = macb_get_link_ksettings,
3297 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003298 .get_ringparam = macb_get_ringparam,
3299 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003300 .get_rxnfc = gem_get_rxnfc,
3301 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003302};
3303
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003304static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003305{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003306 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003307
3308 if (!netif_running(dev))
3309 return -EINVAL;
3310
Antoine Tenart7897b072019-11-13 10:00:06 +01003311 if (bp->ptp_info) {
3312 switch (cmd) {
3313 case SIOCSHWTSTAMP:
3314 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3315 case SIOCGHWTSTAMP:
3316 return bp->ptp_info->get_hwtst(dev, rq);
3317 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003318 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003319
3320 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003321}
3322
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003323static inline void macb_set_txcsum_feature(struct macb *bp,
3324 netdev_features_t features)
3325{
3326 u32 val;
3327
3328 if (!macb_is_gem(bp))
3329 return;
3330
3331 val = gem_readl(bp, DMACFG);
3332 if (features & NETIF_F_HW_CSUM)
3333 val |= GEM_BIT(TXCOEN);
3334 else
3335 val &= ~GEM_BIT(TXCOEN);
3336
3337 gem_writel(bp, DMACFG, val);
3338}
3339
3340static inline void macb_set_rxcsum_feature(struct macb *bp,
3341 netdev_features_t features)
3342{
3343 struct net_device *netdev = bp->dev;
3344 u32 val;
3345
3346 if (!macb_is_gem(bp))
3347 return;
3348
3349 val = gem_readl(bp, NCFGR);
3350 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3351 val |= GEM_BIT(RXCOEN);
3352 else
3353 val &= ~GEM_BIT(RXCOEN);
3354
3355 gem_writel(bp, NCFGR, val);
3356}
3357
3358static inline void macb_set_rxflow_feature(struct macb *bp,
3359 netdev_features_t features)
3360{
3361 if (!macb_is_gem(bp))
3362 return;
3363
3364 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3365}
3366
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003367static int macb_set_features(struct net_device *netdev,
3368 netdev_features_t features)
3369{
3370 struct macb *bp = netdev_priv(netdev);
3371 netdev_features_t changed = features ^ netdev->features;
3372
3373 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003374 if (changed & NETIF_F_HW_CSUM)
3375 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003376
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003377 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003378 if (changed & NETIF_F_RXCSUM)
3379 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003380
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003381 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003382 if (changed & NETIF_F_NTUPLE)
3383 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003384
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003385 return 0;
3386}
3387
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003388static void macb_restore_features(struct macb *bp)
3389{
3390 struct net_device *netdev = bp->dev;
3391 netdev_features_t features = netdev->features;
3392
3393 /* TX checksum offload */
3394 macb_set_txcsum_feature(bp, features);
3395
3396 /* RX checksum offload */
3397 macb_set_rxcsum_feature(bp, features);
3398
3399 /* RX Flow Filters */
3400 macb_set_rxflow_feature(bp, features);
3401}
3402
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003403static const struct net_device_ops macb_netdev_ops = {
3404 .ndo_open = macb_open,
3405 .ndo_stop = macb_close,
3406 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003407 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003408 .ndo_get_stats = macb_get_stats,
3409 .ndo_do_ioctl = macb_ioctl,
3410 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303411 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003412 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003413#ifdef CONFIG_NET_POLL_CONTROLLER
3414 .ndo_poll_controller = macb_poll_controller,
3415#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003416 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003417 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003418};
3419
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003420/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003421 * and integration options used
3422 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003423static void macb_configure_caps(struct macb *bp,
3424 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003425{
3426 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003427
Nicolas Ferref6970502015-03-31 15:02:01 +02003428 if (dt_conf)
3429 bp->caps = dt_conf->caps;
3430
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003431 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003432 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3433
Nicolas Ferree1755872014-07-24 13:50:58 +02003434 dcfg = gem_readl(bp, DCFG1);
3435 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3436 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3437 dcfg = gem_readl(bp, DCFG2);
3438 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3439 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003440#ifdef CONFIG_MACB_USE_HWSTAMP
3441 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003442 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003443 dev_err(&bp->pdev->dev,
3444 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003445 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003446 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003447 bp->ptp_info = &gem_ptp_info;
3448 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003449 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003450#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003451 }
3452
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003453 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003454}
3455
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003456static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003457 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003458 unsigned int *queue_mask,
3459 unsigned int *num_queues)
3460{
3461 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003462
3463 *queue_mask = 0x1;
3464 *num_queues = 1;
3465
Nicolas Ferreda120112015-03-31 15:02:00 +02003466 /* is it macb or gem ?
3467 *
3468 * We need to read directly from the hardware here because
3469 * we are early in the probe process and don't have the
3470 * MACB_CAPS_MACB_IS_GEM flag positioned
3471 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003472 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003473 return;
3474
3475 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303476 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3477
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003478 *queue_mask |= 0x1;
3479
3480 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3481 if (*queue_mask & (1 << hw_q))
3482 (*num_queues)++;
3483}
3484
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003485static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303486 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303487 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003488{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003489 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003490 int err;
3491
Bartosz Folta83a77e92016-12-14 06:39:15 +00003492 pdata = dev_get_platdata(&pdev->dev);
3493 if (pdata) {
3494 *pclk = pdata->pclk;
3495 *hclk = pdata->hclk;
3496 } else {
3497 *pclk = devm_clk_get(&pdev->dev, "pclk");
3498 *hclk = devm_clk_get(&pdev->dev, "hclk");
3499 }
3500
Harini Katakamcd5afa92019-03-20 19:12:22 +05303501 if (IS_ERR_OR_NULL(*pclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003502 err = PTR_ERR(*pclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303503 if (!err)
3504 err = -ENODEV;
3505
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003506 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003507 return err;
3508 }
3509
Harini Katakamcd5afa92019-03-20 19:12:22 +05303510 if (IS_ERR_OR_NULL(*hclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003511 err = PTR_ERR(*hclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303512 if (!err)
3513 err = -ENODEV;
3514
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003515 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003516 return err;
3517 }
3518
Michael Tretterbd310aca2019-10-18 16:11:43 +02003519 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003520 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003521 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003522
Michael Tretterbd310aca2019-10-18 16:11:43 +02003523 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303524 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003525 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303526
Michael Tretterbd310aca2019-10-18 16:11:43 +02003527 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303528 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003529 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303530
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003531 err = clk_prepare_enable(*pclk);
3532 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003533 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003534 return err;
3535 }
3536
3537 err = clk_prepare_enable(*hclk);
3538 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003539 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003540 goto err_disable_pclk;
3541 }
3542
3543 err = clk_prepare_enable(*tx_clk);
3544 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003545 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003546 goto err_disable_hclk;
3547 }
3548
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303549 err = clk_prepare_enable(*rx_clk);
3550 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003551 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303552 goto err_disable_txclk;
3553 }
3554
Harini Katakamf5473d12019-03-01 16:20:33 +05303555 err = clk_prepare_enable(*tsu_clk);
3556 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003557 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303558 goto err_disable_rxclk;
3559 }
3560
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003561 return 0;
3562
Harini Katakamf5473d12019-03-01 16:20:33 +05303563err_disable_rxclk:
3564 clk_disable_unprepare(*rx_clk);
3565
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303566err_disable_txclk:
3567 clk_disable_unprepare(*tx_clk);
3568
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003569err_disable_hclk:
3570 clk_disable_unprepare(*hclk);
3571
3572err_disable_pclk:
3573 clk_disable_unprepare(*pclk);
3574
3575 return err;
3576}
3577
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003578static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003579{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003580 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003581 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003582 struct macb *bp = netdev_priv(dev);
3583 struct macb_queue *queue;
3584 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003585 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003586
Zach Brownb410d132016-10-19 09:56:57 -05003587 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3588 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3589
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003590 /* set the queue register mapping once for all: queue0 has a special
3591 * register mapping but we don't want to test the queue index then
3592 * compute the corresponding register offset at run time.
3593 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003594 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003595 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003596 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003597
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003598 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003599 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003600 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003601 if (hw_q) {
3602 queue->ISR = GEM_ISR(hw_q - 1);
3603 queue->IER = GEM_IER(hw_q - 1);
3604 queue->IDR = GEM_IDR(hw_q - 1);
3605 queue->IMR = GEM_IMR(hw_q - 1);
3606 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003607 queue->RBQP = GEM_RBQP(hw_q - 1);
3608 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303609#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003610 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003611 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003612 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3613 }
Harini Katakamfff80192016-08-09 13:15:53 +05303614#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003615 } else {
3616 /* queue0 uses legacy registers */
3617 queue->ISR = MACB_ISR;
3618 queue->IER = MACB_IER;
3619 queue->IDR = MACB_IDR;
3620 queue->IMR = MACB_IMR;
3621 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003622 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303623#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003624 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003625 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003626 queue->RBQPH = MACB_RBQPH;
3627 }
Harini Katakamfff80192016-08-09 13:15:53 +05303628#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003629 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003630
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003631 /* get irq: here we use the linux queue index, not the hardware
3632 * queue index. the queue irq definitions in the device tree
3633 * must remove the optional gaps that could exist in the
3634 * hardware queue mask.
3635 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003636 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003637 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003638 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003639 if (err) {
3640 dev_err(&pdev->dev,
3641 "Unable to request IRQ %d (error %d)\n",
3642 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003643 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003644 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003645
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003646 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003647 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003648 }
3649
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003650 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003651
Nicolas Ferre4df95132013-06-04 21:57:12 +00003652 /* setup appropriated routines according to adapter type */
3653 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003654 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003655 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3656 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3657 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3658 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003659 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003660 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003661 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003662 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3663 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3664 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3665 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003666 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003667 }
3668
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003669 /* Set features */
3670 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003671
3672 /* Check LSO capability */
3673 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3674 dev->hw_features |= MACB_NETIF_LSO;
3675
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003676 /* Checksum offload is only available on gem with packet buffer */
3677 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003678 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003679 if (bp->caps & MACB_CAPS_SG_DISABLED)
3680 dev->hw_features &= ~NETIF_F_SG;
3681 dev->features = dev->hw_features;
3682
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003683 /* Check RX Flow Filters support.
3684 * Max Rx flows set by availability of screeners & compare regs:
3685 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3686 */
3687 reg = gem_readl(bp, DCFG8);
3688 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3689 GEM_BFEXT(T2SCR, reg));
3690 if (bp->max_tuples > 0) {
3691 /* also needs one ethtype match to check IPv4 */
3692 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3693 /* program this reg now */
3694 reg = 0;
3695 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3696 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3697 /* Filtering is supported in hw but don't enable it in kernel now */
3698 dev->hw_features |= NETIF_F_NTUPLE;
3699 /* init Rx flow definitions */
3700 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3701 bp->rx_fs_list.count = 0;
3702 spin_lock_init(&bp->rx_fs_lock);
3703 } else
3704 bp->max_tuples = 0;
3705 }
3706
Neil Armstrongce721a72016-01-05 14:39:16 +01003707 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3708 val = 0;
3709 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3710 val = GEM_BIT(RGMII);
3711 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003712 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003713 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003714 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003715 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003716
Neil Armstrongce721a72016-01-05 14:39:16 +01003717 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3718 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003719
Neil Armstrongce721a72016-01-05 14:39:16 +01003720 macb_or_gem_writel(bp, USRIO, val);
3721 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003722
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003723 /* Set MII management clock divider */
3724 val = macb_mdc_clk_div(bp);
3725 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303726 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3727 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003728 macb_writel(bp, NCFGR, val);
3729
3730 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003731}
3732
3733#if defined(CONFIG_OF)
3734/* 1518 rounded up */
3735#define AT91ETHER_MAX_RBUFF_SZ 0x600
3736/* max number of receive buffers */
3737#define AT91ETHER_MAX_RX_DESCR 9
3738
Arnd Bergmann49db9222019-07-08 14:48:23 +02003739static struct sifive_fu540_macb_mgmt *mgmt;
3740
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003741/* Initialize and start the Receiver and Transmit subsystems */
3742static int at91ether_start(struct net_device *dev)
3743{
3744 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003745 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003746 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003747 dma_addr_t addr;
3748 u32 ctl;
3749 int i;
3750
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003751 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003752 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003753 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003754 &q->rx_ring_dma, GFP_KERNEL);
3755 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003756 return -ENOMEM;
3757
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003758 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003759 AT91ETHER_MAX_RX_DESCR *
3760 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003761 &q->rx_buffers_dma, GFP_KERNEL);
3762 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003763 dma_free_coherent(&lp->pdev->dev,
3764 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003765 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003766 q->rx_ring, q->rx_ring_dma);
3767 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003768 return -ENOMEM;
3769 }
3770
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003771 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003772 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003773 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003774 macb_set_addr(lp, desc, addr);
3775 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003776 addr += AT91ETHER_MAX_RBUFF_SZ;
3777 }
3778
3779 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003780 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003781
3782 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003783 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003784
3785 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003786 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003787
3788 /* Enable Receive and Transmit */
3789 ctl = macb_readl(lp, NCR);
3790 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3791
3792 return 0;
3793}
3794
3795/* Open the ethernet interface */
3796static int at91ether_open(struct net_device *dev)
3797{
3798 struct macb *lp = netdev_priv(dev);
3799 u32 ctl;
3800 int ret;
3801
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01003802 ret = pm_runtime_get_sync(&lp->pdev->dev);
3803 if (ret < 0)
3804 return ret;
3805
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003806 /* Clear internal statistics */
3807 ctl = macb_readl(lp, NCR);
3808 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3809
3810 macb_set_hwaddr(lp);
3811
3812 ret = at91ether_start(dev);
3813 if (ret)
3814 return ret;
3815
3816 /* Enable MAC interrupts */
3817 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3818 MACB_BIT(RXUBR) |
3819 MACB_BIT(ISR_TUND) |
3820 MACB_BIT(ISR_RLE) |
3821 MACB_BIT(TCOMP) |
3822 MACB_BIT(ISR_ROVR) |
3823 MACB_BIT(HRESP));
3824
Antoine Tenart7897b072019-11-13 10:00:06 +01003825 ret = macb_phylink_connect(lp);
3826 if (ret)
3827 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003828
3829 netif_start_queue(dev);
3830
3831 return 0;
3832}
3833
3834/* Close the interface */
3835static int at91ether_close(struct net_device *dev)
3836{
3837 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003838 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003839 u32 ctl;
3840
3841 /* Disable Receiver and Transmitter */
3842 ctl = macb_readl(lp, NCR);
3843 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3844
3845 /* Disable MAC interrupts */
3846 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3847 MACB_BIT(RXUBR) |
3848 MACB_BIT(ISR_TUND) |
3849 MACB_BIT(ISR_RLE) |
3850 MACB_BIT(TCOMP) |
3851 MACB_BIT(ISR_ROVR) |
3852 MACB_BIT(HRESP));
3853
3854 netif_stop_queue(dev);
3855
Antoine Tenart7897b072019-11-13 10:00:06 +01003856 phylink_stop(lp->phylink);
3857 phylink_disconnect_phy(lp->phylink);
3858
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003859 dma_free_coherent(&lp->pdev->dev,
3860 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003861 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003862 q->rx_ring, q->rx_ring_dma);
3863 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003864
3865 dma_free_coherent(&lp->pdev->dev,
3866 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003867 q->rx_buffers, q->rx_buffers_dma);
3868 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003869
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01003870 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003871}
3872
3873/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003874static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3875 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003876{
3877 struct macb *lp = netdev_priv(dev);
3878
3879 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3880 netif_stop_queue(dev);
3881
3882 /* Store packet information (to free when Tx completed) */
3883 lp->skb = skb;
3884 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003885 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3886 skb->len, DMA_TO_DEVICE);
3887 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003888 dev_kfree_skb_any(skb);
3889 dev->stats.tx_dropped++;
3890 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3891 return NETDEV_TX_OK;
3892 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003893
3894 /* Set address of the data in the Transmit Address register */
3895 macb_writel(lp, TAR, lp->skb_physaddr);
3896 /* Set length of the packet in the Transmit Control register */
3897 macb_writel(lp, TCR, skb->len);
3898
3899 } else {
3900 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3901 return NETDEV_TX_BUSY;
3902 }
3903
3904 return NETDEV_TX_OK;
3905}
3906
3907/* Extract received frame from buffer descriptors and sent to upper layers.
3908 * (Called from interrupt context)
3909 */
3910static void at91ether_rx(struct net_device *dev)
3911{
3912 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003913 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003914 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003915 unsigned char *p_recv;
3916 struct sk_buff *skb;
3917 unsigned int pktlen;
3918
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003919 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003920 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003921 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003922 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003923 skb = netdev_alloc_skb(dev, pktlen + 2);
3924 if (skb) {
3925 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003926 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003927
3928 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003929 dev->stats.rx_packets++;
3930 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003931 netif_rx(skb);
3932 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003933 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003934 }
3935
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003936 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003937 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003938
3939 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003940 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003941
3942 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003943 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3944 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003945 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003946 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003947
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003948 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003949 }
3950}
3951
3952/* MAC interrupt handler */
3953static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3954{
3955 struct net_device *dev = dev_id;
3956 struct macb *lp = netdev_priv(dev);
3957 u32 intstatus, ctl;
3958
3959 /* MAC Interrupt Status register indicates what interrupts are pending.
3960 * It is automatically cleared once read.
3961 */
3962 intstatus = macb_readl(lp, ISR);
3963
3964 /* Receive complete */
3965 if (intstatus & MACB_BIT(RCOMP))
3966 at91ether_rx(dev);
3967
3968 /* Transmit complete */
3969 if (intstatus & MACB_BIT(TCOMP)) {
3970 /* The TCOM bit is set even if the transmission failed */
3971 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003972 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003973
3974 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003975 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003976 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003977 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003978 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003979 dev->stats.tx_packets++;
3980 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003981 }
3982 netif_wake_queue(dev);
3983 }
3984
3985 /* Work-around for EMAC Errata section 41.3.1 */
3986 if (intstatus & MACB_BIT(RXUBR)) {
3987 ctl = macb_readl(lp, NCR);
3988 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003989 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003990 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3991 }
3992
3993 if (intstatus & MACB_BIT(ISR_ROVR))
3994 netdev_err(dev, "ROVR error\n");
3995
3996 return IRQ_HANDLED;
3997}
3998
3999#ifdef CONFIG_NET_POLL_CONTROLLER
4000static void at91ether_poll_controller(struct net_device *dev)
4001{
4002 unsigned long flags;
4003
4004 local_irq_save(flags);
4005 at91ether_interrupt(dev->irq, dev);
4006 local_irq_restore(flags);
4007}
4008#endif
4009
4010static const struct net_device_ops at91ether_netdev_ops = {
4011 .ndo_open = at91ether_open,
4012 .ndo_stop = at91ether_close,
4013 .ndo_start_xmit = at91ether_start_xmit,
4014 .ndo_get_stats = macb_get_stats,
4015 .ndo_set_rx_mode = macb_set_rx_mode,
4016 .ndo_set_mac_address = eth_mac_addr,
4017 .ndo_do_ioctl = macb_ioctl,
4018 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004019#ifdef CONFIG_NET_POLL_CONTROLLER
4020 .ndo_poll_controller = at91ether_poll_controller,
4021#endif
4022};
4023
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004024static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304025 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304026 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004027{
4028 int err;
4029
4030 *hclk = NULL;
4031 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304032 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304033 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004034
4035 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4036 if (IS_ERR(*pclk))
4037 return PTR_ERR(*pclk);
4038
4039 err = clk_prepare_enable(*pclk);
4040 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004041 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004042 return err;
4043 }
4044
4045 return 0;
4046}
4047
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004048static int at91ether_init(struct platform_device *pdev)
4049{
4050 struct net_device *dev = platform_get_drvdata(pdev);
4051 struct macb *bp = netdev_priv(dev);
4052 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004053
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004054 bp->queues[0].bp = bp;
4055
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004056 dev->netdev_ops = &at91ether_netdev_ops;
4057 dev->ethtool_ops = &macb_ethtool_ops;
4058
4059 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4060 0, dev->name, dev);
4061 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004062 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004063
4064 macb_writel(bp, NCR, 0);
4065
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004066 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004067
4068 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004069}
4070
Yash Shahc218ad52019-06-18 13:26:08 +05304071static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4072 unsigned long parent_rate)
4073{
4074 return mgmt->rate;
4075}
4076
4077static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4078 unsigned long *parent_rate)
4079{
4080 if (WARN_ON(rate < 2500000))
4081 return 2500000;
4082 else if (rate == 2500000)
4083 return 2500000;
4084 else if (WARN_ON(rate < 13750000))
4085 return 2500000;
4086 else if (WARN_ON(rate < 25000000))
4087 return 25000000;
4088 else if (rate == 25000000)
4089 return 25000000;
4090 else if (WARN_ON(rate < 75000000))
4091 return 25000000;
4092 else if (WARN_ON(rate < 125000000))
4093 return 125000000;
4094 else if (rate == 125000000)
4095 return 125000000;
4096
4097 WARN_ON(rate > 125000000);
4098
4099 return 125000000;
4100}
4101
4102static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4103 unsigned long parent_rate)
4104{
4105 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4106 if (rate != 125000000)
4107 iowrite32(1, mgmt->reg);
4108 else
4109 iowrite32(0, mgmt->reg);
4110 mgmt->rate = rate;
4111
4112 return 0;
4113}
4114
4115static const struct clk_ops fu540_c000_ops = {
4116 .recalc_rate = fu540_macb_tx_recalc_rate,
4117 .round_rate = fu540_macb_tx_round_rate,
4118 .set_rate = fu540_macb_tx_set_rate,
4119};
4120
4121static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4122 struct clk **hclk, struct clk **tx_clk,
4123 struct clk **rx_clk, struct clk **tsu_clk)
4124{
4125 struct clk_init_data init;
4126 int err = 0;
4127
4128 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4129 if (err)
4130 return err;
4131
4132 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4133 if (!mgmt)
4134 return -ENOMEM;
4135
4136 init.name = "sifive-gemgxl-mgmt";
4137 init.ops = &fu540_c000_ops;
4138 init.flags = 0;
4139 init.num_parents = 0;
4140
4141 mgmt->rate = 0;
4142 mgmt->hw.init = &init;
4143
Stephen Boydd89091a2020-01-03 16:19:21 -08004144 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Yash Shahc218ad52019-06-18 13:26:08 +05304145 if (IS_ERR(*tx_clk))
4146 return PTR_ERR(*tx_clk);
4147
4148 err = clk_prepare_enable(*tx_clk);
4149 if (err)
4150 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4151 else
4152 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4153
4154 return 0;
4155}
4156
4157static int fu540_c000_init(struct platform_device *pdev)
4158{
4159 struct resource *res;
4160
4161 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
4162 if (!res)
4163 return -ENODEV;
4164
4165 mgmt->reg = ioremap(res->start, resource_size(res));
4166 if (!mgmt->reg)
4167 return -ENOMEM;
4168
4169 return macb_init(pdev);
4170}
4171
4172static const struct macb_config fu540_c000_config = {
4173 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4174 MACB_CAPS_GEM_HAS_PTP,
4175 .dma_burst_length = 16,
4176 .clk_init = fu540_c000_clk_init,
4177 .init = fu540_c000_init,
4178 .jumbo_max_len = 10240,
4179};
4180
David S. Miller3cef5c52015-03-09 23:38:02 -04004181static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004182 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004183 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004184 .init = macb_init,
4185};
4186
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004187static const struct macb_config sama5d3macb_config = {
4188 .caps = MACB_CAPS_SG_DISABLED
4189 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4190 .clk_init = macb_clk_init,
4191 .init = macb_init,
4192};
4193
David S. Miller3cef5c52015-03-09 23:38:02 -04004194static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004195 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4196 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004197 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004198 .init = macb_init,
4199};
4200
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004201static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004202 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004203 .dma_burst_length = 16,
4204 .clk_init = macb_clk_init,
4205 .init = macb_init,
4206};
4207
David S. Miller3cef5c52015-03-09 23:38:02 -04004208static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004209 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004210 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004211 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004212 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004213 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004214 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004215};
4216
David S. Miller3cef5c52015-03-09 23:38:02 -04004217static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004218 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004219 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004220 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004221 .init = macb_init,
4222};
4223
David S. Miller3cef5c52015-03-09 23:38:02 -04004224static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004225 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004226 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004227 .init = at91ether_init,
4228};
4229
Neil Armstronge611b5b2016-01-05 14:39:17 +01004230static const struct macb_config np4_config = {
4231 .caps = MACB_CAPS_USRIO_DISABLED,
4232 .clk_init = macb_clk_init,
4233 .init = macb_init,
4234};
David S. Miller36583eb2015-05-23 01:22:35 -04004235
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304236static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004237 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4238 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304239 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304240 .dma_burst_length = 16,
4241 .clk_init = macb_clk_init,
4242 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304243 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304244};
4245
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004246static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304247 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4248 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004249 .dma_burst_length = 16,
4250 .clk_init = macb_clk_init,
4251 .init = macb_init,
4252};
4253
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004254static const struct of_device_id macb_dt_ids[] = {
4255 { .compatible = "cdns,at32ap7000-macb" },
4256 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4257 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004258 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004259 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4260 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004261 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004262 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004263 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004264 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004265 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4266 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4267 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304268 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004269 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304270 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004271 { /* sentinel */ }
4272};
4273MODULE_DEVICE_TABLE(of, macb_dt_ids);
4274#endif /* CONFIG_OF */
4275
Bartosz Folta83a77e92016-12-14 06:39:15 +00004276static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004277 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4278 MACB_CAPS_JUMBO |
4279 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004280 .dma_burst_length = 16,
4281 .clk_init = macb_clk_init,
4282 .init = macb_init,
4283 .jumbo_max_len = 10240,
4284};
4285
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004286static int macb_probe(struct platform_device *pdev)
4287{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004288 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004289 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304290 struct clk **, struct clk **, struct clk **,
4291 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004292 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004293 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304294 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304295 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004296 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004297 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004298 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004299 struct net_device *dev;
4300 struct resource *regs;
4301 void __iomem *mem;
4302 const char *mac;
4303 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304304 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004305
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004306 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4307 mem = devm_ioremap_resource(&pdev->dev, regs);
4308 if (IS_ERR(mem))
4309 return PTR_ERR(mem);
4310
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004311 if (np) {
4312 const struct of_device_id *match;
4313
4314 match = of_match_node(macb_dt_ids, np);
4315 if (match && match->data) {
4316 macb_config = match->data;
4317 clk_init = macb_config->clk_init;
4318 init = macb_config->init;
4319 }
4320 }
4321
Harini Katakamf5473d12019-03-01 16:20:33 +05304322 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004323 if (err)
4324 return err;
4325
Harini Katakamd54f89a2019-03-01 16:20:34 +05304326 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4327 pm_runtime_use_autosuspend(&pdev->dev);
4328 pm_runtime_get_noresume(&pdev->dev);
4329 pm_runtime_set_active(&pdev->dev);
4330 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004331 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004332
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004333 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004334 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004335 if (!dev) {
4336 err = -ENOMEM;
4337 goto err_disable_clocks;
4338 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004339
4340 dev->base_addr = regs->start;
4341
4342 SET_NETDEV_DEV(dev, &pdev->dev);
4343
4344 bp = netdev_priv(dev);
4345 bp->pdev = pdev;
4346 bp->dev = dev;
4347 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004348 bp->native_io = native_io;
4349 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004350 bp->macb_reg_readl = hw_readl_native;
4351 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004352 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004353 bp->macb_reg_readl = hw_readl;
4354 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004355 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004356 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004357 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004358 if (macb_config)
4359 bp->dma_burst_length = macb_config->dma_burst_length;
4360 bp->pclk = pclk;
4361 bp->hclk = hclk;
4362 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304363 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304364 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004365 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304366 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304367
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004368 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004369 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004370 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4371 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4372
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004373 spin_lock_init(&bp->lock);
4374
Nicolas Ferread783472015-03-31 15:02:02 +02004375 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004376 macb_configure_caps(bp, macb_config);
4377
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004378#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4379 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4380 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4381 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4382 }
4383#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004384 platform_set_drvdata(pdev, dev);
4385
4386 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004387 if (dev->irq < 0) {
4388 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004389 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004390 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004391
Jarod Wilson44770e12016-10-17 15:54:17 -04004392 /* MTU range: 68 - 1500 or 10240 */
4393 dev->min_mtu = GEM_MTU_MIN_SIZE;
4394 if (bp->caps & MACB_CAPS_JUMBO)
4395 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4396 else
4397 dev->max_mtu = ETH_DATA_LEN;
4398
Harini Katakam404cd082018-07-06 12:18:58 +05304399 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4400 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4401 if (val)
4402 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4403 macb_dma_desc_get_size(bp);
4404
4405 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4406 if (val)
4407 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4408 macb_dma_desc_get_size(bp);
4409 }
4410
Harini Katakame5010702019-01-29 15:20:03 +05304411 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4412 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4413 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4414
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004415 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004416 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4417 err = -EPROBE_DEFER;
4418 goto err_out_free_netdev;
Antoine Tenart2bf4ecb2019-06-21 17:26:35 +02004419 } else if (!IS_ERR_OR_NULL(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004420 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004421 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004422 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004423 }
frederic RODO6c36a702007-07-12 19:07:24 +02004424
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004425 err = of_get_phy_mode(np, &interface);
4426 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004427 /* not found in DT, MII by default */
4428 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4429 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004430 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004431
Antoine Tenart7897b072019-11-13 10:00:06 +01004432 bp->speed = SPEED_UNKNOWN;
4433
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004434 /* IP specific init */
4435 err = init(pdev);
4436 if (err)
4437 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004438
Florian Fainellicf669662016-05-02 18:38:45 -07004439 err = macb_mii_init(bp);
4440 if (err)
4441 goto err_out_free_netdev;
4442
Florian Fainellicf669662016-05-02 18:38:45 -07004443 netif_carrier_off(dev);
4444
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004445 err = register_netdev(dev);
4446 if (err) {
4447 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004448 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004449 }
4450
Harini Katakam032dc412018-01-27 12:09:01 +05304451 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4452 (unsigned long)bp);
4453
Bo Shen58798232014-09-13 01:57:49 +02004454 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4455 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4456 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004457
Harini Katakamd54f89a2019-03-01 16:20:34 +05304458 pm_runtime_mark_last_busy(&bp->pdev->dev);
4459 pm_runtime_put_autosuspend(&bp->pdev->dev);
4460
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004461 return 0;
4462
Florian Fainellicf669662016-05-02 18:38:45 -07004463err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004464 mdiobus_unregister(bp->mii_bus);
4465 mdiobus_free(bp->mii_bus);
4466
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004467err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004468 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004469
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004470err_disable_clocks:
4471 clk_disable_unprepare(tx_clk);
4472 clk_disable_unprepare(hclk);
4473 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304474 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304475 clk_disable_unprepare(tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304476 pm_runtime_disable(&pdev->dev);
4477 pm_runtime_set_suspended(&pdev->dev);
4478 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004479
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004480 return err;
4481}
4482
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004483static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004484{
4485 struct net_device *dev;
4486 struct macb *bp;
4487
4488 dev = platform_get_drvdata(pdev);
4489
4490 if (dev) {
4491 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004492 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004493 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004494
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004495 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004496 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304497 pm_runtime_disable(&pdev->dev);
4498 pm_runtime_dont_use_autosuspend(&pdev->dev);
4499 if (!pm_runtime_suspended(&pdev->dev)) {
4500 clk_disable_unprepare(bp->tx_clk);
4501 clk_disable_unprepare(bp->hclk);
4502 clk_disable_unprepare(bp->pclk);
4503 clk_disable_unprepare(bp->rx_clk);
4504 clk_disable_unprepare(bp->tsu_clk);
4505 pm_runtime_set_suspended(&pdev->dev);
4506 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004507 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004508 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004509 }
4510
4511 return 0;
4512}
4513
Michal Simekd23823d2015-01-23 09:36:03 +01004514static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004515{
Wolfram Sangce886a42018-10-21 22:00:14 +02004516 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004517 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304518 struct macb_queue *queue = bp->queues;
4519 unsigned long flags;
4520 unsigned int q;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004521
Harini Katakamde991c52019-03-01 16:20:35 +05304522 if (!netif_running(netdev))
4523 return 0;
4524
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004525 if (bp->wol & MACB_WOL_ENABLED) {
4526 macb_writel(bp, IER, MACB_BIT(WOL));
4527 macb_writel(bp, WOL, MACB_BIT(MAG));
4528 enable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304529 netif_device_detach(netdev);
4530 } else {
4531 netif_device_detach(netdev);
4532 for (q = 0, queue = bp->queues; q < bp->num_queues;
4533 ++q, ++queue)
4534 napi_disable(&queue->napi);
Antoine Tenart7897b072019-11-13 10:00:06 +01004535 rtnl_lock();
4536 phylink_stop(bp->phylink);
4537 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304538 spin_lock_irqsave(&bp->lock, flags);
4539 macb_reset_hw(bp);
4540 spin_unlock_irqrestore(&bp->lock, flags);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004541
4542 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4543 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4544
4545 if (netdev->hw_features & NETIF_F_NTUPLE)
4546 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304547 }
4548
Harini Katakamde991c52019-03-01 16:20:35 +05304549 netif_carrier_off(netdev);
4550 if (bp->ptp_info)
4551 bp->ptp_info->ptp_remove(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304552 pm_runtime_force_suspend(dev);
4553
4554 return 0;
4555}
4556
4557static int __maybe_unused macb_resume(struct device *dev)
4558{
4559 struct net_device *netdev = dev_get_drvdata(dev);
4560 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304561 struct macb_queue *queue = bp->queues;
4562 unsigned int q;
4563
4564 if (!netif_running(netdev))
4565 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304566
4567 pm_runtime_force_resume(dev);
4568
4569 if (bp->wol & MACB_WOL_ENABLED) {
4570 macb_writel(bp, IDR, MACB_BIT(WOL));
4571 macb_writel(bp, WOL, 0);
4572 disable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304573 } else {
4574 macb_writel(bp, NCR, MACB_BIT(MPE));
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004575
4576 if (netdev->hw_features & NETIF_F_NTUPLE)
4577 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4578
4579 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4580 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4581
Harini Katakamde991c52019-03-01 16:20:35 +05304582 for (q = 0, queue = bp->queues; q < bp->num_queues;
4583 ++q, ++queue)
4584 napi_enable(&queue->napi);
Antoine Tenart7897b072019-11-13 10:00:06 +01004585 rtnl_lock();
4586 phylink_start(bp->phylink);
4587 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304588 }
4589
Harini Katakamde991c52019-03-01 16:20:35 +05304590 macb_init_hw(bp);
4591 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004592 macb_restore_features(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304593 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304594 if (bp->ptp_info)
4595 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304596
4597 return 0;
4598}
4599
4600static int __maybe_unused macb_runtime_suspend(struct device *dev)
4601{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004602 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304603 struct macb *bp = netdev_priv(netdev);
4604
4605 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004606 clk_disable_unprepare(bp->tx_clk);
4607 clk_disable_unprepare(bp->hclk);
4608 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304609 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004610 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304611 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004612
4613 return 0;
4614}
4615
Harini Katakamd54f89a2019-03-01 16:20:34 +05304616static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004617{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004618 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004619 struct macb *bp = netdev_priv(netdev);
4620
Harini Katakamd54f89a2019-03-01 16:20:34 +05304621 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004622 clk_prepare_enable(bp->pclk);
4623 clk_prepare_enable(bp->hclk);
4624 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304625 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004626 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304627 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004628
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004629 return 0;
4630}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004631
Harini Katakamd54f89a2019-03-01 16:20:34 +05304632static const struct dev_pm_ops macb_pm_ops = {
4633 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4634 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4635};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004636
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004637static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004638 .probe = macb_probe,
4639 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004640 .driver = {
4641 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004642 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004643 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004644 },
4645};
4646
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004647module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004648
4649MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004650MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004651MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004652MODULE_ALIAS("platform:macb");