blob: bd6a62f4bd7dcd529b12248806160003e5aa348f [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030013#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000018#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010019#include <linux/slab.h>
20#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080021#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000022#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010023#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000028#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010029#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020030#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080031#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010033#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020034#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010035#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000036#include <linux/ip.h>
37#include <linux/udp.h>
38#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053039#include <linux/iopoll.h>
Harini Katakamd54f89a2019-03-01 16:20:34 +053040#include <linux/pm_runtime.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010041#include "macb.h"
42
Nicolas Ferre1b447912013-06-04 21:57:11 +000043#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000044#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050045
Zach Brownb410d132016-10-19 09:56:57 -050046#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050047#define MIN_RX_RING_SIZE 64
48#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000049#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050050 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010051
Zach Brownb410d132016-10-19 09:56:57 -050052#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050053#define MIN_TX_RING_SIZE 64
54#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000055#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050056 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
Nicolas Ferre909a8582012-11-19 06:00:21 +000058/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050059#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010060
Harini Katakame5010702019-01-29 15:20:03 +053061#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000062#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
63 | MACB_BIT(ISR_RLE) \
64 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000065#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
66 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000067
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000068/* Max length of transmit frame must be a multiple of 8 bytes */
69#define MACB_TX_LEN_ALIGN 8
70#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
71#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020072
Jarod Wilson44770e12016-10-17 15:54:17 -040073#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070074#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053075
Sergio Prado3e2a5e12016-02-09 12:07:16 -020076#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
77#define MACB_WOL_ENABLED (0x1 << 1)
78
Moritz Fischer64ec42f2016-03-29 19:11:12 -070079/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000080 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
81 */
82#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010083
Harini Katakamd54f89a2019-03-01 16:20:34 +053084#define MACB_PM_TIMEOUT 100 /* ms */
85
Harini Katakam8beb79b2019-03-01 16:20:32 +053086#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
87
Rafal Ozieblodc97a892017-01-27 15:08:20 +000088/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010089 * depends on hardware configuration:
90 *
91 * 1. dma address width 32 bits:
92 * word 1: 32 bit address of Data Buffer
93 * word 2: control
94 *
95 * 2. dma address width 64 bits:
96 * word 1: 32 bit address of Data Buffer
97 * word 2: control
98 * word 3: upper 32 bit address of Data Buffer
99 * word 4: unused
100 *
101 * 3. dma address width 32 bits with hardware timestamping:
102 * word 1: 32 bit address of Data Buffer
103 * word 2: control
104 * word 3: timestamp word 1
105 * word 4: timestamp word 2
106 *
107 * 4. dma address width 64 bits with hardware timestamping:
108 * word 1: 32 bit address of Data Buffer
109 * word 2: control
110 * word 3: upper 32 bit address of Data Buffer
111 * word 4: unused
112 * word 5: timestamp word 1
113 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000114 */
115static unsigned int macb_dma_desc_get_size(struct macb *bp)
116{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100117#ifdef MACB_EXT_DESC
118 unsigned int desc_size;
119
120 switch (bp->hw_dma_cap) {
121 case HW_DMA_CAP_64B:
122 desc_size = sizeof(struct macb_dma_desc)
123 + sizeof(struct macb_dma_desc_64);
124 break;
125 case HW_DMA_CAP_PTP:
126 desc_size = sizeof(struct macb_dma_desc)
127 + sizeof(struct macb_dma_desc_ptp);
128 break;
129 case HW_DMA_CAP_64B_PTP:
130 desc_size = sizeof(struct macb_dma_desc)
131 + sizeof(struct macb_dma_desc_64)
132 + sizeof(struct macb_dma_desc_ptp);
133 break;
134 default:
135 desc_size = sizeof(struct macb_dma_desc);
136 }
137 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000138#endif
139 return sizeof(struct macb_dma_desc);
140}
141
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100142static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000143{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100144#ifdef MACB_EXT_DESC
145 switch (bp->hw_dma_cap) {
146 case HW_DMA_CAP_64B:
147 case HW_DMA_CAP_PTP:
148 desc_idx <<= 1;
149 break;
150 case HW_DMA_CAP_64B_PTP:
151 desc_idx *= 3;
152 break;
153 default:
154 break;
155 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000156#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100157 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000158}
159
160#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
161static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
162{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100163 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
164 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
165 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000166}
167#endif
168
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000169/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500170static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000171{
Zach Brownb410d132016-10-19 09:56:57 -0500172 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000173}
174
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100175static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
176 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000177{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000178 index = macb_tx_ring_wrap(queue->bp, index);
179 index = macb_adj_dma_desc_idx(queue->bp, index);
180 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181}
182
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100183static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
184 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000185{
Zach Brownb410d132016-10-19 09:56:57 -0500186 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000187}
188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100189static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000190{
191 dma_addr_t offset;
192
Zach Brownb410d132016-10-19 09:56:57 -0500193 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000194 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100196 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000197}
198
Zach Brownb410d132016-10-19 09:56:57 -0500199static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000200{
Zach Brownb410d132016-10-19 09:56:57 -0500201 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000202}
203
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000204static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000206 index = macb_rx_ring_wrap(queue->bp, index);
207 index = macb_adj_dma_desc_idx(queue->bp, index);
208 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000209}
210
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000211static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000212{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000213 return queue->rx_buffers + queue->bp->rx_buffer_size *
214 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000215}
216
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300217/* I/O accessors */
218static u32 hw_readl_native(struct macb *bp, int offset)
219{
220 return __raw_readl(bp->regs + offset);
221}
222
223static void hw_writel_native(struct macb *bp, int offset, u32 value)
224{
225 __raw_writel(value, bp->regs + offset);
226}
227
228static u32 hw_readl(struct macb *bp, int offset)
229{
230 return readl_relaxed(bp->regs + offset);
231}
232
233static void hw_writel(struct macb *bp, int offset, u32 value)
234{
235 writel_relaxed(value, bp->regs + offset);
236}
237
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700238/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700239 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300240 * descriptor access.
241 */
242static bool hw_is_native_io(void __iomem *addr)
243{
244 u32 value = MACB_BIT(LLB);
245
246 __raw_writel(value, addr + MACB_NCR);
247 value = __raw_readl(addr + MACB_NCR);
248
249 /* Write 0 back to disable everything */
250 __raw_writel(0, addr + MACB_NCR);
251
252 return value == MACB_BIT(LLB);
253}
254
255static bool hw_is_gem(void __iomem *addr, bool native_io)
256{
257 u32 id;
258
259 if (native_io)
260 id = __raw_readl(addr + MACB_MID);
261 else
262 id = readl_relaxed(addr + MACB_MID);
263
264 return MACB_BFEXT(IDNUM, id) >= 0x2;
265}
266
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100267static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100268{
269 u32 bottom;
270 u16 top;
271
272 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000273 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100274 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000275 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000276
277 /* Clear unused address register sets */
278 macb_or_gem_writel(bp, SA2B, 0);
279 macb_or_gem_writel(bp, SA2T, 0);
280 macb_or_gem_writel(bp, SA3B, 0);
281 macb_or_gem_writel(bp, SA3T, 0);
282 macb_or_gem_writel(bp, SA4B, 0);
283 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284}
285
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100286static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100287{
288 u32 bottom;
289 u16 top;
290 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000291 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100292
Moritz Fischeraa50b552016-03-29 19:11:13 -0700293 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000294 for (i = 0; i < 4; i++) {
295 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
296 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297
Nicolas Ferre8b952742019-05-03 12:36:58 +0200298 addr[0] = bottom & 0xff;
299 addr[1] = (bottom >> 8) & 0xff;
300 addr[2] = (bottom >> 16) & 0xff;
301 addr[3] = (bottom >> 24) & 0xff;
302 addr[4] = top & 0xff;
303 addr[5] = (top >> 8) & 0xff;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100304
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000305 if (is_valid_ether_addr(addr)) {
306 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
307 return;
308 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700309 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000310
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300311 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000312 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100313}
314
Harini Katakam8beb79b2019-03-01 16:20:32 +0530315static int macb_mdio_wait_for_idle(struct macb *bp)
316{
317 u32 val;
318
319 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
320 1, MACB_MDIO_TIMEOUT);
321}
322
frederic RODO6c36a702007-07-12 19:07:24 +0200323static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100324{
frederic RODO6c36a702007-07-12 19:07:24 +0200325 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530326 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530327
Harini Katakamd54f89a2019-03-01 16:20:34 +0530328 status = pm_runtime_get_sync(&bp->pdev->dev);
329 if (status < 0)
330 goto mdio_pm_exit;
331
332 status = macb_mdio_wait_for_idle(bp);
333 if (status < 0)
334 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100335
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
337 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200338 | MACB_BF(PHYA, mii_id)
339 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100340 | MACB_BF(CODE, MACB_MAN_CODE)));
341
Harini Katakamd54f89a2019-03-01 16:20:34 +0530342 status = macb_mdio_wait_for_idle(bp);
343 if (status < 0)
344 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
Harini Katakamd54f89a2019-03-01 16:20:34 +0530346 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347
Harini Katakamd54f89a2019-03-01 16:20:34 +0530348mdio_read_exit:
349 pm_runtime_mark_last_busy(&bp->pdev->dev);
350 pm_runtime_put_autosuspend(&bp->pdev->dev);
351mdio_pm_exit:
352 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100353}
354
frederic RODO6c36a702007-07-12 19:07:24 +0200355static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
356 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100357{
frederic RODO6c36a702007-07-12 19:07:24 +0200358 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530359 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530360
Harini Katakamd54f89a2019-03-01 16:20:34 +0530361 status = pm_runtime_get_sync(&bp->pdev->dev);
362 if (status < 0)
363 goto mdio_pm_exit;
364
365 status = macb_mdio_wait_for_idle(bp);
366 if (status < 0)
367 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100368
369 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
370 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200371 | MACB_BF(PHYA, mii_id)
372 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100373 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200374 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100375
Harini Katakamd54f89a2019-03-01 16:20:34 +0530376 status = macb_mdio_wait_for_idle(bp);
377 if (status < 0)
378 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100379
Harini Katakamd54f89a2019-03-01 16:20:34 +0530380mdio_write_exit:
381 pm_runtime_mark_last_busy(&bp->pdev->dev);
382 pm_runtime_put_autosuspend(&bp->pdev->dev);
383mdio_pm_exit:
384 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100385}
386
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800387/**
388 * macb_set_tx_clk() - Set a clock to a new frequency
389 * @clk Pointer to the clock to change
390 * @rate New frequency in Hz
391 * @dev Pointer to the struct net_device
392 */
393static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
394{
395 long ferr, rate, rate_rounded;
396
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100397 if (!clk)
398 return;
399
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800400 switch (speed) {
401 case SPEED_10:
402 rate = 2500000;
403 break;
404 case SPEED_100:
405 rate = 25000000;
406 break;
407 case SPEED_1000:
408 rate = 125000000;
409 break;
410 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800411 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800412 }
413
414 rate_rounded = clk_round_rate(clk, rate);
415 if (rate_rounded < 0)
416 return;
417
418 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
419 * is not satisfied.
420 */
421 ferr = abs(rate_rounded - rate);
422 ferr = DIV_ROUND_UP(ferr, rate / 100000);
423 if (ferr > 5)
424 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700425 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800426
427 if (clk_set_rate(clk, rate_rounded))
428 netdev_err(dev, "adjusting tx_clk failed.\n");
429}
430
frederic RODO6c36a702007-07-12 19:07:24 +0200431static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100432{
frederic RODO6c36a702007-07-12 19:07:24 +0200433 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200434 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200435 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200436 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100437
frederic RODO6c36a702007-07-12 19:07:24 +0200438 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100439
frederic RODO6c36a702007-07-12 19:07:24 +0200440 if (phydev->link) {
441 if ((bp->speed != phydev->speed) ||
442 (bp->duplex != phydev->duplex)) {
443 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
frederic RODO6c36a702007-07-12 19:07:24 +0200445 reg = macb_readl(bp, NCFGR);
446 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000447 if (macb_is_gem(bp))
448 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200449
450 if (phydev->duplex)
451 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900452 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200453 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200454 if (phydev->speed == SPEED_1000 &&
455 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000456 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200457
Patrice Vilchez140b7552012-10-31 06:04:50 +0000458 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200459
460 bp->speed = phydev->speed;
461 bp->duplex = phydev->duplex;
462 status_change = 1;
463 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100464 }
465
frederic RODO6c36a702007-07-12 19:07:24 +0200466 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700467 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200468 bp->speed = 0;
469 bp->duplex = -1;
470 }
471 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100472
frederic RODO6c36a702007-07-12 19:07:24 +0200473 status_change = 1;
474 }
475
476 spin_unlock_irqrestore(&bp->lock, flags);
477
478 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000479 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500480 /* Update the TX clock rate if and only if the link is
481 * up and there has been a link change.
482 */
483 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
484
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000485 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000486 netdev_info(dev, "link up (%d/%s)\n",
487 phydev->speed,
488 phydev->duplex == DUPLEX_FULL ?
489 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000490 } else {
491 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000492 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000493 }
frederic RODO6c36a702007-07-12 19:07:24 +0200494 }
495}
496
497/* based on au1000_eth. c*/
498static int macb_mii_probe(struct net_device *dev)
499{
500 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000501 struct phy_device *phydev;
Brad Mouring739de9a2018-03-13 16:32:13 -0500502 struct device_node *np;
Nicolas Ferre8b952742019-05-03 12:36:58 +0200503 int ret, i;
Brad Mouring739de9a2018-03-13 16:32:13 -0500504
Brad Mouring739de9a2018-03-13 16:32:13 -0500505 np = bp->pdev->dev.of_node;
506 ret = 0;
507
508 if (np) {
509 if (of_phy_is_fixed_link(np)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500510 bp->phy_node = of_node_get(np);
511 } else {
Brad Mouring2105a5d2018-03-13 16:32:15 -0500512 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
513 /* fallback to standard phy registration if no
514 * phy-handle was found nor any phy found during
515 * dt phy registration
Brad Mouring739de9a2018-03-13 16:32:13 -0500516 */
Brad Mouring2105a5d2018-03-13 16:32:15 -0500517 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500518 for (i = 0; i < PHY_MAX_ADDR; i++) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500519 phydev = mdiobus_scan(bp->mii_bus, i);
520 if (IS_ERR(phydev) &&
521 PTR_ERR(phydev) != -ENODEV) {
522 ret = PTR_ERR(phydev);
523 break;
524 }
525 }
526
527 if (ret)
528 return -ENODEV;
529 }
530 }
531 }
frederic RODO6c36a702007-07-12 19:07:24 +0200532
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200533 if (bp->phy_node) {
534 phydev = of_phy_connect(dev, bp->phy_node,
535 &macb_handle_link_change, 0,
536 bp->phy_interface);
537 if (!phydev)
538 return -ENODEV;
539 } else {
540 phydev = phy_find_first(bp->mii_bus);
541 if (!phydev) {
542 netdev_err(dev, "no PHY found\n");
543 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000544 }
frederic RODO6c36a702007-07-12 19:07:24 +0200545
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200546 /* attach the mac to the phy */
547 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
548 bp->phy_interface);
549 if (ret) {
550 netdev_err(dev, "Could not attach to PHY\n");
551 return ret;
552 }
frederic RODO6c36a702007-07-12 19:07:24 +0200553 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100554
frederic RODO6c36a702007-07-12 19:07:24 +0200555 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200556 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Andrew Lunn58056c12018-09-12 01:53:11 +0200557 phy_set_max_speed(phydev, SPEED_1000);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000558 else
Andrew Lunn58056c12018-09-12 01:53:11 +0200559 phy_set_max_speed(phydev, SPEED_100);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100560
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500561 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
Andrew Lunn41124fa2018-09-12 01:53:14 +0200562 phy_remove_link_mode(phydev,
563 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100564
frederic RODO6c36a702007-07-12 19:07:24 +0200565 bp->link = 0;
566 bp->speed = 0;
567 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200568
569 return 0;
570}
571
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100572static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200573{
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200574 struct device_node *np;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200575 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200576
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200577 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200578 macb_writel(bp, NCR, MACB_BIT(MPE));
579
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700580 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700581 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200582 err = -ENOMEM;
583 goto err_out;
584 }
585
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700586 bp->mii_bus->name = "MACB_mii_bus";
587 bp->mii_bus->read = &macb_mdio_read;
588 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000589 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700590 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700591 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700592 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700593
Jamie Iles91523942011-02-28 04:05:25 +0000594 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200595
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200596 np = bp->pdev->dev.of_node;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200597 if (np && of_phy_is_fixed_link(np)) {
598 if (of_phy_register_fixed_link(np) < 0) {
599 dev_err(&bp->pdev->dev,
600 "broken fixed-link specification %pOF\n", np);
601 goto err_out_free_mdiobus;
602 }
Brad Mouring739de9a2018-03-13 16:32:13 -0500603
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200604 err = mdiobus_register(bp->mii_bus);
605 } else {
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200606 err = of_mdiobus_register(bp->mii_bus, np);
607 }
608
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200609 if (err)
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200610 goto err_out_free_fixed_link;
frederic RODO6c36a702007-07-12 19:07:24 +0200611
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200612 err = macb_mii_probe(bp->dev);
613 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200614 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200615
616 return 0;
617
618err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700619 mdiobus_unregister(bp->mii_bus);
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200620err_out_free_fixed_link:
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100621 if (np && of_phy_is_fixed_link(np))
622 of_phy_deregister_fixed_link(np);
Brad Mouring739de9a2018-03-13 16:32:13 -0500623err_out_free_mdiobus:
624 of_node_put(bp->phy_node);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700625 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200626err_out:
627 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100628}
629
630static void macb_update_stats(struct macb *bp)
631{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000632 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
633 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300634 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100635
636 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
637
Moritz Fischer96ec6312016-03-29 19:11:11 -0700638 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700639 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100640}
641
Nicolas Ferree86cd532012-10-31 06:04:57 +0000642static int macb_halt_tx(struct macb *bp)
643{
644 unsigned long halt_time, timeout;
645 u32 status;
646
647 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
648
649 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
650 do {
651 halt_time = jiffies;
652 status = macb_readl(bp, TSR);
653 if (!(status & MACB_BIT(TGO)))
654 return 0;
655
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800656 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000657 } while (time_before(halt_time, timeout));
658
659 return -ETIMEDOUT;
660}
661
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200662static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
663{
664 if (tx_skb->mapping) {
665 if (tx_skb->mapped_as_page)
666 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
667 tx_skb->size, DMA_TO_DEVICE);
668 else
669 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
670 tx_skb->size, DMA_TO_DEVICE);
671 tx_skb->mapping = 0;
672 }
673
674 if (tx_skb->skb) {
675 dev_kfree_skb_any(tx_skb->skb);
676 tx_skb->skb = NULL;
677 }
678}
679
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000680static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530681{
Harini Katakamfff80192016-08-09 13:15:53 +0530682#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000683 struct macb_dma_desc_64 *desc_64;
684
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100685 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000686 desc_64 = macb_64b_desc(bp, desc);
687 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200688 /* The low bits of RX address contain the RX_USED bit, clearing
689 * of which allows packet RX. Make sure the high bits are also
690 * visible to HW at that point.
691 */
692 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000693 }
Harini Katakamfff80192016-08-09 13:15:53 +0530694#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000695 desc->addr = lower_32_bits(addr);
696}
697
698static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
699{
700 dma_addr_t addr = 0;
701#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
702 struct macb_dma_desc_64 *desc_64;
703
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100704 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000705 desc_64 = macb_64b_desc(bp, desc);
706 addr = ((u64)(desc_64->addrh) << 32);
707 }
708#endif
709 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
710 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530711}
712
Nicolas Ferree86cd532012-10-31 06:04:57 +0000713static void macb_tx_error_task(struct work_struct *work)
714{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100715 struct macb_queue *queue = container_of(work, struct macb_queue,
716 tx_error_task);
717 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000718 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100719 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000720 struct sk_buff *skb;
721 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100722 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000723
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100724 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
725 (unsigned int)(queue - bp->queues),
726 queue->tx_tail, queue->tx_head);
727
728 /* Prevent the queue IRQ handlers from running: each of them may call
729 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
730 * As explained below, we have to halt the transmission before updating
731 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
732 * network engine about the macb/gem being halted.
733 */
734 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000735
736 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100737 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000738
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700739 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000740 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100741 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000742 */
743 if (macb_halt_tx(bp))
744 /* Just complain for now, reinitializing TX path can be good */
745 netdev_err(bp->dev, "BUG: halt tx timed out\n");
746
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700747 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000748 * Free transmit buffers in upper layer.
749 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100750 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
751 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000752
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100753 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000754 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100755 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000756 skb = tx_skb->skb;
757
758 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200759 /* skb is set for the last buffer of the frame */
760 while (!skb) {
761 macb_tx_unmap(bp, tx_skb);
762 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100763 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200764 skb = tx_skb->skb;
765 }
766
767 /* ctrl still refers to the first buffer descriptor
768 * since it's the only one written back by the hardware
769 */
770 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
771 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500772 macb_tx_ring_wrap(bp, tail),
773 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200774 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000775 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200776 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000777 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200778 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000779 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700780 /* "Buffers exhausted mid-frame" errors may only happen
781 * if the driver is buggy, so complain loudly about
782 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000783 */
784 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
785 netdev_err(bp->dev,
786 "BUG: TX buffers exhausted mid-frame\n");
787
788 desc->ctrl = ctrl | MACB_BIT(TX_USED);
789 }
790
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200791 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000792 }
793
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100794 /* Set end of TX queue */
795 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000796 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100797 desc->ctrl = MACB_BIT(TX_USED);
798
Nicolas Ferree86cd532012-10-31 06:04:57 +0000799 /* Make descriptor updates visible to hardware */
800 wmb();
801
802 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000803 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530804#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100805 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000806 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530807#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000808 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100809 queue->tx_head = 0;
810 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000811
812 /* Housework before enabling TX IRQ */
813 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100814 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
815
816 /* Now we are ready to start transmission again */
817 netif_tx_start_all_queues(bp->dev);
818 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
819
820 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000821}
822
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100823static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100824{
825 unsigned int tail;
826 unsigned int head;
827 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100828 struct macb *bp = queue->bp;
829 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100830
831 status = macb_readl(bp, TSR);
832 macb_writel(bp, TSR, status);
833
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000834 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100835 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000836
Nicolas Ferree86cd532012-10-31 06:04:57 +0000837 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700838 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100839
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100840 head = queue->tx_head;
841 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000842 struct macb_tx_skb *tx_skb;
843 struct sk_buff *skb;
844 struct macb_dma_desc *desc;
845 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100847 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100848
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000849 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100850 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000851
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000852 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100853
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200854 /* TX_USED bit is only set by hardware on the very first buffer
855 * descriptor of the transmitted frame.
856 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000857 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100858 break;
859
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200860 /* Process all buffers of the current transmitted frame */
861 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100862 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200863 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000864
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200865 /* First, update TX stats if needed */
866 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -0400867 if (unlikely(skb_shinfo(skb)->tx_flags &
868 SKBTX_HW_TSTAMP) &&
869 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100870 /* skb now belongs to timestamp buffer
871 * and will be removed later
872 */
873 tx_skb->skb = NULL;
874 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200875 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500876 macb_tx_ring_wrap(bp, tail),
877 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200878 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000879 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200880 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000881 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200882 }
883
884 /* Now we can safely release resources */
885 macb_tx_unmap(bp, tx_skb);
886
887 /* skb is set only for the last buffer of the frame.
888 * WARNING: at this point skb has been freed by
889 * macb_tx_unmap().
890 */
891 if (skb)
892 break;
893 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100894 }
895
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100896 queue->tx_tail = tail;
897 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
898 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500899 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100900 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100901}
902
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000903static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000904{
905 unsigned int entry;
906 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000907 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000908 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000909 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000910
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000911 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
912 bp->rx_ring_size) > 0) {
913 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000914
915 /* Make hw descriptor updates visible to CPU */
916 rmb();
917
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000918 queue->rx_prepared_head++;
919 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000920
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000921 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000922 /* allocate sk_buff for this free entry in ring */
923 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700924 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000925 netdev_err(bp->dev,
926 "Unable to allocate sk_buff\n");
927 break;
928 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000929
930 /* now fill corresponding descriptor entry */
931 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700932 bp->rx_buffer_size,
933 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800934 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
935 dev_kfree_skb(skb);
936 break;
937 }
938
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000939 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000940
Zach Brownb410d132016-10-19 09:56:57 -0500941 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000942 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000943 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200944 /* Setting addr clears RX_USED and allows reception,
945 * make sure ctrl is cleared first to avoid a race.
946 */
947 dma_wmb();
948 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000949
950 /* properly align Ethernet header */
951 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530952 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000953 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200954 dma_wmb();
955 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000956 }
957 }
958
959 /* Make descriptor updates visible to hardware */
960 wmb();
961
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000962 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
963 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000964}
965
966/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000967static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000968 unsigned int end)
969{
970 unsigned int frag;
971
972 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000973 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700974
Nicolas Ferre4df95132013-06-04 21:57:12 +0000975 desc->addr &= ~MACB_BIT(RX_USED);
976 }
977
978 /* Make descriptor updates visible to hardware */
979 wmb();
980
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700981 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000982 * whatever caused this is updated, so we don't have to record
983 * anything.
984 */
985}
986
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000987static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000988{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000989 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000990 unsigned int len;
991 unsigned int entry;
992 struct sk_buff *skb;
993 struct macb_dma_desc *desc;
994 int count = 0;
995
996 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530997 u32 ctrl;
998 dma_addr_t addr;
999 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001000
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001001 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1002 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001003
1004 /* Make hw descriptor updates visible to CPU */
1005 rmb();
1006
Harini Katakamfff80192016-08-09 13:15:53 +05301007 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001008 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001009
Harini Katakamfff80192016-08-09 13:15:53 +05301010 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001011 break;
1012
Anssi Hannula6e0af292018-12-17 15:05:41 +02001013 /* Ensure ctrl is at least as up-to-date as rxused */
1014 dma_rmb();
1015
1016 ctrl = desc->ctrl;
1017
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001018 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001019 count++;
1020
1021 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1022 netdev_err(bp->dev,
1023 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001024 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001025 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001026 break;
1027 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001028 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001029 if (unlikely(!skb)) {
1030 netdev_err(bp->dev,
1031 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001032 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001033 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001034 break;
1035 }
1036 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001037 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301038 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001039
1040 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1041
1042 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001043 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001044 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001045
1046 skb->protocol = eth_type_trans(skb, bp->dev);
1047 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001048 if (bp->dev->features & NETIF_F_RXCSUM &&
1049 !(bp->dev->flags & IFF_PROMISC) &&
1050 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1051 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001052
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001053 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001054 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001055 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001056 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001057
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001058 gem_ptp_do_rxstamp(bp, skb, desc);
1059
Nicolas Ferre4df95132013-06-04 21:57:12 +00001060#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1061 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1062 skb->len, skb->csum);
1063 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001064 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001065 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1066 skb->data, 32, true);
1067#endif
1068
1069 netif_receive_skb(skb);
1070 }
1071
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001072 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001073
1074 return count;
1075}
1076
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001077static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078 unsigned int last_frag)
1079{
1080 unsigned int len;
1081 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001082 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001084 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001085 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001086
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001087 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301088 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001089
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001090 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001091 macb_rx_ring_wrap(bp, first_frag),
1092 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001094 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001095 * first buffer. Since the header is 14 bytes, this makes the
1096 * payload word-aligned.
1097 *
1098 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1099 * the two padding bytes into the skb so that we avoid hitting
1100 * the slowpath in memcpy(), and pull them off afterwards.
1101 */
1102 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001103 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001104 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001105 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001106 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001107 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001108 if (frag == last_frag)
1109 break;
1110 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001111
1112 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001113 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001114
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001115 return 1;
1116 }
1117
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001118 offset = 0;
1119 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001120 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001121 skb_put(skb, len);
1122
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001123 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001124 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001125
1126 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001127 if (unlikely(frag != last_frag)) {
1128 dev_kfree_skb_any(skb);
1129 return -1;
1130 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131 frag_len = len - offset;
1132 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001133 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001134 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001135 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001136 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001137 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001138 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001139
1140 if (frag == last_frag)
1141 break;
1142 }
1143
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001144 /* Make descriptor updates visible to hardware */
1145 wmb();
1146
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001147 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001148 skb->protocol = eth_type_trans(skb, bp->dev);
1149
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001150 bp->dev->stats.rx_packets++;
1151 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001152 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001153 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001154 netif_receive_skb(skb);
1155
1156 return 0;
1157}
1158
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001159static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001160{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001161 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001162 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001163 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001164 int i;
1165
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001166 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001167 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001168 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001169 macb_set_addr(bp, desc, addr);
1170 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001171 addr += bp->rx_buffer_size;
1172 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001173 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001174 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001175}
1176
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001177static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001178{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001179 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001180 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001181 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001182 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001183 int first_frag = -1;
1184
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001185 for (tail = queue->rx_tail; budget > 0; tail++) {
1186 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001187 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001188
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001189 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001190 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001191
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001192 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001193 break;
1194
Anssi Hannula6e0af292018-12-17 15:05:41 +02001195 /* Ensure ctrl is at least as up-to-date as addr */
1196 dma_rmb();
1197
1198 ctrl = desc->ctrl;
1199
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001200 if (ctrl & MACB_BIT(RX_SOF)) {
1201 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001202 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203 first_frag = tail;
1204 }
1205
1206 if (ctrl & MACB_BIT(RX_EOF)) {
1207 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001208
1209 if (unlikely(first_frag == -1)) {
1210 reset_rx_queue = true;
1211 continue;
1212 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001213
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001214 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001215 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001216 if (unlikely(dropped < 0)) {
1217 reset_rx_queue = true;
1218 continue;
1219 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001220 if (!dropped) {
1221 received++;
1222 budget--;
1223 }
1224 }
1225 }
1226
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001227 if (unlikely(reset_rx_queue)) {
1228 unsigned long flags;
1229 u32 ctrl;
1230
1231 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1232
1233 spin_lock_irqsave(&bp->lock, flags);
1234
1235 ctrl = macb_readl(bp, NCR);
1236 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1237
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001238 macb_init_rx_ring(queue);
1239 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001240
1241 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1242
1243 spin_unlock_irqrestore(&bp->lock, flags);
1244 return received;
1245 }
1246
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001248 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001249 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001250 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001251
1252 return received;
1253}
1254
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001255static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001256{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001257 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1258 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001259 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001260 u32 status;
1261
1262 status = macb_readl(bp, RSR);
1263 macb_writel(bp, RSR, status);
1264
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001265 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001266 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001267
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001268 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001269 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001270 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001271
Nicolas Ferre8770e912013-02-12 11:08:48 +01001272 /* Packets received while interrupts were disabled */
1273 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001274 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001275 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001276 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001277 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001278 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301279 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001280 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001281 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001282
1283 /* TODO: Handle errors */
1284
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001285 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001286}
1287
Harini Katakam032dc412018-01-27 12:09:01 +05301288static void macb_hresp_error_task(unsigned long data)
1289{
1290 struct macb *bp = (struct macb *)data;
1291 struct net_device *dev = bp->dev;
1292 struct macb_queue *queue = bp->queues;
1293 unsigned int q;
1294 u32 ctrl;
1295
1296 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301297 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301298 MACB_TX_INT_FLAGS |
1299 MACB_BIT(HRESP));
1300 }
1301 ctrl = macb_readl(bp, NCR);
1302 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1303 macb_writel(bp, NCR, ctrl);
1304
1305 netif_tx_stop_all_queues(dev);
1306 netif_carrier_off(dev);
1307
1308 bp->macbgem_ops.mog_init_rings(bp);
1309
1310 /* Initialize TX and RX buffers */
1311 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1312 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1313#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1314 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1315 queue_writel(queue, RBQPH,
1316 upper_32_bits(queue->rx_ring_dma));
1317#endif
1318 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1319#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1320 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1321 queue_writel(queue, TBQPH,
1322 upper_32_bits(queue->tx_ring_dma));
1323#endif
1324
1325 /* Enable interrupts */
1326 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301327 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301328 MACB_TX_INT_FLAGS |
1329 MACB_BIT(HRESP));
1330 }
1331
1332 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1333 macb_writel(bp, NCR, ctrl);
1334
1335 netif_carrier_on(dev);
1336 netif_tx_start_all_queues(dev);
1337}
1338
Claudiu Beznea42983882018-12-17 10:02:42 +00001339static void macb_tx_restart(struct macb_queue *queue)
1340{
1341 unsigned int head = queue->tx_head;
1342 unsigned int tail = queue->tx_tail;
1343 struct macb *bp = queue->bp;
1344
1345 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1346 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1347
1348 if (head == tail)
1349 return;
1350
1351 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1352}
1353
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001354static irqreturn_t macb_interrupt(int irq, void *dev_id)
1355{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001356 struct macb_queue *queue = dev_id;
1357 struct macb *bp = queue->bp;
1358 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001359 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001360
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001361 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362
1363 if (unlikely(!status))
1364 return IRQ_NONE;
1365
1366 spin_lock(&bp->lock);
1367
1368 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001369 /* close possible race with dev_close */
1370 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001371 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001372 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1373 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001374 break;
1375 }
1376
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001377 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1378 (unsigned int)(queue - bp->queues),
1379 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001380
Harini Katakame5010702019-01-29 15:20:03 +05301381 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001382 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001383 * until we have processed the buffers. The
1384 * scheduling call may fail if the poll routine
1385 * is already scheduled, so disable interrupts
1386 * now.
1387 */
Harini Katakame5010702019-01-29 15:20:03 +05301388 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001389 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001390 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001391
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001392 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001393 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001394 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001395 }
1396 }
1397
Nicolas Ferree86cd532012-10-31 06:04:57 +00001398 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001399 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1400 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001401
1402 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001403 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001404
Nicolas Ferree86cd532012-10-31 06:04:57 +00001405 break;
1406 }
1407
1408 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001409 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001410
Claudiu Beznea42983882018-12-17 10:02:42 +00001411 if (status & MACB_BIT(TXUBR))
1412 macb_tx_restart(queue);
1413
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001414 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001415 * add that if/when we get our hands on a full-blown MII PHY.
1416 */
1417
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001418 /* There is a hardware issue under heavy load where DMA can
1419 * stop, this causes endless "used buffer descriptor read"
1420 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301421 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1422 * section 16.7.4 for details. RXUBR is only enabled for
1423 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001424 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001425 if (status & MACB_BIT(RXUBR)) {
1426 ctrl = macb_readl(bp, NCR);
1427 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001428 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001429 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1430
1431 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001432 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001433 }
1434
Alexander Steinb19f7f72011-04-13 05:03:24 +00001435 if (status & MACB_BIT(ISR_ROVR)) {
1436 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001437 if (macb_is_gem(bp))
1438 bp->hw_stats.gem.rx_overruns++;
1439 else
1440 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001441
1442 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001443 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001444 }
1445
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001446 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301447 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001448 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001449
1450 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001451 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001452 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001453 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001454 }
1455
1456 spin_unlock(&bp->lock);
1457
1458 return IRQ_HANDLED;
1459}
1460
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001461#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001462/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001463 * to allow network i/o with interrupts disabled.
1464 */
1465static void macb_poll_controller(struct net_device *dev)
1466{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001467 struct macb *bp = netdev_priv(dev);
1468 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001469 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001470 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001471
1472 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001473 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1474 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001475 local_irq_restore(flags);
1476}
1477#endif
1478
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001479static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001480 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001481 struct sk_buff *skb,
1482 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001483{
1484 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001485 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001486 struct macb_tx_skb *tx_skb = NULL;
1487 struct macb_dma_desc *desc;
1488 unsigned int offset, size, count = 0;
1489 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001490 unsigned int eof = 1, mss_mfs = 0;
1491 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1492
1493 /* LSO */
1494 if (skb_shinfo(skb)->gso_size != 0) {
1495 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1496 /* UDP - UFO */
1497 lso_ctrl = MACB_LSO_UFO_ENABLE;
1498 else
1499 /* TCP - TSO */
1500 lso_ctrl = MACB_LSO_TSO_ENABLE;
1501 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001502
1503 /* First, map non-paged data */
1504 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001505
1506 /* first buffer length */
1507 size = hdrlen;
1508
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001509 offset = 0;
1510 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001511 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001512 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001513
1514 mapping = dma_map_single(&bp->pdev->dev,
1515 skb->data + offset,
1516 size, DMA_TO_DEVICE);
1517 if (dma_mapping_error(&bp->pdev->dev, mapping))
1518 goto dma_error;
1519
1520 /* Save info to properly release resources */
1521 tx_skb->skb = NULL;
1522 tx_skb->mapping = mapping;
1523 tx_skb->size = size;
1524 tx_skb->mapped_as_page = false;
1525
1526 len -= size;
1527 offset += size;
1528 count++;
1529 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001530
1531 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001532 }
1533
1534 /* Then, map paged data from fragments */
1535 for (f = 0; f < nr_frags; f++) {
1536 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1537
1538 len = skb_frag_size(frag);
1539 offset = 0;
1540 while (len) {
1541 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001542 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001543 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001544
1545 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1546 offset, size, DMA_TO_DEVICE);
1547 if (dma_mapping_error(&bp->pdev->dev, mapping))
1548 goto dma_error;
1549
1550 /* Save info to properly release resources */
1551 tx_skb->skb = NULL;
1552 tx_skb->mapping = mapping;
1553 tx_skb->size = size;
1554 tx_skb->mapped_as_page = true;
1555
1556 len -= size;
1557 offset += size;
1558 count++;
1559 tx_head++;
1560 }
1561 }
1562
1563 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001564 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001565 netdev_err(bp->dev, "BUG! empty skb!\n");
1566 return 0;
1567 }
1568
1569 /* This is the last buffer of the frame: save socket buffer */
1570 tx_skb->skb = skb;
1571
1572 /* Update TX ring: update buffer descriptors in reverse order
1573 * to avoid race condition
1574 */
1575
1576 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1577 * to set the end of TX queue
1578 */
1579 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001580 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001581 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001582 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001583 desc->ctrl = ctrl;
1584
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001585 if (lso_ctrl) {
1586 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1587 /* include header and FCS in value given to h/w */
1588 mss_mfs = skb_shinfo(skb)->gso_size +
1589 skb_transport_offset(skb) +
1590 ETH_FCS_LEN;
1591 else /* TSO */ {
1592 mss_mfs = skb_shinfo(skb)->gso_size;
1593 /* TCP Sequence Number Source Select
1594 * can be set only for TSO
1595 */
1596 seq_ctrl = 0;
1597 }
1598 }
1599
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001600 do {
1601 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001602 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001603 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001604 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001605
1606 ctrl = (u32)tx_skb->size;
1607 if (eof) {
1608 ctrl |= MACB_BIT(TX_LAST);
1609 eof = 0;
1610 }
Zach Brownb410d132016-10-19 09:56:57 -05001611 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001612 ctrl |= MACB_BIT(TX_WRAP);
1613
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001614 /* First descriptor is header descriptor */
1615 if (i == queue->tx_head) {
1616 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1617 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001618 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1619 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1620 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001621 } else
1622 /* Only set MSS/MFS on payload descriptors
1623 * (second or later descriptor)
1624 */
1625 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1626
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001627 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001628 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001629 /* desc->addr must be visible to hardware before clearing
1630 * 'TX_USED' bit in desc->ctrl.
1631 */
1632 wmb();
1633 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001634 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001635
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001636 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001637
1638 return count;
1639
1640dma_error:
1641 netdev_err(bp->dev, "TX DMA map failed\n");
1642
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001643 for (i = queue->tx_head; i != tx_head; i++) {
1644 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001645
1646 macb_tx_unmap(bp, tx_skb);
1647 }
1648
1649 return 0;
1650}
1651
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001652static netdev_features_t macb_features_check(struct sk_buff *skb,
1653 struct net_device *dev,
1654 netdev_features_t features)
1655{
1656 unsigned int nr_frags, f;
1657 unsigned int hdrlen;
1658
1659 /* Validate LSO compatibility */
1660
1661 /* there is only one buffer */
1662 if (!skb_is_nonlinear(skb))
1663 return features;
1664
1665 /* length of header */
1666 hdrlen = skb_transport_offset(skb);
1667 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1668 hdrlen += tcp_hdrlen(skb);
1669
1670 /* For LSO:
1671 * When software supplies two or more payload buffers all payload buffers
1672 * apart from the last must be a multiple of 8 bytes in size.
1673 */
1674 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1675 return features & ~MACB_NETIF_LSO;
1676
1677 nr_frags = skb_shinfo(skb)->nr_frags;
1678 /* No need to check last fragment */
1679 nr_frags--;
1680 for (f = 0; f < nr_frags; f++) {
1681 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1682
1683 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1684 return features & ~MACB_NETIF_LSO;
1685 }
1686 return features;
1687}
1688
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001689static inline int macb_clear_csum(struct sk_buff *skb)
1690{
1691 /* no change for packets without checksum offloading */
1692 if (skb->ip_summed != CHECKSUM_PARTIAL)
1693 return 0;
1694
1695 /* make sure we can modify the header */
1696 if (unlikely(skb_cow_head(skb, 0)))
1697 return -1;
1698
1699 /* initialize checksum field
1700 * This is required - at least for Zynq, which otherwise calculates
1701 * wrong UDP header checksums for UDP packets with UDP data len <=2
1702 */
1703 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1704 return 0;
1705}
1706
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001707static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1708{
1709 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1710 int padlen = ETH_ZLEN - (*skb)->len;
1711 int headroom = skb_headroom(*skb);
1712 int tailroom = skb_tailroom(*skb);
1713 struct sk_buff *nskb;
1714 u32 fcs;
1715
1716 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1717 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1718 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1719 return 0;
1720
1721 if (padlen <= 0) {
1722 /* FCS could be appeded to tailroom. */
1723 if (tailroom >= ETH_FCS_LEN)
1724 goto add_fcs;
1725 /* FCS could be appeded by moving data to headroom. */
1726 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1727 padlen = 0;
1728 /* No room for FCS, need to reallocate skb. */
1729 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001730 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001731 } else {
1732 /* Add room for FCS. */
1733 padlen += ETH_FCS_LEN;
1734 }
1735
1736 if (!cloned && headroom + tailroom >= padlen) {
1737 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1738 skb_set_tail_pointer(*skb, (*skb)->len);
1739 } else {
1740 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1741 if (!nskb)
1742 return -ENOMEM;
1743
Huang Zijiangf3e5c072019-02-14 14:41:18 +08001744 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001745 *skb = nskb;
1746 }
1747
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001748 if (padlen > ETH_FCS_LEN)
1749 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001750
1751add_fcs:
1752 /* set FCS to packet */
1753 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1754 fcs = ~fcs;
1755
1756 skb_put_u8(*skb, fcs & 0xff);
1757 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1758 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1759 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1760
1761 return 0;
1762}
1763
Claudiu Beznead1c38952018-08-07 12:25:12 +03001764static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001765{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001766 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001767 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001768 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001769 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001770 unsigned int desc_cnt, nr_frags, frag_size, f;
1771 unsigned int hdrlen;
1772 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001773 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001774
Claudiu Beznea33729f22018-08-07 12:25:13 +03001775 if (macb_clear_csum(skb)) {
1776 dev_kfree_skb_any(skb);
1777 return ret;
1778 }
1779
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001780 if (macb_pad_and_fcs(&skb, dev)) {
1781 dev_kfree_skb_any(skb);
1782 return ret;
1783 }
1784
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001785 is_lso = (skb_shinfo(skb)->gso_size != 0);
1786
1787 if (is_lso) {
1788 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1789
1790 /* length of headers */
1791 if (is_udp)
1792 /* only queue eth + ip headers separately for UDP */
1793 hdrlen = skb_transport_offset(skb);
1794 else
1795 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1796 if (skb_headlen(skb) < hdrlen) {
1797 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1798 /* if this is required, would need to copy to single buffer */
1799 return NETDEV_TX_BUSY;
1800 }
1801 } else
1802 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001803
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001804#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1805 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001806 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1807 queue_index, skb->len, skb->head, skb->data,
1808 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001809 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1810 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001811#endif
1812
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001813 /* Count how many TX buffer descriptors are needed to send this
1814 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001815 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001816 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001817 if (is_lso && (skb_headlen(skb) > hdrlen))
1818 /* extra header descriptor if also payload in first buffer */
1819 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1820 else
1821 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001822 nr_frags = skb_shinfo(skb)->nr_frags;
1823 for (f = 0; f < nr_frags; f++) {
1824 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001825 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001826 }
1827
Dongdong Deng48719532009-08-23 19:49:07 -07001828 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001829
1830 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001831 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001832 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001833 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001834 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001835 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001836 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001837 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001838 }
1839
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001840 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001841 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001842 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001843 goto unlock;
1844 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001845
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001846 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001847 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001848 skb_tx_timestamp(skb);
1849
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001850 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1851
Zach Brownb410d132016-10-19 09:56:57 -05001852 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001853 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001854
Soren Brinkmann92030902014-03-04 08:46:39 -08001855unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001856 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001857
Claudiu Beznead1c38952018-08-07 12:25:12 +03001858 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001859}
1860
Nicolas Ferre4df95132013-06-04 21:57:12 +00001861static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001862{
1863 if (!macb_is_gem(bp)) {
1864 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1865 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001866 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001867
Nicolas Ferre1b447912013-06-04 21:57:11 +00001868 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001869 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001870 "RX buffer must be multiple of %d bytes, expanding\n",
1871 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001872 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001873 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001874 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001875 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001876
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001877 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001878 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001879}
1880
Nicolas Ferre4df95132013-06-04 21:57:12 +00001881static void gem_free_rx_buffers(struct macb *bp)
1882{
1883 struct sk_buff *skb;
1884 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001885 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001886 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001887 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001888 int i;
1889
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001890 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1891 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001892 continue;
1893
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001894 for (i = 0; i < bp->rx_ring_size; i++) {
1895 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001896
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001897 if (!skb)
1898 continue;
1899
1900 desc = macb_rx_desc(queue, i);
1901 addr = macb_get_addr(bp, desc);
1902
1903 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1904 DMA_FROM_DEVICE);
1905 dev_kfree_skb_any(skb);
1906 skb = NULL;
1907 }
1908
1909 kfree(queue->rx_skbuff);
1910 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001911 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001912}
1913
1914static void macb_free_rx_buffers(struct macb *bp)
1915{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001916 struct macb_queue *queue = &bp->queues[0];
1917
1918 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001919 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001920 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001921 queue->rx_buffers, queue->rx_buffers_dma);
1922 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001923 }
1924}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001925
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001926static void macb_free_consistent(struct macb *bp)
1927{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001928 struct macb_queue *queue;
1929 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301930 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001931
Nicolas Ferre4df95132013-06-04 21:57:12 +00001932 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001933
1934 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1935 kfree(queue->tx_skb);
1936 queue->tx_skb = NULL;
1937 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301938 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1939 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001940 queue->tx_ring, queue->tx_ring_dma);
1941 queue->tx_ring = NULL;
1942 }
Harini Katakame50b7702018-07-06 12:18:57 +05301943 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301944 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1945 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301946 queue->rx_ring, queue->rx_ring_dma);
1947 queue->rx_ring = NULL;
1948 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001949 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001950}
1951
1952static int gem_alloc_rx_buffers(struct macb *bp)
1953{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001954 struct macb_queue *queue;
1955 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001956 int size;
1957
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001958 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1959 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1960 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1961 if (!queue->rx_skbuff)
1962 return -ENOMEM;
1963 else
1964 netdev_dbg(bp->dev,
1965 "Allocated %d RX struct sk_buff entries at %p\n",
1966 bp->rx_ring_size, queue->rx_skbuff);
1967 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001968 return 0;
1969}
1970
1971static int macb_alloc_rx_buffers(struct macb *bp)
1972{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001973 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001974 int size;
1975
Zach Brownb410d132016-10-19 09:56:57 -05001976 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001977 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1978 &queue->rx_buffers_dma, GFP_KERNEL);
1979 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001980 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001981
1982 netdev_dbg(bp->dev,
1983 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001984 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001985 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001986}
1987
1988static int macb_alloc_consistent(struct macb *bp)
1989{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001990 struct macb_queue *queue;
1991 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992 int size;
1993
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001994 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05301995 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001996 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1997 &queue->tx_ring_dma,
1998 GFP_KERNEL);
1999 if (!queue->tx_ring)
2000 goto out_err;
2001 netdev_dbg(bp->dev,
2002 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2003 q, size, (unsigned long)queue->tx_ring_dma,
2004 queue->tx_ring);
2005
Zach Brownb410d132016-10-19 09:56:57 -05002006 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002007 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2008 if (!queue->tx_skb)
2009 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002010
Harini Katakam404cd082018-07-06 12:18:58 +05302011 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002012 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2013 &queue->rx_ring_dma, GFP_KERNEL);
2014 if (!queue->rx_ring)
2015 goto out_err;
2016 netdev_dbg(bp->dev,
2017 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2018 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002019 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002020 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002021 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002022
2023 return 0;
2024
2025out_err:
2026 macb_free_consistent(bp);
2027 return -ENOMEM;
2028}
2029
Nicolas Ferre4df95132013-06-04 21:57:12 +00002030static void gem_init_rings(struct macb *bp)
2031{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002032 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002033 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002034 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002035 int i;
2036
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002037 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002038 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002039 desc = macb_tx_desc(queue, i);
2040 macb_set_addr(bp, desc, 0);
2041 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002042 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002043 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002044 queue->tx_head = 0;
2045 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002046
2047 queue->rx_tail = 0;
2048 queue->rx_prepared_head = 0;
2049
2050 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002051 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002052
Nicolas Ferre4df95132013-06-04 21:57:12 +00002053}
2054
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002055static void macb_init_rings(struct macb *bp)
2056{
2057 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002058 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002059
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002060 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002061
Zach Brownb410d132016-10-19 09:56:57 -05002062 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002063 desc = macb_tx_desc(&bp->queues[0], i);
2064 macb_set_addr(bp, desc, 0);
2065 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002066 }
Ben Shelton21d35152015-04-22 17:28:54 -05002067 bp->queues[0].tx_head = 0;
2068 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002069 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002070}
2071
2072static void macb_reset_hw(struct macb *bp)
2073{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002074 struct macb_queue *queue;
2075 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002076 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002077
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002078 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002079 * more gracefully?)
2080 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002081 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002082
2083 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002084 ctrl |= MACB_BIT(CLRSTAT);
2085
2086 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002087
2088 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002089 macb_writel(bp, TSR, -1);
2090 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002091
2092 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002093 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2094 queue_writel(queue, IDR, -1);
2095 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002096 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2097 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002098 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002099}
2100
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002101static u32 gem_mdc_clk_div(struct macb *bp)
2102{
2103 u32 config;
2104 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2105
2106 if (pclk_hz <= 20000000)
2107 config = GEM_BF(CLK, GEM_CLK_DIV8);
2108 else if (pclk_hz <= 40000000)
2109 config = GEM_BF(CLK, GEM_CLK_DIV16);
2110 else if (pclk_hz <= 80000000)
2111 config = GEM_BF(CLK, GEM_CLK_DIV32);
2112 else if (pclk_hz <= 120000000)
2113 config = GEM_BF(CLK, GEM_CLK_DIV48);
2114 else if (pclk_hz <= 160000000)
2115 config = GEM_BF(CLK, GEM_CLK_DIV64);
2116 else
2117 config = GEM_BF(CLK, GEM_CLK_DIV96);
2118
2119 return config;
2120}
2121
2122static u32 macb_mdc_clk_div(struct macb *bp)
2123{
2124 u32 config;
2125 unsigned long pclk_hz;
2126
2127 if (macb_is_gem(bp))
2128 return gem_mdc_clk_div(bp);
2129
2130 pclk_hz = clk_get_rate(bp->pclk);
2131 if (pclk_hz <= 20000000)
2132 config = MACB_BF(CLK, MACB_CLK_DIV8);
2133 else if (pclk_hz <= 40000000)
2134 config = MACB_BF(CLK, MACB_CLK_DIV16);
2135 else if (pclk_hz <= 80000000)
2136 config = MACB_BF(CLK, MACB_CLK_DIV32);
2137 else
2138 config = MACB_BF(CLK, MACB_CLK_DIV64);
2139
2140 return config;
2141}
2142
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002143/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002144 * should program. We find the width from decoding the design configuration
2145 * register to find the maximum supported data bus width.
2146 */
2147static u32 macb_dbw(struct macb *bp)
2148{
2149 if (!macb_is_gem(bp))
2150 return 0;
2151
2152 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2153 case 4:
2154 return GEM_BF(DBW, GEM_DBW128);
2155 case 2:
2156 return GEM_BF(DBW, GEM_DBW64);
2157 case 1:
2158 default:
2159 return GEM_BF(DBW, GEM_DBW32);
2160 }
2161}
2162
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002163/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002164 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002165 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002166 * (if not supported by FIFO, it will fallback to default)
2167 * - set both rx/tx packet buffers to full memory size
2168 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002169 */
2170static void macb_configure_dma(struct macb *bp)
2171{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002172 struct macb_queue *queue;
2173 u32 buffer_size;
2174 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002175 u32 dmacfg;
2176
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002177 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002178 if (macb_is_gem(bp)) {
2179 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002180 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2181 if (q)
2182 queue_writel(queue, RBQS, buffer_size);
2183 else
2184 dmacfg |= GEM_BF(RXBS, buffer_size);
2185 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002186 if (bp->dma_burst_length)
2187 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002188 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302189 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302190
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002191 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302192 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2193 else
2194 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2195
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002196 if (bp->dev->features & NETIF_F_HW_CSUM)
2197 dmacfg |= GEM_BIT(TXCOEN);
2198 else
2199 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302200
Michal Simekbd620722018-09-25 08:32:50 +02002201 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302202#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002203 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002204 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302205#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002206#ifdef CONFIG_MACB_USE_HWSTAMP
2207 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2208 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2209#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002210 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2211 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002212 gem_writel(bp, DMACFG, dmacfg);
2213 }
2214}
2215
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002216static void macb_init_hw(struct macb *bp)
2217{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002218 struct macb_queue *queue;
2219 unsigned int q;
2220
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002221 u32 config;
2222
2223 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002224 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002225
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002226 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302227 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2228 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002229 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002230 config |= MACB_BIT(PAE); /* PAuse Enable */
2231 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002232 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302233 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2234 else
2235 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236 if (bp->dev->flags & IFF_PROMISC)
2237 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002238 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2239 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002240 if (!(bp->dev->flags & IFF_BROADCAST))
2241 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002242 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002243 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002244 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302245 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002246 bp->speed = SPEED_10;
2247 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302248 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002249 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302250 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002251
Jamie Iles0116da42011-03-14 17:38:30 +00002252 macb_configure_dma(bp);
2253
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002254 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002255 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002256 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2257#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2258 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2259 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2260#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002261 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302262#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002263 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002264 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302265#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002266
2267 /* Enable interrupts */
2268 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05302269 bp->rx_intr_mask |
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002270 MACB_TX_INT_FLAGS |
2271 MACB_BIT(HRESP));
2272 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002273
2274 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002275 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002276}
2277
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002278/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002279 * locations in the memory map. The least significant bits are stored
2280 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2281 *
2282 * The unicast hash enable and the multicast hash enable bits in the
2283 * network configuration register enable the reception of hash matched
2284 * frames. The destination address is reduced to a 6 bit index into
2285 * the 64 bit hash register using the following hash function. The
2286 * hash function is an exclusive or of every sixth bit of the
2287 * destination address.
2288 *
2289 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2290 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2291 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2292 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2293 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2294 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2295 *
2296 * da[0] represents the least significant bit of the first byte
2297 * received, that is, the multicast/unicast indicator, and da[47]
2298 * represents the most significant bit of the last byte received. If
2299 * the hash index, hi[n], points to a bit that is set in the hash
2300 * register then the frame will be matched according to whether the
2301 * frame is multicast or unicast. A multicast match will be signalled
2302 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2303 * index points to a bit set in the hash register. A unicast match
2304 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2305 * and the hash index points to a bit set in the hash register. To
2306 * receive all multicast frames, the hash register should be set with
2307 * all ones and the multicast hash enable bit should be set in the
2308 * network configuration register.
2309 */
2310
2311static inline int hash_bit_value(int bitnr, __u8 *addr)
2312{
2313 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2314 return 1;
2315 return 0;
2316}
2317
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002318/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002319static int hash_get_index(__u8 *addr)
2320{
2321 int i, j, bitval;
2322 int hash_index = 0;
2323
2324 for (j = 0; j < 6; j++) {
2325 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002326 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002327
2328 hash_index |= (bitval << j);
2329 }
2330
2331 return hash_index;
2332}
2333
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002334/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002335static void macb_sethashtable(struct net_device *dev)
2336{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002337 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002338 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002339 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002340 struct macb *bp = netdev_priv(dev);
2341
Moritz Fischeraa50b552016-03-29 19:11:13 -07002342 mc_filter[0] = 0;
2343 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002344
Jiri Pirko22bedad32010-04-01 21:22:57 +00002345 netdev_for_each_mc_addr(ha, dev) {
2346 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002347 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2348 }
2349
Jamie Ilesf75ba502011-11-08 10:12:32 +00002350 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2351 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002352}
2353
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002354/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002355static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002356{
2357 unsigned long cfg;
2358 struct macb *bp = netdev_priv(dev);
2359
2360 cfg = macb_readl(bp, NCFGR);
2361
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002362 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002363 /* Enable promiscuous mode */
2364 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002365
2366 /* Disable RX checksum offload */
2367 if (macb_is_gem(bp))
2368 cfg &= ~GEM_BIT(RXCOEN);
2369 } else {
2370 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002371 cfg &= ~MACB_BIT(CAF);
2372
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002373 /* Enable RX checksum offload only if requested */
2374 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2375 cfg |= GEM_BIT(RXCOEN);
2376 }
2377
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002378 if (dev->flags & IFF_ALLMULTI) {
2379 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002380 macb_or_gem_writel(bp, HRB, -1);
2381 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002382 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002383 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002384 /* Enable specific multicasts */
2385 macb_sethashtable(dev);
2386 cfg |= MACB_BIT(NCFGR_MTI);
2387 } else if (dev->flags & (~IFF_ALLMULTI)) {
2388 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002389 macb_or_gem_writel(bp, HRB, 0);
2390 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002391 cfg &= ~MACB_BIT(NCFGR_MTI);
2392 }
2393
2394 macb_writel(bp, NCFGR, cfg);
2395}
2396
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002397static int macb_open(struct net_device *dev)
2398{
2399 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002400 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002401 struct macb_queue *queue;
2402 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002403 int err;
2404
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002405 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002406
Harini Katakamd54f89a2019-03-01 16:20:34 +05302407 err = pm_runtime_get_sync(&bp->pdev->dev);
2408 if (err < 0)
2409 goto pm_exit;
2410
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002411 /* carrier starts down */
2412 netif_carrier_off(dev);
2413
frederic RODO6c36a702007-07-12 19:07:24 +02002414 /* if the phy is not yet register, retry later*/
Harini Katakamd54f89a2019-03-01 16:20:34 +05302415 if (!dev->phydev) {
2416 err = -EAGAIN;
2417 goto pm_exit;
2418 }
frederic RODO6c36a702007-07-12 19:07:24 +02002419
Nicolas Ferre1b447912013-06-04 21:57:11 +00002420 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002421 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002422
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002423 err = macb_alloc_consistent(bp);
2424 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002425 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2426 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302427 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002428 }
2429
Nicolas Ferre4df95132013-06-04 21:57:12 +00002430 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002431 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002432
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002433 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2434 napi_enable(&queue->napi);
2435
frederic RODO6c36a702007-07-12 19:07:24 +02002436 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002437 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002438
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002439 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002440
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002441 if (bp->ptp_info)
2442 bp->ptp_info->ptp_init(dev);
2443
Harini Katakamd54f89a2019-03-01 16:20:34 +05302444pm_exit:
2445 if (err) {
2446 pm_runtime_put_sync(&bp->pdev->dev);
2447 return err;
2448 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002449 return 0;
2450}
2451
2452static int macb_close(struct net_device *dev)
2453{
2454 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002455 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002456 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002457 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002458
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002459 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002460
2461 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2462 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002463
Philippe Reynes0a912812016-06-22 00:32:35 +02002464 if (dev->phydev)
2465 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002466
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002467 spin_lock_irqsave(&bp->lock, flags);
2468 macb_reset_hw(bp);
2469 netif_carrier_off(dev);
2470 spin_unlock_irqrestore(&bp->lock, flags);
2471
2472 macb_free_consistent(bp);
2473
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002474 if (bp->ptp_info)
2475 bp->ptp_info->ptp_remove(dev);
2476
Harini Katakamd54f89a2019-03-01 16:20:34 +05302477 pm_runtime_put(&bp->pdev->dev);
2478
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002479 return 0;
2480}
2481
Harini Katakama5898ea2015-05-06 22:27:18 +05302482static int macb_change_mtu(struct net_device *dev, int new_mtu)
2483{
Harini Katakama5898ea2015-05-06 22:27:18 +05302484 if (netif_running(dev))
2485 return -EBUSY;
2486
Harini Katakama5898ea2015-05-06 22:27:18 +05302487 dev->mtu = new_mtu;
2488
2489 return 0;
2490}
2491
Jamie Ilesa494ed82011-03-09 16:26:35 +00002492static void gem_update_stats(struct macb *bp)
2493{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002494 struct macb_queue *queue;
2495 unsigned int i, q, idx;
2496 unsigned long *stat;
2497
Jamie Ilesa494ed82011-03-09 16:26:35 +00002498 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002499
Xander Huff3ff13f12015-01-13 16:15:51 -06002500 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2501 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002502 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002503
2504 bp->ethtool_stats[i] += val;
2505 *p += val;
2506
2507 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2508 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002509 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002510 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002511 *(++p) += val;
2512 }
2513 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002514
2515 idx = GEM_STATS_LEN;
2516 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2517 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2518 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002519}
2520
2521static struct net_device_stats *gem_get_stats(struct macb *bp)
2522{
2523 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002524 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002525
2526 gem_update_stats(bp);
2527
2528 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2529 hwstat->rx_alignment_errors +
2530 hwstat->rx_resource_errors +
2531 hwstat->rx_overruns +
2532 hwstat->rx_oversize_frames +
2533 hwstat->rx_jabbers +
2534 hwstat->rx_undersized_frames +
2535 hwstat->rx_length_field_frame_errors);
2536 nstat->tx_errors = (hwstat->tx_late_collisions +
2537 hwstat->tx_excessive_collisions +
2538 hwstat->tx_underrun +
2539 hwstat->tx_carrier_sense_errors);
2540 nstat->multicast = hwstat->rx_multicast_frames;
2541 nstat->collisions = (hwstat->tx_single_collision_frames +
2542 hwstat->tx_multiple_collision_frames +
2543 hwstat->tx_excessive_collisions);
2544 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2545 hwstat->rx_jabbers +
2546 hwstat->rx_undersized_frames +
2547 hwstat->rx_length_field_frame_errors);
2548 nstat->rx_over_errors = hwstat->rx_resource_errors;
2549 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2550 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2551 nstat->rx_fifo_errors = hwstat->rx_overruns;
2552 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2553 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2554 nstat->tx_fifo_errors = hwstat->tx_underrun;
2555
2556 return nstat;
2557}
2558
Xander Huff3ff13f12015-01-13 16:15:51 -06002559static void gem_get_ethtool_stats(struct net_device *dev,
2560 struct ethtool_stats *stats, u64 *data)
2561{
2562 struct macb *bp;
2563
2564 bp = netdev_priv(dev);
2565 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002566 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2567 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002568}
2569
2570static int gem_get_sset_count(struct net_device *dev, int sset)
2571{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002572 struct macb *bp = netdev_priv(dev);
2573
Xander Huff3ff13f12015-01-13 16:15:51 -06002574 switch (sset) {
2575 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002576 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002577 default:
2578 return -EOPNOTSUPP;
2579 }
2580}
2581
2582static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2583{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002584 char stat_string[ETH_GSTRING_LEN];
2585 struct macb *bp = netdev_priv(dev);
2586 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002587 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002588 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002589
2590 switch (sset) {
2591 case ETH_SS_STATS:
2592 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2593 memcpy(p, gem_statistics[i].stat_string,
2594 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002595
2596 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2597 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2598 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2599 q, queue_statistics[i].stat_string);
2600 memcpy(p, stat_string, ETH_GSTRING_LEN);
2601 }
2602 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002603 break;
2604 }
2605}
2606
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002607static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002608{
2609 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002610 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002611 struct macb_stats *hwstat = &bp->hw_stats.macb;
2612
2613 if (macb_is_gem(bp))
2614 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002615
frederic RODO6c36a702007-07-12 19:07:24 +02002616 /* read stats from hardware */
2617 macb_update_stats(bp);
2618
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002619 /* Convert HW stats into netdevice stats */
2620 nstat->rx_errors = (hwstat->rx_fcs_errors +
2621 hwstat->rx_align_errors +
2622 hwstat->rx_resource_errors +
2623 hwstat->rx_overruns +
2624 hwstat->rx_oversize_pkts +
2625 hwstat->rx_jabbers +
2626 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002627 hwstat->rx_length_mismatch);
2628 nstat->tx_errors = (hwstat->tx_late_cols +
2629 hwstat->tx_excessive_cols +
2630 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002631 hwstat->tx_carrier_errors +
2632 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002633 nstat->collisions = (hwstat->tx_single_cols +
2634 hwstat->tx_multiple_cols +
2635 hwstat->tx_excessive_cols);
2636 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2637 hwstat->rx_jabbers +
2638 hwstat->rx_undersize_pkts +
2639 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002640 nstat->rx_over_errors = hwstat->rx_resource_errors +
2641 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002642 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2643 nstat->rx_frame_errors = hwstat->rx_align_errors;
2644 nstat->rx_fifo_errors = hwstat->rx_overruns;
2645 /* XXX: What does "missed" mean? */
2646 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2647 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2648 nstat->tx_fifo_errors = hwstat->tx_underruns;
2649 /* Don't know about heartbeat or window errors... */
2650
2651 return nstat;
2652}
2653
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002654static int macb_get_regs_len(struct net_device *netdev)
2655{
2656 return MACB_GREGS_NBR * sizeof(u32);
2657}
2658
2659static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2660 void *p)
2661{
2662 struct macb *bp = netdev_priv(dev);
2663 unsigned int tail, head;
2664 u32 *regs_buff = p;
2665
2666 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2667 | MACB_GREGS_VERSION;
2668
Zach Brownb410d132016-10-19 09:56:57 -05002669 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2670 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002671
2672 regs_buff[0] = macb_readl(bp, NCR);
2673 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2674 regs_buff[2] = macb_readl(bp, NSR);
2675 regs_buff[3] = macb_readl(bp, TSR);
2676 regs_buff[4] = macb_readl(bp, RBQP);
2677 regs_buff[5] = macb_readl(bp, TBQP);
2678 regs_buff[6] = macb_readl(bp, RSR);
2679 regs_buff[7] = macb_readl(bp, IMR);
2680
2681 regs_buff[8] = tail;
2682 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002683 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2684 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002685
Neil Armstrongce721a72016-01-05 14:39:16 +01002686 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2687 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002688 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002689 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002690}
2691
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002692static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2693{
2694 struct macb *bp = netdev_priv(netdev);
2695
2696 wol->supported = 0;
2697 wol->wolopts = 0;
2698
2699 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2700 wol->supported = WAKE_MAGIC;
2701
2702 if (bp->wol & MACB_WOL_ENABLED)
2703 wol->wolopts |= WAKE_MAGIC;
2704 }
2705}
2706
2707static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2708{
2709 struct macb *bp = netdev_priv(netdev);
2710
2711 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2712 (wol->wolopts & ~WAKE_MAGIC))
2713 return -EOPNOTSUPP;
2714
2715 if (wol->wolopts & WAKE_MAGIC)
2716 bp->wol |= MACB_WOL_ENABLED;
2717 else
2718 bp->wol &= ~MACB_WOL_ENABLED;
2719
2720 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2721
2722 return 0;
2723}
2724
Zach Brown8441bb32016-10-19 09:56:58 -05002725static void macb_get_ringparam(struct net_device *netdev,
2726 struct ethtool_ringparam *ring)
2727{
2728 struct macb *bp = netdev_priv(netdev);
2729
2730 ring->rx_max_pending = MAX_RX_RING_SIZE;
2731 ring->tx_max_pending = MAX_TX_RING_SIZE;
2732
2733 ring->rx_pending = bp->rx_ring_size;
2734 ring->tx_pending = bp->tx_ring_size;
2735}
2736
2737static int macb_set_ringparam(struct net_device *netdev,
2738 struct ethtool_ringparam *ring)
2739{
2740 struct macb *bp = netdev_priv(netdev);
2741 u32 new_rx_size, new_tx_size;
2742 unsigned int reset = 0;
2743
2744 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2745 return -EINVAL;
2746
2747 new_rx_size = clamp_t(u32, ring->rx_pending,
2748 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2749 new_rx_size = roundup_pow_of_two(new_rx_size);
2750
2751 new_tx_size = clamp_t(u32, ring->tx_pending,
2752 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2753 new_tx_size = roundup_pow_of_two(new_tx_size);
2754
2755 if ((new_tx_size == bp->tx_ring_size) &&
2756 (new_rx_size == bp->rx_ring_size)) {
2757 /* nothing to do */
2758 return 0;
2759 }
2760
2761 if (netif_running(bp->dev)) {
2762 reset = 1;
2763 macb_close(bp->dev);
2764 }
2765
2766 bp->rx_ring_size = new_rx_size;
2767 bp->tx_ring_size = new_tx_size;
2768
2769 if (reset)
2770 macb_open(bp->dev);
2771
2772 return 0;
2773}
2774
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002775#ifdef CONFIG_MACB_USE_HWSTAMP
2776static unsigned int gem_get_tsu_rate(struct macb *bp)
2777{
2778 struct clk *tsu_clk;
2779 unsigned int tsu_rate;
2780
2781 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2782 if (!IS_ERR(tsu_clk))
2783 tsu_rate = clk_get_rate(tsu_clk);
2784 /* try pclk instead */
2785 else if (!IS_ERR(bp->pclk)) {
2786 tsu_clk = bp->pclk;
2787 tsu_rate = clk_get_rate(tsu_clk);
2788 } else
2789 return -ENOTSUPP;
2790 return tsu_rate;
2791}
2792
2793static s32 gem_get_ptp_max_adj(void)
2794{
2795 return 64000000;
2796}
2797
2798static int gem_get_ts_info(struct net_device *dev,
2799 struct ethtool_ts_info *info)
2800{
2801 struct macb *bp = netdev_priv(dev);
2802
2803 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2804 ethtool_op_get_ts_info(dev, info);
2805 return 0;
2806 }
2807
2808 info->so_timestamping =
2809 SOF_TIMESTAMPING_TX_SOFTWARE |
2810 SOF_TIMESTAMPING_RX_SOFTWARE |
2811 SOF_TIMESTAMPING_SOFTWARE |
2812 SOF_TIMESTAMPING_TX_HARDWARE |
2813 SOF_TIMESTAMPING_RX_HARDWARE |
2814 SOF_TIMESTAMPING_RAW_HARDWARE;
2815 info->tx_types =
2816 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2817 (1 << HWTSTAMP_TX_OFF) |
2818 (1 << HWTSTAMP_TX_ON);
2819 info->rx_filters =
2820 (1 << HWTSTAMP_FILTER_NONE) |
2821 (1 << HWTSTAMP_FILTER_ALL);
2822
2823 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2824
2825 return 0;
2826}
2827
2828static struct macb_ptp_info gem_ptp_info = {
2829 .ptp_init = gem_ptp_init,
2830 .ptp_remove = gem_ptp_remove,
2831 .get_ptp_max_adj = gem_get_ptp_max_adj,
2832 .get_tsu_rate = gem_get_tsu_rate,
2833 .get_ts_info = gem_get_ts_info,
2834 .get_hwtst = gem_get_hwtst,
2835 .set_hwtst = gem_set_hwtst,
2836};
2837#endif
2838
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002839static int macb_get_ts_info(struct net_device *netdev,
2840 struct ethtool_ts_info *info)
2841{
2842 struct macb *bp = netdev_priv(netdev);
2843
2844 if (bp->ptp_info)
2845 return bp->ptp_info->get_ts_info(netdev, info);
2846
2847 return ethtool_op_get_ts_info(netdev, info);
2848}
2849
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002850static void gem_enable_flow_filters(struct macb *bp, bool enable)
2851{
2852 struct ethtool_rx_fs_item *item;
2853 u32 t2_scr;
2854 int num_t2_scr;
2855
2856 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2857
2858 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2859 struct ethtool_rx_flow_spec *fs = &item->fs;
2860 struct ethtool_tcpip4_spec *tp4sp_m;
2861
2862 if (fs->location >= num_t2_scr)
2863 continue;
2864
2865 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2866
2867 /* enable/disable screener regs for the flow entry */
2868 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2869
2870 /* only enable fields with no masking */
2871 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2872
2873 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2874 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2875 else
2876 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2877
2878 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2879 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2880 else
2881 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2882
2883 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2884 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2885 else
2886 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2887
2888 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2889 }
2890}
2891
2892static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2893{
2894 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2895 uint16_t index = fs->location;
2896 u32 w0, w1, t2_scr;
2897 bool cmp_a = false;
2898 bool cmp_b = false;
2899 bool cmp_c = false;
2900
2901 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2902 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2903
2904 /* ignore field if any masking set */
2905 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2906 /* 1st compare reg - IP source address */
2907 w0 = 0;
2908 w1 = 0;
2909 w0 = tp4sp_v->ip4src;
2910 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2911 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2912 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2913 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2914 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2915 cmp_a = true;
2916 }
2917
2918 /* ignore field if any masking set */
2919 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2920 /* 2nd compare reg - IP destination address */
2921 w0 = 0;
2922 w1 = 0;
2923 w0 = tp4sp_v->ip4dst;
2924 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2925 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2926 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2927 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2928 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2929 cmp_b = true;
2930 }
2931
2932 /* ignore both port fields if masking set in both */
2933 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2934 /* 3rd compare reg - source port, destination port */
2935 w0 = 0;
2936 w1 = 0;
2937 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2938 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2939 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2940 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2941 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2942 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2943 } else {
2944 /* only one port definition */
2945 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2946 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2947 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2948 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2949 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2950 } else { /* dst port */
2951 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2952 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2953 }
2954 }
2955 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2956 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2957 cmp_c = true;
2958 }
2959
2960 t2_scr = 0;
2961 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2962 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2963 if (cmp_a)
2964 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2965 if (cmp_b)
2966 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2967 if (cmp_c)
2968 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2969 gem_writel_n(bp, SCRT2, index, t2_scr);
2970}
2971
2972static int gem_add_flow_filter(struct net_device *netdev,
2973 struct ethtool_rxnfc *cmd)
2974{
2975 struct macb *bp = netdev_priv(netdev);
2976 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2977 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002978 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002979 int ret = -EINVAL;
2980 bool added = false;
2981
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002982 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002983 if (newfs == NULL)
2984 return -ENOMEM;
2985 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2986
2987 netdev_dbg(netdev,
2988 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2989 fs->flow_type, (int)fs->ring_cookie, fs->location,
2990 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2991 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2992 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2993
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002994 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2995
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002996 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002997 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2998 if (item->fs.location > newfs->fs.location) {
2999 list_add_tail(&newfs->list, &item->list);
3000 added = true;
3001 break;
3002 } else if (item->fs.location == fs->location) {
3003 netdev_err(netdev, "Rule not added: location %d not free!\n",
3004 fs->location);
3005 ret = -EBUSY;
3006 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003007 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003008 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003009 if (!added)
3010 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003011
3012 gem_prog_cmp_regs(bp, fs);
3013 bp->rx_fs_list.count++;
3014 /* enable filtering if NTUPLE on */
3015 if (netdev->features & NETIF_F_NTUPLE)
3016 gem_enable_flow_filters(bp, 1);
3017
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003018 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003019 return 0;
3020
3021err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003022 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003023 kfree(newfs);
3024 return ret;
3025}
3026
3027static int gem_del_flow_filter(struct net_device *netdev,
3028 struct ethtool_rxnfc *cmd)
3029{
3030 struct macb *bp = netdev_priv(netdev);
3031 struct ethtool_rx_fs_item *item;
3032 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003033 unsigned long flags;
3034
3035 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003036
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003037 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3038 if (item->fs.location == cmd->fs.location) {
3039 /* disable screener regs for the flow entry */
3040 fs = &(item->fs);
3041 netdev_dbg(netdev,
3042 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3043 fs->flow_type, (int)fs->ring_cookie, fs->location,
3044 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3045 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3046 htons(fs->h_u.tcp_ip4_spec.psrc),
3047 htons(fs->h_u.tcp_ip4_spec.pdst));
3048
3049 gem_writel_n(bp, SCRT2, fs->location, 0);
3050
3051 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003052 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003053 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3054 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003055 return 0;
3056 }
3057 }
3058
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003059 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003060 return -EINVAL;
3061}
3062
3063static int gem_get_flow_entry(struct net_device *netdev,
3064 struct ethtool_rxnfc *cmd)
3065{
3066 struct macb *bp = netdev_priv(netdev);
3067 struct ethtool_rx_fs_item *item;
3068
3069 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3070 if (item->fs.location == cmd->fs.location) {
3071 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3072 return 0;
3073 }
3074 }
3075 return -EINVAL;
3076}
3077
3078static int gem_get_all_flow_entries(struct net_device *netdev,
3079 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3080{
3081 struct macb *bp = netdev_priv(netdev);
3082 struct ethtool_rx_fs_item *item;
3083 uint32_t cnt = 0;
3084
3085 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3086 if (cnt == cmd->rule_cnt)
3087 return -EMSGSIZE;
3088 rule_locs[cnt] = item->fs.location;
3089 cnt++;
3090 }
3091 cmd->data = bp->max_tuples;
3092 cmd->rule_cnt = cnt;
3093
3094 return 0;
3095}
3096
3097static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3098 u32 *rule_locs)
3099{
3100 struct macb *bp = netdev_priv(netdev);
3101 int ret = 0;
3102
3103 switch (cmd->cmd) {
3104 case ETHTOOL_GRXRINGS:
3105 cmd->data = bp->num_queues;
3106 break;
3107 case ETHTOOL_GRXCLSRLCNT:
3108 cmd->rule_cnt = bp->rx_fs_list.count;
3109 break;
3110 case ETHTOOL_GRXCLSRULE:
3111 ret = gem_get_flow_entry(netdev, cmd);
3112 break;
3113 case ETHTOOL_GRXCLSRLALL:
3114 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3115 break;
3116 default:
3117 netdev_err(netdev,
3118 "Command parameter %d is not supported\n", cmd->cmd);
3119 ret = -EOPNOTSUPP;
3120 }
3121
3122 return ret;
3123}
3124
3125static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3126{
3127 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003128 int ret;
3129
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003130 switch (cmd->cmd) {
3131 case ETHTOOL_SRXCLSRLINS:
3132 if ((cmd->fs.location >= bp->max_tuples)
3133 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3134 ret = -EINVAL;
3135 break;
3136 }
3137 ret = gem_add_flow_filter(netdev, cmd);
3138 break;
3139 case ETHTOOL_SRXCLSRLDEL:
3140 ret = gem_del_flow_filter(netdev, cmd);
3141 break;
3142 default:
3143 netdev_err(netdev,
3144 "Command parameter %d is not supported\n", cmd->cmd);
3145 ret = -EOPNOTSUPP;
3146 }
3147
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003148 return ret;
3149}
3150
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003151static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003152 .get_regs_len = macb_get_regs_len,
3153 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003154 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003155 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003156 .get_wol = macb_get_wol,
3157 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003158 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3159 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003160 .get_ringparam = macb_get_ringparam,
3161 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003162};
Xander Huff8cd5a562015-01-15 15:55:20 -06003163
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003164static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003165 .get_regs_len = macb_get_regs_len,
3166 .get_regs = macb_get_regs,
3167 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003168 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003169 .get_ethtool_stats = gem_get_ethtool_stats,
3170 .get_strings = gem_get_ethtool_strings,
3171 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003172 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3173 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003174 .get_ringparam = macb_get_ringparam,
3175 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003176 .get_rxnfc = gem_get_rxnfc,
3177 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003178};
3179
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003180static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003181{
Philippe Reynes0a912812016-06-22 00:32:35 +02003182 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003183 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003184
3185 if (!netif_running(dev))
3186 return -EINVAL;
3187
frederic RODO6c36a702007-07-12 19:07:24 +02003188 if (!phydev)
3189 return -ENODEV;
3190
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003191 if (!bp->ptp_info)
3192 return phy_mii_ioctl(phydev, rq, cmd);
3193
3194 switch (cmd) {
3195 case SIOCSHWTSTAMP:
3196 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3197 case SIOCGHWTSTAMP:
3198 return bp->ptp_info->get_hwtst(dev, rq);
3199 default:
3200 return phy_mii_ioctl(phydev, rq, cmd);
3201 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003202}
3203
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003204static int macb_set_features(struct net_device *netdev,
3205 netdev_features_t features)
3206{
3207 struct macb *bp = netdev_priv(netdev);
3208 netdev_features_t changed = features ^ netdev->features;
3209
3210 /* TX checksum offload */
3211 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3212 u32 dmacfg;
3213
3214 dmacfg = gem_readl(bp, DMACFG);
3215 if (features & NETIF_F_HW_CSUM)
3216 dmacfg |= GEM_BIT(TXCOEN);
3217 else
3218 dmacfg &= ~GEM_BIT(TXCOEN);
3219 gem_writel(bp, DMACFG, dmacfg);
3220 }
3221
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003222 /* RX checksum offload */
3223 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3224 u32 netcfg;
3225
3226 netcfg = gem_readl(bp, NCFGR);
3227 if (features & NETIF_F_RXCSUM &&
3228 !(netdev->flags & IFF_PROMISC))
3229 netcfg |= GEM_BIT(RXCOEN);
3230 else
3231 netcfg &= ~GEM_BIT(RXCOEN);
3232 gem_writel(bp, NCFGR, netcfg);
3233 }
3234
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003235 /* RX Flow Filters */
3236 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3237 bool turn_on = features & NETIF_F_NTUPLE;
3238
3239 gem_enable_flow_filters(bp, turn_on);
3240 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003241 return 0;
3242}
3243
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003244static const struct net_device_ops macb_netdev_ops = {
3245 .ndo_open = macb_open,
3246 .ndo_stop = macb_close,
3247 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003248 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003249 .ndo_get_stats = macb_get_stats,
3250 .ndo_do_ioctl = macb_ioctl,
3251 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303252 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003253 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003254#ifdef CONFIG_NET_POLL_CONTROLLER
3255 .ndo_poll_controller = macb_poll_controller,
3256#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003257 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003258 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003259};
3260
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003261/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003262 * and integration options used
3263 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003264static void macb_configure_caps(struct macb *bp,
3265 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003266{
3267 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003268
Nicolas Ferref6970502015-03-31 15:02:01 +02003269 if (dt_conf)
3270 bp->caps = dt_conf->caps;
3271
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003272 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003273 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3274
Nicolas Ferree1755872014-07-24 13:50:58 +02003275 dcfg = gem_readl(bp, DCFG1);
3276 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3277 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3278 dcfg = gem_readl(bp, DCFG2);
3279 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3280 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003281#ifdef CONFIG_MACB_USE_HWSTAMP
3282 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003283 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3284 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003285 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003286 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003287 bp->ptp_info = &gem_ptp_info;
3288 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003289 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003290#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003291 }
3292
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003293 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003294}
3295
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003296static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003297 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003298 unsigned int *queue_mask,
3299 unsigned int *num_queues)
3300{
3301 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003302
3303 *queue_mask = 0x1;
3304 *num_queues = 1;
3305
Nicolas Ferreda120112015-03-31 15:02:00 +02003306 /* is it macb or gem ?
3307 *
3308 * We need to read directly from the hardware here because
3309 * we are early in the probe process and don't have the
3310 * MACB_CAPS_MACB_IS_GEM flag positioned
3311 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003312 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003313 return;
3314
3315 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303316 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3317
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003318 *queue_mask |= 0x1;
3319
3320 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3321 if (*queue_mask & (1 << hw_q))
3322 (*num_queues)++;
3323}
3324
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003325static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303326 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303327 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003328{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003329 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003330 int err;
3331
Bartosz Folta83a77e92016-12-14 06:39:15 +00003332 pdata = dev_get_platdata(&pdev->dev);
3333 if (pdata) {
3334 *pclk = pdata->pclk;
3335 *hclk = pdata->hclk;
3336 } else {
3337 *pclk = devm_clk_get(&pdev->dev, "pclk");
3338 *hclk = devm_clk_get(&pdev->dev, "hclk");
3339 }
3340
Harini Katakamcd5afa92019-03-20 19:12:22 +05303341 if (IS_ERR_OR_NULL(*pclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003342 err = PTR_ERR(*pclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303343 if (!err)
3344 err = -ENODEV;
3345
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003346 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3347 return err;
3348 }
3349
Harini Katakamcd5afa92019-03-20 19:12:22 +05303350 if (IS_ERR_OR_NULL(*hclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003351 err = PTR_ERR(*hclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303352 if (!err)
3353 err = -ENODEV;
3354
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003355 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3356 return err;
3357 }
3358
3359 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3360 if (IS_ERR(*tx_clk))
3361 *tx_clk = NULL;
3362
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303363 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3364 if (IS_ERR(*rx_clk))
3365 *rx_clk = NULL;
3366
Harini Katakamf5473d12019-03-01 16:20:33 +05303367 *tsu_clk = devm_clk_get(&pdev->dev, "tsu_clk");
3368 if (IS_ERR(*tsu_clk))
3369 *tsu_clk = NULL;
3370
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003371 err = clk_prepare_enable(*pclk);
3372 if (err) {
3373 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3374 return err;
3375 }
3376
3377 err = clk_prepare_enable(*hclk);
3378 if (err) {
3379 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3380 goto err_disable_pclk;
3381 }
3382
3383 err = clk_prepare_enable(*tx_clk);
3384 if (err) {
3385 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3386 goto err_disable_hclk;
3387 }
3388
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303389 err = clk_prepare_enable(*rx_clk);
3390 if (err) {
3391 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3392 goto err_disable_txclk;
3393 }
3394
Harini Katakamf5473d12019-03-01 16:20:33 +05303395 err = clk_prepare_enable(*tsu_clk);
3396 if (err) {
3397 dev_err(&pdev->dev, "failed to enable tsu_clk (%u)\n", err);
3398 goto err_disable_rxclk;
3399 }
3400
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003401 return 0;
3402
Harini Katakamf5473d12019-03-01 16:20:33 +05303403err_disable_rxclk:
3404 clk_disable_unprepare(*rx_clk);
3405
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303406err_disable_txclk:
3407 clk_disable_unprepare(*tx_clk);
3408
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003409err_disable_hclk:
3410 clk_disable_unprepare(*hclk);
3411
3412err_disable_pclk:
3413 clk_disable_unprepare(*pclk);
3414
3415 return err;
3416}
3417
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003418static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003419{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003420 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003421 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003422 struct macb *bp = netdev_priv(dev);
3423 struct macb_queue *queue;
3424 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003425 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003426
Zach Brownb410d132016-10-19 09:56:57 -05003427 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3428 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3429
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003430 /* set the queue register mapping once for all: queue0 has a special
3431 * register mapping but we don't want to test the queue index then
3432 * compute the corresponding register offset at run time.
3433 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003434 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003435 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003436 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003437
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003438 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003439 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003440 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003441 if (hw_q) {
3442 queue->ISR = GEM_ISR(hw_q - 1);
3443 queue->IER = GEM_IER(hw_q - 1);
3444 queue->IDR = GEM_IDR(hw_q - 1);
3445 queue->IMR = GEM_IMR(hw_q - 1);
3446 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003447 queue->RBQP = GEM_RBQP(hw_q - 1);
3448 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303449#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003450 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003451 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003452 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3453 }
Harini Katakamfff80192016-08-09 13:15:53 +05303454#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003455 } else {
3456 /* queue0 uses legacy registers */
3457 queue->ISR = MACB_ISR;
3458 queue->IER = MACB_IER;
3459 queue->IDR = MACB_IDR;
3460 queue->IMR = MACB_IMR;
3461 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003462 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303463#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003464 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003465 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003466 queue->RBQPH = MACB_RBQPH;
3467 }
Harini Katakamfff80192016-08-09 13:15:53 +05303468#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003469 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003470
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003471 /* get irq: here we use the linux queue index, not the hardware
3472 * queue index. the queue irq definitions in the device tree
3473 * must remove the optional gaps that could exist in the
3474 * hardware queue mask.
3475 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003476 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003477 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003478 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003479 if (err) {
3480 dev_err(&pdev->dev,
3481 "Unable to request IRQ %d (error %d)\n",
3482 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003483 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003484 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003485
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003486 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003487 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003488 }
3489
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003490 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003491
Nicolas Ferre4df95132013-06-04 21:57:12 +00003492 /* setup appropriated routines according to adapter type */
3493 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003494 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003495 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3496 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3497 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3498 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003499 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003500 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003501 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003502 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3503 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3504 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3505 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003506 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003507 }
3508
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003509 /* Set features */
3510 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003511
3512 /* Check LSO capability */
3513 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3514 dev->hw_features |= MACB_NETIF_LSO;
3515
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003516 /* Checksum offload is only available on gem with packet buffer */
3517 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003518 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003519 if (bp->caps & MACB_CAPS_SG_DISABLED)
3520 dev->hw_features &= ~NETIF_F_SG;
3521 dev->features = dev->hw_features;
3522
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003523 /* Check RX Flow Filters support.
3524 * Max Rx flows set by availability of screeners & compare regs:
3525 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3526 */
3527 reg = gem_readl(bp, DCFG8);
3528 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3529 GEM_BFEXT(T2SCR, reg));
3530 if (bp->max_tuples > 0) {
3531 /* also needs one ethtype match to check IPv4 */
3532 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3533 /* program this reg now */
3534 reg = 0;
3535 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3536 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3537 /* Filtering is supported in hw but don't enable it in kernel now */
3538 dev->hw_features |= NETIF_F_NTUPLE;
3539 /* init Rx flow definitions */
3540 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3541 bp->rx_fs_list.count = 0;
3542 spin_lock_init(&bp->rx_fs_lock);
3543 } else
3544 bp->max_tuples = 0;
3545 }
3546
Neil Armstrongce721a72016-01-05 14:39:16 +01003547 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3548 val = 0;
3549 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3550 val = GEM_BIT(RGMII);
3551 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003552 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003553 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003554 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003555 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003556
Neil Armstrongce721a72016-01-05 14:39:16 +01003557 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3558 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003559
Neil Armstrongce721a72016-01-05 14:39:16 +01003560 macb_or_gem_writel(bp, USRIO, val);
3561 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003562
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003563 /* Set MII management clock divider */
3564 val = macb_mdc_clk_div(bp);
3565 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303566 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3567 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003568 macb_writel(bp, NCFGR, val);
3569
3570 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003571}
3572
3573#if defined(CONFIG_OF)
3574/* 1518 rounded up */
3575#define AT91ETHER_MAX_RBUFF_SZ 0x600
3576/* max number of receive buffers */
3577#define AT91ETHER_MAX_RX_DESCR 9
3578
3579/* Initialize and start the Receiver and Transmit subsystems */
3580static int at91ether_start(struct net_device *dev)
3581{
3582 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003583 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003584 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003585 dma_addr_t addr;
3586 u32 ctl;
3587 int i;
3588
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003589 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003590 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003591 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003592 &q->rx_ring_dma, GFP_KERNEL);
3593 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003594 return -ENOMEM;
3595
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003596 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003597 AT91ETHER_MAX_RX_DESCR *
3598 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003599 &q->rx_buffers_dma, GFP_KERNEL);
3600 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003601 dma_free_coherent(&lp->pdev->dev,
3602 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003603 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003604 q->rx_ring, q->rx_ring_dma);
3605 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003606 return -ENOMEM;
3607 }
3608
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003609 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003610 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003611 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003612 macb_set_addr(lp, desc, addr);
3613 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003614 addr += AT91ETHER_MAX_RBUFF_SZ;
3615 }
3616
3617 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003618 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003619
3620 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003621 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003622
3623 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003624 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003625
3626 /* Enable Receive and Transmit */
3627 ctl = macb_readl(lp, NCR);
3628 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3629
3630 return 0;
3631}
3632
3633/* Open the ethernet interface */
3634static int at91ether_open(struct net_device *dev)
3635{
3636 struct macb *lp = netdev_priv(dev);
3637 u32 ctl;
3638 int ret;
3639
3640 /* Clear internal statistics */
3641 ctl = macb_readl(lp, NCR);
3642 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3643
3644 macb_set_hwaddr(lp);
3645
3646 ret = at91ether_start(dev);
3647 if (ret)
3648 return ret;
3649
3650 /* Enable MAC interrupts */
3651 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3652 MACB_BIT(RXUBR) |
3653 MACB_BIT(ISR_TUND) |
3654 MACB_BIT(ISR_RLE) |
3655 MACB_BIT(TCOMP) |
3656 MACB_BIT(ISR_ROVR) |
3657 MACB_BIT(HRESP));
3658
3659 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003660 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003661
3662 netif_start_queue(dev);
3663
3664 return 0;
3665}
3666
3667/* Close the interface */
3668static int at91ether_close(struct net_device *dev)
3669{
3670 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003671 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003672 u32 ctl;
3673
3674 /* Disable Receiver and Transmitter */
3675 ctl = macb_readl(lp, NCR);
3676 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3677
3678 /* Disable MAC interrupts */
3679 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3680 MACB_BIT(RXUBR) |
3681 MACB_BIT(ISR_TUND) |
3682 MACB_BIT(ISR_RLE) |
3683 MACB_BIT(TCOMP) |
3684 MACB_BIT(ISR_ROVR) |
3685 MACB_BIT(HRESP));
3686
3687 netif_stop_queue(dev);
3688
3689 dma_free_coherent(&lp->pdev->dev,
3690 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003691 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003692 q->rx_ring, q->rx_ring_dma);
3693 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003694
3695 dma_free_coherent(&lp->pdev->dev,
3696 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003697 q->rx_buffers, q->rx_buffers_dma);
3698 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003699
3700 return 0;
3701}
3702
3703/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003704static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3705 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003706{
3707 struct macb *lp = netdev_priv(dev);
3708
3709 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3710 netif_stop_queue(dev);
3711
3712 /* Store packet information (to free when Tx completed) */
3713 lp->skb = skb;
3714 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003715 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3716 skb->len, DMA_TO_DEVICE);
3717 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003718 dev_kfree_skb_any(skb);
3719 dev->stats.tx_dropped++;
3720 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3721 return NETDEV_TX_OK;
3722 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003723
3724 /* Set address of the data in the Transmit Address register */
3725 macb_writel(lp, TAR, lp->skb_physaddr);
3726 /* Set length of the packet in the Transmit Control register */
3727 macb_writel(lp, TCR, skb->len);
3728
3729 } else {
3730 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3731 return NETDEV_TX_BUSY;
3732 }
3733
3734 return NETDEV_TX_OK;
3735}
3736
3737/* Extract received frame from buffer descriptors and sent to upper layers.
3738 * (Called from interrupt context)
3739 */
3740static void at91ether_rx(struct net_device *dev)
3741{
3742 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003743 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003744 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003745 unsigned char *p_recv;
3746 struct sk_buff *skb;
3747 unsigned int pktlen;
3748
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003749 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003750 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003751 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003752 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003753 skb = netdev_alloc_skb(dev, pktlen + 2);
3754 if (skb) {
3755 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003756 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003757
3758 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003759 dev->stats.rx_packets++;
3760 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003761 netif_rx(skb);
3762 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003763 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003764 }
3765
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003766 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003767 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003768
3769 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003770 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003771
3772 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003773 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3774 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003775 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003776 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003777
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003778 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003779 }
3780}
3781
3782/* MAC interrupt handler */
3783static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3784{
3785 struct net_device *dev = dev_id;
3786 struct macb *lp = netdev_priv(dev);
3787 u32 intstatus, ctl;
3788
3789 /* MAC Interrupt Status register indicates what interrupts are pending.
3790 * It is automatically cleared once read.
3791 */
3792 intstatus = macb_readl(lp, ISR);
3793
3794 /* Receive complete */
3795 if (intstatus & MACB_BIT(RCOMP))
3796 at91ether_rx(dev);
3797
3798 /* Transmit complete */
3799 if (intstatus & MACB_BIT(TCOMP)) {
3800 /* The TCOM bit is set even if the transmission failed */
3801 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003802 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003803
3804 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003805 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003806 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003807 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003808 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003809 dev->stats.tx_packets++;
3810 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003811 }
3812 netif_wake_queue(dev);
3813 }
3814
3815 /* Work-around for EMAC Errata section 41.3.1 */
3816 if (intstatus & MACB_BIT(RXUBR)) {
3817 ctl = macb_readl(lp, NCR);
3818 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003819 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003820 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3821 }
3822
3823 if (intstatus & MACB_BIT(ISR_ROVR))
3824 netdev_err(dev, "ROVR error\n");
3825
3826 return IRQ_HANDLED;
3827}
3828
3829#ifdef CONFIG_NET_POLL_CONTROLLER
3830static void at91ether_poll_controller(struct net_device *dev)
3831{
3832 unsigned long flags;
3833
3834 local_irq_save(flags);
3835 at91ether_interrupt(dev->irq, dev);
3836 local_irq_restore(flags);
3837}
3838#endif
3839
3840static const struct net_device_ops at91ether_netdev_ops = {
3841 .ndo_open = at91ether_open,
3842 .ndo_stop = at91ether_close,
3843 .ndo_start_xmit = at91ether_start_xmit,
3844 .ndo_get_stats = macb_get_stats,
3845 .ndo_set_rx_mode = macb_set_rx_mode,
3846 .ndo_set_mac_address = eth_mac_addr,
3847 .ndo_do_ioctl = macb_ioctl,
3848 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003849#ifdef CONFIG_NET_POLL_CONTROLLER
3850 .ndo_poll_controller = at91ether_poll_controller,
3851#endif
3852};
3853
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003854static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303855 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303856 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003857{
3858 int err;
3859
3860 *hclk = NULL;
3861 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303862 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05303863 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003864
3865 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3866 if (IS_ERR(*pclk))
3867 return PTR_ERR(*pclk);
3868
3869 err = clk_prepare_enable(*pclk);
3870 if (err) {
3871 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3872 return err;
3873 }
3874
3875 return 0;
3876}
3877
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003878static int at91ether_init(struct platform_device *pdev)
3879{
3880 struct net_device *dev = platform_get_drvdata(pdev);
3881 struct macb *bp = netdev_priv(dev);
3882 int err;
3883 u32 reg;
3884
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003885 bp->queues[0].bp = bp;
3886
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003887 dev->netdev_ops = &at91ether_netdev_ops;
3888 dev->ethtool_ops = &macb_ethtool_ops;
3889
3890 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3891 0, dev->name, dev);
3892 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003893 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003894
3895 macb_writel(bp, NCR, 0);
3896
3897 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3898 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3899 reg |= MACB_BIT(RM9200_RMII);
3900
3901 macb_writel(bp, NCFGR, reg);
3902
3903 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003904}
3905
David S. Miller3cef5c52015-03-09 23:38:02 -04003906static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003907 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003908 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003909 .init = macb_init,
3910};
3911
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003912static const struct macb_config sama5d3macb_config = {
3913 .caps = MACB_CAPS_SG_DISABLED
3914 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3915 .clk_init = macb_clk_init,
3916 .init = macb_init,
3917};
3918
David S. Miller3cef5c52015-03-09 23:38:02 -04003919static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003920 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3921 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003922 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003923 .init = macb_init,
3924};
3925
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003926static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003927 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003928 .dma_burst_length = 16,
3929 .clk_init = macb_clk_init,
3930 .init = macb_init,
3931};
3932
David S. Miller3cef5c52015-03-09 23:38:02 -04003933static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003934 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003935 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003936 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003937 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003938 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003939 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003940};
3941
David S. Miller3cef5c52015-03-09 23:38:02 -04003942static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003943 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003944 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003945 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003946 .init = macb_init,
3947};
3948
David S. Miller3cef5c52015-03-09 23:38:02 -04003949static const struct macb_config emac_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303950 .caps = MACB_CAPS_NEEDS_RSTONUBR,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003951 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003952 .init = at91ether_init,
3953};
3954
Neil Armstronge611b5b2016-01-05 14:39:17 +01003955static const struct macb_config np4_config = {
3956 .caps = MACB_CAPS_USRIO_DISABLED,
3957 .clk_init = macb_clk_init,
3958 .init = macb_init,
3959};
David S. Miller36583eb2015-05-23 01:22:35 -04003960
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303961static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003962 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3963 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05303964 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303965 .dma_burst_length = 16,
3966 .clk_init = macb_clk_init,
3967 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303968 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303969};
3970
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003971static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303972 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
3973 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003974 .dma_burst_length = 16,
3975 .clk_init = macb_clk_init,
3976 .init = macb_init,
3977};
3978
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003979static const struct of_device_id macb_dt_ids[] = {
3980 { .compatible = "cdns,at32ap7000-macb" },
3981 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3982 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003983 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003984 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3985 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01003986 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003987 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003988 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003989 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003990 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3991 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3992 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303993 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003994 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003995 { /* sentinel */ }
3996};
3997MODULE_DEVICE_TABLE(of, macb_dt_ids);
3998#endif /* CONFIG_OF */
3999
Bartosz Folta83a77e92016-12-14 06:39:15 +00004000static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004001 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4002 MACB_CAPS_JUMBO |
4003 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004004 .dma_burst_length = 16,
4005 .clk_init = macb_clk_init,
4006 .init = macb_init,
4007 .jumbo_max_len = 10240,
4008};
4009
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004010static int macb_probe(struct platform_device *pdev)
4011{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004012 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004013 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304014 struct clk **, struct clk **, struct clk **,
4015 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004016 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004017 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304018 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304019 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004020 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004021 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004022 struct phy_device *phydev;
4023 struct net_device *dev;
4024 struct resource *regs;
4025 void __iomem *mem;
4026 const char *mac;
4027 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304028 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004029
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004030 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4031 mem = devm_ioremap_resource(&pdev->dev, regs);
4032 if (IS_ERR(mem))
4033 return PTR_ERR(mem);
4034
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004035 if (np) {
4036 const struct of_device_id *match;
4037
4038 match = of_match_node(macb_dt_ids, np);
4039 if (match && match->data) {
4040 macb_config = match->data;
4041 clk_init = macb_config->clk_init;
4042 init = macb_config->init;
4043 }
4044 }
4045
Harini Katakamf5473d12019-03-01 16:20:33 +05304046 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004047 if (err)
4048 return err;
4049
Harini Katakamd54f89a2019-03-01 16:20:34 +05304050 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4051 pm_runtime_use_autosuspend(&pdev->dev);
4052 pm_runtime_get_noresume(&pdev->dev);
4053 pm_runtime_set_active(&pdev->dev);
4054 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004055 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004056
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004057 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004058 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004059 if (!dev) {
4060 err = -ENOMEM;
4061 goto err_disable_clocks;
4062 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004063
4064 dev->base_addr = regs->start;
4065
4066 SET_NETDEV_DEV(dev, &pdev->dev);
4067
4068 bp = netdev_priv(dev);
4069 bp->pdev = pdev;
4070 bp->dev = dev;
4071 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004072 bp->native_io = native_io;
4073 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004074 bp->macb_reg_readl = hw_readl_native;
4075 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004076 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004077 bp->macb_reg_readl = hw_readl;
4078 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004079 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004080 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004081 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004082 if (macb_config)
4083 bp->dma_burst_length = macb_config->dma_burst_length;
4084 bp->pclk = pclk;
4085 bp->hclk = hclk;
4086 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304087 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304088 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004089 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304090 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304091
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004092 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004093 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004094 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4095 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4096
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004097 spin_lock_init(&bp->lock);
4098
Nicolas Ferread783472015-03-31 15:02:02 +02004099 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004100 macb_configure_caps(bp, macb_config);
4101
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004102#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4103 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4104 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4105 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4106 }
4107#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004108 platform_set_drvdata(pdev, dev);
4109
4110 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004111 if (dev->irq < 0) {
4112 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004113 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004114 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004115
Jarod Wilson44770e12016-10-17 15:54:17 -04004116 /* MTU range: 68 - 1500 or 10240 */
4117 dev->min_mtu = GEM_MTU_MIN_SIZE;
4118 if (bp->caps & MACB_CAPS_JUMBO)
4119 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4120 else
4121 dev->max_mtu = ETH_DATA_LEN;
4122
Harini Katakam404cd082018-07-06 12:18:58 +05304123 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4124 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4125 if (val)
4126 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4127 macb_dma_desc_get_size(bp);
4128
4129 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4130 if (val)
4131 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4132 macb_dma_desc_get_size(bp);
4133 }
4134
Harini Katakame5010702019-01-29 15:20:03 +05304135 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4136 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4137 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4138
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004139 mac = of_get_mac_address(np);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004140 if (mac) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004141 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004142 } else {
Bartosz Golaszewskicce41b82018-11-30 09:20:58 +01004143 err = nvmem_get_mac_address(&pdev->dev, bp->dev->dev_addr);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004144 if (err) {
4145 if (err == -EPROBE_DEFER)
4146 goto err_out_free_netdev;
4147 macb_get_hwaddr(bp);
4148 }
4149 }
frederic RODO6c36a702007-07-12 19:07:24 +02004150
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004151 err = of_get_phy_mode(np);
Nicolas Ferre8b952742019-05-03 12:36:58 +02004152 if (err < 0)
4153 /* not found in DT, MII by default */
4154 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4155 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004156 bp->phy_interface = err;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004157
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004158 /* IP specific init */
4159 err = init(pdev);
4160 if (err)
4161 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004162
Florian Fainellicf669662016-05-02 18:38:45 -07004163 err = macb_mii_init(bp);
4164 if (err)
4165 goto err_out_free_netdev;
4166
Philippe Reynes0a912812016-06-22 00:32:35 +02004167 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004168
4169 netif_carrier_off(dev);
4170
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004171 err = register_netdev(dev);
4172 if (err) {
4173 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004174 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004175 }
4176
Harini Katakam032dc412018-01-27 12:09:01 +05304177 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4178 (unsigned long)bp);
4179
Florian Fainellicf669662016-05-02 18:38:45 -07004180 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004181
Bo Shen58798232014-09-13 01:57:49 +02004182 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4183 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4184 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004185
Harini Katakamd54f89a2019-03-01 16:20:34 +05304186 pm_runtime_mark_last_busy(&bp->pdev->dev);
4187 pm_runtime_put_autosuspend(&bp->pdev->dev);
4188
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004189 return 0;
4190
Florian Fainellicf669662016-05-02 18:38:45 -07004191err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004192 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004193 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004194 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004195 if (np && of_phy_is_fixed_link(np))
4196 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004197 mdiobus_free(bp->mii_bus);
4198
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004199err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004200 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004201
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004202err_disable_clocks:
4203 clk_disable_unprepare(tx_clk);
4204 clk_disable_unprepare(hclk);
4205 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304206 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304207 clk_disable_unprepare(tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304208 pm_runtime_disable(&pdev->dev);
4209 pm_runtime_set_suspended(&pdev->dev);
4210 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004211
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004212 return err;
4213}
4214
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004215static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004216{
4217 struct net_device *dev;
4218 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004219 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004220
4221 dev = platform_get_drvdata(pdev);
4222
4223 if (dev) {
4224 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004225 if (dev->phydev)
4226 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004227 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004228 if (np && of_phy_is_fixed_link(np))
4229 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004230 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004231 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004232
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004233 unregister_netdev(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304234 pm_runtime_disable(&pdev->dev);
4235 pm_runtime_dont_use_autosuspend(&pdev->dev);
4236 if (!pm_runtime_suspended(&pdev->dev)) {
4237 clk_disable_unprepare(bp->tx_clk);
4238 clk_disable_unprepare(bp->hclk);
4239 clk_disable_unprepare(bp->pclk);
4240 clk_disable_unprepare(bp->rx_clk);
4241 clk_disable_unprepare(bp->tsu_clk);
4242 pm_runtime_set_suspended(&pdev->dev);
4243 }
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004244 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004245 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004246 }
4247
4248 return 0;
4249}
4250
Michal Simekd23823d2015-01-23 09:36:03 +01004251static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004252{
Wolfram Sangce886a42018-10-21 22:00:14 +02004253 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004254 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304255 struct macb_queue *queue = bp->queues;
4256 unsigned long flags;
4257 unsigned int q;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004258
Harini Katakamde991c52019-03-01 16:20:35 +05304259 if (!netif_running(netdev))
4260 return 0;
4261
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004262
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004263 if (bp->wol & MACB_WOL_ENABLED) {
4264 macb_writel(bp, IER, MACB_BIT(WOL));
4265 macb_writel(bp, WOL, MACB_BIT(MAG));
4266 enable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304267 netif_device_detach(netdev);
4268 } else {
4269 netif_device_detach(netdev);
4270 for (q = 0, queue = bp->queues; q < bp->num_queues;
4271 ++q, ++queue)
4272 napi_disable(&queue->napi);
4273 phy_stop(netdev->phydev);
4274 phy_suspend(netdev->phydev);
4275 spin_lock_irqsave(&bp->lock, flags);
4276 macb_reset_hw(bp);
4277 spin_unlock_irqrestore(&bp->lock, flags);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304278 }
4279
Harini Katakamde991c52019-03-01 16:20:35 +05304280 netif_carrier_off(netdev);
4281 if (bp->ptp_info)
4282 bp->ptp_info->ptp_remove(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304283 pm_runtime_force_suspend(dev);
4284
4285 return 0;
4286}
4287
4288static int __maybe_unused macb_resume(struct device *dev)
4289{
4290 struct net_device *netdev = dev_get_drvdata(dev);
4291 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304292 struct macb_queue *queue = bp->queues;
4293 unsigned int q;
4294
4295 if (!netif_running(netdev))
4296 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304297
4298 pm_runtime_force_resume(dev);
4299
4300 if (bp->wol & MACB_WOL_ENABLED) {
4301 macb_writel(bp, IDR, MACB_BIT(WOL));
4302 macb_writel(bp, WOL, 0);
4303 disable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304304 } else {
4305 macb_writel(bp, NCR, MACB_BIT(MPE));
4306 for (q = 0, queue = bp->queues; q < bp->num_queues;
4307 ++q, ++queue)
4308 napi_enable(&queue->napi);
4309 phy_resume(netdev->phydev);
4310 phy_init_hw(netdev->phydev);
4311 phy_start(netdev->phydev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304312 }
4313
Harini Katakamde991c52019-03-01 16:20:35 +05304314 bp->macbgem_ops.mog_init_rings(bp);
4315 macb_init_hw(bp);
4316 macb_set_rx_mode(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304317 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304318 if (bp->ptp_info)
4319 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304320
4321 return 0;
4322}
4323
4324static int __maybe_unused macb_runtime_suspend(struct device *dev)
4325{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004326 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304327 struct macb *bp = netdev_priv(netdev);
4328
4329 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004330 clk_disable_unprepare(bp->tx_clk);
4331 clk_disable_unprepare(bp->hclk);
4332 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304333 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004334 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304335 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004336
4337 return 0;
4338}
4339
Harini Katakamd54f89a2019-03-01 16:20:34 +05304340static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004341{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004342 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004343 struct macb *bp = netdev_priv(netdev);
4344
Harini Katakamd54f89a2019-03-01 16:20:34 +05304345 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004346 clk_prepare_enable(bp->pclk);
4347 clk_prepare_enable(bp->hclk);
4348 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304349 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004350 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304351 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004352
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004353 return 0;
4354}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004355
Harini Katakamd54f89a2019-03-01 16:20:34 +05304356static const struct dev_pm_ops macb_pm_ops = {
4357 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4358 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4359};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004360
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004361static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004362 .probe = macb_probe,
4363 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004364 .driver = {
4365 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004366 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004367 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004368 },
4369};
4370
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004371module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004372
4373MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004374MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004375MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004376MODULE_ALIAS("platform:macb");