blob: 6bc7d41d519b7065f809b18be4f5b450b871c2d3 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00003 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004 *
5 * Copyright (C) 2004-2006 Atmel Corporation
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01006 */
7
Jamie Ilesc220f8c2011-03-08 20:27:08 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01009#include <linux/clk.h>
Yash Shahc218ad52019-06-18 13:26:08 +053010#include <linux/clk-provider.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030011#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000016#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010017#include <linux/slab.h>
18#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080019#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000020#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010021#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/platform_device.h>
Antoine Tenart7897b072019-11-13 10:00:06 +010027#include <linux/phylink.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080028#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010029#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010030#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000033#include <linux/ip.h>
34#include <linux/udp.h>
35#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053036#include <linux/iopoll.h>
Harini Katakamd54f89a2019-03-01 16:20:34 +053037#include <linux/pm_runtime.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Yash Shahc218ad52019-06-18 13:26:08 +053040/* This structure is only used for MACB on SiFive FU540 devices */
41struct sifive_fu540_macb_mgmt {
42 void __iomem *reg;
43 unsigned long rate;
44 struct clk_hw hw;
45};
46
Nicolas Ferre1b447912013-06-04 21:57:11 +000047#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000048#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050049
Zach Brownb410d132016-10-19 09:56:57 -050050#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050051#define MIN_RX_RING_SIZE 64
52#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000053#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050054 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
Zach Brownb410d132016-10-19 09:56:57 -050056#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050057#define MIN_TX_RING_SIZE 64
58#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000059#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050060 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010061
Nicolas Ferre909a8582012-11-19 06:00:21 +000062/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050063#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010064
Harini Katakame5010702019-01-29 15:20:03 +053065#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000066#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
67 | MACB_BIT(ISR_RLE) \
68 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000069#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
70 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000071
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000072/* Max length of transmit frame must be a multiple of 8 bytes */
73#define MACB_TX_LEN_ALIGN 8
74#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Harini Katakamf822e9c2020-02-05 18:08:12 +053075/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
76 * false amba_error in TX path from the DMA assuming there is not enough
77 * space in the SRAM (16KB) even when there is.
78 */
79#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020080
Jarod Wilson44770e12016-10-17 15:54:17 -040081#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070082#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053083
Sergio Prado3e2a5e12016-02-09 12:07:16 -020084#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
85#define MACB_WOL_ENABLED (0x1 << 1)
86
Parshuram Thombaree4e143e2020-10-29 13:47:07 +010087#define HS_SPEED_10000M 4
88#define MACB_SERDES_RATE_10G 1
89
Moritz Fischer64ec42f2016-03-29 19:11:12 -070090/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000091 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
92 */
93#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010094
Harini Katakamd54f89a2019-03-01 16:20:34 +053095#define MACB_PM_TIMEOUT 100 /* ms */
96
Harini Katakam8beb79b2019-03-01 16:20:32 +053097#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
98
Rafal Ozieblodc97a892017-01-27 15:08:20 +000099/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100100 * depends on hardware configuration:
101 *
102 * 1. dma address width 32 bits:
103 * word 1: 32 bit address of Data Buffer
104 * word 2: control
105 *
106 * 2. dma address width 64 bits:
107 * word 1: 32 bit address of Data Buffer
108 * word 2: control
109 * word 3: upper 32 bit address of Data Buffer
110 * word 4: unused
111 *
112 * 3. dma address width 32 bits with hardware timestamping:
113 * word 1: 32 bit address of Data Buffer
114 * word 2: control
115 * word 3: timestamp word 1
116 * word 4: timestamp word 2
117 *
118 * 4. dma address width 64 bits with hardware timestamping:
119 * word 1: 32 bit address of Data Buffer
120 * word 2: control
121 * word 3: upper 32 bit address of Data Buffer
122 * word 4: unused
123 * word 5: timestamp word 1
124 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000125 */
126static unsigned int macb_dma_desc_get_size(struct macb *bp)
127{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100128#ifdef MACB_EXT_DESC
129 unsigned int desc_size;
130
131 switch (bp->hw_dma_cap) {
132 case HW_DMA_CAP_64B:
133 desc_size = sizeof(struct macb_dma_desc)
134 + sizeof(struct macb_dma_desc_64);
135 break;
136 case HW_DMA_CAP_PTP:
137 desc_size = sizeof(struct macb_dma_desc)
138 + sizeof(struct macb_dma_desc_ptp);
139 break;
140 case HW_DMA_CAP_64B_PTP:
141 desc_size = sizeof(struct macb_dma_desc)
142 + sizeof(struct macb_dma_desc_64)
143 + sizeof(struct macb_dma_desc_ptp);
144 break;
145 default:
146 desc_size = sizeof(struct macb_dma_desc);
147 }
148 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
150 return sizeof(struct macb_dma_desc);
151}
152
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100153static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000154{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100155#ifdef MACB_EXT_DESC
156 switch (bp->hw_dma_cap) {
157 case HW_DMA_CAP_64B:
158 case HW_DMA_CAP_PTP:
159 desc_idx <<= 1;
160 break;
161 case HW_DMA_CAP_64B_PTP:
162 desc_idx *= 3;
163 break;
164 default:
165 break;
166 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000167#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100168 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000169}
170
171#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
172static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
173{
Shubhrajyoti Datta99dcb842019-09-23 14:03:51 +0530174 return (struct macb_dma_desc_64 *)((void *)desc
175 + sizeof(struct macb_dma_desc));
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000176}
177#endif
178
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500180static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181{
Zach Brownb410d132016-10-19 09:56:57 -0500182 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183}
184
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100185static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
186 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000187{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000188 index = macb_tx_ring_wrap(queue->bp, index);
189 index = macb_adj_dma_desc_idx(queue->bp, index);
190 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000191}
192
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100193static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
194 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195{
Zach Brownb410d132016-10-19 09:56:57 -0500196 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000197}
198
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100199static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000200{
201 dma_addr_t offset;
202
Zach Brownb410d132016-10-19 09:56:57 -0500203 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000204 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100206 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000207}
208
Zach Brownb410d132016-10-19 09:56:57 -0500209static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000210{
Zach Brownb410d132016-10-19 09:56:57 -0500211 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000212}
213
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000214static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000215{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000216 index = macb_rx_ring_wrap(queue->bp, index);
217 index = macb_adj_dma_desc_idx(queue->bp, index);
218 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000219}
220
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000221static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000222{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000223 return queue->rx_buffers + queue->bp->rx_buffer_size *
224 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000225}
226
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300227/* I/O accessors */
228static u32 hw_readl_native(struct macb *bp, int offset)
229{
230 return __raw_readl(bp->regs + offset);
231}
232
233static void hw_writel_native(struct macb *bp, int offset, u32 value)
234{
235 __raw_writel(value, bp->regs + offset);
236}
237
238static u32 hw_readl(struct macb *bp, int offset)
239{
240 return readl_relaxed(bp->regs + offset);
241}
242
243static void hw_writel(struct macb *bp, int offset, u32 value)
244{
245 writel_relaxed(value, bp->regs + offset);
246}
247
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700248/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700249 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300250 * descriptor access.
251 */
252static bool hw_is_native_io(void __iomem *addr)
253{
254 u32 value = MACB_BIT(LLB);
255
256 __raw_writel(value, addr + MACB_NCR);
257 value = __raw_readl(addr + MACB_NCR);
258
259 /* Write 0 back to disable everything */
260 __raw_writel(0, addr + MACB_NCR);
261
262 return value == MACB_BIT(LLB);
263}
264
265static bool hw_is_gem(void __iomem *addr, bool native_io)
266{
267 u32 id;
268
269 if (native_io)
270 id = __raw_readl(addr + MACB_MID);
271 else
272 id = readl_relaxed(addr + MACB_MID);
273
274 return MACB_BFEXT(IDNUM, id) >= 0x2;
275}
276
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100277static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100278{
279 u32 bottom;
280 u16 top;
281
282 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000283 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000285 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000286
287 /* Clear unused address register sets */
288 macb_or_gem_writel(bp, SA2B, 0);
289 macb_or_gem_writel(bp, SA2T, 0);
290 macb_or_gem_writel(bp, SA3B, 0);
291 macb_or_gem_writel(bp, SA3T, 0);
292 macb_or_gem_writel(bp, SA4B, 0);
293 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294}
295
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100296static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297{
298 u32 bottom;
299 u16 top;
300 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000301 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100302
Moritz Fischeraa50b552016-03-29 19:11:13 -0700303 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000304 for (i = 0; i < 4; i++) {
305 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
306 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100307
Nicolas Ferre8b952742019-05-03 12:36:58 +0200308 addr[0] = bottom & 0xff;
309 addr[1] = (bottom >> 8) & 0xff;
310 addr[2] = (bottom >> 16) & 0xff;
311 addr[3] = (bottom >> 24) & 0xff;
312 addr[4] = top & 0xff;
313 addr[5] = (top >> 8) & 0xff;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100314
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315 if (is_valid_ether_addr(addr)) {
316 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
317 return;
318 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700319 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000320
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300321 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000322 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323}
324
Harini Katakam8beb79b2019-03-01 16:20:32 +0530325static int macb_mdio_wait_for_idle(struct macb *bp)
326{
327 u32 val;
328
329 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
330 1, MACB_MDIO_TIMEOUT);
331}
332
frederic RODO6c36a702007-07-12 19:07:24 +0200333static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334{
frederic RODO6c36a702007-07-12 19:07:24 +0200335 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530336 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530337
Harini Katakamd54f89a2019-03-01 16:20:34 +0530338 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300339 if (status < 0) {
340 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530341 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300342 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530343
344 status = macb_mdio_wait_for_idle(bp);
345 if (status < 0)
346 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347
Milind Parab43ad3522020-01-09 08:36:46 +0000348 if (regnum & MII_ADDR_C45) {
349 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
350 | MACB_BF(RW, MACB_MAN_C45_ADDR)
351 | MACB_BF(PHYA, mii_id)
352 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
353 | MACB_BF(DATA, regnum & 0xFFFF)
354 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
355
356 status = macb_mdio_wait_for_idle(bp);
357 if (status < 0)
358 goto mdio_read_exit;
359
360 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
361 | MACB_BF(RW, MACB_MAN_C45_READ)
362 | MACB_BF(PHYA, mii_id)
363 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
364 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
365 } else {
366 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
367 | MACB_BF(RW, MACB_MAN_C22_READ)
368 | MACB_BF(PHYA, mii_id)
369 | MACB_BF(REGA, regnum)
370 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
371 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100372
Harini Katakamd54f89a2019-03-01 16:20:34 +0530373 status = macb_mdio_wait_for_idle(bp);
374 if (status < 0)
375 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100376
Harini Katakamd54f89a2019-03-01 16:20:34 +0530377 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100378
Harini Katakamd54f89a2019-03-01 16:20:34 +0530379mdio_read_exit:
380 pm_runtime_mark_last_busy(&bp->pdev->dev);
381 pm_runtime_put_autosuspend(&bp->pdev->dev);
382mdio_pm_exit:
383 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100384}
385
frederic RODO6c36a702007-07-12 19:07:24 +0200386static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
387 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100388{
frederic RODO6c36a702007-07-12 19:07:24 +0200389 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530390 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530391
Harini Katakamd54f89a2019-03-01 16:20:34 +0530392 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300393 if (status < 0) {
394 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530395 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300396 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530397
398 status = macb_mdio_wait_for_idle(bp);
399 if (status < 0)
400 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100401
Milind Parab43ad3522020-01-09 08:36:46 +0000402 if (regnum & MII_ADDR_C45) {
403 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
404 | MACB_BF(RW, MACB_MAN_C45_ADDR)
405 | MACB_BF(PHYA, mii_id)
406 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
407 | MACB_BF(DATA, regnum & 0xFFFF)
408 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
409
410 status = macb_mdio_wait_for_idle(bp);
411 if (status < 0)
412 goto mdio_write_exit;
413
414 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
415 | MACB_BF(RW, MACB_MAN_C45_WRITE)
416 | MACB_BF(PHYA, mii_id)
417 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
418 | MACB_BF(CODE, MACB_MAN_C45_CODE)
419 | MACB_BF(DATA, value)));
420 } else {
421 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
422 | MACB_BF(RW, MACB_MAN_C22_WRITE)
423 | MACB_BF(PHYA, mii_id)
424 | MACB_BF(REGA, regnum)
425 | MACB_BF(CODE, MACB_MAN_C22_CODE)
426 | MACB_BF(DATA, value)));
427 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100428
Harini Katakamd54f89a2019-03-01 16:20:34 +0530429 status = macb_mdio_wait_for_idle(bp);
430 if (status < 0)
431 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100432
Harini Katakamd54f89a2019-03-01 16:20:34 +0530433mdio_write_exit:
434 pm_runtime_mark_last_busy(&bp->pdev->dev);
435 pm_runtime_put_autosuspend(&bp->pdev->dev);
436mdio_pm_exit:
437 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438}
439
Antoine Tenart6e952d92019-11-13 10:00:05 +0100440static void macb_init_buffers(struct macb *bp)
441{
442 struct macb_queue *queue;
443 unsigned int q;
444
445 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
446 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
447#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
448 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
449 queue_writel(queue, RBQPH,
450 upper_32_bits(queue->rx_ring_dma));
451#endif
452 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
453#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
454 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
455 queue_writel(queue, TBQPH,
456 upper_32_bits(queue->tx_ring_dma));
457#endif
458 }
459}
460
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800461/**
462 * macb_set_tx_clk() - Set a clock to a new frequency
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200463 * @bp: pointer to struct macb
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -0700464 * @speed: New frequency in Hz
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800465 */
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200466static void macb_set_tx_clk(struct macb *bp, int speed)
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800467{
468 long ferr, rate, rate_rounded;
469
Charles Keepax1d0d5612021-01-04 10:38:02 +0000470 if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100471 return;
472
Michael Walle43e576312021-01-20 20:43:03 +0100473 /* In case of MII the PHY is the clock master */
474 if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
475 return;
476
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800477 switch (speed) {
478 case SPEED_10:
479 rate = 2500000;
480 break;
481 case SPEED_100:
482 rate = 25000000;
483 break;
484 case SPEED_1000:
485 rate = 125000000;
486 break;
487 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800488 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800489 }
490
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200491 rate_rounded = clk_round_rate(bp->tx_clk, rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800492 if (rate_rounded < 0)
493 return;
494
495 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
496 * is not satisfied.
497 */
498 ferr = abs(rate_rounded - rate);
499 ferr = DIV_ROUND_UP(ferr, rate / 100000);
500 if (ferr > 5)
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200501 netdev_warn(bp->dev,
502 "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700503 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800504
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200505 if (clk_set_rate(bp->tx_clk, rate_rounded))
506 netdev_err(bp->dev, "adjusting tx_clk failed.\n");
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800507}
508
Antoine Tenart7897b072019-11-13 10:00:06 +0100509static void macb_validate(struct phylink_config *config,
510 unsigned long *supported,
511 struct phylink_link_state *state)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100512{
Antoine Tenart7897b072019-11-13 10:00:06 +0100513 struct net_device *ndev = to_net_dev(config->dev);
514 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
515 struct macb *bp = netdev_priv(ndev);
516
517 /* We only support MII, RMII, GMII, RGMII & SGMII. */
518 if (state->interface != PHY_INTERFACE_MODE_NA &&
519 state->interface != PHY_INTERFACE_MODE_MII &&
520 state->interface != PHY_INTERFACE_MODE_RMII &&
521 state->interface != PHY_INTERFACE_MODE_GMII &&
522 state->interface != PHY_INTERFACE_MODE_SGMII &&
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100523 state->interface != PHY_INTERFACE_MODE_10GBASER &&
Antoine Tenart7897b072019-11-13 10:00:06 +0100524 !phy_interface_mode_is_rgmii(state->interface)) {
525 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
526 return;
527 }
528
529 if (!macb_is_gem(bp) &&
530 (state->interface == PHY_INTERFACE_MODE_GMII ||
531 phy_interface_mode_is_rgmii(state->interface))) {
532 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
533 return;
534 }
535
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100536 if (state->interface == PHY_INTERFACE_MODE_10GBASER &&
537 !(bp->caps & MACB_CAPS_HIGH_SPEED &&
538 bp->caps & MACB_CAPS_PCS)) {
539 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
540 return;
541 }
542
Antoine Tenart7897b072019-11-13 10:00:06 +0100543 phylink_set_port_modes(mask);
544 phylink_set(mask, Autoneg);
545 phylink_set(mask, Asym_Pause);
546
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100547 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
548 (state->interface == PHY_INTERFACE_MODE_NA ||
549 state->interface == PHY_INTERFACE_MODE_10GBASER)) {
550 phylink_set(mask, 10000baseCR_Full);
551 phylink_set(mask, 10000baseER_Full);
552 phylink_set(mask, 10000baseKR_Full);
553 phylink_set(mask, 10000baseLR_Full);
554 phylink_set(mask, 10000baseLRM_Full);
555 phylink_set(mask, 10000baseSR_Full);
556 phylink_set(mask, 10000baseT_Full);
557 if (state->interface != PHY_INTERFACE_MODE_NA)
558 goto out;
559 }
560
Antoine Tenart7897b072019-11-13 10:00:06 +0100561 phylink_set(mask, 10baseT_Half);
562 phylink_set(mask, 10baseT_Full);
563 phylink_set(mask, 100baseT_Half);
564 phylink_set(mask, 100baseT_Full);
565
566 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
567 (state->interface == PHY_INTERFACE_MODE_NA ||
568 state->interface == PHY_INTERFACE_MODE_GMII ||
569 state->interface == PHY_INTERFACE_MODE_SGMII ||
570 phy_interface_mode_is_rgmii(state->interface))) {
571 phylink_set(mask, 1000baseT_Full);
572 phylink_set(mask, 1000baseX_Full);
573
574 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
575 phylink_set(mask, 1000baseT_Half);
576 }
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100577out:
Antoine Tenart7897b072019-11-13 10:00:06 +0100578 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
579 bitmap_and(state->advertising, state->advertising, mask,
580 __ETHTOOL_LINK_MODE_MASK_NBITS);
581}
582
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100583static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
584 phy_interface_t interface, int speed,
585 int duplex)
586{
587 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
588 u32 config;
589
590 config = gem_readl(bp, USX_CONTROL);
591 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
592 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
593 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
594 config |= GEM_BIT(TX_EN);
595 gem_writel(bp, USX_CONTROL, config);
596}
597
598static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
Russell Kingd46b7e42019-11-21 00:36:22 +0000599 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100600{
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100601 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
602 u32 val;
603
604 state->speed = SPEED_10000;
605 state->duplex = 1;
606 state->an_complete = 1;
607
608 val = gem_readl(bp, USX_STATUS);
609 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
610 val = gem_readl(bp, NCFGR);
611 if (val & GEM_BIT(PAE))
612 state->pause = MLO_PAUSE_RX;
613}
614
615static int macb_usx_pcs_config(struct phylink_pcs *pcs,
616 unsigned int mode,
617 phy_interface_t interface,
618 const unsigned long *advertising,
619 bool permit_pause_to_mac)
620{
621 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
622
623 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
624 GEM_BIT(SIGNAL_OK));
625
626 return 0;
627}
628
629static void macb_pcs_get_state(struct phylink_pcs *pcs,
630 struct phylink_link_state *state)
631{
Russell Kingd46b7e42019-11-21 00:36:22 +0000632 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100633}
634
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100635static void macb_pcs_an_restart(struct phylink_pcs *pcs)
Antoine Tenart7897b072019-11-13 10:00:06 +0100636{
637 /* Not supported */
638}
639
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100640static int macb_pcs_config(struct phylink_pcs *pcs,
641 unsigned int mode,
642 phy_interface_t interface,
643 const unsigned long *advertising,
644 bool permit_pause_to_mac)
645{
646 return 0;
647}
648
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100649static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
650 .pcs_get_state = macb_usx_pcs_get_state,
651 .pcs_config = macb_usx_pcs_config,
652 .pcs_link_up = macb_usx_pcs_link_up,
653};
654
655static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
656 .pcs_get_state = macb_pcs_get_state,
657 .pcs_an_restart = macb_pcs_an_restart,
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100658 .pcs_config = macb_pcs_config,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100659};
660
Antoine Tenart7897b072019-11-13 10:00:06 +0100661static void macb_mac_config(struct phylink_config *config, unsigned int mode,
662 const struct phylink_link_state *state)
663{
664 struct net_device *ndev = to_net_dev(config->dev);
665 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200666 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100667 u32 old_ctrl, ctrl;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100668 u32 old_ncr, ncr;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100669
frederic RODO6c36a702007-07-12 19:07:24 +0200670 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100671
Antoine Tenart7897b072019-11-13 10:00:06 +0100672 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100673 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100675 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
676 if (state->interface == PHY_INTERFACE_MODE_RMII)
677 ctrl |= MACB_BIT(RM9200_RMII);
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200678 } else if (macb_is_gem(bp)) {
Russell King633e98a2020-02-26 10:24:06 +0000679 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100680 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100681
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100682 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100683 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100684 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
685 ctrl |= GEM_BIT(PCSSEL);
686 ncr |= GEM_BIT(ENABLE_HS_MAC);
687 }
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100688 }
frederic RODO6c36a702007-07-12 19:07:24 +0200689
Antoine Tenart7897b072019-11-13 10:00:06 +0100690 /* Apply the new configuration, if any */
691 if (old_ctrl ^ ctrl)
692 macb_or_gem_writel(bp, NCFGR, ctrl);
693
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100694 if (old_ncr ^ ncr)
695 macb_or_gem_writel(bp, NCR, ncr);
696
Robert Hancocke276e5e42021-03-11 14:18:13 -0600697 /* Disable AN for SGMII fixed link configuration, enable otherwise.
698 * Must be written after PCSSEL is set in NCFGR,
699 * otherwise writes will not take effect.
700 */
701 if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) {
702 u32 pcsctrl, old_pcsctrl;
703
704 old_pcsctrl = gem_readl(bp, PCSCNTRL);
705 if (mode == MLO_AN_FIXED)
706 pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG);
707 else
708 pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG);
709 if (old_pcsctrl != pcsctrl)
710 gem_writel(bp, PCSCNTRL, pcsctrl);
711 }
712
frederic RODO6c36a702007-07-12 19:07:24 +0200713 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200714}
715
Antoine Tenart7897b072019-11-13 10:00:06 +0100716static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
717 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200718{
Antoine Tenart7897b072019-11-13 10:00:06 +0100719 struct net_device *ndev = to_net_dev(config->dev);
720 struct macb *bp = netdev_priv(ndev);
721 struct macb_queue *queue;
722 unsigned int q;
723 u32 ctrl;
724
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100725 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
726 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
727 queue_writel(queue, IDR,
728 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100729
730 /* Disable Rx and Tx */
731 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
732 macb_writel(bp, NCR, ctrl);
733
734 netif_tx_stop_all_queues(ndev);
735}
736
Russell King91a208f2020-02-26 10:23:41 +0000737static void macb_mac_link_up(struct phylink_config *config,
738 struct phy_device *phy,
739 unsigned int mode, phy_interface_t interface,
740 int speed, int duplex,
741 bool tx_pause, bool rx_pause)
Antoine Tenart7897b072019-11-13 10:00:06 +0100742{
743 struct net_device *ndev = to_net_dev(config->dev);
744 struct macb *bp = netdev_priv(ndev);
745 struct macb_queue *queue;
Russell King633e98a2020-02-26 10:24:06 +0000746 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100747 unsigned int q;
Russell King633e98a2020-02-26 10:24:06 +0000748 u32 ctrl;
749
750 spin_lock_irqsave(&bp->lock, flags);
751
752 ctrl = macb_or_gem_readl(bp, NCFGR);
753
754 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
755
756 if (speed == SPEED_100)
757 ctrl |= MACB_BIT(SPD);
758
759 if (duplex)
760 ctrl |= MACB_BIT(FD);
Antoine Tenart7897b072019-11-13 10:00:06 +0100761
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100762 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200763 ctrl &= ~MACB_BIT(PAE);
764 if (macb_is_gem(bp)) {
765 ctrl &= ~GEM_BIT(GBE);
Russell King633e98a2020-02-26 10:24:06 +0000766
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200767 if (speed == SPEED_1000)
768 ctrl |= GEM_BIT(GBE);
769 }
Russell King633e98a2020-02-26 10:24:06 +0000770
Parshuram Thombared7739b02020-09-05 10:21:33 +0200771 if (rx_pause)
Russell King633e98a2020-02-26 10:24:06 +0000772 ctrl |= MACB_BIT(PAE);
773
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200774 macb_set_tx_clk(bp, speed);
Antoine Tenart7897b072019-11-13 10:00:06 +0100775
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100776 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
777 * cleared the pipeline and control registers.
778 */
779 bp->macbgem_ops.mog_init_rings(bp);
780 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100781
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100782 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
783 queue_writel(queue, IER,
784 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
785 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100786
Russell King633e98a2020-02-26 10:24:06 +0000787 macb_or_gem_writel(bp, NCFGR, ctrl);
788
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100789 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
790 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
791 gem_readl(bp, HS_MAC_CONFIG)));
792
Russell King633e98a2020-02-26 10:24:06 +0000793 spin_unlock_irqrestore(&bp->lock, flags);
794
Antoine Tenart7897b072019-11-13 10:00:06 +0100795 /* Enable Rx and Tx */
796 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
797
798 netif_tx_wake_all_queues(ndev);
799}
800
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100801static int macb_mac_prepare(struct phylink_config *config, unsigned int mode,
802 phy_interface_t interface)
803{
804 struct net_device *ndev = to_net_dev(config->dev);
805 struct macb *bp = netdev_priv(ndev);
806
807 if (interface == PHY_INTERFACE_MODE_10GBASER)
808 bp->phylink_pcs.ops = &macb_phylink_usx_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100809 else if (interface == PHY_INTERFACE_MODE_SGMII)
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100810 bp->phylink_pcs.ops = &macb_phylink_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100811 else
812 bp->phylink_pcs.ops = NULL;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100813
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100814 if (bp->phylink_pcs.ops)
815 phylink_set_pcs(bp->phylink, &bp->phylink_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100816
817 return 0;
818}
819
Antoine Tenart7897b072019-11-13 10:00:06 +0100820static const struct phylink_mac_ops macb_phylink_ops = {
821 .validate = macb_validate,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100822 .mac_prepare = macb_mac_prepare,
Antoine Tenart7897b072019-11-13 10:00:06 +0100823 .mac_config = macb_mac_config,
824 .mac_link_down = macb_mac_link_down,
825 .mac_link_up = macb_mac_link_up,
826};
827
Milind Parabfd2a8912020-01-13 03:30:43 +0000828static bool macb_phy_handle_exists(struct device_node *dn)
829{
830 dn = of_parse_phandle(dn, "phy-handle", 0);
831 of_node_put(dn);
832 return dn != NULL;
833}
834
Antoine Tenart7897b072019-11-13 10:00:06 +0100835static int macb_phylink_connect(struct macb *bp)
836{
Milind Parabfd2a8912020-01-13 03:30:43 +0000837 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100838 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000839 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100840 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500841
Milind Parabfd2a8912020-01-13 03:30:43 +0000842 if (dn)
843 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
844
845 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200846 phydev = phy_find_first(bp->mii_bus);
847 if (!phydev) {
848 netdev_err(dev, "no PHY found\n");
849 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000850 }
frederic RODO6c36a702007-07-12 19:07:24 +0200851
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200852 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100853 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000854 }
855
856 if (ret) {
857 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
858 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200859 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100860
Antoine Tenart7897b072019-11-13 10:00:06 +0100861 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100862
Antoine Tenart7897b072019-11-13 10:00:06 +0100863 return 0;
864}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100865
Robert Hancock8fab1742021-03-11 14:18:12 -0600866static void macb_get_pcs_fixed_state(struct phylink_config *config,
867 struct phylink_link_state *state)
868{
869 struct net_device *ndev = to_net_dev(config->dev);
870 struct macb *bp = netdev_priv(ndev);
871
872 state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
873}
874
Antoine Tenart7897b072019-11-13 10:00:06 +0100875/* based on au1000_eth. c*/
876static int macb_mii_probe(struct net_device *dev)
877{
878 struct macb *bp = netdev_priv(dev);
879
880 bp->phylink_config.dev = &dev->dev;
881 bp->phylink_config.type = PHYLINK_NETDEV;
882
Robert Hancock8fab1742021-03-11 14:18:12 -0600883 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
884 bp->phylink_config.poll_fixed_state = true;
885 bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
886 }
887
Antoine Tenart7897b072019-11-13 10:00:06 +0100888 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
889 bp->phy_interface, &macb_phylink_ops);
890 if (IS_ERR(bp->phylink)) {
891 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
892 PTR_ERR(bp->phylink));
893 return PTR_ERR(bp->phylink);
894 }
frederic RODO6c36a702007-07-12 19:07:24 +0200895
896 return 0;
897}
898
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100899static int macb_mdiobus_register(struct macb *bp)
900{
901 struct device_node *child, *np = bp->pdev->dev.of_node;
902
Codrin Ciubotariu79540d12020-03-31 12:39:35 +0300903 if (of_phy_is_fixed_link(np))
904 return mdiobus_register(bp->mii_bus);
905
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100906 /* Only create the PHY from the device tree if at least one PHY is
907 * described. Otherwise scan the entire MDIO bus. We do this to support
908 * old device tree that did not follow the best practices and did not
909 * describe their network PHYs.
910 */
911 for_each_available_child_of_node(np, child)
912 if (of_mdiobus_child_is_phy(child)) {
913 /* The loop increments the child refcount,
914 * decrement it before returning.
915 */
916 of_node_put(child);
917
918 return of_mdiobus_register(bp->mii_bus, np);
919 }
920
921 return mdiobus_register(bp->mii_bus);
922}
923
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100924static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200925{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200926 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200927
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200928 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200929 macb_writel(bp, NCR, MACB_BIT(MPE));
930
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700931 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700932 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200933 err = -ENOMEM;
934 goto err_out;
935 }
936
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700937 bp->mii_bus->name = "MACB_mii_bus";
938 bp->mii_bus->read = &macb_mdio_read;
939 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000940 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700941 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700942 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700943 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700944
Jamie Iles91523942011-02-28 04:05:25 +0000945 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200946
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100947 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200948 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100949 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200950
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200951 err = macb_mii_probe(bp->dev);
952 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200953 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200954
955 return 0;
956
957err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700958 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500959err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700960 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200961err_out:
962 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100963}
964
965static void macb_update_stats(struct macb *bp)
966{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000967 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
968 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300969 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100970
971 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
972
Moritz Fischer96ec6312016-03-29 19:11:11 -0700973 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700974 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100975}
976
Nicolas Ferree86cd532012-10-31 06:04:57 +0000977static int macb_halt_tx(struct macb *bp)
978{
979 unsigned long halt_time, timeout;
980 u32 status;
981
982 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
983
984 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
985 do {
986 halt_time = jiffies;
987 status = macb_readl(bp, TSR);
988 if (!(status & MACB_BIT(TGO)))
989 return 0;
990
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800991 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000992 } while (time_before(halt_time, timeout));
993
994 return -ETIMEDOUT;
995}
996
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200997static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
998{
999 if (tx_skb->mapping) {
1000 if (tx_skb->mapped_as_page)
1001 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
1002 tx_skb->size, DMA_TO_DEVICE);
1003 else
1004 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
1005 tx_skb->size, DMA_TO_DEVICE);
1006 tx_skb->mapping = 0;
1007 }
1008
1009 if (tx_skb->skb) {
1010 dev_kfree_skb_any(tx_skb->skb);
1011 tx_skb->skb = NULL;
1012 }
1013}
1014
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001015static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +05301016{
Harini Katakamfff80192016-08-09 13:15:53 +05301017#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001018 struct macb_dma_desc_64 *desc_64;
1019
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001020 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001021 desc_64 = macb_64b_desc(bp, desc);
1022 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +02001023 /* The low bits of RX address contain the RX_USED bit, clearing
1024 * of which allows packet RX. Make sure the high bits are also
1025 * visible to HW at that point.
1026 */
1027 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001028 }
Harini Katakamfff80192016-08-09 13:15:53 +05301029#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001030 desc->addr = lower_32_bits(addr);
1031}
1032
1033static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1034{
1035 dma_addr_t addr = 0;
1036#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1037 struct macb_dma_desc_64 *desc_64;
1038
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001039 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001040 desc_64 = macb_64b_desc(bp, desc);
1041 addr = ((u64)(desc_64->addrh) << 32);
1042 }
1043#endif
1044 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1045 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +05301046}
1047
Nicolas Ferree86cd532012-10-31 06:04:57 +00001048static void macb_tx_error_task(struct work_struct *work)
1049{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001050 struct macb_queue *queue = container_of(work, struct macb_queue,
1051 tx_error_task);
1052 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001053 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001054 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001055 struct sk_buff *skb;
1056 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001057 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001058
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001059 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
1060 (unsigned int)(queue - bp->queues),
1061 queue->tx_tail, queue->tx_head);
1062
1063 /* Prevent the queue IRQ handlers from running: each of them may call
1064 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
1065 * As explained below, we have to halt the transmission before updating
1066 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1067 * network engine about the macb/gem being halted.
1068 */
1069 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001070
1071 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001072 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001073
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001074 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +00001075 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001076 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +00001077 */
1078 if (macb_halt_tx(bp))
1079 /* Just complain for now, reinitializing TX path can be good */
1080 netdev_err(bp->dev, "BUG: halt tx timed out\n");
1081
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001082 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001083 * Free transmit buffers in upper layer.
1084 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001085 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1086 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001087
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001088 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001089 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001090 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001091 skb = tx_skb->skb;
1092
1093 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001094 /* skb is set for the last buffer of the frame */
1095 while (!skb) {
1096 macb_tx_unmap(bp, tx_skb);
1097 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001098 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001099 skb = tx_skb->skb;
1100 }
1101
1102 /* ctrl still refers to the first buffer descriptor
1103 * since it's the only one written back by the hardware
1104 */
1105 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1106 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001107 macb_tx_ring_wrap(bp, tail),
1108 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001109 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001110 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001111 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001112 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001113 }
Nicolas Ferree86cd532012-10-31 06:04:57 +00001114 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001115 /* "Buffers exhausted mid-frame" errors may only happen
1116 * if the driver is buggy, so complain loudly about
1117 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001118 */
1119 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1120 netdev_err(bp->dev,
1121 "BUG: TX buffers exhausted mid-frame\n");
1122
1123 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1124 }
1125
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001126 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001127 }
1128
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001129 /* Set end of TX queue */
1130 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001131 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001132 desc->ctrl = MACB_BIT(TX_USED);
1133
Nicolas Ferree86cd532012-10-31 06:04:57 +00001134 /* Make descriptor updates visible to hardware */
1135 wmb();
1136
1137 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001138 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301139#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001140 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001141 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301142#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +00001143 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001144 queue->tx_head = 0;
1145 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001146
1147 /* Housework before enabling TX IRQ */
1148 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001149 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1150
1151 /* Now we are ready to start transmission again */
1152 netif_tx_start_all_queues(bp->dev);
1153 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1154
1155 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001156}
1157
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001158static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001159{
1160 unsigned int tail;
1161 unsigned int head;
1162 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001163 struct macb *bp = queue->bp;
1164 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001165
1166 status = macb_readl(bp, TSR);
1167 macb_writel(bp, TSR, status);
1168
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001169 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001170 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +00001171
Nicolas Ferree86cd532012-10-31 06:04:57 +00001172 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001173 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001174
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001175 head = queue->tx_head;
1176 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001177 struct macb_tx_skb *tx_skb;
1178 struct sk_buff *skb;
1179 struct macb_dma_desc *desc;
1180 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001181
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001182 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001183
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001184 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001185 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001186
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001187 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001188
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001189 /* TX_USED bit is only set by hardware on the very first buffer
1190 * descriptor of the transmitted frame.
1191 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001192 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001193 break;
1194
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001195 /* Process all buffers of the current transmitted frame */
1196 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001197 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001198 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001199
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001200 /* First, update TX stats if needed */
1201 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001202 if (unlikely(skb_shinfo(skb)->tx_flags &
1203 SKBTX_HW_TSTAMP) &&
1204 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001205 /* skb now belongs to timestamp buffer
1206 * and will be removed later
1207 */
1208 tx_skb->skb = NULL;
1209 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001210 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001211 macb_tx_ring_wrap(bp, tail),
1212 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001213 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001214 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001215 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001216 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001217 }
1218
1219 /* Now we can safely release resources */
1220 macb_tx_unmap(bp, tx_skb);
1221
1222 /* skb is set only for the last buffer of the frame.
1223 * WARNING: at this point skb has been freed by
1224 * macb_tx_unmap().
1225 */
1226 if (skb)
1227 break;
1228 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229 }
1230
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001231 queue->tx_tail = tail;
1232 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1233 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001234 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001235 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236}
1237
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001238static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001239{
1240 unsigned int entry;
1241 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001242 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001243 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001244 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001245
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001246 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1247 bp->rx_ring_size) > 0) {
1248 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001249
1250 /* Make hw descriptor updates visible to CPU */
1251 rmb();
1252
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001253 queue->rx_prepared_head++;
1254 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001255
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001256 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001257 /* allocate sk_buff for this free entry in ring */
1258 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001259 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001260 netdev_err(bp->dev,
1261 "Unable to allocate sk_buff\n");
1262 break;
1263 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001264
1265 /* now fill corresponding descriptor entry */
1266 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001267 bp->rx_buffer_size,
1268 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001269 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1270 dev_kfree_skb(skb);
1271 break;
1272 }
1273
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001274 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001275
Zach Brownb410d132016-10-19 09:56:57 -05001276 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001277 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001278 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001279 /* Setting addr clears RX_USED and allows reception,
1280 * make sure ctrl is cleared first to avoid a race.
1281 */
1282 dma_wmb();
1283 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001284
1285 /* properly align Ethernet header */
1286 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301287 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001288 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001289 dma_wmb();
1290 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001291 }
1292 }
1293
1294 /* Make descriptor updates visible to hardware */
1295 wmb();
1296
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001297 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1298 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001299}
1300
1301/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001302static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001303 unsigned int end)
1304{
1305 unsigned int frag;
1306
1307 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001308 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001309
Nicolas Ferre4df95132013-06-04 21:57:12 +00001310 desc->addr &= ~MACB_BIT(RX_USED);
1311 }
1312
1313 /* Make descriptor updates visible to hardware */
1314 wmb();
1315
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001316 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001317 * whatever caused this is updated, so we don't have to record
1318 * anything.
1319 */
1320}
1321
Antoine Tenart97236cd2019-06-21 17:30:02 +02001322static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1323 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001324{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001325 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001326 unsigned int len;
1327 unsigned int entry;
1328 struct sk_buff *skb;
1329 struct macb_dma_desc *desc;
1330 int count = 0;
1331
1332 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301333 u32 ctrl;
1334 dma_addr_t addr;
1335 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001336
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001337 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1338 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001339
1340 /* Make hw descriptor updates visible to CPU */
1341 rmb();
1342
Harini Katakamfff80192016-08-09 13:15:53 +05301343 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001344 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001345
Harini Katakamfff80192016-08-09 13:15:53 +05301346 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001347 break;
1348
Anssi Hannula6e0af292018-12-17 15:05:41 +02001349 /* Ensure ctrl is at least as up-to-date as rxused */
1350 dma_rmb();
1351
1352 ctrl = desc->ctrl;
1353
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001354 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001355 count++;
1356
1357 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1358 netdev_err(bp->dev,
1359 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001360 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001361 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001362 break;
1363 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001364 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001365 if (unlikely(!skb)) {
1366 netdev_err(bp->dev,
1367 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001368 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001369 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001370 break;
1371 }
1372 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001373 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301374 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001375
1376 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1377
1378 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001379 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001380 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001381
1382 skb->protocol = eth_type_trans(skb, bp->dev);
1383 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001384 if (bp->dev->features & NETIF_F_RXCSUM &&
1385 !(bp->dev->flags & IFF_PROMISC) &&
1386 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1387 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001388
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001389 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001390 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001391 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001392 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001393
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001394 gem_ptp_do_rxstamp(bp, skb, desc);
1395
Nicolas Ferre4df95132013-06-04 21:57:12 +00001396#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1397 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1398 skb->len, skb->csum);
1399 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001400 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001401 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1402 skb->data, 32, true);
1403#endif
1404
Antoine Tenart97236cd2019-06-21 17:30:02 +02001405 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001406 }
1407
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001408 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001409
1410 return count;
1411}
1412
Antoine Tenart97236cd2019-06-21 17:30:02 +02001413static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1414 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001415{
1416 unsigned int len;
1417 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001418 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001419 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001420 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001421 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001422
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001423 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301424 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001425
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001426 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001427 macb_rx_ring_wrap(bp, first_frag),
1428 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001430 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001431 * first buffer. Since the header is 14 bytes, this makes the
1432 * payload word-aligned.
1433 *
1434 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1435 * the two padding bytes into the skb so that we avoid hitting
1436 * the slowpath in memcpy(), and pull them off afterwards.
1437 */
1438 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001439 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001440 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001441 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001442 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001443 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001444 if (frag == last_frag)
1445 break;
1446 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001447
1448 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001449 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001450
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001451 return 1;
1452 }
1453
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001454 offset = 0;
1455 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001456 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001457 skb_put(skb, len);
1458
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001459 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001460 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001461
1462 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001463 if (unlikely(frag != last_frag)) {
1464 dev_kfree_skb_any(skb);
1465 return -1;
1466 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001467 frag_len = len - offset;
1468 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001469 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001470 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001471 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001472 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001473 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001474 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001475
1476 if (frag == last_frag)
1477 break;
1478 }
1479
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001480 /* Make descriptor updates visible to hardware */
1481 wmb();
1482
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001483 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001484 skb->protocol = eth_type_trans(skb, bp->dev);
1485
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001486 bp->dev->stats.rx_packets++;
1487 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001488 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001489 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001490 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001491
1492 return 0;
1493}
1494
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001495static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001496{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001497 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001498 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001499 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001500 int i;
1501
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001502 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001503 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001504 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001505 macb_set_addr(bp, desc, addr);
1506 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001507 addr += bp->rx_buffer_size;
1508 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001509 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001510 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001511}
1512
Antoine Tenart97236cd2019-06-21 17:30:02 +02001513static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1514 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001515{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001516 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001517 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001518 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001519 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001520 int first_frag = -1;
1521
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001522 for (tail = queue->rx_tail; budget > 0; tail++) {
1523 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001524 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001525
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001526 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001527 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001528
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001529 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001530 break;
1531
Anssi Hannula6e0af292018-12-17 15:05:41 +02001532 /* Ensure ctrl is at least as up-to-date as addr */
1533 dma_rmb();
1534
1535 ctrl = desc->ctrl;
1536
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001537 if (ctrl & MACB_BIT(RX_SOF)) {
1538 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001539 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001540 first_frag = tail;
1541 }
1542
1543 if (ctrl & MACB_BIT(RX_EOF)) {
1544 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001545
1546 if (unlikely(first_frag == -1)) {
1547 reset_rx_queue = true;
1548 continue;
1549 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001550
Antoine Tenart97236cd2019-06-21 17:30:02 +02001551 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001552 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001553 if (unlikely(dropped < 0)) {
1554 reset_rx_queue = true;
1555 continue;
1556 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001557 if (!dropped) {
1558 received++;
1559 budget--;
1560 }
1561 }
1562 }
1563
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001564 if (unlikely(reset_rx_queue)) {
1565 unsigned long flags;
1566 u32 ctrl;
1567
1568 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1569
1570 spin_lock_irqsave(&bp->lock, flags);
1571
1572 ctrl = macb_readl(bp, NCR);
1573 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1574
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001575 macb_init_rx_ring(queue);
1576 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001577
1578 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1579
1580 spin_unlock_irqrestore(&bp->lock, flags);
1581 return received;
1582 }
1583
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001584 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001585 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001586 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001587 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001588
1589 return received;
1590}
1591
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001592static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001593{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001594 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1595 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001596 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001597 u32 status;
1598
1599 status = macb_readl(bp, RSR);
1600 macb_writel(bp, RSR, status);
1601
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001602 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001603 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001604
Antoine Tenart97236cd2019-06-21 17:30:02 +02001605 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001606 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001607 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001608
Nicolas Ferre8770e912013-02-12 11:08:48 +01001609 /* Packets received while interrupts were disabled */
1610 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001611 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001612 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001613 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001614 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001615 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301616 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001617 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001618 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001619
1620 /* TODO: Handle errors */
1621
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001622 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001623}
1624
Allen Paise7412b82020-09-14 12:59:23 +05301625static void macb_hresp_error_task(struct tasklet_struct *t)
Harini Katakam032dc412018-01-27 12:09:01 +05301626{
Allen Paise7412b82020-09-14 12:59:23 +05301627 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
Harini Katakam032dc412018-01-27 12:09:01 +05301628 struct net_device *dev = bp->dev;
Claudiu Beznea580d3952020-07-02 12:06:00 +03001629 struct macb_queue *queue;
Harini Katakam032dc412018-01-27 12:09:01 +05301630 unsigned int q;
1631 u32 ctrl;
1632
1633 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301634 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301635 MACB_TX_INT_FLAGS |
1636 MACB_BIT(HRESP));
1637 }
1638 ctrl = macb_readl(bp, NCR);
1639 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1640 macb_writel(bp, NCR, ctrl);
1641
1642 netif_tx_stop_all_queues(dev);
1643 netif_carrier_off(dev);
1644
1645 bp->macbgem_ops.mog_init_rings(bp);
1646
1647 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001648 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301649
Antoine Tenart6e952d92019-11-13 10:00:05 +01001650 /* Enable interrupts */
1651 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301652 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301653 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301654 MACB_TX_INT_FLAGS |
1655 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301656
1657 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1658 macb_writel(bp, NCR, ctrl);
1659
1660 netif_carrier_on(dev);
1661 netif_tx_start_all_queues(dev);
1662}
1663
Claudiu Beznea42983882018-12-17 10:02:42 +00001664static void macb_tx_restart(struct macb_queue *queue)
1665{
1666 unsigned int head = queue->tx_head;
1667 unsigned int tail = queue->tx_tail;
1668 struct macb *bp = queue->bp;
1669
1670 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1671 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1672
1673 if (head == tail)
1674 return;
1675
1676 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1677}
1678
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02001679static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
1680{
1681 struct macb_queue *queue = dev_id;
1682 struct macb *bp = queue->bp;
1683 u32 status;
1684
1685 status = queue_readl(queue, ISR);
1686
1687 if (unlikely(!status))
1688 return IRQ_NONE;
1689
1690 spin_lock(&bp->lock);
1691
1692 if (status & MACB_BIT(WOL)) {
1693 queue_writel(queue, IDR, MACB_BIT(WOL));
1694 macb_writel(bp, WOL, 0);
1695 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
1696 (unsigned int)(queue - bp->queues),
1697 (unsigned long)status);
1698 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1699 queue_writel(queue, ISR, MACB_BIT(WOL));
1700 pm_wakeup_event(&bp->pdev->dev, 0);
1701 }
1702
1703 spin_unlock(&bp->lock);
1704
1705 return IRQ_HANDLED;
1706}
1707
Nicolas Ferre558e35c2020-07-20 10:56:52 +02001708static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
1709{
1710 struct macb_queue *queue = dev_id;
1711 struct macb *bp = queue->bp;
1712 u32 status;
1713
1714 status = queue_readl(queue, ISR);
1715
1716 if (unlikely(!status))
1717 return IRQ_NONE;
1718
1719 spin_lock(&bp->lock);
1720
1721 if (status & GEM_BIT(WOL)) {
1722 queue_writel(queue, IDR, GEM_BIT(WOL));
1723 gem_writel(bp, WOL, 0);
1724 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
1725 (unsigned int)(queue - bp->queues),
1726 (unsigned long)status);
1727 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1728 queue_writel(queue, ISR, GEM_BIT(WOL));
1729 pm_wakeup_event(&bp->pdev->dev, 0);
1730 }
1731
1732 spin_unlock(&bp->lock);
1733
1734 return IRQ_HANDLED;
1735}
1736
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001737static irqreturn_t macb_interrupt(int irq, void *dev_id)
1738{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001739 struct macb_queue *queue = dev_id;
1740 struct macb *bp = queue->bp;
1741 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001742 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001743
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001744 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745
1746 if (unlikely(!status))
1747 return IRQ_NONE;
1748
1749 spin_lock(&bp->lock);
1750
1751 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001752 /* close possible race with dev_close */
1753 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001754 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001755 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1756 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001757 break;
1758 }
1759
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001760 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1761 (unsigned int)(queue - bp->queues),
1762 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001763
Harini Katakame5010702019-01-29 15:20:03 +05301764 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001765 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001766 * until we have processed the buffers. The
1767 * scheduling call may fail if the poll routine
1768 * is already scheduled, so disable interrupts
1769 * now.
1770 */
Harini Katakame5010702019-01-29 15:20:03 +05301771 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001772 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001773 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001774
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001775 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001776 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001777 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001778 }
1779 }
1780
Nicolas Ferree86cd532012-10-31 06:04:57 +00001781 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001782 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1783 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001784
1785 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001786 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001787
Nicolas Ferree86cd532012-10-31 06:04:57 +00001788 break;
1789 }
1790
1791 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001792 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001793
Claudiu Beznea42983882018-12-17 10:02:42 +00001794 if (status & MACB_BIT(TXUBR))
1795 macb_tx_restart(queue);
1796
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001797 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001798 * add that if/when we get our hands on a full-blown MII PHY.
1799 */
1800
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001801 /* There is a hardware issue under heavy load where DMA can
1802 * stop, this causes endless "used buffer descriptor read"
1803 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301804 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1805 * section 16.7.4 for details. RXUBR is only enabled for
1806 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001807 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001808 if (status & MACB_BIT(RXUBR)) {
1809 ctrl = macb_readl(bp, NCR);
1810 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001811 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001812 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1813
1814 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001815 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001816 }
1817
Alexander Steinb19f7f72011-04-13 05:03:24 +00001818 if (status & MACB_BIT(ISR_ROVR)) {
1819 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001820 if (macb_is_gem(bp))
1821 bp->hw_stats.gem.rx_overruns++;
1822 else
1823 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001824
1825 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001826 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001827 }
1828
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001829 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301830 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001831 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001832
1833 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001834 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001835 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001836 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001837 }
1838
1839 spin_unlock(&bp->lock);
1840
1841 return IRQ_HANDLED;
1842}
1843
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001844#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001845/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001846 * to allow network i/o with interrupts disabled.
1847 */
1848static void macb_poll_controller(struct net_device *dev)
1849{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001850 struct macb *bp = netdev_priv(dev);
1851 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001852 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001853 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001854
1855 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001856 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1857 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001858 local_irq_restore(flags);
1859}
1860#endif
1861
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001862static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001863 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001864 struct sk_buff *skb,
1865 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001866{
1867 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001868 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001869 struct macb_tx_skb *tx_skb = NULL;
1870 struct macb_dma_desc *desc;
1871 unsigned int offset, size, count = 0;
1872 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001873 unsigned int eof = 1, mss_mfs = 0;
1874 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1875
1876 /* LSO */
1877 if (skb_shinfo(skb)->gso_size != 0) {
1878 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1879 /* UDP - UFO */
1880 lso_ctrl = MACB_LSO_UFO_ENABLE;
1881 else
1882 /* TCP - TSO */
1883 lso_ctrl = MACB_LSO_TSO_ENABLE;
1884 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001885
1886 /* First, map non-paged data */
1887 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001888
1889 /* first buffer length */
1890 size = hdrlen;
1891
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001892 offset = 0;
1893 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001894 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001895 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001896
1897 mapping = dma_map_single(&bp->pdev->dev,
1898 skb->data + offset,
1899 size, DMA_TO_DEVICE);
1900 if (dma_mapping_error(&bp->pdev->dev, mapping))
1901 goto dma_error;
1902
1903 /* Save info to properly release resources */
1904 tx_skb->skb = NULL;
1905 tx_skb->mapping = mapping;
1906 tx_skb->size = size;
1907 tx_skb->mapped_as_page = false;
1908
1909 len -= size;
1910 offset += size;
1911 count++;
1912 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001913
1914 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001915 }
1916
1917 /* Then, map paged data from fragments */
1918 for (f = 0; f < nr_frags; f++) {
1919 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1920
1921 len = skb_frag_size(frag);
1922 offset = 0;
1923 while (len) {
1924 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001925 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001926 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001927
1928 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1929 offset, size, DMA_TO_DEVICE);
1930 if (dma_mapping_error(&bp->pdev->dev, mapping))
1931 goto dma_error;
1932
1933 /* Save info to properly release resources */
1934 tx_skb->skb = NULL;
1935 tx_skb->mapping = mapping;
1936 tx_skb->size = size;
1937 tx_skb->mapped_as_page = true;
1938
1939 len -= size;
1940 offset += size;
1941 count++;
1942 tx_head++;
1943 }
1944 }
1945
1946 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001947 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001948 netdev_err(bp->dev, "BUG! empty skb!\n");
1949 return 0;
1950 }
1951
1952 /* This is the last buffer of the frame: save socket buffer */
1953 tx_skb->skb = skb;
1954
1955 /* Update TX ring: update buffer descriptors in reverse order
1956 * to avoid race condition
1957 */
1958
1959 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1960 * to set the end of TX queue
1961 */
1962 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001963 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001964 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001965 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001966 desc->ctrl = ctrl;
1967
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001968 if (lso_ctrl) {
1969 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1970 /* include header and FCS in value given to h/w */
1971 mss_mfs = skb_shinfo(skb)->gso_size +
1972 skb_transport_offset(skb) +
1973 ETH_FCS_LEN;
1974 else /* TSO */ {
1975 mss_mfs = skb_shinfo(skb)->gso_size;
1976 /* TCP Sequence Number Source Select
1977 * can be set only for TSO
1978 */
1979 seq_ctrl = 0;
1980 }
1981 }
1982
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001983 do {
1984 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001985 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001986 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001987 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001988
1989 ctrl = (u32)tx_skb->size;
1990 if (eof) {
1991 ctrl |= MACB_BIT(TX_LAST);
1992 eof = 0;
1993 }
Zach Brownb410d132016-10-19 09:56:57 -05001994 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001995 ctrl |= MACB_BIT(TX_WRAP);
1996
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001997 /* First descriptor is header descriptor */
1998 if (i == queue->tx_head) {
1999 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
2000 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002001 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
2002 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
2003 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002004 } else
2005 /* Only set MSS/MFS on payload descriptors
2006 * (second or later descriptor)
2007 */
2008 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
2009
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002010 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002011 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002012 /* desc->addr must be visible to hardware before clearing
2013 * 'TX_USED' bit in desc->ctrl.
2014 */
2015 wmb();
2016 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002017 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002018
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002019 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002020
2021 return count;
2022
2023dma_error:
2024 netdev_err(bp->dev, "TX DMA map failed\n");
2025
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002026 for (i = queue->tx_head; i != tx_head; i++) {
2027 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002028
2029 macb_tx_unmap(bp, tx_skb);
2030 }
2031
2032 return 0;
2033}
2034
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002035static netdev_features_t macb_features_check(struct sk_buff *skb,
2036 struct net_device *dev,
2037 netdev_features_t features)
2038{
2039 unsigned int nr_frags, f;
2040 unsigned int hdrlen;
2041
2042 /* Validate LSO compatibility */
2043
Harini Katakam41c1ef92020-02-05 18:08:11 +05302044 /* there is only one buffer or protocol is not UDP */
2045 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002046 return features;
2047
2048 /* length of header */
2049 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002050
Harini Katakam41c1ef92020-02-05 18:08:11 +05302051 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002052 * When software supplies two or more payload buffers all payload buffers
2053 * apart from the last must be a multiple of 8 bytes in size.
2054 */
2055 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2056 return features & ~MACB_NETIF_LSO;
2057
2058 nr_frags = skb_shinfo(skb)->nr_frags;
2059 /* No need to check last fragment */
2060 nr_frags--;
2061 for (f = 0; f < nr_frags; f++) {
2062 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2063
2064 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2065 return features & ~MACB_NETIF_LSO;
2066 }
2067 return features;
2068}
2069
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02002070static inline int macb_clear_csum(struct sk_buff *skb)
2071{
2072 /* no change for packets without checksum offloading */
2073 if (skb->ip_summed != CHECKSUM_PARTIAL)
2074 return 0;
2075
2076 /* make sure we can modify the header */
2077 if (unlikely(skb_cow_head(skb, 0)))
2078 return -1;
2079
2080 /* initialize checksum field
2081 * This is required - at least for Zynq, which otherwise calculates
2082 * wrong UDP header checksums for UDP packets with UDP data len <=2
2083 */
2084 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2085 return 0;
2086}
2087
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002088static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
2089{
Mark Deneen403dc162020-10-30 15:58:14 +00002090 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2091 skb_is_nonlinear(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002092 int padlen = ETH_ZLEN - (*skb)->len;
2093 int headroom = skb_headroom(*skb);
2094 int tailroom = skb_tailroom(*skb);
2095 struct sk_buff *nskb;
2096 u32 fcs;
2097
2098 if (!(ndev->features & NETIF_F_HW_CSUM) ||
2099 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2100 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
2101 return 0;
2102
2103 if (padlen <= 0) {
2104 /* FCS could be appeded to tailroom. */
2105 if (tailroom >= ETH_FCS_LEN)
2106 goto add_fcs;
2107 /* FCS could be appeded by moving data to headroom. */
2108 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
2109 padlen = 0;
2110 /* No room for FCS, need to reallocate skb. */
2111 else
Tristram Ha899ecae2018-10-24 14:51:23 -07002112 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002113 } else {
2114 /* Add room for FCS. */
2115 padlen += ETH_FCS_LEN;
2116 }
2117
2118 if (!cloned && headroom + tailroom >= padlen) {
2119 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
2120 skb_set_tail_pointer(*skb, (*skb)->len);
2121 } else {
2122 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2123 if (!nskb)
2124 return -ENOMEM;
2125
Huang Zijiangf3e5c072019-02-14 14:41:18 +08002126 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002127 *skb = nskb;
2128 }
2129
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00002130 if (padlen > ETH_FCS_LEN)
2131 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002132
2133add_fcs:
2134 /* set FCS to packet */
2135 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2136 fcs = ~fcs;
2137
2138 skb_put_u8(*skb, fcs & 0xff);
2139 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2140 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2141 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2142
2143 return 0;
2144}
2145
Claudiu Beznead1c38952018-08-07 12:25:12 +03002146static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002147{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002148 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002149 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002150 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07002151 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002152 unsigned int desc_cnt, nr_frags, frag_size, f;
2153 unsigned int hdrlen;
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002154 bool is_lso;
Claudiu Beznead1c38952018-08-07 12:25:12 +03002155 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002156
Claudiu Beznea33729f22018-08-07 12:25:13 +03002157 if (macb_clear_csum(skb)) {
2158 dev_kfree_skb_any(skb);
2159 return ret;
2160 }
2161
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002162 if (macb_pad_and_fcs(&skb, dev)) {
2163 dev_kfree_skb_any(skb);
2164 return ret;
2165 }
2166
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002167 is_lso = (skb_shinfo(skb)->gso_size != 0);
2168
2169 if (is_lso) {
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002170 /* length of headers */
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002171 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002172 /* only queue eth + ip headers separately for UDP */
2173 hdrlen = skb_transport_offset(skb);
2174 else
2175 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
2176 if (skb_headlen(skb) < hdrlen) {
2177 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
2178 /* if this is required, would need to copy to single buffer */
2179 return NETDEV_TX_BUSY;
2180 }
2181 } else
2182 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002183
Havard Skinnemoena268adb2012-10-31 06:04:52 +00002184#if defined(DEBUG) && defined(VERBOSE_DEBUG)
2185 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002186 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
2187 queue_index, skb->len, skb->head, skb->data,
2188 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002189 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2190 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002191#endif
2192
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002193 /* Count how many TX buffer descriptors are needed to send this
2194 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07002195 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002196 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002197 if (is_lso && (skb_headlen(skb) > hdrlen))
2198 /* extra header descriptor if also payload in first buffer */
2199 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2200 else
2201 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002202 nr_frags = skb_shinfo(skb)->nr_frags;
2203 for (f = 0; f < nr_frags; f++) {
2204 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002205 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002206 }
2207
Dongdong Deng48719532009-08-23 19:49:07 -07002208 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002209
2210 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05002211 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002212 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002213 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07002214 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002215 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002216 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00002217 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002218 }
2219
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002220 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002221 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07002222 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08002223 goto unlock;
2224 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00002225
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00002226 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002227 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00002228 skb_tx_timestamp(skb);
2229
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002230 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2231
Zach Brownb410d132016-10-19 09:56:57 -05002232 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002233 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002234
Soren Brinkmann92030902014-03-04 08:46:39 -08002235unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002236 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002237
Claudiu Beznead1c38952018-08-07 12:25:12 +03002238 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002239}
2240
Nicolas Ferre4df95132013-06-04 21:57:12 +00002241static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002242{
2243 if (!macb_is_gem(bp)) {
2244 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2245 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002246 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002247
Nicolas Ferre1b447912013-06-04 21:57:11 +00002248 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002249 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002250 "RX buffer must be multiple of %d bytes, expanding\n",
2251 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002252 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002253 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002254 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002255 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002256
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002257 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002258 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002259}
2260
Nicolas Ferre4df95132013-06-04 21:57:12 +00002261static void gem_free_rx_buffers(struct macb *bp)
2262{
2263 struct sk_buff *skb;
2264 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002265 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002266 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002267 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002268 int i;
2269
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002270 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2271 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002272 continue;
2273
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002274 for (i = 0; i < bp->rx_ring_size; i++) {
2275 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002276
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002277 if (!skb)
2278 continue;
2279
2280 desc = macb_rx_desc(queue, i);
2281 addr = macb_get_addr(bp, desc);
2282
2283 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2284 DMA_FROM_DEVICE);
2285 dev_kfree_skb_any(skb);
2286 skb = NULL;
2287 }
2288
2289 kfree(queue->rx_skbuff);
2290 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002291 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002292}
2293
2294static void macb_free_rx_buffers(struct macb *bp)
2295{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002296 struct macb_queue *queue = &bp->queues[0];
2297
2298 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002299 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002300 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002301 queue->rx_buffers, queue->rx_buffers_dma);
2302 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002303 }
2304}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002305
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002306static void macb_free_consistent(struct macb *bp)
2307{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002308 struct macb_queue *queue;
2309 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302310 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002311
Nicolas Ferre4df95132013-06-04 21:57:12 +00002312 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002313
2314 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2315 kfree(queue->tx_skb);
2316 queue->tx_skb = NULL;
2317 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302318 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2319 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002320 queue->tx_ring, queue->tx_ring_dma);
2321 queue->tx_ring = NULL;
2322 }
Harini Katakame50b7702018-07-06 12:18:57 +05302323 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302324 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2325 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302326 queue->rx_ring, queue->rx_ring_dma);
2327 queue->rx_ring = NULL;
2328 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002329 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002330}
2331
2332static int gem_alloc_rx_buffers(struct macb *bp)
2333{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002334 struct macb_queue *queue;
2335 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002336 int size;
2337
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002338 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2339 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2340 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2341 if (!queue->rx_skbuff)
2342 return -ENOMEM;
2343 else
2344 netdev_dbg(bp->dev,
2345 "Allocated %d RX struct sk_buff entries at %p\n",
2346 bp->rx_ring_size, queue->rx_skbuff);
2347 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002348 return 0;
2349}
2350
2351static int macb_alloc_rx_buffers(struct macb *bp)
2352{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002353 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002354 int size;
2355
Zach Brownb410d132016-10-19 09:56:57 -05002356 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002357 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2358 &queue->rx_buffers_dma, GFP_KERNEL);
2359 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002360 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002361
2362 netdev_dbg(bp->dev,
2363 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002364 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002365 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002366}
2367
2368static int macb_alloc_consistent(struct macb *bp)
2369{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002370 struct macb_queue *queue;
2371 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002372 int size;
2373
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002374 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302375 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002376 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2377 &queue->tx_ring_dma,
2378 GFP_KERNEL);
2379 if (!queue->tx_ring)
2380 goto out_err;
2381 netdev_dbg(bp->dev,
2382 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2383 q, size, (unsigned long)queue->tx_ring_dma,
2384 queue->tx_ring);
2385
Zach Brownb410d132016-10-19 09:56:57 -05002386 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002387 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2388 if (!queue->tx_skb)
2389 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002390
Harini Katakam404cd082018-07-06 12:18:58 +05302391 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002392 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2393 &queue->rx_ring_dma, GFP_KERNEL);
2394 if (!queue->rx_ring)
2395 goto out_err;
2396 netdev_dbg(bp->dev,
2397 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2398 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002399 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002400 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002401 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002402
2403 return 0;
2404
2405out_err:
2406 macb_free_consistent(bp);
2407 return -ENOMEM;
2408}
2409
Nicolas Ferre4df95132013-06-04 21:57:12 +00002410static void gem_init_rings(struct macb *bp)
2411{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002412 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002413 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002414 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002415 int i;
2416
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002417 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002418 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002419 desc = macb_tx_desc(queue, i);
2420 macb_set_addr(bp, desc, 0);
2421 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002422 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002423 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002424 queue->tx_head = 0;
2425 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002426
2427 queue->rx_tail = 0;
2428 queue->rx_prepared_head = 0;
2429
2430 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002431 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002432
Nicolas Ferre4df95132013-06-04 21:57:12 +00002433}
2434
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002435static void macb_init_rings(struct macb *bp)
2436{
2437 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002438 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002439
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002440 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002441
Zach Brownb410d132016-10-19 09:56:57 -05002442 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002443 desc = macb_tx_desc(&bp->queues[0], i);
2444 macb_set_addr(bp, desc, 0);
2445 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002446 }
Ben Shelton21d35152015-04-22 17:28:54 -05002447 bp->queues[0].tx_head = 0;
2448 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002449 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002450}
2451
2452static void macb_reset_hw(struct macb *bp)
2453{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002454 struct macb_queue *queue;
2455 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002456 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002457
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002458 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002459 * more gracefully?)
2460 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002461 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002462
2463 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002464 ctrl |= MACB_BIT(CLRSTAT);
2465
2466 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002467
2468 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002469 macb_writel(bp, TSR, -1);
2470 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002471
2472 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002473 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2474 queue_writel(queue, IDR, -1);
2475 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002476 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2477 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002478 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002479}
2480
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002481static u32 gem_mdc_clk_div(struct macb *bp)
2482{
2483 u32 config;
2484 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2485
2486 if (pclk_hz <= 20000000)
2487 config = GEM_BF(CLK, GEM_CLK_DIV8);
2488 else if (pclk_hz <= 40000000)
2489 config = GEM_BF(CLK, GEM_CLK_DIV16);
2490 else if (pclk_hz <= 80000000)
2491 config = GEM_BF(CLK, GEM_CLK_DIV32);
2492 else if (pclk_hz <= 120000000)
2493 config = GEM_BF(CLK, GEM_CLK_DIV48);
2494 else if (pclk_hz <= 160000000)
2495 config = GEM_BF(CLK, GEM_CLK_DIV64);
2496 else
2497 config = GEM_BF(CLK, GEM_CLK_DIV96);
2498
2499 return config;
2500}
2501
2502static u32 macb_mdc_clk_div(struct macb *bp)
2503{
2504 u32 config;
2505 unsigned long pclk_hz;
2506
2507 if (macb_is_gem(bp))
2508 return gem_mdc_clk_div(bp);
2509
2510 pclk_hz = clk_get_rate(bp->pclk);
2511 if (pclk_hz <= 20000000)
2512 config = MACB_BF(CLK, MACB_CLK_DIV8);
2513 else if (pclk_hz <= 40000000)
2514 config = MACB_BF(CLK, MACB_CLK_DIV16);
2515 else if (pclk_hz <= 80000000)
2516 config = MACB_BF(CLK, MACB_CLK_DIV32);
2517 else
2518 config = MACB_BF(CLK, MACB_CLK_DIV64);
2519
2520 return config;
2521}
2522
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002523/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002524 * should program. We find the width from decoding the design configuration
2525 * register to find the maximum supported data bus width.
2526 */
2527static u32 macb_dbw(struct macb *bp)
2528{
2529 if (!macb_is_gem(bp))
2530 return 0;
2531
2532 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2533 case 4:
2534 return GEM_BF(DBW, GEM_DBW128);
2535 case 2:
2536 return GEM_BF(DBW, GEM_DBW64);
2537 case 1:
2538 default:
2539 return GEM_BF(DBW, GEM_DBW32);
2540 }
2541}
2542
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002543/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002544 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002545 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002546 * (if not supported by FIFO, it will fallback to default)
2547 * - set both rx/tx packet buffers to full memory size
2548 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002549 */
2550static void macb_configure_dma(struct macb *bp)
2551{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002552 struct macb_queue *queue;
2553 u32 buffer_size;
2554 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002555 u32 dmacfg;
2556
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002557 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002558 if (macb_is_gem(bp)) {
2559 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002560 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2561 if (q)
2562 queue_writel(queue, RBQS, buffer_size);
2563 else
2564 dmacfg |= GEM_BF(RXBS, buffer_size);
2565 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002566 if (bp->dma_burst_length)
2567 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002568 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302569 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302570
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002571 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302572 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2573 else
2574 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2575
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002576 if (bp->dev->features & NETIF_F_HW_CSUM)
2577 dmacfg |= GEM_BIT(TXCOEN);
2578 else
2579 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302580
Michal Simekbd620722018-09-25 08:32:50 +02002581 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302582#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002583 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002584 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302585#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002586#ifdef CONFIG_MACB_USE_HWSTAMP
2587 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2588 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2589#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002590 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2591 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002592 gem_writel(bp, DMACFG, dmacfg);
2593 }
2594}
2595
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002596static void macb_init_hw(struct macb *bp)
2597{
2598 u32 config;
2599
2600 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002601 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002602
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002603 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002604 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002605 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002606 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302607 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2608 else
2609 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002610 if (bp->dev->flags & IFF_PROMISC)
2611 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002612 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2613 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002614 if (!(bp->dev->flags & IFF_BROADCAST))
2615 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002616 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002617 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002618 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302619 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302620 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002621 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302622 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002623
Jamie Iles0116da42011-03-14 17:38:30 +00002624 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002625}
2626
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002627/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002628 * locations in the memory map. The least significant bits are stored
2629 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2630 *
2631 * The unicast hash enable and the multicast hash enable bits in the
2632 * network configuration register enable the reception of hash matched
2633 * frames. The destination address is reduced to a 6 bit index into
2634 * the 64 bit hash register using the following hash function. The
2635 * hash function is an exclusive or of every sixth bit of the
2636 * destination address.
2637 *
2638 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2639 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2640 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2641 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2642 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2643 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2644 *
2645 * da[0] represents the least significant bit of the first byte
2646 * received, that is, the multicast/unicast indicator, and da[47]
2647 * represents the most significant bit of the last byte received. If
2648 * the hash index, hi[n], points to a bit that is set in the hash
2649 * register then the frame will be matched according to whether the
2650 * frame is multicast or unicast. A multicast match will be signalled
2651 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2652 * index points to a bit set in the hash register. A unicast match
2653 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2654 * and the hash index points to a bit set in the hash register. To
2655 * receive all multicast frames, the hash register should be set with
2656 * all ones and the multicast hash enable bit should be set in the
2657 * network configuration register.
2658 */
2659
2660static inline int hash_bit_value(int bitnr, __u8 *addr)
2661{
2662 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2663 return 1;
2664 return 0;
2665}
2666
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002667/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002668static int hash_get_index(__u8 *addr)
2669{
2670 int i, j, bitval;
2671 int hash_index = 0;
2672
2673 for (j = 0; j < 6; j++) {
2674 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002675 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002676
2677 hash_index |= (bitval << j);
2678 }
2679
2680 return hash_index;
2681}
2682
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002683/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002684static void macb_sethashtable(struct net_device *dev)
2685{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002686 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002687 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002688 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002689 struct macb *bp = netdev_priv(dev);
2690
Moritz Fischeraa50b552016-03-29 19:11:13 -07002691 mc_filter[0] = 0;
2692 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002693
Jiri Pirko22bedad32010-04-01 21:22:57 +00002694 netdev_for_each_mc_addr(ha, dev) {
2695 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002696 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2697 }
2698
Jamie Ilesf75ba502011-11-08 10:12:32 +00002699 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2700 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002701}
2702
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002703/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002704static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002705{
2706 unsigned long cfg;
2707 struct macb *bp = netdev_priv(dev);
2708
2709 cfg = macb_readl(bp, NCFGR);
2710
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002711 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002712 /* Enable promiscuous mode */
2713 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002714
2715 /* Disable RX checksum offload */
2716 if (macb_is_gem(bp))
2717 cfg &= ~GEM_BIT(RXCOEN);
2718 } else {
2719 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002720 cfg &= ~MACB_BIT(CAF);
2721
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002722 /* Enable RX checksum offload only if requested */
2723 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2724 cfg |= GEM_BIT(RXCOEN);
2725 }
2726
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002727 if (dev->flags & IFF_ALLMULTI) {
2728 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002729 macb_or_gem_writel(bp, HRB, -1);
2730 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002731 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002732 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002733 /* Enable specific multicasts */
2734 macb_sethashtable(dev);
2735 cfg |= MACB_BIT(NCFGR_MTI);
2736 } else if (dev->flags & (~IFF_ALLMULTI)) {
2737 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002738 macb_or_gem_writel(bp, HRB, 0);
2739 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002740 cfg &= ~MACB_BIT(NCFGR_MTI);
2741 }
2742
2743 macb_writel(bp, NCFGR, cfg);
2744}
2745
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002746static int macb_open(struct net_device *dev)
2747{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002748 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002749 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002750 struct macb_queue *queue;
2751 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002752 int err;
2753
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002754 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002755
Harini Katakamd54f89a2019-03-01 16:20:34 +05302756 err = pm_runtime_get_sync(&bp->pdev->dev);
2757 if (err < 0)
2758 goto pm_exit;
2759
Nicolas Ferre1b447912013-06-04 21:57:11 +00002760 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002761 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002762
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002763 err = macb_alloc_consistent(bp);
2764 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002765 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2766 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302767 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002768 }
2769
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002770 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2771 napi_enable(&queue->napi);
2772
Harini Katakam05044532019-05-07 19:59:10 +05302773 macb_init_hw(bp);
2774
Antoine Tenart7897b072019-11-13 10:00:06 +01002775 err = macb_phylink_connect(bp);
2776 if (err)
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002777 goto reset_hw;
frederic RODO6c36a702007-07-12 19:07:24 +02002778
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002779 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002780
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002781 if (bp->ptp_info)
2782 bp->ptp_info->ptp_init(dev);
2783
Charles Keepax939a5bf72020-06-15 14:18:54 +01002784 return 0;
2785
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002786reset_hw:
2787 macb_reset_hw(bp);
Corentin Labbe014406b2020-06-10 09:53:44 +00002788 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2789 napi_disable(&queue->napi);
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002790 macb_free_consistent(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302791pm_exit:
Charles Keepax939a5bf72020-06-15 14:18:54 +01002792 pm_runtime_put_sync(&bp->pdev->dev);
2793 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002794}
2795
2796static int macb_close(struct net_device *dev)
2797{
2798 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002799 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002800 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002801 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002802
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002803 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002804
2805 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2806 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002807
Antoine Tenart7897b072019-11-13 10:00:06 +01002808 phylink_stop(bp->phylink);
2809 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002810
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002811 spin_lock_irqsave(&bp->lock, flags);
2812 macb_reset_hw(bp);
2813 netif_carrier_off(dev);
2814 spin_unlock_irqrestore(&bp->lock, flags);
2815
2816 macb_free_consistent(bp);
2817
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002818 if (bp->ptp_info)
2819 bp->ptp_info->ptp_remove(dev);
2820
Harini Katakamd54f89a2019-03-01 16:20:34 +05302821 pm_runtime_put(&bp->pdev->dev);
2822
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002823 return 0;
2824}
2825
Harini Katakama5898ea2015-05-06 22:27:18 +05302826static int macb_change_mtu(struct net_device *dev, int new_mtu)
2827{
Harini Katakama5898ea2015-05-06 22:27:18 +05302828 if (netif_running(dev))
2829 return -EBUSY;
2830
Harini Katakama5898ea2015-05-06 22:27:18 +05302831 dev->mtu = new_mtu;
2832
2833 return 0;
2834}
2835
Jamie Ilesa494ed82011-03-09 16:26:35 +00002836static void gem_update_stats(struct macb *bp)
2837{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002838 struct macb_queue *queue;
2839 unsigned int i, q, idx;
2840 unsigned long *stat;
2841
Jamie Ilesa494ed82011-03-09 16:26:35 +00002842 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002843
Xander Huff3ff13f12015-01-13 16:15:51 -06002844 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2845 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002846 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002847
2848 bp->ethtool_stats[i] += val;
2849 *p += val;
2850
2851 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2852 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002853 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002854 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002855 *(++p) += val;
2856 }
2857 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002858
2859 idx = GEM_STATS_LEN;
2860 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2861 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2862 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002863}
2864
2865static struct net_device_stats *gem_get_stats(struct macb *bp)
2866{
2867 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002868 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002869
2870 gem_update_stats(bp);
2871
2872 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2873 hwstat->rx_alignment_errors +
2874 hwstat->rx_resource_errors +
2875 hwstat->rx_overruns +
2876 hwstat->rx_oversize_frames +
2877 hwstat->rx_jabbers +
2878 hwstat->rx_undersized_frames +
2879 hwstat->rx_length_field_frame_errors);
2880 nstat->tx_errors = (hwstat->tx_late_collisions +
2881 hwstat->tx_excessive_collisions +
2882 hwstat->tx_underrun +
2883 hwstat->tx_carrier_sense_errors);
2884 nstat->multicast = hwstat->rx_multicast_frames;
2885 nstat->collisions = (hwstat->tx_single_collision_frames +
2886 hwstat->tx_multiple_collision_frames +
2887 hwstat->tx_excessive_collisions);
2888 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2889 hwstat->rx_jabbers +
2890 hwstat->rx_undersized_frames +
2891 hwstat->rx_length_field_frame_errors);
2892 nstat->rx_over_errors = hwstat->rx_resource_errors;
2893 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2894 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2895 nstat->rx_fifo_errors = hwstat->rx_overruns;
2896 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2897 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2898 nstat->tx_fifo_errors = hwstat->tx_underrun;
2899
2900 return nstat;
2901}
2902
Xander Huff3ff13f12015-01-13 16:15:51 -06002903static void gem_get_ethtool_stats(struct net_device *dev,
2904 struct ethtool_stats *stats, u64 *data)
2905{
2906 struct macb *bp;
2907
2908 bp = netdev_priv(dev);
2909 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002910 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2911 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002912}
2913
2914static int gem_get_sset_count(struct net_device *dev, int sset)
2915{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002916 struct macb *bp = netdev_priv(dev);
2917
Xander Huff3ff13f12015-01-13 16:15:51 -06002918 switch (sset) {
2919 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002920 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002921 default:
2922 return -EOPNOTSUPP;
2923 }
2924}
2925
2926static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2927{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002928 char stat_string[ETH_GSTRING_LEN];
2929 struct macb *bp = netdev_priv(dev);
2930 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002931 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002932 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002933
2934 switch (sset) {
2935 case ETH_SS_STATS:
2936 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2937 memcpy(p, gem_statistics[i].stat_string,
2938 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002939
2940 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2941 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2942 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2943 q, queue_statistics[i].stat_string);
2944 memcpy(p, stat_string, ETH_GSTRING_LEN);
2945 }
2946 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002947 break;
2948 }
2949}
2950
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002951static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002952{
2953 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002954 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002955 struct macb_stats *hwstat = &bp->hw_stats.macb;
2956
2957 if (macb_is_gem(bp))
2958 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002959
frederic RODO6c36a702007-07-12 19:07:24 +02002960 /* read stats from hardware */
2961 macb_update_stats(bp);
2962
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002963 /* Convert HW stats into netdevice stats */
2964 nstat->rx_errors = (hwstat->rx_fcs_errors +
2965 hwstat->rx_align_errors +
2966 hwstat->rx_resource_errors +
2967 hwstat->rx_overruns +
2968 hwstat->rx_oversize_pkts +
2969 hwstat->rx_jabbers +
2970 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002971 hwstat->rx_length_mismatch);
2972 nstat->tx_errors = (hwstat->tx_late_cols +
2973 hwstat->tx_excessive_cols +
2974 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002975 hwstat->tx_carrier_errors +
2976 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002977 nstat->collisions = (hwstat->tx_single_cols +
2978 hwstat->tx_multiple_cols +
2979 hwstat->tx_excessive_cols);
2980 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2981 hwstat->rx_jabbers +
2982 hwstat->rx_undersize_pkts +
2983 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002984 nstat->rx_over_errors = hwstat->rx_resource_errors +
2985 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002986 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2987 nstat->rx_frame_errors = hwstat->rx_align_errors;
2988 nstat->rx_fifo_errors = hwstat->rx_overruns;
2989 /* XXX: What does "missed" mean? */
2990 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2991 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2992 nstat->tx_fifo_errors = hwstat->tx_underruns;
2993 /* Don't know about heartbeat or window errors... */
2994
2995 return nstat;
2996}
2997
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002998static int macb_get_regs_len(struct net_device *netdev)
2999{
3000 return MACB_GREGS_NBR * sizeof(u32);
3001}
3002
3003static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
3004 void *p)
3005{
3006 struct macb *bp = netdev_priv(dev);
3007 unsigned int tail, head;
3008 u32 *regs_buff = p;
3009
3010 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
3011 | MACB_GREGS_VERSION;
3012
Zach Brownb410d132016-10-19 09:56:57 -05003013 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
3014 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003015
3016 regs_buff[0] = macb_readl(bp, NCR);
3017 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
3018 regs_buff[2] = macb_readl(bp, NSR);
3019 regs_buff[3] = macb_readl(bp, TSR);
3020 regs_buff[4] = macb_readl(bp, RBQP);
3021 regs_buff[5] = macb_readl(bp, TBQP);
3022 regs_buff[6] = macb_readl(bp, RSR);
3023 regs_buff[7] = macb_readl(bp, IMR);
3024
3025 regs_buff[8] = tail;
3026 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003027 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
3028 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003029
Neil Armstrongce721a72016-01-05 14:39:16 +01003030 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
3031 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003032 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003033 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003034}
3035
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003036static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3037{
3038 struct macb *bp = netdev_priv(netdev);
3039
Nicolas Ferre253fe092020-07-10 14:46:43 +02003040 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
Antoine Tenart7897b072019-11-13 10:00:06 +01003041 phylink_ethtool_get_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003042 wol->supported |= WAKE_MAGIC;
3043
3044 if (bp->wol & MACB_WOL_ENABLED)
3045 wol->wolopts |= WAKE_MAGIC;
3046 }
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003047}
3048
3049static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3050{
3051 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01003052 int ret;
3053
Nicolas Ferre253fe092020-07-10 14:46:43 +02003054 /* Pass the order to phylink layer */
Antoine Tenart7897b072019-11-13 10:00:06 +01003055 ret = phylink_ethtool_set_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003056 /* Don't manage WoL on MAC if handled by the PHY
3057 * or if there's a failure in talking to the PHY
3058 */
3059 if (!ret || ret != -EOPNOTSUPP)
3060 return ret;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003061
3062 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
3063 (wol->wolopts & ~WAKE_MAGIC))
3064 return -EOPNOTSUPP;
3065
3066 if (wol->wolopts & WAKE_MAGIC)
3067 bp->wol |= MACB_WOL_ENABLED;
3068 else
3069 bp->wol &= ~MACB_WOL_ENABLED;
3070
3071 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
3072
3073 return 0;
3074}
3075
Antoine Tenart7897b072019-11-13 10:00:06 +01003076static int macb_get_link_ksettings(struct net_device *netdev,
3077 struct ethtool_link_ksettings *kset)
3078{
3079 struct macb *bp = netdev_priv(netdev);
3080
3081 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3082}
3083
3084static int macb_set_link_ksettings(struct net_device *netdev,
3085 const struct ethtool_link_ksettings *kset)
3086{
3087 struct macb *bp = netdev_priv(netdev);
3088
3089 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3090}
3091
Zach Brown8441bb32016-10-19 09:56:58 -05003092static void macb_get_ringparam(struct net_device *netdev,
3093 struct ethtool_ringparam *ring)
3094{
3095 struct macb *bp = netdev_priv(netdev);
3096
3097 ring->rx_max_pending = MAX_RX_RING_SIZE;
3098 ring->tx_max_pending = MAX_TX_RING_SIZE;
3099
3100 ring->rx_pending = bp->rx_ring_size;
3101 ring->tx_pending = bp->tx_ring_size;
3102}
3103
3104static int macb_set_ringparam(struct net_device *netdev,
3105 struct ethtool_ringparam *ring)
3106{
3107 struct macb *bp = netdev_priv(netdev);
3108 u32 new_rx_size, new_tx_size;
3109 unsigned int reset = 0;
3110
3111 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3112 return -EINVAL;
3113
3114 new_rx_size = clamp_t(u32, ring->rx_pending,
3115 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3116 new_rx_size = roundup_pow_of_two(new_rx_size);
3117
3118 new_tx_size = clamp_t(u32, ring->tx_pending,
3119 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3120 new_tx_size = roundup_pow_of_two(new_tx_size);
3121
3122 if ((new_tx_size == bp->tx_ring_size) &&
3123 (new_rx_size == bp->rx_ring_size)) {
3124 /* nothing to do */
3125 return 0;
3126 }
3127
3128 if (netif_running(bp->dev)) {
3129 reset = 1;
3130 macb_close(bp->dev);
3131 }
3132
3133 bp->rx_ring_size = new_rx_size;
3134 bp->tx_ring_size = new_tx_size;
3135
3136 if (reset)
3137 macb_open(bp->dev);
3138
3139 return 0;
3140}
3141
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003142#ifdef CONFIG_MACB_USE_HWSTAMP
3143static unsigned int gem_get_tsu_rate(struct macb *bp)
3144{
3145 struct clk *tsu_clk;
3146 unsigned int tsu_rate;
3147
3148 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
3149 if (!IS_ERR(tsu_clk))
3150 tsu_rate = clk_get_rate(tsu_clk);
3151 /* try pclk instead */
3152 else if (!IS_ERR(bp->pclk)) {
3153 tsu_clk = bp->pclk;
3154 tsu_rate = clk_get_rate(tsu_clk);
3155 } else
3156 return -ENOTSUPP;
3157 return tsu_rate;
3158}
3159
3160static s32 gem_get_ptp_max_adj(void)
3161{
3162 return 64000000;
3163}
3164
3165static int gem_get_ts_info(struct net_device *dev,
3166 struct ethtool_ts_info *info)
3167{
3168 struct macb *bp = netdev_priv(dev);
3169
3170 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
3171 ethtool_op_get_ts_info(dev, info);
3172 return 0;
3173 }
3174
3175 info->so_timestamping =
3176 SOF_TIMESTAMPING_TX_SOFTWARE |
3177 SOF_TIMESTAMPING_RX_SOFTWARE |
3178 SOF_TIMESTAMPING_SOFTWARE |
3179 SOF_TIMESTAMPING_TX_HARDWARE |
3180 SOF_TIMESTAMPING_RX_HARDWARE |
3181 SOF_TIMESTAMPING_RAW_HARDWARE;
3182 info->tx_types =
3183 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3184 (1 << HWTSTAMP_TX_OFF) |
3185 (1 << HWTSTAMP_TX_ON);
3186 info->rx_filters =
3187 (1 << HWTSTAMP_FILTER_NONE) |
3188 (1 << HWTSTAMP_FILTER_ALL);
3189
3190 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
3191
3192 return 0;
3193}
3194
3195static struct macb_ptp_info gem_ptp_info = {
3196 .ptp_init = gem_ptp_init,
3197 .ptp_remove = gem_ptp_remove,
3198 .get_ptp_max_adj = gem_get_ptp_max_adj,
3199 .get_tsu_rate = gem_get_tsu_rate,
3200 .get_ts_info = gem_get_ts_info,
3201 .get_hwtst = gem_get_hwtst,
3202 .set_hwtst = gem_set_hwtst,
3203};
3204#endif
3205
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003206static int macb_get_ts_info(struct net_device *netdev,
3207 struct ethtool_ts_info *info)
3208{
3209 struct macb *bp = netdev_priv(netdev);
3210
3211 if (bp->ptp_info)
3212 return bp->ptp_info->get_ts_info(netdev, info);
3213
3214 return ethtool_op_get_ts_info(netdev, info);
3215}
3216
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003217static void gem_enable_flow_filters(struct macb *bp, bool enable)
3218{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003219 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003220 struct ethtool_rx_fs_item *item;
3221 u32 t2_scr;
3222 int num_t2_scr;
3223
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003224 if (!(netdev->features & NETIF_F_NTUPLE))
3225 return;
3226
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003227 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3228
3229 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3230 struct ethtool_rx_flow_spec *fs = &item->fs;
3231 struct ethtool_tcpip4_spec *tp4sp_m;
3232
3233 if (fs->location >= num_t2_scr)
3234 continue;
3235
3236 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3237
3238 /* enable/disable screener regs for the flow entry */
3239 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3240
3241 /* only enable fields with no masking */
3242 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3243
3244 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3245 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3246 else
3247 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3248
3249 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3250 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3251 else
3252 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3253
3254 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3255 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3256 else
3257 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3258
3259 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3260 }
3261}
3262
3263static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3264{
3265 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3266 uint16_t index = fs->location;
3267 u32 w0, w1, t2_scr;
3268 bool cmp_a = false;
3269 bool cmp_b = false;
3270 bool cmp_c = false;
3271
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003272 if (!macb_is_gem(bp))
3273 return;
3274
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003275 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3276 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3277
3278 /* ignore field if any masking set */
3279 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3280 /* 1st compare reg - IP source address */
3281 w0 = 0;
3282 w1 = 0;
3283 w0 = tp4sp_v->ip4src;
3284 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3285 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3286 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3287 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3288 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3289 cmp_a = true;
3290 }
3291
3292 /* ignore field if any masking set */
3293 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3294 /* 2nd compare reg - IP destination address */
3295 w0 = 0;
3296 w1 = 0;
3297 w0 = tp4sp_v->ip4dst;
3298 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3299 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3300 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3301 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3302 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3303 cmp_b = true;
3304 }
3305
3306 /* ignore both port fields if masking set in both */
3307 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3308 /* 3rd compare reg - source port, destination port */
3309 w0 = 0;
3310 w1 = 0;
3311 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3312 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3313 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3314 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3315 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3316 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3317 } else {
3318 /* only one port definition */
3319 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3320 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3321 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3322 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3323 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3324 } else { /* dst port */
3325 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3326 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3327 }
3328 }
3329 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3330 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3331 cmp_c = true;
3332 }
3333
3334 t2_scr = 0;
3335 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3336 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3337 if (cmp_a)
3338 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3339 if (cmp_b)
3340 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3341 if (cmp_c)
3342 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3343 gem_writel_n(bp, SCRT2, index, t2_scr);
3344}
3345
3346static int gem_add_flow_filter(struct net_device *netdev,
3347 struct ethtool_rxnfc *cmd)
3348{
3349 struct macb *bp = netdev_priv(netdev);
3350 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3351 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003352 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003353 int ret = -EINVAL;
3354 bool added = false;
3355
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003356 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003357 if (newfs == NULL)
3358 return -ENOMEM;
3359 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3360
3361 netdev_dbg(netdev,
3362 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3363 fs->flow_type, (int)fs->ring_cookie, fs->location,
3364 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3365 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3366 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3367
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003368 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3369
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003370 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003371 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3372 if (item->fs.location > newfs->fs.location) {
3373 list_add_tail(&newfs->list, &item->list);
3374 added = true;
3375 break;
3376 } else if (item->fs.location == fs->location) {
3377 netdev_err(netdev, "Rule not added: location %d not free!\n",
3378 fs->location);
3379 ret = -EBUSY;
3380 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003381 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003382 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003383 if (!added)
3384 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003385
3386 gem_prog_cmp_regs(bp, fs);
3387 bp->rx_fs_list.count++;
3388 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003389 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003390
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003391 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003392 return 0;
3393
3394err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003395 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003396 kfree(newfs);
3397 return ret;
3398}
3399
3400static int gem_del_flow_filter(struct net_device *netdev,
3401 struct ethtool_rxnfc *cmd)
3402{
3403 struct macb *bp = netdev_priv(netdev);
3404 struct ethtool_rx_fs_item *item;
3405 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003406 unsigned long flags;
3407
3408 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003409
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003410 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3411 if (item->fs.location == cmd->fs.location) {
3412 /* disable screener regs for the flow entry */
3413 fs = &(item->fs);
3414 netdev_dbg(netdev,
3415 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3416 fs->flow_type, (int)fs->ring_cookie, fs->location,
3417 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3418 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3419 htons(fs->h_u.tcp_ip4_spec.psrc),
3420 htons(fs->h_u.tcp_ip4_spec.pdst));
3421
3422 gem_writel_n(bp, SCRT2, fs->location, 0);
3423
3424 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003425 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003426 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3427 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003428 return 0;
3429 }
3430 }
3431
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003432 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003433 return -EINVAL;
3434}
3435
3436static int gem_get_flow_entry(struct net_device *netdev,
3437 struct ethtool_rxnfc *cmd)
3438{
3439 struct macb *bp = netdev_priv(netdev);
3440 struct ethtool_rx_fs_item *item;
3441
3442 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3443 if (item->fs.location == cmd->fs.location) {
3444 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3445 return 0;
3446 }
3447 }
3448 return -EINVAL;
3449}
3450
3451static int gem_get_all_flow_entries(struct net_device *netdev,
3452 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3453{
3454 struct macb *bp = netdev_priv(netdev);
3455 struct ethtool_rx_fs_item *item;
3456 uint32_t cnt = 0;
3457
3458 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3459 if (cnt == cmd->rule_cnt)
3460 return -EMSGSIZE;
3461 rule_locs[cnt] = item->fs.location;
3462 cnt++;
3463 }
3464 cmd->data = bp->max_tuples;
3465 cmd->rule_cnt = cnt;
3466
3467 return 0;
3468}
3469
3470static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3471 u32 *rule_locs)
3472{
3473 struct macb *bp = netdev_priv(netdev);
3474 int ret = 0;
3475
3476 switch (cmd->cmd) {
3477 case ETHTOOL_GRXRINGS:
3478 cmd->data = bp->num_queues;
3479 break;
3480 case ETHTOOL_GRXCLSRLCNT:
3481 cmd->rule_cnt = bp->rx_fs_list.count;
3482 break;
3483 case ETHTOOL_GRXCLSRULE:
3484 ret = gem_get_flow_entry(netdev, cmd);
3485 break;
3486 case ETHTOOL_GRXCLSRLALL:
3487 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3488 break;
3489 default:
3490 netdev_err(netdev,
3491 "Command parameter %d is not supported\n", cmd->cmd);
3492 ret = -EOPNOTSUPP;
3493 }
3494
3495 return ret;
3496}
3497
3498static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3499{
3500 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003501 int ret;
3502
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003503 switch (cmd->cmd) {
3504 case ETHTOOL_SRXCLSRLINS:
3505 if ((cmd->fs.location >= bp->max_tuples)
3506 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3507 ret = -EINVAL;
3508 break;
3509 }
3510 ret = gem_add_flow_filter(netdev, cmd);
3511 break;
3512 case ETHTOOL_SRXCLSRLDEL:
3513 ret = gem_del_flow_filter(netdev, cmd);
3514 break;
3515 default:
3516 netdev_err(netdev,
3517 "Command parameter %d is not supported\n", cmd->cmd);
3518 ret = -EOPNOTSUPP;
3519 }
3520
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003521 return ret;
3522}
3523
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003524static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003525 .get_regs_len = macb_get_regs_len,
3526 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003527 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003528 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003529 .get_wol = macb_get_wol,
3530 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003531 .get_link_ksettings = macb_get_link_ksettings,
3532 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003533 .get_ringparam = macb_get_ringparam,
3534 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003535};
Xander Huff8cd5a562015-01-15 15:55:20 -06003536
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003537static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003538 .get_regs_len = macb_get_regs_len,
3539 .get_regs = macb_get_regs,
Nicolas Ferre558e35c2020-07-20 10:56:52 +02003540 .get_wol = macb_get_wol,
3541 .set_wol = macb_set_wol,
Xander Huff8cd5a562015-01-15 15:55:20 -06003542 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003543 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003544 .get_ethtool_stats = gem_get_ethtool_stats,
3545 .get_strings = gem_get_ethtool_strings,
3546 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003547 .get_link_ksettings = macb_get_link_ksettings,
3548 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003549 .get_ringparam = macb_get_ringparam,
3550 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003551 .get_rxnfc = gem_get_rxnfc,
3552 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003553};
3554
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003555static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003556{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003557 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003558
3559 if (!netif_running(dev))
3560 return -EINVAL;
3561
Antoine Tenart7897b072019-11-13 10:00:06 +01003562 if (bp->ptp_info) {
3563 switch (cmd) {
3564 case SIOCSHWTSTAMP:
3565 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3566 case SIOCGHWTSTAMP:
3567 return bp->ptp_info->get_hwtst(dev, rq);
3568 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003569 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003570
3571 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003572}
3573
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003574static inline void macb_set_txcsum_feature(struct macb *bp,
3575 netdev_features_t features)
3576{
3577 u32 val;
3578
3579 if (!macb_is_gem(bp))
3580 return;
3581
3582 val = gem_readl(bp, DMACFG);
3583 if (features & NETIF_F_HW_CSUM)
3584 val |= GEM_BIT(TXCOEN);
3585 else
3586 val &= ~GEM_BIT(TXCOEN);
3587
3588 gem_writel(bp, DMACFG, val);
3589}
3590
3591static inline void macb_set_rxcsum_feature(struct macb *bp,
3592 netdev_features_t features)
3593{
3594 struct net_device *netdev = bp->dev;
3595 u32 val;
3596
3597 if (!macb_is_gem(bp))
3598 return;
3599
3600 val = gem_readl(bp, NCFGR);
3601 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3602 val |= GEM_BIT(RXCOEN);
3603 else
3604 val &= ~GEM_BIT(RXCOEN);
3605
3606 gem_writel(bp, NCFGR, val);
3607}
3608
3609static inline void macb_set_rxflow_feature(struct macb *bp,
3610 netdev_features_t features)
3611{
3612 if (!macb_is_gem(bp))
3613 return;
3614
3615 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3616}
3617
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003618static int macb_set_features(struct net_device *netdev,
3619 netdev_features_t features)
3620{
3621 struct macb *bp = netdev_priv(netdev);
3622 netdev_features_t changed = features ^ netdev->features;
3623
3624 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003625 if (changed & NETIF_F_HW_CSUM)
3626 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003627
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003628 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003629 if (changed & NETIF_F_RXCSUM)
3630 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003631
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003632 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003633 if (changed & NETIF_F_NTUPLE)
3634 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003635
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003636 return 0;
3637}
3638
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003639static void macb_restore_features(struct macb *bp)
3640{
3641 struct net_device *netdev = bp->dev;
3642 netdev_features_t features = netdev->features;
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003643 struct ethtool_rx_fs_item *item;
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003644
3645 /* TX checksum offload */
3646 macb_set_txcsum_feature(bp, features);
3647
3648 /* RX checksum offload */
3649 macb_set_rxcsum_feature(bp, features);
3650
3651 /* RX Flow Filters */
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003652 list_for_each_entry(item, &bp->rx_fs_list.list, list)
3653 gem_prog_cmp_regs(bp, &item->fs);
3654
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003655 macb_set_rxflow_feature(bp, features);
3656}
3657
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003658static const struct net_device_ops macb_netdev_ops = {
3659 .ndo_open = macb_open,
3660 .ndo_stop = macb_close,
3661 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003662 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003663 .ndo_get_stats = macb_get_stats,
3664 .ndo_do_ioctl = macb_ioctl,
3665 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303666 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003667 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003668#ifdef CONFIG_NET_POLL_CONTROLLER
3669 .ndo_poll_controller = macb_poll_controller,
3670#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003671 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003672 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003673};
3674
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003675/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003676 * and integration options used
3677 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003678static void macb_configure_caps(struct macb *bp,
3679 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003680{
3681 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003682
Nicolas Ferref6970502015-03-31 15:02:01 +02003683 if (dt_conf)
3684 bp->caps = dt_conf->caps;
3685
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003686 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003687 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3688
Nicolas Ferree1755872014-07-24 13:50:58 +02003689 dcfg = gem_readl(bp, DCFG1);
3690 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3691 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +01003692 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
3693 bp->caps |= MACB_CAPS_PCS;
3694 dcfg = gem_readl(bp, DCFG12);
3695 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
3696 bp->caps |= MACB_CAPS_HIGH_SPEED;
Nicolas Ferree1755872014-07-24 13:50:58 +02003697 dcfg = gem_readl(bp, DCFG2);
3698 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3699 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003700#ifdef CONFIG_MACB_USE_HWSTAMP
3701 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003702 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003703 dev_err(&bp->pdev->dev,
3704 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003705 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003706 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003707 bp->ptp_info = &gem_ptp_info;
3708 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003709 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003710#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003711 }
3712
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003713 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003714}
3715
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003716static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003717 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003718 unsigned int *queue_mask,
3719 unsigned int *num_queues)
3720{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003721 *queue_mask = 0x1;
3722 *num_queues = 1;
3723
Nicolas Ferreda120112015-03-31 15:02:00 +02003724 /* is it macb or gem ?
3725 *
3726 * We need to read directly from the hardware here because
3727 * we are early in the probe process and don't have the
3728 * MACB_CAPS_MACB_IS_GEM flag positioned
3729 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003730 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003731 return;
3732
3733 /* bit 0 is never set but queue 0 always exists */
Claudiu Bezneafec371f2020-07-02 12:05:58 +03003734 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
Claudiu Bezneab7ab39b2020-07-02 12:05:59 +03003735 *num_queues = hweight32(*queue_mask);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003736}
3737
Claudiu Beznea38493da2020-12-09 15:03:34 +02003738static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
3739 struct clk *rx_clk, struct clk *tsu_clk)
3740{
3741 struct clk_bulk_data clks[] = {
3742 { .clk = tsu_clk, },
3743 { .clk = rx_clk, },
3744 { .clk = pclk, },
3745 { .clk = hclk, },
3746 { .clk = tx_clk },
3747 };
3748
3749 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
3750}
3751
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003752static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303753 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303754 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003755{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003756 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003757 int err;
3758
Bartosz Folta83a77e92016-12-14 06:39:15 +00003759 pdata = dev_get_platdata(&pdev->dev);
3760 if (pdata) {
3761 *pclk = pdata->pclk;
3762 *hclk = pdata->hclk;
3763 } else {
3764 *pclk = devm_clk_get(&pdev->dev, "pclk");
3765 *hclk = devm_clk_get(&pdev->dev, "hclk");
3766 }
3767
Michael Trettera04be4b2021-03-17 17:16:09 +01003768 if (IS_ERR_OR_NULL(*pclk))
3769 return dev_err_probe(&pdev->dev,
3770 IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
3771 "failed to get pclk\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003772
Michael Trettera04be4b2021-03-17 17:16:09 +01003773 if (IS_ERR_OR_NULL(*hclk))
3774 return dev_err_probe(&pdev->dev,
3775 IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
3776 "failed to get hclk\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003777
Michael Tretterbd310aca2019-10-18 16:11:43 +02003778 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003779 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003780 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003781
Michael Tretterbd310aca2019-10-18 16:11:43 +02003782 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303783 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003784 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303785
Michael Tretterbd310aca2019-10-18 16:11:43 +02003786 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303787 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003788 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303789
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003790 err = clk_prepare_enable(*pclk);
3791 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003792 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003793 return err;
3794 }
3795
3796 err = clk_prepare_enable(*hclk);
3797 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003798 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003799 goto err_disable_pclk;
3800 }
3801
3802 err = clk_prepare_enable(*tx_clk);
3803 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003804 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003805 goto err_disable_hclk;
3806 }
3807
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303808 err = clk_prepare_enable(*rx_clk);
3809 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003810 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303811 goto err_disable_txclk;
3812 }
3813
Harini Katakamf5473d12019-03-01 16:20:33 +05303814 err = clk_prepare_enable(*tsu_clk);
3815 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003816 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303817 goto err_disable_rxclk;
3818 }
3819
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003820 return 0;
3821
Harini Katakamf5473d12019-03-01 16:20:33 +05303822err_disable_rxclk:
3823 clk_disable_unprepare(*rx_clk);
3824
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303825err_disable_txclk:
3826 clk_disable_unprepare(*tx_clk);
3827
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003828err_disable_hclk:
3829 clk_disable_unprepare(*hclk);
3830
3831err_disable_pclk:
3832 clk_disable_unprepare(*pclk);
3833
3834 return err;
3835}
3836
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003837static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003838{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003839 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003840 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003841 struct macb *bp = netdev_priv(dev);
3842 struct macb_queue *queue;
3843 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003844 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003845
Zach Brownb410d132016-10-19 09:56:57 -05003846 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3847 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3848
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003849 /* set the queue register mapping once for all: queue0 has a special
3850 * register mapping but we don't want to test the queue index then
3851 * compute the corresponding register offset at run time.
3852 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003853 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003854 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003855 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003856
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003857 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003858 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003859 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003860 if (hw_q) {
3861 queue->ISR = GEM_ISR(hw_q - 1);
3862 queue->IER = GEM_IER(hw_q - 1);
3863 queue->IDR = GEM_IDR(hw_q - 1);
3864 queue->IMR = GEM_IMR(hw_q - 1);
3865 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003866 queue->RBQP = GEM_RBQP(hw_q - 1);
3867 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303868#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003869 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003870 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003871 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3872 }
Harini Katakamfff80192016-08-09 13:15:53 +05303873#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003874 } else {
3875 /* queue0 uses legacy registers */
3876 queue->ISR = MACB_ISR;
3877 queue->IER = MACB_IER;
3878 queue->IDR = MACB_IDR;
3879 queue->IMR = MACB_IMR;
3880 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003881 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303882#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003883 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003884 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003885 queue->RBQPH = MACB_RBQPH;
3886 }
Harini Katakamfff80192016-08-09 13:15:53 +05303887#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003888 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003889
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003890 /* get irq: here we use the linux queue index, not the hardware
3891 * queue index. the queue irq definitions in the device tree
3892 * must remove the optional gaps that could exist in the
3893 * hardware queue mask.
3894 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003895 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003896 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003897 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003898 if (err) {
3899 dev_err(&pdev->dev,
3900 "Unable to request IRQ %d (error %d)\n",
3901 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003902 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003903 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003904
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003905 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003906 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003907 }
3908
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003909 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003910
Nicolas Ferre4df95132013-06-04 21:57:12 +00003911 /* setup appropriated routines according to adapter type */
3912 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003913 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003914 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3915 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3916 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3917 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003918 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003919 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003920 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003921 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3922 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3923 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3924 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003925 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003926 }
3927
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003928 /* Set features */
3929 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003930
3931 /* Check LSO capability */
3932 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3933 dev->hw_features |= MACB_NETIF_LSO;
3934
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003935 /* Checksum offload is only available on gem with packet buffer */
3936 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003937 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003938 if (bp->caps & MACB_CAPS_SG_DISABLED)
3939 dev->hw_features &= ~NETIF_F_SG;
3940 dev->features = dev->hw_features;
3941
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003942 /* Check RX Flow Filters support.
3943 * Max Rx flows set by availability of screeners & compare regs:
3944 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3945 */
3946 reg = gem_readl(bp, DCFG8);
3947 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3948 GEM_BFEXT(T2SCR, reg));
Claudiu Bezneaa714e272021-04-14 14:20:29 +03003949 INIT_LIST_HEAD(&bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003950 if (bp->max_tuples > 0) {
3951 /* also needs one ethtype match to check IPv4 */
3952 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3953 /* program this reg now */
3954 reg = 0;
3955 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3956 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3957 /* Filtering is supported in hw but don't enable it in kernel now */
3958 dev->hw_features |= NETIF_F_NTUPLE;
3959 /* init Rx flow definitions */
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003960 bp->rx_fs_list.count = 0;
3961 spin_lock_init(&bp->rx_fs_lock);
3962 } else
3963 bp->max_tuples = 0;
3964 }
3965
Neil Armstrongce721a72016-01-05 14:39:16 +01003966 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3967 val = 0;
Alexandre Belloni2ccb0162020-07-18 01:32:21 +02003968 if (phy_interface_mode_is_rgmii(bp->phy_interface))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003969 val = bp->usrio->rgmii;
Neil Armstrongce721a72016-01-05 14:39:16 +01003970 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003971 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003972 val = bp->usrio->rmii;
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003973 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003974 val = bp->usrio->mii;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003975
Neil Armstrongce721a72016-01-05 14:39:16 +01003976 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003977 val |= bp->usrio->refclk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003978
Neil Armstrongce721a72016-01-05 14:39:16 +01003979 macb_or_gem_writel(bp, USRIO, val);
3980 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003981
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003982 /* Set MII management clock divider */
3983 val = macb_mdc_clk_div(bp);
3984 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303985 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3986 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003987 macb_writel(bp, NCFGR, val);
3988
3989 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003990}
3991
Atish Patrab1242232021-03-03 11:55:49 -08003992static const struct macb_usrio_config macb_default_usrio = {
3993 .mii = MACB_BIT(MII),
3994 .rmii = MACB_BIT(RMII),
3995 .rgmii = GEM_BIT(RGMII),
3996 .refclk = MACB_BIT(CLKEN),
3997};
3998
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003999#if defined(CONFIG_OF)
4000/* 1518 rounded up */
4001#define AT91ETHER_MAX_RBUFF_SZ 0x600
4002/* max number of receive buffers */
4003#define AT91ETHER_MAX_RX_DESCR 9
4004
Arnd Bergmann49db9222019-07-08 14:48:23 +02004005static struct sifive_fu540_macb_mgmt *mgmt;
4006
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004007static int at91ether_alloc_coherent(struct macb *lp)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004008{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004009 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004010
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004011 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004012 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004013 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004014 &q->rx_ring_dma, GFP_KERNEL);
4015 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004016 return -ENOMEM;
4017
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004018 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004019 AT91ETHER_MAX_RX_DESCR *
4020 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004021 &q->rx_buffers_dma, GFP_KERNEL);
4022 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004023 dma_free_coherent(&lp->pdev->dev,
4024 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004025 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004026 q->rx_ring, q->rx_ring_dma);
4027 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004028 return -ENOMEM;
4029 }
4030
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004031 return 0;
4032}
4033
4034static void at91ether_free_coherent(struct macb *lp)
4035{
4036 struct macb_queue *q = &lp->queues[0];
4037
4038 if (q->rx_ring) {
4039 dma_free_coherent(&lp->pdev->dev,
4040 AT91ETHER_MAX_RX_DESCR *
4041 macb_dma_desc_get_size(lp),
4042 q->rx_ring, q->rx_ring_dma);
4043 q->rx_ring = NULL;
4044 }
4045
4046 if (q->rx_buffers) {
4047 dma_free_coherent(&lp->pdev->dev,
4048 AT91ETHER_MAX_RX_DESCR *
4049 AT91ETHER_MAX_RBUFF_SZ,
4050 q->rx_buffers, q->rx_buffers_dma);
4051 q->rx_buffers = NULL;
4052 }
4053}
4054
4055/* Initialize and start the Receiver and Transmit subsystems */
4056static int at91ether_start(struct macb *lp)
4057{
4058 struct macb_queue *q = &lp->queues[0];
4059 struct macb_dma_desc *desc;
4060 dma_addr_t addr;
4061 u32 ctl;
4062 int i, ret;
4063
4064 ret = at91ether_alloc_coherent(lp);
4065 if (ret)
4066 return ret;
4067
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004068 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004069 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004070 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004071 macb_set_addr(lp, desc, addr);
4072 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004073 addr += AT91ETHER_MAX_RBUFF_SZ;
4074 }
4075
4076 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004077 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004078
4079 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004080 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004081
4082 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004083 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004084
4085 /* Enable Receive and Transmit */
4086 ctl = macb_readl(lp, NCR);
4087 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
4088
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004089 /* Enable MAC interrupts */
4090 macb_writel(lp, IER, MACB_BIT(RCOMP) |
4091 MACB_BIT(RXUBR) |
4092 MACB_BIT(ISR_TUND) |
4093 MACB_BIT(ISR_RLE) |
4094 MACB_BIT(TCOMP) |
4095 MACB_BIT(ISR_ROVR) |
4096 MACB_BIT(HRESP));
4097
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004098 return 0;
4099}
4100
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004101static void at91ether_stop(struct macb *lp)
4102{
4103 u32 ctl;
4104
4105 /* Disable MAC interrupts */
4106 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
4107 MACB_BIT(RXUBR) |
4108 MACB_BIT(ISR_TUND) |
4109 MACB_BIT(ISR_RLE) |
4110 MACB_BIT(TCOMP) |
4111 MACB_BIT(ISR_ROVR) |
4112 MACB_BIT(HRESP));
4113
4114 /* Disable Receiver and Transmitter */
4115 ctl = macb_readl(lp, NCR);
4116 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
4117
4118 /* Free resources. */
4119 at91ether_free_coherent(lp);
4120}
4121
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004122/* Open the ethernet interface */
4123static int at91ether_open(struct net_device *dev)
4124{
4125 struct macb *lp = netdev_priv(dev);
4126 u32 ctl;
4127 int ret;
4128
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004129 ret = pm_runtime_get_sync(&lp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004130 if (ret < 0) {
4131 pm_runtime_put_noidle(&lp->pdev->dev);
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004132 return ret;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004133 }
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004134
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004135 /* Clear internal statistics */
4136 ctl = macb_readl(lp, NCR);
4137 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
4138
4139 macb_set_hwaddr(lp);
4140
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004141 ret = at91ether_start(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004142 if (ret)
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004143 goto pm_exit;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004144
Antoine Tenart7897b072019-11-13 10:00:06 +01004145 ret = macb_phylink_connect(lp);
4146 if (ret)
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004147 goto stop;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004148
4149 netif_start_queue(dev);
4150
4151 return 0;
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004152
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004153stop:
4154 at91ether_stop(lp);
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004155pm_exit:
4156 pm_runtime_put_sync(&lp->pdev->dev);
4157 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004158}
4159
4160/* Close the interface */
4161static int at91ether_close(struct net_device *dev)
4162{
4163 struct macb *lp = netdev_priv(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004164
4165 netif_stop_queue(dev);
4166
Antoine Tenart7897b072019-11-13 10:00:06 +01004167 phylink_stop(lp->phylink);
4168 phylink_disconnect_phy(lp->phylink);
4169
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004170 at91ether_stop(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004171
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004172 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004173}
4174
4175/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03004176static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
4177 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004178{
4179 struct macb *lp = netdev_priv(dev);
4180
Willy Tarreau1d608d22020-12-09 19:47:40 +01004181 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
4182 int desc = 0;
4183
4184 netif_stop_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004185
4186 /* Store packet information (to free when Tx completed) */
Willy Tarreau73d74222020-10-11 11:09:43 +02004187 lp->rm9200_txq[desc].skb = skb;
4188 lp->rm9200_txq[desc].size = skb->len;
4189 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
4190 skb->len, DMA_TO_DEVICE);
4191 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03004192 dev_kfree_skb_any(skb);
4193 dev->stats.tx_dropped++;
4194 netdev_err(dev, "%s: DMA mapping error\n", __func__);
4195 return NETDEV_TX_OK;
4196 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004197
4198 /* Set address of the data in the Transmit Address register */
Willy Tarreau73d74222020-10-11 11:09:43 +02004199 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004200 /* Set length of the packet in the Transmit Control register */
4201 macb_writel(lp, TCR, skb->len);
4202
4203 } else {
4204 netdev_err(dev, "%s called, but device is busy!\n", __func__);
4205 return NETDEV_TX_BUSY;
4206 }
4207
4208 return NETDEV_TX_OK;
4209}
4210
4211/* Extract received frame from buffer descriptors and sent to upper layers.
4212 * (Called from interrupt context)
4213 */
4214static void at91ether_rx(struct net_device *dev)
4215{
4216 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004217 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004218 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004219 unsigned char *p_recv;
4220 struct sk_buff *skb;
4221 unsigned int pktlen;
4222
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004223 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004224 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004225 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004226 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004227 skb = netdev_alloc_skb(dev, pktlen + 2);
4228 if (skb) {
4229 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004230 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004231
4232 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004233 dev->stats.rx_packets++;
4234 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004235 netif_rx(skb);
4236 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004237 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004238 }
4239
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004240 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004241 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004242
4243 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004244 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004245
4246 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004247 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
4248 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004249 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004250 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004251
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004252 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004253 }
4254}
4255
4256/* MAC interrupt handler */
4257static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
4258{
4259 struct net_device *dev = dev_id;
4260 struct macb *lp = netdev_priv(dev);
4261 u32 intstatus, ctl;
Willy Tarreau73d74222020-10-11 11:09:43 +02004262 unsigned int desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004263
4264 /* MAC Interrupt Status register indicates what interrupts are pending.
4265 * It is automatically cleared once read.
4266 */
4267 intstatus = macb_readl(lp, ISR);
4268
4269 /* Receive complete */
4270 if (intstatus & MACB_BIT(RCOMP))
4271 at91ether_rx(dev);
4272
4273 /* Transmit complete */
Willy Tarreau1d608d22020-12-09 19:47:40 +01004274 if (intstatus & MACB_BIT(TCOMP)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004275 /* The TCOM bit is set even if the transmission failed */
4276 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004277 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004278
Willy Tarreau1d608d22020-12-09 19:47:40 +01004279 desc = 0;
4280 if (lp->rm9200_txq[desc].skb) {
Willy Tarreau73d74222020-10-11 11:09:43 +02004281 dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
4282 lp->rm9200_txq[desc].skb = NULL;
4283 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
4284 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004285 dev->stats.tx_packets++;
Willy Tarreau73d74222020-10-11 11:09:43 +02004286 dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004287 }
Willy Tarreau1d608d22020-12-09 19:47:40 +01004288 netif_wake_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004289 }
4290
4291 /* Work-around for EMAC Errata section 41.3.1 */
4292 if (intstatus & MACB_BIT(RXUBR)) {
4293 ctl = macb_readl(lp, NCR);
4294 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08004295 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004296 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4297 }
4298
4299 if (intstatus & MACB_BIT(ISR_ROVR))
4300 netdev_err(dev, "ROVR error\n");
4301
4302 return IRQ_HANDLED;
4303}
4304
4305#ifdef CONFIG_NET_POLL_CONTROLLER
4306static void at91ether_poll_controller(struct net_device *dev)
4307{
4308 unsigned long flags;
4309
4310 local_irq_save(flags);
4311 at91ether_interrupt(dev->irq, dev);
4312 local_irq_restore(flags);
4313}
4314#endif
4315
4316static const struct net_device_ops at91ether_netdev_ops = {
4317 .ndo_open = at91ether_open,
4318 .ndo_stop = at91ether_close,
4319 .ndo_start_xmit = at91ether_start_xmit,
4320 .ndo_get_stats = macb_get_stats,
4321 .ndo_set_rx_mode = macb_set_rx_mode,
4322 .ndo_set_mac_address = eth_mac_addr,
4323 .ndo_do_ioctl = macb_ioctl,
4324 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004325#ifdef CONFIG_NET_POLL_CONTROLLER
4326 .ndo_poll_controller = at91ether_poll_controller,
4327#endif
4328};
4329
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004330static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304331 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304332 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004333{
4334 int err;
4335
4336 *hclk = NULL;
4337 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304338 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304339 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004340
4341 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4342 if (IS_ERR(*pclk))
4343 return PTR_ERR(*pclk);
4344
4345 err = clk_prepare_enable(*pclk);
4346 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004347 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004348 return err;
4349 }
4350
4351 return 0;
4352}
4353
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004354static int at91ether_init(struct platform_device *pdev)
4355{
4356 struct net_device *dev = platform_get_drvdata(pdev);
4357 struct macb *bp = netdev_priv(dev);
4358 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004359
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004360 bp->queues[0].bp = bp;
4361
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004362 dev->netdev_ops = &at91ether_netdev_ops;
4363 dev->ethtool_ops = &macb_ethtool_ops;
4364
4365 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4366 0, dev->name, dev);
4367 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004368 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004369
4370 macb_writel(bp, NCR, 0);
4371
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004372 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004373
4374 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004375}
4376
Yash Shahc218ad52019-06-18 13:26:08 +05304377static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4378 unsigned long parent_rate)
4379{
4380 return mgmt->rate;
4381}
4382
4383static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4384 unsigned long *parent_rate)
4385{
4386 if (WARN_ON(rate < 2500000))
4387 return 2500000;
4388 else if (rate == 2500000)
4389 return 2500000;
4390 else if (WARN_ON(rate < 13750000))
4391 return 2500000;
4392 else if (WARN_ON(rate < 25000000))
4393 return 25000000;
4394 else if (rate == 25000000)
4395 return 25000000;
4396 else if (WARN_ON(rate < 75000000))
4397 return 25000000;
4398 else if (WARN_ON(rate < 125000000))
4399 return 125000000;
4400 else if (rate == 125000000)
4401 return 125000000;
4402
4403 WARN_ON(rate > 125000000);
4404
4405 return 125000000;
4406}
4407
4408static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4409 unsigned long parent_rate)
4410{
4411 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4412 if (rate != 125000000)
4413 iowrite32(1, mgmt->reg);
4414 else
4415 iowrite32(0, mgmt->reg);
4416 mgmt->rate = rate;
4417
4418 return 0;
4419}
4420
4421static const struct clk_ops fu540_c000_ops = {
4422 .recalc_rate = fu540_macb_tx_recalc_rate,
4423 .round_rate = fu540_macb_tx_round_rate,
4424 .set_rate = fu540_macb_tx_set_rate,
4425};
4426
4427static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4428 struct clk **hclk, struct clk **tx_clk,
4429 struct clk **rx_clk, struct clk **tsu_clk)
4430{
4431 struct clk_init_data init;
4432 int err = 0;
4433
4434 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4435 if (err)
4436 return err;
4437
4438 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004439 if (!mgmt) {
4440 err = -ENOMEM;
4441 goto err_disable_clks;
4442 }
Yash Shahc218ad52019-06-18 13:26:08 +05304443
4444 init.name = "sifive-gemgxl-mgmt";
4445 init.ops = &fu540_c000_ops;
4446 init.flags = 0;
4447 init.num_parents = 0;
4448
4449 mgmt->rate = 0;
4450 mgmt->hw.init = &init;
4451
Stephen Boydd89091a2020-01-03 16:19:21 -08004452 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004453 if (IS_ERR(*tx_clk)) {
4454 err = PTR_ERR(*tx_clk);
4455 goto err_disable_clks;
4456 }
Yash Shahc218ad52019-06-18 13:26:08 +05304457
4458 err = clk_prepare_enable(*tx_clk);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004459 if (err) {
Yash Shahc218ad52019-06-18 13:26:08 +05304460 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004461 *tx_clk = NULL;
4462 goto err_disable_clks;
4463 } else {
Yash Shahc218ad52019-06-18 13:26:08 +05304464 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004465 }
Yash Shahc218ad52019-06-18 13:26:08 +05304466
4467 return 0;
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004468
4469err_disable_clks:
4470 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
4471
4472 return err;
Yash Shahc218ad52019-06-18 13:26:08 +05304473}
4474
4475static int fu540_c000_init(struct platform_device *pdev)
4476{
Dejin Zhengb959c772020-05-03 20:32:26 +08004477 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4478 if (IS_ERR(mgmt->reg))
4479 return PTR_ERR(mgmt->reg);
Yash Shahc218ad52019-06-18 13:26:08 +05304480
4481 return macb_init(pdev);
4482}
4483
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004484static const struct macb_usrio_config sama7g5_usrio = {
4485 .mii = 0,
4486 .rmii = 1,
4487 .rgmii = 2,
4488 .refclk = BIT(2),
4489 .hdfctlen = BIT(6),
4490};
4491
Yash Shahc218ad52019-06-18 13:26:08 +05304492static const struct macb_config fu540_c000_config = {
4493 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4494 MACB_CAPS_GEM_HAS_PTP,
4495 .dma_burst_length = 16,
4496 .clk_init = fu540_c000_clk_init,
4497 .init = fu540_c000_init,
4498 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004499 .usrio = &macb_default_usrio,
Yash Shahc218ad52019-06-18 13:26:08 +05304500};
4501
David S. Miller3cef5c52015-03-09 23:38:02 -04004502static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004503 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004504 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004505 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004506 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004507};
4508
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004509static const struct macb_config sama5d3macb_config = {
4510 .caps = MACB_CAPS_SG_DISABLED
4511 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4512 .clk_init = macb_clk_init,
4513 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004514 .usrio = &macb_default_usrio,
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004515};
4516
David S. Miller3cef5c52015-03-09 23:38:02 -04004517static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004518 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4519 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004520 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004521 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004522 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004523};
4524
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004525static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004526 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004527 .dma_burst_length = 16,
4528 .clk_init = macb_clk_init,
4529 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004530 .usrio = &macb_default_usrio,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004531};
4532
David S. Miller3cef5c52015-03-09 23:38:02 -04004533static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004534 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004535 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004536 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004537 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004538 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004539 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004540 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004541};
4542
David S. Miller3cef5c52015-03-09 23:38:02 -04004543static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004544 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004545 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004546 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004547 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004548 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004549};
4550
David S. Miller3cef5c52015-03-09 23:38:02 -04004551static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004552 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004553 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004554 .init = at91ether_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004555 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004556};
4557
Neil Armstronge611b5b2016-01-05 14:39:17 +01004558static const struct macb_config np4_config = {
4559 .caps = MACB_CAPS_USRIO_DISABLED,
4560 .clk_init = macb_clk_init,
4561 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004562 .usrio = &macb_default_usrio,
Neil Armstronge611b5b2016-01-05 14:39:17 +01004563};
David S. Miller36583eb2015-05-23 01:22:35 -04004564
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304565static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004566 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4567 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304568 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304569 .dma_burst_length = 16,
4570 .clk_init = macb_clk_init,
4571 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304572 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004573 .usrio = &macb_default_usrio,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304574};
4575
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004576static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304577 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4578 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004579 .dma_burst_length = 16,
4580 .clk_init = macb_clk_init,
4581 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004582 .usrio = &macb_default_usrio,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004583};
4584
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004585static const struct macb_config sama7g5_gem_config = {
4586 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG,
4587 .dma_burst_length = 16,
4588 .clk_init = macb_clk_init,
4589 .init = macb_init,
4590 .usrio = &sama7g5_usrio,
4591};
4592
Claudiu Beznea700d5662020-12-09 15:03:39 +02004593static const struct macb_config sama7g5_emac_config = {
4594 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_USRIO_HAS_CLKEN,
4595 .dma_burst_length = 16,
4596 .clk_init = macb_clk_init,
4597 .init = macb_init,
4598 .usrio = &sama7g5_usrio,
4599};
4600
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004601static const struct of_device_id macb_dt_ids[] = {
4602 { .compatible = "cdns,at32ap7000-macb" },
4603 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4604 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004605 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004606 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4607 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004608 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004609 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004610 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004611 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004612 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4613 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4614 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304615 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004616 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304617 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004618 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
Claudiu Beznea700d5662020-12-09 15:03:39 +02004619 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004620 { /* sentinel */ }
4621};
4622MODULE_DEVICE_TABLE(of, macb_dt_ids);
4623#endif /* CONFIG_OF */
4624
Bartosz Folta83a77e92016-12-14 06:39:15 +00004625static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004626 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4627 MACB_CAPS_JUMBO |
4628 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004629 .dma_burst_length = 16,
4630 .clk_init = macb_clk_init,
4631 .init = macb_init,
Atish Patrab1242232021-03-03 11:55:49 -08004632 .usrio = &macb_default_usrio,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004633 .jumbo_max_len = 10240,
4634};
4635
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004636static int macb_probe(struct platform_device *pdev)
4637{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004638 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004639 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304640 struct clk **, struct clk **, struct clk **,
4641 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004642 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004643 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304644 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304645 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004646 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004647 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004648 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004649 struct net_device *dev;
4650 struct resource *regs;
4651 void __iomem *mem;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004652 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304653 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004654
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004655 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4656 mem = devm_ioremap_resource(&pdev->dev, regs);
4657 if (IS_ERR(mem))
4658 return PTR_ERR(mem);
4659
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004660 if (np) {
4661 const struct of_device_id *match;
4662
4663 match = of_match_node(macb_dt_ids, np);
4664 if (match && match->data) {
4665 macb_config = match->data;
4666 clk_init = macb_config->clk_init;
4667 init = macb_config->init;
4668 }
4669 }
4670
Harini Katakamf5473d12019-03-01 16:20:33 +05304671 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004672 if (err)
4673 return err;
4674
Harini Katakamd54f89a2019-03-01 16:20:34 +05304675 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4676 pm_runtime_use_autosuspend(&pdev->dev);
4677 pm_runtime_get_noresume(&pdev->dev);
4678 pm_runtime_set_active(&pdev->dev);
4679 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004680 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004681
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004682 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004683 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004684 if (!dev) {
4685 err = -ENOMEM;
4686 goto err_disable_clocks;
4687 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004688
4689 dev->base_addr = regs->start;
4690
4691 SET_NETDEV_DEV(dev, &pdev->dev);
4692
4693 bp = netdev_priv(dev);
4694 bp->pdev = pdev;
4695 bp->dev = dev;
4696 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004697 bp->native_io = native_io;
4698 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004699 bp->macb_reg_readl = hw_readl_native;
4700 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004701 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004702 bp->macb_reg_readl = hw_readl;
4703 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004704 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004705 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004706 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004707 if (macb_config)
4708 bp->dma_burst_length = macb_config->dma_burst_length;
4709 bp->pclk = pclk;
4710 bp->hclk = hclk;
4711 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304712 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304713 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004714 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304715 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304716
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004717 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004718 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004719 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
Nicolas Ferreced47992020-07-10 14:46:42 +02004720 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004721
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004722 bp->usrio = macb_config->usrio;
4723
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004724 spin_lock_init(&bp->lock);
4725
Nicolas Ferread783472015-03-31 15:02:02 +02004726 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004727 macb_configure_caps(bp, macb_config);
4728
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004729#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4730 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4731 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4732 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4733 }
4734#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004735 platform_set_drvdata(pdev, dev);
4736
4737 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004738 if (dev->irq < 0) {
4739 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004740 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004741 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004742
Jarod Wilson44770e12016-10-17 15:54:17 -04004743 /* MTU range: 68 - 1500 or 10240 */
4744 dev->min_mtu = GEM_MTU_MIN_SIZE;
4745 if (bp->caps & MACB_CAPS_JUMBO)
4746 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4747 else
4748 dev->max_mtu = ETH_DATA_LEN;
4749
Harini Katakam404cd082018-07-06 12:18:58 +05304750 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4751 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4752 if (val)
4753 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4754 macb_dma_desc_get_size(bp);
4755
4756 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4757 if (val)
4758 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4759 macb_dma_desc_get_size(bp);
4760 }
4761
Harini Katakame5010702019-01-29 15:20:03 +05304762 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4763 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4764 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4765
Michael Walle83216e32021-04-12 19:47:17 +02004766 err = of_get_mac_address(np, bp->dev->dev_addr);
4767 if (err == -EPROBE_DEFER)
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004768 goto err_out_free_netdev;
Michael Walle83216e32021-04-12 19:47:17 +02004769 else if (err)
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004770 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02004771
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004772 err = of_get_phy_mode(np, &interface);
4773 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004774 /* not found in DT, MII by default */
4775 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4776 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004777 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004778
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004779 /* IP specific init */
4780 err = init(pdev);
4781 if (err)
4782 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004783
Florian Fainellicf669662016-05-02 18:38:45 -07004784 err = macb_mii_init(bp);
4785 if (err)
4786 goto err_out_free_netdev;
4787
Florian Fainellicf669662016-05-02 18:38:45 -07004788 netif_carrier_off(dev);
4789
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004790 err = register_netdev(dev);
4791 if (err) {
4792 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004793 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004794 }
4795
Allen Paise7412b82020-09-14 12:59:23 +05304796 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
Harini Katakam032dc412018-01-27 12:09:01 +05304797
Bo Shen58798232014-09-13 01:57:49 +02004798 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4799 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4800 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004801
Harini Katakamd54f89a2019-03-01 16:20:34 +05304802 pm_runtime_mark_last_busy(&bp->pdev->dev);
4803 pm_runtime_put_autosuspend(&bp->pdev->dev);
4804
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004805 return 0;
4806
Florian Fainellicf669662016-05-02 18:38:45 -07004807err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004808 mdiobus_unregister(bp->mii_bus);
4809 mdiobus_free(bp->mii_bus);
4810
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004811err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004812 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004813
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004814err_disable_clocks:
Claudiu Beznea38493da2020-12-09 15:03:34 +02004815 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304816 pm_runtime_disable(&pdev->dev);
4817 pm_runtime_set_suspended(&pdev->dev);
4818 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004819
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004820 return err;
4821}
4822
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004823static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004824{
4825 struct net_device *dev;
4826 struct macb *bp;
4827
4828 dev = platform_get_drvdata(pdev);
4829
4830 if (dev) {
4831 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004832 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004833 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004834
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004835 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004836 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304837 pm_runtime_disable(&pdev->dev);
4838 pm_runtime_dont_use_autosuspend(&pdev->dev);
4839 if (!pm_runtime_suspended(&pdev->dev)) {
Claudiu Beznea38493da2020-12-09 15:03:34 +02004840 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
4841 bp->rx_clk, bp->tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304842 pm_runtime_set_suspended(&pdev->dev);
4843 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004844 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004845 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004846 }
4847
4848 return 0;
4849}
4850
Michal Simekd23823d2015-01-23 09:36:03 +01004851static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004852{
Wolfram Sangce886a42018-10-21 22:00:14 +02004853 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004854 struct macb *bp = netdev_priv(netdev);
Jiapeng Chongbbf6ace2021-04-29 18:25:46 +08004855 struct macb_queue *queue;
Harini Katakamde991c52019-03-01 16:20:35 +05304856 unsigned long flags;
4857 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004858 int err;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004859
Harini Katakamde991c52019-03-01 16:20:35 +05304860 if (!netif_running(netdev))
4861 return 0;
4862
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004863 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004864 spin_lock_irqsave(&bp->lock, flags);
4865 /* Flush all status bits */
4866 macb_writel(bp, TSR, -1);
4867 macb_writel(bp, RSR, -1);
Harini Katakamde991c52019-03-01 16:20:35 +05304868 for (q = 0, queue = bp->queues; q < bp->num_queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004869 ++q, ++queue) {
4870 /* Disable all interrupts */
4871 queue_writel(queue, IDR, -1);
4872 queue_readl(queue, ISR);
4873 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4874 queue_writel(queue, ISR, -1);
4875 }
4876 /* Change interrupt handler and
4877 * Enable WoL IRQ on queue 0
4878 */
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004879 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004880 if (macb_is_gem(bp)) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004881 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
4882 IRQF_SHARED, netdev->name, bp->queues);
4883 if (err) {
4884 dev_err(dev,
4885 "Unable to request IRQ %d (error %d)\n",
4886 bp->queues[0].irq, err);
4887 spin_unlock_irqrestore(&bp->lock, flags);
4888 return err;
4889 }
4890 queue_writel(bp->queues, IER, GEM_BIT(WOL));
4891 gem_writel(bp, WOL, MACB_BIT(MAG));
4892 } else {
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004893 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
4894 IRQF_SHARED, netdev->name, bp->queues);
4895 if (err) {
4896 dev_err(dev,
4897 "Unable to request IRQ %d (error %d)\n",
4898 bp->queues[0].irq, err);
4899 spin_unlock_irqrestore(&bp->lock, flags);
4900 return err;
4901 }
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004902 queue_writel(bp->queues, IER, MACB_BIT(WOL));
4903 macb_writel(bp, WOL, MACB_BIT(MAG));
4904 }
4905 spin_unlock_irqrestore(&bp->lock, flags);
4906
4907 enable_irq_wake(bp->queues[0].irq);
4908 }
4909
4910 netif_device_detach(netdev);
4911 for (q = 0, queue = bp->queues; q < bp->num_queues;
4912 ++q, ++queue)
4913 napi_disable(&queue->napi);
4914
4915 if (!(bp->wol & MACB_WOL_ENABLED)) {
Antoine Tenart7897b072019-11-13 10:00:06 +01004916 rtnl_lock();
4917 phylink_stop(bp->phylink);
4918 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304919 spin_lock_irqsave(&bp->lock, flags);
4920 macb_reset_hw(bp);
4921 spin_unlock_irqrestore(&bp->lock, flags);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304922 }
4923
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004924 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4925 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4926
4927 if (netdev->hw_features & NETIF_F_NTUPLE)
4928 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4929
Harini Katakamde991c52019-03-01 16:20:35 +05304930 if (bp->ptp_info)
4931 bp->ptp_info->ptp_remove(netdev);
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004932 if (!device_may_wakeup(dev))
4933 pm_runtime_force_suspend(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304934
4935 return 0;
4936}
4937
4938static int __maybe_unused macb_resume(struct device *dev)
4939{
4940 struct net_device *netdev = dev_get_drvdata(dev);
4941 struct macb *bp = netdev_priv(netdev);
Jiapeng Chongbbf6ace2021-04-29 18:25:46 +08004942 struct macb_queue *queue;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004943 unsigned long flags;
Harini Katakamde991c52019-03-01 16:20:35 +05304944 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004945 int err;
Harini Katakamde991c52019-03-01 16:20:35 +05304946
4947 if (!netif_running(netdev))
4948 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304949
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004950 if (!device_may_wakeup(dev))
4951 pm_runtime_force_resume(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304952
4953 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004954 spin_lock_irqsave(&bp->lock, flags);
4955 /* Disable WoL */
4956 if (macb_is_gem(bp)) {
4957 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
4958 gem_writel(bp, WOL, 0);
4959 } else {
4960 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
4961 macb_writel(bp, WOL, 0);
4962 }
4963 /* Clear ISR on queue 0 */
4964 queue_readl(bp->queues, ISR);
4965 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4966 queue_writel(bp->queues, ISR, -1);
4967 /* Replace interrupt handler on queue 0 */
4968 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
4969 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
4970 IRQF_SHARED, netdev->name, bp->queues);
4971 if (err) {
4972 dev_err(dev,
4973 "Unable to request IRQ %d (error %d)\n",
4974 bp->queues[0].irq, err);
4975 spin_unlock_irqrestore(&bp->lock, flags);
4976 return err;
4977 }
4978 spin_unlock_irqrestore(&bp->lock, flags);
4979
Harini Katakamd54f89a2019-03-01 16:20:34 +05304980 disable_irq_wake(bp->queues[0].irq);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004981
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004982 /* Now make sure we disable phy before moving
4983 * to common restore path
4984 */
Antoine Tenart7897b072019-11-13 10:00:06 +01004985 rtnl_lock();
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004986 phylink_stop(bp->phylink);
Antoine Tenart7897b072019-11-13 10:00:06 +01004987 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304988 }
4989
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004990 for (q = 0, queue = bp->queues; q < bp->num_queues;
4991 ++q, ++queue)
4992 napi_enable(&queue->napi);
4993
4994 if (netdev->hw_features & NETIF_F_NTUPLE)
4995 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4996
4997 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4998 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4999
5000 macb_writel(bp, NCR, MACB_BIT(MPE));
Harini Katakamde991c52019-03-01 16:20:35 +05305001 macb_init_hw(bp);
5002 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00005003 macb_restore_features(bp);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02005004 rtnl_lock();
5005 phylink_start(bp->phylink);
5006 rtnl_unlock();
5007
Harini Katakamd54f89a2019-03-01 16:20:34 +05305008 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05305009 if (bp->ptp_info)
5010 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05305011
5012 return 0;
5013}
5014
5015static int __maybe_unused macb_runtime_suspend(struct device *dev)
5016{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005017 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05305018 struct macb *bp = netdev_priv(netdev);
5019
Claudiu Beznea38493da2020-12-09 15:03:34 +02005020 if (!(device_may_wakeup(dev)))
5021 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
5022 else
5023 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005024
5025 return 0;
5026}
5027
Harini Katakamd54f89a2019-03-01 16:20:34 +05305028static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005029{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005030 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005031 struct macb *bp = netdev_priv(netdev);
5032
Nicolas Ferre515a10a2020-07-10 14:46:41 +02005033 if (!(device_may_wakeup(dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005034 clk_prepare_enable(bp->pclk);
5035 clk_prepare_enable(bp->hclk);
5036 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05305037 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005038 }
Harini Katakamf5473d12019-03-01 16:20:33 +05305039 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005040
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005041 return 0;
5042}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005043
Harini Katakamd54f89a2019-03-01 16:20:34 +05305044static const struct dev_pm_ops macb_pm_ops = {
5045 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
5046 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
5047};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005048
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005049static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005050 .probe = macb_probe,
5051 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005052 .driver = {
5053 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01005054 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005055 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005056 },
5057};
5058
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005059module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005060
5061MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00005062MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02005063MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07005064MODULE_ALIAS("platform:macb");