blob: be10ee4c791fc0880a47a47876f93aaf8b855b42 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030013#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000018#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010019#include <linux/slab.h>
20#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080021#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000022#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010023#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000028#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010029#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020030#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080031#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010033#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020034#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010035#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000036#include <linux/ip.h>
37#include <linux/udp.h>
38#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053039#include <linux/iopoll.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010040#include "macb.h"
41
Nicolas Ferre1b447912013-06-04 21:57:11 +000042#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000043#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050044
Zach Brownb410d132016-10-19 09:56:57 -050045#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050046#define MIN_RX_RING_SIZE 64
47#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000048#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050049 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010050
Zach Brownb410d132016-10-19 09:56:57 -050051#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050052#define MIN_TX_RING_SIZE 64
53#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000054#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050055 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010056
Nicolas Ferre909a8582012-11-19 06:00:21 +000057/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050058#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010059
Harini Katakame5010702019-01-29 15:20:03 +053060#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | 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
Harini Katakam8beb79b2019-03-01 16:20:32 +053083#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
84
Rafal Ozieblodc97a892017-01-27 15:08:20 +000085/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010086 * depends on hardware configuration:
87 *
88 * 1. dma address width 32 bits:
89 * word 1: 32 bit address of Data Buffer
90 * word 2: control
91 *
92 * 2. dma address width 64 bits:
93 * word 1: 32 bit address of Data Buffer
94 * word 2: control
95 * word 3: upper 32 bit address of Data Buffer
96 * word 4: unused
97 *
98 * 3. dma address width 32 bits with hardware timestamping:
99 * word 1: 32 bit address of Data Buffer
100 * word 2: control
101 * word 3: timestamp word 1
102 * word 4: timestamp word 2
103 *
104 * 4. dma address width 64 bits with hardware timestamping:
105 * word 1: 32 bit address of Data Buffer
106 * word 2: control
107 * word 3: upper 32 bit address of Data Buffer
108 * word 4: unused
109 * word 5: timestamp word 1
110 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000111 */
112static unsigned int macb_dma_desc_get_size(struct macb *bp)
113{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100114#ifdef MACB_EXT_DESC
115 unsigned int desc_size;
116
117 switch (bp->hw_dma_cap) {
118 case HW_DMA_CAP_64B:
119 desc_size = sizeof(struct macb_dma_desc)
120 + sizeof(struct macb_dma_desc_64);
121 break;
122 case HW_DMA_CAP_PTP:
123 desc_size = sizeof(struct macb_dma_desc)
124 + sizeof(struct macb_dma_desc_ptp);
125 break;
126 case HW_DMA_CAP_64B_PTP:
127 desc_size = sizeof(struct macb_dma_desc)
128 + sizeof(struct macb_dma_desc_64)
129 + sizeof(struct macb_dma_desc_ptp);
130 break;
131 default:
132 desc_size = sizeof(struct macb_dma_desc);
133 }
134 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000135#endif
136 return sizeof(struct macb_dma_desc);
137}
138
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100139static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000140{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100141#ifdef MACB_EXT_DESC
142 switch (bp->hw_dma_cap) {
143 case HW_DMA_CAP_64B:
144 case HW_DMA_CAP_PTP:
145 desc_idx <<= 1;
146 break;
147 case HW_DMA_CAP_64B_PTP:
148 desc_idx *= 3;
149 break;
150 default:
151 break;
152 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000153#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100154 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000155}
156
157#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
158static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
159{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100160 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
161 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
162 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000163}
164#endif
165
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000166/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500167static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000168{
Zach Brownb410d132016-10-19 09:56:57 -0500169 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000170}
171
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100172static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
173 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000174{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000175 index = macb_tx_ring_wrap(queue->bp, index);
176 index = macb_adj_dma_desc_idx(queue->bp, index);
177 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000178}
179
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100180static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
181 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000182{
Zach Brownb410d132016-10-19 09:56:57 -0500183 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000184}
185
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100186static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000187{
188 dma_addr_t offset;
189
Zach Brownb410d132016-10-19 09:56:57 -0500190 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000191 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000192
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100193 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000194}
195
Zach Brownb410d132016-10-19 09:56:57 -0500196static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000197{
Zach Brownb410d132016-10-19 09:56:57 -0500198 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000199}
200
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000201static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000202{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000203 index = macb_rx_ring_wrap(queue->bp, index);
204 index = macb_adj_dma_desc_idx(queue->bp, index);
205 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000206}
207
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000208static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000209{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000210 return queue->rx_buffers + queue->bp->rx_buffer_size *
211 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000212}
213
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300214/* I/O accessors */
215static u32 hw_readl_native(struct macb *bp, int offset)
216{
217 return __raw_readl(bp->regs + offset);
218}
219
220static void hw_writel_native(struct macb *bp, int offset, u32 value)
221{
222 __raw_writel(value, bp->regs + offset);
223}
224
225static u32 hw_readl(struct macb *bp, int offset)
226{
227 return readl_relaxed(bp->regs + offset);
228}
229
230static void hw_writel(struct macb *bp, int offset, u32 value)
231{
232 writel_relaxed(value, bp->regs + offset);
233}
234
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700235/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700236 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300237 * descriptor access.
238 */
239static bool hw_is_native_io(void __iomem *addr)
240{
241 u32 value = MACB_BIT(LLB);
242
243 __raw_writel(value, addr + MACB_NCR);
244 value = __raw_readl(addr + MACB_NCR);
245
246 /* Write 0 back to disable everything */
247 __raw_writel(0, addr + MACB_NCR);
248
249 return value == MACB_BIT(LLB);
250}
251
252static bool hw_is_gem(void __iomem *addr, bool native_io)
253{
254 u32 id;
255
256 if (native_io)
257 id = __raw_readl(addr + MACB_MID);
258 else
259 id = readl_relaxed(addr + MACB_MID);
260
261 return MACB_BFEXT(IDNUM, id) >= 0x2;
262}
263
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100264static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100265{
266 u32 bottom;
267 u16 top;
268
269 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000270 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100271 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000272 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000273
274 /* Clear unused address register sets */
275 macb_or_gem_writel(bp, SA2B, 0);
276 macb_or_gem_writel(bp, SA2T, 0);
277 macb_or_gem_writel(bp, SA3B, 0);
278 macb_or_gem_writel(bp, SA3T, 0);
279 macb_or_gem_writel(bp, SA4B, 0);
280 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100281}
282
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100283static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000285 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100286 u32 bottom;
287 u16 top;
288 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000289 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100290
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900291 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000292
Moritz Fischeraa50b552016-03-29 19:11:13 -0700293 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000294 for (i = 0; i < 4; i++) {
295 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
296 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000298 if (pdata && pdata->rev_eth_addr) {
299 addr[5] = bottom & 0xff;
300 addr[4] = (bottom >> 8) & 0xff;
301 addr[3] = (bottom >> 16) & 0xff;
302 addr[2] = (bottom >> 24) & 0xff;
303 addr[1] = top & 0xff;
304 addr[0] = (top & 0xff00) >> 8;
305 } else {
306 addr[0] = bottom & 0xff;
307 addr[1] = (bottom >> 8) & 0xff;
308 addr[2] = (bottom >> 16) & 0xff;
309 addr[3] = (bottom >> 24) & 0xff;
310 addr[4] = top & 0xff;
311 addr[5] = (top >> 8) & 0xff;
312 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100313
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000314 if (is_valid_ether_addr(addr)) {
315 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
316 return;
317 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700318 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000319
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300320 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000321 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100322}
323
Harini Katakam8beb79b2019-03-01 16:20:32 +0530324static int macb_mdio_wait_for_idle(struct macb *bp)
325{
326 u32 val;
327
328 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
329 1, MACB_MDIO_TIMEOUT);
330}
331
frederic RODO6c36a702007-07-12 19:07:24 +0200332static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100333{
frederic RODO6c36a702007-07-12 19:07:24 +0200334 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100335 int value;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530336 int err;
337
338 err = macb_mdio_wait_for_idle(bp);
339 if (err < 0)
340 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100341
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
343 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200344 | MACB_BF(PHYA, mii_id)
345 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100346 | MACB_BF(CODE, MACB_MAN_CODE)));
347
Harini Katakam8beb79b2019-03-01 16:20:32 +0530348 err = macb_mdio_wait_for_idle(bp);
349 if (err < 0)
350 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
352 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100353
354 return value;
355}
356
frederic RODO6c36a702007-07-12 19:07:24 +0200357static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
358 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100359{
frederic RODO6c36a702007-07-12 19:07:24 +0200360 struct macb *bp = bus->priv;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530361 int err;
362
363 err = macb_mdio_wait_for_idle(bp);
364 if (err < 0)
365 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100366
367 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
368 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200369 | MACB_BF(PHYA, mii_id)
370 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100371 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200372 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100373
Harini Katakam8beb79b2019-03-01 16:20:32 +0530374 err = macb_mdio_wait_for_idle(bp);
375 if (err < 0)
376 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100377
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100378 return 0;
379}
380
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800381/**
382 * macb_set_tx_clk() - Set a clock to a new frequency
383 * @clk Pointer to the clock to change
384 * @rate New frequency in Hz
385 * @dev Pointer to the struct net_device
386 */
387static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
388{
389 long ferr, rate, rate_rounded;
390
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100391 if (!clk)
392 return;
393
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800394 switch (speed) {
395 case SPEED_10:
396 rate = 2500000;
397 break;
398 case SPEED_100:
399 rate = 25000000;
400 break;
401 case SPEED_1000:
402 rate = 125000000;
403 break;
404 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800405 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800406 }
407
408 rate_rounded = clk_round_rate(clk, rate);
409 if (rate_rounded < 0)
410 return;
411
412 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
413 * is not satisfied.
414 */
415 ferr = abs(rate_rounded - rate);
416 ferr = DIV_ROUND_UP(ferr, rate / 100000);
417 if (ferr > 5)
418 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700419 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800420
421 if (clk_set_rate(clk, rate_rounded))
422 netdev_err(dev, "adjusting tx_clk failed.\n");
423}
424
frederic RODO6c36a702007-07-12 19:07:24 +0200425static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100426{
frederic RODO6c36a702007-07-12 19:07:24 +0200427 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200428 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200429 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200430 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100431
frederic RODO6c36a702007-07-12 19:07:24 +0200432 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100433
frederic RODO6c36a702007-07-12 19:07:24 +0200434 if (phydev->link) {
435 if ((bp->speed != phydev->speed) ||
436 (bp->duplex != phydev->duplex)) {
437 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438
frederic RODO6c36a702007-07-12 19:07:24 +0200439 reg = macb_readl(bp, NCFGR);
440 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000441 if (macb_is_gem(bp))
442 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200443
444 if (phydev->duplex)
445 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900446 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200447 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200448 if (phydev->speed == SPEED_1000 &&
449 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000450 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200451
Patrice Vilchez140b7552012-10-31 06:04:50 +0000452 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200453
454 bp->speed = phydev->speed;
455 bp->duplex = phydev->duplex;
456 status_change = 1;
457 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100458 }
459
frederic RODO6c36a702007-07-12 19:07:24 +0200460 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700461 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200462 bp->speed = 0;
463 bp->duplex = -1;
464 }
465 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100466
frederic RODO6c36a702007-07-12 19:07:24 +0200467 status_change = 1;
468 }
469
470 spin_unlock_irqrestore(&bp->lock, flags);
471
472 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000473 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500474 /* Update the TX clock rate if and only if the link is
475 * up and there has been a link change.
476 */
477 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
478
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000479 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000480 netdev_info(dev, "link up (%d/%s)\n",
481 phydev->speed,
482 phydev->duplex == DUPLEX_FULL ?
483 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000484 } else {
485 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000486 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000487 }
frederic RODO6c36a702007-07-12 19:07:24 +0200488 }
489}
490
491/* based on au1000_eth. c*/
492static int macb_mii_probe(struct net_device *dev)
493{
494 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000495 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000496 struct phy_device *phydev;
Brad Mouring739de9a2018-03-13 16:32:13 -0500497 struct device_node *np;
498 int phy_irq, ret, i;
499
500 pdata = dev_get_platdata(&bp->pdev->dev);
501 np = bp->pdev->dev.of_node;
502 ret = 0;
503
504 if (np) {
505 if (of_phy_is_fixed_link(np)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500506 bp->phy_node = of_node_get(np);
507 } else {
Brad Mouring2105a5d2018-03-13 16:32:15 -0500508 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
509 /* fallback to standard phy registration if no
510 * phy-handle was found nor any phy found during
511 * dt phy registration
Brad Mouring739de9a2018-03-13 16:32:13 -0500512 */
Brad Mouring2105a5d2018-03-13 16:32:15 -0500513 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
Brad Mouring739de9a2018-03-13 16:32:13 -0500514 for (i = 0; i < PHY_MAX_ADDR; i++) {
515 struct phy_device *phydev;
516
517 phydev = mdiobus_scan(bp->mii_bus, i);
518 if (IS_ERR(phydev) &&
519 PTR_ERR(phydev) != -ENODEV) {
520 ret = PTR_ERR(phydev);
521 break;
522 }
523 }
524
525 if (ret)
526 return -ENODEV;
527 }
528 }
529 }
frederic RODO6c36a702007-07-12 19:07:24 +0200530
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200531 if (bp->phy_node) {
532 phydev = of_phy_connect(dev, bp->phy_node,
533 &macb_handle_link_change, 0,
534 bp->phy_interface);
535 if (!phydev)
536 return -ENODEV;
537 } else {
538 phydev = phy_find_first(bp->mii_bus);
539 if (!phydev) {
540 netdev_err(dev, "no PHY found\n");
541 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000542 }
frederic RODO6c36a702007-07-12 19:07:24 +0200543
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200544 if (pdata) {
545 if (gpio_is_valid(pdata->phy_irq_pin)) {
546 ret = devm_gpio_request(&bp->pdev->dev,
547 pdata->phy_irq_pin, "phy int");
548 if (!ret) {
549 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
550 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
551 }
552 } else {
553 phydev->irq = PHY_POLL;
554 }
555 }
556
557 /* attach the mac to the phy */
558 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
559 bp->phy_interface);
560 if (ret) {
561 netdev_err(dev, "Could not attach to PHY\n");
562 return ret;
563 }
frederic RODO6c36a702007-07-12 19:07:24 +0200564 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100565
frederic RODO6c36a702007-07-12 19:07:24 +0200566 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200567 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Andrew Lunn58056c12018-09-12 01:53:11 +0200568 phy_set_max_speed(phydev, SPEED_1000);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000569 else
Andrew Lunn58056c12018-09-12 01:53:11 +0200570 phy_set_max_speed(phydev, SPEED_100);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100571
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500572 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
Andrew Lunn41124fa2018-09-12 01:53:14 +0200573 phy_remove_link_mode(phydev,
574 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100575
frederic RODO6c36a702007-07-12 19:07:24 +0200576 bp->link = 0;
577 bp->speed = 0;
578 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200579
580 return 0;
581}
582
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100583static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200584{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000585 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200586 struct device_node *np;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200587 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200588
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200589 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200590 macb_writel(bp, NCR, MACB_BIT(MPE));
591
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700592 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700593 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200594 err = -ENOMEM;
595 goto err_out;
596 }
597
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700598 bp->mii_bus->name = "MACB_mii_bus";
599 bp->mii_bus->read = &macb_mdio_read;
600 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000601 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700602 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700603 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700604 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900605 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700606
Jamie Iles91523942011-02-28 04:05:25 +0000607 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200608
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200609 np = bp->pdev->dev.of_node;
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200610 if (np && of_phy_is_fixed_link(np)) {
611 if (of_phy_register_fixed_link(np) < 0) {
612 dev_err(&bp->pdev->dev,
613 "broken fixed-link specification %pOF\n", np);
614 goto err_out_free_mdiobus;
615 }
Brad Mouring739de9a2018-03-13 16:32:13 -0500616
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200617 err = mdiobus_register(bp->mii_bus);
618 } else {
619 if (pdata)
620 bp->mii_bus->phy_mask = pdata->phy_mask;
621
622 err = of_mdiobus_register(bp->mii_bus, np);
623 }
624
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200625 if (err)
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200626 goto err_out_free_fixed_link;
frederic RODO6c36a702007-07-12 19:07:24 +0200627
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200628 err = macb_mii_probe(bp->dev);
629 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200630 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200631
632 return 0;
633
634err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700635 mdiobus_unregister(bp->mii_bus);
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200636err_out_free_fixed_link:
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100637 if (np && of_phy_is_fixed_link(np))
638 of_phy_deregister_fixed_link(np);
Brad Mouring739de9a2018-03-13 16:32:13 -0500639err_out_free_mdiobus:
640 of_node_put(bp->phy_node);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700641 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200642err_out:
643 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100644}
645
646static void macb_update_stats(struct macb *bp)
647{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000648 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
649 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300650 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100651
652 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
653
Moritz Fischer96ec6312016-03-29 19:11:11 -0700654 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700655 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100656}
657
Nicolas Ferree86cd532012-10-31 06:04:57 +0000658static int macb_halt_tx(struct macb *bp)
659{
660 unsigned long halt_time, timeout;
661 u32 status;
662
663 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
664
665 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
666 do {
667 halt_time = jiffies;
668 status = macb_readl(bp, TSR);
669 if (!(status & MACB_BIT(TGO)))
670 return 0;
671
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800672 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000673 } while (time_before(halt_time, timeout));
674
675 return -ETIMEDOUT;
676}
677
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200678static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
679{
680 if (tx_skb->mapping) {
681 if (tx_skb->mapped_as_page)
682 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
683 tx_skb->size, DMA_TO_DEVICE);
684 else
685 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
686 tx_skb->size, DMA_TO_DEVICE);
687 tx_skb->mapping = 0;
688 }
689
690 if (tx_skb->skb) {
691 dev_kfree_skb_any(tx_skb->skb);
692 tx_skb->skb = NULL;
693 }
694}
695
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000696static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530697{
Harini Katakamfff80192016-08-09 13:15:53 +0530698#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000699 struct macb_dma_desc_64 *desc_64;
700
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100701 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000702 desc_64 = macb_64b_desc(bp, desc);
703 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200704 /* The low bits of RX address contain the RX_USED bit, clearing
705 * of which allows packet RX. Make sure the high bits are also
706 * visible to HW at that point.
707 */
708 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000709 }
Harini Katakamfff80192016-08-09 13:15:53 +0530710#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000711 desc->addr = lower_32_bits(addr);
712}
713
714static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
715{
716 dma_addr_t addr = 0;
717#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
718 struct macb_dma_desc_64 *desc_64;
719
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100720 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000721 desc_64 = macb_64b_desc(bp, desc);
722 addr = ((u64)(desc_64->addrh) << 32);
723 }
724#endif
725 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
726 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530727}
728
Nicolas Ferree86cd532012-10-31 06:04:57 +0000729static void macb_tx_error_task(struct work_struct *work)
730{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100731 struct macb_queue *queue = container_of(work, struct macb_queue,
732 tx_error_task);
733 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000734 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100735 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000736 struct sk_buff *skb;
737 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100738 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000739
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
741 (unsigned int)(queue - bp->queues),
742 queue->tx_tail, queue->tx_head);
743
744 /* Prevent the queue IRQ handlers from running: each of them may call
745 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
746 * As explained below, we have to halt the transmission before updating
747 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
748 * network engine about the macb/gem being halted.
749 */
750 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000751
752 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100753 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000754
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700755 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000756 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100757 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000758 */
759 if (macb_halt_tx(bp))
760 /* Just complain for now, reinitializing TX path can be good */
761 netdev_err(bp->dev, "BUG: halt tx timed out\n");
762
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700763 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000764 * Free transmit buffers in upper layer.
765 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100766 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
767 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000768
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100769 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000770 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100771 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000772 skb = tx_skb->skb;
773
774 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200775 /* skb is set for the last buffer of the frame */
776 while (!skb) {
777 macb_tx_unmap(bp, tx_skb);
778 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100779 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200780 skb = tx_skb->skb;
781 }
782
783 /* ctrl still refers to the first buffer descriptor
784 * since it's the only one written back by the hardware
785 */
786 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
787 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500788 macb_tx_ring_wrap(bp, tail),
789 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200790 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000791 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200792 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000793 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200794 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000795 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700796 /* "Buffers exhausted mid-frame" errors may only happen
797 * if the driver is buggy, so complain loudly about
798 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000799 */
800 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
801 netdev_err(bp->dev,
802 "BUG: TX buffers exhausted mid-frame\n");
803
804 desc->ctrl = ctrl | MACB_BIT(TX_USED);
805 }
806
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200807 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000808 }
809
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100810 /* Set end of TX queue */
811 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000812 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100813 desc->ctrl = MACB_BIT(TX_USED);
814
Nicolas Ferree86cd532012-10-31 06:04:57 +0000815 /* Make descriptor updates visible to hardware */
816 wmb();
817
818 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000819 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530820#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100821 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000822 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530823#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000824 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100825 queue->tx_head = 0;
826 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000827
828 /* Housework before enabling TX IRQ */
829 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100830 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
831
832 /* Now we are ready to start transmission again */
833 netif_tx_start_all_queues(bp->dev);
834 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
835
836 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000837}
838
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100839static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840{
841 unsigned int tail;
842 unsigned int head;
843 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100844 struct macb *bp = queue->bp;
845 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846
847 status = macb_readl(bp, TSR);
848 macb_writel(bp, TSR, status);
849
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000850 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100851 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000852
Nicolas Ferree86cd532012-10-31 06:04:57 +0000853 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700854 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100855
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100856 head = queue->tx_head;
857 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000858 struct macb_tx_skb *tx_skb;
859 struct sk_buff *skb;
860 struct macb_dma_desc *desc;
861 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100862
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100863 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100864
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000865 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100866 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000867
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000868 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100869
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200870 /* TX_USED bit is only set by hardware on the very first buffer
871 * descriptor of the transmitted frame.
872 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000873 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100874 break;
875
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200876 /* Process all buffers of the current transmitted frame */
877 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100878 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200879 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000880
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200881 /* First, update TX stats if needed */
882 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100883 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
884 /* skb now belongs to timestamp buffer
885 * and will be removed later
886 */
887 tx_skb->skb = NULL;
888 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200889 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500890 macb_tx_ring_wrap(bp, tail),
891 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200892 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000893 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200894 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000895 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200896 }
897
898 /* Now we can safely release resources */
899 macb_tx_unmap(bp, tx_skb);
900
901 /* skb is set only for the last buffer of the frame.
902 * WARNING: at this point skb has been freed by
903 * macb_tx_unmap().
904 */
905 if (skb)
906 break;
907 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100908 }
909
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100910 queue->tx_tail = tail;
911 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
912 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500913 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100914 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100915}
916
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000917static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000918{
919 unsigned int entry;
920 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000921 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000922 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000923 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000924
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000925 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
926 bp->rx_ring_size) > 0) {
927 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000928
929 /* Make hw descriptor updates visible to CPU */
930 rmb();
931
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000932 queue->rx_prepared_head++;
933 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000934
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000935 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000936 /* allocate sk_buff for this free entry in ring */
937 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700938 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000939 netdev_err(bp->dev,
940 "Unable to allocate sk_buff\n");
941 break;
942 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000943
944 /* now fill corresponding descriptor entry */
945 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700946 bp->rx_buffer_size,
947 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800948 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
949 dev_kfree_skb(skb);
950 break;
951 }
952
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000953 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000954
Zach Brownb410d132016-10-19 09:56:57 -0500955 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000956 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000957 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200958 /* Setting addr clears RX_USED and allows reception,
959 * make sure ctrl is cleared first to avoid a race.
960 */
961 dma_wmb();
962 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000963
964 /* properly align Ethernet header */
965 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530966 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000967 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +0200968 dma_wmb();
969 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000970 }
971 }
972
973 /* Make descriptor updates visible to hardware */
974 wmb();
975
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000976 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
977 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000978}
979
980/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000981static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000982 unsigned int end)
983{
984 unsigned int frag;
985
986 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000987 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700988
Nicolas Ferre4df95132013-06-04 21:57:12 +0000989 desc->addr &= ~MACB_BIT(RX_USED);
990 }
991
992 /* Make descriptor updates visible to hardware */
993 wmb();
994
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700995 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000996 * whatever caused this is updated, so we don't have to record
997 * anything.
998 */
999}
1000
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001001static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001002{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001003 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001004 unsigned int len;
1005 unsigned int entry;
1006 struct sk_buff *skb;
1007 struct macb_dma_desc *desc;
1008 int count = 0;
1009
1010 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301011 u32 ctrl;
1012 dma_addr_t addr;
1013 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001014
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001015 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1016 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001017
1018 /* Make hw descriptor updates visible to CPU */
1019 rmb();
1020
Harini Katakamfff80192016-08-09 13:15:53 +05301021 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001022 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001023
Harini Katakamfff80192016-08-09 13:15:53 +05301024 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001025 break;
1026
Anssi Hannula6e0af292018-12-17 15:05:41 +02001027 /* Ensure ctrl is at least as up-to-date as rxused */
1028 dma_rmb();
1029
1030 ctrl = desc->ctrl;
1031
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001032 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001033 count++;
1034
1035 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1036 netdev_err(bp->dev,
1037 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001038 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001039 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001040 break;
1041 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001042 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001043 if (unlikely(!skb)) {
1044 netdev_err(bp->dev,
1045 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001046 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001047 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001048 break;
1049 }
1050 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001051 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301052 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001053
1054 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1055
1056 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001057 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001058 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001059
1060 skb->protocol = eth_type_trans(skb, bp->dev);
1061 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001062 if (bp->dev->features & NETIF_F_RXCSUM &&
1063 !(bp->dev->flags & IFF_PROMISC) &&
1064 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1065 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001066
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001067 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001068 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001069 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001070 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001071
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001072 gem_ptp_do_rxstamp(bp, skb, desc);
1073
Nicolas Ferre4df95132013-06-04 21:57:12 +00001074#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1075 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1076 skb->len, skb->csum);
1077 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001078 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001079 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1080 skb->data, 32, true);
1081#endif
1082
1083 netif_receive_skb(skb);
1084 }
1085
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001086 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001087
1088 return count;
1089}
1090
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001091static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001092 unsigned int last_frag)
1093{
1094 unsigned int len;
1095 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001096 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001097 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001098 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001099 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001100
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001101 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301102 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001103
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001104 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001105 macb_rx_ring_wrap(bp, first_frag),
1106 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001107
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001108 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001109 * first buffer. Since the header is 14 bytes, this makes the
1110 * payload word-aligned.
1111 *
1112 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1113 * the two padding bytes into the skb so that we avoid hitting
1114 * the slowpath in memcpy(), and pull them off afterwards.
1115 */
1116 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001117 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001118 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001119 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001120 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001121 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001122 if (frag == last_frag)
1123 break;
1124 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001125
1126 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001127 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001128
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001129 return 1;
1130 }
1131
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001132 offset = 0;
1133 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001134 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001135 skb_put(skb, len);
1136
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001137 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001138 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001139
1140 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001141 if (unlikely(frag != last_frag)) {
1142 dev_kfree_skb_any(skb);
1143 return -1;
1144 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001145 frag_len = len - offset;
1146 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001147 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001148 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001149 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001150 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001151 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001152 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001153
1154 if (frag == last_frag)
1155 break;
1156 }
1157
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001158 /* Make descriptor updates visible to hardware */
1159 wmb();
1160
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001161 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001162 skb->protocol = eth_type_trans(skb, bp->dev);
1163
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001164 bp->dev->stats.rx_packets++;
1165 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001166 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001167 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168 netif_receive_skb(skb);
1169
1170 return 0;
1171}
1172
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001173static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001174{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001175 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001176 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001177 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001178 int i;
1179
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001180 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001181 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001182 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001183 macb_set_addr(bp, desc, addr);
1184 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001185 addr += bp->rx_buffer_size;
1186 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001187 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001188 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001189}
1190
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001191static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001192{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001193 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001194 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001195 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001196 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001197 int first_frag = -1;
1198
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001199 for (tail = queue->rx_tail; budget > 0; tail++) {
1200 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001201 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001202
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001203 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001204 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001205
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001206 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001207 break;
1208
Anssi Hannula6e0af292018-12-17 15:05:41 +02001209 /* Ensure ctrl is at least as up-to-date as addr */
1210 dma_rmb();
1211
1212 ctrl = desc->ctrl;
1213
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 if (ctrl & MACB_BIT(RX_SOF)) {
1215 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001216 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001217 first_frag = tail;
1218 }
1219
1220 if (ctrl & MACB_BIT(RX_EOF)) {
1221 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001222
1223 if (unlikely(first_frag == -1)) {
1224 reset_rx_queue = true;
1225 continue;
1226 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001228 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001230 if (unlikely(dropped < 0)) {
1231 reset_rx_queue = true;
1232 continue;
1233 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001234 if (!dropped) {
1235 received++;
1236 budget--;
1237 }
1238 }
1239 }
1240
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001241 if (unlikely(reset_rx_queue)) {
1242 unsigned long flags;
1243 u32 ctrl;
1244
1245 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1246
1247 spin_lock_irqsave(&bp->lock, flags);
1248
1249 ctrl = macb_readl(bp, NCR);
1250 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1251
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001252 macb_init_rx_ring(queue);
1253 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001254
1255 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1256
1257 spin_unlock_irqrestore(&bp->lock, flags);
1258 return received;
1259 }
1260
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001261 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001262 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001263 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001264 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001265
1266 return received;
1267}
1268
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001269static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001270{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001271 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1272 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001273 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001274 u32 status;
1275
1276 status = macb_readl(bp, RSR);
1277 macb_writel(bp, RSR, status);
1278
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001279 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001280 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001281
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001282 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001283 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001284 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001285
Nicolas Ferre8770e912013-02-12 11:08:48 +01001286 /* Packets received while interrupts were disabled */
1287 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001288 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001289 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001290 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001291 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001292 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301293 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001294 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001295 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001296
1297 /* TODO: Handle errors */
1298
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001299 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001300}
1301
Harini Katakam032dc412018-01-27 12:09:01 +05301302static void macb_hresp_error_task(unsigned long data)
1303{
1304 struct macb *bp = (struct macb *)data;
1305 struct net_device *dev = bp->dev;
1306 struct macb_queue *queue = bp->queues;
1307 unsigned int q;
1308 u32 ctrl;
1309
1310 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301311 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301312 MACB_TX_INT_FLAGS |
1313 MACB_BIT(HRESP));
1314 }
1315 ctrl = macb_readl(bp, NCR);
1316 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1317 macb_writel(bp, NCR, ctrl);
1318
1319 netif_tx_stop_all_queues(dev);
1320 netif_carrier_off(dev);
1321
1322 bp->macbgem_ops.mog_init_rings(bp);
1323
1324 /* Initialize TX and RX buffers */
1325 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1326 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1327#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1328 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1329 queue_writel(queue, RBQPH,
1330 upper_32_bits(queue->rx_ring_dma));
1331#endif
1332 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1333#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1334 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1335 queue_writel(queue, TBQPH,
1336 upper_32_bits(queue->tx_ring_dma));
1337#endif
1338
1339 /* Enable interrupts */
1340 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301341 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301342 MACB_TX_INT_FLAGS |
1343 MACB_BIT(HRESP));
1344 }
1345
1346 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1347 macb_writel(bp, NCR, ctrl);
1348
1349 netif_carrier_on(dev);
1350 netif_tx_start_all_queues(dev);
1351}
1352
Claudiu Beznea42983882018-12-17 10:02:42 +00001353static void macb_tx_restart(struct macb_queue *queue)
1354{
1355 unsigned int head = queue->tx_head;
1356 unsigned int tail = queue->tx_tail;
1357 struct macb *bp = queue->bp;
1358
1359 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1360 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1361
1362 if (head == tail)
1363 return;
1364
1365 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1366}
1367
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001368static irqreturn_t macb_interrupt(int irq, void *dev_id)
1369{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 struct macb_queue *queue = dev_id;
1371 struct macb *bp = queue->bp;
1372 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001373 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001374
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001375 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001376
1377 if (unlikely(!status))
1378 return IRQ_NONE;
1379
1380 spin_lock(&bp->lock);
1381
1382 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001383 /* close possible race with dev_close */
1384 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001385 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001386 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1387 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388 break;
1389 }
1390
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001391 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1392 (unsigned int)(queue - bp->queues),
1393 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001394
Harini Katakame5010702019-01-29 15:20:03 +05301395 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001396 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001397 * until we have processed the buffers. The
1398 * scheduling call may fail if the poll routine
1399 * is already scheduled, so disable interrupts
1400 * now.
1401 */
Harini Katakame5010702019-01-29 15:20:03 +05301402 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001403 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001404 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001405
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001406 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001407 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001408 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409 }
1410 }
1411
Nicolas Ferree86cd532012-10-31 06:04:57 +00001412 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001413 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1414 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001415
1416 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001417 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001418
Nicolas Ferree86cd532012-10-31 06:04:57 +00001419 break;
1420 }
1421
1422 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001423 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001424
Claudiu Beznea42983882018-12-17 10:02:42 +00001425 if (status & MACB_BIT(TXUBR))
1426 macb_tx_restart(queue);
1427
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001428 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429 * add that if/when we get our hands on a full-blown MII PHY.
1430 */
1431
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001432 /* There is a hardware issue under heavy load where DMA can
1433 * stop, this causes endless "used buffer descriptor read"
1434 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301435 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1436 * section 16.7.4 for details. RXUBR is only enabled for
1437 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001438 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001439 if (status & MACB_BIT(RXUBR)) {
1440 ctrl = macb_readl(bp, NCR);
1441 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001442 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001443 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1444
1445 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001446 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001447 }
1448
Alexander Steinb19f7f72011-04-13 05:03:24 +00001449 if (status & MACB_BIT(ISR_ROVR)) {
1450 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001451 if (macb_is_gem(bp))
1452 bp->hw_stats.gem.rx_overruns++;
1453 else
1454 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001455
1456 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001457 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001458 }
1459
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001460 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301461 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001462 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001463
1464 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001465 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001466 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001467 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001468 }
1469
1470 spin_unlock(&bp->lock);
1471
1472 return IRQ_HANDLED;
1473}
1474
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001475#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001476/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001477 * to allow network i/o with interrupts disabled.
1478 */
1479static void macb_poll_controller(struct net_device *dev)
1480{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001481 struct macb *bp = netdev_priv(dev);
1482 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001483 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001484 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001485
1486 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001487 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1488 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001489 local_irq_restore(flags);
1490}
1491#endif
1492
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001493static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001494 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001495 struct sk_buff *skb,
1496 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001497{
1498 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001499 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001500 struct macb_tx_skb *tx_skb = NULL;
1501 struct macb_dma_desc *desc;
1502 unsigned int offset, size, count = 0;
1503 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001504 unsigned int eof = 1, mss_mfs = 0;
1505 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1506
1507 /* LSO */
1508 if (skb_shinfo(skb)->gso_size != 0) {
1509 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1510 /* UDP - UFO */
1511 lso_ctrl = MACB_LSO_UFO_ENABLE;
1512 else
1513 /* TCP - TSO */
1514 lso_ctrl = MACB_LSO_TSO_ENABLE;
1515 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001516
1517 /* First, map non-paged data */
1518 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001519
1520 /* first buffer length */
1521 size = hdrlen;
1522
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001523 offset = 0;
1524 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001525 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001526 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001527
1528 mapping = dma_map_single(&bp->pdev->dev,
1529 skb->data + offset,
1530 size, DMA_TO_DEVICE);
1531 if (dma_mapping_error(&bp->pdev->dev, mapping))
1532 goto dma_error;
1533
1534 /* Save info to properly release resources */
1535 tx_skb->skb = NULL;
1536 tx_skb->mapping = mapping;
1537 tx_skb->size = size;
1538 tx_skb->mapped_as_page = false;
1539
1540 len -= size;
1541 offset += size;
1542 count++;
1543 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001544
1545 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001546 }
1547
1548 /* Then, map paged data from fragments */
1549 for (f = 0; f < nr_frags; f++) {
1550 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1551
1552 len = skb_frag_size(frag);
1553 offset = 0;
1554 while (len) {
1555 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001556 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001557 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001558
1559 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1560 offset, size, DMA_TO_DEVICE);
1561 if (dma_mapping_error(&bp->pdev->dev, mapping))
1562 goto dma_error;
1563
1564 /* Save info to properly release resources */
1565 tx_skb->skb = NULL;
1566 tx_skb->mapping = mapping;
1567 tx_skb->size = size;
1568 tx_skb->mapped_as_page = true;
1569
1570 len -= size;
1571 offset += size;
1572 count++;
1573 tx_head++;
1574 }
1575 }
1576
1577 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001578 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001579 netdev_err(bp->dev, "BUG! empty skb!\n");
1580 return 0;
1581 }
1582
1583 /* This is the last buffer of the frame: save socket buffer */
1584 tx_skb->skb = skb;
1585
1586 /* Update TX ring: update buffer descriptors in reverse order
1587 * to avoid race condition
1588 */
1589
1590 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1591 * to set the end of TX queue
1592 */
1593 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001594 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001595 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001596 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001597 desc->ctrl = ctrl;
1598
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001599 if (lso_ctrl) {
1600 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1601 /* include header and FCS in value given to h/w */
1602 mss_mfs = skb_shinfo(skb)->gso_size +
1603 skb_transport_offset(skb) +
1604 ETH_FCS_LEN;
1605 else /* TSO */ {
1606 mss_mfs = skb_shinfo(skb)->gso_size;
1607 /* TCP Sequence Number Source Select
1608 * can be set only for TSO
1609 */
1610 seq_ctrl = 0;
1611 }
1612 }
1613
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001614 do {
1615 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001616 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001617 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001618 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001619
1620 ctrl = (u32)tx_skb->size;
1621 if (eof) {
1622 ctrl |= MACB_BIT(TX_LAST);
1623 eof = 0;
1624 }
Zach Brownb410d132016-10-19 09:56:57 -05001625 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001626 ctrl |= MACB_BIT(TX_WRAP);
1627
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001628 /* First descriptor is header descriptor */
1629 if (i == queue->tx_head) {
1630 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1631 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001632 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1633 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1634 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001635 } else
1636 /* Only set MSS/MFS on payload descriptors
1637 * (second or later descriptor)
1638 */
1639 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1640
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001641 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001642 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001643 /* desc->addr must be visible to hardware before clearing
1644 * 'TX_USED' bit in desc->ctrl.
1645 */
1646 wmb();
1647 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001648 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001649
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001650 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001651
1652 return count;
1653
1654dma_error:
1655 netdev_err(bp->dev, "TX DMA map failed\n");
1656
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001657 for (i = queue->tx_head; i != tx_head; i++) {
1658 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001659
1660 macb_tx_unmap(bp, tx_skb);
1661 }
1662
1663 return 0;
1664}
1665
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001666static netdev_features_t macb_features_check(struct sk_buff *skb,
1667 struct net_device *dev,
1668 netdev_features_t features)
1669{
1670 unsigned int nr_frags, f;
1671 unsigned int hdrlen;
1672
1673 /* Validate LSO compatibility */
1674
1675 /* there is only one buffer */
1676 if (!skb_is_nonlinear(skb))
1677 return features;
1678
1679 /* length of header */
1680 hdrlen = skb_transport_offset(skb);
1681 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1682 hdrlen += tcp_hdrlen(skb);
1683
1684 /* For LSO:
1685 * When software supplies two or more payload buffers all payload buffers
1686 * apart from the last must be a multiple of 8 bytes in size.
1687 */
1688 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1689 return features & ~MACB_NETIF_LSO;
1690
1691 nr_frags = skb_shinfo(skb)->nr_frags;
1692 /* No need to check last fragment */
1693 nr_frags--;
1694 for (f = 0; f < nr_frags; f++) {
1695 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1696
1697 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1698 return features & ~MACB_NETIF_LSO;
1699 }
1700 return features;
1701}
1702
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001703static inline int macb_clear_csum(struct sk_buff *skb)
1704{
1705 /* no change for packets without checksum offloading */
1706 if (skb->ip_summed != CHECKSUM_PARTIAL)
1707 return 0;
1708
1709 /* make sure we can modify the header */
1710 if (unlikely(skb_cow_head(skb, 0)))
1711 return -1;
1712
1713 /* initialize checksum field
1714 * This is required - at least for Zynq, which otherwise calculates
1715 * wrong UDP header checksums for UDP packets with UDP data len <=2
1716 */
1717 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1718 return 0;
1719}
1720
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001721static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1722{
1723 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1724 int padlen = ETH_ZLEN - (*skb)->len;
1725 int headroom = skb_headroom(*skb);
1726 int tailroom = skb_tailroom(*skb);
1727 struct sk_buff *nskb;
1728 u32 fcs;
1729
1730 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1731 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1732 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1733 return 0;
1734
1735 if (padlen <= 0) {
1736 /* FCS could be appeded to tailroom. */
1737 if (tailroom >= ETH_FCS_LEN)
1738 goto add_fcs;
1739 /* FCS could be appeded by moving data to headroom. */
1740 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1741 padlen = 0;
1742 /* No room for FCS, need to reallocate skb. */
1743 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001744 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001745 } else {
1746 /* Add room for FCS. */
1747 padlen += ETH_FCS_LEN;
1748 }
1749
1750 if (!cloned && headroom + tailroom >= padlen) {
1751 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1752 skb_set_tail_pointer(*skb, (*skb)->len);
1753 } else {
1754 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1755 if (!nskb)
1756 return -ENOMEM;
1757
Huang Zijiangf3e5c072019-02-14 14:41:18 +08001758 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001759 *skb = nskb;
1760 }
1761
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001762 if (padlen > ETH_FCS_LEN)
1763 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001764
1765add_fcs:
1766 /* set FCS to packet */
1767 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1768 fcs = ~fcs;
1769
1770 skb_put_u8(*skb, fcs & 0xff);
1771 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1772 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1773 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1774
1775 return 0;
1776}
1777
Claudiu Beznead1c38952018-08-07 12:25:12 +03001778static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001779{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001780 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001781 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001782 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001783 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001784 unsigned int desc_cnt, nr_frags, frag_size, f;
1785 unsigned int hdrlen;
1786 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001787 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001788
Claudiu Beznea33729f22018-08-07 12:25:13 +03001789 if (macb_clear_csum(skb)) {
1790 dev_kfree_skb_any(skb);
1791 return ret;
1792 }
1793
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001794 if (macb_pad_and_fcs(&skb, dev)) {
1795 dev_kfree_skb_any(skb);
1796 return ret;
1797 }
1798
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001799 is_lso = (skb_shinfo(skb)->gso_size != 0);
1800
1801 if (is_lso) {
1802 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1803
1804 /* length of headers */
1805 if (is_udp)
1806 /* only queue eth + ip headers separately for UDP */
1807 hdrlen = skb_transport_offset(skb);
1808 else
1809 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1810 if (skb_headlen(skb) < hdrlen) {
1811 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1812 /* if this is required, would need to copy to single buffer */
1813 return NETDEV_TX_BUSY;
1814 }
1815 } else
1816 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001817
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001818#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1819 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001820 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1821 queue_index, skb->len, skb->head, skb->data,
1822 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001823 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1824 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001825#endif
1826
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001827 /* Count how many TX buffer descriptors are needed to send this
1828 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001829 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001830 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001831 if (is_lso && (skb_headlen(skb) > hdrlen))
1832 /* extra header descriptor if also payload in first buffer */
1833 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1834 else
1835 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001836 nr_frags = skb_shinfo(skb)->nr_frags;
1837 for (f = 0; f < nr_frags; f++) {
1838 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001839 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001840 }
1841
Dongdong Deng48719532009-08-23 19:49:07 -07001842 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001843
1844 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001845 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001846 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001847 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001848 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001849 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001850 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001851 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001852 }
1853
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001854 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001855 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001856 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001857 goto unlock;
1858 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001859
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001860 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001861 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001862 skb_tx_timestamp(skb);
1863
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001864 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1865
Zach Brownb410d132016-10-19 09:56:57 -05001866 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001867 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001868
Soren Brinkmann92030902014-03-04 08:46:39 -08001869unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001870 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001871
Claudiu Beznead1c38952018-08-07 12:25:12 +03001872 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001873}
1874
Nicolas Ferre4df95132013-06-04 21:57:12 +00001875static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001876{
1877 if (!macb_is_gem(bp)) {
1878 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1879 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001880 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001881
Nicolas Ferre1b447912013-06-04 21:57:11 +00001882 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001883 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001884 "RX buffer must be multiple of %d bytes, expanding\n",
1885 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001886 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001887 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001888 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001889 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001890
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001891 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001892 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001893}
1894
Nicolas Ferre4df95132013-06-04 21:57:12 +00001895static void gem_free_rx_buffers(struct macb *bp)
1896{
1897 struct sk_buff *skb;
1898 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001899 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001900 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001901 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001902 int i;
1903
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001904 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1905 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001906 continue;
1907
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001908 for (i = 0; i < bp->rx_ring_size; i++) {
1909 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001910
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001911 if (!skb)
1912 continue;
1913
1914 desc = macb_rx_desc(queue, i);
1915 addr = macb_get_addr(bp, desc);
1916
1917 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1918 DMA_FROM_DEVICE);
1919 dev_kfree_skb_any(skb);
1920 skb = NULL;
1921 }
1922
1923 kfree(queue->rx_skbuff);
1924 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001925 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001926}
1927
1928static void macb_free_rx_buffers(struct macb *bp)
1929{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001930 struct macb_queue *queue = &bp->queues[0];
1931
1932 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001933 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001934 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001935 queue->rx_buffers, queue->rx_buffers_dma);
1936 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001937 }
1938}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001939
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001940static void macb_free_consistent(struct macb *bp)
1941{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001942 struct macb_queue *queue;
1943 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05301944 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001945
Nicolas Ferre4df95132013-06-04 21:57:12 +00001946 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001947
1948 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1949 kfree(queue->tx_skb);
1950 queue->tx_skb = NULL;
1951 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301952 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1953 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001954 queue->tx_ring, queue->tx_ring_dma);
1955 queue->tx_ring = NULL;
1956 }
Harini Katakame50b7702018-07-06 12:18:57 +05301957 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05301958 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1959 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05301960 queue->rx_ring, queue->rx_ring_dma);
1961 queue->rx_ring = NULL;
1962 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001963 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001964}
1965
1966static int gem_alloc_rx_buffers(struct macb *bp)
1967{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001968 struct macb_queue *queue;
1969 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001970 int size;
1971
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001972 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1973 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1974 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1975 if (!queue->rx_skbuff)
1976 return -ENOMEM;
1977 else
1978 netdev_dbg(bp->dev,
1979 "Allocated %d RX struct sk_buff entries at %p\n",
1980 bp->rx_ring_size, queue->rx_skbuff);
1981 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001982 return 0;
1983}
1984
1985static int macb_alloc_rx_buffers(struct macb *bp)
1986{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001987 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001988 int size;
1989
Zach Brownb410d132016-10-19 09:56:57 -05001990 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001991 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1992 &queue->rx_buffers_dma, GFP_KERNEL);
1993 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001994 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001995
1996 netdev_dbg(bp->dev,
1997 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001998 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001999 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002000}
2001
2002static int macb_alloc_consistent(struct macb *bp)
2003{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002004 struct macb_queue *queue;
2005 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002006 int size;
2007
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002008 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302009 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002010 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2011 &queue->tx_ring_dma,
2012 GFP_KERNEL);
2013 if (!queue->tx_ring)
2014 goto out_err;
2015 netdev_dbg(bp->dev,
2016 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2017 q, size, (unsigned long)queue->tx_ring_dma,
2018 queue->tx_ring);
2019
Zach Brownb410d132016-10-19 09:56:57 -05002020 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002021 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2022 if (!queue->tx_skb)
2023 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002024
Harini Katakam404cd082018-07-06 12:18:58 +05302025 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002026 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2027 &queue->rx_ring_dma, GFP_KERNEL);
2028 if (!queue->rx_ring)
2029 goto out_err;
2030 netdev_dbg(bp->dev,
2031 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2032 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002033 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002034 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002035 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002036
2037 return 0;
2038
2039out_err:
2040 macb_free_consistent(bp);
2041 return -ENOMEM;
2042}
2043
Nicolas Ferre4df95132013-06-04 21:57:12 +00002044static void gem_init_rings(struct macb *bp)
2045{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002046 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002047 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002048 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002049 int i;
2050
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002051 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002052 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002053 desc = macb_tx_desc(queue, i);
2054 macb_set_addr(bp, desc, 0);
2055 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002056 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002057 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002058 queue->tx_head = 0;
2059 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002060
2061 queue->rx_tail = 0;
2062 queue->rx_prepared_head = 0;
2063
2064 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002065 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002066
Nicolas Ferre4df95132013-06-04 21:57:12 +00002067}
2068
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002069static void macb_init_rings(struct macb *bp)
2070{
2071 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002072 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002073
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002074 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002075
Zach Brownb410d132016-10-19 09:56:57 -05002076 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002077 desc = macb_tx_desc(&bp->queues[0], i);
2078 macb_set_addr(bp, desc, 0);
2079 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002080 }
Ben Shelton21d35152015-04-22 17:28:54 -05002081 bp->queues[0].tx_head = 0;
2082 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002083 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084}
2085
2086static void macb_reset_hw(struct macb *bp)
2087{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002088 struct macb_queue *queue;
2089 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002090 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002091
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002092 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002093 * more gracefully?)
2094 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002095 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002096
2097 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002098 ctrl |= MACB_BIT(CLRSTAT);
2099
2100 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002101
2102 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002103 macb_writel(bp, TSR, -1);
2104 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002105
2106 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002107 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2108 queue_writel(queue, IDR, -1);
2109 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002110 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2111 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002112 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002113}
2114
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002115static u32 gem_mdc_clk_div(struct macb *bp)
2116{
2117 u32 config;
2118 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2119
2120 if (pclk_hz <= 20000000)
2121 config = GEM_BF(CLK, GEM_CLK_DIV8);
2122 else if (pclk_hz <= 40000000)
2123 config = GEM_BF(CLK, GEM_CLK_DIV16);
2124 else if (pclk_hz <= 80000000)
2125 config = GEM_BF(CLK, GEM_CLK_DIV32);
2126 else if (pclk_hz <= 120000000)
2127 config = GEM_BF(CLK, GEM_CLK_DIV48);
2128 else if (pclk_hz <= 160000000)
2129 config = GEM_BF(CLK, GEM_CLK_DIV64);
2130 else
2131 config = GEM_BF(CLK, GEM_CLK_DIV96);
2132
2133 return config;
2134}
2135
2136static u32 macb_mdc_clk_div(struct macb *bp)
2137{
2138 u32 config;
2139 unsigned long pclk_hz;
2140
2141 if (macb_is_gem(bp))
2142 return gem_mdc_clk_div(bp);
2143
2144 pclk_hz = clk_get_rate(bp->pclk);
2145 if (pclk_hz <= 20000000)
2146 config = MACB_BF(CLK, MACB_CLK_DIV8);
2147 else if (pclk_hz <= 40000000)
2148 config = MACB_BF(CLK, MACB_CLK_DIV16);
2149 else if (pclk_hz <= 80000000)
2150 config = MACB_BF(CLK, MACB_CLK_DIV32);
2151 else
2152 config = MACB_BF(CLK, MACB_CLK_DIV64);
2153
2154 return config;
2155}
2156
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002157/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002158 * should program. We find the width from decoding the design configuration
2159 * register to find the maximum supported data bus width.
2160 */
2161static u32 macb_dbw(struct macb *bp)
2162{
2163 if (!macb_is_gem(bp))
2164 return 0;
2165
2166 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2167 case 4:
2168 return GEM_BF(DBW, GEM_DBW128);
2169 case 2:
2170 return GEM_BF(DBW, GEM_DBW64);
2171 case 1:
2172 default:
2173 return GEM_BF(DBW, GEM_DBW32);
2174 }
2175}
2176
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002177/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002178 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002179 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002180 * (if not supported by FIFO, it will fallback to default)
2181 * - set both rx/tx packet buffers to full memory size
2182 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002183 */
2184static void macb_configure_dma(struct macb *bp)
2185{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002186 struct macb_queue *queue;
2187 u32 buffer_size;
2188 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002189 u32 dmacfg;
2190
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002191 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002192 if (macb_is_gem(bp)) {
2193 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002194 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2195 if (q)
2196 queue_writel(queue, RBQS, buffer_size);
2197 else
2198 dmacfg |= GEM_BF(RXBS, buffer_size);
2199 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002200 if (bp->dma_burst_length)
2201 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002202 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302203 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302204
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002205 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302206 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2207 else
2208 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2209
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002210 if (bp->dev->features & NETIF_F_HW_CSUM)
2211 dmacfg |= GEM_BIT(TXCOEN);
2212 else
2213 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302214
Michal Simekbd620722018-09-25 08:32:50 +02002215 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302216#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002217 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002218 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302219#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002220#ifdef CONFIG_MACB_USE_HWSTAMP
2221 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2222 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2223#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002224 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2225 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002226 gem_writel(bp, DMACFG, dmacfg);
2227 }
2228}
2229
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002230static void macb_init_hw(struct macb *bp)
2231{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002232 struct macb_queue *queue;
2233 unsigned int q;
2234
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002235 u32 config;
2236
2237 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002238 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002239
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002240 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302241 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2242 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002243 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002244 config |= MACB_BIT(PAE); /* PAuse Enable */
2245 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002246 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302247 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2248 else
2249 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002250 if (bp->dev->flags & IFF_PROMISC)
2251 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002252 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2253 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002254 if (!(bp->dev->flags & IFF_BROADCAST))
2255 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002256 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002257 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002258 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302259 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002260 bp->speed = SPEED_10;
2261 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302262 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002263 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302264 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002265
Jamie Iles0116da42011-03-14 17:38:30 +00002266 macb_configure_dma(bp);
2267
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002268 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002269 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002270 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2271#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2272 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2273 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2274#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002275 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302276#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002277 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002278 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302279#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002280
2281 /* Enable interrupts */
2282 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05302283 bp->rx_intr_mask |
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002284 MACB_TX_INT_FLAGS |
2285 MACB_BIT(HRESP));
2286 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002287
2288 /* Enable TX and RX */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002289 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002290}
2291
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002292/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002293 * locations in the memory map. The least significant bits are stored
2294 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2295 *
2296 * The unicast hash enable and the multicast hash enable bits in the
2297 * network configuration register enable the reception of hash matched
2298 * frames. The destination address is reduced to a 6 bit index into
2299 * the 64 bit hash register using the following hash function. The
2300 * hash function is an exclusive or of every sixth bit of the
2301 * destination address.
2302 *
2303 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2304 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2305 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2306 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2307 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2308 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2309 *
2310 * da[0] represents the least significant bit of the first byte
2311 * received, that is, the multicast/unicast indicator, and da[47]
2312 * represents the most significant bit of the last byte received. If
2313 * the hash index, hi[n], points to a bit that is set in the hash
2314 * register then the frame will be matched according to whether the
2315 * frame is multicast or unicast. A multicast match will be signalled
2316 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2317 * index points to a bit set in the hash register. A unicast match
2318 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2319 * and the hash index points to a bit set in the hash register. To
2320 * receive all multicast frames, the hash register should be set with
2321 * all ones and the multicast hash enable bit should be set in the
2322 * network configuration register.
2323 */
2324
2325static inline int hash_bit_value(int bitnr, __u8 *addr)
2326{
2327 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2328 return 1;
2329 return 0;
2330}
2331
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002332/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002333static int hash_get_index(__u8 *addr)
2334{
2335 int i, j, bitval;
2336 int hash_index = 0;
2337
2338 for (j = 0; j < 6; j++) {
2339 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002340 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002341
2342 hash_index |= (bitval << j);
2343 }
2344
2345 return hash_index;
2346}
2347
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002348/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002349static void macb_sethashtable(struct net_device *dev)
2350{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002351 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002352 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002353 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002354 struct macb *bp = netdev_priv(dev);
2355
Moritz Fischeraa50b552016-03-29 19:11:13 -07002356 mc_filter[0] = 0;
2357 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002358
Jiri Pirko22bedad32010-04-01 21:22:57 +00002359 netdev_for_each_mc_addr(ha, dev) {
2360 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002361 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2362 }
2363
Jamie Ilesf75ba502011-11-08 10:12:32 +00002364 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2365 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002366}
2367
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002368/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002369static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002370{
2371 unsigned long cfg;
2372 struct macb *bp = netdev_priv(dev);
2373
2374 cfg = macb_readl(bp, NCFGR);
2375
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002376 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002377 /* Enable promiscuous mode */
2378 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002379
2380 /* Disable RX checksum offload */
2381 if (macb_is_gem(bp))
2382 cfg &= ~GEM_BIT(RXCOEN);
2383 } else {
2384 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002385 cfg &= ~MACB_BIT(CAF);
2386
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002387 /* Enable RX checksum offload only if requested */
2388 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2389 cfg |= GEM_BIT(RXCOEN);
2390 }
2391
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002392 if (dev->flags & IFF_ALLMULTI) {
2393 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002394 macb_or_gem_writel(bp, HRB, -1);
2395 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002396 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002397 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002398 /* Enable specific multicasts */
2399 macb_sethashtable(dev);
2400 cfg |= MACB_BIT(NCFGR_MTI);
2401 } else if (dev->flags & (~IFF_ALLMULTI)) {
2402 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002403 macb_or_gem_writel(bp, HRB, 0);
2404 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002405 cfg &= ~MACB_BIT(NCFGR_MTI);
2406 }
2407
2408 macb_writel(bp, NCFGR, cfg);
2409}
2410
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002411static int macb_open(struct net_device *dev)
2412{
2413 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002414 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002415 struct macb_queue *queue;
2416 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002417 int err;
2418
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002419 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002420
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002421 /* carrier starts down */
2422 netif_carrier_off(dev);
2423
frederic RODO6c36a702007-07-12 19:07:24 +02002424 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002425 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002426 return -EAGAIN;
2427
Nicolas Ferre1b447912013-06-04 21:57:11 +00002428 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002429 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002430
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002431 err = macb_alloc_consistent(bp);
2432 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002433 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2434 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002435 return err;
2436 }
2437
Nicolas Ferre4df95132013-06-04 21:57:12 +00002438 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002439 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002440
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002441 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2442 napi_enable(&queue->napi);
2443
frederic RODO6c36a702007-07-12 19:07:24 +02002444 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002445 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002446
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002447 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002448
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002449 if (bp->ptp_info)
2450 bp->ptp_info->ptp_init(dev);
2451
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002452 return 0;
2453}
2454
2455static int macb_close(struct net_device *dev)
2456{
2457 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002458 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002459 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002460 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002461
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002462 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002463
2464 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2465 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002466
Philippe Reynes0a912812016-06-22 00:32:35 +02002467 if (dev->phydev)
2468 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002469
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002470 spin_lock_irqsave(&bp->lock, flags);
2471 macb_reset_hw(bp);
2472 netif_carrier_off(dev);
2473 spin_unlock_irqrestore(&bp->lock, flags);
2474
2475 macb_free_consistent(bp);
2476
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002477 if (bp->ptp_info)
2478 bp->ptp_info->ptp_remove(dev);
2479
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002480 return 0;
2481}
2482
Harini Katakama5898ea2015-05-06 22:27:18 +05302483static int macb_change_mtu(struct net_device *dev, int new_mtu)
2484{
Harini Katakama5898ea2015-05-06 22:27:18 +05302485 if (netif_running(dev))
2486 return -EBUSY;
2487
Harini Katakama5898ea2015-05-06 22:27:18 +05302488 dev->mtu = new_mtu;
2489
2490 return 0;
2491}
2492
Jamie Ilesa494ed82011-03-09 16:26:35 +00002493static void gem_update_stats(struct macb *bp)
2494{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002495 struct macb_queue *queue;
2496 unsigned int i, q, idx;
2497 unsigned long *stat;
2498
Jamie Ilesa494ed82011-03-09 16:26:35 +00002499 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002500
Xander Huff3ff13f12015-01-13 16:15:51 -06002501 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2502 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002503 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002504
2505 bp->ethtool_stats[i] += val;
2506 *p += val;
2507
2508 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2509 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002510 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002511 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002512 *(++p) += val;
2513 }
2514 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002515
2516 idx = GEM_STATS_LEN;
2517 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2518 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2519 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002520}
2521
2522static struct net_device_stats *gem_get_stats(struct macb *bp)
2523{
2524 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002525 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002526
2527 gem_update_stats(bp);
2528
2529 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2530 hwstat->rx_alignment_errors +
2531 hwstat->rx_resource_errors +
2532 hwstat->rx_overruns +
2533 hwstat->rx_oversize_frames +
2534 hwstat->rx_jabbers +
2535 hwstat->rx_undersized_frames +
2536 hwstat->rx_length_field_frame_errors);
2537 nstat->tx_errors = (hwstat->tx_late_collisions +
2538 hwstat->tx_excessive_collisions +
2539 hwstat->tx_underrun +
2540 hwstat->tx_carrier_sense_errors);
2541 nstat->multicast = hwstat->rx_multicast_frames;
2542 nstat->collisions = (hwstat->tx_single_collision_frames +
2543 hwstat->tx_multiple_collision_frames +
2544 hwstat->tx_excessive_collisions);
2545 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2546 hwstat->rx_jabbers +
2547 hwstat->rx_undersized_frames +
2548 hwstat->rx_length_field_frame_errors);
2549 nstat->rx_over_errors = hwstat->rx_resource_errors;
2550 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2551 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2552 nstat->rx_fifo_errors = hwstat->rx_overruns;
2553 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2554 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2555 nstat->tx_fifo_errors = hwstat->tx_underrun;
2556
2557 return nstat;
2558}
2559
Xander Huff3ff13f12015-01-13 16:15:51 -06002560static void gem_get_ethtool_stats(struct net_device *dev,
2561 struct ethtool_stats *stats, u64 *data)
2562{
2563 struct macb *bp;
2564
2565 bp = netdev_priv(dev);
2566 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002567 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2568 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002569}
2570
2571static int gem_get_sset_count(struct net_device *dev, int sset)
2572{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002573 struct macb *bp = netdev_priv(dev);
2574
Xander Huff3ff13f12015-01-13 16:15:51 -06002575 switch (sset) {
2576 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002577 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002578 default:
2579 return -EOPNOTSUPP;
2580 }
2581}
2582
2583static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2584{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002585 char stat_string[ETH_GSTRING_LEN];
2586 struct macb *bp = netdev_priv(dev);
2587 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002588 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002589 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002590
2591 switch (sset) {
2592 case ETH_SS_STATS:
2593 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2594 memcpy(p, gem_statistics[i].stat_string,
2595 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002596
2597 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2598 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2599 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2600 q, queue_statistics[i].stat_string);
2601 memcpy(p, stat_string, ETH_GSTRING_LEN);
2602 }
2603 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002604 break;
2605 }
2606}
2607
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002608static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002609{
2610 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002611 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002612 struct macb_stats *hwstat = &bp->hw_stats.macb;
2613
2614 if (macb_is_gem(bp))
2615 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002616
frederic RODO6c36a702007-07-12 19:07:24 +02002617 /* read stats from hardware */
2618 macb_update_stats(bp);
2619
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002620 /* Convert HW stats into netdevice stats */
2621 nstat->rx_errors = (hwstat->rx_fcs_errors +
2622 hwstat->rx_align_errors +
2623 hwstat->rx_resource_errors +
2624 hwstat->rx_overruns +
2625 hwstat->rx_oversize_pkts +
2626 hwstat->rx_jabbers +
2627 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002628 hwstat->rx_length_mismatch);
2629 nstat->tx_errors = (hwstat->tx_late_cols +
2630 hwstat->tx_excessive_cols +
2631 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002632 hwstat->tx_carrier_errors +
2633 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002634 nstat->collisions = (hwstat->tx_single_cols +
2635 hwstat->tx_multiple_cols +
2636 hwstat->tx_excessive_cols);
2637 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2638 hwstat->rx_jabbers +
2639 hwstat->rx_undersize_pkts +
2640 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002641 nstat->rx_over_errors = hwstat->rx_resource_errors +
2642 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002643 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2644 nstat->rx_frame_errors = hwstat->rx_align_errors;
2645 nstat->rx_fifo_errors = hwstat->rx_overruns;
2646 /* XXX: What does "missed" mean? */
2647 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2648 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2649 nstat->tx_fifo_errors = hwstat->tx_underruns;
2650 /* Don't know about heartbeat or window errors... */
2651
2652 return nstat;
2653}
2654
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002655static int macb_get_regs_len(struct net_device *netdev)
2656{
2657 return MACB_GREGS_NBR * sizeof(u32);
2658}
2659
2660static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2661 void *p)
2662{
2663 struct macb *bp = netdev_priv(dev);
2664 unsigned int tail, head;
2665 u32 *regs_buff = p;
2666
2667 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2668 | MACB_GREGS_VERSION;
2669
Zach Brownb410d132016-10-19 09:56:57 -05002670 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2671 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002672
2673 regs_buff[0] = macb_readl(bp, NCR);
2674 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2675 regs_buff[2] = macb_readl(bp, NSR);
2676 regs_buff[3] = macb_readl(bp, TSR);
2677 regs_buff[4] = macb_readl(bp, RBQP);
2678 regs_buff[5] = macb_readl(bp, TBQP);
2679 regs_buff[6] = macb_readl(bp, RSR);
2680 regs_buff[7] = macb_readl(bp, IMR);
2681
2682 regs_buff[8] = tail;
2683 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002684 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2685 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002686
Neil Armstrongce721a72016-01-05 14:39:16 +01002687 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2688 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002689 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002690 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002691}
2692
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002693static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2694{
2695 struct macb *bp = netdev_priv(netdev);
2696
2697 wol->supported = 0;
2698 wol->wolopts = 0;
2699
2700 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2701 wol->supported = WAKE_MAGIC;
2702
2703 if (bp->wol & MACB_WOL_ENABLED)
2704 wol->wolopts |= WAKE_MAGIC;
2705 }
2706}
2707
2708static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2709{
2710 struct macb *bp = netdev_priv(netdev);
2711
2712 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2713 (wol->wolopts & ~WAKE_MAGIC))
2714 return -EOPNOTSUPP;
2715
2716 if (wol->wolopts & WAKE_MAGIC)
2717 bp->wol |= MACB_WOL_ENABLED;
2718 else
2719 bp->wol &= ~MACB_WOL_ENABLED;
2720
2721 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2722
2723 return 0;
2724}
2725
Zach Brown8441bb32016-10-19 09:56:58 -05002726static void macb_get_ringparam(struct net_device *netdev,
2727 struct ethtool_ringparam *ring)
2728{
2729 struct macb *bp = netdev_priv(netdev);
2730
2731 ring->rx_max_pending = MAX_RX_RING_SIZE;
2732 ring->tx_max_pending = MAX_TX_RING_SIZE;
2733
2734 ring->rx_pending = bp->rx_ring_size;
2735 ring->tx_pending = bp->tx_ring_size;
2736}
2737
2738static int macb_set_ringparam(struct net_device *netdev,
2739 struct ethtool_ringparam *ring)
2740{
2741 struct macb *bp = netdev_priv(netdev);
2742 u32 new_rx_size, new_tx_size;
2743 unsigned int reset = 0;
2744
2745 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2746 return -EINVAL;
2747
2748 new_rx_size = clamp_t(u32, ring->rx_pending,
2749 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2750 new_rx_size = roundup_pow_of_two(new_rx_size);
2751
2752 new_tx_size = clamp_t(u32, ring->tx_pending,
2753 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2754 new_tx_size = roundup_pow_of_two(new_tx_size);
2755
2756 if ((new_tx_size == bp->tx_ring_size) &&
2757 (new_rx_size == bp->rx_ring_size)) {
2758 /* nothing to do */
2759 return 0;
2760 }
2761
2762 if (netif_running(bp->dev)) {
2763 reset = 1;
2764 macb_close(bp->dev);
2765 }
2766
2767 bp->rx_ring_size = new_rx_size;
2768 bp->tx_ring_size = new_tx_size;
2769
2770 if (reset)
2771 macb_open(bp->dev);
2772
2773 return 0;
2774}
2775
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002776#ifdef CONFIG_MACB_USE_HWSTAMP
2777static unsigned int gem_get_tsu_rate(struct macb *bp)
2778{
2779 struct clk *tsu_clk;
2780 unsigned int tsu_rate;
2781
2782 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2783 if (!IS_ERR(tsu_clk))
2784 tsu_rate = clk_get_rate(tsu_clk);
2785 /* try pclk instead */
2786 else if (!IS_ERR(bp->pclk)) {
2787 tsu_clk = bp->pclk;
2788 tsu_rate = clk_get_rate(tsu_clk);
2789 } else
2790 return -ENOTSUPP;
2791 return tsu_rate;
2792}
2793
2794static s32 gem_get_ptp_max_adj(void)
2795{
2796 return 64000000;
2797}
2798
2799static int gem_get_ts_info(struct net_device *dev,
2800 struct ethtool_ts_info *info)
2801{
2802 struct macb *bp = netdev_priv(dev);
2803
2804 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2805 ethtool_op_get_ts_info(dev, info);
2806 return 0;
2807 }
2808
2809 info->so_timestamping =
2810 SOF_TIMESTAMPING_TX_SOFTWARE |
2811 SOF_TIMESTAMPING_RX_SOFTWARE |
2812 SOF_TIMESTAMPING_SOFTWARE |
2813 SOF_TIMESTAMPING_TX_HARDWARE |
2814 SOF_TIMESTAMPING_RX_HARDWARE |
2815 SOF_TIMESTAMPING_RAW_HARDWARE;
2816 info->tx_types =
2817 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2818 (1 << HWTSTAMP_TX_OFF) |
2819 (1 << HWTSTAMP_TX_ON);
2820 info->rx_filters =
2821 (1 << HWTSTAMP_FILTER_NONE) |
2822 (1 << HWTSTAMP_FILTER_ALL);
2823
2824 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2825
2826 return 0;
2827}
2828
2829static struct macb_ptp_info gem_ptp_info = {
2830 .ptp_init = gem_ptp_init,
2831 .ptp_remove = gem_ptp_remove,
2832 .get_ptp_max_adj = gem_get_ptp_max_adj,
2833 .get_tsu_rate = gem_get_tsu_rate,
2834 .get_ts_info = gem_get_ts_info,
2835 .get_hwtst = gem_get_hwtst,
2836 .set_hwtst = gem_set_hwtst,
2837};
2838#endif
2839
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002840static int macb_get_ts_info(struct net_device *netdev,
2841 struct ethtool_ts_info *info)
2842{
2843 struct macb *bp = netdev_priv(netdev);
2844
2845 if (bp->ptp_info)
2846 return bp->ptp_info->get_ts_info(netdev, info);
2847
2848 return ethtool_op_get_ts_info(netdev, info);
2849}
2850
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002851static void gem_enable_flow_filters(struct macb *bp, bool enable)
2852{
2853 struct ethtool_rx_fs_item *item;
2854 u32 t2_scr;
2855 int num_t2_scr;
2856
2857 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2858
2859 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2860 struct ethtool_rx_flow_spec *fs = &item->fs;
2861 struct ethtool_tcpip4_spec *tp4sp_m;
2862
2863 if (fs->location >= num_t2_scr)
2864 continue;
2865
2866 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2867
2868 /* enable/disable screener regs for the flow entry */
2869 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2870
2871 /* only enable fields with no masking */
2872 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2873
2874 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2875 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2876 else
2877 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2878
2879 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2880 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2881 else
2882 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2883
2884 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2885 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2886 else
2887 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2888
2889 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2890 }
2891}
2892
2893static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2894{
2895 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2896 uint16_t index = fs->location;
2897 u32 w0, w1, t2_scr;
2898 bool cmp_a = false;
2899 bool cmp_b = false;
2900 bool cmp_c = false;
2901
2902 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2903 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2904
2905 /* ignore field if any masking set */
2906 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2907 /* 1st compare reg - IP source address */
2908 w0 = 0;
2909 w1 = 0;
2910 w0 = tp4sp_v->ip4src;
2911 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2912 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2913 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2914 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2915 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2916 cmp_a = true;
2917 }
2918
2919 /* ignore field if any masking set */
2920 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2921 /* 2nd compare reg - IP destination address */
2922 w0 = 0;
2923 w1 = 0;
2924 w0 = tp4sp_v->ip4dst;
2925 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2926 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2927 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2928 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2929 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2930 cmp_b = true;
2931 }
2932
2933 /* ignore both port fields if masking set in both */
2934 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2935 /* 3rd compare reg - source port, destination port */
2936 w0 = 0;
2937 w1 = 0;
2938 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2939 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2940 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2941 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2942 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2943 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2944 } else {
2945 /* only one port definition */
2946 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2947 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2948 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2949 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2950 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2951 } else { /* dst port */
2952 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2953 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2954 }
2955 }
2956 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2957 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2958 cmp_c = true;
2959 }
2960
2961 t2_scr = 0;
2962 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2963 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2964 if (cmp_a)
2965 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2966 if (cmp_b)
2967 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2968 if (cmp_c)
2969 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2970 gem_writel_n(bp, SCRT2, index, t2_scr);
2971}
2972
2973static int gem_add_flow_filter(struct net_device *netdev,
2974 struct ethtool_rxnfc *cmd)
2975{
2976 struct macb *bp = netdev_priv(netdev);
2977 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2978 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002979 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002980 int ret = -EINVAL;
2981 bool added = false;
2982
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002983 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002984 if (newfs == NULL)
2985 return -ENOMEM;
2986 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2987
2988 netdev_dbg(netdev,
2989 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2990 fs->flow_type, (int)fs->ring_cookie, fs->location,
2991 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2992 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2993 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2994
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002995 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2996
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002997 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002998 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2999 if (item->fs.location > newfs->fs.location) {
3000 list_add_tail(&newfs->list, &item->list);
3001 added = true;
3002 break;
3003 } else if (item->fs.location == fs->location) {
3004 netdev_err(netdev, "Rule not added: location %d not free!\n",
3005 fs->location);
3006 ret = -EBUSY;
3007 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003008 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003009 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003010 if (!added)
3011 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003012
3013 gem_prog_cmp_regs(bp, fs);
3014 bp->rx_fs_list.count++;
3015 /* enable filtering if NTUPLE on */
3016 if (netdev->features & NETIF_F_NTUPLE)
3017 gem_enable_flow_filters(bp, 1);
3018
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003019 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003020 return 0;
3021
3022err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003023 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003024 kfree(newfs);
3025 return ret;
3026}
3027
3028static int gem_del_flow_filter(struct net_device *netdev,
3029 struct ethtool_rxnfc *cmd)
3030{
3031 struct macb *bp = netdev_priv(netdev);
3032 struct ethtool_rx_fs_item *item;
3033 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003034 unsigned long flags;
3035
3036 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003037
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003038 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3039 if (item->fs.location == cmd->fs.location) {
3040 /* disable screener regs for the flow entry */
3041 fs = &(item->fs);
3042 netdev_dbg(netdev,
3043 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3044 fs->flow_type, (int)fs->ring_cookie, fs->location,
3045 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3046 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3047 htons(fs->h_u.tcp_ip4_spec.psrc),
3048 htons(fs->h_u.tcp_ip4_spec.pdst));
3049
3050 gem_writel_n(bp, SCRT2, fs->location, 0);
3051
3052 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003053 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003054 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3055 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003056 return 0;
3057 }
3058 }
3059
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003060 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003061 return -EINVAL;
3062}
3063
3064static int gem_get_flow_entry(struct net_device *netdev,
3065 struct ethtool_rxnfc *cmd)
3066{
3067 struct macb *bp = netdev_priv(netdev);
3068 struct ethtool_rx_fs_item *item;
3069
3070 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3071 if (item->fs.location == cmd->fs.location) {
3072 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3073 return 0;
3074 }
3075 }
3076 return -EINVAL;
3077}
3078
3079static int gem_get_all_flow_entries(struct net_device *netdev,
3080 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3081{
3082 struct macb *bp = netdev_priv(netdev);
3083 struct ethtool_rx_fs_item *item;
3084 uint32_t cnt = 0;
3085
3086 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3087 if (cnt == cmd->rule_cnt)
3088 return -EMSGSIZE;
3089 rule_locs[cnt] = item->fs.location;
3090 cnt++;
3091 }
3092 cmd->data = bp->max_tuples;
3093 cmd->rule_cnt = cnt;
3094
3095 return 0;
3096}
3097
3098static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3099 u32 *rule_locs)
3100{
3101 struct macb *bp = netdev_priv(netdev);
3102 int ret = 0;
3103
3104 switch (cmd->cmd) {
3105 case ETHTOOL_GRXRINGS:
3106 cmd->data = bp->num_queues;
3107 break;
3108 case ETHTOOL_GRXCLSRLCNT:
3109 cmd->rule_cnt = bp->rx_fs_list.count;
3110 break;
3111 case ETHTOOL_GRXCLSRULE:
3112 ret = gem_get_flow_entry(netdev, cmd);
3113 break;
3114 case ETHTOOL_GRXCLSRLALL:
3115 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3116 break;
3117 default:
3118 netdev_err(netdev,
3119 "Command parameter %d is not supported\n", cmd->cmd);
3120 ret = -EOPNOTSUPP;
3121 }
3122
3123 return ret;
3124}
3125
3126static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3127{
3128 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003129 int ret;
3130
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003131 switch (cmd->cmd) {
3132 case ETHTOOL_SRXCLSRLINS:
3133 if ((cmd->fs.location >= bp->max_tuples)
3134 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3135 ret = -EINVAL;
3136 break;
3137 }
3138 ret = gem_add_flow_filter(netdev, cmd);
3139 break;
3140 case ETHTOOL_SRXCLSRLDEL:
3141 ret = gem_del_flow_filter(netdev, cmd);
3142 break;
3143 default:
3144 netdev_err(netdev,
3145 "Command parameter %d is not supported\n", cmd->cmd);
3146 ret = -EOPNOTSUPP;
3147 }
3148
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003149 return ret;
3150}
3151
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003152static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003153 .get_regs_len = macb_get_regs_len,
3154 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003155 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003156 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003157 .get_wol = macb_get_wol,
3158 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003159 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3160 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003161 .get_ringparam = macb_get_ringparam,
3162 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003163};
Xander Huff8cd5a562015-01-15 15:55:20 -06003164
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003165static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003166 .get_regs_len = macb_get_regs_len,
3167 .get_regs = macb_get_regs,
3168 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003169 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003170 .get_ethtool_stats = gem_get_ethtool_stats,
3171 .get_strings = gem_get_ethtool_strings,
3172 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003173 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3174 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003175 .get_ringparam = macb_get_ringparam,
3176 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003177 .get_rxnfc = gem_get_rxnfc,
3178 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003179};
3180
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003181static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003182{
Philippe Reynes0a912812016-06-22 00:32:35 +02003183 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003184 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003185
3186 if (!netif_running(dev))
3187 return -EINVAL;
3188
frederic RODO6c36a702007-07-12 19:07:24 +02003189 if (!phydev)
3190 return -ENODEV;
3191
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003192 if (!bp->ptp_info)
3193 return phy_mii_ioctl(phydev, rq, cmd);
3194
3195 switch (cmd) {
3196 case SIOCSHWTSTAMP:
3197 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3198 case SIOCGHWTSTAMP:
3199 return bp->ptp_info->get_hwtst(dev, rq);
3200 default:
3201 return phy_mii_ioctl(phydev, rq, cmd);
3202 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003203}
3204
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003205static int macb_set_features(struct net_device *netdev,
3206 netdev_features_t features)
3207{
3208 struct macb *bp = netdev_priv(netdev);
3209 netdev_features_t changed = features ^ netdev->features;
3210
3211 /* TX checksum offload */
3212 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3213 u32 dmacfg;
3214
3215 dmacfg = gem_readl(bp, DMACFG);
3216 if (features & NETIF_F_HW_CSUM)
3217 dmacfg |= GEM_BIT(TXCOEN);
3218 else
3219 dmacfg &= ~GEM_BIT(TXCOEN);
3220 gem_writel(bp, DMACFG, dmacfg);
3221 }
3222
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003223 /* RX checksum offload */
3224 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3225 u32 netcfg;
3226
3227 netcfg = gem_readl(bp, NCFGR);
3228 if (features & NETIF_F_RXCSUM &&
3229 !(netdev->flags & IFF_PROMISC))
3230 netcfg |= GEM_BIT(RXCOEN);
3231 else
3232 netcfg &= ~GEM_BIT(RXCOEN);
3233 gem_writel(bp, NCFGR, netcfg);
3234 }
3235
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003236 /* RX Flow Filters */
3237 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3238 bool turn_on = features & NETIF_F_NTUPLE;
3239
3240 gem_enable_flow_filters(bp, turn_on);
3241 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003242 return 0;
3243}
3244
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003245static const struct net_device_ops macb_netdev_ops = {
3246 .ndo_open = macb_open,
3247 .ndo_stop = macb_close,
3248 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003249 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003250 .ndo_get_stats = macb_get_stats,
3251 .ndo_do_ioctl = macb_ioctl,
3252 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303253 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003254 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003255#ifdef CONFIG_NET_POLL_CONTROLLER
3256 .ndo_poll_controller = macb_poll_controller,
3257#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003258 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003259 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003260};
3261
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003262/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003263 * and integration options used
3264 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003265static void macb_configure_caps(struct macb *bp,
3266 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003267{
3268 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003269
Nicolas Ferref6970502015-03-31 15:02:01 +02003270 if (dt_conf)
3271 bp->caps = dt_conf->caps;
3272
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003273 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003274 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3275
Nicolas Ferree1755872014-07-24 13:50:58 +02003276 dcfg = gem_readl(bp, DCFG1);
3277 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3278 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3279 dcfg = gem_readl(bp, DCFG2);
3280 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3281 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003282#ifdef CONFIG_MACB_USE_HWSTAMP
3283 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003284 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3285 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003286 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003287 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003288 bp->ptp_info = &gem_ptp_info;
3289 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003290 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003291#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003292 }
3293
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003294 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003295}
3296
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003297static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003298 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003299 unsigned int *queue_mask,
3300 unsigned int *num_queues)
3301{
3302 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003303
3304 *queue_mask = 0x1;
3305 *num_queues = 1;
3306
Nicolas Ferreda120112015-03-31 15:02:00 +02003307 /* is it macb or gem ?
3308 *
3309 * We need to read directly from the hardware here because
3310 * we are early in the probe process and don't have the
3311 * MACB_CAPS_MACB_IS_GEM flag positioned
3312 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003313 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003314 return;
3315
3316 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303317 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3318
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003319 *queue_mask |= 0x1;
3320
3321 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3322 if (*queue_mask & (1 << hw_q))
3323 (*num_queues)++;
3324}
3325
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003326static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303327 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303328 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003329{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003330 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003331 int err;
3332
Bartosz Folta83a77e92016-12-14 06:39:15 +00003333 pdata = dev_get_platdata(&pdev->dev);
3334 if (pdata) {
3335 *pclk = pdata->pclk;
3336 *hclk = pdata->hclk;
3337 } else {
3338 *pclk = devm_clk_get(&pdev->dev, "pclk");
3339 *hclk = devm_clk_get(&pdev->dev, "hclk");
3340 }
3341
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003342 if (IS_ERR(*pclk)) {
3343 err = PTR_ERR(*pclk);
3344 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3345 return err;
3346 }
3347
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003348 if (IS_ERR(*hclk)) {
3349 err = PTR_ERR(*hclk);
3350 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3351 return err;
3352 }
3353
3354 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3355 if (IS_ERR(*tx_clk))
3356 *tx_clk = NULL;
3357
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303358 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3359 if (IS_ERR(*rx_clk))
3360 *rx_clk = NULL;
3361
Harini Katakamf5473d12019-03-01 16:20:33 +05303362 *tsu_clk = devm_clk_get(&pdev->dev, "tsu_clk");
3363 if (IS_ERR(*tsu_clk))
3364 *tsu_clk = NULL;
3365
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003366 err = clk_prepare_enable(*pclk);
3367 if (err) {
3368 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3369 return err;
3370 }
3371
3372 err = clk_prepare_enable(*hclk);
3373 if (err) {
3374 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3375 goto err_disable_pclk;
3376 }
3377
3378 err = clk_prepare_enable(*tx_clk);
3379 if (err) {
3380 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3381 goto err_disable_hclk;
3382 }
3383
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303384 err = clk_prepare_enable(*rx_clk);
3385 if (err) {
3386 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3387 goto err_disable_txclk;
3388 }
3389
Harini Katakamf5473d12019-03-01 16:20:33 +05303390 err = clk_prepare_enable(*tsu_clk);
3391 if (err) {
3392 dev_err(&pdev->dev, "failed to enable tsu_clk (%u)\n", err);
3393 goto err_disable_rxclk;
3394 }
3395
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003396 return 0;
3397
Harini Katakamf5473d12019-03-01 16:20:33 +05303398err_disable_rxclk:
3399 clk_disable_unprepare(*rx_clk);
3400
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303401err_disable_txclk:
3402 clk_disable_unprepare(*tx_clk);
3403
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003404err_disable_hclk:
3405 clk_disable_unprepare(*hclk);
3406
3407err_disable_pclk:
3408 clk_disable_unprepare(*pclk);
3409
3410 return err;
3411}
3412
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003413static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003414{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003415 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003416 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003417 struct macb *bp = netdev_priv(dev);
3418 struct macb_queue *queue;
3419 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003420 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003421
Zach Brownb410d132016-10-19 09:56:57 -05003422 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3423 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3424
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003425 /* set the queue register mapping once for all: queue0 has a special
3426 * register mapping but we don't want to test the queue index then
3427 * compute the corresponding register offset at run time.
3428 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003429 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003430 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003431 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003432
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003433 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003434 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003435 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003436 if (hw_q) {
3437 queue->ISR = GEM_ISR(hw_q - 1);
3438 queue->IER = GEM_IER(hw_q - 1);
3439 queue->IDR = GEM_IDR(hw_q - 1);
3440 queue->IMR = GEM_IMR(hw_q - 1);
3441 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003442 queue->RBQP = GEM_RBQP(hw_q - 1);
3443 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303444#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003445 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003446 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003447 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3448 }
Harini Katakamfff80192016-08-09 13:15:53 +05303449#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003450 } else {
3451 /* queue0 uses legacy registers */
3452 queue->ISR = MACB_ISR;
3453 queue->IER = MACB_IER;
3454 queue->IDR = MACB_IDR;
3455 queue->IMR = MACB_IMR;
3456 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003457 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303458#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003459 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003460 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003461 queue->RBQPH = MACB_RBQPH;
3462 }
Harini Katakamfff80192016-08-09 13:15:53 +05303463#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003464 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003465
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003466 /* get irq: here we use the linux queue index, not the hardware
3467 * queue index. the queue irq definitions in the device tree
3468 * must remove the optional gaps that could exist in the
3469 * hardware queue mask.
3470 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003471 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003472 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003473 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003474 if (err) {
3475 dev_err(&pdev->dev,
3476 "Unable to request IRQ %d (error %d)\n",
3477 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003478 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003479 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003480
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003481 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003482 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003483 }
3484
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003485 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003486
Nicolas Ferre4df95132013-06-04 21:57:12 +00003487 /* setup appropriated routines according to adapter type */
3488 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003489 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003490 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3491 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3492 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3493 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003494 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003495 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003496 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003497 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3498 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3499 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3500 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003501 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003502 }
3503
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003504 /* Set features */
3505 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003506
3507 /* Check LSO capability */
3508 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3509 dev->hw_features |= MACB_NETIF_LSO;
3510
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003511 /* Checksum offload is only available on gem with packet buffer */
3512 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003513 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003514 if (bp->caps & MACB_CAPS_SG_DISABLED)
3515 dev->hw_features &= ~NETIF_F_SG;
3516 dev->features = dev->hw_features;
3517
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003518 /* Check RX Flow Filters support.
3519 * Max Rx flows set by availability of screeners & compare regs:
3520 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3521 */
3522 reg = gem_readl(bp, DCFG8);
3523 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3524 GEM_BFEXT(T2SCR, reg));
3525 if (bp->max_tuples > 0) {
3526 /* also needs one ethtype match to check IPv4 */
3527 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3528 /* program this reg now */
3529 reg = 0;
3530 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3531 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3532 /* Filtering is supported in hw but don't enable it in kernel now */
3533 dev->hw_features |= NETIF_F_NTUPLE;
3534 /* init Rx flow definitions */
3535 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3536 bp->rx_fs_list.count = 0;
3537 spin_lock_init(&bp->rx_fs_lock);
3538 } else
3539 bp->max_tuples = 0;
3540 }
3541
Neil Armstrongce721a72016-01-05 14:39:16 +01003542 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3543 val = 0;
3544 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3545 val = GEM_BIT(RGMII);
3546 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003547 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003548 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003549 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003550 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003551
Neil Armstrongce721a72016-01-05 14:39:16 +01003552 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3553 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003554
Neil Armstrongce721a72016-01-05 14:39:16 +01003555 macb_or_gem_writel(bp, USRIO, val);
3556 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003557
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003558 /* Set MII management clock divider */
3559 val = macb_mdc_clk_div(bp);
3560 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303561 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3562 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003563 macb_writel(bp, NCFGR, val);
3564
3565 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003566}
3567
3568#if defined(CONFIG_OF)
3569/* 1518 rounded up */
3570#define AT91ETHER_MAX_RBUFF_SZ 0x600
3571/* max number of receive buffers */
3572#define AT91ETHER_MAX_RX_DESCR 9
3573
3574/* Initialize and start the Receiver and Transmit subsystems */
3575static int at91ether_start(struct net_device *dev)
3576{
3577 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003578 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003579 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003580 dma_addr_t addr;
3581 u32 ctl;
3582 int i;
3583
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003584 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003585 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003586 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003587 &q->rx_ring_dma, GFP_KERNEL);
3588 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003589 return -ENOMEM;
3590
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003591 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003592 AT91ETHER_MAX_RX_DESCR *
3593 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003594 &q->rx_buffers_dma, GFP_KERNEL);
3595 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003596 dma_free_coherent(&lp->pdev->dev,
3597 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003598 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003599 q->rx_ring, q->rx_ring_dma);
3600 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003601 return -ENOMEM;
3602 }
3603
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003604 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003605 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003606 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003607 macb_set_addr(lp, desc, addr);
3608 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003609 addr += AT91ETHER_MAX_RBUFF_SZ;
3610 }
3611
3612 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003613 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003614
3615 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003616 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003617
3618 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003619 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003620
3621 /* Enable Receive and Transmit */
3622 ctl = macb_readl(lp, NCR);
3623 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3624
3625 return 0;
3626}
3627
3628/* Open the ethernet interface */
3629static int at91ether_open(struct net_device *dev)
3630{
3631 struct macb *lp = netdev_priv(dev);
3632 u32 ctl;
3633 int ret;
3634
3635 /* Clear internal statistics */
3636 ctl = macb_readl(lp, NCR);
3637 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3638
3639 macb_set_hwaddr(lp);
3640
3641 ret = at91ether_start(dev);
3642 if (ret)
3643 return ret;
3644
3645 /* Enable MAC interrupts */
3646 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3647 MACB_BIT(RXUBR) |
3648 MACB_BIT(ISR_TUND) |
3649 MACB_BIT(ISR_RLE) |
3650 MACB_BIT(TCOMP) |
3651 MACB_BIT(ISR_ROVR) |
3652 MACB_BIT(HRESP));
3653
3654 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003655 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003656
3657 netif_start_queue(dev);
3658
3659 return 0;
3660}
3661
3662/* Close the interface */
3663static int at91ether_close(struct net_device *dev)
3664{
3665 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003666 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003667 u32 ctl;
3668
3669 /* Disable Receiver and Transmitter */
3670 ctl = macb_readl(lp, NCR);
3671 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3672
3673 /* Disable MAC interrupts */
3674 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3675 MACB_BIT(RXUBR) |
3676 MACB_BIT(ISR_TUND) |
3677 MACB_BIT(ISR_RLE) |
3678 MACB_BIT(TCOMP) |
3679 MACB_BIT(ISR_ROVR) |
3680 MACB_BIT(HRESP));
3681
3682 netif_stop_queue(dev);
3683
3684 dma_free_coherent(&lp->pdev->dev,
3685 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003686 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003687 q->rx_ring, q->rx_ring_dma);
3688 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003689
3690 dma_free_coherent(&lp->pdev->dev,
3691 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003692 q->rx_buffers, q->rx_buffers_dma);
3693 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003694
3695 return 0;
3696}
3697
3698/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003699static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3700 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003701{
3702 struct macb *lp = netdev_priv(dev);
3703
3704 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3705 netif_stop_queue(dev);
3706
3707 /* Store packet information (to free when Tx completed) */
3708 lp->skb = skb;
3709 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003710 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3711 skb->len, DMA_TO_DEVICE);
3712 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003713 dev_kfree_skb_any(skb);
3714 dev->stats.tx_dropped++;
3715 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3716 return NETDEV_TX_OK;
3717 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003718
3719 /* Set address of the data in the Transmit Address register */
3720 macb_writel(lp, TAR, lp->skb_physaddr);
3721 /* Set length of the packet in the Transmit Control register */
3722 macb_writel(lp, TCR, skb->len);
3723
3724 } else {
3725 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3726 return NETDEV_TX_BUSY;
3727 }
3728
3729 return NETDEV_TX_OK;
3730}
3731
3732/* Extract received frame from buffer descriptors and sent to upper layers.
3733 * (Called from interrupt context)
3734 */
3735static void at91ether_rx(struct net_device *dev)
3736{
3737 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003738 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003739 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003740 unsigned char *p_recv;
3741 struct sk_buff *skb;
3742 unsigned int pktlen;
3743
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003744 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003745 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003746 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003747 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003748 skb = netdev_alloc_skb(dev, pktlen + 2);
3749 if (skb) {
3750 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003751 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003752
3753 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003754 dev->stats.rx_packets++;
3755 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003756 netif_rx(skb);
3757 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003758 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003759 }
3760
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003761 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003762 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003763
3764 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003765 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003766
3767 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003768 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3769 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003770 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003771 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003772
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003773 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003774 }
3775}
3776
3777/* MAC interrupt handler */
3778static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3779{
3780 struct net_device *dev = dev_id;
3781 struct macb *lp = netdev_priv(dev);
3782 u32 intstatus, ctl;
3783
3784 /* MAC Interrupt Status register indicates what interrupts are pending.
3785 * It is automatically cleared once read.
3786 */
3787 intstatus = macb_readl(lp, ISR);
3788
3789 /* Receive complete */
3790 if (intstatus & MACB_BIT(RCOMP))
3791 at91ether_rx(dev);
3792
3793 /* Transmit complete */
3794 if (intstatus & MACB_BIT(TCOMP)) {
3795 /* The TCOM bit is set even if the transmission failed */
3796 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003797 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003798
3799 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003800 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003801 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003802 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003803 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003804 dev->stats.tx_packets++;
3805 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003806 }
3807 netif_wake_queue(dev);
3808 }
3809
3810 /* Work-around for EMAC Errata section 41.3.1 */
3811 if (intstatus & MACB_BIT(RXUBR)) {
3812 ctl = macb_readl(lp, NCR);
3813 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003814 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003815 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3816 }
3817
3818 if (intstatus & MACB_BIT(ISR_ROVR))
3819 netdev_err(dev, "ROVR error\n");
3820
3821 return IRQ_HANDLED;
3822}
3823
3824#ifdef CONFIG_NET_POLL_CONTROLLER
3825static void at91ether_poll_controller(struct net_device *dev)
3826{
3827 unsigned long flags;
3828
3829 local_irq_save(flags);
3830 at91ether_interrupt(dev->irq, dev);
3831 local_irq_restore(flags);
3832}
3833#endif
3834
3835static const struct net_device_ops at91ether_netdev_ops = {
3836 .ndo_open = at91ether_open,
3837 .ndo_stop = at91ether_close,
3838 .ndo_start_xmit = at91ether_start_xmit,
3839 .ndo_get_stats = macb_get_stats,
3840 .ndo_set_rx_mode = macb_set_rx_mode,
3841 .ndo_set_mac_address = eth_mac_addr,
3842 .ndo_do_ioctl = macb_ioctl,
3843 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003844#ifdef CONFIG_NET_POLL_CONTROLLER
3845 .ndo_poll_controller = at91ether_poll_controller,
3846#endif
3847};
3848
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003849static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303850 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303851 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003852{
3853 int err;
3854
3855 *hclk = NULL;
3856 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303857 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05303858 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003859
3860 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3861 if (IS_ERR(*pclk))
3862 return PTR_ERR(*pclk);
3863
3864 err = clk_prepare_enable(*pclk);
3865 if (err) {
3866 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3867 return err;
3868 }
3869
3870 return 0;
3871}
3872
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003873static int at91ether_init(struct platform_device *pdev)
3874{
3875 struct net_device *dev = platform_get_drvdata(pdev);
3876 struct macb *bp = netdev_priv(dev);
3877 int err;
3878 u32 reg;
3879
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02003880 bp->queues[0].bp = bp;
3881
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003882 dev->netdev_ops = &at91ether_netdev_ops;
3883 dev->ethtool_ops = &macb_ethtool_ops;
3884
3885 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3886 0, dev->name, dev);
3887 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003888 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003889
3890 macb_writel(bp, NCR, 0);
3891
3892 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3893 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3894 reg |= MACB_BIT(RM9200_RMII);
3895
3896 macb_writel(bp, NCFGR, reg);
3897
3898 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003899}
3900
David S. Miller3cef5c52015-03-09 23:38:02 -04003901static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003902 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003903 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003904 .init = macb_init,
3905};
3906
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003907static const struct macb_config sama5d3macb_config = {
3908 .caps = MACB_CAPS_SG_DISABLED
3909 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3910 .clk_init = macb_clk_init,
3911 .init = macb_init,
3912};
3913
David S. Miller3cef5c52015-03-09 23:38:02 -04003914static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003915 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3916 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003917 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003918 .init = macb_init,
3919};
3920
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003921static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003922 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003923 .dma_burst_length = 16,
3924 .clk_init = macb_clk_init,
3925 .init = macb_init,
3926};
3927
David S. Miller3cef5c52015-03-09 23:38:02 -04003928static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003929 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003930 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003931 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003932 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003933 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003934 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003935};
3936
David S. Miller3cef5c52015-03-09 23:38:02 -04003937static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003938 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003939 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003940 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003941 .init = macb_init,
3942};
3943
David S. Miller3cef5c52015-03-09 23:38:02 -04003944static const struct macb_config emac_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303945 .caps = MACB_CAPS_NEEDS_RSTONUBR,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003946 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003947 .init = at91ether_init,
3948};
3949
Neil Armstronge611b5b2016-01-05 14:39:17 +01003950static const struct macb_config np4_config = {
3951 .caps = MACB_CAPS_USRIO_DISABLED,
3952 .clk_init = macb_clk_init,
3953 .init = macb_init,
3954};
David S. Miller36583eb2015-05-23 01:22:35 -04003955
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303956static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003957 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3958 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05303959 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303960 .dma_burst_length = 16,
3961 .clk_init = macb_clk_init,
3962 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303963 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303964};
3965
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003966static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05303967 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
3968 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003969 .dma_burst_length = 16,
3970 .clk_init = macb_clk_init,
3971 .init = macb_init,
3972};
3973
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003974static const struct of_device_id macb_dt_ids[] = {
3975 { .compatible = "cdns,at32ap7000-macb" },
3976 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3977 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003978 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003979 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3980 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01003981 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003982 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003983 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02003984 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003985 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3986 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3987 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303988 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003989 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003990 { /* sentinel */ }
3991};
3992MODULE_DEVICE_TABLE(of, macb_dt_ids);
3993#endif /* CONFIG_OF */
3994
Bartosz Folta83a77e92016-12-14 06:39:15 +00003995static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003996 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3997 MACB_CAPS_JUMBO |
3998 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003999 .dma_burst_length = 16,
4000 .clk_init = macb_clk_init,
4001 .init = macb_init,
4002 .jumbo_max_len = 10240,
4003};
4004
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004005static int macb_probe(struct platform_device *pdev)
4006{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004007 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004008 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304009 struct clk **, struct clk **, struct clk **,
4010 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004011 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004012 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304013 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304014 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004015 unsigned int queue_mask, num_queues;
4016 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004017 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004018 struct phy_device *phydev;
4019 struct net_device *dev;
4020 struct resource *regs;
4021 void __iomem *mem;
4022 const char *mac;
4023 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304024 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004025
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004026 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4027 mem = devm_ioremap_resource(&pdev->dev, regs);
4028 if (IS_ERR(mem))
4029 return PTR_ERR(mem);
4030
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004031 if (np) {
4032 const struct of_device_id *match;
4033
4034 match = of_match_node(macb_dt_ids, np);
4035 if (match && match->data) {
4036 macb_config = match->data;
4037 clk_init = macb_config->clk_init;
4038 init = macb_config->init;
4039 }
4040 }
4041
Harini Katakamf5473d12019-03-01 16:20:33 +05304042 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004043 if (err)
4044 return err;
4045
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004046 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004047
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004048 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004049 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004050 if (!dev) {
4051 err = -ENOMEM;
4052 goto err_disable_clocks;
4053 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004054
4055 dev->base_addr = regs->start;
4056
4057 SET_NETDEV_DEV(dev, &pdev->dev);
4058
4059 bp = netdev_priv(dev);
4060 bp->pdev = pdev;
4061 bp->dev = dev;
4062 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004063 bp->native_io = native_io;
4064 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004065 bp->macb_reg_readl = hw_readl_native;
4066 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004067 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004068 bp->macb_reg_readl = hw_readl;
4069 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03004070 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004071 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004072 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004073 if (macb_config)
4074 bp->dma_burst_length = macb_config->dma_burst_length;
4075 bp->pclk = pclk;
4076 bp->hclk = hclk;
4077 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304078 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304079 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe6a2015-07-24 21:24:00 +03004080 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304081 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304082
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004083 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004084 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004085 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4086 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4087
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004088 spin_lock_init(&bp->lock);
4089
Nicolas Ferread783472015-03-31 15:02:02 +02004090 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004091 macb_configure_caps(bp, macb_config);
4092
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004093#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4094 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4095 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4096 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4097 }
4098#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004099 platform_set_drvdata(pdev, dev);
4100
4101 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004102 if (dev->irq < 0) {
4103 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004104 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004105 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004106
Jarod Wilson44770e12016-10-17 15:54:17 -04004107 /* MTU range: 68 - 1500 or 10240 */
4108 dev->min_mtu = GEM_MTU_MIN_SIZE;
4109 if (bp->caps & MACB_CAPS_JUMBO)
4110 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4111 else
4112 dev->max_mtu = ETH_DATA_LEN;
4113
Harini Katakam404cd082018-07-06 12:18:58 +05304114 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4115 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4116 if (val)
4117 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4118 macb_dma_desc_get_size(bp);
4119
4120 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4121 if (val)
4122 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4123 macb_dma_desc_get_size(bp);
4124 }
4125
Harini Katakame5010702019-01-29 15:20:03 +05304126 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4127 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4128 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4129
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004130 mac = of_get_mac_address(np);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004131 if (mac) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004132 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004133 } else {
Bartosz Golaszewskicce41b82018-11-30 09:20:58 +01004134 err = nvmem_get_mac_address(&pdev->dev, bp->dev->dev_addr);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004135 if (err) {
4136 if (err == -EPROBE_DEFER)
4137 goto err_out_free_netdev;
4138 macb_get_hwaddr(bp);
4139 }
4140 }
frederic RODO6c36a702007-07-12 19:07:24 +02004141
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004142 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004143 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09004144 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004145 if (pdata && pdata->is_rmii)
4146 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4147 else
4148 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4149 } else {
4150 bp->phy_interface = err;
4151 }
4152
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004153 /* IP specific init */
4154 err = init(pdev);
4155 if (err)
4156 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004157
Florian Fainellicf669662016-05-02 18:38:45 -07004158 err = macb_mii_init(bp);
4159 if (err)
4160 goto err_out_free_netdev;
4161
Philippe Reynes0a912812016-06-22 00:32:35 +02004162 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07004163
4164 netif_carrier_off(dev);
4165
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004166 err = register_netdev(dev);
4167 if (err) {
4168 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004169 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004170 }
4171
Harini Katakam032dc412018-01-27 12:09:01 +05304172 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4173 (unsigned long)bp);
4174
Florian Fainellicf669662016-05-02 18:38:45 -07004175 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004176
Bo Shen58798232014-09-13 01:57:49 +02004177 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4178 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4179 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004180
4181 return 0;
4182
Florian Fainellicf669662016-05-02 18:38:45 -07004183err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004184 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004185 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004186 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004187 if (np && of_phy_is_fixed_link(np))
4188 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004189 mdiobus_free(bp->mii_bus);
4190
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004191err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004192 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004193
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004194err_disable_clocks:
4195 clk_disable_unprepare(tx_clk);
4196 clk_disable_unprepare(hclk);
4197 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304198 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304199 clk_disable_unprepare(tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004200
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004201 return err;
4202}
4203
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004204static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004205{
4206 struct net_device *dev;
4207 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004208 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004209
4210 dev = platform_get_drvdata(pdev);
4211
4212 if (dev) {
4213 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004214 if (dev->phydev)
4215 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004216 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004217 if (np && of_phy_is_fixed_link(np))
4218 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004219 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004220 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004221
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004222 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004223 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004224 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004225 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304226 clk_disable_unprepare(bp->rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304227 clk_disable_unprepare(bp->tsu_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004228 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004229 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004230 }
4231
4232 return 0;
4233}
4234
Michal Simekd23823d2015-01-23 09:36:03 +01004235static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004236{
Wolfram Sangce886a42018-10-21 22:00:14 +02004237 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004238 struct macb *bp = netdev_priv(netdev);
4239
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004240 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004241 netif_device_detach(netdev);
4242
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004243 if (bp->wol & MACB_WOL_ENABLED) {
4244 macb_writel(bp, IER, MACB_BIT(WOL));
4245 macb_writel(bp, WOL, MACB_BIT(MAG));
4246 enable_irq_wake(bp->queues[0].irq);
4247 } else {
4248 clk_disable_unprepare(bp->tx_clk);
4249 clk_disable_unprepare(bp->hclk);
4250 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304251 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004252 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304253 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004254
4255 return 0;
4256}
4257
Michal Simekd23823d2015-01-23 09:36:03 +01004258static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004259{
Wolfram Sangce886a42018-10-21 22:00:14 +02004260 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004261 struct macb *bp = netdev_priv(netdev);
4262
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004263 if (bp->wol & MACB_WOL_ENABLED) {
4264 macb_writel(bp, IDR, MACB_BIT(WOL));
4265 macb_writel(bp, WOL, 0);
4266 disable_irq_wake(bp->queues[0].irq);
4267 } else {
4268 clk_prepare_enable(bp->pclk);
4269 clk_prepare_enable(bp->hclk);
4270 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304271 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004272 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304273 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004274
4275 netif_device_attach(netdev);
4276
4277 return 0;
4278}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004279
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004280static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4281
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004282static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004283 .probe = macb_probe,
4284 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004285 .driver = {
4286 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004287 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004288 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004289 },
4290};
4291
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004292module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004293
4294MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004295MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004296MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004297MODULE_ALIAS("platform:macb");