mvpp2_main.c 189.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9
/*
 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
 *
 * Copyright (C) 2014 Marvell
 *
 * Marcin Wojtas <mw@semihalf.com>
 */

10
#include <linux/acpi.h>
11 12 13 14 15 16 17 18
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/platform_device.h>
#include <linux/skbuff.h>
#include <linux/inetdevice.h>
#include <linux/mbus.h>
#include <linux/module.h>
A
Antoine Ténart 已提交
19
#include <linux/mfd/syscon.h>
20 21 22 23 24 25 26
#include <linux/interrupt.h>
#include <linux/cpumask.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_mdio.h>
#include <linux/of_net.h>
#include <linux/of_address.h>
27
#include <linux/of_device.h>
28
#include <linux/phy.h>
A
Antoine Tenart 已提交
29
#include <linux/phylink.h>
30
#include <linux/phy/phy.h>
31
#include <linux/ptp_classify.h>
32
#include <linux/clk.h>
33 34
#include <linux/hrtimer.h>
#include <linux/ktime.h>
A
Antoine Ténart 已提交
35
#include <linux/regmap.h>
36 37 38
#include <uapi/linux/ppp_defs.h>
#include <net/ip.h>
#include <net/ipv6.h>
39
#include <net/tso.h>
M
Matteo Croce 已提交
40
#include <linux/bpf_trace.h>
41

42 43 44
#include "mvpp2.h"
#include "mvpp2_prs.h"
#include "mvpp2_cls.h"
45

46 47 48
enum mvpp2_bm_pool_log_num {
	MVPP2_BM_SHORT,
	MVPP2_BM_LONG,
49
	MVPP2_BM_JUMBO,
50
	MVPP2_BM_POOLS_NUM
51 52
};

53 54 55 56
static struct {
	int pkt_size;
	int buf_num;
} mvpp2_pools[MVPP2_BM_POOLS_NUM];
57

58 59 60
/* The prototype is added here to be used in start_dev when using ACPI. This
 * will be removed once phylink is used for all modes (dt+ACPI).
 */
R
Russell King 已提交
61
static void mvpp2_acpi_start(struct mvpp2_port *port);
62

63 64 65
/* Queue modes */
#define MVPP2_QDIST_SINGLE_MODE	0
#define MVPP2_QDIST_MULTI_MODE	1
66

67
static int queue_mode = MVPP2_QDIST_MULTI_MODE;
68

69 70
module_param(queue_mode, int, 0444);
MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
71

72
/* Utility/helper methods */
73

74 75 76
void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
{
	writel(data, priv->swth_base[0] + offset);
77 78
}

79
u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
80
{
81
	return readl(priv->swth_base[0] + offset);
82 83
}

84
static u32 mvpp2_read_relaxed(struct mvpp2 *priv, u32 offset)
85
{
86
	return readl_relaxed(priv->swth_base[0] + offset);
87
}
88

89
static inline u32 mvpp2_cpu_to_thread(struct mvpp2 *priv, int cpu)
90
{
91
	return cpu % priv->nthreads;
92 93
}

M
Matteo Croce 已提交
94
static struct page_pool *
M
Matteo Croce 已提交
95 96
mvpp2_create_page_pool(struct device *dev, int num, int len,
		       enum dma_data_direction dma_dir)
M
Matteo Croce 已提交
97 98 99 100 101 102 103
{
	struct page_pool_params pp_params = {
		/* internal DMA mapping in page_pool */
		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
		.pool_size = num,
		.nid = NUMA_NO_NODE,
		.dev = dev,
M
Matteo Croce 已提交
104
		.dma_dir = dma_dir,
M
Matteo Croce 已提交
105
		.offset = MVPP2_SKB_HEADROOM,
M
Matteo Croce 已提交
106 107 108 109 110 111
		.max_len = len,
	};

	return page_pool_create(&pp_params);
}

112 113
/* These accessors should be used to access:
 *
114
 * - per-thread registers, where each thread has its own copy of the
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
 *   register.
 *
 *   MVPP2_BM_VIRT_ALLOC_REG
 *   MVPP2_BM_ADDR_HIGH_ALLOC
 *   MVPP22_BM_ADDR_HIGH_RLS_REG
 *   MVPP2_BM_VIRT_RLS_REG
 *   MVPP2_ISR_RX_TX_CAUSE_REG
 *   MVPP2_ISR_RX_TX_MASK_REG
 *   MVPP2_TXQ_NUM_REG
 *   MVPP2_AGGR_TXQ_UPDATE_REG
 *   MVPP2_TXQ_RSVD_REQ_REG
 *   MVPP2_TXQ_RSVD_RSLT_REG
 *   MVPP2_TXQ_SENT_REG
 *   MVPP2_RXQ_NUM_REG
 *
130 131
 * - global registers that must be accessed through a specific thread
 *   window, because they are related to an access to a per-thread
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 *   register
 *
 *   MVPP2_BM_PHY_ALLOC_REG    (related to MVPP2_BM_VIRT_ALLOC_REG)
 *   MVPP2_BM_PHY_RLS_REG      (related to MVPP2_BM_VIRT_RLS_REG)
 *   MVPP2_RXQ_THRESH_REG      (related to MVPP2_RXQ_NUM_REG)
 *   MVPP2_RXQ_DESC_ADDR_REG   (related to MVPP2_RXQ_NUM_REG)
 *   MVPP2_RXQ_DESC_SIZE_REG   (related to MVPP2_RXQ_NUM_REG)
 *   MVPP2_RXQ_INDEX_REG       (related to MVPP2_RXQ_NUM_REG)
 *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
 *   MVPP2_TXQ_DESC_ADDR_REG   (related to MVPP2_TXQ_NUM_REG)
 *   MVPP2_TXQ_DESC_SIZE_REG   (related to MVPP2_TXQ_NUM_REG)
 *   MVPP2_TXQ_INDEX_REG       (related to MVPP2_TXQ_NUM_REG)
 *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
 *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
 *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
 */
148
static void mvpp2_thread_write(struct mvpp2 *priv, unsigned int thread,
149 150
			       u32 offset, u32 data)
{
151
	writel(data, priv->swth_base[thread] + offset);
152 153
}

154
static u32 mvpp2_thread_read(struct mvpp2 *priv, unsigned int thread,
155
			     u32 offset)
156
{
157
	return readl(priv->swth_base[thread] + offset);
158
}
159

160
static void mvpp2_thread_write_relaxed(struct mvpp2 *priv, unsigned int thread,
161 162
				       u32 offset, u32 data)
{
163
	writel_relaxed(data, priv->swth_base[thread] + offset);
164
}
165

166
static u32 mvpp2_thread_read_relaxed(struct mvpp2 *priv, unsigned int thread,
167 168
				     u32 offset)
{
169
	return readl_relaxed(priv->swth_base[thread] + offset);
170
}
171

172 173 174 175
static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
					    struct mvpp2_tx_desc *tx_desc)
{
	if (port->priv->hw_version == MVPP21)
176
		return le32_to_cpu(tx_desc->pp21.buf_dma_addr);
177
	else
178 179
		return le64_to_cpu(tx_desc->pp22.buf_dma_addr_ptp) &
		       MVPP2_DESC_DMA_MASK;
180
}
181

182 183 184 185 186
static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
				      struct mvpp2_tx_desc *tx_desc,
				      dma_addr_t dma_addr)
{
	dma_addr_t addr, offset;
187

188 189
	addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
	offset = dma_addr & MVPP2_TX_DESC_ALIGN;
190

191
	if (port->priv->hw_version == MVPP21) {
192
		tx_desc->pp21.buf_dma_addr = cpu_to_le32(addr);
193
		tx_desc->pp21.packet_offset = offset;
194
	} else {
195
		__le64 val = cpu_to_le64(addr);
196

197
		tx_desc->pp22.buf_dma_addr_ptp &= ~cpu_to_le64(MVPP2_DESC_DMA_MASK);
198 199 200
		tx_desc->pp22.buf_dma_addr_ptp |= val;
		tx_desc->pp22.packet_offset = offset;
	}
201 202
}

203 204
static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
				    struct mvpp2_tx_desc *tx_desc)
205
{
206
	if (port->priv->hw_version == MVPP21)
207
		return le16_to_cpu(tx_desc->pp21.data_size);
208
	else
209
		return le16_to_cpu(tx_desc->pp22.data_size);
210 211
}

212 213 214
static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
				  struct mvpp2_tx_desc *tx_desc,
				  size_t size)
215
{
216
	if (port->priv->hw_version == MVPP21)
217
		tx_desc->pp21.data_size = cpu_to_le16(size);
218
	else
219
		tx_desc->pp22.data_size = cpu_to_le16(size);
220 221
}

222 223 224
static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
				 struct mvpp2_tx_desc *tx_desc,
				 unsigned int txq)
225
{
226 227 228 229
	if (port->priv->hw_version == MVPP21)
		tx_desc->pp21.phys_txq = txq;
	else
		tx_desc->pp22.phys_txq = txq;
230 231
}

232 233 234
static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
				 struct mvpp2_tx_desc *tx_desc,
				 unsigned int command)
235
{
236
	if (port->priv->hw_version == MVPP21)
237
		tx_desc->pp21.command = cpu_to_le32(command);
238
	else
239
		tx_desc->pp22.command = cpu_to_le32(command);
240
}
241

242 243 244 245 246 247 248 249
static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
					    struct mvpp2_tx_desc *tx_desc)
{
	if (port->priv->hw_version == MVPP21)
		return tx_desc->pp21.packet_offset;
	else
		return tx_desc->pp22.packet_offset;
}
250

251 252 253 254
static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
					    struct mvpp2_rx_desc *rx_desc)
{
	if (port->priv->hw_version == MVPP21)
255
		return le32_to_cpu(rx_desc->pp21.buf_dma_addr);
256
	else
257 258
		return le64_to_cpu(rx_desc->pp22.buf_dma_addr_key_hash) &
		       MVPP2_DESC_DMA_MASK;
259
}
260

261 262 263 264
static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
					     struct mvpp2_rx_desc *rx_desc)
{
	if (port->priv->hw_version == MVPP21)
265
		return le32_to_cpu(rx_desc->pp21.buf_cookie);
266
	else
267 268
		return le64_to_cpu(rx_desc->pp22.buf_cookie_misc) &
		       MVPP2_DESC_DMA_MASK;
269
}
270

271 272 273 274
static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
				    struct mvpp2_rx_desc *rx_desc)
{
	if (port->priv->hw_version == MVPP21)
275
		return le16_to_cpu(rx_desc->pp21.data_size);
276
	else
277
		return le16_to_cpu(rx_desc->pp22.data_size);
278
}
279

280 281 282 283
static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
				   struct mvpp2_rx_desc *rx_desc)
{
	if (port->priv->hw_version == MVPP21)
284
		return le32_to_cpu(rx_desc->pp21.status);
285
	else
286
		return le32_to_cpu(rx_desc->pp22.status);
287 288
}

289
static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
290
{
291 292 293 294
	txq_pcpu->txq_get_index++;
	if (txq_pcpu->txq_get_index == txq_pcpu->size)
		txq_pcpu->txq_get_index = 0;
}
295

296 297
static void mvpp2_txq_inc_put(struct mvpp2_port *port,
			      struct mvpp2_txq_pcpu *txq_pcpu,
M
Matteo Croce 已提交
298 299 300
			      void *data,
			      struct mvpp2_tx_desc *tx_desc,
			      enum mvpp2_tx_buf_type buf_type)
301 302 303
{
	struct mvpp2_txq_pcpu_buf *tx_buf =
		txq_pcpu->buffs + txq_pcpu->txq_put_index;
M
Matteo Croce 已提交
304 305 306 307 308
	tx_buf->type = buf_type;
	if (buf_type == MVPP2_TYPE_SKB)
		tx_buf->skb = data;
	else
		tx_buf->xdpf = data;
309 310 311 312 313 314 315
	tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
	tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
		mvpp2_txdesc_offset_get(port, tx_desc);
	txq_pcpu->txq_put_index++;
	if (txq_pcpu->txq_put_index == txq_pcpu->size)
		txq_pcpu->txq_put_index = 0;
}
316

M
Matteo Croce 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/* Get number of maximum RXQ */
static int mvpp2_get_nrxqs(struct mvpp2 *priv)
{
	unsigned int nrxqs;

	if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_SINGLE_MODE)
		return 1;

	/* According to the PPv2.2 datasheet and our experiments on
	 * PPv2.1, RX queues have an allocation granularity of 4 (when
	 * more than a single one on PPv2.2).
	 * Round up to nearest multiple of 4.
	 */
	nrxqs = (num_possible_cpus() + 3) & ~0x3;
	if (nrxqs > MVPP2_PORT_MAX_RXQ)
		nrxqs = MVPP2_PORT_MAX_RXQ;

	return nrxqs;
}

337 338 339 340 341
/* Get number of physical egress port */
static inline int mvpp2_egress_port(struct mvpp2_port *port)
{
	return MVPP2_MAX_TCONT + port->id;
}
342

343 344 345 346
/* Get number of physical TXQ */
static inline int mvpp2_txq_phys(int port, int txq)
{
	return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
347 348
}

M
Matteo Croce 已提交
349 350 351
/* Returns a struct page if page_pool is set, otherwise a buffer */
static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool,
			      struct page_pool *page_pool)
352
{
M
Matteo Croce 已提交
353 354 355
	if (page_pool)
		return page_pool_dev_alloc_pages(page_pool);

356 357
	if (likely(pool->frag_size <= PAGE_SIZE))
		return netdev_alloc_frag(pool->frag_size);
M
Matteo Croce 已提交
358 359

	return kmalloc(pool->frag_size, GFP_ATOMIC);
360 361
}

M
Matteo Croce 已提交
362 363
static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool,
			    struct page_pool *page_pool, void *data)
364
{
M
Matteo Croce 已提交
365 366 367
	if (page_pool)
		page_pool_put_full_page(page_pool, virt_to_head_page(data), false);
	else if (likely(pool->frag_size <= PAGE_SIZE))
368 369 370 371 372
		skb_free_frag(data);
	else
		kfree(data);
}

373 374 375
/* Buffer Manager configuration routines */

/* Create pool */
M
Matteo Croce 已提交
376
static int mvpp2_bm_pool_create(struct device *dev, struct mvpp2 *priv,
377 378 379 380
				struct mvpp2_bm_pool *bm_pool, int size)
{
	u32 val;

381 382 383 384 385 386 387 388 389 390 391 392 393 394
	/* Number of buffer pointers must be a multiple of 16, as per
	 * hardware constraints
	 */
	if (!IS_ALIGNED(size, 16))
		return -EINVAL;

	/* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
	 * bytes per buffer pointer
	 */
	if (priv->hw_version == MVPP21)
		bm_pool->size_bytes = 2 * sizeof(u32) * size;
	else
		bm_pool->size_bytes = 2 * sizeof(u64) * size;

M
Matteo Croce 已提交
395
	bm_pool->virt_addr = dma_alloc_coherent(dev, bm_pool->size_bytes,
396
						&bm_pool->dma_addr,
397 398 399 400
						GFP_KERNEL);
	if (!bm_pool->virt_addr)
		return -ENOMEM;

401 402
	if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
			MVPP2_BM_POOL_PTR_ALIGN)) {
M
Matteo Croce 已提交
403
		dma_free_coherent(dev, bm_pool->size_bytes,
404
				  bm_pool->virt_addr, bm_pool->dma_addr);
M
Matteo Croce 已提交
405
		dev_err(dev, "BM pool %d is not %d bytes aligned\n",
406 407 408 409 410
			bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
		return -ENOMEM;
	}

	mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
411
		    lower_32_bits(bm_pool->dma_addr));
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);

	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
	val |= MVPP2_BM_START_MASK;
	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);

	bm_pool->size = size;
	bm_pool->pkt_size = 0;
	bm_pool->buf_num = 0;

	return 0;
}

/* Set pool buffer size */
static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
				      struct mvpp2_bm_pool *bm_pool,
				      int buf_size)
{
	u32 val;

	bm_pool->buf_size = buf_size;

	val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
	mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
}

438 439 440 441 442
static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
				    struct mvpp2_bm_pool *bm_pool,
				    dma_addr_t *dma_addr,
				    phys_addr_t *phys_addr)
{
443
	unsigned int thread = mvpp2_cpu_to_thread(priv, get_cpu());
444

445
	*dma_addr = mvpp2_thread_read(priv, thread,
446
				      MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
447
	*phys_addr = mvpp2_thread_read(priv, thread, MVPP2_BM_VIRT_ALLOC_REG);
448 449 450 451 452

	if (priv->hw_version == MVPP22) {
		u32 val;
		u32 dma_addr_highbits, phys_addr_highbits;

453
		val = mvpp2_thread_read(priv, thread, MVPP22_BM_ADDR_HIGH_ALLOC);
454 455 456 457 458 459 460 461 462 463
		dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
		phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
			MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;

		if (sizeof(dma_addr_t) == 8)
			*dma_addr |= (u64)dma_addr_highbits << 32;

		if (sizeof(phys_addr_t) == 8)
			*phys_addr |= (u64)phys_addr_highbits << 32;
	}
464 465

	put_cpu();
466 467
}

468
/* Free all buffers from the pool */
469
static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
470
			       struct mvpp2_bm_pool *bm_pool, int buf_num)
471
{
M
Matteo Croce 已提交
472
	struct page_pool *pp = NULL;
473 474
	int i;

475 476 477 478 479 480
	if (buf_num > bm_pool->buf_num) {
		WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
		     bm_pool->id, buf_num);
		buf_num = bm_pool->buf_num;
	}

M
Matteo Croce 已提交
481 482 483
	if (priv->percpu_pools)
		pp = priv->page_pool[bm_pool->id];

484
	for (i = 0; i < buf_num; i++) {
485
		dma_addr_t buf_dma_addr;
486 487
		phys_addr_t buf_phys_addr;
		void *data;
488

489 490
		mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
					&buf_dma_addr, &buf_phys_addr);
491

M
Matteo Croce 已提交
492 493 494
		if (!pp)
			dma_unmap_single(dev, buf_dma_addr,
					 bm_pool->buf_size, DMA_FROM_DEVICE);
495

496 497
		data = (void *)phys_to_virt(buf_phys_addr);
		if (!data)
498
			break;
499

M
Matteo Croce 已提交
500
		mvpp2_frag_free(bm_pool, pp, data);
501 502 503 504 505 506
	}

	/* Update BM driver with number of buffers removed from pool */
	bm_pool->buf_num -= i;
}

507
/* Check number of buffers in BM pool */
508
static int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
{
	int buf_num = 0;

	buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
				    MVPP22_BM_POOL_PTRS_NUM_MASK;
	buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
				    MVPP2_BM_BPPI_PTR_NUM_MASK;

	/* HW has one buffer ready which is not reflected in the counters */
	if (buf_num)
		buf_num += 1;

	return buf_num;
}

524
/* Cleanup pool */
M
Matteo Croce 已提交
525
static int mvpp2_bm_pool_destroy(struct device *dev, struct mvpp2 *priv,
526 527
				 struct mvpp2_bm_pool *bm_pool)
{
528
	int buf_num;
529 530
	u32 val;

531
	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
M
Matteo Croce 已提交
532
	mvpp2_bm_bufs_free(dev, priv, bm_pool, buf_num);
533 534 535 536 537 538

	/* Check buffer counters after free */
	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
	if (buf_num) {
		WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
		     bm_pool->id, bm_pool->buf_num);
539 540 541 542 543 544 545
		return 0;
	}

	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
	val |= MVPP2_BM_STOP_MASK;
	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);

M
Matteo Croce 已提交
546
	if (priv->percpu_pools) {
M
Matteo Croce 已提交
547
		page_pool_destroy(priv->page_pool[bm_pool->id]);
M
Matteo Croce 已提交
548 549
		priv->page_pool[bm_pool->id] = NULL;
	}
M
Matteo Croce 已提交
550

M
Matteo Croce 已提交
551
	dma_free_coherent(dev, bm_pool->size_bytes,
552
			  bm_pool->virt_addr,
553
			  bm_pool->dma_addr);
554 555 556
	return 0;
}

M
Matteo Croce 已提交
557
static int mvpp2_bm_pools_init(struct device *dev, struct mvpp2 *priv)
558
{
M
Matteo Croce 已提交
559
	int i, err, size, poolnum = MVPP2_BM_POOLS_NUM;
560 561
	struct mvpp2_bm_pool *bm_pool;

M
Matteo Croce 已提交
562 563 564
	if (priv->percpu_pools)
		poolnum = mvpp2_get_nrxqs(priv) * 2;

565 566
	/* Create all pools with maximum size */
	size = MVPP2_BM_POOL_SIZE_MAX;
M
Matteo Croce 已提交
567
	for (i = 0; i < poolnum; i++) {
568 569
		bm_pool = &priv->bm_pools[i];
		bm_pool->id = i;
M
Matteo Croce 已提交
570
		err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
571 572 573 574 575 576 577
		if (err)
			goto err_unroll_pools;
		mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
	}
	return 0;

err_unroll_pools:
M
Matteo Croce 已提交
578
	dev_err(dev, "failed to create BM pool %d, size %d\n", i, size);
579
	for (i = i - 1; i >= 0; i--)
M
Matteo Croce 已提交
580
		mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
581 582 583
	return err;
}

M
Matteo Croce 已提交
584
static int mvpp2_bm_init(struct device *dev, struct mvpp2 *priv)
585
{
M
Matteo Croce 已提交
586
	enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
M
Matteo Croce 已提交
587
	int i, err, poolnum = MVPP2_BM_POOLS_NUM;
M
Matteo Croce 已提交
588
	struct mvpp2_port *port;
M
Matteo Croce 已提交
589

M
Matteo Croce 已提交
590
	if (priv->percpu_pools) {
M
Matteo Croce 已提交
591 592 593 594 595 596 597 598
		for (i = 0; i < priv->port_count; i++) {
			port = priv->port_list[i];
			if (port->xdp_prog) {
				dma_dir = DMA_BIDIRECTIONAL;
				break;
			}
		}

M
Matteo Croce 已提交
599
		poolnum = mvpp2_get_nrxqs(priv) * 2;
M
Matteo Croce 已提交
600 601 602 603 604 605 606
		for (i = 0; i < poolnum; i++) {
			/* the pool in use */
			int pn = i / (poolnum / 2);

			priv->page_pool[i] =
				mvpp2_create_page_pool(dev,
						       mvpp2_pools[pn].buf_num,
M
Matteo Croce 已提交
607 608
						       mvpp2_pools[pn].pkt_size,
						       dma_dir);
M
Matteo Croce 已提交
609 610 611 612 613 614 615
			if (IS_ERR(priv->page_pool[i])) {
				int j;

				for (j = 0; j < i; j++) {
					page_pool_destroy(priv->page_pool[j]);
					priv->page_pool[j] = NULL;
				}
M
Matteo Croce 已提交
616
				return PTR_ERR(priv->page_pool[i]);
M
Matteo Croce 已提交
617
			}
M
Matteo Croce 已提交
618 619
		}
	}
620

M
Matteo Croce 已提交
621 622 623 624
	dev_info(dev, "using %d %s buffers\n", poolnum,
		 priv->percpu_pools ? "per-cpu" : "shared");

	for (i = 0; i < poolnum; i++) {
625 626 627 628 629 630 631
		/* Mask BM all interrupts */
		mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
		/* Clear BM cause register */
		mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
	}

	/* Allocate and initialize BM pools */
M
Matteo Croce 已提交
632
	priv->bm_pools = devm_kcalloc(dev, poolnum,
633
				      sizeof(*priv->bm_pools), GFP_KERNEL);
634 635 636
	if (!priv->bm_pools)
		return -ENOMEM;

M
Matteo Croce 已提交
637
	err = mvpp2_bm_pools_init(dev, priv);
638 639 640 641 642
	if (err < 0)
		return err;
	return 0;
}

643 644 645 646 647 648 649 650 651
static void mvpp2_setup_bm_pool(void)
{
	/* Short pool */
	mvpp2_pools[MVPP2_BM_SHORT].buf_num  = MVPP2_BM_SHORT_BUF_NUM;
	mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;

	/* Long pool */
	mvpp2_pools[MVPP2_BM_LONG].buf_num  = MVPP2_BM_LONG_BUF_NUM;
	mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
652 653 654 655

	/* Jumbo pool */
	mvpp2_pools[MVPP2_BM_JUMBO].buf_num  = MVPP2_BM_JUMBO_BUF_NUM;
	mvpp2_pools[MVPP2_BM_JUMBO].pkt_size = MVPP2_BM_JUMBO_PKT_SIZE;
656 657
}

658 659 660 661
/* Attach long pool to rxq */
static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
				    int lrxq, int long_pool)
{
662
	u32 val, mask;
663 664 665 666 667
	int prxq;

	/* Get queue physical ID */
	prxq = port->rxqs[lrxq]->id;

668 669 670 671
	if (port->priv->hw_version == MVPP21)
		mask = MVPP21_RXQ_POOL_LONG_MASK;
	else
		mask = MVPP22_RXQ_POOL_LONG_MASK;
672

673 674 675
	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
	val &= ~mask;
	val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
676 677 678 679 680 681 682
	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
}

/* Attach short pool to rxq */
static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
				     int lrxq, int short_pool)
{
683
	u32 val, mask;
684 685 686 687 688
	int prxq;

	/* Get queue physical ID */
	prxq = port->rxqs[lrxq]->id;

689 690 691 692
	if (port->priv->hw_version == MVPP21)
		mask = MVPP21_RXQ_POOL_SHORT_MASK;
	else
		mask = MVPP22_RXQ_POOL_SHORT_MASK;
693

694 695 696
	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
	val &= ~mask;
	val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
697 698 699
	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
}

700 701
static void *mvpp2_buf_alloc(struct mvpp2_port *port,
			     struct mvpp2_bm_pool *bm_pool,
M
Matteo Croce 已提交
702
			     struct page_pool *page_pool,
703
			     dma_addr_t *buf_dma_addr,
704
			     phys_addr_t *buf_phys_addr,
705
			     gfp_t gfp_mask)
706
{
707
	dma_addr_t dma_addr;
M
Matteo Croce 已提交
708
	struct page *page;
709
	void *data;
710

M
Matteo Croce 已提交
711
	data = mvpp2_frag_alloc(bm_pool, page_pool);
712
	if (!data)
713 714
		return NULL;

M
Matteo Croce 已提交
715 716 717 718 719 720 721 722 723 724 725 726
	if (page_pool) {
		page = (struct page *)data;
		dma_addr = page_pool_get_dma_addr(page);
		data = page_to_virt(page);
	} else {
		dma_addr = dma_map_single(port->dev->dev.parent, data,
					  MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
					  DMA_FROM_DEVICE);
		if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
			mvpp2_frag_free(bm_pool, NULL, data);
			return NULL;
		}
727
	}
728
	*buf_dma_addr = dma_addr;
729
	*buf_phys_addr = virt_to_phys(data);
730

731
	return data;
732 733 734 735
}

/* Release buffer to BM */
static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
736
				     dma_addr_t buf_dma_addr,
737
				     phys_addr_t buf_phys_addr)
738
{
739 740 741 742 743
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
	unsigned long flags = 0;

	if (test_bit(thread, &port->priv->lock_map))
		spin_lock_irqsave(&port->bm_lock[thread], flags);
744

745 746 747 748 749 750 751 752 753 754 755 756
	if (port->priv->hw_version == MVPP22) {
		u32 val = 0;

		if (sizeof(dma_addr_t) == 8)
			val |= upper_32_bits(buf_dma_addr) &
				MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;

		if (sizeof(phys_addr_t) == 8)
			val |= (upper_32_bits(buf_phys_addr)
				<< MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
				MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;

757
		mvpp2_thread_write_relaxed(port->priv, thread,
758
					   MVPP22_BM_ADDR_HIGH_RLS_REG, val);
759 760
	}

761 762 763 764 765
	/* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
	 * returned in the "cookie" field of the RX
	 * descriptor. Instead of storing the virtual address, we
	 * store the physical address
	 */
766
	mvpp2_thread_write_relaxed(port->priv, thread,
767
				   MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
768
	mvpp2_thread_write_relaxed(port->priv, thread,
769
				   MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
770

771 772 773
	if (test_bit(thread, &port->priv->lock_map))
		spin_unlock_irqrestore(&port->bm_lock[thread], flags);

774
	put_cpu();
775 776 777 778 779 780 781
}

/* Allocate buffers for the pool */
static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
			     struct mvpp2_bm_pool *bm_pool, int buf_num)
{
	int i, buf_size, total_size;
782
	dma_addr_t dma_addr;
783
	phys_addr_t phys_addr;
M
Matteo Croce 已提交
784
	struct page_pool *pp = NULL;
785
	void *buf;
786

M
Matteo Croce 已提交
787 788 789 790 791 792 793
	if (port->priv->percpu_pools &&
	    bm_pool->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
		netdev_err(port->dev,
			   "attempted to use jumbo frames with per-cpu pools");
		return 0;
	}

794 795 796 797 798 799 800 801 802 803 804
	buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
	total_size = MVPP2_RX_TOTAL_SIZE(buf_size);

	if (buf_num < 0 ||
	    (buf_num + bm_pool->buf_num > bm_pool->size)) {
		netdev_err(port->dev,
			   "cannot allocate %d buffers for pool %d\n",
			   buf_num, bm_pool->id);
		return 0;
	}

M
Matteo Croce 已提交
805 806
	if (port->priv->percpu_pools)
		pp = port->priv->page_pool[bm_pool->id];
807
	for (i = 0; i < buf_num; i++) {
M
Matteo Croce 已提交
808
		buf = mvpp2_buf_alloc(port, bm_pool, pp, &dma_addr,
809
				      &phys_addr, GFP_KERNEL);
810
		if (!buf)
811 812
			break;

813
		mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
814
				  phys_addr);
815 816 817 818 819 820
	}

	/* Update BM driver with number of buffers added to pool */
	bm_pool->buf_num += i;

	netdev_dbg(port->dev,
821
		   "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
822 823 824
		   bm_pool->id, bm_pool->pkt_size, buf_size, total_size);

	netdev_dbg(port->dev,
825
		   "pool %d: %d of %d buffers added\n",
826 827 828 829 830 831 832 833
		   bm_pool->id, i, buf_num);
	return i;
}

/* Notify the driver that BM pool is being used as specific type and return the
 * pool pointer on success
 */
static struct mvpp2_bm_pool *
834
mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
835 836 837 838
{
	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
	int num;

M
Matteo Croce 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	if ((port->priv->percpu_pools && pool > mvpp2_get_nrxqs(port->priv) * 2) ||
	    (!port->priv->percpu_pools && pool >= MVPP2_BM_POOLS_NUM)) {
		netdev_err(port->dev, "Invalid pool %d\n", pool);
		return NULL;
	}

	/* Allocate buffers in case BM pool is used as long pool, but packet
	 * size doesn't match MTU or BM pool hasn't being used yet
	 */
	if (new_pool->pkt_size == 0) {
		int pkts_num;

		/* Set default buffer number or free all the buffers in case
		 * the pool is not empty
		 */
		pkts_num = new_pool->buf_num;
		if (pkts_num == 0) {
			if (port->priv->percpu_pools) {
				if (pool < port->nrxqs)
					pkts_num = mvpp2_pools[MVPP2_BM_SHORT].buf_num;
				else
					pkts_num = mvpp2_pools[MVPP2_BM_LONG].buf_num;
			} else {
				pkts_num = mvpp2_pools[pool].buf_num;
			}
		} else {
			mvpp2_bm_bufs_free(port->dev->dev.parent,
					   port->priv, new_pool, pkts_num);
		}

		new_pool->pkt_size = pkt_size;
		new_pool->frag_size =
			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
			MVPP2_SKB_SHINFO_SIZE;

		/* Allocate buffers for this pool */
		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
		if (num != pkts_num) {
			WARN(1, "pool %d: %d of %d allocated\n",
			     new_pool->id, num, pkts_num);
			return NULL;
		}
	}

	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));

	return new_pool;
}

static struct mvpp2_bm_pool *
mvpp2_bm_pool_use_percpu(struct mvpp2_port *port, int type,
			 unsigned int pool, int pkt_size)
{
	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
	int num;

	if (pool > port->nrxqs * 2) {
897
		netdev_err(port->dev, "Invalid pool %d\n", pool);
898 899 900 901 902 903
		return NULL;
	}

	/* Allocate buffers in case BM pool is used as long pool, but packet
	 * size doesn't match MTU or BM pool hasn't being used yet
	 */
904
	if (new_pool->pkt_size == 0) {
905 906 907 908 909 910 911
		int pkts_num;

		/* Set default buffer number or free all the buffers in case
		 * the pool is not empty
		 */
		pkts_num = new_pool->buf_num;
		if (pkts_num == 0)
M
Matteo Croce 已提交
912
			pkts_num = mvpp2_pools[type].buf_num;
913
		else
914
			mvpp2_bm_bufs_free(port->dev->dev.parent,
915
					   port->priv, new_pool, pkts_num);
916 917

		new_pool->pkt_size = pkt_size;
918 919 920
		new_pool->frag_size =
			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
			MVPP2_SKB_SHINFO_SIZE;
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

		/* Allocate buffers for this pool */
		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
		if (num != pkts_num) {
			WARN(1, "pool %d: %d of %d allocated\n",
			     new_pool->id, num, pkts_num);
			return NULL;
		}
	}

	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));

	return new_pool;
}

M
Matteo Croce 已提交
937 938
/* Initialize pools for swf, shared buffers variant */
static int mvpp2_swf_bm_pool_init_shared(struct mvpp2_port *port)
939
{
940
	enum mvpp2_bm_pool_log_num long_log_pool, short_log_pool;
M
Matteo Croce 已提交
941
	int rxq;
942 943 944 945 946 947 948 949 950 951 952 953

	/* If port pkt_size is higher than 1518B:
	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
	 */
	if (port->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
		long_log_pool = MVPP2_BM_JUMBO;
		short_log_pool = MVPP2_BM_LONG;
	} else {
		long_log_pool = MVPP2_BM_LONG;
		short_log_pool = MVPP2_BM_SHORT;
	}
954 955 956

	if (!port->pool_long) {
		port->pool_long =
957 958
			mvpp2_bm_pool_use(port, long_log_pool,
					  mvpp2_pools[long_log_pool].pkt_size);
959 960 961
		if (!port->pool_long)
			return -ENOMEM;

962
		port->pool_long->port_map |= BIT(port->id);
963

964
		for (rxq = 0; rxq < port->nrxqs; rxq++)
965 966 967 968 969
			mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
	}

	if (!port->pool_short) {
		port->pool_short =
970
			mvpp2_bm_pool_use(port, short_log_pool,
971
					  mvpp2_pools[short_log_pool].pkt_size);
972 973 974
		if (!port->pool_short)
			return -ENOMEM;

975
		port->pool_short->port_map |= BIT(port->id);
976

977
		for (rxq = 0; rxq < port->nrxqs; rxq++)
978 979 980 981 982 983 984
			mvpp2_rxq_short_pool_set(port, rxq,
						 port->pool_short->id);
	}

	return 0;
}

M
Matteo Croce 已提交
985 986 987
/* Initialize pools for swf, percpu buffers variant */
static int mvpp2_swf_bm_pool_init_percpu(struct mvpp2_port *port)
{
988
	struct mvpp2_bm_pool *bm_pool;
M
Matteo Croce 已提交
989 990 991
	int i;

	for (i = 0; i < port->nrxqs; i++) {
992 993 994
		bm_pool = mvpp2_bm_pool_use_percpu(port, MVPP2_BM_SHORT, i,
						   mvpp2_pools[MVPP2_BM_SHORT].pkt_size);
		if (!bm_pool)
M
Matteo Croce 已提交
995 996
			return -ENOMEM;

997 998
		bm_pool->port_map |= BIT(port->id);
		mvpp2_rxq_short_pool_set(port, i, bm_pool->id);
M
Matteo Croce 已提交
999 1000 1001
	}

	for (i = 0; i < port->nrxqs; i++) {
1002 1003 1004
		bm_pool = mvpp2_bm_pool_use_percpu(port, MVPP2_BM_LONG, i + port->nrxqs,
						   mvpp2_pools[MVPP2_BM_LONG].pkt_size);
		if (!bm_pool)
M
Matteo Croce 已提交
1005 1006
			return -ENOMEM;

1007 1008
		bm_pool->port_map |= BIT(port->id);
		mvpp2_rxq_long_pool_set(port, i, bm_pool->id);
M
Matteo Croce 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	}

	port->pool_long = NULL;
	port->pool_short = NULL;

	return 0;
}

static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
{
	if (port->priv->percpu_pools)
		return mvpp2_swf_bm_pool_init_percpu(port);
	else
		return mvpp2_swf_bm_pool_init_shared(port);
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static void mvpp2_set_hw_csum(struct mvpp2_port *port,
			      enum mvpp2_bm_pool_log_num new_long_pool)
{
	const netdev_features_t csums = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;

	/* Update L4 checksum when jumbo enable/disable on port.
	 * Only port 0 supports hardware checksum offload due to
	 * the Tx FIFO size limitation.
	 * Also, don't set NETIF_F_HW_CSUM because L3_offset in TX descriptor
	 * has 7 bits, so the maximum L3 offset is 128.
	 */
	if (new_long_pool == MVPP2_BM_JUMBO && port->id != 0) {
		port->dev->features &= ~csums;
		port->dev->hw_features &= ~csums;
	} else {
		port->dev->features |= csums;
		port->dev->hw_features |= csums;
	}
}

1045 1046 1047
static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
{
	struct mvpp2_port *port = netdev_priv(dev);
1048 1049
	enum mvpp2_bm_pool_log_num new_long_pool;
	int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
1050

M
Matteo Croce 已提交
1051 1052 1053
	if (port->priv->percpu_pools)
		goto out_set;

1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	/* If port MTU is higher than 1518B:
	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
	 */
	if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
		new_long_pool = MVPP2_BM_JUMBO;
	else
		new_long_pool = MVPP2_BM_LONG;

	if (new_long_pool != port->pool_long->id) {
		/* Remove port from old short & long pool */
		port->pool_long = mvpp2_bm_pool_use(port, port->pool_long->id,
						    port->pool_long->pkt_size);
		port->pool_long->port_map &= ~BIT(port->id);
		port->pool_long = NULL;

		port->pool_short = mvpp2_bm_pool_use(port, port->pool_short->id,
						     port->pool_short->pkt_size);
		port->pool_short->port_map &= ~BIT(port->id);
		port->pool_short = NULL;

		port->pkt_size =  pkt_size;

		/* Add port to new short & long pool */
		mvpp2_swf_bm_pool_init(port);

1080
		mvpp2_set_hw_csum(port, new_long_pool);
1081 1082
	}

M
Matteo Croce 已提交
1083
out_set:
1084
	dev->mtu = mtu;
1085 1086
	dev->wanted_features = dev->features;

1087 1088 1089 1090 1091 1092
	netdev_update_features(dev);
	return 0;
}

static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
{
1093 1094 1095 1096
	int i, sw_thread_mask = 0;

	for (i = 0; i < port->nqvecs; i++)
		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
1097 1098

	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1099
		    MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
1100 1101 1102 1103
}

static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
{
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	int i, sw_thread_mask = 0;

	for (i = 0; i < port->nqvecs; i++)
		sw_thread_mask |= port->qvecs[i].sw_thread_mask;

	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
		    MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
}

static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
{
	struct mvpp2_port *port = qvec->port;
1116 1117

	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1118 1119 1120 1121 1122 1123 1124 1125 1126
		    MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
}

static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
{
	struct mvpp2_port *port = qvec->port;

	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
		    MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
1127 1128
}

1129
/* Mask the current thread's Rx/Tx interrupts
1130 1131 1132
 * Called by on_each_cpu(), guaranteed to run with migration disabled,
 * using smp_processor_id() is OK.
 */
1133 1134 1135 1136
static void mvpp2_interrupts_mask(void *arg)
{
	struct mvpp2_port *port = arg;

1137 1138 1139 1140
	/* If the thread isn't used, don't do anything */
	if (smp_processor_id() > port->priv->nthreads)
		return;

1141
	mvpp2_thread_write(port->priv,
1142
			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1143
			   MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
1144 1145
}

1146
/* Unmask the current thread's Rx/Tx interrupts.
1147 1148 1149
 * Called by on_each_cpu(), guaranteed to run with migration disabled,
 * using smp_processor_id() is OK.
 */
1150 1151 1152
static void mvpp2_interrupts_unmask(void *arg)
{
	struct mvpp2_port *port = arg;
1153 1154
	u32 val;

1155 1156 1157 1158
	/* If the thread isn't used, don't do anything */
	if (smp_processor_id() > port->priv->nthreads)
		return;

1159
	val = MVPP2_CAUSE_MISC_SUM_MASK |
1160
		MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
1161 1162
	if (port->has_tx_irqs)
		val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
1163

1164
	mvpp2_thread_write(port->priv,
1165
			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
			   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
}

static void
mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
{
	u32 val;
	int i;

	if (port->priv->hw_version != MVPP22)
		return;

	if (mask)
		val = 0;
	else
1181
		val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(MVPP22);
1182 1183 1184 1185 1186 1187 1188

	for (i = 0; i < port->nqvecs; i++) {
		struct mvpp2_queue_vector *v = port->qvecs + i;

		if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
			continue;

1189
		mvpp2_thread_write(port->priv, v->sw_thread_id,
1190 1191
				   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
	}
1192 1193
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
/* Only GOP port 0 has an XLG MAC */
static bool mvpp2_port_supports_xlg(struct mvpp2_port *port)
{
	return port->gop_id == 0;
}

static bool mvpp2_port_supports_rgmii(struct mvpp2_port *port)
{
	return !(port->priv->hw_version == MVPP22 && port->gop_id == 0);
}

1205
/* Port configuration routines */
1206 1207
static bool mvpp2_is_xlg(phy_interface_t interface)
{
1208
	return interface == PHY_INTERFACE_MODE_10GBASER ||
1209 1210
	       interface == PHY_INTERFACE_MODE_XAUI;
}
1211

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
static void mvpp2_modify(void __iomem *ptr, u32 mask, u32 set)
{
	u32 old, val;

	old = val = readl(ptr);
	val &= ~mask;
	val |= set;
	if (old != val)
		writel(val, ptr);
}

A
Antoine Ténart 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	u32 val;

	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);

	regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
	if (port->gop_id == 2)
		val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
	else if (port->gop_id == 3)
		val |= GENCONF_CTRL0_PORT1_RGMII_MII;
	regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
}

static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	u32 val;

	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
	       GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);

	if (port->gop_id > 1) {
		regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
		if (port->gop_id == 2)
			val &= ~GENCONF_CTRL0_PORT0_RGMII;
		else if (port->gop_id == 3)
			val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
		regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
	}
}

static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
	void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
	u32 val;

	val = readl(xpcs + MVPP22_XPCS_CFG0);
	val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
		 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
	val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
	writel(val, xpcs + MVPP22_XPCS_CFG0);

	val = readl(mpcs + MVPP22_MPCS_CTRL);
	val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
	writel(val, mpcs + MVPP22_MPCS_CTRL);

	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1278
	val &= ~MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7);
A
Antoine Ténart 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
}

static int mvpp22_gop_init(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	u32 val;

	if (!priv->sysctrl_base)
		return 0;

	switch (port->phy_interface) {
	case PHY_INTERFACE_MODE_RGMII:
	case PHY_INTERFACE_MODE_RGMII_ID:
	case PHY_INTERFACE_MODE_RGMII_RXID:
	case PHY_INTERFACE_MODE_RGMII_TXID:
1296
		if (!mvpp2_port_supports_rgmii(port))
A
Antoine Ténart 已提交
1297 1298 1299 1300
			goto invalid_conf;
		mvpp22_gop_init_rgmii(port);
		break;
	case PHY_INTERFACE_MODE_SGMII:
A
Antoine Tenart 已提交
1301
	case PHY_INTERFACE_MODE_1000BASEX:
A
Antoine Tenart 已提交
1302
	case PHY_INTERFACE_MODE_2500BASEX:
A
Antoine Ténart 已提交
1303 1304
		mvpp22_gop_init_sgmii(port);
		break;
1305
	case PHY_INTERFACE_MODE_10GBASER:
1306
		if (!mvpp2_port_supports_xlg(port))
A
Antoine Ténart 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
			goto invalid_conf;
		mvpp22_gop_init_10gkr(port);
		break;
	default:
		goto unsupported_conf;
	}

	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
	val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
	       GENCONF_PORT_CTRL1_EN(port->gop_id);
	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);

	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
	val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);

	regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
	val |= GENCONF_SOFT_RESET1_GOP;
	regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);

unsupported_conf:
	return 0;

invalid_conf:
	netdev_err(port->dev, "Invalid port configuration\n");
	return -EINVAL;
}

1335 1336 1337 1338 1339
static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
{
	u32 val;

	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1340 1341
	    phy_interface_mode_is_8023z(port->phy_interface) ||
	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1342 1343 1344 1345 1346 1347
		/* Enable the GMAC link status irq for this port */
		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
		val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
	}

1348
	if (mvpp2_port_supports_xlg(port)) {
1349 1350
		/* Enable the XLG/GIG irqs for this port */
		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1351
		if (mvpp2_is_xlg(port->phy_interface))
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
			val |= MVPP22_XLG_EXT_INT_MASK_XLG;
		else
			val |= MVPP22_XLG_EXT_INT_MASK_GIG;
		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
	}
}

static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
{
	u32 val;

1363
	if (mvpp2_port_supports_xlg(port)) {
1364 1365
		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
		val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
1366
			 MVPP22_XLG_EXT_INT_MASK_GIG);
1367 1368 1369 1370
		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
	}

	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1371 1372
	    phy_interface_mode_is_8023z(port->phy_interface) ||
	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
		val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
	}
}

static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
{
	u32 val;

1383 1384 1385 1386
	mvpp2_modify(port->base + MVPP22_GMAC_INT_SUM_MASK,
		     MVPP22_GMAC_INT_SUM_MASK_PTP,
		     MVPP22_GMAC_INT_SUM_MASK_PTP);

1387 1388
	if (port->phylink ||
	    phy_interface_mode_is_rgmii(port->phy_interface) ||
1389 1390
	    phy_interface_mode_is_8023z(port->phy_interface) ||
	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1391 1392 1393 1394 1395
		val = readl(port->base + MVPP22_GMAC_INT_MASK);
		val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
		writel(val, port->base + MVPP22_GMAC_INT_MASK);
	}

1396
	if (mvpp2_port_supports_xlg(port)) {
1397 1398 1399
		val = readl(port->base + MVPP22_XLG_INT_MASK);
		val |= MVPP22_XLG_INT_MASK_LINK;
		writel(val, port->base + MVPP22_XLG_INT_MASK);
1400 1401 1402 1403

		mvpp2_modify(port->base + MVPP22_XLG_EXT_INT_MASK,
			     MVPP22_XLG_EXT_INT_MASK_PTP,
			     MVPP22_XLG_EXT_INT_MASK_PTP);
1404 1405 1406 1407 1408
	}

	mvpp22_gop_unmask_irq(port);
}

A
Antoine Tenart 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
/* Sets the PHY mode of the COMPHY (which configures the serdes lanes).
 *
 * The PHY mode used by the PPv2 driver comes from the network subsystem, while
 * the one given to the COMPHY comes from the generic PHY subsystem. Hence they
 * differ.
 *
 * The COMPHY configures the serdes lanes regardless of the actual use of the
 * lanes by the physical layer. This is why configurations like
 * "PPv2 (2500BaseX) - COMPHY (2500SGMII)" are valid.
 */
1419 1420 1421 1422 1423 1424 1425
static int mvpp22_comphy_init(struct mvpp2_port *port)
{
	int ret;

	if (!port->comphy)
		return 0;

1426 1427
	ret = phy_set_mode_ext(port->comphy, PHY_MODE_ETHERNET,
			       port->phy_interface);
1428 1429 1430 1431 1432 1433
	if (ret)
		return ret;

	return phy_power_on(port->comphy);
}

1434 1435 1436 1437
static void mvpp2_port_enable(struct mvpp2_port *port)
{
	u32 val;

1438 1439
	if (mvpp2_port_supports_xlg(port) &&
	    mvpp2_is_xlg(port->phy_interface)) {
1440
		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1441
		val |= MVPP22_XLG_CTRL0_PORT_EN;
1442 1443 1444 1445 1446 1447 1448 1449
		val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
	} else {
		val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
		val |= MVPP2_GMAC_PORT_EN_MASK;
		val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
		writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
	}
1450 1451 1452 1453 1454 1455
}

static void mvpp2_port_disable(struct mvpp2_port *port)
{
	u32 val;

1456 1457
	if (mvpp2_port_supports_xlg(port) &&
	    mvpp2_is_xlg(port->phy_interface)) {
1458
		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
A
Antoine Tenart 已提交
1459 1460
		val &= ~MVPP22_XLG_CTRL0_PORT_EN;
		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1461
	}
1462 1463 1464 1465

	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
	val &= ~(MVPP2_GMAC_PORT_EN_MASK);
	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
}

/* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
{
	u32 val;

	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
		    ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
}

/* Configure loopback port */
A
Antoine Tenart 已提交
1479 1480
static void mvpp2_port_loopback_set(struct mvpp2_port *port,
				    const struct phylink_link_state *state)
1481 1482 1483 1484 1485
{
	u32 val;

	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);

A
Antoine Tenart 已提交
1486
	if (state->speed == 1000)
1487 1488 1489 1490
		val |= MVPP2_GMAC_GMII_LB_EN_MASK;
	else
		val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;

1491 1492
	if (phy_interface_mode_is_8023z(state->interface) ||
	    state->interface == PHY_INTERFACE_MODE_SGMII)
1493 1494 1495 1496 1497 1498 1499
		val |= MVPP2_GMAC_PCS_LB_EN_MASK;
	else
		val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;

	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
}

S
Sven Auhagen 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
enum {
	ETHTOOL_XDP_REDIRECT,
	ETHTOOL_XDP_PASS,
	ETHTOOL_XDP_DROP,
	ETHTOOL_XDP_TX,
	ETHTOOL_XDP_TX_ERR,
	ETHTOOL_XDP_XMIT,
	ETHTOOL_XDP_XMIT_ERR,
};

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
struct mvpp2_ethtool_counter {
	unsigned int offset;
	const char string[ETH_GSTRING_LEN];
	bool reg_is_64b;
};

static u64 mvpp2_read_count(struct mvpp2_port *port,
			    const struct mvpp2_ethtool_counter *counter)
{
	u64 val;

	val = readl(port->stats_base + counter->offset);
	if (counter->reg_is_64b)
		val += (u64)readl(port->stats_base + counter->offset + 4) << 32;

	return val;
}

1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
/* Some counters are accessed indirectly by first writing an index to
 * MVPP2_CTRS_IDX. The index can represent various resources depending on the
 * register we access, it can be a hit counter for some classification tables,
 * a counter specific to a rxq, a txq or a buffer pool.
 */
static u32 mvpp2_read_index(struct mvpp2 *priv, u32 index, u32 reg)
{
	mvpp2_write(priv, MVPP2_CTRS_IDX, index);
	return mvpp2_read(priv, reg);
}

1539 1540 1541 1542 1543 1544 1545 1546 1547
/* Due to the fact that software statistics and hardware statistics are, by
 * design, incremented at different moments in the chain of packet processing,
 * it is very likely that incoming packets could have been dropped after being
 * counted by hardware but before reaching software statistics (most probably
 * multicast packets), and in the oppposite way, during transmission, FCS bytes
 * are added in between as well as TSO skb will be split and header bytes added.
 * Hence, statistics gathered from userspace with ifconfig (software) and
 * ethtool (hardware) cannot be compared.
 */
1548
static const struct mvpp2_ethtool_counter mvpp2_ethtool_mib_regs[] = {
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	{ MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
	{ MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
	{ MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
	{ MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
	{ MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
	{ MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
	{ MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
	{ MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
	{ MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
	{ MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
	{ MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
	{ MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
	{ MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
	{ MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
	{ MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
	{ MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
	{ MVPP2_MIB_FC_SENT, "fc_sent" },
	{ MVPP2_MIB_FC_RCVD, "fc_received" },
	{ MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
	{ MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
	{ MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
	{ MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
	{ MVPP2_MIB_JABBER_RCVD, "jabber_received" },
	{ MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
	{ MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
	{ MVPP2_MIB_COLLISION, "collision" },
	{ MVPP2_MIB_LATE_COLLISION, "late_collision" },
};

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
static const struct mvpp2_ethtool_counter mvpp2_ethtool_port_regs[] = {
	{ MVPP2_OVERRUN_ETH_DROP, "rx_fifo_or_parser_overrun_drops" },
	{ MVPP2_CLS_ETH_DROP, "rx_classifier_drops" },
};

static const struct mvpp2_ethtool_counter mvpp2_ethtool_txq_regs[] = {
	{ MVPP2_TX_DESC_ENQ_CTR, "txq_%d_desc_enqueue" },
	{ MVPP2_TX_DESC_ENQ_TO_DDR_CTR, "txq_%d_desc_enqueue_to_ddr" },
	{ MVPP2_TX_BUFF_ENQ_TO_DDR_CTR, "txq_%d_buff_euqueue_to_ddr" },
	{ MVPP2_TX_DESC_ENQ_HW_FWD_CTR, "txq_%d_desc_hardware_forwarded" },
	{ MVPP2_TX_PKTS_DEQ_CTR, "txq_%d_packets_dequeued" },
	{ MVPP2_TX_PKTS_FULL_QUEUE_DROP_CTR, "txq_%d_queue_full_drops" },
	{ MVPP2_TX_PKTS_EARLY_DROP_CTR, "txq_%d_packets_early_drops" },
	{ MVPP2_TX_PKTS_BM_DROP_CTR, "txq_%d_packets_bm_drops" },
	{ MVPP2_TX_PKTS_BM_MC_DROP_CTR, "txq_%d_packets_rep_bm_drops" },
};

static const struct mvpp2_ethtool_counter mvpp2_ethtool_rxq_regs[] = {
	{ MVPP2_RX_DESC_ENQ_CTR, "rxq_%d_desc_enqueue" },
	{ MVPP2_RX_PKTS_FULL_QUEUE_DROP_CTR, "rxq_%d_queue_full_drops" },
	{ MVPP2_RX_PKTS_EARLY_DROP_CTR, "rxq_%d_packets_early_drops" },
	{ MVPP2_RX_PKTS_BM_DROP_CTR, "rxq_%d_packets_bm_drops" },
};

S
Sven Auhagen 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
static const struct mvpp2_ethtool_counter mvpp2_ethtool_xdp[] = {
	{ ETHTOOL_XDP_REDIRECT, "rx_xdp_redirect", },
	{ ETHTOOL_XDP_PASS, "rx_xdp_pass", },
	{ ETHTOOL_XDP_DROP, "rx_xdp_drop", },
	{ ETHTOOL_XDP_TX, "rx_xdp_tx", },
	{ ETHTOOL_XDP_TX_ERR, "rx_xdp_tx_errors", },
	{ ETHTOOL_XDP_XMIT, "tx_xdp_xmit", },
	{ ETHTOOL_XDP_XMIT_ERR, "tx_xdp_xmit_errors", },
};

1612 1613 1614
#define MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs)	(ARRAY_SIZE(mvpp2_ethtool_mib_regs) + \
						 ARRAY_SIZE(mvpp2_ethtool_port_regs) + \
						 (ARRAY_SIZE(mvpp2_ethtool_txq_regs) * (ntxqs)) + \
S
Sven Auhagen 已提交
1615 1616
						 (ARRAY_SIZE(mvpp2_ethtool_rxq_regs) * (nrxqs)) + \
						 ARRAY_SIZE(mvpp2_ethtool_xdp))
1617

1618 1619 1620
static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
				      u8 *data)
{
1621 1622
	struct mvpp2_port *port = netdev_priv(netdev);
	int i, q;
1623

1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
	if (sset != ETH_SS_STATS)
		return;

	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++) {
		strscpy(data, mvpp2_ethtool_mib_regs[i].string,
			ETH_GSTRING_LEN);
		data += ETH_GSTRING_LEN;
	}

	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++) {
		strscpy(data, mvpp2_ethtool_port_regs[i].string,
			ETH_GSTRING_LEN);
		data += ETH_GSTRING_LEN;
	}

	for (q = 0; q < port->ntxqs; q++) {
		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++) {
			snprintf(data, ETH_GSTRING_LEN,
				 mvpp2_ethtool_txq_regs[i].string, q);
			data += ETH_GSTRING_LEN;
		}
	}

	for (q = 0; q < port->nrxqs; q++) {
		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++) {
			snprintf(data, ETH_GSTRING_LEN,
				 mvpp2_ethtool_rxq_regs[i].string,
				 q);
			data += ETH_GSTRING_LEN;
		}
1654
	}
S
Sven Auhagen 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699

	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_xdp); i++) {
		strscpy(data, mvpp2_ethtool_xdp[i].string,
			ETH_GSTRING_LEN);
		data += ETH_GSTRING_LEN;
	}
}

static void
mvpp2_get_xdp_stats(struct mvpp2_port *port, struct mvpp2_pcpu_stats *xdp_stats)
{
	unsigned int start;
	unsigned int cpu;

	/* Gather XDP Statistics */
	for_each_possible_cpu(cpu) {
		struct mvpp2_pcpu_stats *cpu_stats;
		u64	xdp_redirect;
		u64	xdp_pass;
		u64	xdp_drop;
		u64	xdp_xmit;
		u64	xdp_xmit_err;
		u64	xdp_tx;
		u64	xdp_tx_err;

		cpu_stats = per_cpu_ptr(port->stats, cpu);
		do {
			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
			xdp_redirect = cpu_stats->xdp_redirect;
			xdp_pass   = cpu_stats->xdp_pass;
			xdp_drop = cpu_stats->xdp_drop;
			xdp_xmit   = cpu_stats->xdp_xmit;
			xdp_xmit_err   = cpu_stats->xdp_xmit_err;
			xdp_tx   = cpu_stats->xdp_tx;
			xdp_tx_err   = cpu_stats->xdp_tx_err;
		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));

		xdp_stats->xdp_redirect += xdp_redirect;
		xdp_stats->xdp_pass   += xdp_pass;
		xdp_stats->xdp_drop += xdp_drop;
		xdp_stats->xdp_xmit   += xdp_xmit;
		xdp_stats->xdp_xmit_err   += xdp_xmit_err;
		xdp_stats->xdp_tx   += xdp_tx;
		xdp_stats->xdp_tx_err   += xdp_tx_err;
	}
1700 1701
}

1702 1703
static void mvpp2_read_stats(struct mvpp2_port *port)
{
S
Sven Auhagen 已提交
1704 1705
	struct mvpp2_pcpu_stats xdp_stats = {};
	const struct mvpp2_ethtool_counter *s;
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
	u64 *pstats;
	int i, q;

	pstats = port->ethtool_stats;

	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++)
		*pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_mib_regs[i]);

	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++)
		*pstats++ += mvpp2_read(port->priv,
					mvpp2_ethtool_port_regs[i].offset +
					4 * port->id);

	for (q = 0; q < port->ntxqs; q++)
		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++)
			*pstats++ += mvpp2_read_index(port->priv,
S
Sven Auhagen 已提交
1722
						      MVPP22_CTRS_TX_CTR(port->id, q),
1723 1724 1725 1726 1727 1728 1729 1730
						      mvpp2_ethtool_txq_regs[i].offset);

	/* Rxqs are numbered from 0 from the user standpoint, but not from the
	 * driver's. We need to add the  port->first_rxq offset.
	 */
	for (q = 0; q < port->nrxqs; q++)
		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++)
			*pstats++ += mvpp2_read_index(port->priv,
S
Sven Auhagen 已提交
1731
						      port->first_rxq + q,
1732
						      mvpp2_ethtool_rxq_regs[i].offset);
S
Sven Auhagen 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

	/* Gather XDP Statistics */
	mvpp2_get_xdp_stats(port, &xdp_stats);

	for (i = 0, s = mvpp2_ethtool_xdp;
		 s < mvpp2_ethtool_xdp + ARRAY_SIZE(mvpp2_ethtool_xdp);
	     s++, i++) {
		switch (s->offset) {
		case ETHTOOL_XDP_REDIRECT:
			*pstats++ = xdp_stats.xdp_redirect;
			break;
		case ETHTOOL_XDP_PASS:
			*pstats++ = xdp_stats.xdp_pass;
			break;
		case ETHTOOL_XDP_DROP:
			*pstats++ = xdp_stats.xdp_drop;
			break;
		case ETHTOOL_XDP_TX:
			*pstats++ = xdp_stats.xdp_tx;
			break;
		case ETHTOOL_XDP_TX_ERR:
			*pstats++ = xdp_stats.xdp_tx_err;
			break;
		case ETHTOOL_XDP_XMIT:
			*pstats++ = xdp_stats.xdp_xmit;
			break;
		case ETHTOOL_XDP_XMIT_ERR:
			*pstats++ = xdp_stats.xdp_xmit_err;
			break;
		}
	}
1764 1765
}

1766 1767 1768
static void mvpp2_gather_hw_statistics(struct work_struct *work)
{
	struct delayed_work *del_work = to_delayed_work(work);
1769 1770
	struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
					       stats_work);
1771

1772
	mutex_lock(&port->gather_stats_lock);
1773

1774
	mvpp2_read_stats(port);
1775 1776 1777 1778

	/* No need to read again the counters right after this function if it
	 * was called asynchronously by the user (ie. use of ethtool).
	 */
1779 1780
	cancel_delayed_work(&port->stats_work);
	queue_delayed_work(port->priv->stats_queue, &port->stats_work,
1781 1782
			   MVPP2_MIB_COUNTERS_STATS_DELAY);

1783
	mutex_unlock(&port->gather_stats_lock);
1784 1785 1786 1787 1788 1789 1790
}

static void mvpp2_ethtool_get_stats(struct net_device *dev,
				    struct ethtool_stats *stats, u64 *data)
{
	struct mvpp2_port *port = netdev_priv(dev);

1791 1792 1793 1794
	/* Update statistics for the given port, then take the lock to avoid
	 * concurrent accesses on the ethtool_stats structure during its copy.
	 */
	mvpp2_gather_hw_statistics(&port->stats_work.work);
1795

1796
	mutex_lock(&port->gather_stats_lock);
1797
	memcpy(data, port->ethtool_stats,
1798
	       sizeof(u64) * MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs));
1799
	mutex_unlock(&port->gather_stats_lock);
1800 1801 1802 1803
}

static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
{
1804 1805
	struct mvpp2_port *port = netdev_priv(dev);

1806
	if (sset == ETH_SS_STATS)
1807
		return MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs);
1808 1809 1810 1811

	return -EOPNOTSUPP;
}

1812
static void mvpp2_mac_reset_assert(struct mvpp2_port *port)
1813
{
1814
	u32 val;
1815

1816 1817
	val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) |
	      MVPP2_GMAC_PORT_RESET_MASK;
1818
	writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
1819 1820 1821 1822 1823 1824

	if (port->priv->hw_version == MVPP22 && port->gop_id == 0) {
		val = readl(port->base + MVPP22_XLG_CTRL0_REG) &
		      ~MVPP22_XLG_CTRL0_MAC_RESET_DIS;
		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
	}
1825 1826
}

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
static void mvpp22_pcs_reset_assert(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	void __iomem *mpcs, *xpcs;
	u32 val;

	if (port->priv->hw_version != MVPP22 || port->gop_id != 0)
		return;

	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);

	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
	val &= ~(MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
	val |= MVPP22_MPCS_CLK_RESET_DIV_SET;
	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);

	val = readl(xpcs + MVPP22_XPCS_CFG0);
	writel(val & ~MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
}

static void mvpp22_pcs_reset_deassert(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	void __iomem *mpcs, *xpcs;
	u32 val;

	if (port->priv->hw_version != MVPP22 || port->gop_id != 0)
		return;

	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);

	switch (port->phy_interface) {
1861
	case PHY_INTERFACE_MODE_10GBASER:
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
		val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
		val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX |
		       MAC_CLK_RESET_SD_TX;
		val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
		writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
		break;
	case PHY_INTERFACE_MODE_XAUI:
	case PHY_INTERFACE_MODE_RXAUI:
		val = readl(xpcs + MVPP22_XPCS_CFG0);
		writel(val | MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
		break;
	default:
		break;
	}
}

1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
/* Change maximum receive size of the port */
static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
{
	u32 val;

	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
	val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
	val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
		    MVPP2_GMAC_MAX_RX_SIZE_OFFS);
	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
}

1890 1891 1892 1893 1894 1895 1896 1897
/* Change maximum receive size of the port */
static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
{
	u32 val;

	val =  readl(port->base + MVPP22_XLG_CTRL1_REG);
	val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
	val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
1898
	       MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
1899 1900 1901
	writel(val, port->base + MVPP22_XLG_CTRL1_REG);
}

1902 1903 1904
/* Set defaults to the MVPP2 port */
static void mvpp2_defaults_set(struct mvpp2_port *port)
{
1905
	int tx_port_num, val, queue, lrxq;
1906

1907 1908 1909 1910 1911 1912 1913 1914
	if (port->priv->hw_version == MVPP21) {
		/* Update TX FIFO MIN Threshold */
		val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
		val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
		/* Min. TX threshold must be less than minimal packet length */
		val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
		writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
	}
1915 1916 1917 1918 1919 1920 1921

	/* Disable Legacy WRR, Disable EJP, Release from reset */
	tx_port_num = mvpp2_egress_port(port);
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
		    tx_port_num);
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);

1922 1923 1924
	/* Set TXQ scheduling to Round-Robin */
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_FIXED_PRIO_REG, 0);

1925
	/* Close bandwidth for all queues */
1926
	for (queue = 0; queue < MVPP2_MAX_TXQ; queue++)
1927
		mvpp2_write(port->priv,
1928
			    MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(queue), 0);
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

	/* Set refill period to 1 usec, refill tokens
	 * and bucket size to maximum
	 */
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
		    port->priv->tclk / USEC_PER_SEC);
	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
	val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
	val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
	val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
	val = MVPP2_TXP_TOKEN_SIZE_MAX;
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);

	/* Set MaximumLowLatencyPacketSize value to 256 */
	mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
		    MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
		    MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));

	/* Enable Rx cache snoop */
1949
	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
		queue = port->rxqs[lrxq]->id;
		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
		val |= MVPP2_SNOOP_PKT_SIZE_MASK |
			   MVPP2_SNOOP_BUF_HDR_MASK;
		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
	}

	/* At default, mask all interrupts to all present cpus */
	mvpp2_interrupts_disable(port);
}

/* Enable/disable receiving packets */
static void mvpp2_ingress_enable(struct mvpp2_port *port)
{
	u32 val;
	int lrxq, queue;

1967
	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
		queue = port->rxqs[lrxq]->id;
		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
		val &= ~MVPP2_RXQ_DISABLE_MASK;
		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
	}
}

static void mvpp2_ingress_disable(struct mvpp2_port *port)
{
	u32 val;
	int lrxq, queue;

1980
	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
		queue = port->rxqs[lrxq]->id;
		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
		val |= MVPP2_RXQ_DISABLE_MASK;
		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
	}
}

/* Enable transmit via physical egress queue
 * - HW starts take descriptors from DRAM
 */
static void mvpp2_egress_enable(struct mvpp2_port *port)
{
	u32 qmap;
	int queue;
	int tx_port_num = mvpp2_egress_port(port);

	/* Enable all initialized TXs. */
	qmap = 0;
1999
	for (queue = 0; queue < port->ntxqs; queue++) {
2000 2001
		struct mvpp2_tx_queue *txq = port->txqs[queue];

2002
		if (txq->descs)
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
			qmap |= (1 << queue);
	}

	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
}

/* Disable transmit via physical egress queue
 * - HW doesn't take descriptors from DRAM
 */
static void mvpp2_egress_disable(struct mvpp2_port *port)
{
	u32 reg_data;
	int delay;
	int tx_port_num = mvpp2_egress_port(port);

	/* Issue stop command for active channels only */
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
	reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
		    MVPP2_TXP_SCHED_ENQ_MASK;
	if (reg_data != 0)
		mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
			    (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));

	/* Wait for all Tx activity to terminate. */
	delay = 0;
	do {
		if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
			netdev_warn(port->dev,
				    "Tx stop timed out, status=0x%08x\n",
				    reg_data);
			break;
		}
		mdelay(1);
		delay++;

		/* Check port TX Command register that all
		 * Tx queues are stopped
		 */
		reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
	} while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
}

/* Rx descriptors helper methods */

/* Get number of Rx descriptors occupied by received packets */
static inline int
mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
{
	u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));

	return val & MVPP2_RXQ_OCCUPIED_MASK;
}

/* Update Rx queue status with the number of occupied and available
 * Rx descriptor slots.
 */
static inline void
mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
			int used_count, int free_count)
{
	/* Decrement the number of used descriptors and increment count
	 * increment the number of free descriptors.
	 */
	u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);

	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
}

/* Get pointer to next RX descriptor to be processed by SW */
static inline struct mvpp2_rx_desc *
mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
{
	int rx_desc = rxq->next_desc_to_proc;

	rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
	prefetch(rxq->descs + rxq->next_desc_to_proc);
	return rxq->descs + rx_desc;
}

/* Set rx queue offset */
static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
				 int prxq, int offset)
{
	u32 val;

	/* Convert offset from bytes to units of 32 bytes */
	offset = offset >> 5;

	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
	val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;

	/* Offset is in */
	val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
		    MVPP2_RXQ_PACKET_OFFSET_MASK);

	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
}

/* Tx descriptors helper methods */

/* Get pointer to next Tx descriptor to be processed (send) by HW */
static struct mvpp2_tx_desc *
mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
{
	int tx_desc = txq->next_desc_to_proc;

	txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
	return txq->descs + tx_desc;
}

2114 2115 2116 2117 2118
/* Update HW with number of aggregated Tx descriptors to be sent
 *
 * Called only from mvpp2_tx(), so migration is disabled, using
 * smp_processor_id() is OK.
 */
2119 2120 2121
static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
{
	/* aggregated access - relevant TXQ number is written in TX desc */
2122
	mvpp2_thread_write(port->priv,
2123
			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2124
			   MVPP2_AGGR_TXQ_UPDATE_REG, pending);
2125 2126 2127 2128
}

/* Check if there are enough free descriptors in aggregated txq.
 * If not, update the number of occupied descriptors and repeat the check.
2129 2130 2131
 *
 * Called only from mvpp2_tx(), so migration is disabled, using
 * smp_processor_id() is OK.
2132
 */
2133
static int mvpp2_aggr_desc_num_check(struct mvpp2_port *port,
2134 2135
				     struct mvpp2_tx_queue *aggr_txq, int num)
{
2136
	if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
2137
		/* Update number of occupied aggregated Tx descriptors */
2138 2139 2140
		unsigned int thread =
			mvpp2_cpu_to_thread(port->priv, smp_processor_id());
		u32 val = mvpp2_read_relaxed(port->priv,
2141
					     MVPP2_AGGR_TXQ_STATUS_REG(thread));
2142 2143 2144

		aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;

2145 2146 2147
		if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
			return -ENOMEM;
	}
2148 2149 2150
	return 0;
}

2151 2152 2153 2154 2155 2156
/* Reserved Tx descriptors allocation request
 *
 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
 * only by mvpp2_tx(), so migration is disabled, using
 * smp_processor_id() is OK.
 */
2157
static int mvpp2_txq_alloc_reserved_desc(struct mvpp2_port *port,
2158 2159
					 struct mvpp2_tx_queue *txq, int num)
{
2160 2161
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
	struct mvpp2 *priv = port->priv;
2162 2163 2164
	u32 val;

	val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
2165
	mvpp2_thread_write_relaxed(priv, thread, MVPP2_TXQ_RSVD_REQ_REG, val);
2166

2167
	val = mvpp2_thread_read_relaxed(priv, thread, MVPP2_TXQ_RSVD_RSLT_REG);
2168 2169 2170 2171 2172 2173 2174

	return val & MVPP2_TXQ_RSVD_RSLT_MASK;
}

/* Check if there are enough reserved descriptors for transmission.
 * If not, request chunk of reserved descriptors and check again.
 */
2175
static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port *port,
2176 2177 2178 2179
					    struct mvpp2_tx_queue *txq,
					    struct mvpp2_txq_pcpu *txq_pcpu,
					    int num)
{
2180
	int req, desc_count;
2181
	unsigned int thread;
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191

	if (txq_pcpu->reserved_num >= num)
		return 0;

	/* Not enough descriptors reserved! Update the reserved descriptor
	 * count and check again.
	 */

	desc_count = 0;
	/* Compute total of used descriptors */
2192
	for (thread = 0; thread < port->priv->nthreads; thread++) {
2193 2194
		struct mvpp2_txq_pcpu *txq_pcpu_aux;

2195
		txq_pcpu_aux = per_cpu_ptr(txq->pcpu, thread);
2196 2197 2198 2199 2200 2201 2202 2203
		desc_count += txq_pcpu_aux->count;
		desc_count += txq_pcpu_aux->reserved_num;
	}

	req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
	desc_count += req;

	if (desc_count >
2204
	   (txq->size - (MVPP2_MAX_THREADS * MVPP2_CPU_DESC_CHUNK)))
2205 2206
		return -ENOMEM;

2207
	txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(port, txq, req);
2208

2209
	/* OK, the descriptor could have been updated: check again. */
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
	if (txq_pcpu->reserved_num < num)
		return -ENOMEM;
	return 0;
}

/* Release the last allocated Tx descriptor. Useful to handle DMA
 * mapping failures in the Tx path.
 */
static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
{
	if (txq->next_desc_to_proc == 0)
		txq->next_desc_to_proc = txq->last_desc - 1;
	else
		txq->next_desc_to_proc--;
}

/* Set Tx descriptors fields relevant for CSUM calculation */
2227
static u32 mvpp2_txq_desc_csum(int l3_offs, __be16 l3_proto,
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
			       int ip_hdr_len, int l4_proto)
{
	u32 command;

	/* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
	 * G_L4_chk, L4_type required only for checksum calculation
	 */
	command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
	command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
	command |= MVPP2_TXD_IP_CSUM_DISABLE;

2239
	if (l3_proto == htons(ETH_P_IP)) {
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
		command &= ~MVPP2_TXD_IP_CSUM_DISABLE;	/* enable IPv4 csum */
		command &= ~MVPP2_TXD_L3_IP6;		/* enable IPv4 */
	} else {
		command |= MVPP2_TXD_L3_IP6;		/* enable IPv6 */
	}

	if (l4_proto == IPPROTO_TCP) {
		command &= ~MVPP2_TXD_L4_UDP;		/* enable TCP */
		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
	} else if (l4_proto == IPPROTO_UDP) {
		command |= MVPP2_TXD_L4_UDP;		/* enable UDP */
		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
	} else {
		command |= MVPP2_TXD_L4_CSUM_NOT;
	}

	return command;
}

/* Get number of sent descriptors and decrement counter.
 * The number of sent descriptors is returned.
2261
 * Per-thread access
2262 2263 2264 2265
 *
 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
 * (migration disabled) and from the TX completion tasklet (migration
 * disabled) so using smp_processor_id() is OK.
2266 2267 2268 2269 2270 2271 2272
 */
static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
					   struct mvpp2_tx_queue *txq)
{
	u32 val;

	/* Reading status reg resets transmitted descriptor counter */
2273
	val = mvpp2_thread_read_relaxed(port->priv,
2274
					mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2275
					MVPP2_TXQ_SENT_REG(txq->id));
2276 2277 2278 2279 2280

	return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
		MVPP2_TRANSMITTED_COUNT_OFFSET;
}

2281 2282 2283
/* Called through on_each_cpu(), so runs on all CPUs, with migration
 * disabled, therefore using smp_processor_id() is OK.
 */
2284 2285 2286 2287 2288
static void mvpp2_txq_sent_counter_clear(void *arg)
{
	struct mvpp2_port *port = arg;
	int queue;

2289 2290 2291 2292
	/* If the thread isn't used, don't do anything */
	if (smp_processor_id() > port->priv->nthreads)
		return;

2293
	for (queue = 0; queue < port->ntxqs; queue++) {
2294 2295
		int id = port->txqs[queue]->id;

2296
		mvpp2_thread_read(port->priv,
2297
				  mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2298
				  MVPP2_TXQ_SENT_REG(id));
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
	}
}

/* Set max sizes for Tx queues */
static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
{
	u32	val, size, mtu;
	int	txq, tx_port_num;

	mtu = port->pkt_size * 8;
	if (mtu > MVPP2_TXP_MTU_MAX)
		mtu = MVPP2_TXP_MTU_MAX;

	/* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
	mtu = 3 * mtu;

	/* Indirect access to registers */
	tx_port_num = mvpp2_egress_port(port);
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);

	/* Set MTU */
	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
	val &= ~MVPP2_TXP_MTU_MAX;
	val |= mtu;
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);

	/* TXP token size and all TXQs token size must be larger that MTU */
	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
	size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
	if (size < mtu) {
		size = mtu;
		val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
		val |= size;
		mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
	}

2335
	for (txq = 0; txq < port->ntxqs; txq++) {
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
		val = mvpp2_read(port->priv,
				 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
		size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;

		if (size < mtu) {
			size = mtu;
			val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
			val |= size;
			mvpp2_write(port->priv,
				    MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
				    val);
		}
	}
}

/* Set the number of packets that will be received before Rx interrupt
 * will be generated by HW.
 */
static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
2355
				   struct mvpp2_rx_queue *rxq)
2356
{
2357
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2358

2359 2360
	if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
		rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
2361

2362 2363
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_THRESH_REG,
2364
			   rxq->pkts_coal);
2365 2366

	put_cpu();
2367 2368
}

2369 2370 2371 2372
/* For some reason in the LSP this is done on each CPU. Why ? */
static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
				   struct mvpp2_tx_queue *txq)
{
2373
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2374 2375 2376 2377 2378 2379
	u32 val;

	if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
		txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;

	val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
2380 2381
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_THRESH_REG, val);
2382 2383 2384 2385

	put_cpu();
}

2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
{
	u64 tmp = (u64)clk_hz * usec;

	do_div(tmp, USEC_PER_SEC);

	return tmp > U32_MAX ? U32_MAX : tmp;
}

static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
{
	u64 tmp = (u64)cycles * USEC_PER_SEC;

	do_div(tmp, clk_hz);

	return tmp > U32_MAX ? U32_MAX : tmp;
}

2404 2405
/* Set the time delay in usec before Rx interrupt */
static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
2406
				   struct mvpp2_rx_queue *rxq)
2407
{
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
	unsigned long freq = port->priv->tclk;
	u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);

	if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
		rxq->time_coal =
			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);

		/* re-evaluate to get actual register value */
		val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
	}
2418 2419 2420 2421

	mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
}

2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
{
	unsigned long freq = port->priv->tclk;
	u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);

	if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
		port->tx_time_coal =
			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);

		/* re-evaluate to get actual register value */
		val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
	}

	mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
}

2438 2439 2440 2441 2442 2443 2444 2445
/* Free Tx queue skbuffs */
static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
				struct mvpp2_tx_queue *txq,
				struct mvpp2_txq_pcpu *txq_pcpu, int num)
{
	int i;

	for (i = 0; i < num; i++) {
2446 2447
		struct mvpp2_txq_pcpu_buf *tx_buf =
			txq_pcpu->buffs + txq_pcpu->txq_get_index;
2448

M
Matteo Croce 已提交
2449 2450
		if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma) &&
		    tx_buf->type != MVPP2_TYPE_XDP_TX)
2451 2452
			dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
					 tx_buf->size, DMA_TO_DEVICE);
M
Matteo Croce 已提交
2453
		if (tx_buf->type == MVPP2_TYPE_SKB && tx_buf->skb)
2454
			dev_kfree_skb_any(tx_buf->skb);
M
Matteo Croce 已提交
2455 2456 2457
		else if (tx_buf->type == MVPP2_TYPE_XDP_TX ||
			 tx_buf->type == MVPP2_TYPE_XDP_NDO)
			xdp_return_frame(tx_buf->xdpf);
2458 2459

		mvpp2_txq_inc_get(txq_pcpu);
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
	}
}

static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
							u32 cause)
{
	int queue = fls(cause) - 1;

	return port->rxqs[queue];
}

static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
							u32 cause)
{
2474
	int queue = fls(cause) - 1;
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485

	return port->txqs[queue];
}

/* Handle end of transmission */
static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
			   struct mvpp2_txq_pcpu *txq_pcpu)
{
	struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
	int tx_done;

2486
	if (txq_pcpu->thread != mvpp2_cpu_to_thread(port->priv, smp_processor_id()))
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
		netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");

	tx_done = mvpp2_txq_sent_desc_proc(port, txq);
	if (!tx_done)
		return;
	mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);

	txq_pcpu->count -= tx_done;

	if (netif_tx_queue_stopped(nq))
2497
		if (txq_pcpu->count <= txq_pcpu->wake_threshold)
2498 2499 2500
			netif_tx_wake_queue(nq);
}

2501
static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
2502
				  unsigned int thread)
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
{
	struct mvpp2_tx_queue *txq;
	struct mvpp2_txq_pcpu *txq_pcpu;
	unsigned int tx_todo = 0;

	while (cause) {
		txq = mvpp2_get_tx_queue(port, cause);
		if (!txq)
			break;

2513
		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524

		if (txq_pcpu->count) {
			mvpp2_txq_done(port, txq, txq_pcpu);
			tx_todo += txq_pcpu->count;
		}

		cause &= ~(1 << txq->log_id);
	}
	return tx_todo;
}

2525 2526 2527 2528
/* Rx/Tx queue initialization/cleanup methods */

/* Allocate and initialize descriptors for aggr TXQ */
static int mvpp2_aggr_txq_init(struct platform_device *pdev,
2529
			       struct mvpp2_tx_queue *aggr_txq,
2530
			       unsigned int thread, struct mvpp2 *priv)
2531
{
2532 2533
	u32 txq_dma;

2534
	/* Allocate memory for TX descriptors */
2535 2536 2537
	aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
					     MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
					     &aggr_txq->descs_dma, GFP_KERNEL);
2538 2539 2540
	if (!aggr_txq->descs)
		return -ENOMEM;

2541
	aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
2542 2543 2544

	/* Aggr TXQ no reset WA */
	aggr_txq->next_desc_to_proc = mvpp2_read(priv,
2545
						 MVPP2_AGGR_TXQ_INDEX_REG(thread));
2546

2547 2548 2549 2550 2551 2552 2553 2554 2555
	/* Set Tx descriptors queue starting address indirect
	 * access
	 */
	if (priv->hw_version == MVPP21)
		txq_dma = aggr_txq->descs_dma;
	else
		txq_dma = aggr_txq->descs_dma >>
			MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;

2556 2557
	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(thread), txq_dma);
	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(thread),
2558
		    MVPP2_AGGR_TXQ_SIZE);
2559 2560 2561 2562 2563 2564 2565 2566

	return 0;
}

/* Create a specified Rx queue */
static int mvpp2_rxq_init(struct mvpp2_port *port,
			  struct mvpp2_rx_queue *rxq)
{
M
Matteo Croce 已提交
2567
	struct mvpp2 *priv = port->priv;
2568
	unsigned int thread;
2569
	u32 rxq_dma;
M
Matteo Croce 已提交
2570
	int err;
2571

2572 2573 2574 2575 2576
	rxq->size = port->rx_ring_size;

	/* Allocate memory for RX descriptors */
	rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
					rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2577
					&rxq->descs_dma, GFP_KERNEL);
2578 2579 2580 2581 2582 2583 2584 2585 2586
	if (!rxq->descs)
		return -ENOMEM;

	rxq->last_desc = rxq->size - 1;

	/* Zero occupied and non-occupied counters - direct access */
	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);

	/* Set Rx descriptors queue starting address - indirect access */
2587
	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2588
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2589 2590 2591 2592
	if (port->priv->hw_version == MVPP21)
		rxq_dma = rxq->descs_dma;
	else
		rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
2593 2594 2595
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_INDEX_REG, 0);
2596
	put_cpu();
2597 2598

	/* Set Offset */
M
Matteo Croce 已提交
2599
	mvpp2_rxq_offset_set(port, rxq->id, MVPP2_SKB_HEADROOM);
2600 2601

	/* Set coalescing pkts and time */
2602 2603
	mvpp2_rx_pkts_coal_set(port, rxq);
	mvpp2_rx_time_coal_set(port, rxq);
2604 2605 2606 2607

	/* Add number of descriptors ready for receiving packets */
	mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);

M
Matteo Croce 已提交
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
	if (priv->percpu_pools) {
		err = xdp_rxq_info_reg(&rxq->xdp_rxq_short, port->dev, rxq->id);
		if (err < 0)
			goto err_free_dma;

		err = xdp_rxq_info_reg(&rxq->xdp_rxq_long, port->dev, rxq->id);
		if (err < 0)
			goto err_unregister_rxq_short;

		/* Every RXQ has a pool for short and another for long packets */
		err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_short,
						 MEM_TYPE_PAGE_POOL,
						 priv->page_pool[rxq->logic_rxq]);
		if (err < 0)
			goto err_unregister_rxq_long;

		err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_long,
						 MEM_TYPE_PAGE_POOL,
						 priv->page_pool[rxq->logic_rxq +
								 port->nrxqs]);
		if (err < 0)
			goto err_unregister_mem_rxq_short;
	}

2632
	return 0;
M
Matteo Croce 已提交
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644

err_unregister_mem_rxq_short:
	xdp_rxq_info_unreg_mem_model(&rxq->xdp_rxq_short);
err_unregister_rxq_long:
	xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
err_unregister_rxq_short:
	xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
err_free_dma:
	dma_free_coherent(port->dev->dev.parent,
			  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
			  rxq->descs, rxq->descs_dma);
	return err;
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
}

/* Push packets received by the RXQ to BM pool */
static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
				struct mvpp2_rx_queue *rxq)
{
	int rx_received, i;

	rx_received = mvpp2_rxq_received(port, rxq->id);
	if (!rx_received)
		return;

	for (i = 0; i < rx_received; i++) {
		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
2659 2660 2661 2662 2663
		u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
		int pool;

		pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
			MVPP2_RXD_BM_POOL_ID_OFFS;
2664

2665
		mvpp2_bm_pool_put(port, pool,
2666 2667
				  mvpp2_rxdesc_dma_addr_get(port, rx_desc),
				  mvpp2_rxdesc_cookie_get(port, rx_desc));
2668 2669 2670 2671 2672 2673 2674 2675
	}
	mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
}

/* Cleanup Rx queue */
static void mvpp2_rxq_deinit(struct mvpp2_port *port,
			     struct mvpp2_rx_queue *rxq)
{
2676
	unsigned int thread;
2677

M
Matteo Croce 已提交
2678 2679 2680 2681 2682 2683
	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_short))
		xdp_rxq_info_unreg(&rxq->xdp_rxq_short);

	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_long))
		xdp_rxq_info_unreg(&rxq->xdp_rxq_long);

2684 2685 2686 2687 2688 2689
	mvpp2_rxq_drop_pkts(port, rxq);

	if (rxq->descs)
		dma_free_coherent(port->dev->dev.parent,
				  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
				  rxq->descs,
2690
				  rxq->descs_dma);
2691 2692 2693 2694

	rxq->descs             = NULL;
	rxq->last_desc         = 0;
	rxq->next_desc_to_proc = 0;
2695
	rxq->descs_dma         = 0;
2696 2697 2698 2699 2700

	/* Clear Rx descriptors queue starting address and size;
	 * free descriptor number
	 */
	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2701
	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2702 2703 2704
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, 0);
	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, 0);
2705
	put_cpu();
2706 2707 2708 2709 2710 2711 2712
}

/* Create and initialize a Tx queue */
static int mvpp2_txq_init(struct mvpp2_port *port,
			  struct mvpp2_tx_queue *txq)
{
	u32 val;
2713
	unsigned int thread;
2714
	int desc, desc_per_txq, tx_port_num;
2715 2716 2717 2718 2719 2720 2721
	struct mvpp2_txq_pcpu *txq_pcpu;

	txq->size = port->tx_ring_size;

	/* Allocate memory for Tx descriptors */
	txq->descs = dma_alloc_coherent(port->dev->dev.parent,
				txq->size * MVPP2_DESC_ALIGNED_SIZE,
2722
				&txq->descs_dma, GFP_KERNEL);
2723 2724 2725 2726 2727 2728
	if (!txq->descs)
		return -ENOMEM;

	txq->last_desc = txq->size - 1;

	/* Set Tx descriptors queue starting address - indirect access */
2729
	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2730 2731
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG,
2732
			   txq->descs_dma);
2733
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG,
2734
			   txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
2735 2736
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_INDEX_REG, 0);
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_RSVD_CLR_REG,
2737
			   txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
2738
	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PENDING_REG);
2739
	val &= ~MVPP2_TXQ_PENDING_MASK;
2740
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PENDING_REG, val);
2741 2742 2743 2744

	/* Calculate base address in prefetch buffer. We reserve 16 descriptors
	 * for each existing TXQ.
	 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
2745
	 * GBE ports assumed to be continuous from 0 to MVPP2_MAX_PORTS
2746 2747 2748 2749 2750
	 */
	desc_per_txq = 16;
	desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
	       (txq->log_id * desc_per_txq);

2751
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG,
2752 2753
			   MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
			   MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
2754
	put_cpu();
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769

	/* WRR / EJP configuration - indirect access */
	tx_port_num = mvpp2_egress_port(port);
	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);

	val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
	val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
	val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
	val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);

	val = MVPP2_TXQ_TOKEN_SIZE_MAX;
	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
		    val);

2770
	for (thread = 0; thread < port->priv->nthreads; thread++) {
2771
		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2772
		txq_pcpu->size = txq->size;
2773 2774 2775
		txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
						sizeof(*txq_pcpu->buffs),
						GFP_KERNEL);
2776
		if (!txq_pcpu->buffs)
2777
			return -ENOMEM;
2778 2779 2780 2781 2782

		txq_pcpu->count = 0;
		txq_pcpu->reserved_num = 0;
		txq_pcpu->txq_put_index = 0;
		txq_pcpu->txq_get_index = 0;
2783
		txq_pcpu->tso_headers = NULL;
2784

2785 2786 2787
		txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
		txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;

2788 2789
		txq_pcpu->tso_headers =
			dma_alloc_coherent(port->dev->dev.parent,
2790
					   txq_pcpu->size * TSO_HEADER_SIZE,
2791 2792 2793
					   &txq_pcpu->tso_headers_dma,
					   GFP_KERNEL);
		if (!txq_pcpu->tso_headers)
2794
			return -ENOMEM;
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
	}

	return 0;
}

/* Free allocated TXQ resources */
static void mvpp2_txq_deinit(struct mvpp2_port *port,
			     struct mvpp2_tx_queue *txq)
{
	struct mvpp2_txq_pcpu *txq_pcpu;
2805
	unsigned int thread;
2806

2807
	for (thread = 0; thread < port->priv->nthreads; thread++) {
2808
		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2809
		kfree(txq_pcpu->buffs);
2810

2811 2812 2813 2814 2815 2816 2817
		if (txq_pcpu->tso_headers)
			dma_free_coherent(port->dev->dev.parent,
					  txq_pcpu->size * TSO_HEADER_SIZE,
					  txq_pcpu->tso_headers,
					  txq_pcpu->tso_headers_dma);

		txq_pcpu->tso_headers = NULL;
2818 2819 2820 2821 2822
	}

	if (txq->descs)
		dma_free_coherent(port->dev->dev.parent,
				  txq->size * MVPP2_DESC_ALIGNED_SIZE,
2823
				  txq->descs, txq->descs_dma);
2824 2825 2826 2827

	txq->descs             = NULL;
	txq->last_desc         = 0;
	txq->next_desc_to_proc = 0;
2828
	txq->descs_dma         = 0;
2829 2830

	/* Set minimum bandwidth for disabled TXQs */
2831
	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->log_id), 0);
2832 2833

	/* Set Tx descriptors queue starting address and size */
2834
	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2835 2836 2837
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG, 0);
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG, 0);
2838
	put_cpu();
2839 2840 2841 2842 2843 2844
}

/* Cleanup Tx ports */
static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
{
	struct mvpp2_txq_pcpu *txq_pcpu;
2845
	int delay, pending;
2846
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2847 2848
	u32 val;

2849 2850
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG);
2851
	val |= MVPP2_TXQ_DRAIN_EN_MASK;
2852
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867

	/* The napi queue has been stopped so wait for all packets
	 * to be transmitted.
	 */
	delay = 0;
	do {
		if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
			netdev_warn(port->dev,
				    "port %d: cleaning queue %d timed out\n",
				    port->id, txq->log_id);
			break;
		}
		mdelay(1);
		delay++;

2868
		pending = mvpp2_thread_read(port->priv, thread,
2869 2870
					    MVPP2_TXQ_PENDING_REG);
		pending &= MVPP2_TXQ_PENDING_MASK;
2871 2872 2873
	} while (pending);

	val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
2874
	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
2875
	put_cpu();
2876

2877
	for (thread = 0; thread < port->priv->nthreads; thread++) {
2878
		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902

		/* Release all packets */
		mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);

		/* Reset queue */
		txq_pcpu->count = 0;
		txq_pcpu->txq_put_index = 0;
		txq_pcpu->txq_get_index = 0;
	}
}

/* Cleanup all Tx queues */
static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
{
	struct mvpp2_tx_queue *txq;
	int queue;
	u32 val;

	val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);

	/* Reset Tx ports and delete Tx queues */
	val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);

2903
	for (queue = 0; queue < port->ntxqs; queue++) {
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
		txq = port->txqs[queue];
		mvpp2_txq_clean(port, txq);
		mvpp2_txq_deinit(port, txq);
	}

	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);

	val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
}

/* Cleanup all Rx queues */
static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
{
	int queue;

2920
	for (queue = 0; queue < port->nrxqs; queue++)
2921 2922 2923 2924 2925 2926 2927 2928
		mvpp2_rxq_deinit(port, port->rxqs[queue]);
}

/* Init all Rx queues for port */
static int mvpp2_setup_rxqs(struct mvpp2_port *port)
{
	int queue, err;

2929
	for (queue = 0; queue < port->nrxqs; queue++) {
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
		err = mvpp2_rxq_init(port, port->rxqs[queue]);
		if (err)
			goto err_cleanup;
	}
	return 0;

err_cleanup:
	mvpp2_cleanup_rxqs(port);
	return err;
}

/* Init all tx queues for port */
static int mvpp2_setup_txqs(struct mvpp2_port *port)
{
	struct mvpp2_tx_queue *txq;
M
Matteo Croce 已提交
2945
	int queue, err;
2946

2947
	for (queue = 0; queue < port->ntxqs; queue++) {
2948 2949 2950 2951
		txq = port->txqs[queue];
		err = mvpp2_txq_init(port, txq);
		if (err)
			goto err_cleanup;
2952 2953

		/* Assign this queue to a CPU */
M
Matteo Croce 已提交
2954 2955
		if (queue < num_possible_cpus())
			netif_set_xps_queue(port->dev, cpumask_of(queue), queue);
2956 2957
	}

2958 2959 2960 2961 2962 2963 2964 2965
	if (port->has_tx_irqs) {
		mvpp2_tx_time_coal_set(port);
		for (queue = 0; queue < port->ntxqs; queue++) {
			txq = port->txqs[queue];
			mvpp2_tx_pkts_coal_set(port, txq);
		}
	}

2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
	return 0;

err_cleanup:
	mvpp2_cleanup_txqs(port);
	return err;
}

/* The callback for per-port interrupt */
static irqreturn_t mvpp2_isr(int irq, void *dev_id)
{
2977
	struct mvpp2_queue_vector *qv = dev_id;
2978

2979
	mvpp2_qvec_interrupt_disable(qv);
2980

2981
	napi_schedule(&qv->napi);
2982 2983 2984 2985

	return IRQ_HANDLED;
}

2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
static void mvpp2_isr_handle_ptp_queue(struct mvpp2_port *port, int nq)
{
	struct skb_shared_hwtstamps shhwtstamps;
	struct mvpp2_hwtstamp_queue *queue;
	struct sk_buff *skb;
	void __iomem *ptp_q;
	unsigned int id;
	u32 r0, r1, r2;

	ptp_q = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
	if (nq)
		ptp_q += MVPP22_PTP_TX_Q1_R0 - MVPP22_PTP_TX_Q0_R0;

	queue = &port->tx_hwtstamp_queue[nq];

	while (1) {
		r0 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R0) & 0xffff;
		if (!r0)
			break;

		r1 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R1) & 0xffff;
		r2 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R2) & 0xffff;

		id = (r0 >> 1) & 31;

		skb = queue->skb[id];
		queue->skb[id] = NULL;
		if (skb) {
			u32 ts = r2 << 19 | r1 << 3 | r0 >> 13;

			mvpp22_tai_tstamp(port->priv->tai, ts, &shhwtstamps);
			skb_tstamp_tx(skb, &shhwtstamps);
			dev_kfree_skb_any(skb);
		}
	}
}

static void mvpp2_isr_handle_ptp(struct mvpp2_port *port)
{
	void __iomem *ptp;
	u32 val;

	ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
	val = readl(ptp + MVPP22_PTP_INT_CAUSE);
	if (val & MVPP22_PTP_INT_CAUSE_QUEUE0)
		mvpp2_isr_handle_ptp_queue(port, 0);
	if (val & MVPP22_PTP_INT_CAUSE_QUEUE1)
		mvpp2_isr_handle_ptp_queue(port, 1);
}

3036
static void mvpp2_isr_handle_link(struct mvpp2_port *port, bool link)
3037 3038 3039
{
	struct net_device *dev = port->dev;

A
Antoine Tenart 已提交
3040 3041
	if (port->phylink) {
		phylink_mac_change(port->phylink, link);
3042
		return;
A
Antoine Tenart 已提交
3043 3044
	}

3045 3046
	if (!netif_running(dev))
		return;
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062

	if (link) {
		mvpp2_interrupts_enable(port);

		mvpp2_egress_enable(port);
		mvpp2_ingress_enable(port);
		netif_carrier_on(dev);
		netif_tx_wake_all_queues(dev);
	} else {
		netif_tx_stop_all_queues(dev);
		netif_carrier_off(dev);
		mvpp2_ingress_disable(port);
		mvpp2_egress_disable(port);

		mvpp2_interrupts_disable(port);
	}
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
}

static void mvpp2_isr_handle_xlg(struct mvpp2_port *port)
{
	bool link;
	u32 val;

	val = readl(port->base + MVPP22_XLG_INT_STAT);
	if (val & MVPP22_XLG_INT_STAT_LINK) {
		val = readl(port->base + MVPP22_XLG_STATUS);
3073
		link = (val & MVPP22_XLG_STATUS_LINK_UP);
3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
		mvpp2_isr_handle_link(port, link);
	}
}

static void mvpp2_isr_handle_gmac_internal(struct mvpp2_port *port)
{
	bool link;
	u32 val;

	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
	    phy_interface_mode_is_8023z(port->phy_interface) ||
	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
		val = readl(port->base + MVPP22_GMAC_INT_STAT);
		if (val & MVPP22_GMAC_INT_STAT_LINK) {
			val = readl(port->base + MVPP2_GMAC_STATUS0);
3089
			link = (val & MVPP2_GMAC_STATUS0_LINK_UP);
3090 3091 3092 3093 3094 3095
			mvpp2_isr_handle_link(port, link);
		}
	}
}

/* Per-port interrupt for link status changes */
3096
static irqreturn_t mvpp2_port_isr(int irq, void *dev_id)
3097 3098
{
	struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
3099
	u32 val;
3100 3101 3102 3103 3104

	mvpp22_gop_mask_irq(port);

	if (mvpp2_port_supports_xlg(port) &&
	    mvpp2_is_xlg(port->phy_interface)) {
3105 3106 3107 3108
		/* Check the external status register */
		val = readl(port->base + MVPP22_XLG_EXT_INT_STAT);
		if (val & MVPP22_XLG_EXT_INT_STAT_XLG)
			mvpp2_isr_handle_xlg(port);
3109 3110
		if (val & MVPP22_XLG_EXT_INT_STAT_PTP)
			mvpp2_isr_handle_ptp(port);
3111
	} else {
3112 3113 3114 3115 3116 3117
		/* If it's not the XLG, we must be using the GMAC.
		 * Check the summary status.
		 */
		val = readl(port->base + MVPP22_GMAC_INT_SUM_STAT);
		if (val & MVPP22_GMAC_INT_SUM_STAT_INTERNAL)
			mvpp2_isr_handle_gmac_internal(port);
3118 3119
		if (val & MVPP22_GMAC_INT_SUM_STAT_PTP)
			mvpp2_isr_handle_ptp(port);
3120
	}
3121 3122 3123 3124 3125

	mvpp22_gop_unmask_irq(port);
	return IRQ_HANDLED;
}

3126
static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
3127
{
3128 3129
	struct net_device *dev;
	struct mvpp2_port *port;
3130
	struct mvpp2_port_pcpu *port_pcpu;
3131 3132
	unsigned int tx_todo, cause;

3133 3134
	port_pcpu = container_of(timer, struct mvpp2_port_pcpu, tx_done_timer);
	dev = port_pcpu->dev;
3135

3136
	if (!netif_running(dev))
3137 3138
		return HRTIMER_NORESTART;

3139
	port_pcpu->timer_scheduled = false;
3140
	port = netdev_priv(dev);
3141 3142

	/* Process all the Tx queues */
3143
	cause = (1 << port->ntxqs) - 1;
3144
	tx_todo = mvpp2_tx_done(port, cause,
3145
				mvpp2_cpu_to_thread(port->priv, smp_processor_id()));
3146 3147

	/* Set the timer in case not all the packets were processed */
3148 3149 3150 3151
	if (tx_todo && !port_pcpu->timer_scheduled) {
		port_pcpu->timer_scheduled = true;
		hrtimer_forward_now(&port_pcpu->tx_done_timer,
				    MVPP2_TXDONE_HRTIMER_PERIOD_NS);
3152

3153 3154
		return HRTIMER_RESTART;
	}
3155 3156 3157
	return HRTIMER_NORESTART;
}

3158 3159 3160 3161 3162 3163
/* Main RX/TX processing routines */

/* Display more error info */
static void mvpp2_rx_error(struct mvpp2_port *port,
			   struct mvpp2_rx_desc *rx_desc)
{
3164 3165
	u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
	size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3166
	char *err_str = NULL;
3167 3168 3169

	switch (status & MVPP2_RXD_ERR_CODE_MASK) {
	case MVPP2_RXD_ERR_CRC:
3170
		err_str = "crc";
3171 3172
		break;
	case MVPP2_RXD_ERR_OVERRUN:
3173
		err_str = "overrun";
3174 3175
		break;
	case MVPP2_RXD_ERR_RESOURCE:
3176
		err_str = "resource";
3177 3178
		break;
	}
3179 3180 3181 3182
	if (err_str && net_ratelimit())
		netdev_err(port->dev,
			   "bad rx status %08x (%s error), size=%zu\n",
			   status, err_str, sz);
3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202
}

/* Handle RX checksum offload */
static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
			  struct sk_buff *skb)
{
	if (((status & MVPP2_RXD_L3_IP4) &&
	     !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
	    (status & MVPP2_RXD_L3_IP6))
		if (((status & MVPP2_RXD_L4_UDP) ||
		     (status & MVPP2_RXD_L4_TCP)) &&
		     (status & MVPP2_RXD_L4_CSUM_OK)) {
			skb->csum = 0;
			skb->ip_summed = CHECKSUM_UNNECESSARY;
			return;
		}

	skb->ip_summed = CHECKSUM_NONE;
}

M
Matteo Croce 已提交
3203
/* Allocate a new skb and add it to BM pool */
3204
static int mvpp2_rx_refill(struct mvpp2_port *port,
M
Matteo Croce 已提交
3205 3206
			   struct mvpp2_bm_pool *bm_pool,
			   struct page_pool *page_pool, int pool)
3207
{
3208
	dma_addr_t dma_addr;
3209
	phys_addr_t phys_addr;
3210
	void *buf;
3211

M
Matteo Croce 已提交
3212 3213
	buf = mvpp2_buf_alloc(port, bm_pool, page_pool,
			      &dma_addr, &phys_addr, GFP_ATOMIC);
3214
	if (!buf)
3215 3216
		return -ENOMEM;

3217
	mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3218

3219 3220 3221 3222 3223 3224 3225 3226 3227
	return 0;
}

/* Handle tx checksum */
static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
{
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
		int ip_hdr_len = 0;
		u8 l4_proto;
3228
		__be16 l3_proto = vlan_get_protocol(skb);
3229

3230
		if (l3_proto == htons(ETH_P_IP)) {
3231 3232 3233 3234 3235
			struct iphdr *ip4h = ip_hdr(skb);

			/* Calculate IPv4 checksum and L4 checksum */
			ip_hdr_len = ip4h->ihl;
			l4_proto = ip4h->protocol;
3236
		} else if (l3_proto == htons(ETH_P_IPV6)) {
3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
			struct ipv6hdr *ip6h = ipv6_hdr(skb);

			/* Read l4_protocol from one of IPv6 extra headers */
			if (skb_network_header_len(skb) > 0)
				ip_hdr_len = (skb_network_header_len(skb) >> 2);
			l4_proto = ip6h->nexthdr;
		} else {
			return MVPP2_TXD_L4_CSUM_NOT;
		}

		return mvpp2_txq_desc_csum(skb_network_offset(skb),
3248
					   l3_proto, ip_hdr_len, l4_proto);
3249 3250 3251 3252 3253
	}

	return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
}

M
Matteo Croce 已提交
3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
static void mvpp2_xdp_finish_tx(struct mvpp2_port *port, u16 txq_id, int nxmit, int nxmit_byte)
{
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
	struct mvpp2_tx_queue *aggr_txq;
	struct mvpp2_txq_pcpu *txq_pcpu;
	struct mvpp2_tx_queue *txq;
	struct netdev_queue *nq;

	txq = port->txqs[txq_id];
	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
	nq = netdev_get_tx_queue(port->dev, txq_id);
	aggr_txq = &port->priv->aggr_txqs[thread];

	txq_pcpu->reserved_num -= nxmit;
	txq_pcpu->count += nxmit;
	aggr_txq->count += nxmit;

	/* Enable transmit */
	wmb();
	mvpp2_aggr_txq_pend_desc_add(port, nxmit);

	if (txq_pcpu->count >= txq_pcpu->stop_threshold)
		netif_tx_stop_queue(nq);

	/* Finalize TX processing */
	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
		mvpp2_txq_done(port, txq, txq_pcpu);
}

static int
mvpp2_xdp_submit_frame(struct mvpp2_port *port, u16 txq_id,
		       struct xdp_frame *xdpf, bool dma_map)
{
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
	u32 tx_cmd = MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE |
		     MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
	enum mvpp2_tx_buf_type buf_type;
	struct mvpp2_txq_pcpu *txq_pcpu;
	struct mvpp2_tx_queue *aggr_txq;
	struct mvpp2_tx_desc *tx_desc;
	struct mvpp2_tx_queue *txq;
	int ret = MVPP2_XDP_TX;
	dma_addr_t dma_addr;

	txq = port->txqs[txq_id];
	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
	aggr_txq = &port->priv->aggr_txqs[thread];

	/* Check number of available descriptors */
	if (mvpp2_aggr_desc_num_check(port, aggr_txq, 1) ||
	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, 1)) {
		ret = MVPP2_XDP_DROPPED;
		goto out;
	}

	/* Get a descriptor for the first part of the packet */
	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
	mvpp2_txdesc_size_set(port, tx_desc, xdpf->len);

	if (dma_map) {
		/* XDP_REDIRECT or AF_XDP */
		dma_addr = dma_map_single(port->dev->dev.parent, xdpf->data,
					  xdpf->len, DMA_TO_DEVICE);

		if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
			mvpp2_txq_desc_put(txq);
			ret = MVPP2_XDP_DROPPED;
			goto out;
		}

		buf_type = MVPP2_TYPE_XDP_NDO;
	} else {
		/* XDP_TX */
		struct page *page = virt_to_page(xdpf->data);

		dma_addr = page_pool_get_dma_addr(page) +
			   sizeof(*xdpf) + xdpf->headroom;
		dma_sync_single_for_device(port->dev->dev.parent, dma_addr,
					   xdpf->len, DMA_BIDIRECTIONAL);

		buf_type = MVPP2_TYPE_XDP_TX;
	}

	mvpp2_txdesc_dma_addr_set(port, tx_desc, dma_addr);

	mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
	mvpp2_txq_inc_put(port, txq_pcpu, xdpf, tx_desc, buf_type);

out:
	return ret;
}

static int
mvpp2_xdp_xmit_back(struct mvpp2_port *port, struct xdp_buff *xdp)
{
S
Sven Auhagen 已提交
3350
	struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
M
Matteo Croce 已提交
3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
	struct xdp_frame *xdpf;
	u16 txq_id;
	int ret;

	xdpf = xdp_convert_buff_to_frame(xdp);
	if (unlikely(!xdpf))
		return MVPP2_XDP_DROPPED;

	/* The first of the TX queues are used for XPS,
	 * the second half for XDP_TX
	 */
	txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);

	ret = mvpp2_xdp_submit_frame(port, txq_id, xdpf, false);
S
Sven Auhagen 已提交
3365 3366 3367 3368 3369 3370 3371
	if (ret == MVPP2_XDP_TX) {
		u64_stats_update_begin(&stats->syncp);
		stats->tx_bytes += xdpf->len;
		stats->tx_packets++;
		stats->xdp_tx++;
		u64_stats_update_end(&stats->syncp);

M
Matteo Croce 已提交
3372
		mvpp2_xdp_finish_tx(port, txq_id, 1, xdpf->len);
S
Sven Auhagen 已提交
3373 3374 3375 3376 3377
	} else {
		u64_stats_update_begin(&stats->syncp);
		stats->xdp_tx_err++;
		u64_stats_update_end(&stats->syncp);
	}
M
Matteo Croce 已提交
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387

	return ret;
}

static int
mvpp2_xdp_xmit(struct net_device *dev, int num_frame,
	       struct xdp_frame **frames, u32 flags)
{
	struct mvpp2_port *port = netdev_priv(dev);
	int i, nxmit_byte = 0, nxmit = num_frame;
S
Sven Auhagen 已提交
3388
	struct mvpp2_pcpu_stats *stats;
M
Matteo Croce 已提交
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
	u16 txq_id;
	u32 ret;

	if (unlikely(test_bit(0, &port->state)))
		return -ENETDOWN;

	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
		return -EINVAL;

	/* The first of the TX queues are used for XPS,
	 * the second half for XDP_TX
	 */
	txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);

	for (i = 0; i < num_frame; i++) {
		ret = mvpp2_xdp_submit_frame(port, txq_id, frames[i], true);
		if (ret == MVPP2_XDP_TX) {
			nxmit_byte += frames[i]->len;
		} else {
			xdp_return_frame_rx_napi(frames[i]);
			nxmit--;
		}
	}

S
Sven Auhagen 已提交
3413
	if (likely(nxmit > 0))
M
Matteo Croce 已提交
3414 3415
		mvpp2_xdp_finish_tx(port, txq_id, nxmit, nxmit_byte);

S
Sven Auhagen 已提交
3416 3417 3418 3419 3420 3421 3422 3423
	stats = this_cpu_ptr(port->stats);
	u64_stats_update_begin(&stats->syncp);
	stats->tx_bytes += nxmit_byte;
	stats->tx_packets += nxmit;
	stats->xdp_xmit += nxmit;
	stats->xdp_xmit_err += num_frame - nxmit;
	u64_stats_update_end(&stats->syncp);

M
Matteo Croce 已提交
3424 3425 3426
	return nxmit;
}

M
Matteo Croce 已提交
3427 3428 3429
static int
mvpp2_run_xdp(struct mvpp2_port *port, struct mvpp2_rx_queue *rxq,
	      struct bpf_prog *prog, struct xdp_buff *xdp,
S
Sven Auhagen 已提交
3430
	      struct page_pool *pp, struct mvpp2_pcpu_stats *stats)
M
Matteo Croce 已提交
3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
{
	unsigned int len, sync, err;
	struct page *page;
	u32 ret, act;

	len = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
	act = bpf_prog_run_xdp(prog, xdp);

	/* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
	sync = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
	sync = max(sync, len);

	switch (act) {
	case XDP_PASS:
S
Sven Auhagen 已提交
3445
		stats->xdp_pass++;
M
Matteo Croce 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
		ret = MVPP2_XDP_PASS;
		break;
	case XDP_REDIRECT:
		err = xdp_do_redirect(port->dev, xdp, prog);
		if (unlikely(err)) {
			ret = MVPP2_XDP_DROPPED;
			page = virt_to_head_page(xdp->data);
			page_pool_put_page(pp, page, sync, true);
		} else {
			ret = MVPP2_XDP_REDIR;
S
Sven Auhagen 已提交
3456
			stats->xdp_redirect++;
M
Matteo Croce 已提交
3457 3458
		}
		break;
M
Matteo Croce 已提交
3459 3460 3461 3462 3463 3464 3465
	case XDP_TX:
		ret = mvpp2_xdp_xmit_back(port, xdp);
		if (ret != MVPP2_XDP_TX) {
			page = virt_to_head_page(xdp->data);
			page_pool_put_page(pp, page, sync, true);
		}
		break;
M
Matteo Croce 已提交
3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
	default:
		bpf_warn_invalid_xdp_action(act);
		fallthrough;
	case XDP_ABORTED:
		trace_xdp_exception(port->dev, prog, act);
		fallthrough;
	case XDP_DROP:
		page = virt_to_head_page(xdp->data);
		page_pool_put_page(pp, page, sync, true);
		ret = MVPP2_XDP_DROPPED;
S
Sven Auhagen 已提交
3476
		stats->xdp_drop++;
M
Matteo Croce 已提交
3477 3478 3479 3480 3481 3482
		break;
	}

	return ret;
}

3483
/* Main rx processing */
3484 3485
static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
		    int rx_todo, struct mvpp2_rx_queue *rxq)
3486 3487
{
	struct net_device *dev = port->dev;
S
Sven Auhagen 已提交
3488
	struct mvpp2_pcpu_stats ps = {};
M
Matteo Croce 已提交
3489
	enum dma_data_direction dma_dir;
M
Matteo Croce 已提交
3490 3491
	struct bpf_prog *xdp_prog;
	struct xdp_buff xdp;
3492 3493
	int rx_received;
	int rx_done = 0;
M
Matteo Croce 已提交
3494
	u32 xdp_ret = 0;
3495

M
Matteo Croce 已提交
3496 3497 3498 3499
	rcu_read_lock();

	xdp_prog = READ_ONCE(port->xdp_prog);

3500 3501 3502 3503 3504
	/* Get number of received packets and clamp the to-do */
	rx_received = mvpp2_rxq_received(port, rxq->id);
	if (rx_todo > rx_received)
		rx_todo = rx_received;

3505
	while (rx_done < rx_todo) {
3506 3507
		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
		struct mvpp2_bm_pool *bm_pool;
M
Matteo Croce 已提交
3508
		struct page_pool *pp = NULL;
3509
		struct sk_buff *skb;
3510
		unsigned int frag_size;
3511
		dma_addr_t dma_addr;
3512
		phys_addr_t phys_addr;
3513
		u32 rx_status, timestamp;
M
Matteo Croce 已提交
3514
		int pool, rx_bytes, err, ret;
3515
		void *data;
3516

3517
		rx_done++;
3518 3519 3520 3521 3522 3523 3524
		rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
		rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
		rx_bytes -= MVPP2_MH_SIZE;
		dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
		phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
		data = (void *)phys_to_virt(phys_addr);

3525 3526
		pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
			MVPP2_RXD_BM_POOL_ID_OFFS;
3527 3528 3529 3530 3531 3532 3533
		bm_pool = &port->priv->bm_pools[pool];

		/* In case of an error, release the requested buffer pointer
		 * to the Buffer Manager. This request process is controlled
		 * by the hardware, and the information about the buffer is
		 * comprised by the RX descriptor.
		 */
M
Matteo Croce 已提交
3534 3535
		if (rx_status & MVPP2_RXD_ERR_SUMMARY)
			goto err_drop_frame;
3536

M
Matteo Croce 已提交
3537 3538 3539 3540 3541 3542 3543
		if (port->priv->percpu_pools) {
			pp = port->priv->page_pool[pool];
			dma_dir = page_pool_get_dma_dir(pp);
		} else {
			dma_dir = DMA_FROM_DEVICE;
		}

M
Matteo Croce 已提交
3544 3545
		dma_sync_single_for_cpu(dev->dev.parent, dma_addr,
					rx_bytes + MVPP2_MH_SIZE,
M
Matteo Croce 已提交
3546
					dma_dir);
M
Matteo Croce 已提交
3547

M
Matteo Croce 已提交
3548 3549
		/* Prefetch header */
		prefetch(data);
M
Matteo Croce 已提交
3550

3551 3552 3553 3554 3555
		if (bm_pool->frag_size > PAGE_SIZE)
			frag_size = 0;
		else
			frag_size = bm_pool->frag_size;

M
Matteo Croce 已提交
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
		if (xdp_prog) {
			xdp.data_hard_start = data;
			xdp.data = data + MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM;
			xdp.data_end = xdp.data + rx_bytes;
			xdp.frame_sz = PAGE_SIZE;

			if (bm_pool->pkt_size == MVPP2_BM_SHORT_PKT_SIZE)
				xdp.rxq = &rxq->xdp_rxq_short;
			else
				xdp.rxq = &rxq->xdp_rxq_long;

			xdp_set_data_meta_invalid(&xdp);

S
Sven Auhagen 已提交
3569
			ret = mvpp2_run_xdp(port, rxq, xdp_prog, &xdp, pp, &ps);
M
Matteo Croce 已提交
3570 3571 3572 3573 3574 3575 3576 3577 3578

			if (ret) {
				xdp_ret |= ret;
				err = mvpp2_rx_refill(port, bm_pool, pp, pool);
				if (err) {
					netdev_err(port->dev, "failed to refill BM pools\n");
					goto err_drop_frame;
				}

S
Sven Auhagen 已提交
3579 3580
				ps.rx_packets++;
				ps.rx_bytes += rx_bytes;
M
Matteo Croce 已提交
3581 3582 3583 3584
				continue;
			}
		}

3585 3586 3587 3588 3589
		skb = build_skb(data, frag_size);
		if (!skb) {
			netdev_warn(port->dev, "skb build failed\n");
			goto err_drop_frame;
		}
3590

3591 3592 3593 3594 3595 3596 3597 3598 3599
		/* If we have RX hardware timestamping enabled, grab the
		 * timestamp from the queue and convert.
		 */
		if (mvpp22_rx_hwtstamping(port)) {
			timestamp = le32_to_cpu(rx_desc->pp22.timestamp);
			mvpp22_tai_tstamp(port->priv->tai, timestamp,
					 skb_hwtstamps(skb));
		}

M
Matteo Croce 已提交
3600
		err = mvpp2_rx_refill(port, bm_pool, pp, pool);
3601 3602
		if (err) {
			netdev_err(port->dev, "failed to refill BM pools\n");
3603
			dev_kfree_skb_any(skb);
3604 3605 3606
			goto err_drop_frame;
		}

M
Matteo Croce 已提交
3607 3608 3609 3610 3611 3612
		if (pp)
			page_pool_release_page(pp, virt_to_page(data));
		else
			dma_unmap_single_attrs(dev->dev.parent, dma_addr,
					       bm_pool->buf_size, DMA_FROM_DEVICE,
					       DMA_ATTR_SKIP_CPU_SYNC);
3613

S
Sven Auhagen 已提交
3614 3615
		ps.rx_packets++;
		ps.rx_bytes += rx_bytes;
3616

M
Matteo Croce 已提交
3617
		skb_reserve(skb, MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
3618 3619 3620 3621
		skb_put(skb, rx_bytes);
		skb->protocol = eth_type_trans(skb, dev);
		mvpp2_rx_csum(port, rx_status, skb);

3622
		napi_gro_receive(napi, skb);
M
Matteo Croce 已提交
3623 3624 3625 3626 3627 3628 3629
		continue;

err_drop_frame:
		dev->stats.rx_errors++;
		mvpp2_rx_error(port, rx_desc);
		/* Return the buffer to the pool */
		mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3630 3631
	}

M
Matteo Croce 已提交
3632 3633
	rcu_read_unlock();

M
Matteo Croce 已提交
3634 3635 3636
	if (xdp_ret & MVPP2_XDP_REDIR)
		xdp_do_flush_map();

S
Sven Auhagen 已提交
3637
	if (ps.rx_packets) {
3638 3639 3640
		struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);

		u64_stats_update_begin(&stats->syncp);
S
Sven Auhagen 已提交
3641 3642 3643 3644 3645 3646
		stats->rx_packets += ps.rx_packets;
		stats->rx_bytes   += ps.rx_bytes;
		/* xdp */
		stats->xdp_redirect += ps.xdp_redirect;
		stats->xdp_pass += ps.xdp_pass;
		stats->xdp_drop += ps.xdp_drop;
3647 3648 3649 3650 3651
		u64_stats_update_end(&stats->syncp);
	}

	/* Update Rx queue management counters */
	wmb();
3652
	mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
3653 3654 3655 3656 3657

	return rx_todo;
}

static inline void
3658
tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
3659 3660
		  struct mvpp2_tx_desc *desc)
{
3661
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3662
	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3663

3664 3665 3666 3667
	dma_addr_t buf_dma_addr =
		mvpp2_txdesc_dma_addr_get(port, desc);
	size_t buf_sz =
		mvpp2_txdesc_size_get(port, desc);
3668 3669 3670
	if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
		dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
				 buf_sz, DMA_TO_DEVICE);
3671 3672 3673
	mvpp2_txq_desc_put(txq);
}

3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703
static void mvpp2_txdesc_clear_ptp(struct mvpp2_port *port,
				   struct mvpp2_tx_desc *desc)
{
	/* We only need to clear the low bits */
	if (port->priv->hw_version != MVPP21)
		desc->pp22.ptp_descriptor &=
			cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
}

static bool mvpp2_tx_hw_tstamp(struct mvpp2_port *port,
			       struct mvpp2_tx_desc *tx_desc,
			       struct sk_buff *skb)
{
	struct mvpp2_hwtstamp_queue *queue;
	unsigned int mtype, type, i;
	struct ptp_header *hdr;
	u64 ptpdesc;

	if (port->priv->hw_version == MVPP21 ||
	    port->tx_hwtstamp_type == HWTSTAMP_TX_OFF)
		return false;

	type = ptp_classify_raw(skb);
	if (!type)
		return false;

	hdr = ptp_parse_header(skb, type);
	if (!hdr)
		return false;

R
Russell King 已提交
3704 3705
	skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;

3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761
	ptpdesc = MVPP22_PTP_MACTIMESTAMPINGEN |
		  MVPP22_PTP_ACTION_CAPTURE;
	queue = &port->tx_hwtstamp_queue[0];

	switch (type & PTP_CLASS_VMASK) {
	case PTP_CLASS_V1:
		ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV1);
		break;

	case PTP_CLASS_V2:
		ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV2);
		mtype = hdr->tsmt & 15;
		/* Direct PTP Sync messages to queue 1 */
		if (mtype == 0) {
			ptpdesc |= MVPP22_PTP_TIMESTAMPQUEUESELECT;
			queue = &port->tx_hwtstamp_queue[1];
		}
		break;
	}

	/* Take a reference on the skb and insert into our queue */
	i = queue->next;
	queue->next = (i + 1) & 31;
	if (queue->skb[i])
		dev_kfree_skb_any(queue->skb[i]);
	queue->skb[i] = skb_get(skb);

	ptpdesc |= MVPP22_PTP_TIMESTAMPENTRYID(i);

	/*
	 * 3:0		- PTPAction
	 * 6:4		- PTPPacketFormat
	 * 7		- PTP_CF_WraparoundCheckEn
	 * 9:8		- IngressTimestampSeconds[1:0]
	 * 10		- Reserved
	 * 11		- MACTimestampingEn
	 * 17:12	- PTP_TimestampQueueEntryID[5:0]
	 * 18		- PTPTimestampQueueSelect
	 * 19		- UDPChecksumUpdateEn
	 * 27:20	- TimestampOffset
	 *			PTP, NTPTransmit, OWAMP/TWAMP - L3 to PTP header
	 *			NTPTs, Y.1731 - L3 to timestamp entry
	 * 35:28	- UDP Checksum Offset
	 *
	 * stored in tx descriptor bits 75:64 (11:0) and 191:168 (35:12)
	 */
	tx_desc->pp22.ptp_descriptor &=
		cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
	tx_desc->pp22.ptp_descriptor |=
		cpu_to_le32(ptpdesc & MVPP22_PTP_DESC_MASK_LOW);
	tx_desc->pp22.buf_dma_addr_ptp &= cpu_to_le64(~0xffffff0000000000ULL);
	tx_desc->pp22.buf_dma_addr_ptp |= cpu_to_le64((ptpdesc >> 12) << 40);

	return true;
}

3762 3763 3764 3765 3766
/* Handle tx fragmentation processing */
static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
				 struct mvpp2_tx_queue *aggr_txq,
				 struct mvpp2_tx_queue *txq)
{
3767
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3768
	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3769 3770
	struct mvpp2_tx_desc *tx_desc;
	int i;
3771
	dma_addr_t buf_dma_addr;
3772 3773 3774

	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3775
		void *addr = skb_frag_address(frag);
3776 3777

		tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3778
		mvpp2_txdesc_clear_ptp(port, tx_desc);
3779
		mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3780
		mvpp2_txdesc_size_set(port, tx_desc, skb_frag_size(frag));
3781

3782
		buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
3783 3784
					      skb_frag_size(frag),
					      DMA_TO_DEVICE);
3785
		if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
3786
			mvpp2_txq_desc_put(txq);
3787
			goto cleanup;
3788 3789
		}

3790
		mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3791 3792 3793

		if (i == (skb_shinfo(skb)->nr_frags - 1)) {
			/* Last descriptor */
3794 3795
			mvpp2_txdesc_cmd_set(port, tx_desc,
					     MVPP2_TXD_L_DESC);
M
Matteo Croce 已提交
3796
			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
3797 3798
		} else {
			/* Descriptor in the middle: Not First, Not Last */
3799
			mvpp2_txdesc_cmd_set(port, tx_desc, 0);
M
Matteo Croce 已提交
3800
			mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3801 3802 3803 3804
		}
	}

	return 0;
3805
cleanup:
3806 3807 3808 3809 3810
	/* Release all descriptors that were used to map fragments of
	 * this packet, as well as the corresponding DMA mappings
	 */
	for (i = i - 1; i >= 0; i--) {
		tx_desc = txq->descs + i;
3811
		tx_desc_unmap_put(port, txq, tx_desc);
3812 3813 3814 3815 3816
	}

	return -ENOMEM;
}

3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827
static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
				     struct net_device *dev,
				     struct mvpp2_tx_queue *txq,
				     struct mvpp2_tx_queue *aggr_txq,
				     struct mvpp2_txq_pcpu *txq_pcpu,
				     int hdr_sz)
{
	struct mvpp2_port *port = netdev_priv(dev);
	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
	dma_addr_t addr;

3828
	mvpp2_txdesc_clear_ptp(port, tx_desc);
3829 3830 3831 3832 3833
	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
	mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);

	addr = txq_pcpu->tso_headers_dma +
	       txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
3834
	mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
3835 3836 3837 3838

	mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
					    MVPP2_TXD_F_DESC |
					    MVPP2_TXD_PADDING_DISABLE);
M
Matteo Croce 已提交
3839
	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852
}

static inline int mvpp2_tso_put_data(struct sk_buff *skb,
				     struct net_device *dev, struct tso_t *tso,
				     struct mvpp2_tx_queue *txq,
				     struct mvpp2_tx_queue *aggr_txq,
				     struct mvpp2_txq_pcpu *txq_pcpu,
				     int sz, bool left, bool last)
{
	struct mvpp2_port *port = netdev_priv(dev);
	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
	dma_addr_t buf_dma_addr;

3853
	mvpp2_txdesc_clear_ptp(port, tx_desc);
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863
	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
	mvpp2_txdesc_size_set(port, tx_desc, sz);

	buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
				      DMA_TO_DEVICE);
	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
		mvpp2_txq_desc_put(txq);
		return -ENOMEM;
	}

3864
	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3865 3866 3867 3868

	if (!left) {
		mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
		if (last) {
M
Matteo Croce 已提交
3869
			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
3870 3871 3872 3873 3874 3875
			return 0;
		}
	} else {
		mvpp2_txdesc_cmd_set(port, tx_desc, 0);
	}

M
Matteo Croce 已提交
3876
	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3877 3878 3879 3880 3881 3882 3883 3884 3885
	return 0;
}

static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
			struct mvpp2_tx_queue *txq,
			struct mvpp2_tx_queue *aggr_txq,
			struct mvpp2_txq_pcpu *txq_pcpu)
{
	struct mvpp2_port *port = netdev_priv(dev);
3886
	int hdr_sz, i, len, descs = 0;
3887 3888 3889
	struct tso_t tso;

	/* Check number of available descriptors */
3890
	if (mvpp2_aggr_desc_num_check(port, aggr_txq, tso_count_descs(skb)) ||
3891
	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu,
3892 3893 3894
					     tso_count_descs(skb)))
		return 0;

3895 3896
	hdr_sz = tso_start(skb, &tso);

3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
	len = skb->len - hdr_sz;
	while (len > 0) {
		int left = min_t(int, skb_shinfo(skb)->gso_size, len);
		char *hdr = txq_pcpu->tso_headers +
			    txq_pcpu->txq_put_index * TSO_HEADER_SIZE;

		len -= left;
		descs++;

		tso_build_hdr(skb, hdr, &tso, left, len == 0);
		mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);

		while (left > 0) {
			int sz = min_t(int, tso.size, left);
			left -= sz;
			descs++;

			if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
					       txq_pcpu, sz, left, len == 0))
				goto release;
			tso_build_data(skb, &tso, sz);
		}
	}

	return descs;

release:
	for (i = descs - 1; i >= 0; i--) {
		struct mvpp2_tx_desc *tx_desc = txq->descs + i;
		tx_desc_unmap_put(port, txq, tx_desc);
	}
	return 0;
}

3931
/* Main tx processing */
3932
static netdev_tx_t mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
3933 3934 3935 3936 3937
{
	struct mvpp2_port *port = netdev_priv(dev);
	struct mvpp2_tx_queue *txq, *aggr_txq;
	struct mvpp2_txq_pcpu *txq_pcpu;
	struct mvpp2_tx_desc *tx_desc;
3938
	dma_addr_t buf_dma_addr;
3939
	unsigned long flags = 0;
3940
	unsigned int thread;
3941 3942 3943 3944
	int frags = 0;
	u16 txq_id;
	u32 tx_cmd;

3945
	thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3946

3947 3948
	txq_id = skb_get_queue_mapping(skb);
	txq = port->txqs[txq_id];
3949 3950
	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
	aggr_txq = &port->priv->aggr_txqs[thread];
3951

3952 3953 3954
	if (test_bit(thread, &port->priv->lock_map))
		spin_lock_irqsave(&port->tx_lock[thread], flags);

3955 3956 3957 3958
	if (skb_is_gso(skb)) {
		frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
		goto out;
	}
3959 3960 3961
	frags = skb_shinfo(skb)->nr_frags + 1;

	/* Check number of available descriptors */
3962
	if (mvpp2_aggr_desc_num_check(port, aggr_txq, frags) ||
3963
	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, frags)) {
3964 3965 3966 3967 3968 3969
		frags = 0;
		goto out;
	}

	/* Get a descriptor for the first part of the packet */
	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3970 3971 3972
	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
	    !mvpp2_tx_hw_tstamp(port, tx_desc, skb))
		mvpp2_txdesc_clear_ptp(port, tx_desc);
3973 3974
	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
	mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
3975

3976
	buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
3977
				      skb_headlen(skb), DMA_TO_DEVICE);
3978
	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
3979 3980 3981 3982
		mvpp2_txq_desc_put(txq);
		frags = 0;
		goto out;
	}
3983

3984
	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3985 3986 3987 3988 3989 3990

	tx_cmd = mvpp2_skb_tx_csum(port, skb);

	if (frags == 1) {
		/* First and Last descriptor */
		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
3991
		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
M
Matteo Croce 已提交
3992
		mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
3993 3994 3995
	} else {
		/* First but not Last */
		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
3996
		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
M
Matteo Croce 已提交
3997
		mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3998 3999 4000

		/* Continue with other skb fragments */
		if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
4001
			tx_desc_unmap_put(port, txq, tx_desc);
4002 4003 4004 4005 4006 4007
			frags = 0;
		}
	}

out:
	if (frags > 0) {
4008
		struct mvpp2_pcpu_stats *stats = per_cpu_ptr(port->stats, thread);
4009 4010 4011 4012 4013 4014 4015 4016 4017 4018
		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);

		txq_pcpu->reserved_num -= frags;
		txq_pcpu->count += frags;
		aggr_txq->count += frags;

		/* Enable transmit */
		wmb();
		mvpp2_aggr_txq_pend_desc_add(port, frags);

4019
		if (txq_pcpu->count >= txq_pcpu->stop_threshold)
4020
			netif_tx_stop_queue(nq);
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030

		u64_stats_update_begin(&stats->syncp);
		stats->tx_packets++;
		stats->tx_bytes += skb->len;
		u64_stats_update_end(&stats->syncp);
	} else {
		dev->stats.tx_dropped++;
		dev_kfree_skb_any(skb);
	}

4031
	/* Finalize TX processing */
4032
	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
4033 4034 4035
		mvpp2_txq_done(port, txq, txq_pcpu);

	/* Set the timer in case not all frags were processed */
4036 4037
	if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
	    txq_pcpu->count > 0) {
4038
		struct mvpp2_port_pcpu *port_pcpu = per_cpu_ptr(port->pcpu, thread);
4039

4040 4041 4042 4043 4044 4045
		if (!port_pcpu->timer_scheduled) {
			port_pcpu->timer_scheduled = true;
			hrtimer_start(&port_pcpu->tx_done_timer,
				      MVPP2_TXDONE_HRTIMER_PERIOD_NS,
				      HRTIMER_MODE_REL_PINNED_SOFT);
		}
4046 4047
	}

4048 4049 4050
	if (test_bit(thread, &port->priv->lock_map))
		spin_unlock_irqrestore(&port->tx_lock[thread], flags);

4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063
	return NETDEV_TX_OK;
}

static inline void mvpp2_cause_error(struct net_device *dev, int cause)
{
	if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
		netdev_err(dev, "FCS error\n");
	if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
		netdev_err(dev, "rx fifo overrun error\n");
	if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
		netdev_err(dev, "tx fifo underrun error\n");
}

4064
static int mvpp2_poll(struct napi_struct *napi, int budget)
4065
{
4066
	u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
4067 4068
	int rx_done = 0;
	struct mvpp2_port *port = netdev_priv(napi->dev);
4069
	struct mvpp2_queue_vector *qv;
4070
	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4071

4072 4073
	qv = container_of(napi, struct mvpp2_queue_vector, napi);

4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
	/* Rx/Tx cause register
	 *
	 * Bits 0-15: each bit indicates received packets on the Rx queue
	 * (bit 0 is for Rx queue 0).
	 *
	 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
	 * (bit 16 is for Tx queue 0).
	 *
	 * Each CPU has its own Rx/Tx cause register
	 */
4084
	cause_rx_tx = mvpp2_thread_read_relaxed(port->priv, qv->sw_thread_id,
4085
						MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
4086

4087
	cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
4088 4089 4090 4091 4092
	if (cause_misc) {
		mvpp2_cause_error(port->dev, cause_misc);

		/* Clear the cause register */
		mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
4093
		mvpp2_thread_write(port->priv, thread,
4094 4095
				   MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
				   cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
4096 4097
	}

4098 4099 4100 4101 4102 4103
	if (port->has_tx_irqs) {
		cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
		if (cause_tx) {
			cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
			mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
		}
4104
	}
4105 4106

	/* Process RX packets */
4107 4108
	cause_rx = cause_rx_tx &
		   MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
4109
	cause_rx <<= qv->first_rxq;
4110
	cause_rx |= qv->pending_cause_rx;
4111 4112 4113 4114 4115 4116 4117 4118
	while (cause_rx && budget > 0) {
		int count;
		struct mvpp2_rx_queue *rxq;

		rxq = mvpp2_get_rx_queue(port, cause_rx);
		if (!rxq)
			break;

4119
		count = mvpp2_rx(port, napi, budget, rxq);
4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132
		rx_done += count;
		budget -= count;
		if (budget > 0) {
			/* Clear the bit associated to this Rx queue
			 * so that next iteration will continue from
			 * the next Rx queue.
			 */
			cause_rx &= ~(1 << rxq->logic_rxq);
		}
	}

	if (budget > 0) {
		cause_rx = 0;
4133
		napi_complete_done(napi, rx_done);
4134

4135
		mvpp2_qvec_interrupt_enable(qv);
4136
	}
4137
	qv->pending_cause_rx = cause_rx;
4138 4139 4140
	return rx_done;
}

A
Antoine Tenart 已提交
4141
static void mvpp22_mode_reconfigure(struct mvpp2_port *port)
4142
{
A
Antoine Tenart 已提交
4143 4144
	u32 ctrl3;

4145 4146 4147
	/* Set the GMAC & XLG MAC in reset */
	mvpp2_mac_reset_assert(port);

4148 4149 4150
	/* Set the MPCS and XPCS in reset */
	mvpp22_pcs_reset_assert(port);

A
Antoine Tenart 已提交
4151 4152 4153 4154 4155 4156
	/* comphy reconfiguration */
	mvpp22_comphy_init(port);

	/* gop reconfiguration */
	mvpp22_gop_init(port);

4157 4158
	mvpp22_pcs_reset_deassert(port);

4159
	if (mvpp2_port_supports_xlg(port)) {
A
Antoine Tenart 已提交
4160 4161 4162
		ctrl3 = readl(port->base + MVPP22_XLG_CTRL3_REG);
		ctrl3 &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;

4163
		if (mvpp2_is_xlg(port->phy_interface))
A
Antoine Tenart 已提交
4164 4165 4166 4167 4168 4169
			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
		else
			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;

		writel(ctrl3, port->base + MVPP22_XLG_CTRL3_REG);
	}
4170

4171
	if (mvpp2_port_supports_xlg(port) && mvpp2_is_xlg(port->phy_interface))
4172 4173 4174
		mvpp2_xlg_max_rx_size_set(port);
	else
		mvpp2_gmac_max_rx_size_set(port);
A
Antoine Tenart 已提交
4175 4176 4177 4178 4179 4180
}

/* Set hw internals when starting port */
static void mvpp2_start_dev(struct mvpp2_port *port)
{
	int i;
4181

4182 4183
	mvpp2_txp_max_tx_size_set(port);

4184 4185
	for (i = 0; i < port->nqvecs; i++)
		napi_enable(&port->qvecs[i].napi);
4186

4187
	/* Enable interrupts on all threads */
4188 4189
	mvpp2_interrupts_enable(port);

A
Antoine Tenart 已提交
4190 4191 4192 4193 4194 4195
	if (port->priv->hw_version == MVPP22)
		mvpp22_mode_reconfigure(port);

	if (port->phylink) {
		phylink_start(port->phylink);
	} else {
R
Russell King 已提交
4196
		mvpp2_acpi_start(port);
4197
	}
A
Antoine Ténart 已提交
4198

4199
	netif_tx_start_all_queues(port->dev);
M
Matteo Croce 已提交
4200 4201

	clear_bit(0, &port->state);
4202 4203 4204 4205 4206
}

/* Set hw internals when stopping port */
static void mvpp2_stop_dev(struct mvpp2_port *port)
{
4207
	int i;
4208

M
Matteo Croce 已提交
4209 4210
	set_bit(0, &port->state);

4211
	/* Disable interrupts on all threads */
4212 4213
	mvpp2_interrupts_disable(port);

4214 4215
	for (i = 0; i < port->nqvecs; i++)
		napi_disable(&port->qvecs[i].napi);
4216

A
Antoine Tenart 已提交
4217 4218
	if (port->phylink)
		phylink_stop(port->phylink);
4219
	phy_power_off(port->comphy);
4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230
}

static int mvpp2_check_ringparam_valid(struct net_device *dev,
				       struct ethtool_ringparam *ring)
{
	u16 new_rx_pending = ring->rx_pending;
	u16 new_tx_pending = ring->tx_pending;

	if (ring->rx_pending == 0 || ring->tx_pending == 0)
		return -EINVAL;

4231 4232
	if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
		new_rx_pending = MVPP2_MAX_RXD_MAX;
4233 4234 4235
	else if (!IS_ALIGNED(ring->rx_pending, 16))
		new_rx_pending = ALIGN(ring->rx_pending, 16);

4236 4237
	if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
		new_tx_pending = MVPP2_MAX_TXD_MAX;
4238 4239 4240
	else if (!IS_ALIGNED(ring->tx_pending, 32))
		new_tx_pending = ALIGN(ring->tx_pending, 32);

4241 4242 4243 4244 4245 4246
	/* The Tx ring size cannot be smaller than the minimum number of
	 * descriptors needed for TSO.
	 */
	if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
		new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);

4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261
	if (ring->rx_pending != new_rx_pending) {
		netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
			    ring->rx_pending, new_rx_pending);
		ring->rx_pending = new_rx_pending;
	}

	if (ring->tx_pending != new_tx_pending) {
		netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
			    ring->tx_pending, new_tx_pending);
		ring->tx_pending = new_tx_pending;
	}

	return 0;
}

4262
static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
{
	u32 mac_addr_l, mac_addr_m, mac_addr_h;

	mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
	mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
	mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
	addr[0] = (mac_addr_h >> 24) & 0xFF;
	addr[1] = (mac_addr_h >> 16) & 0xFF;
	addr[2] = (mac_addr_h >> 8) & 0xFF;
	addr[3] = mac_addr_h & 0xFF;
	addr[4] = mac_addr_m & 0xFF;
	addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
}

4277 4278 4279 4280 4281 4282 4283
static int mvpp2_irqs_init(struct mvpp2_port *port)
{
	int err, i;

	for (i = 0; i < port->nqvecs; i++) {
		struct mvpp2_queue_vector *qv = port->qvecs + i;

4284 4285 4286 4287 4288 4289 4290
		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
			qv->mask = kzalloc(cpumask_size(), GFP_KERNEL);
			if (!qv->mask) {
				err = -ENOMEM;
				goto err;
			}

4291
			irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
4292
		}
4293

4294 4295 4296
		err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
		if (err)
			goto err;
4297

4298 4299 4300 4301 4302 4303
		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
			unsigned int cpu;

			for_each_present_cpu(cpu) {
				if (mvpp2_cpu_to_thread(port->priv, cpu) ==
				    qv->sw_thread_id)
4304
					cpumask_set_cpu(cpu, qv->mask);
4305 4306
			}

4307
			irq_set_affinity_hint(qv->irq, qv->mask);
4308
		}
4309 4310 4311 4312 4313 4314 4315
	}

	return 0;
err:
	for (i = 0; i < port->nqvecs; i++) {
		struct mvpp2_queue_vector *qv = port->qvecs + i;

4316
		irq_set_affinity_hint(qv->irq, NULL);
4317 4318
		kfree(qv->mask);
		qv->mask = NULL;
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331
		free_irq(qv->irq, qv);
	}

	return err;
}

static void mvpp2_irqs_deinit(struct mvpp2_port *port)
{
	int i;

	for (i = 0; i < port->nqvecs; i++) {
		struct mvpp2_queue_vector *qv = port->qvecs + i;

4332
		irq_set_affinity_hint(qv->irq, NULL);
4333 4334
		kfree(qv->mask);
		qv->mask = NULL;
4335
		irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
4336 4337 4338 4339
		free_irq(qv->irq, qv);
	}
}

4340 4341 4342 4343 4344
static bool mvpp22_rss_is_supported(void)
{
	return queue_mode == MVPP2_QDIST_MULTI_MODE;
}

4345 4346 4347
static int mvpp2_open(struct net_device *dev)
{
	struct mvpp2_port *port = netdev_priv(dev);
4348
	struct mvpp2 *priv = port->priv;
4349 4350
	unsigned char mac_bcast[ETH_ALEN] = {
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
A
Antoine Tenart 已提交
4351
	bool valid = false;
4352 4353
	int err;

4354
	err = mvpp2_prs_mac_da_accept(port, mac_bcast, true);
4355 4356 4357 4358
	if (err) {
		netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
		return err;
	}
4359
	err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, true);
4360
	if (err) {
4361
		netdev_err(dev, "mvpp2_prs_mac_da_accept own addr failed\n");
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387
		return err;
	}
	err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
	if (err) {
		netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
		return err;
	}
	err = mvpp2_prs_def_flow(port);
	if (err) {
		netdev_err(dev, "mvpp2_prs_def_flow failed\n");
		return err;
	}

	/* Allocate the Rx/Tx queues */
	err = mvpp2_setup_rxqs(port);
	if (err) {
		netdev_err(port->dev, "cannot allocate Rx queues\n");
		return err;
	}

	err = mvpp2_setup_txqs(port);
	if (err) {
		netdev_err(port->dev, "cannot allocate Tx queues\n");
		goto err_cleanup_rxqs;
	}

4388
	err = mvpp2_irqs_init(port);
4389
	if (err) {
4390
		netdev_err(port->dev, "cannot init IRQs\n");
4391 4392 4393
		goto err_cleanup_txqs;
	}

A
Antoine Tenart 已提交
4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405
	/* Phylink isn't supported yet in ACPI mode */
	if (port->of_node) {
		err = phylink_of_phy_connect(port->phylink, port->of_node, 0);
		if (err) {
			netdev_err(port->dev, "could not attach PHY (%d)\n",
				   err);
			goto err_free_irq;
		}

		valid = true;
	}

4406 4407
	if (priv->hw_version == MVPP22 && port->port_irq) {
		err = request_irq(port->port_irq, mvpp2_port_isr, 0,
4408 4409
				  dev->name, port);
		if (err) {
4410 4411 4412
			netdev_err(port->dev,
				   "cannot request port link/ptp IRQ %d\n",
				   port->port_irq);
4413 4414 4415 4416 4417
			goto err_free_irq;
		}

		mvpp22_gop_setup_irq(port);

A
Antoine Tenart 已提交
4418 4419
		/* In default link is down */
		netif_carrier_off(port->dev);
4420

A
Antoine Tenart 已提交
4421 4422
		valid = true;
	} else {
4423
		port->port_irq = 0;
A
Antoine Tenart 已提交
4424 4425 4426 4427 4428 4429 4430
	}

	if (!valid) {
		netdev_err(port->dev,
			   "invalid configuration: no dt or link IRQ");
		goto err_free_irq;
	}
4431 4432 4433

	/* Unmask interrupts on all CPUs */
	on_each_cpu(mvpp2_interrupts_unmask, port, 1);
4434
	mvpp2_shared_interrupt_mask_unmask(port, false);
4435 4436 4437

	mvpp2_start_dev(port);

4438
	/* Start hardware statistics gathering */
4439
	queue_delayed_work(priv->stats_queue, &port->stats_work,
4440 4441
			   MVPP2_MIB_COUNTERS_STATS_DELAY);

4442 4443 4444
	return 0;

err_free_irq:
4445
	mvpp2_irqs_deinit(port);
4446 4447 4448 4449 4450 4451 4452 4453 4454 4455
err_cleanup_txqs:
	mvpp2_cleanup_txqs(port);
err_cleanup_rxqs:
	mvpp2_cleanup_rxqs(port);
	return err;
}

static int mvpp2_stop(struct net_device *dev)
{
	struct mvpp2_port *port = netdev_priv(dev);
4456
	struct mvpp2_port_pcpu *port_pcpu;
4457
	unsigned int thread;
4458 4459 4460

	mvpp2_stop_dev(port);

4461
	/* Mask interrupts on all threads */
4462
	on_each_cpu(mvpp2_interrupts_mask, port, 1);
4463
	mvpp2_shared_interrupt_mask_unmask(port, true);
4464

A
Antoine Tenart 已提交
4465 4466
	if (port->phylink)
		phylink_disconnect_phy(port->phylink);
4467 4468
	if (port->port_irq)
		free_irq(port->port_irq, port);
4469

4470
	mvpp2_irqs_deinit(port);
4471
	if (!port->has_tx_irqs) {
4472
		for (thread = 0; thread < port->priv->nthreads; thread++) {
4473
			port_pcpu = per_cpu_ptr(port->pcpu, thread);
4474

4475 4476 4477
			hrtimer_cancel(&port_pcpu->tx_done_timer);
			port_pcpu->timer_scheduled = false;
		}
4478
	}
4479 4480 4481
	mvpp2_cleanup_rxqs(port);
	mvpp2_cleanup_txqs(port);

4482
	cancel_delayed_work_sync(&port->stats_work);
4483

4484 4485 4486
	mvpp2_mac_reset_assert(port);
	mvpp22_pcs_reset_assert(port);

4487 4488 4489
	return 0;
}

4490 4491
static int mvpp2_prs_mac_da_accept_list(struct mvpp2_port *port,
					struct netdev_hw_addr_list *list)
4492 4493
{
	struct netdev_hw_addr *ha;
4494 4495 4496 4497 4498 4499
	int ret;

	netdev_hw_addr_list_for_each(ha, list) {
		ret = mvpp2_prs_mac_da_accept(port, ha->addr, true);
		if (ret)
			return ret;
4500
	}
4501

4502 4503 4504 4505 4506 4507
	return 0;
}

static void mvpp2_set_rx_promisc(struct mvpp2_port *port, bool enable)
{
	if (!enable && (port->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
4508
		mvpp2_prs_vid_enable_filtering(port);
4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547
	else
		mvpp2_prs_vid_disable_filtering(port);

	mvpp2_prs_mac_promisc_set(port->priv, port->id,
				  MVPP2_PRS_L2_UNI_CAST, enable);

	mvpp2_prs_mac_promisc_set(port->priv, port->id,
				  MVPP2_PRS_L2_MULTI_CAST, enable);
}

static void mvpp2_set_rx_mode(struct net_device *dev)
{
	struct mvpp2_port *port = netdev_priv(dev);

	/* Clear the whole UC and MC list */
	mvpp2_prs_mac_del_all(port);

	if (dev->flags & IFF_PROMISC) {
		mvpp2_set_rx_promisc(port, true);
		return;
	}

	mvpp2_set_rx_promisc(port, false);

	if (netdev_uc_count(dev) > MVPP2_PRS_MAC_UC_FILT_MAX ||
	    mvpp2_prs_mac_da_accept_list(port, &dev->uc))
		mvpp2_prs_mac_promisc_set(port->priv, port->id,
					  MVPP2_PRS_L2_UNI_CAST, true);

	if (dev->flags & IFF_ALLMULTI) {
		mvpp2_prs_mac_promisc_set(port->priv, port->id,
					  MVPP2_PRS_L2_MULTI_CAST, true);
		return;
	}

	if (netdev_mc_count(dev) > MVPP2_PRS_MAC_MC_FILT_MAX ||
	    mvpp2_prs_mac_da_accept_list(port, &dev->mc))
		mvpp2_prs_mac_promisc_set(port->priv, port->id,
					  MVPP2_PRS_L2_MULTI_CAST, true);
4548 4549 4550 4551 4552 4553 4554
}

static int mvpp2_set_mac_address(struct net_device *dev, void *p)
{
	const struct sockaddr *addr = p;
	int err;

4555 4556
	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;
4557 4558

	err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
4559 4560 4561 4562 4563
	if (err) {
		/* Reconfigure parser accept the original MAC address */
		mvpp2_prs_update_mac_da(dev, dev->dev_addr);
		netdev_err(dev, "failed to change MAC address\n");
	}
4564 4565 4566
	return err;
}

M
Matteo Croce 已提交
4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603
/* Shut down all the ports, reconfigure the pools as percpu or shared,
 * then bring up again all ports.
 */
static int mvpp2_bm_switch_buffers(struct mvpp2 *priv, bool percpu)
{
	int numbufs = MVPP2_BM_POOLS_NUM, i;
	struct mvpp2_port *port = NULL;
	bool status[MVPP2_MAX_PORTS];

	for (i = 0; i < priv->port_count; i++) {
		port = priv->port_list[i];
		status[i] = netif_running(port->dev);
		if (status[i])
			mvpp2_stop(port->dev);
	}

	/* nrxqs is the same for all ports */
	if (priv->percpu_pools)
		numbufs = port->nrxqs * 2;

	for (i = 0; i < numbufs; i++)
		mvpp2_bm_pool_destroy(port->dev->dev.parent, priv, &priv->bm_pools[i]);

	devm_kfree(port->dev->dev.parent, priv->bm_pools);
	priv->percpu_pools = percpu;
	mvpp2_bm_init(port->dev->dev.parent, priv);

	for (i = 0; i < priv->port_count; i++) {
		port = priv->port_list[i];
		mvpp2_swf_bm_pool_init(port);
		if (status[i])
			mvpp2_open(port->dev);
	}

	return 0;
}

4604 4605 4606
static int mvpp2_change_mtu(struct net_device *dev, int mtu)
{
	struct mvpp2_port *port = netdev_priv(dev);
M
Matteo Croce 已提交
4607
	bool running = netif_running(dev);
M
Matteo Croce 已提交
4608
	struct mvpp2 *priv = port->priv;
4609 4610
	int err;

4611 4612 4613 4614
	if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
		netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
			    ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
		mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
4615 4616
	}

M
Matteo Croce 已提交
4617
	if (MVPP2_RX_PKT_SIZE(mtu) > MVPP2_BM_LONG_PKT_SIZE) {
M
Matteo Croce 已提交
4618 4619 4620 4621
		if (port->xdp_prog) {
			netdev_err(dev, "Jumbo frames are not supported with XDP\n");
			return -EINVAL;
		}
M
Matteo Croce 已提交
4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
		if (priv->percpu_pools) {
			netdev_warn(dev, "mtu %d too high, switching to shared buffers", mtu);
			mvpp2_bm_switch_buffers(priv, false);
		}
	} else {
		bool jumbo = false;
		int i;

		for (i = 0; i < priv->port_count; i++)
			if (priv->port_list[i] != port &&
			    MVPP2_RX_PKT_SIZE(priv->port_list[i]->dev->mtu) >
			    MVPP2_BM_LONG_PKT_SIZE) {
				jumbo = true;
				break;
			}

		/* No port is using jumbo frames */
		if (!jumbo) {
			dev_info(port->dev->dev.parent,
				 "all ports have a low MTU, switching to per-cpu buffers");
			mvpp2_bm_switch_buffers(priv, true);
		}
	}

M
Matteo Croce 已提交
4646 4647
	if (running)
		mvpp2_stop_dev(port);
4648 4649

	err = mvpp2_bm_update_mtu(dev, mtu);
M
Matteo Croce 已提交
4650 4651 4652 4653 4654
	if (err) {
		netdev_err(dev, "failed to change MTU\n");
		/* Reconfigure BM to the original MTU */
		mvpp2_bm_update_mtu(dev, dev->mtu);
	} else {
4655 4656 4657
		port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
	}

M
Matteo Croce 已提交
4658 4659 4660 4661 4662
	if (running) {
		mvpp2_start_dev(port);
		mvpp2_egress_enable(port);
		mvpp2_ingress_enable(port);
	}
4663 4664 4665 4666

	return err;
}

M
Matteo Croce 已提交
4667 4668 4669 4670 4671 4672 4673 4674 4675
static int mvpp2_check_pagepool_dma(struct mvpp2_port *port)
{
	enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
	struct mvpp2 *priv = port->priv;
	int err = -1, i;

	if (!priv->percpu_pools)
		return err;

M
Matteo Croce 已提交
4676
	if (!priv->page_pool[0])
M
Matteo Croce 已提交
4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693
		return -ENOMEM;

	for (i = 0; i < priv->port_count; i++) {
		port = priv->port_list[i];
		if (port->xdp_prog) {
			dma_dir = DMA_BIDIRECTIONAL;
			break;
		}
	}

	/* All pools are equal in terms of DMA direction */
	if (priv->page_pool[0]->p.dma_dir != dma_dir)
		err = mvpp2_bm_switch_buffers(priv, true);

	return err;
}

4694
static void
4695 4696 4697 4698
mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
{
	struct mvpp2_port *port = netdev_priv(dev);
	unsigned int start;
4699
	unsigned int cpu;
4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727

	for_each_possible_cpu(cpu) {
		struct mvpp2_pcpu_stats *cpu_stats;
		u64 rx_packets;
		u64 rx_bytes;
		u64 tx_packets;
		u64 tx_bytes;

		cpu_stats = per_cpu_ptr(port->stats, cpu);
		do {
			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
			rx_packets = cpu_stats->rx_packets;
			rx_bytes   = cpu_stats->rx_bytes;
			tx_packets = cpu_stats->tx_packets;
			tx_bytes   = cpu_stats->tx_bytes;
		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));

		stats->rx_packets += rx_packets;
		stats->rx_bytes   += rx_bytes;
		stats->tx_packets += tx_packets;
		stats->tx_bytes   += tx_bytes;
	}

	stats->rx_errors	= dev->stats.rx_errors;
	stats->rx_dropped	= dev->stats.rx_dropped;
	stats->tx_dropped	= dev->stats.tx_dropped;
}

4728 4729 4730 4731
static int mvpp2_set_ts_config(struct mvpp2_port *port, struct ifreq *ifr)
{
	struct hwtstamp_config config;
	void __iomem *ptp;
4732
	u32 gcr, int_mask;
4733 4734 4735 4736 4737 4738 4739

	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
		return -EFAULT;

	if (config.flags)
		return -EINVAL;

4740 4741
	if (config.tx_type != HWTSTAMP_TX_OFF &&
	    config.tx_type != HWTSTAMP_TX_ON)
4742 4743 4744
		return -ERANGE;

	ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760

	int_mask = gcr = 0;
	if (config.tx_type != HWTSTAMP_TX_OFF) {
		gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_TX_RESET;
		int_mask |= MVPP22_PTP_INT_MASK_QUEUE1 |
			    MVPP22_PTP_INT_MASK_QUEUE0;
	}

	/* It seems we must also release the TX reset when enabling the TSU */
	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
		gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_RX_RESET |
		       MVPP22_PTP_GCR_TX_RESET;

	if (gcr & MVPP22_PTP_GCR_TSU_ENABLE)
		mvpp22_tai_start(port->priv->tai);

4761 4762 4763 4764 4765
	if (config.rx_filter != HWTSTAMP_FILTER_NONE) {
		config.rx_filter = HWTSTAMP_FILTER_ALL;
		mvpp2_modify(ptp + MVPP22_PTP_GCR,
			     MVPP22_PTP_GCR_RX_RESET |
			     MVPP22_PTP_GCR_TX_RESET |
4766
			     MVPP22_PTP_GCR_TSU_ENABLE, gcr);
4767 4768 4769 4770 4771 4772
		port->rx_hwtstamp = true;
	} else {
		port->rx_hwtstamp = false;
		mvpp2_modify(ptp + MVPP22_PTP_GCR,
			     MVPP22_PTP_GCR_RX_RESET |
			     MVPP22_PTP_GCR_TX_RESET |
4773
			     MVPP22_PTP_GCR_TSU_ENABLE, gcr);
4774 4775
	}

4776 4777 4778 4779 4780 4781 4782 4783 4784
	mvpp2_modify(ptp + MVPP22_PTP_INT_MASK,
		     MVPP22_PTP_INT_MASK_QUEUE1 |
		     MVPP22_PTP_INT_MASK_QUEUE0, int_mask);

	if (!(gcr & MVPP22_PTP_GCR_TSU_ENABLE))
		mvpp22_tai_stop(port->priv->tai);

	port->tx_hwtstamp_type = config.tx_type;

4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796
	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
		return -EFAULT;

	return 0;
}

static int mvpp2_get_ts_config(struct mvpp2_port *port, struct ifreq *ifr)
{
	struct hwtstamp_config config;

	memset(&config, 0, sizeof(config));

4797
	config.tx_type = port->tx_hwtstamp_type;
4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818
	config.rx_filter = port->rx_hwtstamp ?
		HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;

	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
		return -EFAULT;

	return 0;
}

static int mvpp2_ethtool_get_ts_info(struct net_device *dev,
				     struct ethtool_ts_info *info)
{
	struct mvpp2_port *port = netdev_priv(dev);

	if (!port->hwtstamp)
		return -EOPNOTSUPP;

	info->phc_index = mvpp22_tai_ptp_clock_index(port->priv->tai);
	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
				SOF_TIMESTAMPING_RX_SOFTWARE |
				SOF_TIMESTAMPING_SOFTWARE |
4819
				SOF_TIMESTAMPING_TX_HARDWARE |
4820 4821
				SOF_TIMESTAMPING_RX_HARDWARE |
				SOF_TIMESTAMPING_RAW_HARDWARE;
4822 4823
	info->tx_types = BIT(HWTSTAMP_TX_OFF) |
			 BIT(HWTSTAMP_TX_ON);
4824 4825 4826 4827 4828 4829
	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
			   BIT(HWTSTAMP_FILTER_ALL);

	return 0;
}

4830 4831
static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
A
Antoine Tenart 已提交
4832
	struct mvpp2_port *port = netdev_priv(dev);
4833

4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845
	switch (cmd) {
	case SIOCSHWTSTAMP:
		if (port->hwtstamp)
			return mvpp2_set_ts_config(port, ifr);
		break;

	case SIOCGHWTSTAMP:
		if (port->hwtstamp)
			return mvpp2_get_ts_config(port, ifr);
		break;
	}

A
Antoine Tenart 已提交
4846
	if (!port->phylink)
4847 4848
		return -ENOTSUPP;

A
Antoine Tenart 已提交
4849
	return phylink_mii_ioctl(port->phylink, ifr, cmd);
4850 4851
}

4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
{
	struct mvpp2_port *port = netdev_priv(dev);
	int ret;

	ret = mvpp2_prs_vid_entry_add(port, vid);
	if (ret)
		netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
			   MVPP2_PRS_VLAN_FILT_MAX - 1);
	return ret;
}

static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
{
	struct mvpp2_port *port = netdev_priv(dev);

	mvpp2_prs_vid_entry_remove(port, vid);
	return 0;
}

static int mvpp2_set_features(struct net_device *dev,
			      netdev_features_t features)
{
	netdev_features_t changed = dev->features ^ features;
	struct mvpp2_port *port = netdev_priv(dev);

	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
		if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
			mvpp2_prs_vid_enable_filtering(port);
		} else {
			/* Invalidate all registered VID filters for this
			 * port
			 */
			mvpp2_prs_vid_remove_all(port);

			mvpp2_prs_vid_disable_filtering(port);
		}
	}

4891 4892
	if (changed & NETIF_F_RXHASH) {
		if (features & NETIF_F_RXHASH)
4893
			mvpp22_port_rss_enable(port);
4894
		else
4895
			mvpp22_port_rss_disable(port);
4896 4897
	}

4898 4899 4900
	return 0;
}

M
Matteo Croce 已提交
4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916
static int mvpp2_xdp_setup(struct mvpp2_port *port, struct netdev_bpf *bpf)
{
	struct bpf_prog *prog = bpf->prog, *old_prog;
	bool running = netif_running(port->dev);
	bool reset = !prog != !port->xdp_prog;

	if (port->dev->mtu > ETH_DATA_LEN) {
		NL_SET_ERR_MSG_MOD(bpf->extack, "XDP is not supported with jumbo frames enabled");
		return -EOPNOTSUPP;
	}

	if (!port->priv->percpu_pools) {
		NL_SET_ERR_MSG_MOD(bpf->extack, "Per CPU Pools required for XDP");
		return -EOPNOTSUPP;
	}

M
Matteo Croce 已提交
4917 4918 4919
	if (port->ntxqs < num_possible_cpus() * 2) {
		NL_SET_ERR_MSG_MOD(bpf->extack, "XDP_TX needs two TX queues per CPU");
		return -EOPNOTSUPP;
M
Matteo Croce 已提交
4920 4921
	}

M
Matteo Croce 已提交
4922 4923 4924 4925
	/* device is up and bpf is added/removed, must setup the RX queues */
	if (running && reset)
		mvpp2_stop(port->dev);

M
Matteo Croce 已提交
4926 4927 4928 4929 4930 4931 4932 4933 4934
	old_prog = xchg(&port->xdp_prog, prog);
	if (old_prog)
		bpf_prog_put(old_prog);

	/* bpf is just replaced, RXQ and MTU are already setup */
	if (!reset)
		return 0;

	/* device was up, restore the link */
M
Matteo Croce 已提交
4935 4936
	if (running)
		mvpp2_open(port->dev);
M
Matteo Croce 已提交
4937

M
Matteo Croce 已提交
4938 4939
	/* Check Page Pool DMA Direction */
	mvpp2_check_pagepool_dma(port);
M
Matteo Croce 已提交
4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955

	return 0;
}

static int mvpp2_xdp(struct net_device *dev, struct netdev_bpf *xdp)
{
	struct mvpp2_port *port = netdev_priv(dev);

	switch (xdp->command) {
	case XDP_SETUP_PROG:
		return mvpp2_xdp_setup(port, xdp);
	default:
		return -EINVAL;
	}
}

4956 4957
/* Ethtool methods */

A
Antoine Tenart 已提交
4958 4959 4960 4961 4962 4963 4964 4965 4966 4967
static int mvpp2_ethtool_nway_reset(struct net_device *dev)
{
	struct mvpp2_port *port = netdev_priv(dev);

	if (!port->phylink)
		return -ENOTSUPP;

	return phylink_ethtool_nway_reset(port->phylink);
}

4968 4969 4970 4971 4972 4973 4974
/* Set interrupt coalescing for ethtools */
static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
				      struct ethtool_coalesce *c)
{
	struct mvpp2_port *port = netdev_priv(dev);
	int queue;

4975
	for (queue = 0; queue < port->nrxqs; queue++) {
4976 4977 4978 4979
		struct mvpp2_rx_queue *rxq = port->rxqs[queue];

		rxq->time_coal = c->rx_coalesce_usecs;
		rxq->pkts_coal = c->rx_max_coalesced_frames;
4980 4981
		mvpp2_rx_pkts_coal_set(port, rxq);
		mvpp2_rx_time_coal_set(port, rxq);
4982 4983
	}

4984 4985 4986 4987 4988
	if (port->has_tx_irqs) {
		port->tx_time_coal = c->tx_coalesce_usecs;
		mvpp2_tx_time_coal_set(port);
	}

4989
	for (queue = 0; queue < port->ntxqs; queue++) {
4990 4991 4992
		struct mvpp2_tx_queue *txq = port->txqs[queue];

		txq->done_pkts_coal = c->tx_max_coalesced_frames;
4993 4994 4995

		if (port->has_tx_irqs)
			mvpp2_tx_pkts_coal_set(port, txq);
4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006
	}

	return 0;
}

/* get coalescing for ethtools */
static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
				      struct ethtool_coalesce *c)
{
	struct mvpp2_port *port = netdev_priv(dev);

5007 5008 5009
	c->rx_coalesce_usecs       = port->rxqs[0]->time_coal;
	c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
	c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
5010
	c->tx_coalesce_usecs       = port->tx_time_coal;
5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029
	return 0;
}

static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
				      struct ethtool_drvinfo *drvinfo)
{
	strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
		sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
		sizeof(drvinfo->version));
	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
		sizeof(drvinfo->bus_info));
}

static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
					struct ethtool_ringparam *ring)
{
	struct mvpp2_port *port = netdev_priv(dev);

5030 5031
	ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
	ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
	ring->rx_pending = port->rx_ring_size;
	ring->tx_pending = port->tx_ring_size;
}

static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
				       struct ethtool_ringparam *ring)
{
	struct mvpp2_port *port = netdev_priv(dev);
	u16 prev_rx_ring_size = port->rx_ring_size;
	u16 prev_tx_ring_size = port->tx_ring_size;
	int err;

	err = mvpp2_check_ringparam_valid(dev, ring);
	if (err)
		return err;

	if (!netif_running(dev)) {
		port->rx_ring_size = ring->rx_pending;
		port->tx_ring_size = ring->tx_pending;
		return 0;
	}

	/* The interface is running, so we have to force a
	 * reallocation of the queues
	 */
	mvpp2_stop_dev(port);
	mvpp2_cleanup_rxqs(port);
	mvpp2_cleanup_txqs(port);

	port->rx_ring_size = ring->rx_pending;
	port->tx_ring_size = ring->tx_pending;

	err = mvpp2_setup_rxqs(port);
	if (err) {
		/* Reallocate Rx queues with the original ring size */
		port->rx_ring_size = prev_rx_ring_size;
		ring->rx_pending = prev_rx_ring_size;
		err = mvpp2_setup_rxqs(port);
		if (err)
			goto err_out;
	}
	err = mvpp2_setup_txqs(port);
	if (err) {
		/* Reallocate Tx queues with the original ring size */
		port->tx_ring_size = prev_tx_ring_size;
		ring->tx_pending = prev_tx_ring_size;
		err = mvpp2_setup_txqs(port);
		if (err)
			goto err_clean_rxqs;
	}

	mvpp2_start_dev(port);
	mvpp2_egress_enable(port);
	mvpp2_ingress_enable(port);

	return 0;

err_clean_rxqs:
	mvpp2_cleanup_rxqs(port);
err_out:
5092
	netdev_err(dev, "failed to change ring parameters");
5093 5094 5095
	return err;
}

A
Antoine Tenart 已提交
5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139
static void mvpp2_ethtool_get_pause_param(struct net_device *dev,
					  struct ethtool_pauseparam *pause)
{
	struct mvpp2_port *port = netdev_priv(dev);

	if (!port->phylink)
		return;

	phylink_ethtool_get_pauseparam(port->phylink, pause);
}

static int mvpp2_ethtool_set_pause_param(struct net_device *dev,
					 struct ethtool_pauseparam *pause)
{
	struct mvpp2_port *port = netdev_priv(dev);

	if (!port->phylink)
		return -ENOTSUPP;

	return phylink_ethtool_set_pauseparam(port->phylink, pause);
}

static int mvpp2_ethtool_get_link_ksettings(struct net_device *dev,
					    struct ethtool_link_ksettings *cmd)
{
	struct mvpp2_port *port = netdev_priv(dev);

	if (!port->phylink)
		return -ENOTSUPP;

	return phylink_ethtool_ksettings_get(port->phylink, cmd);
}

static int mvpp2_ethtool_set_link_ksettings(struct net_device *dev,
					    const struct ethtool_link_ksettings *cmd)
{
	struct mvpp2_port *port = netdev_priv(dev);

	if (!port->phylink)
		return -ENOTSUPP;

	return phylink_ethtool_ksettings_set(port->phylink, cmd);
}

5140 5141 5142 5143
static int mvpp2_ethtool_get_rxnfc(struct net_device *dev,
				   struct ethtool_rxnfc *info, u32 *rules)
{
	struct mvpp2_port *port = netdev_priv(dev);
5144
	int ret = 0, i, loc = 0;
5145 5146 5147 5148 5149

	if (!mvpp22_rss_is_supported())
		return -EOPNOTSUPP;

	switch (info->cmd) {
5150 5151 5152
	case ETHTOOL_GRXFH:
		ret = mvpp2_ethtool_rxfh_get(port, info);
		break;
5153 5154 5155
	case ETHTOOL_GRXRINGS:
		info->data = port->nrxqs;
		break;
5156 5157 5158 5159 5160 5161 5162
	case ETHTOOL_GRXCLSRLCNT:
		info->rule_cnt = port->n_rfs_rules;
		break;
	case ETHTOOL_GRXCLSRULE:
		ret = mvpp2_ethtool_cls_rule_get(port, info);
		break;
	case ETHTOOL_GRXCLSRLALL:
5163
		for (i = 0; i < MVPP2_N_RFS_ENTRIES_PER_FLOW; i++) {
5164 5165 5166 5167
			if (port->rfs_rules[i])
				rules[loc++] = i;
		}
		break;
5168 5169 5170 5171
	default:
		return -ENOTSUPP;
	}

5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187
	return ret;
}

static int mvpp2_ethtool_set_rxnfc(struct net_device *dev,
				   struct ethtool_rxnfc *info)
{
	struct mvpp2_port *port = netdev_priv(dev);
	int ret = 0;

	if (!mvpp22_rss_is_supported())
		return -EOPNOTSUPP;

	switch (info->cmd) {
	case ETHTOOL_SRXFH:
		ret = mvpp2_ethtool_rxfh_set(port, info);
		break;
5188 5189 5190 5191 5192 5193
	case ETHTOOL_SRXCLSRLINS:
		ret = mvpp2_ethtool_cls_rule_ins(port, info);
		break;
	case ETHTOOL_SRXCLSRLDEL:
		ret = mvpp2_ethtool_cls_rule_del(port, info);
		break;
5194 5195 5196 5197
	default:
		return -EOPNOTSUPP;
	}
	return ret;
5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208
}

static u32 mvpp2_ethtool_get_rxfh_indir_size(struct net_device *dev)
{
	return mvpp22_rss_is_supported() ? MVPP22_RSS_TABLE_ENTRIES : 0;
}

static int mvpp2_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
				  u8 *hfunc)
{
	struct mvpp2_port *port = netdev_priv(dev);
5209
	int ret = 0;
5210 5211 5212 5213 5214

	if (!mvpp22_rss_is_supported())
		return -EOPNOTSUPP;

	if (indir)
5215
		ret = mvpp22_port_rss_ctx_indir_get(port, 0, indir);
5216 5217 5218 5219

	if (hfunc)
		*hfunc = ETH_RSS_HASH_CRC32;

5220
	return ret;
5221 5222 5223 5224 5225 5226
}

static int mvpp2_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
				  const u8 *key, const u8 hfunc)
{
	struct mvpp2_port *port = netdev_priv(dev);
5227
	int ret = 0;
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237

	if (!mvpp22_rss_is_supported())
		return -EOPNOTSUPP;

	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
		return -EOPNOTSUPP;

	if (key)
		return -EOPNOTSUPP;

5238 5239
	if (indir)
		ret = mvpp22_port_rss_ctx_indir_set(port, 0, indir);
5240

5241
	return ret;
5242 5243
}

5244 5245 5246 5247 5248 5249 5250 5251
static int mvpp2_ethtool_get_rxfh_context(struct net_device *dev, u32 *indir,
					  u8 *key, u8 *hfunc, u32 rss_context)
{
	struct mvpp2_port *port = netdev_priv(dev);
	int ret = 0;

	if (!mvpp22_rss_is_supported())
		return -EOPNOTSUPP;
5252 5253
	if (rss_context >= MVPP22_N_RSS_TABLES)
		return -EINVAL;
5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291

	if (hfunc)
		*hfunc = ETH_RSS_HASH_CRC32;

	if (indir)
		ret = mvpp22_port_rss_ctx_indir_get(port, rss_context, indir);

	return ret;
}

static int mvpp2_ethtool_set_rxfh_context(struct net_device *dev,
					  const u32 *indir, const u8 *key,
					  const u8 hfunc, u32 *rss_context,
					  bool delete)
{
	struct mvpp2_port *port = netdev_priv(dev);
	int ret;

	if (!mvpp22_rss_is_supported())
		return -EOPNOTSUPP;

	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
		return -EOPNOTSUPP;

	if (key)
		return -EOPNOTSUPP;

	if (delete)
		return mvpp22_port_rss_ctx_delete(port, *rss_context);

	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
		ret = mvpp22_port_rss_ctx_create(port, rss_context);
		if (ret)
			return ret;
	}

	return mvpp22_port_rss_ctx_indir_set(port, *rss_context, indir);
}
5292 5293 5294 5295 5296 5297 5298 5299 5300 5301
/* Device ops */

static const struct net_device_ops mvpp2_netdev_ops = {
	.ndo_open		= mvpp2_open,
	.ndo_stop		= mvpp2_stop,
	.ndo_start_xmit		= mvpp2_tx,
	.ndo_set_rx_mode	= mvpp2_set_rx_mode,
	.ndo_set_mac_address	= mvpp2_set_mac_address,
	.ndo_change_mtu		= mvpp2_change_mtu,
	.ndo_get_stats64	= mvpp2_get_stats64,
5302
	.ndo_do_ioctl		= mvpp2_ioctl,
5303 5304 5305
	.ndo_vlan_rx_add_vid	= mvpp2_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid	= mvpp2_vlan_rx_kill_vid,
	.ndo_set_features	= mvpp2_set_features,
M
Matteo Croce 已提交
5306
	.ndo_bpf		= mvpp2_xdp,
M
Matteo Croce 已提交
5307
	.ndo_xdp_xmit		= mvpp2_xdp_xmit,
5308 5309 5310
};

static const struct ethtool_ops mvpp2_eth_tool_ops = {
5311 5312
	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
				     ETHTOOL_COALESCE_MAX_FRAMES,
A
Antoine Tenart 已提交
5313
	.nway_reset		= mvpp2_ethtool_nway_reset,
5314
	.get_link		= ethtool_op_get_link,
5315
	.get_ts_info		= mvpp2_ethtool_get_ts_info,
5316 5317 5318 5319 5320 5321 5322 5323
	.set_coalesce		= mvpp2_ethtool_set_coalesce,
	.get_coalesce		= mvpp2_ethtool_get_coalesce,
	.get_drvinfo		= mvpp2_ethtool_get_drvinfo,
	.get_ringparam		= mvpp2_ethtool_get_ringparam,
	.set_ringparam		= mvpp2_ethtool_set_ringparam,
	.get_strings		= mvpp2_ethtool_get_strings,
	.get_ethtool_stats	= mvpp2_ethtool_get_stats,
	.get_sset_count		= mvpp2_ethtool_get_sset_count,
A
Antoine Tenart 已提交
5324 5325 5326 5327
	.get_pauseparam		= mvpp2_ethtool_get_pause_param,
	.set_pauseparam		= mvpp2_ethtool_set_pause_param,
	.get_link_ksettings	= mvpp2_ethtool_get_link_ksettings,
	.set_link_ksettings	= mvpp2_ethtool_set_link_ksettings,
5328
	.get_rxnfc		= mvpp2_ethtool_get_rxnfc,
5329
	.set_rxnfc		= mvpp2_ethtool_set_rxnfc,
5330 5331 5332
	.get_rxfh_indir_size	= mvpp2_ethtool_get_rxfh_indir_size,
	.get_rxfh		= mvpp2_ethtool_get_rxfh,
	.set_rxfh		= mvpp2_ethtool_set_rxfh,
5333 5334
	.get_rxfh_context	= mvpp2_ethtool_get_rxfh_context,
	.set_rxfh_context	= mvpp2_ethtool_set_rxfh_context,
5335 5336
};

5337 5338 5339 5340 5341
/* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
 * had a single IRQ defined per-port.
 */
static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
					   struct device_node *port_node)
5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361
{
	struct mvpp2_queue_vector *v = &port->qvecs[0];

	v->first_rxq = 0;
	v->nrxqs = port->nrxqs;
	v->type = MVPP2_QUEUE_VECTOR_SHARED;
	v->sw_thread_id = 0;
	v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
	v->port = port;
	v->irq = irq_of_parse_and_map(port_node, 0);
	if (v->irq <= 0)
		return -EINVAL;
	netif_napi_add(port->dev, &v->napi, mvpp2_poll,
		       NAPI_POLL_WEIGHT);

	port->nqvecs = 1;

	return 0;
}

5362 5363 5364
static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
					  struct device_node *port_node)
{
5365
	struct mvpp2 *priv = port->priv;
5366 5367 5368
	struct mvpp2_queue_vector *v;
	int i, ret;

5369 5370 5371 5372 5373 5374 5375 5376
	switch (queue_mode) {
	case MVPP2_QDIST_SINGLE_MODE:
		port->nqvecs = priv->nthreads + 1;
		break;
	case MVPP2_QDIST_MULTI_MODE:
		port->nqvecs = priv->nthreads;
		break;
	}
5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387

	for (i = 0; i < port->nqvecs; i++) {
		char irqname[16];

		v = port->qvecs + i;

		v->port = port;
		v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
		v->sw_thread_id = i;
		v->sw_thread_mask = BIT(i);

5388 5389 5390 5391
		if (port->flags & MVPP2_F_DT_COMPAT)
			snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
		else
			snprintf(irqname, sizeof(irqname), "hif%d", i);
5392 5393

		if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
5394 5395
			v->first_rxq = i;
			v->nrxqs = 1;
5396 5397 5398 5399 5400
		} else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
			   i == (port->nqvecs - 1)) {
			v->first_rxq = 0;
			v->nrxqs = port->nrxqs;
			v->type = MVPP2_QUEUE_VECTOR_SHARED;
5401 5402 5403

			if (port->flags & MVPP2_F_DT_COMPAT)
				strncpy(irqname, "rx-shared", sizeof(irqname));
5404 5405
		}

5406 5407 5408 5409
		if (port_node)
			v->irq = of_irq_get_byname(port_node, irqname);
		else
			v->irq = fwnode_irq_get(port->fwnode, i);
5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435
		if (v->irq <= 0) {
			ret = -EINVAL;
			goto err;
		}

		netif_napi_add(port->dev, &v->napi, mvpp2_poll,
			       NAPI_POLL_WEIGHT);
	}

	return 0;

err:
	for (i = 0; i < port->nqvecs; i++)
		irq_dispose_mapping(port->qvecs[i].irq);
	return ret;
}

static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
				    struct device_node *port_node)
{
	if (port->has_tx_irqs)
		return mvpp2_multi_queue_vectors_init(port, port_node);
	else
		return mvpp2_simple_queue_vectors_init(port, port_node);
}

5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473
static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
{
	int i;

	for (i = 0; i < port->nqvecs; i++)
		irq_dispose_mapping(port->qvecs[i].irq);
}

/* Configure Rx queue group interrupt for this port */
static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
{
	struct mvpp2 *priv = port->priv;
	u32 val;
	int i;

	if (priv->hw_version == MVPP21) {
		mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
			    port->nrxqs);
		return;
	}

	/* Handle the more complicated PPv2.2 case */
	for (i = 0; i < port->nqvecs; i++) {
		struct mvpp2_queue_vector *qv = port->qvecs + i;

		if (!qv->nrxqs)
			continue;

		val = qv->sw_thread_id;
		val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
		mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);

		val = qv->first_rxq;
		val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
		mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
	}
}

5474 5475 5476 5477 5478 5479
/* Initialize port HW */
static int mvpp2_port_init(struct mvpp2_port *port)
{
	struct device *dev = port->dev->dev.parent;
	struct mvpp2 *priv = port->priv;
	struct mvpp2_txq_pcpu *txq_pcpu;
5480
	unsigned int thread;
5481
	int queue, err;
5482

5483 5484
	/* Checks for hardware constraints */
	if (port->first_rxq + port->nrxqs >
5485
	    MVPP2_MAX_PORTS * priv->max_port_rxqs)
5486 5487
		return -EINVAL;

5488
	if (port->nrxqs > priv->max_port_rxqs || port->ntxqs > MVPP2_MAX_TXQ)
5489 5490
		return -EINVAL;

5491 5492 5493 5494
	/* Disable port */
	mvpp2_egress_disable(port);
	mvpp2_port_disable(port);

5495 5496
	port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;

5497
	port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
5498 5499 5500 5501 5502 5503 5504
				  GFP_KERNEL);
	if (!port->txqs)
		return -ENOMEM;

	/* Associate physical Tx queues to this port and initialize.
	 * The mapping is predefined.
	 */
5505
	for (queue = 0; queue < port->ntxqs; queue++) {
5506 5507 5508 5509
		int queue_phy_id = mvpp2_txq_phys(port->id, queue);
		struct mvpp2_tx_queue *txq;

		txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
5510 5511 5512 5513
		if (!txq) {
			err = -ENOMEM;
			goto err_free_percpu;
		}
5514 5515 5516 5517 5518 5519 5520 5521 5522 5523

		txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
		if (!txq->pcpu) {
			err = -ENOMEM;
			goto err_free_percpu;
		}

		txq->id = queue_phy_id;
		txq->log_id = queue;
		txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
5524
		for (thread = 0; thread < priv->nthreads; thread++) {
5525 5526
			txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
			txq_pcpu->thread = thread;
5527 5528 5529 5530 5531
		}

		port->txqs[queue] = txq;
	}

5532
	port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
5533 5534 5535 5536 5537 5538 5539
				  GFP_KERNEL);
	if (!port->rxqs) {
		err = -ENOMEM;
		goto err_free_percpu;
	}

	/* Allocate and initialize Rx queue for this port */
5540
	for (queue = 0; queue < port->nrxqs; queue++) {
5541 5542 5543 5544
		struct mvpp2_rx_queue *rxq;

		/* Map physical Rx queue to port's logical Rx queue */
		rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
5545 5546
		if (!rxq) {
			err = -ENOMEM;
5547
			goto err_free_percpu;
5548
		}
5549 5550 5551 5552 5553 5554 5555 5556
		/* Map this Rx queue to a physical queue */
		rxq->id = port->first_rxq + queue;
		rxq->port = port->id;
		rxq->logic_rxq = queue;

		port->rxqs[queue] = rxq;
	}

5557
	mvpp2_rx_irqs_setup(port);
5558 5559

	/* Create Rx descriptor rings */
5560
	for (queue = 0; queue < port->nrxqs; queue++) {
5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576
		struct mvpp2_rx_queue *rxq = port->rxqs[queue];

		rxq->size = port->rx_ring_size;
		rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
		rxq->time_coal = MVPP2_RX_COAL_USEC;
	}

	mvpp2_ingress_disable(port);

	/* Port default configuration */
	mvpp2_defaults_set(port);

	/* Port's classifier configuration */
	mvpp2_cls_oversize_rxq_set(port);
	mvpp2_cls_port_config(port);

5577
	if (mvpp22_rss_is_supported())
5578
		mvpp22_port_rss_init(port);
5579

5580 5581 5582 5583 5584 5585 5586 5587
	/* Provide an initial Rx packet size */
	port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);

	/* Initialize pools for swf */
	err = mvpp2_swf_bm_pool_init(port);
	if (err)
		goto err_free_percpu;

5588 5589 5590 5591
	/* Clear all port stats */
	mvpp2_read_stats(port);
	memset(port->ethtool_stats, 0,
	       MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs) * sizeof(u64));
5592

5593 5594 5595
	return 0;

err_free_percpu:
5596
	for (queue = 0; queue < port->ntxqs; queue++) {
5597 5598 5599 5600 5601 5602 5603
		if (!port->txqs[queue])
			continue;
		free_percpu(port->txqs[queue]->pcpu);
	}
	return err;
}

5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626
static bool mvpp22_port_has_legacy_tx_irqs(struct device_node *port_node,
					   unsigned long *flags)
{
	char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1", "tx-cpu2",
			  "tx-cpu3" };
	int i;

	for (i = 0; i < 5; i++)
		if (of_property_match_string(port_node, "interrupt-names",
					     irqs[i]) < 0)
			return false;

	*flags |= MVPP2_F_DT_COMPAT;
	return true;
}

/* Checks if the port dt description has the required Tx interrupts:
 * - PPv2.1: there are no such interrupts.
 * - PPv2.2:
 *   - The old DTs have: "rx-shared", "tx-cpuX" with X in [0...3]
 *   - The new ones have: "hifX" with X in [0..8]
 *
 * All those variants are supported to keep the backward compatibility.
5627
 */
5628 5629 5630
static bool mvpp2_port_has_irqs(struct mvpp2 *priv,
				struct device_node *port_node,
				unsigned long *flags)
5631
{
5632 5633
	char name[5];
	int i;
5634

5635 5636 5637 5638
	/* ACPI */
	if (!port_node)
		return true;

5639 5640 5641
	if (priv->hw_version == MVPP21)
		return false;

5642 5643 5644 5645 5646 5647 5648
	if (mvpp22_port_has_legacy_tx_irqs(port_node, flags))
		return true;

	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
		snprintf(name, 5, "hif%d", i);
		if (of_property_match_string(port_node, "interrupt-names",
					     name) < 0)
5649 5650 5651 5652 5653 5654
			return false;
	}

	return true;
}

5655
static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
5656
				     struct fwnode_handle *fwnode,
5657 5658 5659 5660
				     char **mac_from)
{
	struct mvpp2_port *port = netdev_priv(dev);
	char hw_mac_addr[ETH_ALEN] = {0};
5661
	char fw_mac_addr[ETH_ALEN];
5662

5663 5664 5665
	if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
		*mac_from = "firmware node";
		ether_addr_copy(dev->dev_addr, fw_mac_addr);
5666 5667
		return;
	}
5668

5669 5670 5671 5672 5673 5674 5675
	if (priv->hw_version == MVPP21) {
		mvpp21_get_mac_address(port, hw_mac_addr);
		if (is_valid_ether_addr(hw_mac_addr)) {
			*mac_from = "hardware";
			ether_addr_copy(dev->dev_addr, hw_mac_addr);
			return;
		}
5676
	}
5677 5678 5679

	*mac_from = "random";
	eth_hw_addr_random(dev);
5680 5681
}

5682 5683 5684 5685 5686
static struct mvpp2_port *mvpp2_phylink_to_port(struct phylink_config *config)
{
	return container_of(config, struct mvpp2_port, phylink_config);
}

5687 5688 5689 5690 5691
static struct mvpp2_port *mvpp2_pcs_to_port(struct phylink_pcs *pcs)
{
	return container_of(pcs, struct mvpp2_port, phylink_pcs);
}

5692 5693
static void mvpp2_xlg_pcs_get_state(struct phylink_pcs *pcs,
				    struct phylink_link_state *state)
5694
{
5695
	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712
	u32 val;

	state->speed = SPEED_10000;
	state->duplex = 1;
	state->an_complete = 1;

	val = readl(port->base + MVPP22_XLG_STATUS);
	state->link = !!(val & MVPP22_XLG_STATUS_LINK_UP);

	state->pause = 0;
	val = readl(port->base + MVPP22_XLG_CTRL0_REG);
	if (val & MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN)
		state->pause |= MLO_PAUSE_TX;
	if (val & MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN)
		state->pause |= MLO_PAUSE_RX;
}

5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727
static int mvpp2_xlg_pcs_config(struct phylink_pcs *pcs,
				unsigned int mode,
				phy_interface_t interface,
				const unsigned long *advertising,
				bool permit_pause_to_mac)
{
	return 0;
}

static const struct phylink_pcs_ops mvpp2_phylink_xlg_pcs_ops = {
	.pcs_get_state = mvpp2_xlg_pcs_get_state,
	.pcs_config = mvpp2_xlg_pcs_config,
};

static void mvpp2_gmac_pcs_get_state(struct phylink_pcs *pcs,
5728 5729
				     struct phylink_link_state *state)
{
5730
	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761
	u32 val;

	val = readl(port->base + MVPP2_GMAC_STATUS0);

	state->an_complete = !!(val & MVPP2_GMAC_STATUS0_AN_COMPLETE);
	state->link = !!(val & MVPP2_GMAC_STATUS0_LINK_UP);
	state->duplex = !!(val & MVPP2_GMAC_STATUS0_FULL_DUPLEX);

	switch (port->phy_interface) {
	case PHY_INTERFACE_MODE_1000BASEX:
		state->speed = SPEED_1000;
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
		state->speed = SPEED_2500;
		break;
	default:
		if (val & MVPP2_GMAC_STATUS0_GMII_SPEED)
			state->speed = SPEED_1000;
		else if (val & MVPP2_GMAC_STATUS0_MII_SPEED)
			state->speed = SPEED_100;
		else
			state->speed = SPEED_10;
	}

	state->pause = 0;
	if (val & MVPP2_GMAC_STATUS0_RX_PAUSE)
		state->pause |= MLO_PAUSE_RX;
	if (val & MVPP2_GMAC_STATUS0_TX_PAUSE)
		state->pause |= MLO_PAUSE_TX;
}

5762
static int mvpp2_gmac_pcs_config(struct phylink_pcs *pcs, unsigned int mode,
5763 5764 5765 5766
				 phy_interface_t interface,
				 const unsigned long *advertising,
				 bool permit_pause_to_mac)
{
5767
	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818
	u32 mask, val, an, old_an, changed;

	mask = MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
	       MVPP2_GMAC_IN_BAND_AUTONEG |
	       MVPP2_GMAC_AN_SPEED_EN |
	       MVPP2_GMAC_FLOW_CTRL_AUTONEG |
	       MVPP2_GMAC_AN_DUPLEX_EN;

	if (phylink_autoneg_inband(mode)) {
		mask |= MVPP2_GMAC_CONFIG_MII_SPEED |
			MVPP2_GMAC_CONFIG_GMII_SPEED |
			MVPP2_GMAC_CONFIG_FULL_DUPLEX;
		val = MVPP2_GMAC_IN_BAND_AUTONEG;

		if (interface == PHY_INTERFACE_MODE_SGMII) {
			/* SGMII mode receives the speed and duplex from PHY */
			val |= MVPP2_GMAC_AN_SPEED_EN |
			       MVPP2_GMAC_AN_DUPLEX_EN;
		} else {
			/* 802.3z mode has fixed speed and duplex */
			val |= MVPP2_GMAC_CONFIG_GMII_SPEED |
			       MVPP2_GMAC_CONFIG_FULL_DUPLEX;

			/* The FLOW_CTRL_AUTONEG bit selects either the hardware
			 * automatically or the bits in MVPP22_GMAC_CTRL_4_REG
			 * manually controls the GMAC pause modes.
			 */
			if (permit_pause_to_mac)
				val |= MVPP2_GMAC_FLOW_CTRL_AUTONEG;

			/* Configure advertisement bits */
			mask |= MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN;
			if (phylink_test(advertising, Pause))
				val |= MVPP2_GMAC_FC_ADV_EN;
			if (phylink_test(advertising, Asym_Pause))
				val |= MVPP2_GMAC_FC_ADV_ASM_EN;
		}
	} else {
		val = 0;
	}

	old_an = an = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
	an = (an & ~mask) | val;
	changed = an ^ old_an;
	if (changed)
		writel(an, port->base + MVPP2_GMAC_AUTONEG_CONFIG);

	/* We are only interested in the advertisement bits changing */
	return changed & (MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN);
}

5819
static void mvpp2_gmac_pcs_an_restart(struct phylink_pcs *pcs)
5820 5821 5822 5823 5824 5825 5826 5827 5828 5829
{
	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
	u32 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);

	writel(val | MVPP2_GMAC_IN_BAND_RESTART_AN,
	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
	writel(val & ~MVPP2_GMAC_IN_BAND_RESTART_AN,
	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
}

5830 5831 5832 5833
static const struct phylink_pcs_ops mvpp2_phylink_gmac_pcs_ops = {
	.pcs_get_state = mvpp2_gmac_pcs_get_state,
	.pcs_config = mvpp2_gmac_pcs_config,
	.pcs_an_restart = mvpp2_gmac_pcs_an_restart,
5834 5835
};

5836
static void mvpp2_phylink_validate(struct phylink_config *config,
A
Antoine Tenart 已提交
5837 5838 5839
				   unsigned long *supported,
				   struct phylink_link_state *state)
{
5840
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
A
Antoine Tenart 已提交
5841 5842
	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };

5843 5844
	/* Invalid combinations */
	switch (state->interface) {
5845
	case PHY_INTERFACE_MODE_10GBASER:
5846
	case PHY_INTERFACE_MODE_XAUI:
5847
		if (!mvpp2_port_supports_xlg(port))
5848 5849 5850 5851 5852 5853
			goto empty_set;
		break;
	case PHY_INTERFACE_MODE_RGMII:
	case PHY_INTERFACE_MODE_RGMII_ID:
	case PHY_INTERFACE_MODE_RGMII_RXID:
	case PHY_INTERFACE_MODE_RGMII_TXID:
5854
		if (!mvpp2_port_supports_rgmii(port))
5855 5856 5857 5858 5859 5860
			goto empty_set;
		break;
	default:
		break;
	}

A
Antoine Tenart 已提交
5861 5862 5863 5864 5865
	phylink_set(mask, Autoneg);
	phylink_set_port_modes(mask);
	phylink_set(mask, Pause);
	phylink_set(mask, Asym_Pause);

A
Antoine Tenart 已提交
5866
	switch (state->interface) {
5867
	case PHY_INTERFACE_MODE_10GBASER:
5868
	case PHY_INTERFACE_MODE_XAUI:
5869
	case PHY_INTERFACE_MODE_NA:
5870
		if (mvpp2_port_supports_xlg(port)) {
5871
			phylink_set(mask, 10000baseT_Full);
5872 5873 5874 5875 5876 5877 5878
			phylink_set(mask, 10000baseCR_Full);
			phylink_set(mask, 10000baseSR_Full);
			phylink_set(mask, 10000baseLR_Full);
			phylink_set(mask, 10000baseLRM_Full);
			phylink_set(mask, 10000baseER_Full);
			phylink_set(mask, 10000baseKR_Full);
		}
5879 5880
		if (state->interface != PHY_INTERFACE_MODE_NA)
			break;
5881
		fallthrough;
5882 5883 5884 5885 5886
	case PHY_INTERFACE_MODE_RGMII:
	case PHY_INTERFACE_MODE_RGMII_ID:
	case PHY_INTERFACE_MODE_RGMII_RXID:
	case PHY_INTERFACE_MODE_RGMII_TXID:
	case PHY_INTERFACE_MODE_SGMII:
A
Antoine Tenart 已提交
5887 5888 5889 5890
		phylink_set(mask, 10baseT_Half);
		phylink_set(mask, 10baseT_Full);
		phylink_set(mask, 100baseT_Half);
		phylink_set(mask, 100baseT_Full);
5891 5892 5893 5894
		phylink_set(mask, 1000baseT_Full);
		phylink_set(mask, 1000baseX_Full);
		if (state->interface != PHY_INTERFACE_MODE_NA)
			break;
5895
		fallthrough;
A
Antoine Tenart 已提交
5896
	case PHY_INTERFACE_MODE_1000BASEX:
A
Antoine Tenart 已提交
5897
	case PHY_INTERFACE_MODE_2500BASEX:
5898 5899 5900 5901 5902 5903 5904 5905 5906 5907
		if (port->comphy ||
		    state->interface != PHY_INTERFACE_MODE_2500BASEX) {
			phylink_set(mask, 1000baseT_Full);
			phylink_set(mask, 1000baseX_Full);
		}
		if (port->comphy ||
		    state->interface == PHY_INTERFACE_MODE_2500BASEX) {
			phylink_set(mask, 2500baseT_Full);
			phylink_set(mask, 2500baseX_Full);
		}
5908 5909 5910
		break;
	default:
		goto empty_set;
A
Antoine Tenart 已提交
5911 5912 5913 5914 5915
	}

	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
	bitmap_and(state->advertising, state->advertising, mask,
		   __ETHTOOL_LINK_MODE_MASK_NBITS);
5916 5917

	phylink_helper_basex_speed(state);
5918 5919 5920 5921
	return;

empty_set:
	bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
A
Antoine Tenart 已提交
5922 5923 5924 5925 5926
}

static void mvpp2_xlg_config(struct mvpp2_port *port, unsigned int mode,
			     const struct phylink_link_state *state)
{
5927
	u32 val;
5928

5929
	mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
5930 5931
		     MVPP22_XLG_CTRL0_MAC_RESET_DIS,
		     MVPP22_XLG_CTRL0_MAC_RESET_DIS);
5932 5933 5934 5935 5936 5937 5938 5939 5940 5941
	mvpp2_modify(port->base + MVPP22_XLG_CTRL4_REG,
		     MVPP22_XLG_CTRL4_MACMODSELECT_GMAC |
		     MVPP22_XLG_CTRL4_EN_IDLE_CHECK |
		     MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC,
		     MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC);

	/* Wait for reset to deassert */
	do {
		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
	} while (!(val & MVPP22_XLG_CTRL0_MAC_RESET_DIS));
A
Antoine Tenart 已提交
5942 5943 5944 5945 5946
}

static void mvpp2_gmac_config(struct mvpp2_port *port, unsigned int mode,
			      const struct phylink_link_state *state)
{
5947 5948 5949
	u32 old_ctrl0, ctrl0;
	u32 old_ctrl2, ctrl2;
	u32 old_ctrl4, ctrl4;
A
Antoine Tenart 已提交
5950

5951 5952 5953
	old_ctrl0 = ctrl0 = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
	old_ctrl2 = ctrl2 = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
	old_ctrl4 = ctrl4 = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
A
Antoine Tenart 已提交
5954 5955

	ctrl0 &= ~MVPP2_GMAC_PORT_TYPE_MASK;
5956
	ctrl2 &= ~(MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK);
A
Antoine Tenart 已提交
5957

5958
	/* Configure port type */
5959
	if (phy_interface_mode_is_8023z(state->interface)) {
5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975
		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK;
		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
			 MVPP22_CTRL4_DP_CLK_SEL |
			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_INBAND_AN_MASK;
		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
			 MVPP22_CTRL4_DP_CLK_SEL |
			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
	} else if (phy_interface_mode_is_rgmii(state->interface)) {
		ctrl4 &= ~MVPP22_CTRL4_DP_CLK_SEL;
		ctrl4 |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
			 MVPP22_CTRL4_SYNC_BYPASS_DIS |
			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
A
Antoine Tenart 已提交
5976
	}
A
Antoine Tenart 已提交
5977

5978 5979
	/* Configure negotiation style */
	if (!phylink_autoneg_inband(mode)) {
5980 5981 5982
		/* Phy or fixed speed - no in-band AN, nothing to do, leave the
		 * configured speed, duplex and flow control as-is.
		 */
5983 5984 5985 5986 5987 5988 5989 5990 5991 5992
	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
		/* SGMII in-band mode receives the speed and duplex from
		 * the PHY. Flow control information is not received. */
	} else if (phy_interface_mode_is_8023z(state->interface)) {
		/* 1000BaseX and 2500BaseX ports cannot negotiate speed nor can
		 * they negotiate duplex: they are always operating with a fixed
		 * speed of 1000/2500Mbps in full duplex, so force 1000/2500
		 * speed and full duplex here.
		 */
		ctrl0 |= MVPP2_GMAC_PORT_TYPE_MASK;
A
Antoine Tenart 已提交
5993 5994
	}

5995 5996 5997 5998 5999 6000
	if (old_ctrl0 != ctrl0)
		writel(ctrl0, port->base + MVPP2_GMAC_CTRL_0_REG);
	if (old_ctrl2 != ctrl2)
		writel(ctrl2, port->base + MVPP2_GMAC_CTRL_2_REG);
	if (old_ctrl4 != ctrl4)
		writel(ctrl4, port->base + MVPP22_GMAC_CTRL_4_REG);
A
Antoine Tenart 已提交
6001 6002
}

6003 6004
static int mvpp2__mac_prepare(struct phylink_config *config, unsigned int mode,
			      phy_interface_t interface)
A
Antoine Tenart 已提交
6005
{
6006
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
A
Antoine Tenart 已提交
6007 6008

	/* Check for invalid configuration */
6009
	if (mvpp2_is_xlg(interface) && port->gop_id != 0) {
6010
		netdev_err(port->dev, "Invalid mode on %s\n", port->dev->name);
6011
		return -EINVAL;
A
Antoine Tenart 已提交
6012 6013
	}

6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033
	if (port->phy_interface != interface ||
	    phylink_autoneg_inband(mode)) {
		/* Force the link down when changing the interface or if in
		 * in-band mode to ensure we do not change the configuration
		 * while the hardware is indicating link is up. We force both
		 * XLG and GMAC down to ensure that they're both in a known
		 * state.
		 */
		mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
			     MVPP2_GMAC_FORCE_LINK_PASS |
			     MVPP2_GMAC_FORCE_LINK_DOWN,
			     MVPP2_GMAC_FORCE_LINK_DOWN);

		if (mvpp2_port_supports_xlg(port))
			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN,
				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN);
	}

A
Antoine Tenart 已提交
6034 6035
	/* Make sure the port is disabled when reconfiguring the mode */
	mvpp2_port_disable(port);
6036

6037 6038 6039 6040 6041 6042 6043 6044
	if (port->phy_interface != interface) {
		/* Place GMAC into reset */
		mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
			     MVPP2_GMAC_PORT_RESET_MASK,
			     MVPP2_GMAC_PORT_RESET_MASK);

		if (port->priv->hw_version == MVPP22) {
			mvpp22_gop_mask_irq(port);
A
Antoine Tenart 已提交
6045

6046 6047
			phy_power_off(port->comphy);
		}
A
Antoine Tenart 已提交
6048 6049
	}

6050 6051 6052 6053 6054 6055 6056 6057 6058
	/* Select the appropriate PCS operations depending on the
	 * configured interface mode. We will only switch to a mode
	 * that the validate() checks have already passed.
	 */
	if (mvpp2_is_xlg(interface))
		port->phylink_pcs.ops = &mvpp2_phylink_xlg_pcs_ops;
	else
		port->phylink_pcs.ops = &mvpp2_phylink_gmac_pcs_ops;

6059 6060 6061
	return 0;
}

6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074
static int mvpp2_mac_prepare(struct phylink_config *config, unsigned int mode,
			     phy_interface_t interface)
{
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
	int ret;

	ret = mvpp2__mac_prepare(config, mode, interface);
	if (ret == 0)
		phylink_set_pcs(port->phylink, &port->phylink_pcs);

	return ret;
}

6075 6076 6077 6078 6079
static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode,
			     const struct phylink_link_state *state)
{
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);

A
Antoine Tenart 已提交
6080
	/* mac (re)configuration */
6081
	if (mvpp2_is_xlg(state->interface))
A
Antoine Tenart 已提交
6082 6083
		mvpp2_xlg_config(port, mode, state);
	else if (phy_interface_mode_is_rgmii(state->interface) ||
6084 6085
		 phy_interface_mode_is_8023z(state->interface) ||
		 state->interface == PHY_INTERFACE_MODE_SGMII)
A
Antoine Tenart 已提交
6086 6087 6088 6089
		mvpp2_gmac_config(port, mode, state);

	if (port->priv->hw_version == MVPP21 && port->flags & MVPP2_F_LOOPBACK)
		mvpp2_port_loopback_set(port, state);
6090 6091 6092 6093 6094 6095 6096 6097 6098 6099
}

static int mvpp2_mac_finish(struct phylink_config *config, unsigned int mode,
			    phy_interface_t interface)
{
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);

	if (port->priv->hw_version == MVPP22 &&
	    port->phy_interface != interface) {
		port->phy_interface = interface;
A
Antoine Tenart 已提交
6100

6101 6102 6103 6104
		/* Reconfigure the serdes lanes */
		mvpp22_mode_reconfigure(port);

		/* Unmask interrupts */
6105
		mvpp22_gop_unmask_irq(port);
6106
	}
6107

6108 6109 6110 6111 6112 6113 6114 6115 6116 6117
	if (!mvpp2_is_xlg(interface)) {
		/* Release GMAC reset and wait */
		mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
			     MVPP2_GMAC_PORT_RESET_MASK, 0);

		while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
		       MVPP2_GMAC_PORT_RESET_MASK)
			continue;
	}

6118
	mvpp2_port_enable(port);
6119

6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133
	/* Allow the link to come up if in in-band mode, otherwise the
	 * link is forced via mac_link_down()/mac_link_up()
	 */
	if (phylink_autoneg_inband(mode)) {
		if (mvpp2_is_xlg(interface))
			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN, 0);
		else
			mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
				     MVPP2_GMAC_FORCE_LINK_PASS |
				     MVPP2_GMAC_FORCE_LINK_DOWN, 0);
	}

6134
	return 0;
A
Antoine Tenart 已提交
6135 6136
}

6137 6138 6139 6140 6141
static void mvpp2_mac_link_up(struct phylink_config *config,
			      struct phy_device *phy,
			      unsigned int mode, phy_interface_t interface,
			      int speed, int duplex,
			      bool tx_pause, bool rx_pause)
A
Antoine Tenart 已提交
6142
{
6143
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
A
Antoine Tenart 已提交
6144 6145
	u32 val;

6146 6147
	if (mvpp2_is_xlg(interface)) {
		if (!phylink_autoneg_inband(mode)) {
6148 6149 6150 6151 6152 6153
			val = MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
			if (tx_pause)
				val |= MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN;
			if (rx_pause)
				val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;

6154 6155
			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN |
6156 6157 6158
				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
				     MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN |
				     MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN, val);
6159 6160 6161
		}
	} else {
		if (!phylink_autoneg_inband(mode)) {
6162
			val = MVPP2_GMAC_FORCE_LINK_PASS;
6163 6164 6165 6166 6167 6168 6169 6170 6171

			if (speed == SPEED_1000 || speed == SPEED_2500)
				val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
			else if (speed == SPEED_100)
				val |= MVPP2_GMAC_CONFIG_MII_SPEED;

			if (duplex == DUPLEX_FULL)
				val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;

6172 6173 6174 6175 6176 6177
			mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
				     MVPP2_GMAC_FORCE_LINK_DOWN |
				     MVPP2_GMAC_FORCE_LINK_PASS |
				     MVPP2_GMAC_CONFIG_MII_SPEED |
				     MVPP2_GMAC_CONFIG_GMII_SPEED |
				     MVPP2_GMAC_CONFIG_FULL_DUPLEX, val);
6178
		}
6179 6180 6181 6182 6183

		/* We can always update the flow control enable bits;
		 * these will only be effective if flow control AN
		 * (MVPP2_GMAC_FLOW_CTRL_AUTONEG) is disabled.
		 */
6184
		val = 0;
6185 6186 6187 6188
		if (tx_pause)
			val |= MVPP22_CTRL4_TX_FC_EN;
		if (rx_pause)
			val |= MVPP22_CTRL4_RX_FC_EN;
6189 6190 6191 6192

		mvpp2_modify(port->base + MVPP22_GMAC_CTRL_4_REG,
			     MVPP22_CTRL4_RX_FC_EN | MVPP22_CTRL4_TX_FC_EN,
			     val);
A
Antoine Tenart 已提交
6193 6194 6195 6196 6197 6198
	}

	mvpp2_port_enable(port);

	mvpp2_egress_enable(port);
	mvpp2_ingress_enable(port);
6199
	netif_tx_wake_all_queues(port->dev);
A
Antoine Tenart 已提交
6200 6201
}

6202 6203
static void mvpp2_mac_link_down(struct phylink_config *config,
				unsigned int mode, phy_interface_t interface)
A
Antoine Tenart 已提交
6204
{
6205
	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
A
Antoine Tenart 已提交
6206 6207
	u32 val;

6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219
	if (!phylink_autoneg_inband(mode)) {
		if (mvpp2_is_xlg(interface)) {
			val = readl(port->base + MVPP22_XLG_CTRL0_REG);
			val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
			val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
			writel(val, port->base + MVPP22_XLG_CTRL0_REG);
		} else {
			val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
			val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
			val |= MVPP2_GMAC_FORCE_LINK_DOWN;
			writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
		}
A
Antoine Tenart 已提交
6220 6221
	}

6222
	netif_tx_stop_all_queues(port->dev);
A
Antoine Tenart 已提交
6223 6224 6225 6226 6227 6228 6229 6230
	mvpp2_egress_disable(port);
	mvpp2_ingress_disable(port);

	mvpp2_port_disable(port);
}

static const struct phylink_mac_ops mvpp2_phylink_ops = {
	.validate = mvpp2_phylink_validate,
6231
	.mac_prepare = mvpp2_mac_prepare,
A
Antoine Tenart 已提交
6232
	.mac_config = mvpp2_mac_config,
6233
	.mac_finish = mvpp2_mac_finish,
A
Antoine Tenart 已提交
6234 6235 6236 6237
	.mac_link_up = mvpp2_mac_link_up,
	.mac_link_down = mvpp2_mac_link_down,
};

R
Russell King 已提交
6238 6239 6240 6241 6242 6243 6244 6245 6246 6247
/* Work-around for ACPI */
static void mvpp2_acpi_start(struct mvpp2_port *port)
{
	/* Phylink isn't used as of now for ACPI, so the MAC has to be
	 * configured manually when the interface is started. This will
	 * be removed as soon as the phylink ACPI support lands in.
	 */
	struct phylink_link_state state = {
		.interface = port->phy_interface,
	};
6248 6249
	mvpp2__mac_prepare(&port->phylink_config, MLO_AN_INBAND,
			   port->phy_interface);
R
Russell King 已提交
6250
	mvpp2_mac_config(&port->phylink_config, MLO_AN_INBAND, &state);
6251 6252 6253
	port->phylink_pcs.ops->pcs_config(&port->phylink_pcs, MLO_AN_INBAND,
					  port->phy_interface,
					  state.advertising, false);
6254 6255
	mvpp2_mac_finish(&port->phylink_config, MLO_AN_INBAND,
			 port->phy_interface);
R
Russell King 已提交
6256 6257 6258 6259 6260
	mvpp2_mac_link_up(&port->phylink_config, NULL,
			  MLO_AN_INBAND, port->phy_interface,
			  SPEED_UNKNOWN, DUPLEX_UNKNOWN, false, false);
}

6261 6262
/* Ports initialization */
static int mvpp2_port_probe(struct platform_device *pdev,
6263
			    struct fwnode_handle *port_fwnode,
6264
			    struct mvpp2 *priv)
6265
{
6266
	struct phy *comphy = NULL;
6267
	struct mvpp2_port *port;
6268
	struct mvpp2_port_pcpu *port_pcpu;
6269
	struct device_node *port_node = to_of_node(port_fwnode);
6270
	netdev_features_t features;
6271
	struct net_device *dev;
A
Antoine Tenart 已提交
6272
	struct phylink *phylink;
6273
	char *mac_from = "";
6274
	unsigned int ntxqs, nrxqs, thread;
6275
	unsigned long flags = 0;
6276
	bool has_tx_irqs;
6277 6278
	u32 id;
	int phy_mode;
6279
	int err, i;
6280

6281 6282 6283 6284 6285
	has_tx_irqs = mvpp2_port_has_irqs(priv, port_node, &flags);
	if (!has_tx_irqs && queue_mode == MVPP2_QDIST_MULTI_MODE) {
		dev_err(&pdev->dev,
			"not enough IRQs to support multi queue mode\n");
		return -EINVAL;
6286
	}
6287

6288
	ntxqs = MVPP2_MAX_TXQ;
M
Matteo Croce 已提交
6289
	nrxqs = mvpp2_get_nrxqs(priv);
6290 6291

	dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
6292 6293 6294
	if (!dev)
		return -ENOMEM;

6295
	phy_mode = fwnode_get_phy_mode(port_fwnode);
6296 6297 6298 6299 6300 6301
	if (phy_mode < 0) {
		dev_err(&pdev->dev, "incorrect phy mode\n");
		err = phy_mode;
		goto err_free_netdev;
	}

6302 6303 6304 6305 6306 6307 6308 6309 6310
	/*
	 * Rewrite 10GBASE-KR to 10GBASE-R for compatibility with existing DT.
	 * Existing usage of 10GBASE-KR is not correct; no backplane
	 * negotiation is done, and this driver does not actually support
	 * 10GBASE-KR.
	 */
	if (phy_mode == PHY_INTERFACE_MODE_10GKR)
		phy_mode = PHY_INTERFACE_MODE_10GBASER;

6311 6312 6313 6314 6315 6316 6317 6318
	if (port_node) {
		comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
		if (IS_ERR(comphy)) {
			if (PTR_ERR(comphy) == -EPROBE_DEFER) {
				err = -EPROBE_DEFER;
				goto err_free_netdev;
			}
			comphy = NULL;
6319 6320 6321
		}
	}

6322
	if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
6323 6324 6325 6326 6327
		err = -EINVAL;
		dev_err(&pdev->dev, "missing port-id value\n");
		goto err_free_netdev;
	}

6328
	dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
6329 6330 6331 6332 6333
	dev->watchdog_timeo = 5 * HZ;
	dev->netdev_ops = &mvpp2_netdev_ops;
	dev->ethtool_ops = &mvpp2_eth_tool_ops;

	port = netdev_priv(dev);
6334
	port->dev = dev;
6335
	port->fwnode = port_fwnode;
A
Antoine Tenart 已提交
6336
	port->has_phy = !!of_find_property(port_node, "phy", NULL);
6337 6338
	port->ntxqs = ntxqs;
	port->nrxqs = nrxqs;
6339 6340
	port->priv = priv;
	port->has_tx_irqs = has_tx_irqs;
6341
	port->flags = flags;
6342

6343 6344
	err = mvpp2_queue_vectors_init(port, port_node);
	if (err)
6345 6346
		goto err_free_netdev;

6347
	if (port_node)
6348
		port->port_irq = of_irq_get_byname(port_node, "link");
6349
	else
6350 6351
		port->port_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
	if (port->port_irq == -EPROBE_DEFER) {
6352 6353 6354
		err = -EPROBE_DEFER;
		goto err_deinit_qvecs;
	}
6355
	if (port->port_irq <= 0)
6356
		/* the link irq is optional */
6357
		port->port_irq = 0;
6358

6359
	if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
6360 6361 6362
		port->flags |= MVPP2_F_LOOPBACK;

	port->id = id;
6363
	if (priv->hw_version == MVPP21)
6364
		port->first_rxq = port->id * port->nrxqs;
6365 6366 6367
	else
		port->first_rxq = port->id * priv->max_port_rxqs;

A
Antoine Tenart 已提交
6368
	port->of_node = port_node;
6369
	port->phy_interface = phy_mode;
6370
	port->comphy = comphy;
6371

6372
	if (priv->hw_version == MVPP21) {
6373
		port->base = devm_platform_ioremap_resource(pdev, 2 + id);
6374 6375
		if (IS_ERR(port->base)) {
			err = PTR_ERR(port->base);
6376
			goto err_free_irq;
6377
		}
6378 6379 6380 6381

		port->stats_base = port->priv->lms_base +
				   MVPP21_MIB_COUNTERS_OFFSET +
				   port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
6382
	} else {
6383 6384
		if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
					     &port->gop_id)) {
6385 6386
			err = -EINVAL;
			dev_err(&pdev->dev, "missing gop-port-id value\n");
6387
			goto err_deinit_qvecs;
6388 6389 6390
		}

		port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
6391 6392 6393
		port->stats_base = port->priv->iface_base +
				   MVPP22_MIB_COUNTERS_OFFSET +
				   port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
6394 6395 6396 6397 6398 6399

		/* We may want a property to describe whether we should use
		 * MAC hardware timestamping.
		 */
		if (priv->tai)
			port->hwtstamp = true;
6400 6401
	}

6402
	/* Alloc per-cpu and ethtool stats */
6403 6404 6405
	port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
	if (!port->stats) {
		err = -ENOMEM;
6406
		goto err_free_irq;
6407 6408
	}

6409
	port->ethtool_stats = devm_kcalloc(&pdev->dev,
6410
					   MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs),
6411 6412 6413 6414 6415 6416
					   sizeof(u64), GFP_KERNEL);
	if (!port->ethtool_stats) {
		err = -ENOMEM;
		goto err_free_stats;
	}

6417 6418 6419
	mutex_init(&port->gather_stats_lock);
	INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);

6420
	mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
6421

6422 6423
	port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
	port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
6424 6425 6426 6427 6428 6429 6430
	SET_NETDEV_DEV(dev, &pdev->dev);

	err = mvpp2_port_init(port);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to init port %d\n", id);
		goto err_free_stats;
	}
6431 6432 6433

	mvpp2_port_periodic_xon_disable(port);

6434
	mvpp2_mac_reset_assert(port);
6435
	mvpp22_pcs_reset_assert(port);
6436

6437 6438 6439 6440 6441 6442
	port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
	if (!port->pcpu) {
		err = -ENOMEM;
		goto err_free_txq_pcpu;
	}

6443
	if (!port->has_tx_irqs) {
6444
		for (thread = 0; thread < priv->nthreads; thread++) {
6445
			port_pcpu = per_cpu_ptr(port->pcpu, thread);
6446

6447
			hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6448
				     HRTIMER_MODE_REL_PINNED_SOFT);
6449 6450
			port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
			port_pcpu->timer_scheduled = false;
6451
			port_pcpu->dev = dev;
6452
		}
6453 6454
	}

6455 6456
	features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
		   NETIF_F_TSO;
6457
	dev->features = features | NETIF_F_RXCSUM;
6458 6459
	dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
			    NETIF_F_HW_VLAN_CTAG_FILTER;
6460

6461
	if (mvpp22_rss_is_supported()) {
6462
		dev->hw_features |= NETIF_F_RXHASH;
6463 6464
		dev->features |= NETIF_F_NTUPLE;
	}
6465

M
Matteo Croce 已提交
6466 6467
	if (!port->priv->percpu_pools)
		mvpp2_set_hw_csum(port, port->pool_long->id);
6468

6469
	dev->vlan_features |= features;
6470
	dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
6471
	dev->priv_flags |= IFF_UNICAST_FLT;
6472

6473
	/* MTU range: 68 - 9704 */
6474
	dev->min_mtu = ETH_MIN_MTU;
6475 6476
	/* 9704 == 9728 - 20 and rounding to 8 */
	dev->max_mtu = MVPP2_BM_JUMBO_PKT_SIZE;
6477
	dev->dev.of_node = port_node;
6478

A
Antoine Tenart 已提交
6479 6480
	/* Phylink isn't used w/ ACPI as of now */
	if (port_node) {
6481 6482 6483 6484 6485
		port->phylink_config.dev = &dev->dev;
		port->phylink_config.type = PHYLINK_NETDEV;

		phylink = phylink_create(&port->phylink_config, port_fwnode,
					 phy_mode, &mvpp2_phylink_ops);
A
Antoine Tenart 已提交
6486 6487 6488 6489 6490 6491 6492 6493 6494
		if (IS_ERR(phylink)) {
			err = PTR_ERR(phylink);
			goto err_free_port_pcpu;
		}
		port->phylink = phylink;
	} else {
		port->phylink = NULL;
	}

6495 6496 6497 6498 6499 6500 6501 6502 6503 6504
	/* Cycle the comphy to power it down, saving 270mW per port -
	 * don't worry about an error powering it up. When the comphy
	 * driver does this, we can remove this code.
	 */
	if (port->comphy) {
		err = mvpp22_comphy_init(port);
		if (err == 0)
			phy_power_off(port->comphy);
	}

6505 6506 6507
	err = register_netdev(dev);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to register netdev\n");
A
Antoine Tenart 已提交
6508
		goto err_phylink;
6509 6510 6511
	}
	netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);

6512 6513
	priv->port_list[priv->port_count++] = port;

6514 6515
	return 0;

A
Antoine Tenart 已提交
6516 6517 6518
err_phylink:
	if (port->phylink)
		phylink_destroy(port->phylink);
6519 6520
err_free_port_pcpu:
	free_percpu(port->pcpu);
6521
err_free_txq_pcpu:
6522
	for (i = 0; i < port->ntxqs; i++)
6523 6524 6525
		free_percpu(port->txqs[i]->pcpu);
err_free_stats:
	free_percpu(port->stats);
6526
err_free_irq:
6527 6528
	if (port->port_irq)
		irq_dispose_mapping(port->port_irq);
6529 6530
err_deinit_qvecs:
	mvpp2_queue_vectors_deinit(port);
6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541
err_free_netdev:
	free_netdev(dev);
	return err;
}

/* Ports removal routine */
static void mvpp2_port_remove(struct mvpp2_port *port)
{
	int i;

	unregister_netdev(port->dev);
A
Antoine Tenart 已提交
6542 6543
	if (port->phylink)
		phylink_destroy(port->phylink);
6544
	free_percpu(port->pcpu);
6545
	free_percpu(port->stats);
6546
	for (i = 0; i < port->ntxqs; i++)
6547
		free_percpu(port->txqs[i]->pcpu);
6548
	mvpp2_queue_vectors_deinit(port);
6549 6550
	if (port->port_irq)
		irq_dispose_mapping(port->port_irq);
6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593
	free_netdev(port->dev);
}

/* Initialize decoding windows */
static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
				    struct mvpp2 *priv)
{
	u32 win_enable;
	int i;

	for (i = 0; i < 6; i++) {
		mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
		mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);

		if (i < 4)
			mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
	}

	win_enable = 0;

	for (i = 0; i < dram->num_cs; i++) {
		const struct mbus_dram_window *cs = dram->cs + i;

		mvpp2_write(priv, MVPP2_WIN_BASE(i),
			    (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
			    dram->mbus_dram_target_id);

		mvpp2_write(priv, MVPP2_WIN_SIZE(i),
			    (cs->size - 1) & 0xffff0000);

		win_enable |= (1 << i);
	}

	mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
}

/* Initialize Rx FIFO's */
static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
{
	int port;

	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6594
			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
6595
		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629
			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
	}

	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
		    MVPP2_RX_FIFO_PORT_MIN_PKT);
	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
}

static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
{
	int port;

	/* The FIFO size parameters are set depending on the maximum speed a
	 * given port can handle:
	 * - Port 0: 10Gbps
	 * - Port 1: 2.5Gbps
	 * - Ports 2 and 3: 1Gbps
	 */

	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
		    MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
		    MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);

	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
		    MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
		    MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);

	for (port = 2; port < MVPP2_MAX_PORTS; port++) {
		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
6630 6631 6632 6633 6634 6635 6636
	}

	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
		    MVPP2_RX_FIFO_PORT_MIN_PKT);
	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
}

6637 6638 6639 6640
/* Initialize Tx FIFO's: the total FIFO size is 19kB on PPv2.2 and 10G
 * interfaces must have a Tx FIFO size of 10kB. As only port 0 can do 10G,
 * configure its Tx FIFO size to 10kB and the others ports Tx FIFO size to 3kB.
 */
6641 6642
static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
{
6643
	int port, size, thrs;
6644

6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655
	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
		if (port == 0) {
			size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
			thrs = MVPP2_TX_FIFO_THRESHOLD_10KB;
		} else {
			size = MVPP22_TX_FIFO_DATA_SIZE_3KB;
			thrs = MVPP2_TX_FIFO_THRESHOLD_3KB;
		}
		mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
		mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), thrs);
	}
6656 6657
}

6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711
static void mvpp2_axi_init(struct mvpp2 *priv)
{
	u32 val, rdval, wrval;

	mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);

	/* AXI Bridge Configuration */

	rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
		<< MVPP22_AXI_ATTR_CACHE_OFFS;
	rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;

	wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
		<< MVPP22_AXI_ATTR_CACHE_OFFS;
	wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;

	/* BM */
	mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
	mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);

	/* Descriptors */
	mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
	mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);

	/* Buffer Data */
	mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
	mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);

	val = MVPP22_AXI_CODE_CACHE_NON_CACHE
		<< MVPP22_AXI_CODE_CACHE_OFFS;
	val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
	mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
	mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);

	val = MVPP22_AXI_CODE_CACHE_RD_CACHE
		<< MVPP22_AXI_CODE_CACHE_OFFS;
	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
		<< MVPP22_AXI_CODE_DOMAIN_OFFS;

	mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);

	val = MVPP22_AXI_CODE_CACHE_WR_CACHE
		<< MVPP22_AXI_CODE_CACHE_OFFS;
	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
		<< MVPP22_AXI_CODE_DOMAIN_OFFS;

	mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
}

6712 6713 6714 6715 6716
/* Initialize network controller common part HW */
static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
{
	const struct mbus_dram_target_info *dram_target_info;
	int err, i;
6717
	u32 val;
6718 6719 6720 6721 6722 6723

	/* MBUS windows configuration */
	dram_target_info = mv_mbus_dram_info();
	if (dram_target_info)
		mvpp2_conf_mbus_windows(dram_target_info, priv);

6724 6725 6726
	if (priv->hw_version == MVPP22)
		mvpp2_axi_init(priv);

6727
	/* Disable HW PHY polling */
6728 6729 6730 6731 6732 6733 6734 6735 6736
	if (priv->hw_version == MVPP21) {
		val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
		val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
		writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
	} else {
		val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
		val &= ~MVPP22_SMI_POLLING_EN;
		writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
	}
6737

6738
	/* Allocate and initialize aggregated TXQs */
6739
	priv->aggr_txqs = devm_kcalloc(&pdev->dev, MVPP2_MAX_THREADS,
6740
				       sizeof(*priv->aggr_txqs),
6741 6742 6743 6744
				       GFP_KERNEL);
	if (!priv->aggr_txqs)
		return -ENOMEM;

6745
	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
6746 6747
		priv->aggr_txqs[i].id = i;
		priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6748
		err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
6749 6750 6751 6752
		if (err < 0)
			return err;
	}

6753 6754
	/* Fifo Init */
	if (priv->hw_version == MVPP21) {
6755
		mvpp2_rx_fifo_init(priv);
6756
	} else {
6757
		mvpp22_rx_fifo_init(priv);
6758 6759
		mvpp22_tx_fifo_init(priv);
	}
6760

6761 6762 6763
	if (priv->hw_version == MVPP21)
		writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
		       priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
6764 6765 6766 6767 6768

	/* Allow cache snoop when transmiting packets */
	mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);

	/* Buffer Manager initialization */
M
Matteo Croce 已提交
6769
	err = mvpp2_bm_init(&pdev->dev, priv);
6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785
	if (err < 0)
		return err;

	/* Parser default initialization */
	err = mvpp2_prs_default_init(pdev, priv);
	if (err < 0)
		return err;

	/* Classifier default initialization */
	mvpp2_cls_init(priv);

	return 0;
}

static int mvpp2_probe(struct platform_device *pdev)
{
6786
	const struct acpi_device_id *acpi_id;
6787 6788
	struct fwnode_handle *fwnode = pdev->dev.fwnode;
	struct fwnode_handle *port_fwnode;
6789 6790
	struct mvpp2 *priv;
	struct resource *res;
6791
	void __iomem *base;
6792
	int i, shared;
6793 6794
	int err;

6795
	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
6796 6797 6798
	if (!priv)
		return -ENOMEM;

6799 6800 6801
	if (has_acpi_companion(&pdev->dev)) {
		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
					    &pdev->dev);
6802 6803
		if (!acpi_id)
			return -EINVAL;
6804 6805 6806 6807 6808
		priv->hw_version = (unsigned long)acpi_id->driver_data;
	} else {
		priv->hw_version =
			(unsigned long)of_device_get_match_data(&pdev->dev);
	}
6809

6810 6811 6812 6813 6814 6815
	/* multi queue mode isn't supported on PPV2.1, fallback to single
	 * mode
	 */
	if (priv->hw_version == MVPP21)
		queue_mode = MVPP2_QDIST_SINGLE_MODE;

6816
	base = devm_platform_ioremap_resource(pdev, 0);
6817 6818 6819 6820
	if (IS_ERR(base))
		return PTR_ERR(base);

	if (priv->hw_version == MVPP21) {
6821
		priv->lms_base = devm_platform_ioremap_resource(pdev, 1);
6822 6823 6824 6825
		if (IS_ERR(priv->lms_base))
			return PTR_ERR(priv->lms_base);
	} else {
		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836
		if (has_acpi_companion(&pdev->dev)) {
			/* In case the MDIO memory region is declared in
			 * the ACPI, it can already appear as 'in-use'
			 * in the OS. Because it is overlapped by second
			 * region of the network controller, make
			 * sure it is released, before requesting it again.
			 * The care is taken by mvpp2 driver to avoid
			 * concurrent access to this memory region.
			 */
			release_resource(res);
		}
6837 6838 6839
		priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
		if (IS_ERR(priv->iface_base))
			return PTR_ERR(priv->iface_base);
6840
	}
A
Antoine Ténart 已提交
6841

6842
	if (priv->hw_version == MVPP22 && dev_of_node(&pdev->dev)) {
A
Antoine Ténart 已提交
6843 6844 6845 6846 6847 6848 6849 6850 6851 6852
		priv->sysctrl_base =
			syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
							"marvell,system-controller");
		if (IS_ERR(priv->sysctrl_base))
			/* The system controller regmap is optional for dt
			 * compatibility reasons. When not provided, the
			 * configuration of the GoP relies on the
			 * firmware/bootloader.
			 */
			priv->sysctrl_base = NULL;
6853 6854
	}

M
Matteo Croce 已提交
6855 6856 6857 6858
	if (priv->hw_version == MVPP22 &&
	    mvpp2_get_nrxqs(priv) * 2 <= MVPP2_BM_MAX_POOLS)
		priv->percpu_pools = 1;

6859 6860
	mvpp2_setup_bm_pool();

6861 6862 6863 6864 6865 6866 6867 6868 6869

	priv->nthreads = min_t(unsigned int, num_present_cpus(),
			       MVPP2_MAX_THREADS);

	shared = num_present_cpus() - priv->nthreads;
	if (shared > 0)
		bitmap_fill(&priv->lock_map,
			    min_t(int, shared, MVPP2_MAX_THREADS));

6870
	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
6871 6872 6873 6874
		u32 addr_space_sz;

		addr_space_sz = (priv->hw_version == MVPP21 ?
				 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
6875
		priv->swth_base[i] = base + i * addr_space_sz;
6876
	}
6877

6878 6879 6880 6881 6882
	if (priv->hw_version == MVPP21)
		priv->max_port_rxqs = 8;
	else
		priv->max_port_rxqs = 32;

6883 6884 6885 6886 6887 6888 6889
	if (dev_of_node(&pdev->dev)) {
		priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
		if (IS_ERR(priv->pp_clk))
			return PTR_ERR(priv->pp_clk);
		err = clk_prepare_enable(priv->pp_clk);
		if (err < 0)
			return err;
6890

6891 6892 6893 6894
		priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
		if (IS_ERR(priv->gop_clk)) {
			err = PTR_ERR(priv->gop_clk);
			goto err_pp_clk;
6895
		}
6896
		err = clk_prepare_enable(priv->gop_clk);
6897
		if (err < 0)
6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909
			goto err_pp_clk;

		if (priv->hw_version == MVPP22) {
			priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
			if (IS_ERR(priv->mg_clk)) {
				err = PTR_ERR(priv->mg_clk);
				goto err_gop_clk;
			}

			err = clk_prepare_enable(priv->mg_clk);
			if (err < 0)
				goto err_gop_clk;
6910 6911 6912 6913 6914 6915 6916 6917 6918

			priv->mg_core_clk = devm_clk_get(&pdev->dev, "mg_core_clk");
			if (IS_ERR(priv->mg_core_clk)) {
				priv->mg_core_clk = NULL;
			} else {
				err = clk_prepare_enable(priv->mg_core_clk);
				if (err < 0)
					goto err_mg_clk;
			}
6919
		}
6920 6921 6922 6923 6924

		priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
		if (IS_ERR(priv->axi_clk)) {
			err = PTR_ERR(priv->axi_clk);
			if (err == -EPROBE_DEFER)
6925
				goto err_mg_core_clk;
6926 6927 6928 6929
			priv->axi_clk = NULL;
		} else {
			err = clk_prepare_enable(priv->axi_clk);
			if (err < 0)
6930
				goto err_mg_core_clk;
6931
		}
6932

6933 6934 6935 6936 6937 6938 6939
		/* Get system's tclk rate */
		priv->tclk = clk_get_rate(priv->pp_clk);
	} else if (device_property_read_u32(&pdev->dev, "clock-frequency",
					    &priv->tclk)) {
		dev_err(&pdev->dev, "missing clock-frequency value\n");
		return -EINVAL;
	}
6940

6941
	if (priv->hw_version == MVPP22) {
6942
		err = dma_set_mask(&pdev->dev, MVPP2_DESC_DMA_MASK);
6943
		if (err)
6944
			goto err_axi_clk;
6945 6946 6947 6948 6949 6950 6951
		/* Sadly, the BM pools all share the same register to
		 * store the high 32 bits of their address. So they
		 * must all have the same high 32 bits, which forces
		 * us to restrict coherent memory to DMA_BIT_MASK(32).
		 */
		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
		if (err)
6952
			goto err_axi_clk;
6953 6954
	}

6955 6956 6957 6958
	/* Initialize network controller */
	err = mvpp2_init(pdev, priv);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to initialize controller\n");
6959
		goto err_axi_clk;
6960 6961
	}

R
Russell King 已提交
6962 6963 6964 6965
	err = mvpp22_tai_probe(&pdev->dev, priv);
	if (err < 0)
		goto err_axi_clk;

6966
	/* Initialize ports */
6967 6968
	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
		err = mvpp2_port_probe(pdev, port_fwnode, priv);
6969
		if (err < 0)
6970
			goto err_port_probe;
6971 6972 6973 6974 6975
	}

	if (priv->port_count == 0) {
		dev_err(&pdev->dev, "no ports enabled\n");
		err = -ENODEV;
6976
		goto err_axi_clk;
6977 6978
	}

6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990
	/* Statistics must be gathered regularly because some of them (like
	 * packets counters) are 32-bit registers and could overflow quite
	 * quickly. For instance, a 10Gb link used at full bandwidth with the
	 * smallest packets (64B) will overflow a 32-bit counter in less than
	 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
	 */
	snprintf(priv->queue_name, sizeof(priv->queue_name),
		 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
		 priv->port_count > 1 ? "+" : "");
	priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
	if (!priv->stats_queue) {
		err = -ENOMEM;
6991
		goto err_port_probe;
6992 6993
	}

6994 6995
	mvpp2_dbgfs_init(priv, pdev->name);

6996 6997 6998
	platform_set_drvdata(pdev, priv);
	return 0;

6999 7000
err_port_probe:
	i = 0;
7001
	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7002 7003 7004 7005
		if (priv->port_list[i])
			mvpp2_port_remove(priv->port_list[i]);
		i++;
	}
7006
err_axi_clk:
7007
	clk_disable_unprepare(priv->axi_clk);
7008 7009 7010 7011

err_mg_core_clk:
	if (priv->hw_version == MVPP22)
		clk_disable_unprepare(priv->mg_core_clk);
7012
err_mg_clk:
7013 7014
	if (priv->hw_version == MVPP22)
		clk_disable_unprepare(priv->mg_clk);
7015 7016 7017 7018 7019 7020 7021 7022 7023 7024
err_gop_clk:
	clk_disable_unprepare(priv->gop_clk);
err_pp_clk:
	clk_disable_unprepare(priv->pp_clk);
	return err;
}

static int mvpp2_remove(struct platform_device *pdev)
{
	struct mvpp2 *priv = platform_get_drvdata(pdev);
7025
	struct fwnode_handle *fwnode = pdev->dev.fwnode;
S
Sven Auhagen 已提交
7026
	int i = 0, poolnum = MVPP2_BM_POOLS_NUM;
7027
	struct fwnode_handle *port_fwnode;
7028

7029 7030
	mvpp2_dbgfs_cleanup(priv);

7031
	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7032 7033
		if (priv->port_list[i]) {
			mutex_destroy(&priv->port_list[i]->gather_stats_lock);
7034
			mvpp2_port_remove(priv->port_list[i]);
7035
		}
7036 7037 7038
		i++;
	}

7039 7040
	destroy_workqueue(priv->stats_queue);

S
Sven Auhagen 已提交
7041 7042 7043 7044
	if (priv->percpu_pools)
		poolnum = mvpp2_get_nrxqs(priv) * 2;

	for (i = 0; i < poolnum; i++) {
7045 7046
		struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];

M
Matteo Croce 已提交
7047
		mvpp2_bm_pool_destroy(&pdev->dev, priv, bm_pool);
7048 7049
	}

7050
	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7051 7052 7053 7054 7055
		struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];

		dma_free_coherent(&pdev->dev,
				  MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
				  aggr_txq->descs,
7056
				  aggr_txq->descs_dma);
7057 7058
	}

7059 7060 7061
	if (is_acpi_node(port_fwnode))
		return 0;

7062
	clk_disable_unprepare(priv->axi_clk);
7063
	clk_disable_unprepare(priv->mg_core_clk);
7064
	clk_disable_unprepare(priv->mg_clk);
7065 7066 7067 7068 7069 7070 7071
	clk_disable_unprepare(priv->pp_clk);
	clk_disable_unprepare(priv->gop_clk);

	return 0;
}

static const struct of_device_id mvpp2_match[] = {
7072 7073 7074 7075
	{
		.compatible = "marvell,armada-375-pp2",
		.data = (void *)MVPP21,
	},
7076 7077 7078 7079
	{
		.compatible = "marvell,armada-7k-pp22",
		.data = (void *)MVPP22,
	},
7080 7081 7082 7083
	{ }
};
MODULE_DEVICE_TABLE(of, mvpp2_match);

7084
#ifdef CONFIG_ACPI
7085 7086 7087 7088 7089
static const struct acpi_device_id mvpp2_acpi_match[] = {
	{ "MRVL0110", MVPP22 },
	{ },
};
MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
7090
#endif
7091

7092 7093 7094 7095 7096 7097
static struct platform_driver mvpp2_driver = {
	.probe = mvpp2_probe,
	.remove = mvpp2_remove,
	.driver = {
		.name = MVPP2_DRIVER_NAME,
		.of_match_table = mvpp2_match,
7098
		.acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
7099 7100 7101 7102 7103 7104 7105
	},
};

module_platform_driver(mvpp2_driver);

MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
7106
MODULE_LICENSE("GPL v2");