blob: 4508f0d150da95d8e838d0ead806e8ef74794cc5 [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 */
575 ctrl &= ~(GEM_BIT(GBE) | MACB_BIT(SPD) | MACB_BIT(FD) | MACB_BIT(PAE) |
576 GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
frederic RODO6c36a702007-07-12 19:07:24 +0200577
Antoine Tenart7897b072019-11-13 10:00:06 +0100578 if (state->speed == SPEED_1000)
579 ctrl |= GEM_BIT(GBE);
580 else if (state->speed == SPEED_100)
581 ctrl |= MACB_BIT(SPD);
frederic RODO6c36a702007-07-12 19:07:24 +0200582
Antoine Tenart7897b072019-11-13 10:00:06 +0100583 if (state->duplex)
584 ctrl |= MACB_BIT(FD);
frederic RODO6c36a702007-07-12 19:07:24 +0200585
Antoine Tenart7897b072019-11-13 10:00:06 +0100586 /* We do not support MLO_PAUSE_RX yet */
587 if (state->pause & MLO_PAUSE_TX)
588 ctrl |= MACB_BIT(PAE);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100589
Antoine Tenart7897b072019-11-13 10:00:06 +0100590 if (state->interface == PHY_INTERFACE_MODE_SGMII)
591 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100592
Antoine Tenart7897b072019-11-13 10:00:06 +0100593 /* Apply the new configuration, if any */
594 if (old_ctrl ^ ctrl)
595 macb_or_gem_writel(bp, NCFGR, ctrl);
596
597 bp->speed = state->speed;
frederic RODO6c36a702007-07-12 19:07:24 +0200598
599 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200600}
601
Antoine Tenart7897b072019-11-13 10:00:06 +0100602static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
603 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200604{
Antoine Tenart7897b072019-11-13 10:00:06 +0100605 struct net_device *ndev = to_net_dev(config->dev);
606 struct macb *bp = netdev_priv(ndev);
607 struct macb_queue *queue;
608 unsigned int q;
609 u32 ctrl;
610
611 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
612 queue_writel(queue, IDR,
613 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
614
615 /* Disable Rx and Tx */
616 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
617 macb_writel(bp, NCR, ctrl);
618
619 netif_tx_stop_all_queues(ndev);
620}
621
622static void macb_mac_link_up(struct phylink_config *config, unsigned int mode,
623 phy_interface_t interface, struct phy_device *phy)
624{
625 struct net_device *ndev = to_net_dev(config->dev);
626 struct macb *bp = netdev_priv(ndev);
627 struct macb_queue *queue;
628 unsigned int q;
629
630 macb_set_tx_clk(bp->tx_clk, bp->speed, ndev);
631
632 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
633 * cleared the pipeline and control registers.
634 */
635 bp->macbgem_ops.mog_init_rings(bp);
636 macb_init_buffers(bp);
637
638 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
639 queue_writel(queue, IER,
640 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
641
642 /* Enable Rx and Tx */
643 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
644
645 netif_tx_wake_all_queues(ndev);
646}
647
648static const struct phylink_mac_ops macb_phylink_ops = {
649 .validate = macb_validate,
Russell Kingd46b7e42019-11-21 00:36:22 +0000650 .mac_pcs_get_state = macb_mac_pcs_get_state,
Antoine Tenart7897b072019-11-13 10:00:06 +0100651 .mac_an_restart = macb_mac_an_restart,
652 .mac_config = macb_mac_config,
653 .mac_link_down = macb_mac_link_down,
654 .mac_link_up = macb_mac_link_up,
655};
656
Milind Parabfd2a8912020-01-13 03:30:43 +0000657static bool macb_phy_handle_exists(struct device_node *dn)
658{
659 dn = of_parse_phandle(dn, "phy-handle", 0);
660 of_node_put(dn);
661 return dn != NULL;
662}
663
Antoine Tenart7897b072019-11-13 10:00:06 +0100664static int macb_phylink_connect(struct macb *bp)
665{
Milind Parabfd2a8912020-01-13 03:30:43 +0000666 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100667 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000668 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100669 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500670
Milind Parabfd2a8912020-01-13 03:30:43 +0000671 if (dn)
672 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
673
674 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200675 phydev = phy_find_first(bp->mii_bus);
676 if (!phydev) {
677 netdev_err(dev, "no PHY found\n");
678 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000679 }
frederic RODO6c36a702007-07-12 19:07:24 +0200680
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200681 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100682 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000683 }
684
685 if (ret) {
686 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
687 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200688 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100689
Antoine Tenart7897b072019-11-13 10:00:06 +0100690 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100691
Antoine Tenart7897b072019-11-13 10:00:06 +0100692 return 0;
693}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100694
Antoine Tenart7897b072019-11-13 10:00:06 +0100695/* based on au1000_eth. c*/
696static int macb_mii_probe(struct net_device *dev)
697{
698 struct macb *bp = netdev_priv(dev);
699
700 bp->phylink_config.dev = &dev->dev;
701 bp->phylink_config.type = PHYLINK_NETDEV;
702
703 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
704 bp->phy_interface, &macb_phylink_ops);
705 if (IS_ERR(bp->phylink)) {
706 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
707 PTR_ERR(bp->phylink));
708 return PTR_ERR(bp->phylink);
709 }
frederic RODO6c36a702007-07-12 19:07:24 +0200710
711 return 0;
712}
713
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100714static int macb_mdiobus_register(struct macb *bp)
715{
716 struct device_node *child, *np = bp->pdev->dev.of_node;
717
718 /* Only create the PHY from the device tree if at least one PHY is
719 * described. Otherwise scan the entire MDIO bus. We do this to support
720 * old device tree that did not follow the best practices and did not
721 * describe their network PHYs.
722 */
723 for_each_available_child_of_node(np, child)
724 if (of_mdiobus_child_is_phy(child)) {
725 /* The loop increments the child refcount,
726 * decrement it before returning.
727 */
728 of_node_put(child);
729
730 return of_mdiobus_register(bp->mii_bus, np);
731 }
732
733 return mdiobus_register(bp->mii_bus);
734}
735
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100736static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200737{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200738 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200739
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200740 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200741 macb_writel(bp, NCR, MACB_BIT(MPE));
742
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700743 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700744 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200745 err = -ENOMEM;
746 goto err_out;
747 }
748
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700749 bp->mii_bus->name = "MACB_mii_bus";
750 bp->mii_bus->read = &macb_mdio_read;
751 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000752 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700753 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700754 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700755 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700756
Jamie Iles91523942011-02-28 04:05:25 +0000757 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200758
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100759 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200760 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100761 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200762
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200763 err = macb_mii_probe(bp->dev);
764 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200765 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200766
767 return 0;
768
769err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700770 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500771err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700772 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200773err_out:
774 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100775}
776
777static void macb_update_stats(struct macb *bp)
778{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000779 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
780 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300781 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100782
783 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
784
Moritz Fischer96ec6312016-03-29 19:11:11 -0700785 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700786 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100787}
788
Nicolas Ferree86cd532012-10-31 06:04:57 +0000789static int macb_halt_tx(struct macb *bp)
790{
791 unsigned long halt_time, timeout;
792 u32 status;
793
794 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
795
796 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
797 do {
798 halt_time = jiffies;
799 status = macb_readl(bp, TSR);
800 if (!(status & MACB_BIT(TGO)))
801 return 0;
802
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800803 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000804 } while (time_before(halt_time, timeout));
805
806 return -ETIMEDOUT;
807}
808
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200809static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
810{
811 if (tx_skb->mapping) {
812 if (tx_skb->mapped_as_page)
813 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
814 tx_skb->size, DMA_TO_DEVICE);
815 else
816 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
817 tx_skb->size, DMA_TO_DEVICE);
818 tx_skb->mapping = 0;
819 }
820
821 if (tx_skb->skb) {
822 dev_kfree_skb_any(tx_skb->skb);
823 tx_skb->skb = NULL;
824 }
825}
826
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000827static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530828{
Harini Katakamfff80192016-08-09 13:15:53 +0530829#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000830 struct macb_dma_desc_64 *desc_64;
831
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100832 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000833 desc_64 = macb_64b_desc(bp, desc);
834 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200835 /* The low bits of RX address contain the RX_USED bit, clearing
836 * of which allows packet RX. Make sure the high bits are also
837 * visible to HW at that point.
838 */
839 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000840 }
Harini Katakamfff80192016-08-09 13:15:53 +0530841#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000842 desc->addr = lower_32_bits(addr);
843}
844
845static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
846{
847 dma_addr_t addr = 0;
848#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
849 struct macb_dma_desc_64 *desc_64;
850
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100851 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000852 desc_64 = macb_64b_desc(bp, desc);
853 addr = ((u64)(desc_64->addrh) << 32);
854 }
855#endif
856 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
857 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530858}
859
Nicolas Ferree86cd532012-10-31 06:04:57 +0000860static void macb_tx_error_task(struct work_struct *work)
861{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100862 struct macb_queue *queue = container_of(work, struct macb_queue,
863 tx_error_task);
864 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000865 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100866 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000867 struct sk_buff *skb;
868 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100869 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000870
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100871 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
872 (unsigned int)(queue - bp->queues),
873 queue->tx_tail, queue->tx_head);
874
875 /* Prevent the queue IRQ handlers from running: each of them may call
876 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
877 * As explained below, we have to halt the transmission before updating
878 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
879 * network engine about the macb/gem being halted.
880 */
881 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000882
883 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100884 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000885
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700886 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000887 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100888 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000889 */
890 if (macb_halt_tx(bp))
891 /* Just complain for now, reinitializing TX path can be good */
892 netdev_err(bp->dev, "BUG: halt tx timed out\n");
893
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700894 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000895 * Free transmit buffers in upper layer.
896 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100897 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
898 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000899
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100900 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000901 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100902 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000903 skb = tx_skb->skb;
904
905 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200906 /* skb is set for the last buffer of the frame */
907 while (!skb) {
908 macb_tx_unmap(bp, tx_skb);
909 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100910 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200911 skb = tx_skb->skb;
912 }
913
914 /* ctrl still refers to the first buffer descriptor
915 * since it's the only one written back by the hardware
916 */
917 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
918 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500919 macb_tx_ring_wrap(bp, tail),
920 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200921 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000922 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200923 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000924 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200925 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000926 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700927 /* "Buffers exhausted mid-frame" errors may only happen
928 * if the driver is buggy, so complain loudly about
929 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000930 */
931 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
932 netdev_err(bp->dev,
933 "BUG: TX buffers exhausted mid-frame\n");
934
935 desc->ctrl = ctrl | MACB_BIT(TX_USED);
936 }
937
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200938 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000939 }
940
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100941 /* Set end of TX queue */
942 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000943 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100944 desc->ctrl = MACB_BIT(TX_USED);
945
Nicolas Ferree86cd532012-10-31 06:04:57 +0000946 /* Make descriptor updates visible to hardware */
947 wmb();
948
949 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000950 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530951#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100952 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000953 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530954#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000955 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100956 queue->tx_head = 0;
957 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000958
959 /* Housework before enabling TX IRQ */
960 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100961 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
962
963 /* Now we are ready to start transmission again */
964 netif_tx_start_all_queues(bp->dev);
965 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
966
967 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000968}
969
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100970static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971{
972 unsigned int tail;
973 unsigned int head;
974 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100975 struct macb *bp = queue->bp;
976 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100977
978 status = macb_readl(bp, TSR);
979 macb_writel(bp, TSR, status);
980
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000981 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100982 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000983
Nicolas Ferree86cd532012-10-31 06:04:57 +0000984 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700985 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100987 head = queue->tx_head;
988 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000989 struct macb_tx_skb *tx_skb;
990 struct sk_buff *skb;
991 struct macb_dma_desc *desc;
992 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100993
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100994 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100995
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000996 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100997 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000998
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000999 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001000
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001001 /* TX_USED bit is only set by hardware on the very first buffer
1002 * descriptor of the transmitted frame.
1003 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001004 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001005 break;
1006
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001007 /* Process all buffers of the current transmitted frame */
1008 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001009 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001010 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001011
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001012 /* First, update TX stats if needed */
1013 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001014 if (unlikely(skb_shinfo(skb)->tx_flags &
1015 SKBTX_HW_TSTAMP) &&
1016 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001017 /* skb now belongs to timestamp buffer
1018 * and will be removed later
1019 */
1020 tx_skb->skb = NULL;
1021 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001022 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001023 macb_tx_ring_wrap(bp, tail),
1024 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001025 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001026 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001027 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001028 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001029 }
1030
1031 /* Now we can safely release resources */
1032 macb_tx_unmap(bp, tx_skb);
1033
1034 /* skb is set only for the last buffer of the frame.
1035 * WARNING: at this point skb has been freed by
1036 * macb_tx_unmap().
1037 */
1038 if (skb)
1039 break;
1040 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001041 }
1042
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001043 queue->tx_tail = tail;
1044 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1045 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001046 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001047 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001048}
1049
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001050static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001051{
1052 unsigned int entry;
1053 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001054 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001055 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001056 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001057
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001058 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1059 bp->rx_ring_size) > 0) {
1060 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001061
1062 /* Make hw descriptor updates visible to CPU */
1063 rmb();
1064
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001065 queue->rx_prepared_head++;
1066 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001067
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001068 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001069 /* allocate sk_buff for this free entry in ring */
1070 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001071 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001072 netdev_err(bp->dev,
1073 "Unable to allocate sk_buff\n");
1074 break;
1075 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001076
1077 /* now fill corresponding descriptor entry */
1078 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001079 bp->rx_buffer_size,
1080 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001081 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1082 dev_kfree_skb(skb);
1083 break;
1084 }
1085
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001086 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001087
Zach Brownb410d132016-10-19 09:56:57 -05001088 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001089 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001090 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001091 /* Setting addr clears RX_USED and allows reception,
1092 * make sure ctrl is cleared first to avoid a race.
1093 */
1094 dma_wmb();
1095 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001096
1097 /* properly align Ethernet header */
1098 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301099 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001100 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001101 dma_wmb();
1102 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001103 }
1104 }
1105
1106 /* Make descriptor updates visible to hardware */
1107 wmb();
1108
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001109 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1110 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001111}
1112
1113/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001114static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001115 unsigned int end)
1116{
1117 unsigned int frag;
1118
1119 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001120 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001121
Nicolas Ferre4df95132013-06-04 21:57:12 +00001122 desc->addr &= ~MACB_BIT(RX_USED);
1123 }
1124
1125 /* Make descriptor updates visible to hardware */
1126 wmb();
1127
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001128 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001129 * whatever caused this is updated, so we don't have to record
1130 * anything.
1131 */
1132}
1133
Antoine Tenart97236cd2019-06-21 17:30:02 +02001134static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1135 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001136{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001137 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001138 unsigned int len;
1139 unsigned int entry;
1140 struct sk_buff *skb;
1141 struct macb_dma_desc *desc;
1142 int count = 0;
1143
1144 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301145 u32 ctrl;
1146 dma_addr_t addr;
1147 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001148
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001149 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1150 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001151
1152 /* Make hw descriptor updates visible to CPU */
1153 rmb();
1154
Harini Katakamfff80192016-08-09 13:15:53 +05301155 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001156 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001157
Harini Katakamfff80192016-08-09 13:15:53 +05301158 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001159 break;
1160
Anssi Hannula6e0af292018-12-17 15:05:41 +02001161 /* Ensure ctrl is at least as up-to-date as rxused */
1162 dma_rmb();
1163
1164 ctrl = desc->ctrl;
1165
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001166 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001167 count++;
1168
1169 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1170 netdev_err(bp->dev,
1171 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001172 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001173 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001174 break;
1175 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001176 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001177 if (unlikely(!skb)) {
1178 netdev_err(bp->dev,
1179 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001180 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001181 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001182 break;
1183 }
1184 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001185 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301186 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001187
1188 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1189
1190 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001191 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001192 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001193
1194 skb->protocol = eth_type_trans(skb, bp->dev);
1195 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001196 if (bp->dev->features & NETIF_F_RXCSUM &&
1197 !(bp->dev->flags & IFF_PROMISC) &&
1198 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1199 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001200
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001201 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001202 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001203 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001204 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001205
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001206 gem_ptp_do_rxstamp(bp, skb, desc);
1207
Nicolas Ferre4df95132013-06-04 21:57:12 +00001208#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1209 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1210 skb->len, skb->csum);
1211 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001212 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001213 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1214 skb->data, 32, true);
1215#endif
1216
Antoine Tenart97236cd2019-06-21 17:30:02 +02001217 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001218 }
1219
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001220 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001221
1222 return count;
1223}
1224
Antoine Tenart97236cd2019-06-21 17:30:02 +02001225static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1226 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227{
1228 unsigned int len;
1229 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001230 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001231 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001232 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001233 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001234
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001235 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301236 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001237
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001238 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001239 macb_rx_ring_wrap(bp, first_frag),
1240 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001241
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001242 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001243 * first buffer. Since the header is 14 bytes, this makes the
1244 * payload word-aligned.
1245 *
1246 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1247 * the two padding bytes into the skb so that we avoid hitting
1248 * the slowpath in memcpy(), and pull them off afterwards.
1249 */
1250 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001251 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001252 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001253 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001254 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001255 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001256 if (frag == last_frag)
1257 break;
1258 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001259
1260 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001261 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001262
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001263 return 1;
1264 }
1265
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001266 offset = 0;
1267 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001268 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001269 skb_put(skb, len);
1270
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001271 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001272 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273
1274 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001275 if (unlikely(frag != last_frag)) {
1276 dev_kfree_skb_any(skb);
1277 return -1;
1278 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001279 frag_len = len - offset;
1280 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001281 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001282 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001283 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001284 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001285 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001286 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001287
1288 if (frag == last_frag)
1289 break;
1290 }
1291
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001292 /* Make descriptor updates visible to hardware */
1293 wmb();
1294
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001295 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001296 skb->protocol = eth_type_trans(skb, bp->dev);
1297
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001298 bp->dev->stats.rx_packets++;
1299 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001300 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001301 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001302 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001303
1304 return 0;
1305}
1306
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001307static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001308{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001309 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001310 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001311 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001312 int i;
1313
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001314 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001315 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001316 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001317 macb_set_addr(bp, desc, addr);
1318 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001319 addr += bp->rx_buffer_size;
1320 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001321 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001322 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001323}
1324
Antoine Tenart97236cd2019-06-21 17:30:02 +02001325static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1326 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001327{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001328 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001329 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001330 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001331 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332 int first_frag = -1;
1333
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001334 for (tail = queue->rx_tail; budget > 0; tail++) {
1335 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001336 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001337
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001338 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001339 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001340
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001341 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001342 break;
1343
Anssi Hannula6e0af292018-12-17 15:05:41 +02001344 /* Ensure ctrl is at least as up-to-date as addr */
1345 dma_rmb();
1346
1347 ctrl = desc->ctrl;
1348
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001349 if (ctrl & MACB_BIT(RX_SOF)) {
1350 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001351 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001352 first_frag = tail;
1353 }
1354
1355 if (ctrl & MACB_BIT(RX_EOF)) {
1356 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001357
1358 if (unlikely(first_frag == -1)) {
1359 reset_rx_queue = true;
1360 continue;
1361 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362
Antoine Tenart97236cd2019-06-21 17:30:02 +02001363 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001364 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001365 if (unlikely(dropped < 0)) {
1366 reset_rx_queue = true;
1367 continue;
1368 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001369 if (!dropped) {
1370 received++;
1371 budget--;
1372 }
1373 }
1374 }
1375
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001376 if (unlikely(reset_rx_queue)) {
1377 unsigned long flags;
1378 u32 ctrl;
1379
1380 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1381
1382 spin_lock_irqsave(&bp->lock, flags);
1383
1384 ctrl = macb_readl(bp, NCR);
1385 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1386
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001387 macb_init_rx_ring(queue);
1388 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001389
1390 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1391
1392 spin_unlock_irqrestore(&bp->lock, flags);
1393 return received;
1394 }
1395
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001396 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001397 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001398 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001399 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001400
1401 return received;
1402}
1403
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001404static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001405{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001406 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1407 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001408 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409 u32 status;
1410
1411 status = macb_readl(bp, RSR);
1412 macb_writel(bp, RSR, status);
1413
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001414 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001415 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001416
Antoine Tenart97236cd2019-06-21 17:30:02 +02001417 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001418 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001419 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001420
Nicolas Ferre8770e912013-02-12 11:08:48 +01001421 /* Packets received while interrupts were disabled */
1422 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001423 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001424 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001425 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001426 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001427 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301428 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001429 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001430 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001431
1432 /* TODO: Handle errors */
1433
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001434 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001435}
1436
Harini Katakam032dc412018-01-27 12:09:01 +05301437static void macb_hresp_error_task(unsigned long data)
1438{
1439 struct macb *bp = (struct macb *)data;
1440 struct net_device *dev = bp->dev;
1441 struct macb_queue *queue = bp->queues;
1442 unsigned int q;
1443 u32 ctrl;
1444
1445 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301446 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301447 MACB_TX_INT_FLAGS |
1448 MACB_BIT(HRESP));
1449 }
1450 ctrl = macb_readl(bp, NCR);
1451 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1452 macb_writel(bp, NCR, ctrl);
1453
1454 netif_tx_stop_all_queues(dev);
1455 netif_carrier_off(dev);
1456
1457 bp->macbgem_ops.mog_init_rings(bp);
1458
1459 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001460 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301461
Antoine Tenart6e952d92019-11-13 10:00:05 +01001462 /* Enable interrupts */
1463 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301464 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301465 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301466 MACB_TX_INT_FLAGS |
1467 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301468
1469 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1470 macb_writel(bp, NCR, ctrl);
1471
1472 netif_carrier_on(dev);
1473 netif_tx_start_all_queues(dev);
1474}
1475
Claudiu Beznea42983882018-12-17 10:02:42 +00001476static void macb_tx_restart(struct macb_queue *queue)
1477{
1478 unsigned int head = queue->tx_head;
1479 unsigned int tail = queue->tx_tail;
1480 struct macb *bp = queue->bp;
1481
1482 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1483 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1484
1485 if (head == tail)
1486 return;
1487
1488 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1489}
1490
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001491static irqreturn_t macb_interrupt(int irq, void *dev_id)
1492{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001493 struct macb_queue *queue = dev_id;
1494 struct macb *bp = queue->bp;
1495 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001496 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001497
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001498 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001499
1500 if (unlikely(!status))
1501 return IRQ_NONE;
1502
1503 spin_lock(&bp->lock);
1504
1505 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001506 /* close possible race with dev_close */
1507 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001508 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001509 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1510 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001511 break;
1512 }
1513
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001514 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1515 (unsigned int)(queue - bp->queues),
1516 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001517
Harini Katakame5010702019-01-29 15:20:03 +05301518 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001519 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001520 * until we have processed the buffers. The
1521 * scheduling call may fail if the poll routine
1522 * is already scheduled, so disable interrupts
1523 * now.
1524 */
Harini Katakame5010702019-01-29 15:20:03 +05301525 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001526 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001527 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001528
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001529 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001530 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001531 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001532 }
1533 }
1534
Nicolas Ferree86cd532012-10-31 06:04:57 +00001535 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001536 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1537 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001538
1539 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001540 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001541
Nicolas Ferree86cd532012-10-31 06:04:57 +00001542 break;
1543 }
1544
1545 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001546 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001547
Claudiu Beznea42983882018-12-17 10:02:42 +00001548 if (status & MACB_BIT(TXUBR))
1549 macb_tx_restart(queue);
1550
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001551 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001552 * add that if/when we get our hands on a full-blown MII PHY.
1553 */
1554
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001555 /* There is a hardware issue under heavy load where DMA can
1556 * stop, this causes endless "used buffer descriptor read"
1557 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301558 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1559 * section 16.7.4 for details. RXUBR is only enabled for
1560 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001561 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001562 if (status & MACB_BIT(RXUBR)) {
1563 ctrl = macb_readl(bp, NCR);
1564 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001565 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001566 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1567
1568 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001569 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001570 }
1571
Alexander Steinb19f7f72011-04-13 05:03:24 +00001572 if (status & MACB_BIT(ISR_ROVR)) {
1573 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001574 if (macb_is_gem(bp))
1575 bp->hw_stats.gem.rx_overruns++;
1576 else
1577 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001578
1579 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001580 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001581 }
1582
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001583 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301584 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001585 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001586
1587 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001588 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001589 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001590 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001591 }
1592
1593 spin_unlock(&bp->lock);
1594
1595 return IRQ_HANDLED;
1596}
1597
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001598#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001599/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001600 * to allow network i/o with interrupts disabled.
1601 */
1602static void macb_poll_controller(struct net_device *dev)
1603{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001604 struct macb *bp = netdev_priv(dev);
1605 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001606 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001607 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001608
1609 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001610 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1611 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001612 local_irq_restore(flags);
1613}
1614#endif
1615
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001616static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001617 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001618 struct sk_buff *skb,
1619 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001620{
1621 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001622 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001623 struct macb_tx_skb *tx_skb = NULL;
1624 struct macb_dma_desc *desc;
1625 unsigned int offset, size, count = 0;
1626 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001627 unsigned int eof = 1, mss_mfs = 0;
1628 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1629
1630 /* LSO */
1631 if (skb_shinfo(skb)->gso_size != 0) {
1632 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1633 /* UDP - UFO */
1634 lso_ctrl = MACB_LSO_UFO_ENABLE;
1635 else
1636 /* TCP - TSO */
1637 lso_ctrl = MACB_LSO_TSO_ENABLE;
1638 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001639
1640 /* First, map non-paged data */
1641 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001642
1643 /* first buffer length */
1644 size = hdrlen;
1645
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001646 offset = 0;
1647 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001648 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001649 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001650
1651 mapping = dma_map_single(&bp->pdev->dev,
1652 skb->data + offset,
1653 size, DMA_TO_DEVICE);
1654 if (dma_mapping_error(&bp->pdev->dev, mapping))
1655 goto dma_error;
1656
1657 /* Save info to properly release resources */
1658 tx_skb->skb = NULL;
1659 tx_skb->mapping = mapping;
1660 tx_skb->size = size;
1661 tx_skb->mapped_as_page = false;
1662
1663 len -= size;
1664 offset += size;
1665 count++;
1666 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001667
1668 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001669 }
1670
1671 /* Then, map paged data from fragments */
1672 for (f = 0; f < nr_frags; f++) {
1673 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1674
1675 len = skb_frag_size(frag);
1676 offset = 0;
1677 while (len) {
1678 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001679 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001680 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001681
1682 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1683 offset, size, DMA_TO_DEVICE);
1684 if (dma_mapping_error(&bp->pdev->dev, mapping))
1685 goto dma_error;
1686
1687 /* Save info to properly release resources */
1688 tx_skb->skb = NULL;
1689 tx_skb->mapping = mapping;
1690 tx_skb->size = size;
1691 tx_skb->mapped_as_page = true;
1692
1693 len -= size;
1694 offset += size;
1695 count++;
1696 tx_head++;
1697 }
1698 }
1699
1700 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001701 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001702 netdev_err(bp->dev, "BUG! empty skb!\n");
1703 return 0;
1704 }
1705
1706 /* This is the last buffer of the frame: save socket buffer */
1707 tx_skb->skb = skb;
1708
1709 /* Update TX ring: update buffer descriptors in reverse order
1710 * to avoid race condition
1711 */
1712
1713 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1714 * to set the end of TX queue
1715 */
1716 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001717 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001718 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001719 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001720 desc->ctrl = ctrl;
1721
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001722 if (lso_ctrl) {
1723 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1724 /* include header and FCS in value given to h/w */
1725 mss_mfs = skb_shinfo(skb)->gso_size +
1726 skb_transport_offset(skb) +
1727 ETH_FCS_LEN;
1728 else /* TSO */ {
1729 mss_mfs = skb_shinfo(skb)->gso_size;
1730 /* TCP Sequence Number Source Select
1731 * can be set only for TSO
1732 */
1733 seq_ctrl = 0;
1734 }
1735 }
1736
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001737 do {
1738 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001739 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001740 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001741 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001742
1743 ctrl = (u32)tx_skb->size;
1744 if (eof) {
1745 ctrl |= MACB_BIT(TX_LAST);
1746 eof = 0;
1747 }
Zach Brownb410d132016-10-19 09:56:57 -05001748 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001749 ctrl |= MACB_BIT(TX_WRAP);
1750
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001751 /* First descriptor is header descriptor */
1752 if (i == queue->tx_head) {
1753 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1754 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001755 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1756 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1757 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001758 } else
1759 /* Only set MSS/MFS on payload descriptors
1760 * (second or later descriptor)
1761 */
1762 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1763
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001764 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001765 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001766 /* desc->addr must be visible to hardware before clearing
1767 * 'TX_USED' bit in desc->ctrl.
1768 */
1769 wmb();
1770 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001771 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001772
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001773 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001774
1775 return count;
1776
1777dma_error:
1778 netdev_err(bp->dev, "TX DMA map failed\n");
1779
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001780 for (i = queue->tx_head; i != tx_head; i++) {
1781 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001782
1783 macb_tx_unmap(bp, tx_skb);
1784 }
1785
1786 return 0;
1787}
1788
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001789static netdev_features_t macb_features_check(struct sk_buff *skb,
1790 struct net_device *dev,
1791 netdev_features_t features)
1792{
1793 unsigned int nr_frags, f;
1794 unsigned int hdrlen;
1795
1796 /* Validate LSO compatibility */
1797
Harini Katakam41c1ef92020-02-05 18:08:11 +05301798 /* there is only one buffer or protocol is not UDP */
1799 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001800 return features;
1801
1802 /* length of header */
1803 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001804
Harini Katakam41c1ef92020-02-05 18:08:11 +05301805 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001806 * When software supplies two or more payload buffers all payload buffers
1807 * apart from the last must be a multiple of 8 bytes in size.
1808 */
1809 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1810 return features & ~MACB_NETIF_LSO;
1811
1812 nr_frags = skb_shinfo(skb)->nr_frags;
1813 /* No need to check last fragment */
1814 nr_frags--;
1815 for (f = 0; f < nr_frags; f++) {
1816 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1817
1818 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1819 return features & ~MACB_NETIF_LSO;
1820 }
1821 return features;
1822}
1823
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001824static inline int macb_clear_csum(struct sk_buff *skb)
1825{
1826 /* no change for packets without checksum offloading */
1827 if (skb->ip_summed != CHECKSUM_PARTIAL)
1828 return 0;
1829
1830 /* make sure we can modify the header */
1831 if (unlikely(skb_cow_head(skb, 0)))
1832 return -1;
1833
1834 /* initialize checksum field
1835 * This is required - at least for Zynq, which otherwise calculates
1836 * wrong UDP header checksums for UDP packets with UDP data len <=2
1837 */
1838 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1839 return 0;
1840}
1841
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001842static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1843{
1844 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1845 int padlen = ETH_ZLEN - (*skb)->len;
1846 int headroom = skb_headroom(*skb);
1847 int tailroom = skb_tailroom(*skb);
1848 struct sk_buff *nskb;
1849 u32 fcs;
1850
1851 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1852 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1853 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1854 return 0;
1855
1856 if (padlen <= 0) {
1857 /* FCS could be appeded to tailroom. */
1858 if (tailroom >= ETH_FCS_LEN)
1859 goto add_fcs;
1860 /* FCS could be appeded by moving data to headroom. */
1861 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1862 padlen = 0;
1863 /* No room for FCS, need to reallocate skb. */
1864 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001865 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001866 } else {
1867 /* Add room for FCS. */
1868 padlen += ETH_FCS_LEN;
1869 }
1870
1871 if (!cloned && headroom + tailroom >= padlen) {
1872 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1873 skb_set_tail_pointer(*skb, (*skb)->len);
1874 } else {
1875 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1876 if (!nskb)
1877 return -ENOMEM;
1878
Huang Zijiangf3e5c072019-02-14 14:41:18 +08001879 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001880 *skb = nskb;
1881 }
1882
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001883 if (padlen > ETH_FCS_LEN)
1884 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001885
1886add_fcs:
1887 /* set FCS to packet */
1888 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1889 fcs = ~fcs;
1890
1891 skb_put_u8(*skb, fcs & 0xff);
1892 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1893 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1894 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1895
1896 return 0;
1897}
1898
Claudiu Beznead1c38952018-08-07 12:25:12 +03001899static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001900{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001901 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001902 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001903 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001904 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001905 unsigned int desc_cnt, nr_frags, frag_size, f;
1906 unsigned int hdrlen;
1907 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001908 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001909
Claudiu Beznea33729f22018-08-07 12:25:13 +03001910 if (macb_clear_csum(skb)) {
1911 dev_kfree_skb_any(skb);
1912 return ret;
1913 }
1914
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001915 if (macb_pad_and_fcs(&skb, dev)) {
1916 dev_kfree_skb_any(skb);
1917 return ret;
1918 }
1919
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001920 is_lso = (skb_shinfo(skb)->gso_size != 0);
1921
1922 if (is_lso) {
1923 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1924
1925 /* length of headers */
1926 if (is_udp)
1927 /* only queue eth + ip headers separately for UDP */
1928 hdrlen = skb_transport_offset(skb);
1929 else
1930 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1931 if (skb_headlen(skb) < hdrlen) {
1932 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1933 /* if this is required, would need to copy to single buffer */
1934 return NETDEV_TX_BUSY;
1935 }
1936 } else
1937 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001938
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001939#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1940 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001941 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1942 queue_index, skb->len, skb->head, skb->data,
1943 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001944 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1945 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001946#endif
1947
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001948 /* Count how many TX buffer descriptors are needed to send this
1949 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001950 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001951 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001952 if (is_lso && (skb_headlen(skb) > hdrlen))
1953 /* extra header descriptor if also payload in first buffer */
1954 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1955 else
1956 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001957 nr_frags = skb_shinfo(skb)->nr_frags;
1958 for (f = 0; f < nr_frags; f++) {
1959 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001960 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001961 }
1962
Dongdong Deng48719532009-08-23 19:49:07 -07001963 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001964
1965 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001966 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001967 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001968 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001969 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001970 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001971 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001972 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001973 }
1974
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001975 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001976 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001977 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001978 goto unlock;
1979 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001980
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001981 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001982 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001983 skb_tx_timestamp(skb);
1984
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001985 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1986
Zach Brownb410d132016-10-19 09:56:57 -05001987 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001988 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001989
Soren Brinkmann92030902014-03-04 08:46:39 -08001990unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001991 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992
Claudiu Beznead1c38952018-08-07 12:25:12 +03001993 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001994}
1995
Nicolas Ferre4df95132013-06-04 21:57:12 +00001996static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001997{
1998 if (!macb_is_gem(bp)) {
1999 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2000 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002001 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002002
Nicolas Ferre1b447912013-06-04 21:57:11 +00002003 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002004 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002005 "RX buffer must be multiple of %d bytes, expanding\n",
2006 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002007 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002008 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002009 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002010 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002011
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002012 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002013 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002014}
2015
Nicolas Ferre4df95132013-06-04 21:57:12 +00002016static void gem_free_rx_buffers(struct macb *bp)
2017{
2018 struct sk_buff *skb;
2019 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002020 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002021 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002022 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002023 int i;
2024
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002025 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2026 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002027 continue;
2028
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002029 for (i = 0; i < bp->rx_ring_size; i++) {
2030 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002031
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002032 if (!skb)
2033 continue;
2034
2035 desc = macb_rx_desc(queue, i);
2036 addr = macb_get_addr(bp, desc);
2037
2038 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2039 DMA_FROM_DEVICE);
2040 dev_kfree_skb_any(skb);
2041 skb = NULL;
2042 }
2043
2044 kfree(queue->rx_skbuff);
2045 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002046 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002047}
2048
2049static void macb_free_rx_buffers(struct macb *bp)
2050{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002051 struct macb_queue *queue = &bp->queues[0];
2052
2053 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002054 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002055 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002056 queue->rx_buffers, queue->rx_buffers_dma);
2057 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002058 }
2059}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002060
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002061static void macb_free_consistent(struct macb *bp)
2062{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002063 struct macb_queue *queue;
2064 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302065 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002066
Nicolas Ferre4df95132013-06-04 21:57:12 +00002067 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002068
2069 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2070 kfree(queue->tx_skb);
2071 queue->tx_skb = NULL;
2072 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302073 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2074 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002075 queue->tx_ring, queue->tx_ring_dma);
2076 queue->tx_ring = NULL;
2077 }
Harini Katakame50b7702018-07-06 12:18:57 +05302078 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302079 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2080 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302081 queue->rx_ring, queue->rx_ring_dma);
2082 queue->rx_ring = NULL;
2083 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002085}
2086
2087static int gem_alloc_rx_buffers(struct macb *bp)
2088{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002089 struct macb_queue *queue;
2090 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002091 int size;
2092
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002093 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2094 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2095 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2096 if (!queue->rx_skbuff)
2097 return -ENOMEM;
2098 else
2099 netdev_dbg(bp->dev,
2100 "Allocated %d RX struct sk_buff entries at %p\n",
2101 bp->rx_ring_size, queue->rx_skbuff);
2102 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002103 return 0;
2104}
2105
2106static int macb_alloc_rx_buffers(struct macb *bp)
2107{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002108 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002109 int size;
2110
Zach Brownb410d132016-10-19 09:56:57 -05002111 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002112 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2113 &queue->rx_buffers_dma, GFP_KERNEL);
2114 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002115 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002116
2117 netdev_dbg(bp->dev,
2118 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002119 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002120 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002121}
2122
2123static int macb_alloc_consistent(struct macb *bp)
2124{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002125 struct macb_queue *queue;
2126 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002127 int size;
2128
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002129 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302130 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002131 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2132 &queue->tx_ring_dma,
2133 GFP_KERNEL);
2134 if (!queue->tx_ring)
2135 goto out_err;
2136 netdev_dbg(bp->dev,
2137 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2138 q, size, (unsigned long)queue->tx_ring_dma,
2139 queue->tx_ring);
2140
Zach Brownb410d132016-10-19 09:56:57 -05002141 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002142 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2143 if (!queue->tx_skb)
2144 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002145
Harini Katakam404cd082018-07-06 12:18:58 +05302146 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002147 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2148 &queue->rx_ring_dma, GFP_KERNEL);
2149 if (!queue->rx_ring)
2150 goto out_err;
2151 netdev_dbg(bp->dev,
2152 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2153 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002154 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002155 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002156 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002157
2158 return 0;
2159
2160out_err:
2161 macb_free_consistent(bp);
2162 return -ENOMEM;
2163}
2164
Nicolas Ferre4df95132013-06-04 21:57:12 +00002165static void gem_init_rings(struct macb *bp)
2166{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002167 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002168 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002169 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002170 int i;
2171
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002172 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002173 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002174 desc = macb_tx_desc(queue, i);
2175 macb_set_addr(bp, desc, 0);
2176 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002177 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002178 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002179 queue->tx_head = 0;
2180 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002181
2182 queue->rx_tail = 0;
2183 queue->rx_prepared_head = 0;
2184
2185 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002186 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002187
Nicolas Ferre4df95132013-06-04 21:57:12 +00002188}
2189
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002190static void macb_init_rings(struct macb *bp)
2191{
2192 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002193 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002194
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002195 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002196
Zach Brownb410d132016-10-19 09:56:57 -05002197 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002198 desc = macb_tx_desc(&bp->queues[0], i);
2199 macb_set_addr(bp, desc, 0);
2200 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002201 }
Ben Shelton21d35152015-04-22 17:28:54 -05002202 bp->queues[0].tx_head = 0;
2203 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002204 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002205}
2206
2207static void macb_reset_hw(struct macb *bp)
2208{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002209 struct macb_queue *queue;
2210 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002211 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002212
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002213 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002214 * more gracefully?)
2215 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002216 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002217
2218 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002219 ctrl |= MACB_BIT(CLRSTAT);
2220
2221 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002222
2223 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002224 macb_writel(bp, TSR, -1);
2225 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002226
2227 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002228 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2229 queue_writel(queue, IDR, -1);
2230 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002231 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2232 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002233 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002234}
2235
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002236static u32 gem_mdc_clk_div(struct macb *bp)
2237{
2238 u32 config;
2239 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2240
2241 if (pclk_hz <= 20000000)
2242 config = GEM_BF(CLK, GEM_CLK_DIV8);
2243 else if (pclk_hz <= 40000000)
2244 config = GEM_BF(CLK, GEM_CLK_DIV16);
2245 else if (pclk_hz <= 80000000)
2246 config = GEM_BF(CLK, GEM_CLK_DIV32);
2247 else if (pclk_hz <= 120000000)
2248 config = GEM_BF(CLK, GEM_CLK_DIV48);
2249 else if (pclk_hz <= 160000000)
2250 config = GEM_BF(CLK, GEM_CLK_DIV64);
2251 else
2252 config = GEM_BF(CLK, GEM_CLK_DIV96);
2253
2254 return config;
2255}
2256
2257static u32 macb_mdc_clk_div(struct macb *bp)
2258{
2259 u32 config;
2260 unsigned long pclk_hz;
2261
2262 if (macb_is_gem(bp))
2263 return gem_mdc_clk_div(bp);
2264
2265 pclk_hz = clk_get_rate(bp->pclk);
2266 if (pclk_hz <= 20000000)
2267 config = MACB_BF(CLK, MACB_CLK_DIV8);
2268 else if (pclk_hz <= 40000000)
2269 config = MACB_BF(CLK, MACB_CLK_DIV16);
2270 else if (pclk_hz <= 80000000)
2271 config = MACB_BF(CLK, MACB_CLK_DIV32);
2272 else
2273 config = MACB_BF(CLK, MACB_CLK_DIV64);
2274
2275 return config;
2276}
2277
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002278/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002279 * should program. We find the width from decoding the design configuration
2280 * register to find the maximum supported data bus width.
2281 */
2282static u32 macb_dbw(struct macb *bp)
2283{
2284 if (!macb_is_gem(bp))
2285 return 0;
2286
2287 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2288 case 4:
2289 return GEM_BF(DBW, GEM_DBW128);
2290 case 2:
2291 return GEM_BF(DBW, GEM_DBW64);
2292 case 1:
2293 default:
2294 return GEM_BF(DBW, GEM_DBW32);
2295 }
2296}
2297
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002298/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002299 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002300 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002301 * (if not supported by FIFO, it will fallback to default)
2302 * - set both rx/tx packet buffers to full memory size
2303 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002304 */
2305static void macb_configure_dma(struct macb *bp)
2306{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002307 struct macb_queue *queue;
2308 u32 buffer_size;
2309 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002310 u32 dmacfg;
2311
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002312 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002313 if (macb_is_gem(bp)) {
2314 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002315 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2316 if (q)
2317 queue_writel(queue, RBQS, buffer_size);
2318 else
2319 dmacfg |= GEM_BF(RXBS, buffer_size);
2320 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002321 if (bp->dma_burst_length)
2322 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002323 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302324 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302325
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002326 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302327 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2328 else
2329 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2330
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002331 if (bp->dev->features & NETIF_F_HW_CSUM)
2332 dmacfg |= GEM_BIT(TXCOEN);
2333 else
2334 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302335
Michal Simekbd620722018-09-25 08:32:50 +02002336 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302337#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002338 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002339 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302340#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002341#ifdef CONFIG_MACB_USE_HWSTAMP
2342 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2343 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2344#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002345 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2346 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002347 gem_writel(bp, DMACFG, dmacfg);
2348 }
2349}
2350
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002351static void macb_init_hw(struct macb *bp)
2352{
2353 u32 config;
2354
2355 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002356 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002357
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002358 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002359 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002360 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002361 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302362 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2363 else
2364 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002365 if (bp->dev->flags & IFF_PROMISC)
2366 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002367 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2368 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002369 if (!(bp->dev->flags & IFF_BROADCAST))
2370 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002371 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002372 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002373 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302374 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302375 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002376 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302377 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002378
Jamie Iles0116da42011-03-14 17:38:30 +00002379 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002380}
2381
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002382/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002383 * locations in the memory map. The least significant bits are stored
2384 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2385 *
2386 * The unicast hash enable and the multicast hash enable bits in the
2387 * network configuration register enable the reception of hash matched
2388 * frames. The destination address is reduced to a 6 bit index into
2389 * the 64 bit hash register using the following hash function. The
2390 * hash function is an exclusive or of every sixth bit of the
2391 * destination address.
2392 *
2393 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2394 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2395 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2396 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2397 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2398 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2399 *
2400 * da[0] represents the least significant bit of the first byte
2401 * received, that is, the multicast/unicast indicator, and da[47]
2402 * represents the most significant bit of the last byte received. If
2403 * the hash index, hi[n], points to a bit that is set in the hash
2404 * register then the frame will be matched according to whether the
2405 * frame is multicast or unicast. A multicast match will be signalled
2406 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2407 * index points to a bit set in the hash register. A unicast match
2408 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2409 * and the hash index points to a bit set in the hash register. To
2410 * receive all multicast frames, the hash register should be set with
2411 * all ones and the multicast hash enable bit should be set in the
2412 * network configuration register.
2413 */
2414
2415static inline int hash_bit_value(int bitnr, __u8 *addr)
2416{
2417 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2418 return 1;
2419 return 0;
2420}
2421
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002422/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002423static int hash_get_index(__u8 *addr)
2424{
2425 int i, j, bitval;
2426 int hash_index = 0;
2427
2428 for (j = 0; j < 6; j++) {
2429 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002430 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002431
2432 hash_index |= (bitval << j);
2433 }
2434
2435 return hash_index;
2436}
2437
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002438/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002439static void macb_sethashtable(struct net_device *dev)
2440{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002441 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002442 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002443 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002444 struct macb *bp = netdev_priv(dev);
2445
Moritz Fischeraa50b552016-03-29 19:11:13 -07002446 mc_filter[0] = 0;
2447 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002448
Jiri Pirko22bedad32010-04-01 21:22:57 +00002449 netdev_for_each_mc_addr(ha, dev) {
2450 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002451 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2452 }
2453
Jamie Ilesf75ba502011-11-08 10:12:32 +00002454 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2455 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002456}
2457
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002458/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002459static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002460{
2461 unsigned long cfg;
2462 struct macb *bp = netdev_priv(dev);
2463
2464 cfg = macb_readl(bp, NCFGR);
2465
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002466 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002467 /* Enable promiscuous mode */
2468 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002469
2470 /* Disable RX checksum offload */
2471 if (macb_is_gem(bp))
2472 cfg &= ~GEM_BIT(RXCOEN);
2473 } else {
2474 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002475 cfg &= ~MACB_BIT(CAF);
2476
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002477 /* Enable RX checksum offload only if requested */
2478 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2479 cfg |= GEM_BIT(RXCOEN);
2480 }
2481
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002482 if (dev->flags & IFF_ALLMULTI) {
2483 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002484 macb_or_gem_writel(bp, HRB, -1);
2485 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002486 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002487 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002488 /* Enable specific multicasts */
2489 macb_sethashtable(dev);
2490 cfg |= MACB_BIT(NCFGR_MTI);
2491 } else if (dev->flags & (~IFF_ALLMULTI)) {
2492 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002493 macb_or_gem_writel(bp, HRB, 0);
2494 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002495 cfg &= ~MACB_BIT(NCFGR_MTI);
2496 }
2497
2498 macb_writel(bp, NCFGR, cfg);
2499}
2500
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002501static int macb_open(struct net_device *dev)
2502{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002503 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002504 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002505 struct macb_queue *queue;
2506 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002507 int err;
2508
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002509 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002510
Harini Katakamd54f89a2019-03-01 16:20:34 +05302511 err = pm_runtime_get_sync(&bp->pdev->dev);
2512 if (err < 0)
2513 goto pm_exit;
2514
Nicolas Ferre1b447912013-06-04 21:57:11 +00002515 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002516 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002517
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002518 err = macb_alloc_consistent(bp);
2519 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002520 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2521 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302522 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002523 }
2524
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002525 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2526 napi_enable(&queue->napi);
2527
Harini Katakam05044532019-05-07 19:59:10 +05302528 macb_init_hw(bp);
2529
Antoine Tenart7897b072019-11-13 10:00:06 +01002530 err = macb_phylink_connect(bp);
2531 if (err)
2532 goto pm_exit;
frederic RODO6c36a702007-07-12 19:07:24 +02002533
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002534 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002535
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002536 if (bp->ptp_info)
2537 bp->ptp_info->ptp_init(dev);
2538
Harini Katakamd54f89a2019-03-01 16:20:34 +05302539pm_exit:
2540 if (err) {
2541 pm_runtime_put_sync(&bp->pdev->dev);
2542 return err;
2543 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002544 return 0;
2545}
2546
2547static int macb_close(struct net_device *dev)
2548{
2549 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002550 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002551 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002552 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002553
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002554 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002555
2556 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2557 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002558
Antoine Tenart7897b072019-11-13 10:00:06 +01002559 phylink_stop(bp->phylink);
2560 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002561
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002562 spin_lock_irqsave(&bp->lock, flags);
2563 macb_reset_hw(bp);
2564 netif_carrier_off(dev);
2565 spin_unlock_irqrestore(&bp->lock, flags);
2566
2567 macb_free_consistent(bp);
2568
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002569 if (bp->ptp_info)
2570 bp->ptp_info->ptp_remove(dev);
2571
Harini Katakamd54f89a2019-03-01 16:20:34 +05302572 pm_runtime_put(&bp->pdev->dev);
2573
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002574 return 0;
2575}
2576
Harini Katakama5898ea2015-05-06 22:27:18 +05302577static int macb_change_mtu(struct net_device *dev, int new_mtu)
2578{
Harini Katakama5898ea2015-05-06 22:27:18 +05302579 if (netif_running(dev))
2580 return -EBUSY;
2581
Harini Katakama5898ea2015-05-06 22:27:18 +05302582 dev->mtu = new_mtu;
2583
2584 return 0;
2585}
2586
Jamie Ilesa494ed82011-03-09 16:26:35 +00002587static void gem_update_stats(struct macb *bp)
2588{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002589 struct macb_queue *queue;
2590 unsigned int i, q, idx;
2591 unsigned long *stat;
2592
Jamie Ilesa494ed82011-03-09 16:26:35 +00002593 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002594
Xander Huff3ff13f12015-01-13 16:15:51 -06002595 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2596 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002597 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002598
2599 bp->ethtool_stats[i] += val;
2600 *p += val;
2601
2602 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2603 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002604 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002605 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002606 *(++p) += val;
2607 }
2608 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002609
2610 idx = GEM_STATS_LEN;
2611 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2612 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2613 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002614}
2615
2616static struct net_device_stats *gem_get_stats(struct macb *bp)
2617{
2618 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002619 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002620
2621 gem_update_stats(bp);
2622
2623 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2624 hwstat->rx_alignment_errors +
2625 hwstat->rx_resource_errors +
2626 hwstat->rx_overruns +
2627 hwstat->rx_oversize_frames +
2628 hwstat->rx_jabbers +
2629 hwstat->rx_undersized_frames +
2630 hwstat->rx_length_field_frame_errors);
2631 nstat->tx_errors = (hwstat->tx_late_collisions +
2632 hwstat->tx_excessive_collisions +
2633 hwstat->tx_underrun +
2634 hwstat->tx_carrier_sense_errors);
2635 nstat->multicast = hwstat->rx_multicast_frames;
2636 nstat->collisions = (hwstat->tx_single_collision_frames +
2637 hwstat->tx_multiple_collision_frames +
2638 hwstat->tx_excessive_collisions);
2639 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2640 hwstat->rx_jabbers +
2641 hwstat->rx_undersized_frames +
2642 hwstat->rx_length_field_frame_errors);
2643 nstat->rx_over_errors = hwstat->rx_resource_errors;
2644 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2645 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2646 nstat->rx_fifo_errors = hwstat->rx_overruns;
2647 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2648 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2649 nstat->tx_fifo_errors = hwstat->tx_underrun;
2650
2651 return nstat;
2652}
2653
Xander Huff3ff13f12015-01-13 16:15:51 -06002654static void gem_get_ethtool_stats(struct net_device *dev,
2655 struct ethtool_stats *stats, u64 *data)
2656{
2657 struct macb *bp;
2658
2659 bp = netdev_priv(dev);
2660 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002661 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2662 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002663}
2664
2665static int gem_get_sset_count(struct net_device *dev, int sset)
2666{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002667 struct macb *bp = netdev_priv(dev);
2668
Xander Huff3ff13f12015-01-13 16:15:51 -06002669 switch (sset) {
2670 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002671 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002672 default:
2673 return -EOPNOTSUPP;
2674 }
2675}
2676
2677static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2678{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002679 char stat_string[ETH_GSTRING_LEN];
2680 struct macb *bp = netdev_priv(dev);
2681 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002682 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002683 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002684
2685 switch (sset) {
2686 case ETH_SS_STATS:
2687 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2688 memcpy(p, gem_statistics[i].stat_string,
2689 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002690
2691 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2692 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2693 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2694 q, queue_statistics[i].stat_string);
2695 memcpy(p, stat_string, ETH_GSTRING_LEN);
2696 }
2697 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002698 break;
2699 }
2700}
2701
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002702static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002703{
2704 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002705 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002706 struct macb_stats *hwstat = &bp->hw_stats.macb;
2707
2708 if (macb_is_gem(bp))
2709 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002710
frederic RODO6c36a702007-07-12 19:07:24 +02002711 /* read stats from hardware */
2712 macb_update_stats(bp);
2713
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002714 /* Convert HW stats into netdevice stats */
2715 nstat->rx_errors = (hwstat->rx_fcs_errors +
2716 hwstat->rx_align_errors +
2717 hwstat->rx_resource_errors +
2718 hwstat->rx_overruns +
2719 hwstat->rx_oversize_pkts +
2720 hwstat->rx_jabbers +
2721 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002722 hwstat->rx_length_mismatch);
2723 nstat->tx_errors = (hwstat->tx_late_cols +
2724 hwstat->tx_excessive_cols +
2725 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002726 hwstat->tx_carrier_errors +
2727 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002728 nstat->collisions = (hwstat->tx_single_cols +
2729 hwstat->tx_multiple_cols +
2730 hwstat->tx_excessive_cols);
2731 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2732 hwstat->rx_jabbers +
2733 hwstat->rx_undersize_pkts +
2734 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002735 nstat->rx_over_errors = hwstat->rx_resource_errors +
2736 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002737 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2738 nstat->rx_frame_errors = hwstat->rx_align_errors;
2739 nstat->rx_fifo_errors = hwstat->rx_overruns;
2740 /* XXX: What does "missed" mean? */
2741 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2742 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2743 nstat->tx_fifo_errors = hwstat->tx_underruns;
2744 /* Don't know about heartbeat or window errors... */
2745
2746 return nstat;
2747}
2748
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002749static int macb_get_regs_len(struct net_device *netdev)
2750{
2751 return MACB_GREGS_NBR * sizeof(u32);
2752}
2753
2754static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2755 void *p)
2756{
2757 struct macb *bp = netdev_priv(dev);
2758 unsigned int tail, head;
2759 u32 *regs_buff = p;
2760
2761 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2762 | MACB_GREGS_VERSION;
2763
Zach Brownb410d132016-10-19 09:56:57 -05002764 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2765 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002766
2767 regs_buff[0] = macb_readl(bp, NCR);
2768 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2769 regs_buff[2] = macb_readl(bp, NSR);
2770 regs_buff[3] = macb_readl(bp, TSR);
2771 regs_buff[4] = macb_readl(bp, RBQP);
2772 regs_buff[5] = macb_readl(bp, TBQP);
2773 regs_buff[6] = macb_readl(bp, RSR);
2774 regs_buff[7] = macb_readl(bp, IMR);
2775
2776 regs_buff[8] = tail;
2777 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002778 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2779 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002780
Neil Armstrongce721a72016-01-05 14:39:16 +01002781 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2782 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002783 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002784 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002785}
2786
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002787static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2788{
2789 struct macb *bp = netdev_priv(netdev);
2790
2791 wol->supported = 0;
2792 wol->wolopts = 0;
2793
Antoine Tenart7897b072019-11-13 10:00:06 +01002794 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET)
2795 phylink_ethtool_get_wol(bp->phylink, wol);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002796}
2797
2798static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2799{
2800 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01002801 int ret;
2802
2803 ret = phylink_ethtool_set_wol(bp->phylink, wol);
2804 if (!ret)
2805 return 0;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002806
2807 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2808 (wol->wolopts & ~WAKE_MAGIC))
2809 return -EOPNOTSUPP;
2810
2811 if (wol->wolopts & WAKE_MAGIC)
2812 bp->wol |= MACB_WOL_ENABLED;
2813 else
2814 bp->wol &= ~MACB_WOL_ENABLED;
2815
2816 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2817
2818 return 0;
2819}
2820
Antoine Tenart7897b072019-11-13 10:00:06 +01002821static int macb_get_link_ksettings(struct net_device *netdev,
2822 struct ethtool_link_ksettings *kset)
2823{
2824 struct macb *bp = netdev_priv(netdev);
2825
2826 return phylink_ethtool_ksettings_get(bp->phylink, kset);
2827}
2828
2829static int macb_set_link_ksettings(struct net_device *netdev,
2830 const struct ethtool_link_ksettings *kset)
2831{
2832 struct macb *bp = netdev_priv(netdev);
2833
2834 return phylink_ethtool_ksettings_set(bp->phylink, kset);
2835}
2836
Zach Brown8441bb32016-10-19 09:56:58 -05002837static void macb_get_ringparam(struct net_device *netdev,
2838 struct ethtool_ringparam *ring)
2839{
2840 struct macb *bp = netdev_priv(netdev);
2841
2842 ring->rx_max_pending = MAX_RX_RING_SIZE;
2843 ring->tx_max_pending = MAX_TX_RING_SIZE;
2844
2845 ring->rx_pending = bp->rx_ring_size;
2846 ring->tx_pending = bp->tx_ring_size;
2847}
2848
2849static int macb_set_ringparam(struct net_device *netdev,
2850 struct ethtool_ringparam *ring)
2851{
2852 struct macb *bp = netdev_priv(netdev);
2853 u32 new_rx_size, new_tx_size;
2854 unsigned int reset = 0;
2855
2856 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2857 return -EINVAL;
2858
2859 new_rx_size = clamp_t(u32, ring->rx_pending,
2860 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2861 new_rx_size = roundup_pow_of_two(new_rx_size);
2862
2863 new_tx_size = clamp_t(u32, ring->tx_pending,
2864 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2865 new_tx_size = roundup_pow_of_two(new_tx_size);
2866
2867 if ((new_tx_size == bp->tx_ring_size) &&
2868 (new_rx_size == bp->rx_ring_size)) {
2869 /* nothing to do */
2870 return 0;
2871 }
2872
2873 if (netif_running(bp->dev)) {
2874 reset = 1;
2875 macb_close(bp->dev);
2876 }
2877
2878 bp->rx_ring_size = new_rx_size;
2879 bp->tx_ring_size = new_tx_size;
2880
2881 if (reset)
2882 macb_open(bp->dev);
2883
2884 return 0;
2885}
2886
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002887#ifdef CONFIG_MACB_USE_HWSTAMP
2888static unsigned int gem_get_tsu_rate(struct macb *bp)
2889{
2890 struct clk *tsu_clk;
2891 unsigned int tsu_rate;
2892
2893 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2894 if (!IS_ERR(tsu_clk))
2895 tsu_rate = clk_get_rate(tsu_clk);
2896 /* try pclk instead */
2897 else if (!IS_ERR(bp->pclk)) {
2898 tsu_clk = bp->pclk;
2899 tsu_rate = clk_get_rate(tsu_clk);
2900 } else
2901 return -ENOTSUPP;
2902 return tsu_rate;
2903}
2904
2905static s32 gem_get_ptp_max_adj(void)
2906{
2907 return 64000000;
2908}
2909
2910static int gem_get_ts_info(struct net_device *dev,
2911 struct ethtool_ts_info *info)
2912{
2913 struct macb *bp = netdev_priv(dev);
2914
2915 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2916 ethtool_op_get_ts_info(dev, info);
2917 return 0;
2918 }
2919
2920 info->so_timestamping =
2921 SOF_TIMESTAMPING_TX_SOFTWARE |
2922 SOF_TIMESTAMPING_RX_SOFTWARE |
2923 SOF_TIMESTAMPING_SOFTWARE |
2924 SOF_TIMESTAMPING_TX_HARDWARE |
2925 SOF_TIMESTAMPING_RX_HARDWARE |
2926 SOF_TIMESTAMPING_RAW_HARDWARE;
2927 info->tx_types =
2928 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2929 (1 << HWTSTAMP_TX_OFF) |
2930 (1 << HWTSTAMP_TX_ON);
2931 info->rx_filters =
2932 (1 << HWTSTAMP_FILTER_NONE) |
2933 (1 << HWTSTAMP_FILTER_ALL);
2934
2935 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2936
2937 return 0;
2938}
2939
2940static struct macb_ptp_info gem_ptp_info = {
2941 .ptp_init = gem_ptp_init,
2942 .ptp_remove = gem_ptp_remove,
2943 .get_ptp_max_adj = gem_get_ptp_max_adj,
2944 .get_tsu_rate = gem_get_tsu_rate,
2945 .get_ts_info = gem_get_ts_info,
2946 .get_hwtst = gem_get_hwtst,
2947 .set_hwtst = gem_set_hwtst,
2948};
2949#endif
2950
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002951static int macb_get_ts_info(struct net_device *netdev,
2952 struct ethtool_ts_info *info)
2953{
2954 struct macb *bp = netdev_priv(netdev);
2955
2956 if (bp->ptp_info)
2957 return bp->ptp_info->get_ts_info(netdev, info);
2958
2959 return ethtool_op_get_ts_info(netdev, info);
2960}
2961
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002962static void gem_enable_flow_filters(struct macb *bp, bool enable)
2963{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002964 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002965 struct ethtool_rx_fs_item *item;
2966 u32 t2_scr;
2967 int num_t2_scr;
2968
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002969 if (!(netdev->features & NETIF_F_NTUPLE))
2970 return;
2971
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002972 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2973
2974 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2975 struct ethtool_rx_flow_spec *fs = &item->fs;
2976 struct ethtool_tcpip4_spec *tp4sp_m;
2977
2978 if (fs->location >= num_t2_scr)
2979 continue;
2980
2981 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2982
2983 /* enable/disable screener regs for the flow entry */
2984 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2985
2986 /* only enable fields with no masking */
2987 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2988
2989 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2990 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2991 else
2992 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2993
2994 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2995 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2996 else
2997 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2998
2999 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3000 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3001 else
3002 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3003
3004 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3005 }
3006}
3007
3008static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3009{
3010 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3011 uint16_t index = fs->location;
3012 u32 w0, w1, t2_scr;
3013 bool cmp_a = false;
3014 bool cmp_b = false;
3015 bool cmp_c = false;
3016
3017 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3018 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3019
3020 /* ignore field if any masking set */
3021 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3022 /* 1st compare reg - IP source address */
3023 w0 = 0;
3024 w1 = 0;
3025 w0 = tp4sp_v->ip4src;
3026 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3027 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3028 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3029 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3030 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3031 cmp_a = true;
3032 }
3033
3034 /* ignore field if any masking set */
3035 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3036 /* 2nd compare reg - IP destination address */
3037 w0 = 0;
3038 w1 = 0;
3039 w0 = tp4sp_v->ip4dst;
3040 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3041 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3042 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3043 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3044 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3045 cmp_b = true;
3046 }
3047
3048 /* ignore both port fields if masking set in both */
3049 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3050 /* 3rd compare reg - source port, destination port */
3051 w0 = 0;
3052 w1 = 0;
3053 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3054 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3055 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3056 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3057 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3058 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3059 } else {
3060 /* only one port definition */
3061 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3062 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3063 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3064 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3065 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3066 } else { /* dst port */
3067 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3068 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3069 }
3070 }
3071 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3072 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3073 cmp_c = true;
3074 }
3075
3076 t2_scr = 0;
3077 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3078 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3079 if (cmp_a)
3080 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3081 if (cmp_b)
3082 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3083 if (cmp_c)
3084 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3085 gem_writel_n(bp, SCRT2, index, t2_scr);
3086}
3087
3088static int gem_add_flow_filter(struct net_device *netdev,
3089 struct ethtool_rxnfc *cmd)
3090{
3091 struct macb *bp = netdev_priv(netdev);
3092 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3093 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003094 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003095 int ret = -EINVAL;
3096 bool added = false;
3097
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003098 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003099 if (newfs == NULL)
3100 return -ENOMEM;
3101 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3102
3103 netdev_dbg(netdev,
3104 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3105 fs->flow_type, (int)fs->ring_cookie, fs->location,
3106 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3107 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3108 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3109
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003110 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3111
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003112 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003113 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3114 if (item->fs.location > newfs->fs.location) {
3115 list_add_tail(&newfs->list, &item->list);
3116 added = true;
3117 break;
3118 } else if (item->fs.location == fs->location) {
3119 netdev_err(netdev, "Rule not added: location %d not free!\n",
3120 fs->location);
3121 ret = -EBUSY;
3122 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003123 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003124 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003125 if (!added)
3126 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003127
3128 gem_prog_cmp_regs(bp, fs);
3129 bp->rx_fs_list.count++;
3130 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003131 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003132
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003133 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003134 return 0;
3135
3136err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003137 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003138 kfree(newfs);
3139 return ret;
3140}
3141
3142static int gem_del_flow_filter(struct net_device *netdev,
3143 struct ethtool_rxnfc *cmd)
3144{
3145 struct macb *bp = netdev_priv(netdev);
3146 struct ethtool_rx_fs_item *item;
3147 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003148 unsigned long flags;
3149
3150 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003151
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003152 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3153 if (item->fs.location == cmd->fs.location) {
3154 /* disable screener regs for the flow entry */
3155 fs = &(item->fs);
3156 netdev_dbg(netdev,
3157 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3158 fs->flow_type, (int)fs->ring_cookie, fs->location,
3159 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3160 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3161 htons(fs->h_u.tcp_ip4_spec.psrc),
3162 htons(fs->h_u.tcp_ip4_spec.pdst));
3163
3164 gem_writel_n(bp, SCRT2, fs->location, 0);
3165
3166 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003167 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003168 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3169 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003170 return 0;
3171 }
3172 }
3173
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003174 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003175 return -EINVAL;
3176}
3177
3178static int gem_get_flow_entry(struct net_device *netdev,
3179 struct ethtool_rxnfc *cmd)
3180{
3181 struct macb *bp = netdev_priv(netdev);
3182 struct ethtool_rx_fs_item *item;
3183
3184 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3185 if (item->fs.location == cmd->fs.location) {
3186 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3187 return 0;
3188 }
3189 }
3190 return -EINVAL;
3191}
3192
3193static int gem_get_all_flow_entries(struct net_device *netdev,
3194 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3195{
3196 struct macb *bp = netdev_priv(netdev);
3197 struct ethtool_rx_fs_item *item;
3198 uint32_t cnt = 0;
3199
3200 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3201 if (cnt == cmd->rule_cnt)
3202 return -EMSGSIZE;
3203 rule_locs[cnt] = item->fs.location;
3204 cnt++;
3205 }
3206 cmd->data = bp->max_tuples;
3207 cmd->rule_cnt = cnt;
3208
3209 return 0;
3210}
3211
3212static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3213 u32 *rule_locs)
3214{
3215 struct macb *bp = netdev_priv(netdev);
3216 int ret = 0;
3217
3218 switch (cmd->cmd) {
3219 case ETHTOOL_GRXRINGS:
3220 cmd->data = bp->num_queues;
3221 break;
3222 case ETHTOOL_GRXCLSRLCNT:
3223 cmd->rule_cnt = bp->rx_fs_list.count;
3224 break;
3225 case ETHTOOL_GRXCLSRULE:
3226 ret = gem_get_flow_entry(netdev, cmd);
3227 break;
3228 case ETHTOOL_GRXCLSRLALL:
3229 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3230 break;
3231 default:
3232 netdev_err(netdev,
3233 "Command parameter %d is not supported\n", cmd->cmd);
3234 ret = -EOPNOTSUPP;
3235 }
3236
3237 return ret;
3238}
3239
3240static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3241{
3242 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003243 int ret;
3244
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003245 switch (cmd->cmd) {
3246 case ETHTOOL_SRXCLSRLINS:
3247 if ((cmd->fs.location >= bp->max_tuples)
3248 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3249 ret = -EINVAL;
3250 break;
3251 }
3252 ret = gem_add_flow_filter(netdev, cmd);
3253 break;
3254 case ETHTOOL_SRXCLSRLDEL:
3255 ret = gem_del_flow_filter(netdev, cmd);
3256 break;
3257 default:
3258 netdev_err(netdev,
3259 "Command parameter %d is not supported\n", cmd->cmd);
3260 ret = -EOPNOTSUPP;
3261 }
3262
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003263 return ret;
3264}
3265
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003266static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003267 .get_regs_len = macb_get_regs_len,
3268 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003269 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003270 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003271 .get_wol = macb_get_wol,
3272 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003273 .get_link_ksettings = macb_get_link_ksettings,
3274 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003275 .get_ringparam = macb_get_ringparam,
3276 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003277};
Xander Huff8cd5a562015-01-15 15:55:20 -06003278
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003279static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003280 .get_regs_len = macb_get_regs_len,
3281 .get_regs = macb_get_regs,
3282 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003283 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003284 .get_ethtool_stats = gem_get_ethtool_stats,
3285 .get_strings = gem_get_ethtool_strings,
3286 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003287 .get_link_ksettings = macb_get_link_ksettings,
3288 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003289 .get_ringparam = macb_get_ringparam,
3290 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003291 .get_rxnfc = gem_get_rxnfc,
3292 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003293};
3294
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003295static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003296{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003297 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003298
3299 if (!netif_running(dev))
3300 return -EINVAL;
3301
Antoine Tenart7897b072019-11-13 10:00:06 +01003302 if (bp->ptp_info) {
3303 switch (cmd) {
3304 case SIOCSHWTSTAMP:
3305 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3306 case SIOCGHWTSTAMP:
3307 return bp->ptp_info->get_hwtst(dev, rq);
3308 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003309 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003310
3311 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003312}
3313
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003314static inline void macb_set_txcsum_feature(struct macb *bp,
3315 netdev_features_t features)
3316{
3317 u32 val;
3318
3319 if (!macb_is_gem(bp))
3320 return;
3321
3322 val = gem_readl(bp, DMACFG);
3323 if (features & NETIF_F_HW_CSUM)
3324 val |= GEM_BIT(TXCOEN);
3325 else
3326 val &= ~GEM_BIT(TXCOEN);
3327
3328 gem_writel(bp, DMACFG, val);
3329}
3330
3331static inline void macb_set_rxcsum_feature(struct macb *bp,
3332 netdev_features_t features)
3333{
3334 struct net_device *netdev = bp->dev;
3335 u32 val;
3336
3337 if (!macb_is_gem(bp))
3338 return;
3339
3340 val = gem_readl(bp, NCFGR);
3341 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3342 val |= GEM_BIT(RXCOEN);
3343 else
3344 val &= ~GEM_BIT(RXCOEN);
3345
3346 gem_writel(bp, NCFGR, val);
3347}
3348
3349static inline void macb_set_rxflow_feature(struct macb *bp,
3350 netdev_features_t features)
3351{
3352 if (!macb_is_gem(bp))
3353 return;
3354
3355 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3356}
3357
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003358static int macb_set_features(struct net_device *netdev,
3359 netdev_features_t features)
3360{
3361 struct macb *bp = netdev_priv(netdev);
3362 netdev_features_t changed = features ^ netdev->features;
3363
3364 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003365 if (changed & NETIF_F_HW_CSUM)
3366 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003367
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003368 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003369 if (changed & NETIF_F_RXCSUM)
3370 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003371
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003372 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003373 if (changed & NETIF_F_NTUPLE)
3374 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003375
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003376 return 0;
3377}
3378
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003379static void macb_restore_features(struct macb *bp)
3380{
3381 struct net_device *netdev = bp->dev;
3382 netdev_features_t features = netdev->features;
3383
3384 /* TX checksum offload */
3385 macb_set_txcsum_feature(bp, features);
3386
3387 /* RX checksum offload */
3388 macb_set_rxcsum_feature(bp, features);
3389
3390 /* RX Flow Filters */
3391 macb_set_rxflow_feature(bp, features);
3392}
3393
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003394static const struct net_device_ops macb_netdev_ops = {
3395 .ndo_open = macb_open,
3396 .ndo_stop = macb_close,
3397 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003398 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003399 .ndo_get_stats = macb_get_stats,
3400 .ndo_do_ioctl = macb_ioctl,
3401 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303402 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003403 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003404#ifdef CONFIG_NET_POLL_CONTROLLER
3405 .ndo_poll_controller = macb_poll_controller,
3406#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003407 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003408 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003409};
3410
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003411/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003412 * and integration options used
3413 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003414static void macb_configure_caps(struct macb *bp,
3415 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003416{
3417 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003418
Nicolas Ferref6970502015-03-31 15:02:01 +02003419 if (dt_conf)
3420 bp->caps = dt_conf->caps;
3421
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003422 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003423 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3424
Nicolas Ferree1755872014-07-24 13:50:58 +02003425 dcfg = gem_readl(bp, DCFG1);
3426 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3427 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3428 dcfg = gem_readl(bp, DCFG2);
3429 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3430 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003431#ifdef CONFIG_MACB_USE_HWSTAMP
3432 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003433 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003434 dev_err(&bp->pdev->dev,
3435 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003436 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003437 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003438 bp->ptp_info = &gem_ptp_info;
3439 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003440 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003441#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003442 }
3443
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003444 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003445}
3446
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003447static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003448 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003449 unsigned int *queue_mask,
3450 unsigned int *num_queues)
3451{
3452 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003453
3454 *queue_mask = 0x1;
3455 *num_queues = 1;
3456
Nicolas Ferreda120112015-03-31 15:02:00 +02003457 /* is it macb or gem ?
3458 *
3459 * We need to read directly from the hardware here because
3460 * we are early in the probe process and don't have the
3461 * MACB_CAPS_MACB_IS_GEM flag positioned
3462 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003463 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003464 return;
3465
3466 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303467 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3468
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003469 *queue_mask |= 0x1;
3470
3471 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3472 if (*queue_mask & (1 << hw_q))
3473 (*num_queues)++;
3474}
3475
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003476static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303477 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303478 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003479{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003480 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003481 int err;
3482
Bartosz Folta83a77e92016-12-14 06:39:15 +00003483 pdata = dev_get_platdata(&pdev->dev);
3484 if (pdata) {
3485 *pclk = pdata->pclk;
3486 *hclk = pdata->hclk;
3487 } else {
3488 *pclk = devm_clk_get(&pdev->dev, "pclk");
3489 *hclk = devm_clk_get(&pdev->dev, "hclk");
3490 }
3491
Harini Katakamcd5afa92019-03-20 19:12:22 +05303492 if (IS_ERR_OR_NULL(*pclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003493 err = PTR_ERR(*pclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303494 if (!err)
3495 err = -ENODEV;
3496
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003497 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003498 return err;
3499 }
3500
Harini Katakamcd5afa92019-03-20 19:12:22 +05303501 if (IS_ERR_OR_NULL(*hclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003502 err = PTR_ERR(*hclk);
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 hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003507 return err;
3508 }
3509
Michael Tretterbd310aca2019-10-18 16:11:43 +02003510 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003511 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003512 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003513
Michael Tretterbd310aca2019-10-18 16:11:43 +02003514 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303515 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003516 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303517
Michael Tretterbd310aca2019-10-18 16:11:43 +02003518 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303519 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003520 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303521
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003522 err = clk_prepare_enable(*pclk);
3523 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003524 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003525 return err;
3526 }
3527
3528 err = clk_prepare_enable(*hclk);
3529 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003530 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003531 goto err_disable_pclk;
3532 }
3533
3534 err = clk_prepare_enable(*tx_clk);
3535 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003536 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003537 goto err_disable_hclk;
3538 }
3539
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303540 err = clk_prepare_enable(*rx_clk);
3541 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003542 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303543 goto err_disable_txclk;
3544 }
3545
Harini Katakamf5473d12019-03-01 16:20:33 +05303546 err = clk_prepare_enable(*tsu_clk);
3547 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003548 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303549 goto err_disable_rxclk;
3550 }
3551
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003552 return 0;
3553
Harini Katakamf5473d12019-03-01 16:20:33 +05303554err_disable_rxclk:
3555 clk_disable_unprepare(*rx_clk);
3556
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303557err_disable_txclk:
3558 clk_disable_unprepare(*tx_clk);
3559
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003560err_disable_hclk:
3561 clk_disable_unprepare(*hclk);
3562
3563err_disable_pclk:
3564 clk_disable_unprepare(*pclk);
3565
3566 return err;
3567}
3568
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003569static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003570{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003571 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003572 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003573 struct macb *bp = netdev_priv(dev);
3574 struct macb_queue *queue;
3575 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003576 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003577
Zach Brownb410d132016-10-19 09:56:57 -05003578 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3579 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3580
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003581 /* set the queue register mapping once for all: queue0 has a special
3582 * register mapping but we don't want to test the queue index then
3583 * compute the corresponding register offset at run time.
3584 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003585 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003586 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003587 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003588
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003589 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003590 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003591 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003592 if (hw_q) {
3593 queue->ISR = GEM_ISR(hw_q - 1);
3594 queue->IER = GEM_IER(hw_q - 1);
3595 queue->IDR = GEM_IDR(hw_q - 1);
3596 queue->IMR = GEM_IMR(hw_q - 1);
3597 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003598 queue->RBQP = GEM_RBQP(hw_q - 1);
3599 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303600#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003601 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003602 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003603 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3604 }
Harini Katakamfff80192016-08-09 13:15:53 +05303605#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003606 } else {
3607 /* queue0 uses legacy registers */
3608 queue->ISR = MACB_ISR;
3609 queue->IER = MACB_IER;
3610 queue->IDR = MACB_IDR;
3611 queue->IMR = MACB_IMR;
3612 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003613 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303614#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003615 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003616 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003617 queue->RBQPH = MACB_RBQPH;
3618 }
Harini Katakamfff80192016-08-09 13:15:53 +05303619#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003620 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003621
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003622 /* get irq: here we use the linux queue index, not the hardware
3623 * queue index. the queue irq definitions in the device tree
3624 * must remove the optional gaps that could exist in the
3625 * hardware queue mask.
3626 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003627 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003628 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003629 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003630 if (err) {
3631 dev_err(&pdev->dev,
3632 "Unable to request IRQ %d (error %d)\n",
3633 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003634 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003635 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003636
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003637 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003638 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003639 }
3640
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003641 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003642
Nicolas Ferre4df95132013-06-04 21:57:12 +00003643 /* setup appropriated routines according to adapter type */
3644 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003645 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003646 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3647 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3648 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3649 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003650 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003651 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003652 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003653 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3654 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3655 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3656 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003657 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003658 }
3659
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003660 /* Set features */
3661 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003662
3663 /* Check LSO capability */
3664 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3665 dev->hw_features |= MACB_NETIF_LSO;
3666
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003667 /* Checksum offload is only available on gem with packet buffer */
3668 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003669 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003670 if (bp->caps & MACB_CAPS_SG_DISABLED)
3671 dev->hw_features &= ~NETIF_F_SG;
3672 dev->features = dev->hw_features;
3673
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003674 /* Check RX Flow Filters support.
3675 * Max Rx flows set by availability of screeners & compare regs:
3676 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3677 */
3678 reg = gem_readl(bp, DCFG8);
3679 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3680 GEM_BFEXT(T2SCR, reg));
3681 if (bp->max_tuples > 0) {
3682 /* also needs one ethtype match to check IPv4 */
3683 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3684 /* program this reg now */
3685 reg = 0;
3686 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3687 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3688 /* Filtering is supported in hw but don't enable it in kernel now */
3689 dev->hw_features |= NETIF_F_NTUPLE;
3690 /* init Rx flow definitions */
3691 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3692 bp->rx_fs_list.count = 0;
3693 spin_lock_init(&bp->rx_fs_lock);
3694 } else
3695 bp->max_tuples = 0;
3696 }
3697
Neil Armstrongce721a72016-01-05 14:39:16 +01003698 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3699 val = 0;
3700 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3701 val = GEM_BIT(RGMII);
3702 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003703 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003704 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003705 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003706 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003707
Neil Armstrongce721a72016-01-05 14:39:16 +01003708 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3709 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003710
Neil Armstrongce721a72016-01-05 14:39:16 +01003711 macb_or_gem_writel(bp, USRIO, val);
3712 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003713
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003714 /* Set MII management clock divider */
3715 val = macb_mdc_clk_div(bp);
3716 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303717 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3718 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003719 macb_writel(bp, NCFGR, val);
3720
3721 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003722}
3723
3724#if defined(CONFIG_OF)
3725/* 1518 rounded up */
3726#define AT91ETHER_MAX_RBUFF_SZ 0x600
3727/* max number of receive buffers */
3728#define AT91ETHER_MAX_RX_DESCR 9
3729
Arnd Bergmann49db9222019-07-08 14:48:23 +02003730static struct sifive_fu540_macb_mgmt *mgmt;
3731
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003732/* Initialize and start the Receiver and Transmit subsystems */
3733static int at91ether_start(struct net_device *dev)
3734{
3735 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003736 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003737 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003738 dma_addr_t addr;
3739 u32 ctl;
3740 int i;
3741
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003742 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003743 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003744 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003745 &q->rx_ring_dma, GFP_KERNEL);
3746 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003747 return -ENOMEM;
3748
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003749 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003750 AT91ETHER_MAX_RX_DESCR *
3751 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003752 &q->rx_buffers_dma, GFP_KERNEL);
3753 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003754 dma_free_coherent(&lp->pdev->dev,
3755 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003756 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003757 q->rx_ring, q->rx_ring_dma);
3758 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003759 return -ENOMEM;
3760 }
3761
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003762 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003763 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003764 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003765 macb_set_addr(lp, desc, addr);
3766 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003767 addr += AT91ETHER_MAX_RBUFF_SZ;
3768 }
3769
3770 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003771 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003772
3773 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003774 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003775
3776 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003777 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003778
3779 /* Enable Receive and Transmit */
3780 ctl = macb_readl(lp, NCR);
3781 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3782
3783 return 0;
3784}
3785
3786/* Open the ethernet interface */
3787static int at91ether_open(struct net_device *dev)
3788{
3789 struct macb *lp = netdev_priv(dev);
3790 u32 ctl;
3791 int ret;
3792
3793 /* Clear internal statistics */
3794 ctl = macb_readl(lp, NCR);
3795 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3796
3797 macb_set_hwaddr(lp);
3798
3799 ret = at91ether_start(dev);
3800 if (ret)
3801 return ret;
3802
3803 /* Enable MAC interrupts */
3804 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3805 MACB_BIT(RXUBR) |
3806 MACB_BIT(ISR_TUND) |
3807 MACB_BIT(ISR_RLE) |
3808 MACB_BIT(TCOMP) |
3809 MACB_BIT(ISR_ROVR) |
3810 MACB_BIT(HRESP));
3811
Antoine Tenart7897b072019-11-13 10:00:06 +01003812 ret = macb_phylink_connect(lp);
3813 if (ret)
3814 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003815
3816 netif_start_queue(dev);
3817
3818 return 0;
3819}
3820
3821/* Close the interface */
3822static int at91ether_close(struct net_device *dev)
3823{
3824 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003825 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003826 u32 ctl;
3827
3828 /* Disable Receiver and Transmitter */
3829 ctl = macb_readl(lp, NCR);
3830 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3831
3832 /* Disable MAC interrupts */
3833 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3834 MACB_BIT(RXUBR) |
3835 MACB_BIT(ISR_TUND) |
3836 MACB_BIT(ISR_RLE) |
3837 MACB_BIT(TCOMP) |
3838 MACB_BIT(ISR_ROVR) |
3839 MACB_BIT(HRESP));
3840
3841 netif_stop_queue(dev);
3842
Antoine Tenart7897b072019-11-13 10:00:06 +01003843 phylink_stop(lp->phylink);
3844 phylink_disconnect_phy(lp->phylink);
3845
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003846 dma_free_coherent(&lp->pdev->dev,
3847 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003848 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003849 q->rx_ring, q->rx_ring_dma);
3850 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003851
3852 dma_free_coherent(&lp->pdev->dev,
3853 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003854 q->rx_buffers, q->rx_buffers_dma);
3855 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003856
3857 return 0;
3858}
3859
3860/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003861static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3862 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003863{
3864 struct macb *lp = netdev_priv(dev);
3865
3866 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3867 netif_stop_queue(dev);
3868
3869 /* Store packet information (to free when Tx completed) */
3870 lp->skb = skb;
3871 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003872 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3873 skb->len, DMA_TO_DEVICE);
3874 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003875 dev_kfree_skb_any(skb);
3876 dev->stats.tx_dropped++;
3877 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3878 return NETDEV_TX_OK;
3879 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003880
3881 /* Set address of the data in the Transmit Address register */
3882 macb_writel(lp, TAR, lp->skb_physaddr);
3883 /* Set length of the packet in the Transmit Control register */
3884 macb_writel(lp, TCR, skb->len);
3885
3886 } else {
3887 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3888 return NETDEV_TX_BUSY;
3889 }
3890
3891 return NETDEV_TX_OK;
3892}
3893
3894/* Extract received frame from buffer descriptors and sent to upper layers.
3895 * (Called from interrupt context)
3896 */
3897static void at91ether_rx(struct net_device *dev)
3898{
3899 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003900 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003901 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003902 unsigned char *p_recv;
3903 struct sk_buff *skb;
3904 unsigned int pktlen;
3905
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003906 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003907 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003908 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003909 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003910 skb = netdev_alloc_skb(dev, pktlen + 2);
3911 if (skb) {
3912 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003913 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003914
3915 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003916 dev->stats.rx_packets++;
3917 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003918 netif_rx(skb);
3919 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003920 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003921 }
3922
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003923 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003924 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003925
3926 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003927 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003928
3929 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003930 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3931 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003932 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003933 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003934
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003935 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003936 }
3937}
3938
3939/* MAC interrupt handler */
3940static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3941{
3942 struct net_device *dev = dev_id;
3943 struct macb *lp = netdev_priv(dev);
3944 u32 intstatus, ctl;
3945
3946 /* MAC Interrupt Status register indicates what interrupts are pending.
3947 * It is automatically cleared once read.
3948 */
3949 intstatus = macb_readl(lp, ISR);
3950
3951 /* Receive complete */
3952 if (intstatus & MACB_BIT(RCOMP))
3953 at91ether_rx(dev);
3954
3955 /* Transmit complete */
3956 if (intstatus & MACB_BIT(TCOMP)) {
3957 /* The TCOM bit is set even if the transmission failed */
3958 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003959 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003960
3961 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003962 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003963 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003964 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003965 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003966 dev->stats.tx_packets++;
3967 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003968 }
3969 netif_wake_queue(dev);
3970 }
3971
3972 /* Work-around for EMAC Errata section 41.3.1 */
3973 if (intstatus & MACB_BIT(RXUBR)) {
3974 ctl = macb_readl(lp, NCR);
3975 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003976 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003977 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3978 }
3979
3980 if (intstatus & MACB_BIT(ISR_ROVR))
3981 netdev_err(dev, "ROVR error\n");
3982
3983 return IRQ_HANDLED;
3984}
3985
3986#ifdef CONFIG_NET_POLL_CONTROLLER
3987static void at91ether_poll_controller(struct net_device *dev)
3988{
3989 unsigned long flags;
3990
3991 local_irq_save(flags);
3992 at91ether_interrupt(dev->irq, dev);
3993 local_irq_restore(flags);
3994}
3995#endif
3996
3997static const struct net_device_ops at91ether_netdev_ops = {
3998 .ndo_open = at91ether_open,
3999 .ndo_stop = at91ether_close,
4000 .ndo_start_xmit = at91ether_start_xmit,
4001 .ndo_get_stats = macb_get_stats,
4002 .ndo_set_rx_mode = macb_set_rx_mode,
4003 .ndo_set_mac_address = eth_mac_addr,
4004 .ndo_do_ioctl = macb_ioctl,
4005 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004006#ifdef CONFIG_NET_POLL_CONTROLLER
4007 .ndo_poll_controller = at91ether_poll_controller,
4008#endif
4009};
4010
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004011static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304012 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304013 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004014{
4015 int err;
4016
4017 *hclk = NULL;
4018 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304019 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304020 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004021
4022 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4023 if (IS_ERR(*pclk))
4024 return PTR_ERR(*pclk);
4025
4026 err = clk_prepare_enable(*pclk);
4027 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004028 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004029 return err;
4030 }
4031
4032 return 0;
4033}
4034
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004035static int at91ether_init(struct platform_device *pdev)
4036{
4037 struct net_device *dev = platform_get_drvdata(pdev);
4038 struct macb *bp = netdev_priv(dev);
4039 int err;
4040 u32 reg;
4041
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004042 bp->queues[0].bp = bp;
4043
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004044 dev->netdev_ops = &at91ether_netdev_ops;
4045 dev->ethtool_ops = &macb_ethtool_ops;
4046
4047 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4048 0, dev->name, dev);
4049 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004050 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004051
4052 macb_writel(bp, NCR, 0);
4053
4054 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
4055 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
4056 reg |= MACB_BIT(RM9200_RMII);
4057
4058 macb_writel(bp, NCFGR, reg);
4059
4060 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004061}
4062
Yash Shahc218ad52019-06-18 13:26:08 +05304063static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4064 unsigned long parent_rate)
4065{
4066 return mgmt->rate;
4067}
4068
4069static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4070 unsigned long *parent_rate)
4071{
4072 if (WARN_ON(rate < 2500000))
4073 return 2500000;
4074 else if (rate == 2500000)
4075 return 2500000;
4076 else if (WARN_ON(rate < 13750000))
4077 return 2500000;
4078 else if (WARN_ON(rate < 25000000))
4079 return 25000000;
4080 else if (rate == 25000000)
4081 return 25000000;
4082 else if (WARN_ON(rate < 75000000))
4083 return 25000000;
4084 else if (WARN_ON(rate < 125000000))
4085 return 125000000;
4086 else if (rate == 125000000)
4087 return 125000000;
4088
4089 WARN_ON(rate > 125000000);
4090
4091 return 125000000;
4092}
4093
4094static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4095 unsigned long parent_rate)
4096{
4097 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4098 if (rate != 125000000)
4099 iowrite32(1, mgmt->reg);
4100 else
4101 iowrite32(0, mgmt->reg);
4102 mgmt->rate = rate;
4103
4104 return 0;
4105}
4106
4107static const struct clk_ops fu540_c000_ops = {
4108 .recalc_rate = fu540_macb_tx_recalc_rate,
4109 .round_rate = fu540_macb_tx_round_rate,
4110 .set_rate = fu540_macb_tx_set_rate,
4111};
4112
4113static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4114 struct clk **hclk, struct clk **tx_clk,
4115 struct clk **rx_clk, struct clk **tsu_clk)
4116{
4117 struct clk_init_data init;
4118 int err = 0;
4119
4120 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4121 if (err)
4122 return err;
4123
4124 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4125 if (!mgmt)
4126 return -ENOMEM;
4127
4128 init.name = "sifive-gemgxl-mgmt";
4129 init.ops = &fu540_c000_ops;
4130 init.flags = 0;
4131 init.num_parents = 0;
4132
4133 mgmt->rate = 0;
4134 mgmt->hw.init = &init;
4135
Stephen Boydd89091a2020-01-03 16:19:21 -08004136 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Yash Shahc218ad52019-06-18 13:26:08 +05304137 if (IS_ERR(*tx_clk))
4138 return PTR_ERR(*tx_clk);
4139
4140 err = clk_prepare_enable(*tx_clk);
4141 if (err)
4142 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4143 else
4144 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4145
4146 return 0;
4147}
4148
4149static int fu540_c000_init(struct platform_device *pdev)
4150{
4151 struct resource *res;
4152
4153 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
4154 if (!res)
4155 return -ENODEV;
4156
4157 mgmt->reg = ioremap(res->start, resource_size(res));
4158 if (!mgmt->reg)
4159 return -ENOMEM;
4160
4161 return macb_init(pdev);
4162}
4163
4164static const struct macb_config fu540_c000_config = {
4165 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4166 MACB_CAPS_GEM_HAS_PTP,
4167 .dma_burst_length = 16,
4168 .clk_init = fu540_c000_clk_init,
4169 .init = fu540_c000_init,
4170 .jumbo_max_len = 10240,
4171};
4172
David S. Miller3cef5c52015-03-09 23:38:02 -04004173static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004174 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004175 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004176 .init = macb_init,
4177};
4178
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004179static const struct macb_config sama5d3macb_config = {
4180 .caps = MACB_CAPS_SG_DISABLED
4181 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4182 .clk_init = macb_clk_init,
4183 .init = macb_init,
4184};
4185
David S. Miller3cef5c52015-03-09 23:38:02 -04004186static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004187 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4188 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004189 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004190 .init = macb_init,
4191};
4192
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004193static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004194 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004195 .dma_burst_length = 16,
4196 .clk_init = macb_clk_init,
4197 .init = macb_init,
4198};
4199
David S. Miller3cef5c52015-03-09 23:38:02 -04004200static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004201 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004202 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004203 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004204 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004205 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004206 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004207};
4208
David S. Miller3cef5c52015-03-09 23:38:02 -04004209static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004210 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004211 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004212 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004213 .init = macb_init,
4214};
4215
David S. Miller3cef5c52015-03-09 23:38:02 -04004216static const struct macb_config emac_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304217 .caps = MACB_CAPS_NEEDS_RSTONUBR,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004218 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004219 .init = at91ether_init,
4220};
4221
Neil Armstronge611b5b2016-01-05 14:39:17 +01004222static const struct macb_config np4_config = {
4223 .caps = MACB_CAPS_USRIO_DISABLED,
4224 .clk_init = macb_clk_init,
4225 .init = macb_init,
4226};
David S. Miller36583eb2015-05-23 01:22:35 -04004227
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304228static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004229 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4230 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304231 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304232 .dma_burst_length = 16,
4233 .clk_init = macb_clk_init,
4234 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304235 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304236};
4237
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004238static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304239 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4240 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004241 .dma_burst_length = 16,
4242 .clk_init = macb_clk_init,
4243 .init = macb_init,
4244};
4245
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004246static const struct of_device_id macb_dt_ids[] = {
4247 { .compatible = "cdns,at32ap7000-macb" },
4248 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4249 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004250 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004251 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4252 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004253 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004254 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004255 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004256 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004257 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4258 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4259 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304260 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004261 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304262 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004263 { /* sentinel */ }
4264};
4265MODULE_DEVICE_TABLE(of, macb_dt_ids);
4266#endif /* CONFIG_OF */
4267
Bartosz Folta83a77e92016-12-14 06:39:15 +00004268static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004269 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4270 MACB_CAPS_JUMBO |
4271 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004272 .dma_burst_length = 16,
4273 .clk_init = macb_clk_init,
4274 .init = macb_init,
4275 .jumbo_max_len = 10240,
4276};
4277
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004278static int macb_probe(struct platform_device *pdev)
4279{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004280 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004281 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304282 struct clk **, struct clk **, struct clk **,
4283 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004284 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004285 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304286 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304287 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004288 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004289 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004290 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004291 struct net_device *dev;
4292 struct resource *regs;
4293 void __iomem *mem;
4294 const char *mac;
4295 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304296 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004297
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004298 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4299 mem = devm_ioremap_resource(&pdev->dev, regs);
4300 if (IS_ERR(mem))
4301 return PTR_ERR(mem);
4302
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004303 if (np) {
4304 const struct of_device_id *match;
4305
4306 match = of_match_node(macb_dt_ids, np);
4307 if (match && match->data) {
4308 macb_config = match->data;
4309 clk_init = macb_config->clk_init;
4310 init = macb_config->init;
4311 }
4312 }
4313
Harini Katakamf5473d12019-03-01 16:20:33 +05304314 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004315 if (err)
4316 return err;
4317
Harini Katakamd54f89a2019-03-01 16:20:34 +05304318 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4319 pm_runtime_use_autosuspend(&pdev->dev);
4320 pm_runtime_get_noresume(&pdev->dev);
4321 pm_runtime_set_active(&pdev->dev);
4322 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004323 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004324
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004325 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004326 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004327 if (!dev) {
4328 err = -ENOMEM;
4329 goto err_disable_clocks;
4330 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004331
4332 dev->base_addr = regs->start;
4333
4334 SET_NETDEV_DEV(dev, &pdev->dev);
4335
4336 bp = netdev_priv(dev);
4337 bp->pdev = pdev;
4338 bp->dev = dev;
4339 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004340 bp->native_io = native_io;
4341 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004342 bp->macb_reg_readl = hw_readl_native;
4343 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004344 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004345 bp->macb_reg_readl = hw_readl;
4346 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004347 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004348 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004349 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004350 if (macb_config)
4351 bp->dma_burst_length = macb_config->dma_burst_length;
4352 bp->pclk = pclk;
4353 bp->hclk = hclk;
4354 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304355 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304356 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004357 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304358 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304359
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004360 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004361 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004362 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4363 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4364
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004365 spin_lock_init(&bp->lock);
4366
Nicolas Ferread783472015-03-31 15:02:02 +02004367 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004368 macb_configure_caps(bp, macb_config);
4369
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004370#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4371 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4372 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4373 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4374 }
4375#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004376 platform_set_drvdata(pdev, dev);
4377
4378 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004379 if (dev->irq < 0) {
4380 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004381 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004382 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004383
Jarod Wilson44770e12016-10-17 15:54:17 -04004384 /* MTU range: 68 - 1500 or 10240 */
4385 dev->min_mtu = GEM_MTU_MIN_SIZE;
4386 if (bp->caps & MACB_CAPS_JUMBO)
4387 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4388 else
4389 dev->max_mtu = ETH_DATA_LEN;
4390
Harini Katakam404cd082018-07-06 12:18:58 +05304391 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4392 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4393 if (val)
4394 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4395 macb_dma_desc_get_size(bp);
4396
4397 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4398 if (val)
4399 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4400 macb_dma_desc_get_size(bp);
4401 }
4402
Harini Katakame5010702019-01-29 15:20:03 +05304403 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4404 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4405 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4406
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004407 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004408 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4409 err = -EPROBE_DEFER;
4410 goto err_out_free_netdev;
Antoine Tenart2bf4ecb2019-06-21 17:26:35 +02004411 } else if (!IS_ERR_OR_NULL(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004412 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004413 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004414 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004415 }
frederic RODO6c36a702007-07-12 19:07:24 +02004416
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004417 err = of_get_phy_mode(np, &interface);
4418 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004419 /* not found in DT, MII by default */
4420 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4421 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004422 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004423
Antoine Tenart7897b072019-11-13 10:00:06 +01004424 bp->speed = SPEED_UNKNOWN;
4425
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004426 /* IP specific init */
4427 err = init(pdev);
4428 if (err)
4429 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004430
Florian Fainellicf669662016-05-02 18:38:45 -07004431 err = macb_mii_init(bp);
4432 if (err)
4433 goto err_out_free_netdev;
4434
Florian Fainellicf669662016-05-02 18:38:45 -07004435 netif_carrier_off(dev);
4436
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004437 err = register_netdev(dev);
4438 if (err) {
4439 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004440 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004441 }
4442
Harini Katakam032dc412018-01-27 12:09:01 +05304443 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4444 (unsigned long)bp);
4445
Bo Shen58798232014-09-13 01:57:49 +02004446 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4447 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4448 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004449
Harini Katakamd54f89a2019-03-01 16:20:34 +05304450 pm_runtime_mark_last_busy(&bp->pdev->dev);
4451 pm_runtime_put_autosuspend(&bp->pdev->dev);
4452
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004453 return 0;
4454
Florian Fainellicf669662016-05-02 18:38:45 -07004455err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004456 mdiobus_unregister(bp->mii_bus);
4457 mdiobus_free(bp->mii_bus);
4458
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004459err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004460 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004461
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004462err_disable_clocks:
4463 clk_disable_unprepare(tx_clk);
4464 clk_disable_unprepare(hclk);
4465 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304466 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304467 clk_disable_unprepare(tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304468 pm_runtime_disable(&pdev->dev);
4469 pm_runtime_set_suspended(&pdev->dev);
4470 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004471
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004472 return err;
4473}
4474
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004475static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004476{
4477 struct net_device *dev;
4478 struct macb *bp;
4479
4480 dev = platform_get_drvdata(pdev);
4481
4482 if (dev) {
4483 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004484 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004485 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004486
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004487 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004488 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304489 pm_runtime_disable(&pdev->dev);
4490 pm_runtime_dont_use_autosuspend(&pdev->dev);
4491 if (!pm_runtime_suspended(&pdev->dev)) {
4492 clk_disable_unprepare(bp->tx_clk);
4493 clk_disable_unprepare(bp->hclk);
4494 clk_disable_unprepare(bp->pclk);
4495 clk_disable_unprepare(bp->rx_clk);
4496 clk_disable_unprepare(bp->tsu_clk);
4497 pm_runtime_set_suspended(&pdev->dev);
4498 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004499 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004500 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004501 }
4502
4503 return 0;
4504}
4505
Michal Simekd23823d2015-01-23 09:36:03 +01004506static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004507{
Wolfram Sangce886a42018-10-21 22:00:14 +02004508 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004509 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304510 struct macb_queue *queue = bp->queues;
4511 unsigned long flags;
4512 unsigned int q;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004513
Harini Katakamde991c52019-03-01 16:20:35 +05304514 if (!netif_running(netdev))
4515 return 0;
4516
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004517 if (bp->wol & MACB_WOL_ENABLED) {
4518 macb_writel(bp, IER, MACB_BIT(WOL));
4519 macb_writel(bp, WOL, MACB_BIT(MAG));
4520 enable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304521 netif_device_detach(netdev);
4522 } else {
4523 netif_device_detach(netdev);
4524 for (q = 0, queue = bp->queues; q < bp->num_queues;
4525 ++q, ++queue)
4526 napi_disable(&queue->napi);
Antoine Tenart7897b072019-11-13 10:00:06 +01004527 rtnl_lock();
4528 phylink_stop(bp->phylink);
4529 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304530 spin_lock_irqsave(&bp->lock, flags);
4531 macb_reset_hw(bp);
4532 spin_unlock_irqrestore(&bp->lock, flags);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004533
4534 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4535 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4536
4537 if (netdev->hw_features & NETIF_F_NTUPLE)
4538 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304539 }
4540
Harini Katakamde991c52019-03-01 16:20:35 +05304541 netif_carrier_off(netdev);
4542 if (bp->ptp_info)
4543 bp->ptp_info->ptp_remove(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304544 pm_runtime_force_suspend(dev);
4545
4546 return 0;
4547}
4548
4549static int __maybe_unused macb_resume(struct device *dev)
4550{
4551 struct net_device *netdev = dev_get_drvdata(dev);
4552 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304553 struct macb_queue *queue = bp->queues;
4554 unsigned int q;
4555
4556 if (!netif_running(netdev))
4557 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304558
4559 pm_runtime_force_resume(dev);
4560
4561 if (bp->wol & MACB_WOL_ENABLED) {
4562 macb_writel(bp, IDR, MACB_BIT(WOL));
4563 macb_writel(bp, WOL, 0);
4564 disable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304565 } else {
4566 macb_writel(bp, NCR, MACB_BIT(MPE));
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004567
4568 if (netdev->hw_features & NETIF_F_NTUPLE)
4569 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4570
4571 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4572 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4573
Harini Katakamde991c52019-03-01 16:20:35 +05304574 for (q = 0, queue = bp->queues; q < bp->num_queues;
4575 ++q, ++queue)
4576 napi_enable(&queue->napi);
Antoine Tenart7897b072019-11-13 10:00:06 +01004577 rtnl_lock();
4578 phylink_start(bp->phylink);
4579 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304580 }
4581
Harini Katakamde991c52019-03-01 16:20:35 +05304582 macb_init_hw(bp);
4583 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004584 macb_restore_features(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304585 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304586 if (bp->ptp_info)
4587 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304588
4589 return 0;
4590}
4591
4592static int __maybe_unused macb_runtime_suspend(struct device *dev)
4593{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004594 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304595 struct macb *bp = netdev_priv(netdev);
4596
4597 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004598 clk_disable_unprepare(bp->tx_clk);
4599 clk_disable_unprepare(bp->hclk);
4600 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304601 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004602 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304603 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004604
4605 return 0;
4606}
4607
Harini Katakamd54f89a2019-03-01 16:20:34 +05304608static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004609{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004610 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004611 struct macb *bp = netdev_priv(netdev);
4612
Harini Katakamd54f89a2019-03-01 16:20:34 +05304613 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004614 clk_prepare_enable(bp->pclk);
4615 clk_prepare_enable(bp->hclk);
4616 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304617 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004618 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304619 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004620
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004621 return 0;
4622}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004623
Harini Katakamd54f89a2019-03-01 16:20:34 +05304624static const struct dev_pm_ops macb_pm_ops = {
4625 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4626 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4627};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004628
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004629static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004630 .probe = macb_probe,
4631 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004632 .driver = {
4633 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004634 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004635 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004636 },
4637};
4638
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004639module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004640
4641MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004642MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004643MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004644MODULE_ALIAS("platform:macb");