blob: e84afcf1ecb508a771cfe3f418e38f7340c5c1d3 [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>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000035#include <linux/ip.h>
36#include <linux/udp.h>
37#include <linux/tcp.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Nicolas Ferre1b447912013-06-04 21:57:11 +000040#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050042
Zach Brownb410d132016-10-19 09:56:57 -050043#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050044#define MIN_RX_RING_SIZE 64
45#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000046#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050047 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
Zach Brownb410d132016-10-19 09:56:57 -050049#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050050#define MIN_TX_RING_SIZE 64
51#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000052#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050053 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010054
Nicolas Ferre909a8582012-11-19 06:00:21 +000055/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050056#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
58#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
59 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000060#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
61 | MACB_BIT(ISR_RLE) \
62 | MACB_BIT(TXERR))
63#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
64
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000065/* Max length of transmit frame must be a multiple of 8 bytes */
66#define MACB_TX_LEN_ALIGN 8
67#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
68#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 +020069
Jarod Wilson44770e12016-10-17 15:54:17 -040070#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070071#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053072
Sergio Prado3e2a5e12016-02-09 12:07:16 -020073#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
74#define MACB_WOL_ENABLED (0x1 << 1)
75
Moritz Fischer64ec42f2016-03-29 19:11:12 -070076/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000077 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
78 */
79#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010080
Rafal Ozieblodc97a892017-01-27 15:08:20 +000081/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010082 * depends on hardware configuration:
83 *
84 * 1. dma address width 32 bits:
85 * word 1: 32 bit address of Data Buffer
86 * word 2: control
87 *
88 * 2. dma address width 64 bits:
89 * word 1: 32 bit address of Data Buffer
90 * word 2: control
91 * word 3: upper 32 bit address of Data Buffer
92 * word 4: unused
93 *
94 * 3. dma address width 32 bits with hardware timestamping:
95 * word 1: 32 bit address of Data Buffer
96 * word 2: control
97 * word 3: timestamp word 1
98 * word 4: timestamp word 2
99 *
100 * 4. dma address width 64 bits with hardware timestamping:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 * word 3: upper 32 bit address of Data Buffer
104 * word 4: unused
105 * word 5: timestamp word 1
106 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000107 */
108static unsigned int macb_dma_desc_get_size(struct macb *bp)
109{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100110#ifdef MACB_EXT_DESC
111 unsigned int desc_size;
112
113 switch (bp->hw_dma_cap) {
114 case HW_DMA_CAP_64B:
115 desc_size = sizeof(struct macb_dma_desc)
116 + sizeof(struct macb_dma_desc_64);
117 break;
118 case HW_DMA_CAP_PTP:
119 desc_size = sizeof(struct macb_dma_desc)
120 + sizeof(struct macb_dma_desc_ptp);
121 break;
122 case HW_DMA_CAP_64B_PTP:
123 desc_size = sizeof(struct macb_dma_desc)
124 + sizeof(struct macb_dma_desc_64)
125 + sizeof(struct macb_dma_desc_ptp);
126 break;
127 default:
128 desc_size = sizeof(struct macb_dma_desc);
129 }
130 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000131#endif
132 return sizeof(struct macb_dma_desc);
133}
134
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100135static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000136{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100137#ifdef MACB_EXT_DESC
138 switch (bp->hw_dma_cap) {
139 case HW_DMA_CAP_64B:
140 case HW_DMA_CAP_PTP:
141 desc_idx <<= 1;
142 break;
143 case HW_DMA_CAP_64B_PTP:
144 desc_idx *= 3;
145 break;
146 default:
147 break;
148 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100150 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000151}
152
153#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
154static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
155{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100156 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
157 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
158 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000159}
160#endif
161
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000162/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500163static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000164{
Zach Brownb410d132016-10-19 09:56:57 -0500165 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000166}
167
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100168static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
169 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000170{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000171 index = macb_tx_ring_wrap(queue->bp, index);
172 index = macb_adj_dma_desc_idx(queue->bp, index);
173 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000174}
175
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100176static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
177 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000178{
Zach Brownb410d132016-10-19 09:56:57 -0500179 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000180}
181
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100182static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183{
184 dma_addr_t offset;
185
Zach Brownb410d132016-10-19 09:56:57 -0500186 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000187 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100189 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000190}
191
Zach Brownb410d132016-10-19 09:56:57 -0500192static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000193{
Zach Brownb410d132016-10-19 09:56:57 -0500194 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195}
196
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000197static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000198{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000199 index = macb_rx_ring_wrap(queue->bp, index);
200 index = macb_adj_dma_desc_idx(queue->bp, index);
201 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000202}
203
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000204static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000206 return queue->rx_buffers + queue->bp->rx_buffer_size *
207 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000208}
209
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300210/* I/O accessors */
211static u32 hw_readl_native(struct macb *bp, int offset)
212{
213 return __raw_readl(bp->regs + offset);
214}
215
216static void hw_writel_native(struct macb *bp, int offset, u32 value)
217{
218 __raw_writel(value, bp->regs + offset);
219}
220
221static u32 hw_readl(struct macb *bp, int offset)
222{
223 return readl_relaxed(bp->regs + offset);
224}
225
226static void hw_writel(struct macb *bp, int offset, u32 value)
227{
228 writel_relaxed(value, bp->regs + offset);
229}
230
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700231/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700232 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300233 * descriptor access.
234 */
235static bool hw_is_native_io(void __iomem *addr)
236{
237 u32 value = MACB_BIT(LLB);
238
239 __raw_writel(value, addr + MACB_NCR);
240 value = __raw_readl(addr + MACB_NCR);
241
242 /* Write 0 back to disable everything */
243 __raw_writel(0, addr + MACB_NCR);
244
245 return value == MACB_BIT(LLB);
246}
247
248static bool hw_is_gem(void __iomem *addr, bool native_io)
249{
250 u32 id;
251
252 if (native_io)
253 id = __raw_readl(addr + MACB_MID);
254 else
255 id = readl_relaxed(addr + MACB_MID);
256
257 return MACB_BFEXT(IDNUM, id) >= 0x2;
258}
259
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100260static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261{
262 u32 bottom;
263 u16 top;
264
265 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000266 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100267 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000268 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000269
270 /* Clear unused address register sets */
271 macb_or_gem_writel(bp, SA2B, 0);
272 macb_or_gem_writel(bp, SA2T, 0);
273 macb_or_gem_writel(bp, SA3B, 0);
274 macb_or_gem_writel(bp, SA3T, 0);
275 macb_or_gem_writel(bp, SA4B, 0);
276 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100277}
278
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100279static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100280{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000281 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 u32 bottom;
283 u16 top;
284 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000285 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100286
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900287 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000288
Moritz Fischeraa50b552016-03-29 19:11:13 -0700289 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000290 for (i = 0; i < 4; i++) {
291 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
292 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100293
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000294 if (pdata && pdata->rev_eth_addr) {
295 addr[5] = bottom & 0xff;
296 addr[4] = (bottom >> 8) & 0xff;
297 addr[3] = (bottom >> 16) & 0xff;
298 addr[2] = (bottom >> 24) & 0xff;
299 addr[1] = top & 0xff;
300 addr[0] = (top & 0xff00) >> 8;
301 } else {
302 addr[0] = bottom & 0xff;
303 addr[1] = (bottom >> 8) & 0xff;
304 addr[2] = (bottom >> 16) & 0xff;
305 addr[3] = (bottom >> 24) & 0xff;
306 addr[4] = top & 0xff;
307 addr[5] = (top >> 8) & 0xff;
308 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000310 if (is_valid_ether_addr(addr)) {
311 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
312 return;
313 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700314 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300316 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000317 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100318}
319
frederic RODO6c36a702007-07-12 19:07:24 +0200320static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321{
frederic RODO6c36a702007-07-12 19:07:24 +0200322 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323 int value;
324
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100325 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
326 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200327 | MACB_BF(PHYA, mii_id)
328 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100329 | MACB_BF(CODE, MACB_MAN_CODE)));
330
frederic RODO6c36a702007-07-12 19:07:24 +0200331 /* wait for end of transfer */
332 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
333 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334
335 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336
337 return value;
338}
339
frederic RODO6c36a702007-07-12 19:07:24 +0200340static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
341 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342{
frederic RODO6c36a702007-07-12 19:07:24 +0200343 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344
345 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
346 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200347 | MACB_BF(PHYA, mii_id)
348 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200350 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
frederic RODO6c36a702007-07-12 19:07:24 +0200352 /* wait for end of transfer */
353 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
354 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100355
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356 return 0;
357}
358
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800359/**
360 * macb_set_tx_clk() - Set a clock to a new frequency
361 * @clk Pointer to the clock to change
362 * @rate New frequency in Hz
363 * @dev Pointer to the struct net_device
364 */
365static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
366{
367 long ferr, rate, rate_rounded;
368
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100369 if (!clk)
370 return;
371
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800372 switch (speed) {
373 case SPEED_10:
374 rate = 2500000;
375 break;
376 case SPEED_100:
377 rate = 25000000;
378 break;
379 case SPEED_1000:
380 rate = 125000000;
381 break;
382 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800383 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800384 }
385
386 rate_rounded = clk_round_rate(clk, rate);
387 if (rate_rounded < 0)
388 return;
389
390 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
391 * is not satisfied.
392 */
393 ferr = abs(rate_rounded - rate);
394 ferr = DIV_ROUND_UP(ferr, rate / 100000);
395 if (ferr > 5)
396 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700397 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800398
399 if (clk_set_rate(clk, rate_rounded))
400 netdev_err(dev, "adjusting tx_clk failed.\n");
401}
402
frederic RODO6c36a702007-07-12 19:07:24 +0200403static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100404{
frederic RODO6c36a702007-07-12 19:07:24 +0200405 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200406 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200407 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200408 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100409
frederic RODO6c36a702007-07-12 19:07:24 +0200410 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 if (phydev->link) {
413 if ((bp->speed != phydev->speed) ||
414 (bp->duplex != phydev->duplex)) {
415 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100416
frederic RODO6c36a702007-07-12 19:07:24 +0200417 reg = macb_readl(bp, NCFGR);
418 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000419 if (macb_is_gem(bp))
420 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200421
422 if (phydev->duplex)
423 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900424 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200425 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200426 if (phydev->speed == SPEED_1000 &&
427 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000428 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200429
Patrice Vilchez140b7552012-10-31 06:04:50 +0000430 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200431
432 bp->speed = phydev->speed;
433 bp->duplex = phydev->duplex;
434 status_change = 1;
435 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436 }
437
frederic RODO6c36a702007-07-12 19:07:24 +0200438 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700439 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200440 bp->speed = 0;
441 bp->duplex = -1;
442 }
443 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
frederic RODO6c36a702007-07-12 19:07:24 +0200445 status_change = 1;
446 }
447
448 spin_unlock_irqrestore(&bp->lock, flags);
449
450 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000451 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500452 /* Update the TX clock rate if and only if the link is
453 * up and there has been a link change.
454 */
455 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
456
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000457 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000458 netdev_info(dev, "link up (%d/%s)\n",
459 phydev->speed,
460 phydev->duplex == DUPLEX_FULL ?
461 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000462 } else {
463 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000464 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000465 }
frederic RODO6c36a702007-07-12 19:07:24 +0200466 }
467}
468
469/* based on au1000_eth. c*/
470static int macb_mii_probe(struct net_device *dev)
471{
472 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000473 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000474 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000475 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000476 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200477
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200478 if (bp->phy_node) {
479 phydev = of_phy_connect(dev, bp->phy_node,
480 &macb_handle_link_change, 0,
481 bp->phy_interface);
482 if (!phydev)
483 return -ENODEV;
484 } else {
485 phydev = phy_find_first(bp->mii_bus);
486 if (!phydev) {
487 netdev_err(dev, "no PHY found\n");
488 return -ENXIO;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000489 }
frederic RODO6c36a702007-07-12 19:07:24 +0200490
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200491 pdata = dev_get_platdata(&bp->pdev->dev);
492 if (pdata) {
493 if (gpio_is_valid(pdata->phy_irq_pin)) {
494 ret = devm_gpio_request(&bp->pdev->dev,
495 pdata->phy_irq_pin, "phy int");
496 if (!ret) {
497 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
498 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
499 }
500 } else {
501 phydev->irq = PHY_POLL;
502 }
503 }
504
505 /* attach the mac to the phy */
506 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
507 bp->phy_interface);
508 if (ret) {
509 netdev_err(dev, "Could not attach to PHY\n");
510 return ret;
511 }
frederic RODO6c36a702007-07-12 19:07:24 +0200512 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100513
frederic RODO6c36a702007-07-12 19:07:24 +0200514 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200515 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000516 phydev->supported &= PHY_GBIT_FEATURES;
517 else
518 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100519
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500520 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
521 phydev->supported &= ~SUPPORTED_1000baseT_Half;
522
frederic RODO6c36a702007-07-12 19:07:24 +0200523 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100524
frederic RODO6c36a702007-07-12 19:07:24 +0200525 bp->link = 0;
526 bp->speed = 0;
527 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200528
529 return 0;
530}
531
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100532static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200533{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000534 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200535 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200536 int err = -ENXIO, i;
537
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200538 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200539 macb_writel(bp, NCR, MACB_BIT(MPE));
540
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700541 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700542 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200543 err = -ENOMEM;
544 goto err_out;
545 }
546
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700547 bp->mii_bus->name = "MACB_mii_bus";
548 bp->mii_bus->read = &macb_mdio_read;
549 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000550 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700551 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700552 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700553 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900554 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700555
Jamie Iles91523942011-02-28 04:05:25 +0000556 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200557
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200558 np = bp->pdev->dev.of_node;
559 if (np) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200560 if (of_phy_is_fixed_link(np)) {
561 if (of_phy_register_fixed_link(np) < 0) {
562 dev_err(&bp->pdev->dev,
563 "broken fixed-link specification\n");
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200564 goto err_out_unregister_bus;
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200565 }
566 bp->phy_node = of_node_get(np);
567
568 err = mdiobus_register(bp->mii_bus);
569 } else {
570 /* try dt phy registration */
571 err = of_mdiobus_register(bp->mii_bus, np);
572
573 /* fallback to standard phy registration if no phy were
574 * found during dt phy registration
575 */
576 if (!err && !phy_find_first(bp->mii_bus)) {
577 for (i = 0; i < PHY_MAX_ADDR; i++) {
578 struct phy_device *phydev;
579
580 phydev = mdiobus_scan(bp->mii_bus, i);
581 if (IS_ERR(phydev) &&
582 PTR_ERR(phydev) != -ENODEV) {
583 err = PTR_ERR(phydev);
584 break;
585 }
586 }
587
588 if (err)
589 goto err_out_unregister_bus;
590 }
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200591 }
592 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000593 for (i = 0; i < PHY_MAX_ADDR; i++)
594 bp->mii_bus->irq[i] = PHY_POLL;
595
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200596 if (pdata)
597 bp->mii_bus->phy_mask = pdata->phy_mask;
598
599 err = mdiobus_register(bp->mii_bus);
600 }
601
602 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100603 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200604
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200605 err = macb_mii_probe(bp->dev);
606 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200607 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200608
609 return 0;
610
611err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700612 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700613err_out_free_mdiobus:
Michael Grzeschik66ee6a02017-11-08 09:56:35 +0100614 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100615 if (np && of_phy_is_fixed_link(np))
616 of_phy_deregister_fixed_link(np);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700617 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200618err_out:
619 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100620}
621
622static void macb_update_stats(struct macb *bp)
623{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000624 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
625 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300626 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100627
628 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
629
Moritz Fischer96ec6312016-03-29 19:11:11 -0700630 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700631 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100632}
633
Nicolas Ferree86cd532012-10-31 06:04:57 +0000634static int macb_halt_tx(struct macb *bp)
635{
636 unsigned long halt_time, timeout;
637 u32 status;
638
639 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
640
641 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
642 do {
643 halt_time = jiffies;
644 status = macb_readl(bp, TSR);
645 if (!(status & MACB_BIT(TGO)))
646 return 0;
647
648 usleep_range(10, 250);
649 } while (time_before(halt_time, timeout));
650
651 return -ETIMEDOUT;
652}
653
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200654static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
655{
656 if (tx_skb->mapping) {
657 if (tx_skb->mapped_as_page)
658 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
659 tx_skb->size, DMA_TO_DEVICE);
660 else
661 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
662 tx_skb->size, DMA_TO_DEVICE);
663 tx_skb->mapping = 0;
664 }
665
666 if (tx_skb->skb) {
667 dev_kfree_skb_any(tx_skb->skb);
668 tx_skb->skb = NULL;
669 }
670}
671
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000672static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530673{
Harini Katakamfff80192016-08-09 13:15:53 +0530674#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000675 struct macb_dma_desc_64 *desc_64;
676
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100677 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000678 desc_64 = macb_64b_desc(bp, desc);
679 desc_64->addrh = upper_32_bits(addr);
680 }
Harini Katakamfff80192016-08-09 13:15:53 +0530681#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000682 desc->addr = lower_32_bits(addr);
683}
684
685static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
686{
687 dma_addr_t addr = 0;
688#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
689 struct macb_dma_desc_64 *desc_64;
690
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100691 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000692 desc_64 = macb_64b_desc(bp, desc);
693 addr = ((u64)(desc_64->addrh) << 32);
694 }
695#endif
696 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
697 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530698}
699
Nicolas Ferree86cd532012-10-31 06:04:57 +0000700static void macb_tx_error_task(struct work_struct *work)
701{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100702 struct macb_queue *queue = container_of(work, struct macb_queue,
703 tx_error_task);
704 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000705 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100706 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000707 struct sk_buff *skb;
708 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100709 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000710
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100711 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
712 (unsigned int)(queue - bp->queues),
713 queue->tx_tail, queue->tx_head);
714
715 /* Prevent the queue IRQ handlers from running: each of them may call
716 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
717 * As explained below, we have to halt the transmission before updating
718 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
719 * network engine about the macb/gem being halted.
720 */
721 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000722
723 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100724 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000725
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700726 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000727 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100728 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000729 */
730 if (macb_halt_tx(bp))
731 /* Just complain for now, reinitializing TX path can be good */
732 netdev_err(bp->dev, "BUG: halt tx timed out\n");
733
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700734 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000735 * Free transmit buffers in upper layer.
736 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100737 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
738 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000739
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000741 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100742 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000743 skb = tx_skb->skb;
744
745 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200746 /* skb is set for the last buffer of the frame */
747 while (!skb) {
748 macb_tx_unmap(bp, tx_skb);
749 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100750 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200751 skb = tx_skb->skb;
752 }
753
754 /* ctrl still refers to the first buffer descriptor
755 * since it's the only one written back by the hardware
756 */
757 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
758 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500759 macb_tx_ring_wrap(bp, tail),
760 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200761 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000762 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200763 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000764 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200765 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000766 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700767 /* "Buffers exhausted mid-frame" errors may only happen
768 * if the driver is buggy, so complain loudly about
769 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000770 */
771 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
772 netdev_err(bp->dev,
773 "BUG: TX buffers exhausted mid-frame\n");
774
775 desc->ctrl = ctrl | MACB_BIT(TX_USED);
776 }
777
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200778 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000779 }
780
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100781 /* Set end of TX queue */
782 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000783 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100784 desc->ctrl = MACB_BIT(TX_USED);
785
Nicolas Ferree86cd532012-10-31 06:04:57 +0000786 /* Make descriptor updates visible to hardware */
787 wmb();
788
789 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000790 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530791#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100792 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000793 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530794#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000795 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100796 queue->tx_head = 0;
797 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000798
799 /* Housework before enabling TX IRQ */
800 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100801 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
802
803 /* Now we are ready to start transmission again */
804 netif_tx_start_all_queues(bp->dev);
805 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
806
807 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000808}
809
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100810static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100811{
812 unsigned int tail;
813 unsigned int head;
814 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100815 struct macb *bp = queue->bp;
816 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100817
818 status = macb_readl(bp, TSR);
819 macb_writel(bp, TSR, status);
820
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000821 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100822 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000823
Nicolas Ferree86cd532012-10-31 06:04:57 +0000824 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700825 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100826
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100827 head = queue->tx_head;
828 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000829 struct macb_tx_skb *tx_skb;
830 struct sk_buff *skb;
831 struct macb_dma_desc *desc;
832 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100833
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100834 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100835
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000836 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100837 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000838
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000839 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200841 /* TX_USED bit is only set by hardware on the very first buffer
842 * descriptor of the transmitted frame.
843 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000844 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100845 break;
846
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200847 /* Process all buffers of the current transmitted frame */
848 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100849 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200850 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000851
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200852 /* First, update TX stats if needed */
853 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100854 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
855 /* skb now belongs to timestamp buffer
856 * and will be removed later
857 */
858 tx_skb->skb = NULL;
859 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200860 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500861 macb_tx_ring_wrap(bp, tail),
862 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200863 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000864 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200865 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000866 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200867 }
868
869 /* Now we can safely release resources */
870 macb_tx_unmap(bp, tx_skb);
871
872 /* skb is set only for the last buffer of the frame.
873 * WARNING: at this point skb has been freed by
874 * macb_tx_unmap().
875 */
876 if (skb)
877 break;
878 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100879 }
880
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100881 queue->tx_tail = tail;
882 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
883 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500884 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100885 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100886}
887
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000888static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000889{
890 unsigned int entry;
891 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000892 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000893 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000894 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000895
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000896 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
897 bp->rx_ring_size) > 0) {
898 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000899
900 /* Make hw descriptor updates visible to CPU */
901 rmb();
902
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000903 queue->rx_prepared_head++;
904 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000905
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000906 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000907 /* allocate sk_buff for this free entry in ring */
908 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700909 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000910 netdev_err(bp->dev,
911 "Unable to allocate sk_buff\n");
912 break;
913 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000914
915 /* now fill corresponding descriptor entry */
916 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700917 bp->rx_buffer_size,
918 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800919 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
920 dev_kfree_skb(skb);
921 break;
922 }
923
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000924 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000925
Zach Brownb410d132016-10-19 09:56:57 -0500926 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000927 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000928 macb_set_addr(bp, desc, paddr);
929 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000930
931 /* properly align Ethernet header */
932 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530933 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000934 desc->addr &= ~MACB_BIT(RX_USED);
935 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000936 }
937 }
938
939 /* Make descriptor updates visible to hardware */
940 wmb();
941
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000942 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
943 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000944}
945
946/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000947static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000948 unsigned int end)
949{
950 unsigned int frag;
951
952 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000953 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700954
Nicolas Ferre4df95132013-06-04 21:57:12 +0000955 desc->addr &= ~MACB_BIT(RX_USED);
956 }
957
958 /* Make descriptor updates visible to hardware */
959 wmb();
960
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700961 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000962 * whatever caused this is updated, so we don't have to record
963 * anything.
964 */
965}
966
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000967static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000968{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000969 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000970 unsigned int len;
971 unsigned int entry;
972 struct sk_buff *skb;
973 struct macb_dma_desc *desc;
974 int count = 0;
975
976 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530977 u32 ctrl;
978 dma_addr_t addr;
979 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000980
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000981 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
982 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000983
984 /* Make hw descriptor updates visible to CPU */
985 rmb();
986
Harini Katakamfff80192016-08-09 13:15:53 +0530987 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000988 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000989 ctrl = desc->ctrl;
990
Harini Katakamfff80192016-08-09 13:15:53 +0530991 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000992 break;
993
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000994 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000995 count++;
996
997 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
998 netdev_err(bp->dev,
999 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001000 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001001 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001002 break;
1003 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001004 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001005 if (unlikely(!skb)) {
1006 netdev_err(bp->dev,
1007 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001008 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001009 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001010 break;
1011 }
1012 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001013 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301014 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001015
1016 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1017
1018 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001019 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001020 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001021
1022 skb->protocol = eth_type_trans(skb, bp->dev);
1023 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001024 if (bp->dev->features & NETIF_F_RXCSUM &&
1025 !(bp->dev->flags & IFF_PROMISC) &&
1026 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1027 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001028
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001029 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001030 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001031 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001032 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001033
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001034 gem_ptp_do_rxstamp(bp, skb, desc);
1035
Nicolas Ferre4df95132013-06-04 21:57:12 +00001036#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1037 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1038 skb->len, skb->csum);
1039 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001040 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001041 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1042 skb->data, 32, true);
1043#endif
1044
1045 netif_receive_skb(skb);
1046 }
1047
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001048 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001049
1050 return count;
1051}
1052
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001053static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001054 unsigned int last_frag)
1055{
1056 unsigned int len;
1057 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001058 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001060 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001061 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001062
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001063 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301064 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001066 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001067 macb_rx_ring_wrap(bp, first_frag),
1068 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001070 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001071 * first buffer. Since the header is 14 bytes, this makes the
1072 * payload word-aligned.
1073 *
1074 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1075 * the two padding bytes into the skb so that we avoid hitting
1076 * the slowpath in memcpy(), and pull them off afterwards.
1077 */
1078 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001079 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001080 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001081 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001082 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001083 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001084 if (frag == last_frag)
1085 break;
1086 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001087
1088 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001089 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001090
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001091 return 1;
1092 }
1093
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001094 offset = 0;
1095 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001096 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001097 skb_put(skb, len);
1098
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001099 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001100 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001101
1102 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001103 if (unlikely(frag != last_frag)) {
1104 dev_kfree_skb_any(skb);
1105 return -1;
1106 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001107 frag_len = len - offset;
1108 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001109 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001110 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001111 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001112 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001113 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001114 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001115
1116 if (frag == last_frag)
1117 break;
1118 }
1119
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001120 /* Make descriptor updates visible to hardware */
1121 wmb();
1122
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001123 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001124 skb->protocol = eth_type_trans(skb, bp->dev);
1125
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001126 bp->dev->stats.rx_packets++;
1127 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001128 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001129 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001130 netif_receive_skb(skb);
1131
1132 return 0;
1133}
1134
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001135static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001136{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001137 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001138 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001139 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001140 int i;
1141
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001142 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001143 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001144 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001145 macb_set_addr(bp, desc, addr);
1146 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001147 addr += bp->rx_buffer_size;
1148 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001149 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001150 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001151}
1152
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001153static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001154{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001155 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001156 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001157 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001158 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001159 int first_frag = -1;
1160
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001161 for (tail = queue->rx_tail; budget > 0; tail++) {
1162 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001163 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001164
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001165 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001166 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001167
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001168 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001169
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001170 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001171 break;
1172
1173 if (ctrl & MACB_BIT(RX_SOF)) {
1174 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001175 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001176 first_frag = tail;
1177 }
1178
1179 if (ctrl & MACB_BIT(RX_EOF)) {
1180 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001181
1182 if (unlikely(first_frag == -1)) {
1183 reset_rx_queue = true;
1184 continue;
1185 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001186
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001187 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001188 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001189 if (unlikely(dropped < 0)) {
1190 reset_rx_queue = true;
1191 continue;
1192 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001193 if (!dropped) {
1194 received++;
1195 budget--;
1196 }
1197 }
1198 }
1199
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001200 if (unlikely(reset_rx_queue)) {
1201 unsigned long flags;
1202 u32 ctrl;
1203
1204 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1205
1206 spin_lock_irqsave(&bp->lock, flags);
1207
1208 ctrl = macb_readl(bp, NCR);
1209 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1210
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001211 macb_init_rx_ring(queue);
1212 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001213
1214 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1215
1216 spin_unlock_irqrestore(&bp->lock, flags);
1217 return received;
1218 }
1219
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001220 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001221 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001222 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001223 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001224
1225 return received;
1226}
1227
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001228static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001230 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1231 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001232 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233 u32 status;
1234
1235 status = macb_readl(bp, RSR);
1236 macb_writel(bp, RSR, status);
1237
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001238 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001239 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001241 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001242 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001243 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001244
Nicolas Ferre8770e912013-02-12 11:08:48 +01001245 /* Packets received while interrupts were disabled */
1246 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001247 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001248 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001249 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001250 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001251 } else {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001252 queue_writel(queue, IER, MACB_RX_INT_FLAGS);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001253 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001254 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001255
1256 /* TODO: Handle errors */
1257
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001258 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001259}
1260
Harini Katakam032dc412018-01-27 12:09:01 +05301261static void macb_hresp_error_task(unsigned long data)
1262{
1263 struct macb *bp = (struct macb *)data;
1264 struct net_device *dev = bp->dev;
1265 struct macb_queue *queue = bp->queues;
1266 unsigned int q;
1267 u32 ctrl;
1268
1269 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1270 queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
1271 MACB_TX_INT_FLAGS |
1272 MACB_BIT(HRESP));
1273 }
1274 ctrl = macb_readl(bp, NCR);
1275 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1276 macb_writel(bp, NCR, ctrl);
1277
1278 netif_tx_stop_all_queues(dev);
1279 netif_carrier_off(dev);
1280
1281 bp->macbgem_ops.mog_init_rings(bp);
1282
1283 /* Initialize TX and RX buffers */
1284 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1285 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1286#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1287 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1288 queue_writel(queue, RBQPH,
1289 upper_32_bits(queue->rx_ring_dma));
1290#endif
1291 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1292#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1293 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1294 queue_writel(queue, TBQPH,
1295 upper_32_bits(queue->tx_ring_dma));
1296#endif
1297
1298 /* Enable interrupts */
1299 queue_writel(queue, IER,
1300 MACB_RX_INT_FLAGS |
1301 MACB_TX_INT_FLAGS |
1302 MACB_BIT(HRESP));
1303 }
1304
1305 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1306 macb_writel(bp, NCR, ctrl);
1307
1308 netif_carrier_on(dev);
1309 netif_tx_start_all_queues(dev);
1310}
1311
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001312static irqreturn_t macb_interrupt(int irq, void *dev_id)
1313{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001314 struct macb_queue *queue = dev_id;
1315 struct macb *bp = queue->bp;
1316 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001317 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001318
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001319 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001320
1321 if (unlikely(!status))
1322 return IRQ_NONE;
1323
1324 spin_lock(&bp->lock);
1325
1326 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001327 /* close possible race with dev_close */
1328 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001329 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001330 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1331 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332 break;
1333 }
1334
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001335 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1336 (unsigned int)(queue - bp->queues),
1337 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001338
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001339 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001340 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001341 * until we have processed the buffers. The
1342 * scheduling call may fail if the poll routine
1343 * is already scheduled, so disable interrupts
1344 * now.
1345 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001346 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001347 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001348 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001349
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001350 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001351 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001352 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001353 }
1354 }
1355
Nicolas Ferree86cd532012-10-31 06:04:57 +00001356 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001357 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1358 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001359
1360 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001361 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001362
Nicolas Ferree86cd532012-10-31 06:04:57 +00001363 break;
1364 }
1365
1366 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001367 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001368
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001369 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001370 * add that if/when we get our hands on a full-blown MII PHY.
1371 */
1372
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001373 /* There is a hardware issue under heavy load where DMA can
1374 * stop, this causes endless "used buffer descriptor read"
1375 * interrupts but it can be cleared by re-enabling RX. See
1376 * the at91 manual, section 41.3.1 or the Zynq manual
1377 * section 16.7.4 for details.
1378 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001379 if (status & MACB_BIT(RXUBR)) {
1380 ctrl = macb_readl(bp, NCR);
1381 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001382 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001383 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1384
1385 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001386 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001387 }
1388
Alexander Steinb19f7f72011-04-13 05:03:24 +00001389 if (status & MACB_BIT(ISR_ROVR)) {
1390 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001391 if (macb_is_gem(bp))
1392 bp->hw_stats.gem.rx_overruns++;
1393 else
1394 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001395
1396 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001397 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001398 }
1399
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001400 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301401 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001402 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001403
1404 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001405 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001406 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001407 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001408 }
1409
1410 spin_unlock(&bp->lock);
1411
1412 return IRQ_HANDLED;
1413}
1414
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001415#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001416/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001417 * to allow network i/o with interrupts disabled.
1418 */
1419static void macb_poll_controller(struct net_device *dev)
1420{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001421 struct macb *bp = netdev_priv(dev);
1422 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001423 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001424 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001425
1426 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001427 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1428 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001429 local_irq_restore(flags);
1430}
1431#endif
1432
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001433static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001434 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001435 struct sk_buff *skb,
1436 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001437{
1438 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001439 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001440 struct macb_tx_skb *tx_skb = NULL;
1441 struct macb_dma_desc *desc;
1442 unsigned int offset, size, count = 0;
1443 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001444 unsigned int eof = 1, mss_mfs = 0;
1445 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1446
1447 /* LSO */
1448 if (skb_shinfo(skb)->gso_size != 0) {
1449 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1450 /* UDP - UFO */
1451 lso_ctrl = MACB_LSO_UFO_ENABLE;
1452 else
1453 /* TCP - TSO */
1454 lso_ctrl = MACB_LSO_TSO_ENABLE;
1455 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001456
1457 /* First, map non-paged data */
1458 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001459
1460 /* first buffer length */
1461 size = hdrlen;
1462
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001463 offset = 0;
1464 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001465 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001466 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001467
1468 mapping = dma_map_single(&bp->pdev->dev,
1469 skb->data + offset,
1470 size, DMA_TO_DEVICE);
1471 if (dma_mapping_error(&bp->pdev->dev, mapping))
1472 goto dma_error;
1473
1474 /* Save info to properly release resources */
1475 tx_skb->skb = NULL;
1476 tx_skb->mapping = mapping;
1477 tx_skb->size = size;
1478 tx_skb->mapped_as_page = false;
1479
1480 len -= size;
1481 offset += size;
1482 count++;
1483 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001484
1485 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001486 }
1487
1488 /* Then, map paged data from fragments */
1489 for (f = 0; f < nr_frags; f++) {
1490 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1491
1492 len = skb_frag_size(frag);
1493 offset = 0;
1494 while (len) {
1495 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001496 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001497 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001498
1499 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1500 offset, size, DMA_TO_DEVICE);
1501 if (dma_mapping_error(&bp->pdev->dev, mapping))
1502 goto dma_error;
1503
1504 /* Save info to properly release resources */
1505 tx_skb->skb = NULL;
1506 tx_skb->mapping = mapping;
1507 tx_skb->size = size;
1508 tx_skb->mapped_as_page = true;
1509
1510 len -= size;
1511 offset += size;
1512 count++;
1513 tx_head++;
1514 }
1515 }
1516
1517 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001518 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001519 netdev_err(bp->dev, "BUG! empty skb!\n");
1520 return 0;
1521 }
1522
1523 /* This is the last buffer of the frame: save socket buffer */
1524 tx_skb->skb = skb;
1525
1526 /* Update TX ring: update buffer descriptors in reverse order
1527 * to avoid race condition
1528 */
1529
1530 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1531 * to set the end of TX queue
1532 */
1533 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001534 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001535 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001536 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001537 desc->ctrl = ctrl;
1538
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001539 if (lso_ctrl) {
1540 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1541 /* include header and FCS in value given to h/w */
1542 mss_mfs = skb_shinfo(skb)->gso_size +
1543 skb_transport_offset(skb) +
1544 ETH_FCS_LEN;
1545 else /* TSO */ {
1546 mss_mfs = skb_shinfo(skb)->gso_size;
1547 /* TCP Sequence Number Source Select
1548 * can be set only for TSO
1549 */
1550 seq_ctrl = 0;
1551 }
1552 }
1553
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001554 do {
1555 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001556 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001557 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001558 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001559
1560 ctrl = (u32)tx_skb->size;
1561 if (eof) {
1562 ctrl |= MACB_BIT(TX_LAST);
1563 eof = 0;
1564 }
Zach Brownb410d132016-10-19 09:56:57 -05001565 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001566 ctrl |= MACB_BIT(TX_WRAP);
1567
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001568 /* First descriptor is header descriptor */
1569 if (i == queue->tx_head) {
1570 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1571 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1572 } else
1573 /* Only set MSS/MFS on payload descriptors
1574 * (second or later descriptor)
1575 */
1576 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1577
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001578 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001579 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001580 /* desc->addr must be visible to hardware before clearing
1581 * 'TX_USED' bit in desc->ctrl.
1582 */
1583 wmb();
1584 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001585 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001586
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001587 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001588
1589 return count;
1590
1591dma_error:
1592 netdev_err(bp->dev, "TX DMA map failed\n");
1593
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001594 for (i = queue->tx_head; i != tx_head; i++) {
1595 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001596
1597 macb_tx_unmap(bp, tx_skb);
1598 }
1599
1600 return 0;
1601}
1602
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001603static netdev_features_t macb_features_check(struct sk_buff *skb,
1604 struct net_device *dev,
1605 netdev_features_t features)
1606{
1607 unsigned int nr_frags, f;
1608 unsigned int hdrlen;
1609
1610 /* Validate LSO compatibility */
1611
1612 /* there is only one buffer */
1613 if (!skb_is_nonlinear(skb))
1614 return features;
1615
1616 /* length of header */
1617 hdrlen = skb_transport_offset(skb);
1618 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1619 hdrlen += tcp_hdrlen(skb);
1620
1621 /* For LSO:
1622 * When software supplies two or more payload buffers all payload buffers
1623 * apart from the last must be a multiple of 8 bytes in size.
1624 */
1625 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1626 return features & ~MACB_NETIF_LSO;
1627
1628 nr_frags = skb_shinfo(skb)->nr_frags;
1629 /* No need to check last fragment */
1630 nr_frags--;
1631 for (f = 0; f < nr_frags; f++) {
1632 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1633
1634 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1635 return features & ~MACB_NETIF_LSO;
1636 }
1637 return features;
1638}
1639
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001640static inline int macb_clear_csum(struct sk_buff *skb)
1641{
1642 /* no change for packets without checksum offloading */
1643 if (skb->ip_summed != CHECKSUM_PARTIAL)
1644 return 0;
1645
1646 /* make sure we can modify the header */
1647 if (unlikely(skb_cow_head(skb, 0)))
1648 return -1;
1649
1650 /* initialize checksum field
1651 * This is required - at least for Zynq, which otherwise calculates
1652 * wrong UDP header checksums for UDP packets with UDP data len <=2
1653 */
1654 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1655 return 0;
1656}
1657
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001658static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1659{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001660 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001661 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001662 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001663 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001664 unsigned int desc_cnt, nr_frags, frag_size, f;
1665 unsigned int hdrlen;
1666 bool is_lso, is_udp = 0;
1667
1668 is_lso = (skb_shinfo(skb)->gso_size != 0);
1669
1670 if (is_lso) {
1671 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1672
1673 /* length of headers */
1674 if (is_udp)
1675 /* only queue eth + ip headers separately for UDP */
1676 hdrlen = skb_transport_offset(skb);
1677 else
1678 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1679 if (skb_headlen(skb) < hdrlen) {
1680 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1681 /* if this is required, would need to copy to single buffer */
1682 return NETDEV_TX_BUSY;
1683 }
1684 } else
1685 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001686
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001687#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1688 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001689 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1690 queue_index, skb->len, skb->head, skb->data,
1691 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001692 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1693 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001694#endif
1695
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001696 /* Count how many TX buffer descriptors are needed to send this
1697 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001698 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001699 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001700 if (is_lso && (skb_headlen(skb) > hdrlen))
1701 /* extra header descriptor if also payload in first buffer */
1702 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1703 else
1704 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001705 nr_frags = skb_shinfo(skb)->nr_frags;
1706 for (f = 0; f < nr_frags; f++) {
1707 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001708 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001709 }
1710
Dongdong Deng48719532009-08-23 19:49:07 -07001711 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001712
1713 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001714 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001715 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001716 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001717 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001718 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001719 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001720 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001721 }
1722
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001723 if (macb_clear_csum(skb)) {
1724 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001725 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001726 }
1727
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001728 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001729 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001730 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001731 goto unlock;
1732 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001733
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001734 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001735 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001736 skb_tx_timestamp(skb);
1737
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001738 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1739
Zach Brownb410d132016-10-19 09:56:57 -05001740 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001741 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001742
Soren Brinkmann92030902014-03-04 08:46:39 -08001743unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001744 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745
Patrick McHardy6ed10652009-06-23 06:03:08 +00001746 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001747}
1748
Nicolas Ferre4df95132013-06-04 21:57:12 +00001749static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001750{
1751 if (!macb_is_gem(bp)) {
1752 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1753 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001754 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001755
Nicolas Ferre1b447912013-06-04 21:57:11 +00001756 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001757 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001758 "RX buffer must be multiple of %d bytes, expanding\n",
1759 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001760 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001761 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001762 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001763 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001764
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001765 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001766 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001767}
1768
Nicolas Ferre4df95132013-06-04 21:57:12 +00001769static void gem_free_rx_buffers(struct macb *bp)
1770{
1771 struct sk_buff *skb;
1772 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001773 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001774 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001775 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001776 int i;
1777
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001778 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1779 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001780 continue;
1781
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001782 for (i = 0; i < bp->rx_ring_size; i++) {
1783 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001784
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001785 if (!skb)
1786 continue;
1787
1788 desc = macb_rx_desc(queue, i);
1789 addr = macb_get_addr(bp, desc);
1790
1791 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1792 DMA_FROM_DEVICE);
1793 dev_kfree_skb_any(skb);
1794 skb = NULL;
1795 }
1796
1797 kfree(queue->rx_skbuff);
1798 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001799 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001800}
1801
1802static void macb_free_rx_buffers(struct macb *bp)
1803{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001804 struct macb_queue *queue = &bp->queues[0];
1805
1806 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001807 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001808 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001809 queue->rx_buffers, queue->rx_buffers_dma);
1810 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001811 }
1812}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001813
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814static void macb_free_consistent(struct macb *bp)
1815{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001816 struct macb_queue *queue;
1817 unsigned int q;
1818
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001819 queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001820 bp->macbgem_ops.mog_free_rx_buffers(bp);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001821 if (queue->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001822 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001823 queue->rx_ring, queue->rx_ring_dma);
1824 queue->rx_ring = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001825 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001826
1827 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1828 kfree(queue->tx_skb);
1829 queue->tx_skb = NULL;
1830 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001831 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001832 queue->tx_ring, queue->tx_ring_dma);
1833 queue->tx_ring = NULL;
1834 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001835 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001836}
1837
1838static int gem_alloc_rx_buffers(struct macb *bp)
1839{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001840 struct macb_queue *queue;
1841 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001842 int size;
1843
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001844 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1845 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1846 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1847 if (!queue->rx_skbuff)
1848 return -ENOMEM;
1849 else
1850 netdev_dbg(bp->dev,
1851 "Allocated %d RX struct sk_buff entries at %p\n",
1852 bp->rx_ring_size, queue->rx_skbuff);
1853 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001854 return 0;
1855}
1856
1857static int macb_alloc_rx_buffers(struct macb *bp)
1858{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001859 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001860 int size;
1861
Zach Brownb410d132016-10-19 09:56:57 -05001862 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001863 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1864 &queue->rx_buffers_dma, GFP_KERNEL);
1865 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001866 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001867
1868 netdev_dbg(bp->dev,
1869 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001870 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001871 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001872}
1873
1874static int macb_alloc_consistent(struct macb *bp)
1875{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001876 struct macb_queue *queue;
1877 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001878 int size;
1879
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001880 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001881 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001882 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1883 &queue->tx_ring_dma,
1884 GFP_KERNEL);
1885 if (!queue->tx_ring)
1886 goto out_err;
1887 netdev_dbg(bp->dev,
1888 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1889 q, size, (unsigned long)queue->tx_ring_dma,
1890 queue->tx_ring);
1891
Zach Brownb410d132016-10-19 09:56:57 -05001892 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001893 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1894 if (!queue->tx_skb)
1895 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001896
1897 size = RX_RING_BYTES(bp);
1898 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1899 &queue->rx_ring_dma, GFP_KERNEL);
1900 if (!queue->rx_ring)
1901 goto out_err;
1902 netdev_dbg(bp->dev,
1903 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1904 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001905 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001906 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001907 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001908
1909 return 0;
1910
1911out_err:
1912 macb_free_consistent(bp);
1913 return -ENOMEM;
1914}
1915
Nicolas Ferre4df95132013-06-04 21:57:12 +00001916static void gem_init_rings(struct macb *bp)
1917{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001918 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001919 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001920 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001921 int i;
1922
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001923 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001924 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001925 desc = macb_tx_desc(queue, i);
1926 macb_set_addr(bp, desc, 0);
1927 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001928 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001929 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001930 queue->tx_head = 0;
1931 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001932
1933 queue->rx_tail = 0;
1934 queue->rx_prepared_head = 0;
1935
1936 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001937 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001938
Nicolas Ferre4df95132013-06-04 21:57:12 +00001939}
1940
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001941static void macb_init_rings(struct macb *bp)
1942{
1943 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001944 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001945
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001946 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001947
Zach Brownb410d132016-10-19 09:56:57 -05001948 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001949 desc = macb_tx_desc(&bp->queues[0], i);
1950 macb_set_addr(bp, desc, 0);
1951 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001952 }
Ben Shelton21d35152015-04-22 17:28:54 -05001953 bp->queues[0].tx_head = 0;
1954 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001955 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001956}
1957
1958static void macb_reset_hw(struct macb *bp)
1959{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001960 struct macb_queue *queue;
1961 unsigned int q;
1962
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001963 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001964 * more gracefully?)
1965 */
1966 macb_writel(bp, NCR, 0);
1967
1968 /* Clear the stats registers (XXX: Update stats first?) */
1969 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1970
1971 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001972 macb_writel(bp, TSR, -1);
1973 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001974
1975 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001976 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1977 queue_writel(queue, IDR, -1);
1978 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001979 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1980 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001981 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001982}
1983
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001984static u32 gem_mdc_clk_div(struct macb *bp)
1985{
1986 u32 config;
1987 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1988
1989 if (pclk_hz <= 20000000)
1990 config = GEM_BF(CLK, GEM_CLK_DIV8);
1991 else if (pclk_hz <= 40000000)
1992 config = GEM_BF(CLK, GEM_CLK_DIV16);
1993 else if (pclk_hz <= 80000000)
1994 config = GEM_BF(CLK, GEM_CLK_DIV32);
1995 else if (pclk_hz <= 120000000)
1996 config = GEM_BF(CLK, GEM_CLK_DIV48);
1997 else if (pclk_hz <= 160000000)
1998 config = GEM_BF(CLK, GEM_CLK_DIV64);
1999 else
2000 config = GEM_BF(CLK, GEM_CLK_DIV96);
2001
2002 return config;
2003}
2004
2005static u32 macb_mdc_clk_div(struct macb *bp)
2006{
2007 u32 config;
2008 unsigned long pclk_hz;
2009
2010 if (macb_is_gem(bp))
2011 return gem_mdc_clk_div(bp);
2012
2013 pclk_hz = clk_get_rate(bp->pclk);
2014 if (pclk_hz <= 20000000)
2015 config = MACB_BF(CLK, MACB_CLK_DIV8);
2016 else if (pclk_hz <= 40000000)
2017 config = MACB_BF(CLK, MACB_CLK_DIV16);
2018 else if (pclk_hz <= 80000000)
2019 config = MACB_BF(CLK, MACB_CLK_DIV32);
2020 else
2021 config = MACB_BF(CLK, MACB_CLK_DIV64);
2022
2023 return config;
2024}
2025
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002026/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002027 * should program. We find the width from decoding the design configuration
2028 * register to find the maximum supported data bus width.
2029 */
2030static u32 macb_dbw(struct macb *bp)
2031{
2032 if (!macb_is_gem(bp))
2033 return 0;
2034
2035 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2036 case 4:
2037 return GEM_BF(DBW, GEM_DBW128);
2038 case 2:
2039 return GEM_BF(DBW, GEM_DBW64);
2040 case 1:
2041 default:
2042 return GEM_BF(DBW, GEM_DBW32);
2043 }
2044}
2045
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002046/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002047 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002048 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002049 * (if not supported by FIFO, it will fallback to default)
2050 * - set both rx/tx packet buffers to full memory size
2051 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002052 */
2053static void macb_configure_dma(struct macb *bp)
2054{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002055 struct macb_queue *queue;
2056 u32 buffer_size;
2057 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002058 u32 dmacfg;
2059
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002060 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002061 if (macb_is_gem(bp)) {
2062 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002063 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2064 if (q)
2065 queue_writel(queue, RBQS, buffer_size);
2066 else
2067 dmacfg |= GEM_BF(RXBS, buffer_size);
2068 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002069 if (bp->dma_burst_length)
2070 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002071 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302072 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302073
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002074 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302075 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2076 else
2077 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2078
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002079 if (bp->dev->features & NETIF_F_HW_CSUM)
2080 dmacfg |= GEM_BIT(TXCOEN);
2081 else
2082 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302083
2084#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002085 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002086 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302087#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002088#ifdef CONFIG_MACB_USE_HWSTAMP
2089 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2090 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2091#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002092 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2093 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002094 gem_writel(bp, DMACFG, dmacfg);
2095 }
2096}
2097
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002098static void macb_init_hw(struct macb *bp)
2099{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002100 struct macb_queue *queue;
2101 unsigned int q;
2102
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002103 u32 config;
2104
2105 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002106 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002107
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002108 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302109 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2110 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002111 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002112 config |= MACB_BIT(PAE); /* PAuse Enable */
2113 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002114 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302115 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2116 else
2117 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002118 if (bp->dev->flags & IFF_PROMISC)
2119 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002120 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2121 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002122 if (!(bp->dev->flags & IFF_BROADCAST))
2123 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002124 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002125 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002126 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302127 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002128 bp->speed = SPEED_10;
2129 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302130 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002131 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302132 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002133
Jamie Iles0116da42011-03-14 17:38:30 +00002134 macb_configure_dma(bp);
2135
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002136 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002137 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002138 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2139#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2140 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2141 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2142#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002143 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302144#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002145 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002146 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302147#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002148
2149 /* Enable interrupts */
2150 queue_writel(queue, IER,
2151 MACB_RX_INT_FLAGS |
2152 MACB_TX_INT_FLAGS |
2153 MACB_BIT(HRESP));
2154 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002155
2156 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02002157 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002158}
2159
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002160/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002161 * locations in the memory map. The least significant bits are stored
2162 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2163 *
2164 * The unicast hash enable and the multicast hash enable bits in the
2165 * network configuration register enable the reception of hash matched
2166 * frames. The destination address is reduced to a 6 bit index into
2167 * the 64 bit hash register using the following hash function. The
2168 * hash function is an exclusive or of every sixth bit of the
2169 * destination address.
2170 *
2171 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2172 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2173 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2174 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2175 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2176 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2177 *
2178 * da[0] represents the least significant bit of the first byte
2179 * received, that is, the multicast/unicast indicator, and da[47]
2180 * represents the most significant bit of the last byte received. If
2181 * the hash index, hi[n], points to a bit that is set in the hash
2182 * register then the frame will be matched according to whether the
2183 * frame is multicast or unicast. A multicast match will be signalled
2184 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2185 * index points to a bit set in the hash register. A unicast match
2186 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2187 * and the hash index points to a bit set in the hash register. To
2188 * receive all multicast frames, the hash register should be set with
2189 * all ones and the multicast hash enable bit should be set in the
2190 * network configuration register.
2191 */
2192
2193static inline int hash_bit_value(int bitnr, __u8 *addr)
2194{
2195 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2196 return 1;
2197 return 0;
2198}
2199
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002200/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002201static int hash_get_index(__u8 *addr)
2202{
2203 int i, j, bitval;
2204 int hash_index = 0;
2205
2206 for (j = 0; j < 6; j++) {
2207 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002208 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002209
2210 hash_index |= (bitval << j);
2211 }
2212
2213 return hash_index;
2214}
2215
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002216/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002217static void macb_sethashtable(struct net_device *dev)
2218{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002219 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002220 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002221 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002222 struct macb *bp = netdev_priv(dev);
2223
Moritz Fischeraa50b552016-03-29 19:11:13 -07002224 mc_filter[0] = 0;
2225 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002226
Jiri Pirko22bedad32010-04-01 21:22:57 +00002227 netdev_for_each_mc_addr(ha, dev) {
2228 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002229 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2230 }
2231
Jamie Ilesf75ba502011-11-08 10:12:32 +00002232 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2233 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002234}
2235
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002236/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002237static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002238{
2239 unsigned long cfg;
2240 struct macb *bp = netdev_priv(dev);
2241
2242 cfg = macb_readl(bp, NCFGR);
2243
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002244 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002245 /* Enable promiscuous mode */
2246 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002247
2248 /* Disable RX checksum offload */
2249 if (macb_is_gem(bp))
2250 cfg &= ~GEM_BIT(RXCOEN);
2251 } else {
2252 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002253 cfg &= ~MACB_BIT(CAF);
2254
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002255 /* Enable RX checksum offload only if requested */
2256 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2257 cfg |= GEM_BIT(RXCOEN);
2258 }
2259
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002260 if (dev->flags & IFF_ALLMULTI) {
2261 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002262 macb_or_gem_writel(bp, HRB, -1);
2263 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002264 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002265 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002266 /* Enable specific multicasts */
2267 macb_sethashtable(dev);
2268 cfg |= MACB_BIT(NCFGR_MTI);
2269 } else if (dev->flags & (~IFF_ALLMULTI)) {
2270 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002271 macb_or_gem_writel(bp, HRB, 0);
2272 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002273 cfg &= ~MACB_BIT(NCFGR_MTI);
2274 }
2275
2276 macb_writel(bp, NCFGR, cfg);
2277}
2278
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002279static int macb_open(struct net_device *dev)
2280{
2281 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002282 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002283 struct macb_queue *queue;
2284 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002285 int err;
2286
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002287 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002288
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002289 /* carrier starts down */
2290 netif_carrier_off(dev);
2291
frederic RODO6c36a702007-07-12 19:07:24 +02002292 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002293 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002294 return -EAGAIN;
2295
Nicolas Ferre1b447912013-06-04 21:57:11 +00002296 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002297 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002298
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002299 err = macb_alloc_consistent(bp);
2300 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002301 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2302 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002303 return err;
2304 }
2305
Nicolas Ferre4df95132013-06-04 21:57:12 +00002306 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002307 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002308
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002309 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2310 napi_enable(&queue->napi);
2311
frederic RODO6c36a702007-07-12 19:07:24 +02002312 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002313 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002314
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002315 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002316
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002317 if (bp->ptp_info)
2318 bp->ptp_info->ptp_init(dev);
2319
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002320 return 0;
2321}
2322
2323static int macb_close(struct net_device *dev)
2324{
2325 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002326 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002327 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002328 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002329
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002330 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002331
2332 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2333 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002334
Philippe Reynes0a912812016-06-22 00:32:35 +02002335 if (dev->phydev)
2336 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002337
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002338 spin_lock_irqsave(&bp->lock, flags);
2339 macb_reset_hw(bp);
2340 netif_carrier_off(dev);
2341 spin_unlock_irqrestore(&bp->lock, flags);
2342
2343 macb_free_consistent(bp);
2344
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002345 if (bp->ptp_info)
2346 bp->ptp_info->ptp_remove(dev);
2347
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002348 return 0;
2349}
2350
Harini Katakama5898ea2015-05-06 22:27:18 +05302351static int macb_change_mtu(struct net_device *dev, int new_mtu)
2352{
Harini Katakama5898ea2015-05-06 22:27:18 +05302353 if (netif_running(dev))
2354 return -EBUSY;
2355
Harini Katakama5898ea2015-05-06 22:27:18 +05302356 dev->mtu = new_mtu;
2357
2358 return 0;
2359}
2360
Jamie Ilesa494ed82011-03-09 16:26:35 +00002361static void gem_update_stats(struct macb *bp)
2362{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002363 struct macb_queue *queue;
2364 unsigned int i, q, idx;
2365 unsigned long *stat;
2366
Jamie Ilesa494ed82011-03-09 16:26:35 +00002367 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002368
Xander Huff3ff13f12015-01-13 16:15:51 -06002369 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2370 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002371 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002372
2373 bp->ethtool_stats[i] += val;
2374 *p += val;
2375
2376 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2377 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002378 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002379 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002380 *(++p) += val;
2381 }
2382 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002383
2384 idx = GEM_STATS_LEN;
2385 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2386 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2387 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002388}
2389
2390static struct net_device_stats *gem_get_stats(struct macb *bp)
2391{
2392 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002393 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002394
2395 gem_update_stats(bp);
2396
2397 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2398 hwstat->rx_alignment_errors +
2399 hwstat->rx_resource_errors +
2400 hwstat->rx_overruns +
2401 hwstat->rx_oversize_frames +
2402 hwstat->rx_jabbers +
2403 hwstat->rx_undersized_frames +
2404 hwstat->rx_length_field_frame_errors);
2405 nstat->tx_errors = (hwstat->tx_late_collisions +
2406 hwstat->tx_excessive_collisions +
2407 hwstat->tx_underrun +
2408 hwstat->tx_carrier_sense_errors);
2409 nstat->multicast = hwstat->rx_multicast_frames;
2410 nstat->collisions = (hwstat->tx_single_collision_frames +
2411 hwstat->tx_multiple_collision_frames +
2412 hwstat->tx_excessive_collisions);
2413 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2414 hwstat->rx_jabbers +
2415 hwstat->rx_undersized_frames +
2416 hwstat->rx_length_field_frame_errors);
2417 nstat->rx_over_errors = hwstat->rx_resource_errors;
2418 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2419 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2420 nstat->rx_fifo_errors = hwstat->rx_overruns;
2421 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2422 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2423 nstat->tx_fifo_errors = hwstat->tx_underrun;
2424
2425 return nstat;
2426}
2427
Xander Huff3ff13f12015-01-13 16:15:51 -06002428static void gem_get_ethtool_stats(struct net_device *dev,
2429 struct ethtool_stats *stats, u64 *data)
2430{
2431 struct macb *bp;
2432
2433 bp = netdev_priv(dev);
2434 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002435 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2436 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002437}
2438
2439static int gem_get_sset_count(struct net_device *dev, int sset)
2440{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002441 struct macb *bp = netdev_priv(dev);
2442
Xander Huff3ff13f12015-01-13 16:15:51 -06002443 switch (sset) {
2444 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002445 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002446 default:
2447 return -EOPNOTSUPP;
2448 }
2449}
2450
2451static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2452{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002453 char stat_string[ETH_GSTRING_LEN];
2454 struct macb *bp = netdev_priv(dev);
2455 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002456 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002457 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002458
2459 switch (sset) {
2460 case ETH_SS_STATS:
2461 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2462 memcpy(p, gem_statistics[i].stat_string,
2463 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002464
2465 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2466 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2467 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2468 q, queue_statistics[i].stat_string);
2469 memcpy(p, stat_string, ETH_GSTRING_LEN);
2470 }
2471 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002472 break;
2473 }
2474}
2475
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002476static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002477{
2478 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002479 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002480 struct macb_stats *hwstat = &bp->hw_stats.macb;
2481
2482 if (macb_is_gem(bp))
2483 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002484
frederic RODO6c36a702007-07-12 19:07:24 +02002485 /* read stats from hardware */
2486 macb_update_stats(bp);
2487
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002488 /* Convert HW stats into netdevice stats */
2489 nstat->rx_errors = (hwstat->rx_fcs_errors +
2490 hwstat->rx_align_errors +
2491 hwstat->rx_resource_errors +
2492 hwstat->rx_overruns +
2493 hwstat->rx_oversize_pkts +
2494 hwstat->rx_jabbers +
2495 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002496 hwstat->rx_length_mismatch);
2497 nstat->tx_errors = (hwstat->tx_late_cols +
2498 hwstat->tx_excessive_cols +
2499 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002500 hwstat->tx_carrier_errors +
2501 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002502 nstat->collisions = (hwstat->tx_single_cols +
2503 hwstat->tx_multiple_cols +
2504 hwstat->tx_excessive_cols);
2505 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2506 hwstat->rx_jabbers +
2507 hwstat->rx_undersize_pkts +
2508 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002509 nstat->rx_over_errors = hwstat->rx_resource_errors +
2510 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002511 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2512 nstat->rx_frame_errors = hwstat->rx_align_errors;
2513 nstat->rx_fifo_errors = hwstat->rx_overruns;
2514 /* XXX: What does "missed" mean? */
2515 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2516 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2517 nstat->tx_fifo_errors = hwstat->tx_underruns;
2518 /* Don't know about heartbeat or window errors... */
2519
2520 return nstat;
2521}
2522
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002523static int macb_get_regs_len(struct net_device *netdev)
2524{
2525 return MACB_GREGS_NBR * sizeof(u32);
2526}
2527
2528static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2529 void *p)
2530{
2531 struct macb *bp = netdev_priv(dev);
2532 unsigned int tail, head;
2533 u32 *regs_buff = p;
2534
2535 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2536 | MACB_GREGS_VERSION;
2537
Zach Brownb410d132016-10-19 09:56:57 -05002538 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2539 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002540
2541 regs_buff[0] = macb_readl(bp, NCR);
2542 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2543 regs_buff[2] = macb_readl(bp, NSR);
2544 regs_buff[3] = macb_readl(bp, TSR);
2545 regs_buff[4] = macb_readl(bp, RBQP);
2546 regs_buff[5] = macb_readl(bp, TBQP);
2547 regs_buff[6] = macb_readl(bp, RSR);
2548 regs_buff[7] = macb_readl(bp, IMR);
2549
2550 regs_buff[8] = tail;
2551 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002552 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2553 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002554
Neil Armstrongce721a72016-01-05 14:39:16 +01002555 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2556 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002557 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002558 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002559}
2560
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002561static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2562{
2563 struct macb *bp = netdev_priv(netdev);
2564
2565 wol->supported = 0;
2566 wol->wolopts = 0;
2567
2568 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2569 wol->supported = WAKE_MAGIC;
2570
2571 if (bp->wol & MACB_WOL_ENABLED)
2572 wol->wolopts |= WAKE_MAGIC;
2573 }
2574}
2575
2576static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2577{
2578 struct macb *bp = netdev_priv(netdev);
2579
2580 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2581 (wol->wolopts & ~WAKE_MAGIC))
2582 return -EOPNOTSUPP;
2583
2584 if (wol->wolopts & WAKE_MAGIC)
2585 bp->wol |= MACB_WOL_ENABLED;
2586 else
2587 bp->wol &= ~MACB_WOL_ENABLED;
2588
2589 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2590
2591 return 0;
2592}
2593
Zach Brown8441bb32016-10-19 09:56:58 -05002594static void macb_get_ringparam(struct net_device *netdev,
2595 struct ethtool_ringparam *ring)
2596{
2597 struct macb *bp = netdev_priv(netdev);
2598
2599 ring->rx_max_pending = MAX_RX_RING_SIZE;
2600 ring->tx_max_pending = MAX_TX_RING_SIZE;
2601
2602 ring->rx_pending = bp->rx_ring_size;
2603 ring->tx_pending = bp->tx_ring_size;
2604}
2605
2606static int macb_set_ringparam(struct net_device *netdev,
2607 struct ethtool_ringparam *ring)
2608{
2609 struct macb *bp = netdev_priv(netdev);
2610 u32 new_rx_size, new_tx_size;
2611 unsigned int reset = 0;
2612
2613 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2614 return -EINVAL;
2615
2616 new_rx_size = clamp_t(u32, ring->rx_pending,
2617 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2618 new_rx_size = roundup_pow_of_two(new_rx_size);
2619
2620 new_tx_size = clamp_t(u32, ring->tx_pending,
2621 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2622 new_tx_size = roundup_pow_of_two(new_tx_size);
2623
2624 if ((new_tx_size == bp->tx_ring_size) &&
2625 (new_rx_size == bp->rx_ring_size)) {
2626 /* nothing to do */
2627 return 0;
2628 }
2629
2630 if (netif_running(bp->dev)) {
2631 reset = 1;
2632 macb_close(bp->dev);
2633 }
2634
2635 bp->rx_ring_size = new_rx_size;
2636 bp->tx_ring_size = new_tx_size;
2637
2638 if (reset)
2639 macb_open(bp->dev);
2640
2641 return 0;
2642}
2643
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002644#ifdef CONFIG_MACB_USE_HWSTAMP
2645static unsigned int gem_get_tsu_rate(struct macb *bp)
2646{
2647 struct clk *tsu_clk;
2648 unsigned int tsu_rate;
2649
2650 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2651 if (!IS_ERR(tsu_clk))
2652 tsu_rate = clk_get_rate(tsu_clk);
2653 /* try pclk instead */
2654 else if (!IS_ERR(bp->pclk)) {
2655 tsu_clk = bp->pclk;
2656 tsu_rate = clk_get_rate(tsu_clk);
2657 } else
2658 return -ENOTSUPP;
2659 return tsu_rate;
2660}
2661
2662static s32 gem_get_ptp_max_adj(void)
2663{
2664 return 64000000;
2665}
2666
2667static int gem_get_ts_info(struct net_device *dev,
2668 struct ethtool_ts_info *info)
2669{
2670 struct macb *bp = netdev_priv(dev);
2671
2672 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2673 ethtool_op_get_ts_info(dev, info);
2674 return 0;
2675 }
2676
2677 info->so_timestamping =
2678 SOF_TIMESTAMPING_TX_SOFTWARE |
2679 SOF_TIMESTAMPING_RX_SOFTWARE |
2680 SOF_TIMESTAMPING_SOFTWARE |
2681 SOF_TIMESTAMPING_TX_HARDWARE |
2682 SOF_TIMESTAMPING_RX_HARDWARE |
2683 SOF_TIMESTAMPING_RAW_HARDWARE;
2684 info->tx_types =
2685 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2686 (1 << HWTSTAMP_TX_OFF) |
2687 (1 << HWTSTAMP_TX_ON);
2688 info->rx_filters =
2689 (1 << HWTSTAMP_FILTER_NONE) |
2690 (1 << HWTSTAMP_FILTER_ALL);
2691
2692 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2693
2694 return 0;
2695}
2696
2697static struct macb_ptp_info gem_ptp_info = {
2698 .ptp_init = gem_ptp_init,
2699 .ptp_remove = gem_ptp_remove,
2700 .get_ptp_max_adj = gem_get_ptp_max_adj,
2701 .get_tsu_rate = gem_get_tsu_rate,
2702 .get_ts_info = gem_get_ts_info,
2703 .get_hwtst = gem_get_hwtst,
2704 .set_hwtst = gem_set_hwtst,
2705};
2706#endif
2707
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002708static int macb_get_ts_info(struct net_device *netdev,
2709 struct ethtool_ts_info *info)
2710{
2711 struct macb *bp = netdev_priv(netdev);
2712
2713 if (bp->ptp_info)
2714 return bp->ptp_info->get_ts_info(netdev, info);
2715
2716 return ethtool_op_get_ts_info(netdev, info);
2717}
2718
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002719static void gem_enable_flow_filters(struct macb *bp, bool enable)
2720{
2721 struct ethtool_rx_fs_item *item;
2722 u32 t2_scr;
2723 int num_t2_scr;
2724
2725 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2726
2727 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2728 struct ethtool_rx_flow_spec *fs = &item->fs;
2729 struct ethtool_tcpip4_spec *tp4sp_m;
2730
2731 if (fs->location >= num_t2_scr)
2732 continue;
2733
2734 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2735
2736 /* enable/disable screener regs for the flow entry */
2737 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2738
2739 /* only enable fields with no masking */
2740 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2741
2742 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2743 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2744 else
2745 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2746
2747 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2748 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2749 else
2750 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2751
2752 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2753 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2754 else
2755 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2756
2757 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2758 }
2759}
2760
2761static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2762{
2763 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2764 uint16_t index = fs->location;
2765 u32 w0, w1, t2_scr;
2766 bool cmp_a = false;
2767 bool cmp_b = false;
2768 bool cmp_c = false;
2769
2770 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2771 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2772
2773 /* ignore field if any masking set */
2774 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2775 /* 1st compare reg - IP source address */
2776 w0 = 0;
2777 w1 = 0;
2778 w0 = tp4sp_v->ip4src;
2779 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2780 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2781 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2782 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2783 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2784 cmp_a = true;
2785 }
2786
2787 /* ignore field if any masking set */
2788 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2789 /* 2nd compare reg - IP destination address */
2790 w0 = 0;
2791 w1 = 0;
2792 w0 = tp4sp_v->ip4dst;
2793 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2794 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2795 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2796 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2797 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2798 cmp_b = true;
2799 }
2800
2801 /* ignore both port fields if masking set in both */
2802 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2803 /* 3rd compare reg - source port, destination port */
2804 w0 = 0;
2805 w1 = 0;
2806 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2807 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2808 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2809 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2810 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2811 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2812 } else {
2813 /* only one port definition */
2814 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2815 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2816 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2817 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2818 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2819 } else { /* dst port */
2820 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2821 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2822 }
2823 }
2824 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2825 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2826 cmp_c = true;
2827 }
2828
2829 t2_scr = 0;
2830 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2831 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2832 if (cmp_a)
2833 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2834 if (cmp_b)
2835 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2836 if (cmp_c)
2837 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2838 gem_writel_n(bp, SCRT2, index, t2_scr);
2839}
2840
2841static int gem_add_flow_filter(struct net_device *netdev,
2842 struct ethtool_rxnfc *cmd)
2843{
2844 struct macb *bp = netdev_priv(netdev);
2845 struct ethtool_rx_flow_spec *fs = &cmd->fs;
2846 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002847 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002848 int ret = -EINVAL;
2849 bool added = false;
2850
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06002851 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002852 if (newfs == NULL)
2853 return -ENOMEM;
2854 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
2855
2856 netdev_dbg(netdev,
2857 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2858 fs->flow_type, (int)fs->ring_cookie, fs->location,
2859 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2860 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2861 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
2862
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002863 spin_lock_irqsave(&bp->rx_fs_lock, flags);
2864
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002865 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002866 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2867 if (item->fs.location > newfs->fs.location) {
2868 list_add_tail(&newfs->list, &item->list);
2869 added = true;
2870 break;
2871 } else if (item->fs.location == fs->location) {
2872 netdev_err(netdev, "Rule not added: location %d not free!\n",
2873 fs->location);
2874 ret = -EBUSY;
2875 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002876 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002877 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06002878 if (!added)
2879 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002880
2881 gem_prog_cmp_regs(bp, fs);
2882 bp->rx_fs_list.count++;
2883 /* enable filtering if NTUPLE on */
2884 if (netdev->features & NETIF_F_NTUPLE)
2885 gem_enable_flow_filters(bp, 1);
2886
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002887 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002888 return 0;
2889
2890err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002891 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002892 kfree(newfs);
2893 return ret;
2894}
2895
2896static int gem_del_flow_filter(struct net_device *netdev,
2897 struct ethtool_rxnfc *cmd)
2898{
2899 struct macb *bp = netdev_priv(netdev);
2900 struct ethtool_rx_fs_item *item;
2901 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002902 unsigned long flags;
2903
2904 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002905
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002906 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2907 if (item->fs.location == cmd->fs.location) {
2908 /* disable screener regs for the flow entry */
2909 fs = &(item->fs);
2910 netdev_dbg(netdev,
2911 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
2912 fs->flow_type, (int)fs->ring_cookie, fs->location,
2913 htonl(fs->h_u.tcp_ip4_spec.ip4src),
2914 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
2915 htons(fs->h_u.tcp_ip4_spec.psrc),
2916 htons(fs->h_u.tcp_ip4_spec.pdst));
2917
2918 gem_writel_n(bp, SCRT2, fs->location, 0);
2919
2920 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002921 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002922 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
2923 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002924 return 0;
2925 }
2926 }
2927
Julia Cartwright7038cdb2017-12-05 18:02:49 -06002928 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002929 return -EINVAL;
2930}
2931
2932static int gem_get_flow_entry(struct net_device *netdev,
2933 struct ethtool_rxnfc *cmd)
2934{
2935 struct macb *bp = netdev_priv(netdev);
2936 struct ethtool_rx_fs_item *item;
2937
2938 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2939 if (item->fs.location == cmd->fs.location) {
2940 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
2941 return 0;
2942 }
2943 }
2944 return -EINVAL;
2945}
2946
2947static int gem_get_all_flow_entries(struct net_device *netdev,
2948 struct ethtool_rxnfc *cmd, u32 *rule_locs)
2949{
2950 struct macb *bp = netdev_priv(netdev);
2951 struct ethtool_rx_fs_item *item;
2952 uint32_t cnt = 0;
2953
2954 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2955 if (cnt == cmd->rule_cnt)
2956 return -EMSGSIZE;
2957 rule_locs[cnt] = item->fs.location;
2958 cnt++;
2959 }
2960 cmd->data = bp->max_tuples;
2961 cmd->rule_cnt = cnt;
2962
2963 return 0;
2964}
2965
2966static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2967 u32 *rule_locs)
2968{
2969 struct macb *bp = netdev_priv(netdev);
2970 int ret = 0;
2971
2972 switch (cmd->cmd) {
2973 case ETHTOOL_GRXRINGS:
2974 cmd->data = bp->num_queues;
2975 break;
2976 case ETHTOOL_GRXCLSRLCNT:
2977 cmd->rule_cnt = bp->rx_fs_list.count;
2978 break;
2979 case ETHTOOL_GRXCLSRULE:
2980 ret = gem_get_flow_entry(netdev, cmd);
2981 break;
2982 case ETHTOOL_GRXCLSRLALL:
2983 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
2984 break;
2985 default:
2986 netdev_err(netdev,
2987 "Command parameter %d is not supported\n", cmd->cmd);
2988 ret = -EOPNOTSUPP;
2989 }
2990
2991 return ret;
2992}
2993
2994static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2995{
2996 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002997 int ret;
2998
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002999 switch (cmd->cmd) {
3000 case ETHTOOL_SRXCLSRLINS:
3001 if ((cmd->fs.location >= bp->max_tuples)
3002 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3003 ret = -EINVAL;
3004 break;
3005 }
3006 ret = gem_add_flow_filter(netdev, cmd);
3007 break;
3008 case ETHTOOL_SRXCLSRLDEL:
3009 ret = gem_del_flow_filter(netdev, cmd);
3010 break;
3011 default:
3012 netdev_err(netdev,
3013 "Command parameter %d is not supported\n", cmd->cmd);
3014 ret = -EOPNOTSUPP;
3015 }
3016
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003017 return ret;
3018}
3019
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003020static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003021 .get_regs_len = macb_get_regs_len,
3022 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003023 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003024 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003025 .get_wol = macb_get_wol,
3026 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02003027 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3028 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003029 .get_ringparam = macb_get_ringparam,
3030 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003031};
Xander Huff8cd5a562015-01-15 15:55:20 -06003032
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003033static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003034 .get_regs_len = macb_get_regs_len,
3035 .get_regs = macb_get_regs,
3036 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003037 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003038 .get_ethtool_stats = gem_get_ethtool_stats,
3039 .get_strings = gem_get_ethtool_strings,
3040 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02003041 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3042 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003043 .get_ringparam = macb_get_ringparam,
3044 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003045 .get_rxnfc = gem_get_rxnfc,
3046 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003047};
3048
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003049static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003050{
Philippe Reynes0a912812016-06-22 00:32:35 +02003051 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003052 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003053
3054 if (!netif_running(dev))
3055 return -EINVAL;
3056
frederic RODO6c36a702007-07-12 19:07:24 +02003057 if (!phydev)
3058 return -ENODEV;
3059
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003060 if (!bp->ptp_info)
3061 return phy_mii_ioctl(phydev, rq, cmd);
3062
3063 switch (cmd) {
3064 case SIOCSHWTSTAMP:
3065 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3066 case SIOCGHWTSTAMP:
3067 return bp->ptp_info->get_hwtst(dev, rq);
3068 default:
3069 return phy_mii_ioctl(phydev, rq, cmd);
3070 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003071}
3072
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003073static int macb_set_features(struct net_device *netdev,
3074 netdev_features_t features)
3075{
3076 struct macb *bp = netdev_priv(netdev);
3077 netdev_features_t changed = features ^ netdev->features;
3078
3079 /* TX checksum offload */
3080 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3081 u32 dmacfg;
3082
3083 dmacfg = gem_readl(bp, DMACFG);
3084 if (features & NETIF_F_HW_CSUM)
3085 dmacfg |= GEM_BIT(TXCOEN);
3086 else
3087 dmacfg &= ~GEM_BIT(TXCOEN);
3088 gem_writel(bp, DMACFG, dmacfg);
3089 }
3090
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003091 /* RX checksum offload */
3092 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3093 u32 netcfg;
3094
3095 netcfg = gem_readl(bp, NCFGR);
3096 if (features & NETIF_F_RXCSUM &&
3097 !(netdev->flags & IFF_PROMISC))
3098 netcfg |= GEM_BIT(RXCOEN);
3099 else
3100 netcfg &= ~GEM_BIT(RXCOEN);
3101 gem_writel(bp, NCFGR, netcfg);
3102 }
3103
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003104 /* RX Flow Filters */
3105 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3106 bool turn_on = features & NETIF_F_NTUPLE;
3107
3108 gem_enable_flow_filters(bp, turn_on);
3109 }
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003110 return 0;
3111}
3112
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003113static const struct net_device_ops macb_netdev_ops = {
3114 .ndo_open = macb_open,
3115 .ndo_stop = macb_close,
3116 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003117 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003118 .ndo_get_stats = macb_get_stats,
3119 .ndo_do_ioctl = macb_ioctl,
3120 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303121 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003122 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003123#ifdef CONFIG_NET_POLL_CONTROLLER
3124 .ndo_poll_controller = macb_poll_controller,
3125#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003126 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003127 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003128};
3129
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003130/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003131 * and integration options used
3132 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003133static void macb_configure_caps(struct macb *bp,
3134 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003135{
3136 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003137
Nicolas Ferref6970502015-03-31 15:02:01 +02003138 if (dt_conf)
3139 bp->caps = dt_conf->caps;
3140
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003141 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003142 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3143
Nicolas Ferree1755872014-07-24 13:50:58 +02003144 dcfg = gem_readl(bp, DCFG1);
3145 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3146 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3147 dcfg = gem_readl(bp, DCFG2);
3148 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3149 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003150#ifdef CONFIG_MACB_USE_HWSTAMP
3151 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003152 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3153 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003154 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003155 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003156 bp->ptp_info = &gem_ptp_info;
3157 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003158 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003159#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003160 }
3161
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003162 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003163}
3164
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003165static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003166 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003167 unsigned int *queue_mask,
3168 unsigned int *num_queues)
3169{
3170 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003171
3172 *queue_mask = 0x1;
3173 *num_queues = 1;
3174
Nicolas Ferreda120112015-03-31 15:02:00 +02003175 /* is it macb or gem ?
3176 *
3177 * We need to read directly from the hardware here because
3178 * we are early in the probe process and don't have the
3179 * MACB_CAPS_MACB_IS_GEM flag positioned
3180 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003181 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003182 return;
3183
3184 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303185 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3186
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003187 *queue_mask |= 0x1;
3188
3189 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3190 if (*queue_mask & (1 << hw_q))
3191 (*num_queues)++;
3192}
3193
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003194static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303195 struct clk **hclk, struct clk **tx_clk,
3196 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003197{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003198 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003199 int err;
3200
Bartosz Folta83a77e92016-12-14 06:39:15 +00003201 pdata = dev_get_platdata(&pdev->dev);
3202 if (pdata) {
3203 *pclk = pdata->pclk;
3204 *hclk = pdata->hclk;
3205 } else {
3206 *pclk = devm_clk_get(&pdev->dev, "pclk");
3207 *hclk = devm_clk_get(&pdev->dev, "hclk");
3208 }
3209
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003210 if (IS_ERR(*pclk)) {
3211 err = PTR_ERR(*pclk);
3212 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3213 return err;
3214 }
3215
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003216 if (IS_ERR(*hclk)) {
3217 err = PTR_ERR(*hclk);
3218 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3219 return err;
3220 }
3221
3222 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3223 if (IS_ERR(*tx_clk))
3224 *tx_clk = NULL;
3225
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303226 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3227 if (IS_ERR(*rx_clk))
3228 *rx_clk = NULL;
3229
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003230 err = clk_prepare_enable(*pclk);
3231 if (err) {
3232 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3233 return err;
3234 }
3235
3236 err = clk_prepare_enable(*hclk);
3237 if (err) {
3238 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3239 goto err_disable_pclk;
3240 }
3241
3242 err = clk_prepare_enable(*tx_clk);
3243 if (err) {
3244 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3245 goto err_disable_hclk;
3246 }
3247
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303248 err = clk_prepare_enable(*rx_clk);
3249 if (err) {
3250 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3251 goto err_disable_txclk;
3252 }
3253
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003254 return 0;
3255
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303256err_disable_txclk:
3257 clk_disable_unprepare(*tx_clk);
3258
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003259err_disable_hclk:
3260 clk_disable_unprepare(*hclk);
3261
3262err_disable_pclk:
3263 clk_disable_unprepare(*pclk);
3264
3265 return err;
3266}
3267
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003268static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003269{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003270 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003271 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003272 struct macb *bp = netdev_priv(dev);
3273 struct macb_queue *queue;
3274 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003275 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003276
Zach Brownb410d132016-10-19 09:56:57 -05003277 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3278 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3279
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003280 /* set the queue register mapping once for all: queue0 has a special
3281 * register mapping but we don't want to test the queue index then
3282 * compute the corresponding register offset at run time.
3283 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003284 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003285 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003286 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003287
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003288 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003289 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003290 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003291 if (hw_q) {
3292 queue->ISR = GEM_ISR(hw_q - 1);
3293 queue->IER = GEM_IER(hw_q - 1);
3294 queue->IDR = GEM_IDR(hw_q - 1);
3295 queue->IMR = GEM_IMR(hw_q - 1);
3296 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003297 queue->RBQP = GEM_RBQP(hw_q - 1);
3298 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303299#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003300 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003301 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003302 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3303 }
Harini Katakamfff80192016-08-09 13:15:53 +05303304#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003305 } else {
3306 /* queue0 uses legacy registers */
3307 queue->ISR = MACB_ISR;
3308 queue->IER = MACB_IER;
3309 queue->IDR = MACB_IDR;
3310 queue->IMR = MACB_IMR;
3311 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003312 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303313#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003314 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003315 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003316 queue->RBQPH = MACB_RBQPH;
3317 }
Harini Katakamfff80192016-08-09 13:15:53 +05303318#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003319 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003320
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003321 /* get irq: here we use the linux queue index, not the hardware
3322 * queue index. the queue irq definitions in the device tree
3323 * must remove the optional gaps that could exist in the
3324 * hardware queue mask.
3325 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003326 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003327 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003328 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003329 if (err) {
3330 dev_err(&pdev->dev,
3331 "Unable to request IRQ %d (error %d)\n",
3332 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003333 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003334 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003335
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003336 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003337 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003338 }
3339
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003340 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003341
Nicolas Ferre4df95132013-06-04 21:57:12 +00003342 /* setup appropriated routines according to adapter type */
3343 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003344 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003345 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3346 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3347 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3348 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003349 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003350 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003351 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003352 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3353 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3354 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3355 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003356 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003357 }
3358
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003359 /* Set features */
3360 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003361
3362 /* Check LSO capability */
3363 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3364 dev->hw_features |= MACB_NETIF_LSO;
3365
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003366 /* Checksum offload is only available on gem with packet buffer */
3367 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003368 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003369 if (bp->caps & MACB_CAPS_SG_DISABLED)
3370 dev->hw_features &= ~NETIF_F_SG;
3371 dev->features = dev->hw_features;
3372
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003373 /* Check RX Flow Filters support.
3374 * Max Rx flows set by availability of screeners & compare regs:
3375 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3376 */
3377 reg = gem_readl(bp, DCFG8);
3378 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3379 GEM_BFEXT(T2SCR, reg));
3380 if (bp->max_tuples > 0) {
3381 /* also needs one ethtype match to check IPv4 */
3382 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3383 /* program this reg now */
3384 reg = 0;
3385 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3386 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3387 /* Filtering is supported in hw but don't enable it in kernel now */
3388 dev->hw_features |= NETIF_F_NTUPLE;
3389 /* init Rx flow definitions */
3390 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3391 bp->rx_fs_list.count = 0;
3392 spin_lock_init(&bp->rx_fs_lock);
3393 } else
3394 bp->max_tuples = 0;
3395 }
3396
Neil Armstrongce721a72016-01-05 14:39:16 +01003397 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3398 val = 0;
3399 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3400 val = GEM_BIT(RGMII);
3401 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003402 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003403 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003404 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003405 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003406
Neil Armstrongce721a72016-01-05 14:39:16 +01003407 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3408 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003409
Neil Armstrongce721a72016-01-05 14:39:16 +01003410 macb_or_gem_writel(bp, USRIO, val);
3411 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003412
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003413 /* Set MII management clock divider */
3414 val = macb_mdc_clk_div(bp);
3415 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303416 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3417 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003418 macb_writel(bp, NCFGR, val);
3419
3420 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003421}
3422
3423#if defined(CONFIG_OF)
3424/* 1518 rounded up */
3425#define AT91ETHER_MAX_RBUFF_SZ 0x600
3426/* max number of receive buffers */
3427#define AT91ETHER_MAX_RX_DESCR 9
3428
3429/* Initialize and start the Receiver and Transmit subsystems */
3430static int at91ether_start(struct net_device *dev)
3431{
3432 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003433 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003434 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003435 dma_addr_t addr;
3436 u32 ctl;
3437 int i;
3438
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003439 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003440 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003441 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003442 &q->rx_ring_dma, GFP_KERNEL);
3443 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003444 return -ENOMEM;
3445
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003446 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003447 AT91ETHER_MAX_RX_DESCR *
3448 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003449 &q->rx_buffers_dma, GFP_KERNEL);
3450 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003451 dma_free_coherent(&lp->pdev->dev,
3452 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003453 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003454 q->rx_ring, q->rx_ring_dma);
3455 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003456 return -ENOMEM;
3457 }
3458
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003459 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003460 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003461 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003462 macb_set_addr(lp, desc, addr);
3463 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003464 addr += AT91ETHER_MAX_RBUFF_SZ;
3465 }
3466
3467 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003468 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003469
3470 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003471 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003472
3473 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003474 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003475
3476 /* Enable Receive and Transmit */
3477 ctl = macb_readl(lp, NCR);
3478 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3479
3480 return 0;
3481}
3482
3483/* Open the ethernet interface */
3484static int at91ether_open(struct net_device *dev)
3485{
3486 struct macb *lp = netdev_priv(dev);
3487 u32 ctl;
3488 int ret;
3489
3490 /* Clear internal statistics */
3491 ctl = macb_readl(lp, NCR);
3492 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3493
3494 macb_set_hwaddr(lp);
3495
3496 ret = at91ether_start(dev);
3497 if (ret)
3498 return ret;
3499
3500 /* Enable MAC interrupts */
3501 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3502 MACB_BIT(RXUBR) |
3503 MACB_BIT(ISR_TUND) |
3504 MACB_BIT(ISR_RLE) |
3505 MACB_BIT(TCOMP) |
3506 MACB_BIT(ISR_ROVR) |
3507 MACB_BIT(HRESP));
3508
3509 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003510 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003511
3512 netif_start_queue(dev);
3513
3514 return 0;
3515}
3516
3517/* Close the interface */
3518static int at91ether_close(struct net_device *dev)
3519{
3520 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003521 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003522 u32 ctl;
3523
3524 /* Disable Receiver and Transmitter */
3525 ctl = macb_readl(lp, NCR);
3526 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3527
3528 /* Disable MAC interrupts */
3529 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3530 MACB_BIT(RXUBR) |
3531 MACB_BIT(ISR_TUND) |
3532 MACB_BIT(ISR_RLE) |
3533 MACB_BIT(TCOMP) |
3534 MACB_BIT(ISR_ROVR) |
3535 MACB_BIT(HRESP));
3536
3537 netif_stop_queue(dev);
3538
3539 dma_free_coherent(&lp->pdev->dev,
3540 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003541 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003542 q->rx_ring, q->rx_ring_dma);
3543 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003544
3545 dma_free_coherent(&lp->pdev->dev,
3546 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003547 q->rx_buffers, q->rx_buffers_dma);
3548 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003549
3550 return 0;
3551}
3552
3553/* Transmit packet */
3554static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
3555{
3556 struct macb *lp = netdev_priv(dev);
3557
3558 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3559 netif_stop_queue(dev);
3560
3561 /* Store packet information (to free when Tx completed) */
3562 lp->skb = skb;
3563 lp->skb_length = skb->len;
3564 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3565 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003566 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3567 dev_kfree_skb_any(skb);
3568 dev->stats.tx_dropped++;
3569 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3570 return NETDEV_TX_OK;
3571 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003572
3573 /* Set address of the data in the Transmit Address register */
3574 macb_writel(lp, TAR, lp->skb_physaddr);
3575 /* Set length of the packet in the Transmit Control register */
3576 macb_writel(lp, TCR, skb->len);
3577
3578 } else {
3579 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3580 return NETDEV_TX_BUSY;
3581 }
3582
3583 return NETDEV_TX_OK;
3584}
3585
3586/* Extract received frame from buffer descriptors and sent to upper layers.
3587 * (Called from interrupt context)
3588 */
3589static void at91ether_rx(struct net_device *dev)
3590{
3591 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003592 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003593 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003594 unsigned char *p_recv;
3595 struct sk_buff *skb;
3596 unsigned int pktlen;
3597
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003598 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003599 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003600 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003601 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003602 skb = netdev_alloc_skb(dev, pktlen + 2);
3603 if (skb) {
3604 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003605 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003606
3607 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003608 dev->stats.rx_packets++;
3609 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003610 netif_rx(skb);
3611 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003612 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003613 }
3614
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003615 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003616 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003617
3618 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003619 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003620
3621 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003622 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3623 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003624 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003625 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003626
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003627 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003628 }
3629}
3630
3631/* MAC interrupt handler */
3632static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3633{
3634 struct net_device *dev = dev_id;
3635 struct macb *lp = netdev_priv(dev);
3636 u32 intstatus, ctl;
3637
3638 /* MAC Interrupt Status register indicates what interrupts are pending.
3639 * It is automatically cleared once read.
3640 */
3641 intstatus = macb_readl(lp, ISR);
3642
3643 /* Receive complete */
3644 if (intstatus & MACB_BIT(RCOMP))
3645 at91ether_rx(dev);
3646
3647 /* Transmit complete */
3648 if (intstatus & MACB_BIT(TCOMP)) {
3649 /* The TCOM bit is set even if the transmission failed */
3650 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003651 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003652
3653 if (lp->skb) {
3654 dev_kfree_skb_irq(lp->skb);
3655 lp->skb = NULL;
3656 dma_unmap_single(NULL, lp->skb_physaddr,
3657 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003658 dev->stats.tx_packets++;
3659 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003660 }
3661 netif_wake_queue(dev);
3662 }
3663
3664 /* Work-around for EMAC Errata section 41.3.1 */
3665 if (intstatus & MACB_BIT(RXUBR)) {
3666 ctl = macb_readl(lp, NCR);
3667 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003668 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003669 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3670 }
3671
3672 if (intstatus & MACB_BIT(ISR_ROVR))
3673 netdev_err(dev, "ROVR error\n");
3674
3675 return IRQ_HANDLED;
3676}
3677
3678#ifdef CONFIG_NET_POLL_CONTROLLER
3679static void at91ether_poll_controller(struct net_device *dev)
3680{
3681 unsigned long flags;
3682
3683 local_irq_save(flags);
3684 at91ether_interrupt(dev->irq, dev);
3685 local_irq_restore(flags);
3686}
3687#endif
3688
3689static const struct net_device_ops at91ether_netdev_ops = {
3690 .ndo_open = at91ether_open,
3691 .ndo_stop = at91ether_close,
3692 .ndo_start_xmit = at91ether_start_xmit,
3693 .ndo_get_stats = macb_get_stats,
3694 .ndo_set_rx_mode = macb_set_rx_mode,
3695 .ndo_set_mac_address = eth_mac_addr,
3696 .ndo_do_ioctl = macb_ioctl,
3697 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003698#ifdef CONFIG_NET_POLL_CONTROLLER
3699 .ndo_poll_controller = at91ether_poll_controller,
3700#endif
3701};
3702
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003703static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303704 struct clk **hclk, struct clk **tx_clk,
3705 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003706{
3707 int err;
3708
3709 *hclk = NULL;
3710 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303711 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003712
3713 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3714 if (IS_ERR(*pclk))
3715 return PTR_ERR(*pclk);
3716
3717 err = clk_prepare_enable(*pclk);
3718 if (err) {
3719 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3720 return err;
3721 }
3722
3723 return 0;
3724}
3725
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003726static int at91ether_init(struct platform_device *pdev)
3727{
3728 struct net_device *dev = platform_get_drvdata(pdev);
3729 struct macb *bp = netdev_priv(dev);
3730 int err;
3731 u32 reg;
3732
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003733 dev->netdev_ops = &at91ether_netdev_ops;
3734 dev->ethtool_ops = &macb_ethtool_ops;
3735
3736 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3737 0, dev->name, dev);
3738 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003739 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003740
3741 macb_writel(bp, NCR, 0);
3742
3743 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3744 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3745 reg |= MACB_BIT(RM9200_RMII);
3746
3747 macb_writel(bp, NCFGR, reg);
3748
3749 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003750}
3751
David S. Miller3cef5c52015-03-09 23:38:02 -04003752static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003753 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003754 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003755 .init = macb_init,
3756};
3757
David S. Miller3cef5c52015-03-09 23:38:02 -04003758static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003759 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3760 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003761 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003762 .init = macb_init,
3763};
3764
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003765static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003766 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003767 .dma_burst_length = 16,
3768 .clk_init = macb_clk_init,
3769 .init = macb_init,
3770};
3771
David S. Miller3cef5c52015-03-09 23:38:02 -04003772static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003773 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003774 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003775 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003776 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003777 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003778 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003779};
3780
David S. Miller3cef5c52015-03-09 23:38:02 -04003781static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003782 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003783 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003784 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003785 .init = macb_init,
3786};
3787
David S. Miller3cef5c52015-03-09 23:38:02 -04003788static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003789 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003790 .init = at91ether_init,
3791};
3792
Neil Armstronge611b5b2016-01-05 14:39:17 +01003793static const struct macb_config np4_config = {
3794 .caps = MACB_CAPS_USRIO_DISABLED,
3795 .clk_init = macb_clk_init,
3796 .init = macb_init,
3797};
David S. Miller36583eb2015-05-23 01:22:35 -04003798
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303799static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003800 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3801 MACB_CAPS_JUMBO |
3802 MACB_CAPS_GEM_HAS_PTP,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303803 .dma_burst_length = 16,
3804 .clk_init = macb_clk_init,
3805 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303806 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303807};
3808
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003809static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303810 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003811 .dma_burst_length = 16,
3812 .clk_init = macb_clk_init,
3813 .init = macb_init,
3814};
3815
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003816static const struct of_device_id macb_dt_ids[] = {
3817 { .compatible = "cdns,at32ap7000-macb" },
3818 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3819 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003820 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003821 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3822 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003823 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003824 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3825 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3826 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3827 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303828 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003829 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003830 { /* sentinel */ }
3831};
3832MODULE_DEVICE_TABLE(of, macb_dt_ids);
3833#endif /* CONFIG_OF */
3834
Bartosz Folta83a77e92016-12-14 06:39:15 +00003835static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003836 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3837 MACB_CAPS_JUMBO |
3838 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003839 .dma_burst_length = 16,
3840 .clk_init = macb_clk_init,
3841 .init = macb_init,
3842 .jumbo_max_len = 10240,
3843};
3844
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003845static int macb_probe(struct platform_device *pdev)
3846{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003847 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003848 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303849 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003850 = macb_config->clk_init;
3851 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003852 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303853 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003854 unsigned int queue_mask, num_queues;
3855 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003856 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003857 struct phy_device *phydev;
3858 struct net_device *dev;
3859 struct resource *regs;
3860 void __iomem *mem;
3861 const char *mac;
3862 struct macb *bp;
3863 int err;
3864
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003865 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3866 mem = devm_ioremap_resource(&pdev->dev, regs);
3867 if (IS_ERR(mem))
3868 return PTR_ERR(mem);
3869
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003870 if (np) {
3871 const struct of_device_id *match;
3872
3873 match = of_match_node(macb_dt_ids, np);
3874 if (match && match->data) {
3875 macb_config = match->data;
3876 clk_init = macb_config->clk_init;
3877 init = macb_config->init;
3878 }
3879 }
3880
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303881 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003882 if (err)
3883 return err;
3884
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003885 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003886
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003887 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003888 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003889 if (!dev) {
3890 err = -ENOMEM;
3891 goto err_disable_clocks;
3892 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003893
3894 dev->base_addr = regs->start;
3895
3896 SET_NETDEV_DEV(dev, &pdev->dev);
3897
3898 bp = netdev_priv(dev);
3899 bp->pdev = pdev;
3900 bp->dev = dev;
3901 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003902 bp->native_io = native_io;
3903 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003904 bp->macb_reg_readl = hw_readl_native;
3905 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003906 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003907 bp->macb_reg_readl = hw_readl;
3908 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003909 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003910 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003911 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003912 if (macb_config)
3913 bp->dma_burst_length = macb_config->dma_burst_length;
3914 bp->pclk = pclk;
3915 bp->hclk = hclk;
3916 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303917 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003918 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303919 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303920
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003921 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003922 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003923 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3924 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3925
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003926 spin_lock_init(&bp->lock);
3927
Nicolas Ferread783472015-03-31 15:02:02 +02003928 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003929 macb_configure_caps(bp, macb_config);
3930
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003931#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3932 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3933 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3934 bp->hw_dma_cap |= HW_DMA_CAP_64B;
3935 }
3936#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003937 platform_set_drvdata(pdev, dev);
3938
3939 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003940 if (dev->irq < 0) {
3941 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003942 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003943 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003944
Jarod Wilson44770e12016-10-17 15:54:17 -04003945 /* MTU range: 68 - 1500 or 10240 */
3946 dev->min_mtu = GEM_MTU_MIN_SIZE;
3947 if (bp->caps & MACB_CAPS_JUMBO)
3948 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3949 else
3950 dev->max_mtu = ETH_DATA_LEN;
3951
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003952 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003953 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003954 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003955 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003956 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003957
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003958 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003959 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003960 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003961 if (pdata && pdata->is_rmii)
3962 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3963 else
3964 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3965 } else {
3966 bp->phy_interface = err;
3967 }
3968
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003969 /* IP specific init */
3970 err = init(pdev);
3971 if (err)
3972 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003973
Florian Fainellicf669662016-05-02 18:38:45 -07003974 err = macb_mii_init(bp);
3975 if (err)
3976 goto err_out_free_netdev;
3977
Philippe Reynes0a912812016-06-22 00:32:35 +02003978 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003979
3980 netif_carrier_off(dev);
3981
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003982 err = register_netdev(dev);
3983 if (err) {
3984 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003985 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003986 }
3987
Harini Katakam032dc412018-01-27 12:09:01 +05303988 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
3989 (unsigned long)bp);
3990
Florian Fainellicf669662016-05-02 18:38:45 -07003991 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003992
Bo Shen58798232014-09-13 01:57:49 +02003993 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3994 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3995 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003996
3997 return 0;
3998
Florian Fainellicf669662016-05-02 18:38:45 -07003999err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02004000 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07004001 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01004002 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004003 if (np && of_phy_is_fixed_link(np))
4004 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07004005 mdiobus_free(bp->mii_bus);
4006
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004007err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004008 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004009
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004010err_disable_clocks:
4011 clk_disable_unprepare(tx_clk);
4012 clk_disable_unprepare(hclk);
4013 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304014 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004015
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004016 return err;
4017}
4018
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004019static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004020{
4021 struct net_device *dev;
4022 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004023 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004024
4025 dev = platform_get_drvdata(pdev);
4026
4027 if (dev) {
4028 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02004029 if (dev->phydev)
4030 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004031 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01004032 if (np && of_phy_is_fixed_link(np))
4033 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05004034 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004035 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004036
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004037 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01004038 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004039 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00004040 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304041 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02004042 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004043 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004044 }
4045
4046 return 0;
4047}
4048
Michal Simekd23823d2015-01-23 09:36:03 +01004049static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004050{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004051 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004052 struct net_device *netdev = platform_get_drvdata(pdev);
4053 struct macb *bp = netdev_priv(netdev);
4054
Nicolas Ferre03fc4722012-07-03 23:14:13 +00004055 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004056 netif_device_detach(netdev);
4057
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004058 if (bp->wol & MACB_WOL_ENABLED) {
4059 macb_writel(bp, IER, MACB_BIT(WOL));
4060 macb_writel(bp, WOL, MACB_BIT(MAG));
4061 enable_irq_wake(bp->queues[0].irq);
4062 } else {
4063 clk_disable_unprepare(bp->tx_clk);
4064 clk_disable_unprepare(bp->hclk);
4065 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304066 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004067 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004068
4069 return 0;
4070}
4071
Michal Simekd23823d2015-01-23 09:36:03 +01004072static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004073{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004074 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004075 struct net_device *netdev = platform_get_drvdata(pdev);
4076 struct macb *bp = netdev_priv(netdev);
4077
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004078 if (bp->wol & MACB_WOL_ENABLED) {
4079 macb_writel(bp, IDR, MACB_BIT(WOL));
4080 macb_writel(bp, WOL, 0);
4081 disable_irq_wake(bp->queues[0].irq);
4082 } else {
4083 clk_prepare_enable(bp->pclk);
4084 clk_prepare_enable(bp->hclk);
4085 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304086 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004087 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004088
4089 netif_device_attach(netdev);
4090
4091 return 0;
4092}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004093
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004094static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4095
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004096static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004097 .probe = macb_probe,
4098 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004099 .driver = {
4100 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004101 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004102 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004103 },
4104};
4105
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004106module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004107
4108MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004109MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004110MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004111MODULE_ALIAS("platform:macb");