blob: c2f03c60f84b856837eb78fcacee16a1fe09e83d [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);
Anssi Hannulae100a892018-12-17 15:05:39 +0200684 /* The low bits of RX address contain the RX_USED bit, clearing
685 * of which allows packet RX. Make sure the high bits are also
686 * visible to HW at that point.
687 */
688 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000689 }
Harini Katakamfff80192016-08-09 13:15:53 +0530690#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000691 desc->addr = lower_32_bits(addr);
692}
693
694static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
695{
696 dma_addr_t addr = 0;
697#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
698 struct macb_dma_desc_64 *desc_64;
699
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100700 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000701 desc_64 = macb_64b_desc(bp, desc);
702 addr = ((u64)(desc_64->addrh) << 32);
703 }
704#endif
705 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
706 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530707}
708
Nicolas Ferree86cd532012-10-31 06:04:57 +0000709static void macb_tx_error_task(struct work_struct *work)
710{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100711 struct macb_queue *queue = container_of(work, struct macb_queue,
712 tx_error_task);
713 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000714 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100715 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000716 struct sk_buff *skb;
717 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100718 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000719
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100720 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
721 (unsigned int)(queue - bp->queues),
722 queue->tx_tail, queue->tx_head);
723
724 /* Prevent the queue IRQ handlers from running: each of them may call
725 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
726 * As explained below, we have to halt the transmission before updating
727 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
728 * network engine about the macb/gem being halted.
729 */
730 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000731
732 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100733 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000734
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700735 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000736 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100737 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000738 */
739 if (macb_halt_tx(bp))
740 /* Just complain for now, reinitializing TX path can be good */
741 netdev_err(bp->dev, "BUG: halt tx timed out\n");
742
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700743 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000744 * Free transmit buffers in upper layer.
745 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100746 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
747 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000748
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100749 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000750 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100751 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000752 skb = tx_skb->skb;
753
754 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200755 /* skb is set for the last buffer of the frame */
756 while (!skb) {
757 macb_tx_unmap(bp, tx_skb);
758 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100759 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200760 skb = tx_skb->skb;
761 }
762
763 /* ctrl still refers to the first buffer descriptor
764 * since it's the only one written back by the hardware
765 */
766 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
767 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500768 macb_tx_ring_wrap(bp, tail),
769 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200770 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000771 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200772 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000773 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200774 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000775 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700776 /* "Buffers exhausted mid-frame" errors may only happen
777 * if the driver is buggy, so complain loudly about
778 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000779 */
780 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
781 netdev_err(bp->dev,
782 "BUG: TX buffers exhausted mid-frame\n");
783
784 desc->ctrl = ctrl | MACB_BIT(TX_USED);
785 }
786
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200787 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000788 }
789
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100790 /* Set end of TX queue */
791 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000792 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100793 desc->ctrl = MACB_BIT(TX_USED);
794
Nicolas Ferree86cd532012-10-31 06:04:57 +0000795 /* Make descriptor updates visible to hardware */
796 wmb();
797
798 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000799 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530800#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100801 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000802 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530803#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000804 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100805 queue->tx_head = 0;
806 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000807
808 /* Housework before enabling TX IRQ */
809 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100810 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
811
812 /* Now we are ready to start transmission again */
813 netif_tx_start_all_queues(bp->dev);
814 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
815
816 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000817}
818
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100819static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100820{
821 unsigned int tail;
822 unsigned int head;
823 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100824 struct macb *bp = queue->bp;
825 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100826
827 status = macb_readl(bp, TSR);
828 macb_writel(bp, TSR, status);
829
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000830 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100831 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000832
Nicolas Ferree86cd532012-10-31 06:04:57 +0000833 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700834 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100835
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100836 head = queue->tx_head;
837 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000838 struct macb_tx_skb *tx_skb;
839 struct sk_buff *skb;
840 struct macb_dma_desc *desc;
841 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100842
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100843 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100844
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000845 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000847
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000848 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100849
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200850 /* TX_USED bit is only set by hardware on the very first buffer
851 * descriptor of the transmitted frame.
852 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000853 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 break;
855
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200856 /* Process all buffers of the current transmitted frame */
857 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100858 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200859 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000860
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200861 /* First, update TX stats if needed */
862 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100863 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
864 /* skb now belongs to timestamp buffer
865 * and will be removed later
866 */
867 tx_skb->skb = NULL;
868 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200869 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500870 macb_tx_ring_wrap(bp, tail),
871 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200872 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000873 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200874 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000875 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200876 }
877
878 /* Now we can safely release resources */
879 macb_tx_unmap(bp, tx_skb);
880
881 /* skb is set only for the last buffer of the frame.
882 * WARNING: at this point skb has been freed by
883 * macb_tx_unmap().
884 */
885 if (skb)
886 break;
887 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100888 }
889
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100890 queue->tx_tail = tail;
891 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
892 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500893 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100894 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100895}
896
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000897static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000898{
899 unsigned int entry;
900 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000901 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000902 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000903 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000904
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000905 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
906 bp->rx_ring_size) > 0) {
907 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000908
909 /* Make hw descriptor updates visible to CPU */
910 rmb();
911
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000912 queue->rx_prepared_head++;
913 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000914
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000915 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000916 /* allocate sk_buff for this free entry in ring */
917 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700918 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000919 netdev_err(bp->dev,
920 "Unable to allocate sk_buff\n");
921 break;
922 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000923
924 /* now fill corresponding descriptor entry */
925 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700926 bp->rx_buffer_size,
927 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800928 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
929 dev_kfree_skb(skb);
930 break;
931 }
932
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000933 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000934
Zach Brownb410d132016-10-19 09:56:57 -0500935 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000936 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000937 macb_set_addr(bp, desc, paddr);
938 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000939
940 /* properly align Ethernet header */
941 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530942 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000943 desc->addr &= ~MACB_BIT(RX_USED);
944 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000945 }
946 }
947
948 /* Make descriptor updates visible to hardware */
949 wmb();
950
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000951 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
952 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000953}
954
955/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000956static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000957 unsigned int end)
958{
959 unsigned int frag;
960
961 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000962 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700963
Nicolas Ferre4df95132013-06-04 21:57:12 +0000964 desc->addr &= ~MACB_BIT(RX_USED);
965 }
966
967 /* Make descriptor updates visible to hardware */
968 wmb();
969
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700970 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000971 * whatever caused this is updated, so we don't have to record
972 * anything.
973 */
974}
975
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000976static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000977{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000978 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000979 unsigned int len;
980 unsigned int entry;
981 struct sk_buff *skb;
982 struct macb_dma_desc *desc;
983 int count = 0;
984
985 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530986 u32 ctrl;
987 dma_addr_t addr;
988 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000989
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000990 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
991 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000992
993 /* Make hw descriptor updates visible to CPU */
994 rmb();
995
Harini Katakamfff80192016-08-09 13:15:53 +0530996 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000997 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000998 ctrl = desc->ctrl;
999
Harini Katakamfff80192016-08-09 13:15:53 +05301000 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001001 break;
1002
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001003 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001004 count++;
1005
1006 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1007 netdev_err(bp->dev,
1008 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001009 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001010 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001011 break;
1012 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001013 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001014 if (unlikely(!skb)) {
1015 netdev_err(bp->dev,
1016 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001017 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001018 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001019 break;
1020 }
1021 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001022 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301023 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001024
1025 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1026
1027 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001028 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001029 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001030
1031 skb->protocol = eth_type_trans(skb, bp->dev);
1032 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001033 if (bp->dev->features & NETIF_F_RXCSUM &&
1034 !(bp->dev->flags & IFF_PROMISC) &&
1035 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1036 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001037
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001038 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001039 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001040 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001041 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001042
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001043 gem_ptp_do_rxstamp(bp, skb, desc);
1044
Nicolas Ferre4df95132013-06-04 21:57:12 +00001045#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1046 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1047 skb->len, skb->csum);
1048 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001049 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001050 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1051 skb->data, 32, true);
1052#endif
1053
1054 netif_receive_skb(skb);
1055 }
1056
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001057 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001058
1059 return count;
1060}
1061
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001062static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001063 unsigned int last_frag)
1064{
1065 unsigned int len;
1066 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001067 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001068 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001069 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001070 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001072 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301073 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001074
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001075 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001076 macb_rx_ring_wrap(bp, first_frag),
1077 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001079 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001080 * first buffer. Since the header is 14 bytes, this makes the
1081 * payload word-aligned.
1082 *
1083 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1084 * the two padding bytes into the skb so that we avoid hitting
1085 * the slowpath in memcpy(), and pull them off afterwards.
1086 */
1087 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001088 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001089 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001090 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001091 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001092 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093 if (frag == last_frag)
1094 break;
1095 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001096
1097 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001098 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001099
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001100 return 1;
1101 }
1102
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001103 offset = 0;
1104 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001105 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001106 skb_put(skb, len);
1107
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001108 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001109 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001110
1111 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001112 if (unlikely(frag != last_frag)) {
1113 dev_kfree_skb_any(skb);
1114 return -1;
1115 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001116 frag_len = len - offset;
1117 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001118 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001119 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001120 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001121 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001122 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001123 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001124
1125 if (frag == last_frag)
1126 break;
1127 }
1128
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001129 /* Make descriptor updates visible to hardware */
1130 wmb();
1131
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001132 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001133 skb->protocol = eth_type_trans(skb, bp->dev);
1134
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001135 bp->dev->stats.rx_packets++;
1136 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001137 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001138 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001139 netif_receive_skb(skb);
1140
1141 return 0;
1142}
1143
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001144static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001145{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001146 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001147 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001148 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001149 int i;
1150
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001151 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001152 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001153 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001154 macb_set_addr(bp, desc, addr);
1155 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001156 addr += bp->rx_buffer_size;
1157 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001158 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001159 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001160}
1161
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001162static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001164 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001165 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001166 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001167 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168 int first_frag = -1;
1169
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001170 for (tail = queue->rx_tail; budget > 0; tail++) {
1171 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001172 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001173
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001174 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001175 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001176
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001177 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001178
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001179 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001180 break;
1181
1182 if (ctrl & MACB_BIT(RX_SOF)) {
1183 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001184 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001185 first_frag = tail;
1186 }
1187
1188 if (ctrl & MACB_BIT(RX_EOF)) {
1189 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001190
1191 if (unlikely(first_frag == -1)) {
1192 reset_rx_queue = true;
1193 continue;
1194 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001195
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001196 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001197 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001198 if (unlikely(dropped < 0)) {
1199 reset_rx_queue = true;
1200 continue;
1201 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001202 if (!dropped) {
1203 received++;
1204 budget--;
1205 }
1206 }
1207 }
1208
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001209 if (unlikely(reset_rx_queue)) {
1210 unsigned long flags;
1211 u32 ctrl;
1212
1213 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1214
1215 spin_lock_irqsave(&bp->lock, flags);
1216
1217 ctrl = macb_readl(bp, NCR);
1218 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1219
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001220 macb_init_rx_ring(queue);
1221 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001222
1223 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1224
1225 spin_unlock_irqrestore(&bp->lock, flags);
1226 return received;
1227 }
1228
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001230 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001231 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001232 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233
1234 return received;
1235}
1236
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001237static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001238{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001239 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1240 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001241 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001242 u32 status;
1243
1244 status = macb_readl(bp, RSR);
1245 macb_writel(bp, RSR, status);
1246
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001247 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001248 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001249
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001250 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001251 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001252 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001253
Nicolas Ferre8770e912013-02-12 11:08:48 +01001254 /* Packets received while interrupts were disabled */
1255 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001256 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001257 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001258 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001259 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001260 } else {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001261 queue_writel(queue, IER, MACB_RX_INT_FLAGS);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001262 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001263 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001264
1265 /* TODO: Handle errors */
1266
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001267 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001268}
1269
Harini Katakam032dc412018-01-27 12:09:01 +05301270static void macb_hresp_error_task(unsigned long data)
1271{
1272 struct macb *bp = (struct macb *)data;
1273 struct net_device *dev = bp->dev;
1274 struct macb_queue *queue = bp->queues;
1275 unsigned int q;
1276 u32 ctrl;
1277
1278 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1279 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
1280 MACB_TX_INT_FLAGS |
1281 MACB_BIT(HRESP));
1282 }
1283 ctrl = macb_readl(bp, NCR);
1284 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1285 macb_writel(bp, NCR, ctrl);
1286
1287 netif_tx_stop_all_queues(dev);
1288 netif_carrier_off(dev);
1289
1290 bp->macbgem_ops.mog_init_rings(bp);
1291
1292 /* Initialize TX and RX buffers */
1293 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1294 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1295#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1296 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1297 queue_writel(queue, RBQPH,
1298 upper_32_bits(queue->rx_ring_dma));
1299#endif
1300 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1301#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1302 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1303 queue_writel(queue, TBQPH,
1304 upper_32_bits(queue->tx_ring_dma));
1305#endif
1306
1307 /* Enable interrupts */
1308 queue_writel(queue, IER,
1309 MACB_RX_INT_FLAGS |
1310 MACB_TX_INT_FLAGS |
1311 MACB_BIT(HRESP));
1312 }
1313
1314 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1315 macb_writel(bp, NCR, ctrl);
1316
1317 netif_carrier_on(dev);
1318 netif_tx_start_all_queues(dev);
1319}
1320
Claudiu Beznea42983882018-12-17 10:02:42 +00001321static void macb_tx_restart(struct macb_queue *queue)
1322{
1323 unsigned int head = queue->tx_head;
1324 unsigned int tail = queue->tx_tail;
1325 struct macb *bp = queue->bp;
1326
1327 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1328 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1329
1330 if (head == tail)
1331 return;
1332
1333 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1334}
1335
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001336static irqreturn_t macb_interrupt(int irq, void *dev_id)
1337{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001338 struct macb_queue *queue = dev_id;
1339 struct macb *bp = queue->bp;
1340 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001341 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001342
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001343 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001344
1345 if (unlikely(!status))
1346 return IRQ_NONE;
1347
1348 spin_lock(&bp->lock);
1349
1350 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001351 /* close possible race with dev_close */
1352 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001353 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001354 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1355 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001356 break;
1357 }
1358
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001359 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1360 (unsigned int)(queue - bp->queues),
1361 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001362
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001363 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001364 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001365 * until we have processed the buffers. The
1366 * scheduling call may fail if the poll routine
1367 * is already scheduled, so disable interrupts
1368 * now.
1369 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001371 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001372 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001373
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001374 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001375 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001376 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001377 }
1378 }
1379
Nicolas Ferree86cd532012-10-31 06:04:57 +00001380 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001381 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1382 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001383
1384 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001385 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001386
Nicolas Ferree86cd532012-10-31 06:04:57 +00001387 break;
1388 }
1389
1390 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001391 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001392
Claudiu Beznea42983882018-12-17 10:02:42 +00001393 if (status & MACB_BIT(TXUBR))
1394 macb_tx_restart(queue);
1395
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001396 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001397 * add that if/when we get our hands on a full-blown MII PHY.
1398 */
1399
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001400 /* There is a hardware issue under heavy load where DMA can
1401 * stop, this causes endless "used buffer descriptor read"
1402 * interrupts but it can be cleared by re-enabling RX. See
1403 * the at91 manual, section 41.3.1 or the Zynq manual
1404 * section 16.7.4 for details.
1405 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001406 if (status & MACB_BIT(RXUBR)) {
1407 ctrl = macb_readl(bp, NCR);
1408 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001409 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001410 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1411
1412 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001413 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001414 }
1415
Alexander Steinb19f7f72011-04-13 05:03:24 +00001416 if (status & MACB_BIT(ISR_ROVR)) {
1417 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001418 if (macb_is_gem(bp))
1419 bp->hw_stats.gem.rx_overruns++;
1420 else
1421 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001422
1423 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001424 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001425 }
1426
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301428 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001429 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001430
1431 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001432 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001433 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001434 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001435 }
1436
1437 spin_unlock(&bp->lock);
1438
1439 return IRQ_HANDLED;
1440}
1441
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001442#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001443/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001444 * to allow network i/o with interrupts disabled.
1445 */
1446static void macb_poll_controller(struct net_device *dev)
1447{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001448 struct macb *bp = netdev_priv(dev);
1449 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001450 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001451 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001452
1453 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001454 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1455 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001456 local_irq_restore(flags);
1457}
1458#endif
1459
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001460static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001461 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001462 struct sk_buff *skb,
1463 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001464{
1465 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001466 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001467 struct macb_tx_skb *tx_skb = NULL;
1468 struct macb_dma_desc *desc;
1469 unsigned int offset, size, count = 0;
1470 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001471 unsigned int eof = 1, mss_mfs = 0;
1472 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1473
1474 /* LSO */
1475 if (skb_shinfo(skb)->gso_size != 0) {
1476 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1477 /* UDP - UFO */
1478 lso_ctrl = MACB_LSO_UFO_ENABLE;
1479 else
1480 /* TCP - TSO */
1481 lso_ctrl = MACB_LSO_TSO_ENABLE;
1482 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001483
1484 /* First, map non-paged data */
1485 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001486
1487 /* first buffer length */
1488 size = hdrlen;
1489
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001490 offset = 0;
1491 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001492 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001493 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001494
1495 mapping = dma_map_single(&bp->pdev->dev,
1496 skb->data + offset,
1497 size, DMA_TO_DEVICE);
1498 if (dma_mapping_error(&bp->pdev->dev, mapping))
1499 goto dma_error;
1500
1501 /* Save info to properly release resources */
1502 tx_skb->skb = NULL;
1503 tx_skb->mapping = mapping;
1504 tx_skb->size = size;
1505 tx_skb->mapped_as_page = false;
1506
1507 len -= size;
1508 offset += size;
1509 count++;
1510 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001511
1512 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001513 }
1514
1515 /* Then, map paged data from fragments */
1516 for (f = 0; f < nr_frags; f++) {
1517 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1518
1519 len = skb_frag_size(frag);
1520 offset = 0;
1521 while (len) {
1522 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001523 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001524 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001525
1526 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1527 offset, size, DMA_TO_DEVICE);
1528 if (dma_mapping_error(&bp->pdev->dev, mapping))
1529 goto dma_error;
1530
1531 /* Save info to properly release resources */
1532 tx_skb->skb = NULL;
1533 tx_skb->mapping = mapping;
1534 tx_skb->size = size;
1535 tx_skb->mapped_as_page = true;
1536
1537 len -= size;
1538 offset += size;
1539 count++;
1540 tx_head++;
1541 }
1542 }
1543
1544 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001545 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001546 netdev_err(bp->dev, "BUG! empty skb!\n");
1547 return 0;
1548 }
1549
1550 /* This is the last buffer of the frame: save socket buffer */
1551 tx_skb->skb = skb;
1552
1553 /* Update TX ring: update buffer descriptors in reverse order
1554 * to avoid race condition
1555 */
1556
1557 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1558 * to set the end of TX queue
1559 */
1560 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001561 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001562 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001563 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001564 desc->ctrl = ctrl;
1565
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001566 if (lso_ctrl) {
1567 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1568 /* include header and FCS in value given to h/w */
1569 mss_mfs = skb_shinfo(skb)->gso_size +
1570 skb_transport_offset(skb) +
1571 ETH_FCS_LEN;
1572 else /* TSO */ {
1573 mss_mfs = skb_shinfo(skb)->gso_size;
1574 /* TCP Sequence Number Source Select
1575 * can be set only for TSO
1576 */
1577 seq_ctrl = 0;
1578 }
1579 }
1580
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001581 do {
1582 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001583 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001584 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001585 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001586
1587 ctrl = (u32)tx_skb->size;
1588 if (eof) {
1589 ctrl |= MACB_BIT(TX_LAST);
1590 eof = 0;
1591 }
Zach Brownb410d132016-10-19 09:56:57 -05001592 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001593 ctrl |= MACB_BIT(TX_WRAP);
1594
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001595 /* First descriptor is header descriptor */
1596 if (i == queue->tx_head) {
1597 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1598 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001599 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1600 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1601 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001602 } else
1603 /* Only set MSS/MFS on payload descriptors
1604 * (second or later descriptor)
1605 */
1606 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1607
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001608 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001609 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001610 /* desc->addr must be visible to hardware before clearing
1611 * 'TX_USED' bit in desc->ctrl.
1612 */
1613 wmb();
1614 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001615 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001616
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001617 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001618
1619 return count;
1620
1621dma_error:
1622 netdev_err(bp->dev, "TX DMA map failed\n");
1623
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001624 for (i = queue->tx_head; i != tx_head; i++) {
1625 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001626
1627 macb_tx_unmap(bp, tx_skb);
1628 }
1629
1630 return 0;
1631}
1632
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001633static netdev_features_t macb_features_check(struct sk_buff *skb,
1634 struct net_device *dev,
1635 netdev_features_t features)
1636{
1637 unsigned int nr_frags, f;
1638 unsigned int hdrlen;
1639
1640 /* Validate LSO compatibility */
1641
1642 /* there is only one buffer */
1643 if (!skb_is_nonlinear(skb))
1644 return features;
1645
1646 /* length of header */
1647 hdrlen = skb_transport_offset(skb);
1648 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1649 hdrlen += tcp_hdrlen(skb);
1650
1651 /* For LSO:
1652 * When software supplies two or more payload buffers all payload buffers
1653 * apart from the last must be a multiple of 8 bytes in size.
1654 */
1655 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1656 return features & ~MACB_NETIF_LSO;
1657
1658 nr_frags = skb_shinfo(skb)->nr_frags;
1659 /* No need to check last fragment */
1660 nr_frags--;
1661 for (f = 0; f < nr_frags; f++) {
1662 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1663
1664 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1665 return features & ~MACB_NETIF_LSO;
1666 }
1667 return features;
1668}
1669
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001670static inline int macb_clear_csum(struct sk_buff *skb)
1671{
1672 /* no change for packets without checksum offloading */
1673 if (skb->ip_summed != CHECKSUM_PARTIAL)
1674 return 0;
1675
1676 /* make sure we can modify the header */
1677 if (unlikely(skb_cow_head(skb, 0)))
1678 return -1;
1679
1680 /* initialize checksum field
1681 * This is required - at least for Zynq, which otherwise calculates
1682 * wrong UDP header checksums for UDP packets with UDP data len <=2
1683 */
1684 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1685 return 0;
1686}
1687
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001688static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1689{
1690 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1691 int padlen = ETH_ZLEN - (*skb)->len;
1692 int headroom = skb_headroom(*skb);
1693 int tailroom = skb_tailroom(*skb);
1694 struct sk_buff *nskb;
1695 u32 fcs;
1696
1697 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1698 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1699 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1700 return 0;
1701
1702 if (padlen <= 0) {
1703 /* FCS could be appeded to tailroom. */
1704 if (tailroom >= ETH_FCS_LEN)
1705 goto add_fcs;
1706 /* FCS could be appeded by moving data to headroom. */
1707 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1708 padlen = 0;
1709 /* No room for FCS, need to reallocate skb. */
1710 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001711 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001712 } else {
1713 /* Add room for FCS. */
1714 padlen += ETH_FCS_LEN;
1715 }
1716
1717 if (!cloned && headroom + tailroom >= padlen) {
1718 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1719 skb_set_tail_pointer(*skb, (*skb)->len);
1720 } else {
1721 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1722 if (!nskb)
1723 return -ENOMEM;
1724
1725 dev_kfree_skb_any(*skb);
1726 *skb = nskb;
1727 }
1728
1729 if (padlen) {
1730 if (padlen >= ETH_FCS_LEN)
1731 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
1732 else
1733 skb_trim(*skb, ETH_FCS_LEN - padlen);
1734 }
1735
1736add_fcs:
1737 /* set FCS to packet */
1738 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1739 fcs = ~fcs;
1740
1741 skb_put_u8(*skb, fcs & 0xff);
1742 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1743 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1744 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1745
1746 return 0;
1747}
1748
Claudiu Beznead1c38952018-08-07 12:25:12 +03001749static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001750{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001751 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001752 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001753 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001754 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001755 unsigned int desc_cnt, nr_frags, frag_size, f;
1756 unsigned int hdrlen;
1757 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001758 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001759
Claudiu Beznea33729f22018-08-07 12:25:13 +03001760 if (macb_clear_csum(skb)) {
1761 dev_kfree_skb_any(skb);
1762 return ret;
1763 }
1764
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001765 if (macb_pad_and_fcs(&skb, dev)) {
1766 dev_kfree_skb_any(skb);
1767 return ret;
1768 }
1769
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001770 is_lso = (skb_shinfo(skb)->gso_size != 0);
1771
1772 if (is_lso) {
1773 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1774
1775 /* length of headers */
1776 if (is_udp)
1777 /* only queue eth + ip headers separately for UDP */
1778 hdrlen = skb_transport_offset(skb);
1779 else
1780 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1781 if (skb_headlen(skb) < hdrlen) {
1782 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1783 /* if this is required, would need to copy to single buffer */
1784 return NETDEV_TX_BUSY;
1785 }
1786 } else
1787 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001788
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001789#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1790 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001791 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1792 queue_index, skb->len, skb->head, skb->data,
1793 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001794 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1795 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001796#endif
1797
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001798 /* Count how many TX buffer descriptors are needed to send this
1799 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001800 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001801 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001802 if (is_lso && (skb_headlen(skb) > hdrlen))
1803 /* extra header descriptor if also payload in first buffer */
1804 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1805 else
1806 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001807 nr_frags = skb_shinfo(skb)->nr_frags;
1808 for (f = 0; f < nr_frags; f++) {
1809 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001810 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001811 }
1812
Dongdong Deng48719532009-08-23 19:49:07 -07001813 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814
1815 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001816 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001817 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001818 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001819 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001820 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001821 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001822 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001823 }
1824
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001825 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001826 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001827 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001828 goto unlock;
1829 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001830
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001831 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001832 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001833 skb_tx_timestamp(skb);
1834
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001835 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1836
Zach Brownb410d132016-10-19 09:56:57 -05001837 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001838 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001839
Soren Brinkmann92030902014-03-04 08:46:39 -08001840unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001841 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001842
Claudiu Beznead1c38952018-08-07 12:25:12 +03001843 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001844}
1845
Nicolas Ferre4df95132013-06-04 21:57:12 +00001846static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001847{
1848 if (!macb_is_gem(bp)) {
1849 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1850 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001851 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001852
Nicolas Ferre1b447912013-06-04 21:57:11 +00001853 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001854 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001855 "RX buffer must be multiple of %d bytes, expanding\n",
1856 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001857 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001858 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001859 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001860 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001861
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001862 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001863 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001864}
1865
Nicolas Ferre4df95132013-06-04 21:57:12 +00001866static void gem_free_rx_buffers(struct macb *bp)
1867{
1868 struct sk_buff *skb;
1869 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001870 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001871 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001872 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001873 int i;
1874
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001875 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1876 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001877 continue;
1878
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001879 for (i = 0; i < bp->rx_ring_size; i++) {
1880 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001881
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001882 if (!skb)
1883 continue;
1884
1885 desc = macb_rx_desc(queue, i);
1886 addr = macb_get_addr(bp, desc);
1887
1888 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1889 DMA_FROM_DEVICE);
1890 dev_kfree_skb_any(skb);
1891 skb = NULL;
1892 }
1893
1894 kfree(queue->rx_skbuff);
1895 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001896 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001897}
1898
1899static void macb_free_rx_buffers(struct macb *bp)
1900{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001901 struct macb_queue *queue = &bp->queues[0];
1902
1903 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001904 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001905 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001906 queue->rx_buffers, queue->rx_buffers_dma);
1907 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001908 }
1909}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001910
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001911static void macb_free_consistent(struct macb *bp)
1912{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001913 struct macb_queue *queue;
1914 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301915 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001916
Nicolas Ferre4df95132013-06-04 21:57:12 +00001917 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001918
1919 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1920 kfree(queue->tx_skb);
1921 queue->tx_skb = NULL;
1922 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301923 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1924 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001925 queue->tx_ring, queue->tx_ring_dma);
1926 queue->tx_ring = NULL;
1927 }
Harini Katakame50b7702018-07-06 12:18:57 +05301928 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301929 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1930 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301931 queue->rx_ring, queue->rx_ring_dma);
1932 queue->rx_ring = NULL;
1933 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001934 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001935}
1936
1937static int gem_alloc_rx_buffers(struct macb *bp)
1938{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001939 struct macb_queue *queue;
1940 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001941 int size;
1942
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001943 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1944 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1945 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1946 if (!queue->rx_skbuff)
1947 return -ENOMEM;
1948 else
1949 netdev_dbg(bp->dev,
1950 "Allocated %d RX struct sk_buff entries at %p\n",
1951 bp->rx_ring_size, queue->rx_skbuff);
1952 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001953 return 0;
1954}
1955
1956static int macb_alloc_rx_buffers(struct macb *bp)
1957{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001958 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001959 int size;
1960
Zach Brownb410d132016-10-19 09:56:57 -05001961 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001962 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1963 &queue->rx_buffers_dma, GFP_KERNEL);
1964 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001965 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001966
1967 netdev_dbg(bp->dev,
1968 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001969 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001970 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001971}
1972
1973static int macb_alloc_consistent(struct macb *bp)
1974{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001975 struct macb_queue *queue;
1976 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001977 int size;
1978
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001979 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05301980 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001981 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1982 &queue->tx_ring_dma,
1983 GFP_KERNEL);
1984 if (!queue->tx_ring)
1985 goto out_err;
1986 netdev_dbg(bp->dev,
1987 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1988 q, size, (unsigned long)queue->tx_ring_dma,
1989 queue->tx_ring);
1990
Zach Brownb410d132016-10-19 09:56:57 -05001991 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001992 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1993 if (!queue->tx_skb)
1994 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001995
Harini Katakam404cd082018-07-06 12:18:58 +05301996 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001997 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1998 &queue->rx_ring_dma, GFP_KERNEL);
1999 if (!queue->rx_ring)
2000 goto out_err;
2001 netdev_dbg(bp->dev,
2002 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2003 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002004 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002005 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002006 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002007
2008 return 0;
2009
2010out_err:
2011 macb_free_consistent(bp);
2012 return -ENOMEM;
2013}
2014
Nicolas Ferre4df95132013-06-04 21:57:12 +00002015static void gem_init_rings(struct macb *bp)
2016{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002017 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002018 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002019 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002020 int i;
2021
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002022 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002023 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002024 desc = macb_tx_desc(queue, i);
2025 macb_set_addr(bp, desc, 0);
2026 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002027 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002028 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002029 queue->tx_head = 0;
2030 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002031
2032 queue->rx_tail = 0;
2033 queue->rx_prepared_head = 0;
2034
2035 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002036 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002037
Nicolas Ferre4df95132013-06-04 21:57:12 +00002038}
2039
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002040static void macb_init_rings(struct macb *bp)
2041{
2042 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002043 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002044
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002045 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002046
Zach Brownb410d132016-10-19 09:56:57 -05002047 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002048 desc = macb_tx_desc(&bp->queues[0], i);
2049 macb_set_addr(bp, desc, 0);
2050 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002051 }
Ben Shelton21d35152015-04-22 17:28:54 -05002052 bp->queues[0].tx_head = 0;
2053 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002054 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002055}
2056
2057static void macb_reset_hw(struct macb *bp)
2058{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002059 struct macb_queue *queue;
2060 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002061 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002062
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002063 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002064 * more gracefully?)
2065 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002066 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002067
2068 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002069 ctrl |= MACB_BIT(CLRSTAT);
2070
2071 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002072
2073 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002074 macb_writel(bp, TSR, -1);
2075 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002076
2077 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002078 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2079 queue_writel(queue, IDR, -1);
2080 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002081 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2082 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002083 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084}
2085
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002086static u32 gem_mdc_clk_div(struct macb *bp)
2087{
2088 u32 config;
2089 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2090
2091 if (pclk_hz <= 20000000)
2092 config = GEM_BF(CLK, GEM_CLK_DIV8);
2093 else if (pclk_hz <= 40000000)
2094 config = GEM_BF(CLK, GEM_CLK_DIV16);
2095 else if (pclk_hz <= 80000000)
2096 config = GEM_BF(CLK, GEM_CLK_DIV32);
2097 else if (pclk_hz <= 120000000)
2098 config = GEM_BF(CLK, GEM_CLK_DIV48);
2099 else if (pclk_hz <= 160000000)
2100 config = GEM_BF(CLK, GEM_CLK_DIV64);
2101 else
2102 config = GEM_BF(CLK, GEM_CLK_DIV96);
2103
2104 return config;
2105}
2106
2107static u32 macb_mdc_clk_div(struct macb *bp)
2108{
2109 u32 config;
2110 unsigned long pclk_hz;
2111
2112 if (macb_is_gem(bp))
2113 return gem_mdc_clk_div(bp);
2114
2115 pclk_hz = clk_get_rate(bp->pclk);
2116 if (pclk_hz <= 20000000)
2117 config = MACB_BF(CLK, MACB_CLK_DIV8);
2118 else if (pclk_hz <= 40000000)
2119 config = MACB_BF(CLK, MACB_CLK_DIV16);
2120 else if (pclk_hz <= 80000000)
2121 config = MACB_BF(CLK, MACB_CLK_DIV32);
2122 else
2123 config = MACB_BF(CLK, MACB_CLK_DIV64);
2124
2125 return config;
2126}
2127
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002128/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002129 * should program. We find the width from decoding the design configuration
2130 * register to find the maximum supported data bus width.
2131 */
2132static u32 macb_dbw(struct macb *bp)
2133{
2134 if (!macb_is_gem(bp))
2135 return 0;
2136
2137 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2138 case 4:
2139 return GEM_BF(DBW, GEM_DBW128);
2140 case 2:
2141 return GEM_BF(DBW, GEM_DBW64);
2142 case 1:
2143 default:
2144 return GEM_BF(DBW, GEM_DBW32);
2145 }
2146}
2147
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002148/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002149 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002150 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002151 * (if not supported by FIFO, it will fallback to default)
2152 * - set both rx/tx packet buffers to full memory size
2153 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002154 */
2155static void macb_configure_dma(struct macb *bp)
2156{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002157 struct macb_queue *queue;
2158 u32 buffer_size;
2159 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002160 u32 dmacfg;
2161
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002162 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002163 if (macb_is_gem(bp)) {
2164 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002165 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2166 if (q)
2167 queue_writel(queue, RBQS, buffer_size);
2168 else
2169 dmacfg |= GEM_BF(RXBS, buffer_size);
2170 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002171 if (bp->dma_burst_length)
2172 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002173 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302174 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302175
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002176 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302177 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2178 else
2179 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2180
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002181 if (bp->dev->features & NETIF_F_HW_CSUM)
2182 dmacfg |= GEM_BIT(TXCOEN);
2183 else
2184 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302185
Michal Simekbd620722018-09-25 08:32:50 +02002186 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302187#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002188 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002189 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302190#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002191#ifdef CONFIG_MACB_USE_HWSTAMP
2192 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2193 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2194#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002195 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2196 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002197 gem_writel(bp, DMACFG, dmacfg);
2198 }
2199}
2200
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002201static void macb_init_hw(struct macb *bp)
2202{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002203 struct macb_queue *queue;
2204 unsigned int q;
2205
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002206 u32 config;
2207
2208 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002209 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002210
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002211 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302212 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2213 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002214 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002215 config |= MACB_BIT(PAE); /* PAuse Enable */
2216 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002217 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302218 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2219 else
2220 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002221 if (bp->dev->flags & IFF_PROMISC)
2222 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002223 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2224 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002225 if (!(bp->dev->flags & IFF_BROADCAST))
2226 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002227 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002228 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002229 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302230 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002231 bp->speed = SPEED_10;
2232 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302233 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002234 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302235 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236
Jamie Iles0116da42011-03-14 17:38:30 +00002237 macb_configure_dma(bp);
2238
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002239 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002240 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002241 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2242#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2243 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2244 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2245#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002246 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302247#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002248 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002249 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302250#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002251
2252 /* Enable interrupts */
2253 queue_writel(queue, IER,
2254 MACB_RX_INT_FLAGS |
2255 MACB_TX_INT_FLAGS |
2256 MACB_BIT(HRESP));
2257 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002258
2259 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002260 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002261}
2262
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002263/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002264 * locations in the memory map. The least significant bits are stored
2265 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2266 *
2267 * The unicast hash enable and the multicast hash enable bits in the
2268 * network configuration register enable the reception of hash matched
2269 * frames. The destination address is reduced to a 6 bit index into
2270 * the 64 bit hash register using the following hash function. The
2271 * hash function is an exclusive or of every sixth bit of the
2272 * destination address.
2273 *
2274 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2275 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2276 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2277 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2278 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2279 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2280 *
2281 * da[0] represents the least significant bit of the first byte
2282 * received, that is, the multicast/unicast indicator, and da[47]
2283 * represents the most significant bit of the last byte received. If
2284 * the hash index, hi[n], points to a bit that is set in the hash
2285 * register then the frame will be matched according to whether the
2286 * frame is multicast or unicast. A multicast match will be signalled
2287 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2288 * index points to a bit set in the hash register. A unicast match
2289 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2290 * and the hash index points to a bit set in the hash register. To
2291 * receive all multicast frames, the hash register should be set with
2292 * all ones and the multicast hash enable bit should be set in the
2293 * network configuration register.
2294 */
2295
2296static inline int hash_bit_value(int bitnr, __u8 *addr)
2297{
2298 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2299 return 1;
2300 return 0;
2301}
2302
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002303/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002304static int hash_get_index(__u8 *addr)
2305{
2306 int i, j, bitval;
2307 int hash_index = 0;
2308
2309 for (j = 0; j < 6; j++) {
2310 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002311 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002312
2313 hash_index |= (bitval << j);
2314 }
2315
2316 return hash_index;
2317}
2318
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002319/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002320static void macb_sethashtable(struct net_device *dev)
2321{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002322 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002323 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002324 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002325 struct macb *bp = netdev_priv(dev);
2326
Moritz Fischeraa50b552016-03-29 19:11:13 -07002327 mc_filter[0] = 0;
2328 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002329
Jiri Pirko22bedad32010-04-01 21:22:57 +00002330 netdev_for_each_mc_addr(ha, dev) {
2331 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002332 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2333 }
2334
Jamie Ilesf75ba502011-11-08 10:12:32 +00002335 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2336 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002337}
2338
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002339/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002340static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002341{
2342 unsigned long cfg;
2343 struct macb *bp = netdev_priv(dev);
2344
2345 cfg = macb_readl(bp, NCFGR);
2346
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002347 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002348 /* Enable promiscuous mode */
2349 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002350
2351 /* Disable RX checksum offload */
2352 if (macb_is_gem(bp))
2353 cfg &= ~GEM_BIT(RXCOEN);
2354 } else {
2355 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002356 cfg &= ~MACB_BIT(CAF);
2357
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002358 /* Enable RX checksum offload only if requested */
2359 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2360 cfg |= GEM_BIT(RXCOEN);
2361 }
2362
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002363 if (dev->flags & IFF_ALLMULTI) {
2364 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002365 macb_or_gem_writel(bp, HRB, -1);
2366 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002367 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002368 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002369 /* Enable specific multicasts */
2370 macb_sethashtable(dev);
2371 cfg |= MACB_BIT(NCFGR_MTI);
2372 } else if (dev->flags & (~IFF_ALLMULTI)) {
2373 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002374 macb_or_gem_writel(bp, HRB, 0);
2375 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002376 cfg &= ~MACB_BIT(NCFGR_MTI);
2377 }
2378
2379 macb_writel(bp, NCFGR, cfg);
2380}
2381
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002382static int macb_open(struct net_device *dev)
2383{
2384 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002385 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002386 struct macb_queue *queue;
2387 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002388 int err;
2389
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002390 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002391
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002392 /* carrier starts down */
2393 netif_carrier_off(dev);
2394
frederic RODO6c36a702007-07-12 19:07:24 +02002395 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002396 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002397 return -EAGAIN;
2398
Nicolas Ferre1b447912013-06-04 21:57:11 +00002399 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002400 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002401
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002402 err = macb_alloc_consistent(bp);
2403 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002404 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2405 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002406 return err;
2407 }
2408
Nicolas Ferre4df95132013-06-04 21:57:12 +00002409 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002410 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002411
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002412 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2413 napi_enable(&queue->napi);
2414
frederic RODO6c36a702007-07-12 19:07:24 +02002415 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002416 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002417
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002418 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002419
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002420 if (bp->ptp_info)
2421 bp->ptp_info->ptp_init(dev);
2422
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002423 return 0;
2424}
2425
2426static int macb_close(struct net_device *dev)
2427{
2428 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002429 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002430 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002431 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002432
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002433 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002434
2435 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2436 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002437
Philippe Reynes0a912812016-06-22 00:32:35 +02002438 if (dev->phydev)
2439 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002440
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002441 spin_lock_irqsave(&bp->lock, flags);
2442 macb_reset_hw(bp);
2443 netif_carrier_off(dev);
2444 spin_unlock_irqrestore(&bp->lock, flags);
2445
2446 macb_free_consistent(bp);
2447
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002448 if (bp->ptp_info)
2449 bp->ptp_info->ptp_remove(dev);
2450
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002451 return 0;
2452}
2453
Harini Katakama5898ea2015-05-06 22:27:18 +05302454static int macb_change_mtu(struct net_device *dev, int new_mtu)
2455{
Harini Katakama5898ea2015-05-06 22:27:18 +05302456 if (netif_running(dev))
2457 return -EBUSY;
2458
Harini Katakama5898ea2015-05-06 22:27:18 +05302459 dev->mtu = new_mtu;
2460
2461 return 0;
2462}
2463
Jamie Ilesa494ed82011-03-09 16:26:35 +00002464static void gem_update_stats(struct macb *bp)
2465{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002466 struct macb_queue *queue;
2467 unsigned int i, q, idx;
2468 unsigned long *stat;
2469
Jamie Ilesa494ed82011-03-09 16:26:35 +00002470 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002471
Xander Huff3ff13f12015-01-13 16:15:51 -06002472 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2473 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002474 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002475
2476 bp->ethtool_stats[i] += val;
2477 *p += val;
2478
2479 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2480 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002481 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002482 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002483 *(++p) += val;
2484 }
2485 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002486
2487 idx = GEM_STATS_LEN;
2488 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2489 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2490 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002491}
2492
2493static struct net_device_stats *gem_get_stats(struct macb *bp)
2494{
2495 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002496 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002497
2498 gem_update_stats(bp);
2499
2500 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2501 hwstat->rx_alignment_errors +
2502 hwstat->rx_resource_errors +
2503 hwstat->rx_overruns +
2504 hwstat->rx_oversize_frames +
2505 hwstat->rx_jabbers +
2506 hwstat->rx_undersized_frames +
2507 hwstat->rx_length_field_frame_errors);
2508 nstat->tx_errors = (hwstat->tx_late_collisions +
2509 hwstat->tx_excessive_collisions +
2510 hwstat->tx_underrun +
2511 hwstat->tx_carrier_sense_errors);
2512 nstat->multicast = hwstat->rx_multicast_frames;
2513 nstat->collisions = (hwstat->tx_single_collision_frames +
2514 hwstat->tx_multiple_collision_frames +
2515 hwstat->tx_excessive_collisions);
2516 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2517 hwstat->rx_jabbers +
2518 hwstat->rx_undersized_frames +
2519 hwstat->rx_length_field_frame_errors);
2520 nstat->rx_over_errors = hwstat->rx_resource_errors;
2521 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2522 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2523 nstat->rx_fifo_errors = hwstat->rx_overruns;
2524 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2525 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2526 nstat->tx_fifo_errors = hwstat->tx_underrun;
2527
2528 return nstat;
2529}
2530
Xander Huff3ff13f12015-01-13 16:15:51 -06002531static void gem_get_ethtool_stats(struct net_device *dev,
2532 struct ethtool_stats *stats, u64 *data)
2533{
2534 struct macb *bp;
2535
2536 bp = netdev_priv(dev);
2537 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002538 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2539 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002540}
2541
2542static int gem_get_sset_count(struct net_device *dev, int sset)
2543{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002544 struct macb *bp = netdev_priv(dev);
2545
Xander Huff3ff13f12015-01-13 16:15:51 -06002546 switch (sset) {
2547 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002548 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002549 default:
2550 return -EOPNOTSUPP;
2551 }
2552}
2553
2554static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2555{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002556 char stat_string[ETH_GSTRING_LEN];
2557 struct macb *bp = netdev_priv(dev);
2558 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002559 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002560 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002561
2562 switch (sset) {
2563 case ETH_SS_STATS:
2564 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2565 memcpy(p, gem_statistics[i].stat_string,
2566 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002567
2568 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2569 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2570 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2571 q, queue_statistics[i].stat_string);
2572 memcpy(p, stat_string, ETH_GSTRING_LEN);
2573 }
2574 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002575 break;
2576 }
2577}
2578
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002579static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002580{
2581 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002582 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002583 struct macb_stats *hwstat = &bp->hw_stats.macb;
2584
2585 if (macb_is_gem(bp))
2586 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002587
frederic RODO6c36a702007-07-12 19:07:24 +02002588 /* read stats from hardware */
2589 macb_update_stats(bp);
2590
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002591 /* Convert HW stats into netdevice stats */
2592 nstat->rx_errors = (hwstat->rx_fcs_errors +
2593 hwstat->rx_align_errors +
2594 hwstat->rx_resource_errors +
2595 hwstat->rx_overruns +
2596 hwstat->rx_oversize_pkts +
2597 hwstat->rx_jabbers +
2598 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002599 hwstat->rx_length_mismatch);
2600 nstat->tx_errors = (hwstat->tx_late_cols +
2601 hwstat->tx_excessive_cols +
2602 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002603 hwstat->tx_carrier_errors +
2604 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002605 nstat->collisions = (hwstat->tx_single_cols +
2606 hwstat->tx_multiple_cols +
2607 hwstat->tx_excessive_cols);
2608 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2609 hwstat->rx_jabbers +
2610 hwstat->rx_undersize_pkts +
2611 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002612 nstat->rx_over_errors = hwstat->rx_resource_errors +
2613 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002614 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2615 nstat->rx_frame_errors = hwstat->rx_align_errors;
2616 nstat->rx_fifo_errors = hwstat->rx_overruns;
2617 /* XXX: What does "missed" mean? */
2618 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2619 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2620 nstat->tx_fifo_errors = hwstat->tx_underruns;
2621 /* Don't know about heartbeat or window errors... */
2622
2623 return nstat;
2624}
2625
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002626static int macb_get_regs_len(struct net_device *netdev)
2627{
2628 return MACB_GREGS_NBR * sizeof(u32);
2629}
2630
2631static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2632 void *p)
2633{
2634 struct macb *bp = netdev_priv(dev);
2635 unsigned int tail, head;
2636 u32 *regs_buff = p;
2637
2638 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2639 | MACB_GREGS_VERSION;
2640
Zach Brownb410d132016-10-19 09:56:57 -05002641 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2642 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002643
2644 regs_buff[0] = macb_readl(bp, NCR);
2645 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2646 regs_buff[2] = macb_readl(bp, NSR);
2647 regs_buff[3] = macb_readl(bp, TSR);
2648 regs_buff[4] = macb_readl(bp, RBQP);
2649 regs_buff[5] = macb_readl(bp, TBQP);
2650 regs_buff[6] = macb_readl(bp, RSR);
2651 regs_buff[7] = macb_readl(bp, IMR);
2652
2653 regs_buff[8] = tail;
2654 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002655 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2656 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002657
Neil Armstrongce721a72016-01-05 14:39:16 +01002658 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2659 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002660 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002661 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002662}
2663
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002664static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2665{
2666 struct macb *bp = netdev_priv(netdev);
2667
2668 wol->supported = 0;
2669 wol->wolopts = 0;
2670
2671 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2672 wol->supported = WAKE_MAGIC;
2673
2674 if (bp->wol & MACB_WOL_ENABLED)
2675 wol->wolopts |= WAKE_MAGIC;
2676 }
2677}
2678
2679static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2680{
2681 struct macb *bp = netdev_priv(netdev);
2682
2683 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2684 (wol->wolopts & ~WAKE_MAGIC))
2685 return -EOPNOTSUPP;
2686
2687 if (wol->wolopts & WAKE_MAGIC)
2688 bp->wol |= MACB_WOL_ENABLED;
2689 else
2690 bp->wol &= ~MACB_WOL_ENABLED;
2691
2692 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2693
2694 return 0;
2695}
2696
Zach Brown8441bb32016-10-19 09:56:58 -05002697static void macb_get_ringparam(struct net_device *netdev,
2698 struct ethtool_ringparam *ring)
2699{
2700 struct macb *bp = netdev_priv(netdev);
2701
2702 ring->rx_max_pending = MAX_RX_RING_SIZE;
2703 ring->tx_max_pending = MAX_TX_RING_SIZE;
2704
2705 ring->rx_pending = bp->rx_ring_size;
2706 ring->tx_pending = bp->tx_ring_size;
2707}
2708
2709static int macb_set_ringparam(struct net_device *netdev,
2710 struct ethtool_ringparam *ring)
2711{
2712 struct macb *bp = netdev_priv(netdev);
2713 u32 new_rx_size, new_tx_size;
2714 unsigned int reset = 0;
2715
2716 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2717 return -EINVAL;
2718
2719 new_rx_size = clamp_t(u32, ring->rx_pending,
2720 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2721 new_rx_size = roundup_pow_of_two(new_rx_size);
2722
2723 new_tx_size = clamp_t(u32, ring->tx_pending,
2724 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2725 new_tx_size = roundup_pow_of_two(new_tx_size);
2726
2727 if ((new_tx_size == bp->tx_ring_size) &&
2728 (new_rx_size == bp->rx_ring_size)) {
2729 /* nothing to do */
2730 return 0;
2731 }
2732
2733 if (netif_running(bp->dev)) {
2734 reset = 1;
2735 macb_close(bp->dev);
2736 }
2737
2738 bp->rx_ring_size = new_rx_size;
2739 bp->tx_ring_size = new_tx_size;
2740
2741 if (reset)
2742 macb_open(bp->dev);
2743
2744 return 0;
2745}
2746
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002747#ifdef CONFIG_MACB_USE_HWSTAMP
2748static unsigned int gem_get_tsu_rate(struct macb *bp)
2749{
2750 struct clk *tsu_clk;
2751 unsigned int tsu_rate;
2752
2753 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2754 if (!IS_ERR(tsu_clk))
2755 tsu_rate = clk_get_rate(tsu_clk);
2756 /* try pclk instead */
2757 else if (!IS_ERR(bp->pclk)) {
2758 tsu_clk = bp->pclk;
2759 tsu_rate = clk_get_rate(tsu_clk);
2760 } else
2761 return -ENOTSUPP;
2762 return tsu_rate;
2763}
2764
2765static s32 gem_get_ptp_max_adj(void)
2766{
2767 return 64000000;
2768}
2769
2770static int gem_get_ts_info(struct net_device *dev,
2771 struct ethtool_ts_info *info)
2772{
2773 struct macb *bp = netdev_priv(dev);
2774
2775 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2776 ethtool_op_get_ts_info(dev, info);
2777 return 0;
2778 }
2779
2780 info->so_timestamping =
2781 SOF_TIMESTAMPING_TX_SOFTWARE |
2782 SOF_TIMESTAMPING_RX_SOFTWARE |
2783 SOF_TIMESTAMPING_SOFTWARE |
2784 SOF_TIMESTAMPING_TX_HARDWARE |
2785 SOF_TIMESTAMPING_RX_HARDWARE |
2786 SOF_TIMESTAMPING_RAW_HARDWARE;
2787 info->tx_types =
2788 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2789 (1 << HWTSTAMP_TX_OFF) |
2790 (1 << HWTSTAMP_TX_ON);
2791 info->rx_filters =
2792 (1 << HWTSTAMP_FILTER_NONE) |
2793 (1 << HWTSTAMP_FILTER_ALL);
2794
2795 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2796
2797 return 0;
2798}
2799
2800static struct macb_ptp_info gem_ptp_info = {
2801 .ptp_init = gem_ptp_init,
2802 .ptp_remove = gem_ptp_remove,
2803 .get_ptp_max_adj = gem_get_ptp_max_adj,
2804 .get_tsu_rate = gem_get_tsu_rate,
2805 .get_ts_info = gem_get_ts_info,
2806 .get_hwtst = gem_get_hwtst,
2807 .set_hwtst = gem_set_hwtst,
2808};
2809#endif
2810
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002811static int macb_get_ts_info(struct net_device *netdev,
2812 struct ethtool_ts_info *info)
2813{
2814 struct macb *bp = netdev_priv(netdev);
2815
2816 if (bp->ptp_info)
2817 return bp->ptp_info->get_ts_info(netdev, info);
2818
2819 return ethtool_op_get_ts_info(netdev, info);
2820}
2821
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002822static void gem_enable_flow_filters(struct macb *bp, bool enable)
2823{
2824 struct ethtool_rx_fs_item *item;
2825 u32 t2_scr;
2826 int num_t2_scr;
2827
2828 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2829
2830 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2831 struct ethtool_rx_flow_spec *fs = &item->fs;
2832 struct ethtool_tcpip4_spec *tp4sp_m;
2833
2834 if (fs->location >= num_t2_scr)
2835 continue;
2836
2837 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2838
2839 /* enable/disable screener regs for the flow entry */
2840 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2841
2842 /* only enable fields with no masking */
2843 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2844
2845 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2846 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2847 else
2848 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2849
2850 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2851 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2852 else
2853 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2854
2855 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2856 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2857 else
2858 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2859
2860 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2861 }
2862}
2863
2864static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2865{
2866 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2867 uint16_t index = fs->location;
2868 u32 w0, w1, t2_scr;
2869 bool cmp_a = false;
2870 bool cmp_b = false;
2871 bool cmp_c = false;
2872
2873 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2874 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2875
2876 /* ignore field if any masking set */
2877 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2878 /* 1st compare reg - IP source address */
2879 w0 = 0;
2880 w1 = 0;
2881 w0 = tp4sp_v->ip4src;
2882 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2883 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2884 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2885 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2886 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2887 cmp_a = true;
2888 }
2889
2890 /* ignore field if any masking set */
2891 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2892 /* 2nd compare reg - IP destination address */
2893 w0 = 0;
2894 w1 = 0;
2895 w0 = tp4sp_v->ip4dst;
2896 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2897 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2898 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2899 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2900 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2901 cmp_b = true;
2902 }
2903
2904 /* ignore both port fields if masking set in both */
2905 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2906 /* 3rd compare reg - source port, destination port */
2907 w0 = 0;
2908 w1 = 0;
2909 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2910 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2911 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2912 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2913 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2914 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2915 } else {
2916 /* only one port definition */
2917 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2918 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2919 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2920 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2921 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2922 } else { /* dst port */
2923 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2924 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2925 }
2926 }
2927 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2928 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2929 cmp_c = true;
2930 }
2931
2932 t2_scr = 0;
2933 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2934 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2935 if (cmp_a)
2936 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2937 if (cmp_b)
2938 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2939 if (cmp_c)
2940 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2941 gem_writel_n(bp, SCRT2, index, t2_scr);
2942}
2943
2944static int gem_add_flow_filter(struct net_device *netdev,
2945 struct ethtool_rxnfc *cmd)
2946{
2947 struct macb *bp = netdev_priv(netdev);
2948 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2949 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002950 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002951 int ret = -EINVAL;
2952 bool added = false;
2953
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002954 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002955 if (newfs == NULL)
2956 return -ENOMEM;
2957 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2958
2959 netdev_dbg(netdev,
2960 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2961 fs->flow_type, (int)fs->ring_cookie, fs->location,
2962 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2963 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2964 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2965
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002966 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2967
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002968 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002969 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2970 if (item->fs.location > newfs->fs.location) {
2971 list_add_tail(&newfs->list, &item->list);
2972 added = true;
2973 break;
2974 } else if (item->fs.location == fs->location) {
2975 netdev_err(netdev, "Rule not added: location %d not free!\n",
2976 fs->location);
2977 ret = -EBUSY;
2978 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002979 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002980 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002981 if (!added)
2982 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002983
2984 gem_prog_cmp_regs(bp, fs);
2985 bp->rx_fs_list.count++;
2986 /* enable filtering if NTUPLE on */
2987 if (netdev->features & NETIF_F_NTUPLE)
2988 gem_enable_flow_filters(bp, 1);
2989
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002990 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002991 return 0;
2992
2993err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002994 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002995 kfree(newfs);
2996 return ret;
2997}
2998
2999static int gem_del_flow_filter(struct net_device *netdev,
3000 struct ethtool_rxnfc *cmd)
3001{
3002 struct macb *bp = netdev_priv(netdev);
3003 struct ethtool_rx_fs_item *item;
3004 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003005 unsigned long flags;
3006
3007 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003008
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003009 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3010 if (item->fs.location == cmd->fs.location) {
3011 /* disable screener regs for the flow entry */
3012 fs = &(item->fs);
3013 netdev_dbg(netdev,
3014 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3015 fs->flow_type, (int)fs->ring_cookie, fs->location,
3016 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3017 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3018 htons(fs->h_u.tcp_ip4_spec.psrc),
3019 htons(fs->h_u.tcp_ip4_spec.pdst));
3020
3021 gem_writel_n(bp, SCRT2, fs->location, 0);
3022
3023 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003024 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003025 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3026 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003027 return 0;
3028 }
3029 }
3030
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003031 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003032 return -EINVAL;
3033}
3034
3035static int gem_get_flow_entry(struct net_device *netdev,
3036 struct ethtool_rxnfc *cmd)
3037{
3038 struct macb *bp = netdev_priv(netdev);
3039 struct ethtool_rx_fs_item *item;
3040
3041 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3042 if (item->fs.location == cmd->fs.location) {
3043 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3044 return 0;
3045 }
3046 }
3047 return -EINVAL;
3048}
3049
3050static int gem_get_all_flow_entries(struct net_device *netdev,
3051 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3052{
3053 struct macb *bp = netdev_priv(netdev);
3054 struct ethtool_rx_fs_item *item;
3055 uint32_t cnt = 0;
3056
3057 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3058 if (cnt == cmd->rule_cnt)
3059 return -EMSGSIZE;
3060 rule_locs[cnt] = item->fs.location;
3061 cnt++;
3062 }
3063 cmd->data = bp->max_tuples;
3064 cmd->rule_cnt = cnt;
3065
3066 return 0;
3067}
3068
3069static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3070 u32 *rule_locs)
3071{
3072 struct macb *bp = netdev_priv(netdev);
3073 int ret = 0;
3074
3075 switch (cmd->cmd) {
3076 case ETHTOOL_GRXRINGS:
3077 cmd->data = bp->num_queues;
3078 break;
3079 case ETHTOOL_GRXCLSRLCNT:
3080 cmd->rule_cnt = bp->rx_fs_list.count;
3081 break;
3082 case ETHTOOL_GRXCLSRULE:
3083 ret = gem_get_flow_entry(netdev, cmd);
3084 break;
3085 case ETHTOOL_GRXCLSRLALL:
3086 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3087 break;
3088 default:
3089 netdev_err(netdev,
3090 "Command parameter %d is not supported\n", cmd->cmd);
3091 ret = -EOPNOTSUPP;
3092 }
3093
3094 return ret;
3095}
3096
3097static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3098{
3099 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003100 int ret;
3101
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003102 switch (cmd->cmd) {
3103 case ETHTOOL_SRXCLSRLINS:
3104 if ((cmd->fs.location >= bp->max_tuples)
3105 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3106 ret = -EINVAL;
3107 break;
3108 }
3109 ret = gem_add_flow_filter(netdev, cmd);
3110 break;
3111 case ETHTOOL_SRXCLSRLDEL:
3112 ret = gem_del_flow_filter(netdev, cmd);
3113 break;
3114 default:
3115 netdev_err(netdev,
3116 "Command parameter %d is not supported\n", cmd->cmd);
3117 ret = -EOPNOTSUPP;
3118 }
3119
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003120 return ret;
3121}
3122
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003123static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003124 .get_regs_len = macb_get_regs_len,
3125 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003126 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003127 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003128 .get_wol = macb_get_wol,
3129 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003130 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3131 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003132 .get_ringparam = macb_get_ringparam,
3133 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003134};
Xander Huff8cd5a562015-01-15 15:55:20 -06003135
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003136static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003137 .get_regs_len = macb_get_regs_len,
3138 .get_regs = macb_get_regs,
3139 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003140 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003141 .get_ethtool_stats = gem_get_ethtool_stats,
3142 .get_strings = gem_get_ethtool_strings,
3143 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003144 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3145 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003146 .get_ringparam = macb_get_ringparam,
3147 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003148 .get_rxnfc = gem_get_rxnfc,
3149 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003150};
3151
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003152static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003153{
Philippe Reynes0a912812016-06-22 00:32:35 +02003154 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003155 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003156
3157 if (!netif_running(dev))
3158 return -EINVAL;
3159
frederic RODO6c36a702007-07-12 19:07:24 +02003160 if (!phydev)
3161 return -ENODEV;
3162
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003163 if (!bp->ptp_info)
3164 return phy_mii_ioctl(phydev, rq, cmd);
3165
3166 switch (cmd) {
3167 case SIOCSHWTSTAMP:
3168 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3169 case SIOCGHWTSTAMP:
3170 return bp->ptp_info->get_hwtst(dev, rq);
3171 default:
3172 return phy_mii_ioctl(phydev, rq, cmd);
3173 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003174}
3175
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003176static int macb_set_features(struct net_device *netdev,
3177 netdev_features_t features)
3178{
3179 struct macb *bp = netdev_priv(netdev);
3180 netdev_features_t changed = features ^ netdev->features;
3181
3182 /* TX checksum offload */
3183 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3184 u32 dmacfg;
3185
3186 dmacfg = gem_readl(bp, DMACFG);
3187 if (features & NETIF_F_HW_CSUM)
3188 dmacfg |= GEM_BIT(TXCOEN);
3189 else
3190 dmacfg &= ~GEM_BIT(TXCOEN);
3191 gem_writel(bp, DMACFG, dmacfg);
3192 }
3193
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003194 /* RX checksum offload */
3195 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3196 u32 netcfg;
3197
3198 netcfg = gem_readl(bp, NCFGR);
3199 if (features & NETIF_F_RXCSUM &&
3200 !(netdev->flags & IFF_PROMISC))
3201 netcfg |= GEM_BIT(RXCOEN);
3202 else
3203 netcfg &= ~GEM_BIT(RXCOEN);
3204 gem_writel(bp, NCFGR, netcfg);
3205 }
3206
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003207 /* RX Flow Filters */
3208 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3209 bool turn_on = features & NETIF_F_NTUPLE;
3210
3211 gem_enable_flow_filters(bp, turn_on);
3212 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003213 return 0;
3214}
3215
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003216static const struct net_device_ops macb_netdev_ops = {
3217 .ndo_open = macb_open,
3218 .ndo_stop = macb_close,
3219 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003220 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003221 .ndo_get_stats = macb_get_stats,
3222 .ndo_do_ioctl = macb_ioctl,
3223 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303224 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003225 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003226#ifdef CONFIG_NET_POLL_CONTROLLER
3227 .ndo_poll_controller = macb_poll_controller,
3228#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003229 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003230 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003231};
3232
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003233/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003234 * and integration options used
3235 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003236static void macb_configure_caps(struct macb *bp,
3237 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003238{
3239 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003240
Nicolas Ferref6970502015-03-31 15:02:01 +02003241 if (dt_conf)
3242 bp->caps = dt_conf->caps;
3243
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003244 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003245 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3246
Nicolas Ferree1755872014-07-24 13:50:58 +02003247 dcfg = gem_readl(bp, DCFG1);
3248 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3249 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3250 dcfg = gem_readl(bp, DCFG2);
3251 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3252 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003253#ifdef CONFIG_MACB_USE_HWSTAMP
3254 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003255 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3256 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003257 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003258 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003259 bp->ptp_info = &gem_ptp_info;
3260 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003261 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003262#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003263 }
3264
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003265 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003266}
3267
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003268static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003269 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003270 unsigned int *queue_mask,
3271 unsigned int *num_queues)
3272{
3273 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003274
3275 *queue_mask = 0x1;
3276 *num_queues = 1;
3277
Nicolas Ferreda120112015-03-31 15:02:00 +02003278 /* is it macb or gem ?
3279 *
3280 * We need to read directly from the hardware here because
3281 * we are early in the probe process and don't have the
3282 * MACB_CAPS_MACB_IS_GEM flag positioned
3283 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003284 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003285 return;
3286
3287 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303288 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3289
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003290 *queue_mask |= 0x1;
3291
3292 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3293 if (*queue_mask & (1 << hw_q))
3294 (*num_queues)++;
3295}
3296
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003297static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303298 struct clk **hclk, struct clk **tx_clk,
3299 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003300{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003301 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003302 int err;
3303
Bartosz Folta83a77e92016-12-14 06:39:15 +00003304 pdata = dev_get_platdata(&pdev->dev);
3305 if (pdata) {
3306 *pclk = pdata->pclk;
3307 *hclk = pdata->hclk;
3308 } else {
3309 *pclk = devm_clk_get(&pdev->dev, "pclk");
3310 *hclk = devm_clk_get(&pdev->dev, "hclk");
3311 }
3312
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003313 if (IS_ERR(*pclk)) {
3314 err = PTR_ERR(*pclk);
3315 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3316 return err;
3317 }
3318
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003319 if (IS_ERR(*hclk)) {
3320 err = PTR_ERR(*hclk);
3321 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3322 return err;
3323 }
3324
3325 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3326 if (IS_ERR(*tx_clk))
3327 *tx_clk = NULL;
3328
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303329 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3330 if (IS_ERR(*rx_clk))
3331 *rx_clk = NULL;
3332
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003333 err = clk_prepare_enable(*pclk);
3334 if (err) {
3335 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3336 return err;
3337 }
3338
3339 err = clk_prepare_enable(*hclk);
3340 if (err) {
3341 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3342 goto err_disable_pclk;
3343 }
3344
3345 err = clk_prepare_enable(*tx_clk);
3346 if (err) {
3347 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3348 goto err_disable_hclk;
3349 }
3350
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303351 err = clk_prepare_enable(*rx_clk);
3352 if (err) {
3353 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3354 goto err_disable_txclk;
3355 }
3356
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003357 return 0;
3358
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303359err_disable_txclk:
3360 clk_disable_unprepare(*tx_clk);
3361
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003362err_disable_hclk:
3363 clk_disable_unprepare(*hclk);
3364
3365err_disable_pclk:
3366 clk_disable_unprepare(*pclk);
3367
3368 return err;
3369}
3370
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003371static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003372{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003373 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003374 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003375 struct macb *bp = netdev_priv(dev);
3376 struct macb_queue *queue;
3377 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003378 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003379
Zach Brownb410d132016-10-19 09:56:57 -05003380 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3381 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3382
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003383 /* set the queue register mapping once for all: queue0 has a special
3384 * register mapping but we don't want to test the queue index then
3385 * compute the corresponding register offset at run time.
3386 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003387 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003388 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003389 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003390
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003391 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003392 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003393 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003394 if (hw_q) {
3395 queue->ISR = GEM_ISR(hw_q - 1);
3396 queue->IER = GEM_IER(hw_q - 1);
3397 queue->IDR = GEM_IDR(hw_q - 1);
3398 queue->IMR = GEM_IMR(hw_q - 1);
3399 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003400 queue->RBQP = GEM_RBQP(hw_q - 1);
3401 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303402#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003403 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003404 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003405 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3406 }
Harini Katakamfff80192016-08-09 13:15:53 +05303407#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003408 } else {
3409 /* queue0 uses legacy registers */
3410 queue->ISR = MACB_ISR;
3411 queue->IER = MACB_IER;
3412 queue->IDR = MACB_IDR;
3413 queue->IMR = MACB_IMR;
3414 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003415 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303416#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003417 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003418 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003419 queue->RBQPH = MACB_RBQPH;
3420 }
Harini Katakamfff80192016-08-09 13:15:53 +05303421#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003422 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003423
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003424 /* get irq: here we use the linux queue index, not the hardware
3425 * queue index. the queue irq definitions in the device tree
3426 * must remove the optional gaps that could exist in the
3427 * hardware queue mask.
3428 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003429 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003430 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003431 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003432 if (err) {
3433 dev_err(&pdev->dev,
3434 "Unable to request IRQ %d (error %d)\n",
3435 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003436 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003437 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003438
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003439 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003440 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003441 }
3442
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003443 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003444
Nicolas Ferre4df95132013-06-04 21:57:12 +00003445 /* setup appropriated routines according to adapter type */
3446 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003447 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003448 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3449 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3450 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3451 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003452 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003453 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003454 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003455 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3456 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3457 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3458 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003459 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003460 }
3461
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003462 /* Set features */
3463 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003464
3465 /* Check LSO capability */
3466 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3467 dev->hw_features |= MACB_NETIF_LSO;
3468
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003469 /* Checksum offload is only available on gem with packet buffer */
3470 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003471 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003472 if (bp->caps & MACB_CAPS_SG_DISABLED)
3473 dev->hw_features &= ~NETIF_F_SG;
3474 dev->features = dev->hw_features;
3475
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003476 /* Check RX Flow Filters support.
3477 * Max Rx flows set by availability of screeners & compare regs:
3478 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3479 */
3480 reg = gem_readl(bp, DCFG8);
3481 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3482 GEM_BFEXT(T2SCR, reg));
3483 if (bp->max_tuples > 0) {
3484 /* also needs one ethtype match to check IPv4 */
3485 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3486 /* program this reg now */
3487 reg = 0;
3488 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3489 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3490 /* Filtering is supported in hw but don't enable it in kernel now */
3491 dev->hw_features |= NETIF_F_NTUPLE;
3492 /* init Rx flow definitions */
3493 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3494 bp->rx_fs_list.count = 0;
3495 spin_lock_init(&bp->rx_fs_lock);
3496 } else
3497 bp->max_tuples = 0;
3498 }
3499
Neil Armstrongce721a72016-01-05 14:39:16 +01003500 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3501 val = 0;
3502 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3503 val = GEM_BIT(RGMII);
3504 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003505 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003506 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003507 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003508 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003509
Neil Armstrongce721a72016-01-05 14:39:16 +01003510 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3511 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003512
Neil Armstrongce721a72016-01-05 14:39:16 +01003513 macb_or_gem_writel(bp, USRIO, val);
3514 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003515
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003516 /* Set MII management clock divider */
3517 val = macb_mdc_clk_div(bp);
3518 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303519 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3520 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003521 macb_writel(bp, NCFGR, val);
3522
3523 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003524}
3525
3526#if defined(CONFIG_OF)
3527/* 1518 rounded up */
3528#define AT91ETHER_MAX_RBUFF_SZ 0x600
3529/* max number of receive buffers */
3530#define AT91ETHER_MAX_RX_DESCR 9
3531
3532/* Initialize and start the Receiver and Transmit subsystems */
3533static int at91ether_start(struct net_device *dev)
3534{
3535 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003536 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003537 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003538 dma_addr_t addr;
3539 u32 ctl;
3540 int i;
3541
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003542 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003543 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003544 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003545 &q->rx_ring_dma, GFP_KERNEL);
3546 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003547 return -ENOMEM;
3548
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003549 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003550 AT91ETHER_MAX_RX_DESCR *
3551 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003552 &q->rx_buffers_dma, GFP_KERNEL);
3553 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003554 dma_free_coherent(&lp->pdev->dev,
3555 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003556 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003557 q->rx_ring, q->rx_ring_dma);
3558 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003559 return -ENOMEM;
3560 }
3561
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003562 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003563 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003564 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003565 macb_set_addr(lp, desc, addr);
3566 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003567 addr += AT91ETHER_MAX_RBUFF_SZ;
3568 }
3569
3570 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003571 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003572
3573 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003574 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003575
3576 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003577 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003578
3579 /* Enable Receive and Transmit */
3580 ctl = macb_readl(lp, NCR);
3581 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3582
3583 return 0;
3584}
3585
3586/* Open the ethernet interface */
3587static int at91ether_open(struct net_device *dev)
3588{
3589 struct macb *lp = netdev_priv(dev);
3590 u32 ctl;
3591 int ret;
3592
3593 /* Clear internal statistics */
3594 ctl = macb_readl(lp, NCR);
3595 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3596
3597 macb_set_hwaddr(lp);
3598
3599 ret = at91ether_start(dev);
3600 if (ret)
3601 return ret;
3602
3603 /* Enable MAC interrupts */
3604 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3605 MACB_BIT(RXUBR) |
3606 MACB_BIT(ISR_TUND) |
3607 MACB_BIT(ISR_RLE) |
3608 MACB_BIT(TCOMP) |
3609 MACB_BIT(ISR_ROVR) |
3610 MACB_BIT(HRESP));
3611
3612 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003613 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003614
3615 netif_start_queue(dev);
3616
3617 return 0;
3618}
3619
3620/* Close the interface */
3621static int at91ether_close(struct net_device *dev)
3622{
3623 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003624 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003625 u32 ctl;
3626
3627 /* Disable Receiver and Transmitter */
3628 ctl = macb_readl(lp, NCR);
3629 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3630
3631 /* Disable MAC interrupts */
3632 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3633 MACB_BIT(RXUBR) |
3634 MACB_BIT(ISR_TUND) |
3635 MACB_BIT(ISR_RLE) |
3636 MACB_BIT(TCOMP) |
3637 MACB_BIT(ISR_ROVR) |
3638 MACB_BIT(HRESP));
3639
3640 netif_stop_queue(dev);
3641
3642 dma_free_coherent(&lp->pdev->dev,
3643 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003644 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003645 q->rx_ring, q->rx_ring_dma);
3646 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003647
3648 dma_free_coherent(&lp->pdev->dev,
3649 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003650 q->rx_buffers, q->rx_buffers_dma);
3651 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003652
3653 return 0;
3654}
3655
3656/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003657static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3658 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003659{
3660 struct macb *lp = netdev_priv(dev);
3661
3662 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3663 netif_stop_queue(dev);
3664
3665 /* Store packet information (to free when Tx completed) */
3666 lp->skb = skb;
3667 lp->skb_length = skb->len;
3668 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3669 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003670 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3671 dev_kfree_skb_any(skb);
3672 dev->stats.tx_dropped++;
3673 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3674 return NETDEV_TX_OK;
3675 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003676
3677 /* Set address of the data in the Transmit Address register */
3678 macb_writel(lp, TAR, lp->skb_physaddr);
3679 /* Set length of the packet in the Transmit Control register */
3680 macb_writel(lp, TCR, skb->len);
3681
3682 } else {
3683 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3684 return NETDEV_TX_BUSY;
3685 }
3686
3687 return NETDEV_TX_OK;
3688}
3689
3690/* Extract received frame from buffer descriptors and sent to upper layers.
3691 * (Called from interrupt context)
3692 */
3693static void at91ether_rx(struct net_device *dev)
3694{
3695 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003696 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003697 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003698 unsigned char *p_recv;
3699 struct sk_buff *skb;
3700 unsigned int pktlen;
3701
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003702 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003703 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003704 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003705 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003706 skb = netdev_alloc_skb(dev, pktlen + 2);
3707 if (skb) {
3708 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003709 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003710
3711 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003712 dev->stats.rx_packets++;
3713 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003714 netif_rx(skb);
3715 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003716 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003717 }
3718
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003719 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003720 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003721
3722 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003723 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003724
3725 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003726 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3727 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003728 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003729 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003730
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003731 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003732 }
3733}
3734
3735/* MAC interrupt handler */
3736static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3737{
3738 struct net_device *dev = dev_id;
3739 struct macb *lp = netdev_priv(dev);
3740 u32 intstatus, ctl;
3741
3742 /* MAC Interrupt Status register indicates what interrupts are pending.
3743 * It is automatically cleared once read.
3744 */
3745 intstatus = macb_readl(lp, ISR);
3746
3747 /* Receive complete */
3748 if (intstatus & MACB_BIT(RCOMP))
3749 at91ether_rx(dev);
3750
3751 /* Transmit complete */
3752 if (intstatus & MACB_BIT(TCOMP)) {
3753 /* The TCOM bit is set even if the transmission failed */
3754 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003755 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003756
3757 if (lp->skb) {
3758 dev_kfree_skb_irq(lp->skb);
3759 lp->skb = NULL;
3760 dma_unmap_single(NULL, lp->skb_physaddr,
3761 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003762 dev->stats.tx_packets++;
3763 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003764 }
3765 netif_wake_queue(dev);
3766 }
3767
3768 /* Work-around for EMAC Errata section 41.3.1 */
3769 if (intstatus & MACB_BIT(RXUBR)) {
3770 ctl = macb_readl(lp, NCR);
3771 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003772 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003773 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3774 }
3775
3776 if (intstatus & MACB_BIT(ISR_ROVR))
3777 netdev_err(dev, "ROVR error\n");
3778
3779 return IRQ_HANDLED;
3780}
3781
3782#ifdef CONFIG_NET_POLL_CONTROLLER
3783static void at91ether_poll_controller(struct net_device *dev)
3784{
3785 unsigned long flags;
3786
3787 local_irq_save(flags);
3788 at91ether_interrupt(dev->irq, dev);
3789 local_irq_restore(flags);
3790}
3791#endif
3792
3793static const struct net_device_ops at91ether_netdev_ops = {
3794 .ndo_open = at91ether_open,
3795 .ndo_stop = at91ether_close,
3796 .ndo_start_xmit = at91ether_start_xmit,
3797 .ndo_get_stats = macb_get_stats,
3798 .ndo_set_rx_mode = macb_set_rx_mode,
3799 .ndo_set_mac_address = eth_mac_addr,
3800 .ndo_do_ioctl = macb_ioctl,
3801 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003802#ifdef CONFIG_NET_POLL_CONTROLLER
3803 .ndo_poll_controller = at91ether_poll_controller,
3804#endif
3805};
3806
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003807static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303808 struct clk **hclk, struct clk **tx_clk,
3809 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003810{
3811 int err;
3812
3813 *hclk = NULL;
3814 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303815 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003816
3817 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3818 if (IS_ERR(*pclk))
3819 return PTR_ERR(*pclk);
3820
3821 err = clk_prepare_enable(*pclk);
3822 if (err) {
3823 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3824 return err;
3825 }
3826
3827 return 0;
3828}
3829
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003830static int at91ether_init(struct platform_device *pdev)
3831{
3832 struct net_device *dev = platform_get_drvdata(pdev);
3833 struct macb *bp = netdev_priv(dev);
3834 int err;
3835 u32 reg;
3836
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003837 bp->queues[0].bp = bp;
3838
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003839 dev->netdev_ops = &at91ether_netdev_ops;
3840 dev->ethtool_ops = &macb_ethtool_ops;
3841
3842 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3843 0, dev->name, dev);
3844 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003845 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003846
3847 macb_writel(bp, NCR, 0);
3848
3849 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3850 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3851 reg |= MACB_BIT(RM9200_RMII);
3852
3853 macb_writel(bp, NCFGR, reg);
3854
3855 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003856}
3857
David S. Miller3cef5c52015-03-09 23:38:02 -04003858static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003859 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003860 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003861 .init = macb_init,
3862};
3863
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003864static const struct macb_config sama5d3macb_config = {
3865 .caps = MACB_CAPS_SG_DISABLED
3866 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3867 .clk_init = macb_clk_init,
3868 .init = macb_init,
3869};
3870
David S. Miller3cef5c52015-03-09 23:38:02 -04003871static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003872 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3873 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003874 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003875 .init = macb_init,
3876};
3877
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003878static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003879 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003880 .dma_burst_length = 16,
3881 .clk_init = macb_clk_init,
3882 .init = macb_init,
3883};
3884
David S. Miller3cef5c52015-03-09 23:38:02 -04003885static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003886 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003887 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003888 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003889 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003890 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003891 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003892};
3893
David S. Miller3cef5c52015-03-09 23:38:02 -04003894static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003895 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003896 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003897 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003898 .init = macb_init,
3899};
3900
David S. Miller3cef5c52015-03-09 23:38:02 -04003901static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003902 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003903 .init = at91ether_init,
3904};
3905
Neil Armstronge611b5b2016-01-05 14:39:17 +01003906static const struct macb_config np4_config = {
3907 .caps = MACB_CAPS_USRIO_DISABLED,
3908 .clk_init = macb_clk_init,
3909 .init = macb_init,
3910};
David S. Miller36583eb2015-05-23 01:22:35 -04003911
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303912static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003913 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3914 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05303915 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303916 .dma_burst_length = 16,
3917 .clk_init = macb_clk_init,
3918 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303919 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303920};
3921
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003922static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303923 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003924 .dma_burst_length = 16,
3925 .clk_init = macb_clk_init,
3926 .init = macb_init,
3927};
3928
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003929static const struct of_device_id macb_dt_ids[] = {
3930 { .compatible = "cdns,at32ap7000-macb" },
3931 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3932 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003933 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003934 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3935 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003936 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003937 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003938 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003939 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3940 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3941 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303942 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003943 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003944 { /* sentinel */ }
3945};
3946MODULE_DEVICE_TABLE(of, macb_dt_ids);
3947#endif /* CONFIG_OF */
3948
Bartosz Folta83a77e92016-12-14 06:39:15 +00003949static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003950 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3951 MACB_CAPS_JUMBO |
3952 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003953 .dma_burst_length = 16,
3954 .clk_init = macb_clk_init,
3955 .init = macb_init,
3956 .jumbo_max_len = 10240,
3957};
3958
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003959static int macb_probe(struct platform_device *pdev)
3960{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003961 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003962 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303963 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003964 = macb_config->clk_init;
3965 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003966 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303967 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003968 unsigned int queue_mask, num_queues;
3969 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003970 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003971 struct phy_device *phydev;
3972 struct net_device *dev;
3973 struct resource *regs;
3974 void __iomem *mem;
3975 const char *mac;
3976 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05303977 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003978
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003979 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3980 mem = devm_ioremap_resource(&pdev->dev, regs);
3981 if (IS_ERR(mem))
3982 return PTR_ERR(mem);
3983
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003984 if (np) {
3985 const struct of_device_id *match;
3986
3987 match = of_match_node(macb_dt_ids, np);
3988 if (match && match->data) {
3989 macb_config = match->data;
3990 clk_init = macb_config->clk_init;
3991 init = macb_config->init;
3992 }
3993 }
3994
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303995 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003996 if (err)
3997 return err;
3998
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003999 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004000
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004001 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004002 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004003 if (!dev) {
4004 err = -ENOMEM;
4005 goto err_disable_clocks;
4006 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004007
4008 dev->base_addr = regs->start;
4009
4010 SET_NETDEV_DEV(dev, &pdev->dev);
4011
4012 bp = netdev_priv(dev);
4013 bp->pdev = pdev;
4014 bp->dev = dev;
4015 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004016 bp->native_io = native_io;
4017 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004018 bp->macb_reg_readl = hw_readl_native;
4019 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004020 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004021 bp->macb_reg_readl = hw_readl;
4022 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004023 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004024 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004025 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004026 if (macb_config)
4027 bp->dma_burst_length = macb_config->dma_burst_length;
4028 bp->pclk = pclk;
4029 bp->hclk = hclk;
4030 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304031 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004032 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304033 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304034
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004035 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004036 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004037 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4038 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4039
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004040 spin_lock_init(&bp->lock);
4041
Nicolas Ferread783472015-03-31 15:02:02 +02004042 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004043 macb_configure_caps(bp, macb_config);
4044
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004045#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4046 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4047 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4048 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4049 }
4050#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004051 platform_set_drvdata(pdev, dev);
4052
4053 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004054 if (dev->irq < 0) {
4055 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004056 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004057 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004058
Jarod Wilson44770e12016-10-17 15:54:17 -04004059 /* MTU range: 68 - 1500 or 10240 */
4060 dev->min_mtu = GEM_MTU_MIN_SIZE;
4061 if (bp->caps & MACB_CAPS_JUMBO)
4062 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4063 else
4064 dev->max_mtu = ETH_DATA_LEN;
4065
Harini Katakam404cd082018-07-06 12:18:58 +05304066 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4067 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4068 if (val)
4069 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4070 macb_dma_desc_get_size(bp);
4071
4072 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4073 if (val)
4074 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4075 macb_dma_desc_get_size(bp);
4076 }
4077
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004078 mac = of_get_mac_address(np);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004079 if (mac) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004080 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004081 } else {
4082 err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
4083 if (err) {
4084 if (err == -EPROBE_DEFER)
4085 goto err_out_free_netdev;
4086 macb_get_hwaddr(bp);
4087 }
4088 }
frederic RODO6c36a702007-07-12 19:07:24 +02004089
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004090 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004091 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09004092 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004093 if (pdata && pdata->is_rmii)
4094 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4095 else
4096 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4097 } else {
4098 bp->phy_interface = err;
4099 }
4100
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004101 /* IP specific init */
4102 err = init(pdev);
4103 if (err)
4104 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004105
Florian Fainellicf669662016-05-02 18:38:45 -07004106 err = macb_mii_init(bp);
4107 if (err)
4108 goto err_out_free_netdev;
4109
Philippe Reynes0a912812016-06-22 00:32:35 +02004110 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004111
4112 netif_carrier_off(dev);
4113
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004114 err = register_netdev(dev);
4115 if (err) {
4116 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004117 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004118 }
4119
Harini Katakam032dc412018-01-27 12:09:01 +05304120 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4121 (unsigned long)bp);
4122
Florian Fainellicf669662016-05-02 18:38:45 -07004123 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004124
Bo Shen58798232014-09-13 01:57:49 +02004125 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4126 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4127 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004128
4129 return 0;
4130
Florian Fainellicf669662016-05-02 18:38:45 -07004131err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004132 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004133 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004134 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004135 if (np && of_phy_is_fixed_link(np))
4136 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004137 mdiobus_free(bp->mii_bus);
4138
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004139err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004140 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004141
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004142err_disable_clocks:
4143 clk_disable_unprepare(tx_clk);
4144 clk_disable_unprepare(hclk);
4145 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304146 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004147
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004148 return err;
4149}
4150
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004151static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004152{
4153 struct net_device *dev;
4154 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004155 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004156
4157 dev = platform_get_drvdata(pdev);
4158
4159 if (dev) {
4160 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004161 if (dev->phydev)
4162 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004163 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004164 if (np && of_phy_is_fixed_link(np))
4165 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004166 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004167 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004168
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004169 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004170 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004171 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004172 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304173 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004174 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004175 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004176 }
4177
4178 return 0;
4179}
4180
Michal Simekd23823d2015-01-23 09:36:03 +01004181static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004182{
Wolfram Sangce886a42018-10-21 22:00:14 +02004183 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004184 struct macb *bp = netdev_priv(netdev);
4185
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004186 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004187 netif_device_detach(netdev);
4188
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004189 if (bp->wol & MACB_WOL_ENABLED) {
4190 macb_writel(bp, IER, MACB_BIT(WOL));
4191 macb_writel(bp, WOL, MACB_BIT(MAG));
4192 enable_irq_wake(bp->queues[0].irq);
4193 } else {
4194 clk_disable_unprepare(bp->tx_clk);
4195 clk_disable_unprepare(bp->hclk);
4196 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304197 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004198 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004199
4200 return 0;
4201}
4202
Michal Simekd23823d2015-01-23 09:36:03 +01004203static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004204{
Wolfram Sangce886a42018-10-21 22:00:14 +02004205 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004206 struct macb *bp = netdev_priv(netdev);
4207
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004208 if (bp->wol & MACB_WOL_ENABLED) {
4209 macb_writel(bp, IDR, MACB_BIT(WOL));
4210 macb_writel(bp, WOL, 0);
4211 disable_irq_wake(bp->queues[0].irq);
4212 } else {
4213 clk_prepare_enable(bp->pclk);
4214 clk_prepare_enable(bp->hclk);
4215 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304216 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004217 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004218
4219 netif_device_attach(netdev);
4220
4221 return 0;
4222}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004223
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004224static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4225
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004226static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004227 .probe = macb_probe,
4228 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004229 .driver = {
4230 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004231 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004232 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004233 },
4234};
4235
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004236module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004237
4238MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004239MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004240MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004241MODULE_ALIAS("platform:macb");