blob: 98498a76ae162e1c15551098ade064fb64b8efce [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)) {
Jakub Kicinskic51e5062021-10-13 13:44:32 -0700316 eth_hw_addr_set(bp->dev, addr);
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000317 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
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100509static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
510 phy_interface_t interface, int speed,
511 int duplex)
512{
Russell King (Oracle)88767692022-01-05 13:15:15 +0000513 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100514 u32 config;
515
516 config = gem_readl(bp, USX_CONTROL);
517 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
518 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
519 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
520 config |= GEM_BIT(TX_EN);
521 gem_writel(bp, USX_CONTROL, config);
522}
523
524static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
Russell Kingd46b7e42019-11-21 00:36:22 +0000525 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100526{
Russell King (Oracle)88767692022-01-05 13:15:15 +0000527 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100528 u32 val;
529
530 state->speed = SPEED_10000;
531 state->duplex = 1;
532 state->an_complete = 1;
533
534 val = gem_readl(bp, USX_STATUS);
535 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
536 val = gem_readl(bp, NCFGR);
537 if (val & GEM_BIT(PAE))
538 state->pause = MLO_PAUSE_RX;
539}
540
541static int macb_usx_pcs_config(struct phylink_pcs *pcs,
542 unsigned int mode,
543 phy_interface_t interface,
544 const unsigned long *advertising,
545 bool permit_pause_to_mac)
546{
Russell King (Oracle)88767692022-01-05 13:15:15 +0000547 struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100548
549 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
550 GEM_BIT(SIGNAL_OK));
551
552 return 0;
553}
554
555static void macb_pcs_get_state(struct phylink_pcs *pcs,
556 struct phylink_link_state *state)
557{
Russell Kingd46b7e42019-11-21 00:36:22 +0000558 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100559}
560
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100561static void macb_pcs_an_restart(struct phylink_pcs *pcs)
Antoine Tenart7897b072019-11-13 10:00:06 +0100562{
563 /* Not supported */
564}
565
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100566static int macb_pcs_config(struct phylink_pcs *pcs,
567 unsigned int mode,
568 phy_interface_t interface,
569 const unsigned long *advertising,
570 bool permit_pause_to_mac)
571{
572 return 0;
573}
574
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100575static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
576 .pcs_get_state = macb_usx_pcs_get_state,
577 .pcs_config = macb_usx_pcs_config,
578 .pcs_link_up = macb_usx_pcs_link_up,
579};
580
581static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
582 .pcs_get_state = macb_pcs_get_state,
583 .pcs_an_restart = macb_pcs_an_restart,
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100584 .pcs_config = macb_pcs_config,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100585};
586
Antoine Tenart7897b072019-11-13 10:00:06 +0100587static void macb_mac_config(struct phylink_config *config, unsigned int mode,
588 const struct phylink_link_state *state)
589{
590 struct net_device *ndev = to_net_dev(config->dev);
591 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200592 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100593 u32 old_ctrl, ctrl;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100594 u32 old_ncr, ncr;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100595
frederic RODO6c36a702007-07-12 19:07:24 +0200596 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100597
Antoine Tenart7897b072019-11-13 10:00:06 +0100598 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100599 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100600
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100601 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
602 if (state->interface == PHY_INTERFACE_MODE_RMII)
603 ctrl |= MACB_BIT(RM9200_RMII);
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200604 } else if (macb_is_gem(bp)) {
Russell King633e98a2020-02-26 10:24:06 +0000605 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100606 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100607
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100608 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100609 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100610 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
611 ctrl |= GEM_BIT(PCSSEL);
612 ncr |= GEM_BIT(ENABLE_HS_MAC);
Claudiu Beznea1a9b5a22021-09-17 16:26:14 +0300613 } else if (bp->caps & MACB_CAPS_MIIONRGMII &&
614 bp->phy_interface == PHY_INTERFACE_MODE_MII) {
615 ncr |= MACB_BIT(MIIONRGMII);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100616 }
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100617 }
frederic RODO6c36a702007-07-12 19:07:24 +0200618
Antoine Tenart7897b072019-11-13 10:00:06 +0100619 /* Apply the new configuration, if any */
620 if (old_ctrl ^ ctrl)
621 macb_or_gem_writel(bp, NCFGR, ctrl);
622
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100623 if (old_ncr ^ ncr)
624 macb_or_gem_writel(bp, NCR, ncr);
625
Robert Hancocke276e5e42021-03-11 14:18:13 -0600626 /* Disable AN for SGMII fixed link configuration, enable otherwise.
627 * Must be written after PCSSEL is set in NCFGR,
628 * otherwise writes will not take effect.
629 */
630 if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) {
631 u32 pcsctrl, old_pcsctrl;
632
633 old_pcsctrl = gem_readl(bp, PCSCNTRL);
634 if (mode == MLO_AN_FIXED)
635 pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG);
636 else
637 pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG);
638 if (old_pcsctrl != pcsctrl)
639 gem_writel(bp, PCSCNTRL, pcsctrl);
640 }
641
frederic RODO6c36a702007-07-12 19:07:24 +0200642 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200643}
644
Antoine Tenart7897b072019-11-13 10:00:06 +0100645static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
646 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200647{
Antoine Tenart7897b072019-11-13 10:00:06 +0100648 struct net_device *ndev = to_net_dev(config->dev);
649 struct macb *bp = netdev_priv(ndev);
650 struct macb_queue *queue;
651 unsigned int q;
652 u32 ctrl;
653
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100654 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
655 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
656 queue_writel(queue, IDR,
657 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100658
659 /* Disable Rx and Tx */
660 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
661 macb_writel(bp, NCR, ctrl);
662
663 netif_tx_stop_all_queues(ndev);
664}
665
Russell King91a208f2020-02-26 10:23:41 +0000666static void macb_mac_link_up(struct phylink_config *config,
667 struct phy_device *phy,
668 unsigned int mode, phy_interface_t interface,
669 int speed, int duplex,
670 bool tx_pause, bool rx_pause)
Antoine Tenart7897b072019-11-13 10:00:06 +0100671{
672 struct net_device *ndev = to_net_dev(config->dev);
673 struct macb *bp = netdev_priv(ndev);
674 struct macb_queue *queue;
Russell King633e98a2020-02-26 10:24:06 +0000675 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100676 unsigned int q;
Russell King633e98a2020-02-26 10:24:06 +0000677 u32 ctrl;
678
679 spin_lock_irqsave(&bp->lock, flags);
680
681 ctrl = macb_or_gem_readl(bp, NCFGR);
682
683 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
684
685 if (speed == SPEED_100)
686 ctrl |= MACB_BIT(SPD);
687
688 if (duplex)
689 ctrl |= MACB_BIT(FD);
Antoine Tenart7897b072019-11-13 10:00:06 +0100690
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100691 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200692 ctrl &= ~MACB_BIT(PAE);
693 if (macb_is_gem(bp)) {
694 ctrl &= ~GEM_BIT(GBE);
Russell King633e98a2020-02-26 10:24:06 +0000695
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200696 if (speed == SPEED_1000)
697 ctrl |= GEM_BIT(GBE);
698 }
Russell King633e98a2020-02-26 10:24:06 +0000699
Parshuram Thombared7739b02020-09-05 10:21:33 +0200700 if (rx_pause)
Russell King633e98a2020-02-26 10:24:06 +0000701 ctrl |= MACB_BIT(PAE);
702
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200703 macb_set_tx_clk(bp, speed);
Antoine Tenart7897b072019-11-13 10:00:06 +0100704
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100705 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
706 * cleared the pipeline and control registers.
707 */
708 bp->macbgem_ops.mog_init_rings(bp);
709 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100710
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100711 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
712 queue_writel(queue, IER,
713 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
714 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100715
Russell King633e98a2020-02-26 10:24:06 +0000716 macb_or_gem_writel(bp, NCFGR, ctrl);
717
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100718 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
719 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
720 gem_readl(bp, HS_MAC_CONFIG)));
721
Russell King633e98a2020-02-26 10:24:06 +0000722 spin_unlock_irqrestore(&bp->lock, flags);
723
Antoine Tenart7897b072019-11-13 10:00:06 +0100724 /* Enable Rx and Tx */
725 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
726
727 netif_tx_wake_all_queues(ndev);
728}
729
Russell King (Oracle)88767692022-01-05 13:15:15 +0000730static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config,
731 phy_interface_t interface)
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100732{
733 struct net_device *ndev = to_net_dev(config->dev);
734 struct macb *bp = netdev_priv(ndev);
735
736 if (interface == PHY_INTERFACE_MODE_10GBASER)
Russell King (Oracle)88767692022-01-05 13:15:15 +0000737 return &bp->phylink_usx_pcs;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100738 else if (interface == PHY_INTERFACE_MODE_SGMII)
Russell King (Oracle)88767692022-01-05 13:15:15 +0000739 return &bp->phylink_sgmii_pcs;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100740 else
Russell King (Oracle)88767692022-01-05 13:15:15 +0000741 return NULL;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100742}
743
Antoine Tenart7897b072019-11-13 10:00:06 +0100744static const struct phylink_mac_ops macb_phylink_ops = {
Russell King (Oracle)cc0a75e2021-11-24 15:44:43 +0000745 .validate = phylink_generic_validate,
Russell King (Oracle)88767692022-01-05 13:15:15 +0000746 .mac_select_pcs = macb_mac_select_pcs,
Antoine Tenart7897b072019-11-13 10:00:06 +0100747 .mac_config = macb_mac_config,
748 .mac_link_down = macb_mac_link_down,
749 .mac_link_up = macb_mac_link_up,
750};
751
Milind Parabfd2a8912020-01-13 03:30:43 +0000752static bool macb_phy_handle_exists(struct device_node *dn)
753{
754 dn = of_parse_phandle(dn, "phy-handle", 0);
755 of_node_put(dn);
756 return dn != NULL;
757}
758
Antoine Tenart7897b072019-11-13 10:00:06 +0100759static int macb_phylink_connect(struct macb *bp)
760{
Milind Parabfd2a8912020-01-13 03:30:43 +0000761 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100762 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000763 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100764 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500765
Milind Parabfd2a8912020-01-13 03:30:43 +0000766 if (dn)
767 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
768
769 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200770 phydev = phy_find_first(bp->mii_bus);
771 if (!phydev) {
772 netdev_err(dev, "no PHY found\n");
773 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000774 }
frederic RODO6c36a702007-07-12 19:07:24 +0200775
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200776 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100777 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000778 }
779
780 if (ret) {
781 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
782 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200783 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100784
Antoine Tenart7897b072019-11-13 10:00:06 +0100785 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100786
Antoine Tenart7897b072019-11-13 10:00:06 +0100787 return 0;
788}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100789
Robert Hancock8fab1742021-03-11 14:18:12 -0600790static void macb_get_pcs_fixed_state(struct phylink_config *config,
791 struct phylink_link_state *state)
792{
793 struct net_device *ndev = to_net_dev(config->dev);
794 struct macb *bp = netdev_priv(ndev);
795
796 state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
797}
798
Antoine Tenart7897b072019-11-13 10:00:06 +0100799/* based on au1000_eth. c*/
800static int macb_mii_probe(struct net_device *dev)
801{
802 struct macb *bp = netdev_priv(dev);
803
Russell King (Oracle)88767692022-01-05 13:15:15 +0000804 bp->phylink_sgmii_pcs.ops = &macb_phylink_pcs_ops;
805 bp->phylink_usx_pcs.ops = &macb_phylink_usx_pcs_ops;
806
Antoine Tenart7897b072019-11-13 10:00:06 +0100807 bp->phylink_config.dev = &dev->dev;
808 bp->phylink_config.type = PHYLINK_NETDEV;
809
Robert Hancock8fab1742021-03-11 14:18:12 -0600810 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
811 bp->phylink_config.poll_fixed_state = true;
812 bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
813 }
814
Russell King (Oracle)cc0a75e2021-11-24 15:44:43 +0000815 bp->phylink_config.mac_capabilities = MAC_ASYM_PAUSE |
816 MAC_10 | MAC_100;
817
818 __set_bit(PHY_INTERFACE_MODE_MII,
819 bp->phylink_config.supported_interfaces);
820 __set_bit(PHY_INTERFACE_MODE_RMII,
821 bp->phylink_config.supported_interfaces);
822
823 /* Determine what modes are supported */
824 if (macb_is_gem(bp) && (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)) {
825 bp->phylink_config.mac_capabilities |= MAC_1000FD;
826 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
827 bp->phylink_config.mac_capabilities |= MAC_1000HD;
828
829 __set_bit(PHY_INTERFACE_MODE_GMII,
830 bp->phylink_config.supported_interfaces);
831 phy_interface_set_rgmii(bp->phylink_config.supported_interfaces);
832
833 if (bp->caps & MACB_CAPS_PCS)
834 __set_bit(PHY_INTERFACE_MODE_SGMII,
835 bp->phylink_config.supported_interfaces);
836
837 if (bp->caps & MACB_CAPS_HIGH_SPEED) {
838 __set_bit(PHY_INTERFACE_MODE_10GBASER,
839 bp->phylink_config.supported_interfaces);
840 bp->phylink_config.mac_capabilities |= MAC_10000FD;
841 }
842 }
843
Antoine Tenart7897b072019-11-13 10:00:06 +0100844 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
845 bp->phy_interface, &macb_phylink_ops);
846 if (IS_ERR(bp->phylink)) {
847 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
848 PTR_ERR(bp->phylink));
849 return PTR_ERR(bp->phylink);
850 }
frederic RODO6c36a702007-07-12 19:07:24 +0200851
852 return 0;
853}
854
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100855static int macb_mdiobus_register(struct macb *bp)
856{
857 struct device_node *child, *np = bp->pdev->dev.of_node;
858
Sean Anderson4d98bb02021-10-22 12:35:48 -0400859 /* If we have a child named mdio, probe it instead of looking for PHYs
860 * directly under the MAC node
861 */
862 child = of_get_child_by_name(np, "mdio");
Guenter Roeck8db3cbc2021-10-26 10:39:50 -0700863 if (child) {
Sean Anderson4d98bb02021-10-22 12:35:48 -0400864 int ret = of_mdiobus_register(bp->mii_bus, child);
865
866 of_node_put(child);
867 return ret;
868 }
869
Codrin Ciubotariu79540d12020-03-31 12:39:35 +0300870 if (of_phy_is_fixed_link(np))
871 return mdiobus_register(bp->mii_bus);
872
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100873 /* Only create the PHY from the device tree if at least one PHY is
874 * described. Otherwise scan the entire MDIO bus. We do this to support
875 * old device tree that did not follow the best practices and did not
876 * describe their network PHYs.
877 */
878 for_each_available_child_of_node(np, child)
879 if (of_mdiobus_child_is_phy(child)) {
880 /* The loop increments the child refcount,
881 * decrement it before returning.
882 */
883 of_node_put(child);
884
885 return of_mdiobus_register(bp->mii_bus, np);
886 }
887
888 return mdiobus_register(bp->mii_bus);
889}
890
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100891static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200892{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200893 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200894
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200895 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200896 macb_writel(bp, NCR, MACB_BIT(MPE));
897
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700898 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700899 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200900 err = -ENOMEM;
901 goto err_out;
902 }
903
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700904 bp->mii_bus->name = "MACB_mii_bus";
905 bp->mii_bus->read = &macb_mdio_read;
906 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000907 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700908 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700909 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700910 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700911
Jamie Iles91523942011-02-28 04:05:25 +0000912 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200913
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100914 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200915 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100916 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200917
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200918 err = macb_mii_probe(bp->dev);
919 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200920 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200921
922 return 0;
923
924err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700925 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500926err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700927 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200928err_out:
929 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100930}
931
932static void macb_update_stats(struct macb *bp)
933{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000934 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
935 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300936 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100937
938 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
939
Moritz Fischer96ec6312016-03-29 19:11:11 -0700940 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700941 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100942}
943
Nicolas Ferree86cd532012-10-31 06:04:57 +0000944static int macb_halt_tx(struct macb *bp)
945{
946 unsigned long halt_time, timeout;
947 u32 status;
948
949 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
950
951 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
952 do {
953 halt_time = jiffies;
954 status = macb_readl(bp, TSR);
955 if (!(status & MACB_BIT(TGO)))
956 return 0;
957
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800958 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000959 } while (time_before(halt_time, timeout));
960
961 return -ETIMEDOUT;
962}
963
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200964static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
965{
966 if (tx_skb->mapping) {
967 if (tx_skb->mapped_as_page)
968 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
969 tx_skb->size, DMA_TO_DEVICE);
970 else
971 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
972 tx_skb->size, DMA_TO_DEVICE);
973 tx_skb->mapping = 0;
974 }
975
976 if (tx_skb->skb) {
977 dev_kfree_skb_any(tx_skb->skb);
978 tx_skb->skb = NULL;
979 }
980}
981
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000982static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530983{
Harini Katakamfff80192016-08-09 13:15:53 +0530984#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000985 struct macb_dma_desc_64 *desc_64;
986
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100987 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000988 desc_64 = macb_64b_desc(bp, desc);
989 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200990 /* The low bits of RX address contain the RX_USED bit, clearing
991 * of which allows packet RX. Make sure the high bits are also
992 * visible to HW at that point.
993 */
994 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000995 }
Harini Katakamfff80192016-08-09 13:15:53 +0530996#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000997 desc->addr = lower_32_bits(addr);
998}
999
1000static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1001{
1002 dma_addr_t addr = 0;
1003#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1004 struct macb_dma_desc_64 *desc_64;
1005
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001006 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001007 desc_64 = macb_64b_desc(bp, desc);
1008 addr = ((u64)(desc_64->addrh) << 32);
1009 }
1010#endif
1011 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1012 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +05301013}
1014
Nicolas Ferree86cd532012-10-31 06:04:57 +00001015static void macb_tx_error_task(struct work_struct *work)
1016{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001017 struct macb_queue *queue = container_of(work, struct macb_queue,
1018 tx_error_task);
1019 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001020 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001021 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001022 struct sk_buff *skb;
1023 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001024 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001025
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001026 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
1027 (unsigned int)(queue - bp->queues),
1028 queue->tx_tail, queue->tx_head);
1029
1030 /* Prevent the queue IRQ handlers from running: each of them may call
1031 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
1032 * As explained below, we have to halt the transmission before updating
1033 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1034 * network engine about the macb/gem being halted.
1035 */
1036 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001037
1038 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001039 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001040
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001041 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +00001042 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001043 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +00001044 */
1045 if (macb_halt_tx(bp))
1046 /* Just complain for now, reinitializing TX path can be good */
1047 netdev_err(bp->dev, "BUG: halt tx timed out\n");
1048
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001049 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001050 * Free transmit buffers in upper layer.
1051 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001052 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1053 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001054
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001055 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001056 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001057 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001058 skb = tx_skb->skb;
1059
1060 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001061 /* skb is set for the last buffer of the frame */
1062 while (!skb) {
1063 macb_tx_unmap(bp, tx_skb);
1064 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001065 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001066 skb = tx_skb->skb;
1067 }
1068
1069 /* ctrl still refers to the first buffer descriptor
1070 * since it's the only one written back by the hardware
1071 */
1072 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1073 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001074 macb_tx_ring_wrap(bp, tail),
1075 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001076 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001077 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001078 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001079 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001080 }
Nicolas Ferree86cd532012-10-31 06:04:57 +00001081 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001082 /* "Buffers exhausted mid-frame" errors may only happen
1083 * if the driver is buggy, so complain loudly about
1084 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001085 */
1086 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1087 netdev_err(bp->dev,
1088 "BUG: TX buffers exhausted mid-frame\n");
1089
1090 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1091 }
1092
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001093 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001094 }
1095
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001096 /* Set end of TX queue */
1097 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001098 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001099 desc->ctrl = MACB_BIT(TX_USED);
1100
Nicolas Ferree86cd532012-10-31 06:04:57 +00001101 /* Make descriptor updates visible to hardware */
1102 wmb();
1103
1104 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001105 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301106#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001107 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001108 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301109#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +00001110 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001111 queue->tx_head = 0;
1112 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001113
1114 /* Housework before enabling TX IRQ */
1115 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001116 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1117
1118 /* Now we are ready to start transmission again */
1119 netif_tx_start_all_queues(bp->dev);
1120 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1121
1122 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001123}
1124
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001125static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001126{
1127 unsigned int tail;
1128 unsigned int head;
1129 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001130 struct macb *bp = queue->bp;
1131 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001132
1133 status = macb_readl(bp, TSR);
1134 macb_writel(bp, TSR, status);
1135
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001136 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001137 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +00001138
Nicolas Ferree86cd532012-10-31 06:04:57 +00001139 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001140 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001141
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001142 head = queue->tx_head;
1143 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001144 struct macb_tx_skb *tx_skb;
1145 struct sk_buff *skb;
1146 struct macb_dma_desc *desc;
1147 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001148
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001149 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001150
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001151 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001152 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001153
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001154 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001155
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001156 /* TX_USED bit is only set by hardware on the very first buffer
1157 * descriptor of the transmitted frame.
1158 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001159 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001160 break;
1161
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001162 /* Process all buffers of the current transmitted frame */
1163 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001164 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001165 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001166
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001167 /* First, update TX stats if needed */
1168 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001169 if (unlikely(skb_shinfo(skb)->tx_flags &
1170 SKBTX_HW_TSTAMP) &&
1171 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001172 /* skb now belongs to timestamp buffer
1173 * and will be removed later
1174 */
1175 tx_skb->skb = NULL;
1176 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001177 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001178 macb_tx_ring_wrap(bp, tail),
1179 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001180 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001181 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001182 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001183 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001184 }
1185
1186 /* Now we can safely release resources */
1187 macb_tx_unmap(bp, tx_skb);
1188
1189 /* skb is set only for the last buffer of the frame.
1190 * WARNING: at this point skb has been freed by
1191 * macb_tx_unmap().
1192 */
1193 if (skb)
1194 break;
1195 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 }
1197
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001198 queue->tx_tail = tail;
1199 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1200 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001201 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001202 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203}
1204
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001205static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001206{
1207 unsigned int entry;
1208 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001209 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001210 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001211 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001212
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001213 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1214 bp->rx_ring_size) > 0) {
1215 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001216
1217 /* Make hw descriptor updates visible to CPU */
1218 rmb();
1219
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001220 queue->rx_prepared_head++;
1221 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001222
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001223 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001224 /* allocate sk_buff for this free entry in ring */
1225 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001226 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001227 netdev_err(bp->dev,
1228 "Unable to allocate sk_buff\n");
1229 break;
1230 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001231
1232 /* now fill corresponding descriptor entry */
1233 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001234 bp->rx_buffer_size,
1235 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001236 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1237 dev_kfree_skb(skb);
1238 break;
1239 }
1240
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001241 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001242
Zach Brownb410d132016-10-19 09:56:57 -05001243 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001244 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001245 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001246 /* Setting addr clears RX_USED and allows reception,
1247 * make sure ctrl is cleared first to avoid a race.
1248 */
1249 dma_wmb();
1250 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001251
1252 /* properly align Ethernet header */
1253 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301254 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001255 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001256 dma_wmb();
1257 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001258 }
1259 }
1260
1261 /* Make descriptor updates visible to hardware */
1262 wmb();
1263
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001264 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1265 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001266}
1267
1268/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001269static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001270 unsigned int end)
1271{
1272 unsigned int frag;
1273
1274 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001275 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001276
Nicolas Ferre4df95132013-06-04 21:57:12 +00001277 desc->addr &= ~MACB_BIT(RX_USED);
1278 }
1279
1280 /* Make descriptor updates visible to hardware */
1281 wmb();
1282
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001283 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001284 * whatever caused this is updated, so we don't have to record
1285 * anything.
1286 */
1287}
1288
Antoine Tenart97236cd2019-06-21 17:30:02 +02001289static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1290 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001291{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001292 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001293 unsigned int len;
1294 unsigned int entry;
1295 struct sk_buff *skb;
1296 struct macb_dma_desc *desc;
1297 int count = 0;
1298
1299 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301300 u32 ctrl;
1301 dma_addr_t addr;
1302 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001303
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001304 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1305 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001306
1307 /* Make hw descriptor updates visible to CPU */
1308 rmb();
1309
Harini Katakamfff80192016-08-09 13:15:53 +05301310 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001311 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001312
Harini Katakamfff80192016-08-09 13:15:53 +05301313 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001314 break;
1315
Anssi Hannula6e0af292018-12-17 15:05:41 +02001316 /* Ensure ctrl is at least as up-to-date as rxused */
1317 dma_rmb();
1318
1319 ctrl = desc->ctrl;
1320
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001321 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001322 count++;
1323
1324 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1325 netdev_err(bp->dev,
1326 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001327 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001328 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001329 break;
1330 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001331 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001332 if (unlikely(!skb)) {
1333 netdev_err(bp->dev,
1334 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001335 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001336 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001337 break;
1338 }
1339 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001340 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301341 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001342
1343 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1344
1345 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001346 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001347 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001348
1349 skb->protocol = eth_type_trans(skb, bp->dev);
1350 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001351 if (bp->dev->features & NETIF_F_RXCSUM &&
1352 !(bp->dev->flags & IFF_PROMISC) &&
1353 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1354 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001355
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001356 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001357 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001358 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001359 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001360
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001361 gem_ptp_do_rxstamp(bp, skb, desc);
1362
Nicolas Ferre4df95132013-06-04 21:57:12 +00001363#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1364 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1365 skb->len, skb->csum);
1366 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001367 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001368 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1369 skb->data, 32, true);
1370#endif
1371
Antoine Tenart97236cd2019-06-21 17:30:02 +02001372 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001373 }
1374
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001375 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001376
1377 return count;
1378}
1379
Antoine Tenart97236cd2019-06-21 17:30:02 +02001380static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1381 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001382{
1383 unsigned int len;
1384 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001385 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001386 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001387 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001388 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001389
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001390 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301391 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001392
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001393 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001394 macb_rx_ring_wrap(bp, first_frag),
1395 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001396
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001397 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001398 * first buffer. Since the header is 14 bytes, this makes the
1399 * payload word-aligned.
1400 *
1401 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1402 * the two padding bytes into the skb so that we avoid hitting
1403 * the slowpath in memcpy(), and pull them off afterwards.
1404 */
1405 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001406 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001407 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001408 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001409 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001410 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001411 if (frag == last_frag)
1412 break;
1413 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001414
1415 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001416 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001417
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001418 return 1;
1419 }
1420
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001421 offset = 0;
1422 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001423 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001424 skb_put(skb, len);
1425
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001426 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001427 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001428
1429 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001430 if (unlikely(frag != last_frag)) {
1431 dev_kfree_skb_any(skb);
1432 return -1;
1433 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001434 frag_len = len - offset;
1435 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001436 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001437 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001438 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001439 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001440 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001441 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001442
1443 if (frag == last_frag)
1444 break;
1445 }
1446
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001447 /* Make descriptor updates visible to hardware */
1448 wmb();
1449
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001450 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001451 skb->protocol = eth_type_trans(skb, bp->dev);
1452
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001453 bp->dev->stats.rx_packets++;
1454 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001455 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001456 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001457 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001458
1459 return 0;
1460}
1461
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001462static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001463{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001464 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001465 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001466 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001467 int i;
1468
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001469 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001470 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001471 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001472 macb_set_addr(bp, desc, addr);
1473 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001474 addr += bp->rx_buffer_size;
1475 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001476 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001477 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001478}
1479
Antoine Tenart97236cd2019-06-21 17:30:02 +02001480static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1481 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001482{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001483 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001484 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001485 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001486 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001487 int first_frag = -1;
1488
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001489 for (tail = queue->rx_tail; budget > 0; tail++) {
1490 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001491 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001492
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001493 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001494 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001495
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001496 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001497 break;
1498
Anssi Hannula6e0af292018-12-17 15:05:41 +02001499 /* Ensure ctrl is at least as up-to-date as addr */
1500 dma_rmb();
1501
1502 ctrl = desc->ctrl;
1503
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001504 if (ctrl & MACB_BIT(RX_SOF)) {
1505 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001506 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001507 first_frag = tail;
1508 }
1509
1510 if (ctrl & MACB_BIT(RX_EOF)) {
1511 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001512
1513 if (unlikely(first_frag == -1)) {
1514 reset_rx_queue = true;
1515 continue;
1516 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001517
Antoine Tenart97236cd2019-06-21 17:30:02 +02001518 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001520 if (unlikely(dropped < 0)) {
1521 reset_rx_queue = true;
1522 continue;
1523 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001524 if (!dropped) {
1525 received++;
1526 budget--;
1527 }
1528 }
1529 }
1530
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001531 if (unlikely(reset_rx_queue)) {
1532 unsigned long flags;
1533 u32 ctrl;
1534
1535 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1536
1537 spin_lock_irqsave(&bp->lock, flags);
1538
1539 ctrl = macb_readl(bp, NCR);
1540 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1541
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001542 macb_init_rx_ring(queue);
1543 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001544
1545 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1546
1547 spin_unlock_irqrestore(&bp->lock, flags);
1548 return received;
1549 }
1550
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001551 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001552 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001553 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001554 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001555
1556 return received;
1557}
1558
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001559static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001560{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001561 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1562 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001563 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001564 u32 status;
1565
1566 status = macb_readl(bp, RSR);
1567 macb_writel(bp, RSR, status);
1568
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001569 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001570 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001571
Antoine Tenart97236cd2019-06-21 17:30:02 +02001572 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001573 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001574 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001575
Nicolas Ferre8770e912013-02-12 11:08:48 +01001576 /* Packets received while interrupts were disabled */
1577 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001578 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001579 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001580 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001581 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001582 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301583 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001584 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001585 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001586
1587 /* TODO: Handle errors */
1588
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001589 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001590}
1591
Allen Paise7412b82020-09-14 12:59:23 +05301592static void macb_hresp_error_task(struct tasklet_struct *t)
Harini Katakam032dc412018-01-27 12:09:01 +05301593{
Allen Paise7412b82020-09-14 12:59:23 +05301594 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
Harini Katakam032dc412018-01-27 12:09:01 +05301595 struct net_device *dev = bp->dev;
Claudiu Beznea580d3952020-07-02 12:06:00 +03001596 struct macb_queue *queue;
Harini Katakam032dc412018-01-27 12:09:01 +05301597 unsigned int q;
1598 u32 ctrl;
1599
1600 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301601 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301602 MACB_TX_INT_FLAGS |
1603 MACB_BIT(HRESP));
1604 }
1605 ctrl = macb_readl(bp, NCR);
1606 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1607 macb_writel(bp, NCR, ctrl);
1608
1609 netif_tx_stop_all_queues(dev);
1610 netif_carrier_off(dev);
1611
1612 bp->macbgem_ops.mog_init_rings(bp);
1613
1614 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001615 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301616
Antoine Tenart6e952d92019-11-13 10:00:05 +01001617 /* Enable interrupts */
1618 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301619 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301620 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301621 MACB_TX_INT_FLAGS |
1622 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301623
1624 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1625 macb_writel(bp, NCR, ctrl);
1626
1627 netif_carrier_on(dev);
1628 netif_tx_start_all_queues(dev);
1629}
1630
Claudiu Beznea42983882018-12-17 10:02:42 +00001631static void macb_tx_restart(struct macb_queue *queue)
1632{
1633 unsigned int head = queue->tx_head;
1634 unsigned int tail = queue->tx_tail;
1635 struct macb *bp = queue->bp;
1636
1637 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1638 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1639
1640 if (head == tail)
1641 return;
1642
1643 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1644}
1645
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02001646static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
1647{
1648 struct macb_queue *queue = dev_id;
1649 struct macb *bp = queue->bp;
1650 u32 status;
1651
1652 status = queue_readl(queue, ISR);
1653
1654 if (unlikely(!status))
1655 return IRQ_NONE;
1656
1657 spin_lock(&bp->lock);
1658
1659 if (status & MACB_BIT(WOL)) {
1660 queue_writel(queue, IDR, MACB_BIT(WOL));
1661 macb_writel(bp, WOL, 0);
1662 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
1663 (unsigned int)(queue - bp->queues),
1664 (unsigned long)status);
1665 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1666 queue_writel(queue, ISR, MACB_BIT(WOL));
1667 pm_wakeup_event(&bp->pdev->dev, 0);
1668 }
1669
1670 spin_unlock(&bp->lock);
1671
1672 return IRQ_HANDLED;
1673}
1674
Nicolas Ferre558e35c2020-07-20 10:56:52 +02001675static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
1676{
1677 struct macb_queue *queue = dev_id;
1678 struct macb *bp = queue->bp;
1679 u32 status;
1680
1681 status = queue_readl(queue, ISR);
1682
1683 if (unlikely(!status))
1684 return IRQ_NONE;
1685
1686 spin_lock(&bp->lock);
1687
1688 if (status & GEM_BIT(WOL)) {
1689 queue_writel(queue, IDR, GEM_BIT(WOL));
1690 gem_writel(bp, WOL, 0);
1691 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
1692 (unsigned int)(queue - bp->queues),
1693 (unsigned long)status);
1694 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1695 queue_writel(queue, ISR, GEM_BIT(WOL));
1696 pm_wakeup_event(&bp->pdev->dev, 0);
1697 }
1698
1699 spin_unlock(&bp->lock);
1700
1701 return IRQ_HANDLED;
1702}
1703
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001704static irqreturn_t macb_interrupt(int irq, void *dev_id)
1705{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001706 struct macb_queue *queue = dev_id;
1707 struct macb *bp = queue->bp;
1708 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001709 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001710
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001711 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001712
1713 if (unlikely(!status))
1714 return IRQ_NONE;
1715
1716 spin_lock(&bp->lock);
1717
1718 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001719 /* close possible race with dev_close */
1720 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001721 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001722 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1723 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001724 break;
1725 }
1726
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001727 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1728 (unsigned int)(queue - bp->queues),
1729 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001730
Harini Katakame5010702019-01-29 15:20:03 +05301731 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001732 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001733 * until we have processed the buffers. The
1734 * scheduling call may fail if the poll routine
1735 * is already scheduled, so disable interrupts
1736 * now.
1737 */
Harini Katakame5010702019-01-29 15:20:03 +05301738 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001739 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001740 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001741
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001742 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001743 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001744 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745 }
1746 }
1747
Nicolas Ferree86cd532012-10-31 06:04:57 +00001748 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001749 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1750 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001751
1752 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001753 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001754
Nicolas Ferree86cd532012-10-31 06:04:57 +00001755 break;
1756 }
1757
1758 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001759 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001760
Claudiu Beznea42983882018-12-17 10:02:42 +00001761 if (status & MACB_BIT(TXUBR))
1762 macb_tx_restart(queue);
1763
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001764 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001765 * add that if/when we get our hands on a full-blown MII PHY.
1766 */
1767
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001768 /* There is a hardware issue under heavy load where DMA can
1769 * stop, this causes endless "used buffer descriptor read"
1770 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301771 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1772 * section 16.7.4 for details. RXUBR is only enabled for
1773 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001774 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001775 if (status & MACB_BIT(RXUBR)) {
1776 ctrl = macb_readl(bp, NCR);
1777 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001778 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001779 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1780
1781 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001782 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001783 }
1784
Alexander Steinb19f7f72011-04-13 05:03:24 +00001785 if (status & MACB_BIT(ISR_ROVR)) {
1786 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001787 if (macb_is_gem(bp))
1788 bp->hw_stats.gem.rx_overruns++;
1789 else
1790 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001791
1792 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001793 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001794 }
1795
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001796 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301797 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001798 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001799
1800 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001801 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001802 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001803 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001804 }
1805
1806 spin_unlock(&bp->lock);
1807
1808 return IRQ_HANDLED;
1809}
1810
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001811#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001812/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001813 * to allow network i/o with interrupts disabled.
1814 */
1815static void macb_poll_controller(struct net_device *dev)
1816{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001817 struct macb *bp = netdev_priv(dev);
1818 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001819 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001820 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001821
1822 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001823 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1824 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001825 local_irq_restore(flags);
1826}
1827#endif
1828
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001829static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001830 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001831 struct sk_buff *skb,
1832 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001833{
1834 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001835 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001836 struct macb_tx_skb *tx_skb = NULL;
1837 struct macb_dma_desc *desc;
1838 unsigned int offset, size, count = 0;
1839 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001840 unsigned int eof = 1, mss_mfs = 0;
1841 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1842
1843 /* LSO */
1844 if (skb_shinfo(skb)->gso_size != 0) {
1845 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1846 /* UDP - UFO */
1847 lso_ctrl = MACB_LSO_UFO_ENABLE;
1848 else
1849 /* TCP - TSO */
1850 lso_ctrl = MACB_LSO_TSO_ENABLE;
1851 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001852
1853 /* First, map non-paged data */
1854 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001855
1856 /* first buffer length */
1857 size = hdrlen;
1858
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001859 offset = 0;
1860 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001861 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001862 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001863
1864 mapping = dma_map_single(&bp->pdev->dev,
1865 skb->data + offset,
1866 size, DMA_TO_DEVICE);
1867 if (dma_mapping_error(&bp->pdev->dev, mapping))
1868 goto dma_error;
1869
1870 /* Save info to properly release resources */
1871 tx_skb->skb = NULL;
1872 tx_skb->mapping = mapping;
1873 tx_skb->size = size;
1874 tx_skb->mapped_as_page = false;
1875
1876 len -= size;
1877 offset += size;
1878 count++;
1879 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001880
1881 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001882 }
1883
1884 /* Then, map paged data from fragments */
1885 for (f = 0; f < nr_frags; f++) {
1886 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1887
1888 len = skb_frag_size(frag);
1889 offset = 0;
1890 while (len) {
1891 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001892 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001893 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001894
1895 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1896 offset, size, DMA_TO_DEVICE);
1897 if (dma_mapping_error(&bp->pdev->dev, mapping))
1898 goto dma_error;
1899
1900 /* Save info to properly release resources */
1901 tx_skb->skb = NULL;
1902 tx_skb->mapping = mapping;
1903 tx_skb->size = size;
1904 tx_skb->mapped_as_page = true;
1905
1906 len -= size;
1907 offset += size;
1908 count++;
1909 tx_head++;
1910 }
1911 }
1912
1913 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001914 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001915 netdev_err(bp->dev, "BUG! empty skb!\n");
1916 return 0;
1917 }
1918
1919 /* This is the last buffer of the frame: save socket buffer */
1920 tx_skb->skb = skb;
1921
1922 /* Update TX ring: update buffer descriptors in reverse order
1923 * to avoid race condition
1924 */
1925
1926 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1927 * to set the end of TX queue
1928 */
1929 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001930 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001931 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001932 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001933 desc->ctrl = ctrl;
1934
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001935 if (lso_ctrl) {
1936 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1937 /* include header and FCS in value given to h/w */
1938 mss_mfs = skb_shinfo(skb)->gso_size +
1939 skb_transport_offset(skb) +
1940 ETH_FCS_LEN;
1941 else /* TSO */ {
1942 mss_mfs = skb_shinfo(skb)->gso_size;
1943 /* TCP Sequence Number Source Select
1944 * can be set only for TSO
1945 */
1946 seq_ctrl = 0;
1947 }
1948 }
1949
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001950 do {
1951 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001952 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001953 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001954 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001955
1956 ctrl = (u32)tx_skb->size;
1957 if (eof) {
1958 ctrl |= MACB_BIT(TX_LAST);
1959 eof = 0;
1960 }
Zach Brownb410d132016-10-19 09:56:57 -05001961 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001962 ctrl |= MACB_BIT(TX_WRAP);
1963
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001964 /* First descriptor is header descriptor */
1965 if (i == queue->tx_head) {
1966 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1967 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001968 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1969 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1970 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001971 } else
1972 /* Only set MSS/MFS on payload descriptors
1973 * (second or later descriptor)
1974 */
1975 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1976
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001977 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001978 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001979 /* desc->addr must be visible to hardware before clearing
1980 * 'TX_USED' bit in desc->ctrl.
1981 */
1982 wmb();
1983 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001984 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001985
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001986 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001987
1988 return count;
1989
1990dma_error:
1991 netdev_err(bp->dev, "TX DMA map failed\n");
1992
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001993 for (i = queue->tx_head; i != tx_head; i++) {
1994 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001995
1996 macb_tx_unmap(bp, tx_skb);
1997 }
1998
1999 return 0;
2000}
2001
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002002static netdev_features_t macb_features_check(struct sk_buff *skb,
2003 struct net_device *dev,
2004 netdev_features_t features)
2005{
2006 unsigned int nr_frags, f;
2007 unsigned int hdrlen;
2008
2009 /* Validate LSO compatibility */
2010
Harini Katakam41c1ef92020-02-05 18:08:11 +05302011 /* there is only one buffer or protocol is not UDP */
2012 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002013 return features;
2014
2015 /* length of header */
2016 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002017
Harini Katakam41c1ef92020-02-05 18:08:11 +05302018 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002019 * When software supplies two or more payload buffers all payload buffers
2020 * apart from the last must be a multiple of 8 bytes in size.
2021 */
2022 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2023 return features & ~MACB_NETIF_LSO;
2024
2025 nr_frags = skb_shinfo(skb)->nr_frags;
2026 /* No need to check last fragment */
2027 nr_frags--;
2028 for (f = 0; f < nr_frags; f++) {
2029 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2030
2031 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2032 return features & ~MACB_NETIF_LSO;
2033 }
2034 return features;
2035}
2036
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02002037static inline int macb_clear_csum(struct sk_buff *skb)
2038{
2039 /* no change for packets without checksum offloading */
2040 if (skb->ip_summed != CHECKSUM_PARTIAL)
2041 return 0;
2042
2043 /* make sure we can modify the header */
2044 if (unlikely(skb_cow_head(skb, 0)))
2045 return -1;
2046
2047 /* initialize checksum field
2048 * This is required - at least for Zynq, which otherwise calculates
2049 * wrong UDP header checksums for UDP packets with UDP data len <=2
2050 */
2051 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2052 return 0;
2053}
2054
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002055static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
2056{
Mark Deneen403dc162020-10-30 15:58:14 +00002057 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2058 skb_is_nonlinear(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002059 int padlen = ETH_ZLEN - (*skb)->len;
2060 int headroom = skb_headroom(*skb);
2061 int tailroom = skb_tailroom(*skb);
2062 struct sk_buff *nskb;
2063 u32 fcs;
2064
2065 if (!(ndev->features & NETIF_F_HW_CSUM) ||
2066 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2067 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
2068 return 0;
2069
2070 if (padlen <= 0) {
2071 /* FCS could be appeded to tailroom. */
2072 if (tailroom >= ETH_FCS_LEN)
2073 goto add_fcs;
2074 /* FCS could be appeded by moving data to headroom. */
2075 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
2076 padlen = 0;
2077 /* No room for FCS, need to reallocate skb. */
2078 else
Tristram Ha899ecae2018-10-24 14:51:23 -07002079 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002080 } else {
2081 /* Add room for FCS. */
2082 padlen += ETH_FCS_LEN;
2083 }
2084
2085 if (!cloned && headroom + tailroom >= padlen) {
2086 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
2087 skb_set_tail_pointer(*skb, (*skb)->len);
2088 } else {
2089 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2090 if (!nskb)
2091 return -ENOMEM;
2092
Huang Zijiangf3e5c072019-02-14 14:41:18 +08002093 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002094 *skb = nskb;
2095 }
2096
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00002097 if (padlen > ETH_FCS_LEN)
2098 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002099
2100add_fcs:
2101 /* set FCS to packet */
2102 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2103 fcs = ~fcs;
2104
2105 skb_put_u8(*skb, fcs & 0xff);
2106 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2107 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2108 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2109
2110 return 0;
2111}
2112
Claudiu Beznead1c38952018-08-07 12:25:12 +03002113static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002114{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002115 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002116 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002117 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07002118 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002119 unsigned int desc_cnt, nr_frags, frag_size, f;
2120 unsigned int hdrlen;
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002121 bool is_lso;
Claudiu Beznead1c38952018-08-07 12:25:12 +03002122 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002123
Claudiu Beznea33729f22018-08-07 12:25:13 +03002124 if (macb_clear_csum(skb)) {
2125 dev_kfree_skb_any(skb);
2126 return ret;
2127 }
2128
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002129 if (macb_pad_and_fcs(&skb, dev)) {
2130 dev_kfree_skb_any(skb);
2131 return ret;
2132 }
2133
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002134 is_lso = (skb_shinfo(skb)->gso_size != 0);
2135
2136 if (is_lso) {
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002137 /* length of headers */
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002138 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002139 /* only queue eth + ip headers separately for UDP */
2140 hdrlen = skb_transport_offset(skb);
2141 else
2142 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
2143 if (skb_headlen(skb) < hdrlen) {
2144 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
2145 /* if this is required, would need to copy to single buffer */
2146 return NETDEV_TX_BUSY;
2147 }
2148 } else
2149 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002150
Havard Skinnemoena268adb2012-10-31 06:04:52 +00002151#if defined(DEBUG) && defined(VERBOSE_DEBUG)
2152 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002153 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
2154 queue_index, skb->len, skb->head, skb->data,
2155 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002156 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2157 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002158#endif
2159
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002160 /* Count how many TX buffer descriptors are needed to send this
2161 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07002162 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002163 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002164 if (is_lso && (skb_headlen(skb) > hdrlen))
2165 /* extra header descriptor if also payload in first buffer */
2166 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2167 else
2168 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002169 nr_frags = skb_shinfo(skb)->nr_frags;
2170 for (f = 0; f < nr_frags; f++) {
2171 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002172 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002173 }
2174
Dongdong Deng48719532009-08-23 19:49:07 -07002175 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002176
2177 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05002178 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002179 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002180 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07002181 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002182 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002183 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00002184 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002185 }
2186
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002187 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002188 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07002189 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08002190 goto unlock;
2191 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00002192
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00002193 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002194 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00002195 skb_tx_timestamp(skb);
2196
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002197 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2198
Zach Brownb410d132016-10-19 09:56:57 -05002199 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002200 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002201
Soren Brinkmann92030902014-03-04 08:46:39 -08002202unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002203 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002204
Claudiu Beznead1c38952018-08-07 12:25:12 +03002205 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002206}
2207
Nicolas Ferre4df95132013-06-04 21:57:12 +00002208static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002209{
2210 if (!macb_is_gem(bp)) {
2211 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2212 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002213 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002214
Nicolas Ferre1b447912013-06-04 21:57:11 +00002215 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002216 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002217 "RX buffer must be multiple of %d bytes, expanding\n",
2218 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002219 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002220 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002221 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002222 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002223
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002224 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002225 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002226}
2227
Nicolas Ferre4df95132013-06-04 21:57:12 +00002228static void gem_free_rx_buffers(struct macb *bp)
2229{
2230 struct sk_buff *skb;
2231 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002232 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002233 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002234 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002235 int i;
2236
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002237 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2238 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002239 continue;
2240
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002241 for (i = 0; i < bp->rx_ring_size; i++) {
2242 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002244 if (!skb)
2245 continue;
2246
2247 desc = macb_rx_desc(queue, i);
2248 addr = macb_get_addr(bp, desc);
2249
2250 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2251 DMA_FROM_DEVICE);
2252 dev_kfree_skb_any(skb);
2253 skb = NULL;
2254 }
2255
2256 kfree(queue->rx_skbuff);
2257 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002258 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002259}
2260
2261static void macb_free_rx_buffers(struct macb *bp)
2262{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002263 struct macb_queue *queue = &bp->queues[0];
2264
2265 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002266 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002267 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002268 queue->rx_buffers, queue->rx_buffers_dma);
2269 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002270 }
2271}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002272
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002273static void macb_free_consistent(struct macb *bp)
2274{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002275 struct macb_queue *queue;
2276 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302277 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002278
Nicolas Ferre4df95132013-06-04 21:57:12 +00002279 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002280
2281 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2282 kfree(queue->tx_skb);
2283 queue->tx_skb = NULL;
2284 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302285 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2286 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002287 queue->tx_ring, queue->tx_ring_dma);
2288 queue->tx_ring = NULL;
2289 }
Harini Katakame50b7702018-07-06 12:18:57 +05302290 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302291 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2292 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302293 queue->rx_ring, queue->rx_ring_dma);
2294 queue->rx_ring = NULL;
2295 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002296 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002297}
2298
2299static int gem_alloc_rx_buffers(struct macb *bp)
2300{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002301 struct macb_queue *queue;
2302 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002303 int size;
2304
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002305 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2306 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2307 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2308 if (!queue->rx_skbuff)
2309 return -ENOMEM;
2310 else
2311 netdev_dbg(bp->dev,
2312 "Allocated %d RX struct sk_buff entries at %p\n",
2313 bp->rx_ring_size, queue->rx_skbuff);
2314 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002315 return 0;
2316}
2317
2318static int macb_alloc_rx_buffers(struct macb *bp)
2319{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002320 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002321 int size;
2322
Zach Brownb410d132016-10-19 09:56:57 -05002323 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002324 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2325 &queue->rx_buffers_dma, GFP_KERNEL);
2326 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002327 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002328
2329 netdev_dbg(bp->dev,
2330 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002331 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002332 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002333}
2334
2335static int macb_alloc_consistent(struct macb *bp)
2336{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002337 struct macb_queue *queue;
2338 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002339 int size;
2340
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002341 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302342 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002343 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2344 &queue->tx_ring_dma,
2345 GFP_KERNEL);
2346 if (!queue->tx_ring)
2347 goto out_err;
2348 netdev_dbg(bp->dev,
2349 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2350 q, size, (unsigned long)queue->tx_ring_dma,
2351 queue->tx_ring);
2352
Zach Brownb410d132016-10-19 09:56:57 -05002353 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002354 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2355 if (!queue->tx_skb)
2356 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002357
Harini Katakam404cd082018-07-06 12:18:58 +05302358 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002359 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2360 &queue->rx_ring_dma, GFP_KERNEL);
2361 if (!queue->rx_ring)
2362 goto out_err;
2363 netdev_dbg(bp->dev,
2364 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2365 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002366 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002367 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002368 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002369
2370 return 0;
2371
2372out_err:
2373 macb_free_consistent(bp);
2374 return -ENOMEM;
2375}
2376
Nicolas Ferre4df95132013-06-04 21:57:12 +00002377static void gem_init_rings(struct macb *bp)
2378{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002379 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002380 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002381 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002382 int i;
2383
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002384 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002385 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002386 desc = macb_tx_desc(queue, i);
2387 macb_set_addr(bp, desc, 0);
2388 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002389 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002390 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002391 queue->tx_head = 0;
2392 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002393
2394 queue->rx_tail = 0;
2395 queue->rx_prepared_head = 0;
2396
2397 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002398 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002399
Nicolas Ferre4df95132013-06-04 21:57:12 +00002400}
2401
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002402static void macb_init_rings(struct macb *bp)
2403{
2404 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002405 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002406
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002407 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002408
Zach Brownb410d132016-10-19 09:56:57 -05002409 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002410 desc = macb_tx_desc(&bp->queues[0], i);
2411 macb_set_addr(bp, desc, 0);
2412 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002413 }
Ben Shelton21d35152015-04-22 17:28:54 -05002414 bp->queues[0].tx_head = 0;
2415 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002416 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002417}
2418
2419static void macb_reset_hw(struct macb *bp)
2420{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002421 struct macb_queue *queue;
2422 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002423 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002424
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002425 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002426 * more gracefully?)
2427 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002428 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002429
2430 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002431 ctrl |= MACB_BIT(CLRSTAT);
2432
2433 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002434
2435 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002436 macb_writel(bp, TSR, -1);
2437 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002438
2439 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002440 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2441 queue_writel(queue, IDR, -1);
2442 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002443 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2444 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002445 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002446}
2447
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002448static u32 gem_mdc_clk_div(struct macb *bp)
2449{
2450 u32 config;
2451 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2452
2453 if (pclk_hz <= 20000000)
2454 config = GEM_BF(CLK, GEM_CLK_DIV8);
2455 else if (pclk_hz <= 40000000)
2456 config = GEM_BF(CLK, GEM_CLK_DIV16);
2457 else if (pclk_hz <= 80000000)
2458 config = GEM_BF(CLK, GEM_CLK_DIV32);
2459 else if (pclk_hz <= 120000000)
2460 config = GEM_BF(CLK, GEM_CLK_DIV48);
2461 else if (pclk_hz <= 160000000)
2462 config = GEM_BF(CLK, GEM_CLK_DIV64);
2463 else
2464 config = GEM_BF(CLK, GEM_CLK_DIV96);
2465
2466 return config;
2467}
2468
2469static u32 macb_mdc_clk_div(struct macb *bp)
2470{
2471 u32 config;
2472 unsigned long pclk_hz;
2473
2474 if (macb_is_gem(bp))
2475 return gem_mdc_clk_div(bp);
2476
2477 pclk_hz = clk_get_rate(bp->pclk);
2478 if (pclk_hz <= 20000000)
2479 config = MACB_BF(CLK, MACB_CLK_DIV8);
2480 else if (pclk_hz <= 40000000)
2481 config = MACB_BF(CLK, MACB_CLK_DIV16);
2482 else if (pclk_hz <= 80000000)
2483 config = MACB_BF(CLK, MACB_CLK_DIV32);
2484 else
2485 config = MACB_BF(CLK, MACB_CLK_DIV64);
2486
2487 return config;
2488}
2489
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002490/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002491 * should program. We find the width from decoding the design configuration
2492 * register to find the maximum supported data bus width.
2493 */
2494static u32 macb_dbw(struct macb *bp)
2495{
2496 if (!macb_is_gem(bp))
2497 return 0;
2498
2499 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2500 case 4:
2501 return GEM_BF(DBW, GEM_DBW128);
2502 case 2:
2503 return GEM_BF(DBW, GEM_DBW64);
2504 case 1:
2505 default:
2506 return GEM_BF(DBW, GEM_DBW32);
2507 }
2508}
2509
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002510/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002511 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002512 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002513 * (if not supported by FIFO, it will fallback to default)
2514 * - set both rx/tx packet buffers to full memory size
2515 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002516 */
2517static void macb_configure_dma(struct macb *bp)
2518{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002519 struct macb_queue *queue;
2520 u32 buffer_size;
2521 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002522 u32 dmacfg;
2523
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002524 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002525 if (macb_is_gem(bp)) {
2526 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002527 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2528 if (q)
2529 queue_writel(queue, RBQS, buffer_size);
2530 else
2531 dmacfg |= GEM_BF(RXBS, buffer_size);
2532 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002533 if (bp->dma_burst_length)
2534 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002535 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302536 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302537
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002538 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302539 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2540 else
2541 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2542
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002543 if (bp->dev->features & NETIF_F_HW_CSUM)
2544 dmacfg |= GEM_BIT(TXCOEN);
2545 else
2546 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302547
Michal Simekbd620722018-09-25 08:32:50 +02002548 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302549#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002550 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002551 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302552#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002553#ifdef CONFIG_MACB_USE_HWSTAMP
2554 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2555 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2556#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002557 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2558 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002559 gem_writel(bp, DMACFG, dmacfg);
2560 }
2561}
2562
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002563static void macb_init_hw(struct macb *bp)
2564{
2565 u32 config;
2566
2567 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002568 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002569
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002570 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002571 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002572 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002573 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302574 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2575 else
2576 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002577 if (bp->dev->flags & IFF_PROMISC)
2578 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002579 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2580 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002581 if (!(bp->dev->flags & IFF_BROADCAST))
2582 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002583 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002584 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002585 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302586 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302587 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002588 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302589 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002590
Jamie Iles0116da42011-03-14 17:38:30 +00002591 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002592}
2593
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002594/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002595 * locations in the memory map. The least significant bits are stored
2596 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2597 *
2598 * The unicast hash enable and the multicast hash enable bits in the
2599 * network configuration register enable the reception of hash matched
2600 * frames. The destination address is reduced to a 6 bit index into
2601 * the 64 bit hash register using the following hash function. The
2602 * hash function is an exclusive or of every sixth bit of the
2603 * destination address.
2604 *
2605 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2606 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2607 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2608 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2609 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2610 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2611 *
2612 * da[0] represents the least significant bit of the first byte
2613 * received, that is, the multicast/unicast indicator, and da[47]
2614 * represents the most significant bit of the last byte received. If
2615 * the hash index, hi[n], points to a bit that is set in the hash
2616 * register then the frame will be matched according to whether the
2617 * frame is multicast or unicast. A multicast match will be signalled
2618 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2619 * index points to a bit set in the hash register. A unicast match
2620 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2621 * and the hash index points to a bit set in the hash register. To
2622 * receive all multicast frames, the hash register should be set with
2623 * all ones and the multicast hash enable bit should be set in the
2624 * network configuration register.
2625 */
2626
2627static inline int hash_bit_value(int bitnr, __u8 *addr)
2628{
2629 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2630 return 1;
2631 return 0;
2632}
2633
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002634/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002635static int hash_get_index(__u8 *addr)
2636{
2637 int i, j, bitval;
2638 int hash_index = 0;
2639
2640 for (j = 0; j < 6; j++) {
2641 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002642 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002643
2644 hash_index |= (bitval << j);
2645 }
2646
2647 return hash_index;
2648}
2649
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002650/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002651static void macb_sethashtable(struct net_device *dev)
2652{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002653 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002654 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002655 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002656 struct macb *bp = netdev_priv(dev);
2657
Moritz Fischeraa50b552016-03-29 19:11:13 -07002658 mc_filter[0] = 0;
2659 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002660
Jiri Pirko22bedad32010-04-01 21:22:57 +00002661 netdev_for_each_mc_addr(ha, dev) {
2662 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002663 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2664 }
2665
Jamie Ilesf75ba502011-11-08 10:12:32 +00002666 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2667 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002668}
2669
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002670/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002671static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002672{
2673 unsigned long cfg;
2674 struct macb *bp = netdev_priv(dev);
2675
2676 cfg = macb_readl(bp, NCFGR);
2677
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002678 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002679 /* Enable promiscuous mode */
2680 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002681
2682 /* Disable RX checksum offload */
2683 if (macb_is_gem(bp))
2684 cfg &= ~GEM_BIT(RXCOEN);
2685 } else {
2686 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002687 cfg &= ~MACB_BIT(CAF);
2688
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002689 /* Enable RX checksum offload only if requested */
2690 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2691 cfg |= GEM_BIT(RXCOEN);
2692 }
2693
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002694 if (dev->flags & IFF_ALLMULTI) {
2695 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002696 macb_or_gem_writel(bp, HRB, -1);
2697 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002698 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002699 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002700 /* Enable specific multicasts */
2701 macb_sethashtable(dev);
2702 cfg |= MACB_BIT(NCFGR_MTI);
2703 } else if (dev->flags & (~IFF_ALLMULTI)) {
2704 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002705 macb_or_gem_writel(bp, HRB, 0);
2706 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002707 cfg &= ~MACB_BIT(NCFGR_MTI);
2708 }
2709
2710 macb_writel(bp, NCFGR, cfg);
2711}
2712
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002713static int macb_open(struct net_device *dev)
2714{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002715 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002716 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002717 struct macb_queue *queue;
2718 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002719 int err;
2720
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002721 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002722
Harini Katakamd54f89a2019-03-01 16:20:34 +05302723 err = pm_runtime_get_sync(&bp->pdev->dev);
2724 if (err < 0)
2725 goto pm_exit;
2726
Nicolas Ferre1b447912013-06-04 21:57:11 +00002727 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002728 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002729
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002730 err = macb_alloc_consistent(bp);
2731 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002732 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2733 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302734 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002735 }
2736
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002737 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2738 napi_enable(&queue->napi);
2739
Harini Katakam05044532019-05-07 19:59:10 +05302740 macb_init_hw(bp);
2741
Antoine Tenart7897b072019-11-13 10:00:06 +01002742 err = macb_phylink_connect(bp);
2743 if (err)
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002744 goto reset_hw;
frederic RODO6c36a702007-07-12 19:07:24 +02002745
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002746 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002747
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002748 if (bp->ptp_info)
2749 bp->ptp_info->ptp_init(dev);
2750
Charles Keepax939a5bf72020-06-15 14:18:54 +01002751 return 0;
2752
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002753reset_hw:
2754 macb_reset_hw(bp);
Corentin Labbe014406b2020-06-10 09:53:44 +00002755 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2756 napi_disable(&queue->napi);
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002757 macb_free_consistent(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302758pm_exit:
Charles Keepax939a5bf72020-06-15 14:18:54 +01002759 pm_runtime_put_sync(&bp->pdev->dev);
2760 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002761}
2762
2763static int macb_close(struct net_device *dev)
2764{
2765 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002766 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002767 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002768 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002769
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002770 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002771
2772 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2773 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002774
Antoine Tenart7897b072019-11-13 10:00:06 +01002775 phylink_stop(bp->phylink);
2776 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002777
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002778 spin_lock_irqsave(&bp->lock, flags);
2779 macb_reset_hw(bp);
2780 netif_carrier_off(dev);
2781 spin_unlock_irqrestore(&bp->lock, flags);
2782
2783 macb_free_consistent(bp);
2784
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002785 if (bp->ptp_info)
2786 bp->ptp_info->ptp_remove(dev);
2787
Harini Katakamd54f89a2019-03-01 16:20:34 +05302788 pm_runtime_put(&bp->pdev->dev);
2789
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002790 return 0;
2791}
2792
Harini Katakama5898ea2015-05-06 22:27:18 +05302793static int macb_change_mtu(struct net_device *dev, int new_mtu)
2794{
Harini Katakama5898ea2015-05-06 22:27:18 +05302795 if (netif_running(dev))
2796 return -EBUSY;
2797
Harini Katakama5898ea2015-05-06 22:27:18 +05302798 dev->mtu = new_mtu;
2799
2800 return 0;
2801}
2802
Jamie Ilesa494ed82011-03-09 16:26:35 +00002803static void gem_update_stats(struct macb *bp)
2804{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002805 struct macb_queue *queue;
2806 unsigned int i, q, idx;
2807 unsigned long *stat;
2808
Jamie Ilesa494ed82011-03-09 16:26:35 +00002809 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002810
Xander Huff3ff13f12015-01-13 16:15:51 -06002811 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2812 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002813 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002814
2815 bp->ethtool_stats[i] += val;
2816 *p += val;
2817
2818 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2819 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002820 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002821 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002822 *(++p) += val;
2823 }
2824 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002825
2826 idx = GEM_STATS_LEN;
2827 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2828 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2829 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002830}
2831
2832static struct net_device_stats *gem_get_stats(struct macb *bp)
2833{
2834 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002835 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002836
Zong Li5eff1462021-05-22 17:16:11 +08002837 if (!netif_running(bp->dev))
2838 return nstat;
2839
Jamie Ilesa494ed82011-03-09 16:26:35 +00002840 gem_update_stats(bp);
2841
2842 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2843 hwstat->rx_alignment_errors +
2844 hwstat->rx_resource_errors +
2845 hwstat->rx_overruns +
2846 hwstat->rx_oversize_frames +
2847 hwstat->rx_jabbers +
2848 hwstat->rx_undersized_frames +
2849 hwstat->rx_length_field_frame_errors);
2850 nstat->tx_errors = (hwstat->tx_late_collisions +
2851 hwstat->tx_excessive_collisions +
2852 hwstat->tx_underrun +
2853 hwstat->tx_carrier_sense_errors);
2854 nstat->multicast = hwstat->rx_multicast_frames;
2855 nstat->collisions = (hwstat->tx_single_collision_frames +
2856 hwstat->tx_multiple_collision_frames +
2857 hwstat->tx_excessive_collisions);
2858 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2859 hwstat->rx_jabbers +
2860 hwstat->rx_undersized_frames +
2861 hwstat->rx_length_field_frame_errors);
2862 nstat->rx_over_errors = hwstat->rx_resource_errors;
2863 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2864 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2865 nstat->rx_fifo_errors = hwstat->rx_overruns;
2866 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2867 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2868 nstat->tx_fifo_errors = hwstat->tx_underrun;
2869
2870 return nstat;
2871}
2872
Xander Huff3ff13f12015-01-13 16:15:51 -06002873static void gem_get_ethtool_stats(struct net_device *dev,
2874 struct ethtool_stats *stats, u64 *data)
2875{
2876 struct macb *bp;
2877
2878 bp = netdev_priv(dev);
2879 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002880 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2881 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002882}
2883
2884static int gem_get_sset_count(struct net_device *dev, int sset)
2885{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002886 struct macb *bp = netdev_priv(dev);
2887
Xander Huff3ff13f12015-01-13 16:15:51 -06002888 switch (sset) {
2889 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002890 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002891 default:
2892 return -EOPNOTSUPP;
2893 }
2894}
2895
2896static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2897{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002898 char stat_string[ETH_GSTRING_LEN];
2899 struct macb *bp = netdev_priv(dev);
2900 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002901 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002902 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002903
2904 switch (sset) {
2905 case ETH_SS_STATS:
2906 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2907 memcpy(p, gem_statistics[i].stat_string,
2908 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002909
2910 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2911 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2912 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2913 q, queue_statistics[i].stat_string);
2914 memcpy(p, stat_string, ETH_GSTRING_LEN);
2915 }
2916 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002917 break;
2918 }
2919}
2920
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002921static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002922{
2923 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002924 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002925 struct macb_stats *hwstat = &bp->hw_stats.macb;
2926
2927 if (macb_is_gem(bp))
2928 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002929
frederic RODO6c36a702007-07-12 19:07:24 +02002930 /* read stats from hardware */
2931 macb_update_stats(bp);
2932
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002933 /* Convert HW stats into netdevice stats */
2934 nstat->rx_errors = (hwstat->rx_fcs_errors +
2935 hwstat->rx_align_errors +
2936 hwstat->rx_resource_errors +
2937 hwstat->rx_overruns +
2938 hwstat->rx_oversize_pkts +
2939 hwstat->rx_jabbers +
2940 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002941 hwstat->rx_length_mismatch);
2942 nstat->tx_errors = (hwstat->tx_late_cols +
2943 hwstat->tx_excessive_cols +
2944 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002945 hwstat->tx_carrier_errors +
2946 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002947 nstat->collisions = (hwstat->tx_single_cols +
2948 hwstat->tx_multiple_cols +
2949 hwstat->tx_excessive_cols);
2950 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2951 hwstat->rx_jabbers +
2952 hwstat->rx_undersize_pkts +
2953 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002954 nstat->rx_over_errors = hwstat->rx_resource_errors +
2955 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002956 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2957 nstat->rx_frame_errors = hwstat->rx_align_errors;
2958 nstat->rx_fifo_errors = hwstat->rx_overruns;
2959 /* XXX: What does "missed" mean? */
2960 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2961 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2962 nstat->tx_fifo_errors = hwstat->tx_underruns;
2963 /* Don't know about heartbeat or window errors... */
2964
2965 return nstat;
2966}
2967
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002968static int macb_get_regs_len(struct net_device *netdev)
2969{
2970 return MACB_GREGS_NBR * sizeof(u32);
2971}
2972
2973static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2974 void *p)
2975{
2976 struct macb *bp = netdev_priv(dev);
2977 unsigned int tail, head;
2978 u32 *regs_buff = p;
2979
2980 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2981 | MACB_GREGS_VERSION;
2982
Zach Brownb410d132016-10-19 09:56:57 -05002983 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2984 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002985
2986 regs_buff[0] = macb_readl(bp, NCR);
2987 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2988 regs_buff[2] = macb_readl(bp, NSR);
2989 regs_buff[3] = macb_readl(bp, TSR);
2990 regs_buff[4] = macb_readl(bp, RBQP);
2991 regs_buff[5] = macb_readl(bp, TBQP);
2992 regs_buff[6] = macb_readl(bp, RSR);
2993 regs_buff[7] = macb_readl(bp, IMR);
2994
2995 regs_buff[8] = tail;
2996 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002997 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2998 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002999
Neil Armstrongce721a72016-01-05 14:39:16 +01003000 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
3001 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003002 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003003 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003004}
3005
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003006static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3007{
3008 struct macb *bp = netdev_priv(netdev);
3009
Nicolas Ferre253fe092020-07-10 14:46:43 +02003010 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
Antoine Tenart7897b072019-11-13 10:00:06 +01003011 phylink_ethtool_get_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003012 wol->supported |= WAKE_MAGIC;
3013
3014 if (bp->wol & MACB_WOL_ENABLED)
3015 wol->wolopts |= WAKE_MAGIC;
3016 }
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003017}
3018
3019static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3020{
3021 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01003022 int ret;
3023
Nicolas Ferre253fe092020-07-10 14:46:43 +02003024 /* Pass the order to phylink layer */
Antoine Tenart7897b072019-11-13 10:00:06 +01003025 ret = phylink_ethtool_set_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003026 /* Don't manage WoL on MAC if handled by the PHY
3027 * or if there's a failure in talking to the PHY
3028 */
3029 if (!ret || ret != -EOPNOTSUPP)
3030 return ret;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003031
3032 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
3033 (wol->wolopts & ~WAKE_MAGIC))
3034 return -EOPNOTSUPP;
3035
3036 if (wol->wolopts & WAKE_MAGIC)
3037 bp->wol |= MACB_WOL_ENABLED;
3038 else
3039 bp->wol &= ~MACB_WOL_ENABLED;
3040
3041 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
3042
3043 return 0;
3044}
3045
Antoine Tenart7897b072019-11-13 10:00:06 +01003046static int macb_get_link_ksettings(struct net_device *netdev,
3047 struct ethtool_link_ksettings *kset)
3048{
3049 struct macb *bp = netdev_priv(netdev);
3050
3051 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3052}
3053
3054static int macb_set_link_ksettings(struct net_device *netdev,
3055 const struct ethtool_link_ksettings *kset)
3056{
3057 struct macb *bp = netdev_priv(netdev);
3058
3059 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3060}
3061
Zach Brown8441bb32016-10-19 09:56:58 -05003062static void macb_get_ringparam(struct net_device *netdev,
Hao Chen74624942021-11-18 20:12:43 +08003063 struct ethtool_ringparam *ring,
3064 struct kernel_ethtool_ringparam *kernel_ring,
3065 struct netlink_ext_ack *extack)
Zach Brown8441bb32016-10-19 09:56:58 -05003066{
3067 struct macb *bp = netdev_priv(netdev);
3068
3069 ring->rx_max_pending = MAX_RX_RING_SIZE;
3070 ring->tx_max_pending = MAX_TX_RING_SIZE;
3071
3072 ring->rx_pending = bp->rx_ring_size;
3073 ring->tx_pending = bp->tx_ring_size;
3074}
3075
3076static int macb_set_ringparam(struct net_device *netdev,
Hao Chen74624942021-11-18 20:12:43 +08003077 struct ethtool_ringparam *ring,
3078 struct kernel_ethtool_ringparam *kernel_ring,
3079 struct netlink_ext_ack *extack)
Zach Brown8441bb32016-10-19 09:56:58 -05003080{
3081 struct macb *bp = netdev_priv(netdev);
3082 u32 new_rx_size, new_tx_size;
3083 unsigned int reset = 0;
3084
3085 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3086 return -EINVAL;
3087
3088 new_rx_size = clamp_t(u32, ring->rx_pending,
3089 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3090 new_rx_size = roundup_pow_of_two(new_rx_size);
3091
3092 new_tx_size = clamp_t(u32, ring->tx_pending,
3093 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3094 new_tx_size = roundup_pow_of_two(new_tx_size);
3095
3096 if ((new_tx_size == bp->tx_ring_size) &&
3097 (new_rx_size == bp->rx_ring_size)) {
3098 /* nothing to do */
3099 return 0;
3100 }
3101
3102 if (netif_running(bp->dev)) {
3103 reset = 1;
3104 macb_close(bp->dev);
3105 }
3106
3107 bp->rx_ring_size = new_rx_size;
3108 bp->tx_ring_size = new_tx_size;
3109
3110 if (reset)
3111 macb_open(bp->dev);
3112
3113 return 0;
3114}
3115
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003116#ifdef CONFIG_MACB_USE_HWSTAMP
3117static unsigned int gem_get_tsu_rate(struct macb *bp)
3118{
3119 struct clk *tsu_clk;
3120 unsigned int tsu_rate;
3121
3122 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
3123 if (!IS_ERR(tsu_clk))
3124 tsu_rate = clk_get_rate(tsu_clk);
3125 /* try pclk instead */
3126 else if (!IS_ERR(bp->pclk)) {
3127 tsu_clk = bp->pclk;
3128 tsu_rate = clk_get_rate(tsu_clk);
3129 } else
3130 return -ENOTSUPP;
3131 return tsu_rate;
3132}
3133
3134static s32 gem_get_ptp_max_adj(void)
3135{
3136 return 64000000;
3137}
3138
3139static int gem_get_ts_info(struct net_device *dev,
3140 struct ethtool_ts_info *info)
3141{
3142 struct macb *bp = netdev_priv(dev);
3143
3144 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
3145 ethtool_op_get_ts_info(dev, info);
3146 return 0;
3147 }
3148
3149 info->so_timestamping =
3150 SOF_TIMESTAMPING_TX_SOFTWARE |
3151 SOF_TIMESTAMPING_RX_SOFTWARE |
3152 SOF_TIMESTAMPING_SOFTWARE |
3153 SOF_TIMESTAMPING_TX_HARDWARE |
3154 SOF_TIMESTAMPING_RX_HARDWARE |
3155 SOF_TIMESTAMPING_RAW_HARDWARE;
3156 info->tx_types =
3157 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3158 (1 << HWTSTAMP_TX_OFF) |
3159 (1 << HWTSTAMP_TX_ON);
3160 info->rx_filters =
3161 (1 << HWTSTAMP_FILTER_NONE) |
3162 (1 << HWTSTAMP_FILTER_ALL);
3163
3164 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
3165
3166 return 0;
3167}
3168
3169static struct macb_ptp_info gem_ptp_info = {
3170 .ptp_init = gem_ptp_init,
3171 .ptp_remove = gem_ptp_remove,
3172 .get_ptp_max_adj = gem_get_ptp_max_adj,
3173 .get_tsu_rate = gem_get_tsu_rate,
3174 .get_ts_info = gem_get_ts_info,
3175 .get_hwtst = gem_get_hwtst,
3176 .set_hwtst = gem_set_hwtst,
3177};
3178#endif
3179
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003180static int macb_get_ts_info(struct net_device *netdev,
3181 struct ethtool_ts_info *info)
3182{
3183 struct macb *bp = netdev_priv(netdev);
3184
3185 if (bp->ptp_info)
3186 return bp->ptp_info->get_ts_info(netdev, info);
3187
3188 return ethtool_op_get_ts_info(netdev, info);
3189}
3190
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003191static void gem_enable_flow_filters(struct macb *bp, bool enable)
3192{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003193 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003194 struct ethtool_rx_fs_item *item;
3195 u32 t2_scr;
3196 int num_t2_scr;
3197
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003198 if (!(netdev->features & NETIF_F_NTUPLE))
3199 return;
3200
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003201 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3202
3203 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3204 struct ethtool_rx_flow_spec *fs = &item->fs;
3205 struct ethtool_tcpip4_spec *tp4sp_m;
3206
3207 if (fs->location >= num_t2_scr)
3208 continue;
3209
3210 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3211
3212 /* enable/disable screener regs for the flow entry */
3213 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3214
3215 /* only enable fields with no masking */
3216 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3217
3218 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3219 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3220 else
3221 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3222
3223 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3224 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3225 else
3226 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3227
3228 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3229 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3230 else
3231 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3232
3233 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3234 }
3235}
3236
3237static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3238{
3239 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3240 uint16_t index = fs->location;
3241 u32 w0, w1, t2_scr;
3242 bool cmp_a = false;
3243 bool cmp_b = false;
3244 bool cmp_c = false;
3245
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003246 if (!macb_is_gem(bp))
3247 return;
3248
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003249 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3250 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3251
3252 /* ignore field if any masking set */
3253 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3254 /* 1st compare reg - IP source address */
3255 w0 = 0;
3256 w1 = 0;
3257 w0 = tp4sp_v->ip4src;
3258 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3259 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3260 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3261 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3262 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3263 cmp_a = true;
3264 }
3265
3266 /* ignore field if any masking set */
3267 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3268 /* 2nd compare reg - IP destination address */
3269 w0 = 0;
3270 w1 = 0;
3271 w0 = tp4sp_v->ip4dst;
3272 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3273 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3274 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3275 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3276 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3277 cmp_b = true;
3278 }
3279
3280 /* ignore both port fields if masking set in both */
3281 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3282 /* 3rd compare reg - source port, destination port */
3283 w0 = 0;
3284 w1 = 0;
3285 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3286 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3287 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3288 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3289 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3290 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3291 } else {
3292 /* only one port definition */
3293 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3294 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3295 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3296 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3297 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3298 } else { /* dst port */
3299 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3300 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3301 }
3302 }
3303 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3304 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3305 cmp_c = true;
3306 }
3307
3308 t2_scr = 0;
3309 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3310 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3311 if (cmp_a)
3312 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3313 if (cmp_b)
3314 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3315 if (cmp_c)
3316 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3317 gem_writel_n(bp, SCRT2, index, t2_scr);
3318}
3319
3320static int gem_add_flow_filter(struct net_device *netdev,
3321 struct ethtool_rxnfc *cmd)
3322{
3323 struct macb *bp = netdev_priv(netdev);
3324 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3325 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003326 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003327 int ret = -EINVAL;
3328 bool added = false;
3329
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003330 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003331 if (newfs == NULL)
3332 return -ENOMEM;
3333 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3334
3335 netdev_dbg(netdev,
3336 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3337 fs->flow_type, (int)fs->ring_cookie, fs->location,
3338 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3339 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3340 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3341
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003342 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3343
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003344 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003345 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3346 if (item->fs.location > newfs->fs.location) {
3347 list_add_tail(&newfs->list, &item->list);
3348 added = true;
3349 break;
3350 } else if (item->fs.location == fs->location) {
3351 netdev_err(netdev, "Rule not added: location %d not free!\n",
3352 fs->location);
3353 ret = -EBUSY;
3354 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003355 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003356 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003357 if (!added)
3358 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003359
3360 gem_prog_cmp_regs(bp, fs);
3361 bp->rx_fs_list.count++;
3362 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003363 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003364
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003365 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003366 return 0;
3367
3368err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003369 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003370 kfree(newfs);
3371 return ret;
3372}
3373
3374static int gem_del_flow_filter(struct net_device *netdev,
3375 struct ethtool_rxnfc *cmd)
3376{
3377 struct macb *bp = netdev_priv(netdev);
3378 struct ethtool_rx_fs_item *item;
3379 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003380 unsigned long flags;
3381
3382 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003383
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003384 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3385 if (item->fs.location == cmd->fs.location) {
3386 /* disable screener regs for the flow entry */
3387 fs = &(item->fs);
3388 netdev_dbg(netdev,
3389 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3390 fs->flow_type, (int)fs->ring_cookie, fs->location,
3391 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3392 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3393 htons(fs->h_u.tcp_ip4_spec.psrc),
3394 htons(fs->h_u.tcp_ip4_spec.pdst));
3395
3396 gem_writel_n(bp, SCRT2, fs->location, 0);
3397
3398 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003399 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003400 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3401 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003402 return 0;
3403 }
3404 }
3405
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003406 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003407 return -EINVAL;
3408}
3409
3410static int gem_get_flow_entry(struct net_device *netdev,
3411 struct ethtool_rxnfc *cmd)
3412{
3413 struct macb *bp = netdev_priv(netdev);
3414 struct ethtool_rx_fs_item *item;
3415
3416 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3417 if (item->fs.location == cmd->fs.location) {
3418 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3419 return 0;
3420 }
3421 }
3422 return -EINVAL;
3423}
3424
3425static int gem_get_all_flow_entries(struct net_device *netdev,
3426 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3427{
3428 struct macb *bp = netdev_priv(netdev);
3429 struct ethtool_rx_fs_item *item;
3430 uint32_t cnt = 0;
3431
3432 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3433 if (cnt == cmd->rule_cnt)
3434 return -EMSGSIZE;
3435 rule_locs[cnt] = item->fs.location;
3436 cnt++;
3437 }
3438 cmd->data = bp->max_tuples;
3439 cmd->rule_cnt = cnt;
3440
3441 return 0;
3442}
3443
3444static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3445 u32 *rule_locs)
3446{
3447 struct macb *bp = netdev_priv(netdev);
3448 int ret = 0;
3449
3450 switch (cmd->cmd) {
3451 case ETHTOOL_GRXRINGS:
3452 cmd->data = bp->num_queues;
3453 break;
3454 case ETHTOOL_GRXCLSRLCNT:
3455 cmd->rule_cnt = bp->rx_fs_list.count;
3456 break;
3457 case ETHTOOL_GRXCLSRULE:
3458 ret = gem_get_flow_entry(netdev, cmd);
3459 break;
3460 case ETHTOOL_GRXCLSRLALL:
3461 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3462 break;
3463 default:
3464 netdev_err(netdev,
3465 "Command parameter %d is not supported\n", cmd->cmd);
3466 ret = -EOPNOTSUPP;
3467 }
3468
3469 return ret;
3470}
3471
3472static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3473{
3474 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003475 int ret;
3476
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003477 switch (cmd->cmd) {
3478 case ETHTOOL_SRXCLSRLINS:
3479 if ((cmd->fs.location >= bp->max_tuples)
3480 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3481 ret = -EINVAL;
3482 break;
3483 }
3484 ret = gem_add_flow_filter(netdev, cmd);
3485 break;
3486 case ETHTOOL_SRXCLSRLDEL:
3487 ret = gem_del_flow_filter(netdev, cmd);
3488 break;
3489 default:
3490 netdev_err(netdev,
3491 "Command parameter %d is not supported\n", cmd->cmd);
3492 ret = -EOPNOTSUPP;
3493 }
3494
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003495 return ret;
3496}
3497
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003498static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003499 .get_regs_len = macb_get_regs_len,
3500 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003501 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003502 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003503 .get_wol = macb_get_wol,
3504 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003505 .get_link_ksettings = macb_get_link_ksettings,
3506 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003507 .get_ringparam = macb_get_ringparam,
3508 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003509};
Xander Huff8cd5a562015-01-15 15:55:20 -06003510
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003511static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003512 .get_regs_len = macb_get_regs_len,
3513 .get_regs = macb_get_regs,
Nicolas Ferre558e35c2020-07-20 10:56:52 +02003514 .get_wol = macb_get_wol,
3515 .set_wol = macb_set_wol,
Xander Huff8cd5a562015-01-15 15:55:20 -06003516 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003517 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003518 .get_ethtool_stats = gem_get_ethtool_stats,
3519 .get_strings = gem_get_ethtool_strings,
3520 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003521 .get_link_ksettings = macb_get_link_ksettings,
3522 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003523 .get_ringparam = macb_get_ringparam,
3524 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003525 .get_rxnfc = gem_get_rxnfc,
3526 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003527};
3528
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003529static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003530{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003531 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003532
3533 if (!netif_running(dev))
3534 return -EINVAL;
3535
Antoine Tenart7897b072019-11-13 10:00:06 +01003536 if (bp->ptp_info) {
3537 switch (cmd) {
3538 case SIOCSHWTSTAMP:
3539 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3540 case SIOCGHWTSTAMP:
3541 return bp->ptp_info->get_hwtst(dev, rq);
3542 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003543 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003544
3545 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003546}
3547
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003548static inline void macb_set_txcsum_feature(struct macb *bp,
3549 netdev_features_t features)
3550{
3551 u32 val;
3552
3553 if (!macb_is_gem(bp))
3554 return;
3555
3556 val = gem_readl(bp, DMACFG);
3557 if (features & NETIF_F_HW_CSUM)
3558 val |= GEM_BIT(TXCOEN);
3559 else
3560 val &= ~GEM_BIT(TXCOEN);
3561
3562 gem_writel(bp, DMACFG, val);
3563}
3564
3565static inline void macb_set_rxcsum_feature(struct macb *bp,
3566 netdev_features_t features)
3567{
3568 struct net_device *netdev = bp->dev;
3569 u32 val;
3570
3571 if (!macb_is_gem(bp))
3572 return;
3573
3574 val = gem_readl(bp, NCFGR);
3575 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3576 val |= GEM_BIT(RXCOEN);
3577 else
3578 val &= ~GEM_BIT(RXCOEN);
3579
3580 gem_writel(bp, NCFGR, val);
3581}
3582
3583static inline void macb_set_rxflow_feature(struct macb *bp,
3584 netdev_features_t features)
3585{
3586 if (!macb_is_gem(bp))
3587 return;
3588
3589 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3590}
3591
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003592static int macb_set_features(struct net_device *netdev,
3593 netdev_features_t features)
3594{
3595 struct macb *bp = netdev_priv(netdev);
3596 netdev_features_t changed = features ^ netdev->features;
3597
3598 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003599 if (changed & NETIF_F_HW_CSUM)
3600 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003601
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003602 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003603 if (changed & NETIF_F_RXCSUM)
3604 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003605
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003606 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003607 if (changed & NETIF_F_NTUPLE)
3608 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003609
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003610 return 0;
3611}
3612
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003613static void macb_restore_features(struct macb *bp)
3614{
3615 struct net_device *netdev = bp->dev;
3616 netdev_features_t features = netdev->features;
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003617 struct ethtool_rx_fs_item *item;
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003618
3619 /* TX checksum offload */
3620 macb_set_txcsum_feature(bp, features);
3621
3622 /* RX checksum offload */
3623 macb_set_rxcsum_feature(bp, features);
3624
3625 /* RX Flow Filters */
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003626 list_for_each_entry(item, &bp->rx_fs_list.list, list)
3627 gem_prog_cmp_regs(bp, &item->fs);
3628
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003629 macb_set_rxflow_feature(bp, features);
3630}
3631
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003632static const struct net_device_ops macb_netdev_ops = {
3633 .ndo_open = macb_open,
3634 .ndo_stop = macb_close,
3635 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003636 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003637 .ndo_get_stats = macb_get_stats,
Arnd Bergmanna7605372021-07-27 15:45:13 +02003638 .ndo_eth_ioctl = macb_ioctl,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003639 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303640 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003641 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003642#ifdef CONFIG_NET_POLL_CONTROLLER
3643 .ndo_poll_controller = macb_poll_controller,
3644#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003645 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003646 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003647};
3648
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003649/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003650 * and integration options used
3651 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003652static void macb_configure_caps(struct macb *bp,
3653 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003654{
3655 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003656
Nicolas Ferref6970502015-03-31 15:02:01 +02003657 if (dt_conf)
3658 bp->caps = dt_conf->caps;
3659
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003660 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003661 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3662
Nicolas Ferree1755872014-07-24 13:50:58 +02003663 dcfg = gem_readl(bp, DCFG1);
3664 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3665 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +01003666 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
3667 bp->caps |= MACB_CAPS_PCS;
3668 dcfg = gem_readl(bp, DCFG12);
3669 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
3670 bp->caps |= MACB_CAPS_HIGH_SPEED;
Nicolas Ferree1755872014-07-24 13:50:58 +02003671 dcfg = gem_readl(bp, DCFG2);
3672 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3673 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003674#ifdef CONFIG_MACB_USE_HWSTAMP
3675 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003676 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003677 dev_err(&bp->pdev->dev,
3678 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003679 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003680 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003681 bp->ptp_info = &gem_ptp_info;
3682 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003683 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003684#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003685 }
3686
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003687 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003688}
3689
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003690static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003691 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003692 unsigned int *queue_mask,
3693 unsigned int *num_queues)
3694{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003695 *queue_mask = 0x1;
3696 *num_queues = 1;
3697
Nicolas Ferreda120112015-03-31 15:02:00 +02003698 /* is it macb or gem ?
3699 *
3700 * We need to read directly from the hardware here because
3701 * we are early in the probe process and don't have the
3702 * MACB_CAPS_MACB_IS_GEM flag positioned
3703 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003704 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003705 return;
3706
3707 /* bit 0 is never set but queue 0 always exists */
Claudiu Bezneafec371f2020-07-02 12:05:58 +03003708 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
Claudiu Bezneab7ab39b2020-07-02 12:05:59 +03003709 *num_queues = hweight32(*queue_mask);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003710}
3711
Claudiu Beznea38493da2020-12-09 15:03:34 +02003712static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
3713 struct clk *rx_clk, struct clk *tsu_clk)
3714{
3715 struct clk_bulk_data clks[] = {
3716 { .clk = tsu_clk, },
3717 { .clk = rx_clk, },
3718 { .clk = pclk, },
3719 { .clk = hclk, },
3720 { .clk = tx_clk },
3721 };
3722
3723 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
3724}
3725
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003726static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303727 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303728 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003729{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003730 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003731 int err;
3732
Bartosz Folta83a77e92016-12-14 06:39:15 +00003733 pdata = dev_get_platdata(&pdev->dev);
3734 if (pdata) {
3735 *pclk = pdata->pclk;
3736 *hclk = pdata->hclk;
3737 } else {
3738 *pclk = devm_clk_get(&pdev->dev, "pclk");
3739 *hclk = devm_clk_get(&pdev->dev, "hclk");
3740 }
3741
Michael Trettera04be4b2021-03-17 17:16:09 +01003742 if (IS_ERR_OR_NULL(*pclk))
3743 return dev_err_probe(&pdev->dev,
3744 IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
3745 "failed to get pclk\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003746
Michael Trettera04be4b2021-03-17 17:16:09 +01003747 if (IS_ERR_OR_NULL(*hclk))
3748 return dev_err_probe(&pdev->dev,
3749 IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
3750 "failed to get hclk\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003751
Michael Tretterbd310aca2019-10-18 16:11:43 +02003752 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003753 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003754 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003755
Michael Tretterbd310aca2019-10-18 16:11:43 +02003756 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303757 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003758 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303759
Michael Tretterbd310aca2019-10-18 16:11:43 +02003760 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303761 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003762 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303763
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003764 err = clk_prepare_enable(*pclk);
3765 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003766 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003767 return err;
3768 }
3769
3770 err = clk_prepare_enable(*hclk);
3771 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003772 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003773 goto err_disable_pclk;
3774 }
3775
3776 err = clk_prepare_enable(*tx_clk);
3777 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003778 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003779 goto err_disable_hclk;
3780 }
3781
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303782 err = clk_prepare_enable(*rx_clk);
3783 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003784 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303785 goto err_disable_txclk;
3786 }
3787
Harini Katakamf5473d12019-03-01 16:20:33 +05303788 err = clk_prepare_enable(*tsu_clk);
3789 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003790 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303791 goto err_disable_rxclk;
3792 }
3793
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003794 return 0;
3795
Harini Katakamf5473d12019-03-01 16:20:33 +05303796err_disable_rxclk:
3797 clk_disable_unprepare(*rx_clk);
3798
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303799err_disable_txclk:
3800 clk_disable_unprepare(*tx_clk);
3801
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003802err_disable_hclk:
3803 clk_disable_unprepare(*hclk);
3804
3805err_disable_pclk:
3806 clk_disable_unprepare(*pclk);
3807
3808 return err;
3809}
3810
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003811static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003812{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003813 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003814 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003815 struct macb *bp = netdev_priv(dev);
3816 struct macb_queue *queue;
3817 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003818 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003819
Zach Brownb410d132016-10-19 09:56:57 -05003820 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3821 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3822
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003823 /* set the queue register mapping once for all: queue0 has a special
3824 * register mapping but we don't want to test the queue index then
3825 * compute the corresponding register offset at run time.
3826 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003827 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003828 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003829 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003830
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003831 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003832 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003833 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003834 if (hw_q) {
3835 queue->ISR = GEM_ISR(hw_q - 1);
3836 queue->IER = GEM_IER(hw_q - 1);
3837 queue->IDR = GEM_IDR(hw_q - 1);
3838 queue->IMR = GEM_IMR(hw_q - 1);
3839 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003840 queue->RBQP = GEM_RBQP(hw_q - 1);
3841 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303842#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003843 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003844 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003845 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3846 }
Harini Katakamfff80192016-08-09 13:15:53 +05303847#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003848 } else {
3849 /* queue0 uses legacy registers */
3850 queue->ISR = MACB_ISR;
3851 queue->IER = MACB_IER;
3852 queue->IDR = MACB_IDR;
3853 queue->IMR = MACB_IMR;
3854 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003855 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303856#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003857 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003858 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003859 queue->RBQPH = MACB_RBQPH;
3860 }
Harini Katakamfff80192016-08-09 13:15:53 +05303861#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003862 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003863
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003864 /* get irq: here we use the linux queue index, not the hardware
3865 * queue index. the queue irq definitions in the device tree
3866 * must remove the optional gaps that could exist in the
3867 * hardware queue mask.
3868 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003869 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003870 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003871 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003872 if (err) {
3873 dev_err(&pdev->dev,
3874 "Unable to request IRQ %d (error %d)\n",
3875 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003876 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003877 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003878
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003879 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003880 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003881 }
3882
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003883 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003884
Nicolas Ferre4df95132013-06-04 21:57:12 +00003885 /* setup appropriated routines according to adapter type */
3886 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003887 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003888 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3889 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3890 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3891 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003892 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003893 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003894 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003895 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3896 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3897 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3898 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003899 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003900 }
3901
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003902 /* Set features */
3903 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003904
3905 /* Check LSO capability */
3906 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3907 dev->hw_features |= MACB_NETIF_LSO;
3908
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003909 /* Checksum offload is only available on gem with packet buffer */
3910 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003911 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003912 if (bp->caps & MACB_CAPS_SG_DISABLED)
3913 dev->hw_features &= ~NETIF_F_SG;
3914 dev->features = dev->hw_features;
3915
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003916 /* Check RX Flow Filters support.
3917 * Max Rx flows set by availability of screeners & compare regs:
3918 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3919 */
3920 reg = gem_readl(bp, DCFG8);
3921 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3922 GEM_BFEXT(T2SCR, reg));
Claudiu Bezneaa714e272021-04-14 14:20:29 +03003923 INIT_LIST_HEAD(&bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003924 if (bp->max_tuples > 0) {
3925 /* also needs one ethtype match to check IPv4 */
3926 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3927 /* program this reg now */
3928 reg = 0;
3929 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3930 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3931 /* Filtering is supported in hw but don't enable it in kernel now */
3932 dev->hw_features |= NETIF_F_NTUPLE;
3933 /* init Rx flow definitions */
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003934 bp->rx_fs_list.count = 0;
3935 spin_lock_init(&bp->rx_fs_lock);
3936 } else
3937 bp->max_tuples = 0;
3938 }
3939
Neil Armstrongce721a72016-01-05 14:39:16 +01003940 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3941 val = 0;
Alexandre Belloni2ccb0162020-07-18 01:32:21 +02003942 if (phy_interface_mode_is_rgmii(bp->phy_interface))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003943 val = bp->usrio->rgmii;
Neil Armstrongce721a72016-01-05 14:39:16 +01003944 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003945 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003946 val = bp->usrio->rmii;
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003947 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003948 val = bp->usrio->mii;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003949
Neil Armstrongce721a72016-01-05 14:39:16 +01003950 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003951 val |= bp->usrio->refclk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003952
Neil Armstrongce721a72016-01-05 14:39:16 +01003953 macb_or_gem_writel(bp, USRIO, val);
3954 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003955
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003956 /* Set MII management clock divider */
3957 val = macb_mdc_clk_div(bp);
3958 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303959 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3960 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003961 macb_writel(bp, NCFGR, val);
3962
3963 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003964}
3965
Atish Patrab1242232021-03-03 11:55:49 -08003966static const struct macb_usrio_config macb_default_usrio = {
3967 .mii = MACB_BIT(MII),
3968 .rmii = MACB_BIT(RMII),
3969 .rgmii = GEM_BIT(RGMII),
3970 .refclk = MACB_BIT(CLKEN),
3971};
3972
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003973#if defined(CONFIG_OF)
3974/* 1518 rounded up */
3975#define AT91ETHER_MAX_RBUFF_SZ 0x600
3976/* max number of receive buffers */
3977#define AT91ETHER_MAX_RX_DESCR 9
3978
Arnd Bergmann49db9222019-07-08 14:48:23 +02003979static struct sifive_fu540_macb_mgmt *mgmt;
3980
Claudiu Beznea33fdef22020-06-24 13:08:18 +03003981static int at91ether_alloc_coherent(struct macb *lp)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003982{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003983 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003984
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003985 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003986 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003987 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003988 &q->rx_ring_dma, GFP_KERNEL);
3989 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003990 return -ENOMEM;
3991
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003992 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003993 AT91ETHER_MAX_RX_DESCR *
3994 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003995 &q->rx_buffers_dma, GFP_KERNEL);
3996 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003997 dma_free_coherent(&lp->pdev->dev,
3998 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003999 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004000 q->rx_ring, q->rx_ring_dma);
4001 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004002 return -ENOMEM;
4003 }
4004
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004005 return 0;
4006}
4007
4008static void at91ether_free_coherent(struct macb *lp)
4009{
4010 struct macb_queue *q = &lp->queues[0];
4011
4012 if (q->rx_ring) {
4013 dma_free_coherent(&lp->pdev->dev,
4014 AT91ETHER_MAX_RX_DESCR *
4015 macb_dma_desc_get_size(lp),
4016 q->rx_ring, q->rx_ring_dma);
4017 q->rx_ring = NULL;
4018 }
4019
4020 if (q->rx_buffers) {
4021 dma_free_coherent(&lp->pdev->dev,
4022 AT91ETHER_MAX_RX_DESCR *
4023 AT91ETHER_MAX_RBUFF_SZ,
4024 q->rx_buffers, q->rx_buffers_dma);
4025 q->rx_buffers = NULL;
4026 }
4027}
4028
4029/* Initialize and start the Receiver and Transmit subsystems */
4030static int at91ether_start(struct macb *lp)
4031{
4032 struct macb_queue *q = &lp->queues[0];
4033 struct macb_dma_desc *desc;
4034 dma_addr_t addr;
4035 u32 ctl;
4036 int i, ret;
4037
4038 ret = at91ether_alloc_coherent(lp);
4039 if (ret)
4040 return ret;
4041
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004042 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004043 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004044 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004045 macb_set_addr(lp, desc, addr);
4046 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004047 addr += AT91ETHER_MAX_RBUFF_SZ;
4048 }
4049
4050 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004051 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004052
4053 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004054 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004055
4056 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004057 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004058
4059 /* Enable Receive and Transmit */
4060 ctl = macb_readl(lp, NCR);
4061 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
4062
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004063 /* Enable MAC interrupts */
4064 macb_writel(lp, IER, MACB_BIT(RCOMP) |
4065 MACB_BIT(RXUBR) |
4066 MACB_BIT(ISR_TUND) |
4067 MACB_BIT(ISR_RLE) |
4068 MACB_BIT(TCOMP) |
4069 MACB_BIT(ISR_ROVR) |
4070 MACB_BIT(HRESP));
4071
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004072 return 0;
4073}
4074
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004075static void at91ether_stop(struct macb *lp)
4076{
4077 u32 ctl;
4078
4079 /* Disable MAC interrupts */
4080 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
4081 MACB_BIT(RXUBR) |
4082 MACB_BIT(ISR_TUND) |
4083 MACB_BIT(ISR_RLE) |
4084 MACB_BIT(TCOMP) |
4085 MACB_BIT(ISR_ROVR) |
4086 MACB_BIT(HRESP));
4087
4088 /* Disable Receiver and Transmitter */
4089 ctl = macb_readl(lp, NCR);
4090 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
4091
4092 /* Free resources. */
4093 at91ether_free_coherent(lp);
4094}
4095
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004096/* Open the ethernet interface */
4097static int at91ether_open(struct net_device *dev)
4098{
4099 struct macb *lp = netdev_priv(dev);
4100 u32 ctl;
4101 int ret;
4102
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004103 ret = pm_runtime_get_sync(&lp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004104 if (ret < 0) {
4105 pm_runtime_put_noidle(&lp->pdev->dev);
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004106 return ret;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004107 }
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004108
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004109 /* Clear internal statistics */
4110 ctl = macb_readl(lp, NCR);
4111 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
4112
4113 macb_set_hwaddr(lp);
4114
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004115 ret = at91ether_start(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004116 if (ret)
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004117 goto pm_exit;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004118
Antoine Tenart7897b072019-11-13 10:00:06 +01004119 ret = macb_phylink_connect(lp);
4120 if (ret)
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004121 goto stop;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004122
4123 netif_start_queue(dev);
4124
4125 return 0;
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004126
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004127stop:
4128 at91ether_stop(lp);
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004129pm_exit:
4130 pm_runtime_put_sync(&lp->pdev->dev);
4131 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004132}
4133
4134/* Close the interface */
4135static int at91ether_close(struct net_device *dev)
4136{
4137 struct macb *lp = netdev_priv(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004138
4139 netif_stop_queue(dev);
4140
Antoine Tenart7897b072019-11-13 10:00:06 +01004141 phylink_stop(lp->phylink);
4142 phylink_disconnect_phy(lp->phylink);
4143
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004144 at91ether_stop(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004145
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004146 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004147}
4148
4149/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03004150static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
4151 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004152{
4153 struct macb *lp = netdev_priv(dev);
4154
Willy Tarreau1d608d22020-12-09 19:47:40 +01004155 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
4156 int desc = 0;
4157
4158 netif_stop_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004159
4160 /* Store packet information (to free when Tx completed) */
Willy Tarreau73d74222020-10-11 11:09:43 +02004161 lp->rm9200_txq[desc].skb = skb;
4162 lp->rm9200_txq[desc].size = skb->len;
4163 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
4164 skb->len, DMA_TO_DEVICE);
4165 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03004166 dev_kfree_skb_any(skb);
4167 dev->stats.tx_dropped++;
4168 netdev_err(dev, "%s: DMA mapping error\n", __func__);
4169 return NETDEV_TX_OK;
4170 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004171
4172 /* Set address of the data in the Transmit Address register */
Willy Tarreau73d74222020-10-11 11:09:43 +02004173 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004174 /* Set length of the packet in the Transmit Control register */
4175 macb_writel(lp, TCR, skb->len);
4176
4177 } else {
4178 netdev_err(dev, "%s called, but device is busy!\n", __func__);
4179 return NETDEV_TX_BUSY;
4180 }
4181
4182 return NETDEV_TX_OK;
4183}
4184
4185/* Extract received frame from buffer descriptors and sent to upper layers.
4186 * (Called from interrupt context)
4187 */
4188static void at91ether_rx(struct net_device *dev)
4189{
4190 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004191 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004192 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004193 unsigned char *p_recv;
4194 struct sk_buff *skb;
4195 unsigned int pktlen;
4196
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004197 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004198 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004199 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004200 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004201 skb = netdev_alloc_skb(dev, pktlen + 2);
4202 if (skb) {
4203 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004204 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004205
4206 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004207 dev->stats.rx_packets++;
4208 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004209 netif_rx(skb);
4210 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004211 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004212 }
4213
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004214 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004215 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004216
4217 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004218 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004219
4220 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004221 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
4222 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004223 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004224 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004225
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004226 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004227 }
4228}
4229
4230/* MAC interrupt handler */
4231static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
4232{
4233 struct net_device *dev = dev_id;
4234 struct macb *lp = netdev_priv(dev);
4235 u32 intstatus, ctl;
Willy Tarreau73d74222020-10-11 11:09:43 +02004236 unsigned int desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004237
4238 /* MAC Interrupt Status register indicates what interrupts are pending.
4239 * It is automatically cleared once read.
4240 */
4241 intstatus = macb_readl(lp, ISR);
4242
4243 /* Receive complete */
4244 if (intstatus & MACB_BIT(RCOMP))
4245 at91ether_rx(dev);
4246
4247 /* Transmit complete */
Willy Tarreau1d608d22020-12-09 19:47:40 +01004248 if (intstatus & MACB_BIT(TCOMP)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004249 /* The TCOM bit is set even if the transmission failed */
4250 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004251 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004252
Willy Tarreau1d608d22020-12-09 19:47:40 +01004253 desc = 0;
4254 if (lp->rm9200_txq[desc].skb) {
Willy Tarreau73d74222020-10-11 11:09:43 +02004255 dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
4256 lp->rm9200_txq[desc].skb = NULL;
4257 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
4258 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004259 dev->stats.tx_packets++;
Willy Tarreau73d74222020-10-11 11:09:43 +02004260 dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004261 }
Willy Tarreau1d608d22020-12-09 19:47:40 +01004262 netif_wake_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004263 }
4264
4265 /* Work-around for EMAC Errata section 41.3.1 */
4266 if (intstatus & MACB_BIT(RXUBR)) {
4267 ctl = macb_readl(lp, NCR);
4268 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08004269 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004270 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4271 }
4272
4273 if (intstatus & MACB_BIT(ISR_ROVR))
4274 netdev_err(dev, "ROVR error\n");
4275
4276 return IRQ_HANDLED;
4277}
4278
4279#ifdef CONFIG_NET_POLL_CONTROLLER
4280static void at91ether_poll_controller(struct net_device *dev)
4281{
4282 unsigned long flags;
4283
4284 local_irq_save(flags);
4285 at91ether_interrupt(dev->irq, dev);
4286 local_irq_restore(flags);
4287}
4288#endif
4289
4290static const struct net_device_ops at91ether_netdev_ops = {
4291 .ndo_open = at91ether_open,
4292 .ndo_stop = at91ether_close,
4293 .ndo_start_xmit = at91ether_start_xmit,
4294 .ndo_get_stats = macb_get_stats,
4295 .ndo_set_rx_mode = macb_set_rx_mode,
4296 .ndo_set_mac_address = eth_mac_addr,
Arnd Bergmanna7605372021-07-27 15:45:13 +02004297 .ndo_eth_ioctl = macb_ioctl,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004298 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004299#ifdef CONFIG_NET_POLL_CONTROLLER
4300 .ndo_poll_controller = at91ether_poll_controller,
4301#endif
4302};
4303
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004304static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304305 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304306 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004307{
4308 int err;
4309
4310 *hclk = NULL;
4311 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304312 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304313 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004314
4315 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4316 if (IS_ERR(*pclk))
4317 return PTR_ERR(*pclk);
4318
4319 err = clk_prepare_enable(*pclk);
4320 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004321 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004322 return err;
4323 }
4324
4325 return 0;
4326}
4327
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004328static int at91ether_init(struct platform_device *pdev)
4329{
4330 struct net_device *dev = platform_get_drvdata(pdev);
4331 struct macb *bp = netdev_priv(dev);
4332 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004333
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004334 bp->queues[0].bp = bp;
4335
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004336 dev->netdev_ops = &at91ether_netdev_ops;
4337 dev->ethtool_ops = &macb_ethtool_ops;
4338
4339 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4340 0, dev->name, dev);
4341 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004342 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004343
4344 macb_writel(bp, NCR, 0);
4345
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004346 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004347
4348 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004349}
4350
Yash Shahc218ad52019-06-18 13:26:08 +05304351static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4352 unsigned long parent_rate)
4353{
4354 return mgmt->rate;
4355}
4356
4357static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4358 unsigned long *parent_rate)
4359{
4360 if (WARN_ON(rate < 2500000))
4361 return 2500000;
4362 else if (rate == 2500000)
4363 return 2500000;
4364 else if (WARN_ON(rate < 13750000))
4365 return 2500000;
4366 else if (WARN_ON(rate < 25000000))
4367 return 25000000;
4368 else if (rate == 25000000)
4369 return 25000000;
4370 else if (WARN_ON(rate < 75000000))
4371 return 25000000;
4372 else if (WARN_ON(rate < 125000000))
4373 return 125000000;
4374 else if (rate == 125000000)
4375 return 125000000;
4376
4377 WARN_ON(rate > 125000000);
4378
4379 return 125000000;
4380}
4381
4382static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4383 unsigned long parent_rate)
4384{
4385 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4386 if (rate != 125000000)
4387 iowrite32(1, mgmt->reg);
4388 else
4389 iowrite32(0, mgmt->reg);
4390 mgmt->rate = rate;
4391
4392 return 0;
4393}
4394
4395static const struct clk_ops fu540_c000_ops = {
4396 .recalc_rate = fu540_macb_tx_recalc_rate,
4397 .round_rate = fu540_macb_tx_round_rate,
4398 .set_rate = fu540_macb_tx_set_rate,
4399};
4400
4401static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4402 struct clk **hclk, struct clk **tx_clk,
4403 struct clk **rx_clk, struct clk **tsu_clk)
4404{
4405 struct clk_init_data init;
4406 int err = 0;
4407
4408 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4409 if (err)
4410 return err;
4411
4412 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004413 if (!mgmt) {
4414 err = -ENOMEM;
4415 goto err_disable_clks;
4416 }
Yash Shahc218ad52019-06-18 13:26:08 +05304417
4418 init.name = "sifive-gemgxl-mgmt";
4419 init.ops = &fu540_c000_ops;
4420 init.flags = 0;
4421 init.num_parents = 0;
4422
4423 mgmt->rate = 0;
4424 mgmt->hw.init = &init;
4425
Stephen Boydd89091a2020-01-03 16:19:21 -08004426 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004427 if (IS_ERR(*tx_clk)) {
4428 err = PTR_ERR(*tx_clk);
4429 goto err_disable_clks;
4430 }
Yash Shahc218ad52019-06-18 13:26:08 +05304431
4432 err = clk_prepare_enable(*tx_clk);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004433 if (err) {
Yash Shahc218ad52019-06-18 13:26:08 +05304434 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004435 *tx_clk = NULL;
4436 goto err_disable_clks;
4437 } else {
Yash Shahc218ad52019-06-18 13:26:08 +05304438 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004439 }
Yash Shahc218ad52019-06-18 13:26:08 +05304440
4441 return 0;
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004442
4443err_disable_clks:
4444 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
4445
4446 return err;
Yash Shahc218ad52019-06-18 13:26:08 +05304447}
4448
4449static int fu540_c000_init(struct platform_device *pdev)
4450{
Dejin Zhengb959c772020-05-03 20:32:26 +08004451 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4452 if (IS_ERR(mgmt->reg))
4453 return PTR_ERR(mgmt->reg);
Yash Shahc218ad52019-06-18 13:26:08 +05304454
4455 return macb_init(pdev);
4456}
4457
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004458static const struct macb_usrio_config sama7g5_usrio = {
4459 .mii = 0,
4460 .rmii = 1,
4461 .rgmii = 2,
4462 .refclk = BIT(2),
4463 .hdfctlen = BIT(6),
4464};
4465
Yash Shahc218ad52019-06-18 13:26:08 +05304466static const struct macb_config fu540_c000_config = {
4467 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4468 MACB_CAPS_GEM_HAS_PTP,
4469 .dma_burst_length = 16,
4470 .clk_init = fu540_c000_clk_init,
4471 .init = fu540_c000_init,
4472 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004473 .usrio = &macb_default_usrio,
Yash Shahc218ad52019-06-18 13:26:08 +05304474};
4475
David S. Miller3cef5c52015-03-09 23:38:02 -04004476static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004477 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004478 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004479 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004480 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004481};
4482
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004483static const struct macb_config sama5d3macb_config = {
4484 .caps = MACB_CAPS_SG_DISABLED
4485 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4486 .clk_init = macb_clk_init,
4487 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004488 .usrio = &macb_default_usrio,
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004489};
4490
David S. Miller3cef5c52015-03-09 23:38:02 -04004491static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004492 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4493 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004494 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004495 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004496 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004497};
4498
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004499static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004500 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004501 .dma_burst_length = 16,
4502 .clk_init = macb_clk_init,
4503 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004504 .usrio = &macb_default_usrio,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004505};
4506
Hari Prasath7d13ad52021-08-12 13:14:21 +05304507static const struct macb_config sama5d29_config = {
4508 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP,
4509 .dma_burst_length = 16,
4510 .clk_init = macb_clk_init,
4511 .init = macb_init,
4512 .usrio = &macb_default_usrio,
4513};
4514
David S. Miller3cef5c52015-03-09 23:38:02 -04004515static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004516 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004517 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004518 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004519 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004520 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004521 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004522 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004523};
4524
David S. Miller3cef5c52015-03-09 23:38:02 -04004525static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004526 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004527 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004528 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004529 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004530 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004531};
4532
David S. Miller3cef5c52015-03-09 23:38:02 -04004533static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004534 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004535 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004536 .init = at91ether_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004537 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004538};
4539
Neil Armstronge611b5b2016-01-05 14:39:17 +01004540static const struct macb_config np4_config = {
4541 .caps = MACB_CAPS_USRIO_DISABLED,
4542 .clk_init = macb_clk_init,
4543 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004544 .usrio = &macb_default_usrio,
Neil Armstronge611b5b2016-01-05 14:39:17 +01004545};
David S. Miller36583eb2015-05-23 01:22:35 -04004546
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304547static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004548 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4549 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304550 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304551 .dma_burst_length = 16,
4552 .clk_init = macb_clk_init,
4553 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304554 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004555 .usrio = &macb_default_usrio,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304556};
4557
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004558static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304559 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4560 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004561 .dma_burst_length = 16,
4562 .clk_init = macb_clk_init,
4563 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004564 .usrio = &macb_default_usrio,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004565};
4566
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004567static const struct macb_config sama7g5_gem_config = {
Claudiu Beznea0f4f6d72021-09-17 16:26:15 +03004568 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
4569 MACB_CAPS_MIIONRGMII,
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004570 .dma_burst_length = 16,
4571 .clk_init = macb_clk_init,
4572 .init = macb_init,
4573 .usrio = &sama7g5_usrio,
4574};
4575
Claudiu Beznea700d5662020-12-09 15:03:39 +02004576static const struct macb_config sama7g5_emac_config = {
Claudiu Beznea0f4f6d72021-09-17 16:26:15 +03004577 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
4578 MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII,
Claudiu Beznea700d5662020-12-09 15:03:39 +02004579 .dma_burst_length = 16,
4580 .clk_init = macb_clk_init,
4581 .init = macb_init,
4582 .usrio = &sama7g5_usrio,
4583};
4584
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004585static const struct of_device_id macb_dt_ids[] = {
4586 { .compatible = "cdns,at32ap7000-macb" },
4587 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4588 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004589 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004590 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4591 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004592 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004593 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Hari Prasath7d13ad52021-08-12 13:14:21 +05304594 { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004595 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004596 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004597 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4598 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4599 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304600 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004601 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304602 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004603 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
Claudiu Beznea700d5662020-12-09 15:03:39 +02004604 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004605 { /* sentinel */ }
4606};
4607MODULE_DEVICE_TABLE(of, macb_dt_ids);
4608#endif /* CONFIG_OF */
4609
Bartosz Folta83a77e92016-12-14 06:39:15 +00004610static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004611 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4612 MACB_CAPS_JUMBO |
4613 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004614 .dma_burst_length = 16,
4615 .clk_init = macb_clk_init,
4616 .init = macb_init,
Atish Patrab1242232021-03-03 11:55:49 -08004617 .usrio = &macb_default_usrio,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004618 .jumbo_max_len = 10240,
4619};
4620
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004621static int macb_probe(struct platform_device *pdev)
4622{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004623 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004624 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304625 struct clk **, struct clk **, struct clk **,
4626 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004627 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004628 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304629 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304630 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004631 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004632 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004633 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004634 struct net_device *dev;
4635 struct resource *regs;
4636 void __iomem *mem;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004637 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304638 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004639
Yang Yingliang809660c2021-06-07 21:43:54 +08004640 mem = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004641 if (IS_ERR(mem))
4642 return PTR_ERR(mem);
4643
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004644 if (np) {
4645 const struct of_device_id *match;
4646
4647 match = of_match_node(macb_dt_ids, np);
4648 if (match && match->data) {
4649 macb_config = match->data;
4650 clk_init = macb_config->clk_init;
4651 init = macb_config->init;
4652 }
4653 }
4654
Harini Katakamf5473d12019-03-01 16:20:33 +05304655 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004656 if (err)
4657 return err;
4658
Harini Katakamd54f89a2019-03-01 16:20:34 +05304659 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4660 pm_runtime_use_autosuspend(&pdev->dev);
4661 pm_runtime_get_noresume(&pdev->dev);
4662 pm_runtime_set_active(&pdev->dev);
4663 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004664 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004665
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004666 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004667 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004668 if (!dev) {
4669 err = -ENOMEM;
4670 goto err_disable_clocks;
4671 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004672
4673 dev->base_addr = regs->start;
4674
4675 SET_NETDEV_DEV(dev, &pdev->dev);
4676
4677 bp = netdev_priv(dev);
4678 bp->pdev = pdev;
4679 bp->dev = dev;
4680 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004681 bp->native_io = native_io;
4682 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004683 bp->macb_reg_readl = hw_readl_native;
4684 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004685 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004686 bp->macb_reg_readl = hw_readl;
4687 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004688 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004689 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004690 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004691 if (macb_config)
4692 bp->dma_burst_length = macb_config->dma_burst_length;
4693 bp->pclk = pclk;
4694 bp->hclk = hclk;
4695 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304696 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304697 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004698 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304699 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304700
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004701 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004702 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004703 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
Nicolas Ferreced47992020-07-10 14:46:42 +02004704 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004705
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004706 bp->usrio = macb_config->usrio;
4707
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004708 spin_lock_init(&bp->lock);
4709
Nicolas Ferread783472015-03-31 15:02:02 +02004710 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004711 macb_configure_caps(bp, macb_config);
4712
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004713#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4714 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
Marc St-Amand37f78602022-02-09 15:13:25 +05304715 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004716 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4717 }
4718#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004719 platform_set_drvdata(pdev, dev);
4720
4721 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004722 if (dev->irq < 0) {
4723 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004724 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004725 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004726
Jarod Wilson44770e12016-10-17 15:54:17 -04004727 /* MTU range: 68 - 1500 or 10240 */
4728 dev->min_mtu = GEM_MTU_MIN_SIZE;
4729 if (bp->caps & MACB_CAPS_JUMBO)
4730 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4731 else
4732 dev->max_mtu = ETH_DATA_LEN;
4733
Harini Katakam404cd082018-07-06 12:18:58 +05304734 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4735 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4736 if (val)
4737 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4738 macb_dma_desc_get_size(bp);
4739
4740 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4741 if (val)
4742 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4743 macb_dma_desc_get_size(bp);
4744 }
4745
Harini Katakame5010702019-01-29 15:20:03 +05304746 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4747 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4748 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4749
Jakub Kicinski9ca01b22021-10-06 18:06:56 -07004750 err = of_get_ethdev_address(np, bp->dev);
Michael Walle83216e32021-04-12 19:47:17 +02004751 if (err == -EPROBE_DEFER)
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004752 goto err_out_free_netdev;
Michael Walle83216e32021-04-12 19:47:17 +02004753 else if (err)
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004754 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02004755
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004756 err = of_get_phy_mode(np, &interface);
4757 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004758 /* not found in DT, MII by default */
4759 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4760 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004761 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004762
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004763 /* IP specific init */
4764 err = init(pdev);
4765 if (err)
4766 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004767
Florian Fainellicf669662016-05-02 18:38:45 -07004768 err = macb_mii_init(bp);
4769 if (err)
4770 goto err_out_free_netdev;
4771
Florian Fainellicf669662016-05-02 18:38:45 -07004772 netif_carrier_off(dev);
4773
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004774 err = register_netdev(dev);
4775 if (err) {
4776 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004777 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004778 }
4779
Allen Paise7412b82020-09-14 12:59:23 +05304780 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
Harini Katakam032dc412018-01-27 12:09:01 +05304781
Bo Shen58798232014-09-13 01:57:49 +02004782 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4783 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4784 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004785
Harini Katakamd54f89a2019-03-01 16:20:34 +05304786 pm_runtime_mark_last_busy(&bp->pdev->dev);
4787 pm_runtime_put_autosuspend(&bp->pdev->dev);
4788
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004789 return 0;
4790
Florian Fainellicf669662016-05-02 18:38:45 -07004791err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004792 mdiobus_unregister(bp->mii_bus);
4793 mdiobus_free(bp->mii_bus);
4794
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004795err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004796 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004797
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004798err_disable_clocks:
Claudiu Beznea38493da2020-12-09 15:03:34 +02004799 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304800 pm_runtime_disable(&pdev->dev);
4801 pm_runtime_set_suspended(&pdev->dev);
4802 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004803
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004804 return err;
4805}
4806
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004807static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004808{
4809 struct net_device *dev;
4810 struct macb *bp;
4811
4812 dev = platform_get_drvdata(pdev);
4813
4814 if (dev) {
4815 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004816 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004817 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004818
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004819 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004820 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304821 pm_runtime_disable(&pdev->dev);
4822 pm_runtime_dont_use_autosuspend(&pdev->dev);
4823 if (!pm_runtime_suspended(&pdev->dev)) {
Claudiu Beznea38493da2020-12-09 15:03:34 +02004824 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
4825 bp->rx_clk, bp->tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304826 pm_runtime_set_suspended(&pdev->dev);
4827 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004828 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004829 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004830 }
4831
4832 return 0;
4833}
4834
Michal Simekd23823d2015-01-23 09:36:03 +01004835static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004836{
Wolfram Sangce886a42018-10-21 22:00:14 +02004837 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004838 struct macb *bp = netdev_priv(netdev);
Jiapeng Chongbbf6ace2021-04-29 18:25:46 +08004839 struct macb_queue *queue;
Harini Katakamde991c52019-03-01 16:20:35 +05304840 unsigned long flags;
4841 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004842 int err;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004843
Harini Katakamde991c52019-03-01 16:20:35 +05304844 if (!netif_running(netdev))
4845 return 0;
4846
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004847 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004848 spin_lock_irqsave(&bp->lock, flags);
4849 /* Flush all status bits */
4850 macb_writel(bp, TSR, -1);
4851 macb_writel(bp, RSR, -1);
Harini Katakamde991c52019-03-01 16:20:35 +05304852 for (q = 0, queue = bp->queues; q < bp->num_queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004853 ++q, ++queue) {
4854 /* Disable all interrupts */
4855 queue_writel(queue, IDR, -1);
4856 queue_readl(queue, ISR);
4857 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4858 queue_writel(queue, ISR, -1);
4859 }
4860 /* Change interrupt handler and
4861 * Enable WoL IRQ on queue 0
4862 */
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004863 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004864 if (macb_is_gem(bp)) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004865 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
4866 IRQF_SHARED, netdev->name, bp->queues);
4867 if (err) {
4868 dev_err(dev,
4869 "Unable to request IRQ %d (error %d)\n",
4870 bp->queues[0].irq, err);
4871 spin_unlock_irqrestore(&bp->lock, flags);
4872 return err;
4873 }
4874 queue_writel(bp->queues, IER, GEM_BIT(WOL));
4875 gem_writel(bp, WOL, MACB_BIT(MAG));
4876 } else {
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004877 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
4878 IRQF_SHARED, netdev->name, bp->queues);
4879 if (err) {
4880 dev_err(dev,
4881 "Unable to request IRQ %d (error %d)\n",
4882 bp->queues[0].irq, err);
4883 spin_unlock_irqrestore(&bp->lock, flags);
4884 return err;
4885 }
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004886 queue_writel(bp->queues, IER, MACB_BIT(WOL));
4887 macb_writel(bp, WOL, MACB_BIT(MAG));
4888 }
4889 spin_unlock_irqrestore(&bp->lock, flags);
4890
4891 enable_irq_wake(bp->queues[0].irq);
4892 }
4893
4894 netif_device_detach(netdev);
4895 for (q = 0, queue = bp->queues; q < bp->num_queues;
4896 ++q, ++queue)
4897 napi_disable(&queue->napi);
4898
4899 if (!(bp->wol & MACB_WOL_ENABLED)) {
Antoine Tenart7897b072019-11-13 10:00:06 +01004900 rtnl_lock();
4901 phylink_stop(bp->phylink);
4902 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304903 spin_lock_irqsave(&bp->lock, flags);
4904 macb_reset_hw(bp);
4905 spin_unlock_irqrestore(&bp->lock, flags);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304906 }
4907
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004908 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4909 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4910
4911 if (netdev->hw_features & NETIF_F_NTUPLE)
4912 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4913
Harini Katakamde991c52019-03-01 16:20:35 +05304914 if (bp->ptp_info)
4915 bp->ptp_info->ptp_remove(netdev);
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004916 if (!device_may_wakeup(dev))
4917 pm_runtime_force_suspend(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304918
4919 return 0;
4920}
4921
4922static int __maybe_unused macb_resume(struct device *dev)
4923{
4924 struct net_device *netdev = dev_get_drvdata(dev);
4925 struct macb *bp = netdev_priv(netdev);
Jiapeng Chongbbf6ace2021-04-29 18:25:46 +08004926 struct macb_queue *queue;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004927 unsigned long flags;
Harini Katakamde991c52019-03-01 16:20:35 +05304928 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004929 int err;
Harini Katakamde991c52019-03-01 16:20:35 +05304930
4931 if (!netif_running(netdev))
4932 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304933
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004934 if (!device_may_wakeup(dev))
4935 pm_runtime_force_resume(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304936
4937 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004938 spin_lock_irqsave(&bp->lock, flags);
4939 /* Disable WoL */
4940 if (macb_is_gem(bp)) {
4941 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
4942 gem_writel(bp, WOL, 0);
4943 } else {
4944 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
4945 macb_writel(bp, WOL, 0);
4946 }
4947 /* Clear ISR on queue 0 */
4948 queue_readl(bp->queues, ISR);
4949 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4950 queue_writel(bp->queues, ISR, -1);
4951 /* Replace interrupt handler on queue 0 */
4952 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
4953 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
4954 IRQF_SHARED, netdev->name, bp->queues);
4955 if (err) {
4956 dev_err(dev,
4957 "Unable to request IRQ %d (error %d)\n",
4958 bp->queues[0].irq, err);
4959 spin_unlock_irqrestore(&bp->lock, flags);
4960 return err;
4961 }
4962 spin_unlock_irqrestore(&bp->lock, flags);
4963
Harini Katakamd54f89a2019-03-01 16:20:34 +05304964 disable_irq_wake(bp->queues[0].irq);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004965
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004966 /* Now make sure we disable phy before moving
4967 * to common restore path
4968 */
Antoine Tenart7897b072019-11-13 10:00:06 +01004969 rtnl_lock();
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004970 phylink_stop(bp->phylink);
Antoine Tenart7897b072019-11-13 10:00:06 +01004971 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304972 }
4973
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004974 for (q = 0, queue = bp->queues; q < bp->num_queues;
4975 ++q, ++queue)
4976 napi_enable(&queue->napi);
4977
4978 if (netdev->hw_features & NETIF_F_NTUPLE)
4979 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4980
4981 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4982 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4983
4984 macb_writel(bp, NCR, MACB_BIT(MPE));
Harini Katakamde991c52019-03-01 16:20:35 +05304985 macb_init_hw(bp);
4986 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004987 macb_restore_features(bp);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004988 rtnl_lock();
4989 phylink_start(bp->phylink);
4990 rtnl_unlock();
4991
Harini Katakamd54f89a2019-03-01 16:20:34 +05304992 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304993 if (bp->ptp_info)
4994 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304995
4996 return 0;
4997}
4998
4999static int __maybe_unused macb_runtime_suspend(struct device *dev)
5000{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005001 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05305002 struct macb *bp = netdev_priv(netdev);
5003
Claudiu Beznea38493da2020-12-09 15:03:34 +02005004 if (!(device_may_wakeup(dev)))
5005 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
5006 else
5007 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005008
5009 return 0;
5010}
5011
Harini Katakamd54f89a2019-03-01 16:20:34 +05305012static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005013{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005014 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005015 struct macb *bp = netdev_priv(netdev);
5016
Nicolas Ferre515a10a2020-07-10 14:46:41 +02005017 if (!(device_may_wakeup(dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005018 clk_prepare_enable(bp->pclk);
5019 clk_prepare_enable(bp->hclk);
5020 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05305021 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005022 }
Harini Katakamf5473d12019-03-01 16:20:33 +05305023 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005024
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005025 return 0;
5026}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005027
Harini Katakamd54f89a2019-03-01 16:20:34 +05305028static const struct dev_pm_ops macb_pm_ops = {
5029 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
5030 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
5031};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005032
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005033static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005034 .probe = macb_probe,
5035 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005036 .driver = {
5037 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01005038 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005039 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005040 },
5041};
5042
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005043module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005044
5045MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00005046MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02005047MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07005048MODULE_ALIAS("platform:macb");