blob: f825e39605403af0b167ff09404a64e697e06d49 [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
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002430 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2431 napi_enable(&queue->napi);
2432
Harini Katakam05044532019-05-07 19:59:10 +05302433 bp->macbgem_ops.mog_init_rings(bp);
2434 macb_init_hw(bp);
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{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002852 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002853 struct ethtool_rx_fs_item *item;
2854 u32 t2_scr;
2855 int num_t2_scr;
2856
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002857 if (!(netdev->features & NETIF_F_NTUPLE))
2858 return;
2859
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002860 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2861
2862 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2863 struct ethtool_rx_flow_spec *fs = &item->fs;
2864 struct ethtool_tcpip4_spec *tp4sp_m;
2865
2866 if (fs->location >= num_t2_scr)
2867 continue;
2868
2869 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2870
2871 /* enable/disable screener regs for the flow entry */
2872 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2873
2874 /* only enable fields with no masking */
2875 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2876
2877 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2878 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2879 else
2880 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2881
2882 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2883 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2884 else
2885 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2886
2887 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2888 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2889 else
2890 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2891
2892 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2893 }
2894}
2895
2896static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2897{
2898 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2899 uint16_t index = fs->location;
2900 u32 w0, w1, t2_scr;
2901 bool cmp_a = false;
2902 bool cmp_b = false;
2903 bool cmp_c = false;
2904
2905 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2906 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2907
2908 /* ignore field if any masking set */
2909 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2910 /* 1st compare reg - IP source address */
2911 w0 = 0;
2912 w1 = 0;
2913 w0 = tp4sp_v->ip4src;
2914 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2915 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2916 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2917 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2918 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2919 cmp_a = true;
2920 }
2921
2922 /* ignore field if any masking set */
2923 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2924 /* 2nd compare reg - IP destination address */
2925 w0 = 0;
2926 w1 = 0;
2927 w0 = tp4sp_v->ip4dst;
2928 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2929 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2930 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2931 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2932 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2933 cmp_b = true;
2934 }
2935
2936 /* ignore both port fields if masking set in both */
2937 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2938 /* 3rd compare reg - source port, destination port */
2939 w0 = 0;
2940 w1 = 0;
2941 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2942 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2943 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2944 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2945 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2946 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2947 } else {
2948 /* only one port definition */
2949 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2950 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2951 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2952 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2953 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2954 } else { /* dst port */
2955 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2956 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2957 }
2958 }
2959 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2960 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2961 cmp_c = true;
2962 }
2963
2964 t2_scr = 0;
2965 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2966 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2967 if (cmp_a)
2968 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2969 if (cmp_b)
2970 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2971 if (cmp_c)
2972 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2973 gem_writel_n(bp, SCRT2, index, t2_scr);
2974}
2975
2976static int gem_add_flow_filter(struct net_device *netdev,
2977 struct ethtool_rxnfc *cmd)
2978{
2979 struct macb *bp = netdev_priv(netdev);
2980 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2981 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002982 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002983 int ret = -EINVAL;
2984 bool added = false;
2985
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002986 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002987 if (newfs == NULL)
2988 return -ENOMEM;
2989 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2990
2991 netdev_dbg(netdev,
2992 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2993 fs->flow_type, (int)fs->ring_cookie, fs->location,
2994 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2995 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2996 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2997
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002998 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2999
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003000 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003001 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3002 if (item->fs.location > newfs->fs.location) {
3003 list_add_tail(&newfs->list, &item->list);
3004 added = true;
3005 break;
3006 } else if (item->fs.location == fs->location) {
3007 netdev_err(netdev, "Rule not added: location %d not free!\n",
3008 fs->location);
3009 ret = -EBUSY;
3010 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003011 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003012 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003013 if (!added)
3014 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003015
3016 gem_prog_cmp_regs(bp, fs);
3017 bp->rx_fs_list.count++;
3018 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003019 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003020
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003021 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003022 return 0;
3023
3024err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003025 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003026 kfree(newfs);
3027 return ret;
3028}
3029
3030static int gem_del_flow_filter(struct net_device *netdev,
3031 struct ethtool_rxnfc *cmd)
3032{
3033 struct macb *bp = netdev_priv(netdev);
3034 struct ethtool_rx_fs_item *item;
3035 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003036 unsigned long flags;
3037
3038 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003039
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003040 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3041 if (item->fs.location == cmd->fs.location) {
3042 /* disable screener regs for the flow entry */
3043 fs = &(item->fs);
3044 netdev_dbg(netdev,
3045 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3046 fs->flow_type, (int)fs->ring_cookie, fs->location,
3047 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3048 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3049 htons(fs->h_u.tcp_ip4_spec.psrc),
3050 htons(fs->h_u.tcp_ip4_spec.pdst));
3051
3052 gem_writel_n(bp, SCRT2, fs->location, 0);
3053
3054 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003055 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003056 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3057 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003058 return 0;
3059 }
3060 }
3061
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003062 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003063 return -EINVAL;
3064}
3065
3066static int gem_get_flow_entry(struct net_device *netdev,
3067 struct ethtool_rxnfc *cmd)
3068{
3069 struct macb *bp = netdev_priv(netdev);
3070 struct ethtool_rx_fs_item *item;
3071
3072 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3073 if (item->fs.location == cmd->fs.location) {
3074 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3075 return 0;
3076 }
3077 }
3078 return -EINVAL;
3079}
3080
3081static int gem_get_all_flow_entries(struct net_device *netdev,
3082 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3083{
3084 struct macb *bp = netdev_priv(netdev);
3085 struct ethtool_rx_fs_item *item;
3086 uint32_t cnt = 0;
3087
3088 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3089 if (cnt == cmd->rule_cnt)
3090 return -EMSGSIZE;
3091 rule_locs[cnt] = item->fs.location;
3092 cnt++;
3093 }
3094 cmd->data = bp->max_tuples;
3095 cmd->rule_cnt = cnt;
3096
3097 return 0;
3098}
3099
3100static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3101 u32 *rule_locs)
3102{
3103 struct macb *bp = netdev_priv(netdev);
3104 int ret = 0;
3105
3106 switch (cmd->cmd) {
3107 case ETHTOOL_GRXRINGS:
3108 cmd->data = bp->num_queues;
3109 break;
3110 case ETHTOOL_GRXCLSRLCNT:
3111 cmd->rule_cnt = bp->rx_fs_list.count;
3112 break;
3113 case ETHTOOL_GRXCLSRULE:
3114 ret = gem_get_flow_entry(netdev, cmd);
3115 break;
3116 case ETHTOOL_GRXCLSRLALL:
3117 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3118 break;
3119 default:
3120 netdev_err(netdev,
3121 "Command parameter %d is not supported\n", cmd->cmd);
3122 ret = -EOPNOTSUPP;
3123 }
3124
3125 return ret;
3126}
3127
3128static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3129{
3130 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003131 int ret;
3132
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003133 switch (cmd->cmd) {
3134 case ETHTOOL_SRXCLSRLINS:
3135 if ((cmd->fs.location >= bp->max_tuples)
3136 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3137 ret = -EINVAL;
3138 break;
3139 }
3140 ret = gem_add_flow_filter(netdev, cmd);
3141 break;
3142 case ETHTOOL_SRXCLSRLDEL:
3143 ret = gem_del_flow_filter(netdev, cmd);
3144 break;
3145 default:
3146 netdev_err(netdev,
3147 "Command parameter %d is not supported\n", cmd->cmd);
3148 ret = -EOPNOTSUPP;
3149 }
3150
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003151 return ret;
3152}
3153
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003154static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003155 .get_regs_len = macb_get_regs_len,
3156 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003157 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003158 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003159 .get_wol = macb_get_wol,
3160 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003161 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3162 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003163 .get_ringparam = macb_get_ringparam,
3164 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003165};
Xander Huff8cd5a562015-01-15 15:55:20 -06003166
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003167static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003168 .get_regs_len = macb_get_regs_len,
3169 .get_regs = macb_get_regs,
3170 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003171 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003172 .get_ethtool_stats = gem_get_ethtool_stats,
3173 .get_strings = gem_get_ethtool_strings,
3174 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003175 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3176 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003177 .get_ringparam = macb_get_ringparam,
3178 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003179 .get_rxnfc = gem_get_rxnfc,
3180 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003181};
3182
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003183static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003184{
Philippe Reynes0a912812016-06-22 00:32:35 +02003185 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003186 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003187
3188 if (!netif_running(dev))
3189 return -EINVAL;
3190
frederic RODO6c36a702007-07-12 19:07:24 +02003191 if (!phydev)
3192 return -ENODEV;
3193
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003194 if (!bp->ptp_info)
3195 return phy_mii_ioctl(phydev, rq, cmd);
3196
3197 switch (cmd) {
3198 case SIOCSHWTSTAMP:
3199 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3200 case SIOCGHWTSTAMP:
3201 return bp->ptp_info->get_hwtst(dev, rq);
3202 default:
3203 return phy_mii_ioctl(phydev, rq, cmd);
3204 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003205}
3206
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003207static inline void macb_set_txcsum_feature(struct macb *bp,
3208 netdev_features_t features)
3209{
3210 u32 val;
3211
3212 if (!macb_is_gem(bp))
3213 return;
3214
3215 val = gem_readl(bp, DMACFG);
3216 if (features & NETIF_F_HW_CSUM)
3217 val |= GEM_BIT(TXCOEN);
3218 else
3219 val &= ~GEM_BIT(TXCOEN);
3220
3221 gem_writel(bp, DMACFG, val);
3222}
3223
3224static inline void macb_set_rxcsum_feature(struct macb *bp,
3225 netdev_features_t features)
3226{
3227 struct net_device *netdev = bp->dev;
3228 u32 val;
3229
3230 if (!macb_is_gem(bp))
3231 return;
3232
3233 val = gem_readl(bp, NCFGR);
3234 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3235 val |= GEM_BIT(RXCOEN);
3236 else
3237 val &= ~GEM_BIT(RXCOEN);
3238
3239 gem_writel(bp, NCFGR, val);
3240}
3241
3242static inline void macb_set_rxflow_feature(struct macb *bp,
3243 netdev_features_t features)
3244{
3245 if (!macb_is_gem(bp))
3246 return;
3247
3248 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3249}
3250
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003251static int macb_set_features(struct net_device *netdev,
3252 netdev_features_t features)
3253{
3254 struct macb *bp = netdev_priv(netdev);
3255 netdev_features_t changed = features ^ netdev->features;
3256
3257 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003258 if (changed & NETIF_F_HW_CSUM)
3259 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003260
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003261 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003262 if (changed & NETIF_F_RXCSUM)
3263 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003264
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003265 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003266 if (changed & NETIF_F_NTUPLE)
3267 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003268
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003269 return 0;
3270}
3271
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003272static void macb_restore_features(struct macb *bp)
3273{
3274 struct net_device *netdev = bp->dev;
3275 netdev_features_t features = netdev->features;
3276
3277 /* TX checksum offload */
3278 macb_set_txcsum_feature(bp, features);
3279
3280 /* RX checksum offload */
3281 macb_set_rxcsum_feature(bp, features);
3282
3283 /* RX Flow Filters */
3284 macb_set_rxflow_feature(bp, features);
3285}
3286
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003287static const struct net_device_ops macb_netdev_ops = {
3288 .ndo_open = macb_open,
3289 .ndo_stop = macb_close,
3290 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003291 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003292 .ndo_get_stats = macb_get_stats,
3293 .ndo_do_ioctl = macb_ioctl,
3294 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303295 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003296 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003297#ifdef CONFIG_NET_POLL_CONTROLLER
3298 .ndo_poll_controller = macb_poll_controller,
3299#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003300 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003301 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003302};
3303
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003304/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003305 * and integration options used
3306 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003307static void macb_configure_caps(struct macb *bp,
3308 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003309{
3310 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003311
Nicolas Ferref6970502015-03-31 15:02:01 +02003312 if (dt_conf)
3313 bp->caps = dt_conf->caps;
3314
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003315 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003316 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3317
Nicolas Ferree1755872014-07-24 13:50:58 +02003318 dcfg = gem_readl(bp, DCFG1);
3319 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3320 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3321 dcfg = gem_readl(bp, DCFG2);
3322 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3323 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003324#ifdef CONFIG_MACB_USE_HWSTAMP
3325 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003326 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3327 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003328 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003329 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003330 bp->ptp_info = &gem_ptp_info;
3331 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003332 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003333#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003334 }
3335
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003336 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003337}
3338
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003339static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003340 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003341 unsigned int *queue_mask,
3342 unsigned int *num_queues)
3343{
3344 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003345
3346 *queue_mask = 0x1;
3347 *num_queues = 1;
3348
Nicolas Ferreda120112015-03-31 15:02:00 +02003349 /* is it macb or gem ?
3350 *
3351 * We need to read directly from the hardware here because
3352 * we are early in the probe process and don't have the
3353 * MACB_CAPS_MACB_IS_GEM flag positioned
3354 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003355 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003356 return;
3357
3358 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303359 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3360
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003361 *queue_mask |= 0x1;
3362
3363 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3364 if (*queue_mask & (1 << hw_q))
3365 (*num_queues)++;
3366}
3367
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003368static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303369 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303370 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003371{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003372 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003373 int err;
3374
Bartosz Folta83a77e92016-12-14 06:39:15 +00003375 pdata = dev_get_platdata(&pdev->dev);
3376 if (pdata) {
3377 *pclk = pdata->pclk;
3378 *hclk = pdata->hclk;
3379 } else {
3380 *pclk = devm_clk_get(&pdev->dev, "pclk");
3381 *hclk = devm_clk_get(&pdev->dev, "hclk");
3382 }
3383
Harini Katakamcd5afa92019-03-20 19:12:22 +05303384 if (IS_ERR_OR_NULL(*pclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003385 err = PTR_ERR(*pclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303386 if (!err)
3387 err = -ENODEV;
3388
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003389 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003390 return err;
3391 }
3392
Harini Katakamcd5afa92019-03-20 19:12:22 +05303393 if (IS_ERR_OR_NULL(*hclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003394 err = PTR_ERR(*hclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303395 if (!err)
3396 err = -ENODEV;
3397
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003398 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003399 return err;
3400 }
3401
3402 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3403 if (IS_ERR(*tx_clk))
3404 *tx_clk = NULL;
3405
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303406 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3407 if (IS_ERR(*rx_clk))
3408 *rx_clk = NULL;
3409
Harini Katakamf5473d12019-03-01 16:20:33 +05303410 *tsu_clk = devm_clk_get(&pdev->dev, "tsu_clk");
3411 if (IS_ERR(*tsu_clk))
3412 *tsu_clk = NULL;
3413
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003414 err = clk_prepare_enable(*pclk);
3415 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003416 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003417 return err;
3418 }
3419
3420 err = clk_prepare_enable(*hclk);
3421 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003422 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003423 goto err_disable_pclk;
3424 }
3425
3426 err = clk_prepare_enable(*tx_clk);
3427 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003428 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003429 goto err_disable_hclk;
3430 }
3431
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303432 err = clk_prepare_enable(*rx_clk);
3433 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003434 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303435 goto err_disable_txclk;
3436 }
3437
Harini Katakamf5473d12019-03-01 16:20:33 +05303438 err = clk_prepare_enable(*tsu_clk);
3439 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003440 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303441 goto err_disable_rxclk;
3442 }
3443
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003444 return 0;
3445
Harini Katakamf5473d12019-03-01 16:20:33 +05303446err_disable_rxclk:
3447 clk_disable_unprepare(*rx_clk);
3448
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303449err_disable_txclk:
3450 clk_disable_unprepare(*tx_clk);
3451
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003452err_disable_hclk:
3453 clk_disable_unprepare(*hclk);
3454
3455err_disable_pclk:
3456 clk_disable_unprepare(*pclk);
3457
3458 return err;
3459}
3460
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003461static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003462{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003463 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003464 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003465 struct macb *bp = netdev_priv(dev);
3466 struct macb_queue *queue;
3467 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003468 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003469
Zach Brownb410d132016-10-19 09:56:57 -05003470 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3471 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3472
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003473 /* set the queue register mapping once for all: queue0 has a special
3474 * register mapping but we don't want to test the queue index then
3475 * compute the corresponding register offset at run time.
3476 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003477 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003478 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003479 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003480
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003481 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003482 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003483 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003484 if (hw_q) {
3485 queue->ISR = GEM_ISR(hw_q - 1);
3486 queue->IER = GEM_IER(hw_q - 1);
3487 queue->IDR = GEM_IDR(hw_q - 1);
3488 queue->IMR = GEM_IMR(hw_q - 1);
3489 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003490 queue->RBQP = GEM_RBQP(hw_q - 1);
3491 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303492#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003493 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003494 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003495 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3496 }
Harini Katakamfff80192016-08-09 13:15:53 +05303497#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003498 } else {
3499 /* queue0 uses legacy registers */
3500 queue->ISR = MACB_ISR;
3501 queue->IER = MACB_IER;
3502 queue->IDR = MACB_IDR;
3503 queue->IMR = MACB_IMR;
3504 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003505 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303506#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003507 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003508 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003509 queue->RBQPH = MACB_RBQPH;
3510 }
Harini Katakamfff80192016-08-09 13:15:53 +05303511#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003512 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003513
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003514 /* get irq: here we use the linux queue index, not the hardware
3515 * queue index. the queue irq definitions in the device tree
3516 * must remove the optional gaps that could exist in the
3517 * hardware queue mask.
3518 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003519 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003520 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003521 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003522 if (err) {
3523 dev_err(&pdev->dev,
3524 "Unable to request IRQ %d (error %d)\n",
3525 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003526 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003527 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003528
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003529 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003530 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003531 }
3532
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003533 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003534
Nicolas Ferre4df95132013-06-04 21:57:12 +00003535 /* setup appropriated routines according to adapter type */
3536 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003537 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003538 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3539 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3540 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3541 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003542 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003543 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003544 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003545 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3546 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3547 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3548 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003549 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003550 }
3551
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003552 /* Set features */
3553 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003554
3555 /* Check LSO capability */
3556 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3557 dev->hw_features |= MACB_NETIF_LSO;
3558
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003559 /* Checksum offload is only available on gem with packet buffer */
3560 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003561 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003562 if (bp->caps & MACB_CAPS_SG_DISABLED)
3563 dev->hw_features &= ~NETIF_F_SG;
3564 dev->features = dev->hw_features;
3565
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003566 /* Check RX Flow Filters support.
3567 * Max Rx flows set by availability of screeners & compare regs:
3568 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3569 */
3570 reg = gem_readl(bp, DCFG8);
3571 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3572 GEM_BFEXT(T2SCR, reg));
3573 if (bp->max_tuples > 0) {
3574 /* also needs one ethtype match to check IPv4 */
3575 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3576 /* program this reg now */
3577 reg = 0;
3578 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3579 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3580 /* Filtering is supported in hw but don't enable it in kernel now */
3581 dev->hw_features |= NETIF_F_NTUPLE;
3582 /* init Rx flow definitions */
3583 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3584 bp->rx_fs_list.count = 0;
3585 spin_lock_init(&bp->rx_fs_lock);
3586 } else
3587 bp->max_tuples = 0;
3588 }
3589
Neil Armstrongce721a72016-01-05 14:39:16 +01003590 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3591 val = 0;
3592 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3593 val = GEM_BIT(RGMII);
3594 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003595 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003596 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003597 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003598 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003599
Neil Armstrongce721a72016-01-05 14:39:16 +01003600 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3601 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003602
Neil Armstrongce721a72016-01-05 14:39:16 +01003603 macb_or_gem_writel(bp, USRIO, val);
3604 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003605
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003606 /* Set MII management clock divider */
3607 val = macb_mdc_clk_div(bp);
3608 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303609 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3610 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003611 macb_writel(bp, NCFGR, val);
3612
3613 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003614}
3615
3616#if defined(CONFIG_OF)
3617/* 1518 rounded up */
3618#define AT91ETHER_MAX_RBUFF_SZ 0x600
3619/* max number of receive buffers */
3620#define AT91ETHER_MAX_RX_DESCR 9
3621
3622/* Initialize and start the Receiver and Transmit subsystems */
3623static int at91ether_start(struct net_device *dev)
3624{
3625 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003626 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003627 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003628 dma_addr_t addr;
3629 u32 ctl;
3630 int i;
3631
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003632 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003633 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003634 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003635 &q->rx_ring_dma, GFP_KERNEL);
3636 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003637 return -ENOMEM;
3638
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003639 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003640 AT91ETHER_MAX_RX_DESCR *
3641 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003642 &q->rx_buffers_dma, GFP_KERNEL);
3643 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003644 dma_free_coherent(&lp->pdev->dev,
3645 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003646 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003647 q->rx_ring, q->rx_ring_dma);
3648 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003649 return -ENOMEM;
3650 }
3651
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003652 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003653 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003654 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003655 macb_set_addr(lp, desc, addr);
3656 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003657 addr += AT91ETHER_MAX_RBUFF_SZ;
3658 }
3659
3660 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003661 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003662
3663 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003664 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003665
3666 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003667 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003668
3669 /* Enable Receive and Transmit */
3670 ctl = macb_readl(lp, NCR);
3671 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3672
3673 return 0;
3674}
3675
3676/* Open the ethernet interface */
3677static int at91ether_open(struct net_device *dev)
3678{
3679 struct macb *lp = netdev_priv(dev);
3680 u32 ctl;
3681 int ret;
3682
3683 /* Clear internal statistics */
3684 ctl = macb_readl(lp, NCR);
3685 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3686
3687 macb_set_hwaddr(lp);
3688
3689 ret = at91ether_start(dev);
3690 if (ret)
3691 return ret;
3692
3693 /* Enable MAC interrupts */
3694 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3695 MACB_BIT(RXUBR) |
3696 MACB_BIT(ISR_TUND) |
3697 MACB_BIT(ISR_RLE) |
3698 MACB_BIT(TCOMP) |
3699 MACB_BIT(ISR_ROVR) |
3700 MACB_BIT(HRESP));
3701
3702 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003703 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003704
3705 netif_start_queue(dev);
3706
3707 return 0;
3708}
3709
3710/* Close the interface */
3711static int at91ether_close(struct net_device *dev)
3712{
3713 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003714 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003715 u32 ctl;
3716
3717 /* Disable Receiver and Transmitter */
3718 ctl = macb_readl(lp, NCR);
3719 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3720
3721 /* Disable MAC interrupts */
3722 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3723 MACB_BIT(RXUBR) |
3724 MACB_BIT(ISR_TUND) |
3725 MACB_BIT(ISR_RLE) |
3726 MACB_BIT(TCOMP) |
3727 MACB_BIT(ISR_ROVR) |
3728 MACB_BIT(HRESP));
3729
3730 netif_stop_queue(dev);
3731
3732 dma_free_coherent(&lp->pdev->dev,
3733 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003734 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003735 q->rx_ring, q->rx_ring_dma);
3736 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003737
3738 dma_free_coherent(&lp->pdev->dev,
3739 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003740 q->rx_buffers, q->rx_buffers_dma);
3741 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003742
3743 return 0;
3744}
3745
3746/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003747static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3748 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003749{
3750 struct macb *lp = netdev_priv(dev);
3751
3752 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3753 netif_stop_queue(dev);
3754
3755 /* Store packet information (to free when Tx completed) */
3756 lp->skb = skb;
3757 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003758 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3759 skb->len, DMA_TO_DEVICE);
3760 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003761 dev_kfree_skb_any(skb);
3762 dev->stats.tx_dropped++;
3763 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3764 return NETDEV_TX_OK;
3765 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003766
3767 /* Set address of the data in the Transmit Address register */
3768 macb_writel(lp, TAR, lp->skb_physaddr);
3769 /* Set length of the packet in the Transmit Control register */
3770 macb_writel(lp, TCR, skb->len);
3771
3772 } else {
3773 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3774 return NETDEV_TX_BUSY;
3775 }
3776
3777 return NETDEV_TX_OK;
3778}
3779
3780/* Extract received frame from buffer descriptors and sent to upper layers.
3781 * (Called from interrupt context)
3782 */
3783static void at91ether_rx(struct net_device *dev)
3784{
3785 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003786 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003787 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003788 unsigned char *p_recv;
3789 struct sk_buff *skb;
3790 unsigned int pktlen;
3791
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003792 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003793 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003794 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003795 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003796 skb = netdev_alloc_skb(dev, pktlen + 2);
3797 if (skb) {
3798 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003799 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003800
3801 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003802 dev->stats.rx_packets++;
3803 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003804 netif_rx(skb);
3805 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003806 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003807 }
3808
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003809 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003810 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003811
3812 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003813 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003814
3815 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003816 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3817 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003818 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003819 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003820
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003821 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003822 }
3823}
3824
3825/* MAC interrupt handler */
3826static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3827{
3828 struct net_device *dev = dev_id;
3829 struct macb *lp = netdev_priv(dev);
3830 u32 intstatus, ctl;
3831
3832 /* MAC Interrupt Status register indicates what interrupts are pending.
3833 * It is automatically cleared once read.
3834 */
3835 intstatus = macb_readl(lp, ISR);
3836
3837 /* Receive complete */
3838 if (intstatus & MACB_BIT(RCOMP))
3839 at91ether_rx(dev);
3840
3841 /* Transmit complete */
3842 if (intstatus & MACB_BIT(TCOMP)) {
3843 /* The TCOM bit is set even if the transmission failed */
3844 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003845 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003846
3847 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003848 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003849 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003850 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003851 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003852 dev->stats.tx_packets++;
3853 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003854 }
3855 netif_wake_queue(dev);
3856 }
3857
3858 /* Work-around for EMAC Errata section 41.3.1 */
3859 if (intstatus & MACB_BIT(RXUBR)) {
3860 ctl = macb_readl(lp, NCR);
3861 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003862 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003863 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3864 }
3865
3866 if (intstatus & MACB_BIT(ISR_ROVR))
3867 netdev_err(dev, "ROVR error\n");
3868
3869 return IRQ_HANDLED;
3870}
3871
3872#ifdef CONFIG_NET_POLL_CONTROLLER
3873static void at91ether_poll_controller(struct net_device *dev)
3874{
3875 unsigned long flags;
3876
3877 local_irq_save(flags);
3878 at91ether_interrupt(dev->irq, dev);
3879 local_irq_restore(flags);
3880}
3881#endif
3882
3883static const struct net_device_ops at91ether_netdev_ops = {
3884 .ndo_open = at91ether_open,
3885 .ndo_stop = at91ether_close,
3886 .ndo_start_xmit = at91ether_start_xmit,
3887 .ndo_get_stats = macb_get_stats,
3888 .ndo_set_rx_mode = macb_set_rx_mode,
3889 .ndo_set_mac_address = eth_mac_addr,
3890 .ndo_do_ioctl = macb_ioctl,
3891 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003892#ifdef CONFIG_NET_POLL_CONTROLLER
3893 .ndo_poll_controller = at91ether_poll_controller,
3894#endif
3895};
3896
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003897static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303898 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303899 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003900{
3901 int err;
3902
3903 *hclk = NULL;
3904 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303905 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05303906 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003907
3908 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3909 if (IS_ERR(*pclk))
3910 return PTR_ERR(*pclk);
3911
3912 err = clk_prepare_enable(*pclk);
3913 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003914 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003915 return err;
3916 }
3917
3918 return 0;
3919}
3920
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003921static int at91ether_init(struct platform_device *pdev)
3922{
3923 struct net_device *dev = platform_get_drvdata(pdev);
3924 struct macb *bp = netdev_priv(dev);
3925 int err;
3926 u32 reg;
3927
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003928 bp->queues[0].bp = bp;
3929
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003930 dev->netdev_ops = &at91ether_netdev_ops;
3931 dev->ethtool_ops = &macb_ethtool_ops;
3932
3933 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3934 0, dev->name, dev);
3935 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003936 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003937
3938 macb_writel(bp, NCR, 0);
3939
3940 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3941 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3942 reg |= MACB_BIT(RM9200_RMII);
3943
3944 macb_writel(bp, NCFGR, reg);
3945
3946 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003947}
3948
David S. Miller3cef5c52015-03-09 23:38:02 -04003949static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003950 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003951 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003952 .init = macb_init,
3953};
3954
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003955static const struct macb_config sama5d3macb_config = {
3956 .caps = MACB_CAPS_SG_DISABLED
3957 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3958 .clk_init = macb_clk_init,
3959 .init = macb_init,
3960};
3961
David S. Miller3cef5c52015-03-09 23:38:02 -04003962static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003963 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3964 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003965 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003966 .init = macb_init,
3967};
3968
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003969static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003970 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003971 .dma_burst_length = 16,
3972 .clk_init = macb_clk_init,
3973 .init = macb_init,
3974};
3975
David S. Miller3cef5c52015-03-09 23:38:02 -04003976static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003977 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003978 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003979 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003980 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003981 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003982 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003983};
3984
David S. Miller3cef5c52015-03-09 23:38:02 -04003985static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003986 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003987 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003988 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003989 .init = macb_init,
3990};
3991
David S. Miller3cef5c52015-03-09 23:38:02 -04003992static const struct macb_config emac_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303993 .caps = MACB_CAPS_NEEDS_RSTONUBR,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003994 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003995 .init = at91ether_init,
3996};
3997
Neil Armstronge611b5b2016-01-05 14:39:17 +01003998static const struct macb_config np4_config = {
3999 .caps = MACB_CAPS_USRIO_DISABLED,
4000 .clk_init = macb_clk_init,
4001 .init = macb_init,
4002};
David S. Miller36583eb2015-05-23 01:22:35 -04004003
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304004static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004005 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4006 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304007 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304008 .dma_burst_length = 16,
4009 .clk_init = macb_clk_init,
4010 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304011 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304012};
4013
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004014static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304015 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4016 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004017 .dma_burst_length = 16,
4018 .clk_init = macb_clk_init,
4019 .init = macb_init,
4020};
4021
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004022static const struct of_device_id macb_dt_ids[] = {
4023 { .compatible = "cdns,at32ap7000-macb" },
4024 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4025 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004026 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004027 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4028 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004029 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004030 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004031 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004032 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004033 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4034 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4035 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304036 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004037 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004038 { /* sentinel */ }
4039};
4040MODULE_DEVICE_TABLE(of, macb_dt_ids);
4041#endif /* CONFIG_OF */
4042
Bartosz Folta83a77e92016-12-14 06:39:15 +00004043static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004044 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4045 MACB_CAPS_JUMBO |
4046 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004047 .dma_burst_length = 16,
4048 .clk_init = macb_clk_init,
4049 .init = macb_init,
4050 .jumbo_max_len = 10240,
4051};
4052
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004053static int macb_probe(struct platform_device *pdev)
4054{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004055 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004056 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304057 struct clk **, struct clk **, struct clk **,
4058 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004059 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004060 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304061 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304062 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004063 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004064 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004065 struct phy_device *phydev;
4066 struct net_device *dev;
4067 struct resource *regs;
4068 void __iomem *mem;
4069 const char *mac;
4070 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304071 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004072
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004073 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4074 mem = devm_ioremap_resource(&pdev->dev, regs);
4075 if (IS_ERR(mem))
4076 return PTR_ERR(mem);
4077
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004078 if (np) {
4079 const struct of_device_id *match;
4080
4081 match = of_match_node(macb_dt_ids, np);
4082 if (match && match->data) {
4083 macb_config = match->data;
4084 clk_init = macb_config->clk_init;
4085 init = macb_config->init;
4086 }
4087 }
4088
Harini Katakamf5473d12019-03-01 16:20:33 +05304089 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004090 if (err)
4091 return err;
4092
Harini Katakamd54f89a2019-03-01 16:20:34 +05304093 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4094 pm_runtime_use_autosuspend(&pdev->dev);
4095 pm_runtime_get_noresume(&pdev->dev);
4096 pm_runtime_set_active(&pdev->dev);
4097 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004098 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004099
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004100 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004101 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004102 if (!dev) {
4103 err = -ENOMEM;
4104 goto err_disable_clocks;
4105 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004106
4107 dev->base_addr = regs->start;
4108
4109 SET_NETDEV_DEV(dev, &pdev->dev);
4110
4111 bp = netdev_priv(dev);
4112 bp->pdev = pdev;
4113 bp->dev = dev;
4114 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004115 bp->native_io = native_io;
4116 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004117 bp->macb_reg_readl = hw_readl_native;
4118 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004119 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004120 bp->macb_reg_readl = hw_readl;
4121 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004122 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004123 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004124 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004125 if (macb_config)
4126 bp->dma_burst_length = macb_config->dma_burst_length;
4127 bp->pclk = pclk;
4128 bp->hclk = hclk;
4129 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304130 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304131 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004132 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304133 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304134
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004135 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004136 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004137 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4138 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4139
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004140 spin_lock_init(&bp->lock);
4141
Nicolas Ferread783472015-03-31 15:02:02 +02004142 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004143 macb_configure_caps(bp, macb_config);
4144
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004145#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4146 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4147 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4148 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4149 }
4150#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004151 platform_set_drvdata(pdev, dev);
4152
4153 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004154 if (dev->irq < 0) {
4155 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004156 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004157 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004158
Jarod Wilson44770e12016-10-17 15:54:17 -04004159 /* MTU range: 68 - 1500 or 10240 */
4160 dev->min_mtu = GEM_MTU_MIN_SIZE;
4161 if (bp->caps & MACB_CAPS_JUMBO)
4162 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4163 else
4164 dev->max_mtu = ETH_DATA_LEN;
4165
Harini Katakam404cd082018-07-06 12:18:58 +05304166 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4167 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4168 if (val)
4169 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4170 macb_dma_desc_get_size(bp);
4171
4172 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4173 if (val)
4174 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4175 macb_dma_desc_get_size(bp);
4176 }
4177
Harini Katakame5010702019-01-29 15:20:03 +05304178 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4179 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4180 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4181
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004182 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004183 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4184 err = -EPROBE_DEFER;
4185 goto err_out_free_netdev;
4186 } else if (!IS_ERR(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004187 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004188 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004189 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004190 }
frederic RODO6c36a702007-07-12 19:07:24 +02004191
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004192 err = of_get_phy_mode(np);
Nicolas Ferre8b952742019-05-03 12:36:58 +02004193 if (err < 0)
4194 /* not found in DT, MII by default */
4195 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4196 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004197 bp->phy_interface = err;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004198
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004199 /* IP specific init */
4200 err = init(pdev);
4201 if (err)
4202 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004203
Florian Fainellicf669662016-05-02 18:38:45 -07004204 err = macb_mii_init(bp);
4205 if (err)
4206 goto err_out_free_netdev;
4207
Philippe Reynes0a912812016-06-22 00:32:35 +02004208 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004209
4210 netif_carrier_off(dev);
4211
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004212 err = register_netdev(dev);
4213 if (err) {
4214 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004215 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004216 }
4217
Harini Katakam032dc412018-01-27 12:09:01 +05304218 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4219 (unsigned long)bp);
4220
Florian Fainellicf669662016-05-02 18:38:45 -07004221 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004222
Bo Shen58798232014-09-13 01:57:49 +02004223 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4224 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4225 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004226
Harini Katakamd54f89a2019-03-01 16:20:34 +05304227 pm_runtime_mark_last_busy(&bp->pdev->dev);
4228 pm_runtime_put_autosuspend(&bp->pdev->dev);
4229
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004230 return 0;
4231
Florian Fainellicf669662016-05-02 18:38:45 -07004232err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004233 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004234 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004235 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004236 if (np && of_phy_is_fixed_link(np))
4237 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004238 mdiobus_free(bp->mii_bus);
4239
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004240err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004241 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004242
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004243err_disable_clocks:
4244 clk_disable_unprepare(tx_clk);
4245 clk_disable_unprepare(hclk);
4246 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304247 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304248 clk_disable_unprepare(tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304249 pm_runtime_disable(&pdev->dev);
4250 pm_runtime_set_suspended(&pdev->dev);
4251 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004252
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004253 return err;
4254}
4255
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004256static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004257{
4258 struct net_device *dev;
4259 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004260 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004261
4262 dev = platform_get_drvdata(pdev);
4263
4264 if (dev) {
4265 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004266 if (dev->phydev)
4267 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004268 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004269 if (np && of_phy_is_fixed_link(np))
4270 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004271 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004272 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004273
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004274 unregister_netdev(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304275 pm_runtime_disable(&pdev->dev);
4276 pm_runtime_dont_use_autosuspend(&pdev->dev);
4277 if (!pm_runtime_suspended(&pdev->dev)) {
4278 clk_disable_unprepare(bp->tx_clk);
4279 clk_disable_unprepare(bp->hclk);
4280 clk_disable_unprepare(bp->pclk);
4281 clk_disable_unprepare(bp->rx_clk);
4282 clk_disable_unprepare(bp->tsu_clk);
4283 pm_runtime_set_suspended(&pdev->dev);
4284 }
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004285 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004286 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004287 }
4288
4289 return 0;
4290}
4291
Michal Simekd23823d2015-01-23 09:36:03 +01004292static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004293{
Wolfram Sangce886a42018-10-21 22:00:14 +02004294 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004295 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304296 struct macb_queue *queue = bp->queues;
4297 unsigned long flags;
4298 unsigned int q;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004299
Harini Katakamde991c52019-03-01 16:20:35 +05304300 if (!netif_running(netdev))
4301 return 0;
4302
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004303
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004304 if (bp->wol & MACB_WOL_ENABLED) {
4305 macb_writel(bp, IER, MACB_BIT(WOL));
4306 macb_writel(bp, WOL, MACB_BIT(MAG));
4307 enable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304308 netif_device_detach(netdev);
4309 } else {
4310 netif_device_detach(netdev);
4311 for (q = 0, queue = bp->queues; q < bp->num_queues;
4312 ++q, ++queue)
4313 napi_disable(&queue->napi);
4314 phy_stop(netdev->phydev);
4315 phy_suspend(netdev->phydev);
4316 spin_lock_irqsave(&bp->lock, flags);
4317 macb_reset_hw(bp);
4318 spin_unlock_irqrestore(&bp->lock, flags);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004319
4320 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4321 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4322
4323 if (netdev->hw_features & NETIF_F_NTUPLE)
4324 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304325 }
4326
Harini Katakamde991c52019-03-01 16:20:35 +05304327 netif_carrier_off(netdev);
4328 if (bp->ptp_info)
4329 bp->ptp_info->ptp_remove(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304330 pm_runtime_force_suspend(dev);
4331
4332 return 0;
4333}
4334
4335static int __maybe_unused macb_resume(struct device *dev)
4336{
4337 struct net_device *netdev = dev_get_drvdata(dev);
4338 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304339 struct macb_queue *queue = bp->queues;
4340 unsigned int q;
4341
4342 if (!netif_running(netdev))
4343 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304344
4345 pm_runtime_force_resume(dev);
4346
4347 if (bp->wol & MACB_WOL_ENABLED) {
4348 macb_writel(bp, IDR, MACB_BIT(WOL));
4349 macb_writel(bp, WOL, 0);
4350 disable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304351 } else {
4352 macb_writel(bp, NCR, MACB_BIT(MPE));
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004353
4354 if (netdev->hw_features & NETIF_F_NTUPLE)
4355 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4356
4357 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4358 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4359
Harini Katakamde991c52019-03-01 16:20:35 +05304360 for (q = 0, queue = bp->queues; q < bp->num_queues;
4361 ++q, ++queue)
4362 napi_enable(&queue->napi);
4363 phy_resume(netdev->phydev);
4364 phy_init_hw(netdev->phydev);
4365 phy_start(netdev->phydev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304366 }
4367
Harini Katakamde991c52019-03-01 16:20:35 +05304368 bp->macbgem_ops.mog_init_rings(bp);
4369 macb_init_hw(bp);
4370 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004371 macb_restore_features(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304372 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304373 if (bp->ptp_info)
4374 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304375
4376 return 0;
4377}
4378
4379static int __maybe_unused macb_runtime_suspend(struct device *dev)
4380{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004381 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304382 struct macb *bp = netdev_priv(netdev);
4383
4384 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004385 clk_disable_unprepare(bp->tx_clk);
4386 clk_disable_unprepare(bp->hclk);
4387 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304388 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004389 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304390 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004391
4392 return 0;
4393}
4394
Harini Katakamd54f89a2019-03-01 16:20:34 +05304395static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004396{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004397 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004398 struct macb *bp = netdev_priv(netdev);
4399
Harini Katakamd54f89a2019-03-01 16:20:34 +05304400 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004401 clk_prepare_enable(bp->pclk);
4402 clk_prepare_enable(bp->hclk);
4403 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304404 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004405 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304406 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004407
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004408 return 0;
4409}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004410
Harini Katakamd54f89a2019-03-01 16:20:34 +05304411static const struct dev_pm_ops macb_pm_ops = {
4412 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4413 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4414};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004415
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004416static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004417 .probe = macb_probe,
4418 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004419 .driver = {
4420 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004421 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004422 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004423 },
4424};
4425
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004426module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004427
4428MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004429MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004430MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004431MODULE_ALIAS("platform:macb");