blob: 81704985a79bc2657ad356d976547e73f9b5261a [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
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200470 if (!bp->tx_clk || !(bp->caps & MACB_CAPS_CLK_HW_CHG))
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100471 return;
472
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800473 switch (speed) {
474 case SPEED_10:
475 rate = 2500000;
476 break;
477 case SPEED_100:
478 rate = 25000000;
479 break;
480 case SPEED_1000:
481 rate = 125000000;
482 break;
483 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800484 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800485 }
486
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200487 rate_rounded = clk_round_rate(bp->tx_clk, rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800488 if (rate_rounded < 0)
489 return;
490
491 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
492 * is not satisfied.
493 */
494 ferr = abs(rate_rounded - rate);
495 ferr = DIV_ROUND_UP(ferr, rate / 100000);
496 if (ferr > 5)
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200497 netdev_warn(bp->dev,
498 "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700499 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800500
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200501 if (clk_set_rate(bp->tx_clk, rate_rounded))
502 netdev_err(bp->dev, "adjusting tx_clk failed.\n");
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800503}
504
Antoine Tenart7897b072019-11-13 10:00:06 +0100505static void macb_validate(struct phylink_config *config,
506 unsigned long *supported,
507 struct phylink_link_state *state)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100508{
Antoine Tenart7897b072019-11-13 10:00:06 +0100509 struct net_device *ndev = to_net_dev(config->dev);
510 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
511 struct macb *bp = netdev_priv(ndev);
512
513 /* We only support MII, RMII, GMII, RGMII & SGMII. */
514 if (state->interface != PHY_INTERFACE_MODE_NA &&
515 state->interface != PHY_INTERFACE_MODE_MII &&
516 state->interface != PHY_INTERFACE_MODE_RMII &&
517 state->interface != PHY_INTERFACE_MODE_GMII &&
518 state->interface != PHY_INTERFACE_MODE_SGMII &&
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100519 state->interface != PHY_INTERFACE_MODE_10GBASER &&
Antoine Tenart7897b072019-11-13 10:00:06 +0100520 !phy_interface_mode_is_rgmii(state->interface)) {
521 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
522 return;
523 }
524
525 if (!macb_is_gem(bp) &&
526 (state->interface == PHY_INTERFACE_MODE_GMII ||
527 phy_interface_mode_is_rgmii(state->interface))) {
528 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
529 return;
530 }
531
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100532 if (state->interface == PHY_INTERFACE_MODE_10GBASER &&
533 !(bp->caps & MACB_CAPS_HIGH_SPEED &&
534 bp->caps & MACB_CAPS_PCS)) {
535 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
536 return;
537 }
538
Antoine Tenart7897b072019-11-13 10:00:06 +0100539 phylink_set_port_modes(mask);
540 phylink_set(mask, Autoneg);
541 phylink_set(mask, Asym_Pause);
542
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100543 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
544 (state->interface == PHY_INTERFACE_MODE_NA ||
545 state->interface == PHY_INTERFACE_MODE_10GBASER)) {
546 phylink_set(mask, 10000baseCR_Full);
547 phylink_set(mask, 10000baseER_Full);
548 phylink_set(mask, 10000baseKR_Full);
549 phylink_set(mask, 10000baseLR_Full);
550 phylink_set(mask, 10000baseLRM_Full);
551 phylink_set(mask, 10000baseSR_Full);
552 phylink_set(mask, 10000baseT_Full);
553 if (state->interface != PHY_INTERFACE_MODE_NA)
554 goto out;
555 }
556
Antoine Tenart7897b072019-11-13 10:00:06 +0100557 phylink_set(mask, 10baseT_Half);
558 phylink_set(mask, 10baseT_Full);
559 phylink_set(mask, 100baseT_Half);
560 phylink_set(mask, 100baseT_Full);
561
562 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
563 (state->interface == PHY_INTERFACE_MODE_NA ||
564 state->interface == PHY_INTERFACE_MODE_GMII ||
565 state->interface == PHY_INTERFACE_MODE_SGMII ||
566 phy_interface_mode_is_rgmii(state->interface))) {
567 phylink_set(mask, 1000baseT_Full);
568 phylink_set(mask, 1000baseX_Full);
569
570 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
571 phylink_set(mask, 1000baseT_Half);
572 }
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100573out:
Antoine Tenart7897b072019-11-13 10:00:06 +0100574 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
575 bitmap_and(state->advertising, state->advertising, mask,
576 __ETHTOOL_LINK_MODE_MASK_NBITS);
577}
578
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100579static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
580 phy_interface_t interface, int speed,
581 int duplex)
582{
583 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
584 u32 config;
585
586 config = gem_readl(bp, USX_CONTROL);
587 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
588 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
589 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
590 config |= GEM_BIT(TX_EN);
591 gem_writel(bp, USX_CONTROL, config);
592}
593
594static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
Russell Kingd46b7e42019-11-21 00:36:22 +0000595 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100596{
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100597 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
598 u32 val;
599
600 state->speed = SPEED_10000;
601 state->duplex = 1;
602 state->an_complete = 1;
603
604 val = gem_readl(bp, USX_STATUS);
605 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
606 val = gem_readl(bp, NCFGR);
607 if (val & GEM_BIT(PAE))
608 state->pause = MLO_PAUSE_RX;
609}
610
611static int macb_usx_pcs_config(struct phylink_pcs *pcs,
612 unsigned int mode,
613 phy_interface_t interface,
614 const unsigned long *advertising,
615 bool permit_pause_to_mac)
616{
617 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
618
619 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
620 GEM_BIT(SIGNAL_OK));
621
622 return 0;
623}
624
625static void macb_pcs_get_state(struct phylink_pcs *pcs,
626 struct phylink_link_state *state)
627{
Russell Kingd46b7e42019-11-21 00:36:22 +0000628 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100629}
630
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100631static void macb_pcs_an_restart(struct phylink_pcs *pcs)
Antoine Tenart7897b072019-11-13 10:00:06 +0100632{
633 /* Not supported */
634}
635
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100636static int macb_pcs_config(struct phylink_pcs *pcs,
637 unsigned int mode,
638 phy_interface_t interface,
639 const unsigned long *advertising,
640 bool permit_pause_to_mac)
641{
642 return 0;
643}
644
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100645static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
646 .pcs_get_state = macb_usx_pcs_get_state,
647 .pcs_config = macb_usx_pcs_config,
648 .pcs_link_up = macb_usx_pcs_link_up,
649};
650
651static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
652 .pcs_get_state = macb_pcs_get_state,
653 .pcs_an_restart = macb_pcs_an_restart,
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100654 .pcs_config = macb_pcs_config,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100655};
656
Antoine Tenart7897b072019-11-13 10:00:06 +0100657static void macb_mac_config(struct phylink_config *config, unsigned int mode,
658 const struct phylink_link_state *state)
659{
660 struct net_device *ndev = to_net_dev(config->dev);
661 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200662 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100663 u32 old_ctrl, ctrl;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100664 u32 old_ncr, ncr;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100665
frederic RODO6c36a702007-07-12 19:07:24 +0200666 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100667
Antoine Tenart7897b072019-11-13 10:00:06 +0100668 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100669 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100670
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100671 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
672 if (state->interface == PHY_INTERFACE_MODE_RMII)
673 ctrl |= MACB_BIT(RM9200_RMII);
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200674 } else if (macb_is_gem(bp)) {
Russell King633e98a2020-02-26 10:24:06 +0000675 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100676 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100677
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100678 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100679 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100680 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
681 ctrl |= GEM_BIT(PCSSEL);
682 ncr |= GEM_BIT(ENABLE_HS_MAC);
683 }
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100684 }
frederic RODO6c36a702007-07-12 19:07:24 +0200685
Antoine Tenart7897b072019-11-13 10:00:06 +0100686 /* Apply the new configuration, if any */
687 if (old_ctrl ^ ctrl)
688 macb_or_gem_writel(bp, NCFGR, ctrl);
689
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100690 if (old_ncr ^ ncr)
691 macb_or_gem_writel(bp, NCR, ncr);
692
frederic RODO6c36a702007-07-12 19:07:24 +0200693 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200694}
695
Antoine Tenart7897b072019-11-13 10:00:06 +0100696static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
697 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200698{
Antoine Tenart7897b072019-11-13 10:00:06 +0100699 struct net_device *ndev = to_net_dev(config->dev);
700 struct macb *bp = netdev_priv(ndev);
701 struct macb_queue *queue;
702 unsigned int q;
703 u32 ctrl;
704
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100705 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
706 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
707 queue_writel(queue, IDR,
708 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100709
710 /* Disable Rx and Tx */
711 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
712 macb_writel(bp, NCR, ctrl);
713
714 netif_tx_stop_all_queues(ndev);
715}
716
Russell King91a208f2020-02-26 10:23:41 +0000717static void macb_mac_link_up(struct phylink_config *config,
718 struct phy_device *phy,
719 unsigned int mode, phy_interface_t interface,
720 int speed, int duplex,
721 bool tx_pause, bool rx_pause)
Antoine Tenart7897b072019-11-13 10:00:06 +0100722{
723 struct net_device *ndev = to_net_dev(config->dev);
724 struct macb *bp = netdev_priv(ndev);
725 struct macb_queue *queue;
Russell King633e98a2020-02-26 10:24:06 +0000726 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100727 unsigned int q;
Russell King633e98a2020-02-26 10:24:06 +0000728 u32 ctrl;
729
730 spin_lock_irqsave(&bp->lock, flags);
731
732 ctrl = macb_or_gem_readl(bp, NCFGR);
733
734 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
735
736 if (speed == SPEED_100)
737 ctrl |= MACB_BIT(SPD);
738
739 if (duplex)
740 ctrl |= MACB_BIT(FD);
Antoine Tenart7897b072019-11-13 10:00:06 +0100741
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100742 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200743 ctrl &= ~MACB_BIT(PAE);
744 if (macb_is_gem(bp)) {
745 ctrl &= ~GEM_BIT(GBE);
Russell King633e98a2020-02-26 10:24:06 +0000746
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200747 if (speed == SPEED_1000)
748 ctrl |= GEM_BIT(GBE);
749 }
Russell King633e98a2020-02-26 10:24:06 +0000750
Parshuram Thombared7739b02020-09-05 10:21:33 +0200751 if (rx_pause)
Russell King633e98a2020-02-26 10:24:06 +0000752 ctrl |= MACB_BIT(PAE);
753
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200754 macb_set_tx_clk(bp, speed);
Antoine Tenart7897b072019-11-13 10:00:06 +0100755
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100756 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
757 * cleared the pipeline and control registers.
758 */
759 bp->macbgem_ops.mog_init_rings(bp);
760 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100761
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100762 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
763 queue_writel(queue, IER,
764 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
765 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100766
Russell King633e98a2020-02-26 10:24:06 +0000767 macb_or_gem_writel(bp, NCFGR, ctrl);
768
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100769 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
770 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
771 gem_readl(bp, HS_MAC_CONFIG)));
772
Russell King633e98a2020-02-26 10:24:06 +0000773 spin_unlock_irqrestore(&bp->lock, flags);
774
Antoine Tenart7897b072019-11-13 10:00:06 +0100775 /* Enable Rx and Tx */
776 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
777
778 netif_tx_wake_all_queues(ndev);
779}
780
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100781static int macb_mac_prepare(struct phylink_config *config, unsigned int mode,
782 phy_interface_t interface)
783{
784 struct net_device *ndev = to_net_dev(config->dev);
785 struct macb *bp = netdev_priv(ndev);
786
787 if (interface == PHY_INTERFACE_MODE_10GBASER)
788 bp->phylink_pcs.ops = &macb_phylink_usx_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100789 else if (interface == PHY_INTERFACE_MODE_SGMII)
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100790 bp->phylink_pcs.ops = &macb_phylink_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100791 else
792 bp->phylink_pcs.ops = NULL;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100793
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100794 if (bp->phylink_pcs.ops)
795 phylink_set_pcs(bp->phylink, &bp->phylink_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100796
797 return 0;
798}
799
Antoine Tenart7897b072019-11-13 10:00:06 +0100800static const struct phylink_mac_ops macb_phylink_ops = {
801 .validate = macb_validate,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100802 .mac_prepare = macb_mac_prepare,
Antoine Tenart7897b072019-11-13 10:00:06 +0100803 .mac_config = macb_mac_config,
804 .mac_link_down = macb_mac_link_down,
805 .mac_link_up = macb_mac_link_up,
806};
807
Milind Parabfd2a8912020-01-13 03:30:43 +0000808static bool macb_phy_handle_exists(struct device_node *dn)
809{
810 dn = of_parse_phandle(dn, "phy-handle", 0);
811 of_node_put(dn);
812 return dn != NULL;
813}
814
Antoine Tenart7897b072019-11-13 10:00:06 +0100815static int macb_phylink_connect(struct macb *bp)
816{
Milind Parabfd2a8912020-01-13 03:30:43 +0000817 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100818 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000819 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100820 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500821
Milind Parabfd2a8912020-01-13 03:30:43 +0000822 if (dn)
823 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
824
825 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200826 phydev = phy_find_first(bp->mii_bus);
827 if (!phydev) {
828 netdev_err(dev, "no PHY found\n");
829 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000830 }
frederic RODO6c36a702007-07-12 19:07:24 +0200831
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200832 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100833 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000834 }
835
836 if (ret) {
837 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
838 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200839 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840
Antoine Tenart7897b072019-11-13 10:00:06 +0100841 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100842
Antoine Tenart7897b072019-11-13 10:00:06 +0100843 return 0;
844}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100845
Antoine Tenart7897b072019-11-13 10:00:06 +0100846/* based on au1000_eth. c*/
847static int macb_mii_probe(struct net_device *dev)
848{
849 struct macb *bp = netdev_priv(dev);
850
851 bp->phylink_config.dev = &dev->dev;
852 bp->phylink_config.type = PHYLINK_NETDEV;
853
854 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
855 bp->phy_interface, &macb_phylink_ops);
856 if (IS_ERR(bp->phylink)) {
857 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
858 PTR_ERR(bp->phylink));
859 return PTR_ERR(bp->phylink);
860 }
frederic RODO6c36a702007-07-12 19:07:24 +0200861
862 return 0;
863}
864
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100865static int macb_mdiobus_register(struct macb *bp)
866{
867 struct device_node *child, *np = bp->pdev->dev.of_node;
868
Codrin Ciubotariu79540d12020-03-31 12:39:35 +0300869 if (of_phy_is_fixed_link(np))
870 return mdiobus_register(bp->mii_bus);
871
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100872 /* Only create the PHY from the device tree if at least one PHY is
873 * described. Otherwise scan the entire MDIO bus. We do this to support
874 * old device tree that did not follow the best practices and did not
875 * describe their network PHYs.
876 */
877 for_each_available_child_of_node(np, child)
878 if (of_mdiobus_child_is_phy(child)) {
879 /* The loop increments the child refcount,
880 * decrement it before returning.
881 */
882 of_node_put(child);
883
884 return of_mdiobus_register(bp->mii_bus, np);
885 }
886
887 return mdiobus_register(bp->mii_bus);
888}
889
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100890static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200891{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200892 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200893
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200894 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200895 macb_writel(bp, NCR, MACB_BIT(MPE));
896
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700897 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700898 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200899 err = -ENOMEM;
900 goto err_out;
901 }
902
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700903 bp->mii_bus->name = "MACB_mii_bus";
904 bp->mii_bus->read = &macb_mdio_read;
905 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000906 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700907 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700908 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700909 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700910
Jamie Iles91523942011-02-28 04:05:25 +0000911 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200912
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100913 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200914 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100915 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200916
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200917 err = macb_mii_probe(bp->dev);
918 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200919 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200920
921 return 0;
922
923err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700924 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500925err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700926 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200927err_out:
928 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100929}
930
931static void macb_update_stats(struct macb *bp)
932{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000933 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
934 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300935 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100936
937 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
938
Moritz Fischer96ec6312016-03-29 19:11:11 -0700939 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700940 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100941}
942
Nicolas Ferree86cd532012-10-31 06:04:57 +0000943static int macb_halt_tx(struct macb *bp)
944{
945 unsigned long halt_time, timeout;
946 u32 status;
947
948 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
949
950 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
951 do {
952 halt_time = jiffies;
953 status = macb_readl(bp, TSR);
954 if (!(status & MACB_BIT(TGO)))
955 return 0;
956
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800957 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000958 } while (time_before(halt_time, timeout));
959
960 return -ETIMEDOUT;
961}
962
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200963static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
964{
965 if (tx_skb->mapping) {
966 if (tx_skb->mapped_as_page)
967 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
968 tx_skb->size, DMA_TO_DEVICE);
969 else
970 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
971 tx_skb->size, DMA_TO_DEVICE);
972 tx_skb->mapping = 0;
973 }
974
975 if (tx_skb->skb) {
976 dev_kfree_skb_any(tx_skb->skb);
977 tx_skb->skb = NULL;
978 }
979}
980
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000981static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530982{
Harini Katakamfff80192016-08-09 13:15:53 +0530983#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000984 struct macb_dma_desc_64 *desc_64;
985
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100986 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000987 desc_64 = macb_64b_desc(bp, desc);
988 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200989 /* The low bits of RX address contain the RX_USED bit, clearing
990 * of which allows packet RX. Make sure the high bits are also
991 * visible to HW at that point.
992 */
993 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000994 }
Harini Katakamfff80192016-08-09 13:15:53 +0530995#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000996 desc->addr = lower_32_bits(addr);
997}
998
999static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1000{
1001 dma_addr_t addr = 0;
1002#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1003 struct macb_dma_desc_64 *desc_64;
1004
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001005 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001006 desc_64 = macb_64b_desc(bp, desc);
1007 addr = ((u64)(desc_64->addrh) << 32);
1008 }
1009#endif
1010 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1011 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +05301012}
1013
Nicolas Ferree86cd532012-10-31 06:04:57 +00001014static void macb_tx_error_task(struct work_struct *work)
1015{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001016 struct macb_queue *queue = container_of(work, struct macb_queue,
1017 tx_error_task);
1018 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001019 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001020 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001021 struct sk_buff *skb;
1022 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001023 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001024
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001025 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
1026 (unsigned int)(queue - bp->queues),
1027 queue->tx_tail, queue->tx_head);
1028
1029 /* Prevent the queue IRQ handlers from running: each of them may call
1030 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
1031 * As explained below, we have to halt the transmission before updating
1032 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1033 * network engine about the macb/gem being halted.
1034 */
1035 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001036
1037 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001038 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001039
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001040 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +00001041 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001042 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +00001043 */
1044 if (macb_halt_tx(bp))
1045 /* Just complain for now, reinitializing TX path can be good */
1046 netdev_err(bp->dev, "BUG: halt tx timed out\n");
1047
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001048 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001049 * Free transmit buffers in upper layer.
1050 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001051 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1052 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001053
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001054 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001055 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001056 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001057 skb = tx_skb->skb;
1058
1059 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001060 /* skb is set for the last buffer of the frame */
1061 while (!skb) {
1062 macb_tx_unmap(bp, tx_skb);
1063 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001064 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001065 skb = tx_skb->skb;
1066 }
1067
1068 /* ctrl still refers to the first buffer descriptor
1069 * since it's the only one written back by the hardware
1070 */
1071 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1072 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001073 macb_tx_ring_wrap(bp, tail),
1074 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001075 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001076 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001077 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001078 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001079 }
Nicolas Ferree86cd532012-10-31 06:04:57 +00001080 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001081 /* "Buffers exhausted mid-frame" errors may only happen
1082 * if the driver is buggy, so complain loudly about
1083 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001084 */
1085 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1086 netdev_err(bp->dev,
1087 "BUG: TX buffers exhausted mid-frame\n");
1088
1089 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1090 }
1091
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001092 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001093 }
1094
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001095 /* Set end of TX queue */
1096 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001097 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001098 desc->ctrl = MACB_BIT(TX_USED);
1099
Nicolas Ferree86cd532012-10-31 06:04:57 +00001100 /* Make descriptor updates visible to hardware */
1101 wmb();
1102
1103 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001104 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301105#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001106 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001107 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301108#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +00001109 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001110 queue->tx_head = 0;
1111 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001112
1113 /* Housework before enabling TX IRQ */
1114 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001115 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1116
1117 /* Now we are ready to start transmission again */
1118 netif_tx_start_all_queues(bp->dev);
1119 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1120
1121 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001122}
1123
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001124static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001125{
1126 unsigned int tail;
1127 unsigned int head;
1128 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001129 struct macb *bp = queue->bp;
1130 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131
1132 status = macb_readl(bp, TSR);
1133 macb_writel(bp, TSR, status);
1134
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001135 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001136 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +00001137
Nicolas Ferree86cd532012-10-31 06:04:57 +00001138 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001139 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001140
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001141 head = queue->tx_head;
1142 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001143 struct macb_tx_skb *tx_skb;
1144 struct sk_buff *skb;
1145 struct macb_dma_desc *desc;
1146 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001147
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001148 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001149
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001150 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001151 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001152
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001153 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001154
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001155 /* TX_USED bit is only set by hardware on the very first buffer
1156 * descriptor of the transmitted frame.
1157 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001158 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001159 break;
1160
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001161 /* Process all buffers of the current transmitted frame */
1162 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001163 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001164 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001165
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001166 /* First, update TX stats if needed */
1167 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001168 if (unlikely(skb_shinfo(skb)->tx_flags &
1169 SKBTX_HW_TSTAMP) &&
1170 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001171 /* skb now belongs to timestamp buffer
1172 * and will be removed later
1173 */
1174 tx_skb->skb = NULL;
1175 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001176 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001177 macb_tx_ring_wrap(bp, tail),
1178 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001179 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001180 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001181 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001182 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001183 }
1184
1185 /* Now we can safely release resources */
1186 macb_tx_unmap(bp, tx_skb);
1187
1188 /* skb is set only for the last buffer of the frame.
1189 * WARNING: at this point skb has been freed by
1190 * macb_tx_unmap().
1191 */
1192 if (skb)
1193 break;
1194 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001195 }
1196
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001197 queue->tx_tail = tail;
1198 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1199 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001200 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001201 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001202}
1203
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001204static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001205{
1206 unsigned int entry;
1207 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001208 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001209 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001210 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001211
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001212 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1213 bp->rx_ring_size) > 0) {
1214 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001215
1216 /* Make hw descriptor updates visible to CPU */
1217 rmb();
1218
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001219 queue->rx_prepared_head++;
1220 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001221
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001222 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001223 /* allocate sk_buff for this free entry in ring */
1224 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001225 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001226 netdev_err(bp->dev,
1227 "Unable to allocate sk_buff\n");
1228 break;
1229 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001230
1231 /* now fill corresponding descriptor entry */
1232 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001233 bp->rx_buffer_size,
1234 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001235 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1236 dev_kfree_skb(skb);
1237 break;
1238 }
1239
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001240 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001241
Zach Brownb410d132016-10-19 09:56:57 -05001242 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001243 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001244 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001245 /* Setting addr clears RX_USED and allows reception,
1246 * make sure ctrl is cleared first to avoid a race.
1247 */
1248 dma_wmb();
1249 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001250
1251 /* properly align Ethernet header */
1252 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301253 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001254 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001255 dma_wmb();
1256 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001257 }
1258 }
1259
1260 /* Make descriptor updates visible to hardware */
1261 wmb();
1262
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001263 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1264 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001265}
1266
1267/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001268static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001269 unsigned int end)
1270{
1271 unsigned int frag;
1272
1273 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001274 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001275
Nicolas Ferre4df95132013-06-04 21:57:12 +00001276 desc->addr &= ~MACB_BIT(RX_USED);
1277 }
1278
1279 /* Make descriptor updates visible to hardware */
1280 wmb();
1281
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001282 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001283 * whatever caused this is updated, so we don't have to record
1284 * anything.
1285 */
1286}
1287
Antoine Tenart97236cd2019-06-21 17:30:02 +02001288static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1289 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001290{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001291 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001292 unsigned int len;
1293 unsigned int entry;
1294 struct sk_buff *skb;
1295 struct macb_dma_desc *desc;
1296 int count = 0;
1297
1298 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301299 u32 ctrl;
1300 dma_addr_t addr;
1301 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001302
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001303 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1304 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001305
1306 /* Make hw descriptor updates visible to CPU */
1307 rmb();
1308
Harini Katakamfff80192016-08-09 13:15:53 +05301309 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001310 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001311
Harini Katakamfff80192016-08-09 13:15:53 +05301312 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001313 break;
1314
Anssi Hannula6e0af292018-12-17 15:05:41 +02001315 /* Ensure ctrl is at least as up-to-date as rxused */
1316 dma_rmb();
1317
1318 ctrl = desc->ctrl;
1319
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001320 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001321 count++;
1322
1323 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1324 netdev_err(bp->dev,
1325 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001326 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001327 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001328 break;
1329 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001330 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001331 if (unlikely(!skb)) {
1332 netdev_err(bp->dev,
1333 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001334 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001335 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001336 break;
1337 }
1338 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001339 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301340 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001341
1342 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1343
1344 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001345 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001346 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001347
1348 skb->protocol = eth_type_trans(skb, bp->dev);
1349 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001350 if (bp->dev->features & NETIF_F_RXCSUM &&
1351 !(bp->dev->flags & IFF_PROMISC) &&
1352 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1353 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001354
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001355 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001356 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001357 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001358 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001359
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001360 gem_ptp_do_rxstamp(bp, skb, desc);
1361
Nicolas Ferre4df95132013-06-04 21:57:12 +00001362#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1363 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1364 skb->len, skb->csum);
1365 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001366 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001367 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1368 skb->data, 32, true);
1369#endif
1370
Antoine Tenart97236cd2019-06-21 17:30:02 +02001371 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001372 }
1373
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001374 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001375
1376 return count;
1377}
1378
Antoine Tenart97236cd2019-06-21 17:30:02 +02001379static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1380 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001381{
1382 unsigned int len;
1383 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001384 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001385 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001386 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001387 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001389 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301390 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001391
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001392 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001393 macb_rx_ring_wrap(bp, first_frag),
1394 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001395
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001396 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001397 * first buffer. Since the header is 14 bytes, this makes the
1398 * payload word-aligned.
1399 *
1400 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1401 * the two padding bytes into the skb so that we avoid hitting
1402 * the slowpath in memcpy(), and pull them off afterwards.
1403 */
1404 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001405 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001406 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001407 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001408 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001409 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001410 if (frag == last_frag)
1411 break;
1412 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001413
1414 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001415 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001416
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001417 return 1;
1418 }
1419
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001420 offset = 0;
1421 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001422 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001423 skb_put(skb, len);
1424
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001425 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001426 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427
1428 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001429 if (unlikely(frag != last_frag)) {
1430 dev_kfree_skb_any(skb);
1431 return -1;
1432 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001433 frag_len = len - offset;
1434 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001435 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001436 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001437 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001438 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001439 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001440 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001441
1442 if (frag == last_frag)
1443 break;
1444 }
1445
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001446 /* Make descriptor updates visible to hardware */
1447 wmb();
1448
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001449 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001450 skb->protocol = eth_type_trans(skb, bp->dev);
1451
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001452 bp->dev->stats.rx_packets++;
1453 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001454 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001455 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001456 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001457
1458 return 0;
1459}
1460
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001461static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001462{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001463 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001464 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001465 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001466 int i;
1467
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001468 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001469 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001470 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001471 macb_set_addr(bp, desc, addr);
1472 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001473 addr += bp->rx_buffer_size;
1474 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001475 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001476 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001477}
1478
Antoine Tenart97236cd2019-06-21 17:30:02 +02001479static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1480 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001481{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001482 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001483 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001484 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001485 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001486 int first_frag = -1;
1487
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001488 for (tail = queue->rx_tail; budget > 0; tail++) {
1489 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001490 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001491
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001492 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001493 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001494
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001495 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001496 break;
1497
Anssi Hannula6e0af292018-12-17 15:05:41 +02001498 /* Ensure ctrl is at least as up-to-date as addr */
1499 dma_rmb();
1500
1501 ctrl = desc->ctrl;
1502
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001503 if (ctrl & MACB_BIT(RX_SOF)) {
1504 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001505 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001506 first_frag = tail;
1507 }
1508
1509 if (ctrl & MACB_BIT(RX_EOF)) {
1510 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001511
1512 if (unlikely(first_frag == -1)) {
1513 reset_rx_queue = true;
1514 continue;
1515 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001516
Antoine Tenart97236cd2019-06-21 17:30:02 +02001517 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001518 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001519 if (unlikely(dropped < 0)) {
1520 reset_rx_queue = true;
1521 continue;
1522 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001523 if (!dropped) {
1524 received++;
1525 budget--;
1526 }
1527 }
1528 }
1529
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001530 if (unlikely(reset_rx_queue)) {
1531 unsigned long flags;
1532 u32 ctrl;
1533
1534 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1535
1536 spin_lock_irqsave(&bp->lock, flags);
1537
1538 ctrl = macb_readl(bp, NCR);
1539 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1540
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001541 macb_init_rx_ring(queue);
1542 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001543
1544 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1545
1546 spin_unlock_irqrestore(&bp->lock, flags);
1547 return received;
1548 }
1549
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001550 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001551 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001552 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001553 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001554
1555 return received;
1556}
1557
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001558static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001559{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001560 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1561 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001562 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001563 u32 status;
1564
1565 status = macb_readl(bp, RSR);
1566 macb_writel(bp, RSR, status);
1567
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001568 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001569 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001570
Antoine Tenart97236cd2019-06-21 17:30:02 +02001571 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001572 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001573 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001574
Nicolas Ferre8770e912013-02-12 11:08:48 +01001575 /* Packets received while interrupts were disabled */
1576 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001577 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001578 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001579 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001580 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001581 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301582 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001583 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001584 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001585
1586 /* TODO: Handle errors */
1587
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001588 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001589}
1590
Allen Paise7412b82020-09-14 12:59:23 +05301591static void macb_hresp_error_task(struct tasklet_struct *t)
Harini Katakam032dc412018-01-27 12:09:01 +05301592{
Allen Paise7412b82020-09-14 12:59:23 +05301593 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
Harini Katakam032dc412018-01-27 12:09:01 +05301594 struct net_device *dev = bp->dev;
Claudiu Beznea580d3952020-07-02 12:06:00 +03001595 struct macb_queue *queue;
Harini Katakam032dc412018-01-27 12:09:01 +05301596 unsigned int q;
1597 u32 ctrl;
1598
1599 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301600 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301601 MACB_TX_INT_FLAGS |
1602 MACB_BIT(HRESP));
1603 }
1604 ctrl = macb_readl(bp, NCR);
1605 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1606 macb_writel(bp, NCR, ctrl);
1607
1608 netif_tx_stop_all_queues(dev);
1609 netif_carrier_off(dev);
1610
1611 bp->macbgem_ops.mog_init_rings(bp);
1612
1613 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001614 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301615
Antoine Tenart6e952d92019-11-13 10:00:05 +01001616 /* Enable interrupts */
1617 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301618 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301619 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301620 MACB_TX_INT_FLAGS |
1621 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301622
1623 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1624 macb_writel(bp, NCR, ctrl);
1625
1626 netif_carrier_on(dev);
1627 netif_tx_start_all_queues(dev);
1628}
1629
Claudiu Beznea42983882018-12-17 10:02:42 +00001630static void macb_tx_restart(struct macb_queue *queue)
1631{
1632 unsigned int head = queue->tx_head;
1633 unsigned int tail = queue->tx_tail;
1634 struct macb *bp = queue->bp;
1635
1636 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1637 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1638
1639 if (head == tail)
1640 return;
1641
1642 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1643}
1644
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02001645static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
1646{
1647 struct macb_queue *queue = dev_id;
1648 struct macb *bp = queue->bp;
1649 u32 status;
1650
1651 status = queue_readl(queue, ISR);
1652
1653 if (unlikely(!status))
1654 return IRQ_NONE;
1655
1656 spin_lock(&bp->lock);
1657
1658 if (status & MACB_BIT(WOL)) {
1659 queue_writel(queue, IDR, MACB_BIT(WOL));
1660 macb_writel(bp, WOL, 0);
1661 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
1662 (unsigned int)(queue - bp->queues),
1663 (unsigned long)status);
1664 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1665 queue_writel(queue, ISR, MACB_BIT(WOL));
1666 pm_wakeup_event(&bp->pdev->dev, 0);
1667 }
1668
1669 spin_unlock(&bp->lock);
1670
1671 return IRQ_HANDLED;
1672}
1673
Nicolas Ferre558e35c2020-07-20 10:56:52 +02001674static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
1675{
1676 struct macb_queue *queue = dev_id;
1677 struct macb *bp = queue->bp;
1678 u32 status;
1679
1680 status = queue_readl(queue, ISR);
1681
1682 if (unlikely(!status))
1683 return IRQ_NONE;
1684
1685 spin_lock(&bp->lock);
1686
1687 if (status & GEM_BIT(WOL)) {
1688 queue_writel(queue, IDR, GEM_BIT(WOL));
1689 gem_writel(bp, WOL, 0);
1690 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
1691 (unsigned int)(queue - bp->queues),
1692 (unsigned long)status);
1693 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1694 queue_writel(queue, ISR, GEM_BIT(WOL));
1695 pm_wakeup_event(&bp->pdev->dev, 0);
1696 }
1697
1698 spin_unlock(&bp->lock);
1699
1700 return IRQ_HANDLED;
1701}
1702
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001703static irqreturn_t macb_interrupt(int irq, void *dev_id)
1704{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001705 struct macb_queue *queue = dev_id;
1706 struct macb *bp = queue->bp;
1707 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001708 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001709
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001710 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001711
1712 if (unlikely(!status))
1713 return IRQ_NONE;
1714
1715 spin_lock(&bp->lock);
1716
1717 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001718 /* close possible race with dev_close */
1719 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001720 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001721 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1722 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001723 break;
1724 }
1725
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001726 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1727 (unsigned int)(queue - bp->queues),
1728 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001729
Harini Katakame5010702019-01-29 15:20:03 +05301730 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001731 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001732 * until we have processed the buffers. The
1733 * scheduling call may fail if the poll routine
1734 * is already scheduled, so disable interrupts
1735 * now.
1736 */
Harini Katakame5010702019-01-29 15:20:03 +05301737 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001738 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001739 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001740
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001741 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001742 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001743 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001744 }
1745 }
1746
Nicolas Ferree86cd532012-10-31 06:04:57 +00001747 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001748 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1749 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001750
1751 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001752 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001753
Nicolas Ferree86cd532012-10-31 06:04:57 +00001754 break;
1755 }
1756
1757 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001758 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001759
Claudiu Beznea42983882018-12-17 10:02:42 +00001760 if (status & MACB_BIT(TXUBR))
1761 macb_tx_restart(queue);
1762
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001763 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001764 * add that if/when we get our hands on a full-blown MII PHY.
1765 */
1766
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001767 /* There is a hardware issue under heavy load where DMA can
1768 * stop, this causes endless "used buffer descriptor read"
1769 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301770 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1771 * section 16.7.4 for details. RXUBR is only enabled for
1772 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001773 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001774 if (status & MACB_BIT(RXUBR)) {
1775 ctrl = macb_readl(bp, NCR);
1776 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001777 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001778 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1779
1780 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001781 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001782 }
1783
Alexander Steinb19f7f72011-04-13 05:03:24 +00001784 if (status & MACB_BIT(ISR_ROVR)) {
1785 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001786 if (macb_is_gem(bp))
1787 bp->hw_stats.gem.rx_overruns++;
1788 else
1789 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001790
1791 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001792 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001793 }
1794
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001795 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301796 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001797 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001798
1799 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001800 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001801 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001802 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001803 }
1804
1805 spin_unlock(&bp->lock);
1806
1807 return IRQ_HANDLED;
1808}
1809
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001810#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001811/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001812 * to allow network i/o with interrupts disabled.
1813 */
1814static void macb_poll_controller(struct net_device *dev)
1815{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001816 struct macb *bp = netdev_priv(dev);
1817 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001818 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001819 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001820
1821 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001822 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1823 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001824 local_irq_restore(flags);
1825}
1826#endif
1827
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001828static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001829 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001830 struct sk_buff *skb,
1831 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001832{
1833 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001834 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001835 struct macb_tx_skb *tx_skb = NULL;
1836 struct macb_dma_desc *desc;
1837 unsigned int offset, size, count = 0;
1838 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001839 unsigned int eof = 1, mss_mfs = 0;
1840 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1841
1842 /* LSO */
1843 if (skb_shinfo(skb)->gso_size != 0) {
1844 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1845 /* UDP - UFO */
1846 lso_ctrl = MACB_LSO_UFO_ENABLE;
1847 else
1848 /* TCP - TSO */
1849 lso_ctrl = MACB_LSO_TSO_ENABLE;
1850 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001851
1852 /* First, map non-paged data */
1853 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001854
1855 /* first buffer length */
1856 size = hdrlen;
1857
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001858 offset = 0;
1859 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001860 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001861 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001862
1863 mapping = dma_map_single(&bp->pdev->dev,
1864 skb->data + offset,
1865 size, DMA_TO_DEVICE);
1866 if (dma_mapping_error(&bp->pdev->dev, mapping))
1867 goto dma_error;
1868
1869 /* Save info to properly release resources */
1870 tx_skb->skb = NULL;
1871 tx_skb->mapping = mapping;
1872 tx_skb->size = size;
1873 tx_skb->mapped_as_page = false;
1874
1875 len -= size;
1876 offset += size;
1877 count++;
1878 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001879
1880 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001881 }
1882
1883 /* Then, map paged data from fragments */
1884 for (f = 0; f < nr_frags; f++) {
1885 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1886
1887 len = skb_frag_size(frag);
1888 offset = 0;
1889 while (len) {
1890 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001891 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001892 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001893
1894 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1895 offset, size, DMA_TO_DEVICE);
1896 if (dma_mapping_error(&bp->pdev->dev, mapping))
1897 goto dma_error;
1898
1899 /* Save info to properly release resources */
1900 tx_skb->skb = NULL;
1901 tx_skb->mapping = mapping;
1902 tx_skb->size = size;
1903 tx_skb->mapped_as_page = true;
1904
1905 len -= size;
1906 offset += size;
1907 count++;
1908 tx_head++;
1909 }
1910 }
1911
1912 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001913 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001914 netdev_err(bp->dev, "BUG! empty skb!\n");
1915 return 0;
1916 }
1917
1918 /* This is the last buffer of the frame: save socket buffer */
1919 tx_skb->skb = skb;
1920
1921 /* Update TX ring: update buffer descriptors in reverse order
1922 * to avoid race condition
1923 */
1924
1925 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1926 * to set the end of TX queue
1927 */
1928 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001929 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001930 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001931 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001932 desc->ctrl = ctrl;
1933
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001934 if (lso_ctrl) {
1935 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1936 /* include header and FCS in value given to h/w */
1937 mss_mfs = skb_shinfo(skb)->gso_size +
1938 skb_transport_offset(skb) +
1939 ETH_FCS_LEN;
1940 else /* TSO */ {
1941 mss_mfs = skb_shinfo(skb)->gso_size;
1942 /* TCP Sequence Number Source Select
1943 * can be set only for TSO
1944 */
1945 seq_ctrl = 0;
1946 }
1947 }
1948
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001949 do {
1950 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001951 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001952 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001953 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001954
1955 ctrl = (u32)tx_skb->size;
1956 if (eof) {
1957 ctrl |= MACB_BIT(TX_LAST);
1958 eof = 0;
1959 }
Zach Brownb410d132016-10-19 09:56:57 -05001960 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001961 ctrl |= MACB_BIT(TX_WRAP);
1962
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001963 /* First descriptor is header descriptor */
1964 if (i == queue->tx_head) {
1965 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1966 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001967 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1968 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1969 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001970 } else
1971 /* Only set MSS/MFS on payload descriptors
1972 * (second or later descriptor)
1973 */
1974 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1975
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001976 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001977 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001978 /* desc->addr must be visible to hardware before clearing
1979 * 'TX_USED' bit in desc->ctrl.
1980 */
1981 wmb();
1982 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001983 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001984
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001985 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001986
1987 return count;
1988
1989dma_error:
1990 netdev_err(bp->dev, "TX DMA map failed\n");
1991
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001992 for (i = queue->tx_head; i != tx_head; i++) {
1993 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001994
1995 macb_tx_unmap(bp, tx_skb);
1996 }
1997
1998 return 0;
1999}
2000
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002001static netdev_features_t macb_features_check(struct sk_buff *skb,
2002 struct net_device *dev,
2003 netdev_features_t features)
2004{
2005 unsigned int nr_frags, f;
2006 unsigned int hdrlen;
2007
2008 /* Validate LSO compatibility */
2009
Harini Katakam41c1ef92020-02-05 18:08:11 +05302010 /* there is only one buffer or protocol is not UDP */
2011 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002012 return features;
2013
2014 /* length of header */
2015 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002016
Harini Katakam41c1ef92020-02-05 18:08:11 +05302017 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002018 * When software supplies two or more payload buffers all payload buffers
2019 * apart from the last must be a multiple of 8 bytes in size.
2020 */
2021 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2022 return features & ~MACB_NETIF_LSO;
2023
2024 nr_frags = skb_shinfo(skb)->nr_frags;
2025 /* No need to check last fragment */
2026 nr_frags--;
2027 for (f = 0; f < nr_frags; f++) {
2028 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2029
2030 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2031 return features & ~MACB_NETIF_LSO;
2032 }
2033 return features;
2034}
2035
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02002036static inline int macb_clear_csum(struct sk_buff *skb)
2037{
2038 /* no change for packets without checksum offloading */
2039 if (skb->ip_summed != CHECKSUM_PARTIAL)
2040 return 0;
2041
2042 /* make sure we can modify the header */
2043 if (unlikely(skb_cow_head(skb, 0)))
2044 return -1;
2045
2046 /* initialize checksum field
2047 * This is required - at least for Zynq, which otherwise calculates
2048 * wrong UDP header checksums for UDP packets with UDP data len <=2
2049 */
2050 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2051 return 0;
2052}
2053
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002054static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
2055{
Mark Deneen403dc162020-10-30 15:58:14 +00002056 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2057 skb_is_nonlinear(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002058 int padlen = ETH_ZLEN - (*skb)->len;
2059 int headroom = skb_headroom(*skb);
2060 int tailroom = skb_tailroom(*skb);
2061 struct sk_buff *nskb;
2062 u32 fcs;
2063
2064 if (!(ndev->features & NETIF_F_HW_CSUM) ||
2065 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2066 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
2067 return 0;
2068
2069 if (padlen <= 0) {
2070 /* FCS could be appeded to tailroom. */
2071 if (tailroom >= ETH_FCS_LEN)
2072 goto add_fcs;
2073 /* FCS could be appeded by moving data to headroom. */
2074 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
2075 padlen = 0;
2076 /* No room for FCS, need to reallocate skb. */
2077 else
Tristram Ha899ecae2018-10-24 14:51:23 -07002078 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002079 } else {
2080 /* Add room for FCS. */
2081 padlen += ETH_FCS_LEN;
2082 }
2083
2084 if (!cloned && headroom + tailroom >= padlen) {
2085 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
2086 skb_set_tail_pointer(*skb, (*skb)->len);
2087 } else {
2088 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2089 if (!nskb)
2090 return -ENOMEM;
2091
Huang Zijiangf3e5c072019-02-14 14:41:18 +08002092 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002093 *skb = nskb;
2094 }
2095
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00002096 if (padlen > ETH_FCS_LEN)
2097 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002098
2099add_fcs:
2100 /* set FCS to packet */
2101 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2102 fcs = ~fcs;
2103
2104 skb_put_u8(*skb, fcs & 0xff);
2105 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2106 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2107 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2108
2109 return 0;
2110}
2111
Claudiu Beznead1c38952018-08-07 12:25:12 +03002112static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002113{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002114 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002115 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002116 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07002117 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002118 unsigned int desc_cnt, nr_frags, frag_size, f;
2119 unsigned int hdrlen;
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002120 bool is_lso;
Claudiu Beznead1c38952018-08-07 12:25:12 +03002121 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002122
Claudiu Beznea33729f22018-08-07 12:25:13 +03002123 if (macb_clear_csum(skb)) {
2124 dev_kfree_skb_any(skb);
2125 return ret;
2126 }
2127
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002128 if (macb_pad_and_fcs(&skb, dev)) {
2129 dev_kfree_skb_any(skb);
2130 return ret;
2131 }
2132
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002133 is_lso = (skb_shinfo(skb)->gso_size != 0);
2134
2135 if (is_lso) {
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002136 /* length of headers */
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002137 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002138 /* only queue eth + ip headers separately for UDP */
2139 hdrlen = skb_transport_offset(skb);
2140 else
2141 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
2142 if (skb_headlen(skb) < hdrlen) {
2143 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
2144 /* if this is required, would need to copy to single buffer */
2145 return NETDEV_TX_BUSY;
2146 }
2147 } else
2148 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002149
Havard Skinnemoena268adb2012-10-31 06:04:52 +00002150#if defined(DEBUG) && defined(VERBOSE_DEBUG)
2151 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002152 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
2153 queue_index, skb->len, skb->head, skb->data,
2154 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002155 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2156 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002157#endif
2158
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002159 /* Count how many TX buffer descriptors are needed to send this
2160 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07002161 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002162 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002163 if (is_lso && (skb_headlen(skb) > hdrlen))
2164 /* extra header descriptor if also payload in first buffer */
2165 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2166 else
2167 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002168 nr_frags = skb_shinfo(skb)->nr_frags;
2169 for (f = 0; f < nr_frags; f++) {
2170 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002171 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002172 }
2173
Dongdong Deng48719532009-08-23 19:49:07 -07002174 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002175
2176 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05002177 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002178 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002179 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07002180 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002181 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002182 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00002183 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002184 }
2185
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002186 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002187 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07002188 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08002189 goto unlock;
2190 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00002191
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00002192 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002193 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00002194 skb_tx_timestamp(skb);
2195
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002196 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2197
Zach Brownb410d132016-10-19 09:56:57 -05002198 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002199 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002200
Soren Brinkmann92030902014-03-04 08:46:39 -08002201unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002202 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002203
Claudiu Beznead1c38952018-08-07 12:25:12 +03002204 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002205}
2206
Nicolas Ferre4df95132013-06-04 21:57:12 +00002207static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002208{
2209 if (!macb_is_gem(bp)) {
2210 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2211 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002212 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002213
Nicolas Ferre1b447912013-06-04 21:57:11 +00002214 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002215 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002216 "RX buffer must be multiple of %d bytes, expanding\n",
2217 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002218 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002219 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002220 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002221 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002222
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002223 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002224 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002225}
2226
Nicolas Ferre4df95132013-06-04 21:57:12 +00002227static void gem_free_rx_buffers(struct macb *bp)
2228{
2229 struct sk_buff *skb;
2230 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002231 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002232 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002233 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002234 int i;
2235
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002236 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2237 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002238 continue;
2239
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002240 for (i = 0; i < bp->rx_ring_size; i++) {
2241 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002242
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002243 if (!skb)
2244 continue;
2245
2246 desc = macb_rx_desc(queue, i);
2247 addr = macb_get_addr(bp, desc);
2248
2249 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2250 DMA_FROM_DEVICE);
2251 dev_kfree_skb_any(skb);
2252 skb = NULL;
2253 }
2254
2255 kfree(queue->rx_skbuff);
2256 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002257 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002258}
2259
2260static void macb_free_rx_buffers(struct macb *bp)
2261{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002262 struct macb_queue *queue = &bp->queues[0];
2263
2264 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002265 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002266 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002267 queue->rx_buffers, queue->rx_buffers_dma);
2268 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002269 }
2270}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002271
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002272static void macb_free_consistent(struct macb *bp)
2273{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002274 struct macb_queue *queue;
2275 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302276 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002277
Nicolas Ferre4df95132013-06-04 21:57:12 +00002278 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002279
2280 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2281 kfree(queue->tx_skb);
2282 queue->tx_skb = NULL;
2283 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302284 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2285 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002286 queue->tx_ring, queue->tx_ring_dma);
2287 queue->tx_ring = NULL;
2288 }
Harini Katakame50b7702018-07-06 12:18:57 +05302289 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302290 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2291 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302292 queue->rx_ring, queue->rx_ring_dma);
2293 queue->rx_ring = NULL;
2294 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002295 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002296}
2297
2298static int gem_alloc_rx_buffers(struct macb *bp)
2299{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002300 struct macb_queue *queue;
2301 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002302 int size;
2303
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002304 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2305 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2306 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2307 if (!queue->rx_skbuff)
2308 return -ENOMEM;
2309 else
2310 netdev_dbg(bp->dev,
2311 "Allocated %d RX struct sk_buff entries at %p\n",
2312 bp->rx_ring_size, queue->rx_skbuff);
2313 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002314 return 0;
2315}
2316
2317static int macb_alloc_rx_buffers(struct macb *bp)
2318{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002319 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002320 int size;
2321
Zach Brownb410d132016-10-19 09:56:57 -05002322 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002323 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2324 &queue->rx_buffers_dma, GFP_KERNEL);
2325 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002326 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002327
2328 netdev_dbg(bp->dev,
2329 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002330 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002331 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002332}
2333
2334static int macb_alloc_consistent(struct macb *bp)
2335{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002336 struct macb_queue *queue;
2337 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002338 int size;
2339
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002340 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302341 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002342 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2343 &queue->tx_ring_dma,
2344 GFP_KERNEL);
2345 if (!queue->tx_ring)
2346 goto out_err;
2347 netdev_dbg(bp->dev,
2348 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2349 q, size, (unsigned long)queue->tx_ring_dma,
2350 queue->tx_ring);
2351
Zach Brownb410d132016-10-19 09:56:57 -05002352 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002353 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2354 if (!queue->tx_skb)
2355 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002356
Harini Katakam404cd082018-07-06 12:18:58 +05302357 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002358 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2359 &queue->rx_ring_dma, GFP_KERNEL);
2360 if (!queue->rx_ring)
2361 goto out_err;
2362 netdev_dbg(bp->dev,
2363 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2364 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002365 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002366 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002367 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002368
2369 return 0;
2370
2371out_err:
2372 macb_free_consistent(bp);
2373 return -ENOMEM;
2374}
2375
Nicolas Ferre4df95132013-06-04 21:57:12 +00002376static void gem_init_rings(struct macb *bp)
2377{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002378 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002379 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002380 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002381 int i;
2382
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002383 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002384 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002385 desc = macb_tx_desc(queue, i);
2386 macb_set_addr(bp, desc, 0);
2387 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002388 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002389 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002390 queue->tx_head = 0;
2391 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002392
2393 queue->rx_tail = 0;
2394 queue->rx_prepared_head = 0;
2395
2396 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002397 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002398
Nicolas Ferre4df95132013-06-04 21:57:12 +00002399}
2400
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002401static void macb_init_rings(struct macb *bp)
2402{
2403 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002404 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002406 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002407
Zach Brownb410d132016-10-19 09:56:57 -05002408 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002409 desc = macb_tx_desc(&bp->queues[0], i);
2410 macb_set_addr(bp, desc, 0);
2411 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002412 }
Ben Shelton21d35152015-04-22 17:28:54 -05002413 bp->queues[0].tx_head = 0;
2414 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002415 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002416}
2417
2418static void macb_reset_hw(struct macb *bp)
2419{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002420 struct macb_queue *queue;
2421 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002422 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002423
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002424 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002425 * more gracefully?)
2426 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002427 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002428
2429 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002430 ctrl |= MACB_BIT(CLRSTAT);
2431
2432 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002433
2434 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002435 macb_writel(bp, TSR, -1);
2436 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002437
2438 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002439 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2440 queue_writel(queue, IDR, -1);
2441 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002442 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2443 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002444 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002445}
2446
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002447static u32 gem_mdc_clk_div(struct macb *bp)
2448{
2449 u32 config;
2450 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2451
2452 if (pclk_hz <= 20000000)
2453 config = GEM_BF(CLK, GEM_CLK_DIV8);
2454 else if (pclk_hz <= 40000000)
2455 config = GEM_BF(CLK, GEM_CLK_DIV16);
2456 else if (pclk_hz <= 80000000)
2457 config = GEM_BF(CLK, GEM_CLK_DIV32);
2458 else if (pclk_hz <= 120000000)
2459 config = GEM_BF(CLK, GEM_CLK_DIV48);
2460 else if (pclk_hz <= 160000000)
2461 config = GEM_BF(CLK, GEM_CLK_DIV64);
2462 else
2463 config = GEM_BF(CLK, GEM_CLK_DIV96);
2464
2465 return config;
2466}
2467
2468static u32 macb_mdc_clk_div(struct macb *bp)
2469{
2470 u32 config;
2471 unsigned long pclk_hz;
2472
2473 if (macb_is_gem(bp))
2474 return gem_mdc_clk_div(bp);
2475
2476 pclk_hz = clk_get_rate(bp->pclk);
2477 if (pclk_hz <= 20000000)
2478 config = MACB_BF(CLK, MACB_CLK_DIV8);
2479 else if (pclk_hz <= 40000000)
2480 config = MACB_BF(CLK, MACB_CLK_DIV16);
2481 else if (pclk_hz <= 80000000)
2482 config = MACB_BF(CLK, MACB_CLK_DIV32);
2483 else
2484 config = MACB_BF(CLK, MACB_CLK_DIV64);
2485
2486 return config;
2487}
2488
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002489/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002490 * should program. We find the width from decoding the design configuration
2491 * register to find the maximum supported data bus width.
2492 */
2493static u32 macb_dbw(struct macb *bp)
2494{
2495 if (!macb_is_gem(bp))
2496 return 0;
2497
2498 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2499 case 4:
2500 return GEM_BF(DBW, GEM_DBW128);
2501 case 2:
2502 return GEM_BF(DBW, GEM_DBW64);
2503 case 1:
2504 default:
2505 return GEM_BF(DBW, GEM_DBW32);
2506 }
2507}
2508
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002509/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002510 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002511 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002512 * (if not supported by FIFO, it will fallback to default)
2513 * - set both rx/tx packet buffers to full memory size
2514 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002515 */
2516static void macb_configure_dma(struct macb *bp)
2517{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002518 struct macb_queue *queue;
2519 u32 buffer_size;
2520 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002521 u32 dmacfg;
2522
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002523 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002524 if (macb_is_gem(bp)) {
2525 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002526 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2527 if (q)
2528 queue_writel(queue, RBQS, buffer_size);
2529 else
2530 dmacfg |= GEM_BF(RXBS, buffer_size);
2531 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002532 if (bp->dma_burst_length)
2533 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002534 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302535 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302536
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002537 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302538 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2539 else
2540 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2541
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002542 if (bp->dev->features & NETIF_F_HW_CSUM)
2543 dmacfg |= GEM_BIT(TXCOEN);
2544 else
2545 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302546
Michal Simekbd620722018-09-25 08:32:50 +02002547 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302548#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002549 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002550 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302551#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002552#ifdef CONFIG_MACB_USE_HWSTAMP
2553 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2554 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2555#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002556 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2557 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002558 gem_writel(bp, DMACFG, dmacfg);
2559 }
2560}
2561
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002562static void macb_init_hw(struct macb *bp)
2563{
2564 u32 config;
2565
2566 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002567 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002568
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002569 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002570 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002571 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002572 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302573 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2574 else
2575 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002576 if (bp->dev->flags & IFF_PROMISC)
2577 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002578 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2579 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002580 if (!(bp->dev->flags & IFF_BROADCAST))
2581 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002582 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002583 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002584 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302585 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302586 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002587 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302588 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002589
Jamie Iles0116da42011-03-14 17:38:30 +00002590 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002591}
2592
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002593/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002594 * locations in the memory map. The least significant bits are stored
2595 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2596 *
2597 * The unicast hash enable and the multicast hash enable bits in the
2598 * network configuration register enable the reception of hash matched
2599 * frames. The destination address is reduced to a 6 bit index into
2600 * the 64 bit hash register using the following hash function. The
2601 * hash function is an exclusive or of every sixth bit of the
2602 * destination address.
2603 *
2604 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2605 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2606 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2607 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2608 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2609 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2610 *
2611 * da[0] represents the least significant bit of the first byte
2612 * received, that is, the multicast/unicast indicator, and da[47]
2613 * represents the most significant bit of the last byte received. If
2614 * the hash index, hi[n], points to a bit that is set in the hash
2615 * register then the frame will be matched according to whether the
2616 * frame is multicast or unicast. A multicast match will be signalled
2617 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2618 * index points to a bit set in the hash register. A unicast match
2619 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2620 * and the hash index points to a bit set in the hash register. To
2621 * receive all multicast frames, the hash register should be set with
2622 * all ones and the multicast hash enable bit should be set in the
2623 * network configuration register.
2624 */
2625
2626static inline int hash_bit_value(int bitnr, __u8 *addr)
2627{
2628 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2629 return 1;
2630 return 0;
2631}
2632
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002633/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002634static int hash_get_index(__u8 *addr)
2635{
2636 int i, j, bitval;
2637 int hash_index = 0;
2638
2639 for (j = 0; j < 6; j++) {
2640 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002641 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002642
2643 hash_index |= (bitval << j);
2644 }
2645
2646 return hash_index;
2647}
2648
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002649/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002650static void macb_sethashtable(struct net_device *dev)
2651{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002652 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002653 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002654 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002655 struct macb *bp = netdev_priv(dev);
2656
Moritz Fischeraa50b552016-03-29 19:11:13 -07002657 mc_filter[0] = 0;
2658 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002659
Jiri Pirko22bedad32010-04-01 21:22:57 +00002660 netdev_for_each_mc_addr(ha, dev) {
2661 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002662 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2663 }
2664
Jamie Ilesf75ba502011-11-08 10:12:32 +00002665 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2666 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002667}
2668
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002669/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002670static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002671{
2672 unsigned long cfg;
2673 struct macb *bp = netdev_priv(dev);
2674
2675 cfg = macb_readl(bp, NCFGR);
2676
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002677 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002678 /* Enable promiscuous mode */
2679 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002680
2681 /* Disable RX checksum offload */
2682 if (macb_is_gem(bp))
2683 cfg &= ~GEM_BIT(RXCOEN);
2684 } else {
2685 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002686 cfg &= ~MACB_BIT(CAF);
2687
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002688 /* Enable RX checksum offload only if requested */
2689 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2690 cfg |= GEM_BIT(RXCOEN);
2691 }
2692
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002693 if (dev->flags & IFF_ALLMULTI) {
2694 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002695 macb_or_gem_writel(bp, HRB, -1);
2696 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002697 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002698 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002699 /* Enable specific multicasts */
2700 macb_sethashtable(dev);
2701 cfg |= MACB_BIT(NCFGR_MTI);
2702 } else if (dev->flags & (~IFF_ALLMULTI)) {
2703 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002704 macb_or_gem_writel(bp, HRB, 0);
2705 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002706 cfg &= ~MACB_BIT(NCFGR_MTI);
2707 }
2708
2709 macb_writel(bp, NCFGR, cfg);
2710}
2711
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002712static int macb_open(struct net_device *dev)
2713{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002714 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002715 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002716 struct macb_queue *queue;
2717 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002718 int err;
2719
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002720 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002721
Harini Katakamd54f89a2019-03-01 16:20:34 +05302722 err = pm_runtime_get_sync(&bp->pdev->dev);
2723 if (err < 0)
2724 goto pm_exit;
2725
Nicolas Ferre1b447912013-06-04 21:57:11 +00002726 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002727 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002728
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002729 err = macb_alloc_consistent(bp);
2730 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002731 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2732 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302733 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002734 }
2735
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002736 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2737 napi_enable(&queue->napi);
2738
Harini Katakam05044532019-05-07 19:59:10 +05302739 macb_init_hw(bp);
2740
Antoine Tenart7897b072019-11-13 10:00:06 +01002741 err = macb_phylink_connect(bp);
2742 if (err)
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002743 goto reset_hw;
frederic RODO6c36a702007-07-12 19:07:24 +02002744
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002745 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002746
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002747 if (bp->ptp_info)
2748 bp->ptp_info->ptp_init(dev);
2749
Charles Keepax939a5bf72020-06-15 14:18:54 +01002750 return 0;
2751
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002752reset_hw:
2753 macb_reset_hw(bp);
Corentin Labbe014406b2020-06-10 09:53:44 +00002754 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2755 napi_disable(&queue->napi);
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002756 macb_free_consistent(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302757pm_exit:
Charles Keepax939a5bf72020-06-15 14:18:54 +01002758 pm_runtime_put_sync(&bp->pdev->dev);
2759 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002760}
2761
2762static int macb_close(struct net_device *dev)
2763{
2764 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002765 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002766 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002767 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002768
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002769 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002770
2771 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2772 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002773
Antoine Tenart7897b072019-11-13 10:00:06 +01002774 phylink_stop(bp->phylink);
2775 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002776
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002777 spin_lock_irqsave(&bp->lock, flags);
2778 macb_reset_hw(bp);
2779 netif_carrier_off(dev);
2780 spin_unlock_irqrestore(&bp->lock, flags);
2781
2782 macb_free_consistent(bp);
2783
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002784 if (bp->ptp_info)
2785 bp->ptp_info->ptp_remove(dev);
2786
Harini Katakamd54f89a2019-03-01 16:20:34 +05302787 pm_runtime_put(&bp->pdev->dev);
2788
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002789 return 0;
2790}
2791
Harini Katakama5898ea2015-05-06 22:27:18 +05302792static int macb_change_mtu(struct net_device *dev, int new_mtu)
2793{
Harini Katakama5898ea2015-05-06 22:27:18 +05302794 if (netif_running(dev))
2795 return -EBUSY;
2796
Harini Katakama5898ea2015-05-06 22:27:18 +05302797 dev->mtu = new_mtu;
2798
2799 return 0;
2800}
2801
Jamie Ilesa494ed82011-03-09 16:26:35 +00002802static void gem_update_stats(struct macb *bp)
2803{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002804 struct macb_queue *queue;
2805 unsigned int i, q, idx;
2806 unsigned long *stat;
2807
Jamie Ilesa494ed82011-03-09 16:26:35 +00002808 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002809
Xander Huff3ff13f12015-01-13 16:15:51 -06002810 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2811 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002812 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002813
2814 bp->ethtool_stats[i] += val;
2815 *p += val;
2816
2817 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2818 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002819 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002820 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002821 *(++p) += val;
2822 }
2823 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002824
2825 idx = GEM_STATS_LEN;
2826 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2827 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2828 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002829}
2830
2831static struct net_device_stats *gem_get_stats(struct macb *bp)
2832{
2833 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002834 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002835
2836 gem_update_stats(bp);
2837
2838 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2839 hwstat->rx_alignment_errors +
2840 hwstat->rx_resource_errors +
2841 hwstat->rx_overruns +
2842 hwstat->rx_oversize_frames +
2843 hwstat->rx_jabbers +
2844 hwstat->rx_undersized_frames +
2845 hwstat->rx_length_field_frame_errors);
2846 nstat->tx_errors = (hwstat->tx_late_collisions +
2847 hwstat->tx_excessive_collisions +
2848 hwstat->tx_underrun +
2849 hwstat->tx_carrier_sense_errors);
2850 nstat->multicast = hwstat->rx_multicast_frames;
2851 nstat->collisions = (hwstat->tx_single_collision_frames +
2852 hwstat->tx_multiple_collision_frames +
2853 hwstat->tx_excessive_collisions);
2854 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2855 hwstat->rx_jabbers +
2856 hwstat->rx_undersized_frames +
2857 hwstat->rx_length_field_frame_errors);
2858 nstat->rx_over_errors = hwstat->rx_resource_errors;
2859 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2860 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2861 nstat->rx_fifo_errors = hwstat->rx_overruns;
2862 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2863 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2864 nstat->tx_fifo_errors = hwstat->tx_underrun;
2865
2866 return nstat;
2867}
2868
Xander Huff3ff13f12015-01-13 16:15:51 -06002869static void gem_get_ethtool_stats(struct net_device *dev,
2870 struct ethtool_stats *stats, u64 *data)
2871{
2872 struct macb *bp;
2873
2874 bp = netdev_priv(dev);
2875 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002876 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2877 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002878}
2879
2880static int gem_get_sset_count(struct net_device *dev, int sset)
2881{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002882 struct macb *bp = netdev_priv(dev);
2883
Xander Huff3ff13f12015-01-13 16:15:51 -06002884 switch (sset) {
2885 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002886 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002887 default:
2888 return -EOPNOTSUPP;
2889 }
2890}
2891
2892static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2893{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002894 char stat_string[ETH_GSTRING_LEN];
2895 struct macb *bp = netdev_priv(dev);
2896 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002897 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002898 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002899
2900 switch (sset) {
2901 case ETH_SS_STATS:
2902 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2903 memcpy(p, gem_statistics[i].stat_string,
2904 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002905
2906 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2907 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2908 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2909 q, queue_statistics[i].stat_string);
2910 memcpy(p, stat_string, ETH_GSTRING_LEN);
2911 }
2912 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002913 break;
2914 }
2915}
2916
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002917static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002918{
2919 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002920 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002921 struct macb_stats *hwstat = &bp->hw_stats.macb;
2922
2923 if (macb_is_gem(bp))
2924 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002925
frederic RODO6c36a702007-07-12 19:07:24 +02002926 /* read stats from hardware */
2927 macb_update_stats(bp);
2928
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002929 /* Convert HW stats into netdevice stats */
2930 nstat->rx_errors = (hwstat->rx_fcs_errors +
2931 hwstat->rx_align_errors +
2932 hwstat->rx_resource_errors +
2933 hwstat->rx_overruns +
2934 hwstat->rx_oversize_pkts +
2935 hwstat->rx_jabbers +
2936 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002937 hwstat->rx_length_mismatch);
2938 nstat->tx_errors = (hwstat->tx_late_cols +
2939 hwstat->tx_excessive_cols +
2940 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002941 hwstat->tx_carrier_errors +
2942 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002943 nstat->collisions = (hwstat->tx_single_cols +
2944 hwstat->tx_multiple_cols +
2945 hwstat->tx_excessive_cols);
2946 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2947 hwstat->rx_jabbers +
2948 hwstat->rx_undersize_pkts +
2949 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002950 nstat->rx_over_errors = hwstat->rx_resource_errors +
2951 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002952 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2953 nstat->rx_frame_errors = hwstat->rx_align_errors;
2954 nstat->rx_fifo_errors = hwstat->rx_overruns;
2955 /* XXX: What does "missed" mean? */
2956 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2957 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2958 nstat->tx_fifo_errors = hwstat->tx_underruns;
2959 /* Don't know about heartbeat or window errors... */
2960
2961 return nstat;
2962}
2963
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002964static int macb_get_regs_len(struct net_device *netdev)
2965{
2966 return MACB_GREGS_NBR * sizeof(u32);
2967}
2968
2969static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2970 void *p)
2971{
2972 struct macb *bp = netdev_priv(dev);
2973 unsigned int tail, head;
2974 u32 *regs_buff = p;
2975
2976 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2977 | MACB_GREGS_VERSION;
2978
Zach Brownb410d132016-10-19 09:56:57 -05002979 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2980 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002981
2982 regs_buff[0] = macb_readl(bp, NCR);
2983 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2984 regs_buff[2] = macb_readl(bp, NSR);
2985 regs_buff[3] = macb_readl(bp, TSR);
2986 regs_buff[4] = macb_readl(bp, RBQP);
2987 regs_buff[5] = macb_readl(bp, TBQP);
2988 regs_buff[6] = macb_readl(bp, RSR);
2989 regs_buff[7] = macb_readl(bp, IMR);
2990
2991 regs_buff[8] = tail;
2992 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002993 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2994 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002995
Neil Armstrongce721a72016-01-05 14:39:16 +01002996 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2997 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002998 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002999 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003000}
3001
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003002static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3003{
3004 struct macb *bp = netdev_priv(netdev);
3005
Nicolas Ferre253fe092020-07-10 14:46:43 +02003006 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
Antoine Tenart7897b072019-11-13 10:00:06 +01003007 phylink_ethtool_get_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003008 wol->supported |= WAKE_MAGIC;
3009
3010 if (bp->wol & MACB_WOL_ENABLED)
3011 wol->wolopts |= WAKE_MAGIC;
3012 }
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003013}
3014
3015static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3016{
3017 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01003018 int ret;
3019
Nicolas Ferre253fe092020-07-10 14:46:43 +02003020 /* Pass the order to phylink layer */
Antoine Tenart7897b072019-11-13 10:00:06 +01003021 ret = phylink_ethtool_set_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003022 /* Don't manage WoL on MAC if handled by the PHY
3023 * or if there's a failure in talking to the PHY
3024 */
3025 if (!ret || ret != -EOPNOTSUPP)
3026 return ret;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003027
3028 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
3029 (wol->wolopts & ~WAKE_MAGIC))
3030 return -EOPNOTSUPP;
3031
3032 if (wol->wolopts & WAKE_MAGIC)
3033 bp->wol |= MACB_WOL_ENABLED;
3034 else
3035 bp->wol &= ~MACB_WOL_ENABLED;
3036
3037 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
3038
3039 return 0;
3040}
3041
Antoine Tenart7897b072019-11-13 10:00:06 +01003042static int macb_get_link_ksettings(struct net_device *netdev,
3043 struct ethtool_link_ksettings *kset)
3044{
3045 struct macb *bp = netdev_priv(netdev);
3046
3047 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3048}
3049
3050static int macb_set_link_ksettings(struct net_device *netdev,
3051 const struct ethtool_link_ksettings *kset)
3052{
3053 struct macb *bp = netdev_priv(netdev);
3054
3055 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3056}
3057
Zach Brown8441bb32016-10-19 09:56:58 -05003058static void macb_get_ringparam(struct net_device *netdev,
3059 struct ethtool_ringparam *ring)
3060{
3061 struct macb *bp = netdev_priv(netdev);
3062
3063 ring->rx_max_pending = MAX_RX_RING_SIZE;
3064 ring->tx_max_pending = MAX_TX_RING_SIZE;
3065
3066 ring->rx_pending = bp->rx_ring_size;
3067 ring->tx_pending = bp->tx_ring_size;
3068}
3069
3070static int macb_set_ringparam(struct net_device *netdev,
3071 struct ethtool_ringparam *ring)
3072{
3073 struct macb *bp = netdev_priv(netdev);
3074 u32 new_rx_size, new_tx_size;
3075 unsigned int reset = 0;
3076
3077 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3078 return -EINVAL;
3079
3080 new_rx_size = clamp_t(u32, ring->rx_pending,
3081 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3082 new_rx_size = roundup_pow_of_two(new_rx_size);
3083
3084 new_tx_size = clamp_t(u32, ring->tx_pending,
3085 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3086 new_tx_size = roundup_pow_of_two(new_tx_size);
3087
3088 if ((new_tx_size == bp->tx_ring_size) &&
3089 (new_rx_size == bp->rx_ring_size)) {
3090 /* nothing to do */
3091 return 0;
3092 }
3093
3094 if (netif_running(bp->dev)) {
3095 reset = 1;
3096 macb_close(bp->dev);
3097 }
3098
3099 bp->rx_ring_size = new_rx_size;
3100 bp->tx_ring_size = new_tx_size;
3101
3102 if (reset)
3103 macb_open(bp->dev);
3104
3105 return 0;
3106}
3107
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003108#ifdef CONFIG_MACB_USE_HWSTAMP
3109static unsigned int gem_get_tsu_rate(struct macb *bp)
3110{
3111 struct clk *tsu_clk;
3112 unsigned int tsu_rate;
3113
3114 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
3115 if (!IS_ERR(tsu_clk))
3116 tsu_rate = clk_get_rate(tsu_clk);
3117 /* try pclk instead */
3118 else if (!IS_ERR(bp->pclk)) {
3119 tsu_clk = bp->pclk;
3120 tsu_rate = clk_get_rate(tsu_clk);
3121 } else
3122 return -ENOTSUPP;
3123 return tsu_rate;
3124}
3125
3126static s32 gem_get_ptp_max_adj(void)
3127{
3128 return 64000000;
3129}
3130
3131static int gem_get_ts_info(struct net_device *dev,
3132 struct ethtool_ts_info *info)
3133{
3134 struct macb *bp = netdev_priv(dev);
3135
3136 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
3137 ethtool_op_get_ts_info(dev, info);
3138 return 0;
3139 }
3140
3141 info->so_timestamping =
3142 SOF_TIMESTAMPING_TX_SOFTWARE |
3143 SOF_TIMESTAMPING_RX_SOFTWARE |
3144 SOF_TIMESTAMPING_SOFTWARE |
3145 SOF_TIMESTAMPING_TX_HARDWARE |
3146 SOF_TIMESTAMPING_RX_HARDWARE |
3147 SOF_TIMESTAMPING_RAW_HARDWARE;
3148 info->tx_types =
3149 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3150 (1 << HWTSTAMP_TX_OFF) |
3151 (1 << HWTSTAMP_TX_ON);
3152 info->rx_filters =
3153 (1 << HWTSTAMP_FILTER_NONE) |
3154 (1 << HWTSTAMP_FILTER_ALL);
3155
3156 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
3157
3158 return 0;
3159}
3160
3161static struct macb_ptp_info gem_ptp_info = {
3162 .ptp_init = gem_ptp_init,
3163 .ptp_remove = gem_ptp_remove,
3164 .get_ptp_max_adj = gem_get_ptp_max_adj,
3165 .get_tsu_rate = gem_get_tsu_rate,
3166 .get_ts_info = gem_get_ts_info,
3167 .get_hwtst = gem_get_hwtst,
3168 .set_hwtst = gem_set_hwtst,
3169};
3170#endif
3171
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003172static int macb_get_ts_info(struct net_device *netdev,
3173 struct ethtool_ts_info *info)
3174{
3175 struct macb *bp = netdev_priv(netdev);
3176
3177 if (bp->ptp_info)
3178 return bp->ptp_info->get_ts_info(netdev, info);
3179
3180 return ethtool_op_get_ts_info(netdev, info);
3181}
3182
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003183static void gem_enable_flow_filters(struct macb *bp, bool enable)
3184{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003185 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003186 struct ethtool_rx_fs_item *item;
3187 u32 t2_scr;
3188 int num_t2_scr;
3189
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003190 if (!(netdev->features & NETIF_F_NTUPLE))
3191 return;
3192
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003193 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3194
3195 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3196 struct ethtool_rx_flow_spec *fs = &item->fs;
3197 struct ethtool_tcpip4_spec *tp4sp_m;
3198
3199 if (fs->location >= num_t2_scr)
3200 continue;
3201
3202 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3203
3204 /* enable/disable screener regs for the flow entry */
3205 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3206
3207 /* only enable fields with no masking */
3208 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3209
3210 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3211 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3212 else
3213 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3214
3215 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3216 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3217 else
3218 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3219
3220 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3221 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3222 else
3223 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3224
3225 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3226 }
3227}
3228
3229static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3230{
3231 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3232 uint16_t index = fs->location;
3233 u32 w0, w1, t2_scr;
3234 bool cmp_a = false;
3235 bool cmp_b = false;
3236 bool cmp_c = false;
3237
3238 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3239 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3240
3241 /* ignore field if any masking set */
3242 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3243 /* 1st compare reg - IP source address */
3244 w0 = 0;
3245 w1 = 0;
3246 w0 = tp4sp_v->ip4src;
3247 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3248 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3249 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3250 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3251 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3252 cmp_a = true;
3253 }
3254
3255 /* ignore field if any masking set */
3256 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3257 /* 2nd compare reg - IP destination address */
3258 w0 = 0;
3259 w1 = 0;
3260 w0 = tp4sp_v->ip4dst;
3261 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3262 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3263 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3264 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3265 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3266 cmp_b = true;
3267 }
3268
3269 /* ignore both port fields if masking set in both */
3270 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3271 /* 3rd compare reg - source port, destination port */
3272 w0 = 0;
3273 w1 = 0;
3274 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3275 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3276 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3277 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3278 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3279 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3280 } else {
3281 /* only one port definition */
3282 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3283 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3284 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3285 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3286 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3287 } else { /* dst port */
3288 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3289 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3290 }
3291 }
3292 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3293 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3294 cmp_c = true;
3295 }
3296
3297 t2_scr = 0;
3298 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3299 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3300 if (cmp_a)
3301 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3302 if (cmp_b)
3303 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3304 if (cmp_c)
3305 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3306 gem_writel_n(bp, SCRT2, index, t2_scr);
3307}
3308
3309static int gem_add_flow_filter(struct net_device *netdev,
3310 struct ethtool_rxnfc *cmd)
3311{
3312 struct macb *bp = netdev_priv(netdev);
3313 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3314 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003315 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003316 int ret = -EINVAL;
3317 bool added = false;
3318
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003319 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003320 if (newfs == NULL)
3321 return -ENOMEM;
3322 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3323
3324 netdev_dbg(netdev,
3325 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3326 fs->flow_type, (int)fs->ring_cookie, fs->location,
3327 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3328 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3329 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3330
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003331 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3332
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003333 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003334 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3335 if (item->fs.location > newfs->fs.location) {
3336 list_add_tail(&newfs->list, &item->list);
3337 added = true;
3338 break;
3339 } else if (item->fs.location == fs->location) {
3340 netdev_err(netdev, "Rule not added: location %d not free!\n",
3341 fs->location);
3342 ret = -EBUSY;
3343 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003344 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003345 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003346 if (!added)
3347 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003348
3349 gem_prog_cmp_regs(bp, fs);
3350 bp->rx_fs_list.count++;
3351 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003352 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003353
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003354 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003355 return 0;
3356
3357err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003358 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003359 kfree(newfs);
3360 return ret;
3361}
3362
3363static int gem_del_flow_filter(struct net_device *netdev,
3364 struct ethtool_rxnfc *cmd)
3365{
3366 struct macb *bp = netdev_priv(netdev);
3367 struct ethtool_rx_fs_item *item;
3368 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003369 unsigned long flags;
3370
3371 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003372
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003373 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3374 if (item->fs.location == cmd->fs.location) {
3375 /* disable screener regs for the flow entry */
3376 fs = &(item->fs);
3377 netdev_dbg(netdev,
3378 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3379 fs->flow_type, (int)fs->ring_cookie, fs->location,
3380 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3381 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3382 htons(fs->h_u.tcp_ip4_spec.psrc),
3383 htons(fs->h_u.tcp_ip4_spec.pdst));
3384
3385 gem_writel_n(bp, SCRT2, fs->location, 0);
3386
3387 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003388 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003389 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3390 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003391 return 0;
3392 }
3393 }
3394
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003395 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003396 return -EINVAL;
3397}
3398
3399static int gem_get_flow_entry(struct net_device *netdev,
3400 struct ethtool_rxnfc *cmd)
3401{
3402 struct macb *bp = netdev_priv(netdev);
3403 struct ethtool_rx_fs_item *item;
3404
3405 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3406 if (item->fs.location == cmd->fs.location) {
3407 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3408 return 0;
3409 }
3410 }
3411 return -EINVAL;
3412}
3413
3414static int gem_get_all_flow_entries(struct net_device *netdev,
3415 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3416{
3417 struct macb *bp = netdev_priv(netdev);
3418 struct ethtool_rx_fs_item *item;
3419 uint32_t cnt = 0;
3420
3421 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3422 if (cnt == cmd->rule_cnt)
3423 return -EMSGSIZE;
3424 rule_locs[cnt] = item->fs.location;
3425 cnt++;
3426 }
3427 cmd->data = bp->max_tuples;
3428 cmd->rule_cnt = cnt;
3429
3430 return 0;
3431}
3432
3433static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3434 u32 *rule_locs)
3435{
3436 struct macb *bp = netdev_priv(netdev);
3437 int ret = 0;
3438
3439 switch (cmd->cmd) {
3440 case ETHTOOL_GRXRINGS:
3441 cmd->data = bp->num_queues;
3442 break;
3443 case ETHTOOL_GRXCLSRLCNT:
3444 cmd->rule_cnt = bp->rx_fs_list.count;
3445 break;
3446 case ETHTOOL_GRXCLSRULE:
3447 ret = gem_get_flow_entry(netdev, cmd);
3448 break;
3449 case ETHTOOL_GRXCLSRLALL:
3450 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3451 break;
3452 default:
3453 netdev_err(netdev,
3454 "Command parameter %d is not supported\n", cmd->cmd);
3455 ret = -EOPNOTSUPP;
3456 }
3457
3458 return ret;
3459}
3460
3461static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3462{
3463 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003464 int ret;
3465
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003466 switch (cmd->cmd) {
3467 case ETHTOOL_SRXCLSRLINS:
3468 if ((cmd->fs.location >= bp->max_tuples)
3469 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3470 ret = -EINVAL;
3471 break;
3472 }
3473 ret = gem_add_flow_filter(netdev, cmd);
3474 break;
3475 case ETHTOOL_SRXCLSRLDEL:
3476 ret = gem_del_flow_filter(netdev, cmd);
3477 break;
3478 default:
3479 netdev_err(netdev,
3480 "Command parameter %d is not supported\n", cmd->cmd);
3481 ret = -EOPNOTSUPP;
3482 }
3483
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003484 return ret;
3485}
3486
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003487static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003488 .get_regs_len = macb_get_regs_len,
3489 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003490 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003491 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003492 .get_wol = macb_get_wol,
3493 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003494 .get_link_ksettings = macb_get_link_ksettings,
3495 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003496 .get_ringparam = macb_get_ringparam,
3497 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003498};
Xander Huff8cd5a562015-01-15 15:55:20 -06003499
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003500static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003501 .get_regs_len = macb_get_regs_len,
3502 .get_regs = macb_get_regs,
Nicolas Ferre558e35c2020-07-20 10:56:52 +02003503 .get_wol = macb_get_wol,
3504 .set_wol = macb_set_wol,
Xander Huff8cd5a562015-01-15 15:55:20 -06003505 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003506 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003507 .get_ethtool_stats = gem_get_ethtool_stats,
3508 .get_strings = gem_get_ethtool_strings,
3509 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003510 .get_link_ksettings = macb_get_link_ksettings,
3511 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003512 .get_ringparam = macb_get_ringparam,
3513 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003514 .get_rxnfc = gem_get_rxnfc,
3515 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003516};
3517
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003518static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003519{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003520 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003521
3522 if (!netif_running(dev))
3523 return -EINVAL;
3524
Antoine Tenart7897b072019-11-13 10:00:06 +01003525 if (bp->ptp_info) {
3526 switch (cmd) {
3527 case SIOCSHWTSTAMP:
3528 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3529 case SIOCGHWTSTAMP:
3530 return bp->ptp_info->get_hwtst(dev, rq);
3531 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003532 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003533
3534 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003535}
3536
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003537static inline void macb_set_txcsum_feature(struct macb *bp,
3538 netdev_features_t features)
3539{
3540 u32 val;
3541
3542 if (!macb_is_gem(bp))
3543 return;
3544
3545 val = gem_readl(bp, DMACFG);
3546 if (features & NETIF_F_HW_CSUM)
3547 val |= GEM_BIT(TXCOEN);
3548 else
3549 val &= ~GEM_BIT(TXCOEN);
3550
3551 gem_writel(bp, DMACFG, val);
3552}
3553
3554static inline void macb_set_rxcsum_feature(struct macb *bp,
3555 netdev_features_t features)
3556{
3557 struct net_device *netdev = bp->dev;
3558 u32 val;
3559
3560 if (!macb_is_gem(bp))
3561 return;
3562
3563 val = gem_readl(bp, NCFGR);
3564 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3565 val |= GEM_BIT(RXCOEN);
3566 else
3567 val &= ~GEM_BIT(RXCOEN);
3568
3569 gem_writel(bp, NCFGR, val);
3570}
3571
3572static inline void macb_set_rxflow_feature(struct macb *bp,
3573 netdev_features_t features)
3574{
3575 if (!macb_is_gem(bp))
3576 return;
3577
3578 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3579}
3580
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003581static int macb_set_features(struct net_device *netdev,
3582 netdev_features_t features)
3583{
3584 struct macb *bp = netdev_priv(netdev);
3585 netdev_features_t changed = features ^ netdev->features;
3586
3587 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003588 if (changed & NETIF_F_HW_CSUM)
3589 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003590
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003591 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003592 if (changed & NETIF_F_RXCSUM)
3593 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003594
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003595 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003596 if (changed & NETIF_F_NTUPLE)
3597 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003598
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003599 return 0;
3600}
3601
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003602static void macb_restore_features(struct macb *bp)
3603{
3604 struct net_device *netdev = bp->dev;
3605 netdev_features_t features = netdev->features;
3606
3607 /* TX checksum offload */
3608 macb_set_txcsum_feature(bp, features);
3609
3610 /* RX checksum offload */
3611 macb_set_rxcsum_feature(bp, features);
3612
3613 /* RX Flow Filters */
3614 macb_set_rxflow_feature(bp, features);
3615}
3616
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003617static const struct net_device_ops macb_netdev_ops = {
3618 .ndo_open = macb_open,
3619 .ndo_stop = macb_close,
3620 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003621 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003622 .ndo_get_stats = macb_get_stats,
3623 .ndo_do_ioctl = macb_ioctl,
3624 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303625 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003626 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003627#ifdef CONFIG_NET_POLL_CONTROLLER
3628 .ndo_poll_controller = macb_poll_controller,
3629#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003630 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003631 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003632};
3633
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003634/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003635 * and integration options used
3636 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003637static void macb_configure_caps(struct macb *bp,
3638 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003639{
3640 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003641
Nicolas Ferref6970502015-03-31 15:02:01 +02003642 if (dt_conf)
3643 bp->caps = dt_conf->caps;
3644
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003645 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003646 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3647
Nicolas Ferree1755872014-07-24 13:50:58 +02003648 dcfg = gem_readl(bp, DCFG1);
3649 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3650 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +01003651 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
3652 bp->caps |= MACB_CAPS_PCS;
3653 dcfg = gem_readl(bp, DCFG12);
3654 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
3655 bp->caps |= MACB_CAPS_HIGH_SPEED;
Nicolas Ferree1755872014-07-24 13:50:58 +02003656 dcfg = gem_readl(bp, DCFG2);
3657 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3658 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003659#ifdef CONFIG_MACB_USE_HWSTAMP
3660 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003661 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003662 dev_err(&bp->pdev->dev,
3663 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003664 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003665 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003666 bp->ptp_info = &gem_ptp_info;
3667 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003668 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003669#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003670 }
3671
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003672 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003673}
3674
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003675static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003676 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003677 unsigned int *queue_mask,
3678 unsigned int *num_queues)
3679{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003680 *queue_mask = 0x1;
3681 *num_queues = 1;
3682
Nicolas Ferreda120112015-03-31 15:02:00 +02003683 /* is it macb or gem ?
3684 *
3685 * We need to read directly from the hardware here because
3686 * we are early in the probe process and don't have the
3687 * MACB_CAPS_MACB_IS_GEM flag positioned
3688 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003689 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003690 return;
3691
3692 /* bit 0 is never set but queue 0 always exists */
Claudiu Bezneafec371f2020-07-02 12:05:58 +03003693 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
Claudiu Bezneab7ab39b2020-07-02 12:05:59 +03003694 *num_queues = hweight32(*queue_mask);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003695}
3696
Claudiu Beznea38493da2020-12-09 15:03:34 +02003697static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
3698 struct clk *rx_clk, struct clk *tsu_clk)
3699{
3700 struct clk_bulk_data clks[] = {
3701 { .clk = tsu_clk, },
3702 { .clk = rx_clk, },
3703 { .clk = pclk, },
3704 { .clk = hclk, },
3705 { .clk = tx_clk },
3706 };
3707
3708 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
3709}
3710
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003711static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303712 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303713 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003714{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003715 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003716 int err;
3717
Bartosz Folta83a77e92016-12-14 06:39:15 +00003718 pdata = dev_get_platdata(&pdev->dev);
3719 if (pdata) {
3720 *pclk = pdata->pclk;
3721 *hclk = pdata->hclk;
3722 } else {
3723 *pclk = devm_clk_get(&pdev->dev, "pclk");
3724 *hclk = devm_clk_get(&pdev->dev, "hclk");
3725 }
3726
Harini Katakamcd5afa92019-03-20 19:12:22 +05303727 if (IS_ERR_OR_NULL(*pclk)) {
YueHaibing9e6cad52020-11-12 22:49:36 +08003728 err = IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV;
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003729 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003730 return err;
3731 }
3732
Harini Katakamcd5afa92019-03-20 19:12:22 +05303733 if (IS_ERR_OR_NULL(*hclk)) {
YueHaibing9e6cad52020-11-12 22:49:36 +08003734 err = IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV;
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003735 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003736 return err;
3737 }
3738
Michael Tretterbd310aca2019-10-18 16:11:43 +02003739 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003740 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003741 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003742
Michael Tretterbd310aca2019-10-18 16:11:43 +02003743 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303744 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003745 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303746
Michael Tretterbd310aca2019-10-18 16:11:43 +02003747 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303748 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003749 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303750
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003751 err = clk_prepare_enable(*pclk);
3752 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003753 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003754 return err;
3755 }
3756
3757 err = clk_prepare_enable(*hclk);
3758 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003759 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003760 goto err_disable_pclk;
3761 }
3762
3763 err = clk_prepare_enable(*tx_clk);
3764 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003765 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003766 goto err_disable_hclk;
3767 }
3768
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303769 err = clk_prepare_enable(*rx_clk);
3770 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003771 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303772 goto err_disable_txclk;
3773 }
3774
Harini Katakamf5473d12019-03-01 16:20:33 +05303775 err = clk_prepare_enable(*tsu_clk);
3776 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003777 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303778 goto err_disable_rxclk;
3779 }
3780
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003781 return 0;
3782
Harini Katakamf5473d12019-03-01 16:20:33 +05303783err_disable_rxclk:
3784 clk_disable_unprepare(*rx_clk);
3785
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303786err_disable_txclk:
3787 clk_disable_unprepare(*tx_clk);
3788
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003789err_disable_hclk:
3790 clk_disable_unprepare(*hclk);
3791
3792err_disable_pclk:
3793 clk_disable_unprepare(*pclk);
3794
3795 return err;
3796}
3797
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003798static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003799{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003800 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003801 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003802 struct macb *bp = netdev_priv(dev);
3803 struct macb_queue *queue;
3804 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003805 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003806
Zach Brownb410d132016-10-19 09:56:57 -05003807 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3808 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3809
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003810 /* set the queue register mapping once for all: queue0 has a special
3811 * register mapping but we don't want to test the queue index then
3812 * compute the corresponding register offset at run time.
3813 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003814 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003815 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003816 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003817
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003818 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003819 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003820 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003821 if (hw_q) {
3822 queue->ISR = GEM_ISR(hw_q - 1);
3823 queue->IER = GEM_IER(hw_q - 1);
3824 queue->IDR = GEM_IDR(hw_q - 1);
3825 queue->IMR = GEM_IMR(hw_q - 1);
3826 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003827 queue->RBQP = GEM_RBQP(hw_q - 1);
3828 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303829#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003830 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003831 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003832 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3833 }
Harini Katakamfff80192016-08-09 13:15:53 +05303834#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003835 } else {
3836 /* queue0 uses legacy registers */
3837 queue->ISR = MACB_ISR;
3838 queue->IER = MACB_IER;
3839 queue->IDR = MACB_IDR;
3840 queue->IMR = MACB_IMR;
3841 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003842 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303843#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003844 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003845 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003846 queue->RBQPH = MACB_RBQPH;
3847 }
Harini Katakamfff80192016-08-09 13:15:53 +05303848#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003849 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003850
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003851 /* get irq: here we use the linux queue index, not the hardware
3852 * queue index. the queue irq definitions in the device tree
3853 * must remove the optional gaps that could exist in the
3854 * hardware queue mask.
3855 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003856 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003857 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003858 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003859 if (err) {
3860 dev_err(&pdev->dev,
3861 "Unable to request IRQ %d (error %d)\n",
3862 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003863 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003864 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003865
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003866 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003867 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003868 }
3869
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003870 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003871
Nicolas Ferre4df95132013-06-04 21:57:12 +00003872 /* setup appropriated routines according to adapter type */
3873 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003874 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003875 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3876 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3877 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3878 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003879 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003880 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003881 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003882 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3883 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3884 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3885 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003886 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003887 }
3888
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003889 /* Set features */
3890 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003891
3892 /* Check LSO capability */
3893 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3894 dev->hw_features |= MACB_NETIF_LSO;
3895
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003896 /* Checksum offload is only available on gem with packet buffer */
3897 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003898 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003899 if (bp->caps & MACB_CAPS_SG_DISABLED)
3900 dev->hw_features &= ~NETIF_F_SG;
3901 dev->features = dev->hw_features;
3902
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003903 /* Check RX Flow Filters support.
3904 * Max Rx flows set by availability of screeners & compare regs:
3905 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3906 */
3907 reg = gem_readl(bp, DCFG8);
3908 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3909 GEM_BFEXT(T2SCR, reg));
3910 if (bp->max_tuples > 0) {
3911 /* also needs one ethtype match to check IPv4 */
3912 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3913 /* program this reg now */
3914 reg = 0;
3915 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3916 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3917 /* Filtering is supported in hw but don't enable it in kernel now */
3918 dev->hw_features |= NETIF_F_NTUPLE;
3919 /* init Rx flow definitions */
3920 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3921 bp->rx_fs_list.count = 0;
3922 spin_lock_init(&bp->rx_fs_lock);
3923 } else
3924 bp->max_tuples = 0;
3925 }
3926
Neil Armstrongce721a72016-01-05 14:39:16 +01003927 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3928 val = 0;
Alexandre Belloni2ccb0162020-07-18 01:32:21 +02003929 if (phy_interface_mode_is_rgmii(bp->phy_interface))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003930 val = bp->usrio->rgmii;
Neil Armstrongce721a72016-01-05 14:39:16 +01003931 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003932 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003933 val = bp->usrio->rmii;
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003934 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003935 val = bp->usrio->mii;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003936
Neil Armstrongce721a72016-01-05 14:39:16 +01003937 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003938 val |= bp->usrio->refclk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003939
Neil Armstrongce721a72016-01-05 14:39:16 +01003940 macb_or_gem_writel(bp, USRIO, val);
3941 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003942
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003943 /* Set MII management clock divider */
3944 val = macb_mdc_clk_div(bp);
3945 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303946 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3947 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003948 macb_writel(bp, NCFGR, val);
3949
3950 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003951}
3952
3953#if defined(CONFIG_OF)
3954/* 1518 rounded up */
3955#define AT91ETHER_MAX_RBUFF_SZ 0x600
3956/* max number of receive buffers */
3957#define AT91ETHER_MAX_RX_DESCR 9
3958
Arnd Bergmann49db9222019-07-08 14:48:23 +02003959static struct sifive_fu540_macb_mgmt *mgmt;
3960
Claudiu Beznea33fdef22020-06-24 13:08:18 +03003961static int at91ether_alloc_coherent(struct macb *lp)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003962{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003963 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003964
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003965 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003966 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003967 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003968 &q->rx_ring_dma, GFP_KERNEL);
3969 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003970 return -ENOMEM;
3971
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003972 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003973 AT91ETHER_MAX_RX_DESCR *
3974 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003975 &q->rx_buffers_dma, GFP_KERNEL);
3976 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003977 dma_free_coherent(&lp->pdev->dev,
3978 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003979 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003980 q->rx_ring, q->rx_ring_dma);
3981 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003982 return -ENOMEM;
3983 }
3984
Claudiu Beznea33fdef22020-06-24 13:08:18 +03003985 return 0;
3986}
3987
3988static void at91ether_free_coherent(struct macb *lp)
3989{
3990 struct macb_queue *q = &lp->queues[0];
3991
3992 if (q->rx_ring) {
3993 dma_free_coherent(&lp->pdev->dev,
3994 AT91ETHER_MAX_RX_DESCR *
3995 macb_dma_desc_get_size(lp),
3996 q->rx_ring, q->rx_ring_dma);
3997 q->rx_ring = NULL;
3998 }
3999
4000 if (q->rx_buffers) {
4001 dma_free_coherent(&lp->pdev->dev,
4002 AT91ETHER_MAX_RX_DESCR *
4003 AT91ETHER_MAX_RBUFF_SZ,
4004 q->rx_buffers, q->rx_buffers_dma);
4005 q->rx_buffers = NULL;
4006 }
4007}
4008
4009/* Initialize and start the Receiver and Transmit subsystems */
4010static int at91ether_start(struct macb *lp)
4011{
4012 struct macb_queue *q = &lp->queues[0];
4013 struct macb_dma_desc *desc;
4014 dma_addr_t addr;
4015 u32 ctl;
4016 int i, ret;
4017
4018 ret = at91ether_alloc_coherent(lp);
4019 if (ret)
4020 return ret;
4021
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004022 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004023 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004024 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004025 macb_set_addr(lp, desc, addr);
4026 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004027 addr += AT91ETHER_MAX_RBUFF_SZ;
4028 }
4029
4030 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004031 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004032
4033 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004034 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004035
4036 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004037 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004038
4039 /* Enable Receive and Transmit */
4040 ctl = macb_readl(lp, NCR);
4041 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
4042
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004043 /* Enable MAC interrupts */
4044 macb_writel(lp, IER, MACB_BIT(RCOMP) |
4045 MACB_BIT(RXUBR) |
4046 MACB_BIT(ISR_TUND) |
4047 MACB_BIT(ISR_RLE) |
4048 MACB_BIT(TCOMP) |
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004049 MACB_BIT(RM9200_TBRE) |
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004050 MACB_BIT(ISR_ROVR) |
4051 MACB_BIT(HRESP));
4052
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004053 return 0;
4054}
4055
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004056static void at91ether_stop(struct macb *lp)
4057{
4058 u32 ctl;
4059
4060 /* Disable MAC interrupts */
4061 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
4062 MACB_BIT(RXUBR) |
4063 MACB_BIT(ISR_TUND) |
4064 MACB_BIT(ISR_RLE) |
4065 MACB_BIT(TCOMP) |
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004066 MACB_BIT(RM9200_TBRE) |
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004067 MACB_BIT(ISR_ROVR) |
4068 MACB_BIT(HRESP));
4069
4070 /* Disable Receiver and Transmitter */
4071 ctl = macb_readl(lp, NCR);
4072 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
4073
4074 /* Free resources. */
4075 at91ether_free_coherent(lp);
4076}
4077
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004078/* Open the ethernet interface */
4079static int at91ether_open(struct net_device *dev)
4080{
4081 struct macb *lp = netdev_priv(dev);
4082 u32 ctl;
4083 int ret;
4084
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004085 ret = pm_runtime_get_sync(&lp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004086 if (ret < 0) {
4087 pm_runtime_put_noidle(&lp->pdev->dev);
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004088 return ret;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004089 }
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004090
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004091 /* Clear internal statistics */
4092 ctl = macb_readl(lp, NCR);
4093 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
4094
4095 macb_set_hwaddr(lp);
4096
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004097 ret = at91ether_start(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004098 if (ret)
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004099 goto pm_exit;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004100
Antoine Tenart7897b072019-11-13 10:00:06 +01004101 ret = macb_phylink_connect(lp);
4102 if (ret)
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004103 goto stop;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004104
4105 netif_start_queue(dev);
4106
4107 return 0;
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004108
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004109stop:
4110 at91ether_stop(lp);
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004111pm_exit:
4112 pm_runtime_put_sync(&lp->pdev->dev);
4113 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004114}
4115
4116/* Close the interface */
4117static int at91ether_close(struct net_device *dev)
4118{
4119 struct macb *lp = netdev_priv(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004120
4121 netif_stop_queue(dev);
4122
Antoine Tenart7897b072019-11-13 10:00:06 +01004123 phylink_stop(lp->phylink);
4124 phylink_disconnect_phy(lp->phylink);
4125
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004126 at91ether_stop(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004127
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004128 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004129}
4130
4131/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03004132static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
4133 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004134{
4135 struct macb *lp = netdev_priv(dev);
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004136 unsigned long flags;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004137
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004138 if (lp->rm9200_tx_len < 2) {
4139 int desc = lp->rm9200_tx_tail;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004140
4141 /* Store packet information (to free when Tx completed) */
Willy Tarreau73d74222020-10-11 11:09:43 +02004142 lp->rm9200_txq[desc].skb = skb;
4143 lp->rm9200_txq[desc].size = skb->len;
4144 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
4145 skb->len, DMA_TO_DEVICE);
4146 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03004147 dev_kfree_skb_any(skb);
4148 dev->stats.tx_dropped++;
4149 netdev_err(dev, "%s: DMA mapping error\n", __func__);
4150 return NETDEV_TX_OK;
4151 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004152
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004153 spin_lock_irqsave(&lp->lock, flags);
4154
4155 lp->rm9200_tx_tail = (desc + 1) & 1;
4156 lp->rm9200_tx_len++;
4157 if (lp->rm9200_tx_len > 1)
4158 netif_stop_queue(dev);
4159
4160 spin_unlock_irqrestore(&lp->lock, flags);
4161
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004162 /* Set address of the data in the Transmit Address register */
Willy Tarreau73d74222020-10-11 11:09:43 +02004163 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004164 /* Set length of the packet in the Transmit Control register */
4165 macb_writel(lp, TCR, skb->len);
4166
4167 } else {
4168 netdev_err(dev, "%s called, but device is busy!\n", __func__);
4169 return NETDEV_TX_BUSY;
4170 }
4171
4172 return NETDEV_TX_OK;
4173}
4174
4175/* Extract received frame from buffer descriptors and sent to upper layers.
4176 * (Called from interrupt context)
4177 */
4178static void at91ether_rx(struct net_device *dev)
4179{
4180 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004181 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004182 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004183 unsigned char *p_recv;
4184 struct sk_buff *skb;
4185 unsigned int pktlen;
4186
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004187 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004188 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004189 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004190 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004191 skb = netdev_alloc_skb(dev, pktlen + 2);
4192 if (skb) {
4193 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004194 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004195
4196 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004197 dev->stats.rx_packets++;
4198 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004199 netif_rx(skb);
4200 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004201 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004202 }
4203
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004204 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004205 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004206
4207 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004208 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004209
4210 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004211 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
4212 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004213 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004214 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004215
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004216 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004217 }
4218}
4219
4220/* MAC interrupt handler */
4221static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
4222{
4223 struct net_device *dev = dev_id;
4224 struct macb *lp = netdev_priv(dev);
4225 u32 intstatus, ctl;
Willy Tarreau73d74222020-10-11 11:09:43 +02004226 unsigned int desc;
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004227 unsigned int qlen;
4228 u32 tsr;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004229
4230 /* MAC Interrupt Status register indicates what interrupts are pending.
4231 * It is automatically cleared once read.
4232 */
4233 intstatus = macb_readl(lp, ISR);
4234
4235 /* Receive complete */
4236 if (intstatus & MACB_BIT(RCOMP))
4237 at91ether_rx(dev);
4238
4239 /* Transmit complete */
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004240 if (intstatus & (MACB_BIT(TCOMP) | MACB_BIT(RM9200_TBRE))) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004241 /* The TCOM bit is set even if the transmission failed */
4242 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004243 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004244
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004245 spin_lock(&lp->lock);
4246
4247 tsr = macb_readl(lp, TSR);
4248
4249 /* we have three possibilities here:
4250 * - all pending packets transmitted (TGO, implies BNQ)
4251 * - only first packet transmitted (!TGO && BNQ)
4252 * - two frames pending (!TGO && !BNQ)
4253 * Note that TGO ("transmit go") is called "IDLE" on RM9200.
4254 */
4255 qlen = (tsr & MACB_BIT(TGO)) ? 0 :
4256 (tsr & MACB_BIT(RM9200_BNQ)) ? 1 : 2;
4257
4258 while (lp->rm9200_tx_len > qlen) {
4259 desc = (lp->rm9200_tx_tail - lp->rm9200_tx_len) & 1;
Willy Tarreau73d74222020-10-11 11:09:43 +02004260 dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
4261 lp->rm9200_txq[desc].skb = NULL;
4262 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
4263 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004264 dev->stats.tx_packets++;
Willy Tarreau73d74222020-10-11 11:09:43 +02004265 dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004266 lp->rm9200_tx_len--;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004267 }
Willy Tarreau0a4e9ce2020-10-11 11:09:44 +02004268
4269 if (lp->rm9200_tx_len < 2 && netif_queue_stopped(dev))
4270 netif_wake_queue(dev);
4271
4272 spin_unlock(&lp->lock);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004273 }
4274
4275 /* Work-around for EMAC Errata section 41.3.1 */
4276 if (intstatus & MACB_BIT(RXUBR)) {
4277 ctl = macb_readl(lp, NCR);
4278 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08004279 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004280 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4281 }
4282
4283 if (intstatus & MACB_BIT(ISR_ROVR))
4284 netdev_err(dev, "ROVR error\n");
4285
4286 return IRQ_HANDLED;
4287}
4288
4289#ifdef CONFIG_NET_POLL_CONTROLLER
4290static void at91ether_poll_controller(struct net_device *dev)
4291{
4292 unsigned long flags;
4293
4294 local_irq_save(flags);
4295 at91ether_interrupt(dev->irq, dev);
4296 local_irq_restore(flags);
4297}
4298#endif
4299
4300static const struct net_device_ops at91ether_netdev_ops = {
4301 .ndo_open = at91ether_open,
4302 .ndo_stop = at91ether_close,
4303 .ndo_start_xmit = at91ether_start_xmit,
4304 .ndo_get_stats = macb_get_stats,
4305 .ndo_set_rx_mode = macb_set_rx_mode,
4306 .ndo_set_mac_address = eth_mac_addr,
4307 .ndo_do_ioctl = macb_ioctl,
4308 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004309#ifdef CONFIG_NET_POLL_CONTROLLER
4310 .ndo_poll_controller = at91ether_poll_controller,
4311#endif
4312};
4313
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004314static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304315 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304316 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004317{
4318 int err;
4319
4320 *hclk = NULL;
4321 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304322 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304323 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004324
4325 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4326 if (IS_ERR(*pclk))
4327 return PTR_ERR(*pclk);
4328
4329 err = clk_prepare_enable(*pclk);
4330 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004331 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004332 return err;
4333 }
4334
4335 return 0;
4336}
4337
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004338static int at91ether_init(struct platform_device *pdev)
4339{
4340 struct net_device *dev = platform_get_drvdata(pdev);
4341 struct macb *bp = netdev_priv(dev);
4342 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004343
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004344 bp->queues[0].bp = bp;
4345
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004346 dev->netdev_ops = &at91ether_netdev_ops;
4347 dev->ethtool_ops = &macb_ethtool_ops;
4348
4349 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4350 0, dev->name, dev);
4351 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004352 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004353
4354 macb_writel(bp, NCR, 0);
4355
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004356 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004357
4358 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004359}
4360
Yash Shahc218ad52019-06-18 13:26:08 +05304361static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4362 unsigned long parent_rate)
4363{
4364 return mgmt->rate;
4365}
4366
4367static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4368 unsigned long *parent_rate)
4369{
4370 if (WARN_ON(rate < 2500000))
4371 return 2500000;
4372 else if (rate == 2500000)
4373 return 2500000;
4374 else if (WARN_ON(rate < 13750000))
4375 return 2500000;
4376 else if (WARN_ON(rate < 25000000))
4377 return 25000000;
4378 else if (rate == 25000000)
4379 return 25000000;
4380 else if (WARN_ON(rate < 75000000))
4381 return 25000000;
4382 else if (WARN_ON(rate < 125000000))
4383 return 125000000;
4384 else if (rate == 125000000)
4385 return 125000000;
4386
4387 WARN_ON(rate > 125000000);
4388
4389 return 125000000;
4390}
4391
4392static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4393 unsigned long parent_rate)
4394{
4395 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4396 if (rate != 125000000)
4397 iowrite32(1, mgmt->reg);
4398 else
4399 iowrite32(0, mgmt->reg);
4400 mgmt->rate = rate;
4401
4402 return 0;
4403}
4404
4405static const struct clk_ops fu540_c000_ops = {
4406 .recalc_rate = fu540_macb_tx_recalc_rate,
4407 .round_rate = fu540_macb_tx_round_rate,
4408 .set_rate = fu540_macb_tx_set_rate,
4409};
4410
4411static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4412 struct clk **hclk, struct clk **tx_clk,
4413 struct clk **rx_clk, struct clk **tsu_clk)
4414{
4415 struct clk_init_data init;
4416 int err = 0;
4417
4418 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4419 if (err)
4420 return err;
4421
4422 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4423 if (!mgmt)
4424 return -ENOMEM;
4425
4426 init.name = "sifive-gemgxl-mgmt";
4427 init.ops = &fu540_c000_ops;
4428 init.flags = 0;
4429 init.num_parents = 0;
4430
4431 mgmt->rate = 0;
4432 mgmt->hw.init = &init;
4433
Stephen Boydd89091a2020-01-03 16:19:21 -08004434 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Yash Shahc218ad52019-06-18 13:26:08 +05304435 if (IS_ERR(*tx_clk))
4436 return PTR_ERR(*tx_clk);
4437
4438 err = clk_prepare_enable(*tx_clk);
4439 if (err)
4440 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4441 else
4442 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4443
4444 return 0;
4445}
4446
4447static int fu540_c000_init(struct platform_device *pdev)
4448{
Dejin Zhengb959c772020-05-03 20:32:26 +08004449 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4450 if (IS_ERR(mgmt->reg))
4451 return PTR_ERR(mgmt->reg);
Yash Shahc218ad52019-06-18 13:26:08 +05304452
4453 return macb_init(pdev);
4454}
4455
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004456static const struct macb_usrio_config macb_default_usrio = {
4457 .mii = MACB_BIT(MII),
4458 .rmii = MACB_BIT(RMII),
4459 .rgmii = GEM_BIT(RGMII),
4460 .refclk = MACB_BIT(CLKEN),
4461};
4462
Yash Shahc218ad52019-06-18 13:26:08 +05304463static const struct macb_config fu540_c000_config = {
4464 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4465 MACB_CAPS_GEM_HAS_PTP,
4466 .dma_burst_length = 16,
4467 .clk_init = fu540_c000_clk_init,
4468 .init = fu540_c000_init,
4469 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004470 .usrio = &macb_default_usrio,
Yash Shahc218ad52019-06-18 13:26:08 +05304471};
4472
David S. Miller3cef5c52015-03-09 23:38:02 -04004473static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004474 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004475 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004476 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004477 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004478};
4479
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004480static const struct macb_config sama5d3macb_config = {
4481 .caps = MACB_CAPS_SG_DISABLED
4482 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4483 .clk_init = macb_clk_init,
4484 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004485 .usrio = &macb_default_usrio,
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004486};
4487
David S. Miller3cef5c52015-03-09 23:38:02 -04004488static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004489 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4490 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004491 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004492 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004493 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004494};
4495
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004496static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004497 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004498 .dma_burst_length = 16,
4499 .clk_init = macb_clk_init,
4500 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004501 .usrio = &macb_default_usrio,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004502};
4503
David S. Miller3cef5c52015-03-09 23:38:02 -04004504static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004505 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004506 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004507 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004508 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004509 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004510 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004511 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004512};
4513
David S. Miller3cef5c52015-03-09 23:38:02 -04004514static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004515 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004516 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004517 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004518 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004519 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004520};
4521
David S. Miller3cef5c52015-03-09 23:38:02 -04004522static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004523 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004524 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004525 .init = at91ether_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004526 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004527};
4528
Neil Armstronge611b5b2016-01-05 14:39:17 +01004529static const struct macb_config np4_config = {
4530 .caps = MACB_CAPS_USRIO_DISABLED,
4531 .clk_init = macb_clk_init,
4532 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004533 .usrio = &macb_default_usrio,
Neil Armstronge611b5b2016-01-05 14:39:17 +01004534};
David S. Miller36583eb2015-05-23 01:22:35 -04004535
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304536static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004537 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4538 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304539 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304540 .dma_burst_length = 16,
4541 .clk_init = macb_clk_init,
4542 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304543 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004544 .usrio = &macb_default_usrio,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304545};
4546
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004547static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304548 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4549 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004550 .dma_burst_length = 16,
4551 .clk_init = macb_clk_init,
4552 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004553 .usrio = &macb_default_usrio,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004554};
4555
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004556static const struct of_device_id macb_dt_ids[] = {
4557 { .compatible = "cdns,at32ap7000-macb" },
4558 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4559 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004560 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004561 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4562 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004563 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004564 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004565 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004566 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004567 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4568 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4569 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304570 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004571 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304572 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004573 { /* sentinel */ }
4574};
4575MODULE_DEVICE_TABLE(of, macb_dt_ids);
4576#endif /* CONFIG_OF */
4577
Bartosz Folta83a77e92016-12-14 06:39:15 +00004578static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004579 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4580 MACB_CAPS_JUMBO |
4581 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004582 .dma_burst_length = 16,
4583 .clk_init = macb_clk_init,
4584 .init = macb_init,
4585 .jumbo_max_len = 10240,
4586};
4587
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004588static int macb_probe(struct platform_device *pdev)
4589{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004590 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004591 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304592 struct clk **, struct clk **, struct clk **,
4593 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004594 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004595 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304596 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304597 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004598 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004599 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004600 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004601 struct net_device *dev;
4602 struct resource *regs;
4603 void __iomem *mem;
4604 const char *mac;
4605 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304606 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004607
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004608 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4609 mem = devm_ioremap_resource(&pdev->dev, regs);
4610 if (IS_ERR(mem))
4611 return PTR_ERR(mem);
4612
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004613 if (np) {
4614 const struct of_device_id *match;
4615
4616 match = of_match_node(macb_dt_ids, np);
4617 if (match && match->data) {
4618 macb_config = match->data;
4619 clk_init = macb_config->clk_init;
4620 init = macb_config->init;
4621 }
4622 }
4623
Harini Katakamf5473d12019-03-01 16:20:33 +05304624 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004625 if (err)
4626 return err;
4627
Harini Katakamd54f89a2019-03-01 16:20:34 +05304628 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4629 pm_runtime_use_autosuspend(&pdev->dev);
4630 pm_runtime_get_noresume(&pdev->dev);
4631 pm_runtime_set_active(&pdev->dev);
4632 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004633 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004634
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004635 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004636 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004637 if (!dev) {
4638 err = -ENOMEM;
4639 goto err_disable_clocks;
4640 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004641
4642 dev->base_addr = regs->start;
4643
4644 SET_NETDEV_DEV(dev, &pdev->dev);
4645
4646 bp = netdev_priv(dev);
4647 bp->pdev = pdev;
4648 bp->dev = dev;
4649 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004650 bp->native_io = native_io;
4651 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004652 bp->macb_reg_readl = hw_readl_native;
4653 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004654 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004655 bp->macb_reg_readl = hw_readl;
4656 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004657 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004658 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004659 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004660 if (macb_config)
4661 bp->dma_burst_length = macb_config->dma_burst_length;
4662 bp->pclk = pclk;
4663 bp->hclk = hclk;
4664 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304665 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304666 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004667 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304668 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304669
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004670 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004671 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004672 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
Nicolas Ferreced47992020-07-10 14:46:42 +02004673 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004674
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004675 bp->usrio = macb_config->usrio;
4676
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004677 spin_lock_init(&bp->lock);
4678
Nicolas Ferread783472015-03-31 15:02:02 +02004679 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004680 macb_configure_caps(bp, macb_config);
4681
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004682#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4683 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4684 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4685 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4686 }
4687#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004688 platform_set_drvdata(pdev, dev);
4689
4690 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004691 if (dev->irq < 0) {
4692 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004693 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004694 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004695
Jarod Wilson44770e12016-10-17 15:54:17 -04004696 /* MTU range: 68 - 1500 or 10240 */
4697 dev->min_mtu = GEM_MTU_MIN_SIZE;
4698 if (bp->caps & MACB_CAPS_JUMBO)
4699 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4700 else
4701 dev->max_mtu = ETH_DATA_LEN;
4702
Harini Katakam404cd082018-07-06 12:18:58 +05304703 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4704 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4705 if (val)
4706 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4707 macb_dma_desc_get_size(bp);
4708
4709 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4710 if (val)
4711 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4712 macb_dma_desc_get_size(bp);
4713 }
4714
Harini Katakame5010702019-01-29 15:20:03 +05304715 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4716 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4717 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4718
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004719 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004720 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4721 err = -EPROBE_DEFER;
4722 goto err_out_free_netdev;
Antoine Tenart2bf4ecb2019-06-21 17:26:35 +02004723 } else if (!IS_ERR_OR_NULL(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004724 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004725 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004726 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004727 }
frederic RODO6c36a702007-07-12 19:07:24 +02004728
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004729 err = of_get_phy_mode(np, &interface);
4730 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004731 /* not found in DT, MII by default */
4732 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4733 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004734 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004735
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004736 /* IP specific init */
4737 err = init(pdev);
4738 if (err)
4739 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004740
Florian Fainellicf669662016-05-02 18:38:45 -07004741 err = macb_mii_init(bp);
4742 if (err)
4743 goto err_out_free_netdev;
4744
Florian Fainellicf669662016-05-02 18:38:45 -07004745 netif_carrier_off(dev);
4746
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004747 err = register_netdev(dev);
4748 if (err) {
4749 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004750 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004751 }
4752
Allen Paise7412b82020-09-14 12:59:23 +05304753 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
Harini Katakam032dc412018-01-27 12:09:01 +05304754
Bo Shen58798232014-09-13 01:57:49 +02004755 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4756 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4757 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004758
Harini Katakamd54f89a2019-03-01 16:20:34 +05304759 pm_runtime_mark_last_busy(&bp->pdev->dev);
4760 pm_runtime_put_autosuspend(&bp->pdev->dev);
4761
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004762 return 0;
4763
Florian Fainellicf669662016-05-02 18:38:45 -07004764err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004765 mdiobus_unregister(bp->mii_bus);
4766 mdiobus_free(bp->mii_bus);
4767
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004768err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004769 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004770
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004771err_disable_clocks:
Claudiu Beznea38493da2020-12-09 15:03:34 +02004772 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304773 pm_runtime_disable(&pdev->dev);
4774 pm_runtime_set_suspended(&pdev->dev);
4775 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004776
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004777 return err;
4778}
4779
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004780static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004781{
4782 struct net_device *dev;
4783 struct macb *bp;
4784
4785 dev = platform_get_drvdata(pdev);
4786
4787 if (dev) {
4788 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004789 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004790 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004791
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004792 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004793 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304794 pm_runtime_disable(&pdev->dev);
4795 pm_runtime_dont_use_autosuspend(&pdev->dev);
4796 if (!pm_runtime_suspended(&pdev->dev)) {
Claudiu Beznea38493da2020-12-09 15:03:34 +02004797 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
4798 bp->rx_clk, bp->tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304799 pm_runtime_set_suspended(&pdev->dev);
4800 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004801 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004802 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004803 }
4804
4805 return 0;
4806}
4807
Michal Simekd23823d2015-01-23 09:36:03 +01004808static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004809{
Wolfram Sangce886a42018-10-21 22:00:14 +02004810 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004811 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304812 struct macb_queue *queue = bp->queues;
4813 unsigned long flags;
4814 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004815 int err;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004816
Harini Katakamde991c52019-03-01 16:20:35 +05304817 if (!netif_running(netdev))
4818 return 0;
4819
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004820 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004821 spin_lock_irqsave(&bp->lock, flags);
4822 /* Flush all status bits */
4823 macb_writel(bp, TSR, -1);
4824 macb_writel(bp, RSR, -1);
Harini Katakamde991c52019-03-01 16:20:35 +05304825 for (q = 0, queue = bp->queues; q < bp->num_queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004826 ++q, ++queue) {
4827 /* Disable all interrupts */
4828 queue_writel(queue, IDR, -1);
4829 queue_readl(queue, ISR);
4830 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4831 queue_writel(queue, ISR, -1);
4832 }
4833 /* Change interrupt handler and
4834 * Enable WoL IRQ on queue 0
4835 */
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004836 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004837 if (macb_is_gem(bp)) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004838 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
4839 IRQF_SHARED, netdev->name, bp->queues);
4840 if (err) {
4841 dev_err(dev,
4842 "Unable to request IRQ %d (error %d)\n",
4843 bp->queues[0].irq, err);
4844 spin_unlock_irqrestore(&bp->lock, flags);
4845 return err;
4846 }
4847 queue_writel(bp->queues, IER, GEM_BIT(WOL));
4848 gem_writel(bp, WOL, MACB_BIT(MAG));
4849 } else {
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004850 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
4851 IRQF_SHARED, netdev->name, bp->queues);
4852 if (err) {
4853 dev_err(dev,
4854 "Unable to request IRQ %d (error %d)\n",
4855 bp->queues[0].irq, err);
4856 spin_unlock_irqrestore(&bp->lock, flags);
4857 return err;
4858 }
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004859 queue_writel(bp->queues, IER, MACB_BIT(WOL));
4860 macb_writel(bp, WOL, MACB_BIT(MAG));
4861 }
4862 spin_unlock_irqrestore(&bp->lock, flags);
4863
4864 enable_irq_wake(bp->queues[0].irq);
4865 }
4866
4867 netif_device_detach(netdev);
4868 for (q = 0, queue = bp->queues; q < bp->num_queues;
4869 ++q, ++queue)
4870 napi_disable(&queue->napi);
4871
4872 if (!(bp->wol & MACB_WOL_ENABLED)) {
Antoine Tenart7897b072019-11-13 10:00:06 +01004873 rtnl_lock();
4874 phylink_stop(bp->phylink);
4875 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304876 spin_lock_irqsave(&bp->lock, flags);
4877 macb_reset_hw(bp);
4878 spin_unlock_irqrestore(&bp->lock, flags);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304879 }
4880
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004881 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4882 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4883
4884 if (netdev->hw_features & NETIF_F_NTUPLE)
4885 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4886
Harini Katakamde991c52019-03-01 16:20:35 +05304887 if (bp->ptp_info)
4888 bp->ptp_info->ptp_remove(netdev);
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004889 if (!device_may_wakeup(dev))
4890 pm_runtime_force_suspend(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304891
4892 return 0;
4893}
4894
4895static int __maybe_unused macb_resume(struct device *dev)
4896{
4897 struct net_device *netdev = dev_get_drvdata(dev);
4898 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304899 struct macb_queue *queue = bp->queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004900 unsigned long flags;
Harini Katakamde991c52019-03-01 16:20:35 +05304901 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004902 int err;
Harini Katakamde991c52019-03-01 16:20:35 +05304903
4904 if (!netif_running(netdev))
4905 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304906
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004907 if (!device_may_wakeup(dev))
4908 pm_runtime_force_resume(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304909
4910 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004911 spin_lock_irqsave(&bp->lock, flags);
4912 /* Disable WoL */
4913 if (macb_is_gem(bp)) {
4914 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
4915 gem_writel(bp, WOL, 0);
4916 } else {
4917 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
4918 macb_writel(bp, WOL, 0);
4919 }
4920 /* Clear ISR on queue 0 */
4921 queue_readl(bp->queues, ISR);
4922 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4923 queue_writel(bp->queues, ISR, -1);
4924 /* Replace interrupt handler on queue 0 */
4925 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
4926 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
4927 IRQF_SHARED, netdev->name, bp->queues);
4928 if (err) {
4929 dev_err(dev,
4930 "Unable to request IRQ %d (error %d)\n",
4931 bp->queues[0].irq, err);
4932 spin_unlock_irqrestore(&bp->lock, flags);
4933 return err;
4934 }
4935 spin_unlock_irqrestore(&bp->lock, flags);
4936
Harini Katakamd54f89a2019-03-01 16:20:34 +05304937 disable_irq_wake(bp->queues[0].irq);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004938
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004939 /* Now make sure we disable phy before moving
4940 * to common restore path
4941 */
Antoine Tenart7897b072019-11-13 10:00:06 +01004942 rtnl_lock();
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004943 phylink_stop(bp->phylink);
Antoine Tenart7897b072019-11-13 10:00:06 +01004944 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304945 }
4946
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004947 for (q = 0, queue = bp->queues; q < bp->num_queues;
4948 ++q, ++queue)
4949 napi_enable(&queue->napi);
4950
4951 if (netdev->hw_features & NETIF_F_NTUPLE)
4952 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4953
4954 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4955 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4956
4957 macb_writel(bp, NCR, MACB_BIT(MPE));
Harini Katakamde991c52019-03-01 16:20:35 +05304958 macb_init_hw(bp);
4959 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004960 macb_restore_features(bp);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004961 rtnl_lock();
4962 phylink_start(bp->phylink);
4963 rtnl_unlock();
4964
Harini Katakamd54f89a2019-03-01 16:20:34 +05304965 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304966 if (bp->ptp_info)
4967 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304968
4969 return 0;
4970}
4971
4972static int __maybe_unused macb_runtime_suspend(struct device *dev)
4973{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004974 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304975 struct macb *bp = netdev_priv(netdev);
4976
Claudiu Beznea38493da2020-12-09 15:03:34 +02004977 if (!(device_may_wakeup(dev)))
4978 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
4979 else
4980 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004981
4982 return 0;
4983}
4984
Harini Katakamd54f89a2019-03-01 16:20:34 +05304985static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004986{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004987 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004988 struct macb *bp = netdev_priv(netdev);
4989
Nicolas Ferre515a10a2020-07-10 14:46:41 +02004990 if (!(device_may_wakeup(dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004991 clk_prepare_enable(bp->pclk);
4992 clk_prepare_enable(bp->hclk);
4993 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304994 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004995 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304996 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004997
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004998 return 0;
4999}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005000
Harini Katakamd54f89a2019-03-01 16:20:34 +05305001static const struct dev_pm_ops macb_pm_ops = {
5002 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
5003 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
5004};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005005
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005006static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005007 .probe = macb_probe,
5008 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005009 .driver = {
5010 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01005011 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005012 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005013 },
5014};
5015
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005016module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005017
5018MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00005019MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02005020MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07005021MODULE_ALIAS("platform:macb");