blob: 0f6a6cb7e98d7714188bd6880b1d8d47b0d46a7d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00003 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004 *
5 * Copyright (C) 2004-2006 Atmel Corporation
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01006 */
7
Jamie Ilesc220f8c2011-03-08 20:27:08 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01009#include <linux/clk.h>
Yash Shahc218ad52019-06-18 13:26:08 +053010#include <linux/clk-provider.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030011#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000016#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010017#include <linux/slab.h>
18#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080019#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000020#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010021#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/platform_device.h>
Antoine Tenart7897b072019-11-13 10:00:06 +010027#include <linux/phylink.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080028#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010029#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010030#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000033#include <linux/ip.h>
34#include <linux/udp.h>
35#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053036#include <linux/iopoll.h>
Harini Katakamd54f89a2019-03-01 16:20:34 +053037#include <linux/pm_runtime.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Yash Shahc218ad52019-06-18 13:26:08 +053040/* This structure is only used for MACB on SiFive FU540 devices */
41struct sifive_fu540_macb_mgmt {
42 void __iomem *reg;
43 unsigned long rate;
44 struct clk_hw hw;
45};
46
Nicolas Ferre1b447912013-06-04 21:57:11 +000047#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000048#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050049
Zach Brownb410d132016-10-19 09:56:57 -050050#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050051#define MIN_RX_RING_SIZE 64
52#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000053#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050054 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
Zach Brownb410d132016-10-19 09:56:57 -050056#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050057#define MIN_TX_RING_SIZE 64
58#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000059#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050060 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010061
Nicolas Ferre909a8582012-11-19 06:00:21 +000062/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050063#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010064
Harini Katakame5010702019-01-29 15:20:03 +053065#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000066#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
67 | MACB_BIT(ISR_RLE) \
68 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000069#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
70 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000071
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000072/* Max length of transmit frame must be a multiple of 8 bytes */
73#define MACB_TX_LEN_ALIGN 8
74#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Harini Katakamf822e9c2020-02-05 18:08:12 +053075/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
76 * false amba_error in TX path from the DMA assuming there is not enough
77 * space in the SRAM (16KB) even when there is.
78 */
79#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020080
Jarod Wilson44770e12016-10-17 15:54:17 -040081#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070082#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053083
Sergio Prado3e2a5e12016-02-09 12:07:16 -020084#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
85#define MACB_WOL_ENABLED (0x1 << 1)
86
Parshuram Thombaree4e143e2020-10-29 13:47:07 +010087#define HS_SPEED_10000M 4
88#define MACB_SERDES_RATE_10G 1
89
Moritz Fischer64ec42f2016-03-29 19:11:12 -070090/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000091 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
92 */
93#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010094
Harini Katakamd54f89a2019-03-01 16:20:34 +053095#define MACB_PM_TIMEOUT 100 /* ms */
96
Harini Katakam8beb79b2019-03-01 16:20:32 +053097#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
98
Rafal Ozieblodc97a892017-01-27 15:08:20 +000099/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100100 * depends on hardware configuration:
101 *
102 * 1. dma address width 32 bits:
103 * word 1: 32 bit address of Data Buffer
104 * word 2: control
105 *
106 * 2. dma address width 64 bits:
107 * word 1: 32 bit address of Data Buffer
108 * word 2: control
109 * word 3: upper 32 bit address of Data Buffer
110 * word 4: unused
111 *
112 * 3. dma address width 32 bits with hardware timestamping:
113 * word 1: 32 bit address of Data Buffer
114 * word 2: control
115 * word 3: timestamp word 1
116 * word 4: timestamp word 2
117 *
118 * 4. dma address width 64 bits with hardware timestamping:
119 * word 1: 32 bit address of Data Buffer
120 * word 2: control
121 * word 3: upper 32 bit address of Data Buffer
122 * word 4: unused
123 * word 5: timestamp word 1
124 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000125 */
126static unsigned int macb_dma_desc_get_size(struct macb *bp)
127{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100128#ifdef MACB_EXT_DESC
129 unsigned int desc_size;
130
131 switch (bp->hw_dma_cap) {
132 case HW_DMA_CAP_64B:
133 desc_size = sizeof(struct macb_dma_desc)
134 + sizeof(struct macb_dma_desc_64);
135 break;
136 case HW_DMA_CAP_PTP:
137 desc_size = sizeof(struct macb_dma_desc)
138 + sizeof(struct macb_dma_desc_ptp);
139 break;
140 case HW_DMA_CAP_64B_PTP:
141 desc_size = sizeof(struct macb_dma_desc)
142 + sizeof(struct macb_dma_desc_64)
143 + sizeof(struct macb_dma_desc_ptp);
144 break;
145 default:
146 desc_size = sizeof(struct macb_dma_desc);
147 }
148 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
150 return sizeof(struct macb_dma_desc);
151}
152
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100153static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000154{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100155#ifdef MACB_EXT_DESC
156 switch (bp->hw_dma_cap) {
157 case HW_DMA_CAP_64B:
158 case HW_DMA_CAP_PTP:
159 desc_idx <<= 1;
160 break;
161 case HW_DMA_CAP_64B_PTP:
162 desc_idx *= 3;
163 break;
164 default:
165 break;
166 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000167#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100168 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000169}
170
171#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
172static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
173{
Shubhrajyoti Datta99dcb842019-09-23 14:03:51 +0530174 return (struct macb_dma_desc_64 *)((void *)desc
175 + sizeof(struct macb_dma_desc));
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000176}
177#endif
178
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500180static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181{
Zach Brownb410d132016-10-19 09:56:57 -0500182 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183}
184
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100185static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
186 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000187{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000188 index = macb_tx_ring_wrap(queue->bp, index);
189 index = macb_adj_dma_desc_idx(queue->bp, index);
190 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000191}
192
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100193static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
194 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195{
Zach Brownb410d132016-10-19 09:56:57 -0500196 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000197}
198
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100199static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000200{
201 dma_addr_t offset;
202
Zach Brownb410d132016-10-19 09:56:57 -0500203 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000204 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100206 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000207}
208
Zach Brownb410d132016-10-19 09:56:57 -0500209static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000210{
Zach Brownb410d132016-10-19 09:56:57 -0500211 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000212}
213
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000214static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000215{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000216 index = macb_rx_ring_wrap(queue->bp, index);
217 index = macb_adj_dma_desc_idx(queue->bp, index);
218 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000219}
220
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000221static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000222{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000223 return queue->rx_buffers + queue->bp->rx_buffer_size *
224 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000225}
226
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300227/* I/O accessors */
228static u32 hw_readl_native(struct macb *bp, int offset)
229{
230 return __raw_readl(bp->regs + offset);
231}
232
233static void hw_writel_native(struct macb *bp, int offset, u32 value)
234{
235 __raw_writel(value, bp->regs + offset);
236}
237
238static u32 hw_readl(struct macb *bp, int offset)
239{
240 return readl_relaxed(bp->regs + offset);
241}
242
243static void hw_writel(struct macb *bp, int offset, u32 value)
244{
245 writel_relaxed(value, bp->regs + offset);
246}
247
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700248/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700249 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300250 * descriptor access.
251 */
252static bool hw_is_native_io(void __iomem *addr)
253{
254 u32 value = MACB_BIT(LLB);
255
256 __raw_writel(value, addr + MACB_NCR);
257 value = __raw_readl(addr + MACB_NCR);
258
259 /* Write 0 back to disable everything */
260 __raw_writel(0, addr + MACB_NCR);
261
262 return value == MACB_BIT(LLB);
263}
264
265static bool hw_is_gem(void __iomem *addr, bool native_io)
266{
267 u32 id;
268
269 if (native_io)
270 id = __raw_readl(addr + MACB_MID);
271 else
272 id = readl_relaxed(addr + MACB_MID);
273
274 return MACB_BFEXT(IDNUM, id) >= 0x2;
275}
276
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100277static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100278{
279 u32 bottom;
280 u16 top;
281
282 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000283 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000285 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000286
287 /* Clear unused address register sets */
288 macb_or_gem_writel(bp, SA2B, 0);
289 macb_or_gem_writel(bp, SA2T, 0);
290 macb_or_gem_writel(bp, SA3B, 0);
291 macb_or_gem_writel(bp, SA3T, 0);
292 macb_or_gem_writel(bp, SA4B, 0);
293 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294}
295
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100296static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297{
298 u32 bottom;
299 u16 top;
300 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000301 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100302
Moritz Fischeraa50b552016-03-29 19:11:13 -0700303 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000304 for (i = 0; i < 4; i++) {
305 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
306 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100307
Nicolas Ferre8b952742019-05-03 12:36:58 +0200308 addr[0] = bottom & 0xff;
309 addr[1] = (bottom >> 8) & 0xff;
310 addr[2] = (bottom >> 16) & 0xff;
311 addr[3] = (bottom >> 24) & 0xff;
312 addr[4] = top & 0xff;
313 addr[5] = (top >> 8) & 0xff;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100314
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315 if (is_valid_ether_addr(addr)) {
316 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
317 return;
318 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700319 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000320
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300321 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000322 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323}
324
Harini Katakam8beb79b2019-03-01 16:20:32 +0530325static int macb_mdio_wait_for_idle(struct macb *bp)
326{
327 u32 val;
328
329 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
330 1, MACB_MDIO_TIMEOUT);
331}
332
frederic RODO6c36a702007-07-12 19:07:24 +0200333static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334{
frederic RODO6c36a702007-07-12 19:07:24 +0200335 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530336 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530337
Harini Katakamd54f89a2019-03-01 16:20:34 +0530338 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300339 if (status < 0) {
340 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530341 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300342 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530343
344 status = macb_mdio_wait_for_idle(bp);
345 if (status < 0)
346 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347
Milind Parab43ad3522020-01-09 08:36:46 +0000348 if (regnum & MII_ADDR_C45) {
349 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
350 | MACB_BF(RW, MACB_MAN_C45_ADDR)
351 | MACB_BF(PHYA, mii_id)
352 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
353 | MACB_BF(DATA, regnum & 0xFFFF)
354 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
355
356 status = macb_mdio_wait_for_idle(bp);
357 if (status < 0)
358 goto mdio_read_exit;
359
360 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
361 | MACB_BF(RW, MACB_MAN_C45_READ)
362 | MACB_BF(PHYA, mii_id)
363 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
364 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
365 } else {
366 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
367 | MACB_BF(RW, MACB_MAN_C22_READ)
368 | MACB_BF(PHYA, mii_id)
369 | MACB_BF(REGA, regnum)
370 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
371 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100372
Harini Katakamd54f89a2019-03-01 16:20:34 +0530373 status = macb_mdio_wait_for_idle(bp);
374 if (status < 0)
375 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100376
Harini Katakamd54f89a2019-03-01 16:20:34 +0530377 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100378
Harini Katakamd54f89a2019-03-01 16:20:34 +0530379mdio_read_exit:
380 pm_runtime_mark_last_busy(&bp->pdev->dev);
381 pm_runtime_put_autosuspend(&bp->pdev->dev);
382mdio_pm_exit:
383 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100384}
385
frederic RODO6c36a702007-07-12 19:07:24 +0200386static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
387 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100388{
frederic RODO6c36a702007-07-12 19:07:24 +0200389 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530390 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530391
Harini Katakamd54f89a2019-03-01 16:20:34 +0530392 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300393 if (status < 0) {
394 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530395 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300396 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530397
398 status = macb_mdio_wait_for_idle(bp);
399 if (status < 0)
400 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100401
Milind Parab43ad3522020-01-09 08:36:46 +0000402 if (regnum & MII_ADDR_C45) {
403 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
404 | MACB_BF(RW, MACB_MAN_C45_ADDR)
405 | MACB_BF(PHYA, mii_id)
406 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
407 | MACB_BF(DATA, regnum & 0xFFFF)
408 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
409
410 status = macb_mdio_wait_for_idle(bp);
411 if (status < 0)
412 goto mdio_write_exit;
413
414 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
415 | MACB_BF(RW, MACB_MAN_C45_WRITE)
416 | MACB_BF(PHYA, mii_id)
417 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
418 | MACB_BF(CODE, MACB_MAN_C45_CODE)
419 | MACB_BF(DATA, value)));
420 } else {
421 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
422 | MACB_BF(RW, MACB_MAN_C22_WRITE)
423 | MACB_BF(PHYA, mii_id)
424 | MACB_BF(REGA, regnum)
425 | MACB_BF(CODE, MACB_MAN_C22_CODE)
426 | MACB_BF(DATA, value)));
427 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100428
Harini Katakamd54f89a2019-03-01 16:20:34 +0530429 status = macb_mdio_wait_for_idle(bp);
430 if (status < 0)
431 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100432
Harini Katakamd54f89a2019-03-01 16:20:34 +0530433mdio_write_exit:
434 pm_runtime_mark_last_busy(&bp->pdev->dev);
435 pm_runtime_put_autosuspend(&bp->pdev->dev);
436mdio_pm_exit:
437 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438}
439
Antoine Tenart6e952d92019-11-13 10:00:05 +0100440static void macb_init_buffers(struct macb *bp)
441{
442 struct macb_queue *queue;
443 unsigned int q;
444
445 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
446 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
447#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
448 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
449 queue_writel(queue, RBQPH,
450 upper_32_bits(queue->rx_ring_dma));
451#endif
452 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
453#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
454 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
455 queue_writel(queue, TBQPH,
456 upper_32_bits(queue->tx_ring_dma));
457#endif
458 }
459}
460
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800461/**
462 * macb_set_tx_clk() - Set a clock to a new frequency
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200463 * @bp: pointer to struct macb
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -0700464 * @speed: New frequency in Hz
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800465 */
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200466static void macb_set_tx_clk(struct macb *bp, int speed)
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800467{
468 long ferr, rate, rate_rounded;
469
Charles Keepax1d0d5612021-01-04 10:38:02 +0000470 if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100471 return;
472
Michael Walle43e576312021-01-20 20:43:03 +0100473 /* In case of MII the PHY is the clock master */
474 if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
475 return;
476
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800477 switch (speed) {
478 case SPEED_10:
479 rate = 2500000;
480 break;
481 case SPEED_100:
482 rate = 25000000;
483 break;
484 case SPEED_1000:
485 rate = 125000000;
486 break;
487 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800488 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800489 }
490
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200491 rate_rounded = clk_round_rate(bp->tx_clk, rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800492 if (rate_rounded < 0)
493 return;
494
495 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
496 * is not satisfied.
497 */
498 ferr = abs(rate_rounded - rate);
499 ferr = DIV_ROUND_UP(ferr, rate / 100000);
500 if (ferr > 5)
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200501 netdev_warn(bp->dev,
502 "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700503 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800504
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200505 if (clk_set_rate(bp->tx_clk, rate_rounded))
506 netdev_err(bp->dev, "adjusting tx_clk failed.\n");
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800507}
508
Antoine Tenart7897b072019-11-13 10:00:06 +0100509static void macb_validate(struct phylink_config *config,
510 unsigned long *supported,
511 struct phylink_link_state *state)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100512{
Antoine Tenart7897b072019-11-13 10:00:06 +0100513 struct net_device *ndev = to_net_dev(config->dev);
514 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
515 struct macb *bp = netdev_priv(ndev);
516
517 /* We only support MII, RMII, GMII, RGMII & SGMII. */
518 if (state->interface != PHY_INTERFACE_MODE_NA &&
519 state->interface != PHY_INTERFACE_MODE_MII &&
520 state->interface != PHY_INTERFACE_MODE_RMII &&
521 state->interface != PHY_INTERFACE_MODE_GMII &&
522 state->interface != PHY_INTERFACE_MODE_SGMII &&
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100523 state->interface != PHY_INTERFACE_MODE_10GBASER &&
Antoine Tenart7897b072019-11-13 10:00:06 +0100524 !phy_interface_mode_is_rgmii(state->interface)) {
525 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
526 return;
527 }
528
529 if (!macb_is_gem(bp) &&
530 (state->interface == PHY_INTERFACE_MODE_GMII ||
531 phy_interface_mode_is_rgmii(state->interface))) {
532 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
533 return;
534 }
535
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100536 if (state->interface == PHY_INTERFACE_MODE_10GBASER &&
537 !(bp->caps & MACB_CAPS_HIGH_SPEED &&
538 bp->caps & MACB_CAPS_PCS)) {
539 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
540 return;
541 }
542
Antoine Tenart7897b072019-11-13 10:00:06 +0100543 phylink_set_port_modes(mask);
544 phylink_set(mask, Autoneg);
545 phylink_set(mask, Asym_Pause);
546
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100547 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
548 (state->interface == PHY_INTERFACE_MODE_NA ||
549 state->interface == PHY_INTERFACE_MODE_10GBASER)) {
550 phylink_set(mask, 10000baseCR_Full);
551 phylink_set(mask, 10000baseER_Full);
552 phylink_set(mask, 10000baseKR_Full);
553 phylink_set(mask, 10000baseLR_Full);
554 phylink_set(mask, 10000baseLRM_Full);
555 phylink_set(mask, 10000baseSR_Full);
556 phylink_set(mask, 10000baseT_Full);
557 if (state->interface != PHY_INTERFACE_MODE_NA)
558 goto out;
559 }
560
Antoine Tenart7897b072019-11-13 10:00:06 +0100561 phylink_set(mask, 10baseT_Half);
562 phylink_set(mask, 10baseT_Full);
563 phylink_set(mask, 100baseT_Half);
564 phylink_set(mask, 100baseT_Full);
565
566 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
567 (state->interface == PHY_INTERFACE_MODE_NA ||
568 state->interface == PHY_INTERFACE_MODE_GMII ||
569 state->interface == PHY_INTERFACE_MODE_SGMII ||
570 phy_interface_mode_is_rgmii(state->interface))) {
571 phylink_set(mask, 1000baseT_Full);
572 phylink_set(mask, 1000baseX_Full);
573
574 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
575 phylink_set(mask, 1000baseT_Half);
576 }
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100577out:
Antoine Tenart7897b072019-11-13 10:00:06 +0100578 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
579 bitmap_and(state->advertising, state->advertising, mask,
580 __ETHTOOL_LINK_MODE_MASK_NBITS);
581}
582
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100583static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
584 phy_interface_t interface, int speed,
585 int duplex)
586{
587 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
588 u32 config;
589
590 config = gem_readl(bp, USX_CONTROL);
591 config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
592 config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
593 config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
594 config |= GEM_BIT(TX_EN);
595 gem_writel(bp, USX_CONTROL, config);
596}
597
598static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
Russell Kingd46b7e42019-11-21 00:36:22 +0000599 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100600{
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100601 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
602 u32 val;
603
604 state->speed = SPEED_10000;
605 state->duplex = 1;
606 state->an_complete = 1;
607
608 val = gem_readl(bp, USX_STATUS);
609 state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
610 val = gem_readl(bp, NCFGR);
611 if (val & GEM_BIT(PAE))
612 state->pause = MLO_PAUSE_RX;
613}
614
615static int macb_usx_pcs_config(struct phylink_pcs *pcs,
616 unsigned int mode,
617 phy_interface_t interface,
618 const unsigned long *advertising,
619 bool permit_pause_to_mac)
620{
621 struct macb *bp = container_of(pcs, struct macb, phylink_pcs);
622
623 gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
624 GEM_BIT(SIGNAL_OK));
625
626 return 0;
627}
628
629static void macb_pcs_get_state(struct phylink_pcs *pcs,
630 struct phylink_link_state *state)
631{
Russell Kingd46b7e42019-11-21 00:36:22 +0000632 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100633}
634
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100635static void macb_pcs_an_restart(struct phylink_pcs *pcs)
Antoine Tenart7897b072019-11-13 10:00:06 +0100636{
637 /* Not supported */
638}
639
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100640static int macb_pcs_config(struct phylink_pcs *pcs,
641 unsigned int mode,
642 phy_interface_t interface,
643 const unsigned long *advertising,
644 bool permit_pause_to_mac)
645{
646 return 0;
647}
648
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100649static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
650 .pcs_get_state = macb_usx_pcs_get_state,
651 .pcs_config = macb_usx_pcs_config,
652 .pcs_link_up = macb_usx_pcs_link_up,
653};
654
655static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
656 .pcs_get_state = macb_pcs_get_state,
657 .pcs_an_restart = macb_pcs_an_restart,
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100658 .pcs_config = macb_pcs_config,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100659};
660
Antoine Tenart7897b072019-11-13 10:00:06 +0100661static void macb_mac_config(struct phylink_config *config, unsigned int mode,
662 const struct phylink_link_state *state)
663{
664 struct net_device *ndev = to_net_dev(config->dev);
665 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200666 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100667 u32 old_ctrl, ctrl;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100668 u32 old_ncr, ncr;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100669
frederic RODO6c36a702007-07-12 19:07:24 +0200670 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100671
Antoine Tenart7897b072019-11-13 10:00:06 +0100672 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100673 old_ncr = ncr = macb_or_gem_readl(bp, NCR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100675 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
676 if (state->interface == PHY_INTERFACE_MODE_RMII)
677 ctrl |= MACB_BIT(RM9200_RMII);
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200678 } else if (macb_is_gem(bp)) {
Russell King633e98a2020-02-26 10:24:06 +0000679 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100680 ncr &= ~GEM_BIT(ENABLE_HS_MAC);
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100681
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100682 if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100683 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100684 } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
685 ctrl |= GEM_BIT(PCSSEL);
686 ncr |= GEM_BIT(ENABLE_HS_MAC);
687 }
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100688 }
frederic RODO6c36a702007-07-12 19:07:24 +0200689
Antoine Tenart7897b072019-11-13 10:00:06 +0100690 /* Apply the new configuration, if any */
691 if (old_ctrl ^ ctrl)
692 macb_or_gem_writel(bp, NCFGR, ctrl);
693
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100694 if (old_ncr ^ ncr)
695 macb_or_gem_writel(bp, NCR, ncr);
696
frederic RODO6c36a702007-07-12 19:07:24 +0200697 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200698}
699
Antoine Tenart7897b072019-11-13 10:00:06 +0100700static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
701 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200702{
Antoine Tenart7897b072019-11-13 10:00:06 +0100703 struct net_device *ndev = to_net_dev(config->dev);
704 struct macb *bp = netdev_priv(ndev);
705 struct macb_queue *queue;
706 unsigned int q;
707 u32 ctrl;
708
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100709 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
710 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
711 queue_writel(queue, IDR,
712 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100713
714 /* Disable Rx and Tx */
715 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
716 macb_writel(bp, NCR, ctrl);
717
718 netif_tx_stop_all_queues(ndev);
719}
720
Russell King91a208f2020-02-26 10:23:41 +0000721static void macb_mac_link_up(struct phylink_config *config,
722 struct phy_device *phy,
723 unsigned int mode, phy_interface_t interface,
724 int speed, int duplex,
725 bool tx_pause, bool rx_pause)
Antoine Tenart7897b072019-11-13 10:00:06 +0100726{
727 struct net_device *ndev = to_net_dev(config->dev);
728 struct macb *bp = netdev_priv(ndev);
729 struct macb_queue *queue;
Russell King633e98a2020-02-26 10:24:06 +0000730 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100731 unsigned int q;
Russell King633e98a2020-02-26 10:24:06 +0000732 u32 ctrl;
733
734 spin_lock_irqsave(&bp->lock, flags);
735
736 ctrl = macb_or_gem_readl(bp, NCFGR);
737
738 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
739
740 if (speed == SPEED_100)
741 ctrl |= MACB_BIT(SPD);
742
743 if (duplex)
744 ctrl |= MACB_BIT(FD);
Antoine Tenart7897b072019-11-13 10:00:06 +0100745
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100746 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200747 ctrl &= ~MACB_BIT(PAE);
748 if (macb_is_gem(bp)) {
749 ctrl &= ~GEM_BIT(GBE);
Russell King633e98a2020-02-26 10:24:06 +0000750
Stefan Roesef7ba7db2020-08-04 14:17:16 +0200751 if (speed == SPEED_1000)
752 ctrl |= GEM_BIT(GBE);
753 }
Russell King633e98a2020-02-26 10:24:06 +0000754
Parshuram Thombared7739b02020-09-05 10:21:33 +0200755 if (rx_pause)
Russell King633e98a2020-02-26 10:24:06 +0000756 ctrl |= MACB_BIT(PAE);
757
Claudiu Bezneadaafa1d2020-12-09 15:03:33 +0200758 macb_set_tx_clk(bp, speed);
Antoine Tenart7897b072019-11-13 10:00:06 +0100759
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100760 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
761 * cleared the pipeline and control registers.
762 */
763 bp->macbgem_ops.mog_init_rings(bp);
764 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100765
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100766 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
767 queue_writel(queue, IER,
768 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
769 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100770
Russell King633e98a2020-02-26 10:24:06 +0000771 macb_or_gem_writel(bp, NCFGR, ctrl);
772
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100773 if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
774 gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
775 gem_readl(bp, HS_MAC_CONFIG)));
776
Russell King633e98a2020-02-26 10:24:06 +0000777 spin_unlock_irqrestore(&bp->lock, flags);
778
Antoine Tenart7897b072019-11-13 10:00:06 +0100779 /* Enable Rx and Tx */
780 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
781
782 netif_tx_wake_all_queues(ndev);
783}
784
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100785static int macb_mac_prepare(struct phylink_config *config, unsigned int mode,
786 phy_interface_t interface)
787{
788 struct net_device *ndev = to_net_dev(config->dev);
789 struct macb *bp = netdev_priv(ndev);
790
791 if (interface == PHY_INTERFACE_MODE_10GBASER)
792 bp->phylink_pcs.ops = &macb_phylink_usx_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100793 else if (interface == PHY_INTERFACE_MODE_SGMII)
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100794 bp->phylink_pcs.ops = &macb_phylink_pcs_ops;
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100795 else
796 bp->phylink_pcs.ops = NULL;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100797
Parshuram Thombare0012eeb2020-11-05 18:58:33 +0100798 if (bp->phylink_pcs.ops)
799 phylink_set_pcs(bp->phylink, &bp->phylink_pcs);
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100800
801 return 0;
802}
803
Antoine Tenart7897b072019-11-13 10:00:06 +0100804static const struct phylink_mac_ops macb_phylink_ops = {
805 .validate = macb_validate,
Parshuram Thombaree4e143e2020-10-29 13:47:07 +0100806 .mac_prepare = macb_mac_prepare,
Antoine Tenart7897b072019-11-13 10:00:06 +0100807 .mac_config = macb_mac_config,
808 .mac_link_down = macb_mac_link_down,
809 .mac_link_up = macb_mac_link_up,
810};
811
Milind Parabfd2a8912020-01-13 03:30:43 +0000812static bool macb_phy_handle_exists(struct device_node *dn)
813{
814 dn = of_parse_phandle(dn, "phy-handle", 0);
815 of_node_put(dn);
816 return dn != NULL;
817}
818
Antoine Tenart7897b072019-11-13 10:00:06 +0100819static int macb_phylink_connect(struct macb *bp)
820{
Milind Parabfd2a8912020-01-13 03:30:43 +0000821 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100822 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000823 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100824 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500825
Milind Parabfd2a8912020-01-13 03:30:43 +0000826 if (dn)
827 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
828
829 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200830 phydev = phy_find_first(bp->mii_bus);
831 if (!phydev) {
832 netdev_err(dev, "no PHY found\n");
833 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000834 }
frederic RODO6c36a702007-07-12 19:07:24 +0200835
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200836 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100837 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000838 }
839
840 if (ret) {
841 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
842 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200843 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100844
Antoine Tenart7897b072019-11-13 10:00:06 +0100845 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846
Antoine Tenart7897b072019-11-13 10:00:06 +0100847 return 0;
848}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100849
Antoine Tenart7897b072019-11-13 10:00:06 +0100850/* based on au1000_eth. c*/
851static int macb_mii_probe(struct net_device *dev)
852{
853 struct macb *bp = netdev_priv(dev);
854
855 bp->phylink_config.dev = &dev->dev;
856 bp->phylink_config.type = PHYLINK_NETDEV;
857
858 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
859 bp->phy_interface, &macb_phylink_ops);
860 if (IS_ERR(bp->phylink)) {
861 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
862 PTR_ERR(bp->phylink));
863 return PTR_ERR(bp->phylink);
864 }
frederic RODO6c36a702007-07-12 19:07:24 +0200865
866 return 0;
867}
868
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100869static int macb_mdiobus_register(struct macb *bp)
870{
871 struct device_node *child, *np = bp->pdev->dev.of_node;
872
Codrin Ciubotariu79540d12020-03-31 12:39:35 +0300873 if (of_phy_is_fixed_link(np))
874 return mdiobus_register(bp->mii_bus);
875
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100876 /* Only create the PHY from the device tree if at least one PHY is
877 * described. Otherwise scan the entire MDIO bus. We do this to support
878 * old device tree that did not follow the best practices and did not
879 * describe their network PHYs.
880 */
881 for_each_available_child_of_node(np, child)
882 if (of_mdiobus_child_is_phy(child)) {
883 /* The loop increments the child refcount,
884 * decrement it before returning.
885 */
886 of_node_put(child);
887
888 return of_mdiobus_register(bp->mii_bus, np);
889 }
890
891 return mdiobus_register(bp->mii_bus);
892}
893
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100894static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200895{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200896 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200897
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200898 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200899 macb_writel(bp, NCR, MACB_BIT(MPE));
900
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700901 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700902 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200903 err = -ENOMEM;
904 goto err_out;
905 }
906
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700907 bp->mii_bus->name = "MACB_mii_bus";
908 bp->mii_bus->read = &macb_mdio_read;
909 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000910 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700911 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700912 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700913 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700914
Jamie Iles91523942011-02-28 04:05:25 +0000915 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200916
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100917 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200918 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100919 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200920
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200921 err = macb_mii_probe(bp->dev);
922 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200923 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200924
925 return 0;
926
927err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700928 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500929err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700930 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200931err_out:
932 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100933}
934
935static void macb_update_stats(struct macb *bp)
936{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000937 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
938 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300939 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100940
941 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
942
Moritz Fischer96ec6312016-03-29 19:11:11 -0700943 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700944 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100945}
946
Nicolas Ferree86cd532012-10-31 06:04:57 +0000947static int macb_halt_tx(struct macb *bp)
948{
949 unsigned long halt_time, timeout;
950 u32 status;
951
952 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
953
954 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
955 do {
956 halt_time = jiffies;
957 status = macb_readl(bp, TSR);
958 if (!(status & MACB_BIT(TGO)))
959 return 0;
960
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800961 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000962 } while (time_before(halt_time, timeout));
963
964 return -ETIMEDOUT;
965}
966
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200967static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
968{
969 if (tx_skb->mapping) {
970 if (tx_skb->mapped_as_page)
971 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
972 tx_skb->size, DMA_TO_DEVICE);
973 else
974 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
975 tx_skb->size, DMA_TO_DEVICE);
976 tx_skb->mapping = 0;
977 }
978
979 if (tx_skb->skb) {
980 dev_kfree_skb_any(tx_skb->skb);
981 tx_skb->skb = NULL;
982 }
983}
984
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000985static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530986{
Harini Katakamfff80192016-08-09 13:15:53 +0530987#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000988 struct macb_dma_desc_64 *desc_64;
989
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100990 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000991 desc_64 = macb_64b_desc(bp, desc);
992 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200993 /* The low bits of RX address contain the RX_USED bit, clearing
994 * of which allows packet RX. Make sure the high bits are also
995 * visible to HW at that point.
996 */
997 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000998 }
Harini Katakamfff80192016-08-09 13:15:53 +0530999#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001000 desc->addr = lower_32_bits(addr);
1001}
1002
1003static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
1004{
1005 dma_addr_t addr = 0;
1006#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1007 struct macb_dma_desc_64 *desc_64;
1008
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001009 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001010 desc_64 = macb_64b_desc(bp, desc);
1011 addr = ((u64)(desc_64->addrh) << 32);
1012 }
1013#endif
1014 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1015 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +05301016}
1017
Nicolas Ferree86cd532012-10-31 06:04:57 +00001018static void macb_tx_error_task(struct work_struct *work)
1019{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001020 struct macb_queue *queue = container_of(work, struct macb_queue,
1021 tx_error_task);
1022 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001023 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001024 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001025 struct sk_buff *skb;
1026 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001027 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001028
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001029 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
1030 (unsigned int)(queue - bp->queues),
1031 queue->tx_tail, queue->tx_head);
1032
1033 /* Prevent the queue IRQ handlers from running: each of them may call
1034 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
1035 * As explained below, we have to halt the transmission before updating
1036 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
1037 * network engine about the macb/gem being halted.
1038 */
1039 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001040
1041 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001042 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001043
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001044 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +00001045 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001046 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +00001047 */
1048 if (macb_halt_tx(bp))
1049 /* Just complain for now, reinitializing TX path can be good */
1050 netdev_err(bp->dev, "BUG: halt tx timed out\n");
1051
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001052 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001053 * Free transmit buffers in upper layer.
1054 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001055 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
1056 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001057
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001058 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001059 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001060 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001061 skb = tx_skb->skb;
1062
1063 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001064 /* skb is set for the last buffer of the frame */
1065 while (!skb) {
1066 macb_tx_unmap(bp, tx_skb);
1067 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001068 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001069 skb = tx_skb->skb;
1070 }
1071
1072 /* ctrl still refers to the first buffer descriptor
1073 * since it's the only one written back by the hardware
1074 */
1075 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
1076 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001077 macb_tx_ring_wrap(bp, tail),
1078 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001079 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001080 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001081 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001082 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001083 }
Nicolas Ferree86cd532012-10-31 06:04:57 +00001084 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001085 /* "Buffers exhausted mid-frame" errors may only happen
1086 * if the driver is buggy, so complain loudly about
1087 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +00001088 */
1089 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
1090 netdev_err(bp->dev,
1091 "BUG: TX buffers exhausted mid-frame\n");
1092
1093 desc->ctrl = ctrl | MACB_BIT(TX_USED);
1094 }
1095
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001096 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001097 }
1098
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001099 /* Set end of TX queue */
1100 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001101 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001102 desc->ctrl = MACB_BIT(TX_USED);
1103
Nicolas Ferree86cd532012-10-31 06:04:57 +00001104 /* Make descriptor updates visible to hardware */
1105 wmb();
1106
1107 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001108 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301109#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001110 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001111 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301112#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +00001113 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001114 queue->tx_head = 0;
1115 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +00001116
1117 /* Housework before enabling TX IRQ */
1118 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001119 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
1120
1121 /* Now we are ready to start transmission again */
1122 netif_tx_start_all_queues(bp->dev);
1123 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1124
1125 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001126}
1127
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001128static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001129{
1130 unsigned int tail;
1131 unsigned int head;
1132 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001133 struct macb *bp = queue->bp;
1134 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001135
1136 status = macb_readl(bp, TSR);
1137 macb_writel(bp, TSR, status);
1138
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001139 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001140 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +00001141
Nicolas Ferree86cd532012-10-31 06:04:57 +00001142 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001143 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001144
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001145 head = queue->tx_head;
1146 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001147 struct macb_tx_skb *tx_skb;
1148 struct sk_buff *skb;
1149 struct macb_dma_desc *desc;
1150 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001151
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001152 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001153
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001154 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001155 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001156
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001157 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001158
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001159 /* TX_USED bit is only set by hardware on the very first buffer
1160 * descriptor of the transmitted frame.
1161 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001162 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163 break;
1164
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001165 /* Process all buffers of the current transmitted frame */
1166 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001167 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001168 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001169
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001170 /* First, update TX stats if needed */
1171 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001172 if (unlikely(skb_shinfo(skb)->tx_flags &
1173 SKBTX_HW_TSTAMP) &&
1174 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001175 /* skb now belongs to timestamp buffer
1176 * and will be removed later
1177 */
1178 tx_skb->skb = NULL;
1179 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001180 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001181 macb_tx_ring_wrap(bp, tail),
1182 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001183 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001184 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001185 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001186 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001187 }
1188
1189 /* Now we can safely release resources */
1190 macb_tx_unmap(bp, tx_skb);
1191
1192 /* skb is set only for the last buffer of the frame.
1193 * WARNING: at this point skb has been freed by
1194 * macb_tx_unmap().
1195 */
1196 if (skb)
1197 break;
1198 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001199 }
1200
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001201 queue->tx_tail = tail;
1202 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1203 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001204 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001205 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001206}
1207
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001208static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001209{
1210 unsigned int entry;
1211 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001212 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001213 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001214 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001215
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001216 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1217 bp->rx_ring_size) > 0) {
1218 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001219
1220 /* Make hw descriptor updates visible to CPU */
1221 rmb();
1222
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001223 queue->rx_prepared_head++;
1224 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001225
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001226 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001227 /* allocate sk_buff for this free entry in ring */
1228 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001229 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001230 netdev_err(bp->dev,
1231 "Unable to allocate sk_buff\n");
1232 break;
1233 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001234
1235 /* now fill corresponding descriptor entry */
1236 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001237 bp->rx_buffer_size,
1238 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001239 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1240 dev_kfree_skb(skb);
1241 break;
1242 }
1243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001244 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001245
Zach Brownb410d132016-10-19 09:56:57 -05001246 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001247 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001248 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001249 /* Setting addr clears RX_USED and allows reception,
1250 * make sure ctrl is cleared first to avoid a race.
1251 */
1252 dma_wmb();
1253 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001254
1255 /* properly align Ethernet header */
1256 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301257 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001258 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001259 dma_wmb();
1260 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001261 }
1262 }
1263
1264 /* Make descriptor updates visible to hardware */
1265 wmb();
1266
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001267 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1268 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001269}
1270
1271/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001272static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001273 unsigned int end)
1274{
1275 unsigned int frag;
1276
1277 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001278 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001279
Nicolas Ferre4df95132013-06-04 21:57:12 +00001280 desc->addr &= ~MACB_BIT(RX_USED);
1281 }
1282
1283 /* Make descriptor updates visible to hardware */
1284 wmb();
1285
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001286 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001287 * whatever caused this is updated, so we don't have to record
1288 * anything.
1289 */
1290}
1291
Antoine Tenart97236cd2019-06-21 17:30:02 +02001292static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1293 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001294{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001295 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001296 unsigned int len;
1297 unsigned int entry;
1298 struct sk_buff *skb;
1299 struct macb_dma_desc *desc;
1300 int count = 0;
1301
1302 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301303 u32 ctrl;
1304 dma_addr_t addr;
1305 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001306
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001307 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1308 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001309
1310 /* Make hw descriptor updates visible to CPU */
1311 rmb();
1312
Harini Katakamfff80192016-08-09 13:15:53 +05301313 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001314 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001315
Harini Katakamfff80192016-08-09 13:15:53 +05301316 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001317 break;
1318
Anssi Hannula6e0af292018-12-17 15:05:41 +02001319 /* Ensure ctrl is at least as up-to-date as rxused */
1320 dma_rmb();
1321
1322 ctrl = desc->ctrl;
1323
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001324 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001325 count++;
1326
1327 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1328 netdev_err(bp->dev,
1329 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001330 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001331 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001332 break;
1333 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001334 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001335 if (unlikely(!skb)) {
1336 netdev_err(bp->dev,
1337 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001338 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001339 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001340 break;
1341 }
1342 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001343 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301344 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001345
1346 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1347
1348 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001349 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001350 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001351
1352 skb->protocol = eth_type_trans(skb, bp->dev);
1353 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001354 if (bp->dev->features & NETIF_F_RXCSUM &&
1355 !(bp->dev->flags & IFF_PROMISC) &&
1356 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1357 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001358
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001359 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001360 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001361 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001362 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001363
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001364 gem_ptp_do_rxstamp(bp, skb, desc);
1365
Nicolas Ferre4df95132013-06-04 21:57:12 +00001366#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1367 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1368 skb->len, skb->csum);
1369 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001370 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001371 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1372 skb->data, 32, true);
1373#endif
1374
Antoine Tenart97236cd2019-06-21 17:30:02 +02001375 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001376 }
1377
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001378 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001379
1380 return count;
1381}
1382
Antoine Tenart97236cd2019-06-21 17:30:02 +02001383static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1384 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001385{
1386 unsigned int len;
1387 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001388 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001389 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001390 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001391 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001392
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001393 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301394 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001395
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001396 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001397 macb_rx_ring_wrap(bp, first_frag),
1398 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001399
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001400 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001401 * first buffer. Since the header is 14 bytes, this makes the
1402 * payload word-aligned.
1403 *
1404 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1405 * the two padding bytes into the skb so that we avoid hitting
1406 * the slowpath in memcpy(), and pull them off afterwards.
1407 */
1408 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001410 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001411 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001412 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001413 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001414 if (frag == last_frag)
1415 break;
1416 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001417
1418 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001419 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001420
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001421 return 1;
1422 }
1423
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001424 offset = 0;
1425 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001426 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 skb_put(skb, len);
1428
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001429 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001430 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001431
1432 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001433 if (unlikely(frag != last_frag)) {
1434 dev_kfree_skb_any(skb);
1435 return -1;
1436 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001437 frag_len = len - offset;
1438 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001439 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001440 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001441 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001442 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001443 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001444 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001445
1446 if (frag == last_frag)
1447 break;
1448 }
1449
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001450 /* Make descriptor updates visible to hardware */
1451 wmb();
1452
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001453 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001454 skb->protocol = eth_type_trans(skb, bp->dev);
1455
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001456 bp->dev->stats.rx_packets++;
1457 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001458 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001459 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001460 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001461
1462 return 0;
1463}
1464
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001465static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001466{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001467 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001468 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001469 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001470 int i;
1471
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001472 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001473 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001474 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001475 macb_set_addr(bp, desc, addr);
1476 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001477 addr += bp->rx_buffer_size;
1478 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001479 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001480 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001481}
1482
Antoine Tenart97236cd2019-06-21 17:30:02 +02001483static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1484 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001485{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001486 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001487 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001488 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001489 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001490 int first_frag = -1;
1491
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001492 for (tail = queue->rx_tail; budget > 0; tail++) {
1493 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001494 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001495
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001496 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001497 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001498
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001499 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500 break;
1501
Anssi Hannula6e0af292018-12-17 15:05:41 +02001502 /* Ensure ctrl is at least as up-to-date as addr */
1503 dma_rmb();
1504
1505 ctrl = desc->ctrl;
1506
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001507 if (ctrl & MACB_BIT(RX_SOF)) {
1508 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001509 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001510 first_frag = tail;
1511 }
1512
1513 if (ctrl & MACB_BIT(RX_EOF)) {
1514 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001515
1516 if (unlikely(first_frag == -1)) {
1517 reset_rx_queue = true;
1518 continue;
1519 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001520
Antoine Tenart97236cd2019-06-21 17:30:02 +02001521 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001522 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001523 if (unlikely(dropped < 0)) {
1524 reset_rx_queue = true;
1525 continue;
1526 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001527 if (!dropped) {
1528 received++;
1529 budget--;
1530 }
1531 }
1532 }
1533
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001534 if (unlikely(reset_rx_queue)) {
1535 unsigned long flags;
1536 u32 ctrl;
1537
1538 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1539
1540 spin_lock_irqsave(&bp->lock, flags);
1541
1542 ctrl = macb_readl(bp, NCR);
1543 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1544
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001545 macb_init_rx_ring(queue);
1546 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001547
1548 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1549
1550 spin_unlock_irqrestore(&bp->lock, flags);
1551 return received;
1552 }
1553
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001554 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001555 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001556 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001557 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001558
1559 return received;
1560}
1561
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001562static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001563{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001564 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1565 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001566 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001567 u32 status;
1568
1569 status = macb_readl(bp, RSR);
1570 macb_writel(bp, RSR, status);
1571
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001572 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001573 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001574
Antoine Tenart97236cd2019-06-21 17:30:02 +02001575 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001576 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001577 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001578
Nicolas Ferre8770e912013-02-12 11:08:48 +01001579 /* Packets received while interrupts were disabled */
1580 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001581 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001582 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001583 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001584 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001585 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301586 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001587 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001588 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001589
1590 /* TODO: Handle errors */
1591
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001592 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001593}
1594
Allen Paise7412b82020-09-14 12:59:23 +05301595static void macb_hresp_error_task(struct tasklet_struct *t)
Harini Katakam032dc412018-01-27 12:09:01 +05301596{
Allen Paise7412b82020-09-14 12:59:23 +05301597 struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
Harini Katakam032dc412018-01-27 12:09:01 +05301598 struct net_device *dev = bp->dev;
Claudiu Beznea580d3952020-07-02 12:06:00 +03001599 struct macb_queue *queue;
Harini Katakam032dc412018-01-27 12:09:01 +05301600 unsigned int q;
1601 u32 ctrl;
1602
1603 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301604 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301605 MACB_TX_INT_FLAGS |
1606 MACB_BIT(HRESP));
1607 }
1608 ctrl = macb_readl(bp, NCR);
1609 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1610 macb_writel(bp, NCR, ctrl);
1611
1612 netif_tx_stop_all_queues(dev);
1613 netif_carrier_off(dev);
1614
1615 bp->macbgem_ops.mog_init_rings(bp);
1616
1617 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001618 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301619
Antoine Tenart6e952d92019-11-13 10:00:05 +01001620 /* Enable interrupts */
1621 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301622 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301623 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301624 MACB_TX_INT_FLAGS |
1625 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301626
1627 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1628 macb_writel(bp, NCR, ctrl);
1629
1630 netif_carrier_on(dev);
1631 netif_tx_start_all_queues(dev);
1632}
1633
Claudiu Beznea42983882018-12-17 10:02:42 +00001634static void macb_tx_restart(struct macb_queue *queue)
1635{
1636 unsigned int head = queue->tx_head;
1637 unsigned int tail = queue->tx_tail;
1638 struct macb *bp = queue->bp;
1639
1640 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1641 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1642
1643 if (head == tail)
1644 return;
1645
1646 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1647}
1648
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02001649static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
1650{
1651 struct macb_queue *queue = dev_id;
1652 struct macb *bp = queue->bp;
1653 u32 status;
1654
1655 status = queue_readl(queue, ISR);
1656
1657 if (unlikely(!status))
1658 return IRQ_NONE;
1659
1660 spin_lock(&bp->lock);
1661
1662 if (status & MACB_BIT(WOL)) {
1663 queue_writel(queue, IDR, MACB_BIT(WOL));
1664 macb_writel(bp, WOL, 0);
1665 netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
1666 (unsigned int)(queue - bp->queues),
1667 (unsigned long)status);
1668 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1669 queue_writel(queue, ISR, MACB_BIT(WOL));
1670 pm_wakeup_event(&bp->pdev->dev, 0);
1671 }
1672
1673 spin_unlock(&bp->lock);
1674
1675 return IRQ_HANDLED;
1676}
1677
Nicolas Ferre558e35c2020-07-20 10:56:52 +02001678static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
1679{
1680 struct macb_queue *queue = dev_id;
1681 struct macb *bp = queue->bp;
1682 u32 status;
1683
1684 status = queue_readl(queue, ISR);
1685
1686 if (unlikely(!status))
1687 return IRQ_NONE;
1688
1689 spin_lock(&bp->lock);
1690
1691 if (status & GEM_BIT(WOL)) {
1692 queue_writel(queue, IDR, GEM_BIT(WOL));
1693 gem_writel(bp, WOL, 0);
1694 netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
1695 (unsigned int)(queue - bp->queues),
1696 (unsigned long)status);
1697 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1698 queue_writel(queue, ISR, GEM_BIT(WOL));
1699 pm_wakeup_event(&bp->pdev->dev, 0);
1700 }
1701
1702 spin_unlock(&bp->lock);
1703
1704 return IRQ_HANDLED;
1705}
1706
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001707static irqreturn_t macb_interrupt(int irq, void *dev_id)
1708{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001709 struct macb_queue *queue = dev_id;
1710 struct macb *bp = queue->bp;
1711 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001712 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001713
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001714 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001715
1716 if (unlikely(!status))
1717 return IRQ_NONE;
1718
1719 spin_lock(&bp->lock);
1720
1721 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001722 /* close possible race with dev_close */
1723 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001724 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001725 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1726 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001727 break;
1728 }
1729
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001730 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1731 (unsigned int)(queue - bp->queues),
1732 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001733
Harini Katakame5010702019-01-29 15:20:03 +05301734 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001735 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001736 * until we have processed the buffers. The
1737 * scheduling call may fail if the poll routine
1738 * is already scheduled, so disable interrupts
1739 * now.
1740 */
Harini Katakame5010702019-01-29 15:20:03 +05301741 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001742 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001743 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001744
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001745 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001746 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001747 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001748 }
1749 }
1750
Nicolas Ferree86cd532012-10-31 06:04:57 +00001751 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001752 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1753 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001754
1755 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001756 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001757
Nicolas Ferree86cd532012-10-31 06:04:57 +00001758 break;
1759 }
1760
1761 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001762 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001763
Claudiu Beznea42983882018-12-17 10:02:42 +00001764 if (status & MACB_BIT(TXUBR))
1765 macb_tx_restart(queue);
1766
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001767 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001768 * add that if/when we get our hands on a full-blown MII PHY.
1769 */
1770
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001771 /* There is a hardware issue under heavy load where DMA can
1772 * stop, this causes endless "used buffer descriptor read"
1773 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301774 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1775 * section 16.7.4 for details. RXUBR is only enabled for
1776 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001777 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001778 if (status & MACB_BIT(RXUBR)) {
1779 ctrl = macb_readl(bp, NCR);
1780 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001781 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001782 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1783
1784 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001785 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001786 }
1787
Alexander Steinb19f7f72011-04-13 05:03:24 +00001788 if (status & MACB_BIT(ISR_ROVR)) {
1789 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001790 if (macb_is_gem(bp))
1791 bp->hw_stats.gem.rx_overruns++;
1792 else
1793 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001794
1795 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001796 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001797 }
1798
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001799 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301800 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001801 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001802
1803 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001804 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001805 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001806 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001807 }
1808
1809 spin_unlock(&bp->lock);
1810
1811 return IRQ_HANDLED;
1812}
1813
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001814#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001815/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001816 * to allow network i/o with interrupts disabled.
1817 */
1818static void macb_poll_controller(struct net_device *dev)
1819{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001820 struct macb *bp = netdev_priv(dev);
1821 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001822 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001823 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001824
1825 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001826 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1827 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001828 local_irq_restore(flags);
1829}
1830#endif
1831
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001832static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001833 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001834 struct sk_buff *skb,
1835 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001836{
1837 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001838 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001839 struct macb_tx_skb *tx_skb = NULL;
1840 struct macb_dma_desc *desc;
1841 unsigned int offset, size, count = 0;
1842 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001843 unsigned int eof = 1, mss_mfs = 0;
1844 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1845
1846 /* LSO */
1847 if (skb_shinfo(skb)->gso_size != 0) {
1848 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1849 /* UDP - UFO */
1850 lso_ctrl = MACB_LSO_UFO_ENABLE;
1851 else
1852 /* TCP - TSO */
1853 lso_ctrl = MACB_LSO_TSO_ENABLE;
1854 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001855
1856 /* First, map non-paged data */
1857 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001858
1859 /* first buffer length */
1860 size = hdrlen;
1861
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001862 offset = 0;
1863 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001864 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001865 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001866
1867 mapping = dma_map_single(&bp->pdev->dev,
1868 skb->data + offset,
1869 size, DMA_TO_DEVICE);
1870 if (dma_mapping_error(&bp->pdev->dev, mapping))
1871 goto dma_error;
1872
1873 /* Save info to properly release resources */
1874 tx_skb->skb = NULL;
1875 tx_skb->mapping = mapping;
1876 tx_skb->size = size;
1877 tx_skb->mapped_as_page = false;
1878
1879 len -= size;
1880 offset += size;
1881 count++;
1882 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001883
1884 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001885 }
1886
1887 /* Then, map paged data from fragments */
1888 for (f = 0; f < nr_frags; f++) {
1889 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1890
1891 len = skb_frag_size(frag);
1892 offset = 0;
1893 while (len) {
1894 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001895 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001896 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001897
1898 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1899 offset, size, DMA_TO_DEVICE);
1900 if (dma_mapping_error(&bp->pdev->dev, mapping))
1901 goto dma_error;
1902
1903 /* Save info to properly release resources */
1904 tx_skb->skb = NULL;
1905 tx_skb->mapping = mapping;
1906 tx_skb->size = size;
1907 tx_skb->mapped_as_page = true;
1908
1909 len -= size;
1910 offset += size;
1911 count++;
1912 tx_head++;
1913 }
1914 }
1915
1916 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001917 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001918 netdev_err(bp->dev, "BUG! empty skb!\n");
1919 return 0;
1920 }
1921
1922 /* This is the last buffer of the frame: save socket buffer */
1923 tx_skb->skb = skb;
1924
1925 /* Update TX ring: update buffer descriptors in reverse order
1926 * to avoid race condition
1927 */
1928
1929 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1930 * to set the end of TX queue
1931 */
1932 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001933 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001934 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001935 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001936 desc->ctrl = ctrl;
1937
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001938 if (lso_ctrl) {
1939 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1940 /* include header and FCS in value given to h/w */
1941 mss_mfs = skb_shinfo(skb)->gso_size +
1942 skb_transport_offset(skb) +
1943 ETH_FCS_LEN;
1944 else /* TSO */ {
1945 mss_mfs = skb_shinfo(skb)->gso_size;
1946 /* TCP Sequence Number Source Select
1947 * can be set only for TSO
1948 */
1949 seq_ctrl = 0;
1950 }
1951 }
1952
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001953 do {
1954 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001955 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001956 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001957 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001958
1959 ctrl = (u32)tx_skb->size;
1960 if (eof) {
1961 ctrl |= MACB_BIT(TX_LAST);
1962 eof = 0;
1963 }
Zach Brownb410d132016-10-19 09:56:57 -05001964 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001965 ctrl |= MACB_BIT(TX_WRAP);
1966
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001967 /* First descriptor is header descriptor */
1968 if (i == queue->tx_head) {
1969 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1970 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001971 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1972 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1973 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001974 } else
1975 /* Only set MSS/MFS on payload descriptors
1976 * (second or later descriptor)
1977 */
1978 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1979
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001980 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001981 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001982 /* desc->addr must be visible to hardware before clearing
1983 * 'TX_USED' bit in desc->ctrl.
1984 */
1985 wmb();
1986 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001987 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001988
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001989 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001990
1991 return count;
1992
1993dma_error:
1994 netdev_err(bp->dev, "TX DMA map failed\n");
1995
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001996 for (i = queue->tx_head; i != tx_head; i++) {
1997 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001998
1999 macb_tx_unmap(bp, tx_skb);
2000 }
2001
2002 return 0;
2003}
2004
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002005static netdev_features_t macb_features_check(struct sk_buff *skb,
2006 struct net_device *dev,
2007 netdev_features_t features)
2008{
2009 unsigned int nr_frags, f;
2010 unsigned int hdrlen;
2011
2012 /* Validate LSO compatibility */
2013
Harini Katakam41c1ef92020-02-05 18:08:11 +05302014 /* there is only one buffer or protocol is not UDP */
2015 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002016 return features;
2017
2018 /* length of header */
2019 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002020
Harini Katakam41c1ef92020-02-05 18:08:11 +05302021 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002022 * When software supplies two or more payload buffers all payload buffers
2023 * apart from the last must be a multiple of 8 bytes in size.
2024 */
2025 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
2026 return features & ~MACB_NETIF_LSO;
2027
2028 nr_frags = skb_shinfo(skb)->nr_frags;
2029 /* No need to check last fragment */
2030 nr_frags--;
2031 for (f = 0; f < nr_frags; f++) {
2032 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2033
2034 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
2035 return features & ~MACB_NETIF_LSO;
2036 }
2037 return features;
2038}
2039
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02002040static inline int macb_clear_csum(struct sk_buff *skb)
2041{
2042 /* no change for packets without checksum offloading */
2043 if (skb->ip_summed != CHECKSUM_PARTIAL)
2044 return 0;
2045
2046 /* make sure we can modify the header */
2047 if (unlikely(skb_cow_head(skb, 0)))
2048 return -1;
2049
2050 /* initialize checksum field
2051 * This is required - at least for Zynq, which otherwise calculates
2052 * wrong UDP header checksums for UDP packets with UDP data len <=2
2053 */
2054 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
2055 return 0;
2056}
2057
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002058static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
2059{
Mark Deneen403dc162020-10-30 15:58:14 +00002060 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
2061 skb_is_nonlinear(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002062 int padlen = ETH_ZLEN - (*skb)->len;
2063 int headroom = skb_headroom(*skb);
2064 int tailroom = skb_tailroom(*skb);
2065 struct sk_buff *nskb;
2066 u32 fcs;
2067
2068 if (!(ndev->features & NETIF_F_HW_CSUM) ||
2069 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
2070 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
2071 return 0;
2072
2073 if (padlen <= 0) {
2074 /* FCS could be appeded to tailroom. */
2075 if (tailroom >= ETH_FCS_LEN)
2076 goto add_fcs;
2077 /* FCS could be appeded by moving data to headroom. */
2078 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
2079 padlen = 0;
2080 /* No room for FCS, need to reallocate skb. */
2081 else
Tristram Ha899ecae2018-10-24 14:51:23 -07002082 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002083 } else {
2084 /* Add room for FCS. */
2085 padlen += ETH_FCS_LEN;
2086 }
2087
2088 if (!cloned && headroom + tailroom >= padlen) {
2089 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
2090 skb_set_tail_pointer(*skb, (*skb)->len);
2091 } else {
2092 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
2093 if (!nskb)
2094 return -ENOMEM;
2095
Huang Zijiangf3e5c072019-02-14 14:41:18 +08002096 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002097 *skb = nskb;
2098 }
2099
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00002100 if (padlen > ETH_FCS_LEN)
2101 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002102
2103add_fcs:
2104 /* set FCS to packet */
2105 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
2106 fcs = ~fcs;
2107
2108 skb_put_u8(*skb, fcs & 0xff);
2109 skb_put_u8(*skb, (fcs >> 8) & 0xff);
2110 skb_put_u8(*skb, (fcs >> 16) & 0xff);
2111 skb_put_u8(*skb, (fcs >> 24) & 0xff);
2112
2113 return 0;
2114}
2115
Claudiu Beznead1c38952018-08-07 12:25:12 +03002116static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002117{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002118 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002119 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002120 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07002121 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002122 unsigned int desc_cnt, nr_frags, frag_size, f;
2123 unsigned int hdrlen;
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002124 bool is_lso;
Claudiu Beznead1c38952018-08-07 12:25:12 +03002125 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002126
Claudiu Beznea33729f22018-08-07 12:25:13 +03002127 if (macb_clear_csum(skb)) {
2128 dev_kfree_skb_any(skb);
2129 return ret;
2130 }
2131
Claudiu Beznea653e92a2018-08-07 12:25:14 +03002132 if (macb_pad_and_fcs(&skb, dev)) {
2133 dev_kfree_skb_any(skb);
2134 return ret;
2135 }
2136
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002137 is_lso = (skb_shinfo(skb)->gso_size != 0);
2138
2139 if (is_lso) {
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002140 /* length of headers */
Claudiu Beznea8932b5a2020-07-02 12:06:01 +03002141 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002142 /* only queue eth + ip headers separately for UDP */
2143 hdrlen = skb_transport_offset(skb);
2144 else
2145 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
2146 if (skb_headlen(skb) < hdrlen) {
2147 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
2148 /* if this is required, would need to copy to single buffer */
2149 return NETDEV_TX_BUSY;
2150 }
2151 } else
2152 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002153
Havard Skinnemoena268adb2012-10-31 06:04:52 +00002154#if defined(DEBUG) && defined(VERBOSE_DEBUG)
2155 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002156 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
2157 queue_index, skb->len, skb->head, skb->data,
2158 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002159 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
2160 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002161#endif
2162
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002163 /* Count how many TX buffer descriptors are needed to send this
2164 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07002165 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002166 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002167 if (is_lso && (skb_headlen(skb) > hdrlen))
2168 /* extra header descriptor if also payload in first buffer */
2169 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
2170 else
2171 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002172 nr_frags = skb_shinfo(skb)->nr_frags;
2173 for (f = 0; f < nr_frags; f++) {
2174 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002175 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002176 }
2177
Dongdong Deng48719532009-08-23 19:49:07 -07002178 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002179
2180 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05002181 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002182 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002183 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07002184 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002185 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002186 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00002187 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002188 }
2189
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002190 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002191 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07002192 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08002193 goto unlock;
2194 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00002195
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00002196 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002197 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00002198 skb_tx_timestamp(skb);
2199
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002200 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2201
Zach Brownb410d132016-10-19 09:56:57 -05002202 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002203 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002204
Soren Brinkmann92030902014-03-04 08:46:39 -08002205unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002206 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002207
Claudiu Beznead1c38952018-08-07 12:25:12 +03002208 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002209}
2210
Nicolas Ferre4df95132013-06-04 21:57:12 +00002211static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002212{
2213 if (!macb_is_gem(bp)) {
2214 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2215 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002216 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002217
Nicolas Ferre1b447912013-06-04 21:57:11 +00002218 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002219 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002220 "RX buffer must be multiple of %d bytes, expanding\n",
2221 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002222 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002223 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002224 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002225 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002226
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002227 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002228 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002229}
2230
Nicolas Ferre4df95132013-06-04 21:57:12 +00002231static void gem_free_rx_buffers(struct macb *bp)
2232{
2233 struct sk_buff *skb;
2234 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002235 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002236 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002237 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002238 int i;
2239
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002240 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2241 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002242 continue;
2243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002244 for (i = 0; i < bp->rx_ring_size; i++) {
2245 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002246
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002247 if (!skb)
2248 continue;
2249
2250 desc = macb_rx_desc(queue, i);
2251 addr = macb_get_addr(bp, desc);
2252
2253 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2254 DMA_FROM_DEVICE);
2255 dev_kfree_skb_any(skb);
2256 skb = NULL;
2257 }
2258
2259 kfree(queue->rx_skbuff);
2260 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002261 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002262}
2263
2264static void macb_free_rx_buffers(struct macb *bp)
2265{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002266 struct macb_queue *queue = &bp->queues[0];
2267
2268 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002269 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002270 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002271 queue->rx_buffers, queue->rx_buffers_dma);
2272 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002273 }
2274}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002275
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002276static void macb_free_consistent(struct macb *bp)
2277{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002278 struct macb_queue *queue;
2279 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302280 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002281
Nicolas Ferre4df95132013-06-04 21:57:12 +00002282 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002283
2284 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2285 kfree(queue->tx_skb);
2286 queue->tx_skb = NULL;
2287 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302288 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2289 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002290 queue->tx_ring, queue->tx_ring_dma);
2291 queue->tx_ring = NULL;
2292 }
Harini Katakame50b7702018-07-06 12:18:57 +05302293 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302294 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2295 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302296 queue->rx_ring, queue->rx_ring_dma);
2297 queue->rx_ring = NULL;
2298 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002299 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002300}
2301
2302static int gem_alloc_rx_buffers(struct macb *bp)
2303{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002304 struct macb_queue *queue;
2305 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002306 int size;
2307
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002308 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2309 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2310 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2311 if (!queue->rx_skbuff)
2312 return -ENOMEM;
2313 else
2314 netdev_dbg(bp->dev,
2315 "Allocated %d RX struct sk_buff entries at %p\n",
2316 bp->rx_ring_size, queue->rx_skbuff);
2317 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002318 return 0;
2319}
2320
2321static int macb_alloc_rx_buffers(struct macb *bp)
2322{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002323 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002324 int size;
2325
Zach Brownb410d132016-10-19 09:56:57 -05002326 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002327 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2328 &queue->rx_buffers_dma, GFP_KERNEL);
2329 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002330 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002331
2332 netdev_dbg(bp->dev,
2333 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002334 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002335 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002336}
2337
2338static int macb_alloc_consistent(struct macb *bp)
2339{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002340 struct macb_queue *queue;
2341 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002342 int size;
2343
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002344 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302345 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002346 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2347 &queue->tx_ring_dma,
2348 GFP_KERNEL);
2349 if (!queue->tx_ring)
2350 goto out_err;
2351 netdev_dbg(bp->dev,
2352 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2353 q, size, (unsigned long)queue->tx_ring_dma,
2354 queue->tx_ring);
2355
Zach Brownb410d132016-10-19 09:56:57 -05002356 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002357 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2358 if (!queue->tx_skb)
2359 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002360
Harini Katakam404cd082018-07-06 12:18:58 +05302361 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002362 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2363 &queue->rx_ring_dma, GFP_KERNEL);
2364 if (!queue->rx_ring)
2365 goto out_err;
2366 netdev_dbg(bp->dev,
2367 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2368 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002369 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002370 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002371 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002372
2373 return 0;
2374
2375out_err:
2376 macb_free_consistent(bp);
2377 return -ENOMEM;
2378}
2379
Nicolas Ferre4df95132013-06-04 21:57:12 +00002380static void gem_init_rings(struct macb *bp)
2381{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002382 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002383 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002384 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002385 int i;
2386
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002387 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002388 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002389 desc = macb_tx_desc(queue, i);
2390 macb_set_addr(bp, desc, 0);
2391 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002392 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002393 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002394 queue->tx_head = 0;
2395 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002396
2397 queue->rx_tail = 0;
2398 queue->rx_prepared_head = 0;
2399
2400 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002401 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002402
Nicolas Ferre4df95132013-06-04 21:57:12 +00002403}
2404
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405static void macb_init_rings(struct macb *bp)
2406{
2407 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002408 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002409
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002410 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002411
Zach Brownb410d132016-10-19 09:56:57 -05002412 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002413 desc = macb_tx_desc(&bp->queues[0], i);
2414 macb_set_addr(bp, desc, 0);
2415 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002416 }
Ben Shelton21d35152015-04-22 17:28:54 -05002417 bp->queues[0].tx_head = 0;
2418 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002419 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002420}
2421
2422static void macb_reset_hw(struct macb *bp)
2423{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002424 struct macb_queue *queue;
2425 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002426 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002427
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002428 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002429 * more gracefully?)
2430 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002431 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002432
2433 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002434 ctrl |= MACB_BIT(CLRSTAT);
2435
2436 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002437
2438 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002439 macb_writel(bp, TSR, -1);
2440 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002441
2442 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002443 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2444 queue_writel(queue, IDR, -1);
2445 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002446 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2447 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002448 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002449}
2450
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002451static u32 gem_mdc_clk_div(struct macb *bp)
2452{
2453 u32 config;
2454 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2455
2456 if (pclk_hz <= 20000000)
2457 config = GEM_BF(CLK, GEM_CLK_DIV8);
2458 else if (pclk_hz <= 40000000)
2459 config = GEM_BF(CLK, GEM_CLK_DIV16);
2460 else if (pclk_hz <= 80000000)
2461 config = GEM_BF(CLK, GEM_CLK_DIV32);
2462 else if (pclk_hz <= 120000000)
2463 config = GEM_BF(CLK, GEM_CLK_DIV48);
2464 else if (pclk_hz <= 160000000)
2465 config = GEM_BF(CLK, GEM_CLK_DIV64);
2466 else
2467 config = GEM_BF(CLK, GEM_CLK_DIV96);
2468
2469 return config;
2470}
2471
2472static u32 macb_mdc_clk_div(struct macb *bp)
2473{
2474 u32 config;
2475 unsigned long pclk_hz;
2476
2477 if (macb_is_gem(bp))
2478 return gem_mdc_clk_div(bp);
2479
2480 pclk_hz = clk_get_rate(bp->pclk);
2481 if (pclk_hz <= 20000000)
2482 config = MACB_BF(CLK, MACB_CLK_DIV8);
2483 else if (pclk_hz <= 40000000)
2484 config = MACB_BF(CLK, MACB_CLK_DIV16);
2485 else if (pclk_hz <= 80000000)
2486 config = MACB_BF(CLK, MACB_CLK_DIV32);
2487 else
2488 config = MACB_BF(CLK, MACB_CLK_DIV64);
2489
2490 return config;
2491}
2492
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002493/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002494 * should program. We find the width from decoding the design configuration
2495 * register to find the maximum supported data bus width.
2496 */
2497static u32 macb_dbw(struct macb *bp)
2498{
2499 if (!macb_is_gem(bp))
2500 return 0;
2501
2502 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2503 case 4:
2504 return GEM_BF(DBW, GEM_DBW128);
2505 case 2:
2506 return GEM_BF(DBW, GEM_DBW64);
2507 case 1:
2508 default:
2509 return GEM_BF(DBW, GEM_DBW32);
2510 }
2511}
2512
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002513/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002514 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002515 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002516 * (if not supported by FIFO, it will fallback to default)
2517 * - set both rx/tx packet buffers to full memory size
2518 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002519 */
2520static void macb_configure_dma(struct macb *bp)
2521{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002522 struct macb_queue *queue;
2523 u32 buffer_size;
2524 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002525 u32 dmacfg;
2526
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002527 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002528 if (macb_is_gem(bp)) {
2529 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002530 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2531 if (q)
2532 queue_writel(queue, RBQS, buffer_size);
2533 else
2534 dmacfg |= GEM_BF(RXBS, buffer_size);
2535 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002536 if (bp->dma_burst_length)
2537 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002538 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302539 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302540
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002541 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302542 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2543 else
2544 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2545
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002546 if (bp->dev->features & NETIF_F_HW_CSUM)
2547 dmacfg |= GEM_BIT(TXCOEN);
2548 else
2549 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302550
Michal Simekbd620722018-09-25 08:32:50 +02002551 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302552#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002553 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002554 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302555#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002556#ifdef CONFIG_MACB_USE_HWSTAMP
2557 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2558 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2559#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002560 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2561 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002562 gem_writel(bp, DMACFG, dmacfg);
2563 }
2564}
2565
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002566static void macb_init_hw(struct macb *bp)
2567{
2568 u32 config;
2569
2570 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002571 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002572
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002573 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002574 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002575 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002576 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302577 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2578 else
2579 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002580 if (bp->dev->flags & IFF_PROMISC)
2581 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002582 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2583 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002584 if (!(bp->dev->flags & IFF_BROADCAST))
2585 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002586 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002587 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002588 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302589 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302590 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002591 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302592 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002593
Jamie Iles0116da42011-03-14 17:38:30 +00002594 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002595}
2596
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002597/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002598 * locations in the memory map. The least significant bits are stored
2599 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2600 *
2601 * The unicast hash enable and the multicast hash enable bits in the
2602 * network configuration register enable the reception of hash matched
2603 * frames. The destination address is reduced to a 6 bit index into
2604 * the 64 bit hash register using the following hash function. The
2605 * hash function is an exclusive or of every sixth bit of the
2606 * destination address.
2607 *
2608 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2609 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2610 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2611 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2612 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2613 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2614 *
2615 * da[0] represents the least significant bit of the first byte
2616 * received, that is, the multicast/unicast indicator, and da[47]
2617 * represents the most significant bit of the last byte received. If
2618 * the hash index, hi[n], points to a bit that is set in the hash
2619 * register then the frame will be matched according to whether the
2620 * frame is multicast or unicast. A multicast match will be signalled
2621 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2622 * index points to a bit set in the hash register. A unicast match
2623 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2624 * and the hash index points to a bit set in the hash register. To
2625 * receive all multicast frames, the hash register should be set with
2626 * all ones and the multicast hash enable bit should be set in the
2627 * network configuration register.
2628 */
2629
2630static inline int hash_bit_value(int bitnr, __u8 *addr)
2631{
2632 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2633 return 1;
2634 return 0;
2635}
2636
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002637/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002638static int hash_get_index(__u8 *addr)
2639{
2640 int i, j, bitval;
2641 int hash_index = 0;
2642
2643 for (j = 0; j < 6; j++) {
2644 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002645 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002646
2647 hash_index |= (bitval << j);
2648 }
2649
2650 return hash_index;
2651}
2652
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002653/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002654static void macb_sethashtable(struct net_device *dev)
2655{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002656 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002657 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002658 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002659 struct macb *bp = netdev_priv(dev);
2660
Moritz Fischeraa50b552016-03-29 19:11:13 -07002661 mc_filter[0] = 0;
2662 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002663
Jiri Pirko22bedad32010-04-01 21:22:57 +00002664 netdev_for_each_mc_addr(ha, dev) {
2665 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002666 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2667 }
2668
Jamie Ilesf75ba502011-11-08 10:12:32 +00002669 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2670 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002671}
2672
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002673/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002674static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002675{
2676 unsigned long cfg;
2677 struct macb *bp = netdev_priv(dev);
2678
2679 cfg = macb_readl(bp, NCFGR);
2680
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002681 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002682 /* Enable promiscuous mode */
2683 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002684
2685 /* Disable RX checksum offload */
2686 if (macb_is_gem(bp))
2687 cfg &= ~GEM_BIT(RXCOEN);
2688 } else {
2689 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002690 cfg &= ~MACB_BIT(CAF);
2691
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002692 /* Enable RX checksum offload only if requested */
2693 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2694 cfg |= GEM_BIT(RXCOEN);
2695 }
2696
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002697 if (dev->flags & IFF_ALLMULTI) {
2698 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002699 macb_or_gem_writel(bp, HRB, -1);
2700 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002701 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002702 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002703 /* Enable specific multicasts */
2704 macb_sethashtable(dev);
2705 cfg |= MACB_BIT(NCFGR_MTI);
2706 } else if (dev->flags & (~IFF_ALLMULTI)) {
2707 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002708 macb_or_gem_writel(bp, HRB, 0);
2709 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002710 cfg &= ~MACB_BIT(NCFGR_MTI);
2711 }
2712
2713 macb_writel(bp, NCFGR, cfg);
2714}
2715
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002716static int macb_open(struct net_device *dev)
2717{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002718 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002719 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002720 struct macb_queue *queue;
2721 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002722 int err;
2723
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002724 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002725
Harini Katakamd54f89a2019-03-01 16:20:34 +05302726 err = pm_runtime_get_sync(&bp->pdev->dev);
2727 if (err < 0)
2728 goto pm_exit;
2729
Nicolas Ferre1b447912013-06-04 21:57:11 +00002730 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002731 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002732
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002733 err = macb_alloc_consistent(bp);
2734 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002735 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2736 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302737 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002738 }
2739
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002740 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2741 napi_enable(&queue->napi);
2742
Harini Katakam05044532019-05-07 19:59:10 +05302743 macb_init_hw(bp);
2744
Antoine Tenart7897b072019-11-13 10:00:06 +01002745 err = macb_phylink_connect(bp);
2746 if (err)
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002747 goto reset_hw;
frederic RODO6c36a702007-07-12 19:07:24 +02002748
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002749 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002750
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002751 if (bp->ptp_info)
2752 bp->ptp_info->ptp_init(dev);
2753
Charles Keepax939a5bf72020-06-15 14:18:54 +01002754 return 0;
2755
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002756reset_hw:
2757 macb_reset_hw(bp);
Corentin Labbe014406b2020-06-10 09:53:44 +00002758 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2759 napi_disable(&queue->napi);
Claudiu Bezneafaa620872020-06-18 11:37:40 +03002760 macb_free_consistent(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302761pm_exit:
Charles Keepax939a5bf72020-06-15 14:18:54 +01002762 pm_runtime_put_sync(&bp->pdev->dev);
2763 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002764}
2765
2766static int macb_close(struct net_device *dev)
2767{
2768 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002769 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002770 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002771 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002772
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002773 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002774
2775 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2776 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002777
Antoine Tenart7897b072019-11-13 10:00:06 +01002778 phylink_stop(bp->phylink);
2779 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002780
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002781 spin_lock_irqsave(&bp->lock, flags);
2782 macb_reset_hw(bp);
2783 netif_carrier_off(dev);
2784 spin_unlock_irqrestore(&bp->lock, flags);
2785
2786 macb_free_consistent(bp);
2787
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002788 if (bp->ptp_info)
2789 bp->ptp_info->ptp_remove(dev);
2790
Harini Katakamd54f89a2019-03-01 16:20:34 +05302791 pm_runtime_put(&bp->pdev->dev);
2792
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002793 return 0;
2794}
2795
Harini Katakama5898ea2015-05-06 22:27:18 +05302796static int macb_change_mtu(struct net_device *dev, int new_mtu)
2797{
Harini Katakama5898ea2015-05-06 22:27:18 +05302798 if (netif_running(dev))
2799 return -EBUSY;
2800
Harini Katakama5898ea2015-05-06 22:27:18 +05302801 dev->mtu = new_mtu;
2802
2803 return 0;
2804}
2805
Jamie Ilesa494ed82011-03-09 16:26:35 +00002806static void gem_update_stats(struct macb *bp)
2807{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002808 struct macb_queue *queue;
2809 unsigned int i, q, idx;
2810 unsigned long *stat;
2811
Jamie Ilesa494ed82011-03-09 16:26:35 +00002812 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002813
Xander Huff3ff13f12015-01-13 16:15:51 -06002814 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2815 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002816 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002817
2818 bp->ethtool_stats[i] += val;
2819 *p += val;
2820
2821 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2822 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002823 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002824 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002825 *(++p) += val;
2826 }
2827 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002828
2829 idx = GEM_STATS_LEN;
2830 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2831 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2832 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002833}
2834
2835static struct net_device_stats *gem_get_stats(struct macb *bp)
2836{
2837 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002838 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002839
2840 gem_update_stats(bp);
2841
2842 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2843 hwstat->rx_alignment_errors +
2844 hwstat->rx_resource_errors +
2845 hwstat->rx_overruns +
2846 hwstat->rx_oversize_frames +
2847 hwstat->rx_jabbers +
2848 hwstat->rx_undersized_frames +
2849 hwstat->rx_length_field_frame_errors);
2850 nstat->tx_errors = (hwstat->tx_late_collisions +
2851 hwstat->tx_excessive_collisions +
2852 hwstat->tx_underrun +
2853 hwstat->tx_carrier_sense_errors);
2854 nstat->multicast = hwstat->rx_multicast_frames;
2855 nstat->collisions = (hwstat->tx_single_collision_frames +
2856 hwstat->tx_multiple_collision_frames +
2857 hwstat->tx_excessive_collisions);
2858 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2859 hwstat->rx_jabbers +
2860 hwstat->rx_undersized_frames +
2861 hwstat->rx_length_field_frame_errors);
2862 nstat->rx_over_errors = hwstat->rx_resource_errors;
2863 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2864 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2865 nstat->rx_fifo_errors = hwstat->rx_overruns;
2866 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2867 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2868 nstat->tx_fifo_errors = hwstat->tx_underrun;
2869
2870 return nstat;
2871}
2872
Xander Huff3ff13f12015-01-13 16:15:51 -06002873static void gem_get_ethtool_stats(struct net_device *dev,
2874 struct ethtool_stats *stats, u64 *data)
2875{
2876 struct macb *bp;
2877
2878 bp = netdev_priv(dev);
2879 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002880 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2881 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002882}
2883
2884static int gem_get_sset_count(struct net_device *dev, int sset)
2885{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002886 struct macb *bp = netdev_priv(dev);
2887
Xander Huff3ff13f12015-01-13 16:15:51 -06002888 switch (sset) {
2889 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002890 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002891 default:
2892 return -EOPNOTSUPP;
2893 }
2894}
2895
2896static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2897{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002898 char stat_string[ETH_GSTRING_LEN];
2899 struct macb *bp = netdev_priv(dev);
2900 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002901 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002902 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002903
2904 switch (sset) {
2905 case ETH_SS_STATS:
2906 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2907 memcpy(p, gem_statistics[i].stat_string,
2908 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002909
2910 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2911 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2912 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2913 q, queue_statistics[i].stat_string);
2914 memcpy(p, stat_string, ETH_GSTRING_LEN);
2915 }
2916 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002917 break;
2918 }
2919}
2920
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002921static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002922{
2923 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002924 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002925 struct macb_stats *hwstat = &bp->hw_stats.macb;
2926
2927 if (macb_is_gem(bp))
2928 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002929
frederic RODO6c36a702007-07-12 19:07:24 +02002930 /* read stats from hardware */
2931 macb_update_stats(bp);
2932
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002933 /* Convert HW stats into netdevice stats */
2934 nstat->rx_errors = (hwstat->rx_fcs_errors +
2935 hwstat->rx_align_errors +
2936 hwstat->rx_resource_errors +
2937 hwstat->rx_overruns +
2938 hwstat->rx_oversize_pkts +
2939 hwstat->rx_jabbers +
2940 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002941 hwstat->rx_length_mismatch);
2942 nstat->tx_errors = (hwstat->tx_late_cols +
2943 hwstat->tx_excessive_cols +
2944 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002945 hwstat->tx_carrier_errors +
2946 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002947 nstat->collisions = (hwstat->tx_single_cols +
2948 hwstat->tx_multiple_cols +
2949 hwstat->tx_excessive_cols);
2950 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2951 hwstat->rx_jabbers +
2952 hwstat->rx_undersize_pkts +
2953 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002954 nstat->rx_over_errors = hwstat->rx_resource_errors +
2955 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002956 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2957 nstat->rx_frame_errors = hwstat->rx_align_errors;
2958 nstat->rx_fifo_errors = hwstat->rx_overruns;
2959 /* XXX: What does "missed" mean? */
2960 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2961 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2962 nstat->tx_fifo_errors = hwstat->tx_underruns;
2963 /* Don't know about heartbeat or window errors... */
2964
2965 return nstat;
2966}
2967
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002968static int macb_get_regs_len(struct net_device *netdev)
2969{
2970 return MACB_GREGS_NBR * sizeof(u32);
2971}
2972
2973static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2974 void *p)
2975{
2976 struct macb *bp = netdev_priv(dev);
2977 unsigned int tail, head;
2978 u32 *regs_buff = p;
2979
2980 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2981 | MACB_GREGS_VERSION;
2982
Zach Brownb410d132016-10-19 09:56:57 -05002983 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2984 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002985
2986 regs_buff[0] = macb_readl(bp, NCR);
2987 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2988 regs_buff[2] = macb_readl(bp, NSR);
2989 regs_buff[3] = macb_readl(bp, TSR);
2990 regs_buff[4] = macb_readl(bp, RBQP);
2991 regs_buff[5] = macb_readl(bp, TBQP);
2992 regs_buff[6] = macb_readl(bp, RSR);
2993 regs_buff[7] = macb_readl(bp, IMR);
2994
2995 regs_buff[8] = tail;
2996 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002997 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2998 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002999
Neil Armstrongce721a72016-01-05 14:39:16 +01003000 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
3001 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003002 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003003 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003004}
3005
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003006static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3007{
3008 struct macb *bp = netdev_priv(netdev);
3009
Nicolas Ferre253fe092020-07-10 14:46:43 +02003010 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
Antoine Tenart7897b072019-11-13 10:00:06 +01003011 phylink_ethtool_get_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003012 wol->supported |= WAKE_MAGIC;
3013
3014 if (bp->wol & MACB_WOL_ENABLED)
3015 wol->wolopts |= WAKE_MAGIC;
3016 }
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003017}
3018
3019static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3020{
3021 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01003022 int ret;
3023
Nicolas Ferre253fe092020-07-10 14:46:43 +02003024 /* Pass the order to phylink layer */
Antoine Tenart7897b072019-11-13 10:00:06 +01003025 ret = phylink_ethtool_set_wol(bp->phylink, wol);
Nicolas Ferre253fe092020-07-10 14:46:43 +02003026 /* Don't manage WoL on MAC if handled by the PHY
3027 * or if there's a failure in talking to the PHY
3028 */
3029 if (!ret || ret != -EOPNOTSUPP)
3030 return ret;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003031
3032 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
3033 (wol->wolopts & ~WAKE_MAGIC))
3034 return -EOPNOTSUPP;
3035
3036 if (wol->wolopts & WAKE_MAGIC)
3037 bp->wol |= MACB_WOL_ENABLED;
3038 else
3039 bp->wol &= ~MACB_WOL_ENABLED;
3040
3041 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
3042
3043 return 0;
3044}
3045
Antoine Tenart7897b072019-11-13 10:00:06 +01003046static int macb_get_link_ksettings(struct net_device *netdev,
3047 struct ethtool_link_ksettings *kset)
3048{
3049 struct macb *bp = netdev_priv(netdev);
3050
3051 return phylink_ethtool_ksettings_get(bp->phylink, kset);
3052}
3053
3054static int macb_set_link_ksettings(struct net_device *netdev,
3055 const struct ethtool_link_ksettings *kset)
3056{
3057 struct macb *bp = netdev_priv(netdev);
3058
3059 return phylink_ethtool_ksettings_set(bp->phylink, kset);
3060}
3061
Zach Brown8441bb32016-10-19 09:56:58 -05003062static void macb_get_ringparam(struct net_device *netdev,
3063 struct ethtool_ringparam *ring)
3064{
3065 struct macb *bp = netdev_priv(netdev);
3066
3067 ring->rx_max_pending = MAX_RX_RING_SIZE;
3068 ring->tx_max_pending = MAX_TX_RING_SIZE;
3069
3070 ring->rx_pending = bp->rx_ring_size;
3071 ring->tx_pending = bp->tx_ring_size;
3072}
3073
3074static int macb_set_ringparam(struct net_device *netdev,
3075 struct ethtool_ringparam *ring)
3076{
3077 struct macb *bp = netdev_priv(netdev);
3078 u32 new_rx_size, new_tx_size;
3079 unsigned int reset = 0;
3080
3081 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
3082 return -EINVAL;
3083
3084 new_rx_size = clamp_t(u32, ring->rx_pending,
3085 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
3086 new_rx_size = roundup_pow_of_two(new_rx_size);
3087
3088 new_tx_size = clamp_t(u32, ring->tx_pending,
3089 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
3090 new_tx_size = roundup_pow_of_two(new_tx_size);
3091
3092 if ((new_tx_size == bp->tx_ring_size) &&
3093 (new_rx_size == bp->rx_ring_size)) {
3094 /* nothing to do */
3095 return 0;
3096 }
3097
3098 if (netif_running(bp->dev)) {
3099 reset = 1;
3100 macb_close(bp->dev);
3101 }
3102
3103 bp->rx_ring_size = new_rx_size;
3104 bp->tx_ring_size = new_tx_size;
3105
3106 if (reset)
3107 macb_open(bp->dev);
3108
3109 return 0;
3110}
3111
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003112#ifdef CONFIG_MACB_USE_HWSTAMP
3113static unsigned int gem_get_tsu_rate(struct macb *bp)
3114{
3115 struct clk *tsu_clk;
3116 unsigned int tsu_rate;
3117
3118 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
3119 if (!IS_ERR(tsu_clk))
3120 tsu_rate = clk_get_rate(tsu_clk);
3121 /* try pclk instead */
3122 else if (!IS_ERR(bp->pclk)) {
3123 tsu_clk = bp->pclk;
3124 tsu_rate = clk_get_rate(tsu_clk);
3125 } else
3126 return -ENOTSUPP;
3127 return tsu_rate;
3128}
3129
3130static s32 gem_get_ptp_max_adj(void)
3131{
3132 return 64000000;
3133}
3134
3135static int gem_get_ts_info(struct net_device *dev,
3136 struct ethtool_ts_info *info)
3137{
3138 struct macb *bp = netdev_priv(dev);
3139
3140 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
3141 ethtool_op_get_ts_info(dev, info);
3142 return 0;
3143 }
3144
3145 info->so_timestamping =
3146 SOF_TIMESTAMPING_TX_SOFTWARE |
3147 SOF_TIMESTAMPING_RX_SOFTWARE |
3148 SOF_TIMESTAMPING_SOFTWARE |
3149 SOF_TIMESTAMPING_TX_HARDWARE |
3150 SOF_TIMESTAMPING_RX_HARDWARE |
3151 SOF_TIMESTAMPING_RAW_HARDWARE;
3152 info->tx_types =
3153 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
3154 (1 << HWTSTAMP_TX_OFF) |
3155 (1 << HWTSTAMP_TX_ON);
3156 info->rx_filters =
3157 (1 << HWTSTAMP_FILTER_NONE) |
3158 (1 << HWTSTAMP_FILTER_ALL);
3159
3160 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
3161
3162 return 0;
3163}
3164
3165static struct macb_ptp_info gem_ptp_info = {
3166 .ptp_init = gem_ptp_init,
3167 .ptp_remove = gem_ptp_remove,
3168 .get_ptp_max_adj = gem_get_ptp_max_adj,
3169 .get_tsu_rate = gem_get_tsu_rate,
3170 .get_ts_info = gem_get_ts_info,
3171 .get_hwtst = gem_get_hwtst,
3172 .set_hwtst = gem_set_hwtst,
3173};
3174#endif
3175
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003176static int macb_get_ts_info(struct net_device *netdev,
3177 struct ethtool_ts_info *info)
3178{
3179 struct macb *bp = netdev_priv(netdev);
3180
3181 if (bp->ptp_info)
3182 return bp->ptp_info->get_ts_info(netdev, info);
3183
3184 return ethtool_op_get_ts_info(netdev, info);
3185}
3186
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003187static void gem_enable_flow_filters(struct macb *bp, bool enable)
3188{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003189 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003190 struct ethtool_rx_fs_item *item;
3191 u32 t2_scr;
3192 int num_t2_scr;
3193
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003194 if (!(netdev->features & NETIF_F_NTUPLE))
3195 return;
3196
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003197 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3198
3199 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3200 struct ethtool_rx_flow_spec *fs = &item->fs;
3201 struct ethtool_tcpip4_spec *tp4sp_m;
3202
3203 if (fs->location >= num_t2_scr)
3204 continue;
3205
3206 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3207
3208 /* enable/disable screener regs for the flow entry */
3209 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3210
3211 /* only enable fields with no masking */
3212 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3213
3214 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3215 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3216 else
3217 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3218
3219 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3220 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3221 else
3222 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3223
3224 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3225 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3226 else
3227 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3228
3229 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3230 }
3231}
3232
3233static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3234{
3235 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3236 uint16_t index = fs->location;
3237 u32 w0, w1, t2_scr;
3238 bool cmp_a = false;
3239 bool cmp_b = false;
3240 bool cmp_c = false;
3241
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003242 if (!macb_is_gem(bp))
3243 return;
3244
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003245 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3246 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3247
3248 /* ignore field if any masking set */
3249 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3250 /* 1st compare reg - IP source address */
3251 w0 = 0;
3252 w1 = 0;
3253 w0 = tp4sp_v->ip4src;
3254 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3255 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3256 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3257 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3258 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3259 cmp_a = true;
3260 }
3261
3262 /* ignore field if any masking set */
3263 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3264 /* 2nd compare reg - IP destination address */
3265 w0 = 0;
3266 w1 = 0;
3267 w0 = tp4sp_v->ip4dst;
3268 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3269 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3270 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3271 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3272 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3273 cmp_b = true;
3274 }
3275
3276 /* ignore both port fields if masking set in both */
3277 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3278 /* 3rd compare reg - source port, destination port */
3279 w0 = 0;
3280 w1 = 0;
3281 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3282 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3283 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3284 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3285 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3286 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3287 } else {
3288 /* only one port definition */
3289 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3290 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3291 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3292 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3293 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3294 } else { /* dst port */
3295 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3296 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3297 }
3298 }
3299 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3300 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3301 cmp_c = true;
3302 }
3303
3304 t2_scr = 0;
3305 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3306 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3307 if (cmp_a)
3308 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3309 if (cmp_b)
3310 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3311 if (cmp_c)
3312 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3313 gem_writel_n(bp, SCRT2, index, t2_scr);
3314}
3315
3316static int gem_add_flow_filter(struct net_device *netdev,
3317 struct ethtool_rxnfc *cmd)
3318{
3319 struct macb *bp = netdev_priv(netdev);
3320 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3321 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003322 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003323 int ret = -EINVAL;
3324 bool added = false;
3325
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003326 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003327 if (newfs == NULL)
3328 return -ENOMEM;
3329 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3330
3331 netdev_dbg(netdev,
3332 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3333 fs->flow_type, (int)fs->ring_cookie, fs->location,
3334 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3335 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3336 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3337
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003338 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3339
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003340 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003341 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3342 if (item->fs.location > newfs->fs.location) {
3343 list_add_tail(&newfs->list, &item->list);
3344 added = true;
3345 break;
3346 } else if (item->fs.location == fs->location) {
3347 netdev_err(netdev, "Rule not added: location %d not free!\n",
3348 fs->location);
3349 ret = -EBUSY;
3350 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003351 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003352 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003353 if (!added)
3354 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003355
3356 gem_prog_cmp_regs(bp, fs);
3357 bp->rx_fs_list.count++;
3358 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003359 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003360
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003361 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003362 return 0;
3363
3364err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003365 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003366 kfree(newfs);
3367 return ret;
3368}
3369
3370static int gem_del_flow_filter(struct net_device *netdev,
3371 struct ethtool_rxnfc *cmd)
3372{
3373 struct macb *bp = netdev_priv(netdev);
3374 struct ethtool_rx_fs_item *item;
3375 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003376 unsigned long flags;
3377
3378 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003379
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003380 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3381 if (item->fs.location == cmd->fs.location) {
3382 /* disable screener regs for the flow entry */
3383 fs = &(item->fs);
3384 netdev_dbg(netdev,
3385 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3386 fs->flow_type, (int)fs->ring_cookie, fs->location,
3387 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3388 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3389 htons(fs->h_u.tcp_ip4_spec.psrc),
3390 htons(fs->h_u.tcp_ip4_spec.pdst));
3391
3392 gem_writel_n(bp, SCRT2, fs->location, 0);
3393
3394 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003395 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003396 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3397 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003398 return 0;
3399 }
3400 }
3401
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003402 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003403 return -EINVAL;
3404}
3405
3406static int gem_get_flow_entry(struct net_device *netdev,
3407 struct ethtool_rxnfc *cmd)
3408{
3409 struct macb *bp = netdev_priv(netdev);
3410 struct ethtool_rx_fs_item *item;
3411
3412 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3413 if (item->fs.location == cmd->fs.location) {
3414 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3415 return 0;
3416 }
3417 }
3418 return -EINVAL;
3419}
3420
3421static int gem_get_all_flow_entries(struct net_device *netdev,
3422 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3423{
3424 struct macb *bp = netdev_priv(netdev);
3425 struct ethtool_rx_fs_item *item;
3426 uint32_t cnt = 0;
3427
3428 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3429 if (cnt == cmd->rule_cnt)
3430 return -EMSGSIZE;
3431 rule_locs[cnt] = item->fs.location;
3432 cnt++;
3433 }
3434 cmd->data = bp->max_tuples;
3435 cmd->rule_cnt = cnt;
3436
3437 return 0;
3438}
3439
3440static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3441 u32 *rule_locs)
3442{
3443 struct macb *bp = netdev_priv(netdev);
3444 int ret = 0;
3445
3446 switch (cmd->cmd) {
3447 case ETHTOOL_GRXRINGS:
3448 cmd->data = bp->num_queues;
3449 break;
3450 case ETHTOOL_GRXCLSRLCNT:
3451 cmd->rule_cnt = bp->rx_fs_list.count;
3452 break;
3453 case ETHTOOL_GRXCLSRULE:
3454 ret = gem_get_flow_entry(netdev, cmd);
3455 break;
3456 case ETHTOOL_GRXCLSRLALL:
3457 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3458 break;
3459 default:
3460 netdev_err(netdev,
3461 "Command parameter %d is not supported\n", cmd->cmd);
3462 ret = -EOPNOTSUPP;
3463 }
3464
3465 return ret;
3466}
3467
3468static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3469{
3470 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003471 int ret;
3472
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003473 switch (cmd->cmd) {
3474 case ETHTOOL_SRXCLSRLINS:
3475 if ((cmd->fs.location >= bp->max_tuples)
3476 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3477 ret = -EINVAL;
3478 break;
3479 }
3480 ret = gem_add_flow_filter(netdev, cmd);
3481 break;
3482 case ETHTOOL_SRXCLSRLDEL:
3483 ret = gem_del_flow_filter(netdev, cmd);
3484 break;
3485 default:
3486 netdev_err(netdev,
3487 "Command parameter %d is not supported\n", cmd->cmd);
3488 ret = -EOPNOTSUPP;
3489 }
3490
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003491 return ret;
3492}
3493
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003494static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003495 .get_regs_len = macb_get_regs_len,
3496 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003497 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003498 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003499 .get_wol = macb_get_wol,
3500 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003501 .get_link_ksettings = macb_get_link_ksettings,
3502 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003503 .get_ringparam = macb_get_ringparam,
3504 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003505};
Xander Huff8cd5a562015-01-15 15:55:20 -06003506
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003507static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003508 .get_regs_len = macb_get_regs_len,
3509 .get_regs = macb_get_regs,
Nicolas Ferre558e35c2020-07-20 10:56:52 +02003510 .get_wol = macb_get_wol,
3511 .set_wol = macb_set_wol,
Xander Huff8cd5a562015-01-15 15:55:20 -06003512 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003513 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003514 .get_ethtool_stats = gem_get_ethtool_stats,
3515 .get_strings = gem_get_ethtool_strings,
3516 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003517 .get_link_ksettings = macb_get_link_ksettings,
3518 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003519 .get_ringparam = macb_get_ringparam,
3520 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003521 .get_rxnfc = gem_get_rxnfc,
3522 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003523};
3524
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003525static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003526{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003527 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003528
3529 if (!netif_running(dev))
3530 return -EINVAL;
3531
Antoine Tenart7897b072019-11-13 10:00:06 +01003532 if (bp->ptp_info) {
3533 switch (cmd) {
3534 case SIOCSHWTSTAMP:
3535 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3536 case SIOCGHWTSTAMP:
3537 return bp->ptp_info->get_hwtst(dev, rq);
3538 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003539 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003540
3541 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003542}
3543
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003544static inline void macb_set_txcsum_feature(struct macb *bp,
3545 netdev_features_t features)
3546{
3547 u32 val;
3548
3549 if (!macb_is_gem(bp))
3550 return;
3551
3552 val = gem_readl(bp, DMACFG);
3553 if (features & NETIF_F_HW_CSUM)
3554 val |= GEM_BIT(TXCOEN);
3555 else
3556 val &= ~GEM_BIT(TXCOEN);
3557
3558 gem_writel(bp, DMACFG, val);
3559}
3560
3561static inline void macb_set_rxcsum_feature(struct macb *bp,
3562 netdev_features_t features)
3563{
3564 struct net_device *netdev = bp->dev;
3565 u32 val;
3566
3567 if (!macb_is_gem(bp))
3568 return;
3569
3570 val = gem_readl(bp, NCFGR);
3571 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3572 val |= GEM_BIT(RXCOEN);
3573 else
3574 val &= ~GEM_BIT(RXCOEN);
3575
3576 gem_writel(bp, NCFGR, val);
3577}
3578
3579static inline void macb_set_rxflow_feature(struct macb *bp,
3580 netdev_features_t features)
3581{
3582 if (!macb_is_gem(bp))
3583 return;
3584
3585 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3586}
3587
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003588static int macb_set_features(struct net_device *netdev,
3589 netdev_features_t features)
3590{
3591 struct macb *bp = netdev_priv(netdev);
3592 netdev_features_t changed = features ^ netdev->features;
3593
3594 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003595 if (changed & NETIF_F_HW_CSUM)
3596 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003597
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003598 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003599 if (changed & NETIF_F_RXCSUM)
3600 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003601
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003602 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003603 if (changed & NETIF_F_NTUPLE)
3604 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003605
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003606 return 0;
3607}
3608
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003609static void macb_restore_features(struct macb *bp)
3610{
3611 struct net_device *netdev = bp->dev;
3612 netdev_features_t features = netdev->features;
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003613 struct ethtool_rx_fs_item *item;
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003614
3615 /* TX checksum offload */
3616 macb_set_txcsum_feature(bp, features);
3617
3618 /* RX checksum offload */
3619 macb_set_rxcsum_feature(bp, features);
3620
3621 /* RX Flow Filters */
Claudiu Bezneaa14d2732021-04-02 15:42:53 +03003622 list_for_each_entry(item, &bp->rx_fs_list.list, list)
3623 gem_prog_cmp_regs(bp, &item->fs);
3624
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003625 macb_set_rxflow_feature(bp, features);
3626}
3627
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003628static const struct net_device_ops macb_netdev_ops = {
3629 .ndo_open = macb_open,
3630 .ndo_stop = macb_close,
3631 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003632 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003633 .ndo_get_stats = macb_get_stats,
3634 .ndo_do_ioctl = macb_ioctl,
3635 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303636 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003637 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003638#ifdef CONFIG_NET_POLL_CONTROLLER
3639 .ndo_poll_controller = macb_poll_controller,
3640#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003641 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003642 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003643};
3644
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003645/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003646 * and integration options used
3647 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003648static void macb_configure_caps(struct macb *bp,
3649 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003650{
3651 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003652
Nicolas Ferref6970502015-03-31 15:02:01 +02003653 if (dt_conf)
3654 bp->caps = dt_conf->caps;
3655
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003656 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003657 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3658
Nicolas Ferree1755872014-07-24 13:50:58 +02003659 dcfg = gem_readl(bp, DCFG1);
3660 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3661 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
Parshuram Thombaree4e143e2020-10-29 13:47:07 +01003662 if (GEM_BFEXT(NO_PCS, dcfg) == 0)
3663 bp->caps |= MACB_CAPS_PCS;
3664 dcfg = gem_readl(bp, DCFG12);
3665 if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
3666 bp->caps |= MACB_CAPS_HIGH_SPEED;
Nicolas Ferree1755872014-07-24 13:50:58 +02003667 dcfg = gem_readl(bp, DCFG2);
3668 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3669 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003670#ifdef CONFIG_MACB_USE_HWSTAMP
3671 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003672 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003673 dev_err(&bp->pdev->dev,
3674 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003675 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003676 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003677 bp->ptp_info = &gem_ptp_info;
3678 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003679 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003680#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003681 }
3682
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003683 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003684}
3685
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003686static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003687 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003688 unsigned int *queue_mask,
3689 unsigned int *num_queues)
3690{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003691 *queue_mask = 0x1;
3692 *num_queues = 1;
3693
Nicolas Ferreda120112015-03-31 15:02:00 +02003694 /* is it macb or gem ?
3695 *
3696 * We need to read directly from the hardware here because
3697 * we are early in the probe process and don't have the
3698 * MACB_CAPS_MACB_IS_GEM flag positioned
3699 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003700 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003701 return;
3702
3703 /* bit 0 is never set but queue 0 always exists */
Claudiu Bezneafec371f2020-07-02 12:05:58 +03003704 *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
Claudiu Bezneab7ab39b2020-07-02 12:05:59 +03003705 *num_queues = hweight32(*queue_mask);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003706}
3707
Claudiu Beznea38493da2020-12-09 15:03:34 +02003708static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
3709 struct clk *rx_clk, struct clk *tsu_clk)
3710{
3711 struct clk_bulk_data clks[] = {
3712 { .clk = tsu_clk, },
3713 { .clk = rx_clk, },
3714 { .clk = pclk, },
3715 { .clk = hclk, },
3716 { .clk = tx_clk },
3717 };
3718
3719 clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
3720}
3721
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003722static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303723 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303724 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003725{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003726 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003727 int err;
3728
Bartosz Folta83a77e92016-12-14 06:39:15 +00003729 pdata = dev_get_platdata(&pdev->dev);
3730 if (pdata) {
3731 *pclk = pdata->pclk;
3732 *hclk = pdata->hclk;
3733 } else {
3734 *pclk = devm_clk_get(&pdev->dev, "pclk");
3735 *hclk = devm_clk_get(&pdev->dev, "hclk");
3736 }
3737
Harini Katakamcd5afa92019-03-20 19:12:22 +05303738 if (IS_ERR_OR_NULL(*pclk)) {
YueHaibing9e6cad52020-11-12 22:49:36 +08003739 err = IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV;
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003740 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003741 return err;
3742 }
3743
Harini Katakamcd5afa92019-03-20 19:12:22 +05303744 if (IS_ERR_OR_NULL(*hclk)) {
YueHaibing9e6cad52020-11-12 22:49:36 +08003745 err = IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV;
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003746 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003747 return err;
3748 }
3749
Michael Tretterbd310aca2019-10-18 16:11:43 +02003750 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003751 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003752 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003753
Michael Tretterbd310aca2019-10-18 16:11:43 +02003754 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303755 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003756 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303757
Michael Tretterbd310aca2019-10-18 16:11:43 +02003758 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303759 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003760 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303761
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003762 err = clk_prepare_enable(*pclk);
3763 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003764 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003765 return err;
3766 }
3767
3768 err = clk_prepare_enable(*hclk);
3769 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003770 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003771 goto err_disable_pclk;
3772 }
3773
3774 err = clk_prepare_enable(*tx_clk);
3775 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003776 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003777 goto err_disable_hclk;
3778 }
3779
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303780 err = clk_prepare_enable(*rx_clk);
3781 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003782 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303783 goto err_disable_txclk;
3784 }
3785
Harini Katakamf5473d12019-03-01 16:20:33 +05303786 err = clk_prepare_enable(*tsu_clk);
3787 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003788 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303789 goto err_disable_rxclk;
3790 }
3791
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003792 return 0;
3793
Harini Katakamf5473d12019-03-01 16:20:33 +05303794err_disable_rxclk:
3795 clk_disable_unprepare(*rx_clk);
3796
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303797err_disable_txclk:
3798 clk_disable_unprepare(*tx_clk);
3799
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003800err_disable_hclk:
3801 clk_disable_unprepare(*hclk);
3802
3803err_disable_pclk:
3804 clk_disable_unprepare(*pclk);
3805
3806 return err;
3807}
3808
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003809static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003810{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003811 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003812 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003813 struct macb *bp = netdev_priv(dev);
3814 struct macb_queue *queue;
3815 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003816 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003817
Zach Brownb410d132016-10-19 09:56:57 -05003818 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3819 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3820
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003821 /* set the queue register mapping once for all: queue0 has a special
3822 * register mapping but we don't want to test the queue index then
3823 * compute the corresponding register offset at run time.
3824 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003825 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003826 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003827 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003828
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003829 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003830 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003831 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003832 if (hw_q) {
3833 queue->ISR = GEM_ISR(hw_q - 1);
3834 queue->IER = GEM_IER(hw_q - 1);
3835 queue->IDR = GEM_IDR(hw_q - 1);
3836 queue->IMR = GEM_IMR(hw_q - 1);
3837 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003838 queue->RBQP = GEM_RBQP(hw_q - 1);
3839 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303840#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003841 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003842 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003843 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3844 }
Harini Katakamfff80192016-08-09 13:15:53 +05303845#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003846 } else {
3847 /* queue0 uses legacy registers */
3848 queue->ISR = MACB_ISR;
3849 queue->IER = MACB_IER;
3850 queue->IDR = MACB_IDR;
3851 queue->IMR = MACB_IMR;
3852 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003853 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303854#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003855 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003856 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003857 queue->RBQPH = MACB_RBQPH;
3858 }
Harini Katakamfff80192016-08-09 13:15:53 +05303859#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003860 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003861
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003862 /* get irq: here we use the linux queue index, not the hardware
3863 * queue index. the queue irq definitions in the device tree
3864 * must remove the optional gaps that could exist in the
3865 * hardware queue mask.
3866 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003867 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003868 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003869 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003870 if (err) {
3871 dev_err(&pdev->dev,
3872 "Unable to request IRQ %d (error %d)\n",
3873 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003874 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003875 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003876
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003877 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003878 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003879 }
3880
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003881 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003882
Nicolas Ferre4df95132013-06-04 21:57:12 +00003883 /* setup appropriated routines according to adapter type */
3884 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003885 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003886 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3887 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3888 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3889 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003890 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003891 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003892 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003893 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3894 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3895 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3896 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003897 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003898 }
3899
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003900 /* Set features */
3901 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003902
3903 /* Check LSO capability */
3904 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3905 dev->hw_features |= MACB_NETIF_LSO;
3906
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003907 /* Checksum offload is only available on gem with packet buffer */
3908 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003909 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003910 if (bp->caps & MACB_CAPS_SG_DISABLED)
3911 dev->hw_features &= ~NETIF_F_SG;
3912 dev->features = dev->hw_features;
3913
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003914 /* Check RX Flow Filters support.
3915 * Max Rx flows set by availability of screeners & compare regs:
3916 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3917 */
3918 reg = gem_readl(bp, DCFG8);
3919 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3920 GEM_BFEXT(T2SCR, reg));
Claudiu Bezneaa714e272021-04-14 14:20:29 +03003921 INIT_LIST_HEAD(&bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003922 if (bp->max_tuples > 0) {
3923 /* also needs one ethtype match to check IPv4 */
3924 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3925 /* program this reg now */
3926 reg = 0;
3927 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3928 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3929 /* Filtering is supported in hw but don't enable it in kernel now */
3930 dev->hw_features |= NETIF_F_NTUPLE;
3931 /* init Rx flow definitions */
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003932 bp->rx_fs_list.count = 0;
3933 spin_lock_init(&bp->rx_fs_lock);
3934 } else
3935 bp->max_tuples = 0;
3936 }
3937
Neil Armstrongce721a72016-01-05 14:39:16 +01003938 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3939 val = 0;
Alexandre Belloni2ccb0162020-07-18 01:32:21 +02003940 if (phy_interface_mode_is_rgmii(bp->phy_interface))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003941 val = bp->usrio->rgmii;
Neil Armstrongce721a72016-01-05 14:39:16 +01003942 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003943 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003944 val = bp->usrio->rmii;
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003945 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003946 val = bp->usrio->mii;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003947
Neil Armstrongce721a72016-01-05 14:39:16 +01003948 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
Claudiu Bezneaedac6382020-12-09 15:03:32 +02003949 val |= bp->usrio->refclk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003950
Neil Armstrongce721a72016-01-05 14:39:16 +01003951 macb_or_gem_writel(bp, USRIO, val);
3952 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003953
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003954 /* Set MII management clock divider */
3955 val = macb_mdc_clk_div(bp);
3956 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303957 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3958 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003959 macb_writel(bp, NCFGR, val);
3960
3961 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003962}
3963
Atish Patrab1242232021-03-03 11:55:49 -08003964static const struct macb_usrio_config macb_default_usrio = {
3965 .mii = MACB_BIT(MII),
3966 .rmii = MACB_BIT(RMII),
3967 .rgmii = GEM_BIT(RGMII),
3968 .refclk = MACB_BIT(CLKEN),
3969};
3970
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003971#if defined(CONFIG_OF)
3972/* 1518 rounded up */
3973#define AT91ETHER_MAX_RBUFF_SZ 0x600
3974/* max number of receive buffers */
3975#define AT91ETHER_MAX_RX_DESCR 9
3976
Arnd Bergmann49db9222019-07-08 14:48:23 +02003977static struct sifive_fu540_macb_mgmt *mgmt;
3978
Claudiu Beznea33fdef22020-06-24 13:08:18 +03003979static int at91ether_alloc_coherent(struct macb *lp)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003980{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003981 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003982
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003983 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003984 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003985 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003986 &q->rx_ring_dma, GFP_KERNEL);
3987 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003988 return -ENOMEM;
3989
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003990 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003991 AT91ETHER_MAX_RX_DESCR *
3992 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003993 &q->rx_buffers_dma, GFP_KERNEL);
3994 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003995 dma_free_coherent(&lp->pdev->dev,
3996 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003997 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003998 q->rx_ring, q->rx_ring_dma);
3999 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004000 return -ENOMEM;
4001 }
4002
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004003 return 0;
4004}
4005
4006static void at91ether_free_coherent(struct macb *lp)
4007{
4008 struct macb_queue *q = &lp->queues[0];
4009
4010 if (q->rx_ring) {
4011 dma_free_coherent(&lp->pdev->dev,
4012 AT91ETHER_MAX_RX_DESCR *
4013 macb_dma_desc_get_size(lp),
4014 q->rx_ring, q->rx_ring_dma);
4015 q->rx_ring = NULL;
4016 }
4017
4018 if (q->rx_buffers) {
4019 dma_free_coherent(&lp->pdev->dev,
4020 AT91ETHER_MAX_RX_DESCR *
4021 AT91ETHER_MAX_RBUFF_SZ,
4022 q->rx_buffers, q->rx_buffers_dma);
4023 q->rx_buffers = NULL;
4024 }
4025}
4026
4027/* Initialize and start the Receiver and Transmit subsystems */
4028static int at91ether_start(struct macb *lp)
4029{
4030 struct macb_queue *q = &lp->queues[0];
4031 struct macb_dma_desc *desc;
4032 dma_addr_t addr;
4033 u32 ctl;
4034 int i, ret;
4035
4036 ret = at91ether_alloc_coherent(lp);
4037 if (ret)
4038 return ret;
4039
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004040 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004041 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004042 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004043 macb_set_addr(lp, desc, addr);
4044 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004045 addr += AT91ETHER_MAX_RBUFF_SZ;
4046 }
4047
4048 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004049 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004050
4051 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004052 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004053
4054 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004055 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004056
4057 /* Enable Receive and Transmit */
4058 ctl = macb_readl(lp, NCR);
4059 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
4060
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004061 /* Enable MAC interrupts */
4062 macb_writel(lp, IER, MACB_BIT(RCOMP) |
4063 MACB_BIT(RXUBR) |
4064 MACB_BIT(ISR_TUND) |
4065 MACB_BIT(ISR_RLE) |
4066 MACB_BIT(TCOMP) |
4067 MACB_BIT(ISR_ROVR) |
4068 MACB_BIT(HRESP));
4069
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004070 return 0;
4071}
4072
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004073static void at91ether_stop(struct macb *lp)
4074{
4075 u32 ctl;
4076
4077 /* Disable MAC interrupts */
4078 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
4079 MACB_BIT(RXUBR) |
4080 MACB_BIT(ISR_TUND) |
4081 MACB_BIT(ISR_RLE) |
4082 MACB_BIT(TCOMP) |
4083 MACB_BIT(ISR_ROVR) |
4084 MACB_BIT(HRESP));
4085
4086 /* Disable Receiver and Transmitter */
4087 ctl = macb_readl(lp, NCR);
4088 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
4089
4090 /* Free resources. */
4091 at91ether_free_coherent(lp);
4092}
4093
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004094/* Open the ethernet interface */
4095static int at91ether_open(struct net_device *dev)
4096{
4097 struct macb *lp = netdev_priv(dev);
4098 u32 ctl;
4099 int ret;
4100
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004101 ret = pm_runtime_get_sync(&lp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004102 if (ret < 0) {
4103 pm_runtime_put_noidle(&lp->pdev->dev);
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004104 return ret;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03004105 }
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004106
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004107 /* Clear internal statistics */
4108 ctl = macb_readl(lp, NCR);
4109 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
4110
4111 macb_set_hwaddr(lp);
4112
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004113 ret = at91ether_start(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004114 if (ret)
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004115 goto pm_exit;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004116
Antoine Tenart7897b072019-11-13 10:00:06 +01004117 ret = macb_phylink_connect(lp);
4118 if (ret)
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004119 goto stop;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004120
4121 netif_start_queue(dev);
4122
4123 return 0;
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004124
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004125stop:
4126 at91ether_stop(lp);
Claudiu Beznea0eaf2282020-06-24 13:08:17 +03004127pm_exit:
4128 pm_runtime_put_sync(&lp->pdev->dev);
4129 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004130}
4131
4132/* Close the interface */
4133static int at91ether_close(struct net_device *dev)
4134{
4135 struct macb *lp = netdev_priv(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004136
4137 netif_stop_queue(dev);
4138
Antoine Tenart7897b072019-11-13 10:00:06 +01004139 phylink_stop(lp->phylink);
4140 phylink_disconnect_phy(lp->phylink);
4141
Claudiu Beznea33fdef22020-06-24 13:08:18 +03004142 at91ether_stop(lp);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004143
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01004144 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004145}
4146
4147/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03004148static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
4149 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004150{
4151 struct macb *lp = netdev_priv(dev);
4152
Willy Tarreau1d608d22020-12-09 19:47:40 +01004153 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
4154 int desc = 0;
4155
4156 netif_stop_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004157
4158 /* Store packet information (to free when Tx completed) */
Willy Tarreau73d74222020-10-11 11:09:43 +02004159 lp->rm9200_txq[desc].skb = skb;
4160 lp->rm9200_txq[desc].size = skb->len;
4161 lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
4162 skb->len, DMA_TO_DEVICE);
4163 if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03004164 dev_kfree_skb_any(skb);
4165 dev->stats.tx_dropped++;
4166 netdev_err(dev, "%s: DMA mapping error\n", __func__);
4167 return NETDEV_TX_OK;
4168 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004169
4170 /* Set address of the data in the Transmit Address register */
Willy Tarreau73d74222020-10-11 11:09:43 +02004171 macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004172 /* Set length of the packet in the Transmit Control register */
4173 macb_writel(lp, TCR, skb->len);
4174
4175 } else {
4176 netdev_err(dev, "%s called, but device is busy!\n", __func__);
4177 return NETDEV_TX_BUSY;
4178 }
4179
4180 return NETDEV_TX_OK;
4181}
4182
4183/* Extract received frame from buffer descriptors and sent to upper layers.
4184 * (Called from interrupt context)
4185 */
4186static void at91ether_rx(struct net_device *dev)
4187{
4188 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004189 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004190 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004191 unsigned char *p_recv;
4192 struct sk_buff *skb;
4193 unsigned int pktlen;
4194
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004195 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004196 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004197 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004198 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004199 skb = netdev_alloc_skb(dev, pktlen + 2);
4200 if (skb) {
4201 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004202 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004203
4204 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004205 dev->stats.rx_packets++;
4206 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004207 netif_rx(skb);
4208 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004209 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004210 }
4211
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004212 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004213 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004214
4215 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004216 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004217
4218 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004219 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
4220 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004221 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004222 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00004223
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00004224 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004225 }
4226}
4227
4228/* MAC interrupt handler */
4229static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
4230{
4231 struct net_device *dev = dev_id;
4232 struct macb *lp = netdev_priv(dev);
4233 u32 intstatus, ctl;
Willy Tarreau73d74222020-10-11 11:09:43 +02004234 unsigned int desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004235
4236 /* MAC Interrupt Status register indicates what interrupts are pending.
4237 * It is automatically cleared once read.
4238 */
4239 intstatus = macb_readl(lp, ISR);
4240
4241 /* Receive complete */
4242 if (intstatus & MACB_BIT(RCOMP))
4243 at91ether_rx(dev);
4244
4245 /* Transmit complete */
Willy Tarreau1d608d22020-12-09 19:47:40 +01004246 if (intstatus & MACB_BIT(TCOMP)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004247 /* The TCOM bit is set even if the transmission failed */
4248 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004249 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004250
Willy Tarreau1d608d22020-12-09 19:47:40 +01004251 desc = 0;
4252 if (lp->rm9200_txq[desc].skb) {
Willy Tarreau73d74222020-10-11 11:09:43 +02004253 dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
4254 lp->rm9200_txq[desc].skb = NULL;
4255 dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
4256 lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004257 dev->stats.tx_packets++;
Willy Tarreau73d74222020-10-11 11:09:43 +02004258 dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004259 }
Willy Tarreau1d608d22020-12-09 19:47:40 +01004260 netif_wake_queue(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004261 }
4262
4263 /* Work-around for EMAC Errata section 41.3.1 */
4264 if (intstatus & MACB_BIT(RXUBR)) {
4265 ctl = macb_readl(lp, NCR);
4266 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08004267 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004268 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4269 }
4270
4271 if (intstatus & MACB_BIT(ISR_ROVR))
4272 netdev_err(dev, "ROVR error\n");
4273
4274 return IRQ_HANDLED;
4275}
4276
4277#ifdef CONFIG_NET_POLL_CONTROLLER
4278static void at91ether_poll_controller(struct net_device *dev)
4279{
4280 unsigned long flags;
4281
4282 local_irq_save(flags);
4283 at91ether_interrupt(dev->irq, dev);
4284 local_irq_restore(flags);
4285}
4286#endif
4287
4288static const struct net_device_ops at91ether_netdev_ops = {
4289 .ndo_open = at91ether_open,
4290 .ndo_stop = at91ether_close,
4291 .ndo_start_xmit = at91ether_start_xmit,
4292 .ndo_get_stats = macb_get_stats,
4293 .ndo_set_rx_mode = macb_set_rx_mode,
4294 .ndo_set_mac_address = eth_mac_addr,
4295 .ndo_do_ioctl = macb_ioctl,
4296 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004297#ifdef CONFIG_NET_POLL_CONTROLLER
4298 .ndo_poll_controller = at91ether_poll_controller,
4299#endif
4300};
4301
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004302static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304303 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304304 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004305{
4306 int err;
4307
4308 *hclk = NULL;
4309 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304310 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304311 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004312
4313 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4314 if (IS_ERR(*pclk))
4315 return PTR_ERR(*pclk);
4316
4317 err = clk_prepare_enable(*pclk);
4318 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004319 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004320 return err;
4321 }
4322
4323 return 0;
4324}
4325
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004326static int at91ether_init(struct platform_device *pdev)
4327{
4328 struct net_device *dev = platform_get_drvdata(pdev);
4329 struct macb *bp = netdev_priv(dev);
4330 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004331
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004332 bp->queues[0].bp = bp;
4333
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004334 dev->netdev_ops = &at91ether_netdev_ops;
4335 dev->ethtool_ops = &macb_ethtool_ops;
4336
4337 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4338 0, dev->name, dev);
4339 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004340 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004341
4342 macb_writel(bp, NCR, 0);
4343
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004344 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004345
4346 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004347}
4348
Yash Shahc218ad52019-06-18 13:26:08 +05304349static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4350 unsigned long parent_rate)
4351{
4352 return mgmt->rate;
4353}
4354
4355static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4356 unsigned long *parent_rate)
4357{
4358 if (WARN_ON(rate < 2500000))
4359 return 2500000;
4360 else if (rate == 2500000)
4361 return 2500000;
4362 else if (WARN_ON(rate < 13750000))
4363 return 2500000;
4364 else if (WARN_ON(rate < 25000000))
4365 return 25000000;
4366 else if (rate == 25000000)
4367 return 25000000;
4368 else if (WARN_ON(rate < 75000000))
4369 return 25000000;
4370 else if (WARN_ON(rate < 125000000))
4371 return 125000000;
4372 else if (rate == 125000000)
4373 return 125000000;
4374
4375 WARN_ON(rate > 125000000);
4376
4377 return 125000000;
4378}
4379
4380static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4381 unsigned long parent_rate)
4382{
4383 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4384 if (rate != 125000000)
4385 iowrite32(1, mgmt->reg);
4386 else
4387 iowrite32(0, mgmt->reg);
4388 mgmt->rate = rate;
4389
4390 return 0;
4391}
4392
4393static const struct clk_ops fu540_c000_ops = {
4394 .recalc_rate = fu540_macb_tx_recalc_rate,
4395 .round_rate = fu540_macb_tx_round_rate,
4396 .set_rate = fu540_macb_tx_set_rate,
4397};
4398
4399static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4400 struct clk **hclk, struct clk **tx_clk,
4401 struct clk **rx_clk, struct clk **tsu_clk)
4402{
4403 struct clk_init_data init;
4404 int err = 0;
4405
4406 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4407 if (err)
4408 return err;
4409
4410 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004411 if (!mgmt) {
4412 err = -ENOMEM;
4413 goto err_disable_clks;
4414 }
Yash Shahc218ad52019-06-18 13:26:08 +05304415
4416 init.name = "sifive-gemgxl-mgmt";
4417 init.ops = &fu540_c000_ops;
4418 init.flags = 0;
4419 init.num_parents = 0;
4420
4421 mgmt->rate = 0;
4422 mgmt->hw.init = &init;
4423
Stephen Boydd89091a2020-01-03 16:19:21 -08004424 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004425 if (IS_ERR(*tx_clk)) {
4426 err = PTR_ERR(*tx_clk);
4427 goto err_disable_clks;
4428 }
Yash Shahc218ad52019-06-18 13:26:08 +05304429
4430 err = clk_prepare_enable(*tx_clk);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004431 if (err) {
Yash Shahc218ad52019-06-18 13:26:08 +05304432 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004433 *tx_clk = NULL;
4434 goto err_disable_clks;
4435 } else {
Yash Shahc218ad52019-06-18 13:26:08 +05304436 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004437 }
Yash Shahc218ad52019-06-18 13:26:08 +05304438
4439 return 0;
Claudiu Bezneaf4de93f2020-12-09 15:03:35 +02004440
4441err_disable_clks:
4442 macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
4443
4444 return err;
Yash Shahc218ad52019-06-18 13:26:08 +05304445}
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 Bezneaec771de2020-12-09 15:03:38 +02004456static const struct macb_usrio_config sama7g5_usrio = {
4457 .mii = 0,
4458 .rmii = 1,
4459 .rgmii = 2,
4460 .refclk = BIT(2),
4461 .hdfctlen = BIT(6),
4462};
4463
Yash Shahc218ad52019-06-18 13:26:08 +05304464static const struct macb_config fu540_c000_config = {
4465 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4466 MACB_CAPS_GEM_HAS_PTP,
4467 .dma_burst_length = 16,
4468 .clk_init = fu540_c000_clk_init,
4469 .init = fu540_c000_init,
4470 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004471 .usrio = &macb_default_usrio,
Yash Shahc218ad52019-06-18 13:26:08 +05304472};
4473
David S. Miller3cef5c52015-03-09 23:38:02 -04004474static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004475 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004476 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004477 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004478 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004479};
4480
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004481static const struct macb_config sama5d3macb_config = {
4482 .caps = MACB_CAPS_SG_DISABLED
4483 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4484 .clk_init = macb_clk_init,
4485 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004486 .usrio = &macb_default_usrio,
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004487};
4488
David S. Miller3cef5c52015-03-09 23:38:02 -04004489static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004490 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4491 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004492 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004493 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004494 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004495};
4496
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004497static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004498 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004499 .dma_burst_length = 16,
4500 .clk_init = macb_clk_init,
4501 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004502 .usrio = &macb_default_usrio,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004503};
4504
David S. Miller3cef5c52015-03-09 23:38:02 -04004505static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004506 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004507 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004508 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004509 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004510 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004511 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004512 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004513};
4514
David S. Miller3cef5c52015-03-09 23:38:02 -04004515static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004516 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004517 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004518 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004519 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004520 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004521};
4522
David S. Miller3cef5c52015-03-09 23:38:02 -04004523static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004524 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004525 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004526 .init = at91ether_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004527 .usrio = &macb_default_usrio,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004528};
4529
Neil Armstronge611b5b2016-01-05 14:39:17 +01004530static const struct macb_config np4_config = {
4531 .caps = MACB_CAPS_USRIO_DISABLED,
4532 .clk_init = macb_clk_init,
4533 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004534 .usrio = &macb_default_usrio,
Neil Armstronge611b5b2016-01-05 14:39:17 +01004535};
David S. Miller36583eb2015-05-23 01:22:35 -04004536
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304537static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004538 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4539 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304540 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304541 .dma_burst_length = 16,
4542 .clk_init = macb_clk_init,
4543 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304544 .jumbo_max_len = 10240,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004545 .usrio = &macb_default_usrio,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304546};
4547
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004548static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304549 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4550 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004551 .dma_burst_length = 16,
4552 .clk_init = macb_clk_init,
4553 .init = macb_init,
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004554 .usrio = &macb_default_usrio,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004555};
4556
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004557static const struct macb_config sama7g5_gem_config = {
4558 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG,
4559 .dma_burst_length = 16,
4560 .clk_init = macb_clk_init,
4561 .init = macb_init,
4562 .usrio = &sama7g5_usrio,
4563};
4564
Claudiu Beznea700d5662020-12-09 15:03:39 +02004565static const struct macb_config sama7g5_emac_config = {
4566 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_USRIO_HAS_CLKEN,
4567 .dma_burst_length = 16,
4568 .clk_init = macb_clk_init,
4569 .init = macb_init,
4570 .usrio = &sama7g5_usrio,
4571};
4572
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004573static const struct of_device_id macb_dt_ids[] = {
4574 { .compatible = "cdns,at32ap7000-macb" },
4575 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4576 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004577 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004578 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4579 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004580 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004581 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004582 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004583 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004584 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4585 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4586 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304587 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004588 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304589 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Claudiu Bezneaec771de2020-12-09 15:03:38 +02004590 { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
Claudiu Beznea700d5662020-12-09 15:03:39 +02004591 { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004592 { /* sentinel */ }
4593};
4594MODULE_DEVICE_TABLE(of, macb_dt_ids);
4595#endif /* CONFIG_OF */
4596
Bartosz Folta83a77e92016-12-14 06:39:15 +00004597static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004598 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4599 MACB_CAPS_JUMBO |
4600 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004601 .dma_burst_length = 16,
4602 .clk_init = macb_clk_init,
4603 .init = macb_init,
Atish Patrab1242232021-03-03 11:55:49 -08004604 .usrio = &macb_default_usrio,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004605 .jumbo_max_len = 10240,
4606};
4607
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004608static int macb_probe(struct platform_device *pdev)
4609{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004610 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004611 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304612 struct clk **, struct clk **, struct clk **,
4613 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004614 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004615 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304616 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304617 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004618 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004619 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004620 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004621 struct net_device *dev;
4622 struct resource *regs;
4623 void __iomem *mem;
4624 const char *mac;
4625 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304626 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004627
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004628 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4629 mem = devm_ioremap_resource(&pdev->dev, regs);
4630 if (IS_ERR(mem))
4631 return PTR_ERR(mem);
4632
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004633 if (np) {
4634 const struct of_device_id *match;
4635
4636 match = of_match_node(macb_dt_ids, np);
4637 if (match && match->data) {
4638 macb_config = match->data;
4639 clk_init = macb_config->clk_init;
4640 init = macb_config->init;
4641 }
4642 }
4643
Harini Katakamf5473d12019-03-01 16:20:33 +05304644 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004645 if (err)
4646 return err;
4647
Harini Katakamd54f89a2019-03-01 16:20:34 +05304648 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4649 pm_runtime_use_autosuspend(&pdev->dev);
4650 pm_runtime_get_noresume(&pdev->dev);
4651 pm_runtime_set_active(&pdev->dev);
4652 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004653 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004654
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004655 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004656 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004657 if (!dev) {
4658 err = -ENOMEM;
4659 goto err_disable_clocks;
4660 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004661
4662 dev->base_addr = regs->start;
4663
4664 SET_NETDEV_DEV(dev, &pdev->dev);
4665
4666 bp = netdev_priv(dev);
4667 bp->pdev = pdev;
4668 bp->dev = dev;
4669 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004670 bp->native_io = native_io;
4671 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004672 bp->macb_reg_readl = hw_readl_native;
4673 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004674 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004675 bp->macb_reg_readl = hw_readl;
4676 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004677 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004678 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004679 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004680 if (macb_config)
4681 bp->dma_burst_length = macb_config->dma_burst_length;
4682 bp->pclk = pclk;
4683 bp->hclk = hclk;
4684 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304685 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304686 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004687 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304688 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304689
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004690 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004691 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004692 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
Nicolas Ferreced47992020-07-10 14:46:42 +02004693 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004694
Claudiu Bezneaedac6382020-12-09 15:03:32 +02004695 bp->usrio = macb_config->usrio;
4696
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004697 spin_lock_init(&bp->lock);
4698
Nicolas Ferread783472015-03-31 15:02:02 +02004699 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004700 macb_configure_caps(bp, macb_config);
4701
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004702#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4703 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4704 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4705 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4706 }
4707#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004708 platform_set_drvdata(pdev, dev);
4709
4710 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004711 if (dev->irq < 0) {
4712 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004713 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004714 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004715
Jarod Wilson44770e12016-10-17 15:54:17 -04004716 /* MTU range: 68 - 1500 or 10240 */
4717 dev->min_mtu = GEM_MTU_MIN_SIZE;
4718 if (bp->caps & MACB_CAPS_JUMBO)
4719 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4720 else
4721 dev->max_mtu = ETH_DATA_LEN;
4722
Harini Katakam404cd082018-07-06 12:18:58 +05304723 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4724 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4725 if (val)
4726 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4727 macb_dma_desc_get_size(bp);
4728
4729 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4730 if (val)
4731 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4732 macb_dma_desc_get_size(bp);
4733 }
4734
Harini Katakame5010702019-01-29 15:20:03 +05304735 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4736 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4737 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4738
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004739 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004740 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4741 err = -EPROBE_DEFER;
4742 goto err_out_free_netdev;
Antoine Tenart2bf4ecb2019-06-21 17:26:35 +02004743 } else if (!IS_ERR_OR_NULL(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004744 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004745 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004746 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004747 }
frederic RODO6c36a702007-07-12 19:07:24 +02004748
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004749 err = of_get_phy_mode(np, &interface);
4750 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004751 /* not found in DT, MII by default */
4752 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4753 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004754 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004755
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004756 /* IP specific init */
4757 err = init(pdev);
4758 if (err)
4759 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004760
Florian Fainellicf669662016-05-02 18:38:45 -07004761 err = macb_mii_init(bp);
4762 if (err)
4763 goto err_out_free_netdev;
4764
Florian Fainellicf669662016-05-02 18:38:45 -07004765 netif_carrier_off(dev);
4766
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004767 err = register_netdev(dev);
4768 if (err) {
4769 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004770 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004771 }
4772
Allen Paise7412b82020-09-14 12:59:23 +05304773 tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
Harini Katakam032dc412018-01-27 12:09:01 +05304774
Bo Shen58798232014-09-13 01:57:49 +02004775 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4776 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4777 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004778
Harini Katakamd54f89a2019-03-01 16:20:34 +05304779 pm_runtime_mark_last_busy(&bp->pdev->dev);
4780 pm_runtime_put_autosuspend(&bp->pdev->dev);
4781
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004782 return 0;
4783
Florian Fainellicf669662016-05-02 18:38:45 -07004784err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004785 mdiobus_unregister(bp->mii_bus);
4786 mdiobus_free(bp->mii_bus);
4787
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004788err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004789 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004790
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004791err_disable_clocks:
Claudiu Beznea38493da2020-12-09 15:03:34 +02004792 macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304793 pm_runtime_disable(&pdev->dev);
4794 pm_runtime_set_suspended(&pdev->dev);
4795 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004796
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004797 return err;
4798}
4799
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004800static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004801{
4802 struct net_device *dev;
4803 struct macb *bp;
4804
4805 dev = platform_get_drvdata(pdev);
4806
4807 if (dev) {
4808 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004809 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004810 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004811
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004812 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004813 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304814 pm_runtime_disable(&pdev->dev);
4815 pm_runtime_dont_use_autosuspend(&pdev->dev);
4816 if (!pm_runtime_suspended(&pdev->dev)) {
Claudiu Beznea38493da2020-12-09 15:03:34 +02004817 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
4818 bp->rx_clk, bp->tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304819 pm_runtime_set_suspended(&pdev->dev);
4820 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004821 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004822 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004823 }
4824
4825 return 0;
4826}
4827
Michal Simekd23823d2015-01-23 09:36:03 +01004828static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004829{
Wolfram Sangce886a42018-10-21 22:00:14 +02004830 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004831 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304832 struct macb_queue *queue = bp->queues;
4833 unsigned long flags;
4834 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004835 int err;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004836
Harini Katakamde991c52019-03-01 16:20:35 +05304837 if (!netif_running(netdev))
4838 return 0;
4839
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004840 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004841 spin_lock_irqsave(&bp->lock, flags);
4842 /* Flush all status bits */
4843 macb_writel(bp, TSR, -1);
4844 macb_writel(bp, RSR, -1);
Harini Katakamde991c52019-03-01 16:20:35 +05304845 for (q = 0, queue = bp->queues; q < bp->num_queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004846 ++q, ++queue) {
4847 /* Disable all interrupts */
4848 queue_writel(queue, IDR, -1);
4849 queue_readl(queue, ISR);
4850 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4851 queue_writel(queue, ISR, -1);
4852 }
4853 /* Change interrupt handler and
4854 * Enable WoL IRQ on queue 0
4855 */
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004856 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004857 if (macb_is_gem(bp)) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004858 err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
4859 IRQF_SHARED, netdev->name, bp->queues);
4860 if (err) {
4861 dev_err(dev,
4862 "Unable to request IRQ %d (error %d)\n",
4863 bp->queues[0].irq, err);
4864 spin_unlock_irqrestore(&bp->lock, flags);
4865 return err;
4866 }
4867 queue_writel(bp->queues, IER, GEM_BIT(WOL));
4868 gem_writel(bp, WOL, MACB_BIT(MAG));
4869 } else {
Nicolas Ferre9d45c8e2020-07-20 10:56:53 +02004870 err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
4871 IRQF_SHARED, netdev->name, bp->queues);
4872 if (err) {
4873 dev_err(dev,
4874 "Unable to request IRQ %d (error %d)\n",
4875 bp->queues[0].irq, err);
4876 spin_unlock_irqrestore(&bp->lock, flags);
4877 return err;
4878 }
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004879 queue_writel(bp->queues, IER, MACB_BIT(WOL));
4880 macb_writel(bp, WOL, MACB_BIT(MAG));
4881 }
4882 spin_unlock_irqrestore(&bp->lock, flags);
4883
4884 enable_irq_wake(bp->queues[0].irq);
4885 }
4886
4887 netif_device_detach(netdev);
4888 for (q = 0, queue = bp->queues; q < bp->num_queues;
4889 ++q, ++queue)
4890 napi_disable(&queue->napi);
4891
4892 if (!(bp->wol & MACB_WOL_ENABLED)) {
Antoine Tenart7897b072019-11-13 10:00:06 +01004893 rtnl_lock();
4894 phylink_stop(bp->phylink);
4895 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304896 spin_lock_irqsave(&bp->lock, flags);
4897 macb_reset_hw(bp);
4898 spin_unlock_irqrestore(&bp->lock, flags);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304899 }
4900
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004901 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4902 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4903
4904 if (netdev->hw_features & NETIF_F_NTUPLE)
4905 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4906
Harini Katakamde991c52019-03-01 16:20:35 +05304907 if (bp->ptp_info)
4908 bp->ptp_info->ptp_remove(netdev);
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004909 if (!device_may_wakeup(dev))
4910 pm_runtime_force_suspend(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304911
4912 return 0;
4913}
4914
4915static int __maybe_unused macb_resume(struct device *dev)
4916{
4917 struct net_device *netdev = dev_get_drvdata(dev);
4918 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304919 struct macb_queue *queue = bp->queues;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004920 unsigned long flags;
Harini Katakamde991c52019-03-01 16:20:35 +05304921 unsigned int q;
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004922 int err;
Harini Katakamde991c52019-03-01 16:20:35 +05304923
4924 if (!netif_running(netdev))
4925 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304926
Nicolas Ferre6c8f85c2020-07-10 14:46:45 +02004927 if (!device_may_wakeup(dev))
4928 pm_runtime_force_resume(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304929
4930 if (bp->wol & MACB_WOL_ENABLED) {
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004931 spin_lock_irqsave(&bp->lock, flags);
4932 /* Disable WoL */
4933 if (macb_is_gem(bp)) {
4934 queue_writel(bp->queues, IDR, GEM_BIT(WOL));
4935 gem_writel(bp, WOL, 0);
4936 } else {
4937 queue_writel(bp->queues, IDR, MACB_BIT(WOL));
4938 macb_writel(bp, WOL, 0);
4939 }
4940 /* Clear ISR on queue 0 */
4941 queue_readl(bp->queues, ISR);
4942 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
4943 queue_writel(bp->queues, ISR, -1);
4944 /* Replace interrupt handler on queue 0 */
4945 devm_free_irq(dev, bp->queues[0].irq, bp->queues);
4946 err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
4947 IRQF_SHARED, netdev->name, bp->queues);
4948 if (err) {
4949 dev_err(dev,
4950 "Unable to request IRQ %d (error %d)\n",
4951 bp->queues[0].irq, err);
4952 spin_unlock_irqrestore(&bp->lock, flags);
4953 return err;
4954 }
4955 spin_unlock_irqrestore(&bp->lock, flags);
4956
Harini Katakamd54f89a2019-03-01 16:20:34 +05304957 disable_irq_wake(bp->queues[0].irq);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004958
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004959 /* Now make sure we disable phy before moving
4960 * to common restore path
4961 */
Antoine Tenart7897b072019-11-13 10:00:06 +01004962 rtnl_lock();
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004963 phylink_stop(bp->phylink);
Antoine Tenart7897b072019-11-13 10:00:06 +01004964 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304965 }
4966
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004967 for (q = 0, queue = bp->queues; q < bp->num_queues;
4968 ++q, ++queue)
4969 napi_enable(&queue->napi);
4970
4971 if (netdev->hw_features & NETIF_F_NTUPLE)
4972 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4973
4974 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4975 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4976
4977 macb_writel(bp, NCR, MACB_BIT(MPE));
Harini Katakamde991c52019-03-01 16:20:35 +05304978 macb_init_hw(bp);
4979 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004980 macb_restore_features(bp);
Nicolas Ferre558e35c2020-07-20 10:56:52 +02004981 rtnl_lock();
4982 phylink_start(bp->phylink);
4983 rtnl_unlock();
4984
Harini Katakamd54f89a2019-03-01 16:20:34 +05304985 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304986 if (bp->ptp_info)
4987 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304988
4989 return 0;
4990}
4991
4992static int __maybe_unused macb_runtime_suspend(struct device *dev)
4993{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004994 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304995 struct macb *bp = netdev_priv(netdev);
4996
Claudiu Beznea38493da2020-12-09 15:03:34 +02004997 if (!(device_may_wakeup(dev)))
4998 macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
4999 else
5000 macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005001
5002 return 0;
5003}
5004
Harini Katakamd54f89a2019-03-01 16:20:34 +05305005static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005006{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01005007 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005008 struct macb *bp = netdev_priv(netdev);
5009
Nicolas Ferre515a10a2020-07-10 14:46:41 +02005010 if (!(device_may_wakeup(dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005011 clk_prepare_enable(bp->pclk);
5012 clk_prepare_enable(bp->hclk);
5013 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05305014 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02005015 }
Harini Katakamf5473d12019-03-01 16:20:33 +05305016 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005017
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005018 return 0;
5019}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01005020
Harini Katakamd54f89a2019-03-01 16:20:34 +05305021static const struct dev_pm_ops macb_pm_ops = {
5022 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
5023 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
5024};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005025
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005026static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005027 .probe = macb_probe,
5028 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005029 .driver = {
5030 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01005031 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08005032 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005033 },
5034};
5035
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00005036module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01005037
5038MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00005039MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02005040MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07005041MODULE_ALIAS("platform:macb");