blob: dfaefe91b96a0d618c313bde54b1e0c3e7566470 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030013#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000018#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010019#include <linux/slab.h>
20#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080021#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000022#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010023#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000028#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010029#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020030#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080031#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010033#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020034#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010035#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000036#include <linux/ip.h>
37#include <linux/udp.h>
38#include <linux/tcp.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010039#include "macb.h"
40
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000042#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050043
Zach Brownb410d132016-10-19 09:56:57 -050044#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050045#define MIN_RX_RING_SIZE 64
46#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000047#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050048 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010049
Zach Brownb410d132016-10-19 09:56:57 -050050#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050051#define MIN_TX_RING_SIZE 64
52#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000053#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050054 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
Nicolas Ferre909a8582012-11-19 06:00:21 +000056/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050057#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010058
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))
Claudiu Beznea42983882018-12-17 10:02:42 +000064#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
65 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000066
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000067/* Max length of transmit frame must be a multiple of 8 bytes */
68#define MACB_TX_LEN_ALIGN 8
69#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
70#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 +020071
Jarod Wilson44770e12016-10-17 15:54:17 -040072#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070073#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053074
Sergio Prado3e2a5e12016-02-09 12:07:16 -020075#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
76#define MACB_WOL_ENABLED (0x1 << 1)
77
Moritz Fischer64ec42f2016-03-29 19:11:12 -070078/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000079 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
80 */
81#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010082
Rafal Ozieblodc97a892017-01-27 15:08:20 +000083/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010084 * depends on hardware configuration:
85 *
86 * 1. dma address width 32 bits:
87 * word 1: 32 bit address of Data Buffer
88 * word 2: control
89 *
90 * 2. dma address width 64 bits:
91 * word 1: 32 bit address of Data Buffer
92 * word 2: control
93 * word 3: upper 32 bit address of Data Buffer
94 * word 4: unused
95 *
96 * 3. dma address width 32 bits with hardware timestamping:
97 * word 1: 32 bit address of Data Buffer
98 * word 2: control
99 * word 3: timestamp word 1
100 * word 4: timestamp word 2
101 *
102 * 4. dma address width 64 bits with hardware timestamping:
103 * word 1: 32 bit address of Data Buffer
104 * word 2: control
105 * word 3: upper 32 bit address of Data Buffer
106 * word 4: unused
107 * word 5: timestamp word 1
108 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000109 */
110static unsigned int macb_dma_desc_get_size(struct macb *bp)
111{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100112#ifdef MACB_EXT_DESC
113 unsigned int desc_size;
114
115 switch (bp->hw_dma_cap) {
116 case HW_DMA_CAP_64B:
117 desc_size = sizeof(struct macb_dma_desc)
118 + sizeof(struct macb_dma_desc_64);
119 break;
120 case HW_DMA_CAP_PTP:
121 desc_size = sizeof(struct macb_dma_desc)
122 + sizeof(struct macb_dma_desc_ptp);
123 break;
124 case HW_DMA_CAP_64B_PTP:
125 desc_size = sizeof(struct macb_dma_desc)
126 + sizeof(struct macb_dma_desc_64)
127 + sizeof(struct macb_dma_desc_ptp);
128 break;
129 default:
130 desc_size = sizeof(struct macb_dma_desc);
131 }
132 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000133#endif
134 return sizeof(struct macb_dma_desc);
135}
136
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100137static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000138{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100139#ifdef MACB_EXT_DESC
140 switch (bp->hw_dma_cap) {
141 case HW_DMA_CAP_64B:
142 case HW_DMA_CAP_PTP:
143 desc_idx <<= 1;
144 break;
145 case HW_DMA_CAP_64B_PTP:
146 desc_idx *= 3;
147 break;
148 default:
149 break;
150 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000151#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100152 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000153}
154
155#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
156static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
157{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100158 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
159 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
160 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000161}
162#endif
163
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000164/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500165static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000166{
Zach Brownb410d132016-10-19 09:56:57 -0500167 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000168}
169
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100170static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
171 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000172{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000173 index = macb_tx_ring_wrap(queue->bp, index);
174 index = macb_adj_dma_desc_idx(queue->bp, index);
175 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000176}
177
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100178static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
179 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000180{
Zach Brownb410d132016-10-19 09:56:57 -0500181 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000182}
183
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100184static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000185{
186 dma_addr_t offset;
187
Zach Brownb410d132016-10-19 09:56:57 -0500188 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000189 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000190
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100191 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000192}
193
Zach Brownb410d132016-10-19 09:56:57 -0500194static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195{
Zach Brownb410d132016-10-19 09:56:57 -0500196 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000197}
198
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000199static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000200{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000201 index = macb_rx_ring_wrap(queue->bp, index);
202 index = macb_adj_dma_desc_idx(queue->bp, index);
203 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000204}
205
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000206static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000207{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000208 return queue->rx_buffers + queue->bp->rx_buffer_size *
209 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000210}
211
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300212/* I/O accessors */
213static u32 hw_readl_native(struct macb *bp, int offset)
214{
215 return __raw_readl(bp->regs + offset);
216}
217
218static void hw_writel_native(struct macb *bp, int offset, u32 value)
219{
220 __raw_writel(value, bp->regs + offset);
221}
222
223static u32 hw_readl(struct macb *bp, int offset)
224{
225 return readl_relaxed(bp->regs + offset);
226}
227
228static void hw_writel(struct macb *bp, int offset, u32 value)
229{
230 writel_relaxed(value, bp->regs + offset);
231}
232
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700233/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700234 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300235 * descriptor access.
236 */
237static bool hw_is_native_io(void __iomem *addr)
238{
239 u32 value = MACB_BIT(LLB);
240
241 __raw_writel(value, addr + MACB_NCR);
242 value = __raw_readl(addr + MACB_NCR);
243
244 /* Write 0 back to disable everything */
245 __raw_writel(0, addr + MACB_NCR);
246
247 return value == MACB_BIT(LLB);
248}
249
250static bool hw_is_gem(void __iomem *addr, bool native_io)
251{
252 u32 id;
253
254 if (native_io)
255 id = __raw_readl(addr + MACB_MID);
256 else
257 id = readl_relaxed(addr + MACB_MID);
258
259 return MACB_BFEXT(IDNUM, id) >= 0x2;
260}
261
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100262static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100263{
264 u32 bottom;
265 u16 top;
266
267 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000268 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100269 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000270 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000271
272 /* Clear unused address register sets */
273 macb_or_gem_writel(bp, SA2B, 0);
274 macb_or_gem_writel(bp, SA2T, 0);
275 macb_or_gem_writel(bp, SA3B, 0);
276 macb_or_gem_writel(bp, SA3T, 0);
277 macb_or_gem_writel(bp, SA4B, 0);
278 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100279}
280
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100281static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000283 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284 u32 bottom;
285 u16 top;
286 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000287 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100288
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900289 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000290
Moritz Fischeraa50b552016-03-29 19:11:13 -0700291 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000292 for (i = 0; i < 4; i++) {
293 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
294 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100295
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000296 if (pdata && pdata->rev_eth_addr) {
297 addr[5] = bottom & 0xff;
298 addr[4] = (bottom >> 8) & 0xff;
299 addr[3] = (bottom >> 16) & 0xff;
300 addr[2] = (bottom >> 24) & 0xff;
301 addr[1] = top & 0xff;
302 addr[0] = (top & 0xff00) >> 8;
303 } else {
304 addr[0] = bottom & 0xff;
305 addr[1] = (bottom >> 8) & 0xff;
306 addr[2] = (bottom >> 16) & 0xff;
307 addr[3] = (bottom >> 24) & 0xff;
308 addr[4] = top & 0xff;
309 addr[5] = (top >> 8) & 0xff;
310 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100311
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000312 if (is_valid_ether_addr(addr)) {
313 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
314 return;
315 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700316 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000317
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300318 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000319 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100320}
321
frederic RODO6c36a702007-07-12 19:07:24 +0200322static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323{
frederic RODO6c36a702007-07-12 19:07:24 +0200324 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100325 int value;
326
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100327 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
328 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200329 | MACB_BF(PHYA, mii_id)
330 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100331 | MACB_BF(CODE, MACB_MAN_CODE)));
332
frederic RODO6c36a702007-07-12 19:07:24 +0200333 /* wait for end of transfer */
334 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
335 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336
337 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100338
339 return value;
340}
341
frederic RODO6c36a702007-07-12 19:07:24 +0200342static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
343 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344{
frederic RODO6c36a702007-07-12 19:07:24 +0200345 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100346
347 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
348 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200349 | MACB_BF(PHYA, mii_id)
350 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200352 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100353
frederic RODO6c36a702007-07-12 19:07:24 +0200354 /* wait for end of transfer */
355 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
356 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100357
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100358 return 0;
359}
360
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800361/**
362 * macb_set_tx_clk() - Set a clock to a new frequency
363 * @clk Pointer to the clock to change
364 * @rate New frequency in Hz
365 * @dev Pointer to the struct net_device
366 */
367static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
368{
369 long ferr, rate, rate_rounded;
370
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100371 if (!clk)
372 return;
373
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800374 switch (speed) {
375 case SPEED_10:
376 rate = 2500000;
377 break;
378 case SPEED_100:
379 rate = 25000000;
380 break;
381 case SPEED_1000:
382 rate = 125000000;
383 break;
384 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800385 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800386 }
387
388 rate_rounded = clk_round_rate(clk, rate);
389 if (rate_rounded < 0)
390 return;
391
392 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
393 * is not satisfied.
394 */
395 ferr = abs(rate_rounded - rate);
396 ferr = DIV_ROUND_UP(ferr, rate / 100000);
397 if (ferr > 5)
398 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700399 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800400
401 if (clk_set_rate(clk, rate_rounded))
402 netdev_err(dev, "adjusting tx_clk failed.\n");
403}
404
frederic RODO6c36a702007-07-12 19:07:24 +0200405static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100406{
frederic RODO6c36a702007-07-12 19:07:24 +0200407 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200408 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200409 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200410 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100413
frederic RODO6c36a702007-07-12 19:07:24 +0200414 if (phydev->link) {
415 if ((bp->speed != phydev->speed) ||
416 (bp->duplex != phydev->duplex)) {
417 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100418
frederic RODO6c36a702007-07-12 19:07:24 +0200419 reg = macb_readl(bp, NCFGR);
420 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000421 if (macb_is_gem(bp))
422 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200423
424 if (phydev->duplex)
425 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900426 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200427 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200428 if (phydev->speed == SPEED_1000 &&
429 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000430 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200431
Patrice Vilchez140b7552012-10-31 06:04:50 +0000432 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200433
434 bp->speed = phydev->speed;
435 bp->duplex = phydev->duplex;
436 status_change = 1;
437 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438 }
439
frederic RODO6c36a702007-07-12 19:07:24 +0200440 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700441 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200442 bp->speed = 0;
443 bp->duplex = -1;
444 }
445 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100446
frederic RODO6c36a702007-07-12 19:07:24 +0200447 status_change = 1;
448 }
449
450 spin_unlock_irqrestore(&bp->lock, flags);
451
452 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000453 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500454 /* Update the TX clock rate if and only if the link is
455 * up and there has been a link change.
456 */
457 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
458
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000459 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000460 netdev_info(dev, "link up (%d/%s)\n",
461 phydev->speed,
462 phydev->duplex == DUPLEX_FULL ?
463 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000464 } else {
465 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000466 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000467 }
frederic RODO6c36a702007-07-12 19:07:24 +0200468 }
469}
470
471/* based on au1000_eth. c*/
472static int macb_mii_probe(struct net_device *dev)
473{
474 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000475 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000476 struct phy_device *phydev;
Brad Mouring739de9a2018-03-13 16:32:13 -0500477 struct device_node *np;
478 int phy_irq, ret, i;
479
480 pdata = dev_get_platdata(&bp->pdev->dev);
481 np = bp->pdev->dev.of_node;
482 ret = 0;
483
484 if (np) {
485 if (of_phy_is_fixed_link(np)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500486 bp->phy_node = of_node_get(np);
487 } else {
Brad Mouring2105a5d2018-03-13 16:32:15 -0500488 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
489 /* fallback to standard phy registration if no
490 * phy-handle was found nor any phy found during
491 * dt phy registration
Brad Mouring739de9a2018-03-13 16:32:13 -0500492 */
Brad Mouring2105a5d2018-03-13 16:32:15 -0500493 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500494 for (i = 0; i < PHY_MAX_ADDR; i++) {
495 struct phy_device *phydev;
496
497 phydev = mdiobus_scan(bp->mii_bus, i);
498 if (IS_ERR(phydev) &&
499 PTR_ERR(phydev) != -ENODEV) {
500 ret = PTR_ERR(phydev);
501 break;
502 }
503 }
504
505 if (ret)
506 return -ENODEV;
507 }
508 }
509 }
frederic RODO6c36a702007-07-12 19:07:24 +0200510
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200511 if (bp->phy_node) {
512 phydev = of_phy_connect(dev, bp->phy_node,
513 &macb_handle_link_change, 0,
514 bp->phy_interface);
515 if (!phydev)
516 return -ENODEV;
517 } else {
518 phydev = phy_find_first(bp->mii_bus);
519 if (!phydev) {
520 netdev_err(dev, "no PHY found\n");
521 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000522 }
frederic RODO6c36a702007-07-12 19:07:24 +0200523
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200524 if (pdata) {
525 if (gpio_is_valid(pdata->phy_irq_pin)) {
526 ret = devm_gpio_request(&bp->pdev->dev,
527 pdata->phy_irq_pin, "phy int");
528 if (!ret) {
529 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
530 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
531 }
532 } else {
533 phydev->irq = PHY_POLL;
534 }
535 }
536
537 /* attach the mac to the phy */
538 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
539 bp->phy_interface);
540 if (ret) {
541 netdev_err(dev, "Could not attach to PHY\n");
542 return ret;
543 }
frederic RODO6c36a702007-07-12 19:07:24 +0200544 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100545
frederic RODO6c36a702007-07-12 19:07:24 +0200546 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200547 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Andrew Lunn58056c12018-09-12 01:53:11 +0200548 phy_set_max_speed(phydev, SPEED_1000);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000549 else
Andrew Lunn58056c12018-09-12 01:53:11 +0200550 phy_set_max_speed(phydev, SPEED_100);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100551
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500552 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
Andrew Lunn41124fa2018-09-12 01:53:14 +0200553 phy_remove_link_mode(phydev,
554 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100555
frederic RODO6c36a702007-07-12 19:07:24 +0200556 bp->link = 0;
557 bp->speed = 0;
558 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200559
560 return 0;
561}
562
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100563static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200564{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000565 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200566 struct device_node *np;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200567 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200568
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200569 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200570 macb_writel(bp, NCR, MACB_BIT(MPE));
571
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700572 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700573 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200574 err = -ENOMEM;
575 goto err_out;
576 }
577
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700578 bp->mii_bus->name = "MACB_mii_bus";
579 bp->mii_bus->read = &macb_mdio_read;
580 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000581 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700582 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700583 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700584 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900585 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700586
Jamie Iles91523942011-02-28 04:05:25 +0000587 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200588
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200589 np = bp->pdev->dev.of_node;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200590 if (np && of_phy_is_fixed_link(np)) {
591 if (of_phy_register_fixed_link(np) < 0) {
592 dev_err(&bp->pdev->dev,
593 "broken fixed-link specification %pOF\n", np);
594 goto err_out_free_mdiobus;
595 }
Brad Mouring739de9a2018-03-13 16:32:13 -0500596
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200597 err = mdiobus_register(bp->mii_bus);
598 } else {
599 if (pdata)
600 bp->mii_bus->phy_mask = pdata->phy_mask;
601
602 err = of_mdiobus_register(bp->mii_bus, np);
603 }
604
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200605 if (err)
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200606 goto err_out_free_fixed_link;
frederic RODO6c36a702007-07-12 19:07:24 +0200607
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200608 err = macb_mii_probe(bp->dev);
609 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200610 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200611
612 return 0;
613
614err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700615 mdiobus_unregister(bp->mii_bus);
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200616err_out_free_fixed_link:
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100617 if (np && of_phy_is_fixed_link(np))
618 of_phy_deregister_fixed_link(np);
Brad Mouring739de9a2018-03-13 16:32:13 -0500619err_out_free_mdiobus:
620 of_node_put(bp->phy_node);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700621 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200622err_out:
623 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100624}
625
626static void macb_update_stats(struct macb *bp)
627{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000628 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
629 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300630 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100631
632 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
633
Moritz Fischer96ec6312016-03-29 19:11:11 -0700634 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700635 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100636}
637
Nicolas Ferree86cd532012-10-31 06:04:57 +0000638static int macb_halt_tx(struct macb *bp)
639{
640 unsigned long halt_time, timeout;
641 u32 status;
642
643 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
644
645 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
646 do {
647 halt_time = jiffies;
648 status = macb_readl(bp, TSR);
649 if (!(status & MACB_BIT(TGO)))
650 return 0;
651
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800652 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000653 } while (time_before(halt_time, timeout));
654
655 return -ETIMEDOUT;
656}
657
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200658static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
659{
660 if (tx_skb->mapping) {
661 if (tx_skb->mapped_as_page)
662 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
663 tx_skb->size, DMA_TO_DEVICE);
664 else
665 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
666 tx_skb->size, DMA_TO_DEVICE);
667 tx_skb->mapping = 0;
668 }
669
670 if (tx_skb->skb) {
671 dev_kfree_skb_any(tx_skb->skb);
672 tx_skb->skb = NULL;
673 }
674}
675
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000676static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530677{
Harini Katakamfff80192016-08-09 13:15:53 +0530678#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000679 struct macb_dma_desc_64 *desc_64;
680
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100681 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000682 desc_64 = macb_64b_desc(bp, desc);
683 desc_64->addrh = upper_32_bits(addr);
684 }
Harini Katakamfff80192016-08-09 13:15:53 +0530685#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000686 desc->addr = lower_32_bits(addr);
687}
688
689static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
690{
691 dma_addr_t addr = 0;
692#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
693 struct macb_dma_desc_64 *desc_64;
694
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100695 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000696 desc_64 = macb_64b_desc(bp, desc);
697 addr = ((u64)(desc_64->addrh) << 32);
698 }
699#endif
700 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
701 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530702}
703
Nicolas Ferree86cd532012-10-31 06:04:57 +0000704static void macb_tx_error_task(struct work_struct *work)
705{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100706 struct macb_queue *queue = container_of(work, struct macb_queue,
707 tx_error_task);
708 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000709 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100710 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000711 struct sk_buff *skb;
712 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100713 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000714
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100715 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
716 (unsigned int)(queue - bp->queues),
717 queue->tx_tail, queue->tx_head);
718
719 /* Prevent the queue IRQ handlers from running: each of them may call
720 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
721 * As explained below, we have to halt the transmission before updating
722 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
723 * network engine about the macb/gem being halted.
724 */
725 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000726
727 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100728 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000729
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700730 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000731 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100732 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000733 */
734 if (macb_halt_tx(bp))
735 /* Just complain for now, reinitializing TX path can be good */
736 netdev_err(bp->dev, "BUG: halt tx timed out\n");
737
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700738 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000739 * Free transmit buffers in upper layer.
740 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100741 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
742 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000743
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100744 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000745 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100746 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000747 skb = tx_skb->skb;
748
749 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200750 /* skb is set for the last buffer of the frame */
751 while (!skb) {
752 macb_tx_unmap(bp, tx_skb);
753 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100754 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200755 skb = tx_skb->skb;
756 }
757
758 /* ctrl still refers to the first buffer descriptor
759 * since it's the only one written back by the hardware
760 */
761 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
762 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500763 macb_tx_ring_wrap(bp, tail),
764 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200765 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000766 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200767 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000768 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200769 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000770 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700771 /* "Buffers exhausted mid-frame" errors may only happen
772 * if the driver is buggy, so complain loudly about
773 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000774 */
775 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
776 netdev_err(bp->dev,
777 "BUG: TX buffers exhausted mid-frame\n");
778
779 desc->ctrl = ctrl | MACB_BIT(TX_USED);
780 }
781
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200782 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000783 }
784
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100785 /* Set end of TX queue */
786 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000787 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100788 desc->ctrl = MACB_BIT(TX_USED);
789
Nicolas Ferree86cd532012-10-31 06:04:57 +0000790 /* Make descriptor updates visible to hardware */
791 wmb();
792
793 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000794 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530795#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100796 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000797 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530798#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000799 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100800 queue->tx_head = 0;
801 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000802
803 /* Housework before enabling TX IRQ */
804 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100805 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
806
807 /* Now we are ready to start transmission again */
808 netif_tx_start_all_queues(bp->dev);
809 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
810
811 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000812}
813
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100814static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100815{
816 unsigned int tail;
817 unsigned int head;
818 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100819 struct macb *bp = queue->bp;
820 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821
822 status = macb_readl(bp, TSR);
823 macb_writel(bp, TSR, status);
824
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000825 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100826 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000827
Nicolas Ferree86cd532012-10-31 06:04:57 +0000828 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700829 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100830
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100831 head = queue->tx_head;
832 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000833 struct macb_tx_skb *tx_skb;
834 struct sk_buff *skb;
835 struct macb_dma_desc *desc;
836 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100837
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100838 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100839
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000840 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100841 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000842
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000843 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100844
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200845 /* TX_USED bit is only set by hardware on the very first buffer
846 * descriptor of the transmitted frame.
847 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000848 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100849 break;
850
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200851 /* Process all buffers of the current transmitted frame */
852 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100853 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200854 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000855
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200856 /* First, update TX stats if needed */
857 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100858 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
859 /* skb now belongs to timestamp buffer
860 * and will be removed later
861 */
862 tx_skb->skb = NULL;
863 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200864 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500865 macb_tx_ring_wrap(bp, tail),
866 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200867 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000868 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200869 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000870 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200871 }
872
873 /* Now we can safely release resources */
874 macb_tx_unmap(bp, tx_skb);
875
876 /* skb is set only for the last buffer of the frame.
877 * WARNING: at this point skb has been freed by
878 * macb_tx_unmap().
879 */
880 if (skb)
881 break;
882 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100883 }
884
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100885 queue->tx_tail = tail;
886 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
887 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500888 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100889 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100890}
891
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000892static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000893{
894 unsigned int entry;
895 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000896 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000897 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000898 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000899
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000900 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
901 bp->rx_ring_size) > 0) {
902 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000903
904 /* Make hw descriptor updates visible to CPU */
905 rmb();
906
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000907 queue->rx_prepared_head++;
908 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000909
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000910 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000911 /* allocate sk_buff for this free entry in ring */
912 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700913 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000914 netdev_err(bp->dev,
915 "Unable to allocate sk_buff\n");
916 break;
917 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000918
919 /* now fill corresponding descriptor entry */
920 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700921 bp->rx_buffer_size,
922 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800923 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
924 dev_kfree_skb(skb);
925 break;
926 }
927
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000928 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000929
Zach Brownb410d132016-10-19 09:56:57 -0500930 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000931 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000932 macb_set_addr(bp, desc, paddr);
933 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000934
935 /* properly align Ethernet header */
936 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530937 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000938 desc->addr &= ~MACB_BIT(RX_USED);
939 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000940 }
941 }
942
943 /* Make descriptor updates visible to hardware */
944 wmb();
945
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000946 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
947 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000948}
949
950/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000951static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000952 unsigned int end)
953{
954 unsigned int frag;
955
956 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000957 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700958
Nicolas Ferre4df95132013-06-04 21:57:12 +0000959 desc->addr &= ~MACB_BIT(RX_USED);
960 }
961
962 /* Make descriptor updates visible to hardware */
963 wmb();
964
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700965 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000966 * whatever caused this is updated, so we don't have to record
967 * anything.
968 */
969}
970
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000971static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000972{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000973 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000974 unsigned int len;
975 unsigned int entry;
976 struct sk_buff *skb;
977 struct macb_dma_desc *desc;
978 int count = 0;
979
980 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530981 u32 ctrl;
982 dma_addr_t addr;
983 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000984
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000985 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
986 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000987
988 /* Make hw descriptor updates visible to CPU */
989 rmb();
990
Harini Katakamfff80192016-08-09 13:15:53 +0530991 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000992 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000993 ctrl = desc->ctrl;
994
Harini Katakamfff80192016-08-09 13:15:53 +0530995 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000996 break;
997
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000998 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000999 count++;
1000
1001 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1002 netdev_err(bp->dev,
1003 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001004 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001005 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001006 break;
1007 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001008 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001009 if (unlikely(!skb)) {
1010 netdev_err(bp->dev,
1011 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001012 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001013 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001014 break;
1015 }
1016 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001017 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301018 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001019
1020 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1021
1022 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001023 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001024 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001025
1026 skb->protocol = eth_type_trans(skb, bp->dev);
1027 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001028 if (bp->dev->features & NETIF_F_RXCSUM &&
1029 !(bp->dev->flags & IFF_PROMISC) &&
1030 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1031 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001032
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001033 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001034 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001035 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001036 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001037
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001038 gem_ptp_do_rxstamp(bp, skb, desc);
1039
Nicolas Ferre4df95132013-06-04 21:57:12 +00001040#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1041 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1042 skb->len, skb->csum);
1043 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001044 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001045 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1046 skb->data, 32, true);
1047#endif
1048
1049 netif_receive_skb(skb);
1050 }
1051
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001052 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001053
1054 return count;
1055}
1056
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001057static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 unsigned int last_frag)
1059{
1060 unsigned int len;
1061 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001062 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001063 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001064 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001065 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001066
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001067 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301068 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001070 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001071 macb_rx_ring_wrap(bp, first_frag),
1072 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001073
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001074 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001075 * first buffer. Since the header is 14 bytes, this makes the
1076 * payload word-aligned.
1077 *
1078 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1079 * the two padding bytes into the skb so that we avoid hitting
1080 * the slowpath in memcpy(), and pull them off afterwards.
1081 */
1082 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001084 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001085 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001086 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001087 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001088 if (frag == last_frag)
1089 break;
1090 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001091
1092 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001094
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001095 return 1;
1096 }
1097
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001098 offset = 0;
1099 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001100 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001101 skb_put(skb, len);
1102
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001103 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001104 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001105
1106 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001107 if (unlikely(frag != last_frag)) {
1108 dev_kfree_skb_any(skb);
1109 return -1;
1110 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001111 frag_len = len - offset;
1112 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001113 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001114 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001115 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001116 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001117 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001118 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001119
1120 if (frag == last_frag)
1121 break;
1122 }
1123
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001124 /* Make descriptor updates visible to hardware */
1125 wmb();
1126
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001127 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001128 skb->protocol = eth_type_trans(skb, bp->dev);
1129
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001130 bp->dev->stats.rx_packets++;
1131 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001132 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001133 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001134 netif_receive_skb(skb);
1135
1136 return 0;
1137}
1138
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001139static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001140{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001141 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001142 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001143 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001144 int i;
1145
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001146 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001147 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001148 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001149 macb_set_addr(bp, desc, addr);
1150 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001151 addr += bp->rx_buffer_size;
1152 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001153 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001154 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001155}
1156
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001157static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001158{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001159 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001160 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001161 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001162 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163 int first_frag = -1;
1164
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001165 for (tail = queue->rx_tail; budget > 0; tail++) {
1166 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001167 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001169 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001170 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001171
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001172 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001173
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001174 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001175 break;
1176
1177 if (ctrl & MACB_BIT(RX_SOF)) {
1178 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001179 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001180 first_frag = tail;
1181 }
1182
1183 if (ctrl & MACB_BIT(RX_EOF)) {
1184 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001185
1186 if (unlikely(first_frag == -1)) {
1187 reset_rx_queue = true;
1188 continue;
1189 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001190
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001191 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001192 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001193 if (unlikely(dropped < 0)) {
1194 reset_rx_queue = true;
1195 continue;
1196 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001197 if (!dropped) {
1198 received++;
1199 budget--;
1200 }
1201 }
1202 }
1203
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001204 if (unlikely(reset_rx_queue)) {
1205 unsigned long flags;
1206 u32 ctrl;
1207
1208 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1209
1210 spin_lock_irqsave(&bp->lock, flags);
1211
1212 ctrl = macb_readl(bp, NCR);
1213 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1214
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001215 macb_init_rx_ring(queue);
1216 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001217
1218 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1219
1220 spin_unlock_irqrestore(&bp->lock, flags);
1221 return received;
1222 }
1223
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001224 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001225 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001226 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001227 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001228
1229 return received;
1230}
1231
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001232static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001234 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1235 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001236 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001237 u32 status;
1238
1239 status = macb_readl(bp, RSR);
1240 macb_writel(bp, RSR, status);
1241
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001242 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001243 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001244
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001245 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001246 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001247 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001248
Nicolas Ferre8770e912013-02-12 11:08:48 +01001249 /* Packets received while interrupts were disabled */
1250 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001251 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001252 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001253 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001254 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001255 } else {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001256 queue_writel(queue, IER, MACB_RX_INT_FLAGS);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001257 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001258 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001259
1260 /* TODO: Handle errors */
1261
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001262 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001263}
1264
Harini Katakam032dc412018-01-27 12:09:01 +05301265static void macb_hresp_error_task(unsigned long data)
1266{
1267 struct macb *bp = (struct macb *)data;
1268 struct net_device *dev = bp->dev;
1269 struct macb_queue *queue = bp->queues;
1270 unsigned int q;
1271 u32 ctrl;
1272
1273 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1274 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
1275 MACB_TX_INT_FLAGS |
1276 MACB_BIT(HRESP));
1277 }
1278 ctrl = macb_readl(bp, NCR);
1279 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1280 macb_writel(bp, NCR, ctrl);
1281
1282 netif_tx_stop_all_queues(dev);
1283 netif_carrier_off(dev);
1284
1285 bp->macbgem_ops.mog_init_rings(bp);
1286
1287 /* Initialize TX and RX buffers */
1288 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1289 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1290#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1291 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1292 queue_writel(queue, RBQPH,
1293 upper_32_bits(queue->rx_ring_dma));
1294#endif
1295 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1296#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1297 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1298 queue_writel(queue, TBQPH,
1299 upper_32_bits(queue->tx_ring_dma));
1300#endif
1301
1302 /* Enable interrupts */
1303 queue_writel(queue, IER,
1304 MACB_RX_INT_FLAGS |
1305 MACB_TX_INT_FLAGS |
1306 MACB_BIT(HRESP));
1307 }
1308
1309 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1310 macb_writel(bp, NCR, ctrl);
1311
1312 netif_carrier_on(dev);
1313 netif_tx_start_all_queues(dev);
1314}
1315
Claudiu Beznea42983882018-12-17 10:02:42 +00001316static void macb_tx_restart(struct macb_queue *queue)
1317{
1318 unsigned int head = queue->tx_head;
1319 unsigned int tail = queue->tx_tail;
1320 struct macb *bp = queue->bp;
1321
1322 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1323 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1324
1325 if (head == tail)
1326 return;
1327
1328 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1329}
1330
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001331static irqreturn_t macb_interrupt(int irq, void *dev_id)
1332{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001333 struct macb_queue *queue = dev_id;
1334 struct macb *bp = queue->bp;
1335 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001336 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001337
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001338 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001339
1340 if (unlikely(!status))
1341 return IRQ_NONE;
1342
1343 spin_lock(&bp->lock);
1344
1345 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001346 /* close possible race with dev_close */
1347 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001348 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001349 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1350 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001351 break;
1352 }
1353
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001354 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1355 (unsigned int)(queue - bp->queues),
1356 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001357
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001358 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001359 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001360 * until we have processed the buffers. The
1361 * scheduling call may fail if the poll routine
1362 * is already scheduled, so disable interrupts
1363 * now.
1364 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001365 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001366 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001367 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001368
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001369 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001370 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001371 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001372 }
1373 }
1374
Nicolas Ferree86cd532012-10-31 06:04:57 +00001375 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001376 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1377 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001378
1379 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001380 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001381
Nicolas Ferree86cd532012-10-31 06:04:57 +00001382 break;
1383 }
1384
1385 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001386 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001387
Claudiu Beznea42983882018-12-17 10:02:42 +00001388 if (status & MACB_BIT(TXUBR))
1389 macb_tx_restart(queue);
1390
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001391 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001392 * add that if/when we get our hands on a full-blown MII PHY.
1393 */
1394
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001395 /* There is a hardware issue under heavy load where DMA can
1396 * stop, this causes endless "used buffer descriptor read"
1397 * interrupts but it can be cleared by re-enabling RX. See
1398 * the at91 manual, section 41.3.1 or the Zynq manual
1399 * section 16.7.4 for details.
1400 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001401 if (status & MACB_BIT(RXUBR)) {
1402 ctrl = macb_readl(bp, NCR);
1403 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001404 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001405 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1406
1407 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001408 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001409 }
1410
Alexander Steinb19f7f72011-04-13 05:03:24 +00001411 if (status & MACB_BIT(ISR_ROVR)) {
1412 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001413 if (macb_is_gem(bp))
1414 bp->hw_stats.gem.rx_overruns++;
1415 else
1416 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001417
1418 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001419 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001420 }
1421
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001422 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301423 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001424 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001425
1426 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001427 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001428 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001429 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001430 }
1431
1432 spin_unlock(&bp->lock);
1433
1434 return IRQ_HANDLED;
1435}
1436
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001437#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001438/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001439 * to allow network i/o with interrupts disabled.
1440 */
1441static void macb_poll_controller(struct net_device *dev)
1442{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001443 struct macb *bp = netdev_priv(dev);
1444 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001445 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001446 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001447
1448 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001449 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1450 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001451 local_irq_restore(flags);
1452}
1453#endif
1454
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001455static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001456 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001457 struct sk_buff *skb,
1458 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001459{
1460 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001461 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001462 struct macb_tx_skb *tx_skb = NULL;
1463 struct macb_dma_desc *desc;
1464 unsigned int offset, size, count = 0;
1465 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001466 unsigned int eof = 1, mss_mfs = 0;
1467 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1468
1469 /* LSO */
1470 if (skb_shinfo(skb)->gso_size != 0) {
1471 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1472 /* UDP - UFO */
1473 lso_ctrl = MACB_LSO_UFO_ENABLE;
1474 else
1475 /* TCP - TSO */
1476 lso_ctrl = MACB_LSO_TSO_ENABLE;
1477 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001478
1479 /* First, map non-paged data */
1480 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001481
1482 /* first buffer length */
1483 size = hdrlen;
1484
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001485 offset = 0;
1486 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001487 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001488 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001489
1490 mapping = dma_map_single(&bp->pdev->dev,
1491 skb->data + offset,
1492 size, DMA_TO_DEVICE);
1493 if (dma_mapping_error(&bp->pdev->dev, mapping))
1494 goto dma_error;
1495
1496 /* Save info to properly release resources */
1497 tx_skb->skb = NULL;
1498 tx_skb->mapping = mapping;
1499 tx_skb->size = size;
1500 tx_skb->mapped_as_page = false;
1501
1502 len -= size;
1503 offset += size;
1504 count++;
1505 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001506
1507 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001508 }
1509
1510 /* Then, map paged data from fragments */
1511 for (f = 0; f < nr_frags; f++) {
1512 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1513
1514 len = skb_frag_size(frag);
1515 offset = 0;
1516 while (len) {
1517 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001518 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001519 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001520
1521 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1522 offset, size, DMA_TO_DEVICE);
1523 if (dma_mapping_error(&bp->pdev->dev, mapping))
1524 goto dma_error;
1525
1526 /* Save info to properly release resources */
1527 tx_skb->skb = NULL;
1528 tx_skb->mapping = mapping;
1529 tx_skb->size = size;
1530 tx_skb->mapped_as_page = true;
1531
1532 len -= size;
1533 offset += size;
1534 count++;
1535 tx_head++;
1536 }
1537 }
1538
1539 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001540 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001541 netdev_err(bp->dev, "BUG! empty skb!\n");
1542 return 0;
1543 }
1544
1545 /* This is the last buffer of the frame: save socket buffer */
1546 tx_skb->skb = skb;
1547
1548 /* Update TX ring: update buffer descriptors in reverse order
1549 * to avoid race condition
1550 */
1551
1552 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1553 * to set the end of TX queue
1554 */
1555 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001556 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001557 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001558 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001559 desc->ctrl = ctrl;
1560
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001561 if (lso_ctrl) {
1562 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1563 /* include header and FCS in value given to h/w */
1564 mss_mfs = skb_shinfo(skb)->gso_size +
1565 skb_transport_offset(skb) +
1566 ETH_FCS_LEN;
1567 else /* TSO */ {
1568 mss_mfs = skb_shinfo(skb)->gso_size;
1569 /* TCP Sequence Number Source Select
1570 * can be set only for TSO
1571 */
1572 seq_ctrl = 0;
1573 }
1574 }
1575
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001576 do {
1577 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001578 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001579 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001580 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001581
1582 ctrl = (u32)tx_skb->size;
1583 if (eof) {
1584 ctrl |= MACB_BIT(TX_LAST);
1585 eof = 0;
1586 }
Zach Brownb410d132016-10-19 09:56:57 -05001587 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001588 ctrl |= MACB_BIT(TX_WRAP);
1589
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001590 /* First descriptor is header descriptor */
1591 if (i == queue->tx_head) {
1592 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1593 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001594 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1595 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1596 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001597 } else
1598 /* Only set MSS/MFS on payload descriptors
1599 * (second or later descriptor)
1600 */
1601 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1602
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001603 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001604 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001605 /* desc->addr must be visible to hardware before clearing
1606 * 'TX_USED' bit in desc->ctrl.
1607 */
1608 wmb();
1609 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001610 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001611
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001612 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001613
1614 return count;
1615
1616dma_error:
1617 netdev_err(bp->dev, "TX DMA map failed\n");
1618
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001619 for (i = queue->tx_head; i != tx_head; i++) {
1620 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001621
1622 macb_tx_unmap(bp, tx_skb);
1623 }
1624
1625 return 0;
1626}
1627
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001628static netdev_features_t macb_features_check(struct sk_buff *skb,
1629 struct net_device *dev,
1630 netdev_features_t features)
1631{
1632 unsigned int nr_frags, f;
1633 unsigned int hdrlen;
1634
1635 /* Validate LSO compatibility */
1636
1637 /* there is only one buffer */
1638 if (!skb_is_nonlinear(skb))
1639 return features;
1640
1641 /* length of header */
1642 hdrlen = skb_transport_offset(skb);
1643 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1644 hdrlen += tcp_hdrlen(skb);
1645
1646 /* For LSO:
1647 * When software supplies two or more payload buffers all payload buffers
1648 * apart from the last must be a multiple of 8 bytes in size.
1649 */
1650 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1651 return features & ~MACB_NETIF_LSO;
1652
1653 nr_frags = skb_shinfo(skb)->nr_frags;
1654 /* No need to check last fragment */
1655 nr_frags--;
1656 for (f = 0; f < nr_frags; f++) {
1657 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1658
1659 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1660 return features & ~MACB_NETIF_LSO;
1661 }
1662 return features;
1663}
1664
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001665static inline int macb_clear_csum(struct sk_buff *skb)
1666{
1667 /* no change for packets without checksum offloading */
1668 if (skb->ip_summed != CHECKSUM_PARTIAL)
1669 return 0;
1670
1671 /* make sure we can modify the header */
1672 if (unlikely(skb_cow_head(skb, 0)))
1673 return -1;
1674
1675 /* initialize checksum field
1676 * This is required - at least for Zynq, which otherwise calculates
1677 * wrong UDP header checksums for UDP packets with UDP data len <=2
1678 */
1679 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1680 return 0;
1681}
1682
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001683static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1684{
1685 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1686 int padlen = ETH_ZLEN - (*skb)->len;
1687 int headroom = skb_headroom(*skb);
1688 int tailroom = skb_tailroom(*skb);
1689 struct sk_buff *nskb;
1690 u32 fcs;
1691
1692 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1693 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1694 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1695 return 0;
1696
1697 if (padlen <= 0) {
1698 /* FCS could be appeded to tailroom. */
1699 if (tailroom >= ETH_FCS_LEN)
1700 goto add_fcs;
1701 /* FCS could be appeded by moving data to headroom. */
1702 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1703 padlen = 0;
1704 /* No room for FCS, need to reallocate skb. */
1705 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001706 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001707 } else {
1708 /* Add room for FCS. */
1709 padlen += ETH_FCS_LEN;
1710 }
1711
1712 if (!cloned && headroom + tailroom >= padlen) {
1713 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1714 skb_set_tail_pointer(*skb, (*skb)->len);
1715 } else {
1716 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1717 if (!nskb)
1718 return -ENOMEM;
1719
1720 dev_kfree_skb_any(*skb);
1721 *skb = nskb;
1722 }
1723
1724 if (padlen) {
1725 if (padlen >= ETH_FCS_LEN)
1726 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
1727 else
1728 skb_trim(*skb, ETH_FCS_LEN - padlen);
1729 }
1730
1731add_fcs:
1732 /* set FCS to packet */
1733 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1734 fcs = ~fcs;
1735
1736 skb_put_u8(*skb, fcs & 0xff);
1737 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1738 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1739 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1740
1741 return 0;
1742}
1743
Claudiu Beznead1c38952018-08-07 12:25:12 +03001744static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001746 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001747 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001748 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001749 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001750 unsigned int desc_cnt, nr_frags, frag_size, f;
1751 unsigned int hdrlen;
1752 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001753 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001754
Claudiu Beznea33729f22018-08-07 12:25:13 +03001755 if (macb_clear_csum(skb)) {
1756 dev_kfree_skb_any(skb);
1757 return ret;
1758 }
1759
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001760 if (macb_pad_and_fcs(&skb, dev)) {
1761 dev_kfree_skb_any(skb);
1762 return ret;
1763 }
1764
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001765 is_lso = (skb_shinfo(skb)->gso_size != 0);
1766
1767 if (is_lso) {
1768 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1769
1770 /* length of headers */
1771 if (is_udp)
1772 /* only queue eth + ip headers separately for UDP */
1773 hdrlen = skb_transport_offset(skb);
1774 else
1775 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1776 if (skb_headlen(skb) < hdrlen) {
1777 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1778 /* if this is required, would need to copy to single buffer */
1779 return NETDEV_TX_BUSY;
1780 }
1781 } else
1782 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001783
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001784#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1785 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001786 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1787 queue_index, skb->len, skb->head, skb->data,
1788 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001789 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1790 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001791#endif
1792
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001793 /* Count how many TX buffer descriptors are needed to send this
1794 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001795 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001796 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001797 if (is_lso && (skb_headlen(skb) > hdrlen))
1798 /* extra header descriptor if also payload in first buffer */
1799 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1800 else
1801 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001802 nr_frags = skb_shinfo(skb)->nr_frags;
1803 for (f = 0; f < nr_frags; f++) {
1804 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001805 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001806 }
1807
Dongdong Deng48719532009-08-23 19:49:07 -07001808 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001809
1810 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001811 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001812 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001813 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001814 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001815 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001816 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001817 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001818 }
1819
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001820 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001821 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001822 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001823 goto unlock;
1824 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001825
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001826 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001827 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001828 skb_tx_timestamp(skb);
1829
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001830 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1831
Zach Brownb410d132016-10-19 09:56:57 -05001832 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001833 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834
Soren Brinkmann92030902014-03-04 08:46:39 -08001835unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001836 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001837
Claudiu Beznead1c38952018-08-07 12:25:12 +03001838 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001839}
1840
Nicolas Ferre4df95132013-06-04 21:57:12 +00001841static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001842{
1843 if (!macb_is_gem(bp)) {
1844 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1845 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001846 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001847
Nicolas Ferre1b447912013-06-04 21:57:11 +00001848 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001849 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001850 "RX buffer must be multiple of %d bytes, expanding\n",
1851 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001852 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001853 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001854 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001855 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001856
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001857 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001858 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001859}
1860
Nicolas Ferre4df95132013-06-04 21:57:12 +00001861static void gem_free_rx_buffers(struct macb *bp)
1862{
1863 struct sk_buff *skb;
1864 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001865 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001866 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001867 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001868 int i;
1869
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001870 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1871 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001872 continue;
1873
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001874 for (i = 0; i < bp->rx_ring_size; i++) {
1875 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001876
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001877 if (!skb)
1878 continue;
1879
1880 desc = macb_rx_desc(queue, i);
1881 addr = macb_get_addr(bp, desc);
1882
1883 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1884 DMA_FROM_DEVICE);
1885 dev_kfree_skb_any(skb);
1886 skb = NULL;
1887 }
1888
1889 kfree(queue->rx_skbuff);
1890 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001891 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001892}
1893
1894static void macb_free_rx_buffers(struct macb *bp)
1895{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001896 struct macb_queue *queue = &bp->queues[0];
1897
1898 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001899 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001900 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001901 queue->rx_buffers, queue->rx_buffers_dma);
1902 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001903 }
1904}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001905
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001906static void macb_free_consistent(struct macb *bp)
1907{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001908 struct macb_queue *queue;
1909 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301910 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001911
Nicolas Ferre4df95132013-06-04 21:57:12 +00001912 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001913
1914 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1915 kfree(queue->tx_skb);
1916 queue->tx_skb = NULL;
1917 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301918 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1919 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001920 queue->tx_ring, queue->tx_ring_dma);
1921 queue->tx_ring = NULL;
1922 }
Harini Katakame50b7702018-07-06 12:18:57 +05301923 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301924 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1925 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301926 queue->rx_ring, queue->rx_ring_dma);
1927 queue->rx_ring = NULL;
1928 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001929 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001930}
1931
1932static int gem_alloc_rx_buffers(struct macb *bp)
1933{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001934 struct macb_queue *queue;
1935 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001936 int size;
1937
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001938 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1939 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1940 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1941 if (!queue->rx_skbuff)
1942 return -ENOMEM;
1943 else
1944 netdev_dbg(bp->dev,
1945 "Allocated %d RX struct sk_buff entries at %p\n",
1946 bp->rx_ring_size, queue->rx_skbuff);
1947 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001948 return 0;
1949}
1950
1951static int macb_alloc_rx_buffers(struct macb *bp)
1952{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001953 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001954 int size;
1955
Zach Brownb410d132016-10-19 09:56:57 -05001956 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001957 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1958 &queue->rx_buffers_dma, GFP_KERNEL);
1959 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001960 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001961
1962 netdev_dbg(bp->dev,
1963 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001964 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001965 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001966}
1967
1968static int macb_alloc_consistent(struct macb *bp)
1969{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001970 struct macb_queue *queue;
1971 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001972 int size;
1973
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001974 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05301975 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001976 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1977 &queue->tx_ring_dma,
1978 GFP_KERNEL);
1979 if (!queue->tx_ring)
1980 goto out_err;
1981 netdev_dbg(bp->dev,
1982 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1983 q, size, (unsigned long)queue->tx_ring_dma,
1984 queue->tx_ring);
1985
Zach Brownb410d132016-10-19 09:56:57 -05001986 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001987 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1988 if (!queue->tx_skb)
1989 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001990
Harini Katakam404cd082018-07-06 12:18:58 +05301991 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001992 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1993 &queue->rx_ring_dma, GFP_KERNEL);
1994 if (!queue->rx_ring)
1995 goto out_err;
1996 netdev_dbg(bp->dev,
1997 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1998 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001999 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002000 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002001 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002002
2003 return 0;
2004
2005out_err:
2006 macb_free_consistent(bp);
2007 return -ENOMEM;
2008}
2009
Nicolas Ferre4df95132013-06-04 21:57:12 +00002010static void gem_init_rings(struct macb *bp)
2011{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002012 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002013 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002014 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002015 int i;
2016
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002017 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002018 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002019 desc = macb_tx_desc(queue, i);
2020 macb_set_addr(bp, desc, 0);
2021 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002022 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002023 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002024 queue->tx_head = 0;
2025 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002026
2027 queue->rx_tail = 0;
2028 queue->rx_prepared_head = 0;
2029
2030 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002031 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002032
Nicolas Ferre4df95132013-06-04 21:57:12 +00002033}
2034
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002035static void macb_init_rings(struct macb *bp)
2036{
2037 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002038 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002039
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002040 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002041
Zach Brownb410d132016-10-19 09:56:57 -05002042 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002043 desc = macb_tx_desc(&bp->queues[0], i);
2044 macb_set_addr(bp, desc, 0);
2045 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002046 }
Ben Shelton21d35152015-04-22 17:28:54 -05002047 bp->queues[0].tx_head = 0;
2048 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002049 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002050}
2051
2052static void macb_reset_hw(struct macb *bp)
2053{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002054 struct macb_queue *queue;
2055 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002056 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002057
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002058 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002059 * more gracefully?)
2060 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002061 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002062
2063 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002064 ctrl |= MACB_BIT(CLRSTAT);
2065
2066 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002067
2068 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002069 macb_writel(bp, TSR, -1);
2070 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002071
2072 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002073 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2074 queue_writel(queue, IDR, -1);
2075 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002076 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2077 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002078 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002079}
2080
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002081static u32 gem_mdc_clk_div(struct macb *bp)
2082{
2083 u32 config;
2084 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2085
2086 if (pclk_hz <= 20000000)
2087 config = GEM_BF(CLK, GEM_CLK_DIV8);
2088 else if (pclk_hz <= 40000000)
2089 config = GEM_BF(CLK, GEM_CLK_DIV16);
2090 else if (pclk_hz <= 80000000)
2091 config = GEM_BF(CLK, GEM_CLK_DIV32);
2092 else if (pclk_hz <= 120000000)
2093 config = GEM_BF(CLK, GEM_CLK_DIV48);
2094 else if (pclk_hz <= 160000000)
2095 config = GEM_BF(CLK, GEM_CLK_DIV64);
2096 else
2097 config = GEM_BF(CLK, GEM_CLK_DIV96);
2098
2099 return config;
2100}
2101
2102static u32 macb_mdc_clk_div(struct macb *bp)
2103{
2104 u32 config;
2105 unsigned long pclk_hz;
2106
2107 if (macb_is_gem(bp))
2108 return gem_mdc_clk_div(bp);
2109
2110 pclk_hz = clk_get_rate(bp->pclk);
2111 if (pclk_hz <= 20000000)
2112 config = MACB_BF(CLK, MACB_CLK_DIV8);
2113 else if (pclk_hz <= 40000000)
2114 config = MACB_BF(CLK, MACB_CLK_DIV16);
2115 else if (pclk_hz <= 80000000)
2116 config = MACB_BF(CLK, MACB_CLK_DIV32);
2117 else
2118 config = MACB_BF(CLK, MACB_CLK_DIV64);
2119
2120 return config;
2121}
2122
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002123/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002124 * should program. We find the width from decoding the design configuration
2125 * register to find the maximum supported data bus width.
2126 */
2127static u32 macb_dbw(struct macb *bp)
2128{
2129 if (!macb_is_gem(bp))
2130 return 0;
2131
2132 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2133 case 4:
2134 return GEM_BF(DBW, GEM_DBW128);
2135 case 2:
2136 return GEM_BF(DBW, GEM_DBW64);
2137 case 1:
2138 default:
2139 return GEM_BF(DBW, GEM_DBW32);
2140 }
2141}
2142
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002143/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002144 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002145 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002146 * (if not supported by FIFO, it will fallback to default)
2147 * - set both rx/tx packet buffers to full memory size
2148 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002149 */
2150static void macb_configure_dma(struct macb *bp)
2151{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002152 struct macb_queue *queue;
2153 u32 buffer_size;
2154 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002155 u32 dmacfg;
2156
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002157 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002158 if (macb_is_gem(bp)) {
2159 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002160 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2161 if (q)
2162 queue_writel(queue, RBQS, buffer_size);
2163 else
2164 dmacfg |= GEM_BF(RXBS, buffer_size);
2165 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002166 if (bp->dma_burst_length)
2167 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002168 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302169 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302170
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002171 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302172 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2173 else
2174 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2175
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002176 if (bp->dev->features & NETIF_F_HW_CSUM)
2177 dmacfg |= GEM_BIT(TXCOEN);
2178 else
2179 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302180
Michal Simekbd620722018-09-25 08:32:50 +02002181 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302182#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002183 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002184 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302185#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002186#ifdef CONFIG_MACB_USE_HWSTAMP
2187 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2188 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2189#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002190 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2191 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002192 gem_writel(bp, DMACFG, dmacfg);
2193 }
2194}
2195
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002196static void macb_init_hw(struct macb *bp)
2197{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002198 struct macb_queue *queue;
2199 unsigned int q;
2200
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002201 u32 config;
2202
2203 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002204 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002205
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002206 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302207 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2208 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002209 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002210 config |= MACB_BIT(PAE); /* PAuse Enable */
2211 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002212 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302213 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2214 else
2215 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002216 if (bp->dev->flags & IFF_PROMISC)
2217 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002218 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2219 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002220 if (!(bp->dev->flags & IFF_BROADCAST))
2221 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002222 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002224 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302225 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002226 bp->speed = SPEED_10;
2227 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302228 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002229 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302230 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002231
Jamie Iles0116da42011-03-14 17:38:30 +00002232 macb_configure_dma(bp);
2233
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002234 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002235 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002236 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2237#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2238 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2239 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2240#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002241 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302242#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002243 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002244 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302245#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002246
2247 /* Enable interrupts */
2248 queue_writel(queue, IER,
2249 MACB_RX_INT_FLAGS |
2250 MACB_TX_INT_FLAGS |
2251 MACB_BIT(HRESP));
2252 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002253
2254 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002255 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002256}
2257
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002258/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002259 * locations in the memory map. The least significant bits are stored
2260 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2261 *
2262 * The unicast hash enable and the multicast hash enable bits in the
2263 * network configuration register enable the reception of hash matched
2264 * frames. The destination address is reduced to a 6 bit index into
2265 * the 64 bit hash register using the following hash function. The
2266 * hash function is an exclusive or of every sixth bit of the
2267 * destination address.
2268 *
2269 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2270 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2271 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2272 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2273 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2274 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2275 *
2276 * da[0] represents the least significant bit of the first byte
2277 * received, that is, the multicast/unicast indicator, and da[47]
2278 * represents the most significant bit of the last byte received. If
2279 * the hash index, hi[n], points to a bit that is set in the hash
2280 * register then the frame will be matched according to whether the
2281 * frame is multicast or unicast. A multicast match will be signalled
2282 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2283 * index points to a bit set in the hash register. A unicast match
2284 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2285 * and the hash index points to a bit set in the hash register. To
2286 * receive all multicast frames, the hash register should be set with
2287 * all ones and the multicast hash enable bit should be set in the
2288 * network configuration register.
2289 */
2290
2291static inline int hash_bit_value(int bitnr, __u8 *addr)
2292{
2293 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2294 return 1;
2295 return 0;
2296}
2297
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002298/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002299static int hash_get_index(__u8 *addr)
2300{
2301 int i, j, bitval;
2302 int hash_index = 0;
2303
2304 for (j = 0; j < 6; j++) {
2305 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002306 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002307
2308 hash_index |= (bitval << j);
2309 }
2310
2311 return hash_index;
2312}
2313
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002314/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002315static void macb_sethashtable(struct net_device *dev)
2316{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002317 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002318 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002319 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002320 struct macb *bp = netdev_priv(dev);
2321
Moritz Fischeraa50b552016-03-29 19:11:13 -07002322 mc_filter[0] = 0;
2323 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002324
Jiri Pirko22bedad32010-04-01 21:22:57 +00002325 netdev_for_each_mc_addr(ha, dev) {
2326 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002327 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2328 }
2329
Jamie Ilesf75ba502011-11-08 10:12:32 +00002330 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2331 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002332}
2333
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002334/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002335static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002336{
2337 unsigned long cfg;
2338 struct macb *bp = netdev_priv(dev);
2339
2340 cfg = macb_readl(bp, NCFGR);
2341
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002342 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002343 /* Enable promiscuous mode */
2344 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002345
2346 /* Disable RX checksum offload */
2347 if (macb_is_gem(bp))
2348 cfg &= ~GEM_BIT(RXCOEN);
2349 } else {
2350 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002351 cfg &= ~MACB_BIT(CAF);
2352
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002353 /* Enable RX checksum offload only if requested */
2354 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2355 cfg |= GEM_BIT(RXCOEN);
2356 }
2357
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002358 if (dev->flags & IFF_ALLMULTI) {
2359 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002360 macb_or_gem_writel(bp, HRB, -1);
2361 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002362 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002363 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002364 /* Enable specific multicasts */
2365 macb_sethashtable(dev);
2366 cfg |= MACB_BIT(NCFGR_MTI);
2367 } else if (dev->flags & (~IFF_ALLMULTI)) {
2368 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002369 macb_or_gem_writel(bp, HRB, 0);
2370 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002371 cfg &= ~MACB_BIT(NCFGR_MTI);
2372 }
2373
2374 macb_writel(bp, NCFGR, cfg);
2375}
2376
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002377static int macb_open(struct net_device *dev)
2378{
2379 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002380 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002381 struct macb_queue *queue;
2382 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002383 int err;
2384
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002385 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002386
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002387 /* carrier starts down */
2388 netif_carrier_off(dev);
2389
frederic RODO6c36a702007-07-12 19:07:24 +02002390 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002391 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002392 return -EAGAIN;
2393
Nicolas Ferre1b447912013-06-04 21:57:11 +00002394 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002395 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002396
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002397 err = macb_alloc_consistent(bp);
2398 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002399 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2400 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002401 return err;
2402 }
2403
Nicolas Ferre4df95132013-06-04 21:57:12 +00002404 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002406
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002407 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2408 napi_enable(&queue->napi);
2409
frederic RODO6c36a702007-07-12 19:07:24 +02002410 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002411 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002412
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002413 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002414
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002415 if (bp->ptp_info)
2416 bp->ptp_info->ptp_init(dev);
2417
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002418 return 0;
2419}
2420
2421static int macb_close(struct net_device *dev)
2422{
2423 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002424 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002425 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002426 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002427
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002428 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002429
2430 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2431 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002432
Philippe Reynes0a912812016-06-22 00:32:35 +02002433 if (dev->phydev)
2434 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002435
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002436 spin_lock_irqsave(&bp->lock, flags);
2437 macb_reset_hw(bp);
2438 netif_carrier_off(dev);
2439 spin_unlock_irqrestore(&bp->lock, flags);
2440
2441 macb_free_consistent(bp);
2442
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002443 if (bp->ptp_info)
2444 bp->ptp_info->ptp_remove(dev);
2445
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002446 return 0;
2447}
2448
Harini Katakama5898ea2015-05-06 22:27:18 +05302449static int macb_change_mtu(struct net_device *dev, int new_mtu)
2450{
Harini Katakama5898ea2015-05-06 22:27:18 +05302451 if (netif_running(dev))
2452 return -EBUSY;
2453
Harini Katakama5898ea2015-05-06 22:27:18 +05302454 dev->mtu = new_mtu;
2455
2456 return 0;
2457}
2458
Jamie Ilesa494ed82011-03-09 16:26:35 +00002459static void gem_update_stats(struct macb *bp)
2460{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002461 struct macb_queue *queue;
2462 unsigned int i, q, idx;
2463 unsigned long *stat;
2464
Jamie Ilesa494ed82011-03-09 16:26:35 +00002465 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002466
Xander Huff3ff13f12015-01-13 16:15:51 -06002467 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2468 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002469 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002470
2471 bp->ethtool_stats[i] += val;
2472 *p += val;
2473
2474 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2475 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002476 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002477 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002478 *(++p) += val;
2479 }
2480 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002481
2482 idx = GEM_STATS_LEN;
2483 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2484 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2485 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002486}
2487
2488static struct net_device_stats *gem_get_stats(struct macb *bp)
2489{
2490 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002491 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002492
2493 gem_update_stats(bp);
2494
2495 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2496 hwstat->rx_alignment_errors +
2497 hwstat->rx_resource_errors +
2498 hwstat->rx_overruns +
2499 hwstat->rx_oversize_frames +
2500 hwstat->rx_jabbers +
2501 hwstat->rx_undersized_frames +
2502 hwstat->rx_length_field_frame_errors);
2503 nstat->tx_errors = (hwstat->tx_late_collisions +
2504 hwstat->tx_excessive_collisions +
2505 hwstat->tx_underrun +
2506 hwstat->tx_carrier_sense_errors);
2507 nstat->multicast = hwstat->rx_multicast_frames;
2508 nstat->collisions = (hwstat->tx_single_collision_frames +
2509 hwstat->tx_multiple_collision_frames +
2510 hwstat->tx_excessive_collisions);
2511 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2512 hwstat->rx_jabbers +
2513 hwstat->rx_undersized_frames +
2514 hwstat->rx_length_field_frame_errors);
2515 nstat->rx_over_errors = hwstat->rx_resource_errors;
2516 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2517 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2518 nstat->rx_fifo_errors = hwstat->rx_overruns;
2519 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2520 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2521 nstat->tx_fifo_errors = hwstat->tx_underrun;
2522
2523 return nstat;
2524}
2525
Xander Huff3ff13f12015-01-13 16:15:51 -06002526static void gem_get_ethtool_stats(struct net_device *dev,
2527 struct ethtool_stats *stats, u64 *data)
2528{
2529 struct macb *bp;
2530
2531 bp = netdev_priv(dev);
2532 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002533 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2534 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002535}
2536
2537static int gem_get_sset_count(struct net_device *dev, int sset)
2538{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002539 struct macb *bp = netdev_priv(dev);
2540
Xander Huff3ff13f12015-01-13 16:15:51 -06002541 switch (sset) {
2542 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002543 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002544 default:
2545 return -EOPNOTSUPP;
2546 }
2547}
2548
2549static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2550{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002551 char stat_string[ETH_GSTRING_LEN];
2552 struct macb *bp = netdev_priv(dev);
2553 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002554 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002555 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002556
2557 switch (sset) {
2558 case ETH_SS_STATS:
2559 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2560 memcpy(p, gem_statistics[i].stat_string,
2561 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002562
2563 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2564 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2565 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2566 q, queue_statistics[i].stat_string);
2567 memcpy(p, stat_string, ETH_GSTRING_LEN);
2568 }
2569 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002570 break;
2571 }
2572}
2573
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002574static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002575{
2576 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002577 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002578 struct macb_stats *hwstat = &bp->hw_stats.macb;
2579
2580 if (macb_is_gem(bp))
2581 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002582
frederic RODO6c36a702007-07-12 19:07:24 +02002583 /* read stats from hardware */
2584 macb_update_stats(bp);
2585
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002586 /* Convert HW stats into netdevice stats */
2587 nstat->rx_errors = (hwstat->rx_fcs_errors +
2588 hwstat->rx_align_errors +
2589 hwstat->rx_resource_errors +
2590 hwstat->rx_overruns +
2591 hwstat->rx_oversize_pkts +
2592 hwstat->rx_jabbers +
2593 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002594 hwstat->rx_length_mismatch);
2595 nstat->tx_errors = (hwstat->tx_late_cols +
2596 hwstat->tx_excessive_cols +
2597 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002598 hwstat->tx_carrier_errors +
2599 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002600 nstat->collisions = (hwstat->tx_single_cols +
2601 hwstat->tx_multiple_cols +
2602 hwstat->tx_excessive_cols);
2603 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2604 hwstat->rx_jabbers +
2605 hwstat->rx_undersize_pkts +
2606 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002607 nstat->rx_over_errors = hwstat->rx_resource_errors +
2608 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002609 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2610 nstat->rx_frame_errors = hwstat->rx_align_errors;
2611 nstat->rx_fifo_errors = hwstat->rx_overruns;
2612 /* XXX: What does "missed" mean? */
2613 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2614 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2615 nstat->tx_fifo_errors = hwstat->tx_underruns;
2616 /* Don't know about heartbeat or window errors... */
2617
2618 return nstat;
2619}
2620
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002621static int macb_get_regs_len(struct net_device *netdev)
2622{
2623 return MACB_GREGS_NBR * sizeof(u32);
2624}
2625
2626static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2627 void *p)
2628{
2629 struct macb *bp = netdev_priv(dev);
2630 unsigned int tail, head;
2631 u32 *regs_buff = p;
2632
2633 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2634 | MACB_GREGS_VERSION;
2635
Zach Brownb410d132016-10-19 09:56:57 -05002636 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2637 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002638
2639 regs_buff[0] = macb_readl(bp, NCR);
2640 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2641 regs_buff[2] = macb_readl(bp, NSR);
2642 regs_buff[3] = macb_readl(bp, TSR);
2643 regs_buff[4] = macb_readl(bp, RBQP);
2644 regs_buff[5] = macb_readl(bp, TBQP);
2645 regs_buff[6] = macb_readl(bp, RSR);
2646 regs_buff[7] = macb_readl(bp, IMR);
2647
2648 regs_buff[8] = tail;
2649 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002650 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2651 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002652
Neil Armstrongce721a72016-01-05 14:39:16 +01002653 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2654 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002655 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002656 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002657}
2658
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002659static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2660{
2661 struct macb *bp = netdev_priv(netdev);
2662
2663 wol->supported = 0;
2664 wol->wolopts = 0;
2665
2666 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2667 wol->supported = WAKE_MAGIC;
2668
2669 if (bp->wol & MACB_WOL_ENABLED)
2670 wol->wolopts |= WAKE_MAGIC;
2671 }
2672}
2673
2674static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2675{
2676 struct macb *bp = netdev_priv(netdev);
2677
2678 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2679 (wol->wolopts & ~WAKE_MAGIC))
2680 return -EOPNOTSUPP;
2681
2682 if (wol->wolopts & WAKE_MAGIC)
2683 bp->wol |= MACB_WOL_ENABLED;
2684 else
2685 bp->wol &= ~MACB_WOL_ENABLED;
2686
2687 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2688
2689 return 0;
2690}
2691
Zach Brown8441bb32016-10-19 09:56:58 -05002692static void macb_get_ringparam(struct net_device *netdev,
2693 struct ethtool_ringparam *ring)
2694{
2695 struct macb *bp = netdev_priv(netdev);
2696
2697 ring->rx_max_pending = MAX_RX_RING_SIZE;
2698 ring->tx_max_pending = MAX_TX_RING_SIZE;
2699
2700 ring->rx_pending = bp->rx_ring_size;
2701 ring->tx_pending = bp->tx_ring_size;
2702}
2703
2704static int macb_set_ringparam(struct net_device *netdev,
2705 struct ethtool_ringparam *ring)
2706{
2707 struct macb *bp = netdev_priv(netdev);
2708 u32 new_rx_size, new_tx_size;
2709 unsigned int reset = 0;
2710
2711 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2712 return -EINVAL;
2713
2714 new_rx_size = clamp_t(u32, ring->rx_pending,
2715 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2716 new_rx_size = roundup_pow_of_two(new_rx_size);
2717
2718 new_tx_size = clamp_t(u32, ring->tx_pending,
2719 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2720 new_tx_size = roundup_pow_of_two(new_tx_size);
2721
2722 if ((new_tx_size == bp->tx_ring_size) &&
2723 (new_rx_size == bp->rx_ring_size)) {
2724 /* nothing to do */
2725 return 0;
2726 }
2727
2728 if (netif_running(bp->dev)) {
2729 reset = 1;
2730 macb_close(bp->dev);
2731 }
2732
2733 bp->rx_ring_size = new_rx_size;
2734 bp->tx_ring_size = new_tx_size;
2735
2736 if (reset)
2737 macb_open(bp->dev);
2738
2739 return 0;
2740}
2741
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002742#ifdef CONFIG_MACB_USE_HWSTAMP
2743static unsigned int gem_get_tsu_rate(struct macb *bp)
2744{
2745 struct clk *tsu_clk;
2746 unsigned int tsu_rate;
2747
2748 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2749 if (!IS_ERR(tsu_clk))
2750 tsu_rate = clk_get_rate(tsu_clk);
2751 /* try pclk instead */
2752 else if (!IS_ERR(bp->pclk)) {
2753 tsu_clk = bp->pclk;
2754 tsu_rate = clk_get_rate(tsu_clk);
2755 } else
2756 return -ENOTSUPP;
2757 return tsu_rate;
2758}
2759
2760static s32 gem_get_ptp_max_adj(void)
2761{
2762 return 64000000;
2763}
2764
2765static int gem_get_ts_info(struct net_device *dev,
2766 struct ethtool_ts_info *info)
2767{
2768 struct macb *bp = netdev_priv(dev);
2769
2770 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2771 ethtool_op_get_ts_info(dev, info);
2772 return 0;
2773 }
2774
2775 info->so_timestamping =
2776 SOF_TIMESTAMPING_TX_SOFTWARE |
2777 SOF_TIMESTAMPING_RX_SOFTWARE |
2778 SOF_TIMESTAMPING_SOFTWARE |
2779 SOF_TIMESTAMPING_TX_HARDWARE |
2780 SOF_TIMESTAMPING_RX_HARDWARE |
2781 SOF_TIMESTAMPING_RAW_HARDWARE;
2782 info->tx_types =
2783 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2784 (1 << HWTSTAMP_TX_OFF) |
2785 (1 << HWTSTAMP_TX_ON);
2786 info->rx_filters =
2787 (1 << HWTSTAMP_FILTER_NONE) |
2788 (1 << HWTSTAMP_FILTER_ALL);
2789
2790 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2791
2792 return 0;
2793}
2794
2795static struct macb_ptp_info gem_ptp_info = {
2796 .ptp_init = gem_ptp_init,
2797 .ptp_remove = gem_ptp_remove,
2798 .get_ptp_max_adj = gem_get_ptp_max_adj,
2799 .get_tsu_rate = gem_get_tsu_rate,
2800 .get_ts_info = gem_get_ts_info,
2801 .get_hwtst = gem_get_hwtst,
2802 .set_hwtst = gem_set_hwtst,
2803};
2804#endif
2805
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002806static int macb_get_ts_info(struct net_device *netdev,
2807 struct ethtool_ts_info *info)
2808{
2809 struct macb *bp = netdev_priv(netdev);
2810
2811 if (bp->ptp_info)
2812 return bp->ptp_info->get_ts_info(netdev, info);
2813
2814 return ethtool_op_get_ts_info(netdev, info);
2815}
2816
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002817static void gem_enable_flow_filters(struct macb *bp, bool enable)
2818{
2819 struct ethtool_rx_fs_item *item;
2820 u32 t2_scr;
2821 int num_t2_scr;
2822
2823 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2824
2825 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2826 struct ethtool_rx_flow_spec *fs = &item->fs;
2827 struct ethtool_tcpip4_spec *tp4sp_m;
2828
2829 if (fs->location >= num_t2_scr)
2830 continue;
2831
2832 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2833
2834 /* enable/disable screener regs for the flow entry */
2835 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2836
2837 /* only enable fields with no masking */
2838 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2839
2840 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2841 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2842 else
2843 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2844
2845 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2846 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2847 else
2848 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2849
2850 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2851 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2852 else
2853 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2854
2855 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2856 }
2857}
2858
2859static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2860{
2861 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2862 uint16_t index = fs->location;
2863 u32 w0, w1, t2_scr;
2864 bool cmp_a = false;
2865 bool cmp_b = false;
2866 bool cmp_c = false;
2867
2868 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2869 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2870
2871 /* ignore field if any masking set */
2872 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2873 /* 1st compare reg - IP source address */
2874 w0 = 0;
2875 w1 = 0;
2876 w0 = tp4sp_v->ip4src;
2877 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2878 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2879 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2880 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2881 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2882 cmp_a = true;
2883 }
2884
2885 /* ignore field if any masking set */
2886 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2887 /* 2nd compare reg - IP destination address */
2888 w0 = 0;
2889 w1 = 0;
2890 w0 = tp4sp_v->ip4dst;
2891 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2892 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2893 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2894 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2895 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2896 cmp_b = true;
2897 }
2898
2899 /* ignore both port fields if masking set in both */
2900 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2901 /* 3rd compare reg - source port, destination port */
2902 w0 = 0;
2903 w1 = 0;
2904 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2905 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2906 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2907 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2908 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2909 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2910 } else {
2911 /* only one port definition */
2912 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2913 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2914 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2915 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2916 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2917 } else { /* dst port */
2918 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2919 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2920 }
2921 }
2922 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2923 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2924 cmp_c = true;
2925 }
2926
2927 t2_scr = 0;
2928 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2929 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2930 if (cmp_a)
2931 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2932 if (cmp_b)
2933 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2934 if (cmp_c)
2935 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2936 gem_writel_n(bp, SCRT2, index, t2_scr);
2937}
2938
2939static int gem_add_flow_filter(struct net_device *netdev,
2940 struct ethtool_rxnfc *cmd)
2941{
2942 struct macb *bp = netdev_priv(netdev);
2943 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2944 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002945 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002946 int ret = -EINVAL;
2947 bool added = false;
2948
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002949 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002950 if (newfs == NULL)
2951 return -ENOMEM;
2952 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2953
2954 netdev_dbg(netdev,
2955 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2956 fs->flow_type, (int)fs->ring_cookie, fs->location,
2957 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2958 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2959 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2960
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002961 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2962
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002963 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002964 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2965 if (item->fs.location > newfs->fs.location) {
2966 list_add_tail(&newfs->list, &item->list);
2967 added = true;
2968 break;
2969 } else if (item->fs.location == fs->location) {
2970 netdev_err(netdev, "Rule not added: location %d not free!\n",
2971 fs->location);
2972 ret = -EBUSY;
2973 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002974 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002975 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002976 if (!added)
2977 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002978
2979 gem_prog_cmp_regs(bp, fs);
2980 bp->rx_fs_list.count++;
2981 /* enable filtering if NTUPLE on */
2982 if (netdev->features & NETIF_F_NTUPLE)
2983 gem_enable_flow_filters(bp, 1);
2984
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002985 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002986 return 0;
2987
2988err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002989 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002990 kfree(newfs);
2991 return ret;
2992}
2993
2994static int gem_del_flow_filter(struct net_device *netdev,
2995 struct ethtool_rxnfc *cmd)
2996{
2997 struct macb *bp = netdev_priv(netdev);
2998 struct ethtool_rx_fs_item *item;
2999 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003000 unsigned long flags;
3001
3002 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003003
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003004 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3005 if (item->fs.location == cmd->fs.location) {
3006 /* disable screener regs for the flow entry */
3007 fs = &(item->fs);
3008 netdev_dbg(netdev,
3009 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3010 fs->flow_type, (int)fs->ring_cookie, fs->location,
3011 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3012 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3013 htons(fs->h_u.tcp_ip4_spec.psrc),
3014 htons(fs->h_u.tcp_ip4_spec.pdst));
3015
3016 gem_writel_n(bp, SCRT2, fs->location, 0);
3017
3018 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003019 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003020 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3021 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003022 return 0;
3023 }
3024 }
3025
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003026 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003027 return -EINVAL;
3028}
3029
3030static int gem_get_flow_entry(struct net_device *netdev,
3031 struct ethtool_rxnfc *cmd)
3032{
3033 struct macb *bp = netdev_priv(netdev);
3034 struct ethtool_rx_fs_item *item;
3035
3036 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3037 if (item->fs.location == cmd->fs.location) {
3038 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3039 return 0;
3040 }
3041 }
3042 return -EINVAL;
3043}
3044
3045static int gem_get_all_flow_entries(struct net_device *netdev,
3046 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3047{
3048 struct macb *bp = netdev_priv(netdev);
3049 struct ethtool_rx_fs_item *item;
3050 uint32_t cnt = 0;
3051
3052 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3053 if (cnt == cmd->rule_cnt)
3054 return -EMSGSIZE;
3055 rule_locs[cnt] = item->fs.location;
3056 cnt++;
3057 }
3058 cmd->data = bp->max_tuples;
3059 cmd->rule_cnt = cnt;
3060
3061 return 0;
3062}
3063
3064static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3065 u32 *rule_locs)
3066{
3067 struct macb *bp = netdev_priv(netdev);
3068 int ret = 0;
3069
3070 switch (cmd->cmd) {
3071 case ETHTOOL_GRXRINGS:
3072 cmd->data = bp->num_queues;
3073 break;
3074 case ETHTOOL_GRXCLSRLCNT:
3075 cmd->rule_cnt = bp->rx_fs_list.count;
3076 break;
3077 case ETHTOOL_GRXCLSRULE:
3078 ret = gem_get_flow_entry(netdev, cmd);
3079 break;
3080 case ETHTOOL_GRXCLSRLALL:
3081 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3082 break;
3083 default:
3084 netdev_err(netdev,
3085 "Command parameter %d is not supported\n", cmd->cmd);
3086 ret = -EOPNOTSUPP;
3087 }
3088
3089 return ret;
3090}
3091
3092static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3093{
3094 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003095 int ret;
3096
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003097 switch (cmd->cmd) {
3098 case ETHTOOL_SRXCLSRLINS:
3099 if ((cmd->fs.location >= bp->max_tuples)
3100 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3101 ret = -EINVAL;
3102 break;
3103 }
3104 ret = gem_add_flow_filter(netdev, cmd);
3105 break;
3106 case ETHTOOL_SRXCLSRLDEL:
3107 ret = gem_del_flow_filter(netdev, cmd);
3108 break;
3109 default:
3110 netdev_err(netdev,
3111 "Command parameter %d is not supported\n", cmd->cmd);
3112 ret = -EOPNOTSUPP;
3113 }
3114
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003115 return ret;
3116}
3117
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003118static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003119 .get_regs_len = macb_get_regs_len,
3120 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003121 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003122 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003123 .get_wol = macb_get_wol,
3124 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003125 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3126 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003127 .get_ringparam = macb_get_ringparam,
3128 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003129};
Xander Huff8cd5a562015-01-15 15:55:20 -06003130
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003131static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003132 .get_regs_len = macb_get_regs_len,
3133 .get_regs = macb_get_regs,
3134 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003135 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003136 .get_ethtool_stats = gem_get_ethtool_stats,
3137 .get_strings = gem_get_ethtool_strings,
3138 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003139 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3140 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003141 .get_ringparam = macb_get_ringparam,
3142 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003143 .get_rxnfc = gem_get_rxnfc,
3144 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003145};
3146
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003147static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003148{
Philippe Reynes0a912812016-06-22 00:32:35 +02003149 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003150 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003151
3152 if (!netif_running(dev))
3153 return -EINVAL;
3154
frederic RODO6c36a702007-07-12 19:07:24 +02003155 if (!phydev)
3156 return -ENODEV;
3157
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003158 if (!bp->ptp_info)
3159 return phy_mii_ioctl(phydev, rq, cmd);
3160
3161 switch (cmd) {
3162 case SIOCSHWTSTAMP:
3163 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3164 case SIOCGHWTSTAMP:
3165 return bp->ptp_info->get_hwtst(dev, rq);
3166 default:
3167 return phy_mii_ioctl(phydev, rq, cmd);
3168 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003169}
3170
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003171static int macb_set_features(struct net_device *netdev,
3172 netdev_features_t features)
3173{
3174 struct macb *bp = netdev_priv(netdev);
3175 netdev_features_t changed = features ^ netdev->features;
3176
3177 /* TX checksum offload */
3178 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3179 u32 dmacfg;
3180
3181 dmacfg = gem_readl(bp, DMACFG);
3182 if (features & NETIF_F_HW_CSUM)
3183 dmacfg |= GEM_BIT(TXCOEN);
3184 else
3185 dmacfg &= ~GEM_BIT(TXCOEN);
3186 gem_writel(bp, DMACFG, dmacfg);
3187 }
3188
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003189 /* RX checksum offload */
3190 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3191 u32 netcfg;
3192
3193 netcfg = gem_readl(bp, NCFGR);
3194 if (features & NETIF_F_RXCSUM &&
3195 !(netdev->flags & IFF_PROMISC))
3196 netcfg |= GEM_BIT(RXCOEN);
3197 else
3198 netcfg &= ~GEM_BIT(RXCOEN);
3199 gem_writel(bp, NCFGR, netcfg);
3200 }
3201
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003202 /* RX Flow Filters */
3203 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3204 bool turn_on = features & NETIF_F_NTUPLE;
3205
3206 gem_enable_flow_filters(bp, turn_on);
3207 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003208 return 0;
3209}
3210
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003211static const struct net_device_ops macb_netdev_ops = {
3212 .ndo_open = macb_open,
3213 .ndo_stop = macb_close,
3214 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003215 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003216 .ndo_get_stats = macb_get_stats,
3217 .ndo_do_ioctl = macb_ioctl,
3218 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303219 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003220 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003221#ifdef CONFIG_NET_POLL_CONTROLLER
3222 .ndo_poll_controller = macb_poll_controller,
3223#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003224 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003225 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003226};
3227
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003228/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003229 * and integration options used
3230 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003231static void macb_configure_caps(struct macb *bp,
3232 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003233{
3234 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003235
Nicolas Ferref6970502015-03-31 15:02:01 +02003236 if (dt_conf)
3237 bp->caps = dt_conf->caps;
3238
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003239 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003240 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3241
Nicolas Ferree1755872014-07-24 13:50:58 +02003242 dcfg = gem_readl(bp, DCFG1);
3243 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3244 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3245 dcfg = gem_readl(bp, DCFG2);
3246 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3247 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003248#ifdef CONFIG_MACB_USE_HWSTAMP
3249 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003250 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3251 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003252 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003253 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003254 bp->ptp_info = &gem_ptp_info;
3255 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003256 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003257#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003258 }
3259
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003260 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003261}
3262
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003263static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003264 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003265 unsigned int *queue_mask,
3266 unsigned int *num_queues)
3267{
3268 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003269
3270 *queue_mask = 0x1;
3271 *num_queues = 1;
3272
Nicolas Ferreda120112015-03-31 15:02:00 +02003273 /* is it macb or gem ?
3274 *
3275 * We need to read directly from the hardware here because
3276 * we are early in the probe process and don't have the
3277 * MACB_CAPS_MACB_IS_GEM flag positioned
3278 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003279 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003280 return;
3281
3282 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303283 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3284
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003285 *queue_mask |= 0x1;
3286
3287 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3288 if (*queue_mask & (1 << hw_q))
3289 (*num_queues)++;
3290}
3291
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003292static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303293 struct clk **hclk, struct clk **tx_clk,
3294 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003295{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003296 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003297 int err;
3298
Bartosz Folta83a77e92016-12-14 06:39:15 +00003299 pdata = dev_get_platdata(&pdev->dev);
3300 if (pdata) {
3301 *pclk = pdata->pclk;
3302 *hclk = pdata->hclk;
3303 } else {
3304 *pclk = devm_clk_get(&pdev->dev, "pclk");
3305 *hclk = devm_clk_get(&pdev->dev, "hclk");
3306 }
3307
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003308 if (IS_ERR(*pclk)) {
3309 err = PTR_ERR(*pclk);
3310 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3311 return err;
3312 }
3313
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003314 if (IS_ERR(*hclk)) {
3315 err = PTR_ERR(*hclk);
3316 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3317 return err;
3318 }
3319
3320 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3321 if (IS_ERR(*tx_clk))
3322 *tx_clk = NULL;
3323
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303324 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3325 if (IS_ERR(*rx_clk))
3326 *rx_clk = NULL;
3327
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003328 err = clk_prepare_enable(*pclk);
3329 if (err) {
3330 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3331 return err;
3332 }
3333
3334 err = clk_prepare_enable(*hclk);
3335 if (err) {
3336 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3337 goto err_disable_pclk;
3338 }
3339
3340 err = clk_prepare_enable(*tx_clk);
3341 if (err) {
3342 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3343 goto err_disable_hclk;
3344 }
3345
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303346 err = clk_prepare_enable(*rx_clk);
3347 if (err) {
3348 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3349 goto err_disable_txclk;
3350 }
3351
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003352 return 0;
3353
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303354err_disable_txclk:
3355 clk_disable_unprepare(*tx_clk);
3356
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003357err_disable_hclk:
3358 clk_disable_unprepare(*hclk);
3359
3360err_disable_pclk:
3361 clk_disable_unprepare(*pclk);
3362
3363 return err;
3364}
3365
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003366static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003367{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003368 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003369 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003370 struct macb *bp = netdev_priv(dev);
3371 struct macb_queue *queue;
3372 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003373 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003374
Zach Brownb410d132016-10-19 09:56:57 -05003375 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3376 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3377
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003378 /* set the queue register mapping once for all: queue0 has a special
3379 * register mapping but we don't want to test the queue index then
3380 * compute the corresponding register offset at run time.
3381 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003382 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003383 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003384 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003385
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003386 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003387 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003388 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003389 if (hw_q) {
3390 queue->ISR = GEM_ISR(hw_q - 1);
3391 queue->IER = GEM_IER(hw_q - 1);
3392 queue->IDR = GEM_IDR(hw_q - 1);
3393 queue->IMR = GEM_IMR(hw_q - 1);
3394 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003395 queue->RBQP = GEM_RBQP(hw_q - 1);
3396 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303397#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003398 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003399 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003400 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3401 }
Harini Katakamfff80192016-08-09 13:15:53 +05303402#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003403 } else {
3404 /* queue0 uses legacy registers */
3405 queue->ISR = MACB_ISR;
3406 queue->IER = MACB_IER;
3407 queue->IDR = MACB_IDR;
3408 queue->IMR = MACB_IMR;
3409 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003410 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303411#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003412 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003413 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003414 queue->RBQPH = MACB_RBQPH;
3415 }
Harini Katakamfff80192016-08-09 13:15:53 +05303416#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003417 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003418
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003419 /* get irq: here we use the linux queue index, not the hardware
3420 * queue index. the queue irq definitions in the device tree
3421 * must remove the optional gaps that could exist in the
3422 * hardware queue mask.
3423 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003424 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003425 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003426 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003427 if (err) {
3428 dev_err(&pdev->dev,
3429 "Unable to request IRQ %d (error %d)\n",
3430 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003431 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003432 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003433
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003434 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003435 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003436 }
3437
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003438 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003439
Nicolas Ferre4df95132013-06-04 21:57:12 +00003440 /* setup appropriated routines according to adapter type */
3441 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003442 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003443 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3444 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3445 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3446 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003447 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003448 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003449 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003450 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3451 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3452 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3453 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003454 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003455 }
3456
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003457 /* Set features */
3458 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003459
3460 /* Check LSO capability */
3461 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3462 dev->hw_features |= MACB_NETIF_LSO;
3463
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003464 /* Checksum offload is only available on gem with packet buffer */
3465 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003466 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003467 if (bp->caps & MACB_CAPS_SG_DISABLED)
3468 dev->hw_features &= ~NETIF_F_SG;
3469 dev->features = dev->hw_features;
3470
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003471 /* Check RX Flow Filters support.
3472 * Max Rx flows set by availability of screeners & compare regs:
3473 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3474 */
3475 reg = gem_readl(bp, DCFG8);
3476 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3477 GEM_BFEXT(T2SCR, reg));
3478 if (bp->max_tuples > 0) {
3479 /* also needs one ethtype match to check IPv4 */
3480 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3481 /* program this reg now */
3482 reg = 0;
3483 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3484 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3485 /* Filtering is supported in hw but don't enable it in kernel now */
3486 dev->hw_features |= NETIF_F_NTUPLE;
3487 /* init Rx flow definitions */
3488 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3489 bp->rx_fs_list.count = 0;
3490 spin_lock_init(&bp->rx_fs_lock);
3491 } else
3492 bp->max_tuples = 0;
3493 }
3494
Neil Armstrongce721a72016-01-05 14:39:16 +01003495 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3496 val = 0;
3497 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3498 val = GEM_BIT(RGMII);
3499 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003500 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003501 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003502 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003503 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003504
Neil Armstrongce721a72016-01-05 14:39:16 +01003505 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3506 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003507
Neil Armstrongce721a72016-01-05 14:39:16 +01003508 macb_or_gem_writel(bp, USRIO, val);
3509 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003510
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003511 /* Set MII management clock divider */
3512 val = macb_mdc_clk_div(bp);
3513 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303514 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3515 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003516 macb_writel(bp, NCFGR, val);
3517
3518 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003519}
3520
3521#if defined(CONFIG_OF)
3522/* 1518 rounded up */
3523#define AT91ETHER_MAX_RBUFF_SZ 0x600
3524/* max number of receive buffers */
3525#define AT91ETHER_MAX_RX_DESCR 9
3526
3527/* Initialize and start the Receiver and Transmit subsystems */
3528static int at91ether_start(struct net_device *dev)
3529{
3530 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003531 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003532 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003533 dma_addr_t addr;
3534 u32 ctl;
3535 int i;
3536
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003537 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003538 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003539 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003540 &q->rx_ring_dma, GFP_KERNEL);
3541 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003542 return -ENOMEM;
3543
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003544 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003545 AT91ETHER_MAX_RX_DESCR *
3546 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003547 &q->rx_buffers_dma, GFP_KERNEL);
3548 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003549 dma_free_coherent(&lp->pdev->dev,
3550 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003551 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003552 q->rx_ring, q->rx_ring_dma);
3553 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003554 return -ENOMEM;
3555 }
3556
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003557 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003558 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003559 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003560 macb_set_addr(lp, desc, addr);
3561 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003562 addr += AT91ETHER_MAX_RBUFF_SZ;
3563 }
3564
3565 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003566 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003567
3568 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003569 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003570
3571 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003572 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003573
3574 /* Enable Receive and Transmit */
3575 ctl = macb_readl(lp, NCR);
3576 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3577
3578 return 0;
3579}
3580
3581/* Open the ethernet interface */
3582static int at91ether_open(struct net_device *dev)
3583{
3584 struct macb *lp = netdev_priv(dev);
3585 u32 ctl;
3586 int ret;
3587
3588 /* Clear internal statistics */
3589 ctl = macb_readl(lp, NCR);
3590 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3591
3592 macb_set_hwaddr(lp);
3593
3594 ret = at91ether_start(dev);
3595 if (ret)
3596 return ret;
3597
3598 /* Enable MAC interrupts */
3599 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3600 MACB_BIT(RXUBR) |
3601 MACB_BIT(ISR_TUND) |
3602 MACB_BIT(ISR_RLE) |
3603 MACB_BIT(TCOMP) |
3604 MACB_BIT(ISR_ROVR) |
3605 MACB_BIT(HRESP));
3606
3607 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003608 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003609
3610 netif_start_queue(dev);
3611
3612 return 0;
3613}
3614
3615/* Close the interface */
3616static int at91ether_close(struct net_device *dev)
3617{
3618 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003619 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003620 u32 ctl;
3621
3622 /* Disable Receiver and Transmitter */
3623 ctl = macb_readl(lp, NCR);
3624 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3625
3626 /* Disable MAC interrupts */
3627 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3628 MACB_BIT(RXUBR) |
3629 MACB_BIT(ISR_TUND) |
3630 MACB_BIT(ISR_RLE) |
3631 MACB_BIT(TCOMP) |
3632 MACB_BIT(ISR_ROVR) |
3633 MACB_BIT(HRESP));
3634
3635 netif_stop_queue(dev);
3636
3637 dma_free_coherent(&lp->pdev->dev,
3638 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003639 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003640 q->rx_ring, q->rx_ring_dma);
3641 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003642
3643 dma_free_coherent(&lp->pdev->dev,
3644 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003645 q->rx_buffers, q->rx_buffers_dma);
3646 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003647
3648 return 0;
3649}
3650
3651/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003652static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3653 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003654{
3655 struct macb *lp = netdev_priv(dev);
3656
3657 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3658 netif_stop_queue(dev);
3659
3660 /* Store packet information (to free when Tx completed) */
3661 lp->skb = skb;
3662 lp->skb_length = skb->len;
3663 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3664 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003665 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3666 dev_kfree_skb_any(skb);
3667 dev->stats.tx_dropped++;
3668 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3669 return NETDEV_TX_OK;
3670 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003671
3672 /* Set address of the data in the Transmit Address register */
3673 macb_writel(lp, TAR, lp->skb_physaddr);
3674 /* Set length of the packet in the Transmit Control register */
3675 macb_writel(lp, TCR, skb->len);
3676
3677 } else {
3678 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3679 return NETDEV_TX_BUSY;
3680 }
3681
3682 return NETDEV_TX_OK;
3683}
3684
3685/* Extract received frame from buffer descriptors and sent to upper layers.
3686 * (Called from interrupt context)
3687 */
3688static void at91ether_rx(struct net_device *dev)
3689{
3690 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003691 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003692 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003693 unsigned char *p_recv;
3694 struct sk_buff *skb;
3695 unsigned int pktlen;
3696
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003697 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003698 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003699 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003700 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003701 skb = netdev_alloc_skb(dev, pktlen + 2);
3702 if (skb) {
3703 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003704 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003705
3706 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003707 dev->stats.rx_packets++;
3708 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003709 netif_rx(skb);
3710 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003711 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003712 }
3713
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003714 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003715 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003716
3717 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003718 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003719
3720 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003721 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3722 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003723 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003724 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003725
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003726 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003727 }
3728}
3729
3730/* MAC interrupt handler */
3731static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3732{
3733 struct net_device *dev = dev_id;
3734 struct macb *lp = netdev_priv(dev);
3735 u32 intstatus, ctl;
3736
3737 /* MAC Interrupt Status register indicates what interrupts are pending.
3738 * It is automatically cleared once read.
3739 */
3740 intstatus = macb_readl(lp, ISR);
3741
3742 /* Receive complete */
3743 if (intstatus & MACB_BIT(RCOMP))
3744 at91ether_rx(dev);
3745
3746 /* Transmit complete */
3747 if (intstatus & MACB_BIT(TCOMP)) {
3748 /* The TCOM bit is set even if the transmission failed */
3749 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003750 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003751
3752 if (lp->skb) {
3753 dev_kfree_skb_irq(lp->skb);
3754 lp->skb = NULL;
3755 dma_unmap_single(NULL, lp->skb_physaddr,
3756 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003757 dev->stats.tx_packets++;
3758 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003759 }
3760 netif_wake_queue(dev);
3761 }
3762
3763 /* Work-around for EMAC Errata section 41.3.1 */
3764 if (intstatus & MACB_BIT(RXUBR)) {
3765 ctl = macb_readl(lp, NCR);
3766 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003767 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003768 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3769 }
3770
3771 if (intstatus & MACB_BIT(ISR_ROVR))
3772 netdev_err(dev, "ROVR error\n");
3773
3774 return IRQ_HANDLED;
3775}
3776
3777#ifdef CONFIG_NET_POLL_CONTROLLER
3778static void at91ether_poll_controller(struct net_device *dev)
3779{
3780 unsigned long flags;
3781
3782 local_irq_save(flags);
3783 at91ether_interrupt(dev->irq, dev);
3784 local_irq_restore(flags);
3785}
3786#endif
3787
3788static const struct net_device_ops at91ether_netdev_ops = {
3789 .ndo_open = at91ether_open,
3790 .ndo_stop = at91ether_close,
3791 .ndo_start_xmit = at91ether_start_xmit,
3792 .ndo_get_stats = macb_get_stats,
3793 .ndo_set_rx_mode = macb_set_rx_mode,
3794 .ndo_set_mac_address = eth_mac_addr,
3795 .ndo_do_ioctl = macb_ioctl,
3796 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003797#ifdef CONFIG_NET_POLL_CONTROLLER
3798 .ndo_poll_controller = at91ether_poll_controller,
3799#endif
3800};
3801
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003802static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303803 struct clk **hclk, struct clk **tx_clk,
3804 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003805{
3806 int err;
3807
3808 *hclk = NULL;
3809 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303810 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003811
3812 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3813 if (IS_ERR(*pclk))
3814 return PTR_ERR(*pclk);
3815
3816 err = clk_prepare_enable(*pclk);
3817 if (err) {
3818 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3819 return err;
3820 }
3821
3822 return 0;
3823}
3824
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003825static int at91ether_init(struct platform_device *pdev)
3826{
3827 struct net_device *dev = platform_get_drvdata(pdev);
3828 struct macb *bp = netdev_priv(dev);
3829 int err;
3830 u32 reg;
3831
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003832 bp->queues[0].bp = bp;
3833
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003834 dev->netdev_ops = &at91ether_netdev_ops;
3835 dev->ethtool_ops = &macb_ethtool_ops;
3836
3837 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3838 0, dev->name, dev);
3839 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003840 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003841
3842 macb_writel(bp, NCR, 0);
3843
3844 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3845 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3846 reg |= MACB_BIT(RM9200_RMII);
3847
3848 macb_writel(bp, NCFGR, reg);
3849
3850 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003851}
3852
David S. Miller3cef5c52015-03-09 23:38:02 -04003853static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003854 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003855 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003856 .init = macb_init,
3857};
3858
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003859static const struct macb_config sama5d3macb_config = {
3860 .caps = MACB_CAPS_SG_DISABLED
3861 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3862 .clk_init = macb_clk_init,
3863 .init = macb_init,
3864};
3865
David S. Miller3cef5c52015-03-09 23:38:02 -04003866static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003867 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3868 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003869 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003870 .init = macb_init,
3871};
3872
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003873static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003874 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003875 .dma_burst_length = 16,
3876 .clk_init = macb_clk_init,
3877 .init = macb_init,
3878};
3879
David S. Miller3cef5c52015-03-09 23:38:02 -04003880static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003881 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003882 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003883 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003884 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003885 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003886 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003887};
3888
David S. Miller3cef5c52015-03-09 23:38:02 -04003889static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003890 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003891 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003892 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003893 .init = macb_init,
3894};
3895
David S. Miller3cef5c52015-03-09 23:38:02 -04003896static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003897 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003898 .init = at91ether_init,
3899};
3900
Neil Armstronge611b5b2016-01-05 14:39:17 +01003901static const struct macb_config np4_config = {
3902 .caps = MACB_CAPS_USRIO_DISABLED,
3903 .clk_init = macb_clk_init,
3904 .init = macb_init,
3905};
David S. Miller36583eb2015-05-23 01:22:35 -04003906
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303907static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003908 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3909 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05303910 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303911 .dma_burst_length = 16,
3912 .clk_init = macb_clk_init,
3913 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303914 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303915};
3916
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003917static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303918 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003919 .dma_burst_length = 16,
3920 .clk_init = macb_clk_init,
3921 .init = macb_init,
3922};
3923
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003924static const struct of_device_id macb_dt_ids[] = {
3925 { .compatible = "cdns,at32ap7000-macb" },
3926 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3927 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003928 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003929 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3930 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003931 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003932 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003933 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003934 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3935 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3936 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303937 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003938 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003939 { /* sentinel */ }
3940};
3941MODULE_DEVICE_TABLE(of, macb_dt_ids);
3942#endif /* CONFIG_OF */
3943
Bartosz Folta83a77e92016-12-14 06:39:15 +00003944static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003945 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3946 MACB_CAPS_JUMBO |
3947 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003948 .dma_burst_length = 16,
3949 .clk_init = macb_clk_init,
3950 .init = macb_init,
3951 .jumbo_max_len = 10240,
3952};
3953
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003954static int macb_probe(struct platform_device *pdev)
3955{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003956 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003957 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303958 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003959 = macb_config->clk_init;
3960 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003961 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303962 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003963 unsigned int queue_mask, num_queues;
3964 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003965 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003966 struct phy_device *phydev;
3967 struct net_device *dev;
3968 struct resource *regs;
3969 void __iomem *mem;
3970 const char *mac;
3971 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05303972 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003973
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003974 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3975 mem = devm_ioremap_resource(&pdev->dev, regs);
3976 if (IS_ERR(mem))
3977 return PTR_ERR(mem);
3978
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003979 if (np) {
3980 const struct of_device_id *match;
3981
3982 match = of_match_node(macb_dt_ids, np);
3983 if (match && match->data) {
3984 macb_config = match->data;
3985 clk_init = macb_config->clk_init;
3986 init = macb_config->init;
3987 }
3988 }
3989
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303990 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003991 if (err)
3992 return err;
3993
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003994 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003995
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003996 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003997 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003998 if (!dev) {
3999 err = -ENOMEM;
4000 goto err_disable_clocks;
4001 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004002
4003 dev->base_addr = regs->start;
4004
4005 SET_NETDEV_DEV(dev, &pdev->dev);
4006
4007 bp = netdev_priv(dev);
4008 bp->pdev = pdev;
4009 bp->dev = dev;
4010 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004011 bp->native_io = native_io;
4012 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004013 bp->macb_reg_readl = hw_readl_native;
4014 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004015 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004016 bp->macb_reg_readl = hw_readl;
4017 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004018 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004019 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004020 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004021 if (macb_config)
4022 bp->dma_burst_length = macb_config->dma_burst_length;
4023 bp->pclk = pclk;
4024 bp->hclk = hclk;
4025 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304026 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004027 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304028 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304029
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004030 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004031 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004032 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4033 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4034
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004035 spin_lock_init(&bp->lock);
4036
Nicolas Ferread783472015-03-31 15:02:02 +02004037 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004038 macb_configure_caps(bp, macb_config);
4039
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004040#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4041 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4042 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4043 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4044 }
4045#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004046 platform_set_drvdata(pdev, dev);
4047
4048 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004049 if (dev->irq < 0) {
4050 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004051 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004052 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004053
Jarod Wilson44770e12016-10-17 15:54:17 -04004054 /* MTU range: 68 - 1500 or 10240 */
4055 dev->min_mtu = GEM_MTU_MIN_SIZE;
4056 if (bp->caps & MACB_CAPS_JUMBO)
4057 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4058 else
4059 dev->max_mtu = ETH_DATA_LEN;
4060
Harini Katakam404cd082018-07-06 12:18:58 +05304061 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4062 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4063 if (val)
4064 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4065 macb_dma_desc_get_size(bp);
4066
4067 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4068 if (val)
4069 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4070 macb_dma_desc_get_size(bp);
4071 }
4072
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004073 mac = of_get_mac_address(np);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004074 if (mac) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004075 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004076 } else {
4077 err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
4078 if (err) {
4079 if (err == -EPROBE_DEFER)
4080 goto err_out_free_netdev;
4081 macb_get_hwaddr(bp);
4082 }
4083 }
frederic RODO6c36a702007-07-12 19:07:24 +02004084
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004085 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004086 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09004087 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004088 if (pdata && pdata->is_rmii)
4089 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4090 else
4091 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4092 } else {
4093 bp->phy_interface = err;
4094 }
4095
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004096 /* IP specific init */
4097 err = init(pdev);
4098 if (err)
4099 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004100
Florian Fainellicf669662016-05-02 18:38:45 -07004101 err = macb_mii_init(bp);
4102 if (err)
4103 goto err_out_free_netdev;
4104
Philippe Reynes0a912812016-06-22 00:32:35 +02004105 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004106
4107 netif_carrier_off(dev);
4108
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004109 err = register_netdev(dev);
4110 if (err) {
4111 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004112 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004113 }
4114
Harini Katakam032dc412018-01-27 12:09:01 +05304115 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4116 (unsigned long)bp);
4117
Florian Fainellicf669662016-05-02 18:38:45 -07004118 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004119
Bo Shen58798232014-09-13 01:57:49 +02004120 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4121 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4122 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004123
4124 return 0;
4125
Florian Fainellicf669662016-05-02 18:38:45 -07004126err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004127 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004128 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004129 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004130 if (np && of_phy_is_fixed_link(np))
4131 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004132 mdiobus_free(bp->mii_bus);
4133
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004134err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004135 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004136
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004137err_disable_clocks:
4138 clk_disable_unprepare(tx_clk);
4139 clk_disable_unprepare(hclk);
4140 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304141 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004142
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004143 return err;
4144}
4145
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004146static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004147{
4148 struct net_device *dev;
4149 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004150 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004151
4152 dev = platform_get_drvdata(pdev);
4153
4154 if (dev) {
4155 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004156 if (dev->phydev)
4157 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004158 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004159 if (np && of_phy_is_fixed_link(np))
4160 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004161 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004162 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004163
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004164 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004165 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004166 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004167 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304168 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004169 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004170 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004171 }
4172
4173 return 0;
4174}
4175
Michal Simekd23823d2015-01-23 09:36:03 +01004176static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004177{
Wolfram Sangce886a42018-10-21 22:00:14 +02004178 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004179 struct macb *bp = netdev_priv(netdev);
4180
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004181 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004182 netif_device_detach(netdev);
4183
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004184 if (bp->wol & MACB_WOL_ENABLED) {
4185 macb_writel(bp, IER, MACB_BIT(WOL));
4186 macb_writel(bp, WOL, MACB_BIT(MAG));
4187 enable_irq_wake(bp->queues[0].irq);
4188 } else {
4189 clk_disable_unprepare(bp->tx_clk);
4190 clk_disable_unprepare(bp->hclk);
4191 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304192 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004193 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004194
4195 return 0;
4196}
4197
Michal Simekd23823d2015-01-23 09:36:03 +01004198static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004199{
Wolfram Sangce886a42018-10-21 22:00:14 +02004200 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004201 struct macb *bp = netdev_priv(netdev);
4202
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004203 if (bp->wol & MACB_WOL_ENABLED) {
4204 macb_writel(bp, IDR, MACB_BIT(WOL));
4205 macb_writel(bp, WOL, 0);
4206 disable_irq_wake(bp->queues[0].irq);
4207 } else {
4208 clk_prepare_enable(bp->pclk);
4209 clk_prepare_enable(bp->hclk);
4210 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304211 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004212 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004213
4214 netif_device_attach(netdev);
4215
4216 return 0;
4217}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004218
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004219static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4220
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004221static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004222 .probe = macb_probe,
4223 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004224 .driver = {
4225 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004226 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004227 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004228 },
4229};
4230
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004231module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004232
4233MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004234MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004235MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004236MODULE_ALIAS("platform:macb");