xref: /linux/drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c (revision b5a051f6b840d48f159166ef073d3021989bfb50)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Marvell PPv2 network controller for Armada 375 SoC.
4  *
5  * Copyright (C) 2014 Marvell
6  *
7  * Marcin Wojtas <mw@semihalf.com>
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/platform_device.h>
15 #include <linux/skbuff.h>
16 #include <linux/inetdevice.h>
17 #include <linux/mbus.h>
18 #include <linux/module.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/interrupt.h>
21 #include <linux/cpumask.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
26 #include <linux/of_address.h>
27 #include <linux/phy.h>
28 #include <linux/phylink.h>
29 #include <linux/phy/phy.h>
30 #include <linux/ptp_classify.h>
31 #include <linux/clk.h>
32 #include <linux/hrtimer.h>
33 #include <linux/ktime.h>
34 #include <linux/regmap.h>
35 #include <uapi/linux/ppp_defs.h>
36 #include <net/ip.h>
37 #include <net/ipv6.h>
38 #include <net/page_pool/helpers.h>
39 #include <net/tso.h>
40 #include <linux/bpf_trace.h>
41 
42 #include "mvpp2.h"
43 #include "mvpp2_prs.h"
44 #include "mvpp2_cls.h"
45 
46 enum mvpp2_bm_pool_log_num {
47 	MVPP2_BM_SHORT,
48 	MVPP2_BM_LONG,
49 	MVPP2_BM_JUMBO,
50 	MVPP2_BM_POOLS_NUM
51 };
52 
53 static struct {
54 	int pkt_size;
55 	int buf_num;
56 } mvpp2_pools[MVPP2_BM_POOLS_NUM];
57 
58 /* The prototype is added here to be used in start_dev when using ACPI. This
59  * will be removed once phylink is used for all modes (dt+ACPI).
60  */
61 static void mvpp2_acpi_start(struct mvpp2_port *port);
62 
63 /* Queue modes */
64 #define MVPP2_QDIST_SINGLE_MODE	0
65 #define MVPP2_QDIST_MULTI_MODE	1
66 
67 static int queue_mode = MVPP2_QDIST_MULTI_MODE;
68 
69 module_param(queue_mode, int, 0444);
70 MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
71 
72 /* Utility/helper methods */
73 
mvpp2_write(struct mvpp2 * priv,u32 offset,u32 data)74 void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
75 {
76 	writel(data, priv->swth_base[0] + offset);
77 }
78 
mvpp2_read(struct mvpp2 * priv,u32 offset)79 u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
80 {
81 	return readl(priv->swth_base[0] + offset);
82 }
83 
mvpp2_read_relaxed(struct mvpp2 * priv,u32 offset)84 static u32 mvpp2_read_relaxed(struct mvpp2 *priv, u32 offset)
85 {
86 	return readl_relaxed(priv->swth_base[0] + offset);
87 }
88 
mvpp2_cpu_to_thread(struct mvpp2 * priv,int cpu)89 static inline u32 mvpp2_cpu_to_thread(struct mvpp2 *priv, int cpu)
90 {
91 	return cpu % priv->nthreads;
92 }
93 
mvpp2_cm3_write(struct mvpp2 * priv,u32 offset,u32 data)94 static void mvpp2_cm3_write(struct mvpp2 *priv, u32 offset, u32 data)
95 {
96 	writel(data, priv->cm3_base + offset);
97 }
98 
mvpp2_cm3_read(struct mvpp2 * priv,u32 offset)99 static u32 mvpp2_cm3_read(struct mvpp2 *priv, u32 offset)
100 {
101 	return readl(priv->cm3_base + offset);
102 }
103 
104 static struct page_pool *
mvpp2_create_page_pool(struct device * dev,int num,int len,enum dma_data_direction dma_dir)105 mvpp2_create_page_pool(struct device *dev, int num, int len,
106 		       enum dma_data_direction dma_dir)
107 {
108 	struct page_pool_params pp_params = {
109 		/* internal DMA mapping in page_pool */
110 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
111 		.pool_size = num,
112 		.nid = NUMA_NO_NODE,
113 		.dev = dev,
114 		.dma_dir = dma_dir,
115 		.offset = MVPP2_SKB_HEADROOM,
116 		.max_len = len,
117 	};
118 
119 	return page_pool_create(&pp_params);
120 }
121 
122 /* These accessors should be used to access:
123  *
124  * - per-thread registers, where each thread has its own copy of the
125  *   register.
126  *
127  *   MVPP2_BM_VIRT_ALLOC_REG
128  *   MVPP2_BM_ADDR_HIGH_ALLOC
129  *   MVPP22_BM_ADDR_HIGH_RLS_REG
130  *   MVPP2_BM_VIRT_RLS_REG
131  *   MVPP2_ISR_RX_TX_CAUSE_REG
132  *   MVPP2_ISR_RX_TX_MASK_REG
133  *   MVPP2_TXQ_NUM_REG
134  *   MVPP2_AGGR_TXQ_UPDATE_REG
135  *   MVPP2_TXQ_RSVD_REQ_REG
136  *   MVPP2_TXQ_RSVD_RSLT_REG
137  *   MVPP2_TXQ_SENT_REG
138  *   MVPP2_RXQ_NUM_REG
139  *
140  * - global registers that must be accessed through a specific thread
141  *   window, because they are related to an access to a per-thread
142  *   register
143  *
144  *   MVPP2_BM_PHY_ALLOC_REG    (related to MVPP2_BM_VIRT_ALLOC_REG)
145  *   MVPP2_BM_PHY_RLS_REG      (related to MVPP2_BM_VIRT_RLS_REG)
146  *   MVPP2_RXQ_THRESH_REG      (related to MVPP2_RXQ_NUM_REG)
147  *   MVPP2_RXQ_DESC_ADDR_REG   (related to MVPP2_RXQ_NUM_REG)
148  *   MVPP2_RXQ_DESC_SIZE_REG   (related to MVPP2_RXQ_NUM_REG)
149  *   MVPP2_RXQ_INDEX_REG       (related to MVPP2_RXQ_NUM_REG)
150  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
151  *   MVPP2_TXQ_DESC_ADDR_REG   (related to MVPP2_TXQ_NUM_REG)
152  *   MVPP2_TXQ_DESC_SIZE_REG   (related to MVPP2_TXQ_NUM_REG)
153  *   MVPP2_TXQ_INDEX_REG       (related to MVPP2_TXQ_NUM_REG)
154  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
155  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
156  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
157  */
mvpp2_thread_write(struct mvpp2 * priv,unsigned int thread,u32 offset,u32 data)158 static void mvpp2_thread_write(struct mvpp2 *priv, unsigned int thread,
159 			       u32 offset, u32 data)
160 {
161 	writel(data, priv->swth_base[thread] + offset);
162 }
163 
mvpp2_thread_read(struct mvpp2 * priv,unsigned int thread,u32 offset)164 static u32 mvpp2_thread_read(struct mvpp2 *priv, unsigned int thread,
165 			     u32 offset)
166 {
167 	return readl(priv->swth_base[thread] + offset);
168 }
169 
mvpp2_thread_write_relaxed(struct mvpp2 * priv,unsigned int thread,u32 offset,u32 data)170 static void mvpp2_thread_write_relaxed(struct mvpp2 *priv, unsigned int thread,
171 				       u32 offset, u32 data)
172 {
173 	writel_relaxed(data, priv->swth_base[thread] + offset);
174 }
175 
mvpp2_thread_read_relaxed(struct mvpp2 * priv,unsigned int thread,u32 offset)176 static u32 mvpp2_thread_read_relaxed(struct mvpp2 *priv, unsigned int thread,
177 				     u32 offset)
178 {
179 	return readl_relaxed(priv->swth_base[thread] + offset);
180 }
181 
mvpp2_txdesc_dma_addr_get(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc)182 static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
183 					    struct mvpp2_tx_desc *tx_desc)
184 {
185 	if (port->priv->hw_version == MVPP21)
186 		return le32_to_cpu(tx_desc->pp21.buf_dma_addr);
187 	else
188 		return le64_to_cpu(tx_desc->pp22.buf_dma_addr_ptp) &
189 		       MVPP2_DESC_DMA_MASK;
190 }
191 
mvpp2_txdesc_dma_addr_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,dma_addr_t dma_addr)192 static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
193 				      struct mvpp2_tx_desc *tx_desc,
194 				      dma_addr_t dma_addr)
195 {
196 	dma_addr_t addr, offset;
197 
198 	addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
199 	offset = dma_addr & MVPP2_TX_DESC_ALIGN;
200 
201 	if (port->priv->hw_version == MVPP21) {
202 		tx_desc->pp21.buf_dma_addr = cpu_to_le32(addr);
203 		tx_desc->pp21.packet_offset = offset;
204 	} else {
205 		__le64 val = cpu_to_le64(addr);
206 
207 		tx_desc->pp22.buf_dma_addr_ptp &= ~cpu_to_le64(MVPP2_DESC_DMA_MASK);
208 		tx_desc->pp22.buf_dma_addr_ptp |= val;
209 		tx_desc->pp22.packet_offset = offset;
210 	}
211 }
212 
mvpp2_txdesc_size_get(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc)213 static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
214 				    struct mvpp2_tx_desc *tx_desc)
215 {
216 	if (port->priv->hw_version == MVPP21)
217 		return le16_to_cpu(tx_desc->pp21.data_size);
218 	else
219 		return le16_to_cpu(tx_desc->pp22.data_size);
220 }
221 
mvpp2_txdesc_size_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,size_t size)222 static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
223 				  struct mvpp2_tx_desc *tx_desc,
224 				  size_t size)
225 {
226 	if (port->priv->hw_version == MVPP21)
227 		tx_desc->pp21.data_size = cpu_to_le16(size);
228 	else
229 		tx_desc->pp22.data_size = cpu_to_le16(size);
230 }
231 
mvpp2_txdesc_txq_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,unsigned int txq)232 static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
233 				 struct mvpp2_tx_desc *tx_desc,
234 				 unsigned int txq)
235 {
236 	if (port->priv->hw_version == MVPP21)
237 		tx_desc->pp21.phys_txq = txq;
238 	else
239 		tx_desc->pp22.phys_txq = txq;
240 }
241 
mvpp2_txdesc_cmd_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,unsigned int command)242 static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
243 				 struct mvpp2_tx_desc *tx_desc,
244 				 unsigned int command)
245 {
246 	if (port->priv->hw_version == MVPP21)
247 		tx_desc->pp21.command = cpu_to_le32(command);
248 	else
249 		tx_desc->pp22.command = cpu_to_le32(command);
250 }
251 
mvpp2_txdesc_offset_get(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc)252 static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
253 					    struct mvpp2_tx_desc *tx_desc)
254 {
255 	if (port->priv->hw_version == MVPP21)
256 		return tx_desc->pp21.packet_offset;
257 	else
258 		return tx_desc->pp22.packet_offset;
259 }
260 
mvpp2_rxdesc_dma_addr_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)261 static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
262 					    struct mvpp2_rx_desc *rx_desc)
263 {
264 	if (port->priv->hw_version == MVPP21)
265 		return le32_to_cpu(rx_desc->pp21.buf_dma_addr);
266 	else
267 		return le64_to_cpu(rx_desc->pp22.buf_dma_addr_key_hash) &
268 		       MVPP2_DESC_DMA_MASK;
269 }
270 
mvpp2_rxdesc_cookie_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)271 static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
272 					     struct mvpp2_rx_desc *rx_desc)
273 {
274 	if (port->priv->hw_version == MVPP21)
275 		return le32_to_cpu(rx_desc->pp21.buf_cookie);
276 	else
277 		return le64_to_cpu(rx_desc->pp22.buf_cookie_misc) &
278 		       MVPP2_DESC_DMA_MASK;
279 }
280 
mvpp2_rxdesc_size_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)281 static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
282 				    struct mvpp2_rx_desc *rx_desc)
283 {
284 	if (port->priv->hw_version == MVPP21)
285 		return le16_to_cpu(rx_desc->pp21.data_size);
286 	else
287 		return le16_to_cpu(rx_desc->pp22.data_size);
288 }
289 
mvpp2_rxdesc_status_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)290 static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
291 				   struct mvpp2_rx_desc *rx_desc)
292 {
293 	if (port->priv->hw_version == MVPP21)
294 		return le32_to_cpu(rx_desc->pp21.status);
295 	else
296 		return le32_to_cpu(rx_desc->pp22.status);
297 }
298 
mvpp2_txq_inc_get(struct mvpp2_txq_pcpu * txq_pcpu)299 static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
300 {
301 	txq_pcpu->txq_get_index++;
302 	if (txq_pcpu->txq_get_index == txq_pcpu->size)
303 		txq_pcpu->txq_get_index = 0;
304 }
305 
mvpp2_txq_inc_put(struct mvpp2_port * port,struct mvpp2_txq_pcpu * txq_pcpu,void * data,struct mvpp2_tx_desc * tx_desc,enum mvpp2_tx_buf_type buf_type)306 static void mvpp2_txq_inc_put(struct mvpp2_port *port,
307 			      struct mvpp2_txq_pcpu *txq_pcpu,
308 			      void *data,
309 			      struct mvpp2_tx_desc *tx_desc,
310 			      enum mvpp2_tx_buf_type buf_type)
311 {
312 	struct mvpp2_txq_pcpu_buf *tx_buf =
313 		txq_pcpu->buffs + txq_pcpu->txq_put_index;
314 	tx_buf->type = buf_type;
315 	if (buf_type == MVPP2_TYPE_SKB)
316 		tx_buf->skb = data;
317 	else
318 		tx_buf->xdpf = data;
319 	tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
320 	tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
321 		mvpp2_txdesc_offset_get(port, tx_desc);
322 	txq_pcpu->txq_put_index++;
323 	if (txq_pcpu->txq_put_index == txq_pcpu->size)
324 		txq_pcpu->txq_put_index = 0;
325 }
326 
327 /* Get number of maximum RXQ */
mvpp2_get_nrxqs(struct mvpp2 * priv)328 static int mvpp2_get_nrxqs(struct mvpp2 *priv)
329 {
330 	unsigned int nrxqs;
331 
332 	if (priv->hw_version >= MVPP22 && queue_mode == MVPP2_QDIST_SINGLE_MODE)
333 		return 1;
334 
335 	/* According to the PPv2.2 datasheet and our experiments on
336 	 * PPv2.1, RX queues have an allocation granularity of 4 (when
337 	 * more than a single one on PPv2.2).
338 	 * Round up to nearest multiple of 4.
339 	 */
340 	nrxqs = (num_possible_cpus() + 3) & ~0x3;
341 	if (nrxqs > MVPP2_PORT_MAX_RXQ)
342 		nrxqs = MVPP2_PORT_MAX_RXQ;
343 
344 	return nrxqs;
345 }
346 
347 /* Get number of physical egress port */
mvpp2_egress_port(struct mvpp2_port * port)348 static inline int mvpp2_egress_port(struct mvpp2_port *port)
349 {
350 	return MVPP2_MAX_TCONT + port->id;
351 }
352 
353 /* Get number of physical TXQ */
mvpp2_txq_phys(int port,int txq)354 static inline int mvpp2_txq_phys(int port, int txq)
355 {
356 	return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
357 }
358 
359 /* Returns a struct page if page_pool is set, otherwise a buffer */
mvpp2_frag_alloc(const struct mvpp2_bm_pool * pool,struct page_pool * page_pool)360 static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool,
361 			      struct page_pool *page_pool)
362 {
363 	if (page_pool)
364 		return page_pool_dev_alloc_pages(page_pool);
365 
366 	if (likely(pool->frag_size <= PAGE_SIZE))
367 		return netdev_alloc_frag(pool->frag_size);
368 
369 	return kmalloc(pool->frag_size, GFP_ATOMIC);
370 }
371 
mvpp2_frag_free(const struct mvpp2_bm_pool * pool,struct page_pool * page_pool,void * data)372 static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool,
373 			    struct page_pool *page_pool, void *data)
374 {
375 	if (page_pool)
376 		page_pool_put_full_page(page_pool, virt_to_head_page(data), false);
377 	else if (likely(pool->frag_size <= PAGE_SIZE))
378 		skb_free_frag(data);
379 	else
380 		kfree(data);
381 }
382 
383 /* Buffer Manager configuration routines */
384 
385 /* Create pool */
mvpp2_bm_pool_create(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,int size)386 static int mvpp2_bm_pool_create(struct device *dev, struct mvpp2 *priv,
387 				struct mvpp2_bm_pool *bm_pool, int size)
388 {
389 	u32 val;
390 
391 	/* Number of buffer pointers must be a multiple of 16, as per
392 	 * hardware constraints
393 	 */
394 	if (!IS_ALIGNED(size, 16))
395 		return -EINVAL;
396 
397 	/* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 and PPv2.3 needs 16
398 	 * bytes per buffer pointer
399 	 */
400 	if (priv->hw_version == MVPP21)
401 		bm_pool->size_bytes = 2 * sizeof(u32) * size;
402 	else
403 		bm_pool->size_bytes = 2 * sizeof(u64) * size;
404 
405 	bm_pool->virt_addr = dma_alloc_coherent(dev, bm_pool->size_bytes,
406 						&bm_pool->dma_addr,
407 						GFP_KERNEL);
408 	if (!bm_pool->virt_addr)
409 		return -ENOMEM;
410 
411 	if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
412 			MVPP2_BM_POOL_PTR_ALIGN)) {
413 		dma_free_coherent(dev, bm_pool->size_bytes,
414 				  bm_pool->virt_addr, bm_pool->dma_addr);
415 		dev_err(dev, "BM pool %d is not %d bytes aligned\n",
416 			bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
417 		return -ENOMEM;
418 	}
419 
420 	mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
421 		    lower_32_bits(bm_pool->dma_addr));
422 	mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
423 
424 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
425 	val |= MVPP2_BM_START_MASK;
426 
427 	val &= ~MVPP2_BM_LOW_THRESH_MASK;
428 	val &= ~MVPP2_BM_HIGH_THRESH_MASK;
429 
430 	/* Set 8 Pools BPPI threshold for MVPP23 */
431 	if (priv->hw_version == MVPP23) {
432 		val |= MVPP2_BM_LOW_THRESH_VALUE(MVPP23_BM_BPPI_LOW_THRESH);
433 		val |= MVPP2_BM_HIGH_THRESH_VALUE(MVPP23_BM_BPPI_HIGH_THRESH);
434 	} else {
435 		val |= MVPP2_BM_LOW_THRESH_VALUE(MVPP2_BM_BPPI_LOW_THRESH);
436 		val |= MVPP2_BM_HIGH_THRESH_VALUE(MVPP2_BM_BPPI_HIGH_THRESH);
437 	}
438 
439 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
440 
441 	bm_pool->size = size;
442 	bm_pool->pkt_size = 0;
443 	bm_pool->buf_num = 0;
444 
445 	return 0;
446 }
447 
448 /* Set pool buffer size */
mvpp2_bm_pool_bufsize_set(struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,int buf_size)449 static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
450 				      struct mvpp2_bm_pool *bm_pool,
451 				      int buf_size)
452 {
453 	u32 val;
454 
455 	bm_pool->buf_size = buf_size;
456 
457 	val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
458 	mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
459 }
460 
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)461 static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
462 				    struct mvpp2_bm_pool *bm_pool,
463 				    dma_addr_t *dma_addr,
464 				    phys_addr_t *phys_addr)
465 {
466 	unsigned int thread = mvpp2_cpu_to_thread(priv, get_cpu());
467 
468 	*dma_addr = mvpp2_thread_read(priv, thread,
469 				      MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
470 	*phys_addr = mvpp2_thread_read(priv, thread, MVPP2_BM_VIRT_ALLOC_REG);
471 
472 	if (priv->hw_version >= MVPP22) {
473 		u32 val;
474 		u32 dma_addr_highbits, phys_addr_highbits;
475 
476 		val = mvpp2_thread_read(priv, thread, MVPP22_BM_ADDR_HIGH_ALLOC);
477 		dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
478 		phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
479 			MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
480 
481 		if (sizeof(dma_addr_t) == 8)
482 			*dma_addr |= (u64)dma_addr_highbits << 32;
483 
484 		if (sizeof(phys_addr_t) == 8)
485 			*phys_addr |= (u64)phys_addr_highbits << 32;
486 	}
487 
488 	put_cpu();
489 }
490 
491 /* Free all buffers from the pool */
mvpp2_bm_bufs_free(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,int buf_num)492 static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
493 			       struct mvpp2_bm_pool *bm_pool, int buf_num)
494 {
495 	struct page_pool *pp = NULL;
496 	int i;
497 
498 	if (buf_num > bm_pool->buf_num) {
499 		WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
500 		     bm_pool->id, buf_num);
501 		buf_num = bm_pool->buf_num;
502 	}
503 
504 	if (priv->percpu_pools)
505 		pp = priv->page_pool[bm_pool->id];
506 
507 	for (i = 0; i < buf_num; i++) {
508 		dma_addr_t buf_dma_addr;
509 		phys_addr_t buf_phys_addr;
510 		void *data;
511 
512 		mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
513 					&buf_dma_addr, &buf_phys_addr);
514 
515 		if (!pp)
516 			dma_unmap_single(dev, buf_dma_addr,
517 					 bm_pool->buf_size, DMA_FROM_DEVICE);
518 
519 		data = (void *)phys_to_virt(buf_phys_addr);
520 		if (!data)
521 			break;
522 
523 		mvpp2_frag_free(bm_pool, pp, data);
524 	}
525 
526 	/* Update BM driver with number of buffers removed from pool */
527 	bm_pool->buf_num -= i;
528 }
529 
530 /* Check number of buffers in BM pool */
mvpp2_check_hw_buf_num(struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool)531 static int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
532 {
533 	int buf_num = 0;
534 
535 	buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
536 				    MVPP22_BM_POOL_PTRS_NUM_MASK;
537 	buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
538 				    MVPP2_BM_BPPI_PTR_NUM_MASK;
539 
540 	/* HW has one buffer ready which is not reflected in the counters */
541 	if (buf_num)
542 		buf_num += 1;
543 
544 	return buf_num;
545 }
546 
547 /* Cleanup pool */
mvpp2_bm_pool_destroy(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool)548 static int mvpp2_bm_pool_destroy(struct device *dev, struct mvpp2 *priv,
549 				 struct mvpp2_bm_pool *bm_pool)
550 {
551 	int buf_num;
552 	u32 val;
553 
554 	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
555 	mvpp2_bm_bufs_free(dev, priv, bm_pool, buf_num);
556 
557 	/* Check buffer counters after free */
558 	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
559 	if (buf_num) {
560 		WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
561 		     bm_pool->id, bm_pool->buf_num);
562 		return 0;
563 	}
564 
565 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
566 	val |= MVPP2_BM_STOP_MASK;
567 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
568 
569 	if (priv->percpu_pools) {
570 		page_pool_destroy(priv->page_pool[bm_pool->id]);
571 		priv->page_pool[bm_pool->id] = NULL;
572 	}
573 
574 	dma_free_coherent(dev, bm_pool->size_bytes,
575 			  bm_pool->virt_addr,
576 			  bm_pool->dma_addr);
577 	return 0;
578 }
579 
mvpp2_bm_pools_init(struct device * dev,struct mvpp2 * priv)580 static int mvpp2_bm_pools_init(struct device *dev, struct mvpp2 *priv)
581 {
582 	int i, err, size, poolnum = MVPP2_BM_POOLS_NUM;
583 	struct mvpp2_bm_pool *bm_pool;
584 
585 	if (priv->percpu_pools)
586 		poolnum = mvpp2_get_nrxqs(priv) * 2;
587 
588 	/* Create all pools with maximum size */
589 	size = MVPP2_BM_POOL_SIZE_MAX;
590 	for (i = 0; i < poolnum; i++) {
591 		bm_pool = &priv->bm_pools[i];
592 		bm_pool->id = i;
593 		err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
594 		if (err)
595 			goto err_unroll_pools;
596 		mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
597 	}
598 	return 0;
599 
600 err_unroll_pools:
601 	dev_err(dev, "failed to create BM pool %d, size %d\n", i, size);
602 	for (i = i - 1; i >= 0; i--)
603 		mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
604 	return err;
605 }
606 
607 /* Routine enable PPv23 8 pool mode */
mvpp23_bm_set_8pool_mode(struct mvpp2 * priv)608 static void mvpp23_bm_set_8pool_mode(struct mvpp2 *priv)
609 {
610 	int val;
611 
612 	val = mvpp2_read(priv, MVPP22_BM_POOL_BASE_ADDR_HIGH_REG);
613 	val |= MVPP23_BM_8POOL_MODE;
614 	mvpp2_write(priv, MVPP22_BM_POOL_BASE_ADDR_HIGH_REG, val);
615 }
616 
617 /* Cleanup pool before actual initialization in the OS */
mvpp2_bm_pool_cleanup(struct mvpp2 * priv,int pool_id)618 static void mvpp2_bm_pool_cleanup(struct mvpp2 *priv, int pool_id)
619 {
620 	unsigned int thread = mvpp2_cpu_to_thread(priv, get_cpu());
621 	u32 val;
622 	int i;
623 
624 	/* Drain the BM from all possible residues left by firmware */
625 	for (i = 0; i < MVPP2_BM_POOL_SIZE_MAX; i++)
626 		mvpp2_thread_read(priv, thread, MVPP2_BM_PHY_ALLOC_REG(pool_id));
627 
628 	put_cpu();
629 
630 	/* Stop the BM pool */
631 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(pool_id));
632 	val |= MVPP2_BM_STOP_MASK;
633 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(pool_id), val);
634 }
635 
mvpp2_bm_init(struct device * dev,struct mvpp2 * priv)636 static int mvpp2_bm_init(struct device *dev, struct mvpp2 *priv)
637 {
638 	enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
639 	int i, err, poolnum = MVPP2_BM_POOLS_NUM;
640 	struct mvpp2_port *port;
641 
642 	if (priv->percpu_pools)
643 		poolnum = mvpp2_get_nrxqs(priv) * 2;
644 
645 	/* Clean up the pool state in case it contains stale state */
646 	for (i = 0; i < poolnum; i++)
647 		mvpp2_bm_pool_cleanup(priv, i);
648 
649 	if (priv->percpu_pools) {
650 		for (i = 0; i < priv->port_count; i++) {
651 			port = priv->port_list[i];
652 			if (port->xdp_prog) {
653 				dma_dir = DMA_BIDIRECTIONAL;
654 				break;
655 			}
656 		}
657 
658 		for (i = 0; i < poolnum; i++) {
659 			/* the pool in use */
660 			int pn = i / (poolnum / 2);
661 
662 			priv->page_pool[i] =
663 				mvpp2_create_page_pool(dev,
664 						       mvpp2_pools[pn].buf_num,
665 						       mvpp2_pools[pn].pkt_size,
666 						       dma_dir);
667 			if (IS_ERR(priv->page_pool[i])) {
668 				int j;
669 
670 				for (j = 0; j < i; j++) {
671 					page_pool_destroy(priv->page_pool[j]);
672 					priv->page_pool[j] = NULL;
673 				}
674 				return PTR_ERR(priv->page_pool[i]);
675 			}
676 		}
677 	}
678 
679 	dev_info(dev, "using %d %s buffers\n", poolnum,
680 		 priv->percpu_pools ? "per-cpu" : "shared");
681 
682 	for (i = 0; i < poolnum; i++) {
683 		/* Mask BM all interrupts */
684 		mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
685 		/* Clear BM cause register */
686 		mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
687 	}
688 
689 	/* Allocate and initialize BM pools */
690 	priv->bm_pools = devm_kcalloc(dev, poolnum,
691 				      sizeof(*priv->bm_pools), GFP_KERNEL);
692 	if (!priv->bm_pools)
693 		return -ENOMEM;
694 
695 	if (priv->hw_version == MVPP23)
696 		mvpp23_bm_set_8pool_mode(priv);
697 
698 	err = mvpp2_bm_pools_init(dev, priv);
699 	if (err < 0)
700 		return err;
701 	return 0;
702 }
703 
mvpp2_setup_bm_pool(void)704 static void mvpp2_setup_bm_pool(void)
705 {
706 	/* Short pool */
707 	mvpp2_pools[MVPP2_BM_SHORT].buf_num  = MVPP2_BM_SHORT_BUF_NUM;
708 	mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;
709 
710 	/* Long pool */
711 	mvpp2_pools[MVPP2_BM_LONG].buf_num  = MVPP2_BM_LONG_BUF_NUM;
712 	mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
713 
714 	/* Jumbo pool */
715 	mvpp2_pools[MVPP2_BM_JUMBO].buf_num  = MVPP2_BM_JUMBO_BUF_NUM;
716 	mvpp2_pools[MVPP2_BM_JUMBO].pkt_size = MVPP2_BM_JUMBO_PKT_SIZE;
717 }
718 
719 /* Attach long pool to rxq */
mvpp2_rxq_long_pool_set(struct mvpp2_port * port,int lrxq,int long_pool)720 static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
721 				    int lrxq, int long_pool)
722 {
723 	u32 val, mask;
724 	int prxq;
725 
726 	/* Get queue physical ID */
727 	prxq = port->rxqs[lrxq]->id;
728 
729 	if (port->priv->hw_version == MVPP21)
730 		mask = MVPP21_RXQ_POOL_LONG_MASK;
731 	else
732 		mask = MVPP22_RXQ_POOL_LONG_MASK;
733 
734 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
735 	val &= ~mask;
736 	val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
737 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
738 }
739 
740 /* Attach short pool to rxq */
mvpp2_rxq_short_pool_set(struct mvpp2_port * port,int lrxq,int short_pool)741 static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
742 				     int lrxq, int short_pool)
743 {
744 	u32 val, mask;
745 	int prxq;
746 
747 	/* Get queue physical ID */
748 	prxq = port->rxqs[lrxq]->id;
749 
750 	if (port->priv->hw_version == MVPP21)
751 		mask = MVPP21_RXQ_POOL_SHORT_MASK;
752 	else
753 		mask = MVPP22_RXQ_POOL_SHORT_MASK;
754 
755 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
756 	val &= ~mask;
757 	val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
758 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
759 }
760 
mvpp2_buf_alloc(struct mvpp2_port * port,struct mvpp2_bm_pool * bm_pool,struct page_pool * page_pool,dma_addr_t * buf_dma_addr,phys_addr_t * buf_phys_addr,gfp_t gfp_mask)761 static void *mvpp2_buf_alloc(struct mvpp2_port *port,
762 			     struct mvpp2_bm_pool *bm_pool,
763 			     struct page_pool *page_pool,
764 			     dma_addr_t *buf_dma_addr,
765 			     phys_addr_t *buf_phys_addr,
766 			     gfp_t gfp_mask)
767 {
768 	dma_addr_t dma_addr;
769 	struct page *page;
770 	void *data;
771 
772 	data = mvpp2_frag_alloc(bm_pool, page_pool);
773 	if (!data)
774 		return NULL;
775 
776 	if (page_pool) {
777 		page = (struct page *)data;
778 		dma_addr = page_pool_get_dma_addr(page);
779 		data = page_to_virt(page);
780 	} else {
781 		dma_addr = dma_map_single(port->dev->dev.parent, data,
782 					  MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
783 					  DMA_FROM_DEVICE);
784 		if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
785 			mvpp2_frag_free(bm_pool, NULL, data);
786 			return NULL;
787 		}
788 	}
789 	*buf_dma_addr = dma_addr;
790 	*buf_phys_addr = virt_to_phys(data);
791 
792 	return data;
793 }
794 
795 /* Routine enable flow control for RXQs condition */
mvpp2_rxq_enable_fc(struct mvpp2_port * port)796 static void mvpp2_rxq_enable_fc(struct mvpp2_port *port)
797 {
798 	int val, cm3_state, host_id, q;
799 	int fq = port->first_rxq;
800 	unsigned long flags;
801 
802 	spin_lock_irqsave(&port->priv->mss_spinlock, flags);
803 
804 	/* Remove Flow control enable bit to prevent race between FW and Kernel
805 	 * If Flow control was enabled, it would be re-enabled.
806 	 */
807 	val = mvpp2_cm3_read(port->priv, MSS_FC_COM_REG);
808 	cm3_state = (val & FLOW_CONTROL_ENABLE_BIT);
809 	val &= ~FLOW_CONTROL_ENABLE_BIT;
810 	mvpp2_cm3_write(port->priv, MSS_FC_COM_REG, val);
811 
812 	/* Set same Flow control for all RXQs */
813 	for (q = 0; q < port->nrxqs; q++) {
814 		/* Set stop and start Flow control RXQ thresholds */
815 		val = MSS_THRESHOLD_START;
816 		val |= (MSS_THRESHOLD_STOP << MSS_RXQ_TRESH_STOP_OFFS);
817 		mvpp2_cm3_write(port->priv, MSS_RXQ_TRESH_REG(q, fq), val);
818 
819 		val = mvpp2_cm3_read(port->priv, MSS_RXQ_ASS_REG(q, fq));
820 		/* Set RXQ port ID */
821 		val &= ~(MSS_RXQ_ASS_PORTID_MASK << MSS_RXQ_ASS_Q_BASE(q, fq));
822 		val |= (port->id << MSS_RXQ_ASS_Q_BASE(q, fq));
823 		val &= ~(MSS_RXQ_ASS_HOSTID_MASK << (MSS_RXQ_ASS_Q_BASE(q, fq)
824 			+ MSS_RXQ_ASS_HOSTID_OFFS));
825 
826 		/* Calculate RXQ host ID:
827 		 * In Single queue mode: Host ID equal to Host ID used for
828 		 *			 shared RX interrupt
829 		 * In Multi queue mode: Host ID equal to number of
830 		 *			RXQ ID / number of CoS queues
831 		 * In Single resource mode: Host ID always equal to 0
832 		 */
833 		if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
834 			host_id = port->nqvecs;
835 		else if (queue_mode == MVPP2_QDIST_MULTI_MODE)
836 			host_id = q;
837 		else
838 			host_id = 0;
839 
840 		/* Set RXQ host ID */
841 		val |= (host_id << (MSS_RXQ_ASS_Q_BASE(q, fq)
842 			+ MSS_RXQ_ASS_HOSTID_OFFS));
843 
844 		mvpp2_cm3_write(port->priv, MSS_RXQ_ASS_REG(q, fq), val);
845 	}
846 
847 	/* Notify Firmware that Flow control config space ready for update */
848 	val = mvpp2_cm3_read(port->priv, MSS_FC_COM_REG);
849 	val |= FLOW_CONTROL_UPDATE_COMMAND_BIT;
850 	val |= cm3_state;
851 	mvpp2_cm3_write(port->priv, MSS_FC_COM_REG, val);
852 
853 	spin_unlock_irqrestore(&port->priv->mss_spinlock, flags);
854 }
855 
856 /* Routine disable flow control for RXQs condition */
mvpp2_rxq_disable_fc(struct mvpp2_port * port)857 static void mvpp2_rxq_disable_fc(struct mvpp2_port *port)
858 {
859 	int val, cm3_state, q;
860 	unsigned long flags;
861 	int fq = port->first_rxq;
862 
863 	spin_lock_irqsave(&port->priv->mss_spinlock, flags);
864 
865 	/* Remove Flow control enable bit to prevent race between FW and Kernel
866 	 * If Flow control was enabled, it would be re-enabled.
867 	 */
868 	val = mvpp2_cm3_read(port->priv, MSS_FC_COM_REG);
869 	cm3_state = (val & FLOW_CONTROL_ENABLE_BIT);
870 	val &= ~FLOW_CONTROL_ENABLE_BIT;
871 	mvpp2_cm3_write(port->priv, MSS_FC_COM_REG, val);
872 
873 	/* Disable Flow control for all RXQs */
874 	for (q = 0; q < port->nrxqs; q++) {
875 		/* Set threshold 0 to disable Flow control */
876 		val = 0;
877 		val |= (0 << MSS_RXQ_TRESH_STOP_OFFS);
878 		mvpp2_cm3_write(port->priv, MSS_RXQ_TRESH_REG(q, fq), val);
879 
880 		val = mvpp2_cm3_read(port->priv, MSS_RXQ_ASS_REG(q, fq));
881 
882 		val &= ~(MSS_RXQ_ASS_PORTID_MASK << MSS_RXQ_ASS_Q_BASE(q, fq));
883 
884 		val &= ~(MSS_RXQ_ASS_HOSTID_MASK << (MSS_RXQ_ASS_Q_BASE(q, fq)
885 			+ MSS_RXQ_ASS_HOSTID_OFFS));
886 
887 		mvpp2_cm3_write(port->priv, MSS_RXQ_ASS_REG(q, fq), val);
888 	}
889 
890 	/* Notify Firmware that Flow control config space ready for update */
891 	val = mvpp2_cm3_read(port->priv, MSS_FC_COM_REG);
892 	val |= FLOW_CONTROL_UPDATE_COMMAND_BIT;
893 	val |= cm3_state;
894 	mvpp2_cm3_write(port->priv, MSS_FC_COM_REG, val);
895 
896 	spin_unlock_irqrestore(&port->priv->mss_spinlock, flags);
897 }
898 
899 /* Routine disable/enable flow control for BM pool condition */
mvpp2_bm_pool_update_fc(struct mvpp2_port * port,struct mvpp2_bm_pool * pool,bool en)900 static void mvpp2_bm_pool_update_fc(struct mvpp2_port *port,
901 				    struct mvpp2_bm_pool *pool,
902 				    bool en)
903 {
904 	int val, cm3_state;
905 	unsigned long flags;
906 
907 	spin_lock_irqsave(&port->priv->mss_spinlock, flags);
908 
909 	/* Remove Flow control enable bit to prevent race between FW and Kernel
910 	 * If Flow control were enabled, it would be re-enabled.
911 	 */
912 	val = mvpp2_cm3_read(port->priv, MSS_FC_COM_REG);
913 	cm3_state = (val & FLOW_CONTROL_ENABLE_BIT);
914 	val &= ~FLOW_CONTROL_ENABLE_BIT;
915 	mvpp2_cm3_write(port->priv, MSS_FC_COM_REG, val);
916 
917 	/* Check if BM pool should be enabled/disable */
918 	if (en) {
919 		/* Set BM pool start and stop thresholds per port */
920 		val = mvpp2_cm3_read(port->priv, MSS_BUF_POOL_REG(pool->id));
921 		val |= MSS_BUF_POOL_PORT_OFFS(port->id);
922 		val &= ~MSS_BUF_POOL_START_MASK;
923 		val |= (MSS_THRESHOLD_START << MSS_BUF_POOL_START_OFFS);
924 		val &= ~MSS_BUF_POOL_STOP_MASK;
925 		val |= MSS_THRESHOLD_STOP;
926 		mvpp2_cm3_write(port->priv, MSS_BUF_POOL_REG(pool->id), val);
927 	} else {
928 		/* Remove BM pool from the port */
929 		val = mvpp2_cm3_read(port->priv, MSS_BUF_POOL_REG(pool->id));
930 		val &= ~MSS_BUF_POOL_PORT_OFFS(port->id);
931 
932 		/* Zero BM pool start and stop thresholds to disable pool
933 		 * flow control if pool empty (not used by any port)
934 		 */
935 		if (!pool->buf_num) {
936 			val &= ~MSS_BUF_POOL_START_MASK;
937 			val &= ~MSS_BUF_POOL_STOP_MASK;
938 		}
939 
940 		mvpp2_cm3_write(port->priv, MSS_BUF_POOL_REG(pool->id), val);
941 	}
942 
943 	/* Notify Firmware that Flow control config space ready for update */
944 	val = mvpp2_cm3_read(port->priv, MSS_FC_COM_REG);
945 	val |= FLOW_CONTROL_UPDATE_COMMAND_BIT;
946 	val |= cm3_state;
947 	mvpp2_cm3_write(port->priv, MSS_FC_COM_REG, val);
948 
949 	spin_unlock_irqrestore(&port->priv->mss_spinlock, flags);
950 }
951 
952 /* disable/enable flow control for BM pool on all ports */
mvpp2_bm_pool_update_priv_fc(struct mvpp2 * priv,bool en)953 static void mvpp2_bm_pool_update_priv_fc(struct mvpp2 *priv, bool en)
954 {
955 	struct mvpp2_port *port;
956 	int i, j;
957 
958 	for (i = 0; i < priv->port_count; i++) {
959 		port = priv->port_list[i];
960 		if (port->priv->percpu_pools) {
961 			for (j = 0; j < port->nrxqs; j++)
962 				mvpp2_bm_pool_update_fc(port, &port->priv->bm_pools[j],
963 							port->tx_fc & en);
964 		} else {
965 			mvpp2_bm_pool_update_fc(port, port->pool_long, port->tx_fc & en);
966 			mvpp2_bm_pool_update_fc(port, port->pool_short, port->tx_fc & en);
967 		}
968 	}
969 }
970 
mvpp2_enable_global_fc(struct mvpp2 * priv)971 static int mvpp2_enable_global_fc(struct mvpp2 *priv)
972 {
973 	int val, timeout = 0;
974 
975 	/* Enable global flow control. In this stage global
976 	 * flow control enabled, but still disabled per port.
977 	 */
978 	val = mvpp2_cm3_read(priv, MSS_FC_COM_REG);
979 	val |= FLOW_CONTROL_ENABLE_BIT;
980 	mvpp2_cm3_write(priv, MSS_FC_COM_REG, val);
981 
982 	/* Check if Firmware running and disable FC if not*/
983 	val |= FLOW_CONTROL_UPDATE_COMMAND_BIT;
984 	mvpp2_cm3_write(priv, MSS_FC_COM_REG, val);
985 
986 	while (timeout < MSS_FC_MAX_TIMEOUT) {
987 		val = mvpp2_cm3_read(priv, MSS_FC_COM_REG);
988 
989 		if (!(val & FLOW_CONTROL_UPDATE_COMMAND_BIT))
990 			return 0;
991 		usleep_range(10, 20);
992 		timeout++;
993 	}
994 
995 	priv->global_tx_fc = false;
996 	return -EOPNOTSUPP;
997 }
998 
999 /* Release buffer to BM */
mvpp2_bm_pool_put(struct mvpp2_port * port,int pool,dma_addr_t buf_dma_addr,phys_addr_t buf_phys_addr)1000 static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
1001 				     dma_addr_t buf_dma_addr,
1002 				     phys_addr_t buf_phys_addr)
1003 {
1004 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
1005 	unsigned long flags = 0;
1006 
1007 	if (test_bit(thread, &port->priv->lock_map))
1008 		spin_lock_irqsave(&port->bm_lock[thread], flags);
1009 
1010 	if (port->priv->hw_version >= MVPP22) {
1011 		u32 val = 0;
1012 
1013 		if (sizeof(dma_addr_t) == 8)
1014 			val |= upper_32_bits(buf_dma_addr) &
1015 				MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
1016 
1017 		if (sizeof(phys_addr_t) == 8)
1018 			val |= (upper_32_bits(buf_phys_addr)
1019 				<< MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
1020 				MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
1021 
1022 		mvpp2_thread_write_relaxed(port->priv, thread,
1023 					   MVPP22_BM_ADDR_HIGH_RLS_REG, val);
1024 	}
1025 
1026 	/* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
1027 	 * returned in the "cookie" field of the RX
1028 	 * descriptor. Instead of storing the virtual address, we
1029 	 * store the physical address
1030 	 */
1031 	mvpp2_thread_write_relaxed(port->priv, thread,
1032 				   MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
1033 	mvpp2_thread_write_relaxed(port->priv, thread,
1034 				   MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
1035 
1036 	if (test_bit(thread, &port->priv->lock_map))
1037 		spin_unlock_irqrestore(&port->bm_lock[thread], flags);
1038 
1039 	put_cpu();
1040 }
1041 
1042 /* Allocate buffers for the pool */
mvpp2_bm_bufs_add(struct mvpp2_port * port,struct mvpp2_bm_pool * bm_pool,int buf_num)1043 static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
1044 			     struct mvpp2_bm_pool *bm_pool, int buf_num)
1045 {
1046 	int i, buf_size, total_size;
1047 	dma_addr_t dma_addr;
1048 	phys_addr_t phys_addr;
1049 	struct page_pool *pp = NULL;
1050 	void *buf;
1051 
1052 	if (port->priv->percpu_pools &&
1053 	    bm_pool->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
1054 		netdev_err(port->dev,
1055 			   "attempted to use jumbo frames with per-cpu pools");
1056 		return 0;
1057 	}
1058 
1059 	buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
1060 	total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
1061 
1062 	if (buf_num < 0 ||
1063 	    (buf_num + bm_pool->buf_num > bm_pool->size)) {
1064 		netdev_err(port->dev,
1065 			   "cannot allocate %d buffers for pool %d\n",
1066 			   buf_num, bm_pool->id);
1067 		return 0;
1068 	}
1069 
1070 	if (port->priv->percpu_pools)
1071 		pp = port->priv->page_pool[bm_pool->id];
1072 	for (i = 0; i < buf_num; i++) {
1073 		buf = mvpp2_buf_alloc(port, bm_pool, pp, &dma_addr,
1074 				      &phys_addr, GFP_KERNEL);
1075 		if (!buf)
1076 			break;
1077 
1078 		mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
1079 				  phys_addr);
1080 	}
1081 
1082 	/* Update BM driver with number of buffers added to pool */
1083 	bm_pool->buf_num += i;
1084 
1085 	netdev_dbg(port->dev,
1086 		   "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
1087 		   bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
1088 
1089 	netdev_dbg(port->dev,
1090 		   "pool %d: %d of %d buffers added\n",
1091 		   bm_pool->id, i, buf_num);
1092 	return i;
1093 }
1094 
1095 /* Notify the driver that BM pool is being used as specific type and return the
1096  * pool pointer on success
1097  */
1098 static struct mvpp2_bm_pool *
mvpp2_bm_pool_use(struct mvpp2_port * port,unsigned pool,int pkt_size)1099 mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
1100 {
1101 	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
1102 	int num;
1103 
1104 	if ((port->priv->percpu_pools && pool > mvpp2_get_nrxqs(port->priv) * 2) ||
1105 	    (!port->priv->percpu_pools && pool >= MVPP2_BM_POOLS_NUM)) {
1106 		netdev_err(port->dev, "Invalid pool %d\n", pool);
1107 		return NULL;
1108 	}
1109 
1110 	/* Allocate buffers in case BM pool is used as long pool, but packet
1111 	 * size doesn't match MTU or BM pool hasn't being used yet
1112 	 */
1113 	if (new_pool->pkt_size == 0) {
1114 		int pkts_num;
1115 
1116 		/* Set default buffer number or free all the buffers in case
1117 		 * the pool is not empty
1118 		 */
1119 		pkts_num = new_pool->buf_num;
1120 		if (pkts_num == 0) {
1121 			if (port->priv->percpu_pools) {
1122 				if (pool < port->nrxqs)
1123 					pkts_num = mvpp2_pools[MVPP2_BM_SHORT].buf_num;
1124 				else
1125 					pkts_num = mvpp2_pools[MVPP2_BM_LONG].buf_num;
1126 			} else {
1127 				pkts_num = mvpp2_pools[pool].buf_num;
1128 			}
1129 		} else {
1130 			mvpp2_bm_bufs_free(port->dev->dev.parent,
1131 					   port->priv, new_pool, pkts_num);
1132 		}
1133 
1134 		new_pool->pkt_size = pkt_size;
1135 		new_pool->frag_size =
1136 			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
1137 			MVPP2_SKB_SHINFO_SIZE;
1138 
1139 		/* Allocate buffers for this pool */
1140 		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
1141 		if (num != pkts_num) {
1142 			WARN(1, "pool %d: %d of %d allocated\n",
1143 			     new_pool->id, num, pkts_num);
1144 			return NULL;
1145 		}
1146 	}
1147 
1148 	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
1149 				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
1150 
1151 	return new_pool;
1152 }
1153 
1154 static struct mvpp2_bm_pool *
mvpp2_bm_pool_use_percpu(struct mvpp2_port * port,int type,unsigned int pool,int pkt_size)1155 mvpp2_bm_pool_use_percpu(struct mvpp2_port *port, int type,
1156 			 unsigned int pool, int pkt_size)
1157 {
1158 	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
1159 	int num;
1160 
1161 	if (pool > port->nrxqs * 2) {
1162 		netdev_err(port->dev, "Invalid pool %d\n", pool);
1163 		return NULL;
1164 	}
1165 
1166 	/* Allocate buffers in case BM pool is used as long pool, but packet
1167 	 * size doesn't match MTU or BM pool hasn't being used yet
1168 	 */
1169 	if (new_pool->pkt_size == 0) {
1170 		int pkts_num;
1171 
1172 		/* Set default buffer number or free all the buffers in case
1173 		 * the pool is not empty
1174 		 */
1175 		pkts_num = new_pool->buf_num;
1176 		if (pkts_num == 0)
1177 			pkts_num = mvpp2_pools[type].buf_num;
1178 		else
1179 			mvpp2_bm_bufs_free(port->dev->dev.parent,
1180 					   port->priv, new_pool, pkts_num);
1181 
1182 		new_pool->pkt_size = pkt_size;
1183 		new_pool->frag_size =
1184 			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
1185 			MVPP2_SKB_SHINFO_SIZE;
1186 
1187 		/* Allocate buffers for this pool */
1188 		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
1189 		if (num != pkts_num) {
1190 			WARN(1, "pool %d: %d of %d allocated\n",
1191 			     new_pool->id, num, pkts_num);
1192 			return NULL;
1193 		}
1194 	}
1195 
1196 	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
1197 				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
1198 
1199 	return new_pool;
1200 }
1201 
1202 /* Initialize pools for swf, shared buffers variant */
mvpp2_swf_bm_pool_init_shared(struct mvpp2_port * port)1203 static int mvpp2_swf_bm_pool_init_shared(struct mvpp2_port *port)
1204 {
1205 	enum mvpp2_bm_pool_log_num long_log_pool, short_log_pool;
1206 	int rxq;
1207 
1208 	/* If port pkt_size is higher than 1518B:
1209 	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
1210 	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
1211 	 */
1212 	if (port->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
1213 		long_log_pool = MVPP2_BM_JUMBO;
1214 		short_log_pool = MVPP2_BM_LONG;
1215 	} else {
1216 		long_log_pool = MVPP2_BM_LONG;
1217 		short_log_pool = MVPP2_BM_SHORT;
1218 	}
1219 
1220 	if (!port->pool_long) {
1221 		port->pool_long =
1222 			mvpp2_bm_pool_use(port, long_log_pool,
1223 					  mvpp2_pools[long_log_pool].pkt_size);
1224 		if (!port->pool_long)
1225 			return -ENOMEM;
1226 
1227 		port->pool_long->port_map |= BIT(port->id);
1228 
1229 		for (rxq = 0; rxq < port->nrxqs; rxq++)
1230 			mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
1231 	}
1232 
1233 	if (!port->pool_short) {
1234 		port->pool_short =
1235 			mvpp2_bm_pool_use(port, short_log_pool,
1236 					  mvpp2_pools[short_log_pool].pkt_size);
1237 		if (!port->pool_short)
1238 			return -ENOMEM;
1239 
1240 		port->pool_short->port_map |= BIT(port->id);
1241 
1242 		for (rxq = 0; rxq < port->nrxqs; rxq++)
1243 			mvpp2_rxq_short_pool_set(port, rxq,
1244 						 port->pool_short->id);
1245 	}
1246 
1247 	return 0;
1248 }
1249 
1250 /* Initialize pools for swf, percpu buffers variant */
mvpp2_swf_bm_pool_init_percpu(struct mvpp2_port * port)1251 static int mvpp2_swf_bm_pool_init_percpu(struct mvpp2_port *port)
1252 {
1253 	struct mvpp2_bm_pool *bm_pool;
1254 	int i;
1255 
1256 	for (i = 0; i < port->nrxqs; i++) {
1257 		bm_pool = mvpp2_bm_pool_use_percpu(port, MVPP2_BM_SHORT, i,
1258 						   mvpp2_pools[MVPP2_BM_SHORT].pkt_size);
1259 		if (!bm_pool)
1260 			return -ENOMEM;
1261 
1262 		bm_pool->port_map |= BIT(port->id);
1263 		mvpp2_rxq_short_pool_set(port, i, bm_pool->id);
1264 	}
1265 
1266 	for (i = 0; i < port->nrxqs; i++) {
1267 		bm_pool = mvpp2_bm_pool_use_percpu(port, MVPP2_BM_LONG, i + port->nrxqs,
1268 						   mvpp2_pools[MVPP2_BM_LONG].pkt_size);
1269 		if (!bm_pool)
1270 			return -ENOMEM;
1271 
1272 		bm_pool->port_map |= BIT(port->id);
1273 		mvpp2_rxq_long_pool_set(port, i, bm_pool->id);
1274 	}
1275 
1276 	port->pool_long = NULL;
1277 	port->pool_short = NULL;
1278 
1279 	return 0;
1280 }
1281 
mvpp2_swf_bm_pool_init(struct mvpp2_port * port)1282 static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
1283 {
1284 	if (port->priv->percpu_pools)
1285 		return mvpp2_swf_bm_pool_init_percpu(port);
1286 	else
1287 		return mvpp2_swf_bm_pool_init_shared(port);
1288 }
1289 
mvpp2_set_hw_csum(struct mvpp2_port * port,enum mvpp2_bm_pool_log_num new_long_pool)1290 static void mvpp2_set_hw_csum(struct mvpp2_port *port,
1291 			      enum mvpp2_bm_pool_log_num new_long_pool)
1292 {
1293 	const netdev_features_t csums = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1294 
1295 	/* Update L4 checksum when jumbo enable/disable on port.
1296 	 * Only port 0 supports hardware checksum offload due to
1297 	 * the Tx FIFO size limitation.
1298 	 * Also, don't set NETIF_F_HW_CSUM because L3_offset in TX descriptor
1299 	 * has 7 bits, so the maximum L3 offset is 128.
1300 	 */
1301 	if (new_long_pool == MVPP2_BM_JUMBO && port->id != 0) {
1302 		port->dev->features &= ~csums;
1303 		port->dev->hw_features &= ~csums;
1304 	} else {
1305 		port->dev->features |= csums;
1306 		port->dev->hw_features |= csums;
1307 	}
1308 }
1309 
mvpp2_bm_update_mtu(struct net_device * dev,int mtu)1310 static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
1311 {
1312 	struct mvpp2_port *port = netdev_priv(dev);
1313 	enum mvpp2_bm_pool_log_num new_long_pool;
1314 	int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
1315 
1316 	if (port->priv->percpu_pools)
1317 		goto out_set;
1318 
1319 	/* If port MTU is higher than 1518B:
1320 	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
1321 	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
1322 	 */
1323 	if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
1324 		new_long_pool = MVPP2_BM_JUMBO;
1325 	else
1326 		new_long_pool = MVPP2_BM_LONG;
1327 
1328 	if (new_long_pool != port->pool_long->id) {
1329 		if (port->tx_fc) {
1330 			if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
1331 				mvpp2_bm_pool_update_fc(port,
1332 							port->pool_short,
1333 							false);
1334 			else
1335 				mvpp2_bm_pool_update_fc(port, port->pool_long,
1336 							false);
1337 		}
1338 
1339 		/* Remove port from old short & long pool */
1340 		port->pool_long = mvpp2_bm_pool_use(port, port->pool_long->id,
1341 						    port->pool_long->pkt_size);
1342 		port->pool_long->port_map &= ~BIT(port->id);
1343 		port->pool_long = NULL;
1344 
1345 		port->pool_short = mvpp2_bm_pool_use(port, port->pool_short->id,
1346 						     port->pool_short->pkt_size);
1347 		port->pool_short->port_map &= ~BIT(port->id);
1348 		port->pool_short = NULL;
1349 
1350 		port->pkt_size =  pkt_size;
1351 
1352 		/* Add port to new short & long pool */
1353 		mvpp2_swf_bm_pool_init(port);
1354 
1355 		mvpp2_set_hw_csum(port, new_long_pool);
1356 
1357 		if (port->tx_fc) {
1358 			if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
1359 				mvpp2_bm_pool_update_fc(port, port->pool_long,
1360 							true);
1361 			else
1362 				mvpp2_bm_pool_update_fc(port, port->pool_short,
1363 							true);
1364 		}
1365 
1366 		/* Update L4 checksum when jumbo enable/disable on port */
1367 		if (new_long_pool == MVPP2_BM_JUMBO && port->id != 0) {
1368 			dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
1369 			dev->hw_features &= ~(NETIF_F_IP_CSUM |
1370 					      NETIF_F_IPV6_CSUM);
1371 		} else {
1372 			dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1373 			dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1374 		}
1375 	}
1376 
1377 out_set:
1378 	WRITE_ONCE(dev->mtu, mtu);
1379 	dev->wanted_features = dev->features;
1380 
1381 	netdev_update_features(dev);
1382 	return 0;
1383 }
1384 
mvpp2_interrupts_enable(struct mvpp2_port * port)1385 static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
1386 {
1387 	int i, sw_thread_mask = 0;
1388 
1389 	for (i = 0; i < port->nqvecs; i++)
1390 		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
1391 
1392 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1393 		    MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
1394 }
1395 
mvpp2_interrupts_disable(struct mvpp2_port * port)1396 static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
1397 {
1398 	int i, sw_thread_mask = 0;
1399 
1400 	for (i = 0; i < port->nqvecs; i++)
1401 		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
1402 
1403 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1404 		    MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
1405 }
1406 
mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector * qvec)1407 static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
1408 {
1409 	struct mvpp2_port *port = qvec->port;
1410 
1411 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1412 		    MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
1413 }
1414 
mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector * qvec)1415 static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
1416 {
1417 	struct mvpp2_port *port = qvec->port;
1418 
1419 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1420 		    MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
1421 }
1422 
1423 /* Mask the current thread's Rx/Tx interrupts
1424  * Called by on_each_cpu(), guaranteed to run with migration disabled,
1425  * using smp_processor_id() is OK.
1426  */
mvpp2_interrupts_mask(void * arg)1427 static void mvpp2_interrupts_mask(void *arg)
1428 {
1429 	struct mvpp2_port *port = arg;
1430 	int cpu = smp_processor_id();
1431 	u32 thread;
1432 
1433 	/* If the thread isn't used, don't do anything */
1434 	if (cpu > port->priv->nthreads)
1435 		return;
1436 
1437 	thread = mvpp2_cpu_to_thread(port->priv, cpu);
1438 
1439 	mvpp2_thread_write(port->priv, thread,
1440 			   MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
1441 	mvpp2_thread_write(port->priv, thread,
1442 			   MVPP2_ISR_RX_ERR_CAUSE_REG(port->id), 0);
1443 }
1444 
1445 /* Unmask the current thread's Rx/Tx interrupts.
1446  * Called by on_each_cpu(), guaranteed to run with migration disabled,
1447  * using smp_processor_id() is OK.
1448  */
mvpp2_interrupts_unmask(void * arg)1449 static void mvpp2_interrupts_unmask(void *arg)
1450 {
1451 	struct mvpp2_port *port = arg;
1452 	int cpu = smp_processor_id();
1453 	u32 val, thread;
1454 
1455 	/* If the thread isn't used, don't do anything */
1456 	if (cpu >= port->priv->nthreads)
1457 		return;
1458 
1459 	thread = mvpp2_cpu_to_thread(port->priv, cpu);
1460 
1461 	val = MVPP2_CAUSE_MISC_SUM_MASK |
1462 		MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
1463 	if (port->has_tx_irqs)
1464 		val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
1465 
1466 	mvpp2_thread_write(port->priv, thread,
1467 			   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
1468 	mvpp2_thread_write(port->priv, thread,
1469 			   MVPP2_ISR_RX_ERR_CAUSE_REG(port->id),
1470 			   MVPP2_ISR_RX_ERR_CAUSE_NONOCC_MASK);
1471 }
1472 
1473 static void
mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port * port,bool mask)1474 mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
1475 {
1476 	u32 val;
1477 	int i;
1478 
1479 	if (port->priv->hw_version == MVPP21)
1480 		return;
1481 
1482 	if (mask)
1483 		val = 0;
1484 	else
1485 		val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(MVPP22);
1486 
1487 	for (i = 0; i < port->nqvecs; i++) {
1488 		struct mvpp2_queue_vector *v = port->qvecs + i;
1489 
1490 		if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
1491 			continue;
1492 
1493 		mvpp2_thread_write(port->priv, v->sw_thread_id,
1494 				   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
1495 		mvpp2_thread_write(port->priv, v->sw_thread_id,
1496 				   MVPP2_ISR_RX_ERR_CAUSE_REG(port->id),
1497 				   MVPP2_ISR_RX_ERR_CAUSE_NONOCC_MASK);
1498 	}
1499 }
1500 
1501 /* Only GOP port 0 has an XLG MAC */
mvpp2_port_supports_xlg(struct mvpp2_port * port)1502 static bool mvpp2_port_supports_xlg(struct mvpp2_port *port)
1503 {
1504 	return port->gop_id == 0;
1505 }
1506 
mvpp2_port_supports_rgmii(struct mvpp2_port * port)1507 static bool mvpp2_port_supports_rgmii(struct mvpp2_port *port)
1508 {
1509 	return !(port->priv->hw_version >= MVPP22 && port->gop_id == 0);
1510 }
1511 
1512 /* Port configuration routines */
mvpp2_is_xlg(phy_interface_t interface)1513 static bool mvpp2_is_xlg(phy_interface_t interface)
1514 {
1515 	return interface == PHY_INTERFACE_MODE_10GBASER ||
1516 	       interface == PHY_INTERFACE_MODE_5GBASER ||
1517 	       interface == PHY_INTERFACE_MODE_XAUI;
1518 }
1519 
mvpp2_modify(void __iomem * ptr,u32 mask,u32 set)1520 static void mvpp2_modify(void __iomem *ptr, u32 mask, u32 set)
1521 {
1522 	u32 old, val;
1523 
1524 	old = val = readl(ptr);
1525 	val &= ~mask;
1526 	val |= set;
1527 	if (old != val)
1528 		writel(val, ptr);
1529 }
1530 
mvpp22_gop_init_rgmii(struct mvpp2_port * port)1531 static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
1532 {
1533 	struct mvpp2 *priv = port->priv;
1534 	u32 val;
1535 
1536 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1537 	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
1538 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1539 
1540 	regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1541 	if (port->gop_id == 2) {
1542 		val |= GENCONF_CTRL0_PORT2_RGMII;
1543 	} else if (port->gop_id == 3) {
1544 		val |= GENCONF_CTRL0_PORT3_RGMII_MII;
1545 
1546 		/* According to the specification, GENCONF_CTRL0_PORT3_RGMII
1547 		 * should be set to 1 for RGMII and 0 for MII. However, tests
1548 		 * show that it is the other way around. This is also what
1549 		 * U-Boot does for mvpp2, so it is assumed to be correct.
1550 		 */
1551 		if (port->phy_interface == PHY_INTERFACE_MODE_MII)
1552 			val |= GENCONF_CTRL0_PORT3_RGMII;
1553 		else
1554 			val &= ~GENCONF_CTRL0_PORT3_RGMII;
1555 	}
1556 	regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1557 }
1558 
mvpp22_gop_init_sgmii(struct mvpp2_port * port)1559 static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
1560 {
1561 	struct mvpp2 *priv = port->priv;
1562 	u32 val;
1563 
1564 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1565 	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
1566 	       GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
1567 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1568 
1569 	if (port->gop_id > 1) {
1570 		regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1571 		if (port->gop_id == 2)
1572 			val &= ~GENCONF_CTRL0_PORT2_RGMII;
1573 		else if (port->gop_id == 3)
1574 			val &= ~GENCONF_CTRL0_PORT3_RGMII_MII;
1575 		regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1576 	}
1577 }
1578 
mvpp22_gop_init_10gkr(struct mvpp2_port * port)1579 static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
1580 {
1581 	struct mvpp2 *priv = port->priv;
1582 	void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1583 	void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1584 	u32 val;
1585 
1586 	val = readl(xpcs + MVPP22_XPCS_CFG0);
1587 	val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
1588 		 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
1589 	val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
1590 	writel(val, xpcs + MVPP22_XPCS_CFG0);
1591 
1592 	val = readl(mpcs + MVPP22_MPCS_CTRL);
1593 	val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
1594 	writel(val, mpcs + MVPP22_MPCS_CTRL);
1595 
1596 	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1597 	val &= ~MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7);
1598 	val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
1599 	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1600 }
1601 
mvpp22_gop_fca_enable_periodic(struct mvpp2_port * port,bool en)1602 static void mvpp22_gop_fca_enable_periodic(struct mvpp2_port *port, bool en)
1603 {
1604 	struct mvpp2 *priv = port->priv;
1605 	void __iomem *fca = priv->iface_base + MVPP22_FCA_BASE(port->gop_id);
1606 	u32 val;
1607 
1608 	val = readl(fca + MVPP22_FCA_CONTROL_REG);
1609 	val &= ~MVPP22_FCA_ENABLE_PERIODIC;
1610 	if (en)
1611 		val |= MVPP22_FCA_ENABLE_PERIODIC;
1612 	writel(val, fca + MVPP22_FCA_CONTROL_REG);
1613 }
1614 
mvpp22_gop_fca_set_timer(struct mvpp2_port * port,u32 timer)1615 static void mvpp22_gop_fca_set_timer(struct mvpp2_port *port, u32 timer)
1616 {
1617 	struct mvpp2 *priv = port->priv;
1618 	void __iomem *fca = priv->iface_base + MVPP22_FCA_BASE(port->gop_id);
1619 	u32 lsb, msb;
1620 
1621 	lsb = timer & MVPP22_FCA_REG_MASK;
1622 	msb = timer >> MVPP22_FCA_REG_SIZE;
1623 
1624 	writel(lsb, fca + MVPP22_PERIODIC_COUNTER_LSB_REG);
1625 	writel(msb, fca + MVPP22_PERIODIC_COUNTER_MSB_REG);
1626 }
1627 
1628 /* Set Flow Control timer x100 faster than pause quanta to ensure that link
1629  * partner won't send traffic if port is in XOFF mode.
1630  */
mvpp22_gop_fca_set_periodic_timer(struct mvpp2_port * port)1631 static void mvpp22_gop_fca_set_periodic_timer(struct mvpp2_port *port)
1632 {
1633 	u32 timer;
1634 
1635 	timer = (port->priv->tclk / (USEC_PER_SEC * FC_CLK_DIVIDER))
1636 		* FC_QUANTA;
1637 
1638 	mvpp22_gop_fca_enable_periodic(port, false);
1639 
1640 	mvpp22_gop_fca_set_timer(port, timer);
1641 
1642 	mvpp22_gop_fca_enable_periodic(port, true);
1643 }
1644 
mvpp22_gop_init(struct mvpp2_port * port,phy_interface_t interface)1645 static int mvpp22_gop_init(struct mvpp2_port *port, phy_interface_t interface)
1646 {
1647 	struct mvpp2 *priv = port->priv;
1648 	u32 val;
1649 
1650 	if (!priv->sysctrl_base)
1651 		return 0;
1652 
1653 	switch (interface) {
1654 	case PHY_INTERFACE_MODE_MII:
1655 	case PHY_INTERFACE_MODE_RGMII:
1656 	case PHY_INTERFACE_MODE_RGMII_ID:
1657 	case PHY_INTERFACE_MODE_RGMII_RXID:
1658 	case PHY_INTERFACE_MODE_RGMII_TXID:
1659 		if (!mvpp2_port_supports_rgmii(port))
1660 			goto invalid_conf;
1661 		mvpp22_gop_init_rgmii(port);
1662 		break;
1663 	case PHY_INTERFACE_MODE_SGMII:
1664 	case PHY_INTERFACE_MODE_1000BASEX:
1665 	case PHY_INTERFACE_MODE_2500BASEX:
1666 		mvpp22_gop_init_sgmii(port);
1667 		break;
1668 	case PHY_INTERFACE_MODE_5GBASER:
1669 	case PHY_INTERFACE_MODE_10GBASER:
1670 		if (!mvpp2_port_supports_xlg(port))
1671 			goto invalid_conf;
1672 		mvpp22_gop_init_10gkr(port);
1673 		break;
1674 	default:
1675 		goto unsupported_conf;
1676 	}
1677 
1678 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
1679 	val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
1680 	       GENCONF_PORT_CTRL1_EN(port->gop_id);
1681 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
1682 
1683 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1684 	val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
1685 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1686 
1687 	regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
1688 	val |= GENCONF_SOFT_RESET1_GOP;
1689 	regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
1690 
1691 	mvpp22_gop_fca_set_periodic_timer(port);
1692 
1693 unsupported_conf:
1694 	return 0;
1695 
1696 invalid_conf:
1697 	netdev_err(port->dev, "Invalid port configuration\n");
1698 	return -EINVAL;
1699 }
1700 
mvpp22_gop_unmask_irq(struct mvpp2_port * port)1701 static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
1702 {
1703 	u32 val;
1704 
1705 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1706 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1707 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1708 		/* Enable the GMAC link status irq for this port */
1709 		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1710 		val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1711 		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1712 	}
1713 
1714 	if (mvpp2_port_supports_xlg(port)) {
1715 		/* Enable the XLG/GIG irqs for this port */
1716 		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1717 		if (mvpp2_is_xlg(port->phy_interface))
1718 			val |= MVPP22_XLG_EXT_INT_MASK_XLG;
1719 		else
1720 			val |= MVPP22_XLG_EXT_INT_MASK_GIG;
1721 		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1722 	}
1723 }
1724 
mvpp22_gop_mask_irq(struct mvpp2_port * port)1725 static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
1726 {
1727 	u32 val;
1728 
1729 	if (mvpp2_port_supports_xlg(port)) {
1730 		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1731 		val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
1732 			 MVPP22_XLG_EXT_INT_MASK_GIG);
1733 		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1734 	}
1735 
1736 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1737 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1738 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1739 		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1740 		val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1741 		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1742 	}
1743 }
1744 
mvpp22_gop_setup_irq(struct mvpp2_port * port)1745 static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
1746 {
1747 	u32 val;
1748 
1749 	mvpp2_modify(port->base + MVPP22_GMAC_INT_SUM_MASK,
1750 		     MVPP22_GMAC_INT_SUM_MASK_PTP,
1751 		     MVPP22_GMAC_INT_SUM_MASK_PTP);
1752 
1753 	if (port->phylink ||
1754 	    phy_interface_mode_is_rgmii(port->phy_interface) ||
1755 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1756 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1757 		val = readl(port->base + MVPP22_GMAC_INT_MASK);
1758 		val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
1759 		writel(val, port->base + MVPP22_GMAC_INT_MASK);
1760 	}
1761 
1762 	if (mvpp2_port_supports_xlg(port)) {
1763 		val = readl(port->base + MVPP22_XLG_INT_MASK);
1764 		val |= MVPP22_XLG_INT_MASK_LINK;
1765 		writel(val, port->base + MVPP22_XLG_INT_MASK);
1766 
1767 		mvpp2_modify(port->base + MVPP22_XLG_EXT_INT_MASK,
1768 			     MVPP22_XLG_EXT_INT_MASK_PTP,
1769 			     MVPP22_XLG_EXT_INT_MASK_PTP);
1770 	}
1771 
1772 	mvpp22_gop_unmask_irq(port);
1773 }
1774 
1775 /* Sets the PHY mode of the COMPHY (which configures the serdes lanes).
1776  *
1777  * The PHY mode used by the PPv2 driver comes from the network subsystem, while
1778  * the one given to the COMPHY comes from the generic PHY subsystem. Hence they
1779  * differ.
1780  *
1781  * The COMPHY configures the serdes lanes regardless of the actual use of the
1782  * lanes by the physical layer. This is why configurations like
1783  * "PPv2 (2500BaseX) - COMPHY (2500SGMII)" are valid.
1784  */
mvpp22_comphy_init(struct mvpp2_port * port,phy_interface_t interface)1785 static int mvpp22_comphy_init(struct mvpp2_port *port,
1786 			      phy_interface_t interface)
1787 {
1788 	int ret;
1789 
1790 	if (!port->comphy)
1791 		return 0;
1792 
1793 	ret = phy_set_mode_ext(port->comphy, PHY_MODE_ETHERNET, interface);
1794 	if (ret)
1795 		return ret;
1796 
1797 	return phy_power_on(port->comphy);
1798 }
1799 
mvpp2_port_enable(struct mvpp2_port * port)1800 static void mvpp2_port_enable(struct mvpp2_port *port)
1801 {
1802 	u32 val;
1803 
1804 	if (mvpp2_port_supports_xlg(port) &&
1805 	    mvpp2_is_xlg(port->phy_interface)) {
1806 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1807 		val |= MVPP22_XLG_CTRL0_PORT_EN;
1808 		val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
1809 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1810 	} else {
1811 		val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1812 		val |= MVPP2_GMAC_PORT_EN_MASK;
1813 		val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
1814 		writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1815 	}
1816 }
1817 
mvpp2_port_disable(struct mvpp2_port * port)1818 static void mvpp2_port_disable(struct mvpp2_port *port)
1819 {
1820 	u32 val;
1821 
1822 	if (mvpp2_port_supports_xlg(port) &&
1823 	    mvpp2_is_xlg(port->phy_interface)) {
1824 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1825 		val &= ~MVPP22_XLG_CTRL0_PORT_EN;
1826 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1827 	}
1828 
1829 	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1830 	val &= ~(MVPP2_GMAC_PORT_EN_MASK);
1831 	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1832 }
1833 
1834 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
mvpp2_port_periodic_xon_disable(struct mvpp2_port * port)1835 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
1836 {
1837 	u32 val;
1838 
1839 	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
1840 		    ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
1841 	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1842 }
1843 
1844 /* Configure loopback port */
mvpp2_port_loopback_set(struct mvpp2_port * port,const struct phylink_link_state * state)1845 static void mvpp2_port_loopback_set(struct mvpp2_port *port,
1846 				    const struct phylink_link_state *state)
1847 {
1848 	u32 val;
1849 
1850 	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
1851 
1852 	if (state->speed == 1000)
1853 		val |= MVPP2_GMAC_GMII_LB_EN_MASK;
1854 	else
1855 		val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
1856 
1857 	if (phy_interface_mode_is_8023z(state->interface) ||
1858 	    state->interface == PHY_INTERFACE_MODE_SGMII)
1859 		val |= MVPP2_GMAC_PCS_LB_EN_MASK;
1860 	else
1861 		val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
1862 
1863 	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1864 }
1865 
1866 enum {
1867 	ETHTOOL_XDP_REDIRECT,
1868 	ETHTOOL_XDP_PASS,
1869 	ETHTOOL_XDP_DROP,
1870 	ETHTOOL_XDP_TX,
1871 	ETHTOOL_XDP_TX_ERR,
1872 	ETHTOOL_XDP_XMIT,
1873 	ETHTOOL_XDP_XMIT_ERR,
1874 };
1875 
1876 struct mvpp2_ethtool_counter {
1877 	unsigned int offset;
1878 	const char string[ETH_GSTRING_LEN];
1879 	bool reg_is_64b;
1880 };
1881 
mvpp2_read_count(struct mvpp2_port * port,const struct mvpp2_ethtool_counter * counter)1882 static u64 mvpp2_read_count(struct mvpp2_port *port,
1883 			    const struct mvpp2_ethtool_counter *counter)
1884 {
1885 	u64 val;
1886 
1887 	val = readl(port->stats_base + counter->offset);
1888 	if (counter->reg_is_64b)
1889 		val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
1890 
1891 	return val;
1892 }
1893 
1894 /* Some counters are accessed indirectly by first writing an index to
1895  * MVPP2_CTRS_IDX. The index can represent various resources depending on the
1896  * register we access, it can be a hit counter for some classification tables,
1897  * a counter specific to a rxq, a txq or a buffer pool.
1898  */
mvpp2_read_index(struct mvpp2 * priv,u32 index,u32 reg)1899 static u32 mvpp2_read_index(struct mvpp2 *priv, u32 index, u32 reg)
1900 {
1901 	mvpp2_write(priv, MVPP2_CTRS_IDX, index);
1902 	return mvpp2_read(priv, reg);
1903 }
1904 
1905 /* Due to the fact that software statistics and hardware statistics are, by
1906  * design, incremented at different moments in the chain of packet processing,
1907  * it is very likely that incoming packets could have been dropped after being
1908  * counted by hardware but before reaching software statistics (most probably
1909  * multicast packets), and in the opposite way, during transmission, FCS bytes
1910  * are added in between as well as TSO skb will be split and header bytes added.
1911  * Hence, statistics gathered from userspace with ifconfig (software) and
1912  * ethtool (hardware) cannot be compared.
1913  */
1914 static const struct mvpp2_ethtool_counter mvpp2_ethtool_mib_regs[] = {
1915 	{ MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
1916 	{ MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
1917 	{ MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
1918 	{ MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
1919 	{ MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
1920 	{ MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
1921 	{ MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
1922 	{ MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
1923 	{ MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
1924 	{ MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
1925 	{ MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
1926 	{ MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
1927 	{ MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
1928 	{ MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
1929 	{ MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
1930 	{ MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
1931 	{ MVPP2_MIB_FC_SENT, "fc_sent" },
1932 	{ MVPP2_MIB_FC_RCVD, "fc_received" },
1933 	{ MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
1934 	{ MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
1935 	{ MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
1936 	{ MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
1937 	{ MVPP2_MIB_JABBER_RCVD, "jabber_received" },
1938 	{ MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
1939 	{ MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
1940 	{ MVPP2_MIB_COLLISION, "collision" },
1941 	{ MVPP2_MIB_LATE_COLLISION, "late_collision" },
1942 };
1943 
1944 static const struct mvpp2_ethtool_counter mvpp2_ethtool_port_regs[] = {
1945 	{ MVPP2_OVERRUN_ETH_DROP, "rx_fifo_or_parser_overrun_drops" },
1946 	{ MVPP2_CLS_ETH_DROP, "rx_classifier_drops" },
1947 };
1948 
1949 static const struct mvpp2_ethtool_counter mvpp2_ethtool_txq_regs[] = {
1950 	{ MVPP2_TX_DESC_ENQ_CTR, "txq_%d_desc_enqueue" },
1951 	{ MVPP2_TX_DESC_ENQ_TO_DDR_CTR, "txq_%d_desc_enqueue_to_ddr" },
1952 	{ MVPP2_TX_BUFF_ENQ_TO_DDR_CTR, "txq_%d_buff_euqueue_to_ddr" },
1953 	{ MVPP2_TX_DESC_ENQ_HW_FWD_CTR, "txq_%d_desc_hardware_forwarded" },
1954 	{ MVPP2_TX_PKTS_DEQ_CTR, "txq_%d_packets_dequeued" },
1955 	{ MVPP2_TX_PKTS_FULL_QUEUE_DROP_CTR, "txq_%d_queue_full_drops" },
1956 	{ MVPP2_TX_PKTS_EARLY_DROP_CTR, "txq_%d_packets_early_drops" },
1957 	{ MVPP2_TX_PKTS_BM_DROP_CTR, "txq_%d_packets_bm_drops" },
1958 	{ MVPP2_TX_PKTS_BM_MC_DROP_CTR, "txq_%d_packets_rep_bm_drops" },
1959 };
1960 
1961 static const struct mvpp2_ethtool_counter mvpp2_ethtool_rxq_regs[] = {
1962 	{ MVPP2_RX_DESC_ENQ_CTR, "rxq_%d_desc_enqueue" },
1963 	{ MVPP2_RX_PKTS_FULL_QUEUE_DROP_CTR, "rxq_%d_queue_full_drops" },
1964 	{ MVPP2_RX_PKTS_EARLY_DROP_CTR, "rxq_%d_packets_early_drops" },
1965 	{ MVPP2_RX_PKTS_BM_DROP_CTR, "rxq_%d_packets_bm_drops" },
1966 };
1967 
1968 static const struct mvpp2_ethtool_counter mvpp2_ethtool_xdp[] = {
1969 	{ ETHTOOL_XDP_REDIRECT, "rx_xdp_redirect", },
1970 	{ ETHTOOL_XDP_PASS, "rx_xdp_pass", },
1971 	{ ETHTOOL_XDP_DROP, "rx_xdp_drop", },
1972 	{ ETHTOOL_XDP_TX, "rx_xdp_tx", },
1973 	{ ETHTOOL_XDP_TX_ERR, "rx_xdp_tx_errors", },
1974 	{ ETHTOOL_XDP_XMIT, "tx_xdp_xmit", },
1975 	{ ETHTOOL_XDP_XMIT_ERR, "tx_xdp_xmit_errors", },
1976 };
1977 
1978 #define MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs)	(ARRAY_SIZE(mvpp2_ethtool_mib_regs) + \
1979 						 ARRAY_SIZE(mvpp2_ethtool_port_regs) + \
1980 						 (ARRAY_SIZE(mvpp2_ethtool_txq_regs) * (ntxqs)) + \
1981 						 (ARRAY_SIZE(mvpp2_ethtool_rxq_regs) * (nrxqs)) + \
1982 						 ARRAY_SIZE(mvpp2_ethtool_xdp))
1983 
mvpp2_ethtool_get_strings(struct net_device * netdev,u32 sset,u8 * data)1984 static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
1985 				      u8 *data)
1986 {
1987 	struct mvpp2_port *port = netdev_priv(netdev);
1988 	const char *str;
1989 	int i, q;
1990 
1991 	if (sset != ETH_SS_STATS)
1992 		return;
1993 
1994 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++)
1995 		ethtool_puts(&data, mvpp2_ethtool_mib_regs[i].string);
1996 
1997 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++)
1998 		ethtool_puts(&data, mvpp2_ethtool_port_regs[i].string);
1999 
2000 	for (q = 0; q < port->ntxqs; q++)
2001 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++) {
2002 			str = mvpp2_ethtool_txq_regs[i].string;
2003 			ethtool_sprintf(&data, str, q);
2004 		}
2005 
2006 	for (q = 0; q < port->nrxqs; q++)
2007 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++) {
2008 			str = mvpp2_ethtool_rxq_regs[i].string;
2009 			ethtool_sprintf(&data, str, q);
2010 		}
2011 
2012 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_xdp); i++)
2013 		ethtool_puts(&data, mvpp2_ethtool_xdp[i].string);
2014 }
2015 
2016 static void
mvpp2_get_xdp_stats(struct mvpp2_port * port,struct mvpp2_pcpu_stats * xdp_stats)2017 mvpp2_get_xdp_stats(struct mvpp2_port *port, struct mvpp2_pcpu_stats *xdp_stats)
2018 {
2019 	unsigned int start;
2020 	unsigned int cpu;
2021 
2022 	/* Gather XDP Statistics */
2023 	for_each_possible_cpu(cpu) {
2024 		struct mvpp2_pcpu_stats *cpu_stats;
2025 		u64	xdp_redirect;
2026 		u64	xdp_pass;
2027 		u64	xdp_drop;
2028 		u64	xdp_xmit;
2029 		u64	xdp_xmit_err;
2030 		u64	xdp_tx;
2031 		u64	xdp_tx_err;
2032 
2033 		cpu_stats = per_cpu_ptr(port->stats, cpu);
2034 		do {
2035 			start = u64_stats_fetch_begin(&cpu_stats->syncp);
2036 			xdp_redirect = cpu_stats->xdp_redirect;
2037 			xdp_pass   = cpu_stats->xdp_pass;
2038 			xdp_drop = cpu_stats->xdp_drop;
2039 			xdp_xmit   = cpu_stats->xdp_xmit;
2040 			xdp_xmit_err   = cpu_stats->xdp_xmit_err;
2041 			xdp_tx   = cpu_stats->xdp_tx;
2042 			xdp_tx_err   = cpu_stats->xdp_tx_err;
2043 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
2044 
2045 		xdp_stats->xdp_redirect += xdp_redirect;
2046 		xdp_stats->xdp_pass   += xdp_pass;
2047 		xdp_stats->xdp_drop += xdp_drop;
2048 		xdp_stats->xdp_xmit   += xdp_xmit;
2049 		xdp_stats->xdp_xmit_err   += xdp_xmit_err;
2050 		xdp_stats->xdp_tx   += xdp_tx;
2051 		xdp_stats->xdp_tx_err   += xdp_tx_err;
2052 	}
2053 }
2054 
mvpp2_read_stats(struct mvpp2_port * port)2055 static void mvpp2_read_stats(struct mvpp2_port *port)
2056 {
2057 	struct mvpp2_pcpu_stats xdp_stats = {};
2058 	const struct mvpp2_ethtool_counter *s;
2059 	u64 *pstats;
2060 	int i, q;
2061 
2062 	pstats = port->ethtool_stats;
2063 
2064 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++)
2065 		*pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_mib_regs[i]);
2066 
2067 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++)
2068 		*pstats++ += mvpp2_read(port->priv,
2069 					mvpp2_ethtool_port_regs[i].offset +
2070 					4 * port->id);
2071 
2072 	for (q = 0; q < port->ntxqs; q++)
2073 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++)
2074 			*pstats++ += mvpp2_read_index(port->priv,
2075 						      MVPP22_CTRS_TX_CTR(port->id, q),
2076 						      mvpp2_ethtool_txq_regs[i].offset);
2077 
2078 	/* Rxqs are numbered from 0 from the user standpoint, but not from the
2079 	 * driver's. We need to add the  port->first_rxq offset.
2080 	 */
2081 	for (q = 0; q < port->nrxqs; q++)
2082 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++)
2083 			*pstats++ += mvpp2_read_index(port->priv,
2084 						      port->first_rxq + q,
2085 						      mvpp2_ethtool_rxq_regs[i].offset);
2086 
2087 	/* Gather XDP Statistics */
2088 	mvpp2_get_xdp_stats(port, &xdp_stats);
2089 
2090 	for (i = 0, s = mvpp2_ethtool_xdp;
2091 		 s < mvpp2_ethtool_xdp + ARRAY_SIZE(mvpp2_ethtool_xdp);
2092 	     s++, i++) {
2093 		switch (s->offset) {
2094 		case ETHTOOL_XDP_REDIRECT:
2095 			*pstats++ = xdp_stats.xdp_redirect;
2096 			break;
2097 		case ETHTOOL_XDP_PASS:
2098 			*pstats++ = xdp_stats.xdp_pass;
2099 			break;
2100 		case ETHTOOL_XDP_DROP:
2101 			*pstats++ = xdp_stats.xdp_drop;
2102 			break;
2103 		case ETHTOOL_XDP_TX:
2104 			*pstats++ = xdp_stats.xdp_tx;
2105 			break;
2106 		case ETHTOOL_XDP_TX_ERR:
2107 			*pstats++ = xdp_stats.xdp_tx_err;
2108 			break;
2109 		case ETHTOOL_XDP_XMIT:
2110 			*pstats++ = xdp_stats.xdp_xmit;
2111 			break;
2112 		case ETHTOOL_XDP_XMIT_ERR:
2113 			*pstats++ = xdp_stats.xdp_xmit_err;
2114 			break;
2115 		}
2116 	}
2117 }
2118 
mvpp2_gather_hw_statistics(struct work_struct * work)2119 static void mvpp2_gather_hw_statistics(struct work_struct *work)
2120 {
2121 	struct delayed_work *del_work = to_delayed_work(work);
2122 	struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
2123 					       stats_work);
2124 
2125 	mutex_lock(&port->gather_stats_lock);
2126 
2127 	mvpp2_read_stats(port);
2128 
2129 	/* No need to read again the counters right after this function if it
2130 	 * was called asynchronously by the user (ie. use of ethtool).
2131 	 */
2132 	cancel_delayed_work(&port->stats_work);
2133 	queue_delayed_work(port->priv->stats_queue, &port->stats_work,
2134 			   MVPP2_MIB_COUNTERS_STATS_DELAY);
2135 
2136 	mutex_unlock(&port->gather_stats_lock);
2137 }
2138 
mvpp2_ethtool_get_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)2139 static void mvpp2_ethtool_get_stats(struct net_device *dev,
2140 				    struct ethtool_stats *stats, u64 *data)
2141 {
2142 	struct mvpp2_port *port = netdev_priv(dev);
2143 
2144 	/* Update statistics for the given port, then take the lock to avoid
2145 	 * concurrent accesses on the ethtool_stats structure during its copy.
2146 	 */
2147 	mvpp2_gather_hw_statistics(&port->stats_work.work);
2148 
2149 	mutex_lock(&port->gather_stats_lock);
2150 	memcpy(data, port->ethtool_stats,
2151 	       sizeof(u64) * MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs));
2152 	mutex_unlock(&port->gather_stats_lock);
2153 }
2154 
mvpp2_ethtool_get_sset_count(struct net_device * dev,int sset)2155 static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
2156 {
2157 	struct mvpp2_port *port = netdev_priv(dev);
2158 
2159 	if (sset == ETH_SS_STATS)
2160 		return MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs);
2161 
2162 	return -EOPNOTSUPP;
2163 }
2164 
mvpp2_mac_reset_assert(struct mvpp2_port * port)2165 static void mvpp2_mac_reset_assert(struct mvpp2_port *port)
2166 {
2167 	u32 val;
2168 
2169 	val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) |
2170 	      MVPP2_GMAC_PORT_RESET_MASK;
2171 	writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2172 
2173 	if (port->priv->hw_version >= MVPP22 && port->gop_id == 0) {
2174 		val = readl(port->base + MVPP22_XLG_CTRL0_REG) &
2175 		      ~MVPP22_XLG_CTRL0_MAC_RESET_DIS;
2176 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
2177 	}
2178 }
2179 
mvpp22_pcs_reset_assert(struct mvpp2_port * port)2180 static void mvpp22_pcs_reset_assert(struct mvpp2_port *port)
2181 {
2182 	struct mvpp2 *priv = port->priv;
2183 	void __iomem *mpcs, *xpcs;
2184 	u32 val;
2185 
2186 	if (port->priv->hw_version == MVPP21 || port->gop_id != 0)
2187 		return;
2188 
2189 	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
2190 	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
2191 
2192 	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
2193 	val &= ~(MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
2194 	val |= MVPP22_MPCS_CLK_RESET_DIV_SET;
2195 	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
2196 
2197 	val = readl(xpcs + MVPP22_XPCS_CFG0);
2198 	writel(val & ~MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
2199 }
2200 
mvpp22_pcs_reset_deassert(struct mvpp2_port * port,phy_interface_t interface)2201 static void mvpp22_pcs_reset_deassert(struct mvpp2_port *port,
2202 				      phy_interface_t interface)
2203 {
2204 	struct mvpp2 *priv = port->priv;
2205 	void __iomem *mpcs, *xpcs;
2206 	u32 val;
2207 
2208 	if (port->priv->hw_version == MVPP21 || port->gop_id != 0)
2209 		return;
2210 
2211 	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
2212 	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
2213 
2214 	switch (interface) {
2215 	case PHY_INTERFACE_MODE_5GBASER:
2216 	case PHY_INTERFACE_MODE_10GBASER:
2217 		val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
2218 		val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX |
2219 		       MAC_CLK_RESET_SD_TX;
2220 		val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
2221 		writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
2222 		break;
2223 	case PHY_INTERFACE_MODE_XAUI:
2224 	case PHY_INTERFACE_MODE_RXAUI:
2225 		val = readl(xpcs + MVPP22_XPCS_CFG0);
2226 		writel(val | MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
2227 		break;
2228 	default:
2229 		break;
2230 	}
2231 }
2232 
2233 /* Change maximum receive size of the port */
mvpp2_gmac_max_rx_size_set(struct mvpp2_port * port)2234 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
2235 {
2236 	u32 val;
2237 
2238 	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2239 	val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
2240 	val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2241 		    MVPP2_GMAC_MAX_RX_SIZE_OFFS);
2242 	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2243 }
2244 
2245 /* Change maximum receive size of the port */
mvpp2_xlg_max_rx_size_set(struct mvpp2_port * port)2246 static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
2247 {
2248 	u32 val;
2249 
2250 	val =  readl(port->base + MVPP22_XLG_CTRL1_REG);
2251 	val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
2252 	val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2253 	       MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
2254 	writel(val, port->base + MVPP22_XLG_CTRL1_REG);
2255 }
2256 
2257 /* Set defaults to the MVPP2 port */
mvpp2_defaults_set(struct mvpp2_port * port)2258 static void mvpp2_defaults_set(struct mvpp2_port *port)
2259 {
2260 	int tx_port_num, val, queue, lrxq;
2261 
2262 	if (port->priv->hw_version == MVPP21) {
2263 		/* Update TX FIFO MIN Threshold */
2264 		val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2265 		val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
2266 		/* Min. TX threshold must be less than minimal packet length */
2267 		val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
2268 		writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2269 	}
2270 
2271 	/* Disable Legacy WRR, Disable EJP, Release from reset */
2272 	tx_port_num = mvpp2_egress_port(port);
2273 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
2274 		    tx_port_num);
2275 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
2276 
2277 	/* Set TXQ scheduling to Round-Robin */
2278 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_FIXED_PRIO_REG, 0);
2279 
2280 	/* Close bandwidth for all queues */
2281 	for (queue = 0; queue < MVPP2_MAX_TXQ; queue++)
2282 		mvpp2_write(port->priv,
2283 			    MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(queue), 0);
2284 
2285 	/* Set refill period to 1 usec, refill tokens
2286 	 * and bucket size to maximum
2287 	 */
2288 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
2289 		    port->priv->tclk / USEC_PER_SEC);
2290 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
2291 	val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
2292 	val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
2293 	val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
2294 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
2295 	val = MVPP2_TXP_TOKEN_SIZE_MAX;
2296 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2297 
2298 	/* Set MaximumLowLatencyPacketSize value to 256 */
2299 	mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
2300 		    MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
2301 		    MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
2302 
2303 	/* Enable Rx cache snoop */
2304 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
2305 		queue = port->rxqs[lrxq]->id;
2306 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2307 		val |= MVPP2_SNOOP_PKT_SIZE_MASK |
2308 			   MVPP2_SNOOP_BUF_HDR_MASK;
2309 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2310 	}
2311 
2312 	/* At default, mask all interrupts to all present cpus */
2313 	mvpp2_interrupts_disable(port);
2314 }
2315 
2316 /* Enable/disable receiving packets */
mvpp2_ingress_enable(struct mvpp2_port * port)2317 static void mvpp2_ingress_enable(struct mvpp2_port *port)
2318 {
2319 	u32 val;
2320 	int lrxq, queue;
2321 
2322 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
2323 		queue = port->rxqs[lrxq]->id;
2324 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2325 		val &= ~MVPP2_RXQ_DISABLE_MASK;
2326 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2327 	}
2328 }
2329 
mvpp2_ingress_disable(struct mvpp2_port * port)2330 static void mvpp2_ingress_disable(struct mvpp2_port *port)
2331 {
2332 	u32 val;
2333 	int lrxq, queue;
2334 
2335 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
2336 		queue = port->rxqs[lrxq]->id;
2337 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2338 		val |= MVPP2_RXQ_DISABLE_MASK;
2339 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2340 	}
2341 }
2342 
2343 /* Enable transmit via physical egress queue
2344  * - HW starts take descriptors from DRAM
2345  */
mvpp2_egress_enable(struct mvpp2_port * port)2346 static void mvpp2_egress_enable(struct mvpp2_port *port)
2347 {
2348 	u32 qmap;
2349 	int queue;
2350 	int tx_port_num = mvpp2_egress_port(port);
2351 
2352 	/* Enable all initialized TXs. */
2353 	qmap = 0;
2354 	for (queue = 0; queue < port->ntxqs; queue++) {
2355 		struct mvpp2_tx_queue *txq = port->txqs[queue];
2356 
2357 		if (txq->descs)
2358 			qmap |= (1 << queue);
2359 	}
2360 
2361 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2362 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
2363 }
2364 
2365 /* Disable transmit via physical egress queue
2366  * - HW doesn't take descriptors from DRAM
2367  */
mvpp2_egress_disable(struct mvpp2_port * port)2368 static void mvpp2_egress_disable(struct mvpp2_port *port)
2369 {
2370 	u32 reg_data;
2371 	int delay;
2372 	int tx_port_num = mvpp2_egress_port(port);
2373 
2374 	/* Issue stop command for active channels only */
2375 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2376 	reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
2377 		    MVPP2_TXP_SCHED_ENQ_MASK;
2378 	if (reg_data != 0)
2379 		mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
2380 			    (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
2381 
2382 	/* Wait for all Tx activity to terminate. */
2383 	delay = 0;
2384 	do {
2385 		if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
2386 			netdev_warn(port->dev,
2387 				    "Tx stop timed out, status=0x%08x\n",
2388 				    reg_data);
2389 			break;
2390 		}
2391 		mdelay(1);
2392 		delay++;
2393 
2394 		/* Check port TX Command register that all
2395 		 * Tx queues are stopped
2396 		 */
2397 		reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
2398 	} while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
2399 }
2400 
2401 /* Rx descriptors helper methods */
2402 
2403 /* Get number of Rx descriptors occupied by received packets */
2404 static inline int
mvpp2_rxq_received(struct mvpp2_port * port,int rxq_id)2405 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
2406 {
2407 	u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
2408 
2409 	return val & MVPP2_RXQ_OCCUPIED_MASK;
2410 }
2411 
2412 /* Update Rx queue status with the number of occupied and available
2413  * Rx descriptor slots.
2414  */
2415 static inline void
mvpp2_rxq_status_update(struct mvpp2_port * port,int rxq_id,int used_count,int free_count)2416 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
2417 			int used_count, int free_count)
2418 {
2419 	/* Decrement the number of used descriptors and increment count
2420 	 * increment the number of free descriptors.
2421 	 */
2422 	u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
2423 
2424 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
2425 }
2426 
2427 /* Get pointer to next RX descriptor to be processed by SW */
2428 static inline struct mvpp2_rx_desc *
mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue * rxq)2429 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
2430 {
2431 	int rx_desc = rxq->next_desc_to_proc;
2432 
2433 	rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
2434 	prefetch(rxq->descs + rxq->next_desc_to_proc);
2435 	return rxq->descs + rx_desc;
2436 }
2437 
2438 /* Set rx queue offset */
mvpp2_rxq_offset_set(struct mvpp2_port * port,int prxq,int offset)2439 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
2440 				 int prxq, int offset)
2441 {
2442 	u32 val;
2443 
2444 	/* Convert offset from bytes to units of 32 bytes */
2445 	offset = offset >> 5;
2446 
2447 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2448 	val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
2449 
2450 	/* Offset is in */
2451 	val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
2452 		    MVPP2_RXQ_PACKET_OFFSET_MASK);
2453 
2454 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2455 }
2456 
2457 /* Tx descriptors helper methods */
2458 
2459 /* Get pointer to next Tx descriptor to be processed (send) by HW */
2460 static struct mvpp2_tx_desc *
mvpp2_txq_next_desc_get(struct mvpp2_tx_queue * txq)2461 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
2462 {
2463 	int tx_desc = txq->next_desc_to_proc;
2464 
2465 	txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
2466 	return txq->descs + tx_desc;
2467 }
2468 
2469 /* Update HW with number of aggregated Tx descriptors to be sent
2470  *
2471  * Called only from mvpp2_tx(), so migration is disabled, using
2472  * smp_processor_id() is OK.
2473  */
mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port * port,int pending)2474 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
2475 {
2476 	/* aggregated access - relevant TXQ number is written in TX desc */
2477 	mvpp2_thread_write(port->priv,
2478 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2479 			   MVPP2_AGGR_TXQ_UPDATE_REG, pending);
2480 }
2481 
2482 /* Check if there are enough free descriptors in aggregated txq.
2483  * If not, update the number of occupied descriptors and repeat the check.
2484  *
2485  * Called only from mvpp2_tx(), so migration is disabled, using
2486  * smp_processor_id() is OK.
2487  */
mvpp2_aggr_desc_num_check(struct mvpp2_port * port,struct mvpp2_tx_queue * aggr_txq,int num)2488 static int mvpp2_aggr_desc_num_check(struct mvpp2_port *port,
2489 				     struct mvpp2_tx_queue *aggr_txq, int num)
2490 {
2491 	if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
2492 		/* Update number of occupied aggregated Tx descriptors */
2493 		unsigned int thread =
2494 			mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2495 		u32 val = mvpp2_read_relaxed(port->priv,
2496 					     MVPP2_AGGR_TXQ_STATUS_REG(thread));
2497 
2498 		aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
2499 
2500 		if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
2501 			return -ENOMEM;
2502 	}
2503 	return 0;
2504 }
2505 
2506 /* Reserved Tx descriptors allocation request
2507  *
2508  * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
2509  * only by mvpp2_tx(), so migration is disabled, using
2510  * smp_processor_id() is OK.
2511  */
mvpp2_txq_alloc_reserved_desc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,int num)2512 static int mvpp2_txq_alloc_reserved_desc(struct mvpp2_port *port,
2513 					 struct mvpp2_tx_queue *txq, int num)
2514 {
2515 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2516 	struct mvpp2 *priv = port->priv;
2517 	u32 val;
2518 
2519 	val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
2520 	mvpp2_thread_write_relaxed(priv, thread, MVPP2_TXQ_RSVD_REQ_REG, val);
2521 
2522 	val = mvpp2_thread_read_relaxed(priv, thread, MVPP2_TXQ_RSVD_RSLT_REG);
2523 
2524 	return val & MVPP2_TXQ_RSVD_RSLT_MASK;
2525 }
2526 
2527 /* Check if there are enough reserved descriptors for transmission.
2528  * If not, request chunk of reserved descriptors and check again.
2529  */
mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu,int num)2530 static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port *port,
2531 					    struct mvpp2_tx_queue *txq,
2532 					    struct mvpp2_txq_pcpu *txq_pcpu,
2533 					    int num)
2534 {
2535 	int req, desc_count;
2536 	unsigned int thread;
2537 
2538 	if (txq_pcpu->reserved_num >= num)
2539 		return 0;
2540 
2541 	/* Not enough descriptors reserved! Update the reserved descriptor
2542 	 * count and check again.
2543 	 */
2544 
2545 	desc_count = 0;
2546 	/* Compute total of used descriptors */
2547 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2548 		struct mvpp2_txq_pcpu *txq_pcpu_aux;
2549 
2550 		txq_pcpu_aux = per_cpu_ptr(txq->pcpu, thread);
2551 		desc_count += txq_pcpu_aux->count;
2552 		desc_count += txq_pcpu_aux->reserved_num;
2553 	}
2554 
2555 	req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
2556 	desc_count += req;
2557 
2558 	if (desc_count >
2559 	   (txq->size - (MVPP2_MAX_THREADS * MVPP2_CPU_DESC_CHUNK)))
2560 		return -ENOMEM;
2561 
2562 	txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(port, txq, req);
2563 
2564 	/* OK, the descriptor could have been updated: check again. */
2565 	if (txq_pcpu->reserved_num < num)
2566 		return -ENOMEM;
2567 	return 0;
2568 }
2569 
2570 /* Release the last allocated Tx descriptor. Useful to handle DMA
2571  * mapping failures in the Tx path.
2572  */
mvpp2_txq_desc_put(struct mvpp2_tx_queue * txq)2573 static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
2574 {
2575 	if (txq->next_desc_to_proc == 0)
2576 		txq->next_desc_to_proc = txq->last_desc - 1;
2577 	else
2578 		txq->next_desc_to_proc--;
2579 }
2580 
2581 /* Set Tx descriptors fields relevant for CSUM calculation */
mvpp2_txq_desc_csum(int l3_offs,__be16 l3_proto,int ip_hdr_len,int l4_proto)2582 static u32 mvpp2_txq_desc_csum(int l3_offs, __be16 l3_proto,
2583 			       int ip_hdr_len, int l4_proto)
2584 {
2585 	u32 command;
2586 
2587 	/* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
2588 	 * G_L4_chk, L4_type required only for checksum calculation
2589 	 */
2590 	command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
2591 	command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
2592 	command |= MVPP2_TXD_IP_CSUM_DISABLE;
2593 
2594 	if (l3_proto == htons(ETH_P_IP)) {
2595 		command &= ~MVPP2_TXD_IP_CSUM_DISABLE;	/* enable IPv4 csum */
2596 		command &= ~MVPP2_TXD_L3_IP6;		/* enable IPv4 */
2597 	} else {
2598 		command |= MVPP2_TXD_L3_IP6;		/* enable IPv6 */
2599 	}
2600 
2601 	if (l4_proto == IPPROTO_TCP) {
2602 		command &= ~MVPP2_TXD_L4_UDP;		/* enable TCP */
2603 		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
2604 	} else if (l4_proto == IPPROTO_UDP) {
2605 		command |= MVPP2_TXD_L4_UDP;		/* enable UDP */
2606 		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
2607 	} else {
2608 		command |= MVPP2_TXD_L4_CSUM_NOT;
2609 	}
2610 
2611 	return command;
2612 }
2613 
2614 /* Get number of sent descriptors and decrement counter.
2615  * The number of sent descriptors is returned.
2616  * Per-thread access
2617  *
2618  * Called only from mvpp2_txq_done(), called from mvpp2_tx()
2619  * (migration disabled) and from the TX completion tasklet (migration
2620  * disabled) so using smp_processor_id() is OK.
2621  */
mvpp2_txq_sent_desc_proc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2622 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
2623 					   struct mvpp2_tx_queue *txq)
2624 {
2625 	u32 val;
2626 
2627 	/* Reading status reg resets transmitted descriptor counter */
2628 	val = mvpp2_thread_read_relaxed(port->priv,
2629 					mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2630 					MVPP2_TXQ_SENT_REG(txq->id));
2631 
2632 	return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
2633 		MVPP2_TRANSMITTED_COUNT_OFFSET;
2634 }
2635 
2636 /* Called through on_each_cpu(), so runs on all CPUs, with migration
2637  * disabled, therefore using smp_processor_id() is OK.
2638  */
mvpp2_txq_sent_counter_clear(void * arg)2639 static void mvpp2_txq_sent_counter_clear(void *arg)
2640 {
2641 	struct mvpp2_port *port = arg;
2642 	int queue;
2643 
2644 	/* If the thread isn't used, don't do anything */
2645 	if (smp_processor_id() >= port->priv->nthreads)
2646 		return;
2647 
2648 	for (queue = 0; queue < port->ntxqs; queue++) {
2649 		int id = port->txqs[queue]->id;
2650 
2651 		mvpp2_thread_read(port->priv,
2652 				  mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2653 				  MVPP2_TXQ_SENT_REG(id));
2654 	}
2655 }
2656 
2657 /* Set max sizes for Tx queues */
mvpp2_txp_max_tx_size_set(struct mvpp2_port * port)2658 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
2659 {
2660 	u32	val, size, mtu;
2661 	int	txq, tx_port_num;
2662 
2663 	mtu = port->pkt_size * 8;
2664 	if (mtu > MVPP2_TXP_MTU_MAX)
2665 		mtu = MVPP2_TXP_MTU_MAX;
2666 
2667 	/* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
2668 	mtu = 3 * mtu;
2669 
2670 	/* Indirect access to registers */
2671 	tx_port_num = mvpp2_egress_port(port);
2672 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2673 
2674 	/* Set MTU */
2675 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
2676 	val &= ~MVPP2_TXP_MTU_MAX;
2677 	val |= mtu;
2678 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
2679 
2680 	/* TXP token size and all TXQs token size must be larger that MTU */
2681 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
2682 	size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
2683 	if (size < mtu) {
2684 		size = mtu;
2685 		val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
2686 		val |= size;
2687 		mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2688 	}
2689 
2690 	for (txq = 0; txq < port->ntxqs; txq++) {
2691 		val = mvpp2_read(port->priv,
2692 				 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
2693 		size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
2694 
2695 		if (size < mtu) {
2696 			size = mtu;
2697 			val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
2698 			val |= size;
2699 			mvpp2_write(port->priv,
2700 				    MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
2701 				    val);
2702 		}
2703 	}
2704 }
2705 
2706 /* Set the number of non-occupied descriptors threshold */
mvpp2_set_rxq_free_tresh(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2707 static void mvpp2_set_rxq_free_tresh(struct mvpp2_port *port,
2708 				     struct mvpp2_rx_queue *rxq)
2709 {
2710 	u32 val;
2711 
2712 	mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
2713 
2714 	val = mvpp2_read(port->priv, MVPP2_RXQ_THRESH_REG);
2715 	val &= ~MVPP2_RXQ_NON_OCCUPIED_MASK;
2716 	val |= MSS_THRESHOLD_STOP << MVPP2_RXQ_NON_OCCUPIED_OFFSET;
2717 	mvpp2_write(port->priv, MVPP2_RXQ_THRESH_REG, val);
2718 }
2719 
2720 /* Set the number of packets that will be received before Rx interrupt
2721  * will be generated by HW.
2722  */
mvpp2_rx_pkts_coal_set(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2723 static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
2724 				   struct mvpp2_rx_queue *rxq)
2725 {
2726 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2727 
2728 	if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
2729 		rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
2730 
2731 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2732 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_THRESH_REG,
2733 			   rxq->pkts_coal);
2734 
2735 	put_cpu();
2736 }
2737 
2738 /* For some reason in the LSP this is done on each CPU. Why ? */
mvpp2_tx_pkts_coal_set(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2739 static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
2740 				   struct mvpp2_tx_queue *txq)
2741 {
2742 	unsigned int thread;
2743 	u32 val;
2744 
2745 	if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
2746 		txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
2747 
2748 	val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
2749 	/* PKT-coalescing registers are per-queue + per-thread */
2750 	for (thread = 0; thread < MVPP2_MAX_THREADS; thread++) {
2751 		mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2752 		mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_THRESH_REG, val);
2753 	}
2754 }
2755 
mvpp2_usec_to_cycles(u32 usec,unsigned long clk_hz)2756 static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
2757 {
2758 	u64 tmp = (u64)clk_hz * usec;
2759 
2760 	do_div(tmp, USEC_PER_SEC);
2761 
2762 	return tmp > U32_MAX ? U32_MAX : tmp;
2763 }
2764 
mvpp2_cycles_to_usec(u32 cycles,unsigned long clk_hz)2765 static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
2766 {
2767 	u64 tmp = (u64)cycles * USEC_PER_SEC;
2768 
2769 	do_div(tmp, clk_hz);
2770 
2771 	return tmp > U32_MAX ? U32_MAX : tmp;
2772 }
2773 
2774 /* Set the time delay in usec before Rx interrupt */
mvpp2_rx_time_coal_set(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2775 static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
2776 				   struct mvpp2_rx_queue *rxq)
2777 {
2778 	unsigned long freq = port->priv->tclk;
2779 	u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2780 
2781 	if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
2782 		rxq->time_coal =
2783 			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
2784 
2785 		/* re-evaluate to get actual register value */
2786 		val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2787 	}
2788 
2789 	mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
2790 }
2791 
mvpp2_tx_time_coal_set(struct mvpp2_port * port)2792 static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
2793 {
2794 	unsigned long freq = port->priv->tclk;
2795 	u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2796 
2797 	if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
2798 		port->tx_time_coal =
2799 			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
2800 
2801 		/* re-evaluate to get actual register value */
2802 		val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2803 	}
2804 
2805 	mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
2806 }
2807 
2808 /* Free Tx queue skbuffs */
mvpp2_txq_bufs_free(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu,int num)2809 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
2810 				struct mvpp2_tx_queue *txq,
2811 				struct mvpp2_txq_pcpu *txq_pcpu, int num)
2812 {
2813 	struct xdp_frame_bulk bq;
2814 	int i;
2815 
2816 	xdp_frame_bulk_init(&bq);
2817 
2818 	rcu_read_lock(); /* need for xdp_return_frame_bulk */
2819 
2820 	for (i = 0; i < num; i++) {
2821 		struct mvpp2_txq_pcpu_buf *tx_buf =
2822 			txq_pcpu->buffs + txq_pcpu->txq_get_index;
2823 
2824 		if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma) &&
2825 		    tx_buf->type != MVPP2_TYPE_XDP_TX)
2826 			dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
2827 					 tx_buf->size, DMA_TO_DEVICE);
2828 		if (tx_buf->type == MVPP2_TYPE_SKB && tx_buf->skb)
2829 			dev_kfree_skb_any(tx_buf->skb);
2830 		else if (tx_buf->type == MVPP2_TYPE_XDP_TX ||
2831 			 tx_buf->type == MVPP2_TYPE_XDP_NDO)
2832 			xdp_return_frame_bulk(tx_buf->xdpf, &bq);
2833 
2834 		mvpp2_txq_inc_get(txq_pcpu);
2835 	}
2836 	xdp_flush_frame_bulk(&bq);
2837 
2838 	rcu_read_unlock();
2839 }
2840 
mvpp2_get_rx_queue(struct mvpp2_port * port,u32 cause)2841 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
2842 							u32 cause)
2843 {
2844 	int queue = fls(cause) - 1;
2845 
2846 	return port->rxqs[queue];
2847 }
2848 
mvpp2_get_tx_queue(struct mvpp2_port * port,u32 cause)2849 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
2850 							u32 cause)
2851 {
2852 	int queue = fls(cause) - 1;
2853 
2854 	return port->txqs[queue];
2855 }
2856 
2857 /* Handle end of transmission */
mvpp2_txq_done(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu)2858 static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
2859 			   struct mvpp2_txq_pcpu *txq_pcpu)
2860 {
2861 	struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
2862 	int tx_done;
2863 
2864 	if (txq_pcpu->thread != mvpp2_cpu_to_thread(port->priv, smp_processor_id()))
2865 		netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
2866 
2867 	tx_done = mvpp2_txq_sent_desc_proc(port, txq);
2868 	if (!tx_done)
2869 		return;
2870 	mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
2871 
2872 	txq_pcpu->count -= tx_done;
2873 
2874 	if (netif_tx_queue_stopped(nq))
2875 		if (txq_pcpu->count <= txq_pcpu->wake_threshold)
2876 			netif_tx_wake_queue(nq);
2877 }
2878 
mvpp2_tx_done(struct mvpp2_port * port,u32 cause,unsigned int thread)2879 static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
2880 				  unsigned int thread)
2881 {
2882 	struct mvpp2_tx_queue *txq;
2883 	struct mvpp2_txq_pcpu *txq_pcpu;
2884 	unsigned int tx_todo = 0;
2885 
2886 	while (cause) {
2887 		txq = mvpp2_get_tx_queue(port, cause);
2888 		if (!txq)
2889 			break;
2890 
2891 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2892 
2893 		if (txq_pcpu->count) {
2894 			mvpp2_txq_done(port, txq, txq_pcpu);
2895 			tx_todo += txq_pcpu->count;
2896 		}
2897 
2898 		cause &= ~(1 << txq->log_id);
2899 	}
2900 	return tx_todo;
2901 }
2902 
2903 /* Rx/Tx queue initialization/cleanup methods */
2904 
2905 /* Allocate and initialize descriptors for aggr TXQ */
mvpp2_aggr_txq_init(struct platform_device * pdev,struct mvpp2_tx_queue * aggr_txq,unsigned int thread,struct mvpp2 * priv)2906 static int mvpp2_aggr_txq_init(struct platform_device *pdev,
2907 			       struct mvpp2_tx_queue *aggr_txq,
2908 			       unsigned int thread, struct mvpp2 *priv)
2909 {
2910 	u32 txq_dma;
2911 
2912 	/* Allocate memory for TX descriptors */
2913 	aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
2914 					     MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
2915 					     &aggr_txq->descs_dma, GFP_KERNEL);
2916 	if (!aggr_txq->descs)
2917 		return -ENOMEM;
2918 
2919 	aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
2920 
2921 	/* Aggr TXQ no reset WA */
2922 	aggr_txq->next_desc_to_proc = mvpp2_read(priv,
2923 						 MVPP2_AGGR_TXQ_INDEX_REG(thread));
2924 
2925 	/* Set Tx descriptors queue starting address indirect
2926 	 * access
2927 	 */
2928 	if (priv->hw_version == MVPP21)
2929 		txq_dma = aggr_txq->descs_dma;
2930 	else
2931 		txq_dma = aggr_txq->descs_dma >>
2932 			MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
2933 
2934 	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(thread), txq_dma);
2935 	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(thread),
2936 		    MVPP2_AGGR_TXQ_SIZE);
2937 
2938 	return 0;
2939 }
2940 
2941 /* Create a specified Rx queue */
mvpp2_rxq_init(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2942 static int mvpp2_rxq_init(struct mvpp2_port *port,
2943 			  struct mvpp2_rx_queue *rxq)
2944 {
2945 	struct mvpp2 *priv = port->priv;
2946 	unsigned int thread;
2947 	u32 rxq_dma;
2948 	int err;
2949 
2950 	rxq->size = port->rx_ring_size;
2951 
2952 	/* Allocate memory for RX descriptors */
2953 	rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
2954 					rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2955 					&rxq->descs_dma, GFP_KERNEL);
2956 	if (!rxq->descs)
2957 		return -ENOMEM;
2958 
2959 	rxq->last_desc = rxq->size - 1;
2960 
2961 	/* Zero occupied and non-occupied counters - direct access */
2962 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2963 
2964 	/* Set Rx descriptors queue starting address - indirect access */
2965 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2966 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2967 	if (port->priv->hw_version == MVPP21)
2968 		rxq_dma = rxq->descs_dma;
2969 	else
2970 		rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
2971 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
2972 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
2973 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_INDEX_REG, 0);
2974 	put_cpu();
2975 
2976 	/* Set Offset */
2977 	mvpp2_rxq_offset_set(port, rxq->id, MVPP2_SKB_HEADROOM);
2978 
2979 	/* Set coalescing pkts and time */
2980 	mvpp2_rx_pkts_coal_set(port, rxq);
2981 	mvpp2_rx_time_coal_set(port, rxq);
2982 
2983 	/* Set the number of non occupied descriptors threshold */
2984 	mvpp2_set_rxq_free_tresh(port, rxq);
2985 
2986 	/* Add number of descriptors ready for receiving packets */
2987 	mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
2988 
2989 	if (priv->percpu_pools) {
2990 		err = xdp_rxq_info_reg(&rxq->xdp_rxq_short, port->dev, rxq->logic_rxq, 0);
2991 		if (err < 0)
2992 			goto err_free_dma;
2993 
2994 		err = xdp_rxq_info_reg(&rxq->xdp_rxq_long, port->dev, rxq->logic_rxq, 0);
2995 		if (err < 0)
2996 			goto err_unregister_rxq_short;
2997 
2998 		/* Every RXQ has a pool for short and another for long packets */
2999 		err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_short,
3000 						 MEM_TYPE_PAGE_POOL,
3001 						 priv->page_pool[rxq->logic_rxq]);
3002 		if (err < 0)
3003 			goto err_unregister_rxq_long;
3004 
3005 		err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_long,
3006 						 MEM_TYPE_PAGE_POOL,
3007 						 priv->page_pool[rxq->logic_rxq +
3008 								 port->nrxqs]);
3009 		if (err < 0)
3010 			goto err_unregister_mem_rxq_short;
3011 	}
3012 
3013 	return 0;
3014 
3015 err_unregister_mem_rxq_short:
3016 	xdp_rxq_info_unreg_mem_model(&rxq->xdp_rxq_short);
3017 err_unregister_rxq_long:
3018 	xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
3019 err_unregister_rxq_short:
3020 	xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
3021 err_free_dma:
3022 	dma_free_coherent(port->dev->dev.parent,
3023 			  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
3024 			  rxq->descs, rxq->descs_dma);
3025 	return err;
3026 }
3027 
3028 /* Push packets received by the RXQ to BM pool */
mvpp2_rxq_drop_pkts(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)3029 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
3030 				struct mvpp2_rx_queue *rxq)
3031 {
3032 	int rx_received, i;
3033 
3034 	rx_received = mvpp2_rxq_received(port, rxq->id);
3035 	if (!rx_received)
3036 		return;
3037 
3038 	for (i = 0; i < rx_received; i++) {
3039 		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3040 		u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3041 		int pool;
3042 
3043 		pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
3044 			MVPP2_RXD_BM_POOL_ID_OFFS;
3045 
3046 		mvpp2_bm_pool_put(port, pool,
3047 				  mvpp2_rxdesc_dma_addr_get(port, rx_desc),
3048 				  mvpp2_rxdesc_cookie_get(port, rx_desc));
3049 	}
3050 	mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
3051 }
3052 
3053 /* Cleanup Rx queue */
mvpp2_rxq_deinit(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)3054 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
3055 			     struct mvpp2_rx_queue *rxq)
3056 {
3057 	unsigned int thread;
3058 
3059 	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_short))
3060 		xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
3061 
3062 	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_long))
3063 		xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
3064 
3065 	mvpp2_rxq_drop_pkts(port, rxq);
3066 
3067 	if (rxq->descs)
3068 		dma_free_coherent(port->dev->dev.parent,
3069 				  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
3070 				  rxq->descs,
3071 				  rxq->descs_dma);
3072 
3073 	rxq->descs             = NULL;
3074 	rxq->last_desc         = 0;
3075 	rxq->next_desc_to_proc = 0;
3076 	rxq->descs_dma         = 0;
3077 
3078 	/* Clear Rx descriptors queue starting address and size;
3079 	 * free descriptor number
3080 	 */
3081 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3082 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
3083 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
3084 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, 0);
3085 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, 0);
3086 	put_cpu();
3087 }
3088 
3089 /* Create and initialize a Tx queue */
mvpp2_txq_init(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)3090 static int mvpp2_txq_init(struct mvpp2_port *port,
3091 			  struct mvpp2_tx_queue *txq)
3092 {
3093 	u32 val;
3094 	unsigned int thread;
3095 	int desc, desc_per_txq, tx_port_num;
3096 	struct mvpp2_txq_pcpu *txq_pcpu;
3097 
3098 	txq->size = port->tx_ring_size;
3099 
3100 	/* Allocate memory for Tx descriptors */
3101 	txq->descs = dma_alloc_coherent(port->dev->dev.parent,
3102 				txq->size * MVPP2_DESC_ALIGNED_SIZE,
3103 				&txq->descs_dma, GFP_KERNEL);
3104 	if (!txq->descs)
3105 		return -ENOMEM;
3106 
3107 	txq->last_desc = txq->size - 1;
3108 
3109 	/* Set Tx descriptors queue starting address - indirect access */
3110 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
3111 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
3112 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG,
3113 			   txq->descs_dma);
3114 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG,
3115 			   txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
3116 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_INDEX_REG, 0);
3117 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_RSVD_CLR_REG,
3118 			   txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
3119 	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PENDING_REG);
3120 	val &= ~MVPP2_TXQ_PENDING_MASK;
3121 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PENDING_REG, val);
3122 
3123 	/* Calculate base address in prefetch buffer. We reserve 16 descriptors
3124 	 * for each existing TXQ.
3125 	 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
3126 	 * GBE ports assumed to be continuous from 0 to MVPP2_MAX_PORTS
3127 	 */
3128 	desc_per_txq = 16;
3129 	desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
3130 	       (txq->log_id * desc_per_txq);
3131 
3132 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG,
3133 			   MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
3134 			   MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
3135 	put_cpu();
3136 
3137 	/* WRR / EJP configuration - indirect access */
3138 	tx_port_num = mvpp2_egress_port(port);
3139 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3140 
3141 	val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
3142 	val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
3143 	val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
3144 	val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
3145 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
3146 
3147 	val = MVPP2_TXQ_TOKEN_SIZE_MAX;
3148 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
3149 		    val);
3150 
3151 	for (thread = 0; thread < port->priv->nthreads; thread++) {
3152 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3153 		txq_pcpu->size = txq->size;
3154 		txq_pcpu->buffs = kmalloc_objs(*txq_pcpu->buffs, txq_pcpu->size);
3155 		if (!txq_pcpu->buffs)
3156 			return -ENOMEM;
3157 
3158 		txq_pcpu->count = 0;
3159 		txq_pcpu->reserved_num = 0;
3160 		txq_pcpu->txq_put_index = 0;
3161 		txq_pcpu->txq_get_index = 0;
3162 		txq_pcpu->tso_headers = NULL;
3163 
3164 		txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
3165 		txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
3166 
3167 		txq_pcpu->tso_headers =
3168 			dma_alloc_coherent(port->dev->dev.parent,
3169 					   txq_pcpu->size * TSO_HEADER_SIZE,
3170 					   &txq_pcpu->tso_headers_dma,
3171 					   GFP_KERNEL);
3172 		if (!txq_pcpu->tso_headers)
3173 			return -ENOMEM;
3174 	}
3175 
3176 	return 0;
3177 }
3178 
3179 /* Free allocated TXQ resources */
mvpp2_txq_deinit(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)3180 static void mvpp2_txq_deinit(struct mvpp2_port *port,
3181 			     struct mvpp2_tx_queue *txq)
3182 {
3183 	struct mvpp2_txq_pcpu *txq_pcpu;
3184 	unsigned int thread;
3185 
3186 	for (thread = 0; thread < port->priv->nthreads; thread++) {
3187 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3188 		kfree(txq_pcpu->buffs);
3189 
3190 		if (txq_pcpu->tso_headers)
3191 			dma_free_coherent(port->dev->dev.parent,
3192 					  txq_pcpu->size * TSO_HEADER_SIZE,
3193 					  txq_pcpu->tso_headers,
3194 					  txq_pcpu->tso_headers_dma);
3195 
3196 		txq_pcpu->tso_headers = NULL;
3197 	}
3198 
3199 	if (txq->descs)
3200 		dma_free_coherent(port->dev->dev.parent,
3201 				  txq->size * MVPP2_DESC_ALIGNED_SIZE,
3202 				  txq->descs, txq->descs_dma);
3203 
3204 	txq->descs             = NULL;
3205 	txq->last_desc         = 0;
3206 	txq->next_desc_to_proc = 0;
3207 	txq->descs_dma         = 0;
3208 
3209 	/* Set minimum bandwidth for disabled TXQs */
3210 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->log_id), 0);
3211 
3212 	/* Set Tx descriptors queue starting address and size */
3213 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
3214 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
3215 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG, 0);
3216 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG, 0);
3217 	put_cpu();
3218 }
3219 
3220 /* Cleanup Tx ports */
mvpp2_txq_clean(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)3221 static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
3222 {
3223 	struct mvpp2_txq_pcpu *txq_pcpu;
3224 	int delay, pending;
3225 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
3226 	u32 val;
3227 
3228 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
3229 	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG);
3230 	val |= MVPP2_TXQ_DRAIN_EN_MASK;
3231 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
3232 
3233 	/* The napi queue has been stopped so wait for all packets
3234 	 * to be transmitted.
3235 	 */
3236 	delay = 0;
3237 	do {
3238 		if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
3239 			netdev_warn(port->dev,
3240 				    "port %d: cleaning queue %d timed out\n",
3241 				    port->id, txq->log_id);
3242 			break;
3243 		}
3244 		mdelay(1);
3245 		delay++;
3246 
3247 		pending = mvpp2_thread_read(port->priv, thread,
3248 					    MVPP2_TXQ_PENDING_REG);
3249 		pending &= MVPP2_TXQ_PENDING_MASK;
3250 	} while (pending);
3251 
3252 	val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
3253 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
3254 	put_cpu();
3255 
3256 	for (thread = 0; thread < port->priv->nthreads; thread++) {
3257 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3258 
3259 		/* Release all packets */
3260 		mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
3261 
3262 		/* Reset queue */
3263 		txq_pcpu->count = 0;
3264 		txq_pcpu->txq_put_index = 0;
3265 		txq_pcpu->txq_get_index = 0;
3266 	}
3267 }
3268 
3269 /* Cleanup all Tx queues */
mvpp2_cleanup_txqs(struct mvpp2_port * port)3270 static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
3271 {
3272 	struct mvpp2_tx_queue *txq;
3273 	int queue;
3274 	u32 val;
3275 
3276 	val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
3277 
3278 	/* Reset Tx ports and delete Tx queues */
3279 	val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
3280 	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3281 
3282 	for (queue = 0; queue < port->ntxqs; queue++) {
3283 		txq = port->txqs[queue];
3284 		mvpp2_txq_clean(port, txq);
3285 		mvpp2_txq_deinit(port, txq);
3286 	}
3287 
3288 	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
3289 
3290 	val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
3291 	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
3292 }
3293 
3294 /* Cleanup all Rx queues */
mvpp2_cleanup_rxqs(struct mvpp2_port * port)3295 static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
3296 {
3297 	int queue;
3298 
3299 	for (queue = 0; queue < port->nrxqs; queue++)
3300 		mvpp2_rxq_deinit(port, port->rxqs[queue]);
3301 
3302 	if (port->tx_fc)
3303 		mvpp2_rxq_disable_fc(port);
3304 }
3305 
3306 /* Init all Rx queues for port */
mvpp2_setup_rxqs(struct mvpp2_port * port)3307 static int mvpp2_setup_rxqs(struct mvpp2_port *port)
3308 {
3309 	int queue, err;
3310 
3311 	for (queue = 0; queue < port->nrxqs; queue++) {
3312 		err = mvpp2_rxq_init(port, port->rxqs[queue]);
3313 		if (err)
3314 			goto err_cleanup;
3315 	}
3316 
3317 	if (port->tx_fc)
3318 		mvpp2_rxq_enable_fc(port);
3319 
3320 	return 0;
3321 
3322 err_cleanup:
3323 	mvpp2_cleanup_rxqs(port);
3324 	return err;
3325 }
3326 
3327 /* Init all tx queues for port */
mvpp2_setup_txqs(struct mvpp2_port * port)3328 static int mvpp2_setup_txqs(struct mvpp2_port *port)
3329 {
3330 	struct mvpp2_tx_queue *txq;
3331 	int queue, err;
3332 
3333 	for (queue = 0; queue < port->ntxqs; queue++) {
3334 		txq = port->txqs[queue];
3335 		err = mvpp2_txq_init(port, txq);
3336 		if (err)
3337 			goto err_cleanup;
3338 
3339 		/* Assign this queue to a CPU */
3340 		if (queue < num_possible_cpus())
3341 			netif_set_xps_queue(port->dev, cpumask_of(queue), queue);
3342 	}
3343 
3344 	if (port->has_tx_irqs) {
3345 		mvpp2_tx_time_coal_set(port);
3346 		for (queue = 0; queue < port->ntxqs; queue++) {
3347 			txq = port->txqs[queue];
3348 			mvpp2_tx_pkts_coal_set(port, txq);
3349 		}
3350 	}
3351 
3352 	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
3353 	return 0;
3354 
3355 err_cleanup:
3356 	mvpp2_cleanup_txqs(port);
3357 	return err;
3358 }
3359 
3360 /* The callback for per-port interrupt */
mvpp2_isr(int irq,void * dev_id)3361 static irqreturn_t mvpp2_isr(int irq, void *dev_id)
3362 {
3363 	struct mvpp2_queue_vector *qv = dev_id;
3364 
3365 	mvpp2_qvec_interrupt_disable(qv);
3366 
3367 	napi_schedule(&qv->napi);
3368 
3369 	return IRQ_HANDLED;
3370 }
3371 
mvpp2_isr_handle_ptp_queue(struct mvpp2_port * port,int nq)3372 static void mvpp2_isr_handle_ptp_queue(struct mvpp2_port *port, int nq)
3373 {
3374 	struct skb_shared_hwtstamps shhwtstamps;
3375 	struct mvpp2_hwtstamp_queue *queue;
3376 	struct sk_buff *skb;
3377 	void __iomem *ptp_q;
3378 	unsigned int id;
3379 	u32 r0, r1, r2;
3380 
3381 	ptp_q = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
3382 	if (nq)
3383 		ptp_q += MVPP22_PTP_TX_Q1_R0 - MVPP22_PTP_TX_Q0_R0;
3384 
3385 	queue = &port->tx_hwtstamp_queue[nq];
3386 
3387 	while (1) {
3388 		r0 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R0) & 0xffff;
3389 		if (!r0)
3390 			break;
3391 
3392 		r1 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R1) & 0xffff;
3393 		r2 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R2) & 0xffff;
3394 
3395 		id = (r0 >> 1) & 31;
3396 
3397 		skb = queue->skb[id];
3398 		queue->skb[id] = NULL;
3399 		if (skb) {
3400 			u32 ts = r2 << 19 | r1 << 3 | r0 >> 13;
3401 
3402 			mvpp22_tai_tstamp(port->priv->tai, ts, &shhwtstamps);
3403 			skb_tstamp_tx(skb, &shhwtstamps);
3404 			dev_kfree_skb_any(skb);
3405 		}
3406 	}
3407 }
3408 
mvpp2_isr_handle_ptp(struct mvpp2_port * port)3409 static void mvpp2_isr_handle_ptp(struct mvpp2_port *port)
3410 {
3411 	void __iomem *ptp;
3412 	u32 val;
3413 
3414 	ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
3415 	val = readl(ptp + MVPP22_PTP_INT_CAUSE);
3416 	if (val & MVPP22_PTP_INT_CAUSE_QUEUE0)
3417 		mvpp2_isr_handle_ptp_queue(port, 0);
3418 	if (val & MVPP22_PTP_INT_CAUSE_QUEUE1)
3419 		mvpp2_isr_handle_ptp_queue(port, 1);
3420 }
3421 
mvpp2_isr_handle_link(struct mvpp2_port * port,struct phylink_pcs * pcs,bool link)3422 static void mvpp2_isr_handle_link(struct mvpp2_port *port,
3423 				  struct phylink_pcs *pcs, bool link)
3424 {
3425 	struct net_device *dev = port->dev;
3426 
3427 	if (port->phylink) {
3428 		phylink_pcs_change(pcs, link);
3429 		return;
3430 	}
3431 
3432 	if (!netif_running(dev))
3433 		return;
3434 
3435 	if (link) {
3436 		mvpp2_interrupts_enable(port);
3437 
3438 		mvpp2_egress_enable(port);
3439 		mvpp2_ingress_enable(port);
3440 		netif_carrier_on(dev);
3441 		netif_tx_wake_all_queues(dev);
3442 	} else {
3443 		netif_tx_stop_all_queues(dev);
3444 		netif_carrier_off(dev);
3445 		mvpp2_ingress_disable(port);
3446 		mvpp2_egress_disable(port);
3447 
3448 		mvpp2_interrupts_disable(port);
3449 	}
3450 }
3451 
mvpp2_isr_handle_xlg(struct mvpp2_port * port)3452 static void mvpp2_isr_handle_xlg(struct mvpp2_port *port)
3453 {
3454 	bool link;
3455 	u32 val;
3456 
3457 	val = readl(port->base + MVPP22_XLG_INT_STAT);
3458 	if (val & MVPP22_XLG_INT_STAT_LINK) {
3459 		val = readl(port->base + MVPP22_XLG_STATUS);
3460 		link = (val & MVPP22_XLG_STATUS_LINK_UP);
3461 		mvpp2_isr_handle_link(port, &port->pcs_xlg, link);
3462 	}
3463 }
3464 
mvpp2_isr_handle_gmac_internal(struct mvpp2_port * port)3465 static void mvpp2_isr_handle_gmac_internal(struct mvpp2_port *port)
3466 {
3467 	bool link;
3468 	u32 val;
3469 
3470 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
3471 	    phy_interface_mode_is_8023z(port->phy_interface) ||
3472 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
3473 		val = readl(port->base + MVPP22_GMAC_INT_STAT);
3474 		if (val & MVPP22_GMAC_INT_STAT_LINK) {
3475 			val = readl(port->base + MVPP2_GMAC_STATUS0);
3476 			link = (val & MVPP2_GMAC_STATUS0_LINK_UP);
3477 			mvpp2_isr_handle_link(port, &port->pcs_gmac, link);
3478 		}
3479 	}
3480 }
3481 
3482 /* Per-port interrupt for link status changes */
mvpp2_port_isr(int irq,void * dev_id)3483 static irqreturn_t mvpp2_port_isr(int irq, void *dev_id)
3484 {
3485 	struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
3486 	u32 val;
3487 
3488 	mvpp22_gop_mask_irq(port);
3489 
3490 	if (mvpp2_port_supports_xlg(port) &&
3491 	    mvpp2_is_xlg(port->phy_interface)) {
3492 		/* Check the external status register */
3493 		val = readl(port->base + MVPP22_XLG_EXT_INT_STAT);
3494 		if (val & MVPP22_XLG_EXT_INT_STAT_XLG)
3495 			mvpp2_isr_handle_xlg(port);
3496 		if (val & MVPP22_XLG_EXT_INT_STAT_PTP)
3497 			mvpp2_isr_handle_ptp(port);
3498 	} else {
3499 		/* If it's not the XLG, we must be using the GMAC.
3500 		 * Check the summary status.
3501 		 */
3502 		val = readl(port->base + MVPP22_GMAC_INT_SUM_STAT);
3503 		if (val & MVPP22_GMAC_INT_SUM_STAT_INTERNAL)
3504 			mvpp2_isr_handle_gmac_internal(port);
3505 		if (val & MVPP22_GMAC_INT_SUM_STAT_PTP)
3506 			mvpp2_isr_handle_ptp(port);
3507 	}
3508 
3509 	mvpp22_gop_unmask_irq(port);
3510 	return IRQ_HANDLED;
3511 }
3512 
mvpp2_hr_timer_cb(struct hrtimer * timer)3513 static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
3514 {
3515 	struct net_device *dev;
3516 	struct mvpp2_port *port;
3517 	struct mvpp2_port_pcpu *port_pcpu;
3518 	unsigned int tx_todo, cause;
3519 
3520 	port_pcpu = container_of(timer, struct mvpp2_port_pcpu, tx_done_timer);
3521 	dev = port_pcpu->dev;
3522 
3523 	if (!netif_running(dev))
3524 		return HRTIMER_NORESTART;
3525 
3526 	port_pcpu->timer_scheduled = false;
3527 	port = netdev_priv(dev);
3528 
3529 	/* Process all the Tx queues */
3530 	cause = (1 << port->ntxqs) - 1;
3531 	tx_todo = mvpp2_tx_done(port, cause,
3532 				mvpp2_cpu_to_thread(port->priv, smp_processor_id()));
3533 
3534 	/* Set the timer in case not all the packets were processed */
3535 	if (tx_todo && !port_pcpu->timer_scheduled) {
3536 		port_pcpu->timer_scheduled = true;
3537 		hrtimer_forward_now(&port_pcpu->tx_done_timer,
3538 				    MVPP2_TXDONE_HRTIMER_PERIOD_NS);
3539 
3540 		return HRTIMER_RESTART;
3541 	}
3542 	return HRTIMER_NORESTART;
3543 }
3544 
3545 /* Main RX/TX processing routines */
3546 
3547 /* Display more error info */
mvpp2_rx_error(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)3548 static void mvpp2_rx_error(struct mvpp2_port *port,
3549 			   struct mvpp2_rx_desc *rx_desc)
3550 {
3551 	u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3552 	size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3553 	char *err_str = NULL;
3554 
3555 	switch (status & MVPP2_RXD_ERR_CODE_MASK) {
3556 	case MVPP2_RXD_ERR_CRC:
3557 		err_str = "crc";
3558 		break;
3559 	case MVPP2_RXD_ERR_OVERRUN:
3560 		err_str = "overrun";
3561 		break;
3562 	case MVPP2_RXD_ERR_RESOURCE:
3563 		err_str = "resource";
3564 		break;
3565 	}
3566 	if (err_str && net_ratelimit())
3567 		netdev_err(port->dev,
3568 			   "bad rx status %08x (%s error), size=%zu\n",
3569 			   status, err_str, sz);
3570 }
3571 
3572 /* Handle RX checksum offload */
mvpp2_rx_csum(struct mvpp2_port * port,u32 status)3573 static int mvpp2_rx_csum(struct mvpp2_port *port, u32 status)
3574 {
3575 	if (((status & MVPP2_RXD_L3_IP4) &&
3576 	     !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
3577 	    (status & MVPP2_RXD_L3_IP6))
3578 		if (((status & MVPP2_RXD_L4_UDP) ||
3579 		     (status & MVPP2_RXD_L4_TCP)) &&
3580 		     (status & MVPP2_RXD_L4_CSUM_OK))
3581 			return CHECKSUM_UNNECESSARY;
3582 
3583 	return CHECKSUM_NONE;
3584 }
3585 
3586 /* Allocate a new skb and add it to BM pool */
mvpp2_rx_refill(struct mvpp2_port * port,struct mvpp2_bm_pool * bm_pool,struct page_pool * page_pool,int pool)3587 static int mvpp2_rx_refill(struct mvpp2_port *port,
3588 			   struct mvpp2_bm_pool *bm_pool,
3589 			   struct page_pool *page_pool, int pool)
3590 {
3591 	dma_addr_t dma_addr;
3592 	phys_addr_t phys_addr;
3593 	void *buf;
3594 
3595 	buf = mvpp2_buf_alloc(port, bm_pool, page_pool,
3596 			      &dma_addr, &phys_addr, GFP_ATOMIC);
3597 	if (!buf)
3598 		return -ENOMEM;
3599 
3600 	mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3601 
3602 	return 0;
3603 }
3604 
3605 /* Handle tx checksum */
mvpp2_skb_tx_csum(struct mvpp2_port * port,struct sk_buff * skb)3606 static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
3607 {
3608 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3609 		int ip_hdr_len = 0;
3610 		u8 l4_proto;
3611 		__be16 l3_proto = vlan_get_protocol(skb);
3612 
3613 		if (l3_proto == htons(ETH_P_IP)) {
3614 			struct iphdr *ip4h = ip_hdr(skb);
3615 
3616 			/* Calculate IPv4 checksum and L4 checksum */
3617 			ip_hdr_len = ip4h->ihl;
3618 			l4_proto = ip4h->protocol;
3619 		} else if (l3_proto == htons(ETH_P_IPV6)) {
3620 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
3621 
3622 			/* Read l4_protocol from one of IPv6 extra headers */
3623 			if (skb_network_header_len(skb) > 0)
3624 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
3625 			l4_proto = ip6h->nexthdr;
3626 		} else {
3627 			return MVPP2_TXD_L4_CSUM_NOT;
3628 		}
3629 
3630 		return mvpp2_txq_desc_csum(skb_network_offset(skb),
3631 					   l3_proto, ip_hdr_len, l4_proto);
3632 	}
3633 
3634 	return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
3635 }
3636 
mvpp2_xdp_finish_tx(struct mvpp2_port * port,u16 txq_id,int nxmit,int nxmit_byte)3637 static void mvpp2_xdp_finish_tx(struct mvpp2_port *port, u16 txq_id, int nxmit, int nxmit_byte)
3638 {
3639 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3640 	struct mvpp2_tx_queue *aggr_txq;
3641 	struct mvpp2_txq_pcpu *txq_pcpu;
3642 	struct mvpp2_tx_queue *txq;
3643 	struct netdev_queue *nq;
3644 
3645 	txq = port->txqs[txq_id];
3646 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3647 	nq = netdev_get_tx_queue(port->dev, txq_id);
3648 	aggr_txq = &port->priv->aggr_txqs[thread];
3649 
3650 	txq_pcpu->reserved_num -= nxmit;
3651 	txq_pcpu->count += nxmit;
3652 	aggr_txq->count += nxmit;
3653 
3654 	/* Enable transmit */
3655 	wmb();
3656 	mvpp2_aggr_txq_pend_desc_add(port, nxmit);
3657 
3658 	if (txq_pcpu->count >= txq_pcpu->stop_threshold)
3659 		netif_tx_stop_queue(nq);
3660 
3661 	/* Finalize TX processing */
3662 	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
3663 		mvpp2_txq_done(port, txq, txq_pcpu);
3664 }
3665 
3666 static int
mvpp2_xdp_submit_frame(struct mvpp2_port * port,u16 txq_id,struct xdp_frame * xdpf,bool dma_map)3667 mvpp2_xdp_submit_frame(struct mvpp2_port *port, u16 txq_id,
3668 		       struct xdp_frame *xdpf, bool dma_map)
3669 {
3670 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3671 	u32 tx_cmd = MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE |
3672 		     MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
3673 	enum mvpp2_tx_buf_type buf_type;
3674 	struct mvpp2_txq_pcpu *txq_pcpu;
3675 	struct mvpp2_tx_queue *aggr_txq;
3676 	struct mvpp2_tx_desc *tx_desc;
3677 	struct mvpp2_tx_queue *txq;
3678 	int ret = MVPP2_XDP_TX;
3679 	dma_addr_t dma_addr;
3680 
3681 	txq = port->txqs[txq_id];
3682 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3683 	aggr_txq = &port->priv->aggr_txqs[thread];
3684 
3685 	/* Check number of available descriptors */
3686 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, 1) ||
3687 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, 1)) {
3688 		ret = MVPP2_XDP_DROPPED;
3689 		goto out;
3690 	}
3691 
3692 	/* Get a descriptor for the first part of the packet */
3693 	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3694 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3695 	mvpp2_txdesc_size_set(port, tx_desc, xdpf->len);
3696 
3697 	if (dma_map) {
3698 		/* XDP_REDIRECT or AF_XDP */
3699 		dma_addr = dma_map_single(port->dev->dev.parent, xdpf->data,
3700 					  xdpf->len, DMA_TO_DEVICE);
3701 
3702 		if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
3703 			mvpp2_txq_desc_put(txq);
3704 			ret = MVPP2_XDP_DROPPED;
3705 			goto out;
3706 		}
3707 
3708 		buf_type = MVPP2_TYPE_XDP_NDO;
3709 	} else {
3710 		/* XDP_TX */
3711 		struct page *page = virt_to_page(xdpf->data);
3712 
3713 		dma_addr = page_pool_get_dma_addr(page) +
3714 			   sizeof(*xdpf) + xdpf->headroom;
3715 		dma_sync_single_for_device(port->dev->dev.parent, dma_addr,
3716 					   xdpf->len, DMA_BIDIRECTIONAL);
3717 
3718 		buf_type = MVPP2_TYPE_XDP_TX;
3719 	}
3720 
3721 	mvpp2_txdesc_dma_addr_set(port, tx_desc, dma_addr);
3722 
3723 	mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
3724 	mvpp2_txq_inc_put(port, txq_pcpu, xdpf, tx_desc, buf_type);
3725 
3726 out:
3727 	return ret;
3728 }
3729 
3730 static int
mvpp2_xdp_xmit_back(struct mvpp2_port * port,struct xdp_buff * xdp)3731 mvpp2_xdp_xmit_back(struct mvpp2_port *port, struct xdp_buff *xdp)
3732 {
3733 	struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
3734 	struct xdp_frame *xdpf;
3735 	u16 txq_id;
3736 	int ret;
3737 
3738 	xdpf = xdp_convert_buff_to_frame(xdp);
3739 	if (unlikely(!xdpf))
3740 		return MVPP2_XDP_DROPPED;
3741 
3742 	/* The first of the TX queues are used for XPS,
3743 	 * the second half for XDP_TX
3744 	 */
3745 	txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);
3746 
3747 	ret = mvpp2_xdp_submit_frame(port, txq_id, xdpf, false);
3748 	if (ret == MVPP2_XDP_TX) {
3749 		u64_stats_update_begin(&stats->syncp);
3750 		stats->tx_bytes += xdpf->len;
3751 		stats->tx_packets++;
3752 		stats->xdp_tx++;
3753 		u64_stats_update_end(&stats->syncp);
3754 
3755 		mvpp2_xdp_finish_tx(port, txq_id, 1, xdpf->len);
3756 	} else {
3757 		u64_stats_update_begin(&stats->syncp);
3758 		stats->xdp_tx_err++;
3759 		u64_stats_update_end(&stats->syncp);
3760 	}
3761 
3762 	return ret;
3763 }
3764 
3765 static int
mvpp2_xdp_xmit(struct net_device * dev,int num_frame,struct xdp_frame ** frames,u32 flags)3766 mvpp2_xdp_xmit(struct net_device *dev, int num_frame,
3767 	       struct xdp_frame **frames, u32 flags)
3768 {
3769 	struct mvpp2_port *port = netdev_priv(dev);
3770 	int i, nxmit_byte = 0, nxmit = 0;
3771 	struct mvpp2_pcpu_stats *stats;
3772 	u16 txq_id;
3773 	u32 ret;
3774 
3775 	if (unlikely(test_bit(0, &port->state)))
3776 		return -ENETDOWN;
3777 
3778 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3779 		return -EINVAL;
3780 
3781 	/* The first of the TX queues are used for XPS,
3782 	 * the second half for XDP_TX
3783 	 */
3784 	txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);
3785 
3786 	for (i = 0; i < num_frame; i++) {
3787 		ret = mvpp2_xdp_submit_frame(port, txq_id, frames[i], true);
3788 		if (ret != MVPP2_XDP_TX)
3789 			break;
3790 
3791 		nxmit_byte += frames[i]->len;
3792 		nxmit++;
3793 	}
3794 
3795 	if (likely(nxmit > 0))
3796 		mvpp2_xdp_finish_tx(port, txq_id, nxmit, nxmit_byte);
3797 
3798 	stats = this_cpu_ptr(port->stats);
3799 	u64_stats_update_begin(&stats->syncp);
3800 	stats->tx_bytes += nxmit_byte;
3801 	stats->tx_packets += nxmit;
3802 	stats->xdp_xmit += nxmit;
3803 	stats->xdp_xmit_err += num_frame - nxmit;
3804 	u64_stats_update_end(&stats->syncp);
3805 
3806 	return nxmit;
3807 }
3808 
3809 static int
mvpp2_run_xdp(struct mvpp2_port * port,struct bpf_prog * prog,struct xdp_buff * xdp,struct page_pool * pp,struct mvpp2_pcpu_stats * stats)3810 mvpp2_run_xdp(struct mvpp2_port *port, struct bpf_prog *prog,
3811 	      struct xdp_buff *xdp, struct page_pool *pp,
3812 	      struct mvpp2_pcpu_stats *stats)
3813 {
3814 	unsigned int len, sync, err;
3815 	struct page *page;
3816 	u32 ret, act;
3817 
3818 	len = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
3819 	act = bpf_prog_run_xdp(prog, xdp);
3820 
3821 	/* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
3822 	sync = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
3823 	sync = max(sync, len);
3824 
3825 	switch (act) {
3826 	case XDP_PASS:
3827 		stats->xdp_pass++;
3828 		ret = MVPP2_XDP_PASS;
3829 		break;
3830 	case XDP_REDIRECT:
3831 		err = xdp_do_redirect(port->dev, xdp, prog);
3832 		if (unlikely(err)) {
3833 			ret = MVPP2_XDP_DROPPED;
3834 			page = virt_to_head_page(xdp->data);
3835 			page_pool_put_page(pp, page, sync, true);
3836 		} else {
3837 			ret = MVPP2_XDP_REDIR;
3838 			stats->xdp_redirect++;
3839 		}
3840 		break;
3841 	case XDP_TX:
3842 		ret = mvpp2_xdp_xmit_back(port, xdp);
3843 		if (ret != MVPP2_XDP_TX) {
3844 			page = virt_to_head_page(xdp->data);
3845 			page_pool_put_page(pp, page, sync, true);
3846 		}
3847 		break;
3848 	default:
3849 		bpf_warn_invalid_xdp_action(port->dev, prog, act);
3850 		fallthrough;
3851 	case XDP_ABORTED:
3852 		trace_xdp_exception(port->dev, prog, act);
3853 		fallthrough;
3854 	case XDP_DROP:
3855 		page = virt_to_head_page(xdp->data);
3856 		page_pool_put_page(pp, page, sync, true);
3857 		ret = MVPP2_XDP_DROPPED;
3858 		stats->xdp_drop++;
3859 		break;
3860 	}
3861 
3862 	return ret;
3863 }
3864 
mvpp2_buff_hdr_pool_put(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc,int pool,u32 rx_status)3865 static void mvpp2_buff_hdr_pool_put(struct mvpp2_port *port, struct mvpp2_rx_desc *rx_desc,
3866 				    int pool, u32 rx_status)
3867 {
3868 	phys_addr_t phys_addr, phys_addr_next;
3869 	dma_addr_t dma_addr, dma_addr_next;
3870 	struct mvpp2_buff_hdr *buff_hdr;
3871 
3872 	phys_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
3873 	dma_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
3874 
3875 	do {
3876 		buff_hdr = (struct mvpp2_buff_hdr *)phys_to_virt(phys_addr);
3877 
3878 		phys_addr_next = le32_to_cpu(buff_hdr->next_phys_addr);
3879 		dma_addr_next = le32_to_cpu(buff_hdr->next_dma_addr);
3880 
3881 		if (port->priv->hw_version >= MVPP22) {
3882 			phys_addr_next |= ((u64)buff_hdr->next_phys_addr_high << 32);
3883 			dma_addr_next |= ((u64)buff_hdr->next_dma_addr_high << 32);
3884 		}
3885 
3886 		mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3887 
3888 		phys_addr = phys_addr_next;
3889 		dma_addr = dma_addr_next;
3890 
3891 	} while (!MVPP2_B_HDR_INFO_IS_LAST(le16_to_cpu(buff_hdr->info)));
3892 }
3893 
3894 /* Main rx processing */
mvpp2_rx(struct mvpp2_port * port,struct napi_struct * napi,int rx_todo,struct mvpp2_rx_queue * rxq)3895 static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
3896 		    int rx_todo, struct mvpp2_rx_queue *rxq)
3897 {
3898 	struct net_device *dev = port->dev;
3899 	struct mvpp2_pcpu_stats ps = {};
3900 	enum dma_data_direction dma_dir;
3901 	struct bpf_prog *xdp_prog;
3902 	struct xdp_buff xdp;
3903 	int rx_received;
3904 	int rx_done = 0;
3905 	u32 xdp_ret = 0;
3906 
3907 	xdp_prog = READ_ONCE(port->xdp_prog);
3908 
3909 	/* Get number of received packets and clamp the to-do */
3910 	rx_received = mvpp2_rxq_received(port, rxq->id);
3911 	if (rx_todo > rx_received)
3912 		rx_todo = rx_received;
3913 
3914 	while (rx_done < rx_todo) {
3915 		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3916 		u32 rx_status, timestamp, metasize = 0;
3917 		struct mvpp2_bm_pool *bm_pool;
3918 		struct page_pool *pp = NULL;
3919 		struct sk_buff *skb;
3920 		unsigned int frag_size, rx_sync_size;
3921 		dma_addr_t dma_addr;
3922 		phys_addr_t phys_addr;
3923 		int pool, rx_bytes, rx_offset, err, ret;
3924 		struct page *page;
3925 		void *data;
3926 
3927 		phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
3928 		data = (void *)phys_to_virt(phys_addr);
3929 		page = virt_to_page(data);
3930 		prefetch(page);
3931 
3932 		rx_done++;
3933 		rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
3934 		rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
3935 		rx_bytes -= MVPP2_MH_SIZE;
3936 		rx_sync_size = rx_bytes + MVPP2_MH_SIZE;
3937 		rx_offset = MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM;
3938 		dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
3939 
3940 		pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
3941 			MVPP2_RXD_BM_POOL_ID_OFFS;
3942 		bm_pool = &port->priv->bm_pools[pool];
3943 
3944 		if (port->priv->percpu_pools) {
3945 			pp = port->priv->page_pool[pool];
3946 			dma_dir = page_pool_get_dma_dir(pp);
3947 		} else {
3948 			dma_dir = DMA_FROM_DEVICE;
3949 		}
3950 
3951 		dma_sync_single_range_for_cpu(dev->dev.parent, dma_addr,
3952 					      MVPP2_SKB_HEADROOM,
3953 					      rx_sync_size,
3954 					      dma_dir);
3955 
3956 		/* Buffer header not supported */
3957 		if (rx_status & MVPP2_RXD_BUF_HDR)
3958 			goto err_drop_frame;
3959 
3960 		/* In case of an error, release the requested buffer pointer
3961 		 * to the Buffer Manager. This request process is controlled
3962 		 * by the hardware, and the information about the buffer is
3963 		 * comprised by the RX descriptor.
3964 		 */
3965 		if (rx_status & MVPP2_RXD_ERR_SUMMARY)
3966 			goto err_drop_frame;
3967 
3968 		/* Prefetch header */
3969 		prefetch(data + MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
3970 
3971 		if (bm_pool->frag_size > PAGE_SIZE)
3972 			frag_size = 0;
3973 		else
3974 			frag_size = bm_pool->frag_size;
3975 
3976 		err = mvpp2_rx_refill(port, bm_pool, pp, pool);
3977 		if (err) {
3978 			netdev_err(port->dev, "failed to refill BM pools\n");
3979 			goto err_drop_frame;
3980 		}
3981 
3982 		if (xdp_prog) {
3983 			struct xdp_rxq_info *xdp_rxq;
3984 
3985 			if (bm_pool->pkt_size == MVPP2_BM_SHORT_PKT_SIZE)
3986 				xdp_rxq = &rxq->xdp_rxq_short;
3987 			else
3988 				xdp_rxq = &rxq->xdp_rxq_long;
3989 
3990 			xdp_init_buff(&xdp, bm_pool->frag_size, xdp_rxq);
3991 			xdp_prepare_buff(&xdp, data,
3992 					 MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM,
3993 					 rx_bytes, true);
3994 
3995 			ret = mvpp2_run_xdp(port, xdp_prog, &xdp, pp, &ps);
3996 
3997 			if (ret) {
3998 				xdp_ret |= ret;
3999 				ps.rx_packets++;
4000 				ps.rx_bytes += rx_bytes;
4001 				continue;
4002 			}
4003 
4004 			rx_sync_size = max_t(unsigned int, rx_sync_size,
4005 					     xdp.data_end - xdp.data_hard_start -
4006 					     MVPP2_SKB_HEADROOM);
4007 
4008 			/* Update offset and length to reflect any XDP adjustments. */
4009 			rx_offset = xdp.data     - data;
4010 			rx_bytes  = xdp.data_end - xdp.data;
4011 
4012 			metasize = xdp.data - xdp.data_meta;
4013 		}
4014 
4015 		if (frag_size)
4016 			skb = build_skb(data, frag_size);
4017 		else
4018 			skb = slab_build_skb(data);
4019 		if (!skb) {
4020 			netdev_warn(port->dev, "skb build failed\n");
4021 			if (pp) {
4022 				page_pool_put_page(pp, virt_to_head_page(data),
4023 						   rx_sync_size, true);
4024 			} else {
4025 				dma_unmap_single_attrs(dev->dev.parent, dma_addr,
4026 						       bm_pool->buf_size,
4027 						       DMA_FROM_DEVICE,
4028 						       DMA_ATTR_SKIP_CPU_SYNC);
4029 				mvpp2_frag_free(bm_pool, pp, data);
4030 			}
4031 			goto err_drop_frame_retired;
4032 		}
4033 		if (pp)
4034 			skb_mark_for_recycle(skb);
4035 
4036 		/* If we have RX hardware timestamping enabled, grab the
4037 		 * timestamp from the queue and convert.
4038 		 */
4039 		if (mvpp22_rx_hwtstamping(port)) {
4040 			timestamp = le32_to_cpu(rx_desc->pp22.timestamp);
4041 			mvpp22_tai_tstamp(port->priv->tai, timestamp,
4042 					 skb_hwtstamps(skb));
4043 		}
4044 
4045 		if (!pp)
4046 			dma_unmap_single_attrs(dev->dev.parent, dma_addr,
4047 					       bm_pool->buf_size, DMA_FROM_DEVICE,
4048 					       DMA_ATTR_SKIP_CPU_SYNC);
4049 
4050 		ps.rx_packets++;
4051 		ps.rx_bytes += rx_bytes;
4052 
4053 		skb_reserve(skb, rx_offset);
4054 		skb_put(skb, rx_bytes);
4055 		if (metasize)
4056 			skb_metadata_set(skb, metasize);
4057 		skb->ip_summed = mvpp2_rx_csum(port, rx_status);
4058 		skb->protocol = eth_type_trans(skb, dev);
4059 
4060 		napi_gro_receive(napi, skb);
4061 		continue;
4062 
4063 err_drop_frame:
4064 		/* Return the buffer to the pool */
4065 		if (rx_status & MVPP2_RXD_BUF_HDR)
4066 			mvpp2_buff_hdr_pool_put(port, rx_desc, pool, rx_status);
4067 		else
4068 			mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
4069 err_drop_frame_retired:
4070 		dev->stats.rx_errors++;
4071 		mvpp2_rx_error(port, rx_desc);
4072 	}
4073 
4074 	if (xdp_ret & MVPP2_XDP_REDIR)
4075 		xdp_do_flush();
4076 
4077 	if (ps.rx_packets) {
4078 		struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
4079 
4080 		u64_stats_update_begin(&stats->syncp);
4081 		stats->rx_packets += ps.rx_packets;
4082 		stats->rx_bytes   += ps.rx_bytes;
4083 		/* xdp */
4084 		stats->xdp_redirect += ps.xdp_redirect;
4085 		stats->xdp_pass += ps.xdp_pass;
4086 		stats->xdp_drop += ps.xdp_drop;
4087 		u64_stats_update_end(&stats->syncp);
4088 	}
4089 
4090 	/* Update Rx queue management counters */
4091 	wmb();
4092 	mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
4093 
4094 	return rx_todo;
4095 }
4096 
4097 static inline void
tx_desc_unmap_put(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_tx_desc * desc)4098 tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4099 		  struct mvpp2_tx_desc *desc)
4100 {
4101 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4102 	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4103 
4104 	dma_addr_t buf_dma_addr =
4105 		mvpp2_txdesc_dma_addr_get(port, desc);
4106 	size_t buf_sz =
4107 		mvpp2_txdesc_size_get(port, desc);
4108 	if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
4109 		dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
4110 				 buf_sz, DMA_TO_DEVICE);
4111 	mvpp2_txq_desc_put(txq);
4112 }
4113 
mvpp2_txdesc_clear_ptp(struct mvpp2_port * port,struct mvpp2_tx_desc * desc)4114 static void mvpp2_txdesc_clear_ptp(struct mvpp2_port *port,
4115 				   struct mvpp2_tx_desc *desc)
4116 {
4117 	/* We only need to clear the low bits */
4118 	if (port->priv->hw_version >= MVPP22)
4119 		desc->pp22.ptp_descriptor &=
4120 			cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
4121 }
4122 
mvpp2_tx_hw_tstamp(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,struct sk_buff * skb)4123 static bool mvpp2_tx_hw_tstamp(struct mvpp2_port *port,
4124 			       struct mvpp2_tx_desc *tx_desc,
4125 			       struct sk_buff *skb)
4126 {
4127 	struct mvpp2_hwtstamp_queue *queue;
4128 	unsigned int mtype, type, i;
4129 	struct ptp_header *hdr;
4130 	u64 ptpdesc;
4131 
4132 	if (port->priv->hw_version == MVPP21 ||
4133 	    port->tx_hwtstamp_type == HWTSTAMP_TX_OFF)
4134 		return false;
4135 
4136 	type = ptp_classify_raw(skb);
4137 	if (!type)
4138 		return false;
4139 
4140 	hdr = ptp_parse_header(skb, type);
4141 	if (!hdr)
4142 		return false;
4143 
4144 	skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4145 
4146 	ptpdesc = MVPP22_PTP_MACTIMESTAMPINGEN |
4147 		  MVPP22_PTP_ACTION_CAPTURE;
4148 	queue = &port->tx_hwtstamp_queue[0];
4149 
4150 	switch (type & PTP_CLASS_VMASK) {
4151 	case PTP_CLASS_V1:
4152 		ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV1);
4153 		break;
4154 
4155 	case PTP_CLASS_V2:
4156 		ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV2);
4157 		mtype = hdr->tsmt & 15;
4158 		/* Direct PTP Sync messages to queue 1 */
4159 		if (mtype == 0) {
4160 			ptpdesc |= MVPP22_PTP_TIMESTAMPQUEUESELECT;
4161 			queue = &port->tx_hwtstamp_queue[1];
4162 		}
4163 		break;
4164 	}
4165 
4166 	/* Take a reference on the skb and insert into our queue */
4167 	i = queue->next;
4168 	queue->next = (i + 1) & 31;
4169 	if (queue->skb[i])
4170 		dev_kfree_skb_any(queue->skb[i]);
4171 	queue->skb[i] = skb_get(skb);
4172 
4173 	ptpdesc |= MVPP22_PTP_TIMESTAMPENTRYID(i);
4174 
4175 	/*
4176 	 * 3:0		- PTPAction
4177 	 * 6:4		- PTPPacketFormat
4178 	 * 7		- PTP_CF_WraparoundCheckEn
4179 	 * 9:8		- IngressTimestampSeconds[1:0]
4180 	 * 10		- Reserved
4181 	 * 11		- MACTimestampingEn
4182 	 * 17:12	- PTP_TimestampQueueEntryID[5:0]
4183 	 * 18		- PTPTimestampQueueSelect
4184 	 * 19		- UDPChecksumUpdateEn
4185 	 * 27:20	- TimestampOffset
4186 	 *			PTP, NTPTransmit, OWAMP/TWAMP - L3 to PTP header
4187 	 *			NTPTs, Y.1731 - L3 to timestamp entry
4188 	 * 35:28	- UDP Checksum Offset
4189 	 *
4190 	 * stored in tx descriptor bits 75:64 (11:0) and 191:168 (35:12)
4191 	 */
4192 	tx_desc->pp22.ptp_descriptor &=
4193 		cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
4194 	tx_desc->pp22.ptp_descriptor |=
4195 		cpu_to_le32(ptpdesc & MVPP22_PTP_DESC_MASK_LOW);
4196 	tx_desc->pp22.buf_dma_addr_ptp &= cpu_to_le64(~0xffffff0000000000ULL);
4197 	tx_desc->pp22.buf_dma_addr_ptp |= cpu_to_le64((ptpdesc >> 12) << 40);
4198 
4199 	return true;
4200 }
4201 
4202 /* Handle tx fragmentation processing */
mvpp2_tx_frag_process(struct mvpp2_port * port,struct sk_buff * skb,struct mvpp2_tx_queue * aggr_txq,struct mvpp2_tx_queue * txq)4203 static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
4204 				 struct mvpp2_tx_queue *aggr_txq,
4205 				 struct mvpp2_tx_queue *txq)
4206 {
4207 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4208 	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4209 	struct mvpp2_tx_desc *tx_desc;
4210 	int i;
4211 	dma_addr_t buf_dma_addr;
4212 
4213 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4214 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4215 		void *addr = skb_frag_address(frag);
4216 
4217 		tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4218 		mvpp2_txdesc_clear_ptp(port, tx_desc);
4219 		mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4220 		mvpp2_txdesc_size_set(port, tx_desc, skb_frag_size(frag));
4221 
4222 		buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
4223 					      skb_frag_size(frag),
4224 					      DMA_TO_DEVICE);
4225 		if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
4226 			mvpp2_txq_desc_put(txq);
4227 			goto cleanup;
4228 		}
4229 
4230 		mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4231 
4232 		if (i == (skb_shinfo(skb)->nr_frags - 1)) {
4233 			/* Last descriptor */
4234 			mvpp2_txdesc_cmd_set(port, tx_desc,
4235 					     MVPP2_TXD_L_DESC);
4236 			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4237 		} else {
4238 			/* Descriptor in the middle: Not First, Not Last */
4239 			mvpp2_txdesc_cmd_set(port, tx_desc, 0);
4240 			mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4241 		}
4242 	}
4243 
4244 	return 0;
4245 cleanup:
4246 	/* Release all descriptors that were used to map fragments of
4247 	 * this packet, as well as the corresponding DMA mappings
4248 	 */
4249 	for (i = i - 1; i >= 0; i--) {
4250 		tx_desc = txq->descs + i;
4251 		tx_desc_unmap_put(port, txq, tx_desc);
4252 	}
4253 
4254 	return -ENOMEM;
4255 }
4256 
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)4257 static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
4258 				     struct net_device *dev,
4259 				     struct mvpp2_tx_queue *txq,
4260 				     struct mvpp2_tx_queue *aggr_txq,
4261 				     struct mvpp2_txq_pcpu *txq_pcpu,
4262 				     int hdr_sz)
4263 {
4264 	struct mvpp2_port *port = netdev_priv(dev);
4265 	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4266 	dma_addr_t addr;
4267 
4268 	mvpp2_txdesc_clear_ptp(port, tx_desc);
4269 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4270 	mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
4271 
4272 	addr = txq_pcpu->tso_headers_dma +
4273 	       txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
4274 	mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
4275 
4276 	mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
4277 					    MVPP2_TXD_F_DESC |
4278 					    MVPP2_TXD_PADDING_DISABLE);
4279 	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4280 }
4281 
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)4282 static inline int mvpp2_tso_put_data(struct sk_buff *skb,
4283 				     struct net_device *dev, struct tso_t *tso,
4284 				     struct mvpp2_tx_queue *txq,
4285 				     struct mvpp2_tx_queue *aggr_txq,
4286 				     struct mvpp2_txq_pcpu *txq_pcpu,
4287 				     int sz, bool left, bool last)
4288 {
4289 	struct mvpp2_port *port = netdev_priv(dev);
4290 	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4291 	dma_addr_t buf_dma_addr;
4292 
4293 	mvpp2_txdesc_clear_ptp(port, tx_desc);
4294 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4295 	mvpp2_txdesc_size_set(port, tx_desc, sz);
4296 
4297 	buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
4298 				      DMA_TO_DEVICE);
4299 	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
4300 		mvpp2_txq_desc_put(txq);
4301 		return -ENOMEM;
4302 	}
4303 
4304 	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4305 
4306 	if (!left) {
4307 		mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
4308 		if (last) {
4309 			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4310 			return 0;
4311 		}
4312 	} else {
4313 		mvpp2_txdesc_cmd_set(port, tx_desc, 0);
4314 	}
4315 
4316 	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4317 	return 0;
4318 }
4319 
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)4320 static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
4321 			struct mvpp2_tx_queue *txq,
4322 			struct mvpp2_tx_queue *aggr_txq,
4323 			struct mvpp2_txq_pcpu *txq_pcpu)
4324 {
4325 	struct mvpp2_port *port = netdev_priv(dev);
4326 	int hdr_sz, i, len, descs = 0;
4327 	struct tso_t tso;
4328 
4329 	/* Check number of available descriptors */
4330 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, tso_count_descs(skb)) ||
4331 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu,
4332 					     tso_count_descs(skb)))
4333 		return 0;
4334 
4335 	hdr_sz = tso_start(skb, &tso);
4336 
4337 	len = skb->len - hdr_sz;
4338 	while (len > 0) {
4339 		int left = min_t(int, skb_shinfo(skb)->gso_size, len);
4340 		char *hdr = txq_pcpu->tso_headers +
4341 			    txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
4342 
4343 		len -= left;
4344 		descs++;
4345 
4346 		tso_build_hdr(skb, hdr, &tso, left, len == 0);
4347 		mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
4348 
4349 		while (left > 0) {
4350 			int sz = min_t(int, tso.size, left);
4351 			left -= sz;
4352 			descs++;
4353 
4354 			if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
4355 					       txq_pcpu, sz, left, len == 0))
4356 				goto release;
4357 			tso_build_data(skb, &tso, sz);
4358 		}
4359 	}
4360 
4361 	return descs;
4362 
4363 release:
4364 	for (i = descs - 1; i >= 0; i--) {
4365 		struct mvpp2_tx_desc *tx_desc = txq->descs + i;
4366 		tx_desc_unmap_put(port, txq, tx_desc);
4367 	}
4368 	return 0;
4369 }
4370 
4371 /* Main tx processing */
mvpp2_tx(struct sk_buff * skb,struct net_device * dev)4372 static netdev_tx_t mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
4373 {
4374 	struct mvpp2_port *port = netdev_priv(dev);
4375 	struct mvpp2_tx_queue *txq, *aggr_txq;
4376 	struct mvpp2_txq_pcpu *txq_pcpu;
4377 	struct mvpp2_tx_desc *tx_desc;
4378 	dma_addr_t buf_dma_addr;
4379 	unsigned long flags = 0;
4380 	unsigned int thread;
4381 	int frags = 0;
4382 	u16 txq_id;
4383 	u32 tx_cmd;
4384 
4385 	thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4386 
4387 	txq_id = skb_get_queue_mapping(skb);
4388 	txq = port->txqs[txq_id];
4389 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4390 	aggr_txq = &port->priv->aggr_txqs[thread];
4391 
4392 	if (test_bit(thread, &port->priv->lock_map))
4393 		spin_lock_irqsave(&port->tx_lock[thread], flags);
4394 
4395 	if (skb_is_gso(skb)) {
4396 		frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
4397 		goto out;
4398 	}
4399 	frags = skb_shinfo(skb)->nr_frags + 1;
4400 
4401 	/* Check number of available descriptors */
4402 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, frags) ||
4403 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, frags)) {
4404 		frags = 0;
4405 		goto out;
4406 	}
4407 
4408 	/* Get a descriptor for the first part of the packet */
4409 	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4410 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
4411 	    !mvpp2_tx_hw_tstamp(port, tx_desc, skb))
4412 		mvpp2_txdesc_clear_ptp(port, tx_desc);
4413 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4414 	mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
4415 
4416 	buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
4417 				      skb_headlen(skb), DMA_TO_DEVICE);
4418 	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
4419 		mvpp2_txq_desc_put(txq);
4420 		frags = 0;
4421 		goto out;
4422 	}
4423 
4424 	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4425 
4426 	tx_cmd = mvpp2_skb_tx_csum(port, skb);
4427 
4428 	if (frags == 1) {
4429 		/* First and Last descriptor */
4430 		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
4431 		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
4432 		mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4433 	} else {
4434 		/* First but not Last */
4435 		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
4436 		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
4437 		mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4438 
4439 		/* Continue with other skb fragments */
4440 		if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
4441 			tx_desc_unmap_put(port, txq, tx_desc);
4442 			frags = 0;
4443 		}
4444 	}
4445 
4446 out:
4447 	if (frags > 0) {
4448 		struct mvpp2_pcpu_stats *stats = per_cpu_ptr(port->stats, thread);
4449 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
4450 
4451 		txq_pcpu->reserved_num -= frags;
4452 		txq_pcpu->count += frags;
4453 		aggr_txq->count += frags;
4454 
4455 		skb_tx_timestamp(skb);
4456 
4457 		/* Enable transmit */
4458 		wmb();
4459 		mvpp2_aggr_txq_pend_desc_add(port, frags);
4460 
4461 		if (txq_pcpu->count >= txq_pcpu->stop_threshold)
4462 			netif_tx_stop_queue(nq);
4463 
4464 		u64_stats_update_begin(&stats->syncp);
4465 		stats->tx_packets++;
4466 		stats->tx_bytes += skb->len;
4467 		u64_stats_update_end(&stats->syncp);
4468 	} else {
4469 		dev->stats.tx_dropped++;
4470 		dev_kfree_skb_any(skb);
4471 	}
4472 
4473 	/* Finalize TX processing */
4474 	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
4475 		mvpp2_txq_done(port, txq, txq_pcpu);
4476 
4477 	/* Set the timer in case not all frags were processed */
4478 	if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
4479 	    txq_pcpu->count > 0) {
4480 		struct mvpp2_port_pcpu *port_pcpu = per_cpu_ptr(port->pcpu, thread);
4481 
4482 		if (!port_pcpu->timer_scheduled) {
4483 			port_pcpu->timer_scheduled = true;
4484 			hrtimer_start(&port_pcpu->tx_done_timer,
4485 				      MVPP2_TXDONE_HRTIMER_PERIOD_NS,
4486 				      HRTIMER_MODE_REL_PINNED_SOFT);
4487 		}
4488 	}
4489 
4490 	if (test_bit(thread, &port->priv->lock_map))
4491 		spin_unlock_irqrestore(&port->tx_lock[thread], flags);
4492 
4493 	return NETDEV_TX_OK;
4494 }
4495 
mvpp2_cause_error(struct net_device * dev,int cause)4496 static inline void mvpp2_cause_error(struct net_device *dev, int cause)
4497 {
4498 	if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
4499 		netdev_err(dev, "FCS error\n");
4500 	if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
4501 		netdev_err(dev, "rx fifo overrun error\n");
4502 	if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
4503 		netdev_err(dev, "tx fifo underrun error\n");
4504 }
4505 
mvpp2_poll(struct napi_struct * napi,int budget)4506 static int mvpp2_poll(struct napi_struct *napi, int budget)
4507 {
4508 	u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
4509 	int rx_done = 0;
4510 	struct mvpp2_port *port = netdev_priv(napi->dev);
4511 	struct mvpp2_queue_vector *qv;
4512 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4513 
4514 	qv = container_of(napi, struct mvpp2_queue_vector, napi);
4515 
4516 	/* Rx/Tx cause register
4517 	 *
4518 	 * Bits 0-15: each bit indicates received packets on the Rx queue
4519 	 * (bit 0 is for Rx queue 0).
4520 	 *
4521 	 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
4522 	 * (bit 16 is for Tx queue 0).
4523 	 *
4524 	 * Each CPU has its own Rx/Tx cause register
4525 	 */
4526 	cause_rx_tx = mvpp2_thread_read_relaxed(port->priv, qv->sw_thread_id,
4527 						MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
4528 
4529 	cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
4530 	if (cause_misc) {
4531 		mvpp2_cause_error(port->dev, cause_misc);
4532 
4533 		/* Clear the cause register */
4534 		mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
4535 		mvpp2_thread_write(port->priv, thread,
4536 				   MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
4537 				   cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
4538 	}
4539 
4540 	if (port->has_tx_irqs) {
4541 		cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
4542 		if (cause_tx) {
4543 			cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
4544 			mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
4545 		}
4546 	}
4547 
4548 	/* Process RX packets */
4549 	cause_rx = cause_rx_tx &
4550 		   MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
4551 	cause_rx <<= qv->first_rxq;
4552 	cause_rx |= qv->pending_cause_rx;
4553 	while (cause_rx && budget > 0) {
4554 		int count;
4555 		struct mvpp2_rx_queue *rxq;
4556 
4557 		rxq = mvpp2_get_rx_queue(port, cause_rx);
4558 		if (!rxq)
4559 			break;
4560 
4561 		count = mvpp2_rx(port, napi, budget, rxq);
4562 		rx_done += count;
4563 		budget -= count;
4564 		if (budget > 0) {
4565 			/* Clear the bit associated to this Rx queue
4566 			 * so that next iteration will continue from
4567 			 * the next Rx queue.
4568 			 */
4569 			cause_rx &= ~(1 << rxq->logic_rxq);
4570 		}
4571 	}
4572 
4573 	if (budget > 0) {
4574 		cause_rx = 0;
4575 		napi_complete_done(napi, rx_done);
4576 
4577 		mvpp2_qvec_interrupt_enable(qv);
4578 	}
4579 	qv->pending_cause_rx = cause_rx;
4580 	return rx_done;
4581 }
4582 
mvpp22_mode_reconfigure(struct mvpp2_port * port,phy_interface_t interface)4583 static void mvpp22_mode_reconfigure(struct mvpp2_port *port,
4584 				    phy_interface_t interface)
4585 {
4586 	u32 ctrl3;
4587 
4588 	/* Set the GMAC & XLG MAC in reset */
4589 	mvpp2_mac_reset_assert(port);
4590 
4591 	/* Set the MPCS and XPCS in reset */
4592 	mvpp22_pcs_reset_assert(port);
4593 
4594 	/* comphy reconfiguration */
4595 	mvpp22_comphy_init(port, interface);
4596 
4597 	/* gop reconfiguration */
4598 	mvpp22_gop_init(port, interface);
4599 
4600 	mvpp22_pcs_reset_deassert(port, interface);
4601 
4602 	if (mvpp2_port_supports_xlg(port)) {
4603 		ctrl3 = readl(port->base + MVPP22_XLG_CTRL3_REG);
4604 		ctrl3 &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
4605 
4606 		if (mvpp2_is_xlg(interface))
4607 			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4608 		else
4609 			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4610 
4611 		writel(ctrl3, port->base + MVPP22_XLG_CTRL3_REG);
4612 	}
4613 
4614 	if (mvpp2_port_supports_xlg(port) && mvpp2_is_xlg(interface))
4615 		mvpp2_xlg_max_rx_size_set(port);
4616 	else
4617 		mvpp2_gmac_max_rx_size_set(port);
4618 }
4619 
4620 /* Set hw internals when starting port */
mvpp2_start_dev(struct mvpp2_port * port)4621 static void mvpp2_start_dev(struct mvpp2_port *port)
4622 {
4623 	int i;
4624 
4625 	mvpp2_txp_max_tx_size_set(port);
4626 
4627 	for (i = 0; i < port->nqvecs; i++)
4628 		napi_enable(&port->qvecs[i].napi);
4629 
4630 	/* Enable interrupts on all threads */
4631 	mvpp2_interrupts_enable(port);
4632 
4633 	if (port->priv->hw_version >= MVPP22)
4634 		mvpp22_mode_reconfigure(port, port->phy_interface);
4635 
4636 	if (port->phylink) {
4637 		phylink_start(port->phylink);
4638 	} else {
4639 		mvpp2_acpi_start(port);
4640 	}
4641 
4642 	netif_tx_start_all_queues(port->dev);
4643 
4644 	clear_bit(0, &port->state);
4645 }
4646 
4647 /* Set hw internals when stopping port */
mvpp2_stop_dev(struct mvpp2_port * port)4648 static void mvpp2_stop_dev(struct mvpp2_port *port)
4649 {
4650 	int i;
4651 
4652 	set_bit(0, &port->state);
4653 
4654 	/* Disable interrupts on all threads */
4655 	mvpp2_interrupts_disable(port);
4656 
4657 	for (i = 0; i < port->nqvecs; i++)
4658 		napi_disable(&port->qvecs[i].napi);
4659 
4660 	if (port->phylink)
4661 		phylink_stop(port->phylink);
4662 	phy_power_off(port->comphy);
4663 }
4664 
mvpp2_check_ringparam_valid(struct net_device * dev,struct ethtool_ringparam * ring)4665 static int mvpp2_check_ringparam_valid(struct net_device *dev,
4666 				       struct ethtool_ringparam *ring)
4667 {
4668 	u16 new_rx_pending = ring->rx_pending;
4669 	u16 new_tx_pending = ring->tx_pending;
4670 
4671 	if (ring->rx_pending == 0 || ring->tx_pending == 0)
4672 		return -EINVAL;
4673 
4674 	if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
4675 		new_rx_pending = MVPP2_MAX_RXD_MAX;
4676 	else if (ring->rx_pending < MSS_THRESHOLD_START)
4677 		new_rx_pending = MSS_THRESHOLD_START;
4678 	else if (!IS_ALIGNED(ring->rx_pending, 16))
4679 		new_rx_pending = ALIGN(ring->rx_pending, 16);
4680 
4681 	if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
4682 		new_tx_pending = MVPP2_MAX_TXD_MAX;
4683 	else if (!IS_ALIGNED(ring->tx_pending, 32))
4684 		new_tx_pending = ALIGN(ring->tx_pending, 32);
4685 
4686 	/* The Tx ring size cannot be smaller than the minimum number of
4687 	 * descriptors needed for TSO.
4688 	 */
4689 	if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
4690 		new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
4691 
4692 	if (ring->rx_pending != new_rx_pending) {
4693 		netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
4694 			    ring->rx_pending, new_rx_pending);
4695 		ring->rx_pending = new_rx_pending;
4696 	}
4697 
4698 	if (ring->tx_pending != new_tx_pending) {
4699 		netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
4700 			    ring->tx_pending, new_tx_pending);
4701 		ring->tx_pending = new_tx_pending;
4702 	}
4703 
4704 	return 0;
4705 }
4706 
mvpp21_get_mac_address(struct mvpp2_port * port,unsigned char * addr)4707 static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
4708 {
4709 	u32 mac_addr_l, mac_addr_m, mac_addr_h;
4710 
4711 	mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4712 	mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
4713 	mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
4714 	addr[0] = (mac_addr_h >> 24) & 0xFF;
4715 	addr[1] = (mac_addr_h >> 16) & 0xFF;
4716 	addr[2] = (mac_addr_h >> 8) & 0xFF;
4717 	addr[3] = mac_addr_h & 0xFF;
4718 	addr[4] = mac_addr_m & 0xFF;
4719 	addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
4720 }
4721 
mvpp2_irqs_init(struct mvpp2_port * port)4722 static int mvpp2_irqs_init(struct mvpp2_port *port)
4723 {
4724 	int err, i;
4725 
4726 	for (i = 0; i < port->nqvecs; i++) {
4727 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4728 
4729 		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
4730 			qv->mask = kzalloc(cpumask_size(), GFP_KERNEL);
4731 			if (!qv->mask) {
4732 				err = -ENOMEM;
4733 				goto err;
4734 			}
4735 
4736 			irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
4737 		}
4738 
4739 		err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
4740 		if (err)
4741 			goto err;
4742 
4743 		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
4744 			unsigned int cpu;
4745 
4746 			for_each_present_cpu(cpu) {
4747 				if (mvpp2_cpu_to_thread(port->priv, cpu) ==
4748 				    qv->sw_thread_id)
4749 					cpumask_set_cpu(cpu, qv->mask);
4750 			}
4751 
4752 			irq_set_affinity_hint(qv->irq, qv->mask);
4753 		}
4754 	}
4755 
4756 	return 0;
4757 err:
4758 	for (i = 0; i < port->nqvecs; i++) {
4759 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4760 
4761 		irq_set_affinity_hint(qv->irq, NULL);
4762 		kfree(qv->mask);
4763 		qv->mask = NULL;
4764 		free_irq(qv->irq, qv);
4765 	}
4766 
4767 	return err;
4768 }
4769 
mvpp2_irqs_deinit(struct mvpp2_port * port)4770 static void mvpp2_irqs_deinit(struct mvpp2_port *port)
4771 {
4772 	int i;
4773 
4774 	for (i = 0; i < port->nqvecs; i++) {
4775 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4776 
4777 		irq_set_affinity_hint(qv->irq, NULL);
4778 		kfree(qv->mask);
4779 		qv->mask = NULL;
4780 		irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
4781 		free_irq(qv->irq, qv);
4782 	}
4783 }
4784 
mvpp22_rss_is_supported(struct mvpp2_port * port)4785 static bool mvpp22_rss_is_supported(struct mvpp2_port *port)
4786 {
4787 	return (queue_mode == MVPP2_QDIST_MULTI_MODE) &&
4788 		!(port->flags & MVPP2_F_LOOPBACK);
4789 }
4790 
mvpp2_open(struct net_device * dev)4791 static int mvpp2_open(struct net_device *dev)
4792 {
4793 	struct mvpp2_port *port = netdev_priv(dev);
4794 	struct mvpp2 *priv = port->priv;
4795 	unsigned char mac_bcast[ETH_ALEN] = {
4796 			0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4797 	bool valid = false;
4798 	int err;
4799 
4800 	err = mvpp2_prs_mac_da_accept(port, mac_bcast, true);
4801 	if (err) {
4802 		netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
4803 		return err;
4804 	}
4805 	err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, true);
4806 	if (err) {
4807 		netdev_err(dev, "mvpp2_prs_mac_da_accept own addr failed\n");
4808 		return err;
4809 	}
4810 	err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
4811 	if (err) {
4812 		netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
4813 		return err;
4814 	}
4815 	err = mvpp2_prs_def_flow(port);
4816 	if (err) {
4817 		netdev_err(dev, "mvpp2_prs_def_flow failed\n");
4818 		return err;
4819 	}
4820 
4821 	/* Allocate the Rx/Tx queues */
4822 	err = mvpp2_setup_rxqs(port);
4823 	if (err) {
4824 		netdev_err(port->dev, "cannot allocate Rx queues\n");
4825 		return err;
4826 	}
4827 
4828 	err = mvpp2_setup_txqs(port);
4829 	if (err) {
4830 		netdev_err(port->dev, "cannot allocate Tx queues\n");
4831 		goto err_cleanup_rxqs;
4832 	}
4833 
4834 	err = mvpp2_irqs_init(port);
4835 	if (err) {
4836 		netdev_err(port->dev, "cannot init IRQs\n");
4837 		goto err_cleanup_txqs;
4838 	}
4839 
4840 	if (port->phylink) {
4841 		err = phylink_fwnode_phy_connect(port->phylink, port->fwnode, 0);
4842 		if (err) {
4843 			netdev_err(port->dev, "could not attach PHY (%d)\n",
4844 				   err);
4845 			goto err_free_irq;
4846 		}
4847 
4848 		valid = true;
4849 	}
4850 
4851 	if (priv->hw_version >= MVPP22 && port->port_irq) {
4852 		err = request_irq(port->port_irq, mvpp2_port_isr, 0,
4853 				  dev->name, port);
4854 		if (err) {
4855 			netdev_err(port->dev,
4856 				   "cannot request port link/ptp IRQ %d\n",
4857 				   port->port_irq);
4858 			goto err_free_irq;
4859 		}
4860 
4861 		mvpp22_gop_setup_irq(port);
4862 
4863 		/* In default link is down */
4864 		netif_carrier_off(port->dev);
4865 
4866 		valid = true;
4867 	} else {
4868 		port->port_irq = 0;
4869 	}
4870 
4871 	if (!valid) {
4872 		netdev_err(port->dev,
4873 			   "invalid configuration: no dt or link IRQ");
4874 		err = -ENOENT;
4875 		goto err_free_irq;
4876 	}
4877 
4878 	/* Unmask interrupts on all CPUs */
4879 	on_each_cpu(mvpp2_interrupts_unmask, port, 1);
4880 	mvpp2_shared_interrupt_mask_unmask(port, false);
4881 
4882 	mvpp2_start_dev(port);
4883 
4884 	/* Start hardware statistics gathering */
4885 	queue_delayed_work(priv->stats_queue, &port->stats_work,
4886 			   MVPP2_MIB_COUNTERS_STATS_DELAY);
4887 
4888 	return 0;
4889 
4890 err_free_irq:
4891 	mvpp2_irqs_deinit(port);
4892 err_cleanup_txqs:
4893 	mvpp2_cleanup_txqs(port);
4894 err_cleanup_rxqs:
4895 	mvpp2_cleanup_rxqs(port);
4896 	return err;
4897 }
4898 
mvpp2_stop(struct net_device * dev)4899 static int mvpp2_stop(struct net_device *dev)
4900 {
4901 	struct mvpp2_port *port = netdev_priv(dev);
4902 	struct mvpp2_port_pcpu *port_pcpu;
4903 	unsigned int thread;
4904 
4905 	mvpp2_stop_dev(port);
4906 
4907 	/* Mask interrupts on all threads */
4908 	on_each_cpu(mvpp2_interrupts_mask, port, 1);
4909 	mvpp2_shared_interrupt_mask_unmask(port, true);
4910 
4911 	if (port->phylink)
4912 		phylink_disconnect_phy(port->phylink);
4913 	if (port->port_irq)
4914 		free_irq(port->port_irq, port);
4915 
4916 	mvpp2_irqs_deinit(port);
4917 	if (!port->has_tx_irqs) {
4918 		for (thread = 0; thread < port->priv->nthreads; thread++) {
4919 			port_pcpu = per_cpu_ptr(port->pcpu, thread);
4920 
4921 			hrtimer_cancel(&port_pcpu->tx_done_timer);
4922 			port_pcpu->timer_scheduled = false;
4923 		}
4924 	}
4925 	mvpp2_cleanup_rxqs(port);
4926 	mvpp2_cleanup_txqs(port);
4927 
4928 	cancel_delayed_work_sync(&port->stats_work);
4929 
4930 	mvpp2_mac_reset_assert(port);
4931 	mvpp22_pcs_reset_assert(port);
4932 
4933 	return 0;
4934 }
4935 
mvpp2_prs_mac_da_accept_list(struct mvpp2_port * port,struct netdev_hw_addr_list * list)4936 static int mvpp2_prs_mac_da_accept_list(struct mvpp2_port *port,
4937 					struct netdev_hw_addr_list *list)
4938 {
4939 	struct netdev_hw_addr *ha;
4940 	int ret;
4941 
4942 	netdev_hw_addr_list_for_each(ha, list) {
4943 		ret = mvpp2_prs_mac_da_accept(port, ha->addr, true);
4944 		if (ret)
4945 			return ret;
4946 	}
4947 
4948 	return 0;
4949 }
4950 
mvpp2_set_rx_promisc(struct mvpp2_port * port,bool enable)4951 static void mvpp2_set_rx_promisc(struct mvpp2_port *port, bool enable)
4952 {
4953 	if (!enable && (port->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
4954 		mvpp2_prs_vid_enable_filtering(port);
4955 	else
4956 		mvpp2_prs_vid_disable_filtering(port);
4957 
4958 	mvpp2_prs_mac_promisc_set(port->priv, port->id,
4959 				  MVPP2_PRS_L2_UNI_CAST, enable);
4960 
4961 	mvpp2_prs_mac_promisc_set(port->priv, port->id,
4962 				  MVPP2_PRS_L2_MULTI_CAST, enable);
4963 }
4964 
mvpp2_set_rx_mode(struct net_device * dev)4965 static void mvpp2_set_rx_mode(struct net_device *dev)
4966 {
4967 	struct mvpp2_port *port = netdev_priv(dev);
4968 
4969 	/* Clear the whole UC and MC list */
4970 	mvpp2_prs_mac_del_all(port);
4971 
4972 	if (dev->flags & IFF_PROMISC) {
4973 		mvpp2_set_rx_promisc(port, true);
4974 		return;
4975 	}
4976 
4977 	mvpp2_set_rx_promisc(port, false);
4978 
4979 	if (netdev_uc_count(dev) > MVPP2_PRS_MAC_UC_FILT_MAX ||
4980 	    mvpp2_prs_mac_da_accept_list(port, &dev->uc))
4981 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
4982 					  MVPP2_PRS_L2_UNI_CAST, true);
4983 
4984 	if (dev->flags & IFF_ALLMULTI) {
4985 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
4986 					  MVPP2_PRS_L2_MULTI_CAST, true);
4987 		return;
4988 	}
4989 
4990 	if (netdev_mc_count(dev) > MVPP2_PRS_MAC_MC_FILT_MAX ||
4991 	    mvpp2_prs_mac_da_accept_list(port, &dev->mc))
4992 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
4993 					  MVPP2_PRS_L2_MULTI_CAST, true);
4994 }
4995 
mvpp2_set_mac_address(struct net_device * dev,void * p)4996 static int mvpp2_set_mac_address(struct net_device *dev, void *p)
4997 {
4998 	const struct sockaddr *addr = p;
4999 	int err;
5000 
5001 	if (!is_valid_ether_addr(addr->sa_data))
5002 		return -EADDRNOTAVAIL;
5003 
5004 	err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
5005 	if (err) {
5006 		/* Reconfigure parser accept the original MAC address */
5007 		mvpp2_prs_update_mac_da(dev, dev->dev_addr);
5008 		netdev_err(dev, "failed to change MAC address\n");
5009 	}
5010 	return err;
5011 }
5012 
5013 /* Shut down all the ports, reconfigure the pools as percpu or shared,
5014  * then bring up again all ports.
5015  */
mvpp2_bm_switch_buffers(struct mvpp2 * priv,bool percpu)5016 static int mvpp2_bm_switch_buffers(struct mvpp2 *priv, bool percpu)
5017 {
5018 	bool change_percpu = (percpu != priv->percpu_pools);
5019 	int numbufs = MVPP2_BM_POOLS_NUM, i;
5020 	struct mvpp2_port *port = NULL;
5021 	bool status[MVPP2_MAX_PORTS];
5022 
5023 	for (i = 0; i < priv->port_count; i++) {
5024 		port = priv->port_list[i];
5025 		status[i] = netif_running(port->dev);
5026 		if (status[i])
5027 			mvpp2_stop(port->dev);
5028 	}
5029 
5030 	/* nrxqs is the same for all ports */
5031 	if (priv->percpu_pools)
5032 		numbufs = port->nrxqs * 2;
5033 
5034 	if (change_percpu && priv->global_tx_fc)
5035 		mvpp2_bm_pool_update_priv_fc(priv, false);
5036 
5037 	for (i = 0; i < numbufs; i++)
5038 		mvpp2_bm_pool_destroy(port->dev->dev.parent, priv, &priv->bm_pools[i]);
5039 
5040 	devm_kfree(port->dev->dev.parent, priv->bm_pools);
5041 	priv->percpu_pools = percpu;
5042 	mvpp2_bm_init(port->dev->dev.parent, priv);
5043 
5044 	for (i = 0; i < priv->port_count; i++) {
5045 		port = priv->port_list[i];
5046 		if (percpu && port->ntxqs >= num_possible_cpus() * 2)
5047 			xdp_set_features_flag(port->dev,
5048 					      NETDEV_XDP_ACT_BASIC |
5049 					      NETDEV_XDP_ACT_REDIRECT |
5050 					      NETDEV_XDP_ACT_NDO_XMIT);
5051 		else
5052 			xdp_clear_features_flag(port->dev);
5053 
5054 		mvpp2_swf_bm_pool_init(port);
5055 		if (status[i])
5056 			mvpp2_open(port->dev);
5057 	}
5058 
5059 	if (change_percpu && priv->global_tx_fc)
5060 		mvpp2_bm_pool_update_priv_fc(priv, true);
5061 
5062 	return 0;
5063 }
5064 
mvpp2_change_mtu(struct net_device * dev,int mtu)5065 static int mvpp2_change_mtu(struct net_device *dev, int mtu)
5066 {
5067 	struct mvpp2_port *port = netdev_priv(dev);
5068 	bool running = netif_running(dev);
5069 	struct mvpp2 *priv = port->priv;
5070 	int err;
5071 
5072 	if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
5073 		netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
5074 			    ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
5075 		mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
5076 	}
5077 
5078 	if (port->xdp_prog && mtu > MVPP2_MAX_RX_BUF_SIZE) {
5079 		netdev_err(dev, "Illegal MTU value %d (> %d) for XDP mode\n",
5080 			   mtu, (int)MVPP2_MAX_RX_BUF_SIZE);
5081 		return -EINVAL;
5082 	}
5083 
5084 	if (MVPP2_RX_PKT_SIZE(mtu) > MVPP2_BM_LONG_PKT_SIZE) {
5085 		if (priv->percpu_pools) {
5086 			netdev_warn(dev, "mtu %d too high, switching to shared buffers", mtu);
5087 			mvpp2_bm_switch_buffers(priv, false);
5088 		}
5089 	} else if (priv->hw_version >= MVPP22 &&
5090 		   mvpp2_get_nrxqs(priv) * 2 <= MVPP2_BM_MAX_POOLS) {
5091 		bool jumbo = false;
5092 		int i;
5093 
5094 		for (i = 0; i < priv->port_count; i++)
5095 			if (priv->port_list[i] != port &&
5096 			    MVPP2_RX_PKT_SIZE(priv->port_list[i]->dev->mtu) >
5097 			    MVPP2_BM_LONG_PKT_SIZE) {
5098 				jumbo = true;
5099 				break;
5100 			}
5101 
5102 		/* No port is using jumbo frames */
5103 		if (!jumbo) {
5104 			dev_info(port->dev->dev.parent,
5105 				 "all ports have a low MTU, switching to per-cpu buffers");
5106 			mvpp2_bm_switch_buffers(priv, true);
5107 		}
5108 	}
5109 
5110 	if (running)
5111 		mvpp2_stop_dev(port);
5112 
5113 	err = mvpp2_bm_update_mtu(dev, mtu);
5114 	if (err) {
5115 		netdev_err(dev, "failed to change MTU\n");
5116 		/* Reconfigure BM to the original MTU */
5117 		mvpp2_bm_update_mtu(dev, dev->mtu);
5118 	} else {
5119 		port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
5120 	}
5121 
5122 	if (running) {
5123 		mvpp2_start_dev(port);
5124 		mvpp2_egress_enable(port);
5125 		mvpp2_ingress_enable(port);
5126 	}
5127 
5128 	return err;
5129 }
5130 
mvpp2_check_pagepool_dma(struct mvpp2_port * port)5131 static int mvpp2_check_pagepool_dma(struct mvpp2_port *port)
5132 {
5133 	enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
5134 	struct mvpp2 *priv = port->priv;
5135 	int err = -1, i;
5136 
5137 	if (!priv->percpu_pools)
5138 		return err;
5139 
5140 	if (!priv->page_pool[0])
5141 		return -ENOMEM;
5142 
5143 	for (i = 0; i < priv->port_count; i++) {
5144 		port = priv->port_list[i];
5145 		if (port->xdp_prog) {
5146 			dma_dir = DMA_BIDIRECTIONAL;
5147 			break;
5148 		}
5149 	}
5150 
5151 	/* All pools are equal in terms of DMA direction */
5152 	if (priv->page_pool[0]->p.dma_dir != dma_dir)
5153 		err = mvpp2_bm_switch_buffers(priv, true);
5154 
5155 	return err;
5156 }
5157 
5158 static void
mvpp2_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)5159 mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5160 {
5161 	struct mvpp2_port *port = netdev_priv(dev);
5162 	unsigned int start;
5163 	unsigned int cpu;
5164 
5165 	for_each_possible_cpu(cpu) {
5166 		struct mvpp2_pcpu_stats *cpu_stats;
5167 		u64 rx_packets;
5168 		u64 rx_bytes;
5169 		u64 tx_packets;
5170 		u64 tx_bytes;
5171 
5172 		cpu_stats = per_cpu_ptr(port->stats, cpu);
5173 		do {
5174 			start = u64_stats_fetch_begin(&cpu_stats->syncp);
5175 			rx_packets = cpu_stats->rx_packets;
5176 			rx_bytes   = cpu_stats->rx_bytes;
5177 			tx_packets = cpu_stats->tx_packets;
5178 			tx_bytes   = cpu_stats->tx_bytes;
5179 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
5180 
5181 		stats->rx_packets += rx_packets;
5182 		stats->rx_bytes   += rx_bytes;
5183 		stats->tx_packets += tx_packets;
5184 		stats->tx_bytes   += tx_bytes;
5185 	}
5186 
5187 	stats->rx_errors	= dev->stats.rx_errors;
5188 	stats->rx_dropped	= dev->stats.rx_dropped;
5189 	stats->tx_dropped	= dev->stats.tx_dropped;
5190 }
5191 
mvpp2_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)5192 static int mvpp2_hwtstamp_set(struct net_device *dev,
5193 			      struct kernel_hwtstamp_config *config,
5194 			      struct netlink_ext_ack *extack)
5195 {
5196 	struct mvpp2_port *port = netdev_priv(dev);
5197 	void __iomem *ptp;
5198 	u32 gcr, int_mask;
5199 
5200 	if (!port->hwtstamp)
5201 		return -EOPNOTSUPP;
5202 
5203 	if (config->tx_type != HWTSTAMP_TX_OFF &&
5204 	    config->tx_type != HWTSTAMP_TX_ON)
5205 		return -ERANGE;
5206 
5207 	ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
5208 
5209 	int_mask = gcr = 0;
5210 	if (config->tx_type != HWTSTAMP_TX_OFF) {
5211 		gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_TX_RESET;
5212 		int_mask |= MVPP22_PTP_INT_MASK_QUEUE1 |
5213 			    MVPP22_PTP_INT_MASK_QUEUE0;
5214 	}
5215 
5216 	/* It seems we must also release the TX reset when enabling the TSU */
5217 	if (config->rx_filter != HWTSTAMP_FILTER_NONE)
5218 		gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_RX_RESET |
5219 		       MVPP22_PTP_GCR_TX_RESET;
5220 
5221 	if (gcr & MVPP22_PTP_GCR_TSU_ENABLE)
5222 		mvpp22_tai_start(port->priv->tai);
5223 
5224 	if (config->rx_filter != HWTSTAMP_FILTER_NONE) {
5225 		config->rx_filter = HWTSTAMP_FILTER_ALL;
5226 		mvpp2_modify(ptp + MVPP22_PTP_GCR,
5227 			     MVPP22_PTP_GCR_RX_RESET |
5228 			     MVPP22_PTP_GCR_TX_RESET |
5229 			     MVPP22_PTP_GCR_TSU_ENABLE, gcr);
5230 		port->rx_hwtstamp = true;
5231 	} else {
5232 		port->rx_hwtstamp = false;
5233 		mvpp2_modify(ptp + MVPP22_PTP_GCR,
5234 			     MVPP22_PTP_GCR_RX_RESET |
5235 			     MVPP22_PTP_GCR_TX_RESET |
5236 			     MVPP22_PTP_GCR_TSU_ENABLE, gcr);
5237 	}
5238 
5239 	mvpp2_modify(ptp + MVPP22_PTP_INT_MASK,
5240 		     MVPP22_PTP_INT_MASK_QUEUE1 |
5241 		     MVPP22_PTP_INT_MASK_QUEUE0, int_mask);
5242 
5243 	if (!(gcr & MVPP22_PTP_GCR_TSU_ENABLE))
5244 		mvpp22_tai_stop(port->priv->tai);
5245 
5246 	port->tx_hwtstamp_type = config->tx_type;
5247 
5248 	return 0;
5249 }
5250 
mvpp2_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * config)5251 static int mvpp2_hwtstamp_get(struct net_device *dev,
5252 			      struct kernel_hwtstamp_config *config)
5253 {
5254 	struct mvpp2_port *port = netdev_priv(dev);
5255 
5256 	if (!port->hwtstamp)
5257 		return -EOPNOTSUPP;
5258 
5259 	config->tx_type = port->tx_hwtstamp_type;
5260 	config->rx_filter = port->rx_hwtstamp ? HWTSTAMP_FILTER_ALL :
5261 			    HWTSTAMP_FILTER_NONE;
5262 
5263 	return 0;
5264 }
5265 
mvpp2_ethtool_get_ts_info(struct net_device * dev,struct kernel_ethtool_ts_info * info)5266 static int mvpp2_ethtool_get_ts_info(struct net_device *dev,
5267 				     struct kernel_ethtool_ts_info *info)
5268 {
5269 	struct mvpp2_port *port = netdev_priv(dev);
5270 
5271 	ethtool_op_get_ts_info(dev, info);
5272 	if (!port->hwtstamp)
5273 		return 0;
5274 
5275 	info->phc_index = mvpp22_tai_ptp_clock_index(port->priv->tai);
5276 	info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
5277 				 SOF_TIMESTAMPING_RX_HARDWARE |
5278 				 SOF_TIMESTAMPING_RAW_HARDWARE;
5279 	info->tx_types = BIT(HWTSTAMP_TX_OFF) |
5280 			 BIT(HWTSTAMP_TX_ON);
5281 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
5282 			   BIT(HWTSTAMP_FILTER_ALL);
5283 
5284 	return 0;
5285 }
5286 
mvpp2_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)5287 static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5288 {
5289 	struct mvpp2_port *port = netdev_priv(dev);
5290 
5291 	if (!port->phylink)
5292 		return -ENOTSUPP;
5293 
5294 	return phylink_mii_ioctl(port->phylink, ifr, cmd);
5295 }
5296 
mvpp2_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)5297 static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
5298 {
5299 	struct mvpp2_port *port = netdev_priv(dev);
5300 	int ret;
5301 
5302 	ret = mvpp2_prs_vid_entry_add(port, vid);
5303 	if (ret)
5304 		netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
5305 			   MVPP2_PRS_VLAN_FILT_MAX - 1);
5306 	return ret;
5307 }
5308 
mvpp2_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)5309 static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
5310 {
5311 	struct mvpp2_port *port = netdev_priv(dev);
5312 
5313 	mvpp2_prs_vid_entry_remove(port, vid);
5314 	return 0;
5315 }
5316 
mvpp2_set_features(struct net_device * dev,netdev_features_t features)5317 static int mvpp2_set_features(struct net_device *dev,
5318 			      netdev_features_t features)
5319 {
5320 	netdev_features_t changed = dev->features ^ features;
5321 	struct mvpp2_port *port = netdev_priv(dev);
5322 
5323 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
5324 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
5325 			mvpp2_prs_vid_enable_filtering(port);
5326 		} else {
5327 			/* Invalidate all registered VID filters for this
5328 			 * port
5329 			 */
5330 			mvpp2_prs_vid_remove_all(port);
5331 
5332 			mvpp2_prs_vid_disable_filtering(port);
5333 		}
5334 	}
5335 
5336 	if (changed & NETIF_F_RXHASH) {
5337 		if (features & NETIF_F_RXHASH)
5338 			mvpp22_port_rss_enable(port);
5339 		else
5340 			mvpp22_port_rss_disable(port);
5341 	}
5342 
5343 	return 0;
5344 }
5345 
mvpp2_xdp_setup(struct mvpp2_port * port,struct netdev_bpf * bpf)5346 static int mvpp2_xdp_setup(struct mvpp2_port *port, struct netdev_bpf *bpf)
5347 {
5348 	struct bpf_prog *prog = bpf->prog, *old_prog;
5349 	bool running = netif_running(port->dev);
5350 	bool reset = !prog != !port->xdp_prog;
5351 
5352 	if (port->dev->mtu > MVPP2_MAX_RX_BUF_SIZE) {
5353 		NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
5354 		return -EOPNOTSUPP;
5355 	}
5356 
5357 	if (!port->priv->percpu_pools) {
5358 		NL_SET_ERR_MSG_MOD(bpf->extack, "Per CPU Pools required for XDP");
5359 		return -EOPNOTSUPP;
5360 	}
5361 
5362 	if (port->ntxqs < num_possible_cpus() * 2) {
5363 		NL_SET_ERR_MSG_MOD(bpf->extack, "XDP_TX needs two TX queues per CPU");
5364 		return -EOPNOTSUPP;
5365 	}
5366 
5367 	/* device is up and bpf is added/removed, must setup the RX queues */
5368 	if (running && reset)
5369 		mvpp2_stop(port->dev);
5370 
5371 	old_prog = xchg(&port->xdp_prog, prog);
5372 	if (old_prog)
5373 		bpf_prog_put(old_prog);
5374 
5375 	/* bpf is just replaced, RXQ and MTU are already setup */
5376 	if (!reset)
5377 		return 0;
5378 
5379 	/* device was up, restore the link */
5380 	if (running)
5381 		mvpp2_open(port->dev);
5382 
5383 	/* Check Page Pool DMA Direction */
5384 	mvpp2_check_pagepool_dma(port);
5385 
5386 	return 0;
5387 }
5388 
mvpp2_xdp(struct net_device * dev,struct netdev_bpf * xdp)5389 static int mvpp2_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5390 {
5391 	struct mvpp2_port *port = netdev_priv(dev);
5392 
5393 	switch (xdp->command) {
5394 	case XDP_SETUP_PROG:
5395 		return mvpp2_xdp_setup(port, xdp);
5396 	default:
5397 		return -EINVAL;
5398 	}
5399 }
5400 
5401 /* Ethtool methods */
5402 
mvpp2_ethtool_nway_reset(struct net_device * dev)5403 static int mvpp2_ethtool_nway_reset(struct net_device *dev)
5404 {
5405 	struct mvpp2_port *port = netdev_priv(dev);
5406 
5407 	if (!port->phylink)
5408 		return -ENOTSUPP;
5409 
5410 	return phylink_ethtool_nway_reset(port->phylink);
5411 }
5412 
5413 /* Set interrupt coalescing for ethtools */
5414 static int
mvpp2_ethtool_set_coalesce(struct net_device * dev,struct ethtool_coalesce * c,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)5415 mvpp2_ethtool_set_coalesce(struct net_device *dev,
5416 			   struct ethtool_coalesce *c,
5417 			   struct kernel_ethtool_coalesce *kernel_coal,
5418 			   struct netlink_ext_ack *extack)
5419 {
5420 	struct mvpp2_port *port = netdev_priv(dev);
5421 	int queue;
5422 
5423 	for (queue = 0; queue < port->nrxqs; queue++) {
5424 		struct mvpp2_rx_queue *rxq = port->rxqs[queue];
5425 
5426 		rxq->time_coal = c->rx_coalesce_usecs;
5427 		rxq->pkts_coal = c->rx_max_coalesced_frames;
5428 		mvpp2_rx_pkts_coal_set(port, rxq);
5429 		mvpp2_rx_time_coal_set(port, rxq);
5430 	}
5431 
5432 	if (port->has_tx_irqs) {
5433 		port->tx_time_coal = c->tx_coalesce_usecs;
5434 		mvpp2_tx_time_coal_set(port);
5435 	}
5436 
5437 	for (queue = 0; queue < port->ntxqs; queue++) {
5438 		struct mvpp2_tx_queue *txq = port->txqs[queue];
5439 
5440 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
5441 
5442 		if (port->has_tx_irqs)
5443 			mvpp2_tx_pkts_coal_set(port, txq);
5444 	}
5445 
5446 	return 0;
5447 }
5448 
5449 /* get coalescing for ethtools */
5450 static int
mvpp2_ethtool_get_coalesce(struct net_device * dev,struct ethtool_coalesce * c,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)5451 mvpp2_ethtool_get_coalesce(struct net_device *dev,
5452 			   struct ethtool_coalesce *c,
5453 			   struct kernel_ethtool_coalesce *kernel_coal,
5454 			   struct netlink_ext_ack *extack)
5455 {
5456 	struct mvpp2_port *port = netdev_priv(dev);
5457 
5458 	c->rx_coalesce_usecs       = port->rxqs[0]->time_coal;
5459 	c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
5460 	c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
5461 	c->tx_coalesce_usecs       = port->tx_time_coal;
5462 	return 0;
5463 }
5464 
mvpp2_ethtool_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)5465 static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
5466 				      struct ethtool_drvinfo *drvinfo)
5467 {
5468 	strscpy(drvinfo->driver, MVPP2_DRIVER_NAME,
5469 		sizeof(drvinfo->driver));
5470 	strscpy(drvinfo->version, MVPP2_DRIVER_VERSION,
5471 		sizeof(drvinfo->version));
5472 	strscpy(drvinfo->bus_info, dev_name(&dev->dev),
5473 		sizeof(drvinfo->bus_info));
5474 }
5475 
5476 static void
mvpp2_ethtool_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)5477 mvpp2_ethtool_get_ringparam(struct net_device *dev,
5478 			    struct ethtool_ringparam *ring,
5479 			    struct kernel_ethtool_ringparam *kernel_ring,
5480 			    struct netlink_ext_ack *extack)
5481 {
5482 	struct mvpp2_port *port = netdev_priv(dev);
5483 
5484 	ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
5485 	ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
5486 	ring->rx_pending = port->rx_ring_size;
5487 	ring->tx_pending = port->tx_ring_size;
5488 }
5489 
5490 static int
mvpp2_ethtool_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)5491 mvpp2_ethtool_set_ringparam(struct net_device *dev,
5492 			    struct ethtool_ringparam *ring,
5493 			    struct kernel_ethtool_ringparam *kernel_ring,
5494 			    struct netlink_ext_ack *extack)
5495 {
5496 	struct mvpp2_port *port = netdev_priv(dev);
5497 	u16 prev_rx_ring_size = port->rx_ring_size;
5498 	u16 prev_tx_ring_size = port->tx_ring_size;
5499 	int err;
5500 
5501 	err = mvpp2_check_ringparam_valid(dev, ring);
5502 	if (err)
5503 		return err;
5504 
5505 	if (!netif_running(dev)) {
5506 		port->rx_ring_size = ring->rx_pending;
5507 		port->tx_ring_size = ring->tx_pending;
5508 		return 0;
5509 	}
5510 
5511 	/* The interface is running, so we have to force a
5512 	 * reallocation of the queues
5513 	 */
5514 	mvpp2_stop_dev(port);
5515 	mvpp2_cleanup_rxqs(port);
5516 	mvpp2_cleanup_txqs(port);
5517 
5518 	port->rx_ring_size = ring->rx_pending;
5519 	port->tx_ring_size = ring->tx_pending;
5520 
5521 	err = mvpp2_setup_rxqs(port);
5522 	if (err) {
5523 		/* Reallocate Rx queues with the original ring size */
5524 		port->rx_ring_size = prev_rx_ring_size;
5525 		ring->rx_pending = prev_rx_ring_size;
5526 		err = mvpp2_setup_rxqs(port);
5527 		if (err)
5528 			goto err_out;
5529 	}
5530 	err = mvpp2_setup_txqs(port);
5531 	if (err) {
5532 		/* Reallocate Tx queues with the original ring size */
5533 		port->tx_ring_size = prev_tx_ring_size;
5534 		ring->tx_pending = prev_tx_ring_size;
5535 		err = mvpp2_setup_txqs(port);
5536 		if (err)
5537 			goto err_clean_rxqs;
5538 	}
5539 
5540 	mvpp2_start_dev(port);
5541 	mvpp2_egress_enable(port);
5542 	mvpp2_ingress_enable(port);
5543 
5544 	return 0;
5545 
5546 err_clean_rxqs:
5547 	mvpp2_cleanup_rxqs(port);
5548 err_out:
5549 	netdev_err(dev, "failed to change ring parameters");
5550 	return err;
5551 }
5552 
mvpp2_ethtool_get_pause_param(struct net_device * dev,struct ethtool_pauseparam * pause)5553 static void mvpp2_ethtool_get_pause_param(struct net_device *dev,
5554 					  struct ethtool_pauseparam *pause)
5555 {
5556 	struct mvpp2_port *port = netdev_priv(dev);
5557 
5558 	if (!port->phylink)
5559 		return;
5560 
5561 	phylink_ethtool_get_pauseparam(port->phylink, pause);
5562 }
5563 
mvpp2_ethtool_set_pause_param(struct net_device * dev,struct ethtool_pauseparam * pause)5564 static int mvpp2_ethtool_set_pause_param(struct net_device *dev,
5565 					 struct ethtool_pauseparam *pause)
5566 {
5567 	struct mvpp2_port *port = netdev_priv(dev);
5568 
5569 	if (!port->phylink)
5570 		return -ENOTSUPP;
5571 
5572 	return phylink_ethtool_set_pauseparam(port->phylink, pause);
5573 }
5574 
mvpp2_ethtool_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)5575 static int mvpp2_ethtool_get_link_ksettings(struct net_device *dev,
5576 					    struct ethtool_link_ksettings *cmd)
5577 {
5578 	struct mvpp2_port *port = netdev_priv(dev);
5579 
5580 	if (!port->phylink)
5581 		return -ENOTSUPP;
5582 
5583 	return phylink_ethtool_ksettings_get(port->phylink, cmd);
5584 }
5585 
mvpp2_ethtool_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)5586 static int mvpp2_ethtool_set_link_ksettings(struct net_device *dev,
5587 					    const struct ethtool_link_ksettings *cmd)
5588 {
5589 	struct mvpp2_port *port = netdev_priv(dev);
5590 
5591 	if (!port->phylink)
5592 		return -ENOTSUPP;
5593 
5594 	return phylink_ethtool_ksettings_set(port->phylink, cmd);
5595 }
5596 
mvpp2_ethtool_get_rx_ring_count(struct net_device * dev)5597 static u32 mvpp2_ethtool_get_rx_ring_count(struct net_device *dev)
5598 {
5599 	struct mvpp2_port *port = netdev_priv(dev);
5600 
5601 	return port->nrxqs;
5602 }
5603 
mvpp2_ethtool_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rules)5604 static int mvpp2_ethtool_get_rxnfc(struct net_device *dev,
5605 				   struct ethtool_rxnfc *info, u32 *rules)
5606 {
5607 	struct mvpp2_port *port = netdev_priv(dev);
5608 	int ret = 0, i, loc = 0;
5609 
5610 	if (!mvpp22_rss_is_supported(port))
5611 		return -EOPNOTSUPP;
5612 
5613 	switch (info->cmd) {
5614 	case ETHTOOL_GRXCLSRLCNT:
5615 		info->rule_cnt = port->n_rfs_rules;
5616 		break;
5617 	case ETHTOOL_GRXCLSRULE:
5618 		ret = mvpp2_ethtool_cls_rule_get(port, info);
5619 		break;
5620 	case ETHTOOL_GRXCLSRLALL:
5621 		for (i = 0; i < MVPP2_N_RFS_ENTRIES_PER_FLOW; i++) {
5622 			if (loc == info->rule_cnt) {
5623 				ret = -EMSGSIZE;
5624 				break;
5625 			}
5626 
5627 			if (port->rfs_rules[i])
5628 				rules[loc++] = i;
5629 		}
5630 		break;
5631 	default:
5632 		return -ENOTSUPP;
5633 	}
5634 
5635 	return ret;
5636 }
5637 
mvpp2_ethtool_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)5638 static int mvpp2_ethtool_set_rxnfc(struct net_device *dev,
5639 				   struct ethtool_rxnfc *info)
5640 {
5641 	struct mvpp2_port *port = netdev_priv(dev);
5642 	int ret = 0;
5643 
5644 	if (!mvpp22_rss_is_supported(port))
5645 		return -EOPNOTSUPP;
5646 
5647 	switch (info->cmd) {
5648 	case ETHTOOL_SRXCLSRLINS:
5649 		ret = mvpp2_ethtool_cls_rule_ins(port, info);
5650 		break;
5651 	case ETHTOOL_SRXCLSRLDEL:
5652 		ret = mvpp2_ethtool_cls_rule_del(port, info);
5653 		break;
5654 	default:
5655 		return -EOPNOTSUPP;
5656 	}
5657 	return ret;
5658 }
5659 
mvpp2_ethtool_get_rxfh_indir_size(struct net_device * dev)5660 static u32 mvpp2_ethtool_get_rxfh_indir_size(struct net_device *dev)
5661 {
5662 	struct mvpp2_port *port = netdev_priv(dev);
5663 
5664 	return mvpp22_rss_is_supported(port) ? MVPP22_RSS_TABLE_ENTRIES : 0;
5665 }
5666 
mvpp2_ethtool_get_rxfh(struct net_device * dev,struct ethtool_rxfh_param * rxfh)5667 static int mvpp2_ethtool_get_rxfh(struct net_device *dev,
5668 				  struct ethtool_rxfh_param *rxfh)
5669 {
5670 	struct mvpp2_port *port = netdev_priv(dev);
5671 	u32 rss_context = rxfh->rss_context;
5672 	int ret = 0;
5673 
5674 	if (!mvpp22_rss_is_supported(port))
5675 		return -EOPNOTSUPP;
5676 	if (rss_context >= MVPP22_N_RSS_TABLES)
5677 		return -EINVAL;
5678 
5679 	rxfh->hfunc = ETH_RSS_HASH_CRC32;
5680 
5681 	if (rxfh->indir)
5682 		ret = mvpp22_port_rss_ctx_indir_get(port, rss_context,
5683 						    rxfh->indir);
5684 
5685 	return ret;
5686 }
5687 
mvpp2_ethtool_rxfh_okay(struct mvpp2_port * port,const struct ethtool_rxfh_param * rxfh)5688 static bool mvpp2_ethtool_rxfh_okay(struct mvpp2_port *port,
5689 				    const struct ethtool_rxfh_param *rxfh)
5690 {
5691 	if (!mvpp22_rss_is_supported(port))
5692 		return false;
5693 
5694 	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
5695 	    rxfh->hfunc != ETH_RSS_HASH_CRC32)
5696 		return false;
5697 
5698 	if (rxfh->key)
5699 		return false;
5700 
5701 	return true;
5702 }
5703 
mvpp2_create_rxfh_context(struct net_device * dev,struct ethtool_rxfh_context * ctx,const struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)5704 static int mvpp2_create_rxfh_context(struct net_device *dev,
5705 				     struct ethtool_rxfh_context *ctx,
5706 				     const struct ethtool_rxfh_param *rxfh,
5707 				     struct netlink_ext_ack *extack)
5708 {
5709 	struct mvpp2_port *port = netdev_priv(dev);
5710 	int ret = 0;
5711 
5712 	if (!mvpp2_ethtool_rxfh_okay(port, rxfh))
5713 		return -EOPNOTSUPP;
5714 
5715 	ctx->hfunc = ETH_RSS_HASH_CRC32;
5716 
5717 	ret = mvpp22_port_rss_ctx_create(port, rxfh->rss_context);
5718 	if (ret)
5719 		return ret;
5720 
5721 	if (!rxfh->indir)
5722 		ret = mvpp22_port_rss_ctx_indir_get(port, rxfh->rss_context,
5723 						    ethtool_rxfh_context_indir(ctx));
5724 	else
5725 		ret = mvpp22_port_rss_ctx_indir_set(port, rxfh->rss_context,
5726 						    rxfh->indir);
5727 	return ret;
5728 }
5729 
mvpp2_modify_rxfh_context(struct net_device * dev,struct ethtool_rxfh_context * ctx,const struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)5730 static int mvpp2_modify_rxfh_context(struct net_device *dev,
5731 				     struct ethtool_rxfh_context *ctx,
5732 				     const struct ethtool_rxfh_param *rxfh,
5733 				     struct netlink_ext_ack *extack)
5734 {
5735 	struct mvpp2_port *port = netdev_priv(dev);
5736 	int ret = 0;
5737 
5738 	if (!mvpp2_ethtool_rxfh_okay(port, rxfh))
5739 		return -EOPNOTSUPP;
5740 
5741 	if (rxfh->indir)
5742 		ret = mvpp22_port_rss_ctx_indir_set(port, rxfh->rss_context,
5743 						    rxfh->indir);
5744 	return ret;
5745 }
5746 
mvpp2_remove_rxfh_context(struct net_device * dev,struct ethtool_rxfh_context * ctx,u32 rss_context,struct netlink_ext_ack * extack)5747 static int mvpp2_remove_rxfh_context(struct net_device *dev,
5748 				     struct ethtool_rxfh_context *ctx,
5749 				     u32 rss_context,
5750 				     struct netlink_ext_ack *extack)
5751 {
5752 	struct mvpp2_port *port = netdev_priv(dev);
5753 
5754 	return mvpp22_port_rss_ctx_delete(port, rss_context);
5755 }
5756 
mvpp2_ethtool_set_rxfh(struct net_device * dev,struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)5757 static int mvpp2_ethtool_set_rxfh(struct net_device *dev,
5758 				  struct ethtool_rxfh_param *rxfh,
5759 				  struct netlink_ext_ack *extack)
5760 {
5761 	return mvpp2_modify_rxfh_context(dev, NULL, rxfh, extack);
5762 }
5763 
mvpp2_ethtool_get_rxfh_fields(struct net_device * dev,struct ethtool_rxfh_fields * info)5764 static int mvpp2_ethtool_get_rxfh_fields(struct net_device *dev,
5765 					 struct ethtool_rxfh_fields *info)
5766 {
5767 	struct mvpp2_port *port = netdev_priv(dev);
5768 
5769 	if (!mvpp22_rss_is_supported(port))
5770 		return -EOPNOTSUPP;
5771 
5772 	return mvpp2_ethtool_rxfh_get(port, info);
5773 }
5774 
mvpp2_ethtool_set_rxfh_fields(struct net_device * dev,const struct ethtool_rxfh_fields * info,struct netlink_ext_ack * extack)5775 static int mvpp2_ethtool_set_rxfh_fields(struct net_device *dev,
5776 					 const struct ethtool_rxfh_fields *info,
5777 					 struct netlink_ext_ack *extack)
5778 {
5779 	struct mvpp2_port *port = netdev_priv(dev);
5780 
5781 	if (!mvpp22_rss_is_supported(port))
5782 		return -EOPNOTSUPP;
5783 
5784 	return mvpp2_ethtool_rxfh_set(port, info);
5785 }
5786 
mvpp2_ethtool_get_eee(struct net_device * dev,struct ethtool_keee * eee)5787 static int mvpp2_ethtool_get_eee(struct net_device *dev,
5788 				 struct ethtool_keee *eee)
5789 {
5790 	struct mvpp2_port *port = netdev_priv(dev);
5791 
5792 	if (!port->phylink)
5793 		return -EOPNOTSUPP;
5794 
5795 	return phylink_ethtool_get_eee(port->phylink, eee);
5796 }
5797 
mvpp2_ethtool_set_eee(struct net_device * dev,struct ethtool_keee * eee)5798 static int mvpp2_ethtool_set_eee(struct net_device *dev,
5799 				 struct ethtool_keee *eee)
5800 {
5801 	struct mvpp2_port *port = netdev_priv(dev);
5802 
5803 	if (!port->phylink)
5804 		return -EOPNOTSUPP;
5805 
5806 	return phylink_ethtool_set_eee(port->phylink, eee);
5807 }
5808 
5809 /* Device ops */
5810 
5811 static const struct net_device_ops mvpp2_netdev_ops = {
5812 	.ndo_open		= mvpp2_open,
5813 	.ndo_stop		= mvpp2_stop,
5814 	.ndo_start_xmit		= mvpp2_tx,
5815 	.ndo_set_rx_mode	= mvpp2_set_rx_mode,
5816 	.ndo_set_mac_address	= mvpp2_set_mac_address,
5817 	.ndo_change_mtu		= mvpp2_change_mtu,
5818 	.ndo_get_stats64	= mvpp2_get_stats64,
5819 	.ndo_eth_ioctl		= mvpp2_ioctl,
5820 	.ndo_vlan_rx_add_vid	= mvpp2_vlan_rx_add_vid,
5821 	.ndo_vlan_rx_kill_vid	= mvpp2_vlan_rx_kill_vid,
5822 	.ndo_set_features	= mvpp2_set_features,
5823 	.ndo_bpf		= mvpp2_xdp,
5824 	.ndo_xdp_xmit		= mvpp2_xdp_xmit,
5825 	.ndo_hwtstamp_get	= mvpp2_hwtstamp_get,
5826 	.ndo_hwtstamp_set	= mvpp2_hwtstamp_set,
5827 };
5828 
5829 static const struct ethtool_ops mvpp2_eth_tool_ops = {
5830 	.rxfh_max_num_contexts	= MVPP22_N_RSS_TABLES,
5831 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
5832 				     ETHTOOL_COALESCE_MAX_FRAMES,
5833 	.nway_reset		= mvpp2_ethtool_nway_reset,
5834 	.get_link		= ethtool_op_get_link,
5835 	.get_ts_info		= mvpp2_ethtool_get_ts_info,
5836 	.set_coalesce		= mvpp2_ethtool_set_coalesce,
5837 	.get_coalesce		= mvpp2_ethtool_get_coalesce,
5838 	.get_drvinfo		= mvpp2_ethtool_get_drvinfo,
5839 	.get_ringparam		= mvpp2_ethtool_get_ringparam,
5840 	.set_ringparam		= mvpp2_ethtool_set_ringparam,
5841 	.get_strings		= mvpp2_ethtool_get_strings,
5842 	.get_ethtool_stats	= mvpp2_ethtool_get_stats,
5843 	.get_sset_count		= mvpp2_ethtool_get_sset_count,
5844 	.get_pauseparam		= mvpp2_ethtool_get_pause_param,
5845 	.set_pauseparam		= mvpp2_ethtool_set_pause_param,
5846 	.get_link_ksettings	= mvpp2_ethtool_get_link_ksettings,
5847 	.set_link_ksettings	= mvpp2_ethtool_set_link_ksettings,
5848 	.get_rx_ring_count	= mvpp2_ethtool_get_rx_ring_count,
5849 	.get_rxnfc		= mvpp2_ethtool_get_rxnfc,
5850 	.set_rxnfc		= mvpp2_ethtool_set_rxnfc,
5851 	.get_rxfh_indir_size	= mvpp2_ethtool_get_rxfh_indir_size,
5852 	.get_rxfh		= mvpp2_ethtool_get_rxfh,
5853 	.set_rxfh		= mvpp2_ethtool_set_rxfh,
5854 	.get_rxfh_fields	= mvpp2_ethtool_get_rxfh_fields,
5855 	.set_rxfh_fields	= mvpp2_ethtool_set_rxfh_fields,
5856 	.create_rxfh_context	= mvpp2_create_rxfh_context,
5857 	.modify_rxfh_context	= mvpp2_modify_rxfh_context,
5858 	.remove_rxfh_context	= mvpp2_remove_rxfh_context,
5859 	.get_eee		= mvpp2_ethtool_get_eee,
5860 	.set_eee		= mvpp2_ethtool_set_eee,
5861 };
5862 
5863 /* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
5864  * had a single IRQ defined per-port.
5865  */
mvpp2_simple_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5866 static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
5867 					   struct device_node *port_node)
5868 {
5869 	struct mvpp2_queue_vector *v = &port->qvecs[0];
5870 
5871 	v->first_rxq = 0;
5872 	v->nrxqs = port->nrxqs;
5873 	v->type = MVPP2_QUEUE_VECTOR_SHARED;
5874 	v->sw_thread_id = 0;
5875 	v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
5876 	v->port = port;
5877 	v->irq = irq_of_parse_and_map(port_node, 0);
5878 	if (v->irq <= 0)
5879 		return -EINVAL;
5880 	netif_napi_add(port->dev, &v->napi, mvpp2_poll);
5881 
5882 	port->nqvecs = 1;
5883 
5884 	return 0;
5885 }
5886 
mvpp2_multi_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5887 static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
5888 					  struct device_node *port_node)
5889 {
5890 	struct mvpp2 *priv = port->priv;
5891 	struct mvpp2_queue_vector *v;
5892 	int i, ret;
5893 
5894 	switch (queue_mode) {
5895 	case MVPP2_QDIST_SINGLE_MODE:
5896 		port->nqvecs = priv->nthreads + 1;
5897 		break;
5898 	case MVPP2_QDIST_MULTI_MODE:
5899 		port->nqvecs = priv->nthreads;
5900 		break;
5901 	}
5902 
5903 	for (i = 0; i < port->nqvecs; i++) {
5904 		char irqname[16];
5905 
5906 		v = port->qvecs + i;
5907 
5908 		v->port = port;
5909 		v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
5910 		v->sw_thread_id = i;
5911 		v->sw_thread_mask = BIT(i);
5912 
5913 		if (port->flags & MVPP2_F_DT_COMPAT)
5914 			snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
5915 		else
5916 			snprintf(irqname, sizeof(irqname), "hif%d", i);
5917 
5918 		if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
5919 			v->first_rxq = i;
5920 			v->nrxqs = 1;
5921 		} else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
5922 			   i == (port->nqvecs - 1)) {
5923 			v->first_rxq = 0;
5924 			v->nrxqs = port->nrxqs;
5925 			v->type = MVPP2_QUEUE_VECTOR_SHARED;
5926 
5927 			if (port->flags & MVPP2_F_DT_COMPAT)
5928 				strscpy(irqname, "rx-shared", sizeof(irqname));
5929 		}
5930 
5931 		if (port_node)
5932 			v->irq = of_irq_get_byname(port_node, irqname);
5933 		else
5934 			v->irq = fwnode_irq_get(port->fwnode, i);
5935 		if (v->irq <= 0) {
5936 			ret = -EINVAL;
5937 			goto err;
5938 		}
5939 
5940 		netif_napi_add(port->dev, &v->napi, mvpp2_poll);
5941 	}
5942 
5943 	return 0;
5944 
5945 err:
5946 	for (i = 0; i < port->nqvecs; i++)
5947 		irq_dispose_mapping(port->qvecs[i].irq);
5948 	return ret;
5949 }
5950 
mvpp2_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5951 static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
5952 				    struct device_node *port_node)
5953 {
5954 	if (port->has_tx_irqs)
5955 		return mvpp2_multi_queue_vectors_init(port, port_node);
5956 	else
5957 		return mvpp2_simple_queue_vectors_init(port, port_node);
5958 }
5959 
mvpp2_queue_vectors_deinit(struct mvpp2_port * port)5960 static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
5961 {
5962 	int i;
5963 
5964 	for (i = 0; i < port->nqvecs; i++)
5965 		irq_dispose_mapping(port->qvecs[i].irq);
5966 }
5967 
5968 /* Configure Rx queue group interrupt for this port */
mvpp2_rx_irqs_setup(struct mvpp2_port * port)5969 static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
5970 {
5971 	struct mvpp2 *priv = port->priv;
5972 	u32 val;
5973 	int i;
5974 
5975 	if (priv->hw_version == MVPP21) {
5976 		mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
5977 			    port->nrxqs);
5978 		return;
5979 	}
5980 
5981 	/* Handle the more complicated PPv2.2 and PPv2.3 case */
5982 	for (i = 0; i < port->nqvecs; i++) {
5983 		struct mvpp2_queue_vector *qv = port->qvecs + i;
5984 
5985 		if (!qv->nrxqs)
5986 			continue;
5987 
5988 		val = qv->sw_thread_id;
5989 		val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
5990 		mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
5991 
5992 		val = qv->first_rxq;
5993 		val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
5994 		mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
5995 	}
5996 }
5997 
5998 /* Initialize port HW */
mvpp2_port_init(struct mvpp2_port * port)5999 static int mvpp2_port_init(struct mvpp2_port *port)
6000 {
6001 	struct device *dev = port->dev->dev.parent;
6002 	struct mvpp2 *priv = port->priv;
6003 	struct mvpp2_txq_pcpu *txq_pcpu;
6004 	unsigned int thread;
6005 	int queue, err, val;
6006 
6007 	/* Checks for hardware constraints */
6008 	if (port->first_rxq + port->nrxqs >
6009 	    MVPP2_MAX_PORTS * priv->max_port_rxqs)
6010 		return -EINVAL;
6011 
6012 	if (port->nrxqs > priv->max_port_rxqs || port->ntxqs > MVPP2_MAX_TXQ)
6013 		return -EINVAL;
6014 
6015 	/* Disable port */
6016 	mvpp2_egress_disable(port);
6017 	mvpp2_port_disable(port);
6018 
6019 	if (mvpp2_is_xlg(port->phy_interface)) {
6020 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6021 		val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6022 		val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
6023 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
6024 	} else {
6025 		val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6026 		val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
6027 		val |= MVPP2_GMAC_FORCE_LINK_DOWN;
6028 		writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6029 	}
6030 
6031 	port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
6032 
6033 	port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
6034 				  GFP_KERNEL);
6035 	if (!port->txqs)
6036 		return -ENOMEM;
6037 
6038 	/* Associate physical Tx queues to this port and initialize.
6039 	 * The mapping is predefined.
6040 	 */
6041 	for (queue = 0; queue < port->ntxqs; queue++) {
6042 		int queue_phy_id = mvpp2_txq_phys(port->id, queue);
6043 		struct mvpp2_tx_queue *txq;
6044 
6045 		txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
6046 		if (!txq) {
6047 			err = -ENOMEM;
6048 			goto err_free_percpu;
6049 		}
6050 
6051 		txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
6052 		if (!txq->pcpu) {
6053 			err = -ENOMEM;
6054 			goto err_free_percpu;
6055 		}
6056 
6057 		txq->id = queue_phy_id;
6058 		txq->log_id = queue;
6059 		txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
6060 		for (thread = 0; thread < priv->nthreads; thread++) {
6061 			txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
6062 			txq_pcpu->thread = thread;
6063 		}
6064 
6065 		port->txqs[queue] = txq;
6066 	}
6067 
6068 	port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
6069 				  GFP_KERNEL);
6070 	if (!port->rxqs) {
6071 		err = -ENOMEM;
6072 		goto err_free_percpu;
6073 	}
6074 
6075 	/* Allocate and initialize Rx queue for this port */
6076 	for (queue = 0; queue < port->nrxqs; queue++) {
6077 		struct mvpp2_rx_queue *rxq;
6078 
6079 		/* Map physical Rx queue to port's logical Rx queue */
6080 		rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
6081 		if (!rxq) {
6082 			err = -ENOMEM;
6083 			goto err_free_percpu;
6084 		}
6085 		/* Map this Rx queue to a physical queue */
6086 		rxq->id = port->first_rxq + queue;
6087 		rxq->port = port->id;
6088 		rxq->logic_rxq = queue;
6089 
6090 		port->rxqs[queue] = rxq;
6091 	}
6092 
6093 	mvpp2_rx_irqs_setup(port);
6094 
6095 	/* Create Rx descriptor rings */
6096 	for (queue = 0; queue < port->nrxqs; queue++) {
6097 		struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6098 
6099 		rxq->size = port->rx_ring_size;
6100 		rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6101 		rxq->time_coal = MVPP2_RX_COAL_USEC;
6102 	}
6103 
6104 	mvpp2_ingress_disable(port);
6105 
6106 	/* Port default configuration */
6107 	mvpp2_defaults_set(port);
6108 
6109 	/* Port's classifier configuration */
6110 	mvpp2_cls_oversize_rxq_set(port);
6111 	mvpp2_cls_port_config(port);
6112 
6113 	if (mvpp22_rss_is_supported(port))
6114 		mvpp22_port_rss_init(port);
6115 
6116 	/* Provide an initial Rx packet size */
6117 	port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6118 
6119 	/* Initialize pools for swf */
6120 	err = mvpp2_swf_bm_pool_init(port);
6121 	if (err)
6122 		goto err_free_percpu;
6123 
6124 	/* Clear all port stats */
6125 	mvpp2_read_stats(port);
6126 	memset(port->ethtool_stats, 0,
6127 	       MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs) * sizeof(u64));
6128 
6129 	return 0;
6130 
6131 err_free_percpu:
6132 	for (queue = 0; queue < port->ntxqs; queue++) {
6133 		if (!port->txqs[queue])
6134 			continue;
6135 		free_percpu(port->txqs[queue]->pcpu);
6136 	}
6137 	return err;
6138 }
6139 
mvpp22_port_has_legacy_tx_irqs(struct device_node * port_node,unsigned long * flags)6140 static bool mvpp22_port_has_legacy_tx_irqs(struct device_node *port_node,
6141 					   unsigned long *flags)
6142 {
6143 	char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1", "tx-cpu2",
6144 			  "tx-cpu3" };
6145 	int i;
6146 
6147 	for (i = 0; i < 5; i++)
6148 		if (of_property_match_string(port_node, "interrupt-names",
6149 					     irqs[i]) < 0)
6150 			return false;
6151 
6152 	*flags |= MVPP2_F_DT_COMPAT;
6153 	return true;
6154 }
6155 
6156 /* Checks if the port dt description has the required Tx interrupts:
6157  * - PPv2.1: there are no such interrupts.
6158  * - PPv2.2 and PPv2.3:
6159  *   - The old DTs have: "rx-shared", "tx-cpuX" with X in [0...3]
6160  *   - The new ones have: "hifX" with X in [0..8]
6161  *
6162  * All those variants are supported to keep the backward compatibility.
6163  */
mvpp2_port_has_irqs(struct mvpp2 * priv,struct device_node * port_node,unsigned long * flags)6164 static bool mvpp2_port_has_irqs(struct mvpp2 *priv,
6165 				struct device_node *port_node,
6166 				unsigned long *flags)
6167 {
6168 	char name[5];
6169 	int i;
6170 
6171 	/* ACPI */
6172 	if (!port_node)
6173 		return true;
6174 
6175 	if (priv->hw_version == MVPP21)
6176 		return false;
6177 
6178 	if (mvpp22_port_has_legacy_tx_irqs(port_node, flags))
6179 		return true;
6180 
6181 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
6182 		snprintf(name, 5, "hif%d", i);
6183 		if (of_property_match_string(port_node, "interrupt-names",
6184 					     name) < 0)
6185 			return false;
6186 	}
6187 
6188 	return true;
6189 }
6190 
mvpp2_port_copy_mac_addr(struct net_device * dev,struct mvpp2 * priv,struct fwnode_handle * fwnode,char ** mac_from)6191 static int mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
6192 				    struct fwnode_handle *fwnode,
6193 				    char **mac_from)
6194 {
6195 	struct mvpp2_port *port = netdev_priv(dev);
6196 	char hw_mac_addr[ETH_ALEN] = {0};
6197 	char fw_mac_addr[ETH_ALEN];
6198 	int ret;
6199 
6200 	if (!fwnode_get_mac_address(fwnode, fw_mac_addr)) {
6201 		*mac_from = "firmware node";
6202 		eth_hw_addr_set(dev, fw_mac_addr);
6203 		return 0;
6204 	}
6205 
6206 	if (priv->hw_version == MVPP21) {
6207 		mvpp21_get_mac_address(port, hw_mac_addr);
6208 		if (is_valid_ether_addr(hw_mac_addr)) {
6209 			*mac_from = "hardware";
6210 			eth_hw_addr_set(dev, hw_mac_addr);
6211 			return 0;
6212 		}
6213 	}
6214 
6215 	/* Only valid on OF enabled platforms */
6216 	ret = of_get_mac_address_nvmem(to_of_node(fwnode), fw_mac_addr);
6217 	if (ret == -EPROBE_DEFER)
6218 		return ret;
6219 	if (!ret) {
6220 		*mac_from = "nvmem cell";
6221 		eth_hw_addr_set(dev, fw_mac_addr);
6222 		return 0;
6223 	}
6224 
6225 	*mac_from = "random";
6226 	eth_hw_addr_random(dev);
6227 
6228 	return 0;
6229 }
6230 
mvpp2_phylink_to_port(struct phylink_config * config)6231 static struct mvpp2_port *mvpp2_phylink_to_port(struct phylink_config *config)
6232 {
6233 	return container_of(config, struct mvpp2_port, phylink_config);
6234 }
6235 
mvpp2_pcs_xlg_to_port(struct phylink_pcs * pcs)6236 static struct mvpp2_port *mvpp2_pcs_xlg_to_port(struct phylink_pcs *pcs)
6237 {
6238 	return container_of(pcs, struct mvpp2_port, pcs_xlg);
6239 }
6240 
mvpp2_pcs_gmac_to_port(struct phylink_pcs * pcs)6241 static struct mvpp2_port *mvpp2_pcs_gmac_to_port(struct phylink_pcs *pcs)
6242 {
6243 	return container_of(pcs, struct mvpp2_port, pcs_gmac);
6244 }
6245 
mvpp2_xjg_pcs_inband_caps(struct phylink_pcs * pcs,phy_interface_t interface)6246 static unsigned int mvpp2_xjg_pcs_inband_caps(struct phylink_pcs *pcs,
6247 					      phy_interface_t interface)
6248 {
6249 	return LINK_INBAND_DISABLE;
6250 }
6251 
mvpp2_xlg_pcs_get_state(struct phylink_pcs * pcs,unsigned int neg_mode,struct phylink_link_state * state)6252 static void mvpp2_xlg_pcs_get_state(struct phylink_pcs *pcs,
6253 				    unsigned int neg_mode,
6254 				    struct phylink_link_state *state)
6255 {
6256 	struct mvpp2_port *port = mvpp2_pcs_xlg_to_port(pcs);
6257 	u32 val;
6258 
6259 	if (port->phy_interface == PHY_INTERFACE_MODE_5GBASER)
6260 		state->speed = SPEED_5000;
6261 	else
6262 		state->speed = SPEED_10000;
6263 	state->duplex = 1;
6264 	state->an_complete = 1;
6265 
6266 	val = readl(port->base + MVPP22_XLG_STATUS);
6267 	state->link = !!(val & MVPP22_XLG_STATUS_LINK_UP);
6268 
6269 	state->pause = 0;
6270 	val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6271 	if (val & MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN)
6272 		state->pause |= MLO_PAUSE_TX;
6273 	if (val & MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN)
6274 		state->pause |= MLO_PAUSE_RX;
6275 }
6276 
mvpp2_xlg_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)6277 static int mvpp2_xlg_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
6278 				phy_interface_t interface,
6279 				const unsigned long *advertising,
6280 				bool permit_pause_to_mac)
6281 {
6282 	return 0;
6283 }
6284 
6285 static const struct phylink_pcs_ops mvpp2_phylink_xlg_pcs_ops = {
6286 	.pcs_inband_caps = mvpp2_xjg_pcs_inband_caps,
6287 	.pcs_get_state = mvpp2_xlg_pcs_get_state,
6288 	.pcs_config = mvpp2_xlg_pcs_config,
6289 };
6290 
mvpp2_gmac_pcs_inband_caps(struct phylink_pcs * pcs,phy_interface_t interface)6291 static unsigned int mvpp2_gmac_pcs_inband_caps(struct phylink_pcs *pcs,
6292 					       phy_interface_t interface)
6293 {
6294 	/* When operating in an 802.3z mode, we must have AN enabled:
6295 	 * Bit 2 Field InBandAnEn In-band Auto-Negotiation enable. ...
6296 	 * When <PortType> = 1 (1000BASE-X) this field must be set to 1.
6297 	 * Therefore, inband is "required".
6298 	 */
6299 	if (phy_interface_mode_is_8023z(interface))
6300 		return LINK_INBAND_ENABLE;
6301 
6302 	/* SGMII and RGMII can be configured to use inband signalling of the
6303 	 * AN result. Indicate these as "possible".
6304 	 */
6305 	if (interface == PHY_INTERFACE_MODE_SGMII ||
6306 	    phy_interface_mode_is_rgmii(interface))
6307 		return LINK_INBAND_DISABLE | LINK_INBAND_ENABLE;
6308 
6309 	/* For any other modes, indicate that inband is not supported. */
6310 	return LINK_INBAND_DISABLE;
6311 }
6312 
mvpp2_gmac_pcs_get_state(struct phylink_pcs * pcs,unsigned int neg_mode,struct phylink_link_state * state)6313 static void mvpp2_gmac_pcs_get_state(struct phylink_pcs *pcs,
6314 				     unsigned int neg_mode,
6315 				     struct phylink_link_state *state)
6316 {
6317 	struct mvpp2_port *port = mvpp2_pcs_gmac_to_port(pcs);
6318 	u32 val;
6319 
6320 	val = readl(port->base + MVPP2_GMAC_STATUS0);
6321 
6322 	state->an_complete = !!(val & MVPP2_GMAC_STATUS0_AN_COMPLETE);
6323 	state->link = !!(val & MVPP2_GMAC_STATUS0_LINK_UP);
6324 	state->duplex = !!(val & MVPP2_GMAC_STATUS0_FULL_DUPLEX);
6325 
6326 	switch (port->phy_interface) {
6327 	case PHY_INTERFACE_MODE_1000BASEX:
6328 		state->speed = SPEED_1000;
6329 		break;
6330 	case PHY_INTERFACE_MODE_2500BASEX:
6331 		state->speed = SPEED_2500;
6332 		break;
6333 	default:
6334 		if (val & MVPP2_GMAC_STATUS0_GMII_SPEED)
6335 			state->speed = SPEED_1000;
6336 		else if (val & MVPP2_GMAC_STATUS0_MII_SPEED)
6337 			state->speed = SPEED_100;
6338 		else
6339 			state->speed = SPEED_10;
6340 	}
6341 
6342 	state->pause = 0;
6343 	if (val & MVPP2_GMAC_STATUS0_RX_PAUSE)
6344 		state->pause |= MLO_PAUSE_RX;
6345 	if (val & MVPP2_GMAC_STATUS0_TX_PAUSE)
6346 		state->pause |= MLO_PAUSE_TX;
6347 }
6348 
mvpp2_gmac_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)6349 static int mvpp2_gmac_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
6350 				 phy_interface_t interface,
6351 				 const unsigned long *advertising,
6352 				 bool permit_pause_to_mac)
6353 {
6354 	struct mvpp2_port *port = mvpp2_pcs_gmac_to_port(pcs);
6355 	u32 mask, val, an, old_an, changed;
6356 
6357 	mask = MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
6358 	       MVPP2_GMAC_IN_BAND_AUTONEG |
6359 	       MVPP2_GMAC_AN_SPEED_EN |
6360 	       MVPP2_GMAC_FLOW_CTRL_AUTONEG |
6361 	       MVPP2_GMAC_AN_DUPLEX_EN;
6362 
6363 	if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
6364 		mask |= MVPP2_GMAC_CONFIG_MII_SPEED |
6365 			MVPP2_GMAC_CONFIG_GMII_SPEED |
6366 			MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6367 		val = MVPP2_GMAC_IN_BAND_AUTONEG;
6368 
6369 		if (interface == PHY_INTERFACE_MODE_SGMII) {
6370 			/* SGMII mode receives the speed and duplex from PHY */
6371 			val |= MVPP2_GMAC_AN_SPEED_EN |
6372 			       MVPP2_GMAC_AN_DUPLEX_EN;
6373 		} else {
6374 			/* 802.3z mode has fixed speed and duplex */
6375 			val |= MVPP2_GMAC_CONFIG_GMII_SPEED |
6376 			       MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6377 
6378 			/* The FLOW_CTRL_AUTONEG bit selects either the hardware
6379 			 * automatically or the bits in MVPP22_GMAC_CTRL_4_REG
6380 			 * manually controls the GMAC pause modes.
6381 			 */
6382 			if (permit_pause_to_mac)
6383 				val |= MVPP2_GMAC_FLOW_CTRL_AUTONEG;
6384 
6385 			/* Configure advertisement bits */
6386 			mask |= MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN;
6387 			if (phylink_test(advertising, Pause))
6388 				val |= MVPP2_GMAC_FC_ADV_EN;
6389 			if (phylink_test(advertising, Asym_Pause))
6390 				val |= MVPP2_GMAC_FC_ADV_ASM_EN;
6391 		}
6392 	} else {
6393 		val = 0;
6394 	}
6395 
6396 	old_an = an = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6397 	an = (an & ~mask) | val;
6398 	changed = an ^ old_an;
6399 	if (changed)
6400 		writel(an, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6401 
6402 	/* We are only interested in the advertisement bits changing */
6403 	return changed & (MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN);
6404 }
6405 
mvpp2_gmac_pcs_an_restart(struct phylink_pcs * pcs)6406 static void mvpp2_gmac_pcs_an_restart(struct phylink_pcs *pcs)
6407 {
6408 	struct mvpp2_port *port = mvpp2_pcs_gmac_to_port(pcs);
6409 	u32 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6410 
6411 	writel(val | MVPP2_GMAC_IN_BAND_RESTART_AN,
6412 	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6413 	writel(val & ~MVPP2_GMAC_IN_BAND_RESTART_AN,
6414 	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6415 }
6416 
6417 static const struct phylink_pcs_ops mvpp2_phylink_gmac_pcs_ops = {
6418 	.pcs_inband_caps = mvpp2_gmac_pcs_inband_caps,
6419 	.pcs_get_state = mvpp2_gmac_pcs_get_state,
6420 	.pcs_config = mvpp2_gmac_pcs_config,
6421 	.pcs_an_restart = mvpp2_gmac_pcs_an_restart,
6422 };
6423 
mvpp2_xlg_config(struct mvpp2_port * port,unsigned int mode,const struct phylink_link_state * state)6424 static void mvpp2_xlg_config(struct mvpp2_port *port, unsigned int mode,
6425 			     const struct phylink_link_state *state)
6426 {
6427 	u32 val;
6428 
6429 	mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6430 		     MVPP22_XLG_CTRL0_MAC_RESET_DIS,
6431 		     MVPP22_XLG_CTRL0_MAC_RESET_DIS);
6432 	mvpp2_modify(port->base + MVPP22_XLG_CTRL4_REG,
6433 		     MVPP22_XLG_CTRL4_MACMODSELECT_GMAC |
6434 		     MVPP22_XLG_CTRL4_EN_IDLE_CHECK |
6435 		     MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC,
6436 		     MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC);
6437 
6438 	/* Wait for reset to deassert */
6439 	do {
6440 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6441 	} while (!(val & MVPP22_XLG_CTRL0_MAC_RESET_DIS));
6442 }
6443 
mvpp2_gmac_config(struct mvpp2_port * port,unsigned int mode,const struct phylink_link_state * state)6444 static void mvpp2_gmac_config(struct mvpp2_port *port, unsigned int mode,
6445 			      const struct phylink_link_state *state)
6446 {
6447 	u32 old_ctrl0, ctrl0;
6448 	u32 old_ctrl2, ctrl2;
6449 	u32 old_ctrl4, ctrl4;
6450 
6451 	old_ctrl0 = ctrl0 = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
6452 	old_ctrl2 = ctrl2 = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
6453 	old_ctrl4 = ctrl4 = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
6454 
6455 	ctrl0 &= ~MVPP2_GMAC_PORT_TYPE_MASK;
6456 	ctrl2 &= ~(MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_FLOW_CTRL_MASK);
6457 
6458 	/* Configure port type */
6459 	if (phy_interface_mode_is_8023z(state->interface)) {
6460 		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK;
6461 		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
6462 		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
6463 			 MVPP22_CTRL4_DP_CLK_SEL |
6464 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6465 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
6466 		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_INBAND_AN_MASK;
6467 		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
6468 		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
6469 			 MVPP22_CTRL4_DP_CLK_SEL |
6470 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6471 	} else if (phy_interface_mode_is_rgmii(state->interface)) {
6472 		ctrl4 &= ~MVPP22_CTRL4_DP_CLK_SEL;
6473 		ctrl4 |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
6474 			 MVPP22_CTRL4_SYNC_BYPASS_DIS |
6475 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6476 	}
6477 
6478 	/* Configure negotiation style */
6479 	if (!phylink_autoneg_inband(mode)) {
6480 		/* Phy or fixed speed - no in-band AN, nothing to do, leave the
6481 		 * configured speed, duplex and flow control as-is.
6482 		 */
6483 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
6484 		/* SGMII in-band mode receives the speed and duplex from
6485 		 * the PHY. Flow control information is not received. */
6486 	} else if (phy_interface_mode_is_8023z(state->interface)) {
6487 		/* 1000BaseX and 2500BaseX ports cannot negotiate speed nor can
6488 		 * they negotiate duplex: they are always operating with a fixed
6489 		 * speed of 1000/2500Mbps in full duplex, so force 1000/2500
6490 		 * speed and full duplex here.
6491 		 */
6492 		ctrl0 |= MVPP2_GMAC_PORT_TYPE_MASK;
6493 	}
6494 
6495 	if (old_ctrl0 != ctrl0)
6496 		writel(ctrl0, port->base + MVPP2_GMAC_CTRL_0_REG);
6497 	if (old_ctrl2 != ctrl2)
6498 		writel(ctrl2, port->base + MVPP2_GMAC_CTRL_2_REG);
6499 	if (old_ctrl4 != ctrl4)
6500 		writel(ctrl4, port->base + MVPP22_GMAC_CTRL_4_REG);
6501 }
6502 
mvpp2_select_pcs(struct phylink_config * config,phy_interface_t interface)6503 static struct phylink_pcs *mvpp2_select_pcs(struct phylink_config *config,
6504 					    phy_interface_t interface)
6505 {
6506 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6507 
6508 	/* Select the appropriate PCS operations depending on the
6509 	 * configured interface mode. We will only switch to a mode
6510 	 * that the validate() checks have already passed.
6511 	 */
6512 	if (mvpp2_is_xlg(interface))
6513 		return &port->pcs_xlg;
6514 	else
6515 		return &port->pcs_gmac;
6516 }
6517 
mvpp2_mac_prepare(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6518 static int mvpp2_mac_prepare(struct phylink_config *config, unsigned int mode,
6519 			     phy_interface_t interface)
6520 {
6521 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6522 
6523 	/* Check for invalid configuration */
6524 	if (mvpp2_is_xlg(interface) && port->gop_id != 0) {
6525 		netdev_err(port->dev, "Invalid mode on %s\n", port->dev->name);
6526 		return -EINVAL;
6527 	}
6528 
6529 	if (port->phy_interface != interface ||
6530 	    phylink_autoneg_inband(mode)) {
6531 		/* Force the link down when changing the interface or if in
6532 		 * in-band mode to ensure we do not change the configuration
6533 		 * while the hardware is indicating link is up. We force both
6534 		 * XLG and GMAC down to ensure that they're both in a known
6535 		 * state.
6536 		 */
6537 		mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6538 			     MVPP2_GMAC_FORCE_LINK_PASS |
6539 			     MVPP2_GMAC_FORCE_LINK_DOWN,
6540 			     MVPP2_GMAC_FORCE_LINK_DOWN);
6541 
6542 		if (mvpp2_port_supports_xlg(port))
6543 			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6544 				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6545 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN,
6546 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN);
6547 	}
6548 
6549 	/* Make sure the port is disabled when reconfiguring the mode */
6550 	mvpp2_port_disable(port);
6551 
6552 	if (port->phy_interface != interface) {
6553 		/* Place GMAC into reset */
6554 		mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
6555 			     MVPP2_GMAC_PORT_RESET_MASK,
6556 			     MVPP2_GMAC_PORT_RESET_MASK);
6557 
6558 		if (port->priv->hw_version >= MVPP22) {
6559 			mvpp22_gop_mask_irq(port);
6560 
6561 			phy_power_off(port->comphy);
6562 
6563 			/* Reconfigure the serdes lanes */
6564 			mvpp22_mode_reconfigure(port, interface);
6565 		}
6566 	}
6567 
6568 	return 0;
6569 }
6570 
mvpp2_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)6571 static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode,
6572 			     const struct phylink_link_state *state)
6573 {
6574 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6575 
6576 	/* mac (re)configuration */
6577 	if (mvpp2_is_xlg(state->interface))
6578 		mvpp2_xlg_config(port, mode, state);
6579 	else if (phy_interface_mode_is_rgmii(state->interface) ||
6580 		 phy_interface_mode_is_8023z(state->interface) ||
6581 		 state->interface == PHY_INTERFACE_MODE_SGMII)
6582 		mvpp2_gmac_config(port, mode, state);
6583 
6584 	if (port->priv->hw_version == MVPP21 && port->flags & MVPP2_F_LOOPBACK)
6585 		mvpp2_port_loopback_set(port, state);
6586 }
6587 
mvpp2_mac_finish(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6588 static int mvpp2_mac_finish(struct phylink_config *config, unsigned int mode,
6589 			    phy_interface_t interface)
6590 {
6591 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6592 
6593 	if (port->priv->hw_version >= MVPP22 &&
6594 	    port->phy_interface != interface) {
6595 		port->phy_interface = interface;
6596 
6597 		/* Unmask interrupts */
6598 		mvpp22_gop_unmask_irq(port);
6599 	}
6600 
6601 	if (!mvpp2_is_xlg(interface)) {
6602 		/* Release GMAC reset and wait */
6603 		mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
6604 			     MVPP2_GMAC_PORT_RESET_MASK, 0);
6605 
6606 		while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
6607 		       MVPP2_GMAC_PORT_RESET_MASK)
6608 			continue;
6609 	}
6610 
6611 	mvpp2_port_enable(port);
6612 
6613 	/* Allow the link to come up if in in-band mode, otherwise the
6614 	 * link is forced via mac_link_down()/mac_link_up()
6615 	 */
6616 	if (phylink_autoneg_inband(mode)) {
6617 		if (mvpp2_is_xlg(interface))
6618 			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6619 				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6620 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN, 0);
6621 		else
6622 			mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6623 				     MVPP2_GMAC_FORCE_LINK_PASS |
6624 				     MVPP2_GMAC_FORCE_LINK_DOWN, 0);
6625 	}
6626 
6627 	return 0;
6628 }
6629 
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)6630 static void mvpp2_mac_link_up(struct phylink_config *config,
6631 			      struct phy_device *phy,
6632 			      unsigned int mode, phy_interface_t interface,
6633 			      int speed, int duplex,
6634 			      bool tx_pause, bool rx_pause)
6635 {
6636 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6637 	u32 val;
6638 	int i;
6639 
6640 	if (mvpp2_is_xlg(interface)) {
6641 		if (!phylink_autoneg_inband(mode)) {
6642 			val = MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6643 			if (tx_pause)
6644 				val |= MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN;
6645 			if (rx_pause)
6646 				val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
6647 
6648 			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6649 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN |
6650 				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6651 				     MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN |
6652 				     MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN, val);
6653 		}
6654 	} else {
6655 		if (!phylink_autoneg_inband(mode)) {
6656 			val = MVPP2_GMAC_FORCE_LINK_PASS;
6657 
6658 			if (speed == SPEED_1000 || speed == SPEED_2500)
6659 				val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6660 			else if (speed == SPEED_100)
6661 				val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6662 
6663 			if (duplex == DUPLEX_FULL)
6664 				val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6665 
6666 			mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6667 				     MVPP2_GMAC_FORCE_LINK_DOWN |
6668 				     MVPP2_GMAC_FORCE_LINK_PASS |
6669 				     MVPP2_GMAC_CONFIG_MII_SPEED |
6670 				     MVPP2_GMAC_CONFIG_GMII_SPEED |
6671 				     MVPP2_GMAC_CONFIG_FULL_DUPLEX, val);
6672 		}
6673 
6674 		/* We can always update the flow control enable bits;
6675 		 * these will only be effective if flow control AN
6676 		 * (MVPP2_GMAC_FLOW_CTRL_AUTONEG) is disabled.
6677 		 */
6678 		val = 0;
6679 		if (tx_pause)
6680 			val |= MVPP22_CTRL4_TX_FC_EN;
6681 		if (rx_pause)
6682 			val |= MVPP22_CTRL4_RX_FC_EN;
6683 
6684 		mvpp2_modify(port->base + MVPP22_GMAC_CTRL_4_REG,
6685 			     MVPP22_CTRL4_RX_FC_EN | MVPP22_CTRL4_TX_FC_EN,
6686 			     val);
6687 	}
6688 
6689 	if (port->priv->global_tx_fc) {
6690 		port->tx_fc = tx_pause;
6691 		if (tx_pause)
6692 			mvpp2_rxq_enable_fc(port);
6693 		else
6694 			mvpp2_rxq_disable_fc(port);
6695 		if (port->priv->percpu_pools) {
6696 			for (i = 0; i < port->nrxqs; i++)
6697 				mvpp2_bm_pool_update_fc(port, &port->priv->bm_pools[i], tx_pause);
6698 		} else {
6699 			mvpp2_bm_pool_update_fc(port, port->pool_long, tx_pause);
6700 			mvpp2_bm_pool_update_fc(port, port->pool_short, tx_pause);
6701 		}
6702 		if (port->priv->hw_version == MVPP23)
6703 			mvpp23_rx_fifo_fc_en(port->priv, port->id, tx_pause);
6704 	}
6705 
6706 	mvpp2_port_enable(port);
6707 
6708 	mvpp2_egress_enable(port);
6709 	mvpp2_ingress_enable(port);
6710 	netif_tx_wake_all_queues(port->dev);
6711 }
6712 
mvpp2_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6713 static void mvpp2_mac_link_down(struct phylink_config *config,
6714 				unsigned int mode, phy_interface_t interface)
6715 {
6716 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6717 	u32 val;
6718 
6719 	if (!phylink_autoneg_inband(mode)) {
6720 		if (mvpp2_is_xlg(interface)) {
6721 			val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6722 			val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6723 			val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
6724 			writel(val, port->base + MVPP22_XLG_CTRL0_REG);
6725 		} else {
6726 			val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6727 			val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
6728 			val |= MVPP2_GMAC_FORCE_LINK_DOWN;
6729 			writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6730 		}
6731 	}
6732 
6733 	netif_tx_stop_all_queues(port->dev);
6734 	mvpp2_egress_disable(port);
6735 	mvpp2_ingress_disable(port);
6736 
6737 	mvpp2_port_disable(port);
6738 }
6739 
mvpp2_mac_disable_tx_lpi(struct phylink_config * config)6740 static void mvpp2_mac_disable_tx_lpi(struct phylink_config *config)
6741 {
6742 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6743 
6744 	mvpp2_modify(port->base + MVPP2_GMAC_LPI_CTRL1,
6745 		     MVPP2_GMAC_LPI_CTRL1_REQ_EN, 0);
6746 }
6747 
mvpp2_mac_enable_tx_lpi(struct phylink_config * config,u32 timer,bool tx_clk_stop)6748 static int mvpp2_mac_enable_tx_lpi(struct phylink_config *config, u32 timer,
6749 				   bool tx_clk_stop)
6750 {
6751 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6752 	u32 ts, tw, lpi1, status;
6753 
6754 	status = readl(port->base + MVPP2_GMAC_STATUS0);
6755 	if (status & MVPP2_GMAC_STATUS0_GMII_SPEED) {
6756 		/* At 1G speeds, the timer resolution are 1us, and
6757 		 * 802.3 says tw is 16.5us. Round up to 17us.
6758 		 */
6759 		tw = 17;
6760 		ts = timer;
6761 	} else {
6762 		/* At 100M speeds, the timer resolutions are 10us, and
6763 		 * 802.3 says tw is 30us.
6764 		 */
6765 		tw = 3;
6766 		ts = DIV_ROUND_UP(timer, 10);
6767 	}
6768 
6769 	if (ts > 255)
6770 		ts = 255;
6771 
6772 	/* Configure ts */
6773 	mvpp2_modify(port->base + MVPP2_GMAC_LPI_CTRL0,
6774 		     MVPP2_GMAC_LPI_CTRL0_TS_MASK,
6775 		     FIELD_PREP(MVPP2_GMAC_LPI_CTRL0_TS_MASK, ts));
6776 
6777 	lpi1 = readl(port->base + MVPP2_GMAC_LPI_CTRL1);
6778 
6779 	/* Configure tw */
6780 	lpi1 = u32_replace_bits(lpi1, tw, MVPP2_GMAC_LPI_CTRL1_TW_MASK);
6781 
6782 	/* Enable LPI generation */
6783 	writel(lpi1 | MVPP2_GMAC_LPI_CTRL1_REQ_EN,
6784 	       port->base + MVPP2_GMAC_LPI_CTRL1);
6785 
6786 	return 0;
6787 }
6788 
6789 static const struct phylink_mac_ops mvpp2_phylink_ops = {
6790 	.mac_select_pcs = mvpp2_select_pcs,
6791 	.mac_prepare = mvpp2_mac_prepare,
6792 	.mac_config = mvpp2_mac_config,
6793 	.mac_finish = mvpp2_mac_finish,
6794 	.mac_link_up = mvpp2_mac_link_up,
6795 	.mac_link_down = mvpp2_mac_link_down,
6796 	.mac_enable_tx_lpi = mvpp2_mac_enable_tx_lpi,
6797 	.mac_disable_tx_lpi = mvpp2_mac_disable_tx_lpi,
6798 };
6799 
6800 /* Work-around for ACPI */
mvpp2_acpi_start(struct mvpp2_port * port)6801 static void mvpp2_acpi_start(struct mvpp2_port *port)
6802 {
6803 	/* Phylink isn't used as of now for ACPI, so the MAC has to be
6804 	 * configured manually when the interface is started. This will
6805 	 * be removed as soon as the phylink ACPI support lands in.
6806 	 */
6807 	struct phylink_link_state state = {
6808 		.interface = port->phy_interface,
6809 	};
6810 	struct phylink_pcs *pcs;
6811 
6812 	pcs = mvpp2_select_pcs(&port->phylink_config, port->phy_interface);
6813 
6814 	mvpp2_mac_prepare(&port->phylink_config, MLO_AN_INBAND,
6815 			  port->phy_interface);
6816 	mvpp2_mac_config(&port->phylink_config, MLO_AN_INBAND, &state);
6817 	pcs->ops->pcs_config(pcs, PHYLINK_PCS_NEG_INBAND_ENABLED,
6818 			     port->phy_interface, state.advertising,
6819 			     false);
6820 	mvpp2_mac_finish(&port->phylink_config, MLO_AN_INBAND,
6821 			 port->phy_interface);
6822 	mvpp2_mac_link_up(&port->phylink_config, NULL,
6823 			  MLO_AN_INBAND, port->phy_interface,
6824 			  SPEED_UNKNOWN, DUPLEX_UNKNOWN, false, false);
6825 }
6826 
6827 /* In order to ensure backward compatibility for ACPI, check if the port
6828  * firmware node comprises the necessary description allowing to use phylink.
6829  */
mvpp2_use_acpi_compat_mode(struct fwnode_handle * port_fwnode)6830 static bool mvpp2_use_acpi_compat_mode(struct fwnode_handle *port_fwnode)
6831 {
6832 	if (!is_acpi_node(port_fwnode))
6833 		return false;
6834 
6835 	return (!fwnode_property_present(port_fwnode, "phy-handle") &&
6836 		!fwnode_property_present(port_fwnode, "managed") &&
6837 		!fwnode_get_named_child_node(port_fwnode, "fixed-link"));
6838 }
6839 
6840 /* Ports initialization */
mvpp2_port_probe(struct platform_device * pdev,struct fwnode_handle * port_fwnode,struct mvpp2 * priv)6841 static int mvpp2_port_probe(struct platform_device *pdev,
6842 			    struct fwnode_handle *port_fwnode,
6843 			    struct mvpp2 *priv)
6844 {
6845 	struct phy *comphy = NULL;
6846 	struct mvpp2_port *port;
6847 	struct mvpp2_port_pcpu *port_pcpu;
6848 	struct device_node *port_node = to_of_node(port_fwnode);
6849 	netdev_features_t features;
6850 	struct net_device *dev;
6851 	struct phylink *phylink;
6852 	char *mac_from = "";
6853 	unsigned int ntxqs, nrxqs, thread;
6854 	unsigned long flags = 0;
6855 	bool has_tx_irqs;
6856 	u32 id;
6857 	int phy_mode;
6858 	int err, i;
6859 
6860 	has_tx_irqs = mvpp2_port_has_irqs(priv, port_node, &flags);
6861 	if (!has_tx_irqs && queue_mode == MVPP2_QDIST_MULTI_MODE) {
6862 		dev_err(&pdev->dev,
6863 			"not enough IRQs to support multi queue mode\n");
6864 		return -EINVAL;
6865 	}
6866 
6867 	ntxqs = MVPP2_MAX_TXQ;
6868 	nrxqs = mvpp2_get_nrxqs(priv);
6869 
6870 	dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
6871 	if (!dev)
6872 		return -ENOMEM;
6873 
6874 	phy_mode = fwnode_get_phy_mode(port_fwnode);
6875 	if (phy_mode < 0) {
6876 		dev_err(&pdev->dev, "incorrect phy mode\n");
6877 		err = phy_mode;
6878 		goto err_free_netdev;
6879 	}
6880 
6881 	/*
6882 	 * Rewrite 10GBASE-KR to 10GBASE-R for compatibility with existing DT.
6883 	 * Existing usage of 10GBASE-KR is not correct; no backplane
6884 	 * negotiation is done, and this driver does not actually support
6885 	 * 10GBASE-KR.
6886 	 */
6887 	if (phy_mode == PHY_INTERFACE_MODE_10GKR)
6888 		phy_mode = PHY_INTERFACE_MODE_10GBASER;
6889 
6890 	if (port_node) {
6891 		comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
6892 		if (IS_ERR(comphy)) {
6893 			if (PTR_ERR(comphy) == -EPROBE_DEFER) {
6894 				err = -EPROBE_DEFER;
6895 				goto err_free_netdev;
6896 			}
6897 			comphy = NULL;
6898 		}
6899 	}
6900 
6901 	if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
6902 		err = -EINVAL;
6903 		dev_err(&pdev->dev, "missing port-id value\n");
6904 		goto err_free_netdev;
6905 	}
6906 
6907 	dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
6908 	dev->watchdog_timeo = 5 * HZ;
6909 	dev->netdev_ops = &mvpp2_netdev_ops;
6910 	dev->ethtool_ops = &mvpp2_eth_tool_ops;
6911 
6912 	port = netdev_priv(dev);
6913 	port->dev = dev;
6914 	port->fwnode = port_fwnode;
6915 	port->ntxqs = ntxqs;
6916 	port->nrxqs = nrxqs;
6917 	port->priv = priv;
6918 	port->has_tx_irqs = has_tx_irqs;
6919 	port->flags = flags;
6920 
6921 	err = mvpp2_queue_vectors_init(port, port_node);
6922 	if (err)
6923 		goto err_free_netdev;
6924 
6925 	if (port_node)
6926 		port->port_irq = of_irq_get_byname(port_node, "link");
6927 	else
6928 		port->port_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
6929 	if (port->port_irq == -EPROBE_DEFER) {
6930 		err = -EPROBE_DEFER;
6931 		goto err_deinit_qvecs;
6932 	}
6933 	if (port->port_irq <= 0)
6934 		/* the link irq is optional */
6935 		port->port_irq = 0;
6936 
6937 	if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
6938 		port->flags |= MVPP2_F_LOOPBACK;
6939 
6940 	port->id = id;
6941 	if (priv->hw_version == MVPP21)
6942 		port->first_rxq = port->id * port->nrxqs;
6943 	else
6944 		port->first_rxq = port->id * priv->max_port_rxqs;
6945 
6946 	port->of_node = port_node;
6947 	port->phy_interface = phy_mode;
6948 	port->comphy = comphy;
6949 
6950 	if (priv->hw_version == MVPP21) {
6951 		port->base = devm_platform_ioremap_resource(pdev, 2 + id);
6952 		if (IS_ERR(port->base)) {
6953 			err = PTR_ERR(port->base);
6954 			goto err_free_irq;
6955 		}
6956 
6957 		port->stats_base = port->priv->lms_base +
6958 				   MVPP21_MIB_COUNTERS_OFFSET +
6959 				   port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
6960 	} else {
6961 		if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
6962 					     &port->gop_id)) {
6963 			err = -EINVAL;
6964 			dev_err(&pdev->dev, "missing gop-port-id value\n");
6965 			goto err_deinit_qvecs;
6966 		}
6967 
6968 		port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
6969 		port->stats_base = port->priv->iface_base +
6970 				   MVPP22_MIB_COUNTERS_OFFSET +
6971 				   port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
6972 
6973 		/* We may want a property to describe whether we should use
6974 		 * MAC hardware timestamping.
6975 		 */
6976 		if (priv->tai)
6977 			port->hwtstamp = true;
6978 	}
6979 
6980 	/* Alloc per-cpu and ethtool stats */
6981 	port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6982 	if (!port->stats) {
6983 		err = -ENOMEM;
6984 		goto err_free_irq;
6985 	}
6986 
6987 	port->ethtool_stats = devm_kcalloc(&pdev->dev,
6988 					   MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs),
6989 					   sizeof(u64), GFP_KERNEL);
6990 	if (!port->ethtool_stats) {
6991 		err = -ENOMEM;
6992 		goto err_free_stats;
6993 	}
6994 
6995 	mutex_init(&port->gather_stats_lock);
6996 	INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
6997 
6998 	err = mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
6999 	if (err < 0)
7000 		goto err_free_stats;
7001 
7002 	port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
7003 	port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
7004 	SET_NETDEV_DEV(dev, &pdev->dev);
7005 
7006 	err = mvpp2_port_init(port);
7007 	if (err < 0) {
7008 		dev_err(&pdev->dev, "failed to init port %d\n", id);
7009 		goto err_free_stats;
7010 	}
7011 
7012 	mvpp2_port_periodic_xon_disable(port);
7013 
7014 	mvpp2_mac_reset_assert(port);
7015 	mvpp22_pcs_reset_assert(port);
7016 
7017 	port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7018 	if (!port->pcpu) {
7019 		err = -ENOMEM;
7020 		goto err_free_txq_pcpu;
7021 	}
7022 
7023 	if (!port->has_tx_irqs) {
7024 		for (thread = 0; thread < priv->nthreads; thread++) {
7025 			port_pcpu = per_cpu_ptr(port->pcpu, thread);
7026 
7027 			hrtimer_setup(&port_pcpu->tx_done_timer, mvpp2_hr_timer_cb, CLOCK_MONOTONIC,
7028 				      HRTIMER_MODE_REL_PINNED_SOFT);
7029 			port_pcpu->timer_scheduled = false;
7030 			port_pcpu->dev = dev;
7031 		}
7032 	}
7033 
7034 	features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
7035 		   NETIF_F_TSO;
7036 	dev->features = features | NETIF_F_RXCSUM;
7037 	dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
7038 			    NETIF_F_HW_VLAN_CTAG_FILTER;
7039 
7040 	if (mvpp22_rss_is_supported(port)) {
7041 		dev->hw_features |= NETIF_F_RXHASH;
7042 		dev->features |= NETIF_F_NTUPLE;
7043 	}
7044 
7045 	if (!port->priv->percpu_pools)
7046 		mvpp2_set_hw_csum(port, port->pool_long->id);
7047 	else if (port->ntxqs >= num_possible_cpus() * 2)
7048 		dev->xdp_features = NETDEV_XDP_ACT_BASIC |
7049 				    NETDEV_XDP_ACT_REDIRECT |
7050 				    NETDEV_XDP_ACT_NDO_XMIT;
7051 
7052 	dev->vlan_features |= features;
7053 	netif_set_tso_max_segs(dev, MVPP2_MAX_TSO_SEGS);
7054 
7055 	dev->priv_flags |= IFF_UNICAST_FLT;
7056 
7057 	/* MTU range: 68 - 9704 */
7058 	dev->min_mtu = ETH_MIN_MTU;
7059 	/* 9704 == 9728 - 20 and rounding to 8 */
7060 	dev->max_mtu = MVPP2_BM_JUMBO_PKT_SIZE;
7061 	device_set_node(&dev->dev, port_fwnode);
7062 	dev->dev_port = port->id;
7063 
7064 	port->pcs_gmac.ops = &mvpp2_phylink_gmac_pcs_ops;
7065 	port->pcs_xlg.ops = &mvpp2_phylink_xlg_pcs_ops;
7066 
7067 	if (!mvpp2_use_acpi_compat_mode(port_fwnode)) {
7068 		port->phylink_config.dev = &dev->dev;
7069 		port->phylink_config.type = PHYLINK_NETDEV;
7070 		port->phylink_config.mac_capabilities =
7071 			MAC_2500FD | MAC_1000FD | MAC_100 | MAC_10;
7072 
7073 		__set_bit(PHY_INTERFACE_MODE_SGMII,
7074 			  port->phylink_config.lpi_interfaces);
7075 
7076 		port->phylink_config.lpi_capabilities = MAC_1000FD | MAC_100FD;
7077 
7078 		/* Setup EEE.  Choose 250us idle. */
7079 		port->phylink_config.lpi_timer_default = 250;
7080 		port->phylink_config.eee_enabled_default = true;
7081 
7082 		if (port->priv->global_tx_fc)
7083 			port->phylink_config.mac_capabilities |=
7084 				MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
7085 
7086 		if (mvpp2_port_supports_xlg(port)) {
7087 			/* If a COMPHY is present, we can support any of
7088 			 * the serdes modes and switch between them.
7089 			 */
7090 			if (comphy) {
7091 				__set_bit(PHY_INTERFACE_MODE_5GBASER,
7092 					  port->phylink_config.supported_interfaces);
7093 				__set_bit(PHY_INTERFACE_MODE_10GBASER,
7094 					  port->phylink_config.supported_interfaces);
7095 				__set_bit(PHY_INTERFACE_MODE_XAUI,
7096 					  port->phylink_config.supported_interfaces);
7097 			} else if (phy_mode == PHY_INTERFACE_MODE_5GBASER) {
7098 				__set_bit(PHY_INTERFACE_MODE_5GBASER,
7099 					  port->phylink_config.supported_interfaces);
7100 			} else if (phy_mode == PHY_INTERFACE_MODE_10GBASER) {
7101 				__set_bit(PHY_INTERFACE_MODE_10GBASER,
7102 					  port->phylink_config.supported_interfaces);
7103 			} else if (phy_mode == PHY_INTERFACE_MODE_XAUI) {
7104 				__set_bit(PHY_INTERFACE_MODE_XAUI,
7105 					  port->phylink_config.supported_interfaces);
7106 			}
7107 
7108 			if (comphy)
7109 				port->phylink_config.mac_capabilities |=
7110 					MAC_10000FD | MAC_5000FD;
7111 			else if (phy_mode == PHY_INTERFACE_MODE_5GBASER)
7112 				port->phylink_config.mac_capabilities |=
7113 					MAC_5000FD;
7114 			else
7115 				port->phylink_config.mac_capabilities |=
7116 					MAC_10000FD;
7117 		}
7118 
7119 		if (mvpp2_port_supports_rgmii(port)) {
7120 			phy_interface_set_rgmii(port->phylink_config.supported_interfaces);
7121 			__set_bit(PHY_INTERFACE_MODE_MII,
7122 				  port->phylink_config.supported_interfaces);
7123 		}
7124 
7125 		if (comphy) {
7126 			/* If a COMPHY is present, we can support any of the
7127 			 * serdes modes and switch between them.
7128 			 */
7129 			__set_bit(PHY_INTERFACE_MODE_SGMII,
7130 				  port->phylink_config.supported_interfaces);
7131 			__set_bit(PHY_INTERFACE_MODE_1000BASEX,
7132 				  port->phylink_config.supported_interfaces);
7133 			__set_bit(PHY_INTERFACE_MODE_2500BASEX,
7134 				  port->phylink_config.supported_interfaces);
7135 		} else if (phy_mode == PHY_INTERFACE_MODE_2500BASEX) {
7136 			/* No COMPHY, with only 2500BASE-X mode supported */
7137 			__set_bit(PHY_INTERFACE_MODE_2500BASEX,
7138 				  port->phylink_config.supported_interfaces);
7139 		} else if (phy_mode == PHY_INTERFACE_MODE_1000BASEX ||
7140 			   phy_mode == PHY_INTERFACE_MODE_SGMII) {
7141 			/* No COMPHY, we can switch between 1000BASE-X and SGMII
7142 			 */
7143 			__set_bit(PHY_INTERFACE_MODE_1000BASEX,
7144 				  port->phylink_config.supported_interfaces);
7145 			__set_bit(PHY_INTERFACE_MODE_SGMII,
7146 				  port->phylink_config.supported_interfaces);
7147 		}
7148 
7149 		phylink = phylink_create(&port->phylink_config, port_fwnode,
7150 					 phy_mode, &mvpp2_phylink_ops);
7151 		if (IS_ERR(phylink)) {
7152 			err = PTR_ERR(phylink);
7153 			goto err_free_port_pcpu;
7154 		}
7155 		port->phylink = phylink;
7156 
7157 		mvpp2_mac_disable_tx_lpi(&port->phylink_config);
7158 	} else {
7159 		dev_warn(&pdev->dev, "Use link irqs for port#%d. FW update required\n", port->id);
7160 		port->phylink = NULL;
7161 	}
7162 
7163 	/* Cycle the comphy to power it down, saving 270mW per port -
7164 	 * don't worry about an error powering it up. When the comphy
7165 	 * driver does this, we can remove this code.
7166 	 */
7167 	if (port->comphy) {
7168 		err = mvpp22_comphy_init(port, port->phy_interface);
7169 		if (err == 0)
7170 			phy_power_off(port->comphy);
7171 	}
7172 
7173 	err = register_netdev(dev);
7174 	if (err < 0) {
7175 		dev_err(&pdev->dev, "failed to register netdev\n");
7176 		goto err_phylink;
7177 	}
7178 	netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7179 
7180 	priv->port_list[priv->port_count++] = port;
7181 
7182 	return 0;
7183 
7184 err_phylink:
7185 	if (port->phylink)
7186 		phylink_destroy(port->phylink);
7187 err_free_port_pcpu:
7188 	free_percpu(port->pcpu);
7189 err_free_txq_pcpu:
7190 	for (i = 0; i < port->ntxqs; i++)
7191 		free_percpu(port->txqs[i]->pcpu);
7192 err_free_stats:
7193 	free_percpu(port->stats);
7194 err_free_irq:
7195 	if (port->port_irq)
7196 		irq_dispose_mapping(port->port_irq);
7197 err_deinit_qvecs:
7198 	mvpp2_queue_vectors_deinit(port);
7199 err_free_netdev:
7200 	free_netdev(dev);
7201 	return err;
7202 }
7203 
7204 /* Ports removal routine */
mvpp2_port_remove(struct mvpp2_port * port)7205 static void mvpp2_port_remove(struct mvpp2_port *port)
7206 {
7207 	int i;
7208 
7209 	unregister_netdev(port->dev);
7210 	if (port->phylink)
7211 		phylink_destroy(port->phylink);
7212 	free_percpu(port->pcpu);
7213 	free_percpu(port->stats);
7214 	for (i = 0; i < port->ntxqs; i++)
7215 		free_percpu(port->txqs[i]->pcpu);
7216 	mvpp2_queue_vectors_deinit(port);
7217 	if (port->port_irq)
7218 		irq_dispose_mapping(port->port_irq);
7219 	free_netdev(port->dev);
7220 }
7221 
7222 /* Initialize decoding windows */
mvpp2_conf_mbus_windows(const struct mbus_dram_target_info * dram,struct mvpp2 * priv)7223 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7224 				    struct mvpp2 *priv)
7225 {
7226 	u32 win_enable;
7227 	int i;
7228 
7229 	for (i = 0; i < 6; i++) {
7230 		mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7231 		mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7232 
7233 		if (i < 4)
7234 			mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7235 	}
7236 
7237 	win_enable = 0;
7238 
7239 	for (i = 0; i < dram->num_cs; i++) {
7240 		const struct mbus_dram_window *cs = dram->cs + i;
7241 
7242 		mvpp2_write(priv, MVPP2_WIN_BASE(i),
7243 			    (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7244 			    dram->mbus_dram_target_id);
7245 
7246 		mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7247 			    (cs->size - 1) & 0xffff0000);
7248 
7249 		win_enable |= (1 << i);
7250 	}
7251 
7252 	mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7253 }
7254 
7255 /* Initialize Rx FIFO's */
mvpp2_rx_fifo_init(struct mvpp2 * priv)7256 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7257 {
7258 	int port;
7259 
7260 	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7261 		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7262 			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7263 		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7264 			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
7265 	}
7266 
7267 	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7268 		    MVPP2_RX_FIFO_PORT_MIN_PKT);
7269 	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7270 }
7271 
mvpp22_rx_fifo_set_hw(struct mvpp2 * priv,int port,int data_size)7272 static void mvpp22_rx_fifo_set_hw(struct mvpp2 *priv, int port, int data_size)
7273 {
7274 	int attr_size = MVPP2_RX_FIFO_PORT_ATTR_SIZE(data_size);
7275 
7276 	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port), data_size);
7277 	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port), attr_size);
7278 }
7279 
7280 /* Initialize TX FIFO's: the total FIFO size is 48kB on PPv2.2 and PPv2.3.
7281  * 4kB fixed space must be assigned for the loopback port.
7282  * Redistribute remaining avialable 44kB space among all active ports.
7283  * Guarantee minimum 32kB for 10G port and 8kB for port 1, capable of 2.5G
7284  * SGMII link.
7285  */
mvpp22_rx_fifo_init(struct mvpp2 * priv)7286 static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
7287 {
7288 	int remaining_ports_count;
7289 	unsigned long port_map;
7290 	int size_remainder;
7291 	int port, size;
7292 
7293 	/* The loopback requires fixed 4kB of the FIFO space assignment. */
7294 	mvpp22_rx_fifo_set_hw(priv, MVPP2_LOOPBACK_PORT_INDEX,
7295 			      MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7296 	port_map = priv->port_map & ~BIT(MVPP2_LOOPBACK_PORT_INDEX);
7297 
7298 	/* Set RX FIFO size to 0 for inactive ports. */
7299 	for_each_clear_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX)
7300 		mvpp22_rx_fifo_set_hw(priv, port, 0);
7301 
7302 	/* Assign remaining RX FIFO space among all active ports. */
7303 	size_remainder = MVPP2_RX_FIFO_PORT_DATA_SIZE_44KB;
7304 	remaining_ports_count = hweight_long(port_map);
7305 
7306 	for_each_set_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX) {
7307 		if (remaining_ports_count == 1)
7308 			size = size_remainder;
7309 		else if (port == 0)
7310 			size = max(size_remainder / remaining_ports_count,
7311 				   MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
7312 		else if (port == 1)
7313 			size = max(size_remainder / remaining_ports_count,
7314 				   MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
7315 		else
7316 			size = size_remainder / remaining_ports_count;
7317 
7318 		size_remainder -= size;
7319 		remaining_ports_count--;
7320 
7321 		mvpp22_rx_fifo_set_hw(priv, port, size);
7322 	}
7323 
7324 	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7325 		    MVPP2_RX_FIFO_PORT_MIN_PKT);
7326 	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7327 }
7328 
7329 /* Configure Rx FIFO Flow control thresholds */
mvpp23_rx_fifo_fc_set_tresh(struct mvpp2 * priv)7330 static void mvpp23_rx_fifo_fc_set_tresh(struct mvpp2 *priv)
7331 {
7332 	int port, val;
7333 
7334 	/* Port 0: maximum speed -10Gb/s port
7335 	 *	   required by spec RX FIFO threshold 9KB
7336 	 * Port 1: maximum speed -5Gb/s port
7337 	 *	   required by spec RX FIFO threshold 4KB
7338 	 * Port 2: maximum speed -1Gb/s port
7339 	 *	   required by spec RX FIFO threshold 2KB
7340 	 */
7341 
7342 	/* Without loopback port */
7343 	for (port = 0; port < (MVPP2_MAX_PORTS - 1); port++) {
7344 		if (port == 0) {
7345 			val = (MVPP23_PORT0_FIFO_TRSH / MVPP2_RX_FC_TRSH_UNIT)
7346 				<< MVPP2_RX_FC_TRSH_OFFS;
7347 			val &= MVPP2_RX_FC_TRSH_MASK;
7348 			mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7349 		} else if (port == 1) {
7350 			val = (MVPP23_PORT1_FIFO_TRSH / MVPP2_RX_FC_TRSH_UNIT)
7351 				<< MVPP2_RX_FC_TRSH_OFFS;
7352 			val &= MVPP2_RX_FC_TRSH_MASK;
7353 			mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7354 		} else {
7355 			val = (MVPP23_PORT2_FIFO_TRSH / MVPP2_RX_FC_TRSH_UNIT)
7356 				<< MVPP2_RX_FC_TRSH_OFFS;
7357 			val &= MVPP2_RX_FC_TRSH_MASK;
7358 			mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7359 		}
7360 	}
7361 }
7362 
7363 /* Configure Rx FIFO Flow control thresholds */
mvpp23_rx_fifo_fc_en(struct mvpp2 * priv,int port,bool en)7364 void mvpp23_rx_fifo_fc_en(struct mvpp2 *priv, int port, bool en)
7365 {
7366 	int val;
7367 
7368 	val = mvpp2_read(priv, MVPP2_RX_FC_REG(port));
7369 
7370 	if (en)
7371 		val |= MVPP2_RX_FC_EN;
7372 	else
7373 		val &= ~MVPP2_RX_FC_EN;
7374 
7375 	mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7376 }
7377 
mvpp22_tx_fifo_set_hw(struct mvpp2 * priv,int port,int size)7378 static void mvpp22_tx_fifo_set_hw(struct mvpp2 *priv, int port, int size)
7379 {
7380 	int threshold = MVPP2_TX_FIFO_THRESHOLD(size);
7381 
7382 	mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
7383 	mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), threshold);
7384 }
7385 
7386 /* Initialize TX FIFO's: the total FIFO size is 19kB on PPv2.2 and PPv2.3.
7387  * 1kB fixed space must be assigned for the loopback port.
7388  * Redistribute remaining avialable 18kB space among all active ports.
7389  * The 10G interface should use 10kB (which is maximum possible size
7390  * per single port).
7391  */
mvpp22_tx_fifo_init(struct mvpp2 * priv)7392 static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
7393 {
7394 	int remaining_ports_count;
7395 	unsigned long port_map;
7396 	int size_remainder;
7397 	int port, size;
7398 
7399 	/* The loopback requires fixed 1kB of the FIFO space assignment. */
7400 	mvpp22_tx_fifo_set_hw(priv, MVPP2_LOOPBACK_PORT_INDEX,
7401 			      MVPP22_TX_FIFO_DATA_SIZE_1KB);
7402 	port_map = priv->port_map & ~BIT(MVPP2_LOOPBACK_PORT_INDEX);
7403 
7404 	/* Set TX FIFO size to 0 for inactive ports. */
7405 	for_each_clear_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX)
7406 		mvpp22_tx_fifo_set_hw(priv, port, 0);
7407 
7408 	/* Assign remaining TX FIFO space among all active ports. */
7409 	size_remainder = MVPP22_TX_FIFO_DATA_SIZE_18KB;
7410 	remaining_ports_count = hweight_long(port_map);
7411 
7412 	for_each_set_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX) {
7413 		if (remaining_ports_count == 1)
7414 			size = min(size_remainder,
7415 				   MVPP22_TX_FIFO_DATA_SIZE_10KB);
7416 		else if (port == 0)
7417 			size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
7418 		else
7419 			size = size_remainder / remaining_ports_count;
7420 
7421 		size_remainder -= size;
7422 		remaining_ports_count--;
7423 
7424 		mvpp22_tx_fifo_set_hw(priv, port, size);
7425 	}
7426 }
7427 
mvpp2_axi_init(struct mvpp2 * priv)7428 static void mvpp2_axi_init(struct mvpp2 *priv)
7429 {
7430 	u32 val, rdval, wrval;
7431 
7432 	mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7433 
7434 	/* AXI Bridge Configuration */
7435 
7436 	rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7437 		<< MVPP22_AXI_ATTR_CACHE_OFFS;
7438 	rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7439 		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;
7440 
7441 	wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7442 		<< MVPP22_AXI_ATTR_CACHE_OFFS;
7443 	wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7444 		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;
7445 
7446 	/* BM */
7447 	mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7448 	mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7449 
7450 	/* Descriptors */
7451 	mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7452 	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7453 	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7454 	mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7455 
7456 	/* Buffer Data */
7457 	mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7458 	mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7459 
7460 	val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7461 		<< MVPP22_AXI_CODE_CACHE_OFFS;
7462 	val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7463 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
7464 	mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7465 	mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7466 
7467 	val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7468 		<< MVPP22_AXI_CODE_CACHE_OFFS;
7469 	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7470 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
7471 
7472 	mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7473 
7474 	val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7475 		<< MVPP22_AXI_CODE_CACHE_OFFS;
7476 	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7477 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
7478 
7479 	mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7480 }
7481 
7482 /* Initialize network controller common part HW */
mvpp2_init(struct platform_device * pdev,struct mvpp2 * priv)7483 static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7484 {
7485 	const struct mbus_dram_target_info *dram_target_info;
7486 	int err, i;
7487 	u32 val;
7488 
7489 	/* MBUS windows configuration */
7490 	dram_target_info = mv_mbus_dram_info();
7491 	if (dram_target_info)
7492 		mvpp2_conf_mbus_windows(dram_target_info, priv);
7493 
7494 	if (priv->hw_version >= MVPP22)
7495 		mvpp2_axi_init(priv);
7496 
7497 	/* Disable HW PHY polling */
7498 	if (priv->hw_version == MVPP21) {
7499 		val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7500 		val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7501 		writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7502 	} else {
7503 		val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7504 		val &= ~MVPP22_SMI_POLLING_EN;
7505 		writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7506 	}
7507 
7508 	/* Allocate and initialize aggregated TXQs */
7509 	priv->aggr_txqs = devm_kcalloc(&pdev->dev, MVPP2_MAX_THREADS,
7510 				       sizeof(*priv->aggr_txqs),
7511 				       GFP_KERNEL);
7512 	if (!priv->aggr_txqs)
7513 		return -ENOMEM;
7514 
7515 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7516 		priv->aggr_txqs[i].id = i;
7517 		priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
7518 		err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
7519 		if (err < 0)
7520 			return err;
7521 	}
7522 
7523 	/* Fifo Init */
7524 	if (priv->hw_version == MVPP21) {
7525 		mvpp2_rx_fifo_init(priv);
7526 	} else {
7527 		mvpp22_rx_fifo_init(priv);
7528 		mvpp22_tx_fifo_init(priv);
7529 		if (priv->hw_version == MVPP23)
7530 			mvpp23_rx_fifo_fc_set_tresh(priv);
7531 	}
7532 
7533 	if (priv->hw_version == MVPP21)
7534 		writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7535 		       priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
7536 
7537 	/* Allow cache snoop when transmiting packets */
7538 	mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7539 
7540 	/* Buffer Manager initialization */
7541 	err = mvpp2_bm_init(&pdev->dev, priv);
7542 	if (err < 0)
7543 		return err;
7544 
7545 	/* Parser default initialization */
7546 	err = mvpp2_prs_default_init(pdev, priv);
7547 	if (err < 0)
7548 		return err;
7549 
7550 	/* Classifier default initialization */
7551 	mvpp2_cls_init(priv);
7552 
7553 	return 0;
7554 }
7555 
mvpp2_get_sram(struct platform_device * pdev,struct mvpp2 * priv)7556 static int mvpp2_get_sram(struct platform_device *pdev,
7557 			  struct mvpp2 *priv)
7558 {
7559 	struct resource *res;
7560 	void __iomem *base;
7561 
7562 	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
7563 	if (!res) {
7564 		if (has_acpi_companion(&pdev->dev))
7565 			dev_warn(&pdev->dev, "ACPI is too old, Flow control not supported\n");
7566 		else
7567 			dev_warn(&pdev->dev, "DT is too old, Flow control not supported\n");
7568 		return 0;
7569 	}
7570 
7571 	base = devm_ioremap_resource(&pdev->dev, res);
7572 	if (IS_ERR(base))
7573 		return PTR_ERR(base);
7574 
7575 	priv->cm3_base = base;
7576 	return 0;
7577 }
7578 
mvpp2_probe(struct platform_device * pdev)7579 static int mvpp2_probe(struct platform_device *pdev)
7580 {
7581 	struct mvpp2 *priv;
7582 	struct resource *res;
7583 	void __iomem *base;
7584 	int i, shared;
7585 	int err;
7586 
7587 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
7588 	if (!priv)
7589 		return -ENOMEM;
7590 
7591 	priv->hw_version = (unsigned long)device_get_match_data(&pdev->dev);
7592 
7593 	/* multi queue mode isn't supported on PPV2.1, fallback to single
7594 	 * mode
7595 	 */
7596 	if (priv->hw_version == MVPP21)
7597 		queue_mode = MVPP2_QDIST_SINGLE_MODE;
7598 
7599 	base = devm_platform_ioremap_resource(pdev, 0);
7600 	if (IS_ERR(base))
7601 		return PTR_ERR(base);
7602 
7603 	if (priv->hw_version == MVPP21) {
7604 		priv->lms_base = devm_platform_ioremap_resource(pdev, 1);
7605 		if (IS_ERR(priv->lms_base))
7606 			return PTR_ERR(priv->lms_base);
7607 	} else {
7608 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7609 		if (!res) {
7610 			dev_err(&pdev->dev, "Invalid resource\n");
7611 			return -EINVAL;
7612 		}
7613 		if (has_acpi_companion(&pdev->dev)) {
7614 			/* In case the MDIO memory region is declared in
7615 			 * the ACPI, it can already appear as 'in-use'
7616 			 * in the OS. Because it is overlapped by second
7617 			 * region of the network controller, make
7618 			 * sure it is released, before requesting it again.
7619 			 * The care is taken by mvpp2 driver to avoid
7620 			 * concurrent access to this memory region.
7621 			 */
7622 			release_resource(res);
7623 		}
7624 		priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7625 		if (IS_ERR(priv->iface_base))
7626 			return PTR_ERR(priv->iface_base);
7627 
7628 		/* Map CM3 SRAM */
7629 		err = mvpp2_get_sram(pdev, priv);
7630 		if (err)
7631 			dev_warn(&pdev->dev, "Fail to alloc CM3 SRAM\n");
7632 
7633 		/* Enable global Flow Control only if handler to SRAM not NULL */
7634 		if (priv->cm3_base)
7635 			priv->global_tx_fc = true;
7636 	}
7637 
7638 	if (priv->hw_version >= MVPP22 && dev_of_node(&pdev->dev)) {
7639 		priv->sysctrl_base =
7640 			syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7641 							"marvell,system-controller");
7642 		if (IS_ERR(priv->sysctrl_base))
7643 			/* The system controller regmap is optional for dt
7644 			 * compatibility reasons. When not provided, the
7645 			 * configuration of the GoP relies on the
7646 			 * firmware/bootloader.
7647 			 */
7648 			priv->sysctrl_base = NULL;
7649 	}
7650 
7651 	if (priv->hw_version >= MVPP22 &&
7652 	    mvpp2_get_nrxqs(priv) * 2 <= MVPP2_BM_MAX_POOLS)
7653 		priv->percpu_pools = 1;
7654 
7655 	mvpp2_setup_bm_pool();
7656 
7657 
7658 	priv->nthreads = min_t(unsigned int, num_present_cpus(),
7659 			       MVPP2_MAX_THREADS);
7660 
7661 	shared = num_present_cpus() - priv->nthreads;
7662 	if (shared > 0)
7663 		bitmap_set(&priv->lock_map, 0,
7664 			    min_t(int, shared, MVPP2_MAX_THREADS));
7665 
7666 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7667 		u32 addr_space_sz;
7668 
7669 		addr_space_sz = (priv->hw_version == MVPP21 ?
7670 				 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
7671 		priv->swth_base[i] = base + i * addr_space_sz;
7672 	}
7673 
7674 	if (priv->hw_version == MVPP21)
7675 		priv->max_port_rxqs = 8;
7676 	else
7677 		priv->max_port_rxqs = 32;
7678 
7679 	if (dev_of_node(&pdev->dev)) {
7680 		priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7681 		if (IS_ERR(priv->pp_clk))
7682 			return PTR_ERR(priv->pp_clk);
7683 		err = clk_prepare_enable(priv->pp_clk);
7684 		if (err < 0)
7685 			return err;
7686 
7687 		priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
7688 		if (IS_ERR(priv->gop_clk)) {
7689 			err = PTR_ERR(priv->gop_clk);
7690 			goto err_pp_clk;
7691 		}
7692 		err = clk_prepare_enable(priv->gop_clk);
7693 		if (err < 0)
7694 			goto err_pp_clk;
7695 
7696 		if (priv->hw_version >= MVPP22) {
7697 			priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
7698 			if (IS_ERR(priv->mg_clk)) {
7699 				err = PTR_ERR(priv->mg_clk);
7700 				goto err_gop_clk;
7701 			}
7702 
7703 			err = clk_prepare_enable(priv->mg_clk);
7704 			if (err < 0)
7705 				goto err_gop_clk;
7706 
7707 			priv->mg_core_clk = devm_clk_get_optional(&pdev->dev, "mg_core_clk");
7708 			if (IS_ERR(priv->mg_core_clk)) {
7709 				err = PTR_ERR(priv->mg_core_clk);
7710 				goto err_mg_clk;
7711 			}
7712 
7713 			err = clk_prepare_enable(priv->mg_core_clk);
7714 			if (err < 0)
7715 				goto err_mg_clk;
7716 		}
7717 
7718 		priv->axi_clk = devm_clk_get_optional(&pdev->dev, "axi_clk");
7719 		if (IS_ERR(priv->axi_clk)) {
7720 			err = PTR_ERR(priv->axi_clk);
7721 			goto err_mg_core_clk;
7722 		}
7723 
7724 		err = clk_prepare_enable(priv->axi_clk);
7725 		if (err < 0)
7726 			goto err_mg_core_clk;
7727 
7728 		/* Get system's tclk rate */
7729 		priv->tclk = clk_get_rate(priv->pp_clk);
7730 	} else {
7731 		err = device_property_read_u32(&pdev->dev, "clock-frequency", &priv->tclk);
7732 		if (err) {
7733 			dev_err(&pdev->dev, "missing clock-frequency value\n");
7734 			return err;
7735 		}
7736 	}
7737 
7738 	if (priv->hw_version >= MVPP22) {
7739 		err = dma_set_mask(&pdev->dev, MVPP2_DESC_DMA_MASK);
7740 		if (err)
7741 			goto err_axi_clk;
7742 		/* Sadly, the BM pools all share the same register to
7743 		 * store the high 32 bits of their address. So they
7744 		 * must all have the same high 32 bits, which forces
7745 		 * us to restrict coherent memory to DMA_BIT_MASK(32).
7746 		 */
7747 		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7748 		if (err)
7749 			goto err_axi_clk;
7750 	}
7751 
7752 	/* Map DTS-active ports. Should be done before FIFO mvpp2_init */
7753 	device_for_each_child_node_scoped(&pdev->dev, port_fwnode) {
7754 		if (!fwnode_property_read_u32(port_fwnode, "port-id", &i))
7755 			priv->port_map |= BIT(i);
7756 	}
7757 
7758 	if (mvpp2_read(priv, MVPP2_VER_ID_REG) == MVPP2_VER_PP23)
7759 		priv->hw_version = MVPP23;
7760 
7761 	/* Init locks for shared packet processor resources */
7762 	spin_lock_init(&priv->mss_spinlock);
7763 	spin_lock_init(&priv->prs_spinlock);
7764 
7765 	/* Initialize network controller */
7766 	err = mvpp2_init(pdev, priv);
7767 	if (err < 0) {
7768 		dev_err(&pdev->dev, "failed to initialize controller\n");
7769 		goto err_axi_clk;
7770 	}
7771 
7772 	err = mvpp22_tai_probe(&pdev->dev, priv);
7773 	if (err < 0)
7774 		goto err_axi_clk;
7775 
7776 	/* Initialize ports */
7777 	device_for_each_child_node_scoped(&pdev->dev, port_fwnode) {
7778 		err = mvpp2_port_probe(pdev, port_fwnode, priv);
7779 		if (err < 0)
7780 			goto err_port_probe;
7781 	}
7782 
7783 	if (priv->port_count == 0) {
7784 		dev_err(&pdev->dev, "no ports enabled\n");
7785 		err = -ENODEV;
7786 		goto err_axi_clk;
7787 	}
7788 
7789 	/* Statistics must be gathered regularly because some of them (like
7790 	 * packets counters) are 32-bit registers and could overflow quite
7791 	 * quickly. For instance, a 10Gb link used at full bandwidth with the
7792 	 * smallest packets (64B) will overflow a 32-bit counter in less than
7793 	 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
7794 	 */
7795 	snprintf(priv->queue_name, sizeof(priv->queue_name),
7796 		 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
7797 		 priv->port_count > 1 ? "+" : "");
7798 	priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
7799 	if (!priv->stats_queue) {
7800 		err = -ENOMEM;
7801 		goto err_port_probe;
7802 	}
7803 
7804 	if (priv->global_tx_fc && priv->hw_version >= MVPP22) {
7805 		err = mvpp2_enable_global_fc(priv);
7806 		if (err)
7807 			dev_warn(&pdev->dev, "Minimum of CM3 firmware 18.09 and chip revision B0 required for flow control\n");
7808 	}
7809 
7810 	mvpp2_dbgfs_init(priv, pdev->name);
7811 
7812 	platform_set_drvdata(pdev, priv);
7813 	return 0;
7814 
7815 err_port_probe:
7816 	for (i = 0; i < priv->port_count; i++)
7817 		mvpp2_port_remove(priv->port_list[i]);
7818 err_axi_clk:
7819 	clk_disable_unprepare(priv->axi_clk);
7820 err_mg_core_clk:
7821 	clk_disable_unprepare(priv->mg_core_clk);
7822 err_mg_clk:
7823 	clk_disable_unprepare(priv->mg_clk);
7824 err_gop_clk:
7825 	clk_disable_unprepare(priv->gop_clk);
7826 err_pp_clk:
7827 	clk_disable_unprepare(priv->pp_clk);
7828 	return err;
7829 }
7830 
mvpp2_remove(struct platform_device * pdev)7831 static void mvpp2_remove(struct platform_device *pdev)
7832 {
7833 	struct mvpp2 *priv = platform_get_drvdata(pdev);
7834 	int i, poolnum = MVPP2_BM_POOLS_NUM;
7835 
7836 	mvpp2_dbgfs_cleanup(priv);
7837 
7838 	for (i = 0; i < priv->port_count; i++) {
7839 		mutex_destroy(&priv->port_list[i]->gather_stats_lock);
7840 		mvpp2_port_remove(priv->port_list[i]);
7841 	}
7842 
7843 	destroy_workqueue(priv->stats_queue);
7844 
7845 	if (priv->percpu_pools)
7846 		poolnum = mvpp2_get_nrxqs(priv) * 2;
7847 
7848 	for (i = 0; i < poolnum; i++) {
7849 		struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7850 
7851 		mvpp2_bm_pool_destroy(&pdev->dev, priv, bm_pool);
7852 	}
7853 
7854 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7855 		struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7856 
7857 		dma_free_coherent(&pdev->dev,
7858 				  MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7859 				  aggr_txq->descs,
7860 				  aggr_txq->descs_dma);
7861 	}
7862 
7863 	if (!dev_of_node(&pdev->dev))
7864 		return;
7865 
7866 	clk_disable_unprepare(priv->axi_clk);
7867 	clk_disable_unprepare(priv->mg_core_clk);
7868 	clk_disable_unprepare(priv->mg_clk);
7869 	clk_disable_unprepare(priv->pp_clk);
7870 	clk_disable_unprepare(priv->gop_clk);
7871 }
7872 
7873 static const struct of_device_id mvpp2_match[] = {
7874 	{
7875 		.compatible = "marvell,armada-375-pp2",
7876 		.data = (void *)MVPP21,
7877 	},
7878 	{
7879 		.compatible = "marvell,armada-7k-pp22",
7880 		.data = (void *)MVPP22,
7881 	},
7882 	{ }
7883 };
7884 MODULE_DEVICE_TABLE(of, mvpp2_match);
7885 
7886 #ifdef CONFIG_ACPI
7887 static const struct acpi_device_id mvpp2_acpi_match[] = {
7888 	{ "MRVL0110", MVPP22 },
7889 	{ },
7890 };
7891 MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
7892 #endif
7893 
7894 static struct platform_driver mvpp2_driver = {
7895 	.probe = mvpp2_probe,
7896 	.remove = mvpp2_remove,
7897 	.driver = {
7898 		.name = MVPP2_DRIVER_NAME,
7899 		.of_match_table = mvpp2_match,
7900 		.acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
7901 	},
7902 };
7903 
mvpp2_driver_init(void)7904 static int __init mvpp2_driver_init(void)
7905 {
7906 	return platform_driver_register(&mvpp2_driver);
7907 }
7908 module_init(mvpp2_driver_init);
7909 
mvpp2_driver_exit(void)7910 static void __exit mvpp2_driver_exit(void)
7911 {
7912 	platform_driver_unregister(&mvpp2_driver);
7913 	mvpp2_dbgfs_exit();
7914 }
7915 module_exit(mvpp2_driver_exit);
7916 
7917 MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7918 MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
7919 MODULE_LICENSE("GPL v2");
7920