blob: 0d6288dd609a3b055ee300789e44cd6d4a1cdd4b [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>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010039#include "macb.h"
40
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000042#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050043
Zach Brownb410d132016-10-19 09:56:57 -050044#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050045#define MIN_RX_RING_SIZE 64
46#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000047#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050048 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010049
Zach Brownb410d132016-10-19 09:56:57 -050050#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050051#define MIN_TX_RING_SIZE 64
52#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000053#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050054 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
Nicolas Ferre909a8582012-11-19 06:00:21 +000056/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050057#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010058
Harini Katakame5010702019-01-29 15:20:03 +053059#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000060#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
61 | MACB_BIT(ISR_RLE) \
62 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000063#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
64 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000065
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000066/* Max length of transmit frame must be a multiple of 8 bytes */
67#define MACB_TX_LEN_ALIGN 8
68#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
69#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 +020070
Jarod Wilson44770e12016-10-17 15:54:17 -040071#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070072#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053073
Sergio Prado3e2a5e12016-02-09 12:07:16 -020074#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
75#define MACB_WOL_ENABLED (0x1 << 1)
76
Moritz Fischer64ec42f2016-03-29 19:11:12 -070077/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000078 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
79 */
80#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010081
Rafal Ozieblodc97a892017-01-27 15:08:20 +000082/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010083 * depends on hardware configuration:
84 *
85 * 1. dma address width 32 bits:
86 * word 1: 32 bit address of Data Buffer
87 * word 2: control
88 *
89 * 2. dma address width 64 bits:
90 * word 1: 32 bit address of Data Buffer
91 * word 2: control
92 * word 3: upper 32 bit address of Data Buffer
93 * word 4: unused
94 *
95 * 3. dma address width 32 bits with hardware timestamping:
96 * word 1: 32 bit address of Data Buffer
97 * word 2: control
98 * word 3: timestamp word 1
99 * word 4: timestamp word 2
100 *
101 * 4. dma address width 64 bits with hardware timestamping:
102 * word 1: 32 bit address of Data Buffer
103 * word 2: control
104 * word 3: upper 32 bit address of Data Buffer
105 * word 4: unused
106 * word 5: timestamp word 1
107 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000108 */
109static unsigned int macb_dma_desc_get_size(struct macb *bp)
110{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100111#ifdef MACB_EXT_DESC
112 unsigned int desc_size;
113
114 switch (bp->hw_dma_cap) {
115 case HW_DMA_CAP_64B:
116 desc_size = sizeof(struct macb_dma_desc)
117 + sizeof(struct macb_dma_desc_64);
118 break;
119 case HW_DMA_CAP_PTP:
120 desc_size = sizeof(struct macb_dma_desc)
121 + sizeof(struct macb_dma_desc_ptp);
122 break;
123 case HW_DMA_CAP_64B_PTP:
124 desc_size = sizeof(struct macb_dma_desc)
125 + sizeof(struct macb_dma_desc_64)
126 + sizeof(struct macb_dma_desc_ptp);
127 break;
128 default:
129 desc_size = sizeof(struct macb_dma_desc);
130 }
131 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000132#endif
133 return sizeof(struct macb_dma_desc);
134}
135
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100136static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000137{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100138#ifdef MACB_EXT_DESC
139 switch (bp->hw_dma_cap) {
140 case HW_DMA_CAP_64B:
141 case HW_DMA_CAP_PTP:
142 desc_idx <<= 1;
143 break;
144 case HW_DMA_CAP_64B_PTP:
145 desc_idx *= 3;
146 break;
147 default:
148 break;
149 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000150#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100151 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000152}
153
154#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
155static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
156{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100157 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
158 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
159 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000160}
161#endif
162
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000163/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500164static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000165{
Zach Brownb410d132016-10-19 09:56:57 -0500166 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000167}
168
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100169static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
170 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000171{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000172 index = macb_tx_ring_wrap(queue->bp, index);
173 index = macb_adj_dma_desc_idx(queue->bp, index);
174 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000175}
176
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100177static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
178 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179{
Zach Brownb410d132016-10-19 09:56:57 -0500180 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181}
182
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100183static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000184{
185 dma_addr_t offset;
186
Zach Brownb410d132016-10-19 09:56:57 -0500187 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000188 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000189
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100190 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000191}
192
Zach Brownb410d132016-10-19 09:56:57 -0500193static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000194{
Zach Brownb410d132016-10-19 09:56:57 -0500195 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000196}
197
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000198static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000199{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000200 index = macb_rx_ring_wrap(queue->bp, index);
201 index = macb_adj_dma_desc_idx(queue->bp, index);
202 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000203}
204
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000205static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000206{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000207 return queue->rx_buffers + queue->bp->rx_buffer_size *
208 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000209}
210
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300211/* I/O accessors */
212static u32 hw_readl_native(struct macb *bp, int offset)
213{
214 return __raw_readl(bp->regs + offset);
215}
216
217static void hw_writel_native(struct macb *bp, int offset, u32 value)
218{
219 __raw_writel(value, bp->regs + offset);
220}
221
222static u32 hw_readl(struct macb *bp, int offset)
223{
224 return readl_relaxed(bp->regs + offset);
225}
226
227static void hw_writel(struct macb *bp, int offset, u32 value)
228{
229 writel_relaxed(value, bp->regs + offset);
230}
231
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700232/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700233 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300234 * descriptor access.
235 */
236static bool hw_is_native_io(void __iomem *addr)
237{
238 u32 value = MACB_BIT(LLB);
239
240 __raw_writel(value, addr + MACB_NCR);
241 value = __raw_readl(addr + MACB_NCR);
242
243 /* Write 0 back to disable everything */
244 __raw_writel(0, addr + MACB_NCR);
245
246 return value == MACB_BIT(LLB);
247}
248
249static bool hw_is_gem(void __iomem *addr, bool native_io)
250{
251 u32 id;
252
253 if (native_io)
254 id = __raw_readl(addr + MACB_MID);
255 else
256 id = readl_relaxed(addr + MACB_MID);
257
258 return MACB_BFEXT(IDNUM, id) >= 0x2;
259}
260
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100261static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100262{
263 u32 bottom;
264 u16 top;
265
266 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000267 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100268 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000269 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000270
271 /* Clear unused address register sets */
272 macb_or_gem_writel(bp, SA2B, 0);
273 macb_or_gem_writel(bp, SA2T, 0);
274 macb_or_gem_writel(bp, SA3B, 0);
275 macb_or_gem_writel(bp, SA3T, 0);
276 macb_or_gem_writel(bp, SA4B, 0);
277 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100278}
279
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100280static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100281{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000282 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100283 u32 bottom;
284 u16 top;
285 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000286 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100287
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900288 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000289
Moritz Fischeraa50b552016-03-29 19:11:13 -0700290 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000291 for (i = 0; i < 4; i++) {
292 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
293 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000295 if (pdata && pdata->rev_eth_addr) {
296 addr[5] = bottom & 0xff;
297 addr[4] = (bottom >> 8) & 0xff;
298 addr[3] = (bottom >> 16) & 0xff;
299 addr[2] = (bottom >> 24) & 0xff;
300 addr[1] = top & 0xff;
301 addr[0] = (top & 0xff00) >> 8;
302 } else {
303 addr[0] = bottom & 0xff;
304 addr[1] = (bottom >> 8) & 0xff;
305 addr[2] = (bottom >> 16) & 0xff;
306 addr[3] = (bottom >> 24) & 0xff;
307 addr[4] = top & 0xff;
308 addr[5] = (top >> 8) & 0xff;
309 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100310
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000311 if (is_valid_ether_addr(addr)) {
312 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
313 return;
314 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700315 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000316
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300317 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000318 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100319}
320
frederic RODO6c36a702007-07-12 19:07:24 +0200321static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100322{
frederic RODO6c36a702007-07-12 19:07:24 +0200323 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100324 int value;
325
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100326 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
327 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200328 | MACB_BF(PHYA, mii_id)
329 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100330 | MACB_BF(CODE, MACB_MAN_CODE)));
331
frederic RODO6c36a702007-07-12 19:07:24 +0200332 /* wait for end of transfer */
333 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
334 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100335
336 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100337
338 return value;
339}
340
frederic RODO6c36a702007-07-12 19:07:24 +0200341static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
342 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100343{
frederic RODO6c36a702007-07-12 19:07:24 +0200344 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
346 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
347 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200348 | MACB_BF(PHYA, mii_id)
349 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100350 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200351 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100352
frederic RODO6c36a702007-07-12 19:07:24 +0200353 /* wait for end of transfer */
354 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
355 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100357 return 0;
358}
359
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800360/**
361 * macb_set_tx_clk() - Set a clock to a new frequency
362 * @clk Pointer to the clock to change
363 * @rate New frequency in Hz
364 * @dev Pointer to the struct net_device
365 */
366static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
367{
368 long ferr, rate, rate_rounded;
369
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100370 if (!clk)
371 return;
372
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800373 switch (speed) {
374 case SPEED_10:
375 rate = 2500000;
376 break;
377 case SPEED_100:
378 rate = 25000000;
379 break;
380 case SPEED_1000:
381 rate = 125000000;
382 break;
383 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800384 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800385 }
386
387 rate_rounded = clk_round_rate(clk, rate);
388 if (rate_rounded < 0)
389 return;
390
391 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
392 * is not satisfied.
393 */
394 ferr = abs(rate_rounded - rate);
395 ferr = DIV_ROUND_UP(ferr, rate / 100000);
396 if (ferr > 5)
397 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700398 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800399
400 if (clk_set_rate(clk, rate_rounded))
401 netdev_err(dev, "adjusting tx_clk failed.\n");
402}
403
frederic RODO6c36a702007-07-12 19:07:24 +0200404static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100405{
frederic RODO6c36a702007-07-12 19:07:24 +0200406 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200407 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200408 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200409 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100410
frederic RODO6c36a702007-07-12 19:07:24 +0200411 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100412
frederic RODO6c36a702007-07-12 19:07:24 +0200413 if (phydev->link) {
414 if ((bp->speed != phydev->speed) ||
415 (bp->duplex != phydev->duplex)) {
416 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100417
frederic RODO6c36a702007-07-12 19:07:24 +0200418 reg = macb_readl(bp, NCFGR);
419 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000420 if (macb_is_gem(bp))
421 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200422
423 if (phydev->duplex)
424 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900425 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200426 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200427 if (phydev->speed == SPEED_1000 &&
428 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000429 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200430
Patrice Vilchez140b7552012-10-31 06:04:50 +0000431 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200432
433 bp->speed = phydev->speed;
434 bp->duplex = phydev->duplex;
435 status_change = 1;
436 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100437 }
438
frederic RODO6c36a702007-07-12 19:07:24 +0200439 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700440 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200441 bp->speed = 0;
442 bp->duplex = -1;
443 }
444 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100445
frederic RODO6c36a702007-07-12 19:07:24 +0200446 status_change = 1;
447 }
448
449 spin_unlock_irqrestore(&bp->lock, flags);
450
451 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000452 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500453 /* Update the TX clock rate if and only if the link is
454 * up and there has been a link change.
455 */
456 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
457
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000458 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000459 netdev_info(dev, "link up (%d/%s)\n",
460 phydev->speed,
461 phydev->duplex == DUPLEX_FULL ?
462 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000463 } else {
464 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000465 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000466 }
frederic RODO6c36a702007-07-12 19:07:24 +0200467 }
468}
469
470/* based on au1000_eth. c*/
471static int macb_mii_probe(struct net_device *dev)
472{
473 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000474 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000475 struct phy_device *phydev;
Brad Mouring739de9a2018-03-13 16:32:13 -0500476 struct device_node *np;
477 int phy_irq, ret, i;
478
479 pdata = dev_get_platdata(&bp->pdev->dev);
480 np = bp->pdev->dev.of_node;
481 ret = 0;
482
483 if (np) {
484 if (of_phy_is_fixed_link(np)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500485 bp->phy_node = of_node_get(np);
486 } else {
Brad Mouring2105a5d2018-03-13 16:32:15 -0500487 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
488 /* fallback to standard phy registration if no
489 * phy-handle was found nor any phy found during
490 * dt phy registration
Brad Mouring739de9a2018-03-13 16:32:13 -0500491 */
Brad Mouring2105a5d2018-03-13 16:32:15 -0500492 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500493 for (i = 0; i < PHY_MAX_ADDR; i++) {
494 struct phy_device *phydev;
495
496 phydev = mdiobus_scan(bp->mii_bus, i);
497 if (IS_ERR(phydev) &&
498 PTR_ERR(phydev) != -ENODEV) {
499 ret = PTR_ERR(phydev);
500 break;
501 }
502 }
503
504 if (ret)
505 return -ENODEV;
506 }
507 }
508 }
frederic RODO6c36a702007-07-12 19:07:24 +0200509
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200510 if (bp->phy_node) {
511 phydev = of_phy_connect(dev, bp->phy_node,
512 &macb_handle_link_change, 0,
513 bp->phy_interface);
514 if (!phydev)
515 return -ENODEV;
516 } else {
517 phydev = phy_find_first(bp->mii_bus);
518 if (!phydev) {
519 netdev_err(dev, "no PHY found\n");
520 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000521 }
frederic RODO6c36a702007-07-12 19:07:24 +0200522
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200523 if (pdata) {
524 if (gpio_is_valid(pdata->phy_irq_pin)) {
525 ret = devm_gpio_request(&bp->pdev->dev,
526 pdata->phy_irq_pin, "phy int");
527 if (!ret) {
528 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
529 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
530 }
531 } else {
532 phydev->irq = PHY_POLL;
533 }
534 }
535
536 /* attach the mac to the phy */
537 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
538 bp->phy_interface);
539 if (ret) {
540 netdev_err(dev, "Could not attach to PHY\n");
541 return ret;
542 }
frederic RODO6c36a702007-07-12 19:07:24 +0200543 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100544
frederic RODO6c36a702007-07-12 19:07:24 +0200545 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200546 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Andrew Lunn58056c12018-09-12 01:53:11 +0200547 phy_set_max_speed(phydev, SPEED_1000);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000548 else
Andrew Lunn58056c12018-09-12 01:53:11 +0200549 phy_set_max_speed(phydev, SPEED_100);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100550
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500551 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
Andrew Lunn41124fa2018-09-12 01:53:14 +0200552 phy_remove_link_mode(phydev,
553 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100554
frederic RODO6c36a702007-07-12 19:07:24 +0200555 bp->link = 0;
556 bp->speed = 0;
557 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200558
559 return 0;
560}
561
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100562static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200563{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000564 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200565 struct device_node *np;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200566 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200567
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200568 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200569 macb_writel(bp, NCR, MACB_BIT(MPE));
570
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700571 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700572 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200573 err = -ENOMEM;
574 goto err_out;
575 }
576
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700577 bp->mii_bus->name = "MACB_mii_bus";
578 bp->mii_bus->read = &macb_mdio_read;
579 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000580 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700581 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700582 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700583 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900584 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700585
Jamie Iles91523942011-02-28 04:05:25 +0000586 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200587
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200588 np = bp->pdev->dev.of_node;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200589 if (np && of_phy_is_fixed_link(np)) {
590 if (of_phy_register_fixed_link(np) < 0) {
591 dev_err(&bp->pdev->dev,
592 "broken fixed-link specification %pOF\n", np);
593 goto err_out_free_mdiobus;
594 }
Brad Mouring739de9a2018-03-13 16:32:13 -0500595
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200596 err = mdiobus_register(bp->mii_bus);
597 } else {
598 if (pdata)
599 bp->mii_bus->phy_mask = pdata->phy_mask;
600
601 err = of_mdiobus_register(bp->mii_bus, np);
602 }
603
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200604 if (err)
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200605 goto err_out_free_fixed_link;
frederic RODO6c36a702007-07-12 19:07:24 +0200606
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200607 err = macb_mii_probe(bp->dev);
608 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200609 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200610
611 return 0;
612
613err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700614 mdiobus_unregister(bp->mii_bus);
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200615err_out_free_fixed_link:
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100616 if (np && of_phy_is_fixed_link(np))
617 of_phy_deregister_fixed_link(np);
Brad Mouring739de9a2018-03-13 16:32:13 -0500618err_out_free_mdiobus:
619 of_node_put(bp->phy_node);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700620 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200621err_out:
622 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100623}
624
625static void macb_update_stats(struct macb *bp)
626{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000627 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
628 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300629 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100630
631 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
632
Moritz Fischer96ec6312016-03-29 19:11:11 -0700633 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700634 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100635}
636
Nicolas Ferree86cd532012-10-31 06:04:57 +0000637static int macb_halt_tx(struct macb *bp)
638{
639 unsigned long halt_time, timeout;
640 u32 status;
641
642 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
643
644 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
645 do {
646 halt_time = jiffies;
647 status = macb_readl(bp, TSR);
648 if (!(status & MACB_BIT(TGO)))
649 return 0;
650
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800651 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000652 } while (time_before(halt_time, timeout));
653
654 return -ETIMEDOUT;
655}
656
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200657static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
658{
659 if (tx_skb->mapping) {
660 if (tx_skb->mapped_as_page)
661 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
662 tx_skb->size, DMA_TO_DEVICE);
663 else
664 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
665 tx_skb->size, DMA_TO_DEVICE);
666 tx_skb->mapping = 0;
667 }
668
669 if (tx_skb->skb) {
670 dev_kfree_skb_any(tx_skb->skb);
671 tx_skb->skb = NULL;
672 }
673}
674
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000675static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530676{
Harini Katakamfff80192016-08-09 13:15:53 +0530677#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000678 struct macb_dma_desc_64 *desc_64;
679
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100680 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000681 desc_64 = macb_64b_desc(bp, desc);
682 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200683 /* The low bits of RX address contain the RX_USED bit, clearing
684 * of which allows packet RX. Make sure the high bits are also
685 * visible to HW at that point.
686 */
687 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000688 }
Harini Katakamfff80192016-08-09 13:15:53 +0530689#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000690 desc->addr = lower_32_bits(addr);
691}
692
693static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
694{
695 dma_addr_t addr = 0;
696#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
697 struct macb_dma_desc_64 *desc_64;
698
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100699 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000700 desc_64 = macb_64b_desc(bp, desc);
701 addr = ((u64)(desc_64->addrh) << 32);
702 }
703#endif
704 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
705 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530706}
707
Nicolas Ferree86cd532012-10-31 06:04:57 +0000708static void macb_tx_error_task(struct work_struct *work)
709{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100710 struct macb_queue *queue = container_of(work, struct macb_queue,
711 tx_error_task);
712 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000713 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100714 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000715 struct sk_buff *skb;
716 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100717 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000718
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100719 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
720 (unsigned int)(queue - bp->queues),
721 queue->tx_tail, queue->tx_head);
722
723 /* Prevent the queue IRQ handlers from running: each of them may call
724 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
725 * As explained below, we have to halt the transmission before updating
726 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
727 * network engine about the macb/gem being halted.
728 */
729 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000730
731 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100732 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000733
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700734 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000735 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100736 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000737 */
738 if (macb_halt_tx(bp))
739 /* Just complain for now, reinitializing TX path can be good */
740 netdev_err(bp->dev, "BUG: halt tx timed out\n");
741
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700742 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000743 * Free transmit buffers in upper layer.
744 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100745 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
746 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000747
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100748 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000749 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100750 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000751 skb = tx_skb->skb;
752
753 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200754 /* skb is set for the last buffer of the frame */
755 while (!skb) {
756 macb_tx_unmap(bp, tx_skb);
757 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100758 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200759 skb = tx_skb->skb;
760 }
761
762 /* ctrl still refers to the first buffer descriptor
763 * since it's the only one written back by the hardware
764 */
765 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
766 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500767 macb_tx_ring_wrap(bp, tail),
768 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200769 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000770 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200771 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000772 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200773 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000774 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700775 /* "Buffers exhausted mid-frame" errors may only happen
776 * if the driver is buggy, so complain loudly about
777 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000778 */
779 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
780 netdev_err(bp->dev,
781 "BUG: TX buffers exhausted mid-frame\n");
782
783 desc->ctrl = ctrl | MACB_BIT(TX_USED);
784 }
785
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200786 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000787 }
788
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100789 /* Set end of TX queue */
790 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000791 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100792 desc->ctrl = MACB_BIT(TX_USED);
793
Nicolas Ferree86cd532012-10-31 06:04:57 +0000794 /* Make descriptor updates visible to hardware */
795 wmb();
796
797 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000798 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530799#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100800 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000801 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530802#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000803 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100804 queue->tx_head = 0;
805 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000806
807 /* Housework before enabling TX IRQ */
808 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100809 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
810
811 /* Now we are ready to start transmission again */
812 netif_tx_start_all_queues(bp->dev);
813 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
814
815 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000816}
817
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100818static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100819{
820 unsigned int tail;
821 unsigned int head;
822 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100823 struct macb *bp = queue->bp;
824 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100825
826 status = macb_readl(bp, TSR);
827 macb_writel(bp, TSR, status);
828
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000829 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100830 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000831
Nicolas Ferree86cd532012-10-31 06:04:57 +0000832 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700833 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100834
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100835 head = queue->tx_head;
836 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000837 struct macb_tx_skb *tx_skb;
838 struct sk_buff *skb;
839 struct macb_dma_desc *desc;
840 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100841
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100842 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000844 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100845 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000846
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000847 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100848
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200849 /* TX_USED bit is only set by hardware on the very first buffer
850 * descriptor of the transmitted frame.
851 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000852 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100853 break;
854
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200855 /* Process all buffers of the current transmitted frame */
856 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100857 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200858 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000859
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200860 /* First, update TX stats if needed */
861 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100862 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
863 /* skb now belongs to timestamp buffer
864 * and will be removed later
865 */
866 tx_skb->skb = NULL;
867 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200868 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500869 macb_tx_ring_wrap(bp, tail),
870 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200871 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000872 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200873 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000874 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200875 }
876
877 /* Now we can safely release resources */
878 macb_tx_unmap(bp, tx_skb);
879
880 /* skb is set only for the last buffer of the frame.
881 * WARNING: at this point skb has been freed by
882 * macb_tx_unmap().
883 */
884 if (skb)
885 break;
886 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100887 }
888
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100889 queue->tx_tail = tail;
890 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
891 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500892 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100893 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100894}
895
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000896static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000897{
898 unsigned int entry;
899 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000900 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000901 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000902 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000903
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000904 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
905 bp->rx_ring_size) > 0) {
906 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000907
908 /* Make hw descriptor updates visible to CPU */
909 rmb();
910
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000911 queue->rx_prepared_head++;
912 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000913
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000914 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000915 /* allocate sk_buff for this free entry in ring */
916 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700917 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000918 netdev_err(bp->dev,
919 "Unable to allocate sk_buff\n");
920 break;
921 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000922
923 /* now fill corresponding descriptor entry */
924 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700925 bp->rx_buffer_size,
926 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800927 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
928 dev_kfree_skb(skb);
929 break;
930 }
931
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000932 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000933
Zach Brownb410d132016-10-19 09:56:57 -0500934 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000935 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000936 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200937 /* Setting addr clears RX_USED and allows reception,
938 * make sure ctrl is cleared first to avoid a race.
939 */
940 dma_wmb();
941 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000942
943 /* properly align Ethernet header */
944 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530945 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000946 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200947 dma_wmb();
948 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000949 }
950 }
951
952 /* Make descriptor updates visible to hardware */
953 wmb();
954
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000955 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
956 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000957}
958
959/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000960static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000961 unsigned int end)
962{
963 unsigned int frag;
964
965 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000966 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700967
Nicolas Ferre4df95132013-06-04 21:57:12 +0000968 desc->addr &= ~MACB_BIT(RX_USED);
969 }
970
971 /* Make descriptor updates visible to hardware */
972 wmb();
973
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700974 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000975 * whatever caused this is updated, so we don't have to record
976 * anything.
977 */
978}
979
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000980static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000981{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000982 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000983 unsigned int len;
984 unsigned int entry;
985 struct sk_buff *skb;
986 struct macb_dma_desc *desc;
987 int count = 0;
988
989 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530990 u32 ctrl;
991 dma_addr_t addr;
992 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000993
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000994 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
995 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000996
997 /* Make hw descriptor updates visible to CPU */
998 rmb();
999
Harini Katakamfff80192016-08-09 13:15:53 +05301000 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001001 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001002
Harini Katakamfff80192016-08-09 13:15:53 +05301003 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001004 break;
1005
Anssi Hannula6e0af292018-12-17 15:05:41 +02001006 /* Ensure ctrl is at least as up-to-date as rxused */
1007 dma_rmb();
1008
1009 ctrl = desc->ctrl;
1010
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001011 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001012 count++;
1013
1014 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1015 netdev_err(bp->dev,
1016 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001017 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001018 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001019 break;
1020 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001021 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001022 if (unlikely(!skb)) {
1023 netdev_err(bp->dev,
1024 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001025 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001026 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001027 break;
1028 }
1029 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001030 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301031 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001032
1033 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1034
1035 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001036 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001037 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001038
1039 skb->protocol = eth_type_trans(skb, bp->dev);
1040 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001041 if (bp->dev->features & NETIF_F_RXCSUM &&
1042 !(bp->dev->flags & IFF_PROMISC) &&
1043 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1044 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001045
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001046 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001047 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001048 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001049 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001050
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001051 gem_ptp_do_rxstamp(bp, skb, desc);
1052
Nicolas Ferre4df95132013-06-04 21:57:12 +00001053#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1054 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1055 skb->len, skb->csum);
1056 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001057 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001058 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1059 skb->data, 32, true);
1060#endif
1061
1062 netif_receive_skb(skb);
1063 }
1064
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001065 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001066
1067 return count;
1068}
1069
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001070static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071 unsigned int last_frag)
1072{
1073 unsigned int len;
1074 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001075 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001077 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001078 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001079
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001080 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301081 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001082
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001083 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001084 macb_rx_ring_wrap(bp, first_frag),
1085 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001086
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001087 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001088 * first buffer. Since the header is 14 bytes, this makes the
1089 * payload word-aligned.
1090 *
1091 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1092 * the two padding bytes into the skb so that we avoid hitting
1093 * the slowpath in memcpy(), and pull them off afterwards.
1094 */
1095 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001096 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001097 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001098 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001099 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001100 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001101 if (frag == last_frag)
1102 break;
1103 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001104
1105 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001106 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001107
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001108 return 1;
1109 }
1110
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001111 offset = 0;
1112 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001113 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001114 skb_put(skb, len);
1115
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001116 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001117 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001118
1119 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001120 if (unlikely(frag != last_frag)) {
1121 dev_kfree_skb_any(skb);
1122 return -1;
1123 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001124 frag_len = len - offset;
1125 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001126 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001127 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001128 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001129 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001130 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001131 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001132
1133 if (frag == last_frag)
1134 break;
1135 }
1136
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001137 /* Make descriptor updates visible to hardware */
1138 wmb();
1139
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001140 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001141 skb->protocol = eth_type_trans(skb, bp->dev);
1142
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001143 bp->dev->stats.rx_packets++;
1144 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001145 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001146 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001147 netif_receive_skb(skb);
1148
1149 return 0;
1150}
1151
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001152static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001153{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001154 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001155 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001156 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001157 int i;
1158
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001159 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001160 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001161 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001162 macb_set_addr(bp, desc, addr);
1163 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001164 addr += bp->rx_buffer_size;
1165 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001166 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001167 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001168}
1169
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001170static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001171{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001172 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001173 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001174 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001175 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001176 int first_frag = -1;
1177
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001178 for (tail = queue->rx_tail; budget > 0; tail++) {
1179 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001180 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001181
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001182 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001183 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001184
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001185 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001186 break;
1187
Anssi Hannula6e0af292018-12-17 15:05:41 +02001188 /* Ensure ctrl is at least as up-to-date as addr */
1189 dma_rmb();
1190
1191 ctrl = desc->ctrl;
1192
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001193 if (ctrl & MACB_BIT(RX_SOF)) {
1194 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001195 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 first_frag = tail;
1197 }
1198
1199 if (ctrl & MACB_BIT(RX_EOF)) {
1200 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001201
1202 if (unlikely(first_frag == -1)) {
1203 reset_rx_queue = true;
1204 continue;
1205 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001206
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001207 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001208 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001209 if (unlikely(dropped < 0)) {
1210 reset_rx_queue = true;
1211 continue;
1212 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001213 if (!dropped) {
1214 received++;
1215 budget--;
1216 }
1217 }
1218 }
1219
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001220 if (unlikely(reset_rx_queue)) {
1221 unsigned long flags;
1222 u32 ctrl;
1223
1224 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1225
1226 spin_lock_irqsave(&bp->lock, flags);
1227
1228 ctrl = macb_readl(bp, NCR);
1229 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1230
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001231 macb_init_rx_ring(queue);
1232 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001233
1234 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1235
1236 spin_unlock_irqrestore(&bp->lock, flags);
1237 return received;
1238 }
1239
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001241 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001242 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001243 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001244
1245 return received;
1246}
1247
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001248static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001249{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001250 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1251 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001252 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001253 u32 status;
1254
1255 status = macb_readl(bp, RSR);
1256 macb_writel(bp, RSR, status);
1257
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001258 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001259 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001260
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001261 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001262 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001263 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001264
Nicolas Ferre8770e912013-02-12 11:08:48 +01001265 /* Packets received while interrupts were disabled */
1266 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001267 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001268 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001269 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001270 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001271 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301272 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001273 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001274 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001275
1276 /* TODO: Handle errors */
1277
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001278 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001279}
1280
Harini Katakam032dc412018-01-27 12:09:01 +05301281static void macb_hresp_error_task(unsigned long data)
1282{
1283 struct macb *bp = (struct macb *)data;
1284 struct net_device *dev = bp->dev;
1285 struct macb_queue *queue = bp->queues;
1286 unsigned int q;
1287 u32 ctrl;
1288
1289 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301290 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301291 MACB_TX_INT_FLAGS |
1292 MACB_BIT(HRESP));
1293 }
1294 ctrl = macb_readl(bp, NCR);
1295 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1296 macb_writel(bp, NCR, ctrl);
1297
1298 netif_tx_stop_all_queues(dev);
1299 netif_carrier_off(dev);
1300
1301 bp->macbgem_ops.mog_init_rings(bp);
1302
1303 /* Initialize TX and RX buffers */
1304 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1305 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1306#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1307 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1308 queue_writel(queue, RBQPH,
1309 upper_32_bits(queue->rx_ring_dma));
1310#endif
1311 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1312#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1313 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1314 queue_writel(queue, TBQPH,
1315 upper_32_bits(queue->tx_ring_dma));
1316#endif
1317
1318 /* Enable interrupts */
1319 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301320 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301321 MACB_TX_INT_FLAGS |
1322 MACB_BIT(HRESP));
1323 }
1324
1325 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1326 macb_writel(bp, NCR, ctrl);
1327
1328 netif_carrier_on(dev);
1329 netif_tx_start_all_queues(dev);
1330}
1331
Claudiu Beznea42983882018-12-17 10:02:42 +00001332static void macb_tx_restart(struct macb_queue *queue)
1333{
1334 unsigned int head = queue->tx_head;
1335 unsigned int tail = queue->tx_tail;
1336 struct macb *bp = queue->bp;
1337
1338 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1339 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1340
1341 if (head == tail)
1342 return;
1343
1344 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1345}
1346
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001347static irqreturn_t macb_interrupt(int irq, void *dev_id)
1348{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001349 struct macb_queue *queue = dev_id;
1350 struct macb *bp = queue->bp;
1351 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001352 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001353
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001354 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001355
1356 if (unlikely(!status))
1357 return IRQ_NONE;
1358
1359 spin_lock(&bp->lock);
1360
1361 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362 /* close possible race with dev_close */
1363 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001364 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001365 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1366 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001367 break;
1368 }
1369
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1371 (unsigned int)(queue - bp->queues),
1372 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001373
Harini Katakame5010702019-01-29 15:20:03 +05301374 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001375 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001376 * until we have processed the buffers. The
1377 * scheduling call may fail if the poll routine
1378 * is already scheduled, so disable interrupts
1379 * now.
1380 */
Harini Katakame5010702019-01-29 15:20:03 +05301381 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001382 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001383 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001384
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001385 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001386 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001387 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388 }
1389 }
1390
Nicolas Ferree86cd532012-10-31 06:04:57 +00001391 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001392 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1393 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001394
1395 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001396 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001397
Nicolas Ferree86cd532012-10-31 06:04:57 +00001398 break;
1399 }
1400
1401 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001402 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001403
Claudiu Beznea42983882018-12-17 10:02:42 +00001404 if (status & MACB_BIT(TXUBR))
1405 macb_tx_restart(queue);
1406
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001407 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001408 * add that if/when we get our hands on a full-blown MII PHY.
1409 */
1410
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001411 /* There is a hardware issue under heavy load where DMA can
1412 * stop, this causes endless "used buffer descriptor read"
1413 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301414 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1415 * section 16.7.4 for details. RXUBR is only enabled for
1416 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001417 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001418 if (status & MACB_BIT(RXUBR)) {
1419 ctrl = macb_readl(bp, NCR);
1420 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001421 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001422 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1423
1424 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001425 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001426 }
1427
Alexander Steinb19f7f72011-04-13 05:03:24 +00001428 if (status & MACB_BIT(ISR_ROVR)) {
1429 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001430 if (macb_is_gem(bp))
1431 bp->hw_stats.gem.rx_overruns++;
1432 else
1433 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001434
1435 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001436 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001437 }
1438
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001439 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301440 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001441 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001442
1443 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001444 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001445 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001446 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001447 }
1448
1449 spin_unlock(&bp->lock);
1450
1451 return IRQ_HANDLED;
1452}
1453
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001454#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001455/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001456 * to allow network i/o with interrupts disabled.
1457 */
1458static void macb_poll_controller(struct net_device *dev)
1459{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001460 struct macb *bp = netdev_priv(dev);
1461 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001462 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001463 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001464
1465 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001466 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1467 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001468 local_irq_restore(flags);
1469}
1470#endif
1471
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001472static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001473 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001474 struct sk_buff *skb,
1475 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001476{
1477 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001478 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001479 struct macb_tx_skb *tx_skb = NULL;
1480 struct macb_dma_desc *desc;
1481 unsigned int offset, size, count = 0;
1482 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001483 unsigned int eof = 1, mss_mfs = 0;
1484 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1485
1486 /* LSO */
1487 if (skb_shinfo(skb)->gso_size != 0) {
1488 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1489 /* UDP - UFO */
1490 lso_ctrl = MACB_LSO_UFO_ENABLE;
1491 else
1492 /* TCP - TSO */
1493 lso_ctrl = MACB_LSO_TSO_ENABLE;
1494 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001495
1496 /* First, map non-paged data */
1497 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001498
1499 /* first buffer length */
1500 size = hdrlen;
1501
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001502 offset = 0;
1503 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001504 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001505 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001506
1507 mapping = dma_map_single(&bp->pdev->dev,
1508 skb->data + offset,
1509 size, DMA_TO_DEVICE);
1510 if (dma_mapping_error(&bp->pdev->dev, mapping))
1511 goto dma_error;
1512
1513 /* Save info to properly release resources */
1514 tx_skb->skb = NULL;
1515 tx_skb->mapping = mapping;
1516 tx_skb->size = size;
1517 tx_skb->mapped_as_page = false;
1518
1519 len -= size;
1520 offset += size;
1521 count++;
1522 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001523
1524 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001525 }
1526
1527 /* Then, map paged data from fragments */
1528 for (f = 0; f < nr_frags; f++) {
1529 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1530
1531 len = skb_frag_size(frag);
1532 offset = 0;
1533 while (len) {
1534 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001535 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001536 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001537
1538 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1539 offset, size, DMA_TO_DEVICE);
1540 if (dma_mapping_error(&bp->pdev->dev, mapping))
1541 goto dma_error;
1542
1543 /* Save info to properly release resources */
1544 tx_skb->skb = NULL;
1545 tx_skb->mapping = mapping;
1546 tx_skb->size = size;
1547 tx_skb->mapped_as_page = true;
1548
1549 len -= size;
1550 offset += size;
1551 count++;
1552 tx_head++;
1553 }
1554 }
1555
1556 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001557 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001558 netdev_err(bp->dev, "BUG! empty skb!\n");
1559 return 0;
1560 }
1561
1562 /* This is the last buffer of the frame: save socket buffer */
1563 tx_skb->skb = skb;
1564
1565 /* Update TX ring: update buffer descriptors in reverse order
1566 * to avoid race condition
1567 */
1568
1569 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1570 * to set the end of TX queue
1571 */
1572 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001573 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001574 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001575 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001576 desc->ctrl = ctrl;
1577
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001578 if (lso_ctrl) {
1579 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1580 /* include header and FCS in value given to h/w */
1581 mss_mfs = skb_shinfo(skb)->gso_size +
1582 skb_transport_offset(skb) +
1583 ETH_FCS_LEN;
1584 else /* TSO */ {
1585 mss_mfs = skb_shinfo(skb)->gso_size;
1586 /* TCP Sequence Number Source Select
1587 * can be set only for TSO
1588 */
1589 seq_ctrl = 0;
1590 }
1591 }
1592
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001593 do {
1594 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001595 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001596 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001597 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001598
1599 ctrl = (u32)tx_skb->size;
1600 if (eof) {
1601 ctrl |= MACB_BIT(TX_LAST);
1602 eof = 0;
1603 }
Zach Brownb410d132016-10-19 09:56:57 -05001604 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001605 ctrl |= MACB_BIT(TX_WRAP);
1606
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001607 /* First descriptor is header descriptor */
1608 if (i == queue->tx_head) {
1609 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1610 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001611 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1612 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1613 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001614 } else
1615 /* Only set MSS/MFS on payload descriptors
1616 * (second or later descriptor)
1617 */
1618 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1619
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001620 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001621 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001622 /* desc->addr must be visible to hardware before clearing
1623 * 'TX_USED' bit in desc->ctrl.
1624 */
1625 wmb();
1626 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001627 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001628
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001629 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001630
1631 return count;
1632
1633dma_error:
1634 netdev_err(bp->dev, "TX DMA map failed\n");
1635
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001636 for (i = queue->tx_head; i != tx_head; i++) {
1637 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001638
1639 macb_tx_unmap(bp, tx_skb);
1640 }
1641
1642 return 0;
1643}
1644
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001645static netdev_features_t macb_features_check(struct sk_buff *skb,
1646 struct net_device *dev,
1647 netdev_features_t features)
1648{
1649 unsigned int nr_frags, f;
1650 unsigned int hdrlen;
1651
1652 /* Validate LSO compatibility */
1653
1654 /* there is only one buffer */
1655 if (!skb_is_nonlinear(skb))
1656 return features;
1657
1658 /* length of header */
1659 hdrlen = skb_transport_offset(skb);
1660 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1661 hdrlen += tcp_hdrlen(skb);
1662
1663 /* For LSO:
1664 * When software supplies two or more payload buffers all payload buffers
1665 * apart from the last must be a multiple of 8 bytes in size.
1666 */
1667 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1668 return features & ~MACB_NETIF_LSO;
1669
1670 nr_frags = skb_shinfo(skb)->nr_frags;
1671 /* No need to check last fragment */
1672 nr_frags--;
1673 for (f = 0; f < nr_frags; f++) {
1674 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1675
1676 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1677 return features & ~MACB_NETIF_LSO;
1678 }
1679 return features;
1680}
1681
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001682static inline int macb_clear_csum(struct sk_buff *skb)
1683{
1684 /* no change for packets without checksum offloading */
1685 if (skb->ip_summed != CHECKSUM_PARTIAL)
1686 return 0;
1687
1688 /* make sure we can modify the header */
1689 if (unlikely(skb_cow_head(skb, 0)))
1690 return -1;
1691
1692 /* initialize checksum field
1693 * This is required - at least for Zynq, which otherwise calculates
1694 * wrong UDP header checksums for UDP packets with UDP data len <=2
1695 */
1696 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1697 return 0;
1698}
1699
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001700static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1701{
1702 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1703 int padlen = ETH_ZLEN - (*skb)->len;
1704 int headroom = skb_headroom(*skb);
1705 int tailroom = skb_tailroom(*skb);
1706 struct sk_buff *nskb;
1707 u32 fcs;
1708
1709 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1710 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1711 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1712 return 0;
1713
1714 if (padlen <= 0) {
1715 /* FCS could be appeded to tailroom. */
1716 if (tailroom >= ETH_FCS_LEN)
1717 goto add_fcs;
1718 /* FCS could be appeded by moving data to headroom. */
1719 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1720 padlen = 0;
1721 /* No room for FCS, need to reallocate skb. */
1722 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001723 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001724 } else {
1725 /* Add room for FCS. */
1726 padlen += ETH_FCS_LEN;
1727 }
1728
1729 if (!cloned && headroom + tailroom >= padlen) {
1730 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1731 skb_set_tail_pointer(*skb, (*skb)->len);
1732 } else {
1733 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1734 if (!nskb)
1735 return -ENOMEM;
1736
1737 dev_kfree_skb_any(*skb);
1738 *skb = nskb;
1739 }
1740
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001741 if (padlen > ETH_FCS_LEN)
1742 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001743
1744add_fcs:
1745 /* set FCS to packet */
1746 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1747 fcs = ~fcs;
1748
1749 skb_put_u8(*skb, fcs & 0xff);
1750 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1751 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1752 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1753
1754 return 0;
1755}
1756
Claudiu Beznead1c38952018-08-07 12:25:12 +03001757static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001758{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001759 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001760 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001761 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001762 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001763 unsigned int desc_cnt, nr_frags, frag_size, f;
1764 unsigned int hdrlen;
1765 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001766 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001767
Claudiu Beznea33729f22018-08-07 12:25:13 +03001768 if (macb_clear_csum(skb)) {
1769 dev_kfree_skb_any(skb);
1770 return ret;
1771 }
1772
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001773 if (macb_pad_and_fcs(&skb, dev)) {
1774 dev_kfree_skb_any(skb);
1775 return ret;
1776 }
1777
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001778 is_lso = (skb_shinfo(skb)->gso_size != 0);
1779
1780 if (is_lso) {
1781 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1782
1783 /* length of headers */
1784 if (is_udp)
1785 /* only queue eth + ip headers separately for UDP */
1786 hdrlen = skb_transport_offset(skb);
1787 else
1788 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1789 if (skb_headlen(skb) < hdrlen) {
1790 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1791 /* if this is required, would need to copy to single buffer */
1792 return NETDEV_TX_BUSY;
1793 }
1794 } else
1795 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001796
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001797#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1798 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001799 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1800 queue_index, skb->len, skb->head, skb->data,
1801 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001802 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1803 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001804#endif
1805
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001806 /* Count how many TX buffer descriptors are needed to send this
1807 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001808 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001809 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001810 if (is_lso && (skb_headlen(skb) > hdrlen))
1811 /* extra header descriptor if also payload in first buffer */
1812 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1813 else
1814 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001815 nr_frags = skb_shinfo(skb)->nr_frags;
1816 for (f = 0; f < nr_frags; f++) {
1817 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001818 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001819 }
1820
Dongdong Deng48719532009-08-23 19:49:07 -07001821 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001822
1823 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001824 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001825 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001826 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001827 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001828 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001829 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001830 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001831 }
1832
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001833 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001834 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001835 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001836 goto unlock;
1837 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001838
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001839 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001840 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001841 skb_tx_timestamp(skb);
1842
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001843 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1844
Zach Brownb410d132016-10-19 09:56:57 -05001845 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001846 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001847
Soren Brinkmann92030902014-03-04 08:46:39 -08001848unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001849 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001850
Claudiu Beznead1c38952018-08-07 12:25:12 +03001851 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001852}
1853
Nicolas Ferre4df95132013-06-04 21:57:12 +00001854static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001855{
1856 if (!macb_is_gem(bp)) {
1857 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1858 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001859 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001860
Nicolas Ferre1b447912013-06-04 21:57:11 +00001861 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001862 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001863 "RX buffer must be multiple of %d bytes, expanding\n",
1864 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001865 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001866 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001867 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001868 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001869
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001870 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001871 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001872}
1873
Nicolas Ferre4df95132013-06-04 21:57:12 +00001874static void gem_free_rx_buffers(struct macb *bp)
1875{
1876 struct sk_buff *skb;
1877 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001878 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001879 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001880 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001881 int i;
1882
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001883 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1884 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001885 continue;
1886
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001887 for (i = 0; i < bp->rx_ring_size; i++) {
1888 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001889
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001890 if (!skb)
1891 continue;
1892
1893 desc = macb_rx_desc(queue, i);
1894 addr = macb_get_addr(bp, desc);
1895
1896 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1897 DMA_FROM_DEVICE);
1898 dev_kfree_skb_any(skb);
1899 skb = NULL;
1900 }
1901
1902 kfree(queue->rx_skbuff);
1903 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001904 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001905}
1906
1907static void macb_free_rx_buffers(struct macb *bp)
1908{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001909 struct macb_queue *queue = &bp->queues[0];
1910
1911 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001912 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001913 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001914 queue->rx_buffers, queue->rx_buffers_dma);
1915 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001916 }
1917}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001918
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001919static void macb_free_consistent(struct macb *bp)
1920{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001921 struct macb_queue *queue;
1922 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301923 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001924
Nicolas Ferre4df95132013-06-04 21:57:12 +00001925 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001926
1927 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1928 kfree(queue->tx_skb);
1929 queue->tx_skb = NULL;
1930 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301931 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1932 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001933 queue->tx_ring, queue->tx_ring_dma);
1934 queue->tx_ring = NULL;
1935 }
Harini Katakame50b7702018-07-06 12:18:57 +05301936 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301937 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1938 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301939 queue->rx_ring, queue->rx_ring_dma);
1940 queue->rx_ring = NULL;
1941 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001942 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001943}
1944
1945static int gem_alloc_rx_buffers(struct macb *bp)
1946{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001947 struct macb_queue *queue;
1948 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001949 int size;
1950
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001951 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1952 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1953 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1954 if (!queue->rx_skbuff)
1955 return -ENOMEM;
1956 else
1957 netdev_dbg(bp->dev,
1958 "Allocated %d RX struct sk_buff entries at %p\n",
1959 bp->rx_ring_size, queue->rx_skbuff);
1960 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001961 return 0;
1962}
1963
1964static int macb_alloc_rx_buffers(struct macb *bp)
1965{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001966 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001967 int size;
1968
Zach Brownb410d132016-10-19 09:56:57 -05001969 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001970 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1971 &queue->rx_buffers_dma, GFP_KERNEL);
1972 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001973 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001974
1975 netdev_dbg(bp->dev,
1976 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001977 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001978 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001979}
1980
1981static int macb_alloc_consistent(struct macb *bp)
1982{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001983 struct macb_queue *queue;
1984 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001985 int size;
1986
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001987 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05301988 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001989 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1990 &queue->tx_ring_dma,
1991 GFP_KERNEL);
1992 if (!queue->tx_ring)
1993 goto out_err;
1994 netdev_dbg(bp->dev,
1995 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1996 q, size, (unsigned long)queue->tx_ring_dma,
1997 queue->tx_ring);
1998
Zach Brownb410d132016-10-19 09:56:57 -05001999 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002000 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2001 if (!queue->tx_skb)
2002 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002003
Harini Katakam404cd082018-07-06 12:18:58 +05302004 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002005 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2006 &queue->rx_ring_dma, GFP_KERNEL);
2007 if (!queue->rx_ring)
2008 goto out_err;
2009 netdev_dbg(bp->dev,
2010 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2011 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002012 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002013 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002014 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002015
2016 return 0;
2017
2018out_err:
2019 macb_free_consistent(bp);
2020 return -ENOMEM;
2021}
2022
Nicolas Ferre4df95132013-06-04 21:57:12 +00002023static void gem_init_rings(struct macb *bp)
2024{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002025 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002026 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002027 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002028 int i;
2029
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002030 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002031 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002032 desc = macb_tx_desc(queue, i);
2033 macb_set_addr(bp, desc, 0);
2034 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002035 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002036 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002037 queue->tx_head = 0;
2038 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002039
2040 queue->rx_tail = 0;
2041 queue->rx_prepared_head = 0;
2042
2043 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002044 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002045
Nicolas Ferre4df95132013-06-04 21:57:12 +00002046}
2047
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048static void macb_init_rings(struct macb *bp)
2049{
2050 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002051 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002052
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002053 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002054
Zach Brownb410d132016-10-19 09:56:57 -05002055 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002056 desc = macb_tx_desc(&bp->queues[0], i);
2057 macb_set_addr(bp, desc, 0);
2058 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002059 }
Ben Shelton21d35152015-04-22 17:28:54 -05002060 bp->queues[0].tx_head = 0;
2061 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002062 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002063}
2064
2065static void macb_reset_hw(struct macb *bp)
2066{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002067 struct macb_queue *queue;
2068 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002069 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002070
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002071 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002072 * more gracefully?)
2073 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002074 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002075
2076 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002077 ctrl |= MACB_BIT(CLRSTAT);
2078
2079 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002080
2081 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002082 macb_writel(bp, TSR, -1);
2083 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084
2085 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002086 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2087 queue_writel(queue, IDR, -1);
2088 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002089 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2090 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002091 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002092}
2093
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002094static u32 gem_mdc_clk_div(struct macb *bp)
2095{
2096 u32 config;
2097 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2098
2099 if (pclk_hz <= 20000000)
2100 config = GEM_BF(CLK, GEM_CLK_DIV8);
2101 else if (pclk_hz <= 40000000)
2102 config = GEM_BF(CLK, GEM_CLK_DIV16);
2103 else if (pclk_hz <= 80000000)
2104 config = GEM_BF(CLK, GEM_CLK_DIV32);
2105 else if (pclk_hz <= 120000000)
2106 config = GEM_BF(CLK, GEM_CLK_DIV48);
2107 else if (pclk_hz <= 160000000)
2108 config = GEM_BF(CLK, GEM_CLK_DIV64);
2109 else
2110 config = GEM_BF(CLK, GEM_CLK_DIV96);
2111
2112 return config;
2113}
2114
2115static u32 macb_mdc_clk_div(struct macb *bp)
2116{
2117 u32 config;
2118 unsigned long pclk_hz;
2119
2120 if (macb_is_gem(bp))
2121 return gem_mdc_clk_div(bp);
2122
2123 pclk_hz = clk_get_rate(bp->pclk);
2124 if (pclk_hz <= 20000000)
2125 config = MACB_BF(CLK, MACB_CLK_DIV8);
2126 else if (pclk_hz <= 40000000)
2127 config = MACB_BF(CLK, MACB_CLK_DIV16);
2128 else if (pclk_hz <= 80000000)
2129 config = MACB_BF(CLK, MACB_CLK_DIV32);
2130 else
2131 config = MACB_BF(CLK, MACB_CLK_DIV64);
2132
2133 return config;
2134}
2135
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002136/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002137 * should program. We find the width from decoding the design configuration
2138 * register to find the maximum supported data bus width.
2139 */
2140static u32 macb_dbw(struct macb *bp)
2141{
2142 if (!macb_is_gem(bp))
2143 return 0;
2144
2145 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2146 case 4:
2147 return GEM_BF(DBW, GEM_DBW128);
2148 case 2:
2149 return GEM_BF(DBW, GEM_DBW64);
2150 case 1:
2151 default:
2152 return GEM_BF(DBW, GEM_DBW32);
2153 }
2154}
2155
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002156/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002157 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002158 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002159 * (if not supported by FIFO, it will fallback to default)
2160 * - set both rx/tx packet buffers to full memory size
2161 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002162 */
2163static void macb_configure_dma(struct macb *bp)
2164{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002165 struct macb_queue *queue;
2166 u32 buffer_size;
2167 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002168 u32 dmacfg;
2169
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002170 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002171 if (macb_is_gem(bp)) {
2172 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002173 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2174 if (q)
2175 queue_writel(queue, RBQS, buffer_size);
2176 else
2177 dmacfg |= GEM_BF(RXBS, buffer_size);
2178 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002179 if (bp->dma_burst_length)
2180 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002181 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302182 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302183
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002184 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302185 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2186 else
2187 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2188
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002189 if (bp->dev->features & NETIF_F_HW_CSUM)
2190 dmacfg |= GEM_BIT(TXCOEN);
2191 else
2192 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302193
Michal Simekbd620722018-09-25 08:32:50 +02002194 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302195#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002196 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002197 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302198#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002199#ifdef CONFIG_MACB_USE_HWSTAMP
2200 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2201 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2202#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002203 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2204 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002205 gem_writel(bp, DMACFG, dmacfg);
2206 }
2207}
2208
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002209static void macb_init_hw(struct macb *bp)
2210{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002211 struct macb_queue *queue;
2212 unsigned int q;
2213
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002214 u32 config;
2215
2216 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002217 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002218
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002219 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302220 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2221 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002222 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223 config |= MACB_BIT(PAE); /* PAuse Enable */
2224 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002225 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302226 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2227 else
2228 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002229 if (bp->dev->flags & IFF_PROMISC)
2230 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002231 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2232 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002233 if (!(bp->dev->flags & IFF_BROADCAST))
2234 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002235 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002237 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302238 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002239 bp->speed = SPEED_10;
2240 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302241 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002242 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302243 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002244
Jamie Iles0116da42011-03-14 17:38:30 +00002245 macb_configure_dma(bp);
2246
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002247 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002248 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002249 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2250#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2251 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2252 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2253#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002254 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302255#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002256 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002257 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302258#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002259
2260 /* Enable interrupts */
2261 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05302262 bp->rx_intr_mask |
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002263 MACB_TX_INT_FLAGS |
2264 MACB_BIT(HRESP));
2265 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002266
2267 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002268 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002269}
2270
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002271/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002272 * locations in the memory map. The least significant bits are stored
2273 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2274 *
2275 * The unicast hash enable and the multicast hash enable bits in the
2276 * network configuration register enable the reception of hash matched
2277 * frames. The destination address is reduced to a 6 bit index into
2278 * the 64 bit hash register using the following hash function. The
2279 * hash function is an exclusive or of every sixth bit of the
2280 * destination address.
2281 *
2282 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2283 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2284 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2285 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2286 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2287 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2288 *
2289 * da[0] represents the least significant bit of the first byte
2290 * received, that is, the multicast/unicast indicator, and da[47]
2291 * represents the most significant bit of the last byte received. If
2292 * the hash index, hi[n], points to a bit that is set in the hash
2293 * register then the frame will be matched according to whether the
2294 * frame is multicast or unicast. A multicast match will be signalled
2295 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2296 * index points to a bit set in the hash register. A unicast match
2297 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2298 * and the hash index points to a bit set in the hash register. To
2299 * receive all multicast frames, the hash register should be set with
2300 * all ones and the multicast hash enable bit should be set in the
2301 * network configuration register.
2302 */
2303
2304static inline int hash_bit_value(int bitnr, __u8 *addr)
2305{
2306 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2307 return 1;
2308 return 0;
2309}
2310
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002311/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002312static int hash_get_index(__u8 *addr)
2313{
2314 int i, j, bitval;
2315 int hash_index = 0;
2316
2317 for (j = 0; j < 6; j++) {
2318 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002319 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002320
2321 hash_index |= (bitval << j);
2322 }
2323
2324 return hash_index;
2325}
2326
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002327/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002328static void macb_sethashtable(struct net_device *dev)
2329{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002330 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002331 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002332 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002333 struct macb *bp = netdev_priv(dev);
2334
Moritz Fischeraa50b552016-03-29 19:11:13 -07002335 mc_filter[0] = 0;
2336 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002337
Jiri Pirko22bedad32010-04-01 21:22:57 +00002338 netdev_for_each_mc_addr(ha, dev) {
2339 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002340 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2341 }
2342
Jamie Ilesf75ba502011-11-08 10:12:32 +00002343 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2344 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002345}
2346
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002347/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002348static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002349{
2350 unsigned long cfg;
2351 struct macb *bp = netdev_priv(dev);
2352
2353 cfg = macb_readl(bp, NCFGR);
2354
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002355 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002356 /* Enable promiscuous mode */
2357 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002358
2359 /* Disable RX checksum offload */
2360 if (macb_is_gem(bp))
2361 cfg &= ~GEM_BIT(RXCOEN);
2362 } else {
2363 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002364 cfg &= ~MACB_BIT(CAF);
2365
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002366 /* Enable RX checksum offload only if requested */
2367 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2368 cfg |= GEM_BIT(RXCOEN);
2369 }
2370
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002371 if (dev->flags & IFF_ALLMULTI) {
2372 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002373 macb_or_gem_writel(bp, HRB, -1);
2374 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002375 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002376 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002377 /* Enable specific multicasts */
2378 macb_sethashtable(dev);
2379 cfg |= MACB_BIT(NCFGR_MTI);
2380 } else if (dev->flags & (~IFF_ALLMULTI)) {
2381 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002382 macb_or_gem_writel(bp, HRB, 0);
2383 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002384 cfg &= ~MACB_BIT(NCFGR_MTI);
2385 }
2386
2387 macb_writel(bp, NCFGR, cfg);
2388}
2389
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002390static int macb_open(struct net_device *dev)
2391{
2392 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002393 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002394 struct macb_queue *queue;
2395 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002396 int err;
2397
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002398 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002399
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002400 /* carrier starts down */
2401 netif_carrier_off(dev);
2402
frederic RODO6c36a702007-07-12 19:07:24 +02002403 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002404 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002405 return -EAGAIN;
2406
Nicolas Ferre1b447912013-06-04 21:57:11 +00002407 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002408 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002409
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002410 err = macb_alloc_consistent(bp);
2411 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002412 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2413 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002414 return err;
2415 }
2416
Nicolas Ferre4df95132013-06-04 21:57:12 +00002417 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002418 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002419
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002420 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2421 napi_enable(&queue->napi);
2422
frederic RODO6c36a702007-07-12 19:07:24 +02002423 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002424 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002425
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002426 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002427
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002428 if (bp->ptp_info)
2429 bp->ptp_info->ptp_init(dev);
2430
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002431 return 0;
2432}
2433
2434static int macb_close(struct net_device *dev)
2435{
2436 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002437 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002438 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002439 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002440
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002441 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002442
2443 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2444 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002445
Philippe Reynes0a912812016-06-22 00:32:35 +02002446 if (dev->phydev)
2447 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002448
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002449 spin_lock_irqsave(&bp->lock, flags);
2450 macb_reset_hw(bp);
2451 netif_carrier_off(dev);
2452 spin_unlock_irqrestore(&bp->lock, flags);
2453
2454 macb_free_consistent(bp);
2455
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002456 if (bp->ptp_info)
2457 bp->ptp_info->ptp_remove(dev);
2458
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002459 return 0;
2460}
2461
Harini Katakama5898ea2015-05-06 22:27:18 +05302462static int macb_change_mtu(struct net_device *dev, int new_mtu)
2463{
Harini Katakama5898ea2015-05-06 22:27:18 +05302464 if (netif_running(dev))
2465 return -EBUSY;
2466
Harini Katakama5898ea2015-05-06 22:27:18 +05302467 dev->mtu = new_mtu;
2468
2469 return 0;
2470}
2471
Jamie Ilesa494ed82011-03-09 16:26:35 +00002472static void gem_update_stats(struct macb *bp)
2473{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002474 struct macb_queue *queue;
2475 unsigned int i, q, idx;
2476 unsigned long *stat;
2477
Jamie Ilesa494ed82011-03-09 16:26:35 +00002478 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002479
Xander Huff3ff13f12015-01-13 16:15:51 -06002480 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2481 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002482 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002483
2484 bp->ethtool_stats[i] += val;
2485 *p += val;
2486
2487 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2488 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002489 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002490 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002491 *(++p) += val;
2492 }
2493 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002494
2495 idx = GEM_STATS_LEN;
2496 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2497 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2498 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002499}
2500
2501static struct net_device_stats *gem_get_stats(struct macb *bp)
2502{
2503 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002504 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002505
2506 gem_update_stats(bp);
2507
2508 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2509 hwstat->rx_alignment_errors +
2510 hwstat->rx_resource_errors +
2511 hwstat->rx_overruns +
2512 hwstat->rx_oversize_frames +
2513 hwstat->rx_jabbers +
2514 hwstat->rx_undersized_frames +
2515 hwstat->rx_length_field_frame_errors);
2516 nstat->tx_errors = (hwstat->tx_late_collisions +
2517 hwstat->tx_excessive_collisions +
2518 hwstat->tx_underrun +
2519 hwstat->tx_carrier_sense_errors);
2520 nstat->multicast = hwstat->rx_multicast_frames;
2521 nstat->collisions = (hwstat->tx_single_collision_frames +
2522 hwstat->tx_multiple_collision_frames +
2523 hwstat->tx_excessive_collisions);
2524 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2525 hwstat->rx_jabbers +
2526 hwstat->rx_undersized_frames +
2527 hwstat->rx_length_field_frame_errors);
2528 nstat->rx_over_errors = hwstat->rx_resource_errors;
2529 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2530 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2531 nstat->rx_fifo_errors = hwstat->rx_overruns;
2532 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2533 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2534 nstat->tx_fifo_errors = hwstat->tx_underrun;
2535
2536 return nstat;
2537}
2538
Xander Huff3ff13f12015-01-13 16:15:51 -06002539static void gem_get_ethtool_stats(struct net_device *dev,
2540 struct ethtool_stats *stats, u64 *data)
2541{
2542 struct macb *bp;
2543
2544 bp = netdev_priv(dev);
2545 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002546 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2547 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002548}
2549
2550static int gem_get_sset_count(struct net_device *dev, int sset)
2551{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002552 struct macb *bp = netdev_priv(dev);
2553
Xander Huff3ff13f12015-01-13 16:15:51 -06002554 switch (sset) {
2555 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002556 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002557 default:
2558 return -EOPNOTSUPP;
2559 }
2560}
2561
2562static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2563{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002564 char stat_string[ETH_GSTRING_LEN];
2565 struct macb *bp = netdev_priv(dev);
2566 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002567 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002568 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002569
2570 switch (sset) {
2571 case ETH_SS_STATS:
2572 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2573 memcpy(p, gem_statistics[i].stat_string,
2574 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002575
2576 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2577 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2578 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2579 q, queue_statistics[i].stat_string);
2580 memcpy(p, stat_string, ETH_GSTRING_LEN);
2581 }
2582 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002583 break;
2584 }
2585}
2586
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002587static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002588{
2589 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002590 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002591 struct macb_stats *hwstat = &bp->hw_stats.macb;
2592
2593 if (macb_is_gem(bp))
2594 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002595
frederic RODO6c36a702007-07-12 19:07:24 +02002596 /* read stats from hardware */
2597 macb_update_stats(bp);
2598
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002599 /* Convert HW stats into netdevice stats */
2600 nstat->rx_errors = (hwstat->rx_fcs_errors +
2601 hwstat->rx_align_errors +
2602 hwstat->rx_resource_errors +
2603 hwstat->rx_overruns +
2604 hwstat->rx_oversize_pkts +
2605 hwstat->rx_jabbers +
2606 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002607 hwstat->rx_length_mismatch);
2608 nstat->tx_errors = (hwstat->tx_late_cols +
2609 hwstat->tx_excessive_cols +
2610 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002611 hwstat->tx_carrier_errors +
2612 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002613 nstat->collisions = (hwstat->tx_single_cols +
2614 hwstat->tx_multiple_cols +
2615 hwstat->tx_excessive_cols);
2616 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2617 hwstat->rx_jabbers +
2618 hwstat->rx_undersize_pkts +
2619 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002620 nstat->rx_over_errors = hwstat->rx_resource_errors +
2621 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002622 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2623 nstat->rx_frame_errors = hwstat->rx_align_errors;
2624 nstat->rx_fifo_errors = hwstat->rx_overruns;
2625 /* XXX: What does "missed" mean? */
2626 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2627 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2628 nstat->tx_fifo_errors = hwstat->tx_underruns;
2629 /* Don't know about heartbeat or window errors... */
2630
2631 return nstat;
2632}
2633
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002634static int macb_get_regs_len(struct net_device *netdev)
2635{
2636 return MACB_GREGS_NBR * sizeof(u32);
2637}
2638
2639static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2640 void *p)
2641{
2642 struct macb *bp = netdev_priv(dev);
2643 unsigned int tail, head;
2644 u32 *regs_buff = p;
2645
2646 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2647 | MACB_GREGS_VERSION;
2648
Zach Brownb410d132016-10-19 09:56:57 -05002649 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2650 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002651
2652 regs_buff[0] = macb_readl(bp, NCR);
2653 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2654 regs_buff[2] = macb_readl(bp, NSR);
2655 regs_buff[3] = macb_readl(bp, TSR);
2656 regs_buff[4] = macb_readl(bp, RBQP);
2657 regs_buff[5] = macb_readl(bp, TBQP);
2658 regs_buff[6] = macb_readl(bp, RSR);
2659 regs_buff[7] = macb_readl(bp, IMR);
2660
2661 regs_buff[8] = tail;
2662 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002663 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2664 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002665
Neil Armstrongce721a72016-01-05 14:39:16 +01002666 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2667 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002668 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002669 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002670}
2671
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002672static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2673{
2674 struct macb *bp = netdev_priv(netdev);
2675
2676 wol->supported = 0;
2677 wol->wolopts = 0;
2678
2679 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2680 wol->supported = WAKE_MAGIC;
2681
2682 if (bp->wol & MACB_WOL_ENABLED)
2683 wol->wolopts |= WAKE_MAGIC;
2684 }
2685}
2686
2687static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2688{
2689 struct macb *bp = netdev_priv(netdev);
2690
2691 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2692 (wol->wolopts & ~WAKE_MAGIC))
2693 return -EOPNOTSUPP;
2694
2695 if (wol->wolopts & WAKE_MAGIC)
2696 bp->wol |= MACB_WOL_ENABLED;
2697 else
2698 bp->wol &= ~MACB_WOL_ENABLED;
2699
2700 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2701
2702 return 0;
2703}
2704
Zach Brown8441bb32016-10-19 09:56:58 -05002705static void macb_get_ringparam(struct net_device *netdev,
2706 struct ethtool_ringparam *ring)
2707{
2708 struct macb *bp = netdev_priv(netdev);
2709
2710 ring->rx_max_pending = MAX_RX_RING_SIZE;
2711 ring->tx_max_pending = MAX_TX_RING_SIZE;
2712
2713 ring->rx_pending = bp->rx_ring_size;
2714 ring->tx_pending = bp->tx_ring_size;
2715}
2716
2717static int macb_set_ringparam(struct net_device *netdev,
2718 struct ethtool_ringparam *ring)
2719{
2720 struct macb *bp = netdev_priv(netdev);
2721 u32 new_rx_size, new_tx_size;
2722 unsigned int reset = 0;
2723
2724 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2725 return -EINVAL;
2726
2727 new_rx_size = clamp_t(u32, ring->rx_pending,
2728 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2729 new_rx_size = roundup_pow_of_two(new_rx_size);
2730
2731 new_tx_size = clamp_t(u32, ring->tx_pending,
2732 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2733 new_tx_size = roundup_pow_of_two(new_tx_size);
2734
2735 if ((new_tx_size == bp->tx_ring_size) &&
2736 (new_rx_size == bp->rx_ring_size)) {
2737 /* nothing to do */
2738 return 0;
2739 }
2740
2741 if (netif_running(bp->dev)) {
2742 reset = 1;
2743 macb_close(bp->dev);
2744 }
2745
2746 bp->rx_ring_size = new_rx_size;
2747 bp->tx_ring_size = new_tx_size;
2748
2749 if (reset)
2750 macb_open(bp->dev);
2751
2752 return 0;
2753}
2754
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002755#ifdef CONFIG_MACB_USE_HWSTAMP
2756static unsigned int gem_get_tsu_rate(struct macb *bp)
2757{
2758 struct clk *tsu_clk;
2759 unsigned int tsu_rate;
2760
2761 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2762 if (!IS_ERR(tsu_clk))
2763 tsu_rate = clk_get_rate(tsu_clk);
2764 /* try pclk instead */
2765 else if (!IS_ERR(bp->pclk)) {
2766 tsu_clk = bp->pclk;
2767 tsu_rate = clk_get_rate(tsu_clk);
2768 } else
2769 return -ENOTSUPP;
2770 return tsu_rate;
2771}
2772
2773static s32 gem_get_ptp_max_adj(void)
2774{
2775 return 64000000;
2776}
2777
2778static int gem_get_ts_info(struct net_device *dev,
2779 struct ethtool_ts_info *info)
2780{
2781 struct macb *bp = netdev_priv(dev);
2782
2783 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2784 ethtool_op_get_ts_info(dev, info);
2785 return 0;
2786 }
2787
2788 info->so_timestamping =
2789 SOF_TIMESTAMPING_TX_SOFTWARE |
2790 SOF_TIMESTAMPING_RX_SOFTWARE |
2791 SOF_TIMESTAMPING_SOFTWARE |
2792 SOF_TIMESTAMPING_TX_HARDWARE |
2793 SOF_TIMESTAMPING_RX_HARDWARE |
2794 SOF_TIMESTAMPING_RAW_HARDWARE;
2795 info->tx_types =
2796 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2797 (1 << HWTSTAMP_TX_OFF) |
2798 (1 << HWTSTAMP_TX_ON);
2799 info->rx_filters =
2800 (1 << HWTSTAMP_FILTER_NONE) |
2801 (1 << HWTSTAMP_FILTER_ALL);
2802
2803 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2804
2805 return 0;
2806}
2807
2808static struct macb_ptp_info gem_ptp_info = {
2809 .ptp_init = gem_ptp_init,
2810 .ptp_remove = gem_ptp_remove,
2811 .get_ptp_max_adj = gem_get_ptp_max_adj,
2812 .get_tsu_rate = gem_get_tsu_rate,
2813 .get_ts_info = gem_get_ts_info,
2814 .get_hwtst = gem_get_hwtst,
2815 .set_hwtst = gem_set_hwtst,
2816};
2817#endif
2818
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002819static int macb_get_ts_info(struct net_device *netdev,
2820 struct ethtool_ts_info *info)
2821{
2822 struct macb *bp = netdev_priv(netdev);
2823
2824 if (bp->ptp_info)
2825 return bp->ptp_info->get_ts_info(netdev, info);
2826
2827 return ethtool_op_get_ts_info(netdev, info);
2828}
2829
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002830static void gem_enable_flow_filters(struct macb *bp, bool enable)
2831{
2832 struct ethtool_rx_fs_item *item;
2833 u32 t2_scr;
2834 int num_t2_scr;
2835
2836 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2837
2838 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2839 struct ethtool_rx_flow_spec *fs = &item->fs;
2840 struct ethtool_tcpip4_spec *tp4sp_m;
2841
2842 if (fs->location >= num_t2_scr)
2843 continue;
2844
2845 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2846
2847 /* enable/disable screener regs for the flow entry */
2848 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2849
2850 /* only enable fields with no masking */
2851 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2852
2853 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2854 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2855 else
2856 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2857
2858 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2859 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2860 else
2861 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2862
2863 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2864 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2865 else
2866 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2867
2868 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2869 }
2870}
2871
2872static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2873{
2874 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2875 uint16_t index = fs->location;
2876 u32 w0, w1, t2_scr;
2877 bool cmp_a = false;
2878 bool cmp_b = false;
2879 bool cmp_c = false;
2880
2881 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2882 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2883
2884 /* ignore field if any masking set */
2885 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2886 /* 1st compare reg - IP source address */
2887 w0 = 0;
2888 w1 = 0;
2889 w0 = tp4sp_v->ip4src;
2890 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2891 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2892 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2893 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2894 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2895 cmp_a = true;
2896 }
2897
2898 /* ignore field if any masking set */
2899 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2900 /* 2nd compare reg - IP destination address */
2901 w0 = 0;
2902 w1 = 0;
2903 w0 = tp4sp_v->ip4dst;
2904 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2905 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2906 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2907 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2908 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2909 cmp_b = true;
2910 }
2911
2912 /* ignore both port fields if masking set in both */
2913 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2914 /* 3rd compare reg - source port, destination port */
2915 w0 = 0;
2916 w1 = 0;
2917 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2918 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2919 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2920 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2921 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2922 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2923 } else {
2924 /* only one port definition */
2925 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2926 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2927 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2928 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2929 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2930 } else { /* dst port */
2931 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2932 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2933 }
2934 }
2935 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2936 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2937 cmp_c = true;
2938 }
2939
2940 t2_scr = 0;
2941 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2942 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2943 if (cmp_a)
2944 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2945 if (cmp_b)
2946 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2947 if (cmp_c)
2948 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2949 gem_writel_n(bp, SCRT2, index, t2_scr);
2950}
2951
2952static int gem_add_flow_filter(struct net_device *netdev,
2953 struct ethtool_rxnfc *cmd)
2954{
2955 struct macb *bp = netdev_priv(netdev);
2956 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2957 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002958 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002959 int ret = -EINVAL;
2960 bool added = false;
2961
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002962 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002963 if (newfs == NULL)
2964 return -ENOMEM;
2965 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2966
2967 netdev_dbg(netdev,
2968 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2969 fs->flow_type, (int)fs->ring_cookie, fs->location,
2970 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2971 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2972 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2973
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002974 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2975
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002976 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002977 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2978 if (item->fs.location > newfs->fs.location) {
2979 list_add_tail(&newfs->list, &item->list);
2980 added = true;
2981 break;
2982 } else if (item->fs.location == fs->location) {
2983 netdev_err(netdev, "Rule not added: location %d not free!\n",
2984 fs->location);
2985 ret = -EBUSY;
2986 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002987 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002988 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002989 if (!added)
2990 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002991
2992 gem_prog_cmp_regs(bp, fs);
2993 bp->rx_fs_list.count++;
2994 /* enable filtering if NTUPLE on */
2995 if (netdev->features & NETIF_F_NTUPLE)
2996 gem_enable_flow_filters(bp, 1);
2997
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002998 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002999 return 0;
3000
3001err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003002 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003003 kfree(newfs);
3004 return ret;
3005}
3006
3007static int gem_del_flow_filter(struct net_device *netdev,
3008 struct ethtool_rxnfc *cmd)
3009{
3010 struct macb *bp = netdev_priv(netdev);
3011 struct ethtool_rx_fs_item *item;
3012 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003013 unsigned long flags;
3014
3015 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003016
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003017 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3018 if (item->fs.location == cmd->fs.location) {
3019 /* disable screener regs for the flow entry */
3020 fs = &(item->fs);
3021 netdev_dbg(netdev,
3022 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3023 fs->flow_type, (int)fs->ring_cookie, fs->location,
3024 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3025 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3026 htons(fs->h_u.tcp_ip4_spec.psrc),
3027 htons(fs->h_u.tcp_ip4_spec.pdst));
3028
3029 gem_writel_n(bp, SCRT2, fs->location, 0);
3030
3031 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003032 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003033 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3034 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003035 return 0;
3036 }
3037 }
3038
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003039 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003040 return -EINVAL;
3041}
3042
3043static int gem_get_flow_entry(struct net_device *netdev,
3044 struct ethtool_rxnfc *cmd)
3045{
3046 struct macb *bp = netdev_priv(netdev);
3047 struct ethtool_rx_fs_item *item;
3048
3049 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3050 if (item->fs.location == cmd->fs.location) {
3051 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3052 return 0;
3053 }
3054 }
3055 return -EINVAL;
3056}
3057
3058static int gem_get_all_flow_entries(struct net_device *netdev,
3059 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3060{
3061 struct macb *bp = netdev_priv(netdev);
3062 struct ethtool_rx_fs_item *item;
3063 uint32_t cnt = 0;
3064
3065 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3066 if (cnt == cmd->rule_cnt)
3067 return -EMSGSIZE;
3068 rule_locs[cnt] = item->fs.location;
3069 cnt++;
3070 }
3071 cmd->data = bp->max_tuples;
3072 cmd->rule_cnt = cnt;
3073
3074 return 0;
3075}
3076
3077static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3078 u32 *rule_locs)
3079{
3080 struct macb *bp = netdev_priv(netdev);
3081 int ret = 0;
3082
3083 switch (cmd->cmd) {
3084 case ETHTOOL_GRXRINGS:
3085 cmd->data = bp->num_queues;
3086 break;
3087 case ETHTOOL_GRXCLSRLCNT:
3088 cmd->rule_cnt = bp->rx_fs_list.count;
3089 break;
3090 case ETHTOOL_GRXCLSRULE:
3091 ret = gem_get_flow_entry(netdev, cmd);
3092 break;
3093 case ETHTOOL_GRXCLSRLALL:
3094 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3095 break;
3096 default:
3097 netdev_err(netdev,
3098 "Command parameter %d is not supported\n", cmd->cmd);
3099 ret = -EOPNOTSUPP;
3100 }
3101
3102 return ret;
3103}
3104
3105static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3106{
3107 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003108 int ret;
3109
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003110 switch (cmd->cmd) {
3111 case ETHTOOL_SRXCLSRLINS:
3112 if ((cmd->fs.location >= bp->max_tuples)
3113 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3114 ret = -EINVAL;
3115 break;
3116 }
3117 ret = gem_add_flow_filter(netdev, cmd);
3118 break;
3119 case ETHTOOL_SRXCLSRLDEL:
3120 ret = gem_del_flow_filter(netdev, cmd);
3121 break;
3122 default:
3123 netdev_err(netdev,
3124 "Command parameter %d is not supported\n", cmd->cmd);
3125 ret = -EOPNOTSUPP;
3126 }
3127
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003128 return ret;
3129}
3130
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003131static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003132 .get_regs_len = macb_get_regs_len,
3133 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003134 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003135 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003136 .get_wol = macb_get_wol,
3137 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003138 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3139 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003140 .get_ringparam = macb_get_ringparam,
3141 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003142};
Xander Huff8cd5a562015-01-15 15:55:20 -06003143
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003144static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003145 .get_regs_len = macb_get_regs_len,
3146 .get_regs = macb_get_regs,
3147 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003148 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003149 .get_ethtool_stats = gem_get_ethtool_stats,
3150 .get_strings = gem_get_ethtool_strings,
3151 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003152 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3153 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003154 .get_ringparam = macb_get_ringparam,
3155 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003156 .get_rxnfc = gem_get_rxnfc,
3157 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003158};
3159
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003160static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003161{
Philippe Reynes0a912812016-06-22 00:32:35 +02003162 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003163 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003164
3165 if (!netif_running(dev))
3166 return -EINVAL;
3167
frederic RODO6c36a702007-07-12 19:07:24 +02003168 if (!phydev)
3169 return -ENODEV;
3170
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003171 if (!bp->ptp_info)
3172 return phy_mii_ioctl(phydev, rq, cmd);
3173
3174 switch (cmd) {
3175 case SIOCSHWTSTAMP:
3176 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3177 case SIOCGHWTSTAMP:
3178 return bp->ptp_info->get_hwtst(dev, rq);
3179 default:
3180 return phy_mii_ioctl(phydev, rq, cmd);
3181 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003182}
3183
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003184static int macb_set_features(struct net_device *netdev,
3185 netdev_features_t features)
3186{
3187 struct macb *bp = netdev_priv(netdev);
3188 netdev_features_t changed = features ^ netdev->features;
3189
3190 /* TX checksum offload */
3191 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3192 u32 dmacfg;
3193
3194 dmacfg = gem_readl(bp, DMACFG);
3195 if (features & NETIF_F_HW_CSUM)
3196 dmacfg |= GEM_BIT(TXCOEN);
3197 else
3198 dmacfg &= ~GEM_BIT(TXCOEN);
3199 gem_writel(bp, DMACFG, dmacfg);
3200 }
3201
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003202 /* RX checksum offload */
3203 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3204 u32 netcfg;
3205
3206 netcfg = gem_readl(bp, NCFGR);
3207 if (features & NETIF_F_RXCSUM &&
3208 !(netdev->flags & IFF_PROMISC))
3209 netcfg |= GEM_BIT(RXCOEN);
3210 else
3211 netcfg &= ~GEM_BIT(RXCOEN);
3212 gem_writel(bp, NCFGR, netcfg);
3213 }
3214
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003215 /* RX Flow Filters */
3216 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3217 bool turn_on = features & NETIF_F_NTUPLE;
3218
3219 gem_enable_flow_filters(bp, turn_on);
3220 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003221 return 0;
3222}
3223
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003224static const struct net_device_ops macb_netdev_ops = {
3225 .ndo_open = macb_open,
3226 .ndo_stop = macb_close,
3227 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003228 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003229 .ndo_get_stats = macb_get_stats,
3230 .ndo_do_ioctl = macb_ioctl,
3231 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303232 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003233 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003234#ifdef CONFIG_NET_POLL_CONTROLLER
3235 .ndo_poll_controller = macb_poll_controller,
3236#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003237 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003238 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003239};
3240
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003241/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003242 * and integration options used
3243 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003244static void macb_configure_caps(struct macb *bp,
3245 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003246{
3247 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003248
Nicolas Ferref6970502015-03-31 15:02:01 +02003249 if (dt_conf)
3250 bp->caps = dt_conf->caps;
3251
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003252 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003253 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3254
Nicolas Ferree1755872014-07-24 13:50:58 +02003255 dcfg = gem_readl(bp, DCFG1);
3256 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3257 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3258 dcfg = gem_readl(bp, DCFG2);
3259 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3260 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003261#ifdef CONFIG_MACB_USE_HWSTAMP
3262 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003263 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3264 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003265 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003266 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003267 bp->ptp_info = &gem_ptp_info;
3268 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003269 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003270#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003271 }
3272
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003273 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003274}
3275
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003276static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003277 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003278 unsigned int *queue_mask,
3279 unsigned int *num_queues)
3280{
3281 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003282
3283 *queue_mask = 0x1;
3284 *num_queues = 1;
3285
Nicolas Ferreda120112015-03-31 15:02:00 +02003286 /* is it macb or gem ?
3287 *
3288 * We need to read directly from the hardware here because
3289 * we are early in the probe process and don't have the
3290 * MACB_CAPS_MACB_IS_GEM flag positioned
3291 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003292 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003293 return;
3294
3295 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303296 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3297
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003298 *queue_mask |= 0x1;
3299
3300 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3301 if (*queue_mask & (1 << hw_q))
3302 (*num_queues)++;
3303}
3304
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003305static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303306 struct clk **hclk, struct clk **tx_clk,
3307 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003308{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003309 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003310 int err;
3311
Bartosz Folta83a77e92016-12-14 06:39:15 +00003312 pdata = dev_get_platdata(&pdev->dev);
3313 if (pdata) {
3314 *pclk = pdata->pclk;
3315 *hclk = pdata->hclk;
3316 } else {
3317 *pclk = devm_clk_get(&pdev->dev, "pclk");
3318 *hclk = devm_clk_get(&pdev->dev, "hclk");
3319 }
3320
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003321 if (IS_ERR(*pclk)) {
3322 err = PTR_ERR(*pclk);
3323 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3324 return err;
3325 }
3326
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003327 if (IS_ERR(*hclk)) {
3328 err = PTR_ERR(*hclk);
3329 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3330 return err;
3331 }
3332
3333 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3334 if (IS_ERR(*tx_clk))
3335 *tx_clk = NULL;
3336
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303337 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3338 if (IS_ERR(*rx_clk))
3339 *rx_clk = NULL;
3340
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003341 err = clk_prepare_enable(*pclk);
3342 if (err) {
3343 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3344 return err;
3345 }
3346
3347 err = clk_prepare_enable(*hclk);
3348 if (err) {
3349 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3350 goto err_disable_pclk;
3351 }
3352
3353 err = clk_prepare_enable(*tx_clk);
3354 if (err) {
3355 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3356 goto err_disable_hclk;
3357 }
3358
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303359 err = clk_prepare_enable(*rx_clk);
3360 if (err) {
3361 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3362 goto err_disable_txclk;
3363 }
3364
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003365 return 0;
3366
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303367err_disable_txclk:
3368 clk_disable_unprepare(*tx_clk);
3369
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003370err_disable_hclk:
3371 clk_disable_unprepare(*hclk);
3372
3373err_disable_pclk:
3374 clk_disable_unprepare(*pclk);
3375
3376 return err;
3377}
3378
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003379static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003380{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003381 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003382 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003383 struct macb *bp = netdev_priv(dev);
3384 struct macb_queue *queue;
3385 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003386 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003387
Zach Brownb410d132016-10-19 09:56:57 -05003388 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3389 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3390
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003391 /* set the queue register mapping once for all: queue0 has a special
3392 * register mapping but we don't want to test the queue index then
3393 * compute the corresponding register offset at run time.
3394 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003395 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003396 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003397 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003398
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003399 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003400 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003401 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003402 if (hw_q) {
3403 queue->ISR = GEM_ISR(hw_q - 1);
3404 queue->IER = GEM_IER(hw_q - 1);
3405 queue->IDR = GEM_IDR(hw_q - 1);
3406 queue->IMR = GEM_IMR(hw_q - 1);
3407 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003408 queue->RBQP = GEM_RBQP(hw_q - 1);
3409 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303410#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003411 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003412 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003413 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3414 }
Harini Katakamfff80192016-08-09 13:15:53 +05303415#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003416 } else {
3417 /* queue0 uses legacy registers */
3418 queue->ISR = MACB_ISR;
3419 queue->IER = MACB_IER;
3420 queue->IDR = MACB_IDR;
3421 queue->IMR = MACB_IMR;
3422 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003423 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303424#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003425 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003426 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003427 queue->RBQPH = MACB_RBQPH;
3428 }
Harini Katakamfff80192016-08-09 13:15:53 +05303429#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003430 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003431
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003432 /* get irq: here we use the linux queue index, not the hardware
3433 * queue index. the queue irq definitions in the device tree
3434 * must remove the optional gaps that could exist in the
3435 * hardware queue mask.
3436 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003437 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003438 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003439 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003440 if (err) {
3441 dev_err(&pdev->dev,
3442 "Unable to request IRQ %d (error %d)\n",
3443 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003444 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003445 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003446
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003447 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003448 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003449 }
3450
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003451 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003452
Nicolas Ferre4df95132013-06-04 21:57:12 +00003453 /* setup appropriated routines according to adapter type */
3454 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003455 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003456 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3457 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3458 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3459 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003460 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003461 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003462 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003463 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3464 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3465 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3466 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003467 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003468 }
3469
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003470 /* Set features */
3471 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003472
3473 /* Check LSO capability */
3474 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3475 dev->hw_features |= MACB_NETIF_LSO;
3476
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003477 /* Checksum offload is only available on gem with packet buffer */
3478 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003479 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003480 if (bp->caps & MACB_CAPS_SG_DISABLED)
3481 dev->hw_features &= ~NETIF_F_SG;
3482 dev->features = dev->hw_features;
3483
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003484 /* Check RX Flow Filters support.
3485 * Max Rx flows set by availability of screeners & compare regs:
3486 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3487 */
3488 reg = gem_readl(bp, DCFG8);
3489 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3490 GEM_BFEXT(T2SCR, reg));
3491 if (bp->max_tuples > 0) {
3492 /* also needs one ethtype match to check IPv4 */
3493 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3494 /* program this reg now */
3495 reg = 0;
3496 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3497 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3498 /* Filtering is supported in hw but don't enable it in kernel now */
3499 dev->hw_features |= NETIF_F_NTUPLE;
3500 /* init Rx flow definitions */
3501 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3502 bp->rx_fs_list.count = 0;
3503 spin_lock_init(&bp->rx_fs_lock);
3504 } else
3505 bp->max_tuples = 0;
3506 }
3507
Neil Armstrongce721a72016-01-05 14:39:16 +01003508 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3509 val = 0;
3510 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3511 val = GEM_BIT(RGMII);
3512 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003513 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003514 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003515 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003516 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003517
Neil Armstrongce721a72016-01-05 14:39:16 +01003518 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3519 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003520
Neil Armstrongce721a72016-01-05 14:39:16 +01003521 macb_or_gem_writel(bp, USRIO, val);
3522 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003523
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003524 /* Set MII management clock divider */
3525 val = macb_mdc_clk_div(bp);
3526 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303527 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3528 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003529 macb_writel(bp, NCFGR, val);
3530
3531 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003532}
3533
3534#if defined(CONFIG_OF)
3535/* 1518 rounded up */
3536#define AT91ETHER_MAX_RBUFF_SZ 0x600
3537/* max number of receive buffers */
3538#define AT91ETHER_MAX_RX_DESCR 9
3539
3540/* Initialize and start the Receiver and Transmit subsystems */
3541static int at91ether_start(struct net_device *dev)
3542{
3543 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003544 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003545 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003546 dma_addr_t addr;
3547 u32 ctl;
3548 int i;
3549
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003550 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003551 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003552 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003553 &q->rx_ring_dma, GFP_KERNEL);
3554 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003555 return -ENOMEM;
3556
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003557 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003558 AT91ETHER_MAX_RX_DESCR *
3559 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003560 &q->rx_buffers_dma, GFP_KERNEL);
3561 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003562 dma_free_coherent(&lp->pdev->dev,
3563 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003564 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003565 q->rx_ring, q->rx_ring_dma);
3566 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003567 return -ENOMEM;
3568 }
3569
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003570 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003571 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003572 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003573 macb_set_addr(lp, desc, addr);
3574 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003575 addr += AT91ETHER_MAX_RBUFF_SZ;
3576 }
3577
3578 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003579 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003580
3581 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003582 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003583
3584 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003585 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003586
3587 /* Enable Receive and Transmit */
3588 ctl = macb_readl(lp, NCR);
3589 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3590
3591 return 0;
3592}
3593
3594/* Open the ethernet interface */
3595static int at91ether_open(struct net_device *dev)
3596{
3597 struct macb *lp = netdev_priv(dev);
3598 u32 ctl;
3599 int ret;
3600
3601 /* Clear internal statistics */
3602 ctl = macb_readl(lp, NCR);
3603 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3604
3605 macb_set_hwaddr(lp);
3606
3607 ret = at91ether_start(dev);
3608 if (ret)
3609 return ret;
3610
3611 /* Enable MAC interrupts */
3612 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3613 MACB_BIT(RXUBR) |
3614 MACB_BIT(ISR_TUND) |
3615 MACB_BIT(ISR_RLE) |
3616 MACB_BIT(TCOMP) |
3617 MACB_BIT(ISR_ROVR) |
3618 MACB_BIT(HRESP));
3619
3620 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003621 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003622
3623 netif_start_queue(dev);
3624
3625 return 0;
3626}
3627
3628/* Close the interface */
3629static int at91ether_close(struct net_device *dev)
3630{
3631 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003632 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003633 u32 ctl;
3634
3635 /* Disable Receiver and Transmitter */
3636 ctl = macb_readl(lp, NCR);
3637 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3638
3639 /* Disable MAC interrupts */
3640 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3641 MACB_BIT(RXUBR) |
3642 MACB_BIT(ISR_TUND) |
3643 MACB_BIT(ISR_RLE) |
3644 MACB_BIT(TCOMP) |
3645 MACB_BIT(ISR_ROVR) |
3646 MACB_BIT(HRESP));
3647
3648 netif_stop_queue(dev);
3649
3650 dma_free_coherent(&lp->pdev->dev,
3651 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003652 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003653 q->rx_ring, q->rx_ring_dma);
3654 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003655
3656 dma_free_coherent(&lp->pdev->dev,
3657 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003658 q->rx_buffers, q->rx_buffers_dma);
3659 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003660
3661 return 0;
3662}
3663
3664/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003665static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3666 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003667{
3668 struct macb *lp = netdev_priv(dev);
3669
3670 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3671 netif_stop_queue(dev);
3672
3673 /* Store packet information (to free when Tx completed) */
3674 lp->skb = skb;
3675 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003676 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3677 skb->len, DMA_TO_DEVICE);
3678 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003679 dev_kfree_skb_any(skb);
3680 dev->stats.tx_dropped++;
3681 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3682 return NETDEV_TX_OK;
3683 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003684
3685 /* Set address of the data in the Transmit Address register */
3686 macb_writel(lp, TAR, lp->skb_physaddr);
3687 /* Set length of the packet in the Transmit Control register */
3688 macb_writel(lp, TCR, skb->len);
3689
3690 } else {
3691 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3692 return NETDEV_TX_BUSY;
3693 }
3694
3695 return NETDEV_TX_OK;
3696}
3697
3698/* Extract received frame from buffer descriptors and sent to upper layers.
3699 * (Called from interrupt context)
3700 */
3701static void at91ether_rx(struct net_device *dev)
3702{
3703 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003704 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003705 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003706 unsigned char *p_recv;
3707 struct sk_buff *skb;
3708 unsigned int pktlen;
3709
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003710 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003711 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003712 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003713 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003714 skb = netdev_alloc_skb(dev, pktlen + 2);
3715 if (skb) {
3716 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003717 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003718
3719 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003720 dev->stats.rx_packets++;
3721 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003722 netif_rx(skb);
3723 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003724 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003725 }
3726
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003727 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003728 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003729
3730 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003731 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003732
3733 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003734 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3735 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003736 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003737 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003738
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003739 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003740 }
3741}
3742
3743/* MAC interrupt handler */
3744static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3745{
3746 struct net_device *dev = dev_id;
3747 struct macb *lp = netdev_priv(dev);
3748 u32 intstatus, ctl;
3749
3750 /* MAC Interrupt Status register indicates what interrupts are pending.
3751 * It is automatically cleared once read.
3752 */
3753 intstatus = macb_readl(lp, ISR);
3754
3755 /* Receive complete */
3756 if (intstatus & MACB_BIT(RCOMP))
3757 at91ether_rx(dev);
3758
3759 /* Transmit complete */
3760 if (intstatus & MACB_BIT(TCOMP)) {
3761 /* The TCOM bit is set even if the transmission failed */
3762 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003763 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003764
3765 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003766 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003767 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003768 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003769 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003770 dev->stats.tx_packets++;
3771 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003772 }
3773 netif_wake_queue(dev);
3774 }
3775
3776 /* Work-around for EMAC Errata section 41.3.1 */
3777 if (intstatus & MACB_BIT(RXUBR)) {
3778 ctl = macb_readl(lp, NCR);
3779 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003780 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003781 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3782 }
3783
3784 if (intstatus & MACB_BIT(ISR_ROVR))
3785 netdev_err(dev, "ROVR error\n");
3786
3787 return IRQ_HANDLED;
3788}
3789
3790#ifdef CONFIG_NET_POLL_CONTROLLER
3791static void at91ether_poll_controller(struct net_device *dev)
3792{
3793 unsigned long flags;
3794
3795 local_irq_save(flags);
3796 at91ether_interrupt(dev->irq, dev);
3797 local_irq_restore(flags);
3798}
3799#endif
3800
3801static const struct net_device_ops at91ether_netdev_ops = {
3802 .ndo_open = at91ether_open,
3803 .ndo_stop = at91ether_close,
3804 .ndo_start_xmit = at91ether_start_xmit,
3805 .ndo_get_stats = macb_get_stats,
3806 .ndo_set_rx_mode = macb_set_rx_mode,
3807 .ndo_set_mac_address = eth_mac_addr,
3808 .ndo_do_ioctl = macb_ioctl,
3809 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003810#ifdef CONFIG_NET_POLL_CONTROLLER
3811 .ndo_poll_controller = at91ether_poll_controller,
3812#endif
3813};
3814
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003815static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303816 struct clk **hclk, struct clk **tx_clk,
3817 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003818{
3819 int err;
3820
3821 *hclk = NULL;
3822 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303823 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003824
3825 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3826 if (IS_ERR(*pclk))
3827 return PTR_ERR(*pclk);
3828
3829 err = clk_prepare_enable(*pclk);
3830 if (err) {
3831 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3832 return err;
3833 }
3834
3835 return 0;
3836}
3837
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003838static int at91ether_init(struct platform_device *pdev)
3839{
3840 struct net_device *dev = platform_get_drvdata(pdev);
3841 struct macb *bp = netdev_priv(dev);
3842 int err;
3843 u32 reg;
3844
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003845 bp->queues[0].bp = bp;
3846
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003847 dev->netdev_ops = &at91ether_netdev_ops;
3848 dev->ethtool_ops = &macb_ethtool_ops;
3849
3850 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3851 0, dev->name, dev);
3852 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003853 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003854
3855 macb_writel(bp, NCR, 0);
3856
3857 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3858 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3859 reg |= MACB_BIT(RM9200_RMII);
3860
3861 macb_writel(bp, NCFGR, reg);
3862
3863 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003864}
3865
David S. Miller3cef5c52015-03-09 23:38:02 -04003866static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003867 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003868 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003869 .init = macb_init,
3870};
3871
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003872static const struct macb_config sama5d3macb_config = {
3873 .caps = MACB_CAPS_SG_DISABLED
3874 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3875 .clk_init = macb_clk_init,
3876 .init = macb_init,
3877};
3878
David S. Miller3cef5c52015-03-09 23:38:02 -04003879static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003880 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3881 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003882 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003883 .init = macb_init,
3884};
3885
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003886static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003887 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003888 .dma_burst_length = 16,
3889 .clk_init = macb_clk_init,
3890 .init = macb_init,
3891};
3892
David S. Miller3cef5c52015-03-09 23:38:02 -04003893static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003894 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003895 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003896 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003897 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003898 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003899 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003900};
3901
David S. Miller3cef5c52015-03-09 23:38:02 -04003902static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003903 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003904 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003905 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003906 .init = macb_init,
3907};
3908
David S. Miller3cef5c52015-03-09 23:38:02 -04003909static const struct macb_config emac_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303910 .caps = MACB_CAPS_NEEDS_RSTONUBR,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003911 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003912 .init = at91ether_init,
3913};
3914
Neil Armstronge611b5b2016-01-05 14:39:17 +01003915static const struct macb_config np4_config = {
3916 .caps = MACB_CAPS_USRIO_DISABLED,
3917 .clk_init = macb_clk_init,
3918 .init = macb_init,
3919};
David S. Miller36583eb2015-05-23 01:22:35 -04003920
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303921static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003922 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3923 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05303924 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303925 .dma_burst_length = 16,
3926 .clk_init = macb_clk_init,
3927 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303928 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303929};
3930
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003931static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303932 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
3933 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003934 .dma_burst_length = 16,
3935 .clk_init = macb_clk_init,
3936 .init = macb_init,
3937};
3938
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003939static const struct of_device_id macb_dt_ids[] = {
3940 { .compatible = "cdns,at32ap7000-macb" },
3941 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3942 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003943 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003944 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3945 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01003946 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003947 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003948 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003949 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003950 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3951 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3952 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303953 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003954 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003955 { /* sentinel */ }
3956};
3957MODULE_DEVICE_TABLE(of, macb_dt_ids);
3958#endif /* CONFIG_OF */
3959
Bartosz Folta83a77e92016-12-14 06:39:15 +00003960static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003961 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3962 MACB_CAPS_JUMBO |
3963 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003964 .dma_burst_length = 16,
3965 .clk_init = macb_clk_init,
3966 .init = macb_init,
3967 .jumbo_max_len = 10240,
3968};
3969
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003970static int macb_probe(struct platform_device *pdev)
3971{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003972 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003973 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303974 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003975 = macb_config->clk_init;
3976 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003977 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303978 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003979 unsigned int queue_mask, num_queues;
3980 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003981 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003982 struct phy_device *phydev;
3983 struct net_device *dev;
3984 struct resource *regs;
3985 void __iomem *mem;
3986 const char *mac;
3987 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05303988 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003989
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003990 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3991 mem = devm_ioremap_resource(&pdev->dev, regs);
3992 if (IS_ERR(mem))
3993 return PTR_ERR(mem);
3994
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003995 if (np) {
3996 const struct of_device_id *match;
3997
3998 match = of_match_node(macb_dt_ids, np);
3999 if (match && match->data) {
4000 macb_config = match->data;
4001 clk_init = macb_config->clk_init;
4002 init = macb_config->init;
4003 }
4004 }
4005
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304006 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004007 if (err)
4008 return err;
4009
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004010 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004011
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004012 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004013 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004014 if (!dev) {
4015 err = -ENOMEM;
4016 goto err_disable_clocks;
4017 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004018
4019 dev->base_addr = regs->start;
4020
4021 SET_NETDEV_DEV(dev, &pdev->dev);
4022
4023 bp = netdev_priv(dev);
4024 bp->pdev = pdev;
4025 bp->dev = dev;
4026 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004027 bp->native_io = native_io;
4028 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004029 bp->macb_reg_readl = hw_readl_native;
4030 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004031 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004032 bp->macb_reg_readl = hw_readl;
4033 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004034 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004035 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004036 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004037 if (macb_config)
4038 bp->dma_burst_length = macb_config->dma_burst_length;
4039 bp->pclk = pclk;
4040 bp->hclk = hclk;
4041 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304042 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004043 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304044 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304045
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004046 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004047 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004048 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4049 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4050
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004051 spin_lock_init(&bp->lock);
4052
Nicolas Ferread783472015-03-31 15:02:02 +02004053 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004054 macb_configure_caps(bp, macb_config);
4055
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004056#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4057 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4058 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4059 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4060 }
4061#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004062 platform_set_drvdata(pdev, dev);
4063
4064 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004065 if (dev->irq < 0) {
4066 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004067 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004068 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004069
Jarod Wilson44770e12016-10-17 15:54:17 -04004070 /* MTU range: 68 - 1500 or 10240 */
4071 dev->min_mtu = GEM_MTU_MIN_SIZE;
4072 if (bp->caps & MACB_CAPS_JUMBO)
4073 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4074 else
4075 dev->max_mtu = ETH_DATA_LEN;
4076
Harini Katakam404cd082018-07-06 12:18:58 +05304077 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4078 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4079 if (val)
4080 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4081 macb_dma_desc_get_size(bp);
4082
4083 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4084 if (val)
4085 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4086 macb_dma_desc_get_size(bp);
4087 }
4088
Harini Katakame5010702019-01-29 15:20:03 +05304089 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4090 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4091 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4092
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004093 mac = of_get_mac_address(np);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004094 if (mac) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004095 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004096 } else {
Bartosz Golaszewskicce41b82018-11-30 09:20:58 +01004097 err = nvmem_get_mac_address(&pdev->dev, bp->dev->dev_addr);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004098 if (err) {
4099 if (err == -EPROBE_DEFER)
4100 goto err_out_free_netdev;
4101 macb_get_hwaddr(bp);
4102 }
4103 }
frederic RODO6c36a702007-07-12 19:07:24 +02004104
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004105 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004106 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09004107 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004108 if (pdata && pdata->is_rmii)
4109 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4110 else
4111 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4112 } else {
4113 bp->phy_interface = err;
4114 }
4115
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004116 /* IP specific init */
4117 err = init(pdev);
4118 if (err)
4119 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004120
Florian Fainellicf669662016-05-02 18:38:45 -07004121 err = macb_mii_init(bp);
4122 if (err)
4123 goto err_out_free_netdev;
4124
Philippe Reynes0a912812016-06-22 00:32:35 +02004125 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004126
4127 netif_carrier_off(dev);
4128
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004129 err = register_netdev(dev);
4130 if (err) {
4131 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004132 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004133 }
4134
Harini Katakam032dc412018-01-27 12:09:01 +05304135 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4136 (unsigned long)bp);
4137
Florian Fainellicf669662016-05-02 18:38:45 -07004138 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004139
Bo Shen58798232014-09-13 01:57:49 +02004140 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4141 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4142 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004143
4144 return 0;
4145
Florian Fainellicf669662016-05-02 18:38:45 -07004146err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004147 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004148 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004149 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004150 if (np && of_phy_is_fixed_link(np))
4151 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004152 mdiobus_free(bp->mii_bus);
4153
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004154err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004155 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004156
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004157err_disable_clocks:
4158 clk_disable_unprepare(tx_clk);
4159 clk_disable_unprepare(hclk);
4160 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304161 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004162
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004163 return err;
4164}
4165
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004166static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004167{
4168 struct net_device *dev;
4169 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004170 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004171
4172 dev = platform_get_drvdata(pdev);
4173
4174 if (dev) {
4175 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004176 if (dev->phydev)
4177 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004178 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004179 if (np && of_phy_is_fixed_link(np))
4180 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004181 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004182 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004183
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004184 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004185 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004186 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004187 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304188 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004189 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004190 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004191 }
4192
4193 return 0;
4194}
4195
Michal Simekd23823d2015-01-23 09:36:03 +01004196static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004197{
Wolfram Sangce886a42018-10-21 22:00:14 +02004198 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004199 struct macb *bp = netdev_priv(netdev);
4200
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004201 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004202 netif_device_detach(netdev);
4203
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004204 if (bp->wol & MACB_WOL_ENABLED) {
4205 macb_writel(bp, IER, MACB_BIT(WOL));
4206 macb_writel(bp, WOL, MACB_BIT(MAG));
4207 enable_irq_wake(bp->queues[0].irq);
4208 } else {
4209 clk_disable_unprepare(bp->tx_clk);
4210 clk_disable_unprepare(bp->hclk);
4211 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304212 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004213 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004214
4215 return 0;
4216}
4217
Michal Simekd23823d2015-01-23 09:36:03 +01004218static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004219{
Wolfram Sangce886a42018-10-21 22:00:14 +02004220 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004221 struct macb *bp = netdev_priv(netdev);
4222
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004223 if (bp->wol & MACB_WOL_ENABLED) {
4224 macb_writel(bp, IDR, MACB_BIT(WOL));
4225 macb_writel(bp, WOL, 0);
4226 disable_irq_wake(bp->queues[0].irq);
4227 } else {
4228 clk_prepare_enable(bp->pclk);
4229 clk_prepare_enable(bp->hclk);
4230 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304231 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004232 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004233
4234 netif_device_attach(netdev);
4235
4236 return 0;
4237}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004238
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004239static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4240
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004241static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004242 .probe = macb_probe,
4243 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004244 .driver = {
4245 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004246 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004247 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004248 },
4249};
4250
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004251module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004252
4253MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004254MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004255MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004256MODULE_ALIAS("platform:macb");