blob: 96ae8c99281042c81cd5d640ddd657335c46d3d2 [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 Eastwood2dbfdbb2012-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
59#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
60 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000061#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
62 | MACB_BIT(ISR_RLE) \
63 | MACB_BIT(TXERR))
64#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
65
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 Eastwood2dbfdbb2012-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 Eastwood2dbfdbb2012-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);
683 }
Harini Katakamfff80192016-08-09 13:15:53 +0530684#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000685 desc->addr = lower_32_bits(addr);
686}
687
688static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
689{
690 dma_addr_t addr = 0;
691#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
692 struct macb_dma_desc_64 *desc_64;
693
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100694 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000695 desc_64 = macb_64b_desc(bp, desc);
696 addr = ((u64)(desc_64->addrh) << 32);
697 }
698#endif
699 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
700 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530701}
702
Nicolas Ferree86cd532012-10-31 06:04:57 +0000703static void macb_tx_error_task(struct work_struct *work)
704{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100705 struct macb_queue *queue = container_of(work, struct macb_queue,
706 tx_error_task);
707 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000708 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100709 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000710 struct sk_buff *skb;
711 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100712 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000713
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100714 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
715 (unsigned int)(queue - bp->queues),
716 queue->tx_tail, queue->tx_head);
717
718 /* Prevent the queue IRQ handlers from running: each of them may call
719 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
720 * As explained below, we have to halt the transmission before updating
721 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
722 * network engine about the macb/gem being halted.
723 */
724 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000725
726 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100727 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000728
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700729 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000730 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100731 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000732 */
733 if (macb_halt_tx(bp))
734 /* Just complain for now, reinitializing TX path can be good */
735 netdev_err(bp->dev, "BUG: halt tx timed out\n");
736
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700737 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000738 * Free transmit buffers in upper layer.
739 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
741 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000742
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100743 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000744 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100745 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000746 skb = tx_skb->skb;
747
748 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200749 /* skb is set for the last buffer of the frame */
750 while (!skb) {
751 macb_tx_unmap(bp, tx_skb);
752 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100753 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200754 skb = tx_skb->skb;
755 }
756
757 /* ctrl still refers to the first buffer descriptor
758 * since it's the only one written back by the hardware
759 */
760 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
761 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500762 macb_tx_ring_wrap(bp, tail),
763 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200764 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000765 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200766 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000767 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200768 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000769 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700770 /* "Buffers exhausted mid-frame" errors may only happen
771 * if the driver is buggy, so complain loudly about
772 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000773 */
774 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
775 netdev_err(bp->dev,
776 "BUG: TX buffers exhausted mid-frame\n");
777
778 desc->ctrl = ctrl | MACB_BIT(TX_USED);
779 }
780
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200781 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000782 }
783
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100784 /* Set end of TX queue */
785 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000786 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100787 desc->ctrl = MACB_BIT(TX_USED);
788
Nicolas Ferree86cd532012-10-31 06:04:57 +0000789 /* Make descriptor updates visible to hardware */
790 wmb();
791
792 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000793 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530794#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100795 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000796 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530797#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000798 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100799 queue->tx_head = 0;
800 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000801
802 /* Housework before enabling TX IRQ */
803 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100804 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
805
806 /* Now we are ready to start transmission again */
807 netif_tx_start_all_queues(bp->dev);
808 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
809
810 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000811}
812
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100813static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100814{
815 unsigned int tail;
816 unsigned int head;
817 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100818 struct macb *bp = queue->bp;
819 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100820
821 status = macb_readl(bp, TSR);
822 macb_writel(bp, TSR, status);
823
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000824 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100825 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000826
Nicolas Ferree86cd532012-10-31 06:04:57 +0000827 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700828 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100830 head = queue->tx_head;
831 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000832 struct macb_tx_skb *tx_skb;
833 struct sk_buff *skb;
834 struct macb_dma_desc *desc;
835 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100836
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100837 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100838
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000839 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000841
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000842 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200844 /* TX_USED bit is only set by hardware on the very first buffer
845 * descriptor of the transmitted frame.
846 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000847 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100848 break;
849
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200850 /* Process all buffers of the current transmitted frame */
851 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100852 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200853 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000854
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200855 /* First, update TX stats if needed */
856 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100857 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
858 /* skb now belongs to timestamp buffer
859 * and will be removed later
860 */
861 tx_skb->skb = NULL;
862 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200863 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500864 macb_tx_ring_wrap(bp, tail),
865 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200866 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000867 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200868 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000869 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200870 }
871
872 /* Now we can safely release resources */
873 macb_tx_unmap(bp, tx_skb);
874
875 /* skb is set only for the last buffer of the frame.
876 * WARNING: at this point skb has been freed by
877 * macb_tx_unmap().
878 */
879 if (skb)
880 break;
881 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100882 }
883
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100884 queue->tx_tail = tail;
885 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
886 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500887 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100888 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100889}
890
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000891static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000892{
893 unsigned int entry;
894 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000895 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000896 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000897 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000898
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000899 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
900 bp->rx_ring_size) > 0) {
901 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000902
903 /* Make hw descriptor updates visible to CPU */
904 rmb();
905
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000906 queue->rx_prepared_head++;
907 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000908
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000909 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000910 /* allocate sk_buff for this free entry in ring */
911 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700912 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000913 netdev_err(bp->dev,
914 "Unable to allocate sk_buff\n");
915 break;
916 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000917
918 /* now fill corresponding descriptor entry */
919 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700920 bp->rx_buffer_size,
921 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800922 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
923 dev_kfree_skb(skb);
924 break;
925 }
926
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000927 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000928
Zach Brownb410d132016-10-19 09:56:57 -0500929 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000930 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000931 macb_set_addr(bp, desc, paddr);
932 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000933
934 /* properly align Ethernet header */
935 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530936 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000937 desc->addr &= ~MACB_BIT(RX_USED);
938 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000939 }
940 }
941
942 /* Make descriptor updates visible to hardware */
943 wmb();
944
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000945 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
946 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000947}
948
949/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000950static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000951 unsigned int end)
952{
953 unsigned int frag;
954
955 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000956 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700957
Nicolas Ferre4df95132013-06-04 21:57:12 +0000958 desc->addr &= ~MACB_BIT(RX_USED);
959 }
960
961 /* Make descriptor updates visible to hardware */
962 wmb();
963
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700964 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000965 * whatever caused this is updated, so we don't have to record
966 * anything.
967 */
968}
969
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000970static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000971{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000972 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000973 unsigned int len;
974 unsigned int entry;
975 struct sk_buff *skb;
976 struct macb_dma_desc *desc;
977 int count = 0;
978
979 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530980 u32 ctrl;
981 dma_addr_t addr;
982 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000983
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000984 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
985 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000986
987 /* Make hw descriptor updates visible to CPU */
988 rmb();
989
Harini Katakamfff80192016-08-09 13:15:53 +0530990 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000991 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000992 ctrl = desc->ctrl;
993
Harini Katakamfff80192016-08-09 13:15:53 +0530994 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000995 break;
996
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000997 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000998 count++;
999
1000 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1001 netdev_err(bp->dev,
1002 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001003 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001004 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001005 break;
1006 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001007 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001008 if (unlikely(!skb)) {
1009 netdev_err(bp->dev,
1010 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001011 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001012 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001013 break;
1014 }
1015 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001016 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301017 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001018
1019 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1020
1021 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001022 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001023 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001024
1025 skb->protocol = eth_type_trans(skb, bp->dev);
1026 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001027 if (bp->dev->features & NETIF_F_RXCSUM &&
1028 !(bp->dev->flags & IFF_PROMISC) &&
1029 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1030 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001031
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001032 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001033 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001034 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001035 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001036
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001037 gem_ptp_do_rxstamp(bp, skb, desc);
1038
Nicolas Ferre4df95132013-06-04 21:57:12 +00001039#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1040 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1041 skb->len, skb->csum);
1042 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001043 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001044 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1045 skb->data, 32, true);
1046#endif
1047
1048 netif_receive_skb(skb);
1049 }
1050
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001051 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001052
1053 return count;
1054}
1055
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001056static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001057 unsigned int last_frag)
1058{
1059 unsigned int len;
1060 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001061 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001062 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001063 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001064 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001066 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301067 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001068
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001069 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001070 macb_rx_ring_wrap(bp, first_frag),
1071 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001072
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001073 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001074 * first buffer. Since the header is 14 bytes, this makes the
1075 * payload word-aligned.
1076 *
1077 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1078 * the two padding bytes into the skb so that we avoid hitting
1079 * the slowpath in memcpy(), and pull them off afterwards.
1080 */
1081 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001082 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001083 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001084 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001085 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001086 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001087 if (frag == last_frag)
1088 break;
1089 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001090
1091 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001092 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001093
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094 return 1;
1095 }
1096
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001097 offset = 0;
1098 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001099 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001100 skb_put(skb, len);
1101
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001102 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001103 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001104
1105 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001106 if (unlikely(frag != last_frag)) {
1107 dev_kfree_skb_any(skb);
1108 return -1;
1109 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001110 frag_len = len - offset;
1111 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001112 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001113 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001114 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001115 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001116 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001117 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001118
1119 if (frag == last_frag)
1120 break;
1121 }
1122
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001123 /* Make descriptor updates visible to hardware */
1124 wmb();
1125
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001126 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001127 skb->protocol = eth_type_trans(skb, bp->dev);
1128
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001129 bp->dev->stats.rx_packets++;
1130 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001131 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001132 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001133 netif_receive_skb(skb);
1134
1135 return 0;
1136}
1137
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001138static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001139{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001140 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001141 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001142 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001143 int i;
1144
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001145 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001146 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001147 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001148 macb_set_addr(bp, desc, addr);
1149 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001150 addr += bp->rx_buffer_size;
1151 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001152 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001153 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001154}
1155
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001156static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001157{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001158 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001159 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001160 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001161 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001162 int first_frag = -1;
1163
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001164 for (tail = queue->rx_tail; budget > 0; tail++) {
1165 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001166 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001167
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001168 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001169 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001170
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001171 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001172
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001173 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001174 break;
1175
1176 if (ctrl & MACB_BIT(RX_SOF)) {
1177 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001178 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001179 first_frag = tail;
1180 }
1181
1182 if (ctrl & MACB_BIT(RX_EOF)) {
1183 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001184
1185 if (unlikely(first_frag == -1)) {
1186 reset_rx_queue = true;
1187 continue;
1188 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001189
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001190 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001192 if (unlikely(dropped < 0)) {
1193 reset_rx_queue = true;
1194 continue;
1195 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 if (!dropped) {
1197 received++;
1198 budget--;
1199 }
1200 }
1201 }
1202
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001203 if (unlikely(reset_rx_queue)) {
1204 unsigned long flags;
1205 u32 ctrl;
1206
1207 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1208
1209 spin_lock_irqsave(&bp->lock, flags);
1210
1211 ctrl = macb_readl(bp, NCR);
1212 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1213
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001214 macb_init_rx_ring(queue);
1215 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001216
1217 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1218
1219 spin_unlock_irqrestore(&bp->lock, flags);
1220 return received;
1221 }
1222
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001223 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001224 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001226 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227
1228 return received;
1229}
1230
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001231static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001232{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001233 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1234 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001235 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236 u32 status;
1237
1238 status = macb_readl(bp, RSR);
1239 macb_writel(bp, RSR, status);
1240
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001241 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001242 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001243
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001244 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001245 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001246 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247
Nicolas Ferre8770e912013-02-12 11:08:48 +01001248 /* Packets received while interrupts were disabled */
1249 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001250 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001251 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001252 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001253 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001254 } else {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001255 queue_writel(queue, IER, MACB_RX_INT_FLAGS);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001256 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001257 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001258
1259 /* TODO: Handle errors */
1260
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001261 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001262}
1263
Harini Katakam032dc412018-01-27 12:09:01 +05301264static void macb_hresp_error_task(unsigned long data)
1265{
1266 struct macb *bp = (struct macb *)data;
1267 struct net_device *dev = bp->dev;
1268 struct macb_queue *queue = bp->queues;
1269 unsigned int q;
1270 u32 ctrl;
1271
1272 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1273 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
1274 MACB_TX_INT_FLAGS |
1275 MACB_BIT(HRESP));
1276 }
1277 ctrl = macb_readl(bp, NCR);
1278 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1279 macb_writel(bp, NCR, ctrl);
1280
1281 netif_tx_stop_all_queues(dev);
1282 netif_carrier_off(dev);
1283
1284 bp->macbgem_ops.mog_init_rings(bp);
1285
1286 /* Initialize TX and RX buffers */
1287 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1288 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1289#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1290 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1291 queue_writel(queue, RBQPH,
1292 upper_32_bits(queue->rx_ring_dma));
1293#endif
1294 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1295#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1296 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1297 queue_writel(queue, TBQPH,
1298 upper_32_bits(queue->tx_ring_dma));
1299#endif
1300
1301 /* Enable interrupts */
1302 queue_writel(queue, IER,
1303 MACB_RX_INT_FLAGS |
1304 MACB_TX_INT_FLAGS |
1305 MACB_BIT(HRESP));
1306 }
1307
1308 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1309 macb_writel(bp, NCR, ctrl);
1310
1311 netif_carrier_on(dev);
1312 netif_tx_start_all_queues(dev);
1313}
1314
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001315static irqreturn_t macb_interrupt(int irq, void *dev_id)
1316{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001317 struct macb_queue *queue = dev_id;
1318 struct macb *bp = queue->bp;
1319 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001320 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001321
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001322 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001323
1324 if (unlikely(!status))
1325 return IRQ_NONE;
1326
1327 spin_lock(&bp->lock);
1328
1329 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001330 /* close possible race with dev_close */
1331 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001332 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001333 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1334 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001335 break;
1336 }
1337
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001338 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1339 (unsigned int)(queue - bp->queues),
1340 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001341
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001342 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001343 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001344 * until we have processed the buffers. The
1345 * scheduling call may fail if the poll routine
1346 * is already scheduled, so disable interrupts
1347 * now.
1348 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001349 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001350 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001351 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001352
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001353 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001354 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001355 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001356 }
1357 }
1358
Nicolas Ferree86cd532012-10-31 06:04:57 +00001359 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001360 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1361 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001362
1363 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001364 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001365
Nicolas Ferree86cd532012-10-31 06:04:57 +00001366 break;
1367 }
1368
1369 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001372 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001373 * add that if/when we get our hands on a full-blown MII PHY.
1374 */
1375
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001376 /* There is a hardware issue under heavy load where DMA can
1377 * stop, this causes endless "used buffer descriptor read"
1378 * interrupts but it can be cleared by re-enabling RX. See
1379 * the at91 manual, section 41.3.1 or the Zynq manual
1380 * section 16.7.4 for details.
1381 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001382 if (status & MACB_BIT(RXUBR)) {
1383 ctrl = macb_readl(bp, NCR);
1384 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001385 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001386 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1387
1388 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001389 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001390 }
1391
Alexander Steinb19f7f72011-04-13 05:03:24 +00001392 if (status & MACB_BIT(ISR_ROVR)) {
1393 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001394 if (macb_is_gem(bp))
1395 bp->hw_stats.gem.rx_overruns++;
1396 else
1397 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001398
1399 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001400 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001401 }
1402
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001403 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301404 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001405 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001406
1407 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001408 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001410 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001411 }
1412
1413 spin_unlock(&bp->lock);
1414
1415 return IRQ_HANDLED;
1416}
1417
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001418#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001419/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001420 * to allow network i/o with interrupts disabled.
1421 */
1422static void macb_poll_controller(struct net_device *dev)
1423{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001424 struct macb *bp = netdev_priv(dev);
1425 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001426 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001427 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001428
1429 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001430 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1431 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001432 local_irq_restore(flags);
1433}
1434#endif
1435
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001436static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001437 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001438 struct sk_buff *skb,
1439 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001440{
1441 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001442 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001443 struct macb_tx_skb *tx_skb = NULL;
1444 struct macb_dma_desc *desc;
1445 unsigned int offset, size, count = 0;
1446 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001447 unsigned int eof = 1, mss_mfs = 0;
1448 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1449
1450 /* LSO */
1451 if (skb_shinfo(skb)->gso_size != 0) {
1452 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1453 /* UDP - UFO */
1454 lso_ctrl = MACB_LSO_UFO_ENABLE;
1455 else
1456 /* TCP - TSO */
1457 lso_ctrl = MACB_LSO_TSO_ENABLE;
1458 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001459
1460 /* First, map non-paged data */
1461 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001462
1463 /* first buffer length */
1464 size = hdrlen;
1465
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001466 offset = 0;
1467 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001468 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001469 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001470
1471 mapping = dma_map_single(&bp->pdev->dev,
1472 skb->data + offset,
1473 size, DMA_TO_DEVICE);
1474 if (dma_mapping_error(&bp->pdev->dev, mapping))
1475 goto dma_error;
1476
1477 /* Save info to properly release resources */
1478 tx_skb->skb = NULL;
1479 tx_skb->mapping = mapping;
1480 tx_skb->size = size;
1481 tx_skb->mapped_as_page = false;
1482
1483 len -= size;
1484 offset += size;
1485 count++;
1486 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001487
1488 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001489 }
1490
1491 /* Then, map paged data from fragments */
1492 for (f = 0; f < nr_frags; f++) {
1493 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1494
1495 len = skb_frag_size(frag);
1496 offset = 0;
1497 while (len) {
1498 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001499 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001500 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001501
1502 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1503 offset, size, DMA_TO_DEVICE);
1504 if (dma_mapping_error(&bp->pdev->dev, mapping))
1505 goto dma_error;
1506
1507 /* Save info to properly release resources */
1508 tx_skb->skb = NULL;
1509 tx_skb->mapping = mapping;
1510 tx_skb->size = size;
1511 tx_skb->mapped_as_page = true;
1512
1513 len -= size;
1514 offset += size;
1515 count++;
1516 tx_head++;
1517 }
1518 }
1519
1520 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001521 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001522 netdev_err(bp->dev, "BUG! empty skb!\n");
1523 return 0;
1524 }
1525
1526 /* This is the last buffer of the frame: save socket buffer */
1527 tx_skb->skb = skb;
1528
1529 /* Update TX ring: update buffer descriptors in reverse order
1530 * to avoid race condition
1531 */
1532
1533 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1534 * to set the end of TX queue
1535 */
1536 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001537 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001538 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001539 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001540 desc->ctrl = ctrl;
1541
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001542 if (lso_ctrl) {
1543 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1544 /* include header and FCS in value given to h/w */
1545 mss_mfs = skb_shinfo(skb)->gso_size +
1546 skb_transport_offset(skb) +
1547 ETH_FCS_LEN;
1548 else /* TSO */ {
1549 mss_mfs = skb_shinfo(skb)->gso_size;
1550 /* TCP Sequence Number Source Select
1551 * can be set only for TSO
1552 */
1553 seq_ctrl = 0;
1554 }
1555 }
1556
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001557 do {
1558 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001559 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001560 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001561 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001562
1563 ctrl = (u32)tx_skb->size;
1564 if (eof) {
1565 ctrl |= MACB_BIT(TX_LAST);
1566 eof = 0;
1567 }
Zach Brownb410d132016-10-19 09:56:57 -05001568 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001569 ctrl |= MACB_BIT(TX_WRAP);
1570
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001571 /* First descriptor is header descriptor */
1572 if (i == queue->tx_head) {
1573 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1574 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001575 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1576 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1577 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001578 } else
1579 /* Only set MSS/MFS on payload descriptors
1580 * (second or later descriptor)
1581 */
1582 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1583
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001584 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001585 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001586 /* desc->addr must be visible to hardware before clearing
1587 * 'TX_USED' bit in desc->ctrl.
1588 */
1589 wmb();
1590 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001591 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001592
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001593 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001594
1595 return count;
1596
1597dma_error:
1598 netdev_err(bp->dev, "TX DMA map failed\n");
1599
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001600 for (i = queue->tx_head; i != tx_head; i++) {
1601 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001602
1603 macb_tx_unmap(bp, tx_skb);
1604 }
1605
1606 return 0;
1607}
1608
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001609static netdev_features_t macb_features_check(struct sk_buff *skb,
1610 struct net_device *dev,
1611 netdev_features_t features)
1612{
1613 unsigned int nr_frags, f;
1614 unsigned int hdrlen;
1615
1616 /* Validate LSO compatibility */
1617
1618 /* there is only one buffer */
1619 if (!skb_is_nonlinear(skb))
1620 return features;
1621
1622 /* length of header */
1623 hdrlen = skb_transport_offset(skb);
1624 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1625 hdrlen += tcp_hdrlen(skb);
1626
1627 /* For LSO:
1628 * When software supplies two or more payload buffers all payload buffers
1629 * apart from the last must be a multiple of 8 bytes in size.
1630 */
1631 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1632 return features & ~MACB_NETIF_LSO;
1633
1634 nr_frags = skb_shinfo(skb)->nr_frags;
1635 /* No need to check last fragment */
1636 nr_frags--;
1637 for (f = 0; f < nr_frags; f++) {
1638 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1639
1640 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1641 return features & ~MACB_NETIF_LSO;
1642 }
1643 return features;
1644}
1645
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001646static inline int macb_clear_csum(struct sk_buff *skb)
1647{
1648 /* no change for packets without checksum offloading */
1649 if (skb->ip_summed != CHECKSUM_PARTIAL)
1650 return 0;
1651
1652 /* make sure we can modify the header */
1653 if (unlikely(skb_cow_head(skb, 0)))
1654 return -1;
1655
1656 /* initialize checksum field
1657 * This is required - at least for Zynq, which otherwise calculates
1658 * wrong UDP header checksums for UDP packets with UDP data len <=2
1659 */
1660 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1661 return 0;
1662}
1663
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001664static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1665{
1666 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1667 int padlen = ETH_ZLEN - (*skb)->len;
1668 int headroom = skb_headroom(*skb);
1669 int tailroom = skb_tailroom(*skb);
1670 struct sk_buff *nskb;
1671 u32 fcs;
1672
1673 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1674 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1675 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1676 return 0;
1677
1678 if (padlen <= 0) {
1679 /* FCS could be appeded to tailroom. */
1680 if (tailroom >= ETH_FCS_LEN)
1681 goto add_fcs;
1682 /* FCS could be appeded by moving data to headroom. */
1683 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1684 padlen = 0;
1685 /* No room for FCS, need to reallocate skb. */
1686 else
1687 padlen = ETH_FCS_LEN - tailroom;
1688 } else {
1689 /* Add room for FCS. */
1690 padlen += ETH_FCS_LEN;
1691 }
1692
1693 if (!cloned && headroom + tailroom >= padlen) {
1694 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1695 skb_set_tail_pointer(*skb, (*skb)->len);
1696 } else {
1697 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1698 if (!nskb)
1699 return -ENOMEM;
1700
1701 dev_kfree_skb_any(*skb);
1702 *skb = nskb;
1703 }
1704
1705 if (padlen) {
1706 if (padlen >= ETH_FCS_LEN)
1707 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
1708 else
1709 skb_trim(*skb, ETH_FCS_LEN - padlen);
1710 }
1711
1712add_fcs:
1713 /* set FCS to packet */
1714 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1715 fcs = ~fcs;
1716
1717 skb_put_u8(*skb, fcs & 0xff);
1718 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1719 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1720 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1721
1722 return 0;
1723}
1724
Claudiu Beznead1c38952018-08-07 12:25:12 +03001725static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001726{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001727 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001728 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001729 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001730 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001731 unsigned int desc_cnt, nr_frags, frag_size, f;
1732 unsigned int hdrlen;
1733 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001734 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001735
Claudiu Beznea33729f22018-08-07 12:25:13 +03001736 if (macb_clear_csum(skb)) {
1737 dev_kfree_skb_any(skb);
1738 return ret;
1739 }
1740
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001741 if (macb_pad_and_fcs(&skb, dev)) {
1742 dev_kfree_skb_any(skb);
1743 return ret;
1744 }
1745
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001746 is_lso = (skb_shinfo(skb)->gso_size != 0);
1747
1748 if (is_lso) {
1749 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1750
1751 /* length of headers */
1752 if (is_udp)
1753 /* only queue eth + ip headers separately for UDP */
1754 hdrlen = skb_transport_offset(skb);
1755 else
1756 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1757 if (skb_headlen(skb) < hdrlen) {
1758 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1759 /* if this is required, would need to copy to single buffer */
1760 return NETDEV_TX_BUSY;
1761 }
1762 } else
1763 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001764
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001765#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1766 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001767 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1768 queue_index, skb->len, skb->head, skb->data,
1769 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001770 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1771 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001772#endif
1773
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001774 /* Count how many TX buffer descriptors are needed to send this
1775 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001776 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001777 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001778 if (is_lso && (skb_headlen(skb) > hdrlen))
1779 /* extra header descriptor if also payload in first buffer */
1780 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1781 else
1782 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001783 nr_frags = skb_shinfo(skb)->nr_frags;
1784 for (f = 0; f < nr_frags; f++) {
1785 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001786 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001787 }
1788
Dongdong Deng48719532009-08-23 19:49:07 -07001789 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001790
1791 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001792 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001793 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001794 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001795 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001796 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001797 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001798 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001799 }
1800
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001801 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001802 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001803 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001804 goto unlock;
1805 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001806
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001807 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001808 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001809 skb_tx_timestamp(skb);
1810
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001811 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1812
Zach Brownb410d132016-10-19 09:56:57 -05001813 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001814 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001815
Soren Brinkmann92030902014-03-04 08:46:39 -08001816unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001817 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001818
Claudiu Beznead1c38952018-08-07 12:25:12 +03001819 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001820}
1821
Nicolas Ferre4df95132013-06-04 21:57:12 +00001822static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001823{
1824 if (!macb_is_gem(bp)) {
1825 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1826 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001827 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001828
Nicolas Ferre1b447912013-06-04 21:57:11 +00001829 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001830 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001831 "RX buffer must be multiple of %d bytes, expanding\n",
1832 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001833 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001834 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001835 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001836 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001837
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001838 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001839 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001840}
1841
Nicolas Ferre4df95132013-06-04 21:57:12 +00001842static void gem_free_rx_buffers(struct macb *bp)
1843{
1844 struct sk_buff *skb;
1845 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001846 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001847 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001848 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001849 int i;
1850
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001851 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1852 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001853 continue;
1854
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001855 for (i = 0; i < bp->rx_ring_size; i++) {
1856 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001857
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001858 if (!skb)
1859 continue;
1860
1861 desc = macb_rx_desc(queue, i);
1862 addr = macb_get_addr(bp, desc);
1863
1864 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1865 DMA_FROM_DEVICE);
1866 dev_kfree_skb_any(skb);
1867 skb = NULL;
1868 }
1869
1870 kfree(queue->rx_skbuff);
1871 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001872 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001873}
1874
1875static void macb_free_rx_buffers(struct macb *bp)
1876{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001877 struct macb_queue *queue = &bp->queues[0];
1878
1879 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001880 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001881 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001882 queue->rx_buffers, queue->rx_buffers_dma);
1883 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001884 }
1885}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001886
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001887static void macb_free_consistent(struct macb *bp)
1888{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001889 struct macb_queue *queue;
1890 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301891 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001892
Nicolas Ferre4df95132013-06-04 21:57:12 +00001893 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001894
1895 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1896 kfree(queue->tx_skb);
1897 queue->tx_skb = NULL;
1898 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301899 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1900 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001901 queue->tx_ring, queue->tx_ring_dma);
1902 queue->tx_ring = NULL;
1903 }
Harini Katakame50b7702018-07-06 12:18:57 +05301904 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301905 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1906 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301907 queue->rx_ring, queue->rx_ring_dma);
1908 queue->rx_ring = NULL;
1909 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001910 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001911}
1912
1913static int gem_alloc_rx_buffers(struct macb *bp)
1914{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001915 struct macb_queue *queue;
1916 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001917 int size;
1918
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001919 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1920 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1921 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1922 if (!queue->rx_skbuff)
1923 return -ENOMEM;
1924 else
1925 netdev_dbg(bp->dev,
1926 "Allocated %d RX struct sk_buff entries at %p\n",
1927 bp->rx_ring_size, queue->rx_skbuff);
1928 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001929 return 0;
1930}
1931
1932static int macb_alloc_rx_buffers(struct macb *bp)
1933{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001934 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001935 int size;
1936
Zach Brownb410d132016-10-19 09:56:57 -05001937 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001938 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1939 &queue->rx_buffers_dma, GFP_KERNEL);
1940 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001941 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001942
1943 netdev_dbg(bp->dev,
1944 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001945 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001946 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001947}
1948
1949static int macb_alloc_consistent(struct macb *bp)
1950{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001951 struct macb_queue *queue;
1952 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001953 int size;
1954
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001955 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05301956 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001957 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1958 &queue->tx_ring_dma,
1959 GFP_KERNEL);
1960 if (!queue->tx_ring)
1961 goto out_err;
1962 netdev_dbg(bp->dev,
1963 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1964 q, size, (unsigned long)queue->tx_ring_dma,
1965 queue->tx_ring);
1966
Zach Brownb410d132016-10-19 09:56:57 -05001967 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001968 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1969 if (!queue->tx_skb)
1970 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001971
Harini Katakam404cd082018-07-06 12:18:58 +05301972 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001973 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1974 &queue->rx_ring_dma, GFP_KERNEL);
1975 if (!queue->rx_ring)
1976 goto out_err;
1977 netdev_dbg(bp->dev,
1978 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1979 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001980 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001981 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001982 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001983
1984 return 0;
1985
1986out_err:
1987 macb_free_consistent(bp);
1988 return -ENOMEM;
1989}
1990
Nicolas Ferre4df95132013-06-04 21:57:12 +00001991static void gem_init_rings(struct macb *bp)
1992{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001993 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001994 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001995 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001996 int i;
1997
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001998 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001999 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002000 desc = macb_tx_desc(queue, i);
2001 macb_set_addr(bp, desc, 0);
2002 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002003 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002004 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002005 queue->tx_head = 0;
2006 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002007
2008 queue->rx_tail = 0;
2009 queue->rx_prepared_head = 0;
2010
2011 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002012 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002013
Nicolas Ferre4df95132013-06-04 21:57:12 +00002014}
2015
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002016static void macb_init_rings(struct macb *bp)
2017{
2018 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002019 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002020
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002021 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002022
Zach Brownb410d132016-10-19 09:56:57 -05002023 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002024 desc = macb_tx_desc(&bp->queues[0], i);
2025 macb_set_addr(bp, desc, 0);
2026 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002027 }
Ben Shelton21d35152015-04-22 17:28:54 -05002028 bp->queues[0].tx_head = 0;
2029 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002030 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002031}
2032
2033static void macb_reset_hw(struct macb *bp)
2034{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002035 struct macb_queue *queue;
2036 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002037 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002038
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002039 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002040 * more gracefully?)
2041 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002042 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002043
2044 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002045 ctrl |= MACB_BIT(CLRSTAT);
2046
2047 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048
2049 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002050 macb_writel(bp, TSR, -1);
2051 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002052
2053 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002054 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2055 queue_writel(queue, IDR, -1);
2056 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002057 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2058 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002059 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002060}
2061
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002062static u32 gem_mdc_clk_div(struct macb *bp)
2063{
2064 u32 config;
2065 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2066
2067 if (pclk_hz <= 20000000)
2068 config = GEM_BF(CLK, GEM_CLK_DIV8);
2069 else if (pclk_hz <= 40000000)
2070 config = GEM_BF(CLK, GEM_CLK_DIV16);
2071 else if (pclk_hz <= 80000000)
2072 config = GEM_BF(CLK, GEM_CLK_DIV32);
2073 else if (pclk_hz <= 120000000)
2074 config = GEM_BF(CLK, GEM_CLK_DIV48);
2075 else if (pclk_hz <= 160000000)
2076 config = GEM_BF(CLK, GEM_CLK_DIV64);
2077 else
2078 config = GEM_BF(CLK, GEM_CLK_DIV96);
2079
2080 return config;
2081}
2082
2083static u32 macb_mdc_clk_div(struct macb *bp)
2084{
2085 u32 config;
2086 unsigned long pclk_hz;
2087
2088 if (macb_is_gem(bp))
2089 return gem_mdc_clk_div(bp);
2090
2091 pclk_hz = clk_get_rate(bp->pclk);
2092 if (pclk_hz <= 20000000)
2093 config = MACB_BF(CLK, MACB_CLK_DIV8);
2094 else if (pclk_hz <= 40000000)
2095 config = MACB_BF(CLK, MACB_CLK_DIV16);
2096 else if (pclk_hz <= 80000000)
2097 config = MACB_BF(CLK, MACB_CLK_DIV32);
2098 else
2099 config = MACB_BF(CLK, MACB_CLK_DIV64);
2100
2101 return config;
2102}
2103
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002104/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002105 * should program. We find the width from decoding the design configuration
2106 * register to find the maximum supported data bus width.
2107 */
2108static u32 macb_dbw(struct macb *bp)
2109{
2110 if (!macb_is_gem(bp))
2111 return 0;
2112
2113 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2114 case 4:
2115 return GEM_BF(DBW, GEM_DBW128);
2116 case 2:
2117 return GEM_BF(DBW, GEM_DBW64);
2118 case 1:
2119 default:
2120 return GEM_BF(DBW, GEM_DBW32);
2121 }
2122}
2123
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002124/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002125 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002126 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002127 * (if not supported by FIFO, it will fallback to default)
2128 * - set both rx/tx packet buffers to full memory size
2129 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002130 */
2131static void macb_configure_dma(struct macb *bp)
2132{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002133 struct macb_queue *queue;
2134 u32 buffer_size;
2135 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002136 u32 dmacfg;
2137
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002138 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002139 if (macb_is_gem(bp)) {
2140 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002141 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2142 if (q)
2143 queue_writel(queue, RBQS, buffer_size);
2144 else
2145 dmacfg |= GEM_BF(RXBS, buffer_size);
2146 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002147 if (bp->dma_burst_length)
2148 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002149 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302150 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302151
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002152 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302153 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2154 else
2155 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2156
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002157 if (bp->dev->features & NETIF_F_HW_CSUM)
2158 dmacfg |= GEM_BIT(TXCOEN);
2159 else
2160 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302161
2162#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002163 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002164 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302165#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002166#ifdef CONFIG_MACB_USE_HWSTAMP
2167 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2168 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2169#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002170 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2171 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002172 gem_writel(bp, DMACFG, dmacfg);
2173 }
2174}
2175
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002176static void macb_init_hw(struct macb *bp)
2177{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002178 struct macb_queue *queue;
2179 unsigned int q;
2180
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002181 u32 config;
2182
2183 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002184 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002185
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002186 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302187 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2188 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002189 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002190 config |= MACB_BIT(PAE); /* PAuse Enable */
2191 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002192 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302193 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2194 else
2195 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002196 if (bp->dev->flags & IFF_PROMISC)
2197 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002198 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2199 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002200 if (!(bp->dev->flags & IFF_BROADCAST))
2201 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002202 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002203 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002204 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302205 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002206 bp->speed = SPEED_10;
2207 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302208 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002209 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302210 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002211
Jamie Iles0116da42011-03-14 17:38:30 +00002212 macb_configure_dma(bp);
2213
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002214 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002215 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002216 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2217#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2218 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2219 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2220#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002221 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302222#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002223 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002224 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302225#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002226
2227 /* Enable interrupts */
2228 queue_writel(queue, IER,
2229 MACB_RX_INT_FLAGS |
2230 MACB_TX_INT_FLAGS |
2231 MACB_BIT(HRESP));
2232 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002233
2234 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002235 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236}
2237
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002238/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002239 * locations in the memory map. The least significant bits are stored
2240 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2241 *
2242 * The unicast hash enable and the multicast hash enable bits in the
2243 * network configuration register enable the reception of hash matched
2244 * frames. The destination address is reduced to a 6 bit index into
2245 * the 64 bit hash register using the following hash function. The
2246 * hash function is an exclusive or of every sixth bit of the
2247 * destination address.
2248 *
2249 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2250 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2251 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2252 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2253 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2254 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2255 *
2256 * da[0] represents the least significant bit of the first byte
2257 * received, that is, the multicast/unicast indicator, and da[47]
2258 * represents the most significant bit of the last byte received. If
2259 * the hash index, hi[n], points to a bit that is set in the hash
2260 * register then the frame will be matched according to whether the
2261 * frame is multicast or unicast. A multicast match will be signalled
2262 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2263 * index points to a bit set in the hash register. A unicast match
2264 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2265 * and the hash index points to a bit set in the hash register. To
2266 * receive all multicast frames, the hash register should be set with
2267 * all ones and the multicast hash enable bit should be set in the
2268 * network configuration register.
2269 */
2270
2271static inline int hash_bit_value(int bitnr, __u8 *addr)
2272{
2273 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2274 return 1;
2275 return 0;
2276}
2277
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002278/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002279static int hash_get_index(__u8 *addr)
2280{
2281 int i, j, bitval;
2282 int hash_index = 0;
2283
2284 for (j = 0; j < 6; j++) {
2285 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002286 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002287
2288 hash_index |= (bitval << j);
2289 }
2290
2291 return hash_index;
2292}
2293
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002294/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002295static void macb_sethashtable(struct net_device *dev)
2296{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002297 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002298 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002299 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002300 struct macb *bp = netdev_priv(dev);
2301
Moritz Fischeraa50b552016-03-29 19:11:13 -07002302 mc_filter[0] = 0;
2303 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002304
Jiri Pirko22bedad32010-04-01 21:22:57 +00002305 netdev_for_each_mc_addr(ha, dev) {
2306 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002307 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2308 }
2309
Jamie Ilesf75ba502011-11-08 10:12:32 +00002310 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2311 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002312}
2313
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002314/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002315static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002316{
2317 unsigned long cfg;
2318 struct macb *bp = netdev_priv(dev);
2319
2320 cfg = macb_readl(bp, NCFGR);
2321
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002322 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002323 /* Enable promiscuous mode */
2324 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002325
2326 /* Disable RX checksum offload */
2327 if (macb_is_gem(bp))
2328 cfg &= ~GEM_BIT(RXCOEN);
2329 } else {
2330 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002331 cfg &= ~MACB_BIT(CAF);
2332
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002333 /* Enable RX checksum offload only if requested */
2334 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2335 cfg |= GEM_BIT(RXCOEN);
2336 }
2337
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002338 if (dev->flags & IFF_ALLMULTI) {
2339 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002340 macb_or_gem_writel(bp, HRB, -1);
2341 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002342 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002343 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002344 /* Enable specific multicasts */
2345 macb_sethashtable(dev);
2346 cfg |= MACB_BIT(NCFGR_MTI);
2347 } else if (dev->flags & (~IFF_ALLMULTI)) {
2348 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002349 macb_or_gem_writel(bp, HRB, 0);
2350 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002351 cfg &= ~MACB_BIT(NCFGR_MTI);
2352 }
2353
2354 macb_writel(bp, NCFGR, cfg);
2355}
2356
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002357static int macb_open(struct net_device *dev)
2358{
2359 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002360 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002361 struct macb_queue *queue;
2362 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002363 int err;
2364
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002365 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002366
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002367 /* carrier starts down */
2368 netif_carrier_off(dev);
2369
frederic RODO6c36a702007-07-12 19:07:24 +02002370 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002371 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002372 return -EAGAIN;
2373
Nicolas Ferre1b447912013-06-04 21:57:11 +00002374 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002375 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002376
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002377 err = macb_alloc_consistent(bp);
2378 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002379 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2380 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002381 return err;
2382 }
2383
Nicolas Ferre4df95132013-06-04 21:57:12 +00002384 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002385 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002386
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002387 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2388 napi_enable(&queue->napi);
2389
frederic RODO6c36a702007-07-12 19:07:24 +02002390 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002391 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002392
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002393 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002394
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002395 if (bp->ptp_info)
2396 bp->ptp_info->ptp_init(dev);
2397
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002398 return 0;
2399}
2400
2401static int macb_close(struct net_device *dev)
2402{
2403 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002404 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002406 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002407
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002408 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002409
2410 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2411 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002412
Philippe Reynes0a912812016-06-22 00:32:35 +02002413 if (dev->phydev)
2414 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002415
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002416 spin_lock_irqsave(&bp->lock, flags);
2417 macb_reset_hw(bp);
2418 netif_carrier_off(dev);
2419 spin_unlock_irqrestore(&bp->lock, flags);
2420
2421 macb_free_consistent(bp);
2422
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002423 if (bp->ptp_info)
2424 bp->ptp_info->ptp_remove(dev);
2425
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002426 return 0;
2427}
2428
Harini Katakama5898ea2015-05-06 22:27:18 +05302429static int macb_change_mtu(struct net_device *dev, int new_mtu)
2430{
Harini Katakama5898ea2015-05-06 22:27:18 +05302431 if (netif_running(dev))
2432 return -EBUSY;
2433
Harini Katakama5898ea2015-05-06 22:27:18 +05302434 dev->mtu = new_mtu;
2435
2436 return 0;
2437}
2438
Jamie Ilesa494ed82011-03-09 16:26:35 +00002439static void gem_update_stats(struct macb *bp)
2440{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002441 struct macb_queue *queue;
2442 unsigned int i, q, idx;
2443 unsigned long *stat;
2444
Jamie Ilesa494ed82011-03-09 16:26:35 +00002445 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002446
Xander Huff3ff13f12015-01-13 16:15:51 -06002447 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2448 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002449 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002450
2451 bp->ethtool_stats[i] += val;
2452 *p += val;
2453
2454 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2455 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002456 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002457 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002458 *(++p) += val;
2459 }
2460 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002461
2462 idx = GEM_STATS_LEN;
2463 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2464 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2465 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002466}
2467
2468static struct net_device_stats *gem_get_stats(struct macb *bp)
2469{
2470 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002471 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002472
2473 gem_update_stats(bp);
2474
2475 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2476 hwstat->rx_alignment_errors +
2477 hwstat->rx_resource_errors +
2478 hwstat->rx_overruns +
2479 hwstat->rx_oversize_frames +
2480 hwstat->rx_jabbers +
2481 hwstat->rx_undersized_frames +
2482 hwstat->rx_length_field_frame_errors);
2483 nstat->tx_errors = (hwstat->tx_late_collisions +
2484 hwstat->tx_excessive_collisions +
2485 hwstat->tx_underrun +
2486 hwstat->tx_carrier_sense_errors);
2487 nstat->multicast = hwstat->rx_multicast_frames;
2488 nstat->collisions = (hwstat->tx_single_collision_frames +
2489 hwstat->tx_multiple_collision_frames +
2490 hwstat->tx_excessive_collisions);
2491 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2492 hwstat->rx_jabbers +
2493 hwstat->rx_undersized_frames +
2494 hwstat->rx_length_field_frame_errors);
2495 nstat->rx_over_errors = hwstat->rx_resource_errors;
2496 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2497 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2498 nstat->rx_fifo_errors = hwstat->rx_overruns;
2499 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2500 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2501 nstat->tx_fifo_errors = hwstat->tx_underrun;
2502
2503 return nstat;
2504}
2505
Xander Huff3ff13f12015-01-13 16:15:51 -06002506static void gem_get_ethtool_stats(struct net_device *dev,
2507 struct ethtool_stats *stats, u64 *data)
2508{
2509 struct macb *bp;
2510
2511 bp = netdev_priv(dev);
2512 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002513 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2514 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002515}
2516
2517static int gem_get_sset_count(struct net_device *dev, int sset)
2518{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002519 struct macb *bp = netdev_priv(dev);
2520
Xander Huff3ff13f12015-01-13 16:15:51 -06002521 switch (sset) {
2522 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002523 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002524 default:
2525 return -EOPNOTSUPP;
2526 }
2527}
2528
2529static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2530{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002531 char stat_string[ETH_GSTRING_LEN];
2532 struct macb *bp = netdev_priv(dev);
2533 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002534 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002535 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002536
2537 switch (sset) {
2538 case ETH_SS_STATS:
2539 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2540 memcpy(p, gem_statistics[i].stat_string,
2541 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002542
2543 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2544 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2545 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2546 q, queue_statistics[i].stat_string);
2547 memcpy(p, stat_string, ETH_GSTRING_LEN);
2548 }
2549 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002550 break;
2551 }
2552}
2553
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002554static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002555{
2556 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002557 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002558 struct macb_stats *hwstat = &bp->hw_stats.macb;
2559
2560 if (macb_is_gem(bp))
2561 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002562
frederic RODO6c36a702007-07-12 19:07:24 +02002563 /* read stats from hardware */
2564 macb_update_stats(bp);
2565
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002566 /* Convert HW stats into netdevice stats */
2567 nstat->rx_errors = (hwstat->rx_fcs_errors +
2568 hwstat->rx_align_errors +
2569 hwstat->rx_resource_errors +
2570 hwstat->rx_overruns +
2571 hwstat->rx_oversize_pkts +
2572 hwstat->rx_jabbers +
2573 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002574 hwstat->rx_length_mismatch);
2575 nstat->tx_errors = (hwstat->tx_late_cols +
2576 hwstat->tx_excessive_cols +
2577 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002578 hwstat->tx_carrier_errors +
2579 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002580 nstat->collisions = (hwstat->tx_single_cols +
2581 hwstat->tx_multiple_cols +
2582 hwstat->tx_excessive_cols);
2583 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2584 hwstat->rx_jabbers +
2585 hwstat->rx_undersize_pkts +
2586 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002587 nstat->rx_over_errors = hwstat->rx_resource_errors +
2588 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002589 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2590 nstat->rx_frame_errors = hwstat->rx_align_errors;
2591 nstat->rx_fifo_errors = hwstat->rx_overruns;
2592 /* XXX: What does "missed" mean? */
2593 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2594 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2595 nstat->tx_fifo_errors = hwstat->tx_underruns;
2596 /* Don't know about heartbeat or window errors... */
2597
2598 return nstat;
2599}
2600
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002601static int macb_get_regs_len(struct net_device *netdev)
2602{
2603 return MACB_GREGS_NBR * sizeof(u32);
2604}
2605
2606static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2607 void *p)
2608{
2609 struct macb *bp = netdev_priv(dev);
2610 unsigned int tail, head;
2611 u32 *regs_buff = p;
2612
2613 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2614 | MACB_GREGS_VERSION;
2615
Zach Brownb410d132016-10-19 09:56:57 -05002616 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2617 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002618
2619 regs_buff[0] = macb_readl(bp, NCR);
2620 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2621 regs_buff[2] = macb_readl(bp, NSR);
2622 regs_buff[3] = macb_readl(bp, TSR);
2623 regs_buff[4] = macb_readl(bp, RBQP);
2624 regs_buff[5] = macb_readl(bp, TBQP);
2625 regs_buff[6] = macb_readl(bp, RSR);
2626 regs_buff[7] = macb_readl(bp, IMR);
2627
2628 regs_buff[8] = tail;
2629 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002630 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2631 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002632
Neil Armstrongce721a72016-01-05 14:39:16 +01002633 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2634 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002635 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002636 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002637}
2638
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002639static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2640{
2641 struct macb *bp = netdev_priv(netdev);
2642
2643 wol->supported = 0;
2644 wol->wolopts = 0;
2645
2646 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2647 wol->supported = WAKE_MAGIC;
2648
2649 if (bp->wol & MACB_WOL_ENABLED)
2650 wol->wolopts |= WAKE_MAGIC;
2651 }
2652}
2653
2654static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2655{
2656 struct macb *bp = netdev_priv(netdev);
2657
2658 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2659 (wol->wolopts & ~WAKE_MAGIC))
2660 return -EOPNOTSUPP;
2661
2662 if (wol->wolopts & WAKE_MAGIC)
2663 bp->wol |= MACB_WOL_ENABLED;
2664 else
2665 bp->wol &= ~MACB_WOL_ENABLED;
2666
2667 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2668
2669 return 0;
2670}
2671
Zach Brown8441bb32016-10-19 09:56:58 -05002672static void macb_get_ringparam(struct net_device *netdev,
2673 struct ethtool_ringparam *ring)
2674{
2675 struct macb *bp = netdev_priv(netdev);
2676
2677 ring->rx_max_pending = MAX_RX_RING_SIZE;
2678 ring->tx_max_pending = MAX_TX_RING_SIZE;
2679
2680 ring->rx_pending = bp->rx_ring_size;
2681 ring->tx_pending = bp->tx_ring_size;
2682}
2683
2684static int macb_set_ringparam(struct net_device *netdev,
2685 struct ethtool_ringparam *ring)
2686{
2687 struct macb *bp = netdev_priv(netdev);
2688 u32 new_rx_size, new_tx_size;
2689 unsigned int reset = 0;
2690
2691 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2692 return -EINVAL;
2693
2694 new_rx_size = clamp_t(u32, ring->rx_pending,
2695 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2696 new_rx_size = roundup_pow_of_two(new_rx_size);
2697
2698 new_tx_size = clamp_t(u32, ring->tx_pending,
2699 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2700 new_tx_size = roundup_pow_of_two(new_tx_size);
2701
2702 if ((new_tx_size == bp->tx_ring_size) &&
2703 (new_rx_size == bp->rx_ring_size)) {
2704 /* nothing to do */
2705 return 0;
2706 }
2707
2708 if (netif_running(bp->dev)) {
2709 reset = 1;
2710 macb_close(bp->dev);
2711 }
2712
2713 bp->rx_ring_size = new_rx_size;
2714 bp->tx_ring_size = new_tx_size;
2715
2716 if (reset)
2717 macb_open(bp->dev);
2718
2719 return 0;
2720}
2721
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002722#ifdef CONFIG_MACB_USE_HWSTAMP
2723static unsigned int gem_get_tsu_rate(struct macb *bp)
2724{
2725 struct clk *tsu_clk;
2726 unsigned int tsu_rate;
2727
2728 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2729 if (!IS_ERR(tsu_clk))
2730 tsu_rate = clk_get_rate(tsu_clk);
2731 /* try pclk instead */
2732 else if (!IS_ERR(bp->pclk)) {
2733 tsu_clk = bp->pclk;
2734 tsu_rate = clk_get_rate(tsu_clk);
2735 } else
2736 return -ENOTSUPP;
2737 return tsu_rate;
2738}
2739
2740static s32 gem_get_ptp_max_adj(void)
2741{
2742 return 64000000;
2743}
2744
2745static int gem_get_ts_info(struct net_device *dev,
2746 struct ethtool_ts_info *info)
2747{
2748 struct macb *bp = netdev_priv(dev);
2749
2750 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2751 ethtool_op_get_ts_info(dev, info);
2752 return 0;
2753 }
2754
2755 info->so_timestamping =
2756 SOF_TIMESTAMPING_TX_SOFTWARE |
2757 SOF_TIMESTAMPING_RX_SOFTWARE |
2758 SOF_TIMESTAMPING_SOFTWARE |
2759 SOF_TIMESTAMPING_TX_HARDWARE |
2760 SOF_TIMESTAMPING_RX_HARDWARE |
2761 SOF_TIMESTAMPING_RAW_HARDWARE;
2762 info->tx_types =
2763 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2764 (1 << HWTSTAMP_TX_OFF) |
2765 (1 << HWTSTAMP_TX_ON);
2766 info->rx_filters =
2767 (1 << HWTSTAMP_FILTER_NONE) |
2768 (1 << HWTSTAMP_FILTER_ALL);
2769
2770 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2771
2772 return 0;
2773}
2774
2775static struct macb_ptp_info gem_ptp_info = {
2776 .ptp_init = gem_ptp_init,
2777 .ptp_remove = gem_ptp_remove,
2778 .get_ptp_max_adj = gem_get_ptp_max_adj,
2779 .get_tsu_rate = gem_get_tsu_rate,
2780 .get_ts_info = gem_get_ts_info,
2781 .get_hwtst = gem_get_hwtst,
2782 .set_hwtst = gem_set_hwtst,
2783};
2784#endif
2785
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002786static int macb_get_ts_info(struct net_device *netdev,
2787 struct ethtool_ts_info *info)
2788{
2789 struct macb *bp = netdev_priv(netdev);
2790
2791 if (bp->ptp_info)
2792 return bp->ptp_info->get_ts_info(netdev, info);
2793
2794 return ethtool_op_get_ts_info(netdev, info);
2795}
2796
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002797static void gem_enable_flow_filters(struct macb *bp, bool enable)
2798{
2799 struct ethtool_rx_fs_item *item;
2800 u32 t2_scr;
2801 int num_t2_scr;
2802
2803 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2804
2805 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2806 struct ethtool_rx_flow_spec *fs = &item->fs;
2807 struct ethtool_tcpip4_spec *tp4sp_m;
2808
2809 if (fs->location >= num_t2_scr)
2810 continue;
2811
2812 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2813
2814 /* enable/disable screener regs for the flow entry */
2815 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2816
2817 /* only enable fields with no masking */
2818 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2819
2820 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2821 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2822 else
2823 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2824
2825 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2826 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2827 else
2828 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2829
2830 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2831 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2832 else
2833 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2834
2835 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2836 }
2837}
2838
2839static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2840{
2841 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2842 uint16_t index = fs->location;
2843 u32 w0, w1, t2_scr;
2844 bool cmp_a = false;
2845 bool cmp_b = false;
2846 bool cmp_c = false;
2847
2848 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2849 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2850
2851 /* ignore field if any masking set */
2852 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2853 /* 1st compare reg - IP source address */
2854 w0 = 0;
2855 w1 = 0;
2856 w0 = tp4sp_v->ip4src;
2857 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2858 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2859 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2860 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2861 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2862 cmp_a = true;
2863 }
2864
2865 /* ignore field if any masking set */
2866 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2867 /* 2nd compare reg - IP destination address */
2868 w0 = 0;
2869 w1 = 0;
2870 w0 = tp4sp_v->ip4dst;
2871 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2872 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2873 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2874 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2875 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2876 cmp_b = true;
2877 }
2878
2879 /* ignore both port fields if masking set in both */
2880 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2881 /* 3rd compare reg - source port, destination port */
2882 w0 = 0;
2883 w1 = 0;
2884 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2885 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2886 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2887 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2888 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2889 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2890 } else {
2891 /* only one port definition */
2892 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2893 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2894 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2895 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2896 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2897 } else { /* dst port */
2898 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2899 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2900 }
2901 }
2902 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2903 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2904 cmp_c = true;
2905 }
2906
2907 t2_scr = 0;
2908 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2909 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2910 if (cmp_a)
2911 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2912 if (cmp_b)
2913 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2914 if (cmp_c)
2915 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2916 gem_writel_n(bp, SCRT2, index, t2_scr);
2917}
2918
2919static int gem_add_flow_filter(struct net_device *netdev,
2920 struct ethtool_rxnfc *cmd)
2921{
2922 struct macb *bp = netdev_priv(netdev);
2923 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2924 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002925 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002926 int ret = -EINVAL;
2927 bool added = false;
2928
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002929 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002930 if (newfs == NULL)
2931 return -ENOMEM;
2932 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2933
2934 netdev_dbg(netdev,
2935 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2936 fs->flow_type, (int)fs->ring_cookie, fs->location,
2937 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2938 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2939 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2940
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002941 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2942
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002943 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002944 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2945 if (item->fs.location > newfs->fs.location) {
2946 list_add_tail(&newfs->list, &item->list);
2947 added = true;
2948 break;
2949 } else if (item->fs.location == fs->location) {
2950 netdev_err(netdev, "Rule not added: location %d not free!\n",
2951 fs->location);
2952 ret = -EBUSY;
2953 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002954 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002955 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002956 if (!added)
2957 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002958
2959 gem_prog_cmp_regs(bp, fs);
2960 bp->rx_fs_list.count++;
2961 /* enable filtering if NTUPLE on */
2962 if (netdev->features & NETIF_F_NTUPLE)
2963 gem_enable_flow_filters(bp, 1);
2964
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002965 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002966 return 0;
2967
2968err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002969 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002970 kfree(newfs);
2971 return ret;
2972}
2973
2974static int gem_del_flow_filter(struct net_device *netdev,
2975 struct ethtool_rxnfc *cmd)
2976{
2977 struct macb *bp = netdev_priv(netdev);
2978 struct ethtool_rx_fs_item *item;
2979 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002980 unsigned long flags;
2981
2982 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002983
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002984 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2985 if (item->fs.location == cmd->fs.location) {
2986 /* disable screener regs for the flow entry */
2987 fs = &(item->fs);
2988 netdev_dbg(netdev,
2989 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2990 fs->flow_type, (int)fs->ring_cookie, fs->location,
2991 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2992 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2993 htons(fs->h_u.tcp_ip4_spec.psrc),
2994 htons(fs->h_u.tcp_ip4_spec.pdst));
2995
2996 gem_writel_n(bp, SCRT2, fs->location, 0);
2997
2998 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002999 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003000 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3001 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003002 return 0;
3003 }
3004 }
3005
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003006 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003007 return -EINVAL;
3008}
3009
3010static int gem_get_flow_entry(struct net_device *netdev,
3011 struct ethtool_rxnfc *cmd)
3012{
3013 struct macb *bp = netdev_priv(netdev);
3014 struct ethtool_rx_fs_item *item;
3015
3016 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3017 if (item->fs.location == cmd->fs.location) {
3018 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3019 return 0;
3020 }
3021 }
3022 return -EINVAL;
3023}
3024
3025static int gem_get_all_flow_entries(struct net_device *netdev,
3026 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3027{
3028 struct macb *bp = netdev_priv(netdev);
3029 struct ethtool_rx_fs_item *item;
3030 uint32_t cnt = 0;
3031
3032 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3033 if (cnt == cmd->rule_cnt)
3034 return -EMSGSIZE;
3035 rule_locs[cnt] = item->fs.location;
3036 cnt++;
3037 }
3038 cmd->data = bp->max_tuples;
3039 cmd->rule_cnt = cnt;
3040
3041 return 0;
3042}
3043
3044static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3045 u32 *rule_locs)
3046{
3047 struct macb *bp = netdev_priv(netdev);
3048 int ret = 0;
3049
3050 switch (cmd->cmd) {
3051 case ETHTOOL_GRXRINGS:
3052 cmd->data = bp->num_queues;
3053 break;
3054 case ETHTOOL_GRXCLSRLCNT:
3055 cmd->rule_cnt = bp->rx_fs_list.count;
3056 break;
3057 case ETHTOOL_GRXCLSRULE:
3058 ret = gem_get_flow_entry(netdev, cmd);
3059 break;
3060 case ETHTOOL_GRXCLSRLALL:
3061 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3062 break;
3063 default:
3064 netdev_err(netdev,
3065 "Command parameter %d is not supported\n", cmd->cmd);
3066 ret = -EOPNOTSUPP;
3067 }
3068
3069 return ret;
3070}
3071
3072static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3073{
3074 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003075 int ret;
3076
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003077 switch (cmd->cmd) {
3078 case ETHTOOL_SRXCLSRLINS:
3079 if ((cmd->fs.location >= bp->max_tuples)
3080 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3081 ret = -EINVAL;
3082 break;
3083 }
3084 ret = gem_add_flow_filter(netdev, cmd);
3085 break;
3086 case ETHTOOL_SRXCLSRLDEL:
3087 ret = gem_del_flow_filter(netdev, cmd);
3088 break;
3089 default:
3090 netdev_err(netdev,
3091 "Command parameter %d is not supported\n", cmd->cmd);
3092 ret = -EOPNOTSUPP;
3093 }
3094
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003095 return ret;
3096}
3097
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003098static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003099 .get_regs_len = macb_get_regs_len,
3100 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003101 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003102 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003103 .get_wol = macb_get_wol,
3104 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003105 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3106 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003107 .get_ringparam = macb_get_ringparam,
3108 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003109};
Xander Huff8cd5a562015-01-15 15:55:20 -06003110
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003111static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003112 .get_regs_len = macb_get_regs_len,
3113 .get_regs = macb_get_regs,
3114 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003115 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003116 .get_ethtool_stats = gem_get_ethtool_stats,
3117 .get_strings = gem_get_ethtool_strings,
3118 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003119 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3120 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003121 .get_ringparam = macb_get_ringparam,
3122 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003123 .get_rxnfc = gem_get_rxnfc,
3124 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003125};
3126
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003127static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003128{
Philippe Reynes0a912812016-06-22 00:32:35 +02003129 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003130 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003131
3132 if (!netif_running(dev))
3133 return -EINVAL;
3134
frederic RODO6c36a702007-07-12 19:07:24 +02003135 if (!phydev)
3136 return -ENODEV;
3137
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003138 if (!bp->ptp_info)
3139 return phy_mii_ioctl(phydev, rq, cmd);
3140
3141 switch (cmd) {
3142 case SIOCSHWTSTAMP:
3143 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3144 case SIOCGHWTSTAMP:
3145 return bp->ptp_info->get_hwtst(dev, rq);
3146 default:
3147 return phy_mii_ioctl(phydev, rq, cmd);
3148 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003149}
3150
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003151static int macb_set_features(struct net_device *netdev,
3152 netdev_features_t features)
3153{
3154 struct macb *bp = netdev_priv(netdev);
3155 netdev_features_t changed = features ^ netdev->features;
3156
3157 /* TX checksum offload */
3158 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3159 u32 dmacfg;
3160
3161 dmacfg = gem_readl(bp, DMACFG);
3162 if (features & NETIF_F_HW_CSUM)
3163 dmacfg |= GEM_BIT(TXCOEN);
3164 else
3165 dmacfg &= ~GEM_BIT(TXCOEN);
3166 gem_writel(bp, DMACFG, dmacfg);
3167 }
3168
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003169 /* RX checksum offload */
3170 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3171 u32 netcfg;
3172
3173 netcfg = gem_readl(bp, NCFGR);
3174 if (features & NETIF_F_RXCSUM &&
3175 !(netdev->flags & IFF_PROMISC))
3176 netcfg |= GEM_BIT(RXCOEN);
3177 else
3178 netcfg &= ~GEM_BIT(RXCOEN);
3179 gem_writel(bp, NCFGR, netcfg);
3180 }
3181
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003182 /* RX Flow Filters */
3183 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3184 bool turn_on = features & NETIF_F_NTUPLE;
3185
3186 gem_enable_flow_filters(bp, turn_on);
3187 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003188 return 0;
3189}
3190
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003191static const struct net_device_ops macb_netdev_ops = {
3192 .ndo_open = macb_open,
3193 .ndo_stop = macb_close,
3194 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003195 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003196 .ndo_get_stats = macb_get_stats,
3197 .ndo_do_ioctl = macb_ioctl,
3198 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303199 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003200 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003201#ifdef CONFIG_NET_POLL_CONTROLLER
3202 .ndo_poll_controller = macb_poll_controller,
3203#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003204 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003205 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003206};
3207
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003208/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003209 * and integration options used
3210 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003211static void macb_configure_caps(struct macb *bp,
3212 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003213{
3214 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003215
Nicolas Ferref6970502015-03-31 15:02:01 +02003216 if (dt_conf)
3217 bp->caps = dt_conf->caps;
3218
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003219 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003220 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3221
Nicolas Ferree1755872014-07-24 13:50:58 +02003222 dcfg = gem_readl(bp, DCFG1);
3223 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3224 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3225 dcfg = gem_readl(bp, DCFG2);
3226 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3227 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003228#ifdef CONFIG_MACB_USE_HWSTAMP
3229 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003230 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3231 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003232 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003233 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003234 bp->ptp_info = &gem_ptp_info;
3235 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003236 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003237#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003238 }
3239
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003240 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003241}
3242
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003243static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003244 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003245 unsigned int *queue_mask,
3246 unsigned int *num_queues)
3247{
3248 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003249
3250 *queue_mask = 0x1;
3251 *num_queues = 1;
3252
Nicolas Ferreda120112015-03-31 15:02:00 +02003253 /* is it macb or gem ?
3254 *
3255 * We need to read directly from the hardware here because
3256 * we are early in the probe process and don't have the
3257 * MACB_CAPS_MACB_IS_GEM flag positioned
3258 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003259 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003260 return;
3261
3262 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303263 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3264
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003265 *queue_mask |= 0x1;
3266
3267 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3268 if (*queue_mask & (1 << hw_q))
3269 (*num_queues)++;
3270}
3271
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003272static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303273 struct clk **hclk, struct clk **tx_clk,
3274 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003275{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003276 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003277 int err;
3278
Bartosz Folta83a77e92016-12-14 06:39:15 +00003279 pdata = dev_get_platdata(&pdev->dev);
3280 if (pdata) {
3281 *pclk = pdata->pclk;
3282 *hclk = pdata->hclk;
3283 } else {
3284 *pclk = devm_clk_get(&pdev->dev, "pclk");
3285 *hclk = devm_clk_get(&pdev->dev, "hclk");
3286 }
3287
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003288 if (IS_ERR(*pclk)) {
3289 err = PTR_ERR(*pclk);
3290 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3291 return err;
3292 }
3293
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003294 if (IS_ERR(*hclk)) {
3295 err = PTR_ERR(*hclk);
3296 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3297 return err;
3298 }
3299
3300 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3301 if (IS_ERR(*tx_clk))
3302 *tx_clk = NULL;
3303
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303304 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3305 if (IS_ERR(*rx_clk))
3306 *rx_clk = NULL;
3307
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003308 err = clk_prepare_enable(*pclk);
3309 if (err) {
3310 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3311 return err;
3312 }
3313
3314 err = clk_prepare_enable(*hclk);
3315 if (err) {
3316 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3317 goto err_disable_pclk;
3318 }
3319
3320 err = clk_prepare_enable(*tx_clk);
3321 if (err) {
3322 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3323 goto err_disable_hclk;
3324 }
3325
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303326 err = clk_prepare_enable(*rx_clk);
3327 if (err) {
3328 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3329 goto err_disable_txclk;
3330 }
3331
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003332 return 0;
3333
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303334err_disable_txclk:
3335 clk_disable_unprepare(*tx_clk);
3336
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003337err_disable_hclk:
3338 clk_disable_unprepare(*hclk);
3339
3340err_disable_pclk:
3341 clk_disable_unprepare(*pclk);
3342
3343 return err;
3344}
3345
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003346static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003347{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003348 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003349 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003350 struct macb *bp = netdev_priv(dev);
3351 struct macb_queue *queue;
3352 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003353 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003354
Zach Brownb410d132016-10-19 09:56:57 -05003355 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3356 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3357
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003358 /* set the queue register mapping once for all: queue0 has a special
3359 * register mapping but we don't want to test the queue index then
3360 * compute the corresponding register offset at run time.
3361 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003362 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003363 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003364 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003365
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003366 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003367 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003368 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003369 if (hw_q) {
3370 queue->ISR = GEM_ISR(hw_q - 1);
3371 queue->IER = GEM_IER(hw_q - 1);
3372 queue->IDR = GEM_IDR(hw_q - 1);
3373 queue->IMR = GEM_IMR(hw_q - 1);
3374 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003375 queue->RBQP = GEM_RBQP(hw_q - 1);
3376 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303377#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003378 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003379 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003380 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3381 }
Harini Katakamfff80192016-08-09 13:15:53 +05303382#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003383 } else {
3384 /* queue0 uses legacy registers */
3385 queue->ISR = MACB_ISR;
3386 queue->IER = MACB_IER;
3387 queue->IDR = MACB_IDR;
3388 queue->IMR = MACB_IMR;
3389 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003390 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303391#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003392 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003393 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003394 queue->RBQPH = MACB_RBQPH;
3395 }
Harini Katakamfff80192016-08-09 13:15:53 +05303396#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003397 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003398
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003399 /* get irq: here we use the linux queue index, not the hardware
3400 * queue index. the queue irq definitions in the device tree
3401 * must remove the optional gaps that could exist in the
3402 * hardware queue mask.
3403 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003404 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003405 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003406 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003407 if (err) {
3408 dev_err(&pdev->dev,
3409 "Unable to request IRQ %d (error %d)\n",
3410 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003411 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003412 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003413
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003414 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003415 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003416 }
3417
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003418 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003419
Nicolas Ferre4df95132013-06-04 21:57:12 +00003420 /* setup appropriated routines according to adapter type */
3421 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003422 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003423 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3424 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3425 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3426 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003427 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003428 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003429 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003430 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3431 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3432 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3433 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003434 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003435 }
3436
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003437 /* Set features */
3438 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003439
3440 /* Check LSO capability */
3441 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3442 dev->hw_features |= MACB_NETIF_LSO;
3443
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003444 /* Checksum offload is only available on gem with packet buffer */
3445 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003446 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003447 if (bp->caps & MACB_CAPS_SG_DISABLED)
3448 dev->hw_features &= ~NETIF_F_SG;
3449 dev->features = dev->hw_features;
3450
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003451 /* Check RX Flow Filters support.
3452 * Max Rx flows set by availability of screeners & compare regs:
3453 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3454 */
3455 reg = gem_readl(bp, DCFG8);
3456 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3457 GEM_BFEXT(T2SCR, reg));
3458 if (bp->max_tuples > 0) {
3459 /* also needs one ethtype match to check IPv4 */
3460 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3461 /* program this reg now */
3462 reg = 0;
3463 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3464 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3465 /* Filtering is supported in hw but don't enable it in kernel now */
3466 dev->hw_features |= NETIF_F_NTUPLE;
3467 /* init Rx flow definitions */
3468 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3469 bp->rx_fs_list.count = 0;
3470 spin_lock_init(&bp->rx_fs_lock);
3471 } else
3472 bp->max_tuples = 0;
3473 }
3474
Neil Armstrongce721a72016-01-05 14:39:16 +01003475 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3476 val = 0;
3477 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3478 val = GEM_BIT(RGMII);
3479 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003480 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003481 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003482 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003483 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003484
Neil Armstrongce721a72016-01-05 14:39:16 +01003485 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3486 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003487
Neil Armstrongce721a72016-01-05 14:39:16 +01003488 macb_or_gem_writel(bp, USRIO, val);
3489 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003490
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003491 /* Set MII management clock divider */
3492 val = macb_mdc_clk_div(bp);
3493 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303494 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3495 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003496 macb_writel(bp, NCFGR, val);
3497
3498 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003499}
3500
3501#if defined(CONFIG_OF)
3502/* 1518 rounded up */
3503#define AT91ETHER_MAX_RBUFF_SZ 0x600
3504/* max number of receive buffers */
3505#define AT91ETHER_MAX_RX_DESCR 9
3506
3507/* Initialize and start the Receiver and Transmit subsystems */
3508static int at91ether_start(struct net_device *dev)
3509{
3510 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003511 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003512 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003513 dma_addr_t addr;
3514 u32 ctl;
3515 int i;
3516
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003517 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003518 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003519 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003520 &q->rx_ring_dma, GFP_KERNEL);
3521 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003522 return -ENOMEM;
3523
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003524 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003525 AT91ETHER_MAX_RX_DESCR *
3526 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003527 &q->rx_buffers_dma, GFP_KERNEL);
3528 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003529 dma_free_coherent(&lp->pdev->dev,
3530 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003531 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003532 q->rx_ring, q->rx_ring_dma);
3533 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003534 return -ENOMEM;
3535 }
3536
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003537 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003538 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003539 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003540 macb_set_addr(lp, desc, addr);
3541 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003542 addr += AT91ETHER_MAX_RBUFF_SZ;
3543 }
3544
3545 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003546 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003547
3548 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003549 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003550
3551 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003552 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003553
3554 /* Enable Receive and Transmit */
3555 ctl = macb_readl(lp, NCR);
3556 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3557
3558 return 0;
3559}
3560
3561/* Open the ethernet interface */
3562static int at91ether_open(struct net_device *dev)
3563{
3564 struct macb *lp = netdev_priv(dev);
3565 u32 ctl;
3566 int ret;
3567
3568 /* Clear internal statistics */
3569 ctl = macb_readl(lp, NCR);
3570 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3571
3572 macb_set_hwaddr(lp);
3573
3574 ret = at91ether_start(dev);
3575 if (ret)
3576 return ret;
3577
3578 /* Enable MAC interrupts */
3579 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3580 MACB_BIT(RXUBR) |
3581 MACB_BIT(ISR_TUND) |
3582 MACB_BIT(ISR_RLE) |
3583 MACB_BIT(TCOMP) |
3584 MACB_BIT(ISR_ROVR) |
3585 MACB_BIT(HRESP));
3586
3587 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003588 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003589
3590 netif_start_queue(dev);
3591
3592 return 0;
3593}
3594
3595/* Close the interface */
3596static int at91ether_close(struct net_device *dev)
3597{
3598 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003599 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003600 u32 ctl;
3601
3602 /* Disable Receiver and Transmitter */
3603 ctl = macb_readl(lp, NCR);
3604 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3605
3606 /* Disable MAC interrupts */
3607 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3608 MACB_BIT(RXUBR) |
3609 MACB_BIT(ISR_TUND) |
3610 MACB_BIT(ISR_RLE) |
3611 MACB_BIT(TCOMP) |
3612 MACB_BIT(ISR_ROVR) |
3613 MACB_BIT(HRESP));
3614
3615 netif_stop_queue(dev);
3616
3617 dma_free_coherent(&lp->pdev->dev,
3618 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003619 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003620 q->rx_ring, q->rx_ring_dma);
3621 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003622
3623 dma_free_coherent(&lp->pdev->dev,
3624 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003625 q->rx_buffers, q->rx_buffers_dma);
3626 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003627
3628 return 0;
3629}
3630
3631/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003632static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3633 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003634{
3635 struct macb *lp = netdev_priv(dev);
3636
3637 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3638 netif_stop_queue(dev);
3639
3640 /* Store packet information (to free when Tx completed) */
3641 lp->skb = skb;
3642 lp->skb_length = skb->len;
3643 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3644 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003645 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3646 dev_kfree_skb_any(skb);
3647 dev->stats.tx_dropped++;
3648 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3649 return NETDEV_TX_OK;
3650 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003651
3652 /* Set address of the data in the Transmit Address register */
3653 macb_writel(lp, TAR, lp->skb_physaddr);
3654 /* Set length of the packet in the Transmit Control register */
3655 macb_writel(lp, TCR, skb->len);
3656
3657 } else {
3658 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3659 return NETDEV_TX_BUSY;
3660 }
3661
3662 return NETDEV_TX_OK;
3663}
3664
3665/* Extract received frame from buffer descriptors and sent to upper layers.
3666 * (Called from interrupt context)
3667 */
3668static void at91ether_rx(struct net_device *dev)
3669{
3670 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003671 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003672 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003673 unsigned char *p_recv;
3674 struct sk_buff *skb;
3675 unsigned int pktlen;
3676
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003677 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003678 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003679 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003680 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003681 skb = netdev_alloc_skb(dev, pktlen + 2);
3682 if (skb) {
3683 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003684 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003685
3686 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003687 dev->stats.rx_packets++;
3688 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003689 netif_rx(skb);
3690 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003691 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003692 }
3693
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003694 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003695 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003696
3697 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003698 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003699
3700 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003701 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3702 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003703 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003704 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003705
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003706 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003707 }
3708}
3709
3710/* MAC interrupt handler */
3711static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3712{
3713 struct net_device *dev = dev_id;
3714 struct macb *lp = netdev_priv(dev);
3715 u32 intstatus, ctl;
3716
3717 /* MAC Interrupt Status register indicates what interrupts are pending.
3718 * It is automatically cleared once read.
3719 */
3720 intstatus = macb_readl(lp, ISR);
3721
3722 /* Receive complete */
3723 if (intstatus & MACB_BIT(RCOMP))
3724 at91ether_rx(dev);
3725
3726 /* Transmit complete */
3727 if (intstatus & MACB_BIT(TCOMP)) {
3728 /* The TCOM bit is set even if the transmission failed */
3729 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003730 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003731
3732 if (lp->skb) {
3733 dev_kfree_skb_irq(lp->skb);
3734 lp->skb = NULL;
3735 dma_unmap_single(NULL, lp->skb_physaddr,
3736 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003737 dev->stats.tx_packets++;
3738 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003739 }
3740 netif_wake_queue(dev);
3741 }
3742
3743 /* Work-around for EMAC Errata section 41.3.1 */
3744 if (intstatus & MACB_BIT(RXUBR)) {
3745 ctl = macb_readl(lp, NCR);
3746 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003747 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003748 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3749 }
3750
3751 if (intstatus & MACB_BIT(ISR_ROVR))
3752 netdev_err(dev, "ROVR error\n");
3753
3754 return IRQ_HANDLED;
3755}
3756
3757#ifdef CONFIG_NET_POLL_CONTROLLER
3758static void at91ether_poll_controller(struct net_device *dev)
3759{
3760 unsigned long flags;
3761
3762 local_irq_save(flags);
3763 at91ether_interrupt(dev->irq, dev);
3764 local_irq_restore(flags);
3765}
3766#endif
3767
3768static const struct net_device_ops at91ether_netdev_ops = {
3769 .ndo_open = at91ether_open,
3770 .ndo_stop = at91ether_close,
3771 .ndo_start_xmit = at91ether_start_xmit,
3772 .ndo_get_stats = macb_get_stats,
3773 .ndo_set_rx_mode = macb_set_rx_mode,
3774 .ndo_set_mac_address = eth_mac_addr,
3775 .ndo_do_ioctl = macb_ioctl,
3776 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003777#ifdef CONFIG_NET_POLL_CONTROLLER
3778 .ndo_poll_controller = at91ether_poll_controller,
3779#endif
3780};
3781
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003782static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303783 struct clk **hclk, struct clk **tx_clk,
3784 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003785{
3786 int err;
3787
3788 *hclk = NULL;
3789 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303790 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003791
3792 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3793 if (IS_ERR(*pclk))
3794 return PTR_ERR(*pclk);
3795
3796 err = clk_prepare_enable(*pclk);
3797 if (err) {
3798 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3799 return err;
3800 }
3801
3802 return 0;
3803}
3804
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003805static int at91ether_init(struct platform_device *pdev)
3806{
3807 struct net_device *dev = platform_get_drvdata(pdev);
3808 struct macb *bp = netdev_priv(dev);
3809 int err;
3810 u32 reg;
3811
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003812 bp->queues[0].bp = bp;
3813
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003814 dev->netdev_ops = &at91ether_netdev_ops;
3815 dev->ethtool_ops = &macb_ethtool_ops;
3816
3817 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3818 0, dev->name, dev);
3819 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003820 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003821
3822 macb_writel(bp, NCR, 0);
3823
3824 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3825 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3826 reg |= MACB_BIT(RM9200_RMII);
3827
3828 macb_writel(bp, NCFGR, reg);
3829
3830 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003831}
3832
David S. Miller3cef5c52015-03-09 23:38:02 -04003833static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003834 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003835 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003836 .init = macb_init,
3837};
3838
David S. Miller3cef5c52015-03-09 23:38:02 -04003839static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003840 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3841 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003842 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003843 .init = macb_init,
3844};
3845
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003846static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003847 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003848 .dma_burst_length = 16,
3849 .clk_init = macb_clk_init,
3850 .init = macb_init,
3851};
3852
David S. Miller3cef5c52015-03-09 23:38:02 -04003853static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003854 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003855 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003856 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003857 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003858 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003859 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003860};
3861
David S. Miller3cef5c52015-03-09 23:38:02 -04003862static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003863 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003864 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003865 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003866 .init = macb_init,
3867};
3868
David S. Miller3cef5c52015-03-09 23:38:02 -04003869static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003870 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003871 .init = at91ether_init,
3872};
3873
Neil Armstronge611b5b2016-01-05 14:39:17 +01003874static const struct macb_config np4_config = {
3875 .caps = MACB_CAPS_USRIO_DISABLED,
3876 .clk_init = macb_clk_init,
3877 .init = macb_init,
3878};
David S. Miller36583eb2015-05-23 01:22:35 -04003879
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303880static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003881 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3882 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05303883 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303884 .dma_burst_length = 16,
3885 .clk_init = macb_clk_init,
3886 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303887 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303888};
3889
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003890static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303891 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003892 .dma_burst_length = 16,
3893 .clk_init = macb_clk_init,
3894 .init = macb_init,
3895};
3896
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003897static const struct of_device_id macb_dt_ids[] = {
3898 { .compatible = "cdns,at32ap7000-macb" },
3899 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3900 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003901 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003902 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3903 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003904 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003905 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3906 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3907 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3908 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303909 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003910 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003911 { /* sentinel */ }
3912};
3913MODULE_DEVICE_TABLE(of, macb_dt_ids);
3914#endif /* CONFIG_OF */
3915
Bartosz Folta83a77e92016-12-14 06:39:15 +00003916static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003917 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3918 MACB_CAPS_JUMBO |
3919 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003920 .dma_burst_length = 16,
3921 .clk_init = macb_clk_init,
3922 .init = macb_init,
3923 .jumbo_max_len = 10240,
3924};
3925
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003926static int macb_probe(struct platform_device *pdev)
3927{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003928 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003929 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303930 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003931 = macb_config->clk_init;
3932 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003933 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303934 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003935 unsigned int queue_mask, num_queues;
3936 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003937 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003938 struct phy_device *phydev;
3939 struct net_device *dev;
3940 struct resource *regs;
3941 void __iomem *mem;
3942 const char *mac;
3943 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05303944 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003945
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003946 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3947 mem = devm_ioremap_resource(&pdev->dev, regs);
3948 if (IS_ERR(mem))
3949 return PTR_ERR(mem);
3950
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003951 if (np) {
3952 const struct of_device_id *match;
3953
3954 match = of_match_node(macb_dt_ids, np);
3955 if (match && match->data) {
3956 macb_config = match->data;
3957 clk_init = macb_config->clk_init;
3958 init = macb_config->init;
3959 }
3960 }
3961
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303962 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003963 if (err)
3964 return err;
3965
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003966 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003967
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003968 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003969 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003970 if (!dev) {
3971 err = -ENOMEM;
3972 goto err_disable_clocks;
3973 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003974
3975 dev->base_addr = regs->start;
3976
3977 SET_NETDEV_DEV(dev, &pdev->dev);
3978
3979 bp = netdev_priv(dev);
3980 bp->pdev = pdev;
3981 bp->dev = dev;
3982 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003983 bp->native_io = native_io;
3984 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003985 bp->macb_reg_readl = hw_readl_native;
3986 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003987 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003988 bp->macb_reg_readl = hw_readl;
3989 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003990 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003991 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003992 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003993 if (macb_config)
3994 bp->dma_burst_length = macb_config->dma_burst_length;
3995 bp->pclk = pclk;
3996 bp->hclk = hclk;
3997 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303998 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003999 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304000 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304001
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004002 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004003 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004004 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4005 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4006
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004007 spin_lock_init(&bp->lock);
4008
Nicolas Ferread783472015-03-31 15:02:02 +02004009 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004010 macb_configure_caps(bp, macb_config);
4011
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004012#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4013 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4014 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4015 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4016 }
4017#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004018 platform_set_drvdata(pdev, dev);
4019
4020 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004021 if (dev->irq < 0) {
4022 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004023 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004024 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004025
Jarod Wilson44770e12016-10-17 15:54:17 -04004026 /* MTU range: 68 - 1500 or 10240 */
4027 dev->min_mtu = GEM_MTU_MIN_SIZE;
4028 if (bp->caps & MACB_CAPS_JUMBO)
4029 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4030 else
4031 dev->max_mtu = ETH_DATA_LEN;
4032
Harini Katakam404cd082018-07-06 12:18:58 +05304033 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4034 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4035 if (val)
4036 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4037 macb_dma_desc_get_size(bp);
4038
4039 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4040 if (val)
4041 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4042 macb_dma_desc_get_size(bp);
4043 }
4044
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004045 mac = of_get_mac_address(np);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004046 if (mac) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004047 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004048 } else {
4049 err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
4050 if (err) {
4051 if (err == -EPROBE_DEFER)
4052 goto err_out_free_netdev;
4053 macb_get_hwaddr(bp);
4054 }
4055 }
frederic RODO6c36a702007-07-12 19:07:24 +02004056
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004057 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004058 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09004059 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004060 if (pdata && pdata->is_rmii)
4061 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4062 else
4063 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4064 } else {
4065 bp->phy_interface = err;
4066 }
4067
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004068 /* IP specific init */
4069 err = init(pdev);
4070 if (err)
4071 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004072
Florian Fainellicf669662016-05-02 18:38:45 -07004073 err = macb_mii_init(bp);
4074 if (err)
4075 goto err_out_free_netdev;
4076
Philippe Reynes0a912812016-06-22 00:32:35 +02004077 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004078
4079 netif_carrier_off(dev);
4080
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004081 err = register_netdev(dev);
4082 if (err) {
4083 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004084 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004085 }
4086
Harini Katakam032dc412018-01-27 12:09:01 +05304087 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4088 (unsigned long)bp);
4089
Florian Fainellicf669662016-05-02 18:38:45 -07004090 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004091
Bo Shen58798232014-09-13 01:57:49 +02004092 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4093 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4094 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004095
4096 return 0;
4097
Florian Fainellicf669662016-05-02 18:38:45 -07004098err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004099 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004100 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004101 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004102 if (np && of_phy_is_fixed_link(np))
4103 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004104 mdiobus_free(bp->mii_bus);
4105
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004106err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004107 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004108
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004109err_disable_clocks:
4110 clk_disable_unprepare(tx_clk);
4111 clk_disable_unprepare(hclk);
4112 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304113 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004114
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004115 return err;
4116}
4117
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004118static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004119{
4120 struct net_device *dev;
4121 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004122 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004123
4124 dev = platform_get_drvdata(pdev);
4125
4126 if (dev) {
4127 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004128 if (dev->phydev)
4129 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004130 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004131 if (np && of_phy_is_fixed_link(np))
4132 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004133 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004134 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004135
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004136 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004137 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004138 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004139 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304140 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004141 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004142 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004143 }
4144
4145 return 0;
4146}
4147
Michal Simekd23823d2015-01-23 09:36:03 +01004148static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004149{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004150 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004151 struct net_device *netdev = platform_get_drvdata(pdev);
4152 struct macb *bp = netdev_priv(netdev);
4153
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004154 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004155 netif_device_detach(netdev);
4156
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004157 if (bp->wol & MACB_WOL_ENABLED) {
4158 macb_writel(bp, IER, MACB_BIT(WOL));
4159 macb_writel(bp, WOL, MACB_BIT(MAG));
4160 enable_irq_wake(bp->queues[0].irq);
4161 } else {
4162 clk_disable_unprepare(bp->tx_clk);
4163 clk_disable_unprepare(bp->hclk);
4164 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304165 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004166 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004167
4168 return 0;
4169}
4170
Michal Simekd23823d2015-01-23 09:36:03 +01004171static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004172{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004173 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004174 struct net_device *netdev = platform_get_drvdata(pdev);
4175 struct macb *bp = netdev_priv(netdev);
4176
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004177 if (bp->wol & MACB_WOL_ENABLED) {
4178 macb_writel(bp, IDR, MACB_BIT(WOL));
4179 macb_writel(bp, WOL, 0);
4180 disable_irq_wake(bp->queues[0].irq);
4181 } else {
4182 clk_prepare_enable(bp->pclk);
4183 clk_prepare_enable(bp->hclk);
4184 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304185 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004186 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004187
4188 netif_device_attach(netdev);
4189
4190 return 0;
4191}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004192
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004193static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4194
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004195static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004196 .probe = macb_probe,
4197 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004198 .driver = {
4199 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004200 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004201 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004202 },
4203};
4204
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004205module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004206
4207MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004208MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004209MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004210MODULE_ALIAS("platform:macb");