1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Marvell PPv2 network controller for Armada 375 SoC.
4  *
5  * Copyright (C) 2014 Marvell
6  *
7  * Marcin Wojtas <mw@semihalf.com>
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/platform_device.h>
15 #include <linux/skbuff.h>
16 #include <linux/inetdevice.h>
17 #include <linux/mbus.h>
18 #include <linux/module.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/interrupt.h>
21 #include <linux/cpumask.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/phy.h>
29 #include <linux/phylink.h>
30 #include <linux/phy/phy.h>
31 #include <linux/clk.h>
32 #include <linux/hrtimer.h>
33 #include <linux/ktime.h>
34 #include <linux/regmap.h>
35 #include <uapi/linux/ppp_defs.h>
36 #include <net/ip.h>
37 #include <net/ipv6.h>
38 #include <net/tso.h>
39 
40 #include "mvpp2.h"
41 #include "mvpp2_prs.h"
42 #include "mvpp2_cls.h"
43 
44 enum mvpp2_bm_pool_log_num {
45 	MVPP2_BM_SHORT,
46 	MVPP2_BM_LONG,
47 	MVPP2_BM_JUMBO,
48 	MVPP2_BM_POOLS_NUM
49 };
50 
51 static struct {
52 	int pkt_size;
53 	int buf_num;
54 } mvpp2_pools[MVPP2_BM_POOLS_NUM];
55 
56 /* The prototype is added here to be used in start_dev when using ACPI. This
57  * will be removed once phylink is used for all modes (dt+ACPI).
58  */
59 static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode,
60 			     const struct phylink_link_state *state);
61 static void mvpp2_mac_link_up(struct phylink_config *config, unsigned int mode,
62 			      phy_interface_t interface, struct phy_device *phy);
63 
64 /* Queue modes */
65 #define MVPP2_QDIST_SINGLE_MODE	0
66 #define MVPP2_QDIST_MULTI_MODE	1
67 
68 static int queue_mode = MVPP2_QDIST_MULTI_MODE;
69 
70 module_param(queue_mode, int, 0444);
71 MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
72 
73 /* Utility/helper methods */
74 
75 void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
76 {
77 	writel(data, priv->swth_base[0] + offset);
78 }
79 
80 u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
81 {
82 	return readl(priv->swth_base[0] + offset);
83 }
84 
85 static u32 mvpp2_read_relaxed(struct mvpp2 *priv, u32 offset)
86 {
87 	return readl_relaxed(priv->swth_base[0] + offset);
88 }
89 
90 static inline u32 mvpp2_cpu_to_thread(struct mvpp2 *priv, int cpu)
91 {
92 	return cpu % priv->nthreads;
93 }
94 
95 /* These accessors should be used to access:
96  *
97  * - per-thread registers, where each thread has its own copy of the
98  *   register.
99  *
100  *   MVPP2_BM_VIRT_ALLOC_REG
101  *   MVPP2_BM_ADDR_HIGH_ALLOC
102  *   MVPP22_BM_ADDR_HIGH_RLS_REG
103  *   MVPP2_BM_VIRT_RLS_REG
104  *   MVPP2_ISR_RX_TX_CAUSE_REG
105  *   MVPP2_ISR_RX_TX_MASK_REG
106  *   MVPP2_TXQ_NUM_REG
107  *   MVPP2_AGGR_TXQ_UPDATE_REG
108  *   MVPP2_TXQ_RSVD_REQ_REG
109  *   MVPP2_TXQ_RSVD_RSLT_REG
110  *   MVPP2_TXQ_SENT_REG
111  *   MVPP2_RXQ_NUM_REG
112  *
113  * - global registers that must be accessed through a specific thread
114  *   window, because they are related to an access to a per-thread
115  *   register
116  *
117  *   MVPP2_BM_PHY_ALLOC_REG    (related to MVPP2_BM_VIRT_ALLOC_REG)
118  *   MVPP2_BM_PHY_RLS_REG      (related to MVPP2_BM_VIRT_RLS_REG)
119  *   MVPP2_RXQ_THRESH_REG      (related to MVPP2_RXQ_NUM_REG)
120  *   MVPP2_RXQ_DESC_ADDR_REG   (related to MVPP2_RXQ_NUM_REG)
121  *   MVPP2_RXQ_DESC_SIZE_REG   (related to MVPP2_RXQ_NUM_REG)
122  *   MVPP2_RXQ_INDEX_REG       (related to MVPP2_RXQ_NUM_REG)
123  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
124  *   MVPP2_TXQ_DESC_ADDR_REG   (related to MVPP2_TXQ_NUM_REG)
125  *   MVPP2_TXQ_DESC_SIZE_REG   (related to MVPP2_TXQ_NUM_REG)
126  *   MVPP2_TXQ_INDEX_REG       (related to MVPP2_TXQ_NUM_REG)
127  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
128  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
129  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
130  */
131 static void mvpp2_thread_write(struct mvpp2 *priv, unsigned int thread,
132 			       u32 offset, u32 data)
133 {
134 	writel(data, priv->swth_base[thread] + offset);
135 }
136 
137 static u32 mvpp2_thread_read(struct mvpp2 *priv, unsigned int thread,
138 			     u32 offset)
139 {
140 	return readl(priv->swth_base[thread] + offset);
141 }
142 
143 static void mvpp2_thread_write_relaxed(struct mvpp2 *priv, unsigned int thread,
144 				       u32 offset, u32 data)
145 {
146 	writel_relaxed(data, priv->swth_base[thread] + offset);
147 }
148 
149 static u32 mvpp2_thread_read_relaxed(struct mvpp2 *priv, unsigned int thread,
150 				     u32 offset)
151 {
152 	return readl_relaxed(priv->swth_base[thread] + offset);
153 }
154 
155 static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
156 					    struct mvpp2_tx_desc *tx_desc)
157 {
158 	if (port->priv->hw_version == MVPP21)
159 		return le32_to_cpu(tx_desc->pp21.buf_dma_addr);
160 	else
161 		return le64_to_cpu(tx_desc->pp22.buf_dma_addr_ptp) &
162 		       MVPP2_DESC_DMA_MASK;
163 }
164 
165 static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
166 				      struct mvpp2_tx_desc *tx_desc,
167 				      dma_addr_t dma_addr)
168 {
169 	dma_addr_t addr, offset;
170 
171 	addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
172 	offset = dma_addr & MVPP2_TX_DESC_ALIGN;
173 
174 	if (port->priv->hw_version == MVPP21) {
175 		tx_desc->pp21.buf_dma_addr = cpu_to_le32(addr);
176 		tx_desc->pp21.packet_offset = offset;
177 	} else {
178 		__le64 val = cpu_to_le64(addr);
179 
180 		tx_desc->pp22.buf_dma_addr_ptp &= ~cpu_to_le64(MVPP2_DESC_DMA_MASK);
181 		tx_desc->pp22.buf_dma_addr_ptp |= val;
182 		tx_desc->pp22.packet_offset = offset;
183 	}
184 }
185 
186 static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
187 				    struct mvpp2_tx_desc *tx_desc)
188 {
189 	if (port->priv->hw_version == MVPP21)
190 		return le16_to_cpu(tx_desc->pp21.data_size);
191 	else
192 		return le16_to_cpu(tx_desc->pp22.data_size);
193 }
194 
195 static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
196 				  struct mvpp2_tx_desc *tx_desc,
197 				  size_t size)
198 {
199 	if (port->priv->hw_version == MVPP21)
200 		tx_desc->pp21.data_size = cpu_to_le16(size);
201 	else
202 		tx_desc->pp22.data_size = cpu_to_le16(size);
203 }
204 
205 static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
206 				 struct mvpp2_tx_desc *tx_desc,
207 				 unsigned int txq)
208 {
209 	if (port->priv->hw_version == MVPP21)
210 		tx_desc->pp21.phys_txq = txq;
211 	else
212 		tx_desc->pp22.phys_txq = txq;
213 }
214 
215 static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
216 				 struct mvpp2_tx_desc *tx_desc,
217 				 unsigned int command)
218 {
219 	if (port->priv->hw_version == MVPP21)
220 		tx_desc->pp21.command = cpu_to_le32(command);
221 	else
222 		tx_desc->pp22.command = cpu_to_le32(command);
223 }
224 
225 static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
226 					    struct mvpp2_tx_desc *tx_desc)
227 {
228 	if (port->priv->hw_version == MVPP21)
229 		return tx_desc->pp21.packet_offset;
230 	else
231 		return tx_desc->pp22.packet_offset;
232 }
233 
234 static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
235 					    struct mvpp2_rx_desc *rx_desc)
236 {
237 	if (port->priv->hw_version == MVPP21)
238 		return le32_to_cpu(rx_desc->pp21.buf_dma_addr);
239 	else
240 		return le64_to_cpu(rx_desc->pp22.buf_dma_addr_key_hash) &
241 		       MVPP2_DESC_DMA_MASK;
242 }
243 
244 static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
245 					     struct mvpp2_rx_desc *rx_desc)
246 {
247 	if (port->priv->hw_version == MVPP21)
248 		return le32_to_cpu(rx_desc->pp21.buf_cookie);
249 	else
250 		return le64_to_cpu(rx_desc->pp22.buf_cookie_misc) &
251 		       MVPP2_DESC_DMA_MASK;
252 }
253 
254 static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
255 				    struct mvpp2_rx_desc *rx_desc)
256 {
257 	if (port->priv->hw_version == MVPP21)
258 		return le16_to_cpu(rx_desc->pp21.data_size);
259 	else
260 		return le16_to_cpu(rx_desc->pp22.data_size);
261 }
262 
263 static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
264 				   struct mvpp2_rx_desc *rx_desc)
265 {
266 	if (port->priv->hw_version == MVPP21)
267 		return le32_to_cpu(rx_desc->pp21.status);
268 	else
269 		return le32_to_cpu(rx_desc->pp22.status);
270 }
271 
272 static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
273 {
274 	txq_pcpu->txq_get_index++;
275 	if (txq_pcpu->txq_get_index == txq_pcpu->size)
276 		txq_pcpu->txq_get_index = 0;
277 }
278 
279 static void mvpp2_txq_inc_put(struct mvpp2_port *port,
280 			      struct mvpp2_txq_pcpu *txq_pcpu,
281 			      struct sk_buff *skb,
282 			      struct mvpp2_tx_desc *tx_desc)
283 {
284 	struct mvpp2_txq_pcpu_buf *tx_buf =
285 		txq_pcpu->buffs + txq_pcpu->txq_put_index;
286 	tx_buf->skb = skb;
287 	tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
288 	tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
289 		mvpp2_txdesc_offset_get(port, tx_desc);
290 	txq_pcpu->txq_put_index++;
291 	if (txq_pcpu->txq_put_index == txq_pcpu->size)
292 		txq_pcpu->txq_put_index = 0;
293 }
294 
295 /* Get number of physical egress port */
296 static inline int mvpp2_egress_port(struct mvpp2_port *port)
297 {
298 	return MVPP2_MAX_TCONT + port->id;
299 }
300 
301 /* Get number of physical TXQ */
302 static inline int mvpp2_txq_phys(int port, int txq)
303 {
304 	return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
305 }
306 
307 static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
308 {
309 	if (likely(pool->frag_size <= PAGE_SIZE))
310 		return netdev_alloc_frag(pool->frag_size);
311 	else
312 		return kmalloc(pool->frag_size, GFP_ATOMIC);
313 }
314 
315 static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
316 {
317 	if (likely(pool->frag_size <= PAGE_SIZE))
318 		skb_free_frag(data);
319 	else
320 		kfree(data);
321 }
322 
323 /* Buffer Manager configuration routines */
324 
325 /* Create pool */
326 static int mvpp2_bm_pool_create(struct platform_device *pdev,
327 				struct mvpp2 *priv,
328 				struct mvpp2_bm_pool *bm_pool, int size)
329 {
330 	u32 val;
331 
332 	/* Number of buffer pointers must be a multiple of 16, as per
333 	 * hardware constraints
334 	 */
335 	if (!IS_ALIGNED(size, 16))
336 		return -EINVAL;
337 
338 	/* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
339 	 * bytes per buffer pointer
340 	 */
341 	if (priv->hw_version == MVPP21)
342 		bm_pool->size_bytes = 2 * sizeof(u32) * size;
343 	else
344 		bm_pool->size_bytes = 2 * sizeof(u64) * size;
345 
346 	bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
347 						&bm_pool->dma_addr,
348 						GFP_KERNEL);
349 	if (!bm_pool->virt_addr)
350 		return -ENOMEM;
351 
352 	if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
353 			MVPP2_BM_POOL_PTR_ALIGN)) {
354 		dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
355 				  bm_pool->virt_addr, bm_pool->dma_addr);
356 		dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
357 			bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
358 		return -ENOMEM;
359 	}
360 
361 	mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
362 		    lower_32_bits(bm_pool->dma_addr));
363 	mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
364 
365 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
366 	val |= MVPP2_BM_START_MASK;
367 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
368 
369 	bm_pool->size = size;
370 	bm_pool->pkt_size = 0;
371 	bm_pool->buf_num = 0;
372 
373 	return 0;
374 }
375 
376 /* Set pool buffer size */
377 static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
378 				      struct mvpp2_bm_pool *bm_pool,
379 				      int buf_size)
380 {
381 	u32 val;
382 
383 	bm_pool->buf_size = buf_size;
384 
385 	val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
386 	mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
387 }
388 
389 static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
390 				    struct mvpp2_bm_pool *bm_pool,
391 				    dma_addr_t *dma_addr,
392 				    phys_addr_t *phys_addr)
393 {
394 	unsigned int thread = mvpp2_cpu_to_thread(priv, get_cpu());
395 
396 	*dma_addr = mvpp2_thread_read(priv, thread,
397 				      MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
398 	*phys_addr = mvpp2_thread_read(priv, thread, MVPP2_BM_VIRT_ALLOC_REG);
399 
400 	if (priv->hw_version == MVPP22) {
401 		u32 val;
402 		u32 dma_addr_highbits, phys_addr_highbits;
403 
404 		val = mvpp2_thread_read(priv, thread, MVPP22_BM_ADDR_HIGH_ALLOC);
405 		dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
406 		phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
407 			MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
408 
409 		if (sizeof(dma_addr_t) == 8)
410 			*dma_addr |= (u64)dma_addr_highbits << 32;
411 
412 		if (sizeof(phys_addr_t) == 8)
413 			*phys_addr |= (u64)phys_addr_highbits << 32;
414 	}
415 
416 	put_cpu();
417 }
418 
419 /* Free all buffers from the pool */
420 static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
421 			       struct mvpp2_bm_pool *bm_pool, int buf_num)
422 {
423 	int i;
424 
425 	if (buf_num > bm_pool->buf_num) {
426 		WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
427 		     bm_pool->id, buf_num);
428 		buf_num = bm_pool->buf_num;
429 	}
430 
431 	for (i = 0; i < buf_num; i++) {
432 		dma_addr_t buf_dma_addr;
433 		phys_addr_t buf_phys_addr;
434 		void *data;
435 
436 		mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
437 					&buf_dma_addr, &buf_phys_addr);
438 
439 		dma_unmap_single(dev, buf_dma_addr,
440 				 bm_pool->buf_size, DMA_FROM_DEVICE);
441 
442 		data = (void *)phys_to_virt(buf_phys_addr);
443 		if (!data)
444 			break;
445 
446 		mvpp2_frag_free(bm_pool, data);
447 	}
448 
449 	/* Update BM driver with number of buffers removed from pool */
450 	bm_pool->buf_num -= i;
451 }
452 
453 /* Check number of buffers in BM pool */
454 static int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
455 {
456 	int buf_num = 0;
457 
458 	buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
459 				    MVPP22_BM_POOL_PTRS_NUM_MASK;
460 	buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
461 				    MVPP2_BM_BPPI_PTR_NUM_MASK;
462 
463 	/* HW has one buffer ready which is not reflected in the counters */
464 	if (buf_num)
465 		buf_num += 1;
466 
467 	return buf_num;
468 }
469 
470 /* Cleanup pool */
471 static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
472 				 struct mvpp2 *priv,
473 				 struct mvpp2_bm_pool *bm_pool)
474 {
475 	int buf_num;
476 	u32 val;
477 
478 	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
479 	mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool, buf_num);
480 
481 	/* Check buffer counters after free */
482 	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
483 	if (buf_num) {
484 		WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
485 		     bm_pool->id, bm_pool->buf_num);
486 		return 0;
487 	}
488 
489 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
490 	val |= MVPP2_BM_STOP_MASK;
491 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
492 
493 	dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
494 			  bm_pool->virt_addr,
495 			  bm_pool->dma_addr);
496 	return 0;
497 }
498 
499 static int mvpp2_bm_pools_init(struct platform_device *pdev,
500 			       struct mvpp2 *priv)
501 {
502 	int i, err, size;
503 	struct mvpp2_bm_pool *bm_pool;
504 
505 	/* Create all pools with maximum size */
506 	size = MVPP2_BM_POOL_SIZE_MAX;
507 	for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
508 		bm_pool = &priv->bm_pools[i];
509 		bm_pool->id = i;
510 		err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
511 		if (err)
512 			goto err_unroll_pools;
513 		mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
514 	}
515 	return 0;
516 
517 err_unroll_pools:
518 	dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
519 	for (i = i - 1; i >= 0; i--)
520 		mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
521 	return err;
522 }
523 
524 static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
525 {
526 	int i, err;
527 
528 	for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
529 		/* Mask BM all interrupts */
530 		mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
531 		/* Clear BM cause register */
532 		mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
533 	}
534 
535 	/* Allocate and initialize BM pools */
536 	priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
537 				      sizeof(*priv->bm_pools), GFP_KERNEL);
538 	if (!priv->bm_pools)
539 		return -ENOMEM;
540 
541 	err = mvpp2_bm_pools_init(pdev, priv);
542 	if (err < 0)
543 		return err;
544 	return 0;
545 }
546 
547 static void mvpp2_setup_bm_pool(void)
548 {
549 	/* Short pool */
550 	mvpp2_pools[MVPP2_BM_SHORT].buf_num  = MVPP2_BM_SHORT_BUF_NUM;
551 	mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;
552 
553 	/* Long pool */
554 	mvpp2_pools[MVPP2_BM_LONG].buf_num  = MVPP2_BM_LONG_BUF_NUM;
555 	mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
556 
557 	/* Jumbo pool */
558 	mvpp2_pools[MVPP2_BM_JUMBO].buf_num  = MVPP2_BM_JUMBO_BUF_NUM;
559 	mvpp2_pools[MVPP2_BM_JUMBO].pkt_size = MVPP2_BM_JUMBO_PKT_SIZE;
560 }
561 
562 /* Attach long pool to rxq */
563 static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
564 				    int lrxq, int long_pool)
565 {
566 	u32 val, mask;
567 	int prxq;
568 
569 	/* Get queue physical ID */
570 	prxq = port->rxqs[lrxq]->id;
571 
572 	if (port->priv->hw_version == MVPP21)
573 		mask = MVPP21_RXQ_POOL_LONG_MASK;
574 	else
575 		mask = MVPP22_RXQ_POOL_LONG_MASK;
576 
577 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
578 	val &= ~mask;
579 	val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
580 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
581 }
582 
583 /* Attach short pool to rxq */
584 static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
585 				     int lrxq, int short_pool)
586 {
587 	u32 val, mask;
588 	int prxq;
589 
590 	/* Get queue physical ID */
591 	prxq = port->rxqs[lrxq]->id;
592 
593 	if (port->priv->hw_version == MVPP21)
594 		mask = MVPP21_RXQ_POOL_SHORT_MASK;
595 	else
596 		mask = MVPP22_RXQ_POOL_SHORT_MASK;
597 
598 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
599 	val &= ~mask;
600 	val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
601 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
602 }
603 
604 static void *mvpp2_buf_alloc(struct mvpp2_port *port,
605 			     struct mvpp2_bm_pool *bm_pool,
606 			     dma_addr_t *buf_dma_addr,
607 			     phys_addr_t *buf_phys_addr,
608 			     gfp_t gfp_mask)
609 {
610 	dma_addr_t dma_addr;
611 	void *data;
612 
613 	data = mvpp2_frag_alloc(bm_pool);
614 	if (!data)
615 		return NULL;
616 
617 	dma_addr = dma_map_single(port->dev->dev.parent, data,
618 				  MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
619 				  DMA_FROM_DEVICE);
620 	if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
621 		mvpp2_frag_free(bm_pool, data);
622 		return NULL;
623 	}
624 	*buf_dma_addr = dma_addr;
625 	*buf_phys_addr = virt_to_phys(data);
626 
627 	return data;
628 }
629 
630 /* Release buffer to BM */
631 static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
632 				     dma_addr_t buf_dma_addr,
633 				     phys_addr_t buf_phys_addr)
634 {
635 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
636 	unsigned long flags = 0;
637 
638 	if (test_bit(thread, &port->priv->lock_map))
639 		spin_lock_irqsave(&port->bm_lock[thread], flags);
640 
641 	if (port->priv->hw_version == MVPP22) {
642 		u32 val = 0;
643 
644 		if (sizeof(dma_addr_t) == 8)
645 			val |= upper_32_bits(buf_dma_addr) &
646 				MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
647 
648 		if (sizeof(phys_addr_t) == 8)
649 			val |= (upper_32_bits(buf_phys_addr)
650 				<< MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
651 				MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
652 
653 		mvpp2_thread_write_relaxed(port->priv, thread,
654 					   MVPP22_BM_ADDR_HIGH_RLS_REG, val);
655 	}
656 
657 	/* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
658 	 * returned in the "cookie" field of the RX
659 	 * descriptor. Instead of storing the virtual address, we
660 	 * store the physical address
661 	 */
662 	mvpp2_thread_write_relaxed(port->priv, thread,
663 				   MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
664 	mvpp2_thread_write_relaxed(port->priv, thread,
665 				   MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
666 
667 	if (test_bit(thread, &port->priv->lock_map))
668 		spin_unlock_irqrestore(&port->bm_lock[thread], flags);
669 
670 	put_cpu();
671 }
672 
673 /* Allocate buffers for the pool */
674 static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
675 			     struct mvpp2_bm_pool *bm_pool, int buf_num)
676 {
677 	int i, buf_size, total_size;
678 	dma_addr_t dma_addr;
679 	phys_addr_t phys_addr;
680 	void *buf;
681 
682 	buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
683 	total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
684 
685 	if (buf_num < 0 ||
686 	    (buf_num + bm_pool->buf_num > bm_pool->size)) {
687 		netdev_err(port->dev,
688 			   "cannot allocate %d buffers for pool %d\n",
689 			   buf_num, bm_pool->id);
690 		return 0;
691 	}
692 
693 	for (i = 0; i < buf_num; i++) {
694 		buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
695 				      &phys_addr, GFP_KERNEL);
696 		if (!buf)
697 			break;
698 
699 		mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
700 				  phys_addr);
701 	}
702 
703 	/* Update BM driver with number of buffers added to pool */
704 	bm_pool->buf_num += i;
705 
706 	netdev_dbg(port->dev,
707 		   "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
708 		   bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
709 
710 	netdev_dbg(port->dev,
711 		   "pool %d: %d of %d buffers added\n",
712 		   bm_pool->id, i, buf_num);
713 	return i;
714 }
715 
716 /* Notify the driver that BM pool is being used as specific type and return the
717  * pool pointer on success
718  */
719 static struct mvpp2_bm_pool *
720 mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
721 {
722 	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
723 	int num;
724 
725 	if (pool >= MVPP2_BM_POOLS_NUM) {
726 		netdev_err(port->dev, "Invalid pool %d\n", pool);
727 		return NULL;
728 	}
729 
730 	/* Allocate buffers in case BM pool is used as long pool, but packet
731 	 * size doesn't match MTU or BM pool hasn't being used yet
732 	 */
733 	if (new_pool->pkt_size == 0) {
734 		int pkts_num;
735 
736 		/* Set default buffer number or free all the buffers in case
737 		 * the pool is not empty
738 		 */
739 		pkts_num = new_pool->buf_num;
740 		if (pkts_num == 0)
741 			pkts_num = mvpp2_pools[pool].buf_num;
742 		else
743 			mvpp2_bm_bufs_free(port->dev->dev.parent,
744 					   port->priv, new_pool, pkts_num);
745 
746 		new_pool->pkt_size = pkt_size;
747 		new_pool->frag_size =
748 			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
749 			MVPP2_SKB_SHINFO_SIZE;
750 
751 		/* Allocate buffers for this pool */
752 		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
753 		if (num != pkts_num) {
754 			WARN(1, "pool %d: %d of %d allocated\n",
755 			     new_pool->id, num, pkts_num);
756 			return NULL;
757 		}
758 	}
759 
760 	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
761 				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
762 
763 	return new_pool;
764 }
765 
766 /* Initialize pools for swf */
767 static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
768 {
769 	int rxq;
770 	enum mvpp2_bm_pool_log_num long_log_pool, short_log_pool;
771 
772 	/* If port pkt_size is higher than 1518B:
773 	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
774 	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
775 	 */
776 	if (port->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
777 		long_log_pool = MVPP2_BM_JUMBO;
778 		short_log_pool = MVPP2_BM_LONG;
779 	} else {
780 		long_log_pool = MVPP2_BM_LONG;
781 		short_log_pool = MVPP2_BM_SHORT;
782 	}
783 
784 	if (!port->pool_long) {
785 		port->pool_long =
786 			mvpp2_bm_pool_use(port, long_log_pool,
787 					  mvpp2_pools[long_log_pool].pkt_size);
788 		if (!port->pool_long)
789 			return -ENOMEM;
790 
791 		port->pool_long->port_map |= BIT(port->id);
792 
793 		for (rxq = 0; rxq < port->nrxqs; rxq++)
794 			mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
795 	}
796 
797 	if (!port->pool_short) {
798 		port->pool_short =
799 			mvpp2_bm_pool_use(port, short_log_pool,
800 					  mvpp2_pools[short_log_pool].pkt_size);
801 		if (!port->pool_short)
802 			return -ENOMEM;
803 
804 		port->pool_short->port_map |= BIT(port->id);
805 
806 		for (rxq = 0; rxq < port->nrxqs; rxq++)
807 			mvpp2_rxq_short_pool_set(port, rxq,
808 						 port->pool_short->id);
809 	}
810 
811 	return 0;
812 }
813 
814 static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
815 {
816 	struct mvpp2_port *port = netdev_priv(dev);
817 	enum mvpp2_bm_pool_log_num new_long_pool;
818 	int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
819 
820 	/* If port MTU is higher than 1518B:
821 	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
822 	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
823 	 */
824 	if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
825 		new_long_pool = MVPP2_BM_JUMBO;
826 	else
827 		new_long_pool = MVPP2_BM_LONG;
828 
829 	if (new_long_pool != port->pool_long->id) {
830 		/* Remove port from old short & long pool */
831 		port->pool_long = mvpp2_bm_pool_use(port, port->pool_long->id,
832 						    port->pool_long->pkt_size);
833 		port->pool_long->port_map &= ~BIT(port->id);
834 		port->pool_long = NULL;
835 
836 		port->pool_short = mvpp2_bm_pool_use(port, port->pool_short->id,
837 						     port->pool_short->pkt_size);
838 		port->pool_short->port_map &= ~BIT(port->id);
839 		port->pool_short = NULL;
840 
841 		port->pkt_size =  pkt_size;
842 
843 		/* Add port to new short & long pool */
844 		mvpp2_swf_bm_pool_init(port);
845 
846 		/* Update L4 checksum when jumbo enable/disable on port */
847 		if (new_long_pool == MVPP2_BM_JUMBO && port->id != 0) {
848 			dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
849 			dev->hw_features &= ~(NETIF_F_IP_CSUM |
850 					      NETIF_F_IPV6_CSUM);
851 		} else {
852 			dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
853 			dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
854 		}
855 	}
856 
857 	dev->mtu = mtu;
858 	dev->wanted_features = dev->features;
859 
860 	netdev_update_features(dev);
861 	return 0;
862 }
863 
864 static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
865 {
866 	int i, sw_thread_mask = 0;
867 
868 	for (i = 0; i < port->nqvecs; i++)
869 		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
870 
871 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
872 		    MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
873 }
874 
875 static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
876 {
877 	int i, sw_thread_mask = 0;
878 
879 	for (i = 0; i < port->nqvecs; i++)
880 		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
881 
882 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
883 		    MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
884 }
885 
886 static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
887 {
888 	struct mvpp2_port *port = qvec->port;
889 
890 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
891 		    MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
892 }
893 
894 static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
895 {
896 	struct mvpp2_port *port = qvec->port;
897 
898 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
899 		    MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
900 }
901 
902 /* Mask the current thread's Rx/Tx interrupts
903  * Called by on_each_cpu(), guaranteed to run with migration disabled,
904  * using smp_processor_id() is OK.
905  */
906 static void mvpp2_interrupts_mask(void *arg)
907 {
908 	struct mvpp2_port *port = arg;
909 
910 	/* If the thread isn't used, don't do anything */
911 	if (smp_processor_id() > port->priv->nthreads)
912 		return;
913 
914 	mvpp2_thread_write(port->priv,
915 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
916 			   MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
917 }
918 
919 /* Unmask the current thread's Rx/Tx interrupts.
920  * Called by on_each_cpu(), guaranteed to run with migration disabled,
921  * using smp_processor_id() is OK.
922  */
923 static void mvpp2_interrupts_unmask(void *arg)
924 {
925 	struct mvpp2_port *port = arg;
926 	u32 val;
927 
928 	/* If the thread isn't used, don't do anything */
929 	if (smp_processor_id() > port->priv->nthreads)
930 		return;
931 
932 	val = MVPP2_CAUSE_MISC_SUM_MASK |
933 		MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
934 	if (port->has_tx_irqs)
935 		val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
936 
937 	mvpp2_thread_write(port->priv,
938 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
939 			   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
940 }
941 
942 static void
943 mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
944 {
945 	u32 val;
946 	int i;
947 
948 	if (port->priv->hw_version != MVPP22)
949 		return;
950 
951 	if (mask)
952 		val = 0;
953 	else
954 		val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(MVPP22);
955 
956 	for (i = 0; i < port->nqvecs; i++) {
957 		struct mvpp2_queue_vector *v = port->qvecs + i;
958 
959 		if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
960 			continue;
961 
962 		mvpp2_thread_write(port->priv, v->sw_thread_id,
963 				   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
964 	}
965 }
966 
967 /* Port configuration routines */
968 static bool mvpp2_is_xlg(phy_interface_t interface)
969 {
970 	return interface == PHY_INTERFACE_MODE_10GKR ||
971 	       interface == PHY_INTERFACE_MODE_XAUI;
972 }
973 
974 static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
975 {
976 	struct mvpp2 *priv = port->priv;
977 	u32 val;
978 
979 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
980 	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
981 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
982 
983 	regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
984 	if (port->gop_id == 2)
985 		val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
986 	else if (port->gop_id == 3)
987 		val |= GENCONF_CTRL0_PORT1_RGMII_MII;
988 	regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
989 }
990 
991 static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
992 {
993 	struct mvpp2 *priv = port->priv;
994 	u32 val;
995 
996 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
997 	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
998 	       GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
999 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1000 
1001 	if (port->gop_id > 1) {
1002 		regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1003 		if (port->gop_id == 2)
1004 			val &= ~GENCONF_CTRL0_PORT0_RGMII;
1005 		else if (port->gop_id == 3)
1006 			val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
1007 		regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1008 	}
1009 }
1010 
1011 static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
1012 {
1013 	struct mvpp2 *priv = port->priv;
1014 	void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1015 	void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1016 	u32 val;
1017 
1018 	val = readl(xpcs + MVPP22_XPCS_CFG0);
1019 	val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
1020 		 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
1021 	val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
1022 	writel(val, xpcs + MVPP22_XPCS_CFG0);
1023 
1024 	val = readl(mpcs + MVPP22_MPCS_CTRL);
1025 	val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
1026 	writel(val, mpcs + MVPP22_MPCS_CTRL);
1027 
1028 	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1029 	val &= ~MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7);
1030 	val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
1031 	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1032 }
1033 
1034 static int mvpp22_gop_init(struct mvpp2_port *port)
1035 {
1036 	struct mvpp2 *priv = port->priv;
1037 	u32 val;
1038 
1039 	if (!priv->sysctrl_base)
1040 		return 0;
1041 
1042 	switch (port->phy_interface) {
1043 	case PHY_INTERFACE_MODE_RGMII:
1044 	case PHY_INTERFACE_MODE_RGMII_ID:
1045 	case PHY_INTERFACE_MODE_RGMII_RXID:
1046 	case PHY_INTERFACE_MODE_RGMII_TXID:
1047 		if (port->gop_id == 0)
1048 			goto invalid_conf;
1049 		mvpp22_gop_init_rgmii(port);
1050 		break;
1051 	case PHY_INTERFACE_MODE_SGMII:
1052 	case PHY_INTERFACE_MODE_1000BASEX:
1053 	case PHY_INTERFACE_MODE_2500BASEX:
1054 		mvpp22_gop_init_sgmii(port);
1055 		break;
1056 	case PHY_INTERFACE_MODE_10GKR:
1057 		if (port->gop_id != 0)
1058 			goto invalid_conf;
1059 		mvpp22_gop_init_10gkr(port);
1060 		break;
1061 	default:
1062 		goto unsupported_conf;
1063 	}
1064 
1065 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
1066 	val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
1067 	       GENCONF_PORT_CTRL1_EN(port->gop_id);
1068 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
1069 
1070 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1071 	val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
1072 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1073 
1074 	regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
1075 	val |= GENCONF_SOFT_RESET1_GOP;
1076 	regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
1077 
1078 unsupported_conf:
1079 	return 0;
1080 
1081 invalid_conf:
1082 	netdev_err(port->dev, "Invalid port configuration\n");
1083 	return -EINVAL;
1084 }
1085 
1086 static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
1087 {
1088 	u32 val;
1089 
1090 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1091 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1092 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1093 		/* Enable the GMAC link status irq for this port */
1094 		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1095 		val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1096 		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1097 	}
1098 
1099 	if (port->gop_id == 0) {
1100 		/* Enable the XLG/GIG irqs for this port */
1101 		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1102 		if (mvpp2_is_xlg(port->phy_interface))
1103 			val |= MVPP22_XLG_EXT_INT_MASK_XLG;
1104 		else
1105 			val |= MVPP22_XLG_EXT_INT_MASK_GIG;
1106 		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1107 	}
1108 }
1109 
1110 static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
1111 {
1112 	u32 val;
1113 
1114 	if (port->gop_id == 0) {
1115 		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1116 		val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
1117 			 MVPP22_XLG_EXT_INT_MASK_GIG);
1118 		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1119 	}
1120 
1121 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1122 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1123 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1124 		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1125 		val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1126 		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1127 	}
1128 }
1129 
1130 static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
1131 {
1132 	u32 val;
1133 
1134 	if (port->phylink ||
1135 	    phy_interface_mode_is_rgmii(port->phy_interface) ||
1136 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1137 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1138 		val = readl(port->base + MVPP22_GMAC_INT_MASK);
1139 		val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
1140 		writel(val, port->base + MVPP22_GMAC_INT_MASK);
1141 	}
1142 
1143 	if (port->gop_id == 0) {
1144 		val = readl(port->base + MVPP22_XLG_INT_MASK);
1145 		val |= MVPP22_XLG_INT_MASK_LINK;
1146 		writel(val, port->base + MVPP22_XLG_INT_MASK);
1147 	}
1148 
1149 	mvpp22_gop_unmask_irq(port);
1150 }
1151 
1152 /* Sets the PHY mode of the COMPHY (which configures the serdes lanes).
1153  *
1154  * The PHY mode used by the PPv2 driver comes from the network subsystem, while
1155  * the one given to the COMPHY comes from the generic PHY subsystem. Hence they
1156  * differ.
1157  *
1158  * The COMPHY configures the serdes lanes regardless of the actual use of the
1159  * lanes by the physical layer. This is why configurations like
1160  * "PPv2 (2500BaseX) - COMPHY (2500SGMII)" are valid.
1161  */
1162 static int mvpp22_comphy_init(struct mvpp2_port *port)
1163 {
1164 	int ret;
1165 
1166 	if (!port->comphy)
1167 		return 0;
1168 
1169 	ret = phy_set_mode_ext(port->comphy, PHY_MODE_ETHERNET,
1170 			       port->phy_interface);
1171 	if (ret)
1172 		return ret;
1173 
1174 	return phy_power_on(port->comphy);
1175 }
1176 
1177 static void mvpp2_port_enable(struct mvpp2_port *port)
1178 {
1179 	u32 val;
1180 
1181 	/* Only GOP port 0 has an XLG MAC */
1182 	if (port->gop_id == 0 && mvpp2_is_xlg(port->phy_interface)) {
1183 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1184 		val |= MVPP22_XLG_CTRL0_PORT_EN;
1185 		val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
1186 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1187 	} else {
1188 		val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1189 		val |= MVPP2_GMAC_PORT_EN_MASK;
1190 		val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
1191 		writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1192 	}
1193 }
1194 
1195 static void mvpp2_port_disable(struct mvpp2_port *port)
1196 {
1197 	u32 val;
1198 
1199 	/* Only GOP port 0 has an XLG MAC */
1200 	if (port->gop_id == 0 && mvpp2_is_xlg(port->phy_interface)) {
1201 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1202 		val &= ~MVPP22_XLG_CTRL0_PORT_EN;
1203 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1204 	}
1205 
1206 	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1207 	val &= ~(MVPP2_GMAC_PORT_EN_MASK);
1208 	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1209 }
1210 
1211 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
1212 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
1213 {
1214 	u32 val;
1215 
1216 	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
1217 		    ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
1218 	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1219 }
1220 
1221 /* Configure loopback port */
1222 static void mvpp2_port_loopback_set(struct mvpp2_port *port,
1223 				    const struct phylink_link_state *state)
1224 {
1225 	u32 val;
1226 
1227 	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
1228 
1229 	if (state->speed == 1000)
1230 		val |= MVPP2_GMAC_GMII_LB_EN_MASK;
1231 	else
1232 		val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
1233 
1234 	if (phy_interface_mode_is_8023z(port->phy_interface) ||
1235 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII)
1236 		val |= MVPP2_GMAC_PCS_LB_EN_MASK;
1237 	else
1238 		val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
1239 
1240 	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1241 }
1242 
1243 struct mvpp2_ethtool_counter {
1244 	unsigned int offset;
1245 	const char string[ETH_GSTRING_LEN];
1246 	bool reg_is_64b;
1247 };
1248 
1249 static u64 mvpp2_read_count(struct mvpp2_port *port,
1250 			    const struct mvpp2_ethtool_counter *counter)
1251 {
1252 	u64 val;
1253 
1254 	val = readl(port->stats_base + counter->offset);
1255 	if (counter->reg_is_64b)
1256 		val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
1257 
1258 	return val;
1259 }
1260 
1261 /* Some counters are accessed indirectly by first writing an index to
1262  * MVPP2_CTRS_IDX. The index can represent various resources depending on the
1263  * register we access, it can be a hit counter for some classification tables,
1264  * a counter specific to a rxq, a txq or a buffer pool.
1265  */
1266 static u32 mvpp2_read_index(struct mvpp2 *priv, u32 index, u32 reg)
1267 {
1268 	mvpp2_write(priv, MVPP2_CTRS_IDX, index);
1269 	return mvpp2_read(priv, reg);
1270 }
1271 
1272 /* Due to the fact that software statistics and hardware statistics are, by
1273  * design, incremented at different moments in the chain of packet processing,
1274  * it is very likely that incoming packets could have been dropped after being
1275  * counted by hardware but before reaching software statistics (most probably
1276  * multicast packets), and in the oppposite way, during transmission, FCS bytes
1277  * are added in between as well as TSO skb will be split and header bytes added.
1278  * Hence, statistics gathered from userspace with ifconfig (software) and
1279  * ethtool (hardware) cannot be compared.
1280  */
1281 static const struct mvpp2_ethtool_counter mvpp2_ethtool_mib_regs[] = {
1282 	{ MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
1283 	{ MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
1284 	{ MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
1285 	{ MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
1286 	{ MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
1287 	{ MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
1288 	{ MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
1289 	{ MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
1290 	{ MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
1291 	{ MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
1292 	{ MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
1293 	{ MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
1294 	{ MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
1295 	{ MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
1296 	{ MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
1297 	{ MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
1298 	{ MVPP2_MIB_FC_SENT, "fc_sent" },
1299 	{ MVPP2_MIB_FC_RCVD, "fc_received" },
1300 	{ MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
1301 	{ MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
1302 	{ MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
1303 	{ MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
1304 	{ MVPP2_MIB_JABBER_RCVD, "jabber_received" },
1305 	{ MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
1306 	{ MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
1307 	{ MVPP2_MIB_COLLISION, "collision" },
1308 	{ MVPP2_MIB_LATE_COLLISION, "late_collision" },
1309 };
1310 
1311 static const struct mvpp2_ethtool_counter mvpp2_ethtool_port_regs[] = {
1312 	{ MVPP2_OVERRUN_ETH_DROP, "rx_fifo_or_parser_overrun_drops" },
1313 	{ MVPP2_CLS_ETH_DROP, "rx_classifier_drops" },
1314 };
1315 
1316 static const struct mvpp2_ethtool_counter mvpp2_ethtool_txq_regs[] = {
1317 	{ MVPP2_TX_DESC_ENQ_CTR, "txq_%d_desc_enqueue" },
1318 	{ MVPP2_TX_DESC_ENQ_TO_DDR_CTR, "txq_%d_desc_enqueue_to_ddr" },
1319 	{ MVPP2_TX_BUFF_ENQ_TO_DDR_CTR, "txq_%d_buff_euqueue_to_ddr" },
1320 	{ MVPP2_TX_DESC_ENQ_HW_FWD_CTR, "txq_%d_desc_hardware_forwarded" },
1321 	{ MVPP2_TX_PKTS_DEQ_CTR, "txq_%d_packets_dequeued" },
1322 	{ MVPP2_TX_PKTS_FULL_QUEUE_DROP_CTR, "txq_%d_queue_full_drops" },
1323 	{ MVPP2_TX_PKTS_EARLY_DROP_CTR, "txq_%d_packets_early_drops" },
1324 	{ MVPP2_TX_PKTS_BM_DROP_CTR, "txq_%d_packets_bm_drops" },
1325 	{ MVPP2_TX_PKTS_BM_MC_DROP_CTR, "txq_%d_packets_rep_bm_drops" },
1326 };
1327 
1328 static const struct mvpp2_ethtool_counter mvpp2_ethtool_rxq_regs[] = {
1329 	{ MVPP2_RX_DESC_ENQ_CTR, "rxq_%d_desc_enqueue" },
1330 	{ MVPP2_RX_PKTS_FULL_QUEUE_DROP_CTR, "rxq_%d_queue_full_drops" },
1331 	{ MVPP2_RX_PKTS_EARLY_DROP_CTR, "rxq_%d_packets_early_drops" },
1332 	{ MVPP2_RX_PKTS_BM_DROP_CTR, "rxq_%d_packets_bm_drops" },
1333 };
1334 
1335 #define MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs)	(ARRAY_SIZE(mvpp2_ethtool_mib_regs) + \
1336 						 ARRAY_SIZE(mvpp2_ethtool_port_regs) + \
1337 						 (ARRAY_SIZE(mvpp2_ethtool_txq_regs) * (ntxqs)) + \
1338 						 (ARRAY_SIZE(mvpp2_ethtool_rxq_regs) * (nrxqs)))
1339 
1340 static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
1341 				      u8 *data)
1342 {
1343 	struct mvpp2_port *port = netdev_priv(netdev);
1344 	int i, q;
1345 
1346 	if (sset != ETH_SS_STATS)
1347 		return;
1348 
1349 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++) {
1350 		strscpy(data, mvpp2_ethtool_mib_regs[i].string,
1351 			ETH_GSTRING_LEN);
1352 		data += ETH_GSTRING_LEN;
1353 	}
1354 
1355 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++) {
1356 		strscpy(data, mvpp2_ethtool_port_regs[i].string,
1357 			ETH_GSTRING_LEN);
1358 		data += ETH_GSTRING_LEN;
1359 	}
1360 
1361 	for (q = 0; q < port->ntxqs; q++) {
1362 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++) {
1363 			snprintf(data, ETH_GSTRING_LEN,
1364 				 mvpp2_ethtool_txq_regs[i].string, q);
1365 			data += ETH_GSTRING_LEN;
1366 		}
1367 	}
1368 
1369 	for (q = 0; q < port->nrxqs; q++) {
1370 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++) {
1371 			snprintf(data, ETH_GSTRING_LEN,
1372 				 mvpp2_ethtool_rxq_regs[i].string,
1373 				 q);
1374 			data += ETH_GSTRING_LEN;
1375 		}
1376 	}
1377 }
1378 
1379 static void mvpp2_read_stats(struct mvpp2_port *port)
1380 {
1381 	u64 *pstats;
1382 	int i, q;
1383 
1384 	pstats = port->ethtool_stats;
1385 
1386 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++)
1387 		*pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_mib_regs[i]);
1388 
1389 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++)
1390 		*pstats++ += mvpp2_read(port->priv,
1391 					mvpp2_ethtool_port_regs[i].offset +
1392 					4 * port->id);
1393 
1394 	for (q = 0; q < port->ntxqs; q++)
1395 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++)
1396 			*pstats++ += mvpp2_read_index(port->priv,
1397 						      MVPP22_CTRS_TX_CTR(port->id, i),
1398 						      mvpp2_ethtool_txq_regs[i].offset);
1399 
1400 	/* Rxqs are numbered from 0 from the user standpoint, but not from the
1401 	 * driver's. We need to add the  port->first_rxq offset.
1402 	 */
1403 	for (q = 0; q < port->nrxqs; q++)
1404 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++)
1405 			*pstats++ += mvpp2_read_index(port->priv,
1406 						      port->first_rxq + i,
1407 						      mvpp2_ethtool_rxq_regs[i].offset);
1408 }
1409 
1410 static void mvpp2_gather_hw_statistics(struct work_struct *work)
1411 {
1412 	struct delayed_work *del_work = to_delayed_work(work);
1413 	struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
1414 					       stats_work);
1415 
1416 	mutex_lock(&port->gather_stats_lock);
1417 
1418 	mvpp2_read_stats(port);
1419 
1420 	/* No need to read again the counters right after this function if it
1421 	 * was called asynchronously by the user (ie. use of ethtool).
1422 	 */
1423 	cancel_delayed_work(&port->stats_work);
1424 	queue_delayed_work(port->priv->stats_queue, &port->stats_work,
1425 			   MVPP2_MIB_COUNTERS_STATS_DELAY);
1426 
1427 	mutex_unlock(&port->gather_stats_lock);
1428 }
1429 
1430 static void mvpp2_ethtool_get_stats(struct net_device *dev,
1431 				    struct ethtool_stats *stats, u64 *data)
1432 {
1433 	struct mvpp2_port *port = netdev_priv(dev);
1434 
1435 	/* Update statistics for the given port, then take the lock to avoid
1436 	 * concurrent accesses on the ethtool_stats structure during its copy.
1437 	 */
1438 	mvpp2_gather_hw_statistics(&port->stats_work.work);
1439 
1440 	mutex_lock(&port->gather_stats_lock);
1441 	memcpy(data, port->ethtool_stats,
1442 	       sizeof(u64) * MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs));
1443 	mutex_unlock(&port->gather_stats_lock);
1444 }
1445 
1446 static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
1447 {
1448 	struct mvpp2_port *port = netdev_priv(dev);
1449 
1450 	if (sset == ETH_SS_STATS)
1451 		return MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs);
1452 
1453 	return -EOPNOTSUPP;
1454 }
1455 
1456 static void mvpp2_mac_reset_assert(struct mvpp2_port *port)
1457 {
1458 	u32 val;
1459 
1460 	val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) |
1461 	      MVPP2_GMAC_PORT_RESET_MASK;
1462 	writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
1463 
1464 	if (port->priv->hw_version == MVPP22 && port->gop_id == 0) {
1465 		val = readl(port->base + MVPP22_XLG_CTRL0_REG) &
1466 		      ~MVPP22_XLG_CTRL0_MAC_RESET_DIS;
1467 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1468 	}
1469 }
1470 
1471 static void mvpp22_pcs_reset_assert(struct mvpp2_port *port)
1472 {
1473 	struct mvpp2 *priv = port->priv;
1474 	void __iomem *mpcs, *xpcs;
1475 	u32 val;
1476 
1477 	if (port->priv->hw_version != MVPP22 || port->gop_id != 0)
1478 		return;
1479 
1480 	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1481 	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1482 
1483 	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1484 	val &= ~(MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
1485 	val |= MVPP22_MPCS_CLK_RESET_DIV_SET;
1486 	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1487 
1488 	val = readl(xpcs + MVPP22_XPCS_CFG0);
1489 	writel(val & ~MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
1490 }
1491 
1492 static void mvpp22_pcs_reset_deassert(struct mvpp2_port *port)
1493 {
1494 	struct mvpp2 *priv = port->priv;
1495 	void __iomem *mpcs, *xpcs;
1496 	u32 val;
1497 
1498 	if (port->priv->hw_version != MVPP22 || port->gop_id != 0)
1499 		return;
1500 
1501 	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1502 	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1503 
1504 	switch (port->phy_interface) {
1505 	case PHY_INTERFACE_MODE_10GKR:
1506 		val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1507 		val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX |
1508 		       MAC_CLK_RESET_SD_TX;
1509 		val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
1510 		writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1511 		break;
1512 	case PHY_INTERFACE_MODE_XAUI:
1513 	case PHY_INTERFACE_MODE_RXAUI:
1514 		val = readl(xpcs + MVPP22_XPCS_CFG0);
1515 		writel(val | MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
1516 		break;
1517 	default:
1518 		break;
1519 	}
1520 }
1521 
1522 /* Change maximum receive size of the port */
1523 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
1524 {
1525 	u32 val;
1526 
1527 	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1528 	val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
1529 	val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
1530 		    MVPP2_GMAC_MAX_RX_SIZE_OFFS);
1531 	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1532 }
1533 
1534 /* Change maximum receive size of the port */
1535 static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
1536 {
1537 	u32 val;
1538 
1539 	val =  readl(port->base + MVPP22_XLG_CTRL1_REG);
1540 	val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
1541 	val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
1542 	       MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
1543 	writel(val, port->base + MVPP22_XLG_CTRL1_REG);
1544 }
1545 
1546 /* Set defaults to the MVPP2 port */
1547 static void mvpp2_defaults_set(struct mvpp2_port *port)
1548 {
1549 	int tx_port_num, val, queue, lrxq;
1550 
1551 	if (port->priv->hw_version == MVPP21) {
1552 		/* Update TX FIFO MIN Threshold */
1553 		val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
1554 		val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
1555 		/* Min. TX threshold must be less than minimal packet length */
1556 		val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
1557 		writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
1558 	}
1559 
1560 	/* Disable Legacy WRR, Disable EJP, Release from reset */
1561 	tx_port_num = mvpp2_egress_port(port);
1562 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
1563 		    tx_port_num);
1564 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
1565 
1566 	/* Set TXQ scheduling to Round-Robin */
1567 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_FIXED_PRIO_REG, 0);
1568 
1569 	/* Close bandwidth for all queues */
1570 	for (queue = 0; queue < MVPP2_MAX_TXQ; queue++)
1571 		mvpp2_write(port->priv,
1572 			    MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(queue), 0);
1573 
1574 	/* Set refill period to 1 usec, refill tokens
1575 	 * and bucket size to maximum
1576 	 */
1577 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
1578 		    port->priv->tclk / USEC_PER_SEC);
1579 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
1580 	val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
1581 	val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
1582 	val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
1583 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
1584 	val = MVPP2_TXP_TOKEN_SIZE_MAX;
1585 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
1586 
1587 	/* Set MaximumLowLatencyPacketSize value to 256 */
1588 	mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
1589 		    MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
1590 		    MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
1591 
1592 	/* Enable Rx cache snoop */
1593 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1594 		queue = port->rxqs[lrxq]->id;
1595 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
1596 		val |= MVPP2_SNOOP_PKT_SIZE_MASK |
1597 			   MVPP2_SNOOP_BUF_HDR_MASK;
1598 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
1599 	}
1600 
1601 	/* At default, mask all interrupts to all present cpus */
1602 	mvpp2_interrupts_disable(port);
1603 }
1604 
1605 /* Enable/disable receiving packets */
1606 static void mvpp2_ingress_enable(struct mvpp2_port *port)
1607 {
1608 	u32 val;
1609 	int lrxq, queue;
1610 
1611 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1612 		queue = port->rxqs[lrxq]->id;
1613 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
1614 		val &= ~MVPP2_RXQ_DISABLE_MASK;
1615 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
1616 	}
1617 }
1618 
1619 static void mvpp2_ingress_disable(struct mvpp2_port *port)
1620 {
1621 	u32 val;
1622 	int lrxq, queue;
1623 
1624 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1625 		queue = port->rxqs[lrxq]->id;
1626 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
1627 		val |= MVPP2_RXQ_DISABLE_MASK;
1628 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
1629 	}
1630 }
1631 
1632 /* Enable transmit via physical egress queue
1633  * - HW starts take descriptors from DRAM
1634  */
1635 static void mvpp2_egress_enable(struct mvpp2_port *port)
1636 {
1637 	u32 qmap;
1638 	int queue;
1639 	int tx_port_num = mvpp2_egress_port(port);
1640 
1641 	/* Enable all initialized TXs. */
1642 	qmap = 0;
1643 	for (queue = 0; queue < port->ntxqs; queue++) {
1644 		struct mvpp2_tx_queue *txq = port->txqs[queue];
1645 
1646 		if (txq->descs)
1647 			qmap |= (1 << queue);
1648 	}
1649 
1650 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
1651 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
1652 }
1653 
1654 /* Disable transmit via physical egress queue
1655  * - HW doesn't take descriptors from DRAM
1656  */
1657 static void mvpp2_egress_disable(struct mvpp2_port *port)
1658 {
1659 	u32 reg_data;
1660 	int delay;
1661 	int tx_port_num = mvpp2_egress_port(port);
1662 
1663 	/* Issue stop command for active channels only */
1664 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
1665 	reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
1666 		    MVPP2_TXP_SCHED_ENQ_MASK;
1667 	if (reg_data != 0)
1668 		mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
1669 			    (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
1670 
1671 	/* Wait for all Tx activity to terminate. */
1672 	delay = 0;
1673 	do {
1674 		if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
1675 			netdev_warn(port->dev,
1676 				    "Tx stop timed out, status=0x%08x\n",
1677 				    reg_data);
1678 			break;
1679 		}
1680 		mdelay(1);
1681 		delay++;
1682 
1683 		/* Check port TX Command register that all
1684 		 * Tx queues are stopped
1685 		 */
1686 		reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
1687 	} while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
1688 }
1689 
1690 /* Rx descriptors helper methods */
1691 
1692 /* Get number of Rx descriptors occupied by received packets */
1693 static inline int
1694 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
1695 {
1696 	u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
1697 
1698 	return val & MVPP2_RXQ_OCCUPIED_MASK;
1699 }
1700 
1701 /* Update Rx queue status with the number of occupied and available
1702  * Rx descriptor slots.
1703  */
1704 static inline void
1705 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
1706 			int used_count, int free_count)
1707 {
1708 	/* Decrement the number of used descriptors and increment count
1709 	 * increment the number of free descriptors.
1710 	 */
1711 	u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
1712 
1713 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
1714 }
1715 
1716 /* Get pointer to next RX descriptor to be processed by SW */
1717 static inline struct mvpp2_rx_desc *
1718 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
1719 {
1720 	int rx_desc = rxq->next_desc_to_proc;
1721 
1722 	rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
1723 	prefetch(rxq->descs + rxq->next_desc_to_proc);
1724 	return rxq->descs + rx_desc;
1725 }
1726 
1727 /* Set rx queue offset */
1728 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
1729 				 int prxq, int offset)
1730 {
1731 	u32 val;
1732 
1733 	/* Convert offset from bytes to units of 32 bytes */
1734 	offset = offset >> 5;
1735 
1736 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
1737 	val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
1738 
1739 	/* Offset is in */
1740 	val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
1741 		    MVPP2_RXQ_PACKET_OFFSET_MASK);
1742 
1743 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
1744 }
1745 
1746 /* Tx descriptors helper methods */
1747 
1748 /* Get pointer to next Tx descriptor to be processed (send) by HW */
1749 static struct mvpp2_tx_desc *
1750 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
1751 {
1752 	int tx_desc = txq->next_desc_to_proc;
1753 
1754 	txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
1755 	return txq->descs + tx_desc;
1756 }
1757 
1758 /* Update HW with number of aggregated Tx descriptors to be sent
1759  *
1760  * Called only from mvpp2_tx(), so migration is disabled, using
1761  * smp_processor_id() is OK.
1762  */
1763 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
1764 {
1765 	/* aggregated access - relevant TXQ number is written in TX desc */
1766 	mvpp2_thread_write(port->priv,
1767 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1768 			   MVPP2_AGGR_TXQ_UPDATE_REG, pending);
1769 }
1770 
1771 /* Check if there are enough free descriptors in aggregated txq.
1772  * If not, update the number of occupied descriptors and repeat the check.
1773  *
1774  * Called only from mvpp2_tx(), so migration is disabled, using
1775  * smp_processor_id() is OK.
1776  */
1777 static int mvpp2_aggr_desc_num_check(struct mvpp2_port *port,
1778 				     struct mvpp2_tx_queue *aggr_txq, int num)
1779 {
1780 	if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
1781 		/* Update number of occupied aggregated Tx descriptors */
1782 		unsigned int thread =
1783 			mvpp2_cpu_to_thread(port->priv, smp_processor_id());
1784 		u32 val = mvpp2_read_relaxed(port->priv,
1785 					     MVPP2_AGGR_TXQ_STATUS_REG(thread));
1786 
1787 		aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
1788 
1789 		if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
1790 			return -ENOMEM;
1791 	}
1792 	return 0;
1793 }
1794 
1795 /* Reserved Tx descriptors allocation request
1796  *
1797  * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
1798  * only by mvpp2_tx(), so migration is disabled, using
1799  * smp_processor_id() is OK.
1800  */
1801 static int mvpp2_txq_alloc_reserved_desc(struct mvpp2_port *port,
1802 					 struct mvpp2_tx_queue *txq, int num)
1803 {
1804 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
1805 	struct mvpp2 *priv = port->priv;
1806 	u32 val;
1807 
1808 	val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
1809 	mvpp2_thread_write_relaxed(priv, thread, MVPP2_TXQ_RSVD_REQ_REG, val);
1810 
1811 	val = mvpp2_thread_read_relaxed(priv, thread, MVPP2_TXQ_RSVD_RSLT_REG);
1812 
1813 	return val & MVPP2_TXQ_RSVD_RSLT_MASK;
1814 }
1815 
1816 /* Check if there are enough reserved descriptors for transmission.
1817  * If not, request chunk of reserved descriptors and check again.
1818  */
1819 static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port *port,
1820 					    struct mvpp2_tx_queue *txq,
1821 					    struct mvpp2_txq_pcpu *txq_pcpu,
1822 					    int num)
1823 {
1824 	int req, desc_count;
1825 	unsigned int thread;
1826 
1827 	if (txq_pcpu->reserved_num >= num)
1828 		return 0;
1829 
1830 	/* Not enough descriptors reserved! Update the reserved descriptor
1831 	 * count and check again.
1832 	 */
1833 
1834 	desc_count = 0;
1835 	/* Compute total of used descriptors */
1836 	for (thread = 0; thread < port->priv->nthreads; thread++) {
1837 		struct mvpp2_txq_pcpu *txq_pcpu_aux;
1838 
1839 		txq_pcpu_aux = per_cpu_ptr(txq->pcpu, thread);
1840 		desc_count += txq_pcpu_aux->count;
1841 		desc_count += txq_pcpu_aux->reserved_num;
1842 	}
1843 
1844 	req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
1845 	desc_count += req;
1846 
1847 	if (desc_count >
1848 	   (txq->size - (MVPP2_MAX_THREADS * MVPP2_CPU_DESC_CHUNK)))
1849 		return -ENOMEM;
1850 
1851 	txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(port, txq, req);
1852 
1853 	/* OK, the descriptor could have been updated: check again. */
1854 	if (txq_pcpu->reserved_num < num)
1855 		return -ENOMEM;
1856 	return 0;
1857 }
1858 
1859 /* Release the last allocated Tx descriptor. Useful to handle DMA
1860  * mapping failures in the Tx path.
1861  */
1862 static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
1863 {
1864 	if (txq->next_desc_to_proc == 0)
1865 		txq->next_desc_to_proc = txq->last_desc - 1;
1866 	else
1867 		txq->next_desc_to_proc--;
1868 }
1869 
1870 /* Set Tx descriptors fields relevant for CSUM calculation */
1871 static u32 mvpp2_txq_desc_csum(int l3_offs, __be16 l3_proto,
1872 			       int ip_hdr_len, int l4_proto)
1873 {
1874 	u32 command;
1875 
1876 	/* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1877 	 * G_L4_chk, L4_type required only for checksum calculation
1878 	 */
1879 	command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
1880 	command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
1881 	command |= MVPP2_TXD_IP_CSUM_DISABLE;
1882 
1883 	if (l3_proto == htons(ETH_P_IP)) {
1884 		command &= ~MVPP2_TXD_IP_CSUM_DISABLE;	/* enable IPv4 csum */
1885 		command &= ~MVPP2_TXD_L3_IP6;		/* enable IPv4 */
1886 	} else {
1887 		command |= MVPP2_TXD_L3_IP6;		/* enable IPv6 */
1888 	}
1889 
1890 	if (l4_proto == IPPROTO_TCP) {
1891 		command &= ~MVPP2_TXD_L4_UDP;		/* enable TCP */
1892 		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
1893 	} else if (l4_proto == IPPROTO_UDP) {
1894 		command |= MVPP2_TXD_L4_UDP;		/* enable UDP */
1895 		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
1896 	} else {
1897 		command |= MVPP2_TXD_L4_CSUM_NOT;
1898 	}
1899 
1900 	return command;
1901 }
1902 
1903 /* Get number of sent descriptors and decrement counter.
1904  * The number of sent descriptors is returned.
1905  * Per-thread access
1906  *
1907  * Called only from mvpp2_txq_done(), called from mvpp2_tx()
1908  * (migration disabled) and from the TX completion tasklet (migration
1909  * disabled) so using smp_processor_id() is OK.
1910  */
1911 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
1912 					   struct mvpp2_tx_queue *txq)
1913 {
1914 	u32 val;
1915 
1916 	/* Reading status reg resets transmitted descriptor counter */
1917 	val = mvpp2_thread_read_relaxed(port->priv,
1918 					mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1919 					MVPP2_TXQ_SENT_REG(txq->id));
1920 
1921 	return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
1922 		MVPP2_TRANSMITTED_COUNT_OFFSET;
1923 }
1924 
1925 /* Called through on_each_cpu(), so runs on all CPUs, with migration
1926  * disabled, therefore using smp_processor_id() is OK.
1927  */
1928 static void mvpp2_txq_sent_counter_clear(void *arg)
1929 {
1930 	struct mvpp2_port *port = arg;
1931 	int queue;
1932 
1933 	/* If the thread isn't used, don't do anything */
1934 	if (smp_processor_id() > port->priv->nthreads)
1935 		return;
1936 
1937 	for (queue = 0; queue < port->ntxqs; queue++) {
1938 		int id = port->txqs[queue]->id;
1939 
1940 		mvpp2_thread_read(port->priv,
1941 				  mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1942 				  MVPP2_TXQ_SENT_REG(id));
1943 	}
1944 }
1945 
1946 /* Set max sizes for Tx queues */
1947 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
1948 {
1949 	u32	val, size, mtu;
1950 	int	txq, tx_port_num;
1951 
1952 	mtu = port->pkt_size * 8;
1953 	if (mtu > MVPP2_TXP_MTU_MAX)
1954 		mtu = MVPP2_TXP_MTU_MAX;
1955 
1956 	/* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
1957 	mtu = 3 * mtu;
1958 
1959 	/* Indirect access to registers */
1960 	tx_port_num = mvpp2_egress_port(port);
1961 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
1962 
1963 	/* Set MTU */
1964 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
1965 	val &= ~MVPP2_TXP_MTU_MAX;
1966 	val |= mtu;
1967 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
1968 
1969 	/* TXP token size and all TXQs token size must be larger that MTU */
1970 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
1971 	size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
1972 	if (size < mtu) {
1973 		size = mtu;
1974 		val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
1975 		val |= size;
1976 		mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
1977 	}
1978 
1979 	for (txq = 0; txq < port->ntxqs; txq++) {
1980 		val = mvpp2_read(port->priv,
1981 				 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
1982 		size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
1983 
1984 		if (size < mtu) {
1985 			size = mtu;
1986 			val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
1987 			val |= size;
1988 			mvpp2_write(port->priv,
1989 				    MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
1990 				    val);
1991 		}
1992 	}
1993 }
1994 
1995 /* Set the number of packets that will be received before Rx interrupt
1996  * will be generated by HW.
1997  */
1998 static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
1999 				   struct mvpp2_rx_queue *rxq)
2000 {
2001 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2002 
2003 	if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
2004 		rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
2005 
2006 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2007 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_THRESH_REG,
2008 			   rxq->pkts_coal);
2009 
2010 	put_cpu();
2011 }
2012 
2013 /* For some reason in the LSP this is done on each CPU. Why ? */
2014 static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
2015 				   struct mvpp2_tx_queue *txq)
2016 {
2017 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2018 	u32 val;
2019 
2020 	if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
2021 		txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
2022 
2023 	val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
2024 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2025 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_THRESH_REG, val);
2026 
2027 	put_cpu();
2028 }
2029 
2030 static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
2031 {
2032 	u64 tmp = (u64)clk_hz * usec;
2033 
2034 	do_div(tmp, USEC_PER_SEC);
2035 
2036 	return tmp > U32_MAX ? U32_MAX : tmp;
2037 }
2038 
2039 static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
2040 {
2041 	u64 tmp = (u64)cycles * USEC_PER_SEC;
2042 
2043 	do_div(tmp, clk_hz);
2044 
2045 	return tmp > U32_MAX ? U32_MAX : tmp;
2046 }
2047 
2048 /* Set the time delay in usec before Rx interrupt */
2049 static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
2050 				   struct mvpp2_rx_queue *rxq)
2051 {
2052 	unsigned long freq = port->priv->tclk;
2053 	u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2054 
2055 	if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
2056 		rxq->time_coal =
2057 			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
2058 
2059 		/* re-evaluate to get actual register value */
2060 		val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2061 	}
2062 
2063 	mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
2064 }
2065 
2066 static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
2067 {
2068 	unsigned long freq = port->priv->tclk;
2069 	u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2070 
2071 	if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
2072 		port->tx_time_coal =
2073 			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
2074 
2075 		/* re-evaluate to get actual register value */
2076 		val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2077 	}
2078 
2079 	mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
2080 }
2081 
2082 /* Free Tx queue skbuffs */
2083 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
2084 				struct mvpp2_tx_queue *txq,
2085 				struct mvpp2_txq_pcpu *txq_pcpu, int num)
2086 {
2087 	int i;
2088 
2089 	for (i = 0; i < num; i++) {
2090 		struct mvpp2_txq_pcpu_buf *tx_buf =
2091 			txq_pcpu->buffs + txq_pcpu->txq_get_index;
2092 
2093 		if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
2094 			dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
2095 					 tx_buf->size, DMA_TO_DEVICE);
2096 		if (tx_buf->skb)
2097 			dev_kfree_skb_any(tx_buf->skb);
2098 
2099 		mvpp2_txq_inc_get(txq_pcpu);
2100 	}
2101 }
2102 
2103 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
2104 							u32 cause)
2105 {
2106 	int queue = fls(cause) - 1;
2107 
2108 	return port->rxqs[queue];
2109 }
2110 
2111 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
2112 							u32 cause)
2113 {
2114 	int queue = fls(cause) - 1;
2115 
2116 	return port->txqs[queue];
2117 }
2118 
2119 /* Handle end of transmission */
2120 static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
2121 			   struct mvpp2_txq_pcpu *txq_pcpu)
2122 {
2123 	struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
2124 	int tx_done;
2125 
2126 	if (txq_pcpu->thread != mvpp2_cpu_to_thread(port->priv, smp_processor_id()))
2127 		netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
2128 
2129 	tx_done = mvpp2_txq_sent_desc_proc(port, txq);
2130 	if (!tx_done)
2131 		return;
2132 	mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
2133 
2134 	txq_pcpu->count -= tx_done;
2135 
2136 	if (netif_tx_queue_stopped(nq))
2137 		if (txq_pcpu->count <= txq_pcpu->wake_threshold)
2138 			netif_tx_wake_queue(nq);
2139 }
2140 
2141 static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
2142 				  unsigned int thread)
2143 {
2144 	struct mvpp2_tx_queue *txq;
2145 	struct mvpp2_txq_pcpu *txq_pcpu;
2146 	unsigned int tx_todo = 0;
2147 
2148 	while (cause) {
2149 		txq = mvpp2_get_tx_queue(port, cause);
2150 		if (!txq)
2151 			break;
2152 
2153 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2154 
2155 		if (txq_pcpu->count) {
2156 			mvpp2_txq_done(port, txq, txq_pcpu);
2157 			tx_todo += txq_pcpu->count;
2158 		}
2159 
2160 		cause &= ~(1 << txq->log_id);
2161 	}
2162 	return tx_todo;
2163 }
2164 
2165 /* Rx/Tx queue initialization/cleanup methods */
2166 
2167 /* Allocate and initialize descriptors for aggr TXQ */
2168 static int mvpp2_aggr_txq_init(struct platform_device *pdev,
2169 			       struct mvpp2_tx_queue *aggr_txq,
2170 			       unsigned int thread, struct mvpp2 *priv)
2171 {
2172 	u32 txq_dma;
2173 
2174 	/* Allocate memory for TX descriptors */
2175 	aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
2176 					     MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
2177 					     &aggr_txq->descs_dma, GFP_KERNEL);
2178 	if (!aggr_txq->descs)
2179 		return -ENOMEM;
2180 
2181 	aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
2182 
2183 	/* Aggr TXQ no reset WA */
2184 	aggr_txq->next_desc_to_proc = mvpp2_read(priv,
2185 						 MVPP2_AGGR_TXQ_INDEX_REG(thread));
2186 
2187 	/* Set Tx descriptors queue starting address indirect
2188 	 * access
2189 	 */
2190 	if (priv->hw_version == MVPP21)
2191 		txq_dma = aggr_txq->descs_dma;
2192 	else
2193 		txq_dma = aggr_txq->descs_dma >>
2194 			MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
2195 
2196 	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(thread), txq_dma);
2197 	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(thread),
2198 		    MVPP2_AGGR_TXQ_SIZE);
2199 
2200 	return 0;
2201 }
2202 
2203 /* Create a specified Rx queue */
2204 static int mvpp2_rxq_init(struct mvpp2_port *port,
2205 			  struct mvpp2_rx_queue *rxq)
2206 
2207 {
2208 	unsigned int thread;
2209 	u32 rxq_dma;
2210 
2211 	rxq->size = port->rx_ring_size;
2212 
2213 	/* Allocate memory for RX descriptors */
2214 	rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
2215 					rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2216 					&rxq->descs_dma, GFP_KERNEL);
2217 	if (!rxq->descs)
2218 		return -ENOMEM;
2219 
2220 	rxq->last_desc = rxq->size - 1;
2221 
2222 	/* Zero occupied and non-occupied counters - direct access */
2223 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2224 
2225 	/* Set Rx descriptors queue starting address - indirect access */
2226 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2227 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2228 	if (port->priv->hw_version == MVPP21)
2229 		rxq_dma = rxq->descs_dma;
2230 	else
2231 		rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
2232 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
2233 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
2234 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_INDEX_REG, 0);
2235 	put_cpu();
2236 
2237 	/* Set Offset */
2238 	mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
2239 
2240 	/* Set coalescing pkts and time */
2241 	mvpp2_rx_pkts_coal_set(port, rxq);
2242 	mvpp2_rx_time_coal_set(port, rxq);
2243 
2244 	/* Add number of descriptors ready for receiving packets */
2245 	mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
2246 
2247 	return 0;
2248 }
2249 
2250 /* Push packets received by the RXQ to BM pool */
2251 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
2252 				struct mvpp2_rx_queue *rxq)
2253 {
2254 	int rx_received, i;
2255 
2256 	rx_received = mvpp2_rxq_received(port, rxq->id);
2257 	if (!rx_received)
2258 		return;
2259 
2260 	for (i = 0; i < rx_received; i++) {
2261 		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
2262 		u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
2263 		int pool;
2264 
2265 		pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
2266 			MVPP2_RXD_BM_POOL_ID_OFFS;
2267 
2268 		mvpp2_bm_pool_put(port, pool,
2269 				  mvpp2_rxdesc_dma_addr_get(port, rx_desc),
2270 				  mvpp2_rxdesc_cookie_get(port, rx_desc));
2271 	}
2272 	mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
2273 }
2274 
2275 /* Cleanup Rx queue */
2276 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
2277 			     struct mvpp2_rx_queue *rxq)
2278 {
2279 	unsigned int thread;
2280 
2281 	mvpp2_rxq_drop_pkts(port, rxq);
2282 
2283 	if (rxq->descs)
2284 		dma_free_coherent(port->dev->dev.parent,
2285 				  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2286 				  rxq->descs,
2287 				  rxq->descs_dma);
2288 
2289 	rxq->descs             = NULL;
2290 	rxq->last_desc         = 0;
2291 	rxq->next_desc_to_proc = 0;
2292 	rxq->descs_dma         = 0;
2293 
2294 	/* Clear Rx descriptors queue starting address and size;
2295 	 * free descriptor number
2296 	 */
2297 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2298 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2299 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2300 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, 0);
2301 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, 0);
2302 	put_cpu();
2303 }
2304 
2305 /* Create and initialize a Tx queue */
2306 static int mvpp2_txq_init(struct mvpp2_port *port,
2307 			  struct mvpp2_tx_queue *txq)
2308 {
2309 	u32 val;
2310 	unsigned int thread;
2311 	int desc, desc_per_txq, tx_port_num;
2312 	struct mvpp2_txq_pcpu *txq_pcpu;
2313 
2314 	txq->size = port->tx_ring_size;
2315 
2316 	/* Allocate memory for Tx descriptors */
2317 	txq->descs = dma_alloc_coherent(port->dev->dev.parent,
2318 				txq->size * MVPP2_DESC_ALIGNED_SIZE,
2319 				&txq->descs_dma, GFP_KERNEL);
2320 	if (!txq->descs)
2321 		return -ENOMEM;
2322 
2323 	txq->last_desc = txq->size - 1;
2324 
2325 	/* Set Tx descriptors queue starting address - indirect access */
2326 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2327 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2328 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG,
2329 			   txq->descs_dma);
2330 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG,
2331 			   txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
2332 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_INDEX_REG, 0);
2333 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_RSVD_CLR_REG,
2334 			   txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
2335 	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PENDING_REG);
2336 	val &= ~MVPP2_TXQ_PENDING_MASK;
2337 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PENDING_REG, val);
2338 
2339 	/* Calculate base address in prefetch buffer. We reserve 16 descriptors
2340 	 * for each existing TXQ.
2341 	 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
2342 	 * GBE ports assumed to be continuous from 0 to MVPP2_MAX_PORTS
2343 	 */
2344 	desc_per_txq = 16;
2345 	desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
2346 	       (txq->log_id * desc_per_txq);
2347 
2348 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG,
2349 			   MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
2350 			   MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
2351 	put_cpu();
2352 
2353 	/* WRR / EJP configuration - indirect access */
2354 	tx_port_num = mvpp2_egress_port(port);
2355 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2356 
2357 	val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
2358 	val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
2359 	val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
2360 	val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
2361 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
2362 
2363 	val = MVPP2_TXQ_TOKEN_SIZE_MAX;
2364 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
2365 		    val);
2366 
2367 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2368 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2369 		txq_pcpu->size = txq->size;
2370 		txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
2371 						sizeof(*txq_pcpu->buffs),
2372 						GFP_KERNEL);
2373 		if (!txq_pcpu->buffs)
2374 			return -ENOMEM;
2375 
2376 		txq_pcpu->count = 0;
2377 		txq_pcpu->reserved_num = 0;
2378 		txq_pcpu->txq_put_index = 0;
2379 		txq_pcpu->txq_get_index = 0;
2380 		txq_pcpu->tso_headers = NULL;
2381 
2382 		txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
2383 		txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
2384 
2385 		txq_pcpu->tso_headers =
2386 			dma_alloc_coherent(port->dev->dev.parent,
2387 					   txq_pcpu->size * TSO_HEADER_SIZE,
2388 					   &txq_pcpu->tso_headers_dma,
2389 					   GFP_KERNEL);
2390 		if (!txq_pcpu->tso_headers)
2391 			return -ENOMEM;
2392 	}
2393 
2394 	return 0;
2395 }
2396 
2397 /* Free allocated TXQ resources */
2398 static void mvpp2_txq_deinit(struct mvpp2_port *port,
2399 			     struct mvpp2_tx_queue *txq)
2400 {
2401 	struct mvpp2_txq_pcpu *txq_pcpu;
2402 	unsigned int thread;
2403 
2404 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2405 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2406 		kfree(txq_pcpu->buffs);
2407 
2408 		if (txq_pcpu->tso_headers)
2409 			dma_free_coherent(port->dev->dev.parent,
2410 					  txq_pcpu->size * TSO_HEADER_SIZE,
2411 					  txq_pcpu->tso_headers,
2412 					  txq_pcpu->tso_headers_dma);
2413 
2414 		txq_pcpu->tso_headers = NULL;
2415 	}
2416 
2417 	if (txq->descs)
2418 		dma_free_coherent(port->dev->dev.parent,
2419 				  txq->size * MVPP2_DESC_ALIGNED_SIZE,
2420 				  txq->descs, txq->descs_dma);
2421 
2422 	txq->descs             = NULL;
2423 	txq->last_desc         = 0;
2424 	txq->next_desc_to_proc = 0;
2425 	txq->descs_dma         = 0;
2426 
2427 	/* Set minimum bandwidth for disabled TXQs */
2428 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->log_id), 0);
2429 
2430 	/* Set Tx descriptors queue starting address and size */
2431 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2432 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2433 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG, 0);
2434 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG, 0);
2435 	put_cpu();
2436 }
2437 
2438 /* Cleanup Tx ports */
2439 static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
2440 {
2441 	struct mvpp2_txq_pcpu *txq_pcpu;
2442 	int delay, pending;
2443 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2444 	u32 val;
2445 
2446 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2447 	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG);
2448 	val |= MVPP2_TXQ_DRAIN_EN_MASK;
2449 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
2450 
2451 	/* The napi queue has been stopped so wait for all packets
2452 	 * to be transmitted.
2453 	 */
2454 	delay = 0;
2455 	do {
2456 		if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
2457 			netdev_warn(port->dev,
2458 				    "port %d: cleaning queue %d timed out\n",
2459 				    port->id, txq->log_id);
2460 			break;
2461 		}
2462 		mdelay(1);
2463 		delay++;
2464 
2465 		pending = mvpp2_thread_read(port->priv, thread,
2466 					    MVPP2_TXQ_PENDING_REG);
2467 		pending &= MVPP2_TXQ_PENDING_MASK;
2468 	} while (pending);
2469 
2470 	val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
2471 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
2472 	put_cpu();
2473 
2474 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2475 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2476 
2477 		/* Release all packets */
2478 		mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
2479 
2480 		/* Reset queue */
2481 		txq_pcpu->count = 0;
2482 		txq_pcpu->txq_put_index = 0;
2483 		txq_pcpu->txq_get_index = 0;
2484 	}
2485 }
2486 
2487 /* Cleanup all Tx queues */
2488 static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
2489 {
2490 	struct mvpp2_tx_queue *txq;
2491 	int queue;
2492 	u32 val;
2493 
2494 	val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
2495 
2496 	/* Reset Tx ports and delete Tx queues */
2497 	val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
2498 	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
2499 
2500 	for (queue = 0; queue < port->ntxqs; queue++) {
2501 		txq = port->txqs[queue];
2502 		mvpp2_txq_clean(port, txq);
2503 		mvpp2_txq_deinit(port, txq);
2504 	}
2505 
2506 	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
2507 
2508 	val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
2509 	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
2510 }
2511 
2512 /* Cleanup all Rx queues */
2513 static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
2514 {
2515 	int queue;
2516 
2517 	for (queue = 0; queue < port->nrxqs; queue++)
2518 		mvpp2_rxq_deinit(port, port->rxqs[queue]);
2519 }
2520 
2521 /* Init all Rx queues for port */
2522 static int mvpp2_setup_rxqs(struct mvpp2_port *port)
2523 {
2524 	int queue, err;
2525 
2526 	for (queue = 0; queue < port->nrxqs; queue++) {
2527 		err = mvpp2_rxq_init(port, port->rxqs[queue]);
2528 		if (err)
2529 			goto err_cleanup;
2530 	}
2531 	return 0;
2532 
2533 err_cleanup:
2534 	mvpp2_cleanup_rxqs(port);
2535 	return err;
2536 }
2537 
2538 /* Init all tx queues for port */
2539 static int mvpp2_setup_txqs(struct mvpp2_port *port)
2540 {
2541 	struct mvpp2_tx_queue *txq;
2542 	int queue, err, cpu;
2543 
2544 	for (queue = 0; queue < port->ntxqs; queue++) {
2545 		txq = port->txqs[queue];
2546 		err = mvpp2_txq_init(port, txq);
2547 		if (err)
2548 			goto err_cleanup;
2549 
2550 		/* Assign this queue to a CPU */
2551 		cpu = queue % num_present_cpus();
2552 		netif_set_xps_queue(port->dev, cpumask_of(cpu), queue);
2553 	}
2554 
2555 	if (port->has_tx_irqs) {
2556 		mvpp2_tx_time_coal_set(port);
2557 		for (queue = 0; queue < port->ntxqs; queue++) {
2558 			txq = port->txqs[queue];
2559 			mvpp2_tx_pkts_coal_set(port, txq);
2560 		}
2561 	}
2562 
2563 	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
2564 	return 0;
2565 
2566 err_cleanup:
2567 	mvpp2_cleanup_txqs(port);
2568 	return err;
2569 }
2570 
2571 /* The callback for per-port interrupt */
2572 static irqreturn_t mvpp2_isr(int irq, void *dev_id)
2573 {
2574 	struct mvpp2_queue_vector *qv = dev_id;
2575 
2576 	mvpp2_qvec_interrupt_disable(qv);
2577 
2578 	napi_schedule(&qv->napi);
2579 
2580 	return IRQ_HANDLED;
2581 }
2582 
2583 /* Per-port interrupt for link status changes */
2584 static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
2585 {
2586 	struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
2587 	struct net_device *dev = port->dev;
2588 	bool event = false, link = false;
2589 	u32 val;
2590 
2591 	mvpp22_gop_mask_irq(port);
2592 
2593 	if (port->gop_id == 0 && mvpp2_is_xlg(port->phy_interface)) {
2594 		val = readl(port->base + MVPP22_XLG_INT_STAT);
2595 		if (val & MVPP22_XLG_INT_STAT_LINK) {
2596 			event = true;
2597 			val = readl(port->base + MVPP22_XLG_STATUS);
2598 			if (val & MVPP22_XLG_STATUS_LINK_UP)
2599 				link = true;
2600 		}
2601 	} else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
2602 		   phy_interface_mode_is_8023z(port->phy_interface) ||
2603 		   port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
2604 		val = readl(port->base + MVPP22_GMAC_INT_STAT);
2605 		if (val & MVPP22_GMAC_INT_STAT_LINK) {
2606 			event = true;
2607 			val = readl(port->base + MVPP2_GMAC_STATUS0);
2608 			if (val & MVPP2_GMAC_STATUS0_LINK_UP)
2609 				link = true;
2610 		}
2611 	}
2612 
2613 	if (port->phylink) {
2614 		phylink_mac_change(port->phylink, link);
2615 		goto handled;
2616 	}
2617 
2618 	if (!netif_running(dev) || !event)
2619 		goto handled;
2620 
2621 	if (link) {
2622 		mvpp2_interrupts_enable(port);
2623 
2624 		mvpp2_egress_enable(port);
2625 		mvpp2_ingress_enable(port);
2626 		netif_carrier_on(dev);
2627 		netif_tx_wake_all_queues(dev);
2628 	} else {
2629 		netif_tx_stop_all_queues(dev);
2630 		netif_carrier_off(dev);
2631 		mvpp2_ingress_disable(port);
2632 		mvpp2_egress_disable(port);
2633 
2634 		mvpp2_interrupts_disable(port);
2635 	}
2636 
2637 handled:
2638 	mvpp22_gop_unmask_irq(port);
2639 	return IRQ_HANDLED;
2640 }
2641 
2642 static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
2643 {
2644 	ktime_t interval;
2645 
2646 	if (!port_pcpu->timer_scheduled) {
2647 		port_pcpu->timer_scheduled = true;
2648 		interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
2649 		hrtimer_start(&port_pcpu->tx_done_timer, interval,
2650 			      HRTIMER_MODE_REL_PINNED);
2651 	}
2652 }
2653 
2654 static void mvpp2_tx_proc_cb(unsigned long data)
2655 {
2656 	struct net_device *dev = (struct net_device *)data;
2657 	struct mvpp2_port *port = netdev_priv(dev);
2658 	struct mvpp2_port_pcpu *port_pcpu;
2659 	unsigned int tx_todo, cause;
2660 
2661 	port_pcpu = per_cpu_ptr(port->pcpu,
2662 				mvpp2_cpu_to_thread(port->priv, smp_processor_id()));
2663 
2664 	if (!netif_running(dev))
2665 		return;
2666 	port_pcpu->timer_scheduled = false;
2667 
2668 	/* Process all the Tx queues */
2669 	cause = (1 << port->ntxqs) - 1;
2670 	tx_todo = mvpp2_tx_done(port, cause,
2671 				mvpp2_cpu_to_thread(port->priv, smp_processor_id()));
2672 
2673 	/* Set the timer in case not all the packets were processed */
2674 	if (tx_todo)
2675 		mvpp2_timer_set(port_pcpu);
2676 }
2677 
2678 static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
2679 {
2680 	struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
2681 							 struct mvpp2_port_pcpu,
2682 							 tx_done_timer);
2683 
2684 	tasklet_schedule(&port_pcpu->tx_done_tasklet);
2685 
2686 	return HRTIMER_NORESTART;
2687 }
2688 
2689 /* Main RX/TX processing routines */
2690 
2691 /* Display more error info */
2692 static void mvpp2_rx_error(struct mvpp2_port *port,
2693 			   struct mvpp2_rx_desc *rx_desc)
2694 {
2695 	u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
2696 	size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
2697 	char *err_str = NULL;
2698 
2699 	switch (status & MVPP2_RXD_ERR_CODE_MASK) {
2700 	case MVPP2_RXD_ERR_CRC:
2701 		err_str = "crc";
2702 		break;
2703 	case MVPP2_RXD_ERR_OVERRUN:
2704 		err_str = "overrun";
2705 		break;
2706 	case MVPP2_RXD_ERR_RESOURCE:
2707 		err_str = "resource";
2708 		break;
2709 	}
2710 	if (err_str && net_ratelimit())
2711 		netdev_err(port->dev,
2712 			   "bad rx status %08x (%s error), size=%zu\n",
2713 			   status, err_str, sz);
2714 }
2715 
2716 /* Handle RX checksum offload */
2717 static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
2718 			  struct sk_buff *skb)
2719 {
2720 	if (((status & MVPP2_RXD_L3_IP4) &&
2721 	     !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
2722 	    (status & MVPP2_RXD_L3_IP6))
2723 		if (((status & MVPP2_RXD_L4_UDP) ||
2724 		     (status & MVPP2_RXD_L4_TCP)) &&
2725 		     (status & MVPP2_RXD_L4_CSUM_OK)) {
2726 			skb->csum = 0;
2727 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2728 			return;
2729 		}
2730 
2731 	skb->ip_summed = CHECKSUM_NONE;
2732 }
2733 
2734 /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
2735 static int mvpp2_rx_refill(struct mvpp2_port *port,
2736 			   struct mvpp2_bm_pool *bm_pool, int pool)
2737 {
2738 	dma_addr_t dma_addr;
2739 	phys_addr_t phys_addr;
2740 	void *buf;
2741 
2742 	/* No recycle or too many buffers are in use, so allocate a new skb */
2743 	buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
2744 			      GFP_ATOMIC);
2745 	if (!buf)
2746 		return -ENOMEM;
2747 
2748 	mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
2749 
2750 	return 0;
2751 }
2752 
2753 /* Handle tx checksum */
2754 static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
2755 {
2756 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2757 		int ip_hdr_len = 0;
2758 		u8 l4_proto;
2759 		__be16 l3_proto = vlan_get_protocol(skb);
2760 
2761 		if (l3_proto == htons(ETH_P_IP)) {
2762 			struct iphdr *ip4h = ip_hdr(skb);
2763 
2764 			/* Calculate IPv4 checksum and L4 checksum */
2765 			ip_hdr_len = ip4h->ihl;
2766 			l4_proto = ip4h->protocol;
2767 		} else if (l3_proto == htons(ETH_P_IPV6)) {
2768 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
2769 
2770 			/* Read l4_protocol from one of IPv6 extra headers */
2771 			if (skb_network_header_len(skb) > 0)
2772 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
2773 			l4_proto = ip6h->nexthdr;
2774 		} else {
2775 			return MVPP2_TXD_L4_CSUM_NOT;
2776 		}
2777 
2778 		return mvpp2_txq_desc_csum(skb_network_offset(skb),
2779 					   l3_proto, ip_hdr_len, l4_proto);
2780 	}
2781 
2782 	return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
2783 }
2784 
2785 /* Main rx processing */
2786 static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
2787 		    int rx_todo, struct mvpp2_rx_queue *rxq)
2788 {
2789 	struct net_device *dev = port->dev;
2790 	int rx_received;
2791 	int rx_done = 0;
2792 	u32 rcvd_pkts = 0;
2793 	u32 rcvd_bytes = 0;
2794 
2795 	/* Get number of received packets and clamp the to-do */
2796 	rx_received = mvpp2_rxq_received(port, rxq->id);
2797 	if (rx_todo > rx_received)
2798 		rx_todo = rx_received;
2799 
2800 	while (rx_done < rx_todo) {
2801 		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
2802 		struct mvpp2_bm_pool *bm_pool;
2803 		struct sk_buff *skb;
2804 		unsigned int frag_size;
2805 		dma_addr_t dma_addr;
2806 		phys_addr_t phys_addr;
2807 		u32 rx_status;
2808 		int pool, rx_bytes, err;
2809 		void *data;
2810 
2811 		rx_done++;
2812 		rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
2813 		rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
2814 		rx_bytes -= MVPP2_MH_SIZE;
2815 		dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
2816 		phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
2817 		data = (void *)phys_to_virt(phys_addr);
2818 
2819 		pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
2820 			MVPP2_RXD_BM_POOL_ID_OFFS;
2821 		bm_pool = &port->priv->bm_pools[pool];
2822 
2823 		/* In case of an error, release the requested buffer pointer
2824 		 * to the Buffer Manager. This request process is controlled
2825 		 * by the hardware, and the information about the buffer is
2826 		 * comprised by the RX descriptor.
2827 		 */
2828 		if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
2829 err_drop_frame:
2830 			dev->stats.rx_errors++;
2831 			mvpp2_rx_error(port, rx_desc);
2832 			/* Return the buffer to the pool */
2833 			mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
2834 			continue;
2835 		}
2836 
2837 		if (bm_pool->frag_size > PAGE_SIZE)
2838 			frag_size = 0;
2839 		else
2840 			frag_size = bm_pool->frag_size;
2841 
2842 		skb = build_skb(data, frag_size);
2843 		if (!skb) {
2844 			netdev_warn(port->dev, "skb build failed\n");
2845 			goto err_drop_frame;
2846 		}
2847 
2848 		err = mvpp2_rx_refill(port, bm_pool, pool);
2849 		if (err) {
2850 			netdev_err(port->dev, "failed to refill BM pools\n");
2851 			goto err_drop_frame;
2852 		}
2853 
2854 		dma_unmap_single(dev->dev.parent, dma_addr,
2855 				 bm_pool->buf_size, DMA_FROM_DEVICE);
2856 
2857 		rcvd_pkts++;
2858 		rcvd_bytes += rx_bytes;
2859 
2860 		skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
2861 		skb_put(skb, rx_bytes);
2862 		skb->protocol = eth_type_trans(skb, dev);
2863 		mvpp2_rx_csum(port, rx_status, skb);
2864 
2865 		napi_gro_receive(napi, skb);
2866 	}
2867 
2868 	if (rcvd_pkts) {
2869 		struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
2870 
2871 		u64_stats_update_begin(&stats->syncp);
2872 		stats->rx_packets += rcvd_pkts;
2873 		stats->rx_bytes   += rcvd_bytes;
2874 		u64_stats_update_end(&stats->syncp);
2875 	}
2876 
2877 	/* Update Rx queue management counters */
2878 	wmb();
2879 	mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
2880 
2881 	return rx_todo;
2882 }
2883 
2884 static inline void
2885 tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
2886 		  struct mvpp2_tx_desc *desc)
2887 {
2888 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2889 	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2890 
2891 	dma_addr_t buf_dma_addr =
2892 		mvpp2_txdesc_dma_addr_get(port, desc);
2893 	size_t buf_sz =
2894 		mvpp2_txdesc_size_get(port, desc);
2895 	if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
2896 		dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
2897 				 buf_sz, DMA_TO_DEVICE);
2898 	mvpp2_txq_desc_put(txq);
2899 }
2900 
2901 /* Handle tx fragmentation processing */
2902 static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
2903 				 struct mvpp2_tx_queue *aggr_txq,
2904 				 struct mvpp2_tx_queue *txq)
2905 {
2906 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2907 	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2908 	struct mvpp2_tx_desc *tx_desc;
2909 	int i;
2910 	dma_addr_t buf_dma_addr;
2911 
2912 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2913 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2914 		void *addr = page_address(frag->page.p) + frag->page_offset;
2915 
2916 		tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
2917 		mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
2918 		mvpp2_txdesc_size_set(port, tx_desc, frag->size);
2919 
2920 		buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
2921 					      frag->size, DMA_TO_DEVICE);
2922 		if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
2923 			mvpp2_txq_desc_put(txq);
2924 			goto cleanup;
2925 		}
2926 
2927 		mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
2928 
2929 		if (i == (skb_shinfo(skb)->nr_frags - 1)) {
2930 			/* Last descriptor */
2931 			mvpp2_txdesc_cmd_set(port, tx_desc,
2932 					     MVPP2_TXD_L_DESC);
2933 			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
2934 		} else {
2935 			/* Descriptor in the middle: Not First, Not Last */
2936 			mvpp2_txdesc_cmd_set(port, tx_desc, 0);
2937 			mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
2938 		}
2939 	}
2940 
2941 	return 0;
2942 cleanup:
2943 	/* Release all descriptors that were used to map fragments of
2944 	 * this packet, as well as the corresponding DMA mappings
2945 	 */
2946 	for (i = i - 1; i >= 0; i--) {
2947 		tx_desc = txq->descs + i;
2948 		tx_desc_unmap_put(port, txq, tx_desc);
2949 	}
2950 
2951 	return -ENOMEM;
2952 }
2953 
2954 static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
2955 				     struct net_device *dev,
2956 				     struct mvpp2_tx_queue *txq,
2957 				     struct mvpp2_tx_queue *aggr_txq,
2958 				     struct mvpp2_txq_pcpu *txq_pcpu,
2959 				     int hdr_sz)
2960 {
2961 	struct mvpp2_port *port = netdev_priv(dev);
2962 	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
2963 	dma_addr_t addr;
2964 
2965 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
2966 	mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
2967 
2968 	addr = txq_pcpu->tso_headers_dma +
2969 	       txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
2970 	mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
2971 
2972 	mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
2973 					    MVPP2_TXD_F_DESC |
2974 					    MVPP2_TXD_PADDING_DISABLE);
2975 	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
2976 }
2977 
2978 static inline int mvpp2_tso_put_data(struct sk_buff *skb,
2979 				     struct net_device *dev, struct tso_t *tso,
2980 				     struct mvpp2_tx_queue *txq,
2981 				     struct mvpp2_tx_queue *aggr_txq,
2982 				     struct mvpp2_txq_pcpu *txq_pcpu,
2983 				     int sz, bool left, bool last)
2984 {
2985 	struct mvpp2_port *port = netdev_priv(dev);
2986 	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
2987 	dma_addr_t buf_dma_addr;
2988 
2989 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
2990 	mvpp2_txdesc_size_set(port, tx_desc, sz);
2991 
2992 	buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
2993 				      DMA_TO_DEVICE);
2994 	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
2995 		mvpp2_txq_desc_put(txq);
2996 		return -ENOMEM;
2997 	}
2998 
2999 	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3000 
3001 	if (!left) {
3002 		mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
3003 		if (last) {
3004 			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
3005 			return 0;
3006 		}
3007 	} else {
3008 		mvpp2_txdesc_cmd_set(port, tx_desc, 0);
3009 	}
3010 
3011 	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
3012 	return 0;
3013 }
3014 
3015 static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
3016 			struct mvpp2_tx_queue *txq,
3017 			struct mvpp2_tx_queue *aggr_txq,
3018 			struct mvpp2_txq_pcpu *txq_pcpu)
3019 {
3020 	struct mvpp2_port *port = netdev_priv(dev);
3021 	struct tso_t tso;
3022 	int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
3023 	int i, len, descs = 0;
3024 
3025 	/* Check number of available descriptors */
3026 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, tso_count_descs(skb)) ||
3027 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu,
3028 					     tso_count_descs(skb)))
3029 		return 0;
3030 
3031 	tso_start(skb, &tso);
3032 	len = skb->len - hdr_sz;
3033 	while (len > 0) {
3034 		int left = min_t(int, skb_shinfo(skb)->gso_size, len);
3035 		char *hdr = txq_pcpu->tso_headers +
3036 			    txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
3037 
3038 		len -= left;
3039 		descs++;
3040 
3041 		tso_build_hdr(skb, hdr, &tso, left, len == 0);
3042 		mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
3043 
3044 		while (left > 0) {
3045 			int sz = min_t(int, tso.size, left);
3046 			left -= sz;
3047 			descs++;
3048 
3049 			if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
3050 					       txq_pcpu, sz, left, len == 0))
3051 				goto release;
3052 			tso_build_data(skb, &tso, sz);
3053 		}
3054 	}
3055 
3056 	return descs;
3057 
3058 release:
3059 	for (i = descs - 1; i >= 0; i--) {
3060 		struct mvpp2_tx_desc *tx_desc = txq->descs + i;
3061 		tx_desc_unmap_put(port, txq, tx_desc);
3062 	}
3063 	return 0;
3064 }
3065 
3066 /* Main tx processing */
3067 static netdev_tx_t mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
3068 {
3069 	struct mvpp2_port *port = netdev_priv(dev);
3070 	struct mvpp2_tx_queue *txq, *aggr_txq;
3071 	struct mvpp2_txq_pcpu *txq_pcpu;
3072 	struct mvpp2_tx_desc *tx_desc;
3073 	dma_addr_t buf_dma_addr;
3074 	unsigned long flags = 0;
3075 	unsigned int thread;
3076 	int frags = 0;
3077 	u16 txq_id;
3078 	u32 tx_cmd;
3079 
3080 	thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3081 
3082 	txq_id = skb_get_queue_mapping(skb);
3083 	txq = port->txqs[txq_id];
3084 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3085 	aggr_txq = &port->priv->aggr_txqs[thread];
3086 
3087 	if (test_bit(thread, &port->priv->lock_map))
3088 		spin_lock_irqsave(&port->tx_lock[thread], flags);
3089 
3090 	if (skb_is_gso(skb)) {
3091 		frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
3092 		goto out;
3093 	}
3094 	frags = skb_shinfo(skb)->nr_frags + 1;
3095 
3096 	/* Check number of available descriptors */
3097 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, frags) ||
3098 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, frags)) {
3099 		frags = 0;
3100 		goto out;
3101 	}
3102 
3103 	/* Get a descriptor for the first part of the packet */
3104 	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3105 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3106 	mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
3107 
3108 	buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
3109 				      skb_headlen(skb), DMA_TO_DEVICE);
3110 	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
3111 		mvpp2_txq_desc_put(txq);
3112 		frags = 0;
3113 		goto out;
3114 	}
3115 
3116 	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3117 
3118 	tx_cmd = mvpp2_skb_tx_csum(port, skb);
3119 
3120 	if (frags == 1) {
3121 		/* First and Last descriptor */
3122 		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
3123 		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
3124 		mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
3125 	} else {
3126 		/* First but not Last */
3127 		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
3128 		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
3129 		mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
3130 
3131 		/* Continue with other skb fragments */
3132 		if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
3133 			tx_desc_unmap_put(port, txq, tx_desc);
3134 			frags = 0;
3135 		}
3136 	}
3137 
3138 out:
3139 	if (frags > 0) {
3140 		struct mvpp2_pcpu_stats *stats = per_cpu_ptr(port->stats, thread);
3141 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
3142 
3143 		txq_pcpu->reserved_num -= frags;
3144 		txq_pcpu->count += frags;
3145 		aggr_txq->count += frags;
3146 
3147 		/* Enable transmit */
3148 		wmb();
3149 		mvpp2_aggr_txq_pend_desc_add(port, frags);
3150 
3151 		if (txq_pcpu->count >= txq_pcpu->stop_threshold)
3152 			netif_tx_stop_queue(nq);
3153 
3154 		u64_stats_update_begin(&stats->syncp);
3155 		stats->tx_packets++;
3156 		stats->tx_bytes += skb->len;
3157 		u64_stats_update_end(&stats->syncp);
3158 	} else {
3159 		dev->stats.tx_dropped++;
3160 		dev_kfree_skb_any(skb);
3161 	}
3162 
3163 	/* Finalize TX processing */
3164 	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
3165 		mvpp2_txq_done(port, txq, txq_pcpu);
3166 
3167 	/* Set the timer in case not all frags were processed */
3168 	if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
3169 	    txq_pcpu->count > 0) {
3170 		struct mvpp2_port_pcpu *port_pcpu = per_cpu_ptr(port->pcpu, thread);
3171 
3172 		mvpp2_timer_set(port_pcpu);
3173 	}
3174 
3175 	if (test_bit(thread, &port->priv->lock_map))
3176 		spin_unlock_irqrestore(&port->tx_lock[thread], flags);
3177 
3178 	return NETDEV_TX_OK;
3179 }
3180 
3181 static inline void mvpp2_cause_error(struct net_device *dev, int cause)
3182 {
3183 	if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
3184 		netdev_err(dev, "FCS error\n");
3185 	if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
3186 		netdev_err(dev, "rx fifo overrun error\n");
3187 	if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
3188 		netdev_err(dev, "tx fifo underrun error\n");
3189 }
3190 
3191 static int mvpp2_poll(struct napi_struct *napi, int budget)
3192 {
3193 	u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
3194 	int rx_done = 0;
3195 	struct mvpp2_port *port = netdev_priv(napi->dev);
3196 	struct mvpp2_queue_vector *qv;
3197 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3198 
3199 	qv = container_of(napi, struct mvpp2_queue_vector, napi);
3200 
3201 	/* Rx/Tx cause register
3202 	 *
3203 	 * Bits 0-15: each bit indicates received packets on the Rx queue
3204 	 * (bit 0 is for Rx queue 0).
3205 	 *
3206 	 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
3207 	 * (bit 16 is for Tx queue 0).
3208 	 *
3209 	 * Each CPU has its own Rx/Tx cause register
3210 	 */
3211 	cause_rx_tx = mvpp2_thread_read_relaxed(port->priv, qv->sw_thread_id,
3212 						MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
3213 
3214 	cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
3215 	if (cause_misc) {
3216 		mvpp2_cause_error(port->dev, cause_misc);
3217 
3218 		/* Clear the cause register */
3219 		mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
3220 		mvpp2_thread_write(port->priv, thread,
3221 				   MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
3222 				   cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
3223 	}
3224 
3225 	if (port->has_tx_irqs) {
3226 		cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
3227 		if (cause_tx) {
3228 			cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
3229 			mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
3230 		}
3231 	}
3232 
3233 	/* Process RX packets */
3234 	cause_rx = cause_rx_tx &
3235 		   MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
3236 	cause_rx <<= qv->first_rxq;
3237 	cause_rx |= qv->pending_cause_rx;
3238 	while (cause_rx && budget > 0) {
3239 		int count;
3240 		struct mvpp2_rx_queue *rxq;
3241 
3242 		rxq = mvpp2_get_rx_queue(port, cause_rx);
3243 		if (!rxq)
3244 			break;
3245 
3246 		count = mvpp2_rx(port, napi, budget, rxq);
3247 		rx_done += count;
3248 		budget -= count;
3249 		if (budget > 0) {
3250 			/* Clear the bit associated to this Rx queue
3251 			 * so that next iteration will continue from
3252 			 * the next Rx queue.
3253 			 */
3254 			cause_rx &= ~(1 << rxq->logic_rxq);
3255 		}
3256 	}
3257 
3258 	if (budget > 0) {
3259 		cause_rx = 0;
3260 		napi_complete_done(napi, rx_done);
3261 
3262 		mvpp2_qvec_interrupt_enable(qv);
3263 	}
3264 	qv->pending_cause_rx = cause_rx;
3265 	return rx_done;
3266 }
3267 
3268 static void mvpp22_mode_reconfigure(struct mvpp2_port *port)
3269 {
3270 	u32 ctrl3;
3271 
3272 	/* Set the GMAC & XLG MAC in reset */
3273 	mvpp2_mac_reset_assert(port);
3274 
3275 	/* Set the MPCS and XPCS in reset */
3276 	mvpp22_pcs_reset_assert(port);
3277 
3278 	/* comphy reconfiguration */
3279 	mvpp22_comphy_init(port);
3280 
3281 	/* gop reconfiguration */
3282 	mvpp22_gop_init(port);
3283 
3284 	mvpp22_pcs_reset_deassert(port);
3285 
3286 	/* Only GOP port 0 has an XLG MAC */
3287 	if (port->gop_id == 0) {
3288 		ctrl3 = readl(port->base + MVPP22_XLG_CTRL3_REG);
3289 		ctrl3 &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
3290 
3291 		if (mvpp2_is_xlg(port->phy_interface))
3292 			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
3293 		else
3294 			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
3295 
3296 		writel(ctrl3, port->base + MVPP22_XLG_CTRL3_REG);
3297 	}
3298 
3299 	if (port->gop_id == 0 && mvpp2_is_xlg(port->phy_interface))
3300 		mvpp2_xlg_max_rx_size_set(port);
3301 	else
3302 		mvpp2_gmac_max_rx_size_set(port);
3303 }
3304 
3305 /* Set hw internals when starting port */
3306 static void mvpp2_start_dev(struct mvpp2_port *port)
3307 {
3308 	int i;
3309 
3310 	mvpp2_txp_max_tx_size_set(port);
3311 
3312 	for (i = 0; i < port->nqvecs; i++)
3313 		napi_enable(&port->qvecs[i].napi);
3314 
3315 	/* Enable interrupts on all threads */
3316 	mvpp2_interrupts_enable(port);
3317 
3318 	if (port->priv->hw_version == MVPP22)
3319 		mvpp22_mode_reconfigure(port);
3320 
3321 	if (port->phylink) {
3322 		phylink_start(port->phylink);
3323 	} else {
3324 		/* Phylink isn't used as of now for ACPI, so the MAC has to be
3325 		 * configured manually when the interface is started. This will
3326 		 * be removed as soon as the phylink ACPI support lands in.
3327 		 */
3328 		struct phylink_link_state state = {
3329 			.interface = port->phy_interface,
3330 		};
3331 		mvpp2_mac_config(&port->phylink_config, MLO_AN_INBAND, &state);
3332 		mvpp2_mac_link_up(&port->phylink_config, MLO_AN_INBAND,
3333 				  port->phy_interface, NULL);
3334 	}
3335 
3336 	netif_tx_start_all_queues(port->dev);
3337 }
3338 
3339 /* Set hw internals when stopping port */
3340 static void mvpp2_stop_dev(struct mvpp2_port *port)
3341 {
3342 	int i;
3343 
3344 	/* Disable interrupts on all threads */
3345 	mvpp2_interrupts_disable(port);
3346 
3347 	for (i = 0; i < port->nqvecs; i++)
3348 		napi_disable(&port->qvecs[i].napi);
3349 
3350 	if (port->phylink)
3351 		phylink_stop(port->phylink);
3352 	phy_power_off(port->comphy);
3353 }
3354 
3355 static int mvpp2_check_ringparam_valid(struct net_device *dev,
3356 				       struct ethtool_ringparam *ring)
3357 {
3358 	u16 new_rx_pending = ring->rx_pending;
3359 	u16 new_tx_pending = ring->tx_pending;
3360 
3361 	if (ring->rx_pending == 0 || ring->tx_pending == 0)
3362 		return -EINVAL;
3363 
3364 	if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
3365 		new_rx_pending = MVPP2_MAX_RXD_MAX;
3366 	else if (!IS_ALIGNED(ring->rx_pending, 16))
3367 		new_rx_pending = ALIGN(ring->rx_pending, 16);
3368 
3369 	if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
3370 		new_tx_pending = MVPP2_MAX_TXD_MAX;
3371 	else if (!IS_ALIGNED(ring->tx_pending, 32))
3372 		new_tx_pending = ALIGN(ring->tx_pending, 32);
3373 
3374 	/* The Tx ring size cannot be smaller than the minimum number of
3375 	 * descriptors needed for TSO.
3376 	 */
3377 	if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
3378 		new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
3379 
3380 	if (ring->rx_pending != new_rx_pending) {
3381 		netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
3382 			    ring->rx_pending, new_rx_pending);
3383 		ring->rx_pending = new_rx_pending;
3384 	}
3385 
3386 	if (ring->tx_pending != new_tx_pending) {
3387 		netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
3388 			    ring->tx_pending, new_tx_pending);
3389 		ring->tx_pending = new_tx_pending;
3390 	}
3391 
3392 	return 0;
3393 }
3394 
3395 static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
3396 {
3397 	u32 mac_addr_l, mac_addr_m, mac_addr_h;
3398 
3399 	mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
3400 	mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
3401 	mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
3402 	addr[0] = (mac_addr_h >> 24) & 0xFF;
3403 	addr[1] = (mac_addr_h >> 16) & 0xFF;
3404 	addr[2] = (mac_addr_h >> 8) & 0xFF;
3405 	addr[3] = mac_addr_h & 0xFF;
3406 	addr[4] = mac_addr_m & 0xFF;
3407 	addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
3408 }
3409 
3410 static int mvpp2_irqs_init(struct mvpp2_port *port)
3411 {
3412 	int err, i;
3413 
3414 	for (i = 0; i < port->nqvecs; i++) {
3415 		struct mvpp2_queue_vector *qv = port->qvecs + i;
3416 
3417 		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
3418 			qv->mask = kzalloc(cpumask_size(), GFP_KERNEL);
3419 			if (!qv->mask) {
3420 				err = -ENOMEM;
3421 				goto err;
3422 			}
3423 
3424 			irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
3425 		}
3426 
3427 		err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
3428 		if (err)
3429 			goto err;
3430 
3431 		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
3432 			unsigned int cpu;
3433 
3434 			for_each_present_cpu(cpu) {
3435 				if (mvpp2_cpu_to_thread(port->priv, cpu) ==
3436 				    qv->sw_thread_id)
3437 					cpumask_set_cpu(cpu, qv->mask);
3438 			}
3439 
3440 			irq_set_affinity_hint(qv->irq, qv->mask);
3441 		}
3442 	}
3443 
3444 	return 0;
3445 err:
3446 	for (i = 0; i < port->nqvecs; i++) {
3447 		struct mvpp2_queue_vector *qv = port->qvecs + i;
3448 
3449 		irq_set_affinity_hint(qv->irq, NULL);
3450 		kfree(qv->mask);
3451 		qv->mask = NULL;
3452 		free_irq(qv->irq, qv);
3453 	}
3454 
3455 	return err;
3456 }
3457 
3458 static void mvpp2_irqs_deinit(struct mvpp2_port *port)
3459 {
3460 	int i;
3461 
3462 	for (i = 0; i < port->nqvecs; i++) {
3463 		struct mvpp2_queue_vector *qv = port->qvecs + i;
3464 
3465 		irq_set_affinity_hint(qv->irq, NULL);
3466 		kfree(qv->mask);
3467 		qv->mask = NULL;
3468 		irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
3469 		free_irq(qv->irq, qv);
3470 	}
3471 }
3472 
3473 static bool mvpp22_rss_is_supported(void)
3474 {
3475 	return queue_mode == MVPP2_QDIST_MULTI_MODE;
3476 }
3477 
3478 static int mvpp2_open(struct net_device *dev)
3479 {
3480 	struct mvpp2_port *port = netdev_priv(dev);
3481 	struct mvpp2 *priv = port->priv;
3482 	unsigned char mac_bcast[ETH_ALEN] = {
3483 			0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3484 	bool valid = false;
3485 	int err;
3486 
3487 	err = mvpp2_prs_mac_da_accept(port, mac_bcast, true);
3488 	if (err) {
3489 		netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
3490 		return err;
3491 	}
3492 	err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, true);
3493 	if (err) {
3494 		netdev_err(dev, "mvpp2_prs_mac_da_accept own addr failed\n");
3495 		return err;
3496 	}
3497 	err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
3498 	if (err) {
3499 		netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
3500 		return err;
3501 	}
3502 	err = mvpp2_prs_def_flow(port);
3503 	if (err) {
3504 		netdev_err(dev, "mvpp2_prs_def_flow failed\n");
3505 		return err;
3506 	}
3507 
3508 	/* Allocate the Rx/Tx queues */
3509 	err = mvpp2_setup_rxqs(port);
3510 	if (err) {
3511 		netdev_err(port->dev, "cannot allocate Rx queues\n");
3512 		return err;
3513 	}
3514 
3515 	err = mvpp2_setup_txqs(port);
3516 	if (err) {
3517 		netdev_err(port->dev, "cannot allocate Tx queues\n");
3518 		goto err_cleanup_rxqs;
3519 	}
3520 
3521 	err = mvpp2_irqs_init(port);
3522 	if (err) {
3523 		netdev_err(port->dev, "cannot init IRQs\n");
3524 		goto err_cleanup_txqs;
3525 	}
3526 
3527 	/* Phylink isn't supported yet in ACPI mode */
3528 	if (port->of_node) {
3529 		err = phylink_of_phy_connect(port->phylink, port->of_node, 0);
3530 		if (err) {
3531 			netdev_err(port->dev, "could not attach PHY (%d)\n",
3532 				   err);
3533 			goto err_free_irq;
3534 		}
3535 
3536 		valid = true;
3537 	}
3538 
3539 	if (priv->hw_version == MVPP22 && port->link_irq && !port->phylink) {
3540 		err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
3541 				  dev->name, port);
3542 		if (err) {
3543 			netdev_err(port->dev, "cannot request link IRQ %d\n",
3544 				   port->link_irq);
3545 			goto err_free_irq;
3546 		}
3547 
3548 		mvpp22_gop_setup_irq(port);
3549 
3550 		/* In default link is down */
3551 		netif_carrier_off(port->dev);
3552 
3553 		valid = true;
3554 	} else {
3555 		port->link_irq = 0;
3556 	}
3557 
3558 	if (!valid) {
3559 		netdev_err(port->dev,
3560 			   "invalid configuration: no dt or link IRQ");
3561 		goto err_free_irq;
3562 	}
3563 
3564 	/* Unmask interrupts on all CPUs */
3565 	on_each_cpu(mvpp2_interrupts_unmask, port, 1);
3566 	mvpp2_shared_interrupt_mask_unmask(port, false);
3567 
3568 	mvpp2_start_dev(port);
3569 
3570 	/* Start hardware statistics gathering */
3571 	queue_delayed_work(priv->stats_queue, &port->stats_work,
3572 			   MVPP2_MIB_COUNTERS_STATS_DELAY);
3573 
3574 	return 0;
3575 
3576 err_free_irq:
3577 	mvpp2_irqs_deinit(port);
3578 err_cleanup_txqs:
3579 	mvpp2_cleanup_txqs(port);
3580 err_cleanup_rxqs:
3581 	mvpp2_cleanup_rxqs(port);
3582 	return err;
3583 }
3584 
3585 static int mvpp2_stop(struct net_device *dev)
3586 {
3587 	struct mvpp2_port *port = netdev_priv(dev);
3588 	struct mvpp2_port_pcpu *port_pcpu;
3589 	unsigned int thread;
3590 
3591 	mvpp2_stop_dev(port);
3592 
3593 	/* Mask interrupts on all threads */
3594 	on_each_cpu(mvpp2_interrupts_mask, port, 1);
3595 	mvpp2_shared_interrupt_mask_unmask(port, true);
3596 
3597 	if (port->phylink)
3598 		phylink_disconnect_phy(port->phylink);
3599 	if (port->link_irq)
3600 		free_irq(port->link_irq, port);
3601 
3602 	mvpp2_irqs_deinit(port);
3603 	if (!port->has_tx_irqs) {
3604 		for (thread = 0; thread < port->priv->nthreads; thread++) {
3605 			port_pcpu = per_cpu_ptr(port->pcpu, thread);
3606 
3607 			hrtimer_cancel(&port_pcpu->tx_done_timer);
3608 			port_pcpu->timer_scheduled = false;
3609 			tasklet_kill(&port_pcpu->tx_done_tasklet);
3610 		}
3611 	}
3612 	mvpp2_cleanup_rxqs(port);
3613 	mvpp2_cleanup_txqs(port);
3614 
3615 	cancel_delayed_work_sync(&port->stats_work);
3616 
3617 	mvpp2_mac_reset_assert(port);
3618 	mvpp22_pcs_reset_assert(port);
3619 
3620 	return 0;
3621 }
3622 
3623 static int mvpp2_prs_mac_da_accept_list(struct mvpp2_port *port,
3624 					struct netdev_hw_addr_list *list)
3625 {
3626 	struct netdev_hw_addr *ha;
3627 	int ret;
3628 
3629 	netdev_hw_addr_list_for_each(ha, list) {
3630 		ret = mvpp2_prs_mac_da_accept(port, ha->addr, true);
3631 		if (ret)
3632 			return ret;
3633 	}
3634 
3635 	return 0;
3636 }
3637 
3638 static void mvpp2_set_rx_promisc(struct mvpp2_port *port, bool enable)
3639 {
3640 	if (!enable && (port->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3641 		mvpp2_prs_vid_enable_filtering(port);
3642 	else
3643 		mvpp2_prs_vid_disable_filtering(port);
3644 
3645 	mvpp2_prs_mac_promisc_set(port->priv, port->id,
3646 				  MVPP2_PRS_L2_UNI_CAST, enable);
3647 
3648 	mvpp2_prs_mac_promisc_set(port->priv, port->id,
3649 				  MVPP2_PRS_L2_MULTI_CAST, enable);
3650 }
3651 
3652 static void mvpp2_set_rx_mode(struct net_device *dev)
3653 {
3654 	struct mvpp2_port *port = netdev_priv(dev);
3655 
3656 	/* Clear the whole UC and MC list */
3657 	mvpp2_prs_mac_del_all(port);
3658 
3659 	if (dev->flags & IFF_PROMISC) {
3660 		mvpp2_set_rx_promisc(port, true);
3661 		return;
3662 	}
3663 
3664 	mvpp2_set_rx_promisc(port, false);
3665 
3666 	if (netdev_uc_count(dev) > MVPP2_PRS_MAC_UC_FILT_MAX ||
3667 	    mvpp2_prs_mac_da_accept_list(port, &dev->uc))
3668 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
3669 					  MVPP2_PRS_L2_UNI_CAST, true);
3670 
3671 	if (dev->flags & IFF_ALLMULTI) {
3672 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
3673 					  MVPP2_PRS_L2_MULTI_CAST, true);
3674 		return;
3675 	}
3676 
3677 	if (netdev_mc_count(dev) > MVPP2_PRS_MAC_MC_FILT_MAX ||
3678 	    mvpp2_prs_mac_da_accept_list(port, &dev->mc))
3679 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
3680 					  MVPP2_PRS_L2_MULTI_CAST, true);
3681 }
3682 
3683 static int mvpp2_set_mac_address(struct net_device *dev, void *p)
3684 {
3685 	const struct sockaddr *addr = p;
3686 	int err;
3687 
3688 	if (!is_valid_ether_addr(addr->sa_data))
3689 		return -EADDRNOTAVAIL;
3690 
3691 	err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
3692 	if (err) {
3693 		/* Reconfigure parser accept the original MAC address */
3694 		mvpp2_prs_update_mac_da(dev, dev->dev_addr);
3695 		netdev_err(dev, "failed to change MAC address\n");
3696 	}
3697 	return err;
3698 }
3699 
3700 static int mvpp2_change_mtu(struct net_device *dev, int mtu)
3701 {
3702 	struct mvpp2_port *port = netdev_priv(dev);
3703 	int err;
3704 
3705 	if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
3706 		netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
3707 			    ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
3708 		mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
3709 	}
3710 
3711 	if (!netif_running(dev)) {
3712 		err = mvpp2_bm_update_mtu(dev, mtu);
3713 		if (!err) {
3714 			port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
3715 			return 0;
3716 		}
3717 
3718 		/* Reconfigure BM to the original MTU */
3719 		err = mvpp2_bm_update_mtu(dev, dev->mtu);
3720 		if (err)
3721 			goto log_error;
3722 	}
3723 
3724 	mvpp2_stop_dev(port);
3725 
3726 	err = mvpp2_bm_update_mtu(dev, mtu);
3727 	if (!err) {
3728 		port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
3729 		goto out_start;
3730 	}
3731 
3732 	/* Reconfigure BM to the original MTU */
3733 	err = mvpp2_bm_update_mtu(dev, dev->mtu);
3734 	if (err)
3735 		goto log_error;
3736 
3737 out_start:
3738 	mvpp2_start_dev(port);
3739 	mvpp2_egress_enable(port);
3740 	mvpp2_ingress_enable(port);
3741 
3742 	return 0;
3743 log_error:
3744 	netdev_err(dev, "failed to change MTU\n");
3745 	return err;
3746 }
3747 
3748 static void
3749 mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
3750 {
3751 	struct mvpp2_port *port = netdev_priv(dev);
3752 	unsigned int start;
3753 	unsigned int cpu;
3754 
3755 	for_each_possible_cpu(cpu) {
3756 		struct mvpp2_pcpu_stats *cpu_stats;
3757 		u64 rx_packets;
3758 		u64 rx_bytes;
3759 		u64 tx_packets;
3760 		u64 tx_bytes;
3761 
3762 		cpu_stats = per_cpu_ptr(port->stats, cpu);
3763 		do {
3764 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
3765 			rx_packets = cpu_stats->rx_packets;
3766 			rx_bytes   = cpu_stats->rx_bytes;
3767 			tx_packets = cpu_stats->tx_packets;
3768 			tx_bytes   = cpu_stats->tx_bytes;
3769 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
3770 
3771 		stats->rx_packets += rx_packets;
3772 		stats->rx_bytes   += rx_bytes;
3773 		stats->tx_packets += tx_packets;
3774 		stats->tx_bytes   += tx_bytes;
3775 	}
3776 
3777 	stats->rx_errors	= dev->stats.rx_errors;
3778 	stats->rx_dropped	= dev->stats.rx_dropped;
3779 	stats->tx_dropped	= dev->stats.tx_dropped;
3780 }
3781 
3782 static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3783 {
3784 	struct mvpp2_port *port = netdev_priv(dev);
3785 
3786 	if (!port->phylink)
3787 		return -ENOTSUPP;
3788 
3789 	return phylink_mii_ioctl(port->phylink, ifr, cmd);
3790 }
3791 
3792 static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
3793 {
3794 	struct mvpp2_port *port = netdev_priv(dev);
3795 	int ret;
3796 
3797 	ret = mvpp2_prs_vid_entry_add(port, vid);
3798 	if (ret)
3799 		netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
3800 			   MVPP2_PRS_VLAN_FILT_MAX - 1);
3801 	return ret;
3802 }
3803 
3804 static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
3805 {
3806 	struct mvpp2_port *port = netdev_priv(dev);
3807 
3808 	mvpp2_prs_vid_entry_remove(port, vid);
3809 	return 0;
3810 }
3811 
3812 static int mvpp2_set_features(struct net_device *dev,
3813 			      netdev_features_t features)
3814 {
3815 	netdev_features_t changed = dev->features ^ features;
3816 	struct mvpp2_port *port = netdev_priv(dev);
3817 
3818 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
3819 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
3820 			mvpp2_prs_vid_enable_filtering(port);
3821 		} else {
3822 			/* Invalidate all registered VID filters for this
3823 			 * port
3824 			 */
3825 			mvpp2_prs_vid_remove_all(port);
3826 
3827 			mvpp2_prs_vid_disable_filtering(port);
3828 		}
3829 	}
3830 
3831 	if (changed & NETIF_F_RXHASH) {
3832 		if (features & NETIF_F_RXHASH)
3833 			mvpp22_port_rss_enable(port);
3834 		else
3835 			mvpp22_port_rss_disable(port);
3836 	}
3837 
3838 	return 0;
3839 }
3840 
3841 /* Ethtool methods */
3842 
3843 static int mvpp2_ethtool_nway_reset(struct net_device *dev)
3844 {
3845 	struct mvpp2_port *port = netdev_priv(dev);
3846 
3847 	if (!port->phylink)
3848 		return -ENOTSUPP;
3849 
3850 	return phylink_ethtool_nway_reset(port->phylink);
3851 }
3852 
3853 /* Set interrupt coalescing for ethtools */
3854 static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
3855 				      struct ethtool_coalesce *c)
3856 {
3857 	struct mvpp2_port *port = netdev_priv(dev);
3858 	int queue;
3859 
3860 	for (queue = 0; queue < port->nrxqs; queue++) {
3861 		struct mvpp2_rx_queue *rxq = port->rxqs[queue];
3862 
3863 		rxq->time_coal = c->rx_coalesce_usecs;
3864 		rxq->pkts_coal = c->rx_max_coalesced_frames;
3865 		mvpp2_rx_pkts_coal_set(port, rxq);
3866 		mvpp2_rx_time_coal_set(port, rxq);
3867 	}
3868 
3869 	if (port->has_tx_irqs) {
3870 		port->tx_time_coal = c->tx_coalesce_usecs;
3871 		mvpp2_tx_time_coal_set(port);
3872 	}
3873 
3874 	for (queue = 0; queue < port->ntxqs; queue++) {
3875 		struct mvpp2_tx_queue *txq = port->txqs[queue];
3876 
3877 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
3878 
3879 		if (port->has_tx_irqs)
3880 			mvpp2_tx_pkts_coal_set(port, txq);
3881 	}
3882 
3883 	return 0;
3884 }
3885 
3886 /* get coalescing for ethtools */
3887 static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
3888 				      struct ethtool_coalesce *c)
3889 {
3890 	struct mvpp2_port *port = netdev_priv(dev);
3891 
3892 	c->rx_coalesce_usecs       = port->rxqs[0]->time_coal;
3893 	c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
3894 	c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
3895 	c->tx_coalesce_usecs       = port->tx_time_coal;
3896 	return 0;
3897 }
3898 
3899 static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
3900 				      struct ethtool_drvinfo *drvinfo)
3901 {
3902 	strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
3903 		sizeof(drvinfo->driver));
3904 	strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
3905 		sizeof(drvinfo->version));
3906 	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3907 		sizeof(drvinfo->bus_info));
3908 }
3909 
3910 static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
3911 					struct ethtool_ringparam *ring)
3912 {
3913 	struct mvpp2_port *port = netdev_priv(dev);
3914 
3915 	ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
3916 	ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
3917 	ring->rx_pending = port->rx_ring_size;
3918 	ring->tx_pending = port->tx_ring_size;
3919 }
3920 
3921 static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
3922 				       struct ethtool_ringparam *ring)
3923 {
3924 	struct mvpp2_port *port = netdev_priv(dev);
3925 	u16 prev_rx_ring_size = port->rx_ring_size;
3926 	u16 prev_tx_ring_size = port->tx_ring_size;
3927 	int err;
3928 
3929 	err = mvpp2_check_ringparam_valid(dev, ring);
3930 	if (err)
3931 		return err;
3932 
3933 	if (!netif_running(dev)) {
3934 		port->rx_ring_size = ring->rx_pending;
3935 		port->tx_ring_size = ring->tx_pending;
3936 		return 0;
3937 	}
3938 
3939 	/* The interface is running, so we have to force a
3940 	 * reallocation of the queues
3941 	 */
3942 	mvpp2_stop_dev(port);
3943 	mvpp2_cleanup_rxqs(port);
3944 	mvpp2_cleanup_txqs(port);
3945 
3946 	port->rx_ring_size = ring->rx_pending;
3947 	port->tx_ring_size = ring->tx_pending;
3948 
3949 	err = mvpp2_setup_rxqs(port);
3950 	if (err) {
3951 		/* Reallocate Rx queues with the original ring size */
3952 		port->rx_ring_size = prev_rx_ring_size;
3953 		ring->rx_pending = prev_rx_ring_size;
3954 		err = mvpp2_setup_rxqs(port);
3955 		if (err)
3956 			goto err_out;
3957 	}
3958 	err = mvpp2_setup_txqs(port);
3959 	if (err) {
3960 		/* Reallocate Tx queues with the original ring size */
3961 		port->tx_ring_size = prev_tx_ring_size;
3962 		ring->tx_pending = prev_tx_ring_size;
3963 		err = mvpp2_setup_txqs(port);
3964 		if (err)
3965 			goto err_clean_rxqs;
3966 	}
3967 
3968 	mvpp2_start_dev(port);
3969 	mvpp2_egress_enable(port);
3970 	mvpp2_ingress_enable(port);
3971 
3972 	return 0;
3973 
3974 err_clean_rxqs:
3975 	mvpp2_cleanup_rxqs(port);
3976 err_out:
3977 	netdev_err(dev, "failed to change ring parameters");
3978 	return err;
3979 }
3980 
3981 static void mvpp2_ethtool_get_pause_param(struct net_device *dev,
3982 					  struct ethtool_pauseparam *pause)
3983 {
3984 	struct mvpp2_port *port = netdev_priv(dev);
3985 
3986 	if (!port->phylink)
3987 		return;
3988 
3989 	phylink_ethtool_get_pauseparam(port->phylink, pause);
3990 }
3991 
3992 static int mvpp2_ethtool_set_pause_param(struct net_device *dev,
3993 					 struct ethtool_pauseparam *pause)
3994 {
3995 	struct mvpp2_port *port = netdev_priv(dev);
3996 
3997 	if (!port->phylink)
3998 		return -ENOTSUPP;
3999 
4000 	return phylink_ethtool_set_pauseparam(port->phylink, pause);
4001 }
4002 
4003 static int mvpp2_ethtool_get_link_ksettings(struct net_device *dev,
4004 					    struct ethtool_link_ksettings *cmd)
4005 {
4006 	struct mvpp2_port *port = netdev_priv(dev);
4007 
4008 	if (!port->phylink)
4009 		return -ENOTSUPP;
4010 
4011 	return phylink_ethtool_ksettings_get(port->phylink, cmd);
4012 }
4013 
4014 static int mvpp2_ethtool_set_link_ksettings(struct net_device *dev,
4015 					    const struct ethtool_link_ksettings *cmd)
4016 {
4017 	struct mvpp2_port *port = netdev_priv(dev);
4018 
4019 	if (!port->phylink)
4020 		return -ENOTSUPP;
4021 
4022 	return phylink_ethtool_ksettings_set(port->phylink, cmd);
4023 }
4024 
4025 static int mvpp2_ethtool_get_rxnfc(struct net_device *dev,
4026 				   struct ethtool_rxnfc *info, u32 *rules)
4027 {
4028 	struct mvpp2_port *port = netdev_priv(dev);
4029 	int ret = 0, i, loc = 0;
4030 
4031 	if (!mvpp22_rss_is_supported())
4032 		return -EOPNOTSUPP;
4033 
4034 	switch (info->cmd) {
4035 	case ETHTOOL_GRXFH:
4036 		ret = mvpp2_ethtool_rxfh_get(port, info);
4037 		break;
4038 	case ETHTOOL_GRXRINGS:
4039 		info->data = port->nrxqs;
4040 		break;
4041 	case ETHTOOL_GRXCLSRLCNT:
4042 		info->rule_cnt = port->n_rfs_rules;
4043 		break;
4044 	case ETHTOOL_GRXCLSRULE:
4045 		ret = mvpp2_ethtool_cls_rule_get(port, info);
4046 		break;
4047 	case ETHTOOL_GRXCLSRLALL:
4048 		for (i = 0; i < MVPP2_N_RFS_ENTRIES_PER_FLOW; i++) {
4049 			if (port->rfs_rules[i])
4050 				rules[loc++] = i;
4051 		}
4052 		break;
4053 	default:
4054 		return -ENOTSUPP;
4055 	}
4056 
4057 	return ret;
4058 }
4059 
4060 static int mvpp2_ethtool_set_rxnfc(struct net_device *dev,
4061 				   struct ethtool_rxnfc *info)
4062 {
4063 	struct mvpp2_port *port = netdev_priv(dev);
4064 	int ret = 0;
4065 
4066 	if (!mvpp22_rss_is_supported())
4067 		return -EOPNOTSUPP;
4068 
4069 	switch (info->cmd) {
4070 	case ETHTOOL_SRXFH:
4071 		ret = mvpp2_ethtool_rxfh_set(port, info);
4072 		break;
4073 	case ETHTOOL_SRXCLSRLINS:
4074 		ret = mvpp2_ethtool_cls_rule_ins(port, info);
4075 		break;
4076 	case ETHTOOL_SRXCLSRLDEL:
4077 		ret = mvpp2_ethtool_cls_rule_del(port, info);
4078 		break;
4079 	default:
4080 		return -EOPNOTSUPP;
4081 	}
4082 	return ret;
4083 }
4084 
4085 static u32 mvpp2_ethtool_get_rxfh_indir_size(struct net_device *dev)
4086 {
4087 	return mvpp22_rss_is_supported() ? MVPP22_RSS_TABLE_ENTRIES : 0;
4088 }
4089 
4090 static int mvpp2_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4091 				  u8 *hfunc)
4092 {
4093 	struct mvpp2_port *port = netdev_priv(dev);
4094 	int ret = 0;
4095 
4096 	if (!mvpp22_rss_is_supported())
4097 		return -EOPNOTSUPP;
4098 
4099 	if (indir)
4100 		ret = mvpp22_port_rss_ctx_indir_get(port, 0, indir);
4101 
4102 	if (hfunc)
4103 		*hfunc = ETH_RSS_HASH_CRC32;
4104 
4105 	return ret;
4106 }
4107 
4108 static int mvpp2_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4109 				  const u8 *key, const u8 hfunc)
4110 {
4111 	struct mvpp2_port *port = netdev_priv(dev);
4112 	int ret = 0;
4113 
4114 	if (!mvpp22_rss_is_supported())
4115 		return -EOPNOTSUPP;
4116 
4117 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
4118 		return -EOPNOTSUPP;
4119 
4120 	if (key)
4121 		return -EOPNOTSUPP;
4122 
4123 	if (indir)
4124 		ret = mvpp22_port_rss_ctx_indir_set(port, 0, indir);
4125 
4126 	return ret;
4127 }
4128 
4129 static int mvpp2_ethtool_get_rxfh_context(struct net_device *dev, u32 *indir,
4130 					  u8 *key, u8 *hfunc, u32 rss_context)
4131 {
4132 	struct mvpp2_port *port = netdev_priv(dev);
4133 	int ret = 0;
4134 
4135 	if (!mvpp22_rss_is_supported())
4136 		return -EOPNOTSUPP;
4137 
4138 	if (hfunc)
4139 		*hfunc = ETH_RSS_HASH_CRC32;
4140 
4141 	if (indir)
4142 		ret = mvpp22_port_rss_ctx_indir_get(port, rss_context, indir);
4143 
4144 	return ret;
4145 }
4146 
4147 static int mvpp2_ethtool_set_rxfh_context(struct net_device *dev,
4148 					  const u32 *indir, const u8 *key,
4149 					  const u8 hfunc, u32 *rss_context,
4150 					  bool delete)
4151 {
4152 	struct mvpp2_port *port = netdev_priv(dev);
4153 	int ret;
4154 
4155 	if (!mvpp22_rss_is_supported())
4156 		return -EOPNOTSUPP;
4157 
4158 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
4159 		return -EOPNOTSUPP;
4160 
4161 	if (key)
4162 		return -EOPNOTSUPP;
4163 
4164 	if (delete)
4165 		return mvpp22_port_rss_ctx_delete(port, *rss_context);
4166 
4167 	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
4168 		ret = mvpp22_port_rss_ctx_create(port, rss_context);
4169 		if (ret)
4170 			return ret;
4171 	}
4172 
4173 	return mvpp22_port_rss_ctx_indir_set(port, *rss_context, indir);
4174 }
4175 /* Device ops */
4176 
4177 static const struct net_device_ops mvpp2_netdev_ops = {
4178 	.ndo_open		= mvpp2_open,
4179 	.ndo_stop		= mvpp2_stop,
4180 	.ndo_start_xmit		= mvpp2_tx,
4181 	.ndo_set_rx_mode	= mvpp2_set_rx_mode,
4182 	.ndo_set_mac_address	= mvpp2_set_mac_address,
4183 	.ndo_change_mtu		= mvpp2_change_mtu,
4184 	.ndo_get_stats64	= mvpp2_get_stats64,
4185 	.ndo_do_ioctl		= mvpp2_ioctl,
4186 	.ndo_vlan_rx_add_vid	= mvpp2_vlan_rx_add_vid,
4187 	.ndo_vlan_rx_kill_vid	= mvpp2_vlan_rx_kill_vid,
4188 	.ndo_set_features	= mvpp2_set_features,
4189 };
4190 
4191 static const struct ethtool_ops mvpp2_eth_tool_ops = {
4192 	.nway_reset		= mvpp2_ethtool_nway_reset,
4193 	.get_link		= ethtool_op_get_link,
4194 	.set_coalesce		= mvpp2_ethtool_set_coalesce,
4195 	.get_coalesce		= mvpp2_ethtool_get_coalesce,
4196 	.get_drvinfo		= mvpp2_ethtool_get_drvinfo,
4197 	.get_ringparam		= mvpp2_ethtool_get_ringparam,
4198 	.set_ringparam		= mvpp2_ethtool_set_ringparam,
4199 	.get_strings		= mvpp2_ethtool_get_strings,
4200 	.get_ethtool_stats	= mvpp2_ethtool_get_stats,
4201 	.get_sset_count		= mvpp2_ethtool_get_sset_count,
4202 	.get_pauseparam		= mvpp2_ethtool_get_pause_param,
4203 	.set_pauseparam		= mvpp2_ethtool_set_pause_param,
4204 	.get_link_ksettings	= mvpp2_ethtool_get_link_ksettings,
4205 	.set_link_ksettings	= mvpp2_ethtool_set_link_ksettings,
4206 	.get_rxnfc		= mvpp2_ethtool_get_rxnfc,
4207 	.set_rxnfc		= mvpp2_ethtool_set_rxnfc,
4208 	.get_rxfh_indir_size	= mvpp2_ethtool_get_rxfh_indir_size,
4209 	.get_rxfh		= mvpp2_ethtool_get_rxfh,
4210 	.set_rxfh		= mvpp2_ethtool_set_rxfh,
4211 	.get_rxfh_context	= mvpp2_ethtool_get_rxfh_context,
4212 	.set_rxfh_context	= mvpp2_ethtool_set_rxfh_context,
4213 };
4214 
4215 /* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
4216  * had a single IRQ defined per-port.
4217  */
4218 static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
4219 					   struct device_node *port_node)
4220 {
4221 	struct mvpp2_queue_vector *v = &port->qvecs[0];
4222 
4223 	v->first_rxq = 0;
4224 	v->nrxqs = port->nrxqs;
4225 	v->type = MVPP2_QUEUE_VECTOR_SHARED;
4226 	v->sw_thread_id = 0;
4227 	v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
4228 	v->port = port;
4229 	v->irq = irq_of_parse_and_map(port_node, 0);
4230 	if (v->irq <= 0)
4231 		return -EINVAL;
4232 	netif_napi_add(port->dev, &v->napi, mvpp2_poll,
4233 		       NAPI_POLL_WEIGHT);
4234 
4235 	port->nqvecs = 1;
4236 
4237 	return 0;
4238 }
4239 
4240 static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
4241 					  struct device_node *port_node)
4242 {
4243 	struct mvpp2 *priv = port->priv;
4244 	struct mvpp2_queue_vector *v;
4245 	int i, ret;
4246 
4247 	switch (queue_mode) {
4248 	case MVPP2_QDIST_SINGLE_MODE:
4249 		port->nqvecs = priv->nthreads + 1;
4250 		break;
4251 	case MVPP2_QDIST_MULTI_MODE:
4252 		port->nqvecs = priv->nthreads;
4253 		break;
4254 	}
4255 
4256 	for (i = 0; i < port->nqvecs; i++) {
4257 		char irqname[16];
4258 
4259 		v = port->qvecs + i;
4260 
4261 		v->port = port;
4262 		v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
4263 		v->sw_thread_id = i;
4264 		v->sw_thread_mask = BIT(i);
4265 
4266 		if (port->flags & MVPP2_F_DT_COMPAT)
4267 			snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
4268 		else
4269 			snprintf(irqname, sizeof(irqname), "hif%d", i);
4270 
4271 		if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
4272 			v->first_rxq = i;
4273 			v->nrxqs = 1;
4274 		} else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
4275 			   i == (port->nqvecs - 1)) {
4276 			v->first_rxq = 0;
4277 			v->nrxqs = port->nrxqs;
4278 			v->type = MVPP2_QUEUE_VECTOR_SHARED;
4279 
4280 			if (port->flags & MVPP2_F_DT_COMPAT)
4281 				strncpy(irqname, "rx-shared", sizeof(irqname));
4282 		}
4283 
4284 		if (port_node)
4285 			v->irq = of_irq_get_byname(port_node, irqname);
4286 		else
4287 			v->irq = fwnode_irq_get(port->fwnode, i);
4288 		if (v->irq <= 0) {
4289 			ret = -EINVAL;
4290 			goto err;
4291 		}
4292 
4293 		netif_napi_add(port->dev, &v->napi, mvpp2_poll,
4294 			       NAPI_POLL_WEIGHT);
4295 	}
4296 
4297 	return 0;
4298 
4299 err:
4300 	for (i = 0; i < port->nqvecs; i++)
4301 		irq_dispose_mapping(port->qvecs[i].irq);
4302 	return ret;
4303 }
4304 
4305 static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
4306 				    struct device_node *port_node)
4307 {
4308 	if (port->has_tx_irqs)
4309 		return mvpp2_multi_queue_vectors_init(port, port_node);
4310 	else
4311 		return mvpp2_simple_queue_vectors_init(port, port_node);
4312 }
4313 
4314 static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
4315 {
4316 	int i;
4317 
4318 	for (i = 0; i < port->nqvecs; i++)
4319 		irq_dispose_mapping(port->qvecs[i].irq);
4320 }
4321 
4322 /* Configure Rx queue group interrupt for this port */
4323 static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
4324 {
4325 	struct mvpp2 *priv = port->priv;
4326 	u32 val;
4327 	int i;
4328 
4329 	if (priv->hw_version == MVPP21) {
4330 		mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
4331 			    port->nrxqs);
4332 		return;
4333 	}
4334 
4335 	/* Handle the more complicated PPv2.2 case */
4336 	for (i = 0; i < port->nqvecs; i++) {
4337 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4338 
4339 		if (!qv->nrxqs)
4340 			continue;
4341 
4342 		val = qv->sw_thread_id;
4343 		val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
4344 		mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
4345 
4346 		val = qv->first_rxq;
4347 		val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
4348 		mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
4349 	}
4350 }
4351 
4352 /* Initialize port HW */
4353 static int mvpp2_port_init(struct mvpp2_port *port)
4354 {
4355 	struct device *dev = port->dev->dev.parent;
4356 	struct mvpp2 *priv = port->priv;
4357 	struct mvpp2_txq_pcpu *txq_pcpu;
4358 	unsigned int thread;
4359 	int queue, err;
4360 
4361 	/* Checks for hardware constraints */
4362 	if (port->first_rxq + port->nrxqs >
4363 	    MVPP2_MAX_PORTS * priv->max_port_rxqs)
4364 		return -EINVAL;
4365 
4366 	if (port->nrxqs > priv->max_port_rxqs || port->ntxqs > MVPP2_MAX_TXQ)
4367 		return -EINVAL;
4368 
4369 	/* Disable port */
4370 	mvpp2_egress_disable(port);
4371 	mvpp2_port_disable(port);
4372 
4373 	port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
4374 
4375 	port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
4376 				  GFP_KERNEL);
4377 	if (!port->txqs)
4378 		return -ENOMEM;
4379 
4380 	/* Associate physical Tx queues to this port and initialize.
4381 	 * The mapping is predefined.
4382 	 */
4383 	for (queue = 0; queue < port->ntxqs; queue++) {
4384 		int queue_phy_id = mvpp2_txq_phys(port->id, queue);
4385 		struct mvpp2_tx_queue *txq;
4386 
4387 		txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
4388 		if (!txq) {
4389 			err = -ENOMEM;
4390 			goto err_free_percpu;
4391 		}
4392 
4393 		txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
4394 		if (!txq->pcpu) {
4395 			err = -ENOMEM;
4396 			goto err_free_percpu;
4397 		}
4398 
4399 		txq->id = queue_phy_id;
4400 		txq->log_id = queue;
4401 		txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
4402 		for (thread = 0; thread < priv->nthreads; thread++) {
4403 			txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4404 			txq_pcpu->thread = thread;
4405 		}
4406 
4407 		port->txqs[queue] = txq;
4408 	}
4409 
4410 	port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
4411 				  GFP_KERNEL);
4412 	if (!port->rxqs) {
4413 		err = -ENOMEM;
4414 		goto err_free_percpu;
4415 	}
4416 
4417 	/* Allocate and initialize Rx queue for this port */
4418 	for (queue = 0; queue < port->nrxqs; queue++) {
4419 		struct mvpp2_rx_queue *rxq;
4420 
4421 		/* Map physical Rx queue to port's logical Rx queue */
4422 		rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
4423 		if (!rxq) {
4424 			err = -ENOMEM;
4425 			goto err_free_percpu;
4426 		}
4427 		/* Map this Rx queue to a physical queue */
4428 		rxq->id = port->first_rxq + queue;
4429 		rxq->port = port->id;
4430 		rxq->logic_rxq = queue;
4431 
4432 		port->rxqs[queue] = rxq;
4433 	}
4434 
4435 	mvpp2_rx_irqs_setup(port);
4436 
4437 	/* Create Rx descriptor rings */
4438 	for (queue = 0; queue < port->nrxqs; queue++) {
4439 		struct mvpp2_rx_queue *rxq = port->rxqs[queue];
4440 
4441 		rxq->size = port->rx_ring_size;
4442 		rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
4443 		rxq->time_coal = MVPP2_RX_COAL_USEC;
4444 	}
4445 
4446 	mvpp2_ingress_disable(port);
4447 
4448 	/* Port default configuration */
4449 	mvpp2_defaults_set(port);
4450 
4451 	/* Port's classifier configuration */
4452 	mvpp2_cls_oversize_rxq_set(port);
4453 	mvpp2_cls_port_config(port);
4454 
4455 	if (mvpp22_rss_is_supported())
4456 		mvpp22_port_rss_init(port);
4457 
4458 	/* Provide an initial Rx packet size */
4459 	port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
4460 
4461 	/* Initialize pools for swf */
4462 	err = mvpp2_swf_bm_pool_init(port);
4463 	if (err)
4464 		goto err_free_percpu;
4465 
4466 	/* Clear all port stats */
4467 	mvpp2_read_stats(port);
4468 	memset(port->ethtool_stats, 0,
4469 	       MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs) * sizeof(u64));
4470 
4471 	return 0;
4472 
4473 err_free_percpu:
4474 	for (queue = 0; queue < port->ntxqs; queue++) {
4475 		if (!port->txqs[queue])
4476 			continue;
4477 		free_percpu(port->txqs[queue]->pcpu);
4478 	}
4479 	return err;
4480 }
4481 
4482 static bool mvpp22_port_has_legacy_tx_irqs(struct device_node *port_node,
4483 					   unsigned long *flags)
4484 {
4485 	char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1", "tx-cpu2",
4486 			  "tx-cpu3" };
4487 	int i;
4488 
4489 	for (i = 0; i < 5; i++)
4490 		if (of_property_match_string(port_node, "interrupt-names",
4491 					     irqs[i]) < 0)
4492 			return false;
4493 
4494 	*flags |= MVPP2_F_DT_COMPAT;
4495 	return true;
4496 }
4497 
4498 /* Checks if the port dt description has the required Tx interrupts:
4499  * - PPv2.1: there are no such interrupts.
4500  * - PPv2.2:
4501  *   - The old DTs have: "rx-shared", "tx-cpuX" with X in [0...3]
4502  *   - The new ones have: "hifX" with X in [0..8]
4503  *
4504  * All those variants are supported to keep the backward compatibility.
4505  */
4506 static bool mvpp2_port_has_irqs(struct mvpp2 *priv,
4507 				struct device_node *port_node,
4508 				unsigned long *flags)
4509 {
4510 	char name[5];
4511 	int i;
4512 
4513 	/* ACPI */
4514 	if (!port_node)
4515 		return true;
4516 
4517 	if (priv->hw_version == MVPP21)
4518 		return false;
4519 
4520 	if (mvpp22_port_has_legacy_tx_irqs(port_node, flags))
4521 		return true;
4522 
4523 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
4524 		snprintf(name, 5, "hif%d", i);
4525 		if (of_property_match_string(port_node, "interrupt-names",
4526 					     name) < 0)
4527 			return false;
4528 	}
4529 
4530 	return true;
4531 }
4532 
4533 static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
4534 				     struct fwnode_handle *fwnode,
4535 				     char **mac_from)
4536 {
4537 	struct mvpp2_port *port = netdev_priv(dev);
4538 	char hw_mac_addr[ETH_ALEN] = {0};
4539 	char fw_mac_addr[ETH_ALEN];
4540 
4541 	if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
4542 		*mac_from = "firmware node";
4543 		ether_addr_copy(dev->dev_addr, fw_mac_addr);
4544 		return;
4545 	}
4546 
4547 	if (priv->hw_version == MVPP21) {
4548 		mvpp21_get_mac_address(port, hw_mac_addr);
4549 		if (is_valid_ether_addr(hw_mac_addr)) {
4550 			*mac_from = "hardware";
4551 			ether_addr_copy(dev->dev_addr, hw_mac_addr);
4552 			return;
4553 		}
4554 	}
4555 
4556 	*mac_from = "random";
4557 	eth_hw_addr_random(dev);
4558 }
4559 
4560 static void mvpp2_phylink_validate(struct phylink_config *config,
4561 				   unsigned long *supported,
4562 				   struct phylink_link_state *state)
4563 {
4564 	struct mvpp2_port *port = container_of(config, struct mvpp2_port,
4565 					       phylink_config);
4566 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
4567 
4568 	/* Invalid combinations */
4569 	switch (state->interface) {
4570 	case PHY_INTERFACE_MODE_10GKR:
4571 	case PHY_INTERFACE_MODE_XAUI:
4572 		if (port->gop_id != 0)
4573 			goto empty_set;
4574 		break;
4575 	case PHY_INTERFACE_MODE_RGMII:
4576 	case PHY_INTERFACE_MODE_RGMII_ID:
4577 	case PHY_INTERFACE_MODE_RGMII_RXID:
4578 	case PHY_INTERFACE_MODE_RGMII_TXID:
4579 		if (port->priv->hw_version == MVPP22 && port->gop_id == 0)
4580 			goto empty_set;
4581 		break;
4582 	default:
4583 		break;
4584 	}
4585 
4586 	phylink_set(mask, Autoneg);
4587 	phylink_set_port_modes(mask);
4588 	phylink_set(mask, Pause);
4589 	phylink_set(mask, Asym_Pause);
4590 
4591 	switch (state->interface) {
4592 	case PHY_INTERFACE_MODE_10GKR:
4593 	case PHY_INTERFACE_MODE_XAUI:
4594 	case PHY_INTERFACE_MODE_NA:
4595 		if (port->gop_id == 0) {
4596 			phylink_set(mask, 10000baseT_Full);
4597 			phylink_set(mask, 10000baseCR_Full);
4598 			phylink_set(mask, 10000baseSR_Full);
4599 			phylink_set(mask, 10000baseLR_Full);
4600 			phylink_set(mask, 10000baseLRM_Full);
4601 			phylink_set(mask, 10000baseER_Full);
4602 			phylink_set(mask, 10000baseKR_Full);
4603 		}
4604 		/* Fall-through */
4605 	case PHY_INTERFACE_MODE_RGMII:
4606 	case PHY_INTERFACE_MODE_RGMII_ID:
4607 	case PHY_INTERFACE_MODE_RGMII_RXID:
4608 	case PHY_INTERFACE_MODE_RGMII_TXID:
4609 	case PHY_INTERFACE_MODE_SGMII:
4610 		phylink_set(mask, 10baseT_Half);
4611 		phylink_set(mask, 10baseT_Full);
4612 		phylink_set(mask, 100baseT_Half);
4613 		phylink_set(mask, 100baseT_Full);
4614 		/* Fall-through */
4615 	case PHY_INTERFACE_MODE_1000BASEX:
4616 	case PHY_INTERFACE_MODE_2500BASEX:
4617 		phylink_set(mask, 1000baseT_Full);
4618 		phylink_set(mask, 1000baseX_Full);
4619 		phylink_set(mask, 2500baseT_Full);
4620 		phylink_set(mask, 2500baseX_Full);
4621 		break;
4622 	default:
4623 		goto empty_set;
4624 	}
4625 
4626 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
4627 	bitmap_and(state->advertising, state->advertising, mask,
4628 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
4629 	return;
4630 
4631 empty_set:
4632 	bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
4633 }
4634 
4635 static void mvpp22_xlg_link_state(struct mvpp2_port *port,
4636 				  struct phylink_link_state *state)
4637 {
4638 	u32 val;
4639 
4640 	state->speed = SPEED_10000;
4641 	state->duplex = 1;
4642 	state->an_complete = 1;
4643 
4644 	val = readl(port->base + MVPP22_XLG_STATUS);
4645 	state->link = !!(val & MVPP22_XLG_STATUS_LINK_UP);
4646 
4647 	state->pause = 0;
4648 	val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4649 	if (val & MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN)
4650 		state->pause |= MLO_PAUSE_TX;
4651 	if (val & MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN)
4652 		state->pause |= MLO_PAUSE_RX;
4653 }
4654 
4655 static void mvpp2_gmac_link_state(struct mvpp2_port *port,
4656 				  struct phylink_link_state *state)
4657 {
4658 	u32 val;
4659 
4660 	val = readl(port->base + MVPP2_GMAC_STATUS0);
4661 
4662 	state->an_complete = !!(val & MVPP2_GMAC_STATUS0_AN_COMPLETE);
4663 	state->link = !!(val & MVPP2_GMAC_STATUS0_LINK_UP);
4664 	state->duplex = !!(val & MVPP2_GMAC_STATUS0_FULL_DUPLEX);
4665 
4666 	switch (port->phy_interface) {
4667 	case PHY_INTERFACE_MODE_1000BASEX:
4668 		state->speed = SPEED_1000;
4669 		break;
4670 	case PHY_INTERFACE_MODE_2500BASEX:
4671 		state->speed = SPEED_2500;
4672 		break;
4673 	default:
4674 		if (val & MVPP2_GMAC_STATUS0_GMII_SPEED)
4675 			state->speed = SPEED_1000;
4676 		else if (val & MVPP2_GMAC_STATUS0_MII_SPEED)
4677 			state->speed = SPEED_100;
4678 		else
4679 			state->speed = SPEED_10;
4680 	}
4681 
4682 	state->pause = 0;
4683 	if (val & MVPP2_GMAC_STATUS0_RX_PAUSE)
4684 		state->pause |= MLO_PAUSE_RX;
4685 	if (val & MVPP2_GMAC_STATUS0_TX_PAUSE)
4686 		state->pause |= MLO_PAUSE_TX;
4687 }
4688 
4689 static int mvpp2_phylink_mac_link_state(struct phylink_config *config,
4690 					struct phylink_link_state *state)
4691 {
4692 	struct mvpp2_port *port = container_of(config, struct mvpp2_port,
4693 					       phylink_config);
4694 
4695 	if (port->priv->hw_version == MVPP22 && port->gop_id == 0) {
4696 		u32 mode = readl(port->base + MVPP22_XLG_CTRL3_REG);
4697 		mode &= MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
4698 
4699 		if (mode == MVPP22_XLG_CTRL3_MACMODESELECT_10G) {
4700 			mvpp22_xlg_link_state(port, state);
4701 			return 1;
4702 		}
4703 	}
4704 
4705 	mvpp2_gmac_link_state(port, state);
4706 	return 1;
4707 }
4708 
4709 static void mvpp2_mac_an_restart(struct phylink_config *config)
4710 {
4711 	struct mvpp2_port *port = container_of(config, struct mvpp2_port,
4712 					       phylink_config);
4713 	u32 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4714 
4715 	writel(val | MVPP2_GMAC_IN_BAND_RESTART_AN,
4716 	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4717 	writel(val & ~MVPP2_GMAC_IN_BAND_RESTART_AN,
4718 	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4719 }
4720 
4721 static void mvpp2_xlg_config(struct mvpp2_port *port, unsigned int mode,
4722 			     const struct phylink_link_state *state)
4723 {
4724 	u32 old_ctrl0, ctrl0;
4725 	u32 old_ctrl4, ctrl4;
4726 
4727 	old_ctrl0 = ctrl0 = readl(port->base + MVPP22_XLG_CTRL0_REG);
4728 	old_ctrl4 = ctrl4 = readl(port->base + MVPP22_XLG_CTRL4_REG);
4729 
4730 	ctrl0 |= MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4731 
4732 	if (state->pause & MLO_PAUSE_TX)
4733 		ctrl0 |= MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN;
4734 	else
4735 		ctrl0 &= ~MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN;
4736 
4737 	if (state->pause & MLO_PAUSE_RX)
4738 		ctrl0 |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4739 	else
4740 		ctrl0 &= ~MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4741 
4742 	ctrl4 &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4743 	ctrl4 |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC |
4744 		 MVPP22_XLG_CTRL4_EN_IDLE_CHECK;
4745 
4746 	if (old_ctrl0 != ctrl0)
4747 		writel(ctrl0, port->base + MVPP22_XLG_CTRL0_REG);
4748 	if (old_ctrl4 != ctrl4)
4749 		writel(ctrl4, port->base + MVPP22_XLG_CTRL4_REG);
4750 
4751 	if (!(old_ctrl0 & MVPP22_XLG_CTRL0_MAC_RESET_DIS)) {
4752 		while (!(readl(port->base + MVPP22_XLG_CTRL0_REG) &
4753 			 MVPP22_XLG_CTRL0_MAC_RESET_DIS))
4754 			continue;
4755 	}
4756 }
4757 
4758 static void mvpp2_gmac_config(struct mvpp2_port *port, unsigned int mode,
4759 			      const struct phylink_link_state *state)
4760 {
4761 	u32 old_an, an;
4762 	u32 old_ctrl0, ctrl0;
4763 	u32 old_ctrl2, ctrl2;
4764 	u32 old_ctrl4, ctrl4;
4765 
4766 	old_an = an = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4767 	old_ctrl0 = ctrl0 = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4768 	old_ctrl2 = ctrl2 = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4769 	old_ctrl4 = ctrl4 = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4770 
4771 	an &= ~(MVPP2_GMAC_CONFIG_MII_SPEED | MVPP2_GMAC_CONFIG_GMII_SPEED |
4772 		MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FC_ADV_EN |
4773 		MVPP2_GMAC_FC_ADV_ASM_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4774 		MVPP2_GMAC_CONFIG_FULL_DUPLEX | MVPP2_GMAC_AN_DUPLEX_EN |
4775 		MVPP2_GMAC_IN_BAND_AUTONEG | MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS);
4776 	ctrl0 &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4777 	ctrl2 &= ~(MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PORT_RESET_MASK |
4778 		   MVPP2_GMAC_PCS_ENABLE_MASK);
4779 	ctrl4 &= ~(MVPP22_CTRL4_RX_FC_EN | MVPP22_CTRL4_TX_FC_EN);
4780 
4781 	/* Configure port type */
4782 	if (phy_interface_mode_is_8023z(state->interface)) {
4783 		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK;
4784 		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4785 		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
4786 			 MVPP22_CTRL4_DP_CLK_SEL |
4787 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4788 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
4789 		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_INBAND_AN_MASK;
4790 		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4791 		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
4792 			 MVPP22_CTRL4_DP_CLK_SEL |
4793 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4794 	} else if (phy_interface_mode_is_rgmii(state->interface)) {
4795 		ctrl4 &= ~MVPP22_CTRL4_DP_CLK_SEL;
4796 		ctrl4 |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4797 			 MVPP22_CTRL4_SYNC_BYPASS_DIS |
4798 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4799 	}
4800 
4801 	/* Configure advertisement bits */
4802 	if (phylink_test(state->advertising, Pause))
4803 		an |= MVPP2_GMAC_FC_ADV_EN;
4804 	if (phylink_test(state->advertising, Asym_Pause))
4805 		an |= MVPP2_GMAC_FC_ADV_ASM_EN;
4806 
4807 	/* Configure negotiation style */
4808 	if (!phylink_autoneg_inband(mode)) {
4809 		/* Phy or fixed speed - no in-band AN */
4810 		if (state->duplex)
4811 			an |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
4812 
4813 		if (state->speed == SPEED_1000 || state->speed == SPEED_2500)
4814 			an |= MVPP2_GMAC_CONFIG_GMII_SPEED;
4815 		else if (state->speed == SPEED_100)
4816 			an |= MVPP2_GMAC_CONFIG_MII_SPEED;
4817 
4818 		if (state->pause & MLO_PAUSE_TX)
4819 			ctrl4 |= MVPP22_CTRL4_TX_FC_EN;
4820 		if (state->pause & MLO_PAUSE_RX)
4821 			ctrl4 |= MVPP22_CTRL4_RX_FC_EN;
4822 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
4823 		/* SGMII in-band mode receives the speed and duplex from
4824 		 * the PHY. Flow control information is not received. */
4825 		an &= ~(MVPP2_GMAC_FORCE_LINK_DOWN | MVPP2_GMAC_FORCE_LINK_PASS);
4826 		an |= MVPP2_GMAC_IN_BAND_AUTONEG |
4827 		      MVPP2_GMAC_AN_SPEED_EN |
4828 		      MVPP2_GMAC_AN_DUPLEX_EN;
4829 
4830 		if (state->pause & MLO_PAUSE_TX)
4831 			ctrl4 |= MVPP22_CTRL4_TX_FC_EN;
4832 		if (state->pause & MLO_PAUSE_RX)
4833 			ctrl4 |= MVPP22_CTRL4_RX_FC_EN;
4834 	} else if (phy_interface_mode_is_8023z(state->interface)) {
4835 		/* 1000BaseX and 2500BaseX ports cannot negotiate speed nor can
4836 		 * they negotiate duplex: they are always operating with a fixed
4837 		 * speed of 1000/2500Mbps in full duplex, so force 1000/2500
4838 		 * speed and full duplex here.
4839 		 */
4840 		ctrl0 |= MVPP2_GMAC_PORT_TYPE_MASK;
4841 		an &= ~(MVPP2_GMAC_FORCE_LINK_DOWN | MVPP2_GMAC_FORCE_LINK_PASS);
4842 		an |= MVPP2_GMAC_IN_BAND_AUTONEG |
4843 		      MVPP2_GMAC_CONFIG_GMII_SPEED |
4844 		      MVPP2_GMAC_CONFIG_FULL_DUPLEX;
4845 
4846 		if (state->pause & MLO_PAUSE_AN && state->an_enabled) {
4847 			an |= MVPP2_GMAC_FLOW_CTRL_AUTONEG;
4848 		} else {
4849 			if (state->pause & MLO_PAUSE_TX)
4850 				ctrl4 |= MVPP22_CTRL4_TX_FC_EN;
4851 			if (state->pause & MLO_PAUSE_RX)
4852 				ctrl4 |= MVPP22_CTRL4_RX_FC_EN;
4853 		}
4854 	}
4855 
4856 /* Some fields of the auto-negotiation register require the port to be down when
4857  * their value is updated.
4858  */
4859 #define MVPP2_GMAC_AN_PORT_DOWN_MASK	\
4860 		(MVPP2_GMAC_IN_BAND_AUTONEG | \
4861 		 MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS | \
4862 		 MVPP2_GMAC_CONFIG_MII_SPEED | MVPP2_GMAC_CONFIG_GMII_SPEED | \
4863 		 MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_CONFIG_FULL_DUPLEX | \
4864 		 MVPP2_GMAC_AN_DUPLEX_EN)
4865 
4866 	if ((old_ctrl0 ^ ctrl0) & MVPP2_GMAC_PORT_TYPE_MASK ||
4867 	    (old_ctrl2 ^ ctrl2) & MVPP2_GMAC_INBAND_AN_MASK ||
4868 	    (old_an ^ an) & MVPP2_GMAC_AN_PORT_DOWN_MASK) {
4869 		/* Force link down */
4870 		old_an &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4871 		old_an |= MVPP2_GMAC_FORCE_LINK_DOWN;
4872 		writel(old_an, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4873 
4874 		/* Set the GMAC in a reset state - do this in a way that
4875 		 * ensures we clear it below.
4876 		 */
4877 		old_ctrl2 |= MVPP2_GMAC_PORT_RESET_MASK;
4878 		writel(old_ctrl2, port->base + MVPP2_GMAC_CTRL_2_REG);
4879 	}
4880 
4881 	if (old_ctrl0 != ctrl0)
4882 		writel(ctrl0, port->base + MVPP2_GMAC_CTRL_0_REG);
4883 	if (old_ctrl2 != ctrl2)
4884 		writel(ctrl2, port->base + MVPP2_GMAC_CTRL_2_REG);
4885 	if (old_ctrl4 != ctrl4)
4886 		writel(ctrl4, port->base + MVPP22_GMAC_CTRL_4_REG);
4887 	if (old_an != an)
4888 		writel(an, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4889 
4890 	if (old_ctrl2 & MVPP2_GMAC_PORT_RESET_MASK) {
4891 		while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4892 		       MVPP2_GMAC_PORT_RESET_MASK)
4893 			continue;
4894 	}
4895 }
4896 
4897 static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode,
4898 			     const struct phylink_link_state *state)
4899 {
4900 	struct net_device *dev = to_net_dev(config->dev);
4901 	struct mvpp2_port *port = netdev_priv(dev);
4902 	bool change_interface = port->phy_interface != state->interface;
4903 
4904 	/* Check for invalid configuration */
4905 	if (mvpp2_is_xlg(state->interface) && port->gop_id != 0) {
4906 		netdev_err(dev, "Invalid mode on %s\n", dev->name);
4907 		return;
4908 	}
4909 
4910 	/* Make sure the port is disabled when reconfiguring the mode */
4911 	mvpp2_port_disable(port);
4912 
4913 	if (port->priv->hw_version == MVPP22 && change_interface) {
4914 		mvpp22_gop_mask_irq(port);
4915 
4916 		port->phy_interface = state->interface;
4917 
4918 		/* Reconfigure the serdes lanes */
4919 		phy_power_off(port->comphy);
4920 		mvpp22_mode_reconfigure(port);
4921 	}
4922 
4923 	/* mac (re)configuration */
4924 	if (mvpp2_is_xlg(state->interface))
4925 		mvpp2_xlg_config(port, mode, state);
4926 	else if (phy_interface_mode_is_rgmii(state->interface) ||
4927 		 phy_interface_mode_is_8023z(state->interface) ||
4928 		 state->interface == PHY_INTERFACE_MODE_SGMII)
4929 		mvpp2_gmac_config(port, mode, state);
4930 
4931 	if (port->priv->hw_version == MVPP21 && port->flags & MVPP2_F_LOOPBACK)
4932 		mvpp2_port_loopback_set(port, state);
4933 
4934 	if (port->priv->hw_version == MVPP22 && change_interface)
4935 		mvpp22_gop_unmask_irq(port);
4936 
4937 	mvpp2_port_enable(port);
4938 }
4939 
4940 static void mvpp2_mac_link_up(struct phylink_config *config, unsigned int mode,
4941 			      phy_interface_t interface, struct phy_device *phy)
4942 {
4943 	struct net_device *dev = to_net_dev(config->dev);
4944 	struct mvpp2_port *port = netdev_priv(dev);
4945 	u32 val;
4946 
4947 	if (!phylink_autoneg_inband(mode)) {
4948 		if (mvpp2_is_xlg(interface)) {
4949 			val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4950 			val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
4951 			val |= MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
4952 			writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4953 		} else {
4954 			val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4955 			val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4956 			val |= MVPP2_GMAC_FORCE_LINK_PASS;
4957 			writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4958 		}
4959 	}
4960 
4961 	mvpp2_port_enable(port);
4962 
4963 	mvpp2_egress_enable(port);
4964 	mvpp2_ingress_enable(port);
4965 	netif_tx_wake_all_queues(dev);
4966 }
4967 
4968 static void mvpp2_mac_link_down(struct phylink_config *config,
4969 				unsigned int mode, phy_interface_t interface)
4970 {
4971 	struct net_device *dev = to_net_dev(config->dev);
4972 	struct mvpp2_port *port = netdev_priv(dev);
4973 	u32 val;
4974 
4975 	if (!phylink_autoneg_inband(mode)) {
4976 		if (mvpp2_is_xlg(interface)) {
4977 			val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4978 			val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
4979 			val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
4980 			writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4981 		} else {
4982 			val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4983 			val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4984 			val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4985 			writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4986 		}
4987 	}
4988 
4989 	netif_tx_stop_all_queues(dev);
4990 	mvpp2_egress_disable(port);
4991 	mvpp2_ingress_disable(port);
4992 
4993 	mvpp2_port_disable(port);
4994 }
4995 
4996 static const struct phylink_mac_ops mvpp2_phylink_ops = {
4997 	.validate = mvpp2_phylink_validate,
4998 	.mac_link_state = mvpp2_phylink_mac_link_state,
4999 	.mac_an_restart = mvpp2_mac_an_restart,
5000 	.mac_config = mvpp2_mac_config,
5001 	.mac_link_up = mvpp2_mac_link_up,
5002 	.mac_link_down = mvpp2_mac_link_down,
5003 };
5004 
5005 /* Ports initialization */
5006 static int mvpp2_port_probe(struct platform_device *pdev,
5007 			    struct fwnode_handle *port_fwnode,
5008 			    struct mvpp2 *priv)
5009 {
5010 	struct phy *comphy = NULL;
5011 	struct mvpp2_port *port;
5012 	struct mvpp2_port_pcpu *port_pcpu;
5013 	struct device_node *port_node = to_of_node(port_fwnode);
5014 	netdev_features_t features;
5015 	struct net_device *dev;
5016 	struct resource *res;
5017 	struct phylink *phylink;
5018 	char *mac_from = "";
5019 	unsigned int ntxqs, nrxqs, thread;
5020 	unsigned long flags = 0;
5021 	bool has_tx_irqs;
5022 	u32 id;
5023 	int phy_mode;
5024 	int err, i;
5025 
5026 	has_tx_irqs = mvpp2_port_has_irqs(priv, port_node, &flags);
5027 	if (!has_tx_irqs && queue_mode == MVPP2_QDIST_MULTI_MODE) {
5028 		dev_err(&pdev->dev,
5029 			"not enough IRQs to support multi queue mode\n");
5030 		return -EINVAL;
5031 	}
5032 
5033 	ntxqs = MVPP2_MAX_TXQ;
5034 	if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_SINGLE_MODE) {
5035 		nrxqs = 1;
5036 	} else {
5037 		/* According to the PPv2.2 datasheet and our experiments on
5038 		 * PPv2.1, RX queues have an allocation granularity of 4 (when
5039 		 * more than a single one on PPv2.2).
5040 		 * Round up to nearest multiple of 4.
5041 		 */
5042 		nrxqs = (num_possible_cpus() + 3) & ~0x3;
5043 		if (nrxqs > MVPP2_PORT_MAX_RXQ)
5044 			nrxqs = MVPP2_PORT_MAX_RXQ;
5045 	}
5046 
5047 	dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
5048 	if (!dev)
5049 		return -ENOMEM;
5050 
5051 	phy_mode = fwnode_get_phy_mode(port_fwnode);
5052 	if (phy_mode < 0) {
5053 		dev_err(&pdev->dev, "incorrect phy mode\n");
5054 		err = phy_mode;
5055 		goto err_free_netdev;
5056 	}
5057 
5058 	if (port_node) {
5059 		comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
5060 		if (IS_ERR(comphy)) {
5061 			if (PTR_ERR(comphy) == -EPROBE_DEFER) {
5062 				err = -EPROBE_DEFER;
5063 				goto err_free_netdev;
5064 			}
5065 			comphy = NULL;
5066 		}
5067 	}
5068 
5069 	if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
5070 		err = -EINVAL;
5071 		dev_err(&pdev->dev, "missing port-id value\n");
5072 		goto err_free_netdev;
5073 	}
5074 
5075 	dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
5076 	dev->watchdog_timeo = 5 * HZ;
5077 	dev->netdev_ops = &mvpp2_netdev_ops;
5078 	dev->ethtool_ops = &mvpp2_eth_tool_ops;
5079 
5080 	port = netdev_priv(dev);
5081 	port->dev = dev;
5082 	port->fwnode = port_fwnode;
5083 	port->has_phy = !!of_find_property(port_node, "phy", NULL);
5084 	port->ntxqs = ntxqs;
5085 	port->nrxqs = nrxqs;
5086 	port->priv = priv;
5087 	port->has_tx_irqs = has_tx_irqs;
5088 	port->flags = flags;
5089 
5090 	err = mvpp2_queue_vectors_init(port, port_node);
5091 	if (err)
5092 		goto err_free_netdev;
5093 
5094 	if (port_node)
5095 		port->link_irq = of_irq_get_byname(port_node, "link");
5096 	else
5097 		port->link_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
5098 	if (port->link_irq == -EPROBE_DEFER) {
5099 		err = -EPROBE_DEFER;
5100 		goto err_deinit_qvecs;
5101 	}
5102 	if (port->link_irq <= 0)
5103 		/* the link irq is optional */
5104 		port->link_irq = 0;
5105 
5106 	if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
5107 		port->flags |= MVPP2_F_LOOPBACK;
5108 
5109 	port->id = id;
5110 	if (priv->hw_version == MVPP21)
5111 		port->first_rxq = port->id * port->nrxqs;
5112 	else
5113 		port->first_rxq = port->id * priv->max_port_rxqs;
5114 
5115 	port->of_node = port_node;
5116 	port->phy_interface = phy_mode;
5117 	port->comphy = comphy;
5118 
5119 	if (priv->hw_version == MVPP21) {
5120 		res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
5121 		port->base = devm_ioremap_resource(&pdev->dev, res);
5122 		if (IS_ERR(port->base)) {
5123 			err = PTR_ERR(port->base);
5124 			goto err_free_irq;
5125 		}
5126 
5127 		port->stats_base = port->priv->lms_base +
5128 				   MVPP21_MIB_COUNTERS_OFFSET +
5129 				   port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
5130 	} else {
5131 		if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
5132 					     &port->gop_id)) {
5133 			err = -EINVAL;
5134 			dev_err(&pdev->dev, "missing gop-port-id value\n");
5135 			goto err_deinit_qvecs;
5136 		}
5137 
5138 		port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
5139 		port->stats_base = port->priv->iface_base +
5140 				   MVPP22_MIB_COUNTERS_OFFSET +
5141 				   port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
5142 	}
5143 
5144 	/* Alloc per-cpu and ethtool stats */
5145 	port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
5146 	if (!port->stats) {
5147 		err = -ENOMEM;
5148 		goto err_free_irq;
5149 	}
5150 
5151 	port->ethtool_stats = devm_kcalloc(&pdev->dev,
5152 					   MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs),
5153 					   sizeof(u64), GFP_KERNEL);
5154 	if (!port->ethtool_stats) {
5155 		err = -ENOMEM;
5156 		goto err_free_stats;
5157 	}
5158 
5159 	mutex_init(&port->gather_stats_lock);
5160 	INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
5161 
5162 	mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
5163 
5164 	port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
5165 	port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
5166 	SET_NETDEV_DEV(dev, &pdev->dev);
5167 
5168 	err = mvpp2_port_init(port);
5169 	if (err < 0) {
5170 		dev_err(&pdev->dev, "failed to init port %d\n", id);
5171 		goto err_free_stats;
5172 	}
5173 
5174 	mvpp2_port_periodic_xon_disable(port);
5175 
5176 	mvpp2_mac_reset_assert(port);
5177 	mvpp22_pcs_reset_assert(port);
5178 
5179 	port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
5180 	if (!port->pcpu) {
5181 		err = -ENOMEM;
5182 		goto err_free_txq_pcpu;
5183 	}
5184 
5185 	if (!port->has_tx_irqs) {
5186 		for (thread = 0; thread < priv->nthreads; thread++) {
5187 			port_pcpu = per_cpu_ptr(port->pcpu, thread);
5188 
5189 			hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
5190 				     HRTIMER_MODE_REL_PINNED);
5191 			port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
5192 			port_pcpu->timer_scheduled = false;
5193 
5194 			tasklet_init(&port_pcpu->tx_done_tasklet,
5195 				     mvpp2_tx_proc_cb,
5196 				     (unsigned long)dev);
5197 		}
5198 	}
5199 
5200 	features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5201 		   NETIF_F_TSO;
5202 	dev->features = features | NETIF_F_RXCSUM;
5203 	dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
5204 			    NETIF_F_HW_VLAN_CTAG_FILTER;
5205 
5206 	if (mvpp22_rss_is_supported()) {
5207 		dev->hw_features |= NETIF_F_RXHASH;
5208 		dev->features |= NETIF_F_NTUPLE;
5209 	}
5210 
5211 	if (port->pool_long->id == MVPP2_BM_JUMBO && port->id != 0) {
5212 		dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
5213 		dev->hw_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
5214 	}
5215 
5216 	dev->vlan_features |= features;
5217 	dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
5218 	dev->priv_flags |= IFF_UNICAST_FLT;
5219 
5220 	/* MTU range: 68 - 9704 */
5221 	dev->min_mtu = ETH_MIN_MTU;
5222 	/* 9704 == 9728 - 20 and rounding to 8 */
5223 	dev->max_mtu = MVPP2_BM_JUMBO_PKT_SIZE;
5224 	dev->dev.of_node = port_node;
5225 
5226 	/* Phylink isn't used w/ ACPI as of now */
5227 	if (port_node) {
5228 		port->phylink_config.dev = &dev->dev;
5229 		port->phylink_config.type = PHYLINK_NETDEV;
5230 
5231 		phylink = phylink_create(&port->phylink_config, port_fwnode,
5232 					 phy_mode, &mvpp2_phylink_ops);
5233 		if (IS_ERR(phylink)) {
5234 			err = PTR_ERR(phylink);
5235 			goto err_free_port_pcpu;
5236 		}
5237 		port->phylink = phylink;
5238 	} else {
5239 		port->phylink = NULL;
5240 	}
5241 
5242 	err = register_netdev(dev);
5243 	if (err < 0) {
5244 		dev_err(&pdev->dev, "failed to register netdev\n");
5245 		goto err_phylink;
5246 	}
5247 	netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
5248 
5249 	priv->port_list[priv->port_count++] = port;
5250 
5251 	return 0;
5252 
5253 err_phylink:
5254 	if (port->phylink)
5255 		phylink_destroy(port->phylink);
5256 err_free_port_pcpu:
5257 	free_percpu(port->pcpu);
5258 err_free_txq_pcpu:
5259 	for (i = 0; i < port->ntxqs; i++)
5260 		free_percpu(port->txqs[i]->pcpu);
5261 err_free_stats:
5262 	free_percpu(port->stats);
5263 err_free_irq:
5264 	if (port->link_irq)
5265 		irq_dispose_mapping(port->link_irq);
5266 err_deinit_qvecs:
5267 	mvpp2_queue_vectors_deinit(port);
5268 err_free_netdev:
5269 	free_netdev(dev);
5270 	return err;
5271 }
5272 
5273 /* Ports removal routine */
5274 static void mvpp2_port_remove(struct mvpp2_port *port)
5275 {
5276 	int i;
5277 
5278 	unregister_netdev(port->dev);
5279 	if (port->phylink)
5280 		phylink_destroy(port->phylink);
5281 	free_percpu(port->pcpu);
5282 	free_percpu(port->stats);
5283 	for (i = 0; i < port->ntxqs; i++)
5284 		free_percpu(port->txqs[i]->pcpu);
5285 	mvpp2_queue_vectors_deinit(port);
5286 	if (port->link_irq)
5287 		irq_dispose_mapping(port->link_irq);
5288 	free_netdev(port->dev);
5289 }
5290 
5291 /* Initialize decoding windows */
5292 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
5293 				    struct mvpp2 *priv)
5294 {
5295 	u32 win_enable;
5296 	int i;
5297 
5298 	for (i = 0; i < 6; i++) {
5299 		mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
5300 		mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
5301 
5302 		if (i < 4)
5303 			mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
5304 	}
5305 
5306 	win_enable = 0;
5307 
5308 	for (i = 0; i < dram->num_cs; i++) {
5309 		const struct mbus_dram_window *cs = dram->cs + i;
5310 
5311 		mvpp2_write(priv, MVPP2_WIN_BASE(i),
5312 			    (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
5313 			    dram->mbus_dram_target_id);
5314 
5315 		mvpp2_write(priv, MVPP2_WIN_SIZE(i),
5316 			    (cs->size - 1) & 0xffff0000);
5317 
5318 		win_enable |= (1 << i);
5319 	}
5320 
5321 	mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
5322 }
5323 
5324 /* Initialize Rx FIFO's */
5325 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
5326 {
5327 	int port;
5328 
5329 	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
5330 		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
5331 			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
5332 		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
5333 			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
5334 	}
5335 
5336 	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
5337 		    MVPP2_RX_FIFO_PORT_MIN_PKT);
5338 	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
5339 }
5340 
5341 static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
5342 {
5343 	int port;
5344 
5345 	/* The FIFO size parameters are set depending on the maximum speed a
5346 	 * given port can handle:
5347 	 * - Port 0: 10Gbps
5348 	 * - Port 1: 2.5Gbps
5349 	 * - Ports 2 and 3: 1Gbps
5350 	 */
5351 
5352 	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
5353 		    MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
5354 	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
5355 		    MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
5356 
5357 	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
5358 		    MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
5359 	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
5360 		    MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
5361 
5362 	for (port = 2; port < MVPP2_MAX_PORTS; port++) {
5363 		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
5364 			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
5365 		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
5366 			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
5367 	}
5368 
5369 	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
5370 		    MVPP2_RX_FIFO_PORT_MIN_PKT);
5371 	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
5372 }
5373 
5374 /* Initialize Tx FIFO's: the total FIFO size is 19kB on PPv2.2 and 10G
5375  * interfaces must have a Tx FIFO size of 10kB. As only port 0 can do 10G,
5376  * configure its Tx FIFO size to 10kB and the others ports Tx FIFO size to 3kB.
5377  */
5378 static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
5379 {
5380 	int port, size, thrs;
5381 
5382 	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
5383 		if (port == 0) {
5384 			size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
5385 			thrs = MVPP2_TX_FIFO_THRESHOLD_10KB;
5386 		} else {
5387 			size = MVPP22_TX_FIFO_DATA_SIZE_3KB;
5388 			thrs = MVPP2_TX_FIFO_THRESHOLD_3KB;
5389 		}
5390 		mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
5391 		mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), thrs);
5392 	}
5393 }
5394 
5395 static void mvpp2_axi_init(struct mvpp2 *priv)
5396 {
5397 	u32 val, rdval, wrval;
5398 
5399 	mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
5400 
5401 	/* AXI Bridge Configuration */
5402 
5403 	rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
5404 		<< MVPP22_AXI_ATTR_CACHE_OFFS;
5405 	rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
5406 		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;
5407 
5408 	wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
5409 		<< MVPP22_AXI_ATTR_CACHE_OFFS;
5410 	wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
5411 		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;
5412 
5413 	/* BM */
5414 	mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
5415 	mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
5416 
5417 	/* Descriptors */
5418 	mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
5419 	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
5420 	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
5421 	mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
5422 
5423 	/* Buffer Data */
5424 	mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
5425 	mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
5426 
5427 	val = MVPP22_AXI_CODE_CACHE_NON_CACHE
5428 		<< MVPP22_AXI_CODE_CACHE_OFFS;
5429 	val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
5430 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
5431 	mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
5432 	mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
5433 
5434 	val = MVPP22_AXI_CODE_CACHE_RD_CACHE
5435 		<< MVPP22_AXI_CODE_CACHE_OFFS;
5436 	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
5437 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
5438 
5439 	mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
5440 
5441 	val = MVPP22_AXI_CODE_CACHE_WR_CACHE
5442 		<< MVPP22_AXI_CODE_CACHE_OFFS;
5443 	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
5444 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
5445 
5446 	mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
5447 }
5448 
5449 /* Initialize network controller common part HW */
5450 static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
5451 {
5452 	const struct mbus_dram_target_info *dram_target_info;
5453 	int err, i;
5454 	u32 val;
5455 
5456 	/* MBUS windows configuration */
5457 	dram_target_info = mv_mbus_dram_info();
5458 	if (dram_target_info)
5459 		mvpp2_conf_mbus_windows(dram_target_info, priv);
5460 
5461 	if (priv->hw_version == MVPP22)
5462 		mvpp2_axi_init(priv);
5463 
5464 	/* Disable HW PHY polling */
5465 	if (priv->hw_version == MVPP21) {
5466 		val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
5467 		val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
5468 		writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
5469 	} else {
5470 		val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
5471 		val &= ~MVPP22_SMI_POLLING_EN;
5472 		writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
5473 	}
5474 
5475 	/* Allocate and initialize aggregated TXQs */
5476 	priv->aggr_txqs = devm_kcalloc(&pdev->dev, MVPP2_MAX_THREADS,
5477 				       sizeof(*priv->aggr_txqs),
5478 				       GFP_KERNEL);
5479 	if (!priv->aggr_txqs)
5480 		return -ENOMEM;
5481 
5482 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
5483 		priv->aggr_txqs[i].id = i;
5484 		priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
5485 		err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
5486 		if (err < 0)
5487 			return err;
5488 	}
5489 
5490 	/* Fifo Init */
5491 	if (priv->hw_version == MVPP21) {
5492 		mvpp2_rx_fifo_init(priv);
5493 	} else {
5494 		mvpp22_rx_fifo_init(priv);
5495 		mvpp22_tx_fifo_init(priv);
5496 	}
5497 
5498 	if (priv->hw_version == MVPP21)
5499 		writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
5500 		       priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
5501 
5502 	/* Allow cache snoop when transmiting packets */
5503 	mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
5504 
5505 	/* Buffer Manager initialization */
5506 	err = mvpp2_bm_init(pdev, priv);
5507 	if (err < 0)
5508 		return err;
5509 
5510 	/* Parser default initialization */
5511 	err = mvpp2_prs_default_init(pdev, priv);
5512 	if (err < 0)
5513 		return err;
5514 
5515 	/* Classifier default initialization */
5516 	mvpp2_cls_init(priv);
5517 
5518 	return 0;
5519 }
5520 
5521 static int mvpp2_probe(struct platform_device *pdev)
5522 {
5523 	const struct acpi_device_id *acpi_id;
5524 	struct fwnode_handle *fwnode = pdev->dev.fwnode;
5525 	struct fwnode_handle *port_fwnode;
5526 	struct mvpp2 *priv;
5527 	struct resource *res;
5528 	void __iomem *base;
5529 	int i, shared;
5530 	int err;
5531 
5532 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
5533 	if (!priv)
5534 		return -ENOMEM;
5535 
5536 	if (has_acpi_companion(&pdev->dev)) {
5537 		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
5538 					    &pdev->dev);
5539 		if (!acpi_id)
5540 			return -EINVAL;
5541 		priv->hw_version = (unsigned long)acpi_id->driver_data;
5542 	} else {
5543 		priv->hw_version =
5544 			(unsigned long)of_device_get_match_data(&pdev->dev);
5545 	}
5546 
5547 	/* multi queue mode isn't supported on PPV2.1, fallback to single
5548 	 * mode
5549 	 */
5550 	if (priv->hw_version == MVPP21)
5551 		queue_mode = MVPP2_QDIST_SINGLE_MODE;
5552 
5553 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5554 	base = devm_ioremap_resource(&pdev->dev, res);
5555 	if (IS_ERR(base))
5556 		return PTR_ERR(base);
5557 
5558 	if (priv->hw_version == MVPP21) {
5559 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
5560 		priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
5561 		if (IS_ERR(priv->lms_base))
5562 			return PTR_ERR(priv->lms_base);
5563 	} else {
5564 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
5565 		if (has_acpi_companion(&pdev->dev)) {
5566 			/* In case the MDIO memory region is declared in
5567 			 * the ACPI, it can already appear as 'in-use'
5568 			 * in the OS. Because it is overlapped by second
5569 			 * region of the network controller, make
5570 			 * sure it is released, before requesting it again.
5571 			 * The care is taken by mvpp2 driver to avoid
5572 			 * concurrent access to this memory region.
5573 			 */
5574 			release_resource(res);
5575 		}
5576 		priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
5577 		if (IS_ERR(priv->iface_base))
5578 			return PTR_ERR(priv->iface_base);
5579 	}
5580 
5581 	if (priv->hw_version == MVPP22 && dev_of_node(&pdev->dev)) {
5582 		priv->sysctrl_base =
5583 			syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
5584 							"marvell,system-controller");
5585 		if (IS_ERR(priv->sysctrl_base))
5586 			/* The system controller regmap is optional for dt
5587 			 * compatibility reasons. When not provided, the
5588 			 * configuration of the GoP relies on the
5589 			 * firmware/bootloader.
5590 			 */
5591 			priv->sysctrl_base = NULL;
5592 	}
5593 
5594 	mvpp2_setup_bm_pool();
5595 
5596 
5597 	priv->nthreads = min_t(unsigned int, num_present_cpus(),
5598 			       MVPP2_MAX_THREADS);
5599 
5600 	shared = num_present_cpus() - priv->nthreads;
5601 	if (shared > 0)
5602 		bitmap_fill(&priv->lock_map,
5603 			    min_t(int, shared, MVPP2_MAX_THREADS));
5604 
5605 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
5606 		u32 addr_space_sz;
5607 
5608 		addr_space_sz = (priv->hw_version == MVPP21 ?
5609 				 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
5610 		priv->swth_base[i] = base + i * addr_space_sz;
5611 	}
5612 
5613 	if (priv->hw_version == MVPP21)
5614 		priv->max_port_rxqs = 8;
5615 	else
5616 		priv->max_port_rxqs = 32;
5617 
5618 	if (dev_of_node(&pdev->dev)) {
5619 		priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
5620 		if (IS_ERR(priv->pp_clk))
5621 			return PTR_ERR(priv->pp_clk);
5622 		err = clk_prepare_enable(priv->pp_clk);
5623 		if (err < 0)
5624 			return err;
5625 
5626 		priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
5627 		if (IS_ERR(priv->gop_clk)) {
5628 			err = PTR_ERR(priv->gop_clk);
5629 			goto err_pp_clk;
5630 		}
5631 		err = clk_prepare_enable(priv->gop_clk);
5632 		if (err < 0)
5633 			goto err_pp_clk;
5634 
5635 		if (priv->hw_version == MVPP22) {
5636 			priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
5637 			if (IS_ERR(priv->mg_clk)) {
5638 				err = PTR_ERR(priv->mg_clk);
5639 				goto err_gop_clk;
5640 			}
5641 
5642 			err = clk_prepare_enable(priv->mg_clk);
5643 			if (err < 0)
5644 				goto err_gop_clk;
5645 
5646 			priv->mg_core_clk = devm_clk_get(&pdev->dev, "mg_core_clk");
5647 			if (IS_ERR(priv->mg_core_clk)) {
5648 				priv->mg_core_clk = NULL;
5649 			} else {
5650 				err = clk_prepare_enable(priv->mg_core_clk);
5651 				if (err < 0)
5652 					goto err_mg_clk;
5653 			}
5654 		}
5655 
5656 		priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
5657 		if (IS_ERR(priv->axi_clk)) {
5658 			err = PTR_ERR(priv->axi_clk);
5659 			if (err == -EPROBE_DEFER)
5660 				goto err_mg_core_clk;
5661 			priv->axi_clk = NULL;
5662 		} else {
5663 			err = clk_prepare_enable(priv->axi_clk);
5664 			if (err < 0)
5665 				goto err_mg_core_clk;
5666 		}
5667 
5668 		/* Get system's tclk rate */
5669 		priv->tclk = clk_get_rate(priv->pp_clk);
5670 	} else if (device_property_read_u32(&pdev->dev, "clock-frequency",
5671 					    &priv->tclk)) {
5672 		dev_err(&pdev->dev, "missing clock-frequency value\n");
5673 		return -EINVAL;
5674 	}
5675 
5676 	if (priv->hw_version == MVPP22) {
5677 		err = dma_set_mask(&pdev->dev, MVPP2_DESC_DMA_MASK);
5678 		if (err)
5679 			goto err_axi_clk;
5680 		/* Sadly, the BM pools all share the same register to
5681 		 * store the high 32 bits of their address. So they
5682 		 * must all have the same high 32 bits, which forces
5683 		 * us to restrict coherent memory to DMA_BIT_MASK(32).
5684 		 */
5685 		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
5686 		if (err)
5687 			goto err_axi_clk;
5688 	}
5689 
5690 	/* Initialize network controller */
5691 	err = mvpp2_init(pdev, priv);
5692 	if (err < 0) {
5693 		dev_err(&pdev->dev, "failed to initialize controller\n");
5694 		goto err_axi_clk;
5695 	}
5696 
5697 	/* Initialize ports */
5698 	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
5699 		err = mvpp2_port_probe(pdev, port_fwnode, priv);
5700 		if (err < 0)
5701 			goto err_port_probe;
5702 	}
5703 
5704 	if (priv->port_count == 0) {
5705 		dev_err(&pdev->dev, "no ports enabled\n");
5706 		err = -ENODEV;
5707 		goto err_axi_clk;
5708 	}
5709 
5710 	/* Statistics must be gathered regularly because some of them (like
5711 	 * packets counters) are 32-bit registers and could overflow quite
5712 	 * quickly. For instance, a 10Gb link used at full bandwidth with the
5713 	 * smallest packets (64B) will overflow a 32-bit counter in less than
5714 	 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
5715 	 */
5716 	snprintf(priv->queue_name, sizeof(priv->queue_name),
5717 		 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
5718 		 priv->port_count > 1 ? "+" : "");
5719 	priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
5720 	if (!priv->stats_queue) {
5721 		err = -ENOMEM;
5722 		goto err_port_probe;
5723 	}
5724 
5725 	mvpp2_dbgfs_init(priv, pdev->name);
5726 
5727 	platform_set_drvdata(pdev, priv);
5728 	return 0;
5729 
5730 err_port_probe:
5731 	i = 0;
5732 	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
5733 		if (priv->port_list[i])
5734 			mvpp2_port_remove(priv->port_list[i]);
5735 		i++;
5736 	}
5737 err_axi_clk:
5738 	clk_disable_unprepare(priv->axi_clk);
5739 
5740 err_mg_core_clk:
5741 	if (priv->hw_version == MVPP22)
5742 		clk_disable_unprepare(priv->mg_core_clk);
5743 err_mg_clk:
5744 	if (priv->hw_version == MVPP22)
5745 		clk_disable_unprepare(priv->mg_clk);
5746 err_gop_clk:
5747 	clk_disable_unprepare(priv->gop_clk);
5748 err_pp_clk:
5749 	clk_disable_unprepare(priv->pp_clk);
5750 	return err;
5751 }
5752 
5753 static int mvpp2_remove(struct platform_device *pdev)
5754 {
5755 	struct mvpp2 *priv = platform_get_drvdata(pdev);
5756 	struct fwnode_handle *fwnode = pdev->dev.fwnode;
5757 	struct fwnode_handle *port_fwnode;
5758 	int i = 0;
5759 
5760 	mvpp2_dbgfs_cleanup(priv);
5761 
5762 	flush_workqueue(priv->stats_queue);
5763 	destroy_workqueue(priv->stats_queue);
5764 
5765 	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
5766 		if (priv->port_list[i]) {
5767 			mutex_destroy(&priv->port_list[i]->gather_stats_lock);
5768 			mvpp2_port_remove(priv->port_list[i]);
5769 		}
5770 		i++;
5771 	}
5772 
5773 	for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
5774 		struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
5775 
5776 		mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
5777 	}
5778 
5779 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
5780 		struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
5781 
5782 		dma_free_coherent(&pdev->dev,
5783 				  MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
5784 				  aggr_txq->descs,
5785 				  aggr_txq->descs_dma);
5786 	}
5787 
5788 	if (is_acpi_node(port_fwnode))
5789 		return 0;
5790 
5791 	clk_disable_unprepare(priv->axi_clk);
5792 	clk_disable_unprepare(priv->mg_core_clk);
5793 	clk_disable_unprepare(priv->mg_clk);
5794 	clk_disable_unprepare(priv->pp_clk);
5795 	clk_disable_unprepare(priv->gop_clk);
5796 
5797 	return 0;
5798 }
5799 
5800 static const struct of_device_id mvpp2_match[] = {
5801 	{
5802 		.compatible = "marvell,armada-375-pp2",
5803 		.data = (void *)MVPP21,
5804 	},
5805 	{
5806 		.compatible = "marvell,armada-7k-pp22",
5807 		.data = (void *)MVPP22,
5808 	},
5809 	{ }
5810 };
5811 MODULE_DEVICE_TABLE(of, mvpp2_match);
5812 
5813 static const struct acpi_device_id mvpp2_acpi_match[] = {
5814 	{ "MRVL0110", MVPP22 },
5815 	{ },
5816 };
5817 MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
5818 
5819 static struct platform_driver mvpp2_driver = {
5820 	.probe = mvpp2_probe,
5821 	.remove = mvpp2_remove,
5822 	.driver = {
5823 		.name = MVPP2_DRIVER_NAME,
5824 		.of_match_table = mvpp2_match,
5825 		.acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
5826 	},
5827 };
5828 
5829 module_platform_driver(mvpp2_driver);
5830 
5831 MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
5832 MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
5833 MODULE_LICENSE("GPL v2");
5834