blob: e2730b3e1a5778a47c57b4b20612d751ca48a3f5 [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>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/platform_device.h>
Antoine Tenart7897b072019-11-13 10:00:06 +010027#include <linux/phylink.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080028#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010029#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010030#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000033#include <linux/ip.h>
34#include <linux/udp.h>
35#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053036#include <linux/iopoll.h>
Harini Katakamd54f89a2019-03-01 16:20:34 +053037#include <linux/pm_runtime.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Yash Shahc218ad52019-06-18 13:26:08 +053040/* This structure is only used for MACB on SiFive FU540 devices */
41struct sifive_fu540_macb_mgmt {
42 void __iomem *reg;
43 unsigned long rate;
44 struct clk_hw hw;
45};
46
Nicolas Ferre1b447912013-06-04 21:57:11 +000047#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000048#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050049
Zach Brownb410d132016-10-19 09:56:57 -050050#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050051#define MIN_RX_RING_SIZE 64
52#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000053#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050054 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
Zach Brownb410d132016-10-19 09:56:57 -050056#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050057#define MIN_TX_RING_SIZE 64
58#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000059#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050060 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010061
Nicolas Ferre909a8582012-11-19 06:00:21 +000062/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050063#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010064
Harini Katakame5010702019-01-29 15:20:03 +053065#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000066#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
67 | MACB_BIT(ISR_RLE) \
68 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000069#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
70 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000071
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000072/* Max length of transmit frame must be a multiple of 8 bytes */
73#define MACB_TX_LEN_ALIGN 8
74#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 +053075/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
76 * false amba_error in TX path from the DMA assuming there is not enough
77 * space in the SRAM (16KB) even when there is.
78 */
79#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020080
Jarod Wilson44770e12016-10-17 15:54:17 -040081#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070082#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053083
Sergio Prado3e2a5e12016-02-09 12:07:16 -020084#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
85#define MACB_WOL_ENABLED (0x1 << 1)
86
Parshuram Thombaree4e143e2020-10-29 13:47:07 +010087#define HS_SPEED_10000M 4
88#define MACB_SERDES_RATE_10G 1
89
Moritz Fischer64ec42f2016-03-29 19:11:12 -070090/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000091 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
92 */
93#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010094
Harini Katakamd54f89a2019-03-01 16:20:34 +053095#define MACB_PM_TIMEOUT 100 /* ms */
96
Harini Katakam8beb79b2019-03-01 16:20:32 +053097#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
98
Rafal Ozieblodc97a892017-01-27 15:08:20 +000099/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100100 * depends on hardware configuration:
101 *
102 * 1. dma address width 32 bits:
103 * word 1: 32 bit address of Data Buffer
104 * word 2: control
105 *
106 * 2. dma address width 64 bits:
107 * word 1: 32 bit address of Data Buffer
108 * word 2: control
109 * word 3: upper 32 bit address of Data Buffer
110 * word 4: unused
111 *
112 * 3. dma address width 32 bits with hardware timestamping:
113 * word 1: 32 bit address of Data Buffer
114 * word 2: control
115 * word 3: timestamp word 1
116 * word 4: timestamp word 2
117 *
118 * 4. dma address width 64 bits with hardware timestamping:
119 * word 1: 32 bit address of Data Buffer
120 * word 2: control
121 * word 3: upper 32 bit address of Data Buffer
122 * word 4: unused
123 * word 5: timestamp word 1
124 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000125 */
126static unsigned int macb_dma_desc_get_size(struct macb *bp)
127{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100128#ifdef MACB_EXT_DESC
129 unsigned int desc_size;
130
131 switch (bp->hw_dma_cap) {
132 case HW_DMA_CAP_64B:
133 desc_size = sizeof(struct macb_dma_desc)
134 + sizeof(struct macb_dma_desc_64);
135 break;
136 case HW_DMA_CAP_PTP:
137 desc_size = sizeof(struct macb_dma_desc)
138 + sizeof(struct macb_dma_desc_ptp);
139 break;
140 case HW_DMA_CAP_64B_PTP:
141 desc_size = sizeof(struct macb_dma_desc)
142 + sizeof(struct macb_dma_desc_64)
143 + sizeof(struct macb_dma_desc_ptp);
144 break;
145 default:
146 desc_size = sizeof(struct macb_dma_desc);
147 }
148 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
150 return sizeof(struct macb_dma_desc);
151}
152
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100153static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000154{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100155#ifdef MACB_EXT_DESC
156 switch (bp->hw_dma_cap) {
157 case HW_DMA_CAP_64B:
158 case HW_DMA_CAP_PTP:
159 desc_idx <<= 1;
160 break;
161 case HW_DMA_CAP_64B_PTP:
162 desc_idx *= 3;
163 break;
164 default:
165 break;
166 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000167#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100168 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000169}
170
171#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
172static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
173{
Shubhrajyoti Datta99dcb842019-09-23 14:03:51 +0530174 return (struct macb_dma_desc_64 *)((void *)desc
175 + sizeof(struct macb_dma_desc));
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000176}
177#endif
178
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500180static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181{
Zach Brownb410d132016-10-19 09:56:57 -0500182 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183}
184
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100185static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
186 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000187{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000188 index = macb_tx_ring_wrap(queue->bp, index);
189 index = macb_adj_dma_desc_idx(queue->bp, index);
190 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000191}
192
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100193static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
194 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195{
Zach Brownb410d132016-10-19 09:56:57 -0500196 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000197}
198
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100199static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000200{
201 dma_addr_t offset;
202
Zach Brownb410d132016-10-19 09:56:57 -0500203 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000204 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100206 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000207}
208
Zach Brownb410d132016-10-19 09:56:57 -0500209static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000210{
Zach Brownb410d132016-10-19 09:56:57 -0500211 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000212}
213
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000214static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000215{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000216 index = macb_rx_ring_wrap(queue->bp, index);
217 index = macb_adj_dma_desc_idx(queue->bp, index);
218 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000219}
220
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000221static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000222{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000223 return queue->rx_buffers + queue->bp->rx_buffer_size *
224 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000225}
226
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300227/* I/O accessors */
228static u32 hw_readl_native(struct macb *bp, int offset)
229{
230 return __raw_readl(bp->regs + offset);
231}
232
233static void hw_writel_native(struct macb *bp, int offset, u32 value)
234{
235 __raw_writel(value, bp->regs + offset);
236}
237
238static u32 hw_readl(struct macb *bp, int offset)
239{
240 return readl_relaxed(bp->regs + offset);
241}
242
243static void hw_writel(struct macb *bp, int offset, u32 value)
244{
245 writel_relaxed(value, bp->regs + offset);
246}
247
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700248/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700249 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300250 * descriptor access.
251 */
252static bool hw_is_native_io(void __iomem *addr)
253{
254 u32 value = MACB_BIT(LLB);
255
256 __raw_writel(value, addr + MACB_NCR);
257 value = __raw_readl(addr + MACB_NCR);
258
259 /* Write 0 back to disable everything */
260 __raw_writel(0, addr + MACB_NCR);
261
262 return value == MACB_BIT(LLB);
263}
264
265static bool hw_is_gem(void __iomem *addr, bool native_io)
266{
267 u32 id;
268
269 if (native_io)
270 id = __raw_readl(addr + MACB_MID);
271 else
272 id = readl_relaxed(addr + MACB_MID);
273
274 return MACB_BFEXT(IDNUM, id) >= 0x2;
275}
276
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100277static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100278{
279 u32 bottom;
280 u16 top;
281
282 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000283 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000285 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000286
287 /* Clear unused address register sets */
288 macb_or_gem_writel(bp, SA2B, 0);
289 macb_or_gem_writel(bp, SA2T, 0);
290 macb_or_gem_writel(bp, SA3B, 0);
291 macb_or_gem_writel(bp, SA3T, 0);
292 macb_or_gem_writel(bp, SA4B, 0);
293 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294}
295
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100296static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297{
298 u32 bottom;
299 u16 top;
300 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000301 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100302
Moritz Fischeraa50b552016-03-29 19:11:13 -0700303 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000304 for (i = 0; i < 4; i++) {
305 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
306 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100307
Nicolas Ferre8b952742019-05-03 12:36:58 +0200308 addr[0] = bottom & 0xff;
309 addr[1] = (bottom >> 8) & 0xff;
310 addr[2] = (bottom >> 16) & 0xff;
311 addr[3] = (bottom >> 24) & 0xff;
312 addr[4] = top & 0xff;
313 addr[5] = (top >> 8) & 0xff;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100314
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315 if (is_valid_ether_addr(addr)) {
316 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
317 return;
318 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700319 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000320
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300321 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000322 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323}
324
Harini Katakam8beb79b2019-03-01 16:20:32 +0530325static int macb_mdio_wait_for_idle(struct macb *bp)
326{
327 u32 val;
328
329 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
330 1, MACB_MDIO_TIMEOUT);
331}
332
frederic RODO6c36a702007-07-12 19:07:24 +0200333static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334{
frederic RODO6c36a702007-07-12 19:07:24 +0200335 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530336 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530337
Harini Katakamd54f89a2019-03-01 16:20:34 +0530338 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300339 if (status < 0) {
340 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530341 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300342 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530343
344 status = macb_mdio_wait_for_idle(bp);
345 if (status < 0)
346 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347
Milind Parab43ad3522020-01-09 08:36:46 +0000348 if (regnum & MII_ADDR_C45) {
349 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
350 | MACB_BF(RW, MACB_MAN_C45_ADDR)
351 | MACB_BF(PHYA, mii_id)
352 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
353 | MACB_BF(DATA, regnum & 0xFFFF)
354 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
355
356 status = macb_mdio_wait_for_idle(bp);
357 if (status < 0)
358 goto mdio_read_exit;
359
360 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
361 | MACB_BF(RW, MACB_MAN_C45_READ)
362 | MACB_BF(PHYA, mii_id)
363 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
364 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
365 } else {
366 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
367 | MACB_BF(RW, MACB_MAN_C22_READ)
368 | MACB_BF(PHYA, mii_id)
369 | MACB_BF(REGA, regnum)
370 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
371 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100372
Harini Katakamd54f89a2019-03-01 16:20:34 +0530373 status = macb_mdio_wait_for_idle(bp);
374 if (status < 0)
375 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100376
Harini Katakamd54f89a2019-03-01 16:20:34 +0530377 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100378
Harini Katakamd54f89a2019-03-01 16:20:34 +0530379mdio_read_exit:
380 pm_runtime_mark_last_busy(&bp->pdev->dev);
381 pm_runtime_put_autosuspend(&bp->pdev->dev);
382mdio_pm_exit:
383 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100384}
385
frederic RODO6c36a702007-07-12 19:07:24 +0200386static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
387 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100388{
frederic RODO6c36a702007-07-12 19:07:24 +0200389 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530390 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530391
Harini Katakamd54f89a2019-03-01 16:20:34 +0530392 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300393 if (status < 0) {
394 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530395 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300396 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530397
398 status = macb_mdio_wait_for_idle(bp);
399 if (status < 0)
400 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100401
Milind Parab43ad3522020-01-09 08:36:46 +0000402 if (regnum & MII_ADDR_C45) {
403 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
404 | MACB_BF(RW, MACB_MAN_C45_ADDR)
405 | MACB_BF(PHYA, mii_id)
406 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
407 | MACB_BF(DATA, regnum & 0xFFFF)
408 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
409
410 status = macb_mdio_wait_for_idle(bp);
411 if (status < 0)
412 goto mdio_write_exit;
413
414 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
415 | MACB_BF(RW, MACB_MAN_C45_WRITE)
416 | MACB_BF(PHYA, mii_id)
417 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
418 | MACB_BF(CODE, MACB_MAN_C45_CODE)
419 | MACB_BF(DATA, value)));
420 } else {
421 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
422 | MACB_BF(RW, MACB_MAN_C22_WRITE)
423 | MACB_BF(PHYA, mii_id)
424 | MACB_BF(REGA, regnum)
425 | MACB_BF(CODE, MACB_MAN_C22_CODE)
426 | MACB_BF(DATA, value)));
427 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100428
Harini Katakamd54f89a2019-03-01 16:20:34 +0530429 status = macb_mdio_wait_for_idle(bp);
430 if (status < 0)
431 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100432
Harini Katakamd54f89a2019-03-01 16:20:34 +0530433mdio_write_exit:
434 pm_runtime_mark_last_busy(&bp->pdev->dev);
435 pm_runtime_put_autosuspend(&bp->pdev->dev);
436mdio_pm_exit:
437 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438}
439
Antoine Tenart6e952d92019-11-13 10:00:05 +0100440static void macb_init_buffers(struct macb *bp)
441{
442 struct macb_queue *queue;
443 unsigned int q;
444
445 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
446 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
447#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
448 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
449 queue_writel(queue, RBQPH,
450 upper_32_bits(queue->rx_ring_dma));
451#endif
452 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
453#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
454 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
455 queue_writel(queue, TBQPH,
456 upper_32_bits(queue->tx_ring_dma));
457#endif
458 }
459}
460
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800461/**
462 * macb_set_tx_clk() - Set a clock to a new frequency
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200463 * @bp: pointer to struct macb
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -0700464 * @speed: New frequency in Hz
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800465 */
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200466static void macb_set_tx_clk(struct macb *bp, int speed)
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800467{
468 long ferr, rate, rate_rounded;
469
Charles Keepax1d0d5612021-01-04 10:38:02 +0000470 if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100471 return;
472
Michael Walle43e576312021-01-20 20:43:03 +0100473 /* In case of MII the PHY is the clock master */
474 if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
475 return;
476
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800477 switch (speed) {
478 case SPEED_10:
479 rate = 2500000;
480 break;
481 case SPEED_100:
482 rate = 25000000;
483 break;
484 case SPEED_1000:
485 rate = 125000000;
486 break;
487 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800488 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800489 }
490
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200491 rate_rounded = clk_round_rate(bp->tx_clk, rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800492 if (rate_rounded < 0)
493 return;
494
495 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
496 * is not satisfied.
497 */
498 ferr = abs(rate_rounded - rate);
499 ferr = DIV_ROUND_UP(ferr, rate / 100000);
500 if (ferr > 5)
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200501 netdev_warn(bp->dev,
502 "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700503 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800504
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200505 if (clk_set_rate(bp->tx_clk, rate_rounded))
506 netdev_err(bp->dev, "adjusting tx_clk failed.\n");
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800507}
508
Antoine Tenart7897b072019-11-13 10:00:06 +0100509static void macb_validate(struct phylink_config *config,
510 unsigned long *supported,
511 struct phylink_link_state *state)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100512{
Antoine Tenart7897b072019-11-13 10:00:06 +0100513 struct net_device *ndev = to_net_dev(config->dev);
514 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
515 struct macb *bp = netdev_priv(ndev);
516
517 /* We only support MII, RMII, GMII, RGMII & SGMII. */
518 if (state->interface != PHY_INTERFACE_MODE_NA &&
519 state->interface != PHY_INTERFACE_MODE_MII &&
520 state->interface != PHY_INTERFACE_MODE_RMII &&
521 state->interface != PHY_INTERFACE_MODE_GMII &&
522 state->interface != PHY_INTERFACE_MODE_SGMII &&
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100523 state->interface != PHY_INTERFACE_MODE_10GBASER &&
Antoine Tenart7897b072019-11-13 10:00:06 +0100524 !phy_interface_mode_is_rgmii(state->interface)) {
525 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
526 return;
527 }
528
529 if (!macb_is_gem(bp) &&
530 (state->interface == PHY_INTERFACE_MODE_GMII ||
531 phy_interface_mode_is_rgmii(state->interface))) {
532 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
533 return;
534 }
535
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100536 if (state->interface == PHY_INTERFACE_MODE_10GBASER &&
537 !(bp->caps & MACB_CAPS_HIGH_SPEED &&
538 bp->caps & MACB_CAPS_PCS)) {
539 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
540 return;
541 }
542
Antoine Tenart7897b072019-11-13 10:00:06 +0100543 phylink_set_port_modes(mask);
544 phylink_set(mask, Autoneg);
545 phylink_set(mask, Asym_Pause);
546
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100547 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
548 (state->interface == PHY_INTERFACE_MODE_NA ||
549 state->interface == PHY_INTERFACE_MODE_10GBASER)) {
550 phylink_set(mask, 10000baseCR_Full);
551 phylink_set(mask, 10000baseER_Full);
552 phylink_set(mask, 10000baseKR_Full);
553 phylink_set(mask, 10000baseLR_Full);
554 phylink_set(mask, 10000baseLRM_Full);
555 phylink_set(mask, 10000baseSR_Full);
556 phylink_set(mask, 10000baseT_Full);
557 if (state->interface != PHY_INTERFACE_MODE_NA)
558 goto out;
559 }
560
Antoine Tenart7897b072019-11-13 10:00:06 +0100561 phylink_set(mask, 10baseT_Half);
562 phylink_set(mask, 10baseT_Full);
563 phylink_set(mask, 100baseT_Half);
564 phylink_set(mask, 100baseT_Full);
565
566 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
567 (state->interface == PHY_INTERFACE_MODE_NA ||
568 state->interface == PHY_INTERFACE_MODE_GMII ||
569 state->interface == PHY_INTERFACE_MODE_SGMII ||
570 phy_interface_mode_is_rgmii(state->interface))) {
571 phylink_set(mask, 1000baseT_Full);
572 phylink_set(mask, 1000baseX_Full);
573
574 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
575 phylink_set(mask, 1000baseT_Half);
576 }
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100577out:
Antoine Tenart7897b072019-11-13 10:00:06 +0100578 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
579 bitmap_and(state->advertising, state->advertising, mask,
580 __ETHTOOL_LINK_MODE_MASK_NBITS);
581}
582
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100583static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
584 phy_interface_t interface, int speed,
585 int duplex)
586{
587 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
588 u32 config;
589
590 config = gem_readl(bp, USX_CONTROL);
591 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
592 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
593 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
594 config |= GEM_BIT(TX_EN);
595 gem_writel(bp, USX_CONTROL, config);
596}
597
598static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
Russell Kingd46b7e42019-11-21 00:36:22 +0000599 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100600{
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100601 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
602 u32 val;
603
604 state->speed = SPEED_10000;
605 state->duplex = 1;
606 state->an_complete = 1;
607
608 val = gem_readl(bp, USX_STATUS);
609 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
610 val = gem_readl(bp, NCFGR);
611 if (val & GEM_BIT(PAE))
612 state->pause = MLO_PAUSE_RX;
613}
614
615static int macb_usx_pcs_config(struct phylink_pcs *pcs,
616 unsigned int mode,
617 phy_interface_t interface,
618 const unsigned long *advertising,
619 bool permit_pause_to_mac)
620{
621 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
622
623 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
624 GEM_BIT(SIGNAL_OK));
625
626 return 0;
627}
628
629static void macb_pcs_get_state(struct phylink_pcs *pcs,
630 struct phylink_link_state *state)
631{
Russell Kingd46b7e42019-11-21 00:36:22 +0000632 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100633}
634
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100635static void macb_pcs_an_restart(struct phylink_pcs *pcs)
Antoine Tenart7897b072019-11-13 10:00:06 +0100636{
637 /* Not supported */
638}
639
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100640static int macb_pcs_config(struct phylink_pcs *pcs,
641 unsigned int mode,
642 phy_interface_t interface,
643 const unsigned long *advertising,
644 bool permit_pause_to_mac)
645{
646 return 0;
647}
648
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100649static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
650 .pcs_get_state = macb_usx_pcs_get_state,
651 .pcs_config = macb_usx_pcs_config,
652 .pcs_link_up = macb_usx_pcs_link_up,
653};
654
655static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
656 .pcs_get_state = macb_pcs_get_state,
657 .pcs_an_restart = macb_pcs_an_restart,
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100658 .pcs_config = macb_pcs_config,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100659};
660
Antoine Tenart7897b072019-11-13 10:00:06 +0100661static void macb_mac_config(struct phylink_config *config, unsigned int mode,
662 const struct phylink_link_state *state)
663{
664 struct net_device *ndev = to_net_dev(config->dev);
665 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200666 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100667 u32 old_ctrl, ctrl;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100668 u32 old_ncr, ncr;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100669
frederic RODO6c36a702007-07-12 19:07:24 +0200670 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100671
Antoine Tenart7897b072019-11-13 10:00:06 +0100672 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100673 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100675 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
676 if (state->interface == PHY_INTERFACE_MODE_RMII)
677 ctrl |= MACB_BIT(RM9200_RMII);
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200678 } else if (macb_is_gem(bp)) {
Russell King633e98a2020-02-26 10:24:06 +0000679 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100680 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100681
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100682 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100683 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100684 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
685 ctrl |= GEM_BIT(PCSSEL);
686 ncr |= GEM_BIT(ENABLE_HS_MAC);
Claudiu Beznea1a9b5a22021-09-17 16:26:14 +0300687 } else if (bp->caps & MACB_CAPS_MIIONRGMII &&
688 bp->phy_interface == PHY_INTERFACE_MODE_MII) {
689 ncr |= MACB_BIT(MIIONRGMII);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100690 }
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100691 }
frederic RODO6c36a702007-07-12 19:07:24 +0200692
Antoine Tenart7897b072019-11-13 10:00:06 +0100693 /* Apply the new configuration, if any */
694 if (old_ctrl ^ ctrl)
695 macb_or_gem_writel(bp, NCFGR, ctrl);
696
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100697 if (old_ncr ^ ncr)
698 macb_or_gem_writel(bp, NCR, ncr);
699
Robert Hancocke276e5e42021-03-11 14:18:13 -0600700 /* Disable AN for SGMII fixed link configuration, enable otherwise.
701 * Must be written after PCSSEL is set in NCFGR,
702 * otherwise writes will not take effect.
703 */
704 if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) {
705 u32 pcsctrl, old_pcsctrl;
706
707 old_pcsctrl = gem_readl(bp, PCSCNTRL);
708 if (mode == MLO_AN_FIXED)
709 pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG);
710 else
711 pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG);
712 if (old_pcsctrl != pcsctrl)
713 gem_writel(bp, PCSCNTRL, pcsctrl);
714 }
715
frederic RODO6c36a702007-07-12 19:07:24 +0200716 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200717}
718
Antoine Tenart7897b072019-11-13 10:00:06 +0100719static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
720 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200721{
Antoine Tenart7897b072019-11-13 10:00:06 +0100722 struct net_device *ndev = to_net_dev(config->dev);
723 struct macb *bp = netdev_priv(ndev);
724 struct macb_queue *queue;
725 unsigned int q;
726 u32 ctrl;
727
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100728 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
729 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
730 queue_writel(queue, IDR,
731 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100732
733 /* Disable Rx and Tx */
734 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
735 macb_writel(bp, NCR, ctrl);
736
737 netif_tx_stop_all_queues(ndev);
738}
739
Russell King91a208f2020-02-26 10:23:41 +0000740static void macb_mac_link_up(struct phylink_config *config,
741 struct phy_device *phy,
742 unsigned int mode, phy_interface_t interface,
743 int speed, int duplex,
744 bool tx_pause, bool rx_pause)
Antoine Tenart7897b072019-11-13 10:00:06 +0100745{
746 struct net_device *ndev = to_net_dev(config->dev);
747 struct macb *bp = netdev_priv(ndev);
748 struct macb_queue *queue;
Russell King633e98a2020-02-26 10:24:06 +0000749 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100750 unsigned int q;
Russell King633e98a2020-02-26 10:24:06 +0000751 u32 ctrl;
752
753 spin_lock_irqsave(&bp->lock, flags);
754
755 ctrl = macb_or_gem_readl(bp, NCFGR);
756
757 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
758
759 if (speed == SPEED_100)
760 ctrl |= MACB_BIT(SPD);
761
762 if (duplex)
763 ctrl |= MACB_BIT(FD);
Antoine Tenart7897b072019-11-13 10:00:06 +0100764
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100765 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200766 ctrl &= ~MACB_BIT(PAE);
767 if (macb_is_gem(bp)) {
768 ctrl &= ~GEM_BIT(GBE);
Russell King633e98a2020-02-26 10:24:06 +0000769
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200770 if (speed == SPEED_1000)
771 ctrl |= GEM_BIT(GBE);
772 }
Russell King633e98a2020-02-26 10:24:06 +0000773
Parshuram Thombared7739b02020-09-05 10:21:33 +0200774 if (rx_pause)
Russell King633e98a2020-02-26 10:24:06 +0000775 ctrl |= MACB_BIT(PAE);
776
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200777 macb_set_tx_clk(bp, speed);
Antoine Tenart7897b072019-11-13 10:00:06 +0100778
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100779 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
780 * cleared the pipeline and control registers.
781 */
782 bp->macbgem_ops.mog_init_rings(bp);
783 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100784
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100785 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
786 queue_writel(queue, IER,
787 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
788 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100789
Russell King633e98a2020-02-26 10:24:06 +0000790 macb_or_gem_writel(bp, NCFGR, ctrl);
791
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100792 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
793 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
794 gem_readl(bp, HS_MAC_CONFIG)));
795
Russell King633e98a2020-02-26 10:24:06 +0000796 spin_unlock_irqrestore(&bp->lock, flags);
797
Antoine Tenart7897b072019-11-13 10:00:06 +0100798 /* Enable Rx and Tx */
799 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
800
801 netif_tx_wake_all_queues(ndev);
802}
803
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100804static int macb_mac_prepare(struct phylink_config *config, unsigned int mode,
805 phy_interface_t interface)
806{
807 struct net_device *ndev = to_net_dev(config->dev);
808 struct macb *bp = netdev_priv(ndev);
809
810 if (interface == PHY_INTERFACE_MODE_10GBASER)
811 bp->phylink_pcs.ops = &macb_phylink_usx_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100812 else if (interface == PHY_INTERFACE_MODE_SGMII)
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100813 bp->phylink_pcs.ops = &macb_phylink_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100814 else
815 bp->phylink_pcs.ops = NULL;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100816
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100817 if (bp->phylink_pcs.ops)
818 phylink_set_pcs(bp->phylink, &bp->phylink_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100819
820 return 0;
821}
822
Antoine Tenart7897b072019-11-13 10:00:06 +0100823static const struct phylink_mac_ops macb_phylink_ops = {
824 .validate = macb_validate,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100825 .mac_prepare = macb_mac_prepare,
Antoine Tenart7897b072019-11-13 10:00:06 +0100826 .mac_config = macb_mac_config,
827 .mac_link_down = macb_mac_link_down,
828 .mac_link_up = macb_mac_link_up,
829};
830
Milind Parabfd2a8912020-01-13 03:30:43 +0000831static bool macb_phy_handle_exists(struct device_node *dn)
832{
833 dn = of_parse_phandle(dn, "phy-handle", 0);
834 of_node_put(dn);
835 return dn != NULL;
836}
837
Antoine Tenart7897b072019-11-13 10:00:06 +0100838static int macb_phylink_connect(struct macb *bp)
839{
Milind Parabfd2a8912020-01-13 03:30:43 +0000840 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100841 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000842 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100843 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500844
Milind Parabfd2a8912020-01-13 03:30:43 +0000845 if (dn)
846 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
847
848 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200849 phydev = phy_find_first(bp->mii_bus);
850 if (!phydev) {
851 netdev_err(dev, "no PHY found\n");
852 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000853 }
frederic RODO6c36a702007-07-12 19:07:24 +0200854
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200855 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100856 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000857 }
858
859 if (ret) {
860 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
861 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200862 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100863
Antoine Tenart7897b072019-11-13 10:00:06 +0100864 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100865
Antoine Tenart7897b072019-11-13 10:00:06 +0100866 return 0;
867}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100868
Robert Hancock8fab1742021-03-11 14:18:12 -0600869static void macb_get_pcs_fixed_state(struct phylink_config *config,
870 struct phylink_link_state *state)
871{
872 struct net_device *ndev = to_net_dev(config->dev);
873 struct macb *bp = netdev_priv(ndev);
874
875 state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
876}
877
Antoine Tenart7897b072019-11-13 10:00:06 +0100878/* based on au1000_eth. c*/
879static int macb_mii_probe(struct net_device *dev)
880{
881 struct macb *bp = netdev_priv(dev);
882
883 bp->phylink_config.dev = &dev->dev;
884 bp->phylink_config.type = PHYLINK_NETDEV;
885
Robert Hancock8fab1742021-03-11 14:18:12 -0600886 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
887 bp->phylink_config.poll_fixed_state = true;
888 bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
889 }
890
Antoine Tenart7897b072019-11-13 10:00:06 +0100891 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
892 bp->phy_interface, &macb_phylink_ops);
893 if (IS_ERR(bp->phylink)) {
894 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
895 PTR_ERR(bp->phylink));
896 return PTR_ERR(bp->phylink);
897 }
frederic RODO6c36a702007-07-12 19:07:24 +0200898
899 return 0;
900}
901
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100902static int macb_mdiobus_register(struct macb *bp)
903{
904 struct device_node *child, *np = bp->pdev->dev.of_node;
905
Codrin Ciubotariu79540d12020-03-31 12:39:35 +0300906 if (of_phy_is_fixed_link(np))
907 return mdiobus_register(bp->mii_bus);
908
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100909 /* Only create the PHY from the device tree if at least one PHY is
910 * described. Otherwise scan the entire MDIO bus. We do this to support
911 * old device tree that did not follow the best practices and did not
912 * describe their network PHYs.
913 */
914 for_each_available_child_of_node(np, child)
915 if (of_mdiobus_child_is_phy(child)) {
916 /* The loop increments the child refcount,
917 * decrement it before returning.
918 */
919 of_node_put(child);
920
921 return of_mdiobus_register(bp->mii_bus, np);
922 }
923
924 return mdiobus_register(bp->mii_bus);
925}
926
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100927static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200928{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200929 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200930
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200931 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200932 macb_writel(bp, NCR, MACB_BIT(MPE));
933
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700934 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700935 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200936 err = -ENOMEM;
937 goto err_out;
938 }
939
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700940 bp->mii_bus->name = "MACB_mii_bus";
941 bp->mii_bus->read = &macb_mdio_read;
942 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000943 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700944 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700945 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700946 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700947
Jamie Iles91523942011-02-28 04:05:25 +0000948 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200949
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100950 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200951 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100952 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200953
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200954 err = macb_mii_probe(bp->dev);
955 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200956 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200957
958 return 0;
959
960err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700961 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500962err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700963 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200964err_out:
965 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100966}
967
968static void macb_update_stats(struct macb *bp)
969{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000970 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
971 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300972 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100973
974 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
975
Moritz Fischer96ec6312016-03-29 19:11:11 -0700976 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700977 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100978}
979
Nicolas Ferree86cd532012-10-31 06:04:57 +0000980static int macb_halt_tx(struct macb *bp)
981{
982 unsigned long halt_time, timeout;
983 u32 status;
984
985 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
986
987 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
988 do {
989 halt_time = jiffies;
990 status = macb_readl(bp, TSR);
991 if (!(status & MACB_BIT(TGO)))
992 return 0;
993
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800994 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000995 } while (time_before(halt_time, timeout));
996
997 return -ETIMEDOUT;
998}
999
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001000static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
1001{
1002 if (tx_skb->mapping) {
1003 if (tx_skb->mapped_as_page)
1004 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
1005 tx_skb->size, DMA_TO_DEVICE);
1006 else
1007 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
1008 tx_skb->size, DMA_TO_DEVICE);
1009 tx_skb->mapping = 0;
1010 }
1011
1012 if (tx_skb->skb) {
1013 dev_kfree_skb_any(tx_skb->skb);
1014 tx_skb->skb = NULL;
1015 }
1016}
1017
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001018static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +05301019{
Harini Katakamfff80192016-08-09 13:15:53 +05301020#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001021 struct macb_dma_desc_64 *desc_64;
1022
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001023 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001024 desc_64 = macb_64b_desc(bp, desc);
1025 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +02001026 /* The low bits of RX address contain the RX_USED bit, clearing
1027 * of which allows packet RX. Make sure the high bits are also
1028 * visible to HW at that point.
1029 */
1030 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001031 }
Harini Katakamfff80192016-08-09 13:15:53 +05301032#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001033 desc->addr = lower_32_bits(addr);
1034}
1035
1036static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1037{
1038 dma_addr_t addr = 0;
1039#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1040 struct macb_dma_desc_64 *desc_64;
1041
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001042 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001043 desc_64 = macb_64b_desc(bp, desc);
1044 addr = ((u64)(desc_64->addrh) << 32);
1045 }
1046#endif
1047 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1048 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +05301049}
1050
Nicolas Ferree86cd532012-10-31 06:04:57 +00001051static void macb_tx_error_task(struct work_struct *work)
1052{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001053 struct macb_queue *queue = container_of(work, struct macb_queue,
1054 tx_error_task);
1055 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001056 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001057 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001058 struct sk_buff *skb;
1059 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001060 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001061
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001062 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
1063 (unsigned int)(queue - bp->queues),
1064 queue->tx_tail, queue->tx_head);
1065
1066 /* Prevent the queue IRQ handlers from running: each of them may call
1067 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
1068 * As explained below, we have to halt the transmission before updating
1069 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1070 * network engine about the macb/gem being halted.
1071 */
1072 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001073
1074 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001075 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001076
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001077 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +00001078 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001079 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +00001080 */
1081 if (macb_halt_tx(bp))
1082 /* Just complain for now, reinitializing TX path can be good */
1083 netdev_err(bp->dev, "BUG: halt tx timed out\n");
1084
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001085 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001086 * Free transmit buffers in upper layer.
1087 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001088 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1089 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001090
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001091 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001092 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001093 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001094 skb = tx_skb->skb;
1095
1096 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001097 /* skb is set for the last buffer of the frame */
1098 while (!skb) {
1099 macb_tx_unmap(bp, tx_skb);
1100 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001101 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001102 skb = tx_skb->skb;
1103 }
1104
1105 /* ctrl still refers to the first buffer descriptor
1106 * since it's the only one written back by the hardware
1107 */
1108 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1109 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001110 macb_tx_ring_wrap(bp, tail),
1111 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001112 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001113 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001114 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001115 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001116 }
Nicolas Ferree86cd532012-10-31 06:04:57 +00001117 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001118 /* "Buffers exhausted mid-frame" errors may only happen
1119 * if the driver is buggy, so complain loudly about
1120 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001121 */
1122 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1123 netdev_err(bp->dev,
1124 "BUG: TX buffers exhausted mid-frame\n");
1125
1126 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1127 }
1128
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001129 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001130 }
1131
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001132 /* Set end of TX queue */
1133 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001134 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001135 desc->ctrl = MACB_BIT(TX_USED);
1136
Nicolas Ferree86cd532012-10-31 06:04:57 +00001137 /* Make descriptor updates visible to hardware */
1138 wmb();
1139
1140 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001141 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301142#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001143 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001144 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301145#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +00001146 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001147 queue->tx_head = 0;
1148 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001149
1150 /* Housework before enabling TX IRQ */
1151 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001152 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1153
1154 /* Now we are ready to start transmission again */
1155 netif_tx_start_all_queues(bp->dev);
1156 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1157
1158 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001159}
1160
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001161static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001162{
1163 unsigned int tail;
1164 unsigned int head;
1165 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001166 struct macb *bp = queue->bp;
1167 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168
1169 status = macb_readl(bp, TSR);
1170 macb_writel(bp, TSR, status);
1171
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001172 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001173 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +00001174
Nicolas Ferree86cd532012-10-31 06:04:57 +00001175 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001176 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001177
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001178 head = queue->tx_head;
1179 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001180 struct macb_tx_skb *tx_skb;
1181 struct sk_buff *skb;
1182 struct macb_dma_desc *desc;
1183 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001184
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001185 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001186
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001187 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001188 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001189
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001190 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001192 /* TX_USED bit is only set by hardware on the very first buffer
1193 * descriptor of the transmitted frame.
1194 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001195 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 break;
1197
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001198 /* Process all buffers of the current transmitted frame */
1199 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001200 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001201 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001202
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001203 /* First, update TX stats if needed */
1204 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001205 if (unlikely(skb_shinfo(skb)->tx_flags &
1206 SKBTX_HW_TSTAMP) &&
1207 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001208 /* skb now belongs to timestamp buffer
1209 * and will be removed later
1210 */
1211 tx_skb->skb = NULL;
1212 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001213 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001214 macb_tx_ring_wrap(bp, tail),
1215 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001216 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001217 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001218 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001219 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001220 }
1221
1222 /* Now we can safely release resources */
1223 macb_tx_unmap(bp, tx_skb);
1224
1225 /* skb is set only for the last buffer of the frame.
1226 * WARNING: at this point skb has been freed by
1227 * macb_tx_unmap().
1228 */
1229 if (skb)
1230 break;
1231 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001232 }
1233
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001234 queue->tx_tail = tail;
1235 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1236 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001237 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001238 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001239}
1240
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001241static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001242{
1243 unsigned int entry;
1244 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001245 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001246 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001247 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001248
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001249 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1250 bp->rx_ring_size) > 0) {
1251 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001252
1253 /* Make hw descriptor updates visible to CPU */
1254 rmb();
1255
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001256 queue->rx_prepared_head++;
1257 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001258
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001259 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001260 /* allocate sk_buff for this free entry in ring */
1261 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001262 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001263 netdev_err(bp->dev,
1264 "Unable to allocate sk_buff\n");
1265 break;
1266 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001267
1268 /* now fill corresponding descriptor entry */
1269 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001270 bp->rx_buffer_size,
1271 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001272 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1273 dev_kfree_skb(skb);
1274 break;
1275 }
1276
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001277 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001278
Zach Brownb410d132016-10-19 09:56:57 -05001279 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001280 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001281 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001282 /* Setting addr clears RX_USED and allows reception,
1283 * make sure ctrl is cleared first to avoid a race.
1284 */
1285 dma_wmb();
1286 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001287
1288 /* properly align Ethernet header */
1289 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301290 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001291 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001292 dma_wmb();
1293 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001294 }
1295 }
1296
1297 /* Make descriptor updates visible to hardware */
1298 wmb();
1299
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001300 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1301 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001302}
1303
1304/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001305static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001306 unsigned int end)
1307{
1308 unsigned int frag;
1309
1310 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001311 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001312
Nicolas Ferre4df95132013-06-04 21:57:12 +00001313 desc->addr &= ~MACB_BIT(RX_USED);
1314 }
1315
1316 /* Make descriptor updates visible to hardware */
1317 wmb();
1318
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001319 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001320 * whatever caused this is updated, so we don't have to record
1321 * anything.
1322 */
1323}
1324
Antoine Tenart97236cd2019-06-21 17:30:02 +02001325static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1326 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001327{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001328 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001329 unsigned int len;
1330 unsigned int entry;
1331 struct sk_buff *skb;
1332 struct macb_dma_desc *desc;
1333 int count = 0;
1334
1335 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301336 u32 ctrl;
1337 dma_addr_t addr;
1338 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001339
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001340 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1341 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001342
1343 /* Make hw descriptor updates visible to CPU */
1344 rmb();
1345
Harini Katakamfff80192016-08-09 13:15:53 +05301346 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001347 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001348
Harini Katakamfff80192016-08-09 13:15:53 +05301349 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001350 break;
1351
Anssi Hannula6e0af292018-12-17 15:05:41 +02001352 /* Ensure ctrl is at least as up-to-date as rxused */
1353 dma_rmb();
1354
1355 ctrl = desc->ctrl;
1356
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001357 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001358 count++;
1359
1360 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1361 netdev_err(bp->dev,
1362 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001363 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001364 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001365 break;
1366 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001367 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001368 if (unlikely(!skb)) {
1369 netdev_err(bp->dev,
1370 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001371 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001372 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001373 break;
1374 }
1375 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001376 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301377 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001378
1379 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1380
1381 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001382 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001383 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001384
1385 skb->protocol = eth_type_trans(skb, bp->dev);
1386 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001387 if (bp->dev->features & NETIF_F_RXCSUM &&
1388 !(bp->dev->flags & IFF_PROMISC) &&
1389 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1390 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001391
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001392 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001393 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001394 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001395 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001396
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001397 gem_ptp_do_rxstamp(bp, skb, desc);
1398
Nicolas Ferre4df95132013-06-04 21:57:12 +00001399#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1400 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1401 skb->len, skb->csum);
1402 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001403 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001404 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1405 skb->data, 32, true);
1406#endif
1407
Antoine Tenart97236cd2019-06-21 17:30:02 +02001408 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001409 }
1410
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001411 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001412
1413 return count;
1414}
1415
Antoine Tenart97236cd2019-06-21 17:30:02 +02001416static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1417 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001418{
1419 unsigned int len;
1420 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001421 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001422 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001423 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001424 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001425
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001426 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301427 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001428
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001429 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001430 macb_rx_ring_wrap(bp, first_frag),
1431 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001432
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001433 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001434 * first buffer. Since the header is 14 bytes, this makes the
1435 * payload word-aligned.
1436 *
1437 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1438 * the two padding bytes into the skb so that we avoid hitting
1439 * the slowpath in memcpy(), and pull them off afterwards.
1440 */
1441 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001442 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001443 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001444 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001445 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001446 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001447 if (frag == last_frag)
1448 break;
1449 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001450
1451 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001452 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001453
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001454 return 1;
1455 }
1456
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001457 offset = 0;
1458 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001459 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001460 skb_put(skb, len);
1461
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001462 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001463 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001464
1465 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001466 if (unlikely(frag != last_frag)) {
1467 dev_kfree_skb_any(skb);
1468 return -1;
1469 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001470 frag_len = len - offset;
1471 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001472 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001473 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001474 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001475 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001476 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001477 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001478
1479 if (frag == last_frag)
1480 break;
1481 }
1482
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001483 /* Make descriptor updates visible to hardware */
1484 wmb();
1485
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001486 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001487 skb->protocol = eth_type_trans(skb, bp->dev);
1488
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001489 bp->dev->stats.rx_packets++;
1490 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001491 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001492 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001493 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001494
1495 return 0;
1496}
1497
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001498static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001499{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001500 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001501 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001502 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001503 int i;
1504
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001505 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001506 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001507 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001508 macb_set_addr(bp, desc, addr);
1509 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001510 addr += bp->rx_buffer_size;
1511 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001512 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001513 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001514}
1515
Antoine Tenart97236cd2019-06-21 17:30:02 +02001516static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1517 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001518{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001519 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001520 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001521 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001522 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001523 int first_frag = -1;
1524
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001525 for (tail = queue->rx_tail; budget > 0; tail++) {
1526 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001527 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001528
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001529 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001530 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001531
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001532 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001533 break;
1534
Anssi Hannula6e0af292018-12-17 15:05:41 +02001535 /* Ensure ctrl is at least as up-to-date as addr */
1536 dma_rmb();
1537
1538 ctrl = desc->ctrl;
1539
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001540 if (ctrl & MACB_BIT(RX_SOF)) {
1541 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001542 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001543 first_frag = tail;
1544 }
1545
1546 if (ctrl & MACB_BIT(RX_EOF)) {
1547 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001548
1549 if (unlikely(first_frag == -1)) {
1550 reset_rx_queue = true;
1551 continue;
1552 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001553
Antoine Tenart97236cd2019-06-21 17:30:02 +02001554 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001555 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001556 if (unlikely(dropped < 0)) {
1557 reset_rx_queue = true;
1558 continue;
1559 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001560 if (!dropped) {
1561 received++;
1562 budget--;
1563 }
1564 }
1565 }
1566
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001567 if (unlikely(reset_rx_queue)) {
1568 unsigned long flags;
1569 u32 ctrl;
1570
1571 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1572
1573 spin_lock_irqsave(&bp->lock, flags);
1574
1575 ctrl = macb_readl(bp, NCR);
1576 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1577
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001578 macb_init_rx_ring(queue);
1579 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001580
1581 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1582
1583 spin_unlock_irqrestore(&bp->lock, flags);
1584 return received;
1585 }
1586
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001587 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001588 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001589 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001590 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001591
1592 return received;
1593}
1594
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001595static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001596{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001597 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1598 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001599 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001600 u32 status;
1601
1602 status = macb_readl(bp, RSR);
1603 macb_writel(bp, RSR, status);
1604
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001605 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001606 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001607
Antoine Tenart97236cd2019-06-21 17:30:02 +02001608 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001609 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001610 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001611
Nicolas Ferre8770e912013-02-12 11:08:48 +01001612 /* Packets received while interrupts were disabled */
1613 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001614 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001615 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001616 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001617 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001618 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301619 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001620 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001621 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001622
1623 /* TODO: Handle errors */
1624
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001625 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001626}
1627
Allen Paise7412b82020-09-14 12:59:23 +05301628static void macb_hresp_error_task(struct tasklet_struct *t)
Harini Katakam032dc412018-01-27 12:09:01 +05301629{
Allen Paise7412b82020-09-14 12:59:23 +05301630 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
Harini Katakam032dc412018-01-27 12:09:01 +05301631 struct net_device *dev = bp->dev;
Claudiu Beznea580d3952020-07-02 12:06:00 +03001632 struct macb_queue *queue;
Harini Katakam032dc412018-01-27 12:09:01 +05301633 unsigned int q;
1634 u32 ctrl;
1635
1636 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301637 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301638 MACB_TX_INT_FLAGS |
1639 MACB_BIT(HRESP));
1640 }
1641 ctrl = macb_readl(bp, NCR);
1642 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1643 macb_writel(bp, NCR, ctrl);
1644
1645 netif_tx_stop_all_queues(dev);
1646 netif_carrier_off(dev);
1647
1648 bp->macbgem_ops.mog_init_rings(bp);
1649
1650 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001651 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301652
Antoine Tenart6e952d92019-11-13 10:00:05 +01001653 /* Enable interrupts */
1654 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301655 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301656 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301657 MACB_TX_INT_FLAGS |
1658 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301659
1660 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1661 macb_writel(bp, NCR, ctrl);
1662
1663 netif_carrier_on(dev);
1664 netif_tx_start_all_queues(dev);
1665}
1666
Claudiu Beznea42983882018-12-17 10:02:42 +00001667static void macb_tx_restart(struct macb_queue *queue)
1668{
1669 unsigned int head = queue->tx_head;
1670 unsigned int tail = queue->tx_tail;
1671 struct macb *bp = queue->bp;
1672
1673 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1674 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1675
1676 if (head == tail)
1677 return;
1678
1679 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1680}
1681
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02001682static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
1683{
1684 struct macb_queue *queue = dev_id;
1685 struct macb *bp = queue->bp;
1686 u32 status;
1687
1688 status = queue_readl(queue, ISR);
1689
1690 if (unlikely(!status))
1691 return IRQ_NONE;
1692
1693 spin_lock(&bp->lock);
1694
1695 if (status & MACB_BIT(WOL)) {
1696 queue_writel(queue, IDR, MACB_BIT(WOL));
1697 macb_writel(bp, WOL, 0);
1698 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
1699 (unsigned int)(queue - bp->queues),
1700 (unsigned long)status);
1701 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1702 queue_writel(queue, ISR, MACB_BIT(WOL));
1703 pm_wakeup_event(&bp->pdev->dev, 0);
1704 }
1705
1706 spin_unlock(&bp->lock);
1707
1708 return IRQ_HANDLED;
1709}
1710
Nicolas Ferre558e35c2020-07-20 10:56:52 +02001711static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
1712{
1713 struct macb_queue *queue = dev_id;
1714 struct macb *bp = queue->bp;
1715 u32 status;
1716
1717 status = queue_readl(queue, ISR);
1718
1719 if (unlikely(!status))
1720 return IRQ_NONE;
1721
1722 spin_lock(&bp->lock);
1723
1724 if (status & GEM_BIT(WOL)) {
1725 queue_writel(queue, IDR, GEM_BIT(WOL));
1726 gem_writel(bp, WOL, 0);
1727 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
1728 (unsigned int)(queue - bp->queues),
1729 (unsigned long)status);
1730 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1731 queue_writel(queue, ISR, GEM_BIT(WOL));
1732 pm_wakeup_event(&bp->pdev->dev, 0);
1733 }
1734
1735 spin_unlock(&bp->lock);
1736
1737 return IRQ_HANDLED;
1738}
1739
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001740static irqreturn_t macb_interrupt(int irq, void *dev_id)
1741{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001742 struct macb_queue *queue = dev_id;
1743 struct macb *bp = queue->bp;
1744 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001745 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001746
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001747 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001748
1749 if (unlikely(!status))
1750 return IRQ_NONE;
1751
1752 spin_lock(&bp->lock);
1753
1754 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001755 /* close possible race with dev_close */
1756 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001757 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001758 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1759 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001760 break;
1761 }
1762
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001763 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1764 (unsigned int)(queue - bp->queues),
1765 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001766
Harini Katakame5010702019-01-29 15:20:03 +05301767 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001768 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001769 * until we have processed the buffers. The
1770 * scheduling call may fail if the poll routine
1771 * is already scheduled, so disable interrupts
1772 * now.
1773 */
Harini Katakame5010702019-01-29 15:20:03 +05301774 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001775 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001776 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001777
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001778 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001779 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001780 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001781 }
1782 }
1783
Nicolas Ferree86cd532012-10-31 06:04:57 +00001784 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001785 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1786 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001787
1788 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001789 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001790
Nicolas Ferree86cd532012-10-31 06:04:57 +00001791 break;
1792 }
1793
1794 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001795 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001796
Claudiu Beznea42983882018-12-17 10:02:42 +00001797 if (status & MACB_BIT(TXUBR))
1798 macb_tx_restart(queue);
1799
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001800 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001801 * add that if/when we get our hands on a full-blown MII PHY.
1802 */
1803
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001804 /* There is a hardware issue under heavy load where DMA can
1805 * stop, this causes endless "used buffer descriptor read"
1806 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301807 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1808 * section 16.7.4 for details. RXUBR is only enabled for
1809 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001810 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001811 if (status & MACB_BIT(RXUBR)) {
1812 ctrl = macb_readl(bp, NCR);
1813 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001814 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001815 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1816
1817 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001818 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001819 }
1820
Alexander Steinb19f7f72011-04-13 05:03:24 +00001821 if (status & MACB_BIT(ISR_ROVR)) {
1822 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001823 if (macb_is_gem(bp))
1824 bp->hw_stats.gem.rx_overruns++;
1825 else
1826 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001827
1828 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001829 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001830 }
1831
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001832 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301833 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001834 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001835
1836 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001837 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001838 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001839 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001840 }
1841
1842 spin_unlock(&bp->lock);
1843
1844 return IRQ_HANDLED;
1845}
1846
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001847#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001848/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001849 * to allow network i/o with interrupts disabled.
1850 */
1851static void macb_poll_controller(struct net_device *dev)
1852{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001853 struct macb *bp = netdev_priv(dev);
1854 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001855 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001856 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001857
1858 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001859 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1860 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001861 local_irq_restore(flags);
1862}
1863#endif
1864
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001865static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001866 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001867 struct sk_buff *skb,
1868 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001869{
1870 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001871 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001872 struct macb_tx_skb *tx_skb = NULL;
1873 struct macb_dma_desc *desc;
1874 unsigned int offset, size, count = 0;
1875 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001876 unsigned int eof = 1, mss_mfs = 0;
1877 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1878
1879 /* LSO */
1880 if (skb_shinfo(skb)->gso_size != 0) {
1881 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1882 /* UDP - UFO */
1883 lso_ctrl = MACB_LSO_UFO_ENABLE;
1884 else
1885 /* TCP - TSO */
1886 lso_ctrl = MACB_LSO_TSO_ENABLE;
1887 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001888
1889 /* First, map non-paged data */
1890 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001891
1892 /* first buffer length */
1893 size = hdrlen;
1894
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001895 offset = 0;
1896 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001897 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001898 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001899
1900 mapping = dma_map_single(&bp->pdev->dev,
1901 skb->data + offset,
1902 size, DMA_TO_DEVICE);
1903 if (dma_mapping_error(&bp->pdev->dev, mapping))
1904 goto dma_error;
1905
1906 /* Save info to properly release resources */
1907 tx_skb->skb = NULL;
1908 tx_skb->mapping = mapping;
1909 tx_skb->size = size;
1910 tx_skb->mapped_as_page = false;
1911
1912 len -= size;
1913 offset += size;
1914 count++;
1915 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001916
1917 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001918 }
1919
1920 /* Then, map paged data from fragments */
1921 for (f = 0; f < nr_frags; f++) {
1922 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1923
1924 len = skb_frag_size(frag);
1925 offset = 0;
1926 while (len) {
1927 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001928 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001929 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001930
1931 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1932 offset, size, DMA_TO_DEVICE);
1933 if (dma_mapping_error(&bp->pdev->dev, mapping))
1934 goto dma_error;
1935
1936 /* Save info to properly release resources */
1937 tx_skb->skb = NULL;
1938 tx_skb->mapping = mapping;
1939 tx_skb->size = size;
1940 tx_skb->mapped_as_page = true;
1941
1942 len -= size;
1943 offset += size;
1944 count++;
1945 tx_head++;
1946 }
1947 }
1948
1949 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001950 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001951 netdev_err(bp->dev, "BUG! empty skb!\n");
1952 return 0;
1953 }
1954
1955 /* This is the last buffer of the frame: save socket buffer */
1956 tx_skb->skb = skb;
1957
1958 /* Update TX ring: update buffer descriptors in reverse order
1959 * to avoid race condition
1960 */
1961
1962 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1963 * to set the end of TX queue
1964 */
1965 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001966 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001967 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001968 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001969 desc->ctrl = ctrl;
1970
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001971 if (lso_ctrl) {
1972 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1973 /* include header and FCS in value given to h/w */
1974 mss_mfs = skb_shinfo(skb)->gso_size +
1975 skb_transport_offset(skb) +
1976 ETH_FCS_LEN;
1977 else /* TSO */ {
1978 mss_mfs = skb_shinfo(skb)->gso_size;
1979 /* TCP Sequence Number Source Select
1980 * can be set only for TSO
1981 */
1982 seq_ctrl = 0;
1983 }
1984 }
1985
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001986 do {
1987 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001988 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001989 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001990 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001991
1992 ctrl = (u32)tx_skb->size;
1993 if (eof) {
1994 ctrl |= MACB_BIT(TX_LAST);
1995 eof = 0;
1996 }
Zach Brownb410d132016-10-19 09:56:57 -05001997 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001998 ctrl |= MACB_BIT(TX_WRAP);
1999
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002000 /* First descriptor is header descriptor */
2001 if (i == queue->tx_head) {
2002 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
2003 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002004 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
2005 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
2006 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002007 } else
2008 /* Only set MSS/MFS on payload descriptors
2009 * (second or later descriptor)
2010 */
2011 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
2012
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002013 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002014 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002015 /* desc->addr must be visible to hardware before clearing
2016 * 'TX_USED' bit in desc->ctrl.
2017 */
2018 wmb();
2019 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002020 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002021
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002022 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002023
2024 return count;
2025
2026dma_error:
2027 netdev_err(bp->dev, "TX DMA map failed\n");
2028
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002029 for (i = queue->tx_head; i != tx_head; i++) {
2030 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002031
2032 macb_tx_unmap(bp, tx_skb);
2033 }
2034
2035 return 0;
2036}
2037
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002038static netdev_features_t macb_features_check(struct sk_buff *skb,
2039 struct net_device *dev,
2040 netdev_features_t features)
2041{
2042 unsigned int nr_frags, f;
2043 unsigned int hdrlen;
2044
2045 /* Validate LSO compatibility */
2046
Harini Katakam41c1ef92020-02-05 18:08:11 +05302047 /* there is only one buffer or protocol is not UDP */
2048 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002049 return features;
2050
2051 /* length of header */
2052 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002053
Harini Katakam41c1ef92020-02-05 18:08:11 +05302054 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002055 * When software supplies two or more payload buffers all payload buffers
2056 * apart from the last must be a multiple of 8 bytes in size.
2057 */
2058 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2059 return features & ~MACB_NETIF_LSO;
2060
2061 nr_frags = skb_shinfo(skb)->nr_frags;
2062 /* No need to check last fragment */
2063 nr_frags--;
2064 for (f = 0; f < nr_frags; f++) {
2065 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2066
2067 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2068 return features & ~MACB_NETIF_LSO;
2069 }
2070 return features;
2071}
2072
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02002073static inline int macb_clear_csum(struct sk_buff *skb)
2074{
2075 /* no change for packets without checksum offloading */
2076 if (skb->ip_summed != CHECKSUM_PARTIAL)
2077 return 0;
2078
2079 /* make sure we can modify the header */
2080 if (unlikely(skb_cow_head(skb, 0)))
2081 return -1;
2082
2083 /* initialize checksum field
2084 * This is required - at least for Zynq, which otherwise calculates
2085 * wrong UDP header checksums for UDP packets with UDP data len <=2
2086 */
2087 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2088 return 0;
2089}
2090
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002091static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
2092{
Mark Deneen403dc162020-10-30 15:58:14 +00002093 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2094 skb_is_nonlinear(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002095 int padlen = ETH_ZLEN - (*skb)->len;
2096 int headroom = skb_headroom(*skb);
2097 int tailroom = skb_tailroom(*skb);
2098 struct sk_buff *nskb;
2099 u32 fcs;
2100
2101 if (!(ndev->features & NETIF_F_HW_CSUM) ||
2102 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2103 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
2104 return 0;
2105
2106 if (padlen <= 0) {
2107 /* FCS could be appeded to tailroom. */
2108 if (tailroom >= ETH_FCS_LEN)
2109 goto add_fcs;
2110 /* FCS could be appeded by moving data to headroom. */
2111 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
2112 padlen = 0;
2113 /* No room for FCS, need to reallocate skb. */
2114 else
Tristram Ha899ecae2018-10-24 14:51:23 -07002115 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002116 } else {
2117 /* Add room for FCS. */
2118 padlen += ETH_FCS_LEN;
2119 }
2120
2121 if (!cloned && headroom + tailroom >= padlen) {
2122 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
2123 skb_set_tail_pointer(*skb, (*skb)->len);
2124 } else {
2125 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2126 if (!nskb)
2127 return -ENOMEM;
2128
Huang Zijiangf3e5c072019-02-14 14:41:18 +08002129 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002130 *skb = nskb;
2131 }
2132
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00002133 if (padlen > ETH_FCS_LEN)
2134 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002135
2136add_fcs:
2137 /* set FCS to packet */
2138 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2139 fcs = ~fcs;
2140
2141 skb_put_u8(*skb, fcs & 0xff);
2142 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2143 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2144 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2145
2146 return 0;
2147}
2148
Claudiu Beznead1c38952018-08-07 12:25:12 +03002149static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002150{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002151 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002152 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002153 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07002154 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002155 unsigned int desc_cnt, nr_frags, frag_size, f;
2156 unsigned int hdrlen;
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002157 bool is_lso;
Claudiu Beznead1c38952018-08-07 12:25:12 +03002158 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002159
Claudiu Beznea33729f22018-08-07 12:25:13 +03002160 if (macb_clear_csum(skb)) {
2161 dev_kfree_skb_any(skb);
2162 return ret;
2163 }
2164
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002165 if (macb_pad_and_fcs(&skb, dev)) {
2166 dev_kfree_skb_any(skb);
2167 return ret;
2168 }
2169
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002170 is_lso = (skb_shinfo(skb)->gso_size != 0);
2171
2172 if (is_lso) {
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002173 /* length of headers */
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002174 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002175 /* only queue eth + ip headers separately for UDP */
2176 hdrlen = skb_transport_offset(skb);
2177 else
2178 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
2179 if (skb_headlen(skb) < hdrlen) {
2180 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
2181 /* if this is required, would need to copy to single buffer */
2182 return NETDEV_TX_BUSY;
2183 }
2184 } else
2185 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002186
Havard Skinnemoena268adb2012-10-31 06:04:52 +00002187#if defined(DEBUG) && defined(VERBOSE_DEBUG)
2188 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002189 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
2190 queue_index, skb->len, skb->head, skb->data,
2191 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002192 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2193 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002194#endif
2195
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002196 /* Count how many TX buffer descriptors are needed to send this
2197 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07002198 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002199 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002200 if (is_lso && (skb_headlen(skb) > hdrlen))
2201 /* extra header descriptor if also payload in first buffer */
2202 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2203 else
2204 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002205 nr_frags = skb_shinfo(skb)->nr_frags;
2206 for (f = 0; f < nr_frags; f++) {
2207 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002208 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002209 }
2210
Dongdong Deng48719532009-08-23 19:49:07 -07002211 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002212
2213 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05002214 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002215 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002216 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07002217 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002218 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002219 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00002220 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002221 }
2222
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002223 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002224 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07002225 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08002226 goto unlock;
2227 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00002228
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00002229 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002230 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00002231 skb_tx_timestamp(skb);
2232
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002233 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2234
Zach Brownb410d132016-10-19 09:56:57 -05002235 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002236 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002237
Soren Brinkmann92030902014-03-04 08:46:39 -08002238unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002239 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002240
Claudiu Beznead1c38952018-08-07 12:25:12 +03002241 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002242}
2243
Nicolas Ferre4df95132013-06-04 21:57:12 +00002244static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002245{
2246 if (!macb_is_gem(bp)) {
2247 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2248 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002249 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002250
Nicolas Ferre1b447912013-06-04 21:57:11 +00002251 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002252 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002253 "RX buffer must be multiple of %d bytes, expanding\n",
2254 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002255 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002256 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002257 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002258 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002259
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002260 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002261 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002262}
2263
Nicolas Ferre4df95132013-06-04 21:57:12 +00002264static void gem_free_rx_buffers(struct macb *bp)
2265{
2266 struct sk_buff *skb;
2267 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002268 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002269 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002270 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002271 int i;
2272
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002273 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2274 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002275 continue;
2276
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002277 for (i = 0; i < bp->rx_ring_size; i++) {
2278 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002279
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002280 if (!skb)
2281 continue;
2282
2283 desc = macb_rx_desc(queue, i);
2284 addr = macb_get_addr(bp, desc);
2285
2286 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2287 DMA_FROM_DEVICE);
2288 dev_kfree_skb_any(skb);
2289 skb = NULL;
2290 }
2291
2292 kfree(queue->rx_skbuff);
2293 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002294 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002295}
2296
2297static void macb_free_rx_buffers(struct macb *bp)
2298{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002299 struct macb_queue *queue = &bp->queues[0];
2300
2301 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002302 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002303 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002304 queue->rx_buffers, queue->rx_buffers_dma);
2305 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002306 }
2307}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002308
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002309static void macb_free_consistent(struct macb *bp)
2310{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002311 struct macb_queue *queue;
2312 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302313 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002314
Nicolas Ferre4df95132013-06-04 21:57:12 +00002315 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002316
2317 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2318 kfree(queue->tx_skb);
2319 queue->tx_skb = NULL;
2320 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302321 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2322 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002323 queue->tx_ring, queue->tx_ring_dma);
2324 queue->tx_ring = NULL;
2325 }
Harini Katakame50b7702018-07-06 12:18:57 +05302326 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302327 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2328 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302329 queue->rx_ring, queue->rx_ring_dma);
2330 queue->rx_ring = NULL;
2331 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002332 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002333}
2334
2335static int gem_alloc_rx_buffers(struct macb *bp)
2336{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002337 struct macb_queue *queue;
2338 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002339 int size;
2340
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002341 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2342 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2343 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2344 if (!queue->rx_skbuff)
2345 return -ENOMEM;
2346 else
2347 netdev_dbg(bp->dev,
2348 "Allocated %d RX struct sk_buff entries at %p\n",
2349 bp->rx_ring_size, queue->rx_skbuff);
2350 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002351 return 0;
2352}
2353
2354static int macb_alloc_rx_buffers(struct macb *bp)
2355{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002356 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002357 int size;
2358
Zach Brownb410d132016-10-19 09:56:57 -05002359 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002360 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2361 &queue->rx_buffers_dma, GFP_KERNEL);
2362 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002363 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002364
2365 netdev_dbg(bp->dev,
2366 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002367 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002368 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002369}
2370
2371static int macb_alloc_consistent(struct macb *bp)
2372{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002373 struct macb_queue *queue;
2374 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002375 int size;
2376
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002377 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302378 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002379 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2380 &queue->tx_ring_dma,
2381 GFP_KERNEL);
2382 if (!queue->tx_ring)
2383 goto out_err;
2384 netdev_dbg(bp->dev,
2385 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2386 q, size, (unsigned long)queue->tx_ring_dma,
2387 queue->tx_ring);
2388
Zach Brownb410d132016-10-19 09:56:57 -05002389 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002390 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2391 if (!queue->tx_skb)
2392 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002393
Harini Katakam404cd082018-07-06 12:18:58 +05302394 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002395 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2396 &queue->rx_ring_dma, GFP_KERNEL);
2397 if (!queue->rx_ring)
2398 goto out_err;
2399 netdev_dbg(bp->dev,
2400 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2401 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002402 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002403 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002404 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405
2406 return 0;
2407
2408out_err:
2409 macb_free_consistent(bp);
2410 return -ENOMEM;
2411}
2412
Nicolas Ferre4df95132013-06-04 21:57:12 +00002413static void gem_init_rings(struct macb *bp)
2414{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002415 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002416 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002417 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002418 int i;
2419
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002420 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002421 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002422 desc = macb_tx_desc(queue, i);
2423 macb_set_addr(bp, desc, 0);
2424 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002425 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002426 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002427 queue->tx_head = 0;
2428 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002429
2430 queue->rx_tail = 0;
2431 queue->rx_prepared_head = 0;
2432
2433 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002434 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002435
Nicolas Ferre4df95132013-06-04 21:57:12 +00002436}
2437
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002438static void macb_init_rings(struct macb *bp)
2439{
2440 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002441 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002442
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002443 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002444
Zach Brownb410d132016-10-19 09:56:57 -05002445 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002446 desc = macb_tx_desc(&bp->queues[0], i);
2447 macb_set_addr(bp, desc, 0);
2448 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002449 }
Ben Shelton21d35152015-04-22 17:28:54 -05002450 bp->queues[0].tx_head = 0;
2451 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002452 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002453}
2454
2455static void macb_reset_hw(struct macb *bp)
2456{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002457 struct macb_queue *queue;
2458 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002459 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002460
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002461 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002462 * more gracefully?)
2463 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002464 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002465
2466 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002467 ctrl |= MACB_BIT(CLRSTAT);
2468
2469 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002470
2471 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002472 macb_writel(bp, TSR, -1);
2473 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002474
2475 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002476 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2477 queue_writel(queue, IDR, -1);
2478 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002479 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2480 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002481 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002482}
2483
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002484static u32 gem_mdc_clk_div(struct macb *bp)
2485{
2486 u32 config;
2487 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2488
2489 if (pclk_hz <= 20000000)
2490 config = GEM_BF(CLK, GEM_CLK_DIV8);
2491 else if (pclk_hz <= 40000000)
2492 config = GEM_BF(CLK, GEM_CLK_DIV16);
2493 else if (pclk_hz <= 80000000)
2494 config = GEM_BF(CLK, GEM_CLK_DIV32);
2495 else if (pclk_hz <= 120000000)
2496 config = GEM_BF(CLK, GEM_CLK_DIV48);
2497 else if (pclk_hz <= 160000000)
2498 config = GEM_BF(CLK, GEM_CLK_DIV64);
2499 else
2500 config = GEM_BF(CLK, GEM_CLK_DIV96);
2501
2502 return config;
2503}
2504
2505static u32 macb_mdc_clk_div(struct macb *bp)
2506{
2507 u32 config;
2508 unsigned long pclk_hz;
2509
2510 if (macb_is_gem(bp))
2511 return gem_mdc_clk_div(bp);
2512
2513 pclk_hz = clk_get_rate(bp->pclk);
2514 if (pclk_hz <= 20000000)
2515 config = MACB_BF(CLK, MACB_CLK_DIV8);
2516 else if (pclk_hz <= 40000000)
2517 config = MACB_BF(CLK, MACB_CLK_DIV16);
2518 else if (pclk_hz <= 80000000)
2519 config = MACB_BF(CLK, MACB_CLK_DIV32);
2520 else
2521 config = MACB_BF(CLK, MACB_CLK_DIV64);
2522
2523 return config;
2524}
2525
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002526/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002527 * should program. We find the width from decoding the design configuration
2528 * register to find the maximum supported data bus width.
2529 */
2530static u32 macb_dbw(struct macb *bp)
2531{
2532 if (!macb_is_gem(bp))
2533 return 0;
2534
2535 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2536 case 4:
2537 return GEM_BF(DBW, GEM_DBW128);
2538 case 2:
2539 return GEM_BF(DBW, GEM_DBW64);
2540 case 1:
2541 default:
2542 return GEM_BF(DBW, GEM_DBW32);
2543 }
2544}
2545
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002546/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002547 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002548 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002549 * (if not supported by FIFO, it will fallback to default)
2550 * - set both rx/tx packet buffers to full memory size
2551 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002552 */
2553static void macb_configure_dma(struct macb *bp)
2554{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002555 struct macb_queue *queue;
2556 u32 buffer_size;
2557 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002558 u32 dmacfg;
2559
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002560 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002561 if (macb_is_gem(bp)) {
2562 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002563 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2564 if (q)
2565 queue_writel(queue, RBQS, buffer_size);
2566 else
2567 dmacfg |= GEM_BF(RXBS, buffer_size);
2568 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002569 if (bp->dma_burst_length)
2570 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002571 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302572 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302573
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002574 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302575 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2576 else
2577 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2578
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002579 if (bp->dev->features & NETIF_F_HW_CSUM)
2580 dmacfg |= GEM_BIT(TXCOEN);
2581 else
2582 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302583
Michal Simekbd620722018-09-25 08:32:50 +02002584 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302585#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002586 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002587 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302588#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002589#ifdef CONFIG_MACB_USE_HWSTAMP
2590 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2591 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2592#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002593 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2594 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002595 gem_writel(bp, DMACFG, dmacfg);
2596 }
2597}
2598
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002599static void macb_init_hw(struct macb *bp)
2600{
2601 u32 config;
2602
2603 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002604 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002605
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002606 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002607 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002608 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002609 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302610 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2611 else
2612 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002613 if (bp->dev->flags & IFF_PROMISC)
2614 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002615 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2616 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002617 if (!(bp->dev->flags & IFF_BROADCAST))
2618 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002619 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002620 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002621 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302622 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302623 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002624 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302625 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002626
Jamie Iles0116da42011-03-14 17:38:30 +00002627 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002628}
2629
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002630/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002631 * locations in the memory map. The least significant bits are stored
2632 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2633 *
2634 * The unicast hash enable and the multicast hash enable bits in the
2635 * network configuration register enable the reception of hash matched
2636 * frames. The destination address is reduced to a 6 bit index into
2637 * the 64 bit hash register using the following hash function. The
2638 * hash function is an exclusive or of every sixth bit of the
2639 * destination address.
2640 *
2641 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2642 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2643 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2644 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2645 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2646 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2647 *
2648 * da[0] represents the least significant bit of the first byte
2649 * received, that is, the multicast/unicast indicator, and da[47]
2650 * represents the most significant bit of the last byte received. If
2651 * the hash index, hi[n], points to a bit that is set in the hash
2652 * register then the frame will be matched according to whether the
2653 * frame is multicast or unicast. A multicast match will be signalled
2654 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2655 * index points to a bit set in the hash register. A unicast match
2656 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2657 * and the hash index points to a bit set in the hash register. To
2658 * receive all multicast frames, the hash register should be set with
2659 * all ones and the multicast hash enable bit should be set in the
2660 * network configuration register.
2661 */
2662
2663static inline int hash_bit_value(int bitnr, __u8 *addr)
2664{
2665 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2666 return 1;
2667 return 0;
2668}
2669
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002670/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002671static int hash_get_index(__u8 *addr)
2672{
2673 int i, j, bitval;
2674 int hash_index = 0;
2675
2676 for (j = 0; j < 6; j++) {
2677 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002678 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002679
2680 hash_index |= (bitval << j);
2681 }
2682
2683 return hash_index;
2684}
2685
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002686/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002687static void macb_sethashtable(struct net_device *dev)
2688{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002689 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002690 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002691 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002692 struct macb *bp = netdev_priv(dev);
2693
Moritz Fischeraa50b552016-03-29 19:11:13 -07002694 mc_filter[0] = 0;
2695 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002696
Jiri Pirko22bedad32010-04-01 21:22:57 +00002697 netdev_for_each_mc_addr(ha, dev) {
2698 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002699 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2700 }
2701
Jamie Ilesf75ba502011-11-08 10:12:32 +00002702 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2703 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002704}
2705
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002706/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002707static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002708{
2709 unsigned long cfg;
2710 struct macb *bp = netdev_priv(dev);
2711
2712 cfg = macb_readl(bp, NCFGR);
2713
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002714 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002715 /* Enable promiscuous mode */
2716 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002717
2718 /* Disable RX checksum offload */
2719 if (macb_is_gem(bp))
2720 cfg &= ~GEM_BIT(RXCOEN);
2721 } else {
2722 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002723 cfg &= ~MACB_BIT(CAF);
2724
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002725 /* Enable RX checksum offload only if requested */
2726 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2727 cfg |= GEM_BIT(RXCOEN);
2728 }
2729
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002730 if (dev->flags & IFF_ALLMULTI) {
2731 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002732 macb_or_gem_writel(bp, HRB, -1);
2733 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002734 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002735 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002736 /* Enable specific multicasts */
2737 macb_sethashtable(dev);
2738 cfg |= MACB_BIT(NCFGR_MTI);
2739 } else if (dev->flags & (~IFF_ALLMULTI)) {
2740 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002741 macb_or_gem_writel(bp, HRB, 0);
2742 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002743 cfg &= ~MACB_BIT(NCFGR_MTI);
2744 }
2745
2746 macb_writel(bp, NCFGR, cfg);
2747}
2748
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002749static int macb_open(struct net_device *dev)
2750{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002751 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002752 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002753 struct macb_queue *queue;
2754 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002755 int err;
2756
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002757 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002758
Harini Katakamd54f89a2019-03-01 16:20:34 +05302759 err = pm_runtime_get_sync(&bp->pdev->dev);
2760 if (err < 0)
2761 goto pm_exit;
2762
Nicolas Ferre1b447912013-06-04 21:57:11 +00002763 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002764 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002765
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002766 err = macb_alloc_consistent(bp);
2767 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002768 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2769 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302770 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002771 }
2772
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002773 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2774 napi_enable(&queue->napi);
2775
Harini Katakam05044532019-05-07 19:59:10 +05302776 macb_init_hw(bp);
2777
Antoine Tenart7897b072019-11-13 10:00:06 +01002778 err = macb_phylink_connect(bp);
2779 if (err)
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002780 goto reset_hw;
frederic RODO6c36a702007-07-12 19:07:24 +02002781
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002782 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002783
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002784 if (bp->ptp_info)
2785 bp->ptp_info->ptp_init(dev);
2786
Charles Keepax939a5bf72020-06-15 14:18:54 +01002787 return 0;
2788
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002789reset_hw:
2790 macb_reset_hw(bp);
Corentin Labbe014406b2020-06-10 09:53:44 +00002791 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2792 napi_disable(&queue->napi);
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002793 macb_free_consistent(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302794pm_exit:
Charles Keepax939a5bf72020-06-15 14:18:54 +01002795 pm_runtime_put_sync(&bp->pdev->dev);
2796 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002797}
2798
2799static int macb_close(struct net_device *dev)
2800{
2801 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002802 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002803 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002804 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002805
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002806 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002807
2808 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2809 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002810
Antoine Tenart7897b072019-11-13 10:00:06 +01002811 phylink_stop(bp->phylink);
2812 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002813
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002814 spin_lock_irqsave(&bp->lock, flags);
2815 macb_reset_hw(bp);
2816 netif_carrier_off(dev);
2817 spin_unlock_irqrestore(&bp->lock, flags);
2818
2819 macb_free_consistent(bp);
2820
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002821 if (bp->ptp_info)
2822 bp->ptp_info->ptp_remove(dev);
2823
Harini Katakamd54f89a2019-03-01 16:20:34 +05302824 pm_runtime_put(&bp->pdev->dev);
2825
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002826 return 0;
2827}
2828
Harini Katakama5898ea2015-05-06 22:27:18 +05302829static int macb_change_mtu(struct net_device *dev, int new_mtu)
2830{
Harini Katakama5898ea2015-05-06 22:27:18 +05302831 if (netif_running(dev))
2832 return -EBUSY;
2833
Harini Katakama5898ea2015-05-06 22:27:18 +05302834 dev->mtu = new_mtu;
2835
2836 return 0;
2837}
2838
Jamie Ilesa494ed82011-03-09 16:26:35 +00002839static void gem_update_stats(struct macb *bp)
2840{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002841 struct macb_queue *queue;
2842 unsigned int i, q, idx;
2843 unsigned long *stat;
2844
Jamie Ilesa494ed82011-03-09 16:26:35 +00002845 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002846
Xander Huff3ff13f12015-01-13 16:15:51 -06002847 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2848 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002849 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002850
2851 bp->ethtool_stats[i] += val;
2852 *p += val;
2853
2854 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2855 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002856 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002857 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002858 *(++p) += val;
2859 }
2860 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002861
2862 idx = GEM_STATS_LEN;
2863 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2864 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2865 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002866}
2867
2868static struct net_device_stats *gem_get_stats(struct macb *bp)
2869{
2870 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002871 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002872
Zong Li5eff1462021-05-22 17:16:11 +08002873 if (!netif_running(bp->dev))
2874 return nstat;
2875
Jamie Ilesa494ed82011-03-09 16:26:35 +00002876 gem_update_stats(bp);
2877
2878 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2879 hwstat->rx_alignment_errors +
2880 hwstat->rx_resource_errors +
2881 hwstat->rx_overruns +
2882 hwstat->rx_oversize_frames +
2883 hwstat->rx_jabbers +
2884 hwstat->rx_undersized_frames +
2885 hwstat->rx_length_field_frame_errors);
2886 nstat->tx_errors = (hwstat->tx_late_collisions +
2887 hwstat->tx_excessive_collisions +
2888 hwstat->tx_underrun +
2889 hwstat->tx_carrier_sense_errors);
2890 nstat->multicast = hwstat->rx_multicast_frames;
2891 nstat->collisions = (hwstat->tx_single_collision_frames +
2892 hwstat->tx_multiple_collision_frames +
2893 hwstat->tx_excessive_collisions);
2894 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2895 hwstat->rx_jabbers +
2896 hwstat->rx_undersized_frames +
2897 hwstat->rx_length_field_frame_errors);
2898 nstat->rx_over_errors = hwstat->rx_resource_errors;
2899 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2900 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2901 nstat->rx_fifo_errors = hwstat->rx_overruns;
2902 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2903 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2904 nstat->tx_fifo_errors = hwstat->tx_underrun;
2905
2906 return nstat;
2907}
2908
Xander Huff3ff13f12015-01-13 16:15:51 -06002909static void gem_get_ethtool_stats(struct net_device *dev,
2910 struct ethtool_stats *stats, u64 *data)
2911{
2912 struct macb *bp;
2913
2914 bp = netdev_priv(dev);
2915 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002916 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2917 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002918}
2919
2920static int gem_get_sset_count(struct net_device *dev, int sset)
2921{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002922 struct macb *bp = netdev_priv(dev);
2923
Xander Huff3ff13f12015-01-13 16:15:51 -06002924 switch (sset) {
2925 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002926 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002927 default:
2928 return -EOPNOTSUPP;
2929 }
2930}
2931
2932static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2933{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002934 char stat_string[ETH_GSTRING_LEN];
2935 struct macb *bp = netdev_priv(dev);
2936 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002937 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002938 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002939
2940 switch (sset) {
2941 case ETH_SS_STATS:
2942 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2943 memcpy(p, gem_statistics[i].stat_string,
2944 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002945
2946 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2947 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2948 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2949 q, queue_statistics[i].stat_string);
2950 memcpy(p, stat_string, ETH_GSTRING_LEN);
2951 }
2952 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002953 break;
2954 }
2955}
2956
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002957static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002958{
2959 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002960 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002961 struct macb_stats *hwstat = &bp->hw_stats.macb;
2962
2963 if (macb_is_gem(bp))
2964 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002965
frederic RODO6c36a702007-07-12 19:07:24 +02002966 /* read stats from hardware */
2967 macb_update_stats(bp);
2968
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002969 /* Convert HW stats into netdevice stats */
2970 nstat->rx_errors = (hwstat->rx_fcs_errors +
2971 hwstat->rx_align_errors +
2972 hwstat->rx_resource_errors +
2973 hwstat->rx_overruns +
2974 hwstat->rx_oversize_pkts +
2975 hwstat->rx_jabbers +
2976 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002977 hwstat->rx_length_mismatch);
2978 nstat->tx_errors = (hwstat->tx_late_cols +
2979 hwstat->tx_excessive_cols +
2980 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002981 hwstat->tx_carrier_errors +
2982 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002983 nstat->collisions = (hwstat->tx_single_cols +
2984 hwstat->tx_multiple_cols +
2985 hwstat->tx_excessive_cols);
2986 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2987 hwstat->rx_jabbers +
2988 hwstat->rx_undersize_pkts +
2989 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002990 nstat->rx_over_errors = hwstat->rx_resource_errors +
2991 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002992 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2993 nstat->rx_frame_errors = hwstat->rx_align_errors;
2994 nstat->rx_fifo_errors = hwstat->rx_overruns;
2995 /* XXX: What does "missed" mean? */
2996 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2997 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2998 nstat->tx_fifo_errors = hwstat->tx_underruns;
2999 /* Don't know about heartbeat or window errors... */
3000
3001 return nstat;
3002}
3003
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003004static int macb_get_regs_len(struct net_device *netdev)
3005{
3006 return MACB_GREGS_NBR * sizeof(u32);
3007}
3008
3009static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
3010 void *p)
3011{
3012 struct macb *bp = netdev_priv(dev);
3013 unsigned int tail, head;
3014 u32 *regs_buff = p;
3015
3016 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
3017 | MACB_GREGS_VERSION;
3018
Zach Brownb410d132016-10-19 09:56:57 -05003019 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
3020 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003021
3022 regs_buff[0] = macb_readl(bp, NCR);
3023 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
3024 regs_buff[2] = macb_readl(bp, NSR);
3025 regs_buff[3] = macb_readl(bp, TSR);
3026 regs_buff[4] = macb_readl(bp, RBQP);
3027 regs_buff[5] = macb_readl(bp, TBQP);
3028 regs_buff[6] = macb_readl(bp, RSR);
3029 regs_buff[7] = macb_readl(bp, IMR);
3030
3031 regs_buff[8] = tail;
3032 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003033 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
3034 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003035
Neil Armstrongce721a72016-01-05 14:39:16 +01003036 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
3037 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003038 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003039 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003040}
3041
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003042static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3043{
3044 struct macb *bp = netdev_priv(netdev);
3045
Nicolas Ferre253fe092020-07-10 14:46:43 +02003046 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
Antoine Tenart7897b072019-11-13 10:00:06 +01003047 phylink_ethtool_get_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003048 wol->supported |= WAKE_MAGIC;
3049
3050 if (bp->wol & MACB_WOL_ENABLED)
3051 wol->wolopts |= WAKE_MAGIC;
3052 }
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003053}
3054
3055static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3056{
3057 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01003058 int ret;
3059
Nicolas Ferre253fe092020-07-10 14:46:43 +02003060 /* Pass the order to phylink layer */
Antoine Tenart7897b072019-11-13 10:00:06 +01003061 ret = phylink_ethtool_set_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003062 /* Don't manage WoL on MAC if handled by the PHY
3063 * or if there's a failure in talking to the PHY
3064 */
3065 if (!ret || ret != -EOPNOTSUPP)
3066 return ret;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003067
3068 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
3069 (wol->wolopts & ~WAKE_MAGIC))
3070 return -EOPNOTSUPP;
3071
3072 if (wol->wolopts & WAKE_MAGIC)
3073 bp->wol |= MACB_WOL_ENABLED;
3074 else
3075 bp->wol &= ~MACB_WOL_ENABLED;
3076
3077 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
3078
3079 return 0;
3080}
3081
Antoine Tenart7897b072019-11-13 10:00:06 +01003082static int macb_get_link_ksettings(struct net_device *netdev,
3083 struct ethtool_link_ksettings *kset)
3084{
3085 struct macb *bp = netdev_priv(netdev);
3086
3087 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3088}
3089
3090static int macb_set_link_ksettings(struct net_device *netdev,
3091 const struct ethtool_link_ksettings *kset)
3092{
3093 struct macb *bp = netdev_priv(netdev);
3094
3095 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3096}
3097
Zach Brown8441bb32016-10-19 09:56:58 -05003098static void macb_get_ringparam(struct net_device *netdev,
3099 struct ethtool_ringparam *ring)
3100{
3101 struct macb *bp = netdev_priv(netdev);
3102
3103 ring->rx_max_pending = MAX_RX_RING_SIZE;
3104 ring->tx_max_pending = MAX_TX_RING_SIZE;
3105
3106 ring->rx_pending = bp->rx_ring_size;
3107 ring->tx_pending = bp->tx_ring_size;
3108}
3109
3110static int macb_set_ringparam(struct net_device *netdev,
3111 struct ethtool_ringparam *ring)
3112{
3113 struct macb *bp = netdev_priv(netdev);
3114 u32 new_rx_size, new_tx_size;
3115 unsigned int reset = 0;
3116
3117 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3118 return -EINVAL;
3119
3120 new_rx_size = clamp_t(u32, ring->rx_pending,
3121 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3122 new_rx_size = roundup_pow_of_two(new_rx_size);
3123
3124 new_tx_size = clamp_t(u32, ring->tx_pending,
3125 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3126 new_tx_size = roundup_pow_of_two(new_tx_size);
3127
3128 if ((new_tx_size == bp->tx_ring_size) &&
3129 (new_rx_size == bp->rx_ring_size)) {
3130 /* nothing to do */
3131 return 0;
3132 }
3133
3134 if (netif_running(bp->dev)) {
3135 reset = 1;
3136 macb_close(bp->dev);
3137 }
3138
3139 bp->rx_ring_size = new_rx_size;
3140 bp->tx_ring_size = new_tx_size;
3141
3142 if (reset)
3143 macb_open(bp->dev);
3144
3145 return 0;
3146}
3147
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003148#ifdef CONFIG_MACB_USE_HWSTAMP
3149static unsigned int gem_get_tsu_rate(struct macb *bp)
3150{
3151 struct clk *tsu_clk;
3152 unsigned int tsu_rate;
3153
3154 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
3155 if (!IS_ERR(tsu_clk))
3156 tsu_rate = clk_get_rate(tsu_clk);
3157 /* try pclk instead */
3158 else if (!IS_ERR(bp->pclk)) {
3159 tsu_clk = bp->pclk;
3160 tsu_rate = clk_get_rate(tsu_clk);
3161 } else
3162 return -ENOTSUPP;
3163 return tsu_rate;
3164}
3165
3166static s32 gem_get_ptp_max_adj(void)
3167{
3168 return 64000000;
3169}
3170
3171static int gem_get_ts_info(struct net_device *dev,
3172 struct ethtool_ts_info *info)
3173{
3174 struct macb *bp = netdev_priv(dev);
3175
3176 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
3177 ethtool_op_get_ts_info(dev, info);
3178 return 0;
3179 }
3180
3181 info->so_timestamping =
3182 SOF_TIMESTAMPING_TX_SOFTWARE |
3183 SOF_TIMESTAMPING_RX_SOFTWARE |
3184 SOF_TIMESTAMPING_SOFTWARE |
3185 SOF_TIMESTAMPING_TX_HARDWARE |
3186 SOF_TIMESTAMPING_RX_HARDWARE |
3187 SOF_TIMESTAMPING_RAW_HARDWARE;
3188 info->tx_types =
3189 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3190 (1 << HWTSTAMP_TX_OFF) |
3191 (1 << HWTSTAMP_TX_ON);
3192 info->rx_filters =
3193 (1 << HWTSTAMP_FILTER_NONE) |
3194 (1 << HWTSTAMP_FILTER_ALL);
3195
3196 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
3197
3198 return 0;
3199}
3200
3201static struct macb_ptp_info gem_ptp_info = {
3202 .ptp_init = gem_ptp_init,
3203 .ptp_remove = gem_ptp_remove,
3204 .get_ptp_max_adj = gem_get_ptp_max_adj,
3205 .get_tsu_rate = gem_get_tsu_rate,
3206 .get_ts_info = gem_get_ts_info,
3207 .get_hwtst = gem_get_hwtst,
3208 .set_hwtst = gem_set_hwtst,
3209};
3210#endif
3211
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003212static int macb_get_ts_info(struct net_device *netdev,
3213 struct ethtool_ts_info *info)
3214{
3215 struct macb *bp = netdev_priv(netdev);
3216
3217 if (bp->ptp_info)
3218 return bp->ptp_info->get_ts_info(netdev, info);
3219
3220 return ethtool_op_get_ts_info(netdev, info);
3221}
3222
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003223static void gem_enable_flow_filters(struct macb *bp, bool enable)
3224{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003225 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003226 struct ethtool_rx_fs_item *item;
3227 u32 t2_scr;
3228 int num_t2_scr;
3229
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003230 if (!(netdev->features & NETIF_F_NTUPLE))
3231 return;
3232
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003233 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3234
3235 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3236 struct ethtool_rx_flow_spec *fs = &item->fs;
3237 struct ethtool_tcpip4_spec *tp4sp_m;
3238
3239 if (fs->location >= num_t2_scr)
3240 continue;
3241
3242 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3243
3244 /* enable/disable screener regs for the flow entry */
3245 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3246
3247 /* only enable fields with no masking */
3248 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3249
3250 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3251 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3252 else
3253 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3254
3255 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3256 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3257 else
3258 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3259
3260 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3261 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3262 else
3263 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3264
3265 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3266 }
3267}
3268
3269static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3270{
3271 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3272 uint16_t index = fs->location;
3273 u32 w0, w1, t2_scr;
3274 bool cmp_a = false;
3275 bool cmp_b = false;
3276 bool cmp_c = false;
3277
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003278 if (!macb_is_gem(bp))
3279 return;
3280
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003281 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3282 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3283
3284 /* ignore field if any masking set */
3285 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3286 /* 1st compare reg - IP source address */
3287 w0 = 0;
3288 w1 = 0;
3289 w0 = tp4sp_v->ip4src;
3290 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3291 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3292 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3293 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3294 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3295 cmp_a = true;
3296 }
3297
3298 /* ignore field if any masking set */
3299 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3300 /* 2nd compare reg - IP destination address */
3301 w0 = 0;
3302 w1 = 0;
3303 w0 = tp4sp_v->ip4dst;
3304 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3305 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3306 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3307 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3308 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3309 cmp_b = true;
3310 }
3311
3312 /* ignore both port fields if masking set in both */
3313 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3314 /* 3rd compare reg - source port, destination port */
3315 w0 = 0;
3316 w1 = 0;
3317 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3318 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3319 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3320 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3321 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3322 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3323 } else {
3324 /* only one port definition */
3325 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3326 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3327 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3328 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3329 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3330 } else { /* dst port */
3331 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3332 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3333 }
3334 }
3335 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3336 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3337 cmp_c = true;
3338 }
3339
3340 t2_scr = 0;
3341 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3342 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3343 if (cmp_a)
3344 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3345 if (cmp_b)
3346 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3347 if (cmp_c)
3348 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3349 gem_writel_n(bp, SCRT2, index, t2_scr);
3350}
3351
3352static int gem_add_flow_filter(struct net_device *netdev,
3353 struct ethtool_rxnfc *cmd)
3354{
3355 struct macb *bp = netdev_priv(netdev);
3356 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3357 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003358 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003359 int ret = -EINVAL;
3360 bool added = false;
3361
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003362 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003363 if (newfs == NULL)
3364 return -ENOMEM;
3365 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3366
3367 netdev_dbg(netdev,
3368 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3369 fs->flow_type, (int)fs->ring_cookie, fs->location,
3370 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3371 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3372 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3373
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003374 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3375
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003376 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003377 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3378 if (item->fs.location > newfs->fs.location) {
3379 list_add_tail(&newfs->list, &item->list);
3380 added = true;
3381 break;
3382 } else if (item->fs.location == fs->location) {
3383 netdev_err(netdev, "Rule not added: location %d not free!\n",
3384 fs->location);
3385 ret = -EBUSY;
3386 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003387 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003388 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003389 if (!added)
3390 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003391
3392 gem_prog_cmp_regs(bp, fs);
3393 bp->rx_fs_list.count++;
3394 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003395 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003396
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003397 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003398 return 0;
3399
3400err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003401 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003402 kfree(newfs);
3403 return ret;
3404}
3405
3406static int gem_del_flow_filter(struct net_device *netdev,
3407 struct ethtool_rxnfc *cmd)
3408{
3409 struct macb *bp = netdev_priv(netdev);
3410 struct ethtool_rx_fs_item *item;
3411 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003412 unsigned long flags;
3413
3414 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003415
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003416 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3417 if (item->fs.location == cmd->fs.location) {
3418 /* disable screener regs for the flow entry */
3419 fs = &(item->fs);
3420 netdev_dbg(netdev,
3421 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3422 fs->flow_type, (int)fs->ring_cookie, fs->location,
3423 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3424 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3425 htons(fs->h_u.tcp_ip4_spec.psrc),
3426 htons(fs->h_u.tcp_ip4_spec.pdst));
3427
3428 gem_writel_n(bp, SCRT2, fs->location, 0);
3429
3430 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003431 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003432 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3433 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003434 return 0;
3435 }
3436 }
3437
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003438 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003439 return -EINVAL;
3440}
3441
3442static int gem_get_flow_entry(struct net_device *netdev,
3443 struct ethtool_rxnfc *cmd)
3444{
3445 struct macb *bp = netdev_priv(netdev);
3446 struct ethtool_rx_fs_item *item;
3447
3448 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3449 if (item->fs.location == cmd->fs.location) {
3450 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3451 return 0;
3452 }
3453 }
3454 return -EINVAL;
3455}
3456
3457static int gem_get_all_flow_entries(struct net_device *netdev,
3458 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3459{
3460 struct macb *bp = netdev_priv(netdev);
3461 struct ethtool_rx_fs_item *item;
3462 uint32_t cnt = 0;
3463
3464 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3465 if (cnt == cmd->rule_cnt)
3466 return -EMSGSIZE;
3467 rule_locs[cnt] = item->fs.location;
3468 cnt++;
3469 }
3470 cmd->data = bp->max_tuples;
3471 cmd->rule_cnt = cnt;
3472
3473 return 0;
3474}
3475
3476static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3477 u32 *rule_locs)
3478{
3479 struct macb *bp = netdev_priv(netdev);
3480 int ret = 0;
3481
3482 switch (cmd->cmd) {
3483 case ETHTOOL_GRXRINGS:
3484 cmd->data = bp->num_queues;
3485 break;
3486 case ETHTOOL_GRXCLSRLCNT:
3487 cmd->rule_cnt = bp->rx_fs_list.count;
3488 break;
3489 case ETHTOOL_GRXCLSRULE:
3490 ret = gem_get_flow_entry(netdev, cmd);
3491 break;
3492 case ETHTOOL_GRXCLSRLALL:
3493 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3494 break;
3495 default:
3496 netdev_err(netdev,
3497 "Command parameter %d is not supported\n", cmd->cmd);
3498 ret = -EOPNOTSUPP;
3499 }
3500
3501 return ret;
3502}
3503
3504static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3505{
3506 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003507 int ret;
3508
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003509 switch (cmd->cmd) {
3510 case ETHTOOL_SRXCLSRLINS:
3511 if ((cmd->fs.location >= bp->max_tuples)
3512 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3513 ret = -EINVAL;
3514 break;
3515 }
3516 ret = gem_add_flow_filter(netdev, cmd);
3517 break;
3518 case ETHTOOL_SRXCLSRLDEL:
3519 ret = gem_del_flow_filter(netdev, cmd);
3520 break;
3521 default:
3522 netdev_err(netdev,
3523 "Command parameter %d is not supported\n", cmd->cmd);
3524 ret = -EOPNOTSUPP;
3525 }
3526
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003527 return ret;
3528}
3529
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003530static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003531 .get_regs_len = macb_get_regs_len,
3532 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003533 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003534 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003535 .get_wol = macb_get_wol,
3536 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003537 .get_link_ksettings = macb_get_link_ksettings,
3538 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003539 .get_ringparam = macb_get_ringparam,
3540 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003541};
Xander Huff8cd5a562015-01-15 15:55:20 -06003542
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003543static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003544 .get_regs_len = macb_get_regs_len,
3545 .get_regs = macb_get_regs,
Nicolas Ferre558e35c2020-07-20 10:56:52 +02003546 .get_wol = macb_get_wol,
3547 .set_wol = macb_set_wol,
Xander Huff8cd5a562015-01-15 15:55:20 -06003548 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003549 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003550 .get_ethtool_stats = gem_get_ethtool_stats,
3551 .get_strings = gem_get_ethtool_strings,
3552 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003553 .get_link_ksettings = macb_get_link_ksettings,
3554 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003555 .get_ringparam = macb_get_ringparam,
3556 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003557 .get_rxnfc = gem_get_rxnfc,
3558 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003559};
3560
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003561static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003562{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003563 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003564
3565 if (!netif_running(dev))
3566 return -EINVAL;
3567
Antoine Tenart7897b072019-11-13 10:00:06 +01003568 if (bp->ptp_info) {
3569 switch (cmd) {
3570 case SIOCSHWTSTAMP:
3571 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3572 case SIOCGHWTSTAMP:
3573 return bp->ptp_info->get_hwtst(dev, rq);
3574 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003575 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003576
3577 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003578}
3579
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003580static inline void macb_set_txcsum_feature(struct macb *bp,
3581 netdev_features_t features)
3582{
3583 u32 val;
3584
3585 if (!macb_is_gem(bp))
3586 return;
3587
3588 val = gem_readl(bp, DMACFG);
3589 if (features & NETIF_F_HW_CSUM)
3590 val |= GEM_BIT(TXCOEN);
3591 else
3592 val &= ~GEM_BIT(TXCOEN);
3593
3594 gem_writel(bp, DMACFG, val);
3595}
3596
3597static inline void macb_set_rxcsum_feature(struct macb *bp,
3598 netdev_features_t features)
3599{
3600 struct net_device *netdev = bp->dev;
3601 u32 val;
3602
3603 if (!macb_is_gem(bp))
3604 return;
3605
3606 val = gem_readl(bp, NCFGR);
3607 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3608 val |= GEM_BIT(RXCOEN);
3609 else
3610 val &= ~GEM_BIT(RXCOEN);
3611
3612 gem_writel(bp, NCFGR, val);
3613}
3614
3615static inline void macb_set_rxflow_feature(struct macb *bp,
3616 netdev_features_t features)
3617{
3618 if (!macb_is_gem(bp))
3619 return;
3620
3621 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3622}
3623
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003624static int macb_set_features(struct net_device *netdev,
3625 netdev_features_t features)
3626{
3627 struct macb *bp = netdev_priv(netdev);
3628 netdev_features_t changed = features ^ netdev->features;
3629
3630 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003631 if (changed & NETIF_F_HW_CSUM)
3632 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003633
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003634 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003635 if (changed & NETIF_F_RXCSUM)
3636 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003637
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003638 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003639 if (changed & NETIF_F_NTUPLE)
3640 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003641
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003642 return 0;
3643}
3644
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003645static void macb_restore_features(struct macb *bp)
3646{
3647 struct net_device *netdev = bp->dev;
3648 netdev_features_t features = netdev->features;
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003649 struct ethtool_rx_fs_item *item;
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003650
3651 /* TX checksum offload */
3652 macb_set_txcsum_feature(bp, features);
3653
3654 /* RX checksum offload */
3655 macb_set_rxcsum_feature(bp, features);
3656
3657 /* RX Flow Filters */
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003658 list_for_each_entry(item, &bp->rx_fs_list.list, list)
3659 gem_prog_cmp_regs(bp, &item->fs);
3660
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003661 macb_set_rxflow_feature(bp, features);
3662}
3663
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003664static const struct net_device_ops macb_netdev_ops = {
3665 .ndo_open = macb_open,
3666 .ndo_stop = macb_close,
3667 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003668 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003669 .ndo_get_stats = macb_get_stats,
Arnd Bergmanna7605372021-07-27 15:45:13 +02003670 .ndo_eth_ioctl = macb_ioctl,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003671 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303672 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003673 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003674#ifdef CONFIG_NET_POLL_CONTROLLER
3675 .ndo_poll_controller = macb_poll_controller,
3676#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003677 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003678 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003679};
3680
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003681/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003682 * and integration options used
3683 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003684static void macb_configure_caps(struct macb *bp,
3685 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003686{
3687 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003688
Nicolas Ferref6970502015-03-31 15:02:01 +02003689 if (dt_conf)
3690 bp->caps = dt_conf->caps;
3691
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003692 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003693 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3694
Nicolas Ferree1755872014-07-24 13:50:58 +02003695 dcfg = gem_readl(bp, DCFG1);
3696 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3697 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +01003698 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
3699 bp->caps |= MACB_CAPS_PCS;
3700 dcfg = gem_readl(bp, DCFG12);
3701 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
3702 bp->caps |= MACB_CAPS_HIGH_SPEED;
Nicolas Ferree1755872014-07-24 13:50:58 +02003703 dcfg = gem_readl(bp, DCFG2);
3704 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3705 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003706#ifdef CONFIG_MACB_USE_HWSTAMP
3707 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003708 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003709 dev_err(&bp->pdev->dev,
3710 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003711 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003712 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003713 bp->ptp_info = &gem_ptp_info;
3714 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003715 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003716#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003717 }
3718
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003719 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003720}
3721
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003722static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003723 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003724 unsigned int *queue_mask,
3725 unsigned int *num_queues)
3726{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003727 *queue_mask = 0x1;
3728 *num_queues = 1;
3729
Nicolas Ferreda120112015-03-31 15:02:00 +02003730 /* is it macb or gem ?
3731 *
3732 * We need to read directly from the hardware here because
3733 * we are early in the probe process and don't have the
3734 * MACB_CAPS_MACB_IS_GEM flag positioned
3735 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003736 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003737 return;
3738
3739 /* bit 0 is never set but queue 0 always exists */
Claudiu Bezneafec371f2020-07-02 12:05:58 +03003740 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
Claudiu Bezneab7ab39b2020-07-02 12:05:59 +03003741 *num_queues = hweight32(*queue_mask);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003742}
3743
Claudiu Beznea38493da2020-12-09 15:03:34 +02003744static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
3745 struct clk *rx_clk, struct clk *tsu_clk)
3746{
3747 struct clk_bulk_data clks[] = {
3748 { .clk = tsu_clk, },
3749 { .clk = rx_clk, },
3750 { .clk = pclk, },
3751 { .clk = hclk, },
3752 { .clk = tx_clk },
3753 };
3754
3755 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
3756}
3757
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003758static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303759 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303760 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003761{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003762 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003763 int err;
3764
Bartosz Folta83a77e92016-12-14 06:39:15 +00003765 pdata = dev_get_platdata(&pdev->dev);
3766 if (pdata) {
3767 *pclk = pdata->pclk;
3768 *hclk = pdata->hclk;
3769 } else {
3770 *pclk = devm_clk_get(&pdev->dev, "pclk");
3771 *hclk = devm_clk_get(&pdev->dev, "hclk");
3772 }
3773
Michael Trettera04be4b2021-03-17 17:16:09 +01003774 if (IS_ERR_OR_NULL(*pclk))
3775 return dev_err_probe(&pdev->dev,
3776 IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
3777 "failed to get pclk\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003778
Michael Trettera04be4b2021-03-17 17:16:09 +01003779 if (IS_ERR_OR_NULL(*hclk))
3780 return dev_err_probe(&pdev->dev,
3781 IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
3782 "failed to get hclk\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003783
Michael Tretterbd310aca2019-10-18 16:11:43 +02003784 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003785 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003786 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003787
Michael Tretterbd310aca2019-10-18 16:11:43 +02003788 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303789 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003790 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303791
Michael Tretterbd310aca2019-10-18 16:11:43 +02003792 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303793 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003794 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303795
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003796 err = clk_prepare_enable(*pclk);
3797 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003798 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003799 return err;
3800 }
3801
3802 err = clk_prepare_enable(*hclk);
3803 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003804 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003805 goto err_disable_pclk;
3806 }
3807
3808 err = clk_prepare_enable(*tx_clk);
3809 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003810 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003811 goto err_disable_hclk;
3812 }
3813
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303814 err = clk_prepare_enable(*rx_clk);
3815 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003816 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303817 goto err_disable_txclk;
3818 }
3819
Harini Katakamf5473d12019-03-01 16:20:33 +05303820 err = clk_prepare_enable(*tsu_clk);
3821 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003822 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303823 goto err_disable_rxclk;
3824 }
3825
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003826 return 0;
3827
Harini Katakamf5473d12019-03-01 16:20:33 +05303828err_disable_rxclk:
3829 clk_disable_unprepare(*rx_clk);
3830
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303831err_disable_txclk:
3832 clk_disable_unprepare(*tx_clk);
3833
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003834err_disable_hclk:
3835 clk_disable_unprepare(*hclk);
3836
3837err_disable_pclk:
3838 clk_disable_unprepare(*pclk);
3839
3840 return err;
3841}
3842
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003843static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003844{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003845 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003846 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003847 struct macb *bp = netdev_priv(dev);
3848 struct macb_queue *queue;
3849 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003850 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003851
Zach Brownb410d132016-10-19 09:56:57 -05003852 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3853 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3854
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003855 /* set the queue register mapping once for all: queue0 has a special
3856 * register mapping but we don't want to test the queue index then
3857 * compute the corresponding register offset at run time.
3858 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003859 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003860 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003861 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003862
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003863 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003864 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003865 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003866 if (hw_q) {
3867 queue->ISR = GEM_ISR(hw_q - 1);
3868 queue->IER = GEM_IER(hw_q - 1);
3869 queue->IDR = GEM_IDR(hw_q - 1);
3870 queue->IMR = GEM_IMR(hw_q - 1);
3871 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003872 queue->RBQP = GEM_RBQP(hw_q - 1);
3873 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303874#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003875 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003876 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003877 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3878 }
Harini Katakamfff80192016-08-09 13:15:53 +05303879#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003880 } else {
3881 /* queue0 uses legacy registers */
3882 queue->ISR = MACB_ISR;
3883 queue->IER = MACB_IER;
3884 queue->IDR = MACB_IDR;
3885 queue->IMR = MACB_IMR;
3886 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003887 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303888#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003889 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003890 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003891 queue->RBQPH = MACB_RBQPH;
3892 }
Harini Katakamfff80192016-08-09 13:15:53 +05303893#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003894 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003895
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003896 /* get irq: here we use the linux queue index, not the hardware
3897 * queue index. the queue irq definitions in the device tree
3898 * must remove the optional gaps that could exist in the
3899 * hardware queue mask.
3900 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003901 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003902 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003903 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003904 if (err) {
3905 dev_err(&pdev->dev,
3906 "Unable to request IRQ %d (error %d)\n",
3907 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003908 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003909 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003910
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003911 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003912 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003913 }
3914
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003915 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003916
Nicolas Ferre4df95132013-06-04 21:57:12 +00003917 /* setup appropriated routines according to adapter type */
3918 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003919 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003920 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3921 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3922 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3923 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003924 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003925 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003926 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003927 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3928 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3929 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3930 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003931 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003932 }
3933
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003934 /* Set features */
3935 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003936
3937 /* Check LSO capability */
3938 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3939 dev->hw_features |= MACB_NETIF_LSO;
3940
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003941 /* Checksum offload is only available on gem with packet buffer */
3942 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003943 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003944 if (bp->caps & MACB_CAPS_SG_DISABLED)
3945 dev->hw_features &= ~NETIF_F_SG;
3946 dev->features = dev->hw_features;
3947
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003948 /* Check RX Flow Filters support.
3949 * Max Rx flows set by availability of screeners & compare regs:
3950 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3951 */
3952 reg = gem_readl(bp, DCFG8);
3953 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3954 GEM_BFEXT(T2SCR, reg));
Claudiu Bezneaa714e272021-04-14 14:20:29 +03003955 INIT_LIST_HEAD(&bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003956 if (bp->max_tuples > 0) {
3957 /* also needs one ethtype match to check IPv4 */
3958 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3959 /* program this reg now */
3960 reg = 0;
3961 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3962 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3963 /* Filtering is supported in hw but don't enable it in kernel now */
3964 dev->hw_features |= NETIF_F_NTUPLE;
3965 /* init Rx flow definitions */
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003966 bp->rx_fs_list.count = 0;
3967 spin_lock_init(&bp->rx_fs_lock);
3968 } else
3969 bp->max_tuples = 0;
3970 }
3971
Neil Armstrongce721a72016-01-05 14:39:16 +01003972 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3973 val = 0;
Alexandre Belloni2ccb0162020-07-18 01:32:21 +02003974 if (phy_interface_mode_is_rgmii(bp->phy_interface))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003975 val = bp->usrio->rgmii;
Neil Armstrongce721a72016-01-05 14:39:16 +01003976 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003977 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003978 val = bp->usrio->rmii;
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003979 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003980 val = bp->usrio->mii;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003981
Neil Armstrongce721a72016-01-05 14:39:16 +01003982 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003983 val |= bp->usrio->refclk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003984
Neil Armstrongce721a72016-01-05 14:39:16 +01003985 macb_or_gem_writel(bp, USRIO, val);
3986 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003987
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003988 /* Set MII management clock divider */
3989 val = macb_mdc_clk_div(bp);
3990 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303991 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3992 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003993 macb_writel(bp, NCFGR, val);
3994
3995 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003996}
3997
Atish Patrab1242232021-03-03 11:55:49 -08003998static const struct macb_usrio_config macb_default_usrio = {
3999 .mii = MACB_BIT(MII),
4000 .rmii = MACB_BIT(RMII),
4001 .rgmii = GEM_BIT(RGMII),
4002 .refclk = MACB_BIT(CLKEN),
4003};
4004
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004005#if defined(CONFIG_OF)
4006/* 1518 rounded up */
4007#define AT91ETHER_MAX_RBUFF_SZ 0x600
4008/* max number of receive buffers */
4009#define AT91ETHER_MAX_RX_DESCR 9
4010
Arnd Bergmann49db9222019-07-08 14:48:23 +02004011static struct sifive_fu540_macb_mgmt *mgmt;
4012
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004013static int at91ether_alloc_coherent(struct macb *lp)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004014{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004015 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004016
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004017 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004018 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004019 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004020 &q->rx_ring_dma, GFP_KERNEL);
4021 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004022 return -ENOMEM;
4023
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004024 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004025 AT91ETHER_MAX_RX_DESCR *
4026 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004027 &q->rx_buffers_dma, GFP_KERNEL);
4028 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004029 dma_free_coherent(&lp->pdev->dev,
4030 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004031 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004032 q->rx_ring, q->rx_ring_dma);
4033 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004034 return -ENOMEM;
4035 }
4036
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004037 return 0;
4038}
4039
4040static void at91ether_free_coherent(struct macb *lp)
4041{
4042 struct macb_queue *q = &lp->queues[0];
4043
4044 if (q->rx_ring) {
4045 dma_free_coherent(&lp->pdev->dev,
4046 AT91ETHER_MAX_RX_DESCR *
4047 macb_dma_desc_get_size(lp),
4048 q->rx_ring, q->rx_ring_dma);
4049 q->rx_ring = NULL;
4050 }
4051
4052 if (q->rx_buffers) {
4053 dma_free_coherent(&lp->pdev->dev,
4054 AT91ETHER_MAX_RX_DESCR *
4055 AT91ETHER_MAX_RBUFF_SZ,
4056 q->rx_buffers, q->rx_buffers_dma);
4057 q->rx_buffers = NULL;
4058 }
4059}
4060
4061/* Initialize and start the Receiver and Transmit subsystems */
4062static int at91ether_start(struct macb *lp)
4063{
4064 struct macb_queue *q = &lp->queues[0];
4065 struct macb_dma_desc *desc;
4066 dma_addr_t addr;
4067 u32 ctl;
4068 int i, ret;
4069
4070 ret = at91ether_alloc_coherent(lp);
4071 if (ret)
4072 return ret;
4073
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004074 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004075 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004076 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004077 macb_set_addr(lp, desc, addr);
4078 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004079 addr += AT91ETHER_MAX_RBUFF_SZ;
4080 }
4081
4082 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004083 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004084
4085 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004086 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004087
4088 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004089 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004090
4091 /* Enable Receive and Transmit */
4092 ctl = macb_readl(lp, NCR);
4093 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
4094
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004095 /* Enable MAC interrupts */
4096 macb_writel(lp, IER, MACB_BIT(RCOMP) |
4097 MACB_BIT(RXUBR) |
4098 MACB_BIT(ISR_TUND) |
4099 MACB_BIT(ISR_RLE) |
4100 MACB_BIT(TCOMP) |
4101 MACB_BIT(ISR_ROVR) |
4102 MACB_BIT(HRESP));
4103
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004104 return 0;
4105}
4106
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004107static void at91ether_stop(struct macb *lp)
4108{
4109 u32 ctl;
4110
4111 /* Disable MAC interrupts */
4112 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
4113 MACB_BIT(RXUBR) |
4114 MACB_BIT(ISR_TUND) |
4115 MACB_BIT(ISR_RLE) |
4116 MACB_BIT(TCOMP) |
4117 MACB_BIT(ISR_ROVR) |
4118 MACB_BIT(HRESP));
4119
4120 /* Disable Receiver and Transmitter */
4121 ctl = macb_readl(lp, NCR);
4122 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
4123
4124 /* Free resources. */
4125 at91ether_free_coherent(lp);
4126}
4127
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004128/* Open the ethernet interface */
4129static int at91ether_open(struct net_device *dev)
4130{
4131 struct macb *lp = netdev_priv(dev);
4132 u32 ctl;
4133 int ret;
4134
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004135 ret = pm_runtime_get_sync(&lp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004136 if (ret < 0) {
4137 pm_runtime_put_noidle(&lp->pdev->dev);
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004138 return ret;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004139 }
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004140
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004141 /* Clear internal statistics */
4142 ctl = macb_readl(lp, NCR);
4143 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
4144
4145 macb_set_hwaddr(lp);
4146
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004147 ret = at91ether_start(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004148 if (ret)
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004149 goto pm_exit;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004150
Antoine Tenart7897b072019-11-13 10:00:06 +01004151 ret = macb_phylink_connect(lp);
4152 if (ret)
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004153 goto stop;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004154
4155 netif_start_queue(dev);
4156
4157 return 0;
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004158
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004159stop:
4160 at91ether_stop(lp);
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004161pm_exit:
4162 pm_runtime_put_sync(&lp->pdev->dev);
4163 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004164}
4165
4166/* Close the interface */
4167static int at91ether_close(struct net_device *dev)
4168{
4169 struct macb *lp = netdev_priv(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004170
4171 netif_stop_queue(dev);
4172
Antoine Tenart7897b072019-11-13 10:00:06 +01004173 phylink_stop(lp->phylink);
4174 phylink_disconnect_phy(lp->phylink);
4175
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004176 at91ether_stop(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004177
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004178 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004179}
4180
4181/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03004182static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
4183 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004184{
4185 struct macb *lp = netdev_priv(dev);
4186
Willy Tarreau1d608d22020-12-09 19:47:40 +01004187 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
4188 int desc = 0;
4189
4190 netif_stop_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004191
4192 /* Store packet information (to free when Tx completed) */
Willy Tarreau73d74222020-10-11 11:09:43 +02004193 lp->rm9200_txq[desc].skb = skb;
4194 lp->rm9200_txq[desc].size = skb->len;
4195 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
4196 skb->len, DMA_TO_DEVICE);
4197 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03004198 dev_kfree_skb_any(skb);
4199 dev->stats.tx_dropped++;
4200 netdev_err(dev, "%s: DMA mapping error\n", __func__);
4201 return NETDEV_TX_OK;
4202 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004203
4204 /* Set address of the data in the Transmit Address register */
Willy Tarreau73d74222020-10-11 11:09:43 +02004205 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004206 /* Set length of the packet in the Transmit Control register */
4207 macb_writel(lp, TCR, skb->len);
4208
4209 } else {
4210 netdev_err(dev, "%s called, but device is busy!\n", __func__);
4211 return NETDEV_TX_BUSY;
4212 }
4213
4214 return NETDEV_TX_OK;
4215}
4216
4217/* Extract received frame from buffer descriptors and sent to upper layers.
4218 * (Called from interrupt context)
4219 */
4220static void at91ether_rx(struct net_device *dev)
4221{
4222 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004223 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004224 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004225 unsigned char *p_recv;
4226 struct sk_buff *skb;
4227 unsigned int pktlen;
4228
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004229 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004230 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004231 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004232 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004233 skb = netdev_alloc_skb(dev, pktlen + 2);
4234 if (skb) {
4235 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004236 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004237
4238 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004239 dev->stats.rx_packets++;
4240 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004241 netif_rx(skb);
4242 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004243 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004244 }
4245
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004246 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004247 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004248
4249 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004250 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004251
4252 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004253 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
4254 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004255 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004256 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004257
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004258 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004259 }
4260}
4261
4262/* MAC interrupt handler */
4263static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
4264{
4265 struct net_device *dev = dev_id;
4266 struct macb *lp = netdev_priv(dev);
4267 u32 intstatus, ctl;
Willy Tarreau73d74222020-10-11 11:09:43 +02004268 unsigned int desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004269
4270 /* MAC Interrupt Status register indicates what interrupts are pending.
4271 * It is automatically cleared once read.
4272 */
4273 intstatus = macb_readl(lp, ISR);
4274
4275 /* Receive complete */
4276 if (intstatus & MACB_BIT(RCOMP))
4277 at91ether_rx(dev);
4278
4279 /* Transmit complete */
Willy Tarreau1d608d22020-12-09 19:47:40 +01004280 if (intstatus & MACB_BIT(TCOMP)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004281 /* The TCOM bit is set even if the transmission failed */
4282 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004283 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004284
Willy Tarreau1d608d22020-12-09 19:47:40 +01004285 desc = 0;
4286 if (lp->rm9200_txq[desc].skb) {
Willy Tarreau73d74222020-10-11 11:09:43 +02004287 dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
4288 lp->rm9200_txq[desc].skb = NULL;
4289 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
4290 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004291 dev->stats.tx_packets++;
Willy Tarreau73d74222020-10-11 11:09:43 +02004292 dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004293 }
Willy Tarreau1d608d22020-12-09 19:47:40 +01004294 netif_wake_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004295 }
4296
4297 /* Work-around for EMAC Errata section 41.3.1 */
4298 if (intstatus & MACB_BIT(RXUBR)) {
4299 ctl = macb_readl(lp, NCR);
4300 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08004301 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004302 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4303 }
4304
4305 if (intstatus & MACB_BIT(ISR_ROVR))
4306 netdev_err(dev, "ROVR error\n");
4307
4308 return IRQ_HANDLED;
4309}
4310
4311#ifdef CONFIG_NET_POLL_CONTROLLER
4312static void at91ether_poll_controller(struct net_device *dev)
4313{
4314 unsigned long flags;
4315
4316 local_irq_save(flags);
4317 at91ether_interrupt(dev->irq, dev);
4318 local_irq_restore(flags);
4319}
4320#endif
4321
4322static const struct net_device_ops at91ether_netdev_ops = {
4323 .ndo_open = at91ether_open,
4324 .ndo_stop = at91ether_close,
4325 .ndo_start_xmit = at91ether_start_xmit,
4326 .ndo_get_stats = macb_get_stats,
4327 .ndo_set_rx_mode = macb_set_rx_mode,
4328 .ndo_set_mac_address = eth_mac_addr,
Arnd Bergmanna7605372021-07-27 15:45:13 +02004329 .ndo_eth_ioctl = macb_ioctl,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004330 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004331#ifdef CONFIG_NET_POLL_CONTROLLER
4332 .ndo_poll_controller = at91ether_poll_controller,
4333#endif
4334};
4335
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004336static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304337 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304338 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004339{
4340 int err;
4341
4342 *hclk = NULL;
4343 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304344 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304345 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004346
4347 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4348 if (IS_ERR(*pclk))
4349 return PTR_ERR(*pclk);
4350
4351 err = clk_prepare_enable(*pclk);
4352 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004353 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004354 return err;
4355 }
4356
4357 return 0;
4358}
4359
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004360static int at91ether_init(struct platform_device *pdev)
4361{
4362 struct net_device *dev = platform_get_drvdata(pdev);
4363 struct macb *bp = netdev_priv(dev);
4364 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004365
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004366 bp->queues[0].bp = bp;
4367
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004368 dev->netdev_ops = &at91ether_netdev_ops;
4369 dev->ethtool_ops = &macb_ethtool_ops;
4370
4371 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4372 0, dev->name, dev);
4373 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004374 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004375
4376 macb_writel(bp, NCR, 0);
4377
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004378 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004379
4380 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004381}
4382
Yash Shahc218ad52019-06-18 13:26:08 +05304383static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4384 unsigned long parent_rate)
4385{
4386 return mgmt->rate;
4387}
4388
4389static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4390 unsigned long *parent_rate)
4391{
4392 if (WARN_ON(rate < 2500000))
4393 return 2500000;
4394 else if (rate == 2500000)
4395 return 2500000;
4396 else if (WARN_ON(rate < 13750000))
4397 return 2500000;
4398 else if (WARN_ON(rate < 25000000))
4399 return 25000000;
4400 else if (rate == 25000000)
4401 return 25000000;
4402 else if (WARN_ON(rate < 75000000))
4403 return 25000000;
4404 else if (WARN_ON(rate < 125000000))
4405 return 125000000;
4406 else if (rate == 125000000)
4407 return 125000000;
4408
4409 WARN_ON(rate > 125000000);
4410
4411 return 125000000;
4412}
4413
4414static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4415 unsigned long parent_rate)
4416{
4417 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4418 if (rate != 125000000)
4419 iowrite32(1, mgmt->reg);
4420 else
4421 iowrite32(0, mgmt->reg);
4422 mgmt->rate = rate;
4423
4424 return 0;
4425}
4426
4427static const struct clk_ops fu540_c000_ops = {
4428 .recalc_rate = fu540_macb_tx_recalc_rate,
4429 .round_rate = fu540_macb_tx_round_rate,
4430 .set_rate = fu540_macb_tx_set_rate,
4431};
4432
4433static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4434 struct clk **hclk, struct clk **tx_clk,
4435 struct clk **rx_clk, struct clk **tsu_clk)
4436{
4437 struct clk_init_data init;
4438 int err = 0;
4439
4440 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4441 if (err)
4442 return err;
4443
4444 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004445 if (!mgmt) {
4446 err = -ENOMEM;
4447 goto err_disable_clks;
4448 }
Yash Shahc218ad52019-06-18 13:26:08 +05304449
4450 init.name = "sifive-gemgxl-mgmt";
4451 init.ops = &fu540_c000_ops;
4452 init.flags = 0;
4453 init.num_parents = 0;
4454
4455 mgmt->rate = 0;
4456 mgmt->hw.init = &init;
4457
Stephen Boydd89091a2020-01-03 16:19:21 -08004458 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004459 if (IS_ERR(*tx_clk)) {
4460 err = PTR_ERR(*tx_clk);
4461 goto err_disable_clks;
4462 }
Yash Shahc218ad52019-06-18 13:26:08 +05304463
4464 err = clk_prepare_enable(*tx_clk);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004465 if (err) {
Yash Shahc218ad52019-06-18 13:26:08 +05304466 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004467 *tx_clk = NULL;
4468 goto err_disable_clks;
4469 } else {
Yash Shahc218ad52019-06-18 13:26:08 +05304470 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004471 }
Yash Shahc218ad52019-06-18 13:26:08 +05304472
4473 return 0;
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004474
4475err_disable_clks:
4476 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
4477
4478 return err;
Yash Shahc218ad52019-06-18 13:26:08 +05304479}
4480
4481static int fu540_c000_init(struct platform_device *pdev)
4482{
Dejin Zhengb959c772020-05-03 20:32:26 +08004483 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4484 if (IS_ERR(mgmt->reg))
4485 return PTR_ERR(mgmt->reg);
Yash Shahc218ad52019-06-18 13:26:08 +05304486
4487 return macb_init(pdev);
4488}
4489
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004490static const struct macb_usrio_config sama7g5_usrio = {
4491 .mii = 0,
4492 .rmii = 1,
4493 .rgmii = 2,
4494 .refclk = BIT(2),
4495 .hdfctlen = BIT(6),
4496};
4497
Yash Shahc218ad52019-06-18 13:26:08 +05304498static const struct macb_config fu540_c000_config = {
4499 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4500 MACB_CAPS_GEM_HAS_PTP,
4501 .dma_burst_length = 16,
4502 .clk_init = fu540_c000_clk_init,
4503 .init = fu540_c000_init,
4504 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004505 .usrio = &macb_default_usrio,
Yash Shahc218ad52019-06-18 13:26:08 +05304506};
4507
David S. Miller3cef5c52015-03-09 23:38:02 -04004508static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004509 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004510 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004511 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004512 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004513};
4514
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004515static const struct macb_config sama5d3macb_config = {
4516 .caps = MACB_CAPS_SG_DISABLED
4517 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4518 .clk_init = macb_clk_init,
4519 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004520 .usrio = &macb_default_usrio,
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004521};
4522
David S. Miller3cef5c52015-03-09 23:38:02 -04004523static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004524 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4525 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004526 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004527 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004528 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004529};
4530
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004531static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004532 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004533 .dma_burst_length = 16,
4534 .clk_init = macb_clk_init,
4535 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004536 .usrio = &macb_default_usrio,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004537};
4538
Hari Prasath7d13ad52021-08-12 13:14:21 +05304539static const struct macb_config sama5d29_config = {
4540 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP,
4541 .dma_burst_length = 16,
4542 .clk_init = macb_clk_init,
4543 .init = macb_init,
4544 .usrio = &macb_default_usrio,
4545};
4546
David S. Miller3cef5c52015-03-09 23:38:02 -04004547static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004548 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004549 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004550 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004551 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004552 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004553 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004554 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004555};
4556
David S. Miller3cef5c52015-03-09 23:38:02 -04004557static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004558 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004559 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004560 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004561 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004562 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004563};
4564
David S. Miller3cef5c52015-03-09 23:38:02 -04004565static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004566 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004567 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004568 .init = at91ether_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004569 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004570};
4571
Neil Armstronge611b5b2016-01-05 14:39:17 +01004572static const struct macb_config np4_config = {
4573 .caps = MACB_CAPS_USRIO_DISABLED,
4574 .clk_init = macb_clk_init,
4575 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004576 .usrio = &macb_default_usrio,
Neil Armstronge611b5b2016-01-05 14:39:17 +01004577};
David S. Miller36583eb2015-05-23 01:22:35 -04004578
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304579static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004580 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4581 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304582 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304583 .dma_burst_length = 16,
4584 .clk_init = macb_clk_init,
4585 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304586 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004587 .usrio = &macb_default_usrio,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304588};
4589
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004590static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304591 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4592 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004593 .dma_burst_length = 16,
4594 .clk_init = macb_clk_init,
4595 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004596 .usrio = &macb_default_usrio,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004597};
4598
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004599static const struct macb_config sama7g5_gem_config = {
Claudiu Beznea0f4f6d72021-09-17 16:26:15 +03004600 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
4601 MACB_CAPS_MIIONRGMII,
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004602 .dma_burst_length = 16,
4603 .clk_init = macb_clk_init,
4604 .init = macb_init,
4605 .usrio = &sama7g5_usrio,
4606};
4607
Claudiu Beznea700d5662020-12-09 15:03:39 +02004608static const struct macb_config sama7g5_emac_config = {
Claudiu Beznea0f4f6d72021-09-17 16:26:15 +03004609 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
4610 MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII,
Claudiu Beznea700d5662020-12-09 15:03:39 +02004611 .dma_burst_length = 16,
4612 .clk_init = macb_clk_init,
4613 .init = macb_init,
4614 .usrio = &sama7g5_usrio,
4615};
4616
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004617static const struct of_device_id macb_dt_ids[] = {
4618 { .compatible = "cdns,at32ap7000-macb" },
4619 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4620 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004621 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004622 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4623 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004624 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004625 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Hari Prasath7d13ad52021-08-12 13:14:21 +05304626 { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004627 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004628 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004629 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4630 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4631 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304632 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004633 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304634 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004635 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
Claudiu Beznea700d5662020-12-09 15:03:39 +02004636 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004637 { /* sentinel */ }
4638};
4639MODULE_DEVICE_TABLE(of, macb_dt_ids);
4640#endif /* CONFIG_OF */
4641
Bartosz Folta83a77e92016-12-14 06:39:15 +00004642static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004643 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4644 MACB_CAPS_JUMBO |
4645 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004646 .dma_burst_length = 16,
4647 .clk_init = macb_clk_init,
4648 .init = macb_init,
Atish Patrab1242232021-03-03 11:55:49 -08004649 .usrio = &macb_default_usrio,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004650 .jumbo_max_len = 10240,
4651};
4652
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004653static int macb_probe(struct platform_device *pdev)
4654{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004655 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004656 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304657 struct clk **, struct clk **, struct clk **,
4658 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004659 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004660 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304661 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304662 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004663 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004664 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004665 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004666 struct net_device *dev;
4667 struct resource *regs;
4668 void __iomem *mem;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004669 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304670 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004671
Yang Yingliang809660c2021-06-07 21:43:54 +08004672 mem = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004673 if (IS_ERR(mem))
4674 return PTR_ERR(mem);
4675
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004676 if (np) {
4677 const struct of_device_id *match;
4678
4679 match = of_match_node(macb_dt_ids, np);
4680 if (match && match->data) {
4681 macb_config = match->data;
4682 clk_init = macb_config->clk_init;
4683 init = macb_config->init;
4684 }
4685 }
4686
Harini Katakamf5473d12019-03-01 16:20:33 +05304687 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004688 if (err)
4689 return err;
4690
Harini Katakamd54f89a2019-03-01 16:20:34 +05304691 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4692 pm_runtime_use_autosuspend(&pdev->dev);
4693 pm_runtime_get_noresume(&pdev->dev);
4694 pm_runtime_set_active(&pdev->dev);
4695 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004696 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004697
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004698 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004699 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004700 if (!dev) {
4701 err = -ENOMEM;
4702 goto err_disable_clocks;
4703 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004704
4705 dev->base_addr = regs->start;
4706
4707 SET_NETDEV_DEV(dev, &pdev->dev);
4708
4709 bp = netdev_priv(dev);
4710 bp->pdev = pdev;
4711 bp->dev = dev;
4712 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004713 bp->native_io = native_io;
4714 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004715 bp->macb_reg_readl = hw_readl_native;
4716 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004717 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004718 bp->macb_reg_readl = hw_readl;
4719 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004720 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004721 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004722 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004723 if (macb_config)
4724 bp->dma_burst_length = macb_config->dma_burst_length;
4725 bp->pclk = pclk;
4726 bp->hclk = hclk;
4727 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304728 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304729 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004730 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304731 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304732
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004733 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004734 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004735 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
Nicolas Ferreced47992020-07-10 14:46:42 +02004736 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004737
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004738 bp->usrio = macb_config->usrio;
4739
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004740 spin_lock_init(&bp->lock);
4741
Nicolas Ferread783472015-03-31 15:02:02 +02004742 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004743 macb_configure_caps(bp, macb_config);
4744
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004745#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4746 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4747 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4748 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4749 }
4750#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004751 platform_set_drvdata(pdev, dev);
4752
4753 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004754 if (dev->irq < 0) {
4755 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004756 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004757 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004758
Jarod Wilson44770e12016-10-17 15:54:17 -04004759 /* MTU range: 68 - 1500 or 10240 */
4760 dev->min_mtu = GEM_MTU_MIN_SIZE;
4761 if (bp->caps & MACB_CAPS_JUMBO)
4762 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4763 else
4764 dev->max_mtu = ETH_DATA_LEN;
4765
Harini Katakam404cd082018-07-06 12:18:58 +05304766 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4767 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4768 if (val)
4769 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4770 macb_dma_desc_get_size(bp);
4771
4772 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4773 if (val)
4774 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4775 macb_dma_desc_get_size(bp);
4776 }
4777
Harini Katakame5010702019-01-29 15:20:03 +05304778 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4779 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4780 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4781
Michael Walle83216e32021-04-12 19:47:17 +02004782 err = of_get_mac_address(np, bp->dev->dev_addr);
4783 if (err == -EPROBE_DEFER)
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004784 goto err_out_free_netdev;
Michael Walle83216e32021-04-12 19:47:17 +02004785 else if (err)
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004786 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02004787
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004788 err = of_get_phy_mode(np, &interface);
4789 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004790 /* not found in DT, MII by default */
4791 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4792 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004793 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004794
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004795 /* IP specific init */
4796 err = init(pdev);
4797 if (err)
4798 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004799
Florian Fainellicf669662016-05-02 18:38:45 -07004800 err = macb_mii_init(bp);
4801 if (err)
4802 goto err_out_free_netdev;
4803
Florian Fainellicf669662016-05-02 18:38:45 -07004804 netif_carrier_off(dev);
4805
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004806 err = register_netdev(dev);
4807 if (err) {
4808 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004809 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004810 }
4811
Allen Paise7412b82020-09-14 12:59:23 +05304812 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
Harini Katakam032dc412018-01-27 12:09:01 +05304813
Bo Shen58798232014-09-13 01:57:49 +02004814 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4815 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4816 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004817
Harini Katakamd54f89a2019-03-01 16:20:34 +05304818 pm_runtime_mark_last_busy(&bp->pdev->dev);
4819 pm_runtime_put_autosuspend(&bp->pdev->dev);
4820
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004821 return 0;
4822
Florian Fainellicf669662016-05-02 18:38:45 -07004823err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004824 mdiobus_unregister(bp->mii_bus);
4825 mdiobus_free(bp->mii_bus);
4826
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004827err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004828 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004829
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004830err_disable_clocks:
Claudiu Beznea38493da2020-12-09 15:03:34 +02004831 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304832 pm_runtime_disable(&pdev->dev);
4833 pm_runtime_set_suspended(&pdev->dev);
4834 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004835
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004836 return err;
4837}
4838
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004839static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004840{
4841 struct net_device *dev;
4842 struct macb *bp;
4843
4844 dev = platform_get_drvdata(pdev);
4845
4846 if (dev) {
4847 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004848 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004849 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004850
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004851 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004852 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304853 pm_runtime_disable(&pdev->dev);
4854 pm_runtime_dont_use_autosuspend(&pdev->dev);
4855 if (!pm_runtime_suspended(&pdev->dev)) {
Claudiu Beznea38493da2020-12-09 15:03:34 +02004856 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
4857 bp->rx_clk, bp->tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304858 pm_runtime_set_suspended(&pdev->dev);
4859 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004860 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004861 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004862 }
4863
4864 return 0;
4865}
4866
Michal Simekd23823d2015-01-23 09:36:03 +01004867static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004868{
Wolfram Sangce886a42018-10-21 22:00:14 +02004869 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004870 struct macb *bp = netdev_priv(netdev);
Jiapeng Chongbbf6ace2021-04-29 18:25:46 +08004871 struct macb_queue *queue;
Harini Katakamde991c52019-03-01 16:20:35 +05304872 unsigned long flags;
4873 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004874 int err;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004875
Harini Katakamde991c52019-03-01 16:20:35 +05304876 if (!netif_running(netdev))
4877 return 0;
4878
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004879 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004880 spin_lock_irqsave(&bp->lock, flags);
4881 /* Flush all status bits */
4882 macb_writel(bp, TSR, -1);
4883 macb_writel(bp, RSR, -1);
Harini Katakamde991c52019-03-01 16:20:35 +05304884 for (q = 0, queue = bp->queues; q < bp->num_queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004885 ++q, ++queue) {
4886 /* Disable all interrupts */
4887 queue_writel(queue, IDR, -1);
4888 queue_readl(queue, ISR);
4889 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4890 queue_writel(queue, ISR, -1);
4891 }
4892 /* Change interrupt handler and
4893 * Enable WoL IRQ on queue 0
4894 */
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004895 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004896 if (macb_is_gem(bp)) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004897 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
4898 IRQF_SHARED, netdev->name, bp->queues);
4899 if (err) {
4900 dev_err(dev,
4901 "Unable to request IRQ %d (error %d)\n",
4902 bp->queues[0].irq, err);
4903 spin_unlock_irqrestore(&bp->lock, flags);
4904 return err;
4905 }
4906 queue_writel(bp->queues, IER, GEM_BIT(WOL));
4907 gem_writel(bp, WOL, MACB_BIT(MAG));
4908 } else {
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004909 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
4910 IRQF_SHARED, netdev->name, bp->queues);
4911 if (err) {
4912 dev_err(dev,
4913 "Unable to request IRQ %d (error %d)\n",
4914 bp->queues[0].irq, err);
4915 spin_unlock_irqrestore(&bp->lock, flags);
4916 return err;
4917 }
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004918 queue_writel(bp->queues, IER, MACB_BIT(WOL));
4919 macb_writel(bp, WOL, MACB_BIT(MAG));
4920 }
4921 spin_unlock_irqrestore(&bp->lock, flags);
4922
4923 enable_irq_wake(bp->queues[0].irq);
4924 }
4925
4926 netif_device_detach(netdev);
4927 for (q = 0, queue = bp->queues; q < bp->num_queues;
4928 ++q, ++queue)
4929 napi_disable(&queue->napi);
4930
4931 if (!(bp->wol & MACB_WOL_ENABLED)) {
Antoine Tenart7897b072019-11-13 10:00:06 +01004932 rtnl_lock();
4933 phylink_stop(bp->phylink);
4934 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304935 spin_lock_irqsave(&bp->lock, flags);
4936 macb_reset_hw(bp);
4937 spin_unlock_irqrestore(&bp->lock, flags);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304938 }
4939
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004940 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4941 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4942
4943 if (netdev->hw_features & NETIF_F_NTUPLE)
4944 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4945
Harini Katakamde991c52019-03-01 16:20:35 +05304946 if (bp->ptp_info)
4947 bp->ptp_info->ptp_remove(netdev);
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004948 if (!device_may_wakeup(dev))
4949 pm_runtime_force_suspend(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304950
4951 return 0;
4952}
4953
4954static int __maybe_unused macb_resume(struct device *dev)
4955{
4956 struct net_device *netdev = dev_get_drvdata(dev);
4957 struct macb *bp = netdev_priv(netdev);
Jiapeng Chongbbf6ace2021-04-29 18:25:46 +08004958 struct macb_queue *queue;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004959 unsigned long flags;
Harini Katakamde991c52019-03-01 16:20:35 +05304960 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004961 int err;
Harini Katakamde991c52019-03-01 16:20:35 +05304962
4963 if (!netif_running(netdev))
4964 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304965
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004966 if (!device_may_wakeup(dev))
4967 pm_runtime_force_resume(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304968
4969 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004970 spin_lock_irqsave(&bp->lock, flags);
4971 /* Disable WoL */
4972 if (macb_is_gem(bp)) {
4973 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
4974 gem_writel(bp, WOL, 0);
4975 } else {
4976 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
4977 macb_writel(bp, WOL, 0);
4978 }
4979 /* Clear ISR on queue 0 */
4980 queue_readl(bp->queues, ISR);
4981 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4982 queue_writel(bp->queues, ISR, -1);
4983 /* Replace interrupt handler on queue 0 */
4984 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
4985 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
4986 IRQF_SHARED, netdev->name, bp->queues);
4987 if (err) {
4988 dev_err(dev,
4989 "Unable to request IRQ %d (error %d)\n",
4990 bp->queues[0].irq, err);
4991 spin_unlock_irqrestore(&bp->lock, flags);
4992 return err;
4993 }
4994 spin_unlock_irqrestore(&bp->lock, flags);
4995
Harini Katakamd54f89a2019-03-01 16:20:34 +05304996 disable_irq_wake(bp->queues[0].irq);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004997
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004998 /* Now make sure we disable phy before moving
4999 * to common restore path
5000 */
Antoine Tenart7897b072019-11-13 10:00:06 +01005001 rtnl_lock();
Nicolas Ferre558e35c2020-07-20 10:56:52 +02005002 phylink_stop(bp->phylink);
Antoine Tenart7897b072019-11-13 10:00:06 +01005003 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05305004 }
5005
Nicolas Ferre558e35c2020-07-20 10:56:52 +02005006 for (q = 0, queue = bp->queues; q < bp->num_queues;
5007 ++q, ++queue)
5008 napi_enable(&queue->napi);
5009
5010 if (netdev->hw_features & NETIF_F_NTUPLE)
5011 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
5012
5013 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
5014 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
5015
5016 macb_writel(bp, NCR, MACB_BIT(MPE));
Harini Katakamde991c52019-03-01 16:20:35 +05305017 macb_init_hw(bp);
5018 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00005019 macb_restore_features(bp);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02005020 rtnl_lock();
5021 phylink_start(bp->phylink);
5022 rtnl_unlock();
5023
Harini Katakamd54f89a2019-03-01 16:20:34 +05305024 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05305025 if (bp->ptp_info)
5026 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05305027
5028 return 0;
5029}
5030
5031static int __maybe_unused macb_runtime_suspend(struct device *dev)
5032{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005033 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05305034 struct macb *bp = netdev_priv(netdev);
5035
Claudiu Beznea38493da2020-12-09 15:03:34 +02005036 if (!(device_may_wakeup(dev)))
5037 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
5038 else
5039 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005040
5041 return 0;
5042}
5043
Harini Katakamd54f89a2019-03-01 16:20:34 +05305044static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005045{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005046 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005047 struct macb *bp = netdev_priv(netdev);
5048
Nicolas Ferre515a10a2020-07-10 14:46:41 +02005049 if (!(device_may_wakeup(dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005050 clk_prepare_enable(bp->pclk);
5051 clk_prepare_enable(bp->hclk);
5052 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05305053 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005054 }
Harini Katakamf5473d12019-03-01 16:20:33 +05305055 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005056
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005057 return 0;
5058}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005059
Harini Katakamd54f89a2019-03-01 16:20:34 +05305060static const struct dev_pm_ops macb_pm_ops = {
5061 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
5062 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
5063};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005064
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005065static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005066 .probe = macb_probe,
5067 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005068 .driver = {
5069 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01005070 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005071 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005072 },
5073};
5074
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005075module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005076
5077MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00005078MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02005079MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07005080MODULE_ALIAS("platform:macb");