1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for Marvell PPv2 network controller for Armada 375 SoC.
4 *
5 * Copyright (C) 2014 Marvell
6 *
7 * Marcin Wojtas <mw@semihalf.com>
8 */
9
10 #include <linux/acpi.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/platform_device.h>
15 #include <linux/skbuff.h>
16 #include <linux/inetdevice.h>
17 #include <linux/mbus.h>
18 #include <linux/module.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/interrupt.h>
21 #include <linux/cpumask.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/phy.h>
29 #include <linux/phylink.h>
30 #include <linux/phy/phy.h>
31 #include <linux/ptp_classify.h>
32 #include <linux/clk.h>
33 #include <linux/hrtimer.h>
34 #include <linux/ktime.h>
35 #include <linux/regmap.h>
36 #include <uapi/linux/ppp_defs.h>
37 #include <net/ip.h>
38 #include <net/ipv6.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;
957
958 for (i = 0; i < priv->port_count; i++) {
959 port = priv->port_list[i];
960 if (port->priv->percpu_pools) {
961 for (i = 0; i < port->nrxqs; i++)
962 mvpp2_bm_pool_update_fc(port, &port->priv->bm_pools[i],
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 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_XAUI;
1517 }
1518
mvpp2_modify(void __iomem * ptr,u32 mask,u32 set)1519 static void mvpp2_modify(void __iomem *ptr, u32 mask, u32 set)
1520 {
1521 u32 old, val;
1522
1523 old = val = readl(ptr);
1524 val &= ~mask;
1525 val |= set;
1526 if (old != val)
1527 writel(val, ptr);
1528 }
1529
mvpp22_gop_init_rgmii(struct mvpp2_port * port)1530 static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
1531 {
1532 struct mvpp2 *priv = port->priv;
1533 u32 val;
1534
1535 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1536 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
1537 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1538
1539 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1540 if (port->gop_id == 2)
1541 val |= GENCONF_CTRL0_PORT2_RGMII;
1542 else if (port->gop_id == 3)
1543 val |= GENCONF_CTRL0_PORT3_RGMII_MII;
1544 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1545 }
1546
mvpp22_gop_init_sgmii(struct mvpp2_port * port)1547 static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
1548 {
1549 struct mvpp2 *priv = port->priv;
1550 u32 val;
1551
1552 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1553 val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
1554 GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
1555 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1556
1557 if (port->gop_id > 1) {
1558 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1559 if (port->gop_id == 2)
1560 val &= ~GENCONF_CTRL0_PORT2_RGMII;
1561 else if (port->gop_id == 3)
1562 val &= ~GENCONF_CTRL0_PORT3_RGMII_MII;
1563 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1564 }
1565 }
1566
mvpp22_gop_init_10gkr(struct mvpp2_port * port)1567 static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
1568 {
1569 struct mvpp2 *priv = port->priv;
1570 void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1571 void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1572 u32 val;
1573
1574 val = readl(xpcs + MVPP22_XPCS_CFG0);
1575 val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
1576 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
1577 val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
1578 writel(val, xpcs + MVPP22_XPCS_CFG0);
1579
1580 val = readl(mpcs + MVPP22_MPCS_CTRL);
1581 val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
1582 writel(val, mpcs + MVPP22_MPCS_CTRL);
1583
1584 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1585 val &= ~MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7);
1586 val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
1587 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1588 }
1589
mvpp22_gop_fca_enable_periodic(struct mvpp2_port * port,bool en)1590 static void mvpp22_gop_fca_enable_periodic(struct mvpp2_port *port, bool en)
1591 {
1592 struct mvpp2 *priv = port->priv;
1593 void __iomem *fca = priv->iface_base + MVPP22_FCA_BASE(port->gop_id);
1594 u32 val;
1595
1596 val = readl(fca + MVPP22_FCA_CONTROL_REG);
1597 val &= ~MVPP22_FCA_ENABLE_PERIODIC;
1598 if (en)
1599 val |= MVPP22_FCA_ENABLE_PERIODIC;
1600 writel(val, fca + MVPP22_FCA_CONTROL_REG);
1601 }
1602
mvpp22_gop_fca_set_timer(struct mvpp2_port * port,u32 timer)1603 static void mvpp22_gop_fca_set_timer(struct mvpp2_port *port, u32 timer)
1604 {
1605 struct mvpp2 *priv = port->priv;
1606 void __iomem *fca = priv->iface_base + MVPP22_FCA_BASE(port->gop_id);
1607 u32 lsb, msb;
1608
1609 lsb = timer & MVPP22_FCA_REG_MASK;
1610 msb = timer >> MVPP22_FCA_REG_SIZE;
1611
1612 writel(lsb, fca + MVPP22_PERIODIC_COUNTER_LSB_REG);
1613 writel(msb, fca + MVPP22_PERIODIC_COUNTER_MSB_REG);
1614 }
1615
1616 /* Set Flow Control timer x100 faster than pause quanta to ensure that link
1617 * partner won't send traffic if port is in XOFF mode.
1618 */
mvpp22_gop_fca_set_periodic_timer(struct mvpp2_port * port)1619 static void mvpp22_gop_fca_set_periodic_timer(struct mvpp2_port *port)
1620 {
1621 u32 timer;
1622
1623 timer = (port->priv->tclk / (USEC_PER_SEC * FC_CLK_DIVIDER))
1624 * FC_QUANTA;
1625
1626 mvpp22_gop_fca_enable_periodic(port, false);
1627
1628 mvpp22_gop_fca_set_timer(port, timer);
1629
1630 mvpp22_gop_fca_enable_periodic(port, true);
1631 }
1632
mvpp22_gop_init(struct mvpp2_port * port,phy_interface_t interface)1633 static int mvpp22_gop_init(struct mvpp2_port *port, phy_interface_t interface)
1634 {
1635 struct mvpp2 *priv = port->priv;
1636 u32 val;
1637
1638 if (!priv->sysctrl_base)
1639 return 0;
1640
1641 switch (interface) {
1642 case PHY_INTERFACE_MODE_RGMII:
1643 case PHY_INTERFACE_MODE_RGMII_ID:
1644 case PHY_INTERFACE_MODE_RGMII_RXID:
1645 case PHY_INTERFACE_MODE_RGMII_TXID:
1646 if (!mvpp2_port_supports_rgmii(port))
1647 goto invalid_conf;
1648 mvpp22_gop_init_rgmii(port);
1649 break;
1650 case PHY_INTERFACE_MODE_SGMII:
1651 case PHY_INTERFACE_MODE_1000BASEX:
1652 case PHY_INTERFACE_MODE_2500BASEX:
1653 mvpp22_gop_init_sgmii(port);
1654 break;
1655 case PHY_INTERFACE_MODE_10GBASER:
1656 if (!mvpp2_port_supports_xlg(port))
1657 goto invalid_conf;
1658 mvpp22_gop_init_10gkr(port);
1659 break;
1660 default:
1661 goto unsupported_conf;
1662 }
1663
1664 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
1665 val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
1666 GENCONF_PORT_CTRL1_EN(port->gop_id);
1667 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
1668
1669 regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1670 val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
1671 regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1672
1673 regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
1674 val |= GENCONF_SOFT_RESET1_GOP;
1675 regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
1676
1677 mvpp22_gop_fca_set_periodic_timer(port);
1678
1679 unsupported_conf:
1680 return 0;
1681
1682 invalid_conf:
1683 netdev_err(port->dev, "Invalid port configuration\n");
1684 return -EINVAL;
1685 }
1686
mvpp22_gop_unmask_irq(struct mvpp2_port * port)1687 static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
1688 {
1689 u32 val;
1690
1691 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1692 phy_interface_mode_is_8023z(port->phy_interface) ||
1693 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1694 /* Enable the GMAC link status irq for this port */
1695 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1696 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1697 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1698 }
1699
1700 if (mvpp2_port_supports_xlg(port)) {
1701 /* Enable the XLG/GIG irqs for this port */
1702 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1703 if (mvpp2_is_xlg(port->phy_interface))
1704 val |= MVPP22_XLG_EXT_INT_MASK_XLG;
1705 else
1706 val |= MVPP22_XLG_EXT_INT_MASK_GIG;
1707 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1708 }
1709 }
1710
mvpp22_gop_mask_irq(struct mvpp2_port * port)1711 static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
1712 {
1713 u32 val;
1714
1715 if (mvpp2_port_supports_xlg(port)) {
1716 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1717 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
1718 MVPP22_XLG_EXT_INT_MASK_GIG);
1719 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1720 }
1721
1722 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1723 phy_interface_mode_is_8023z(port->phy_interface) ||
1724 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1725 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1726 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1727 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1728 }
1729 }
1730
mvpp22_gop_setup_irq(struct mvpp2_port * port)1731 static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
1732 {
1733 u32 val;
1734
1735 mvpp2_modify(port->base + MVPP22_GMAC_INT_SUM_MASK,
1736 MVPP22_GMAC_INT_SUM_MASK_PTP,
1737 MVPP22_GMAC_INT_SUM_MASK_PTP);
1738
1739 if (port->phylink ||
1740 phy_interface_mode_is_rgmii(port->phy_interface) ||
1741 phy_interface_mode_is_8023z(port->phy_interface) ||
1742 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1743 val = readl(port->base + MVPP22_GMAC_INT_MASK);
1744 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
1745 writel(val, port->base + MVPP22_GMAC_INT_MASK);
1746 }
1747
1748 if (mvpp2_port_supports_xlg(port)) {
1749 val = readl(port->base + MVPP22_XLG_INT_MASK);
1750 val |= MVPP22_XLG_INT_MASK_LINK;
1751 writel(val, port->base + MVPP22_XLG_INT_MASK);
1752
1753 mvpp2_modify(port->base + MVPP22_XLG_EXT_INT_MASK,
1754 MVPP22_XLG_EXT_INT_MASK_PTP,
1755 MVPP22_XLG_EXT_INT_MASK_PTP);
1756 }
1757
1758 mvpp22_gop_unmask_irq(port);
1759 }
1760
1761 /* Sets the PHY mode of the COMPHY (which configures the serdes lanes).
1762 *
1763 * The PHY mode used by the PPv2 driver comes from the network subsystem, while
1764 * the one given to the COMPHY comes from the generic PHY subsystem. Hence they
1765 * differ.
1766 *
1767 * The COMPHY configures the serdes lanes regardless of the actual use of the
1768 * lanes by the physical layer. This is why configurations like
1769 * "PPv2 (2500BaseX) - COMPHY (2500SGMII)" are valid.
1770 */
mvpp22_comphy_init(struct mvpp2_port * port,phy_interface_t interface)1771 static int mvpp22_comphy_init(struct mvpp2_port *port,
1772 phy_interface_t interface)
1773 {
1774 int ret;
1775
1776 if (!port->comphy)
1777 return 0;
1778
1779 ret = phy_set_mode_ext(port->comphy, PHY_MODE_ETHERNET, interface);
1780 if (ret)
1781 return ret;
1782
1783 return phy_power_on(port->comphy);
1784 }
1785
mvpp2_port_enable(struct mvpp2_port * port)1786 static void mvpp2_port_enable(struct mvpp2_port *port)
1787 {
1788 u32 val;
1789
1790 if (mvpp2_port_supports_xlg(port) &&
1791 mvpp2_is_xlg(port->phy_interface)) {
1792 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1793 val |= MVPP22_XLG_CTRL0_PORT_EN;
1794 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
1795 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1796 } else {
1797 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1798 val |= MVPP2_GMAC_PORT_EN_MASK;
1799 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
1800 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1801 }
1802 }
1803
mvpp2_port_disable(struct mvpp2_port * port)1804 static void mvpp2_port_disable(struct mvpp2_port *port)
1805 {
1806 u32 val;
1807
1808 if (mvpp2_port_supports_xlg(port) &&
1809 mvpp2_is_xlg(port->phy_interface)) {
1810 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1811 val &= ~MVPP22_XLG_CTRL0_PORT_EN;
1812 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1813 }
1814
1815 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1816 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
1817 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1818 }
1819
1820 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
mvpp2_port_periodic_xon_disable(struct mvpp2_port * port)1821 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
1822 {
1823 u32 val;
1824
1825 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
1826 ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
1827 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1828 }
1829
1830 /* Configure loopback port */
mvpp2_port_loopback_set(struct mvpp2_port * port,const struct phylink_link_state * state)1831 static void mvpp2_port_loopback_set(struct mvpp2_port *port,
1832 const struct phylink_link_state *state)
1833 {
1834 u32 val;
1835
1836 val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
1837
1838 if (state->speed == 1000)
1839 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
1840 else
1841 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
1842
1843 if (phy_interface_mode_is_8023z(state->interface) ||
1844 state->interface == PHY_INTERFACE_MODE_SGMII)
1845 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
1846 else
1847 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
1848
1849 writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1850 }
1851
1852 enum {
1853 ETHTOOL_XDP_REDIRECT,
1854 ETHTOOL_XDP_PASS,
1855 ETHTOOL_XDP_DROP,
1856 ETHTOOL_XDP_TX,
1857 ETHTOOL_XDP_TX_ERR,
1858 ETHTOOL_XDP_XMIT,
1859 ETHTOOL_XDP_XMIT_ERR,
1860 };
1861
1862 struct mvpp2_ethtool_counter {
1863 unsigned int offset;
1864 const char string[ETH_GSTRING_LEN];
1865 bool reg_is_64b;
1866 };
1867
mvpp2_read_count(struct mvpp2_port * port,const struct mvpp2_ethtool_counter * counter)1868 static u64 mvpp2_read_count(struct mvpp2_port *port,
1869 const struct mvpp2_ethtool_counter *counter)
1870 {
1871 u64 val;
1872
1873 val = readl(port->stats_base + counter->offset);
1874 if (counter->reg_is_64b)
1875 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
1876
1877 return val;
1878 }
1879
1880 /* Some counters are accessed indirectly by first writing an index to
1881 * MVPP2_CTRS_IDX. The index can represent various resources depending on the
1882 * register we access, it can be a hit counter for some classification tables,
1883 * a counter specific to a rxq, a txq or a buffer pool.
1884 */
mvpp2_read_index(struct mvpp2 * priv,u32 index,u32 reg)1885 static u32 mvpp2_read_index(struct mvpp2 *priv, u32 index, u32 reg)
1886 {
1887 mvpp2_write(priv, MVPP2_CTRS_IDX, index);
1888 return mvpp2_read(priv, reg);
1889 }
1890
1891 /* Due to the fact that software statistics and hardware statistics are, by
1892 * design, incremented at different moments in the chain of packet processing,
1893 * it is very likely that incoming packets could have been dropped after being
1894 * counted by hardware but before reaching software statistics (most probably
1895 * multicast packets), and in the oppposite way, during transmission, FCS bytes
1896 * are added in between as well as TSO skb will be split and header bytes added.
1897 * Hence, statistics gathered from userspace with ifconfig (software) and
1898 * ethtool (hardware) cannot be compared.
1899 */
1900 static const struct mvpp2_ethtool_counter mvpp2_ethtool_mib_regs[] = {
1901 { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
1902 { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
1903 { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
1904 { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
1905 { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
1906 { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
1907 { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
1908 { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
1909 { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
1910 { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
1911 { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
1912 { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
1913 { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
1914 { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
1915 { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
1916 { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
1917 { MVPP2_MIB_FC_SENT, "fc_sent" },
1918 { MVPP2_MIB_FC_RCVD, "fc_received" },
1919 { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
1920 { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
1921 { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
1922 { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
1923 { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
1924 { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
1925 { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
1926 { MVPP2_MIB_COLLISION, "collision" },
1927 { MVPP2_MIB_LATE_COLLISION, "late_collision" },
1928 };
1929
1930 static const struct mvpp2_ethtool_counter mvpp2_ethtool_port_regs[] = {
1931 { MVPP2_OVERRUN_ETH_DROP, "rx_fifo_or_parser_overrun_drops" },
1932 { MVPP2_CLS_ETH_DROP, "rx_classifier_drops" },
1933 };
1934
1935 static const struct mvpp2_ethtool_counter mvpp2_ethtool_txq_regs[] = {
1936 { MVPP2_TX_DESC_ENQ_CTR, "txq_%d_desc_enqueue" },
1937 { MVPP2_TX_DESC_ENQ_TO_DDR_CTR, "txq_%d_desc_enqueue_to_ddr" },
1938 { MVPP2_TX_BUFF_ENQ_TO_DDR_CTR, "txq_%d_buff_euqueue_to_ddr" },
1939 { MVPP2_TX_DESC_ENQ_HW_FWD_CTR, "txq_%d_desc_hardware_forwarded" },
1940 { MVPP2_TX_PKTS_DEQ_CTR, "txq_%d_packets_dequeued" },
1941 { MVPP2_TX_PKTS_FULL_QUEUE_DROP_CTR, "txq_%d_queue_full_drops" },
1942 { MVPP2_TX_PKTS_EARLY_DROP_CTR, "txq_%d_packets_early_drops" },
1943 { MVPP2_TX_PKTS_BM_DROP_CTR, "txq_%d_packets_bm_drops" },
1944 { MVPP2_TX_PKTS_BM_MC_DROP_CTR, "txq_%d_packets_rep_bm_drops" },
1945 };
1946
1947 static const struct mvpp2_ethtool_counter mvpp2_ethtool_rxq_regs[] = {
1948 { MVPP2_RX_DESC_ENQ_CTR, "rxq_%d_desc_enqueue" },
1949 { MVPP2_RX_PKTS_FULL_QUEUE_DROP_CTR, "rxq_%d_queue_full_drops" },
1950 { MVPP2_RX_PKTS_EARLY_DROP_CTR, "rxq_%d_packets_early_drops" },
1951 { MVPP2_RX_PKTS_BM_DROP_CTR, "rxq_%d_packets_bm_drops" },
1952 };
1953
1954 static const struct mvpp2_ethtool_counter mvpp2_ethtool_xdp[] = {
1955 { ETHTOOL_XDP_REDIRECT, "rx_xdp_redirect", },
1956 { ETHTOOL_XDP_PASS, "rx_xdp_pass", },
1957 { ETHTOOL_XDP_DROP, "rx_xdp_drop", },
1958 { ETHTOOL_XDP_TX, "rx_xdp_tx", },
1959 { ETHTOOL_XDP_TX_ERR, "rx_xdp_tx_errors", },
1960 { ETHTOOL_XDP_XMIT, "tx_xdp_xmit", },
1961 { ETHTOOL_XDP_XMIT_ERR, "tx_xdp_xmit_errors", },
1962 };
1963
1964 #define MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs) (ARRAY_SIZE(mvpp2_ethtool_mib_regs) + \
1965 ARRAY_SIZE(mvpp2_ethtool_port_regs) + \
1966 (ARRAY_SIZE(mvpp2_ethtool_txq_regs) * (ntxqs)) + \
1967 (ARRAY_SIZE(mvpp2_ethtool_rxq_regs) * (nrxqs)) + \
1968 ARRAY_SIZE(mvpp2_ethtool_xdp))
1969
mvpp2_ethtool_get_strings(struct net_device * netdev,u32 sset,u8 * data)1970 static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
1971 u8 *data)
1972 {
1973 struct mvpp2_port *port = netdev_priv(netdev);
1974 int i, q;
1975
1976 if (sset != ETH_SS_STATS)
1977 return;
1978
1979 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++) {
1980 strscpy(data, mvpp2_ethtool_mib_regs[i].string,
1981 ETH_GSTRING_LEN);
1982 data += ETH_GSTRING_LEN;
1983 }
1984
1985 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++) {
1986 strscpy(data, mvpp2_ethtool_port_regs[i].string,
1987 ETH_GSTRING_LEN);
1988 data += ETH_GSTRING_LEN;
1989 }
1990
1991 for (q = 0; q < port->ntxqs; q++) {
1992 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++) {
1993 snprintf(data, ETH_GSTRING_LEN,
1994 mvpp2_ethtool_txq_regs[i].string, q);
1995 data += ETH_GSTRING_LEN;
1996 }
1997 }
1998
1999 for (q = 0; q < port->nrxqs; q++) {
2000 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++) {
2001 snprintf(data, ETH_GSTRING_LEN,
2002 mvpp2_ethtool_rxq_regs[i].string,
2003 q);
2004 data += ETH_GSTRING_LEN;
2005 }
2006 }
2007
2008 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_xdp); i++) {
2009 strscpy(data, mvpp2_ethtool_xdp[i].string,
2010 ETH_GSTRING_LEN);
2011 data += ETH_GSTRING_LEN;
2012 }
2013 }
2014
2015 static void
mvpp2_get_xdp_stats(struct mvpp2_port * port,struct mvpp2_pcpu_stats * xdp_stats)2016 mvpp2_get_xdp_stats(struct mvpp2_port *port, struct mvpp2_pcpu_stats *xdp_stats)
2017 {
2018 unsigned int start;
2019 unsigned int cpu;
2020
2021 /* Gather XDP Statistics */
2022 for_each_possible_cpu(cpu) {
2023 struct mvpp2_pcpu_stats *cpu_stats;
2024 u64 xdp_redirect;
2025 u64 xdp_pass;
2026 u64 xdp_drop;
2027 u64 xdp_xmit;
2028 u64 xdp_xmit_err;
2029 u64 xdp_tx;
2030 u64 xdp_tx_err;
2031
2032 cpu_stats = per_cpu_ptr(port->stats, cpu);
2033 do {
2034 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2035 xdp_redirect = cpu_stats->xdp_redirect;
2036 xdp_pass = cpu_stats->xdp_pass;
2037 xdp_drop = cpu_stats->xdp_drop;
2038 xdp_xmit = cpu_stats->xdp_xmit;
2039 xdp_xmit_err = cpu_stats->xdp_xmit_err;
2040 xdp_tx = cpu_stats->xdp_tx;
2041 xdp_tx_err = cpu_stats->xdp_tx_err;
2042 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2043
2044 xdp_stats->xdp_redirect += xdp_redirect;
2045 xdp_stats->xdp_pass += xdp_pass;
2046 xdp_stats->xdp_drop += xdp_drop;
2047 xdp_stats->xdp_xmit += xdp_xmit;
2048 xdp_stats->xdp_xmit_err += xdp_xmit_err;
2049 xdp_stats->xdp_tx += xdp_tx;
2050 xdp_stats->xdp_tx_err += xdp_tx_err;
2051 }
2052 }
2053
mvpp2_read_stats(struct mvpp2_port * port)2054 static void mvpp2_read_stats(struct mvpp2_port *port)
2055 {
2056 struct mvpp2_pcpu_stats xdp_stats = {};
2057 const struct mvpp2_ethtool_counter *s;
2058 u64 *pstats;
2059 int i, q;
2060
2061 pstats = port->ethtool_stats;
2062
2063 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++)
2064 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_mib_regs[i]);
2065
2066 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++)
2067 *pstats++ += mvpp2_read(port->priv,
2068 mvpp2_ethtool_port_regs[i].offset +
2069 4 * port->id);
2070
2071 for (q = 0; q < port->ntxqs; q++)
2072 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++)
2073 *pstats++ += mvpp2_read_index(port->priv,
2074 MVPP22_CTRS_TX_CTR(port->id, q),
2075 mvpp2_ethtool_txq_regs[i].offset);
2076
2077 /* Rxqs are numbered from 0 from the user standpoint, but not from the
2078 * driver's. We need to add the port->first_rxq offset.
2079 */
2080 for (q = 0; q < port->nrxqs; q++)
2081 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++)
2082 *pstats++ += mvpp2_read_index(port->priv,
2083 port->first_rxq + q,
2084 mvpp2_ethtool_rxq_regs[i].offset);
2085
2086 /* Gather XDP Statistics */
2087 mvpp2_get_xdp_stats(port, &xdp_stats);
2088
2089 for (i = 0, s = mvpp2_ethtool_xdp;
2090 s < mvpp2_ethtool_xdp + ARRAY_SIZE(mvpp2_ethtool_xdp);
2091 s++, i++) {
2092 switch (s->offset) {
2093 case ETHTOOL_XDP_REDIRECT:
2094 *pstats++ = xdp_stats.xdp_redirect;
2095 break;
2096 case ETHTOOL_XDP_PASS:
2097 *pstats++ = xdp_stats.xdp_pass;
2098 break;
2099 case ETHTOOL_XDP_DROP:
2100 *pstats++ = xdp_stats.xdp_drop;
2101 break;
2102 case ETHTOOL_XDP_TX:
2103 *pstats++ = xdp_stats.xdp_tx;
2104 break;
2105 case ETHTOOL_XDP_TX_ERR:
2106 *pstats++ = xdp_stats.xdp_tx_err;
2107 break;
2108 case ETHTOOL_XDP_XMIT:
2109 *pstats++ = xdp_stats.xdp_xmit;
2110 break;
2111 case ETHTOOL_XDP_XMIT_ERR:
2112 *pstats++ = xdp_stats.xdp_xmit_err;
2113 break;
2114 }
2115 }
2116 }
2117
mvpp2_gather_hw_statistics(struct work_struct * work)2118 static void mvpp2_gather_hw_statistics(struct work_struct *work)
2119 {
2120 struct delayed_work *del_work = to_delayed_work(work);
2121 struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
2122 stats_work);
2123
2124 mutex_lock(&port->gather_stats_lock);
2125
2126 mvpp2_read_stats(port);
2127
2128 /* No need to read again the counters right after this function if it
2129 * was called asynchronously by the user (ie. use of ethtool).
2130 */
2131 cancel_delayed_work(&port->stats_work);
2132 queue_delayed_work(port->priv->stats_queue, &port->stats_work,
2133 MVPP2_MIB_COUNTERS_STATS_DELAY);
2134
2135 mutex_unlock(&port->gather_stats_lock);
2136 }
2137
mvpp2_ethtool_get_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)2138 static void mvpp2_ethtool_get_stats(struct net_device *dev,
2139 struct ethtool_stats *stats, u64 *data)
2140 {
2141 struct mvpp2_port *port = netdev_priv(dev);
2142
2143 /* Update statistics for the given port, then take the lock to avoid
2144 * concurrent accesses on the ethtool_stats structure during its copy.
2145 */
2146 mvpp2_gather_hw_statistics(&port->stats_work.work);
2147
2148 mutex_lock(&port->gather_stats_lock);
2149 memcpy(data, port->ethtool_stats,
2150 sizeof(u64) * MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs));
2151 mutex_unlock(&port->gather_stats_lock);
2152 }
2153
mvpp2_ethtool_get_sset_count(struct net_device * dev,int sset)2154 static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
2155 {
2156 struct mvpp2_port *port = netdev_priv(dev);
2157
2158 if (sset == ETH_SS_STATS)
2159 return MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs);
2160
2161 return -EOPNOTSUPP;
2162 }
2163
mvpp2_mac_reset_assert(struct mvpp2_port * port)2164 static void mvpp2_mac_reset_assert(struct mvpp2_port *port)
2165 {
2166 u32 val;
2167
2168 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) |
2169 MVPP2_GMAC_PORT_RESET_MASK;
2170 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
2171
2172 if (port->priv->hw_version >= MVPP22 && port->gop_id == 0) {
2173 val = readl(port->base + MVPP22_XLG_CTRL0_REG) &
2174 ~MVPP22_XLG_CTRL0_MAC_RESET_DIS;
2175 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
2176 }
2177 }
2178
mvpp22_pcs_reset_assert(struct mvpp2_port * port)2179 static void mvpp22_pcs_reset_assert(struct mvpp2_port *port)
2180 {
2181 struct mvpp2 *priv = port->priv;
2182 void __iomem *mpcs, *xpcs;
2183 u32 val;
2184
2185 if (port->priv->hw_version == MVPP21 || port->gop_id != 0)
2186 return;
2187
2188 mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
2189 xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
2190
2191 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
2192 val &= ~(MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
2193 val |= MVPP22_MPCS_CLK_RESET_DIV_SET;
2194 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
2195
2196 val = readl(xpcs + MVPP22_XPCS_CFG0);
2197 writel(val & ~MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
2198 }
2199
mvpp22_pcs_reset_deassert(struct mvpp2_port * port,phy_interface_t interface)2200 static void mvpp22_pcs_reset_deassert(struct mvpp2_port *port,
2201 phy_interface_t interface)
2202 {
2203 struct mvpp2 *priv = port->priv;
2204 void __iomem *mpcs, *xpcs;
2205 u32 val;
2206
2207 if (port->priv->hw_version == MVPP21 || port->gop_id != 0)
2208 return;
2209
2210 mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
2211 xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
2212
2213 switch (interface) {
2214 case PHY_INTERFACE_MODE_10GBASER:
2215 val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
2216 val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX |
2217 MAC_CLK_RESET_SD_TX;
2218 val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
2219 writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
2220 break;
2221 case PHY_INTERFACE_MODE_XAUI:
2222 case PHY_INTERFACE_MODE_RXAUI:
2223 val = readl(xpcs + MVPP22_XPCS_CFG0);
2224 writel(val | MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
2225 break;
2226 default:
2227 break;
2228 }
2229 }
2230
2231 /* Change maximum receive size of the port */
mvpp2_gmac_max_rx_size_set(struct mvpp2_port * port)2232 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
2233 {
2234 u32 val;
2235
2236 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
2237 val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
2238 val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2239 MVPP2_GMAC_MAX_RX_SIZE_OFFS);
2240 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
2241 }
2242
2243 /* Change maximum receive size of the port */
mvpp2_xlg_max_rx_size_set(struct mvpp2_port * port)2244 static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
2245 {
2246 u32 val;
2247
2248 val = readl(port->base + MVPP22_XLG_CTRL1_REG);
2249 val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
2250 val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
2251 MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
2252 writel(val, port->base + MVPP22_XLG_CTRL1_REG);
2253 }
2254
2255 /* Set defaults to the MVPP2 port */
mvpp2_defaults_set(struct mvpp2_port * port)2256 static void mvpp2_defaults_set(struct mvpp2_port *port)
2257 {
2258 int tx_port_num, val, queue, lrxq;
2259
2260 if (port->priv->hw_version == MVPP21) {
2261 /* Update TX FIFO MIN Threshold */
2262 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2263 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
2264 /* Min. TX threshold must be less than minimal packet length */
2265 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
2266 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
2267 }
2268
2269 /* Disable Legacy WRR, Disable EJP, Release from reset */
2270 tx_port_num = mvpp2_egress_port(port);
2271 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
2272 tx_port_num);
2273 mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
2274
2275 /* Set TXQ scheduling to Round-Robin */
2276 mvpp2_write(port->priv, MVPP2_TXP_SCHED_FIXED_PRIO_REG, 0);
2277
2278 /* Close bandwidth for all queues */
2279 for (queue = 0; queue < MVPP2_MAX_TXQ; queue++)
2280 mvpp2_write(port->priv,
2281 MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(queue), 0);
2282
2283 /* Set refill period to 1 usec, refill tokens
2284 * and bucket size to maximum
2285 */
2286 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
2287 port->priv->tclk / USEC_PER_SEC);
2288 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
2289 val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
2290 val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
2291 val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
2292 mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
2293 val = MVPP2_TXP_TOKEN_SIZE_MAX;
2294 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2295
2296 /* Set MaximumLowLatencyPacketSize value to 256 */
2297 mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
2298 MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
2299 MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
2300
2301 /* Enable Rx cache snoop */
2302 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
2303 queue = port->rxqs[lrxq]->id;
2304 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2305 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
2306 MVPP2_SNOOP_BUF_HDR_MASK;
2307 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2308 }
2309
2310 /* At default, mask all interrupts to all present cpus */
2311 mvpp2_interrupts_disable(port);
2312 }
2313
2314 /* Enable/disable receiving packets */
mvpp2_ingress_enable(struct mvpp2_port * port)2315 static void mvpp2_ingress_enable(struct mvpp2_port *port)
2316 {
2317 u32 val;
2318 int lrxq, queue;
2319
2320 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
2321 queue = port->rxqs[lrxq]->id;
2322 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2323 val &= ~MVPP2_RXQ_DISABLE_MASK;
2324 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2325 }
2326 }
2327
mvpp2_ingress_disable(struct mvpp2_port * port)2328 static void mvpp2_ingress_disable(struct mvpp2_port *port)
2329 {
2330 u32 val;
2331 int lrxq, queue;
2332
2333 for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
2334 queue = port->rxqs[lrxq]->id;
2335 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
2336 val |= MVPP2_RXQ_DISABLE_MASK;
2337 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
2338 }
2339 }
2340
2341 /* Enable transmit via physical egress queue
2342 * - HW starts take descriptors from DRAM
2343 */
mvpp2_egress_enable(struct mvpp2_port * port)2344 static void mvpp2_egress_enable(struct mvpp2_port *port)
2345 {
2346 u32 qmap;
2347 int queue;
2348 int tx_port_num = mvpp2_egress_port(port);
2349
2350 /* Enable all initialized TXs. */
2351 qmap = 0;
2352 for (queue = 0; queue < port->ntxqs; queue++) {
2353 struct mvpp2_tx_queue *txq = port->txqs[queue];
2354
2355 if (txq->descs)
2356 qmap |= (1 << queue);
2357 }
2358
2359 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2360 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
2361 }
2362
2363 /* Disable transmit via physical egress queue
2364 * - HW doesn't take descriptors from DRAM
2365 */
mvpp2_egress_disable(struct mvpp2_port * port)2366 static void mvpp2_egress_disable(struct mvpp2_port *port)
2367 {
2368 u32 reg_data;
2369 int delay;
2370 int tx_port_num = mvpp2_egress_port(port);
2371
2372 /* Issue stop command for active channels only */
2373 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2374 reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
2375 MVPP2_TXP_SCHED_ENQ_MASK;
2376 if (reg_data != 0)
2377 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
2378 (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
2379
2380 /* Wait for all Tx activity to terminate. */
2381 delay = 0;
2382 do {
2383 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
2384 netdev_warn(port->dev,
2385 "Tx stop timed out, status=0x%08x\n",
2386 reg_data);
2387 break;
2388 }
2389 mdelay(1);
2390 delay++;
2391
2392 /* Check port TX Command register that all
2393 * Tx queues are stopped
2394 */
2395 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
2396 } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
2397 }
2398
2399 /* Rx descriptors helper methods */
2400
2401 /* Get number of Rx descriptors occupied by received packets */
2402 static inline int
mvpp2_rxq_received(struct mvpp2_port * port,int rxq_id)2403 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
2404 {
2405 u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
2406
2407 return val & MVPP2_RXQ_OCCUPIED_MASK;
2408 }
2409
2410 /* Update Rx queue status with the number of occupied and available
2411 * Rx descriptor slots.
2412 */
2413 static inline void
mvpp2_rxq_status_update(struct mvpp2_port * port,int rxq_id,int used_count,int free_count)2414 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
2415 int used_count, int free_count)
2416 {
2417 /* Decrement the number of used descriptors and increment count
2418 * increment the number of free descriptors.
2419 */
2420 u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
2421
2422 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
2423 }
2424
2425 /* Get pointer to next RX descriptor to be processed by SW */
2426 static inline struct mvpp2_rx_desc *
mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue * rxq)2427 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
2428 {
2429 int rx_desc = rxq->next_desc_to_proc;
2430
2431 rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
2432 prefetch(rxq->descs + rxq->next_desc_to_proc);
2433 return rxq->descs + rx_desc;
2434 }
2435
2436 /* Set rx queue offset */
mvpp2_rxq_offset_set(struct mvpp2_port * port,int prxq,int offset)2437 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
2438 int prxq, int offset)
2439 {
2440 u32 val;
2441
2442 /* Convert offset from bytes to units of 32 bytes */
2443 offset = offset >> 5;
2444
2445 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2446 val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
2447
2448 /* Offset is in */
2449 val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
2450 MVPP2_RXQ_PACKET_OFFSET_MASK);
2451
2452 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2453 }
2454
2455 /* Tx descriptors helper methods */
2456
2457 /* Get pointer to next Tx descriptor to be processed (send) by HW */
2458 static struct mvpp2_tx_desc *
mvpp2_txq_next_desc_get(struct mvpp2_tx_queue * txq)2459 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
2460 {
2461 int tx_desc = txq->next_desc_to_proc;
2462
2463 txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
2464 return txq->descs + tx_desc;
2465 }
2466
2467 /* Update HW with number of aggregated Tx descriptors to be sent
2468 *
2469 * Called only from mvpp2_tx(), so migration is disabled, using
2470 * smp_processor_id() is OK.
2471 */
mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port * port,int pending)2472 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
2473 {
2474 /* aggregated access - relevant TXQ number is written in TX desc */
2475 mvpp2_thread_write(port->priv,
2476 mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2477 MVPP2_AGGR_TXQ_UPDATE_REG, pending);
2478 }
2479
2480 /* Check if there are enough free descriptors in aggregated txq.
2481 * If not, update the number of occupied descriptors and repeat the check.
2482 *
2483 * Called only from mvpp2_tx(), so migration is disabled, using
2484 * smp_processor_id() is OK.
2485 */
mvpp2_aggr_desc_num_check(struct mvpp2_port * port,struct mvpp2_tx_queue * aggr_txq,int num)2486 static int mvpp2_aggr_desc_num_check(struct mvpp2_port *port,
2487 struct mvpp2_tx_queue *aggr_txq, int num)
2488 {
2489 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
2490 /* Update number of occupied aggregated Tx descriptors */
2491 unsigned int thread =
2492 mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2493 u32 val = mvpp2_read_relaxed(port->priv,
2494 MVPP2_AGGR_TXQ_STATUS_REG(thread));
2495
2496 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
2497
2498 if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
2499 return -ENOMEM;
2500 }
2501 return 0;
2502 }
2503
2504 /* Reserved Tx descriptors allocation request
2505 *
2506 * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
2507 * only by mvpp2_tx(), so migration is disabled, using
2508 * smp_processor_id() is OK.
2509 */
mvpp2_txq_alloc_reserved_desc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,int num)2510 static int mvpp2_txq_alloc_reserved_desc(struct mvpp2_port *port,
2511 struct mvpp2_tx_queue *txq, int num)
2512 {
2513 unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2514 struct mvpp2 *priv = port->priv;
2515 u32 val;
2516
2517 val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
2518 mvpp2_thread_write_relaxed(priv, thread, MVPP2_TXQ_RSVD_REQ_REG, val);
2519
2520 val = mvpp2_thread_read_relaxed(priv, thread, MVPP2_TXQ_RSVD_RSLT_REG);
2521
2522 return val & MVPP2_TXQ_RSVD_RSLT_MASK;
2523 }
2524
2525 /* Check if there are enough reserved descriptors for transmission.
2526 * If not, request chunk of reserved descriptors and check again.
2527 */
mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu,int num)2528 static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port *port,
2529 struct mvpp2_tx_queue *txq,
2530 struct mvpp2_txq_pcpu *txq_pcpu,
2531 int num)
2532 {
2533 int req, desc_count;
2534 unsigned int thread;
2535
2536 if (txq_pcpu->reserved_num >= num)
2537 return 0;
2538
2539 /* Not enough descriptors reserved! Update the reserved descriptor
2540 * count and check again.
2541 */
2542
2543 desc_count = 0;
2544 /* Compute total of used descriptors */
2545 for (thread = 0; thread < port->priv->nthreads; thread++) {
2546 struct mvpp2_txq_pcpu *txq_pcpu_aux;
2547
2548 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, thread);
2549 desc_count += txq_pcpu_aux->count;
2550 desc_count += txq_pcpu_aux->reserved_num;
2551 }
2552
2553 req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
2554 desc_count += req;
2555
2556 if (desc_count >
2557 (txq->size - (MVPP2_MAX_THREADS * MVPP2_CPU_DESC_CHUNK)))
2558 return -ENOMEM;
2559
2560 txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(port, txq, req);
2561
2562 /* OK, the descriptor could have been updated: check again. */
2563 if (txq_pcpu->reserved_num < num)
2564 return -ENOMEM;
2565 return 0;
2566 }
2567
2568 /* Release the last allocated Tx descriptor. Useful to handle DMA
2569 * mapping failures in the Tx path.
2570 */
mvpp2_txq_desc_put(struct mvpp2_tx_queue * txq)2571 static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
2572 {
2573 if (txq->next_desc_to_proc == 0)
2574 txq->next_desc_to_proc = txq->last_desc - 1;
2575 else
2576 txq->next_desc_to_proc--;
2577 }
2578
2579 /* 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)2580 static u32 mvpp2_txq_desc_csum(int l3_offs, __be16 l3_proto,
2581 int ip_hdr_len, int l4_proto)
2582 {
2583 u32 command;
2584
2585 /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
2586 * G_L4_chk, L4_type required only for checksum calculation
2587 */
2588 command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
2589 command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
2590 command |= MVPP2_TXD_IP_CSUM_DISABLE;
2591
2592 if (l3_proto == htons(ETH_P_IP)) {
2593 command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
2594 command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
2595 } else {
2596 command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
2597 }
2598
2599 if (l4_proto == IPPROTO_TCP) {
2600 command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
2601 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
2602 } else if (l4_proto == IPPROTO_UDP) {
2603 command |= MVPP2_TXD_L4_UDP; /* enable UDP */
2604 command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
2605 } else {
2606 command |= MVPP2_TXD_L4_CSUM_NOT;
2607 }
2608
2609 return command;
2610 }
2611
2612 /* Get number of sent descriptors and decrement counter.
2613 * The number of sent descriptors is returned.
2614 * Per-thread access
2615 *
2616 * Called only from mvpp2_txq_done(), called from mvpp2_tx()
2617 * (migration disabled) and from the TX completion tasklet (migration
2618 * disabled) so using smp_processor_id() is OK.
2619 */
mvpp2_txq_sent_desc_proc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2620 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
2621 struct mvpp2_tx_queue *txq)
2622 {
2623 u32 val;
2624
2625 /* Reading status reg resets transmitted descriptor counter */
2626 val = mvpp2_thread_read_relaxed(port->priv,
2627 mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2628 MVPP2_TXQ_SENT_REG(txq->id));
2629
2630 return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
2631 MVPP2_TRANSMITTED_COUNT_OFFSET;
2632 }
2633
2634 /* Called through on_each_cpu(), so runs on all CPUs, with migration
2635 * disabled, therefore using smp_processor_id() is OK.
2636 */
mvpp2_txq_sent_counter_clear(void * arg)2637 static void mvpp2_txq_sent_counter_clear(void *arg)
2638 {
2639 struct mvpp2_port *port = arg;
2640 int queue;
2641
2642 /* If the thread isn't used, don't do anything */
2643 if (smp_processor_id() >= port->priv->nthreads)
2644 return;
2645
2646 for (queue = 0; queue < port->ntxqs; queue++) {
2647 int id = port->txqs[queue]->id;
2648
2649 mvpp2_thread_read(port->priv,
2650 mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2651 MVPP2_TXQ_SENT_REG(id));
2652 }
2653 }
2654
2655 /* Set max sizes for Tx queues */
mvpp2_txp_max_tx_size_set(struct mvpp2_port * port)2656 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
2657 {
2658 u32 val, size, mtu;
2659 int txq, tx_port_num;
2660
2661 mtu = port->pkt_size * 8;
2662 if (mtu > MVPP2_TXP_MTU_MAX)
2663 mtu = MVPP2_TXP_MTU_MAX;
2664
2665 /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
2666 mtu = 3 * mtu;
2667
2668 /* Indirect access to registers */
2669 tx_port_num = mvpp2_egress_port(port);
2670 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2671
2672 /* Set MTU */
2673 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
2674 val &= ~MVPP2_TXP_MTU_MAX;
2675 val |= mtu;
2676 mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
2677
2678 /* TXP token size and all TXQs token size must be larger that MTU */
2679 val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
2680 size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
2681 if (size < mtu) {
2682 size = mtu;
2683 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
2684 val |= size;
2685 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2686 }
2687
2688 for (txq = 0; txq < port->ntxqs; txq++) {
2689 val = mvpp2_read(port->priv,
2690 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
2691 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
2692
2693 if (size < mtu) {
2694 size = mtu;
2695 val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
2696 val |= size;
2697 mvpp2_write(port->priv,
2698 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
2699 val);
2700 }
2701 }
2702 }
2703
2704 /* Set the number of non-occupied descriptors threshold */
mvpp2_set_rxq_free_tresh(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2705 static void mvpp2_set_rxq_free_tresh(struct mvpp2_port *port,
2706 struct mvpp2_rx_queue *rxq)
2707 {
2708 u32 val;
2709
2710 mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
2711
2712 val = mvpp2_read(port->priv, MVPP2_RXQ_THRESH_REG);
2713 val &= ~MVPP2_RXQ_NON_OCCUPIED_MASK;
2714 val |= MSS_THRESHOLD_STOP << MVPP2_RXQ_NON_OCCUPIED_OFFSET;
2715 mvpp2_write(port->priv, MVPP2_RXQ_THRESH_REG, val);
2716 }
2717
2718 /* Set the number of packets that will be received before Rx interrupt
2719 * will be generated by HW.
2720 */
mvpp2_rx_pkts_coal_set(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2721 static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
2722 struct mvpp2_rx_queue *rxq)
2723 {
2724 unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2725
2726 if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
2727 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
2728
2729 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2730 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_THRESH_REG,
2731 rxq->pkts_coal);
2732
2733 put_cpu();
2734 }
2735
2736 /* 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)2737 static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
2738 struct mvpp2_tx_queue *txq)
2739 {
2740 unsigned int thread;
2741 u32 val;
2742
2743 if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
2744 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
2745
2746 val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
2747 /* PKT-coalescing registers are per-queue + per-thread */
2748 for (thread = 0; thread < MVPP2_MAX_THREADS; thread++) {
2749 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2750 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_THRESH_REG, val);
2751 }
2752 }
2753
mvpp2_usec_to_cycles(u32 usec,unsigned long clk_hz)2754 static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
2755 {
2756 u64 tmp = (u64)clk_hz * usec;
2757
2758 do_div(tmp, USEC_PER_SEC);
2759
2760 return tmp > U32_MAX ? U32_MAX : tmp;
2761 }
2762
mvpp2_cycles_to_usec(u32 cycles,unsigned long clk_hz)2763 static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
2764 {
2765 u64 tmp = (u64)cycles * USEC_PER_SEC;
2766
2767 do_div(tmp, clk_hz);
2768
2769 return tmp > U32_MAX ? U32_MAX : tmp;
2770 }
2771
2772 /* Set the time delay in usec before Rx interrupt */
mvpp2_rx_time_coal_set(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2773 static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
2774 struct mvpp2_rx_queue *rxq)
2775 {
2776 unsigned long freq = port->priv->tclk;
2777 u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2778
2779 if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
2780 rxq->time_coal =
2781 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
2782
2783 /* re-evaluate to get actual register value */
2784 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2785 }
2786
2787 mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
2788 }
2789
mvpp2_tx_time_coal_set(struct mvpp2_port * port)2790 static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
2791 {
2792 unsigned long freq = port->priv->tclk;
2793 u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2794
2795 if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
2796 port->tx_time_coal =
2797 mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
2798
2799 /* re-evaluate to get actual register value */
2800 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2801 }
2802
2803 mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
2804 }
2805
2806 /* 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)2807 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
2808 struct mvpp2_tx_queue *txq,
2809 struct mvpp2_txq_pcpu *txq_pcpu, int num)
2810 {
2811 struct xdp_frame_bulk bq;
2812 int i;
2813
2814 xdp_frame_bulk_init(&bq);
2815
2816 rcu_read_lock(); /* need for xdp_return_frame_bulk */
2817
2818 for (i = 0; i < num; i++) {
2819 struct mvpp2_txq_pcpu_buf *tx_buf =
2820 txq_pcpu->buffs + txq_pcpu->txq_get_index;
2821
2822 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma) &&
2823 tx_buf->type != MVPP2_TYPE_XDP_TX)
2824 dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
2825 tx_buf->size, DMA_TO_DEVICE);
2826 if (tx_buf->type == MVPP2_TYPE_SKB && tx_buf->skb)
2827 dev_kfree_skb_any(tx_buf->skb);
2828 else if (tx_buf->type == MVPP2_TYPE_XDP_TX ||
2829 tx_buf->type == MVPP2_TYPE_XDP_NDO)
2830 xdp_return_frame_bulk(tx_buf->xdpf, &bq);
2831
2832 mvpp2_txq_inc_get(txq_pcpu);
2833 }
2834 xdp_flush_frame_bulk(&bq);
2835
2836 rcu_read_unlock();
2837 }
2838
mvpp2_get_rx_queue(struct mvpp2_port * port,u32 cause)2839 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
2840 u32 cause)
2841 {
2842 int queue = fls(cause) - 1;
2843
2844 return port->rxqs[queue];
2845 }
2846
mvpp2_get_tx_queue(struct mvpp2_port * port,u32 cause)2847 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
2848 u32 cause)
2849 {
2850 int queue = fls(cause) - 1;
2851
2852 return port->txqs[queue];
2853 }
2854
2855 /* Handle end of transmission */
mvpp2_txq_done(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu)2856 static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
2857 struct mvpp2_txq_pcpu *txq_pcpu)
2858 {
2859 struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
2860 int tx_done;
2861
2862 if (txq_pcpu->thread != mvpp2_cpu_to_thread(port->priv, smp_processor_id()))
2863 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
2864
2865 tx_done = mvpp2_txq_sent_desc_proc(port, txq);
2866 if (!tx_done)
2867 return;
2868 mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
2869
2870 txq_pcpu->count -= tx_done;
2871
2872 if (netif_tx_queue_stopped(nq))
2873 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
2874 netif_tx_wake_queue(nq);
2875 }
2876
mvpp2_tx_done(struct mvpp2_port * port,u32 cause,unsigned int thread)2877 static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
2878 unsigned int thread)
2879 {
2880 struct mvpp2_tx_queue *txq;
2881 struct mvpp2_txq_pcpu *txq_pcpu;
2882 unsigned int tx_todo = 0;
2883
2884 while (cause) {
2885 txq = mvpp2_get_tx_queue(port, cause);
2886 if (!txq)
2887 break;
2888
2889 txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2890
2891 if (txq_pcpu->count) {
2892 mvpp2_txq_done(port, txq, txq_pcpu);
2893 tx_todo += txq_pcpu->count;
2894 }
2895
2896 cause &= ~(1 << txq->log_id);
2897 }
2898 return tx_todo;
2899 }
2900
2901 /* Rx/Tx queue initialization/cleanup methods */
2902
2903 /* 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)2904 static int mvpp2_aggr_txq_init(struct platform_device *pdev,
2905 struct mvpp2_tx_queue *aggr_txq,
2906 unsigned int thread, struct mvpp2 *priv)
2907 {
2908 u32 txq_dma;
2909
2910 /* Allocate memory for TX descriptors */
2911 aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
2912 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
2913 &aggr_txq->descs_dma, GFP_KERNEL);
2914 if (!aggr_txq->descs)
2915 return -ENOMEM;
2916
2917 aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
2918
2919 /* Aggr TXQ no reset WA */
2920 aggr_txq->next_desc_to_proc = mvpp2_read(priv,
2921 MVPP2_AGGR_TXQ_INDEX_REG(thread));
2922
2923 /* Set Tx descriptors queue starting address indirect
2924 * access
2925 */
2926 if (priv->hw_version == MVPP21)
2927 txq_dma = aggr_txq->descs_dma;
2928 else
2929 txq_dma = aggr_txq->descs_dma >>
2930 MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
2931
2932 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(thread), txq_dma);
2933 mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(thread),
2934 MVPP2_AGGR_TXQ_SIZE);
2935
2936 return 0;
2937 }
2938
2939 /* Create a specified Rx queue */
mvpp2_rxq_init(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2940 static int mvpp2_rxq_init(struct mvpp2_port *port,
2941 struct mvpp2_rx_queue *rxq)
2942 {
2943 struct mvpp2 *priv = port->priv;
2944 unsigned int thread;
2945 u32 rxq_dma;
2946 int err;
2947
2948 rxq->size = port->rx_ring_size;
2949
2950 /* Allocate memory for RX descriptors */
2951 rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
2952 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2953 &rxq->descs_dma, GFP_KERNEL);
2954 if (!rxq->descs)
2955 return -ENOMEM;
2956
2957 rxq->last_desc = rxq->size - 1;
2958
2959 /* Zero occupied and non-occupied counters - direct access */
2960 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2961
2962 /* Set Rx descriptors queue starting address - indirect access */
2963 thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2964 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2965 if (port->priv->hw_version == MVPP21)
2966 rxq_dma = rxq->descs_dma;
2967 else
2968 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
2969 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
2970 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
2971 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_INDEX_REG, 0);
2972 put_cpu();
2973
2974 /* Set Offset */
2975 mvpp2_rxq_offset_set(port, rxq->id, MVPP2_SKB_HEADROOM);
2976
2977 /* Set coalescing pkts and time */
2978 mvpp2_rx_pkts_coal_set(port, rxq);
2979 mvpp2_rx_time_coal_set(port, rxq);
2980
2981 /* Set the number of non occupied descriptors threshold */
2982 mvpp2_set_rxq_free_tresh(port, rxq);
2983
2984 /* Add number of descriptors ready for receiving packets */
2985 mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
2986
2987 if (priv->percpu_pools) {
2988 err = xdp_rxq_info_reg(&rxq->xdp_rxq_short, port->dev, rxq->logic_rxq, 0);
2989 if (err < 0)
2990 goto err_free_dma;
2991
2992 err = xdp_rxq_info_reg(&rxq->xdp_rxq_long, port->dev, rxq->logic_rxq, 0);
2993 if (err < 0)
2994 goto err_unregister_rxq_short;
2995
2996 /* Every RXQ has a pool for short and another for long packets */
2997 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_short,
2998 MEM_TYPE_PAGE_POOL,
2999 priv->page_pool[rxq->logic_rxq]);
3000 if (err < 0)
3001 goto err_unregister_rxq_long;
3002
3003 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_long,
3004 MEM_TYPE_PAGE_POOL,
3005 priv->page_pool[rxq->logic_rxq +
3006 port->nrxqs]);
3007 if (err < 0)
3008 goto err_unregister_mem_rxq_short;
3009 }
3010
3011 return 0;
3012
3013 err_unregister_mem_rxq_short:
3014 xdp_rxq_info_unreg_mem_model(&rxq->xdp_rxq_short);
3015 err_unregister_rxq_long:
3016 xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
3017 err_unregister_rxq_short:
3018 xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
3019 err_free_dma:
3020 dma_free_coherent(port->dev->dev.parent,
3021 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
3022 rxq->descs, rxq->descs_dma);
3023 return err;
3024 }
3025
3026 /* Push packets received by the RXQ to BM pool */
mvpp2_rxq_drop_pkts(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)3027 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
3028 struct mvpp2_rx_queue *rxq)
3029 {
3030 int rx_received, i;
3031
3032 rx_received = mvpp2_rxq_received(port, rxq->id);
3033 if (!rx_received)
3034 return;
3035
3036 for (i = 0; i < rx_received; i++) {
3037 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3038 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3039 int pool;
3040
3041 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
3042 MVPP2_RXD_BM_POOL_ID_OFFS;
3043
3044 mvpp2_bm_pool_put(port, pool,
3045 mvpp2_rxdesc_dma_addr_get(port, rx_desc),
3046 mvpp2_rxdesc_cookie_get(port, rx_desc));
3047 }
3048 mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
3049 }
3050
3051 /* Cleanup Rx queue */
mvpp2_rxq_deinit(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)3052 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
3053 struct mvpp2_rx_queue *rxq)
3054 {
3055 unsigned int thread;
3056
3057 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_short))
3058 xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
3059
3060 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_long))
3061 xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
3062
3063 mvpp2_rxq_drop_pkts(port, rxq);
3064
3065 if (rxq->descs)
3066 dma_free_coherent(port->dev->dev.parent,
3067 rxq->size * MVPP2_DESC_ALIGNED_SIZE,
3068 rxq->descs,
3069 rxq->descs_dma);
3070
3071 rxq->descs = NULL;
3072 rxq->last_desc = 0;
3073 rxq->next_desc_to_proc = 0;
3074 rxq->descs_dma = 0;
3075
3076 /* Clear Rx descriptors queue starting address and size;
3077 * free descriptor number
3078 */
3079 mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
3080 thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
3081 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
3082 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, 0);
3083 mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, 0);
3084 put_cpu();
3085 }
3086
3087 /* Create and initialize a Tx queue */
mvpp2_txq_init(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)3088 static int mvpp2_txq_init(struct mvpp2_port *port,
3089 struct mvpp2_tx_queue *txq)
3090 {
3091 u32 val;
3092 unsigned int thread;
3093 int desc, desc_per_txq, tx_port_num;
3094 struct mvpp2_txq_pcpu *txq_pcpu;
3095
3096 txq->size = port->tx_ring_size;
3097
3098 /* Allocate memory for Tx descriptors */
3099 txq->descs = dma_alloc_coherent(port->dev->dev.parent,
3100 txq->size * MVPP2_DESC_ALIGNED_SIZE,
3101 &txq->descs_dma, GFP_KERNEL);
3102 if (!txq->descs)
3103 return -ENOMEM;
3104
3105 txq->last_desc = txq->size - 1;
3106
3107 /* Set Tx descriptors queue starting address - indirect access */
3108 thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
3109 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
3110 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG,
3111 txq->descs_dma);
3112 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG,
3113 txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
3114 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_INDEX_REG, 0);
3115 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_RSVD_CLR_REG,
3116 txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
3117 val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PENDING_REG);
3118 val &= ~MVPP2_TXQ_PENDING_MASK;
3119 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PENDING_REG, val);
3120
3121 /* Calculate base address in prefetch buffer. We reserve 16 descriptors
3122 * for each existing TXQ.
3123 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
3124 * GBE ports assumed to be continuous from 0 to MVPP2_MAX_PORTS
3125 */
3126 desc_per_txq = 16;
3127 desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
3128 (txq->log_id * desc_per_txq);
3129
3130 mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG,
3131 MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
3132 MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
3133 put_cpu();
3134
3135 /* WRR / EJP configuration - indirect access */
3136 tx_port_num = mvpp2_egress_port(port);
3137 mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
3138
3139 val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
3140 val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
3141 val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
3142 val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
3143 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
3144
3145 val = MVPP2_TXQ_TOKEN_SIZE_MAX;
3146 mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
3147 val);
3148
3149 for (thread = 0; thread < port->priv->nthreads; thread++) {
3150 txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3151 txq_pcpu->size = txq->size;
3152 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
3153 sizeof(*txq_pcpu->buffs),
3154 GFP_KERNEL);
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,bool link)3422 static void mvpp2_isr_handle_link(struct mvpp2_port *port, bool link)
3423 {
3424 struct net_device *dev = port->dev;
3425
3426 if (port->phylink) {
3427 phylink_mac_change(port->phylink, link);
3428 return;
3429 }
3430
3431 if (!netif_running(dev))
3432 return;
3433
3434 if (link) {
3435 mvpp2_interrupts_enable(port);
3436
3437 mvpp2_egress_enable(port);
3438 mvpp2_ingress_enable(port);
3439 netif_carrier_on(dev);
3440 netif_tx_wake_all_queues(dev);
3441 } else {
3442 netif_tx_stop_all_queues(dev);
3443 netif_carrier_off(dev);
3444 mvpp2_ingress_disable(port);
3445 mvpp2_egress_disable(port);
3446
3447 mvpp2_interrupts_disable(port);
3448 }
3449 }
3450
mvpp2_isr_handle_xlg(struct mvpp2_port * port)3451 static void mvpp2_isr_handle_xlg(struct mvpp2_port *port)
3452 {
3453 bool link;
3454 u32 val;
3455
3456 val = readl(port->base + MVPP22_XLG_INT_STAT);
3457 if (val & MVPP22_XLG_INT_STAT_LINK) {
3458 val = readl(port->base + MVPP22_XLG_STATUS);
3459 link = (val & MVPP22_XLG_STATUS_LINK_UP);
3460 mvpp2_isr_handle_link(port, link);
3461 }
3462 }
3463
mvpp2_isr_handle_gmac_internal(struct mvpp2_port * port)3464 static void mvpp2_isr_handle_gmac_internal(struct mvpp2_port *port)
3465 {
3466 bool link;
3467 u32 val;
3468
3469 if (phy_interface_mode_is_rgmii(port->phy_interface) ||
3470 phy_interface_mode_is_8023z(port->phy_interface) ||
3471 port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
3472 val = readl(port->base + MVPP22_GMAC_INT_STAT);
3473 if (val & MVPP22_GMAC_INT_STAT_LINK) {
3474 val = readl(port->base + MVPP2_GMAC_STATUS0);
3475 link = (val & MVPP2_GMAC_STATUS0_LINK_UP);
3476 mvpp2_isr_handle_link(port, link);
3477 }
3478 }
3479 }
3480
3481 /* Per-port interrupt for link status changes */
mvpp2_port_isr(int irq,void * dev_id)3482 static irqreturn_t mvpp2_port_isr(int irq, void *dev_id)
3483 {
3484 struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
3485 u32 val;
3486
3487 mvpp22_gop_mask_irq(port);
3488
3489 if (mvpp2_port_supports_xlg(port) &&
3490 mvpp2_is_xlg(port->phy_interface)) {
3491 /* Check the external status register */
3492 val = readl(port->base + MVPP22_XLG_EXT_INT_STAT);
3493 if (val & MVPP22_XLG_EXT_INT_STAT_XLG)
3494 mvpp2_isr_handle_xlg(port);
3495 if (val & MVPP22_XLG_EXT_INT_STAT_PTP)
3496 mvpp2_isr_handle_ptp(port);
3497 } else {
3498 /* If it's not the XLG, we must be using the GMAC.
3499 * Check the summary status.
3500 */
3501 val = readl(port->base + MVPP22_GMAC_INT_SUM_STAT);
3502 if (val & MVPP22_GMAC_INT_SUM_STAT_INTERNAL)
3503 mvpp2_isr_handle_gmac_internal(port);
3504 if (val & MVPP22_GMAC_INT_SUM_STAT_PTP)
3505 mvpp2_isr_handle_ptp(port);
3506 }
3507
3508 mvpp22_gop_unmask_irq(port);
3509 return IRQ_HANDLED;
3510 }
3511
mvpp2_hr_timer_cb(struct hrtimer * timer)3512 static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
3513 {
3514 struct net_device *dev;
3515 struct mvpp2_port *port;
3516 struct mvpp2_port_pcpu *port_pcpu;
3517 unsigned int tx_todo, cause;
3518
3519 port_pcpu = container_of(timer, struct mvpp2_port_pcpu, tx_done_timer);
3520 dev = port_pcpu->dev;
3521
3522 if (!netif_running(dev))
3523 return HRTIMER_NORESTART;
3524
3525 port_pcpu->timer_scheduled = false;
3526 port = netdev_priv(dev);
3527
3528 /* Process all the Tx queues */
3529 cause = (1 << port->ntxqs) - 1;
3530 tx_todo = mvpp2_tx_done(port, cause,
3531 mvpp2_cpu_to_thread(port->priv, smp_processor_id()));
3532
3533 /* Set the timer in case not all the packets were processed */
3534 if (tx_todo && !port_pcpu->timer_scheduled) {
3535 port_pcpu->timer_scheduled = true;
3536 hrtimer_forward_now(&port_pcpu->tx_done_timer,
3537 MVPP2_TXDONE_HRTIMER_PERIOD_NS);
3538
3539 return HRTIMER_RESTART;
3540 }
3541 return HRTIMER_NORESTART;
3542 }
3543
3544 /* Main RX/TX processing routines */
3545
3546 /* Display more error info */
mvpp2_rx_error(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)3547 static void mvpp2_rx_error(struct mvpp2_port *port,
3548 struct mvpp2_rx_desc *rx_desc)
3549 {
3550 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3551 size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3552 char *err_str = NULL;
3553
3554 switch (status & MVPP2_RXD_ERR_CODE_MASK) {
3555 case MVPP2_RXD_ERR_CRC:
3556 err_str = "crc";
3557 break;
3558 case MVPP2_RXD_ERR_OVERRUN:
3559 err_str = "overrun";
3560 break;
3561 case MVPP2_RXD_ERR_RESOURCE:
3562 err_str = "resource";
3563 break;
3564 }
3565 if (err_str && net_ratelimit())
3566 netdev_err(port->dev,
3567 "bad rx status %08x (%s error), size=%zu\n",
3568 status, err_str, sz);
3569 }
3570
3571 /* Handle RX checksum offload */
mvpp2_rx_csum(struct mvpp2_port * port,u32 status)3572 static int mvpp2_rx_csum(struct mvpp2_port *port, u32 status)
3573 {
3574 if (((status & MVPP2_RXD_L3_IP4) &&
3575 !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
3576 (status & MVPP2_RXD_L3_IP6))
3577 if (((status & MVPP2_RXD_L4_UDP) ||
3578 (status & MVPP2_RXD_L4_TCP)) &&
3579 (status & MVPP2_RXD_L4_CSUM_OK))
3580 return CHECKSUM_UNNECESSARY;
3581
3582 return CHECKSUM_NONE;
3583 }
3584
3585 /* 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)3586 static int mvpp2_rx_refill(struct mvpp2_port *port,
3587 struct mvpp2_bm_pool *bm_pool,
3588 struct page_pool *page_pool, int pool)
3589 {
3590 dma_addr_t dma_addr;
3591 phys_addr_t phys_addr;
3592 void *buf;
3593
3594 buf = mvpp2_buf_alloc(port, bm_pool, page_pool,
3595 &dma_addr, &phys_addr, GFP_ATOMIC);
3596 if (!buf)
3597 return -ENOMEM;
3598
3599 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3600
3601 return 0;
3602 }
3603
3604 /* Handle tx checksum */
mvpp2_skb_tx_csum(struct mvpp2_port * port,struct sk_buff * skb)3605 static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
3606 {
3607 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3608 int ip_hdr_len = 0;
3609 u8 l4_proto;
3610 __be16 l3_proto = vlan_get_protocol(skb);
3611
3612 if (l3_proto == htons(ETH_P_IP)) {
3613 struct iphdr *ip4h = ip_hdr(skb);
3614
3615 /* Calculate IPv4 checksum and L4 checksum */
3616 ip_hdr_len = ip4h->ihl;
3617 l4_proto = ip4h->protocol;
3618 } else if (l3_proto == htons(ETH_P_IPV6)) {
3619 struct ipv6hdr *ip6h = ipv6_hdr(skb);
3620
3621 /* Read l4_protocol from one of IPv6 extra headers */
3622 if (skb_network_header_len(skb) > 0)
3623 ip_hdr_len = (skb_network_header_len(skb) >> 2);
3624 l4_proto = ip6h->nexthdr;
3625 } else {
3626 return MVPP2_TXD_L4_CSUM_NOT;
3627 }
3628
3629 return mvpp2_txq_desc_csum(skb_network_offset(skb),
3630 l3_proto, ip_hdr_len, l4_proto);
3631 }
3632
3633 return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
3634 }
3635
mvpp2_xdp_finish_tx(struct mvpp2_port * port,u16 txq_id,int nxmit,int nxmit_byte)3636 static void mvpp2_xdp_finish_tx(struct mvpp2_port *port, u16 txq_id, int nxmit, int nxmit_byte)
3637 {
3638 unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3639 struct mvpp2_tx_queue *aggr_txq;
3640 struct mvpp2_txq_pcpu *txq_pcpu;
3641 struct mvpp2_tx_queue *txq;
3642 struct netdev_queue *nq;
3643
3644 txq = port->txqs[txq_id];
3645 txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3646 nq = netdev_get_tx_queue(port->dev, txq_id);
3647 aggr_txq = &port->priv->aggr_txqs[thread];
3648
3649 txq_pcpu->reserved_num -= nxmit;
3650 txq_pcpu->count += nxmit;
3651 aggr_txq->count += nxmit;
3652
3653 /* Enable transmit */
3654 wmb();
3655 mvpp2_aggr_txq_pend_desc_add(port, nxmit);
3656
3657 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
3658 netif_tx_stop_queue(nq);
3659
3660 /* Finalize TX processing */
3661 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
3662 mvpp2_txq_done(port, txq, txq_pcpu);
3663 }
3664
3665 static int
mvpp2_xdp_submit_frame(struct mvpp2_port * port,u16 txq_id,struct xdp_frame * xdpf,bool dma_map)3666 mvpp2_xdp_submit_frame(struct mvpp2_port *port, u16 txq_id,
3667 struct xdp_frame *xdpf, bool dma_map)
3668 {
3669 unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3670 u32 tx_cmd = MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE |
3671 MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
3672 enum mvpp2_tx_buf_type buf_type;
3673 struct mvpp2_txq_pcpu *txq_pcpu;
3674 struct mvpp2_tx_queue *aggr_txq;
3675 struct mvpp2_tx_desc *tx_desc;
3676 struct mvpp2_tx_queue *txq;
3677 int ret = MVPP2_XDP_TX;
3678 dma_addr_t dma_addr;
3679
3680 txq = port->txqs[txq_id];
3681 txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3682 aggr_txq = &port->priv->aggr_txqs[thread];
3683
3684 /* Check number of available descriptors */
3685 if (mvpp2_aggr_desc_num_check(port, aggr_txq, 1) ||
3686 mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, 1)) {
3687 ret = MVPP2_XDP_DROPPED;
3688 goto out;
3689 }
3690
3691 /* Get a descriptor for the first part of the packet */
3692 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3693 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3694 mvpp2_txdesc_size_set(port, tx_desc, xdpf->len);
3695
3696 if (dma_map) {
3697 /* XDP_REDIRECT or AF_XDP */
3698 dma_addr = dma_map_single(port->dev->dev.parent, xdpf->data,
3699 xdpf->len, DMA_TO_DEVICE);
3700
3701 if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
3702 mvpp2_txq_desc_put(txq);
3703 ret = MVPP2_XDP_DROPPED;
3704 goto out;
3705 }
3706
3707 buf_type = MVPP2_TYPE_XDP_NDO;
3708 } else {
3709 /* XDP_TX */
3710 struct page *page = virt_to_page(xdpf->data);
3711
3712 dma_addr = page_pool_get_dma_addr(page) +
3713 sizeof(*xdpf) + xdpf->headroom;
3714 dma_sync_single_for_device(port->dev->dev.parent, dma_addr,
3715 xdpf->len, DMA_BIDIRECTIONAL);
3716
3717 buf_type = MVPP2_TYPE_XDP_TX;
3718 }
3719
3720 mvpp2_txdesc_dma_addr_set(port, tx_desc, dma_addr);
3721
3722 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
3723 mvpp2_txq_inc_put(port, txq_pcpu, xdpf, tx_desc, buf_type);
3724
3725 out:
3726 return ret;
3727 }
3728
3729 static int
mvpp2_xdp_xmit_back(struct mvpp2_port * port,struct xdp_buff * xdp)3730 mvpp2_xdp_xmit_back(struct mvpp2_port *port, struct xdp_buff *xdp)
3731 {
3732 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
3733 struct xdp_frame *xdpf;
3734 u16 txq_id;
3735 int ret;
3736
3737 xdpf = xdp_convert_buff_to_frame(xdp);
3738 if (unlikely(!xdpf))
3739 return MVPP2_XDP_DROPPED;
3740
3741 /* The first of the TX queues are used for XPS,
3742 * the second half for XDP_TX
3743 */
3744 txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);
3745
3746 ret = mvpp2_xdp_submit_frame(port, txq_id, xdpf, false);
3747 if (ret == MVPP2_XDP_TX) {
3748 u64_stats_update_begin(&stats->syncp);
3749 stats->tx_bytes += xdpf->len;
3750 stats->tx_packets++;
3751 stats->xdp_tx++;
3752 u64_stats_update_end(&stats->syncp);
3753
3754 mvpp2_xdp_finish_tx(port, txq_id, 1, xdpf->len);
3755 } else {
3756 u64_stats_update_begin(&stats->syncp);
3757 stats->xdp_tx_err++;
3758 u64_stats_update_end(&stats->syncp);
3759 }
3760
3761 return ret;
3762 }
3763
3764 static int
mvpp2_xdp_xmit(struct net_device * dev,int num_frame,struct xdp_frame ** frames,u32 flags)3765 mvpp2_xdp_xmit(struct net_device *dev, int num_frame,
3766 struct xdp_frame **frames, u32 flags)
3767 {
3768 struct mvpp2_port *port = netdev_priv(dev);
3769 int i, nxmit_byte = 0, nxmit = 0;
3770 struct mvpp2_pcpu_stats *stats;
3771 u16 txq_id;
3772 u32 ret;
3773
3774 if (unlikely(test_bit(0, &port->state)))
3775 return -ENETDOWN;
3776
3777 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3778 return -EINVAL;
3779
3780 /* The first of the TX queues are used for XPS,
3781 * the second half for XDP_TX
3782 */
3783 txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);
3784
3785 for (i = 0; i < num_frame; i++) {
3786 ret = mvpp2_xdp_submit_frame(port, txq_id, frames[i], true);
3787 if (ret != MVPP2_XDP_TX)
3788 break;
3789
3790 nxmit_byte += frames[i]->len;
3791 nxmit++;
3792 }
3793
3794 if (likely(nxmit > 0))
3795 mvpp2_xdp_finish_tx(port, txq_id, nxmit, nxmit_byte);
3796
3797 stats = this_cpu_ptr(port->stats);
3798 u64_stats_update_begin(&stats->syncp);
3799 stats->tx_bytes += nxmit_byte;
3800 stats->tx_packets += nxmit;
3801 stats->xdp_xmit += nxmit;
3802 stats->xdp_xmit_err += num_frame - nxmit;
3803 u64_stats_update_end(&stats->syncp);
3804
3805 return nxmit;
3806 }
3807
3808 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)3809 mvpp2_run_xdp(struct mvpp2_port *port, struct bpf_prog *prog,
3810 struct xdp_buff *xdp, struct page_pool *pp,
3811 struct mvpp2_pcpu_stats *stats)
3812 {
3813 unsigned int len, sync, err;
3814 struct page *page;
3815 u32 ret, act;
3816
3817 len = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
3818 act = bpf_prog_run_xdp(prog, xdp);
3819
3820 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
3821 sync = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
3822 sync = max(sync, len);
3823
3824 switch (act) {
3825 case XDP_PASS:
3826 stats->xdp_pass++;
3827 ret = MVPP2_XDP_PASS;
3828 break;
3829 case XDP_REDIRECT:
3830 err = xdp_do_redirect(port->dev, xdp, prog);
3831 if (unlikely(err)) {
3832 ret = MVPP2_XDP_DROPPED;
3833 page = virt_to_head_page(xdp->data);
3834 page_pool_put_page(pp, page, sync, true);
3835 } else {
3836 ret = MVPP2_XDP_REDIR;
3837 stats->xdp_redirect++;
3838 }
3839 break;
3840 case XDP_TX:
3841 ret = mvpp2_xdp_xmit_back(port, xdp);
3842 if (ret != MVPP2_XDP_TX) {
3843 page = virt_to_head_page(xdp->data);
3844 page_pool_put_page(pp, page, sync, true);
3845 }
3846 break;
3847 default:
3848 bpf_warn_invalid_xdp_action(act);
3849 fallthrough;
3850 case XDP_ABORTED:
3851 trace_xdp_exception(port->dev, prog, act);
3852 fallthrough;
3853 case XDP_DROP:
3854 page = virt_to_head_page(xdp->data);
3855 page_pool_put_page(pp, page, sync, true);
3856 ret = MVPP2_XDP_DROPPED;
3857 stats->xdp_drop++;
3858 break;
3859 }
3860
3861 return ret;
3862 }
3863
mvpp2_buff_hdr_pool_put(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc,int pool,u32 rx_status)3864 static void mvpp2_buff_hdr_pool_put(struct mvpp2_port *port, struct mvpp2_rx_desc *rx_desc,
3865 int pool, u32 rx_status)
3866 {
3867 phys_addr_t phys_addr, phys_addr_next;
3868 dma_addr_t dma_addr, dma_addr_next;
3869 struct mvpp2_buff_hdr *buff_hdr;
3870
3871 phys_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
3872 dma_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
3873
3874 do {
3875 buff_hdr = (struct mvpp2_buff_hdr *)phys_to_virt(phys_addr);
3876
3877 phys_addr_next = le32_to_cpu(buff_hdr->next_phys_addr);
3878 dma_addr_next = le32_to_cpu(buff_hdr->next_dma_addr);
3879
3880 if (port->priv->hw_version >= MVPP22) {
3881 phys_addr_next |= ((u64)buff_hdr->next_phys_addr_high << 32);
3882 dma_addr_next |= ((u64)buff_hdr->next_dma_addr_high << 32);
3883 }
3884
3885 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3886
3887 phys_addr = phys_addr_next;
3888 dma_addr = dma_addr_next;
3889
3890 } while (!MVPP2_B_HDR_INFO_IS_LAST(le16_to_cpu(buff_hdr->info)));
3891 }
3892
3893 /* Main rx processing */
mvpp2_rx(struct mvpp2_port * port,struct napi_struct * napi,int rx_todo,struct mvpp2_rx_queue * rxq)3894 static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
3895 int rx_todo, struct mvpp2_rx_queue *rxq)
3896 {
3897 struct net_device *dev = port->dev;
3898 struct mvpp2_pcpu_stats ps = {};
3899 enum dma_data_direction dma_dir;
3900 struct bpf_prog *xdp_prog;
3901 struct xdp_buff xdp;
3902 int rx_received;
3903 int rx_done = 0;
3904 u32 xdp_ret = 0;
3905
3906 xdp_prog = READ_ONCE(port->xdp_prog);
3907
3908 /* Get number of received packets and clamp the to-do */
3909 rx_received = mvpp2_rxq_received(port, rxq->id);
3910 if (rx_todo > rx_received)
3911 rx_todo = rx_received;
3912
3913 while (rx_done < rx_todo) {
3914 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3915 struct mvpp2_bm_pool *bm_pool;
3916 struct page_pool *pp = NULL;
3917 struct sk_buff *skb;
3918 unsigned int frag_size;
3919 dma_addr_t dma_addr;
3920 phys_addr_t phys_addr;
3921 u32 rx_status, timestamp;
3922 int pool, rx_bytes, err, ret;
3923 struct page *page;
3924 void *data;
3925
3926 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
3927 data = (void *)phys_to_virt(phys_addr);
3928 page = virt_to_page(data);
3929 prefetch(page);
3930
3931 rx_done++;
3932 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
3933 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
3934 rx_bytes -= MVPP2_MH_SIZE;
3935 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
3936
3937 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
3938 MVPP2_RXD_BM_POOL_ID_OFFS;
3939 bm_pool = &port->priv->bm_pools[pool];
3940
3941 if (port->priv->percpu_pools) {
3942 pp = port->priv->page_pool[pool];
3943 dma_dir = page_pool_get_dma_dir(pp);
3944 } else {
3945 dma_dir = DMA_FROM_DEVICE;
3946 }
3947
3948 dma_sync_single_for_cpu(dev->dev.parent, dma_addr,
3949 rx_bytes + MVPP2_MH_SIZE,
3950 dma_dir);
3951
3952 /* Buffer header not supported */
3953 if (rx_status & MVPP2_RXD_BUF_HDR)
3954 goto err_drop_frame;
3955
3956 /* In case of an error, release the requested buffer pointer
3957 * to the Buffer Manager. This request process is controlled
3958 * by the hardware, and the information about the buffer is
3959 * comprised by the RX descriptor.
3960 */
3961 if (rx_status & MVPP2_RXD_ERR_SUMMARY)
3962 goto err_drop_frame;
3963
3964 /* Prefetch header */
3965 prefetch(data + MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
3966
3967 if (bm_pool->frag_size > PAGE_SIZE)
3968 frag_size = 0;
3969 else
3970 frag_size = bm_pool->frag_size;
3971
3972 if (xdp_prog) {
3973 struct xdp_rxq_info *xdp_rxq;
3974
3975 if (bm_pool->pkt_size == MVPP2_BM_SHORT_PKT_SIZE)
3976 xdp_rxq = &rxq->xdp_rxq_short;
3977 else
3978 xdp_rxq = &rxq->xdp_rxq_long;
3979
3980 xdp_init_buff(&xdp, PAGE_SIZE, xdp_rxq);
3981 xdp_prepare_buff(&xdp, data,
3982 MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM,
3983 rx_bytes, false);
3984
3985 ret = mvpp2_run_xdp(port, xdp_prog, &xdp, pp, &ps);
3986
3987 if (ret) {
3988 xdp_ret |= ret;
3989 err = mvpp2_rx_refill(port, bm_pool, pp, pool);
3990 if (err) {
3991 netdev_err(port->dev, "failed to refill BM pools\n");
3992 goto err_drop_frame;
3993 }
3994
3995 ps.rx_packets++;
3996 ps.rx_bytes += rx_bytes;
3997 continue;
3998 }
3999 }
4000
4001 skb = build_skb(data, frag_size);
4002 if (!skb) {
4003 netdev_warn(port->dev, "skb build failed\n");
4004 goto err_drop_frame;
4005 }
4006
4007 /* If we have RX hardware timestamping enabled, grab the
4008 * timestamp from the queue and convert.
4009 */
4010 if (mvpp22_rx_hwtstamping(port)) {
4011 timestamp = le32_to_cpu(rx_desc->pp22.timestamp);
4012 mvpp22_tai_tstamp(port->priv->tai, timestamp,
4013 skb_hwtstamps(skb));
4014 }
4015
4016 err = mvpp2_rx_refill(port, bm_pool, pp, pool);
4017 if (err) {
4018 netdev_err(port->dev, "failed to refill BM pools\n");
4019 dev_kfree_skb_any(skb);
4020 goto err_drop_frame;
4021 }
4022
4023 if (pp)
4024 skb_mark_for_recycle(skb);
4025 else
4026 dma_unmap_single_attrs(dev->dev.parent, dma_addr,
4027 bm_pool->buf_size, DMA_FROM_DEVICE,
4028 DMA_ATTR_SKIP_CPU_SYNC);
4029
4030 ps.rx_packets++;
4031 ps.rx_bytes += rx_bytes;
4032
4033 skb_reserve(skb, MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
4034 skb_put(skb, rx_bytes);
4035 skb->ip_summed = mvpp2_rx_csum(port, rx_status);
4036 skb->protocol = eth_type_trans(skb, dev);
4037
4038 napi_gro_receive(napi, skb);
4039 continue;
4040
4041 err_drop_frame:
4042 dev->stats.rx_errors++;
4043 mvpp2_rx_error(port, rx_desc);
4044 /* Return the buffer to the pool */
4045 if (rx_status & MVPP2_RXD_BUF_HDR)
4046 mvpp2_buff_hdr_pool_put(port, rx_desc, pool, rx_status);
4047 else
4048 mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
4049 }
4050
4051 if (xdp_ret & MVPP2_XDP_REDIR)
4052 xdp_do_flush_map();
4053
4054 if (ps.rx_packets) {
4055 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
4056
4057 u64_stats_update_begin(&stats->syncp);
4058 stats->rx_packets += ps.rx_packets;
4059 stats->rx_bytes += ps.rx_bytes;
4060 /* xdp */
4061 stats->xdp_redirect += ps.xdp_redirect;
4062 stats->xdp_pass += ps.xdp_pass;
4063 stats->xdp_drop += ps.xdp_drop;
4064 u64_stats_update_end(&stats->syncp);
4065 }
4066
4067 /* Update Rx queue management counters */
4068 wmb();
4069 mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
4070
4071 return rx_todo;
4072 }
4073
4074 static inline void
tx_desc_unmap_put(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_tx_desc * desc)4075 tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
4076 struct mvpp2_tx_desc *desc)
4077 {
4078 unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4079 struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4080
4081 dma_addr_t buf_dma_addr =
4082 mvpp2_txdesc_dma_addr_get(port, desc);
4083 size_t buf_sz =
4084 mvpp2_txdesc_size_get(port, desc);
4085 if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
4086 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
4087 buf_sz, DMA_TO_DEVICE);
4088 mvpp2_txq_desc_put(txq);
4089 }
4090
mvpp2_txdesc_clear_ptp(struct mvpp2_port * port,struct mvpp2_tx_desc * desc)4091 static void mvpp2_txdesc_clear_ptp(struct mvpp2_port *port,
4092 struct mvpp2_tx_desc *desc)
4093 {
4094 /* We only need to clear the low bits */
4095 if (port->priv->hw_version >= MVPP22)
4096 desc->pp22.ptp_descriptor &=
4097 cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
4098 }
4099
mvpp2_tx_hw_tstamp(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,struct sk_buff * skb)4100 static bool mvpp2_tx_hw_tstamp(struct mvpp2_port *port,
4101 struct mvpp2_tx_desc *tx_desc,
4102 struct sk_buff *skb)
4103 {
4104 struct mvpp2_hwtstamp_queue *queue;
4105 unsigned int mtype, type, i;
4106 struct ptp_header *hdr;
4107 u64 ptpdesc;
4108
4109 if (port->priv->hw_version == MVPP21 ||
4110 port->tx_hwtstamp_type == HWTSTAMP_TX_OFF)
4111 return false;
4112
4113 type = ptp_classify_raw(skb);
4114 if (!type)
4115 return false;
4116
4117 hdr = ptp_parse_header(skb, type);
4118 if (!hdr)
4119 return false;
4120
4121 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4122
4123 ptpdesc = MVPP22_PTP_MACTIMESTAMPINGEN |
4124 MVPP22_PTP_ACTION_CAPTURE;
4125 queue = &port->tx_hwtstamp_queue[0];
4126
4127 switch (type & PTP_CLASS_VMASK) {
4128 case PTP_CLASS_V1:
4129 ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV1);
4130 break;
4131
4132 case PTP_CLASS_V2:
4133 ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV2);
4134 mtype = hdr->tsmt & 15;
4135 /* Direct PTP Sync messages to queue 1 */
4136 if (mtype == 0) {
4137 ptpdesc |= MVPP22_PTP_TIMESTAMPQUEUESELECT;
4138 queue = &port->tx_hwtstamp_queue[1];
4139 }
4140 break;
4141 }
4142
4143 /* Take a reference on the skb and insert into our queue */
4144 i = queue->next;
4145 queue->next = (i + 1) & 31;
4146 if (queue->skb[i])
4147 dev_kfree_skb_any(queue->skb[i]);
4148 queue->skb[i] = skb_get(skb);
4149
4150 ptpdesc |= MVPP22_PTP_TIMESTAMPENTRYID(i);
4151
4152 /*
4153 * 3:0 - PTPAction
4154 * 6:4 - PTPPacketFormat
4155 * 7 - PTP_CF_WraparoundCheckEn
4156 * 9:8 - IngressTimestampSeconds[1:0]
4157 * 10 - Reserved
4158 * 11 - MACTimestampingEn
4159 * 17:12 - PTP_TimestampQueueEntryID[5:0]
4160 * 18 - PTPTimestampQueueSelect
4161 * 19 - UDPChecksumUpdateEn
4162 * 27:20 - TimestampOffset
4163 * PTP, NTPTransmit, OWAMP/TWAMP - L3 to PTP header
4164 * NTPTs, Y.1731 - L3 to timestamp entry
4165 * 35:28 - UDP Checksum Offset
4166 *
4167 * stored in tx descriptor bits 75:64 (11:0) and 191:168 (35:12)
4168 */
4169 tx_desc->pp22.ptp_descriptor &=
4170 cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
4171 tx_desc->pp22.ptp_descriptor |=
4172 cpu_to_le32(ptpdesc & MVPP22_PTP_DESC_MASK_LOW);
4173 tx_desc->pp22.buf_dma_addr_ptp &= cpu_to_le64(~0xffffff0000000000ULL);
4174 tx_desc->pp22.buf_dma_addr_ptp |= cpu_to_le64((ptpdesc >> 12) << 40);
4175
4176 return true;
4177 }
4178
4179 /* 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)4180 static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
4181 struct mvpp2_tx_queue *aggr_txq,
4182 struct mvpp2_tx_queue *txq)
4183 {
4184 unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4185 struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4186 struct mvpp2_tx_desc *tx_desc;
4187 int i;
4188 dma_addr_t buf_dma_addr;
4189
4190 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4191 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4192 void *addr = skb_frag_address(frag);
4193
4194 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4195 mvpp2_txdesc_clear_ptp(port, tx_desc);
4196 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4197 mvpp2_txdesc_size_set(port, tx_desc, skb_frag_size(frag));
4198
4199 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
4200 skb_frag_size(frag),
4201 DMA_TO_DEVICE);
4202 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
4203 mvpp2_txq_desc_put(txq);
4204 goto cleanup;
4205 }
4206
4207 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4208
4209 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
4210 /* Last descriptor */
4211 mvpp2_txdesc_cmd_set(port, tx_desc,
4212 MVPP2_TXD_L_DESC);
4213 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4214 } else {
4215 /* Descriptor in the middle: Not First, Not Last */
4216 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
4217 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4218 }
4219 }
4220
4221 return 0;
4222 cleanup:
4223 /* Release all descriptors that were used to map fragments of
4224 * this packet, as well as the corresponding DMA mappings
4225 */
4226 for (i = i - 1; i >= 0; i--) {
4227 tx_desc = txq->descs + i;
4228 tx_desc_unmap_put(port, txq, tx_desc);
4229 }
4230
4231 return -ENOMEM;
4232 }
4233
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)4234 static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
4235 struct net_device *dev,
4236 struct mvpp2_tx_queue *txq,
4237 struct mvpp2_tx_queue *aggr_txq,
4238 struct mvpp2_txq_pcpu *txq_pcpu,
4239 int hdr_sz)
4240 {
4241 struct mvpp2_port *port = netdev_priv(dev);
4242 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4243 dma_addr_t addr;
4244
4245 mvpp2_txdesc_clear_ptp(port, tx_desc);
4246 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4247 mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
4248
4249 addr = txq_pcpu->tso_headers_dma +
4250 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
4251 mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
4252
4253 mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
4254 MVPP2_TXD_F_DESC |
4255 MVPP2_TXD_PADDING_DISABLE);
4256 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4257 }
4258
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)4259 static inline int mvpp2_tso_put_data(struct sk_buff *skb,
4260 struct net_device *dev, struct tso_t *tso,
4261 struct mvpp2_tx_queue *txq,
4262 struct mvpp2_tx_queue *aggr_txq,
4263 struct mvpp2_txq_pcpu *txq_pcpu,
4264 int sz, bool left, bool last)
4265 {
4266 struct mvpp2_port *port = netdev_priv(dev);
4267 struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4268 dma_addr_t buf_dma_addr;
4269
4270 mvpp2_txdesc_clear_ptp(port, tx_desc);
4271 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4272 mvpp2_txdesc_size_set(port, tx_desc, sz);
4273
4274 buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
4275 DMA_TO_DEVICE);
4276 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
4277 mvpp2_txq_desc_put(txq);
4278 return -ENOMEM;
4279 }
4280
4281 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4282
4283 if (!left) {
4284 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
4285 if (last) {
4286 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4287 return 0;
4288 }
4289 } else {
4290 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
4291 }
4292
4293 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4294 return 0;
4295 }
4296
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)4297 static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
4298 struct mvpp2_tx_queue *txq,
4299 struct mvpp2_tx_queue *aggr_txq,
4300 struct mvpp2_txq_pcpu *txq_pcpu)
4301 {
4302 struct mvpp2_port *port = netdev_priv(dev);
4303 int hdr_sz, i, len, descs = 0;
4304 struct tso_t tso;
4305
4306 /* Check number of available descriptors */
4307 if (mvpp2_aggr_desc_num_check(port, aggr_txq, tso_count_descs(skb)) ||
4308 mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu,
4309 tso_count_descs(skb)))
4310 return 0;
4311
4312 hdr_sz = tso_start(skb, &tso);
4313
4314 len = skb->len - hdr_sz;
4315 while (len > 0) {
4316 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
4317 char *hdr = txq_pcpu->tso_headers +
4318 txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
4319
4320 len -= left;
4321 descs++;
4322
4323 tso_build_hdr(skb, hdr, &tso, left, len == 0);
4324 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
4325
4326 while (left > 0) {
4327 int sz = min_t(int, tso.size, left);
4328 left -= sz;
4329 descs++;
4330
4331 if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
4332 txq_pcpu, sz, left, len == 0))
4333 goto release;
4334 tso_build_data(skb, &tso, sz);
4335 }
4336 }
4337
4338 return descs;
4339
4340 release:
4341 for (i = descs - 1; i >= 0; i--) {
4342 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
4343 tx_desc_unmap_put(port, txq, tx_desc);
4344 }
4345 return 0;
4346 }
4347
4348 /* Main tx processing */
mvpp2_tx(struct sk_buff * skb,struct net_device * dev)4349 static netdev_tx_t mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
4350 {
4351 struct mvpp2_port *port = netdev_priv(dev);
4352 struct mvpp2_tx_queue *txq, *aggr_txq;
4353 struct mvpp2_txq_pcpu *txq_pcpu;
4354 struct mvpp2_tx_desc *tx_desc;
4355 dma_addr_t buf_dma_addr;
4356 unsigned long flags = 0;
4357 unsigned int thread;
4358 int frags = 0;
4359 u16 txq_id;
4360 u32 tx_cmd;
4361
4362 thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4363
4364 txq_id = skb_get_queue_mapping(skb);
4365 txq = port->txqs[txq_id];
4366 txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
4367 aggr_txq = &port->priv->aggr_txqs[thread];
4368
4369 if (test_bit(thread, &port->priv->lock_map))
4370 spin_lock_irqsave(&port->tx_lock[thread], flags);
4371
4372 if (skb_is_gso(skb)) {
4373 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
4374 goto out;
4375 }
4376 frags = skb_shinfo(skb)->nr_frags + 1;
4377
4378 /* Check number of available descriptors */
4379 if (mvpp2_aggr_desc_num_check(port, aggr_txq, frags) ||
4380 mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, frags)) {
4381 frags = 0;
4382 goto out;
4383 }
4384
4385 /* Get a descriptor for the first part of the packet */
4386 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4387 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
4388 !mvpp2_tx_hw_tstamp(port, tx_desc, skb))
4389 mvpp2_txdesc_clear_ptp(port, tx_desc);
4390 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4391 mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
4392
4393 buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
4394 skb_headlen(skb), DMA_TO_DEVICE);
4395 if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
4396 mvpp2_txq_desc_put(txq);
4397 frags = 0;
4398 goto out;
4399 }
4400
4401 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4402
4403 tx_cmd = mvpp2_skb_tx_csum(port, skb);
4404
4405 if (frags == 1) {
4406 /* First and Last descriptor */
4407 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
4408 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
4409 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4410 } else {
4411 /* First but not Last */
4412 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
4413 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
4414 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4415
4416 /* Continue with other skb fragments */
4417 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
4418 tx_desc_unmap_put(port, txq, tx_desc);
4419 frags = 0;
4420 }
4421 }
4422
4423 out:
4424 if (frags > 0) {
4425 struct mvpp2_pcpu_stats *stats = per_cpu_ptr(port->stats, thread);
4426 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
4427
4428 txq_pcpu->reserved_num -= frags;
4429 txq_pcpu->count += frags;
4430 aggr_txq->count += frags;
4431
4432 /* Enable transmit */
4433 wmb();
4434 mvpp2_aggr_txq_pend_desc_add(port, frags);
4435
4436 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
4437 netif_tx_stop_queue(nq);
4438
4439 u64_stats_update_begin(&stats->syncp);
4440 stats->tx_packets++;
4441 stats->tx_bytes += skb->len;
4442 u64_stats_update_end(&stats->syncp);
4443 } else {
4444 dev->stats.tx_dropped++;
4445 dev_kfree_skb_any(skb);
4446 }
4447
4448 /* Finalize TX processing */
4449 if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
4450 mvpp2_txq_done(port, txq, txq_pcpu);
4451
4452 /* Set the timer in case not all frags were processed */
4453 if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
4454 txq_pcpu->count > 0) {
4455 struct mvpp2_port_pcpu *port_pcpu = per_cpu_ptr(port->pcpu, thread);
4456
4457 if (!port_pcpu->timer_scheduled) {
4458 port_pcpu->timer_scheduled = true;
4459 hrtimer_start(&port_pcpu->tx_done_timer,
4460 MVPP2_TXDONE_HRTIMER_PERIOD_NS,
4461 HRTIMER_MODE_REL_PINNED_SOFT);
4462 }
4463 }
4464
4465 if (test_bit(thread, &port->priv->lock_map))
4466 spin_unlock_irqrestore(&port->tx_lock[thread], flags);
4467
4468 return NETDEV_TX_OK;
4469 }
4470
mvpp2_cause_error(struct net_device * dev,int cause)4471 static inline void mvpp2_cause_error(struct net_device *dev, int cause)
4472 {
4473 if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
4474 netdev_err(dev, "FCS error\n");
4475 if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
4476 netdev_err(dev, "rx fifo overrun error\n");
4477 if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
4478 netdev_err(dev, "tx fifo underrun error\n");
4479 }
4480
mvpp2_poll(struct napi_struct * napi,int budget)4481 static int mvpp2_poll(struct napi_struct *napi, int budget)
4482 {
4483 u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
4484 int rx_done = 0;
4485 struct mvpp2_port *port = netdev_priv(napi->dev);
4486 struct mvpp2_queue_vector *qv;
4487 unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4488
4489 qv = container_of(napi, struct mvpp2_queue_vector, napi);
4490
4491 /* Rx/Tx cause register
4492 *
4493 * Bits 0-15: each bit indicates received packets on the Rx queue
4494 * (bit 0 is for Rx queue 0).
4495 *
4496 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
4497 * (bit 16 is for Tx queue 0).
4498 *
4499 * Each CPU has its own Rx/Tx cause register
4500 */
4501 cause_rx_tx = mvpp2_thread_read_relaxed(port->priv, qv->sw_thread_id,
4502 MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
4503
4504 cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
4505 if (cause_misc) {
4506 mvpp2_cause_error(port->dev, cause_misc);
4507
4508 /* Clear the cause register */
4509 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
4510 mvpp2_thread_write(port->priv, thread,
4511 MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
4512 cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
4513 }
4514
4515 if (port->has_tx_irqs) {
4516 cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
4517 if (cause_tx) {
4518 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
4519 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
4520 }
4521 }
4522
4523 /* Process RX packets */
4524 cause_rx = cause_rx_tx &
4525 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
4526 cause_rx <<= qv->first_rxq;
4527 cause_rx |= qv->pending_cause_rx;
4528 while (cause_rx && budget > 0) {
4529 int count;
4530 struct mvpp2_rx_queue *rxq;
4531
4532 rxq = mvpp2_get_rx_queue(port, cause_rx);
4533 if (!rxq)
4534 break;
4535
4536 count = mvpp2_rx(port, napi, budget, rxq);
4537 rx_done += count;
4538 budget -= count;
4539 if (budget > 0) {
4540 /* Clear the bit associated to this Rx queue
4541 * so that next iteration will continue from
4542 * the next Rx queue.
4543 */
4544 cause_rx &= ~(1 << rxq->logic_rxq);
4545 }
4546 }
4547
4548 if (budget > 0) {
4549 cause_rx = 0;
4550 napi_complete_done(napi, rx_done);
4551
4552 mvpp2_qvec_interrupt_enable(qv);
4553 }
4554 qv->pending_cause_rx = cause_rx;
4555 return rx_done;
4556 }
4557
mvpp22_mode_reconfigure(struct mvpp2_port * port,phy_interface_t interface)4558 static void mvpp22_mode_reconfigure(struct mvpp2_port *port,
4559 phy_interface_t interface)
4560 {
4561 u32 ctrl3;
4562
4563 /* Set the GMAC & XLG MAC in reset */
4564 mvpp2_mac_reset_assert(port);
4565
4566 /* Set the MPCS and XPCS in reset */
4567 mvpp22_pcs_reset_assert(port);
4568
4569 /* comphy reconfiguration */
4570 mvpp22_comphy_init(port, interface);
4571
4572 /* gop reconfiguration */
4573 mvpp22_gop_init(port, interface);
4574
4575 mvpp22_pcs_reset_deassert(port, interface);
4576
4577 if (mvpp2_port_supports_xlg(port)) {
4578 ctrl3 = readl(port->base + MVPP22_XLG_CTRL3_REG);
4579 ctrl3 &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
4580
4581 if (mvpp2_is_xlg(interface))
4582 ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4583 else
4584 ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4585
4586 writel(ctrl3, port->base + MVPP22_XLG_CTRL3_REG);
4587 }
4588
4589 if (mvpp2_port_supports_xlg(port) && mvpp2_is_xlg(interface))
4590 mvpp2_xlg_max_rx_size_set(port);
4591 else
4592 mvpp2_gmac_max_rx_size_set(port);
4593 }
4594
4595 /* Set hw internals when starting port */
mvpp2_start_dev(struct mvpp2_port * port)4596 static void mvpp2_start_dev(struct mvpp2_port *port)
4597 {
4598 int i;
4599
4600 mvpp2_txp_max_tx_size_set(port);
4601
4602 for (i = 0; i < port->nqvecs; i++)
4603 napi_enable(&port->qvecs[i].napi);
4604
4605 /* Enable interrupts on all threads */
4606 mvpp2_interrupts_enable(port);
4607
4608 if (port->priv->hw_version >= MVPP22)
4609 mvpp22_mode_reconfigure(port, port->phy_interface);
4610
4611 if (port->phylink) {
4612 phylink_start(port->phylink);
4613 } else {
4614 mvpp2_acpi_start(port);
4615 }
4616
4617 netif_tx_start_all_queues(port->dev);
4618
4619 clear_bit(0, &port->state);
4620 }
4621
4622 /* Set hw internals when stopping port */
mvpp2_stop_dev(struct mvpp2_port * port)4623 static void mvpp2_stop_dev(struct mvpp2_port *port)
4624 {
4625 int i;
4626
4627 set_bit(0, &port->state);
4628
4629 /* Disable interrupts on all threads */
4630 mvpp2_interrupts_disable(port);
4631
4632 for (i = 0; i < port->nqvecs; i++)
4633 napi_disable(&port->qvecs[i].napi);
4634
4635 if (port->phylink)
4636 phylink_stop(port->phylink);
4637 phy_power_off(port->comphy);
4638 }
4639
mvpp2_check_ringparam_valid(struct net_device * dev,struct ethtool_ringparam * ring)4640 static int mvpp2_check_ringparam_valid(struct net_device *dev,
4641 struct ethtool_ringparam *ring)
4642 {
4643 u16 new_rx_pending = ring->rx_pending;
4644 u16 new_tx_pending = ring->tx_pending;
4645
4646 if (ring->rx_pending == 0 || ring->tx_pending == 0)
4647 return -EINVAL;
4648
4649 if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
4650 new_rx_pending = MVPP2_MAX_RXD_MAX;
4651 else if (ring->rx_pending < MSS_THRESHOLD_START)
4652 new_rx_pending = MSS_THRESHOLD_START;
4653 else if (!IS_ALIGNED(ring->rx_pending, 16))
4654 new_rx_pending = ALIGN(ring->rx_pending, 16);
4655
4656 if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
4657 new_tx_pending = MVPP2_MAX_TXD_MAX;
4658 else if (!IS_ALIGNED(ring->tx_pending, 32))
4659 new_tx_pending = ALIGN(ring->tx_pending, 32);
4660
4661 /* The Tx ring size cannot be smaller than the minimum number of
4662 * descriptors needed for TSO.
4663 */
4664 if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
4665 new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
4666
4667 if (ring->rx_pending != new_rx_pending) {
4668 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
4669 ring->rx_pending, new_rx_pending);
4670 ring->rx_pending = new_rx_pending;
4671 }
4672
4673 if (ring->tx_pending != new_tx_pending) {
4674 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
4675 ring->tx_pending, new_tx_pending);
4676 ring->tx_pending = new_tx_pending;
4677 }
4678
4679 return 0;
4680 }
4681
mvpp21_get_mac_address(struct mvpp2_port * port,unsigned char * addr)4682 static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
4683 {
4684 u32 mac_addr_l, mac_addr_m, mac_addr_h;
4685
4686 mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4687 mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
4688 mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
4689 addr[0] = (mac_addr_h >> 24) & 0xFF;
4690 addr[1] = (mac_addr_h >> 16) & 0xFF;
4691 addr[2] = (mac_addr_h >> 8) & 0xFF;
4692 addr[3] = mac_addr_h & 0xFF;
4693 addr[4] = mac_addr_m & 0xFF;
4694 addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
4695 }
4696
mvpp2_irqs_init(struct mvpp2_port * port)4697 static int mvpp2_irqs_init(struct mvpp2_port *port)
4698 {
4699 int err, i;
4700
4701 for (i = 0; i < port->nqvecs; i++) {
4702 struct mvpp2_queue_vector *qv = port->qvecs + i;
4703
4704 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
4705 qv->mask = kzalloc(cpumask_size(), GFP_KERNEL);
4706 if (!qv->mask) {
4707 err = -ENOMEM;
4708 goto err;
4709 }
4710
4711 irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
4712 }
4713
4714 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
4715 if (err)
4716 goto err;
4717
4718 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
4719 unsigned int cpu;
4720
4721 for_each_present_cpu(cpu) {
4722 if (mvpp2_cpu_to_thread(port->priv, cpu) ==
4723 qv->sw_thread_id)
4724 cpumask_set_cpu(cpu, qv->mask);
4725 }
4726
4727 irq_set_affinity_hint(qv->irq, qv->mask);
4728 }
4729 }
4730
4731 return 0;
4732 err:
4733 for (i = 0; i < port->nqvecs; i++) {
4734 struct mvpp2_queue_vector *qv = port->qvecs + i;
4735
4736 irq_set_affinity_hint(qv->irq, NULL);
4737 kfree(qv->mask);
4738 qv->mask = NULL;
4739 free_irq(qv->irq, qv);
4740 }
4741
4742 return err;
4743 }
4744
mvpp2_irqs_deinit(struct mvpp2_port * port)4745 static void mvpp2_irqs_deinit(struct mvpp2_port *port)
4746 {
4747 int i;
4748
4749 for (i = 0; i < port->nqvecs; i++) {
4750 struct mvpp2_queue_vector *qv = port->qvecs + i;
4751
4752 irq_set_affinity_hint(qv->irq, NULL);
4753 kfree(qv->mask);
4754 qv->mask = NULL;
4755 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
4756 free_irq(qv->irq, qv);
4757 }
4758 }
4759
mvpp22_rss_is_supported(struct mvpp2_port * port)4760 static bool mvpp22_rss_is_supported(struct mvpp2_port *port)
4761 {
4762 return (queue_mode == MVPP2_QDIST_MULTI_MODE) &&
4763 !(port->flags & MVPP2_F_LOOPBACK);
4764 }
4765
mvpp2_open(struct net_device * dev)4766 static int mvpp2_open(struct net_device *dev)
4767 {
4768 struct mvpp2_port *port = netdev_priv(dev);
4769 struct mvpp2 *priv = port->priv;
4770 unsigned char mac_bcast[ETH_ALEN] = {
4771 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4772 bool valid = false;
4773 int err;
4774
4775 err = mvpp2_prs_mac_da_accept(port, mac_bcast, true);
4776 if (err) {
4777 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
4778 return err;
4779 }
4780 err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, true);
4781 if (err) {
4782 netdev_err(dev, "mvpp2_prs_mac_da_accept own addr failed\n");
4783 return err;
4784 }
4785 err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
4786 if (err) {
4787 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
4788 return err;
4789 }
4790 err = mvpp2_prs_def_flow(port);
4791 if (err) {
4792 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
4793 return err;
4794 }
4795
4796 /* Allocate the Rx/Tx queues */
4797 err = mvpp2_setup_rxqs(port);
4798 if (err) {
4799 netdev_err(port->dev, "cannot allocate Rx queues\n");
4800 return err;
4801 }
4802
4803 err = mvpp2_setup_txqs(port);
4804 if (err) {
4805 netdev_err(port->dev, "cannot allocate Tx queues\n");
4806 goto err_cleanup_rxqs;
4807 }
4808
4809 err = mvpp2_irqs_init(port);
4810 if (err) {
4811 netdev_err(port->dev, "cannot init IRQs\n");
4812 goto err_cleanup_txqs;
4813 }
4814
4815 if (port->phylink) {
4816 err = phylink_fwnode_phy_connect(port->phylink, port->fwnode, 0);
4817 if (err) {
4818 netdev_err(port->dev, "could not attach PHY (%d)\n",
4819 err);
4820 goto err_free_irq;
4821 }
4822
4823 valid = true;
4824 }
4825
4826 if (priv->hw_version >= MVPP22 && port->port_irq) {
4827 err = request_irq(port->port_irq, mvpp2_port_isr, 0,
4828 dev->name, port);
4829 if (err) {
4830 netdev_err(port->dev,
4831 "cannot request port link/ptp IRQ %d\n",
4832 port->port_irq);
4833 goto err_free_irq;
4834 }
4835
4836 mvpp22_gop_setup_irq(port);
4837
4838 /* In default link is down */
4839 netif_carrier_off(port->dev);
4840
4841 valid = true;
4842 } else {
4843 port->port_irq = 0;
4844 }
4845
4846 if (!valid) {
4847 netdev_err(port->dev,
4848 "invalid configuration: no dt or link IRQ");
4849 err = -ENOENT;
4850 goto err_free_irq;
4851 }
4852
4853 /* Unmask interrupts on all CPUs */
4854 on_each_cpu(mvpp2_interrupts_unmask, port, 1);
4855 mvpp2_shared_interrupt_mask_unmask(port, false);
4856
4857 mvpp2_start_dev(port);
4858
4859 /* Start hardware statistics gathering */
4860 queue_delayed_work(priv->stats_queue, &port->stats_work,
4861 MVPP2_MIB_COUNTERS_STATS_DELAY);
4862
4863 return 0;
4864
4865 err_free_irq:
4866 mvpp2_irqs_deinit(port);
4867 err_cleanup_txqs:
4868 mvpp2_cleanup_txqs(port);
4869 err_cleanup_rxqs:
4870 mvpp2_cleanup_rxqs(port);
4871 return err;
4872 }
4873
mvpp2_stop(struct net_device * dev)4874 static int mvpp2_stop(struct net_device *dev)
4875 {
4876 struct mvpp2_port *port = netdev_priv(dev);
4877 struct mvpp2_port_pcpu *port_pcpu;
4878 unsigned int thread;
4879
4880 mvpp2_stop_dev(port);
4881
4882 /* Mask interrupts on all threads */
4883 on_each_cpu(mvpp2_interrupts_mask, port, 1);
4884 mvpp2_shared_interrupt_mask_unmask(port, true);
4885
4886 if (port->phylink)
4887 phylink_disconnect_phy(port->phylink);
4888 if (port->port_irq)
4889 free_irq(port->port_irq, port);
4890
4891 mvpp2_irqs_deinit(port);
4892 if (!port->has_tx_irqs) {
4893 for (thread = 0; thread < port->priv->nthreads; thread++) {
4894 port_pcpu = per_cpu_ptr(port->pcpu, thread);
4895
4896 hrtimer_cancel(&port_pcpu->tx_done_timer);
4897 port_pcpu->timer_scheduled = false;
4898 }
4899 }
4900 mvpp2_cleanup_rxqs(port);
4901 mvpp2_cleanup_txqs(port);
4902
4903 cancel_delayed_work_sync(&port->stats_work);
4904
4905 mvpp2_mac_reset_assert(port);
4906 mvpp22_pcs_reset_assert(port);
4907
4908 return 0;
4909 }
4910
mvpp2_prs_mac_da_accept_list(struct mvpp2_port * port,struct netdev_hw_addr_list * list)4911 static int mvpp2_prs_mac_da_accept_list(struct mvpp2_port *port,
4912 struct netdev_hw_addr_list *list)
4913 {
4914 struct netdev_hw_addr *ha;
4915 int ret;
4916
4917 netdev_hw_addr_list_for_each(ha, list) {
4918 ret = mvpp2_prs_mac_da_accept(port, ha->addr, true);
4919 if (ret)
4920 return ret;
4921 }
4922
4923 return 0;
4924 }
4925
mvpp2_set_rx_promisc(struct mvpp2_port * port,bool enable)4926 static void mvpp2_set_rx_promisc(struct mvpp2_port *port, bool enable)
4927 {
4928 if (!enable && (port->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
4929 mvpp2_prs_vid_enable_filtering(port);
4930 else
4931 mvpp2_prs_vid_disable_filtering(port);
4932
4933 mvpp2_prs_mac_promisc_set(port->priv, port->id,
4934 MVPP2_PRS_L2_UNI_CAST, enable);
4935
4936 mvpp2_prs_mac_promisc_set(port->priv, port->id,
4937 MVPP2_PRS_L2_MULTI_CAST, enable);
4938 }
4939
mvpp2_set_rx_mode(struct net_device * dev)4940 static void mvpp2_set_rx_mode(struct net_device *dev)
4941 {
4942 struct mvpp2_port *port = netdev_priv(dev);
4943
4944 /* Clear the whole UC and MC list */
4945 mvpp2_prs_mac_del_all(port);
4946
4947 if (dev->flags & IFF_PROMISC) {
4948 mvpp2_set_rx_promisc(port, true);
4949 return;
4950 }
4951
4952 mvpp2_set_rx_promisc(port, false);
4953
4954 if (netdev_uc_count(dev) > MVPP2_PRS_MAC_UC_FILT_MAX ||
4955 mvpp2_prs_mac_da_accept_list(port, &dev->uc))
4956 mvpp2_prs_mac_promisc_set(port->priv, port->id,
4957 MVPP2_PRS_L2_UNI_CAST, true);
4958
4959 if (dev->flags & IFF_ALLMULTI) {
4960 mvpp2_prs_mac_promisc_set(port->priv, port->id,
4961 MVPP2_PRS_L2_MULTI_CAST, true);
4962 return;
4963 }
4964
4965 if (netdev_mc_count(dev) > MVPP2_PRS_MAC_MC_FILT_MAX ||
4966 mvpp2_prs_mac_da_accept_list(port, &dev->mc))
4967 mvpp2_prs_mac_promisc_set(port->priv, port->id,
4968 MVPP2_PRS_L2_MULTI_CAST, true);
4969 }
4970
mvpp2_set_mac_address(struct net_device * dev,void * p)4971 static int mvpp2_set_mac_address(struct net_device *dev, void *p)
4972 {
4973 const struct sockaddr *addr = p;
4974 int err;
4975
4976 if (!is_valid_ether_addr(addr->sa_data))
4977 return -EADDRNOTAVAIL;
4978
4979 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
4980 if (err) {
4981 /* Reconfigure parser accept the original MAC address */
4982 mvpp2_prs_update_mac_da(dev, dev->dev_addr);
4983 netdev_err(dev, "failed to change MAC address\n");
4984 }
4985 return err;
4986 }
4987
4988 /* Shut down all the ports, reconfigure the pools as percpu or shared,
4989 * then bring up again all ports.
4990 */
mvpp2_bm_switch_buffers(struct mvpp2 * priv,bool percpu)4991 static int mvpp2_bm_switch_buffers(struct mvpp2 *priv, bool percpu)
4992 {
4993 bool change_percpu = (percpu != priv->percpu_pools);
4994 int numbufs = MVPP2_BM_POOLS_NUM, i;
4995 struct mvpp2_port *port = NULL;
4996 bool status[MVPP2_MAX_PORTS];
4997
4998 for (i = 0; i < priv->port_count; i++) {
4999 port = priv->port_list[i];
5000 status[i] = netif_running(port->dev);
5001 if (status[i])
5002 mvpp2_stop(port->dev);
5003 }
5004
5005 /* nrxqs is the same for all ports */
5006 if (priv->percpu_pools)
5007 numbufs = port->nrxqs * 2;
5008
5009 if (change_percpu)
5010 mvpp2_bm_pool_update_priv_fc(priv, false);
5011
5012 for (i = 0; i < numbufs; i++)
5013 mvpp2_bm_pool_destroy(port->dev->dev.parent, priv, &priv->bm_pools[i]);
5014
5015 devm_kfree(port->dev->dev.parent, priv->bm_pools);
5016 priv->percpu_pools = percpu;
5017 mvpp2_bm_init(port->dev->dev.parent, priv);
5018
5019 for (i = 0; i < priv->port_count; i++) {
5020 port = priv->port_list[i];
5021 mvpp2_swf_bm_pool_init(port);
5022 if (status[i])
5023 mvpp2_open(port->dev);
5024 }
5025
5026 if (change_percpu)
5027 mvpp2_bm_pool_update_priv_fc(priv, true);
5028
5029 return 0;
5030 }
5031
mvpp2_change_mtu(struct net_device * dev,int mtu)5032 static int mvpp2_change_mtu(struct net_device *dev, int mtu)
5033 {
5034 struct mvpp2_port *port = netdev_priv(dev);
5035 bool running = netif_running(dev);
5036 struct mvpp2 *priv = port->priv;
5037 int err;
5038
5039 if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
5040 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
5041 ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
5042 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
5043 }
5044
5045 if (port->xdp_prog && mtu > MVPP2_MAX_RX_BUF_SIZE) {
5046 netdev_err(dev, "Illegal MTU value %d (> %d) for XDP mode\n",
5047 mtu, (int)MVPP2_MAX_RX_BUF_SIZE);
5048 return -EINVAL;
5049 }
5050
5051 if (MVPP2_RX_PKT_SIZE(mtu) > MVPP2_BM_LONG_PKT_SIZE) {
5052 if (priv->percpu_pools) {
5053 netdev_warn(dev, "mtu %d too high, switching to shared buffers", mtu);
5054 mvpp2_bm_switch_buffers(priv, false);
5055 }
5056 } else {
5057 bool jumbo = false;
5058 int i;
5059
5060 for (i = 0; i < priv->port_count; i++)
5061 if (priv->port_list[i] != port &&
5062 MVPP2_RX_PKT_SIZE(priv->port_list[i]->dev->mtu) >
5063 MVPP2_BM_LONG_PKT_SIZE) {
5064 jumbo = true;
5065 break;
5066 }
5067
5068 /* No port is using jumbo frames */
5069 if (!jumbo) {
5070 dev_info(port->dev->dev.parent,
5071 "all ports have a low MTU, switching to per-cpu buffers");
5072 mvpp2_bm_switch_buffers(priv, true);
5073 }
5074 }
5075
5076 if (running)
5077 mvpp2_stop_dev(port);
5078
5079 err = mvpp2_bm_update_mtu(dev, mtu);
5080 if (err) {
5081 netdev_err(dev, "failed to change MTU\n");
5082 /* Reconfigure BM to the original MTU */
5083 mvpp2_bm_update_mtu(dev, dev->mtu);
5084 } else {
5085 port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
5086 }
5087
5088 if (running) {
5089 mvpp2_start_dev(port);
5090 mvpp2_egress_enable(port);
5091 mvpp2_ingress_enable(port);
5092 }
5093
5094 return err;
5095 }
5096
mvpp2_check_pagepool_dma(struct mvpp2_port * port)5097 static int mvpp2_check_pagepool_dma(struct mvpp2_port *port)
5098 {
5099 enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
5100 struct mvpp2 *priv = port->priv;
5101 int err = -1, i;
5102
5103 if (!priv->percpu_pools)
5104 return err;
5105
5106 if (!priv->page_pool[0])
5107 return -ENOMEM;
5108
5109 for (i = 0; i < priv->port_count; i++) {
5110 port = priv->port_list[i];
5111 if (port->xdp_prog) {
5112 dma_dir = DMA_BIDIRECTIONAL;
5113 break;
5114 }
5115 }
5116
5117 /* All pools are equal in terms of DMA direction */
5118 if (priv->page_pool[0]->p.dma_dir != dma_dir)
5119 err = mvpp2_bm_switch_buffers(priv, true);
5120
5121 return err;
5122 }
5123
5124 static void
mvpp2_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)5125 mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5126 {
5127 struct mvpp2_port *port = netdev_priv(dev);
5128 unsigned int start;
5129 unsigned int cpu;
5130
5131 for_each_possible_cpu(cpu) {
5132 struct mvpp2_pcpu_stats *cpu_stats;
5133 u64 rx_packets;
5134 u64 rx_bytes;
5135 u64 tx_packets;
5136 u64 tx_bytes;
5137
5138 cpu_stats = per_cpu_ptr(port->stats, cpu);
5139 do {
5140 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
5141 rx_packets = cpu_stats->rx_packets;
5142 rx_bytes = cpu_stats->rx_bytes;
5143 tx_packets = cpu_stats->tx_packets;
5144 tx_bytes = cpu_stats->tx_bytes;
5145 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
5146
5147 stats->rx_packets += rx_packets;
5148 stats->rx_bytes += rx_bytes;
5149 stats->tx_packets += tx_packets;
5150 stats->tx_bytes += tx_bytes;
5151 }
5152
5153 stats->rx_errors = dev->stats.rx_errors;
5154 stats->rx_dropped = dev->stats.rx_dropped;
5155 stats->tx_dropped = dev->stats.tx_dropped;
5156 }
5157
mvpp2_set_ts_config(struct mvpp2_port * port,struct ifreq * ifr)5158 static int mvpp2_set_ts_config(struct mvpp2_port *port, struct ifreq *ifr)
5159 {
5160 struct hwtstamp_config config;
5161 void __iomem *ptp;
5162 u32 gcr, int_mask;
5163
5164 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
5165 return -EFAULT;
5166
5167 if (config.flags)
5168 return -EINVAL;
5169
5170 if (config.tx_type != HWTSTAMP_TX_OFF &&
5171 config.tx_type != HWTSTAMP_TX_ON)
5172 return -ERANGE;
5173
5174 ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
5175
5176 int_mask = gcr = 0;
5177 if (config.tx_type != HWTSTAMP_TX_OFF) {
5178 gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_TX_RESET;
5179 int_mask |= MVPP22_PTP_INT_MASK_QUEUE1 |
5180 MVPP22_PTP_INT_MASK_QUEUE0;
5181 }
5182
5183 /* It seems we must also release the TX reset when enabling the TSU */
5184 if (config.rx_filter != HWTSTAMP_FILTER_NONE)
5185 gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_RX_RESET |
5186 MVPP22_PTP_GCR_TX_RESET;
5187
5188 if (gcr & MVPP22_PTP_GCR_TSU_ENABLE)
5189 mvpp22_tai_start(port->priv->tai);
5190
5191 if (config.rx_filter != HWTSTAMP_FILTER_NONE) {
5192 config.rx_filter = HWTSTAMP_FILTER_ALL;
5193 mvpp2_modify(ptp + MVPP22_PTP_GCR,
5194 MVPP22_PTP_GCR_RX_RESET |
5195 MVPP22_PTP_GCR_TX_RESET |
5196 MVPP22_PTP_GCR_TSU_ENABLE, gcr);
5197 port->rx_hwtstamp = true;
5198 } else {
5199 port->rx_hwtstamp = false;
5200 mvpp2_modify(ptp + MVPP22_PTP_GCR,
5201 MVPP22_PTP_GCR_RX_RESET |
5202 MVPP22_PTP_GCR_TX_RESET |
5203 MVPP22_PTP_GCR_TSU_ENABLE, gcr);
5204 }
5205
5206 mvpp2_modify(ptp + MVPP22_PTP_INT_MASK,
5207 MVPP22_PTP_INT_MASK_QUEUE1 |
5208 MVPP22_PTP_INT_MASK_QUEUE0, int_mask);
5209
5210 if (!(gcr & MVPP22_PTP_GCR_TSU_ENABLE))
5211 mvpp22_tai_stop(port->priv->tai);
5212
5213 port->tx_hwtstamp_type = config.tx_type;
5214
5215 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
5216 return -EFAULT;
5217
5218 return 0;
5219 }
5220
mvpp2_get_ts_config(struct mvpp2_port * port,struct ifreq * ifr)5221 static int mvpp2_get_ts_config(struct mvpp2_port *port, struct ifreq *ifr)
5222 {
5223 struct hwtstamp_config config;
5224
5225 memset(&config, 0, sizeof(config));
5226
5227 config.tx_type = port->tx_hwtstamp_type;
5228 config.rx_filter = port->rx_hwtstamp ?
5229 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
5230
5231 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
5232 return -EFAULT;
5233
5234 return 0;
5235 }
5236
mvpp2_ethtool_get_ts_info(struct net_device * dev,struct ethtool_ts_info * info)5237 static int mvpp2_ethtool_get_ts_info(struct net_device *dev,
5238 struct ethtool_ts_info *info)
5239 {
5240 struct mvpp2_port *port = netdev_priv(dev);
5241
5242 if (!port->hwtstamp)
5243 return -EOPNOTSUPP;
5244
5245 info->phc_index = mvpp22_tai_ptp_clock_index(port->priv->tai);
5246 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5247 SOF_TIMESTAMPING_RX_SOFTWARE |
5248 SOF_TIMESTAMPING_SOFTWARE |
5249 SOF_TIMESTAMPING_TX_HARDWARE |
5250 SOF_TIMESTAMPING_RX_HARDWARE |
5251 SOF_TIMESTAMPING_RAW_HARDWARE;
5252 info->tx_types = BIT(HWTSTAMP_TX_OFF) |
5253 BIT(HWTSTAMP_TX_ON);
5254 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
5255 BIT(HWTSTAMP_FILTER_ALL);
5256
5257 return 0;
5258 }
5259
mvpp2_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)5260 static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5261 {
5262 struct mvpp2_port *port = netdev_priv(dev);
5263
5264 switch (cmd) {
5265 case SIOCSHWTSTAMP:
5266 if (port->hwtstamp)
5267 return mvpp2_set_ts_config(port, ifr);
5268 break;
5269
5270 case SIOCGHWTSTAMP:
5271 if (port->hwtstamp)
5272 return mvpp2_get_ts_config(port, ifr);
5273 break;
5274 }
5275
5276 if (!port->phylink)
5277 return -ENOTSUPP;
5278
5279 return phylink_mii_ioctl(port->phylink, ifr, cmd);
5280 }
5281
mvpp2_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)5282 static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
5283 {
5284 struct mvpp2_port *port = netdev_priv(dev);
5285 int ret;
5286
5287 ret = mvpp2_prs_vid_entry_add(port, vid);
5288 if (ret)
5289 netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
5290 MVPP2_PRS_VLAN_FILT_MAX - 1);
5291 return ret;
5292 }
5293
mvpp2_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)5294 static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
5295 {
5296 struct mvpp2_port *port = netdev_priv(dev);
5297
5298 mvpp2_prs_vid_entry_remove(port, vid);
5299 return 0;
5300 }
5301
mvpp2_set_features(struct net_device * dev,netdev_features_t features)5302 static int mvpp2_set_features(struct net_device *dev,
5303 netdev_features_t features)
5304 {
5305 netdev_features_t changed = dev->features ^ features;
5306 struct mvpp2_port *port = netdev_priv(dev);
5307
5308 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
5309 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
5310 mvpp2_prs_vid_enable_filtering(port);
5311 } else {
5312 /* Invalidate all registered VID filters for this
5313 * port
5314 */
5315 mvpp2_prs_vid_remove_all(port);
5316
5317 mvpp2_prs_vid_disable_filtering(port);
5318 }
5319 }
5320
5321 if (changed & NETIF_F_RXHASH) {
5322 if (features & NETIF_F_RXHASH)
5323 mvpp22_port_rss_enable(port);
5324 else
5325 mvpp22_port_rss_disable(port);
5326 }
5327
5328 return 0;
5329 }
5330
mvpp2_xdp_setup(struct mvpp2_port * port,struct netdev_bpf * bpf)5331 static int mvpp2_xdp_setup(struct mvpp2_port *port, struct netdev_bpf *bpf)
5332 {
5333 struct bpf_prog *prog = bpf->prog, *old_prog;
5334 bool running = netif_running(port->dev);
5335 bool reset = !prog != !port->xdp_prog;
5336
5337 if (port->dev->mtu > MVPP2_MAX_RX_BUF_SIZE) {
5338 NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
5339 return -EOPNOTSUPP;
5340 }
5341
5342 if (!port->priv->percpu_pools) {
5343 NL_SET_ERR_MSG_MOD(bpf->extack, "Per CPU Pools required for XDP");
5344 return -EOPNOTSUPP;
5345 }
5346
5347 if (port->ntxqs < num_possible_cpus() * 2) {
5348 NL_SET_ERR_MSG_MOD(bpf->extack, "XDP_TX needs two TX queues per CPU");
5349 return -EOPNOTSUPP;
5350 }
5351
5352 /* device is up and bpf is added/removed, must setup the RX queues */
5353 if (running && reset)
5354 mvpp2_stop(port->dev);
5355
5356 old_prog = xchg(&port->xdp_prog, prog);
5357 if (old_prog)
5358 bpf_prog_put(old_prog);
5359
5360 /* bpf is just replaced, RXQ and MTU are already setup */
5361 if (!reset)
5362 return 0;
5363
5364 /* device was up, restore the link */
5365 if (running)
5366 mvpp2_open(port->dev);
5367
5368 /* Check Page Pool DMA Direction */
5369 mvpp2_check_pagepool_dma(port);
5370
5371 return 0;
5372 }
5373
mvpp2_xdp(struct net_device * dev,struct netdev_bpf * xdp)5374 static int mvpp2_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5375 {
5376 struct mvpp2_port *port = netdev_priv(dev);
5377
5378 switch (xdp->command) {
5379 case XDP_SETUP_PROG:
5380 return mvpp2_xdp_setup(port, xdp);
5381 default:
5382 return -EINVAL;
5383 }
5384 }
5385
5386 /* Ethtool methods */
5387
mvpp2_ethtool_nway_reset(struct net_device * dev)5388 static int mvpp2_ethtool_nway_reset(struct net_device *dev)
5389 {
5390 struct mvpp2_port *port = netdev_priv(dev);
5391
5392 if (!port->phylink)
5393 return -ENOTSUPP;
5394
5395 return phylink_ethtool_nway_reset(port->phylink);
5396 }
5397
5398 /* Set interrupt coalescing for ethtools */
5399 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)5400 mvpp2_ethtool_set_coalesce(struct net_device *dev,
5401 struct ethtool_coalesce *c,
5402 struct kernel_ethtool_coalesce *kernel_coal,
5403 struct netlink_ext_ack *extack)
5404 {
5405 struct mvpp2_port *port = netdev_priv(dev);
5406 int queue;
5407
5408 for (queue = 0; queue < port->nrxqs; queue++) {
5409 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
5410
5411 rxq->time_coal = c->rx_coalesce_usecs;
5412 rxq->pkts_coal = c->rx_max_coalesced_frames;
5413 mvpp2_rx_pkts_coal_set(port, rxq);
5414 mvpp2_rx_time_coal_set(port, rxq);
5415 }
5416
5417 if (port->has_tx_irqs) {
5418 port->tx_time_coal = c->tx_coalesce_usecs;
5419 mvpp2_tx_time_coal_set(port);
5420 }
5421
5422 for (queue = 0; queue < port->ntxqs; queue++) {
5423 struct mvpp2_tx_queue *txq = port->txqs[queue];
5424
5425 txq->done_pkts_coal = c->tx_max_coalesced_frames;
5426
5427 if (port->has_tx_irqs)
5428 mvpp2_tx_pkts_coal_set(port, txq);
5429 }
5430
5431 return 0;
5432 }
5433
5434 /* get coalescing for ethtools */
5435 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)5436 mvpp2_ethtool_get_coalesce(struct net_device *dev,
5437 struct ethtool_coalesce *c,
5438 struct kernel_ethtool_coalesce *kernel_coal,
5439 struct netlink_ext_ack *extack)
5440 {
5441 struct mvpp2_port *port = netdev_priv(dev);
5442
5443 c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
5444 c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
5445 c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
5446 c->tx_coalesce_usecs = port->tx_time_coal;
5447 return 0;
5448 }
5449
mvpp2_ethtool_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)5450 static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
5451 struct ethtool_drvinfo *drvinfo)
5452 {
5453 strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
5454 sizeof(drvinfo->driver));
5455 strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
5456 sizeof(drvinfo->version));
5457 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
5458 sizeof(drvinfo->bus_info));
5459 }
5460
mvpp2_ethtool_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)5461 static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
5462 struct ethtool_ringparam *ring)
5463 {
5464 struct mvpp2_port *port = netdev_priv(dev);
5465
5466 ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
5467 ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
5468 ring->rx_pending = port->rx_ring_size;
5469 ring->tx_pending = port->tx_ring_size;
5470 }
5471
mvpp2_ethtool_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)5472 static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
5473 struct ethtool_ringparam *ring)
5474 {
5475 struct mvpp2_port *port = netdev_priv(dev);
5476 u16 prev_rx_ring_size = port->rx_ring_size;
5477 u16 prev_tx_ring_size = port->tx_ring_size;
5478 int err;
5479
5480 err = mvpp2_check_ringparam_valid(dev, ring);
5481 if (err)
5482 return err;
5483
5484 if (!netif_running(dev)) {
5485 port->rx_ring_size = ring->rx_pending;
5486 port->tx_ring_size = ring->tx_pending;
5487 return 0;
5488 }
5489
5490 /* The interface is running, so we have to force a
5491 * reallocation of the queues
5492 */
5493 mvpp2_stop_dev(port);
5494 mvpp2_cleanup_rxqs(port);
5495 mvpp2_cleanup_txqs(port);
5496
5497 port->rx_ring_size = ring->rx_pending;
5498 port->tx_ring_size = ring->tx_pending;
5499
5500 err = mvpp2_setup_rxqs(port);
5501 if (err) {
5502 /* Reallocate Rx queues with the original ring size */
5503 port->rx_ring_size = prev_rx_ring_size;
5504 ring->rx_pending = prev_rx_ring_size;
5505 err = mvpp2_setup_rxqs(port);
5506 if (err)
5507 goto err_out;
5508 }
5509 err = mvpp2_setup_txqs(port);
5510 if (err) {
5511 /* Reallocate Tx queues with the original ring size */
5512 port->tx_ring_size = prev_tx_ring_size;
5513 ring->tx_pending = prev_tx_ring_size;
5514 err = mvpp2_setup_txqs(port);
5515 if (err)
5516 goto err_clean_rxqs;
5517 }
5518
5519 mvpp2_start_dev(port);
5520 mvpp2_egress_enable(port);
5521 mvpp2_ingress_enable(port);
5522
5523 return 0;
5524
5525 err_clean_rxqs:
5526 mvpp2_cleanup_rxqs(port);
5527 err_out:
5528 netdev_err(dev, "failed to change ring parameters");
5529 return err;
5530 }
5531
mvpp2_ethtool_get_pause_param(struct net_device * dev,struct ethtool_pauseparam * pause)5532 static void mvpp2_ethtool_get_pause_param(struct net_device *dev,
5533 struct ethtool_pauseparam *pause)
5534 {
5535 struct mvpp2_port *port = netdev_priv(dev);
5536
5537 if (!port->phylink)
5538 return;
5539
5540 phylink_ethtool_get_pauseparam(port->phylink, pause);
5541 }
5542
mvpp2_ethtool_set_pause_param(struct net_device * dev,struct ethtool_pauseparam * pause)5543 static int mvpp2_ethtool_set_pause_param(struct net_device *dev,
5544 struct ethtool_pauseparam *pause)
5545 {
5546 struct mvpp2_port *port = netdev_priv(dev);
5547
5548 if (!port->phylink)
5549 return -ENOTSUPP;
5550
5551 return phylink_ethtool_set_pauseparam(port->phylink, pause);
5552 }
5553
mvpp2_ethtool_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)5554 static int mvpp2_ethtool_get_link_ksettings(struct net_device *dev,
5555 struct ethtool_link_ksettings *cmd)
5556 {
5557 struct mvpp2_port *port = netdev_priv(dev);
5558
5559 if (!port->phylink)
5560 return -ENOTSUPP;
5561
5562 return phylink_ethtool_ksettings_get(port->phylink, cmd);
5563 }
5564
mvpp2_ethtool_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)5565 static int mvpp2_ethtool_set_link_ksettings(struct net_device *dev,
5566 const struct ethtool_link_ksettings *cmd)
5567 {
5568 struct mvpp2_port *port = netdev_priv(dev);
5569
5570 if (!port->phylink)
5571 return -ENOTSUPP;
5572
5573 return phylink_ethtool_ksettings_set(port->phylink, cmd);
5574 }
5575
mvpp2_ethtool_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rules)5576 static int mvpp2_ethtool_get_rxnfc(struct net_device *dev,
5577 struct ethtool_rxnfc *info, u32 *rules)
5578 {
5579 struct mvpp2_port *port = netdev_priv(dev);
5580 int ret = 0, i, loc = 0;
5581
5582 if (!mvpp22_rss_is_supported(port))
5583 return -EOPNOTSUPP;
5584
5585 switch (info->cmd) {
5586 case ETHTOOL_GRXFH:
5587 ret = mvpp2_ethtool_rxfh_get(port, info);
5588 break;
5589 case ETHTOOL_GRXRINGS:
5590 info->data = port->nrxqs;
5591 break;
5592 case ETHTOOL_GRXCLSRLCNT:
5593 info->rule_cnt = port->n_rfs_rules;
5594 break;
5595 case ETHTOOL_GRXCLSRULE:
5596 ret = mvpp2_ethtool_cls_rule_get(port, info);
5597 break;
5598 case ETHTOOL_GRXCLSRLALL:
5599 for (i = 0; i < MVPP2_N_RFS_ENTRIES_PER_FLOW; i++) {
5600 if (loc == info->rule_cnt) {
5601 ret = -EMSGSIZE;
5602 break;
5603 }
5604
5605 if (port->rfs_rules[i])
5606 rules[loc++] = i;
5607 }
5608 break;
5609 default:
5610 return -ENOTSUPP;
5611 }
5612
5613 return ret;
5614 }
5615
mvpp2_ethtool_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)5616 static int mvpp2_ethtool_set_rxnfc(struct net_device *dev,
5617 struct ethtool_rxnfc *info)
5618 {
5619 struct mvpp2_port *port = netdev_priv(dev);
5620 int ret = 0;
5621
5622 if (!mvpp22_rss_is_supported(port))
5623 return -EOPNOTSUPP;
5624
5625 switch (info->cmd) {
5626 case ETHTOOL_SRXFH:
5627 ret = mvpp2_ethtool_rxfh_set(port, info);
5628 break;
5629 case ETHTOOL_SRXCLSRLINS:
5630 ret = mvpp2_ethtool_cls_rule_ins(port, info);
5631 break;
5632 case ETHTOOL_SRXCLSRLDEL:
5633 ret = mvpp2_ethtool_cls_rule_del(port, info);
5634 break;
5635 default:
5636 return -EOPNOTSUPP;
5637 }
5638 return ret;
5639 }
5640
mvpp2_ethtool_get_rxfh_indir_size(struct net_device * dev)5641 static u32 mvpp2_ethtool_get_rxfh_indir_size(struct net_device *dev)
5642 {
5643 struct mvpp2_port *port = netdev_priv(dev);
5644
5645 return mvpp22_rss_is_supported(port) ? MVPP22_RSS_TABLE_ENTRIES : 0;
5646 }
5647
mvpp2_ethtool_get_rxfh(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc)5648 static int mvpp2_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
5649 u8 *hfunc)
5650 {
5651 struct mvpp2_port *port = netdev_priv(dev);
5652 int ret = 0;
5653
5654 if (!mvpp22_rss_is_supported(port))
5655 return -EOPNOTSUPP;
5656
5657 if (indir)
5658 ret = mvpp22_port_rss_ctx_indir_get(port, 0, indir);
5659
5660 if (hfunc)
5661 *hfunc = ETH_RSS_HASH_CRC32;
5662
5663 return ret;
5664 }
5665
mvpp2_ethtool_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc)5666 static int mvpp2_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
5667 const u8 *key, const u8 hfunc)
5668 {
5669 struct mvpp2_port *port = netdev_priv(dev);
5670 int ret = 0;
5671
5672 if (!mvpp22_rss_is_supported(port))
5673 return -EOPNOTSUPP;
5674
5675 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
5676 return -EOPNOTSUPP;
5677
5678 if (key)
5679 return -EOPNOTSUPP;
5680
5681 if (indir)
5682 ret = mvpp22_port_rss_ctx_indir_set(port, 0, indir);
5683
5684 return ret;
5685 }
5686
mvpp2_ethtool_get_rxfh_context(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc,u32 rss_context)5687 static int mvpp2_ethtool_get_rxfh_context(struct net_device *dev, u32 *indir,
5688 u8 *key, u8 *hfunc, u32 rss_context)
5689 {
5690 struct mvpp2_port *port = netdev_priv(dev);
5691 int ret = 0;
5692
5693 if (!mvpp22_rss_is_supported(port))
5694 return -EOPNOTSUPP;
5695 if (rss_context >= MVPP22_N_RSS_TABLES)
5696 return -EINVAL;
5697
5698 if (hfunc)
5699 *hfunc = ETH_RSS_HASH_CRC32;
5700
5701 if (indir)
5702 ret = mvpp22_port_rss_ctx_indir_get(port, rss_context, indir);
5703
5704 return ret;
5705 }
5706
mvpp2_ethtool_set_rxfh_context(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc,u32 * rss_context,bool delete)5707 static int mvpp2_ethtool_set_rxfh_context(struct net_device *dev,
5708 const u32 *indir, const u8 *key,
5709 const u8 hfunc, u32 *rss_context,
5710 bool delete)
5711 {
5712 struct mvpp2_port *port = netdev_priv(dev);
5713 int ret;
5714
5715 if (!mvpp22_rss_is_supported(port))
5716 return -EOPNOTSUPP;
5717
5718 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
5719 return -EOPNOTSUPP;
5720
5721 if (key)
5722 return -EOPNOTSUPP;
5723
5724 if (delete)
5725 return mvpp22_port_rss_ctx_delete(port, *rss_context);
5726
5727 if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
5728 ret = mvpp22_port_rss_ctx_create(port, rss_context);
5729 if (ret)
5730 return ret;
5731 }
5732
5733 return mvpp22_port_rss_ctx_indir_set(port, *rss_context, indir);
5734 }
5735 /* Device ops */
5736
5737 static const struct net_device_ops mvpp2_netdev_ops = {
5738 .ndo_open = mvpp2_open,
5739 .ndo_stop = mvpp2_stop,
5740 .ndo_start_xmit = mvpp2_tx,
5741 .ndo_set_rx_mode = mvpp2_set_rx_mode,
5742 .ndo_set_mac_address = mvpp2_set_mac_address,
5743 .ndo_change_mtu = mvpp2_change_mtu,
5744 .ndo_get_stats64 = mvpp2_get_stats64,
5745 .ndo_eth_ioctl = mvpp2_ioctl,
5746 .ndo_vlan_rx_add_vid = mvpp2_vlan_rx_add_vid,
5747 .ndo_vlan_rx_kill_vid = mvpp2_vlan_rx_kill_vid,
5748 .ndo_set_features = mvpp2_set_features,
5749 .ndo_bpf = mvpp2_xdp,
5750 .ndo_xdp_xmit = mvpp2_xdp_xmit,
5751 };
5752
5753 static const struct ethtool_ops mvpp2_eth_tool_ops = {
5754 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
5755 ETHTOOL_COALESCE_MAX_FRAMES,
5756 .nway_reset = mvpp2_ethtool_nway_reset,
5757 .get_link = ethtool_op_get_link,
5758 .get_ts_info = mvpp2_ethtool_get_ts_info,
5759 .set_coalesce = mvpp2_ethtool_set_coalesce,
5760 .get_coalesce = mvpp2_ethtool_get_coalesce,
5761 .get_drvinfo = mvpp2_ethtool_get_drvinfo,
5762 .get_ringparam = mvpp2_ethtool_get_ringparam,
5763 .set_ringparam = mvpp2_ethtool_set_ringparam,
5764 .get_strings = mvpp2_ethtool_get_strings,
5765 .get_ethtool_stats = mvpp2_ethtool_get_stats,
5766 .get_sset_count = mvpp2_ethtool_get_sset_count,
5767 .get_pauseparam = mvpp2_ethtool_get_pause_param,
5768 .set_pauseparam = mvpp2_ethtool_set_pause_param,
5769 .get_link_ksettings = mvpp2_ethtool_get_link_ksettings,
5770 .set_link_ksettings = mvpp2_ethtool_set_link_ksettings,
5771 .get_rxnfc = mvpp2_ethtool_get_rxnfc,
5772 .set_rxnfc = mvpp2_ethtool_set_rxnfc,
5773 .get_rxfh_indir_size = mvpp2_ethtool_get_rxfh_indir_size,
5774 .get_rxfh = mvpp2_ethtool_get_rxfh,
5775 .set_rxfh = mvpp2_ethtool_set_rxfh,
5776 .get_rxfh_context = mvpp2_ethtool_get_rxfh_context,
5777 .set_rxfh_context = mvpp2_ethtool_set_rxfh_context,
5778 };
5779
5780 /* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
5781 * had a single IRQ defined per-port.
5782 */
mvpp2_simple_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5783 static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
5784 struct device_node *port_node)
5785 {
5786 struct mvpp2_queue_vector *v = &port->qvecs[0];
5787
5788 v->first_rxq = 0;
5789 v->nrxqs = port->nrxqs;
5790 v->type = MVPP2_QUEUE_VECTOR_SHARED;
5791 v->sw_thread_id = 0;
5792 v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
5793 v->port = port;
5794 v->irq = irq_of_parse_and_map(port_node, 0);
5795 if (v->irq <= 0)
5796 return -EINVAL;
5797 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
5798 NAPI_POLL_WEIGHT);
5799
5800 port->nqvecs = 1;
5801
5802 return 0;
5803 }
5804
mvpp2_multi_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5805 static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
5806 struct device_node *port_node)
5807 {
5808 struct mvpp2 *priv = port->priv;
5809 struct mvpp2_queue_vector *v;
5810 int i, ret;
5811
5812 switch (queue_mode) {
5813 case MVPP2_QDIST_SINGLE_MODE:
5814 port->nqvecs = priv->nthreads + 1;
5815 break;
5816 case MVPP2_QDIST_MULTI_MODE:
5817 port->nqvecs = priv->nthreads;
5818 break;
5819 }
5820
5821 for (i = 0; i < port->nqvecs; i++) {
5822 char irqname[16];
5823
5824 v = port->qvecs + i;
5825
5826 v->port = port;
5827 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
5828 v->sw_thread_id = i;
5829 v->sw_thread_mask = BIT(i);
5830
5831 if (port->flags & MVPP2_F_DT_COMPAT)
5832 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
5833 else
5834 snprintf(irqname, sizeof(irqname), "hif%d", i);
5835
5836 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
5837 v->first_rxq = i;
5838 v->nrxqs = 1;
5839 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
5840 i == (port->nqvecs - 1)) {
5841 v->first_rxq = 0;
5842 v->nrxqs = port->nrxqs;
5843 v->type = MVPP2_QUEUE_VECTOR_SHARED;
5844
5845 if (port->flags & MVPP2_F_DT_COMPAT)
5846 strncpy(irqname, "rx-shared", sizeof(irqname));
5847 }
5848
5849 if (port_node)
5850 v->irq = of_irq_get_byname(port_node, irqname);
5851 else
5852 v->irq = fwnode_irq_get(port->fwnode, i);
5853 if (v->irq <= 0) {
5854 ret = -EINVAL;
5855 goto err;
5856 }
5857
5858 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
5859 NAPI_POLL_WEIGHT);
5860 }
5861
5862 return 0;
5863
5864 err:
5865 for (i = 0; i < port->nqvecs; i++)
5866 irq_dispose_mapping(port->qvecs[i].irq);
5867 return ret;
5868 }
5869
mvpp2_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5870 static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
5871 struct device_node *port_node)
5872 {
5873 if (port->has_tx_irqs)
5874 return mvpp2_multi_queue_vectors_init(port, port_node);
5875 else
5876 return mvpp2_simple_queue_vectors_init(port, port_node);
5877 }
5878
mvpp2_queue_vectors_deinit(struct mvpp2_port * port)5879 static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
5880 {
5881 int i;
5882
5883 for (i = 0; i < port->nqvecs; i++)
5884 irq_dispose_mapping(port->qvecs[i].irq);
5885 }
5886
5887 /* Configure Rx queue group interrupt for this port */
mvpp2_rx_irqs_setup(struct mvpp2_port * port)5888 static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
5889 {
5890 struct mvpp2 *priv = port->priv;
5891 u32 val;
5892 int i;
5893
5894 if (priv->hw_version == MVPP21) {
5895 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
5896 port->nrxqs);
5897 return;
5898 }
5899
5900 /* Handle the more complicated PPv2.2 and PPv2.3 case */
5901 for (i = 0; i < port->nqvecs; i++) {
5902 struct mvpp2_queue_vector *qv = port->qvecs + i;
5903
5904 if (!qv->nrxqs)
5905 continue;
5906
5907 val = qv->sw_thread_id;
5908 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
5909 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
5910
5911 val = qv->first_rxq;
5912 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
5913 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
5914 }
5915 }
5916
5917 /* Initialize port HW */
mvpp2_port_init(struct mvpp2_port * port)5918 static int mvpp2_port_init(struct mvpp2_port *port)
5919 {
5920 struct device *dev = port->dev->dev.parent;
5921 struct mvpp2 *priv = port->priv;
5922 struct mvpp2_txq_pcpu *txq_pcpu;
5923 unsigned int thread;
5924 int queue, err, val;
5925
5926 /* Checks for hardware constraints */
5927 if (port->first_rxq + port->nrxqs >
5928 MVPP2_MAX_PORTS * priv->max_port_rxqs)
5929 return -EINVAL;
5930
5931 if (port->nrxqs > priv->max_port_rxqs || port->ntxqs > MVPP2_MAX_TXQ)
5932 return -EINVAL;
5933
5934 /* Disable port */
5935 mvpp2_egress_disable(port);
5936 mvpp2_port_disable(port);
5937
5938 if (mvpp2_is_xlg(port->phy_interface)) {
5939 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5940 val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
5941 val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
5942 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5943 } else {
5944 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5945 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
5946 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
5947 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5948 }
5949
5950 port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
5951
5952 port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
5953 GFP_KERNEL);
5954 if (!port->txqs)
5955 return -ENOMEM;
5956
5957 /* Associate physical Tx queues to this port and initialize.
5958 * The mapping is predefined.
5959 */
5960 for (queue = 0; queue < port->ntxqs; queue++) {
5961 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
5962 struct mvpp2_tx_queue *txq;
5963
5964 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
5965 if (!txq) {
5966 err = -ENOMEM;
5967 goto err_free_percpu;
5968 }
5969
5970 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
5971 if (!txq->pcpu) {
5972 err = -ENOMEM;
5973 goto err_free_percpu;
5974 }
5975
5976 txq->id = queue_phy_id;
5977 txq->log_id = queue;
5978 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
5979 for (thread = 0; thread < priv->nthreads; thread++) {
5980 txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
5981 txq_pcpu->thread = thread;
5982 }
5983
5984 port->txqs[queue] = txq;
5985 }
5986
5987 port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
5988 GFP_KERNEL);
5989 if (!port->rxqs) {
5990 err = -ENOMEM;
5991 goto err_free_percpu;
5992 }
5993
5994 /* Allocate and initialize Rx queue for this port */
5995 for (queue = 0; queue < port->nrxqs; queue++) {
5996 struct mvpp2_rx_queue *rxq;
5997
5998 /* Map physical Rx queue to port's logical Rx queue */
5999 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
6000 if (!rxq) {
6001 err = -ENOMEM;
6002 goto err_free_percpu;
6003 }
6004 /* Map this Rx queue to a physical queue */
6005 rxq->id = port->first_rxq + queue;
6006 rxq->port = port->id;
6007 rxq->logic_rxq = queue;
6008
6009 port->rxqs[queue] = rxq;
6010 }
6011
6012 mvpp2_rx_irqs_setup(port);
6013
6014 /* Create Rx descriptor rings */
6015 for (queue = 0; queue < port->nrxqs; queue++) {
6016 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
6017
6018 rxq->size = port->rx_ring_size;
6019 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
6020 rxq->time_coal = MVPP2_RX_COAL_USEC;
6021 }
6022
6023 mvpp2_ingress_disable(port);
6024
6025 /* Port default configuration */
6026 mvpp2_defaults_set(port);
6027
6028 /* Port's classifier configuration */
6029 mvpp2_cls_oversize_rxq_set(port);
6030 mvpp2_cls_port_config(port);
6031
6032 if (mvpp22_rss_is_supported(port))
6033 mvpp22_port_rss_init(port);
6034
6035 /* Provide an initial Rx packet size */
6036 port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
6037
6038 /* Initialize pools for swf */
6039 err = mvpp2_swf_bm_pool_init(port);
6040 if (err)
6041 goto err_free_percpu;
6042
6043 /* Clear all port stats */
6044 mvpp2_read_stats(port);
6045 memset(port->ethtool_stats, 0,
6046 MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs) * sizeof(u64));
6047
6048 return 0;
6049
6050 err_free_percpu:
6051 for (queue = 0; queue < port->ntxqs; queue++) {
6052 if (!port->txqs[queue])
6053 continue;
6054 free_percpu(port->txqs[queue]->pcpu);
6055 }
6056 return err;
6057 }
6058
mvpp22_port_has_legacy_tx_irqs(struct device_node * port_node,unsigned long * flags)6059 static bool mvpp22_port_has_legacy_tx_irqs(struct device_node *port_node,
6060 unsigned long *flags)
6061 {
6062 char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1", "tx-cpu2",
6063 "tx-cpu3" };
6064 int i;
6065
6066 for (i = 0; i < 5; i++)
6067 if (of_property_match_string(port_node, "interrupt-names",
6068 irqs[i]) < 0)
6069 return false;
6070
6071 *flags |= MVPP2_F_DT_COMPAT;
6072 return true;
6073 }
6074
6075 /* Checks if the port dt description has the required Tx interrupts:
6076 * - PPv2.1: there are no such interrupts.
6077 * - PPv2.2 and PPv2.3:
6078 * - The old DTs have: "rx-shared", "tx-cpuX" with X in [0...3]
6079 * - The new ones have: "hifX" with X in [0..8]
6080 *
6081 * All those variants are supported to keep the backward compatibility.
6082 */
mvpp2_port_has_irqs(struct mvpp2 * priv,struct device_node * port_node,unsigned long * flags)6083 static bool mvpp2_port_has_irqs(struct mvpp2 *priv,
6084 struct device_node *port_node,
6085 unsigned long *flags)
6086 {
6087 char name[5];
6088 int i;
6089
6090 /* ACPI */
6091 if (!port_node)
6092 return true;
6093
6094 if (priv->hw_version == MVPP21)
6095 return false;
6096
6097 if (mvpp22_port_has_legacy_tx_irqs(port_node, flags))
6098 return true;
6099
6100 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
6101 snprintf(name, 5, "hif%d", i);
6102 if (of_property_match_string(port_node, "interrupt-names",
6103 name) < 0)
6104 return false;
6105 }
6106
6107 return true;
6108 }
6109
mvpp2_port_copy_mac_addr(struct net_device * dev,struct mvpp2 * priv,struct fwnode_handle * fwnode,char ** mac_from)6110 static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
6111 struct fwnode_handle *fwnode,
6112 char **mac_from)
6113 {
6114 struct mvpp2_port *port = netdev_priv(dev);
6115 char hw_mac_addr[ETH_ALEN] = {0};
6116 char fw_mac_addr[ETH_ALEN];
6117
6118 if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
6119 *mac_from = "firmware node";
6120 eth_hw_addr_set(dev, fw_mac_addr);
6121 return;
6122 }
6123
6124 if (priv->hw_version == MVPP21) {
6125 mvpp21_get_mac_address(port, hw_mac_addr);
6126 if (is_valid_ether_addr(hw_mac_addr)) {
6127 *mac_from = "hardware";
6128 eth_hw_addr_set(dev, hw_mac_addr);
6129 return;
6130 }
6131 }
6132
6133 *mac_from = "random";
6134 eth_hw_addr_random(dev);
6135 }
6136
mvpp2_phylink_to_port(struct phylink_config * config)6137 static struct mvpp2_port *mvpp2_phylink_to_port(struct phylink_config *config)
6138 {
6139 return container_of(config, struct mvpp2_port, phylink_config);
6140 }
6141
mvpp2_pcs_to_port(struct phylink_pcs * pcs)6142 static struct mvpp2_port *mvpp2_pcs_to_port(struct phylink_pcs *pcs)
6143 {
6144 return container_of(pcs, struct mvpp2_port, phylink_pcs);
6145 }
6146
mvpp2_xlg_pcs_get_state(struct phylink_pcs * pcs,struct phylink_link_state * state)6147 static void mvpp2_xlg_pcs_get_state(struct phylink_pcs *pcs,
6148 struct phylink_link_state *state)
6149 {
6150 struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
6151 u32 val;
6152
6153 state->speed = SPEED_10000;
6154 state->duplex = 1;
6155 state->an_complete = 1;
6156
6157 val = readl(port->base + MVPP22_XLG_STATUS);
6158 state->link = !!(val & MVPP22_XLG_STATUS_LINK_UP);
6159
6160 state->pause = 0;
6161 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6162 if (val & MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN)
6163 state->pause |= MLO_PAUSE_TX;
6164 if (val & MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN)
6165 state->pause |= MLO_PAUSE_RX;
6166 }
6167
mvpp2_xlg_pcs_config(struct phylink_pcs * pcs,unsigned int mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)6168 static int mvpp2_xlg_pcs_config(struct phylink_pcs *pcs,
6169 unsigned int mode,
6170 phy_interface_t interface,
6171 const unsigned long *advertising,
6172 bool permit_pause_to_mac)
6173 {
6174 return 0;
6175 }
6176
6177 static const struct phylink_pcs_ops mvpp2_phylink_xlg_pcs_ops = {
6178 .pcs_get_state = mvpp2_xlg_pcs_get_state,
6179 .pcs_config = mvpp2_xlg_pcs_config,
6180 };
6181
mvpp2_gmac_pcs_get_state(struct phylink_pcs * pcs,struct phylink_link_state * state)6182 static void mvpp2_gmac_pcs_get_state(struct phylink_pcs *pcs,
6183 struct phylink_link_state *state)
6184 {
6185 struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
6186 u32 val;
6187
6188 val = readl(port->base + MVPP2_GMAC_STATUS0);
6189
6190 state->an_complete = !!(val & MVPP2_GMAC_STATUS0_AN_COMPLETE);
6191 state->link = !!(val & MVPP2_GMAC_STATUS0_LINK_UP);
6192 state->duplex = !!(val & MVPP2_GMAC_STATUS0_FULL_DUPLEX);
6193
6194 switch (port->phy_interface) {
6195 case PHY_INTERFACE_MODE_1000BASEX:
6196 state->speed = SPEED_1000;
6197 break;
6198 case PHY_INTERFACE_MODE_2500BASEX:
6199 state->speed = SPEED_2500;
6200 break;
6201 default:
6202 if (val & MVPP2_GMAC_STATUS0_GMII_SPEED)
6203 state->speed = SPEED_1000;
6204 else if (val & MVPP2_GMAC_STATUS0_MII_SPEED)
6205 state->speed = SPEED_100;
6206 else
6207 state->speed = SPEED_10;
6208 }
6209
6210 state->pause = 0;
6211 if (val & MVPP2_GMAC_STATUS0_RX_PAUSE)
6212 state->pause |= MLO_PAUSE_RX;
6213 if (val & MVPP2_GMAC_STATUS0_TX_PAUSE)
6214 state->pause |= MLO_PAUSE_TX;
6215 }
6216
mvpp2_gmac_pcs_config(struct phylink_pcs * pcs,unsigned int mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)6217 static int mvpp2_gmac_pcs_config(struct phylink_pcs *pcs, unsigned int mode,
6218 phy_interface_t interface,
6219 const unsigned long *advertising,
6220 bool permit_pause_to_mac)
6221 {
6222 struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
6223 u32 mask, val, an, old_an, changed;
6224
6225 mask = MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
6226 MVPP2_GMAC_IN_BAND_AUTONEG |
6227 MVPP2_GMAC_AN_SPEED_EN |
6228 MVPP2_GMAC_FLOW_CTRL_AUTONEG |
6229 MVPP2_GMAC_AN_DUPLEX_EN;
6230
6231 if (phylink_autoneg_inband(mode)) {
6232 mask |= MVPP2_GMAC_CONFIG_MII_SPEED |
6233 MVPP2_GMAC_CONFIG_GMII_SPEED |
6234 MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6235 val = MVPP2_GMAC_IN_BAND_AUTONEG;
6236
6237 if (interface == PHY_INTERFACE_MODE_SGMII) {
6238 /* SGMII mode receives the speed and duplex from PHY */
6239 val |= MVPP2_GMAC_AN_SPEED_EN |
6240 MVPP2_GMAC_AN_DUPLEX_EN;
6241 } else {
6242 /* 802.3z mode has fixed speed and duplex */
6243 val |= MVPP2_GMAC_CONFIG_GMII_SPEED |
6244 MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6245
6246 /* The FLOW_CTRL_AUTONEG bit selects either the hardware
6247 * automatically or the bits in MVPP22_GMAC_CTRL_4_REG
6248 * manually controls the GMAC pause modes.
6249 */
6250 if (permit_pause_to_mac)
6251 val |= MVPP2_GMAC_FLOW_CTRL_AUTONEG;
6252
6253 /* Configure advertisement bits */
6254 mask |= MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN;
6255 if (phylink_test(advertising, Pause))
6256 val |= MVPP2_GMAC_FC_ADV_EN;
6257 if (phylink_test(advertising, Asym_Pause))
6258 val |= MVPP2_GMAC_FC_ADV_ASM_EN;
6259 }
6260 } else {
6261 val = 0;
6262 }
6263
6264 old_an = an = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6265 an = (an & ~mask) | val;
6266 changed = an ^ old_an;
6267 if (changed)
6268 writel(an, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6269
6270 /* We are only interested in the advertisement bits changing */
6271 return changed & (MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN);
6272 }
6273
mvpp2_gmac_pcs_an_restart(struct phylink_pcs * pcs)6274 static void mvpp2_gmac_pcs_an_restart(struct phylink_pcs *pcs)
6275 {
6276 struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
6277 u32 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6278
6279 writel(val | MVPP2_GMAC_IN_BAND_RESTART_AN,
6280 port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6281 writel(val & ~MVPP2_GMAC_IN_BAND_RESTART_AN,
6282 port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6283 }
6284
6285 static const struct phylink_pcs_ops mvpp2_phylink_gmac_pcs_ops = {
6286 .pcs_get_state = mvpp2_gmac_pcs_get_state,
6287 .pcs_config = mvpp2_gmac_pcs_config,
6288 .pcs_an_restart = mvpp2_gmac_pcs_an_restart,
6289 };
6290
mvpp2_phylink_validate(struct phylink_config * config,unsigned long * supported,struct phylink_link_state * state)6291 static void mvpp2_phylink_validate(struct phylink_config *config,
6292 unsigned long *supported,
6293 struct phylink_link_state *state)
6294 {
6295 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6296 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
6297
6298 /* Invalid combinations */
6299 switch (state->interface) {
6300 case PHY_INTERFACE_MODE_10GBASER:
6301 case PHY_INTERFACE_MODE_XAUI:
6302 if (!mvpp2_port_supports_xlg(port))
6303 goto empty_set;
6304 break;
6305 case PHY_INTERFACE_MODE_RGMII:
6306 case PHY_INTERFACE_MODE_RGMII_ID:
6307 case PHY_INTERFACE_MODE_RGMII_RXID:
6308 case PHY_INTERFACE_MODE_RGMII_TXID:
6309 if (!mvpp2_port_supports_rgmii(port))
6310 goto empty_set;
6311 break;
6312 case PHY_INTERFACE_MODE_1000BASEX:
6313 case PHY_INTERFACE_MODE_2500BASEX:
6314 /* When in 802.3z mode, we must have AN enabled:
6315 * Bit 2 Field InBandAnEn In-band Auto-Negotiation enable. ...
6316 * When <PortType> = 1 (1000BASE-X) this field must be set to 1.
6317 */
6318 if (!phylink_test(state->advertising, Autoneg))
6319 goto empty_set;
6320 break;
6321 default:
6322 break;
6323 }
6324
6325 phylink_set(mask, Autoneg);
6326 phylink_set_port_modes(mask);
6327
6328 if (port->priv->global_tx_fc) {
6329 phylink_set(mask, Pause);
6330 phylink_set(mask, Asym_Pause);
6331 }
6332
6333 switch (state->interface) {
6334 case PHY_INTERFACE_MODE_10GBASER:
6335 case PHY_INTERFACE_MODE_XAUI:
6336 case PHY_INTERFACE_MODE_NA:
6337 if (mvpp2_port_supports_xlg(port)) {
6338 phylink_set(mask, 10000baseT_Full);
6339 phylink_set(mask, 10000baseCR_Full);
6340 phylink_set(mask, 10000baseSR_Full);
6341 phylink_set(mask, 10000baseLR_Full);
6342 phylink_set(mask, 10000baseLRM_Full);
6343 phylink_set(mask, 10000baseER_Full);
6344 phylink_set(mask, 10000baseKR_Full);
6345 }
6346 if (state->interface != PHY_INTERFACE_MODE_NA)
6347 break;
6348 fallthrough;
6349 case PHY_INTERFACE_MODE_RGMII:
6350 case PHY_INTERFACE_MODE_RGMII_ID:
6351 case PHY_INTERFACE_MODE_RGMII_RXID:
6352 case PHY_INTERFACE_MODE_RGMII_TXID:
6353 case PHY_INTERFACE_MODE_SGMII:
6354 phylink_set(mask, 10baseT_Half);
6355 phylink_set(mask, 10baseT_Full);
6356 phylink_set(mask, 100baseT_Half);
6357 phylink_set(mask, 100baseT_Full);
6358 phylink_set(mask, 1000baseT_Full);
6359 phylink_set(mask, 1000baseX_Full);
6360 if (state->interface != PHY_INTERFACE_MODE_NA)
6361 break;
6362 fallthrough;
6363 case PHY_INTERFACE_MODE_1000BASEX:
6364 case PHY_INTERFACE_MODE_2500BASEX:
6365 if (port->comphy ||
6366 state->interface != PHY_INTERFACE_MODE_2500BASEX) {
6367 phylink_set(mask, 1000baseT_Full);
6368 phylink_set(mask, 1000baseX_Full);
6369 }
6370 if (port->comphy ||
6371 state->interface == PHY_INTERFACE_MODE_2500BASEX) {
6372 phylink_set(mask, 2500baseT_Full);
6373 phylink_set(mask, 2500baseX_Full);
6374 }
6375 break;
6376 default:
6377 goto empty_set;
6378 }
6379
6380 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
6381 bitmap_and(state->advertising, state->advertising, mask,
6382 __ETHTOOL_LINK_MODE_MASK_NBITS);
6383
6384 phylink_helper_basex_speed(state);
6385 return;
6386
6387 empty_set:
6388 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
6389 }
6390
mvpp2_xlg_config(struct mvpp2_port * port,unsigned int mode,const struct phylink_link_state * state)6391 static void mvpp2_xlg_config(struct mvpp2_port *port, unsigned int mode,
6392 const struct phylink_link_state *state)
6393 {
6394 u32 val;
6395
6396 mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6397 MVPP22_XLG_CTRL0_MAC_RESET_DIS,
6398 MVPP22_XLG_CTRL0_MAC_RESET_DIS);
6399 mvpp2_modify(port->base + MVPP22_XLG_CTRL4_REG,
6400 MVPP22_XLG_CTRL4_MACMODSELECT_GMAC |
6401 MVPP22_XLG_CTRL4_EN_IDLE_CHECK |
6402 MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC,
6403 MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC);
6404
6405 /* Wait for reset to deassert */
6406 do {
6407 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6408 } while (!(val & MVPP22_XLG_CTRL0_MAC_RESET_DIS));
6409 }
6410
mvpp2_gmac_config(struct mvpp2_port * port,unsigned int mode,const struct phylink_link_state * state)6411 static void mvpp2_gmac_config(struct mvpp2_port *port, unsigned int mode,
6412 const struct phylink_link_state *state)
6413 {
6414 u32 old_ctrl0, ctrl0;
6415 u32 old_ctrl2, ctrl2;
6416 u32 old_ctrl4, ctrl4;
6417
6418 old_ctrl0 = ctrl0 = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
6419 old_ctrl2 = ctrl2 = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
6420 old_ctrl4 = ctrl4 = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
6421
6422 ctrl0 &= ~MVPP2_GMAC_PORT_TYPE_MASK;
6423 ctrl2 &= ~(MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_FLOW_CTRL_MASK);
6424
6425 /* Configure port type */
6426 if (phy_interface_mode_is_8023z(state->interface)) {
6427 ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK;
6428 ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
6429 ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
6430 MVPP22_CTRL4_DP_CLK_SEL |
6431 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6432 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
6433 ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_INBAND_AN_MASK;
6434 ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
6435 ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
6436 MVPP22_CTRL4_DP_CLK_SEL |
6437 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6438 } else if (phy_interface_mode_is_rgmii(state->interface)) {
6439 ctrl4 &= ~MVPP22_CTRL4_DP_CLK_SEL;
6440 ctrl4 |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
6441 MVPP22_CTRL4_SYNC_BYPASS_DIS |
6442 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6443 }
6444
6445 /* Configure negotiation style */
6446 if (!phylink_autoneg_inband(mode)) {
6447 /* Phy or fixed speed - no in-band AN, nothing to do, leave the
6448 * configured speed, duplex and flow control as-is.
6449 */
6450 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
6451 /* SGMII in-band mode receives the speed and duplex from
6452 * the PHY. Flow control information is not received. */
6453 } else if (phy_interface_mode_is_8023z(state->interface)) {
6454 /* 1000BaseX and 2500BaseX ports cannot negotiate speed nor can
6455 * they negotiate duplex: they are always operating with a fixed
6456 * speed of 1000/2500Mbps in full duplex, so force 1000/2500
6457 * speed and full duplex here.
6458 */
6459 ctrl0 |= MVPP2_GMAC_PORT_TYPE_MASK;
6460 }
6461
6462 if (old_ctrl0 != ctrl0)
6463 writel(ctrl0, port->base + MVPP2_GMAC_CTRL_0_REG);
6464 if (old_ctrl2 != ctrl2)
6465 writel(ctrl2, port->base + MVPP2_GMAC_CTRL_2_REG);
6466 if (old_ctrl4 != ctrl4)
6467 writel(ctrl4, port->base + MVPP22_GMAC_CTRL_4_REG);
6468 }
6469
mvpp2__mac_prepare(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6470 static int mvpp2__mac_prepare(struct phylink_config *config, unsigned int mode,
6471 phy_interface_t interface)
6472 {
6473 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6474
6475 /* Check for invalid configuration */
6476 if (mvpp2_is_xlg(interface) && port->gop_id != 0) {
6477 netdev_err(port->dev, "Invalid mode on %s\n", port->dev->name);
6478 return -EINVAL;
6479 }
6480
6481 if (port->phy_interface != interface ||
6482 phylink_autoneg_inband(mode)) {
6483 /* Force the link down when changing the interface or if in
6484 * in-band mode to ensure we do not change the configuration
6485 * while the hardware is indicating link is up. We force both
6486 * XLG and GMAC down to ensure that they're both in a known
6487 * state.
6488 */
6489 mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6490 MVPP2_GMAC_FORCE_LINK_PASS |
6491 MVPP2_GMAC_FORCE_LINK_DOWN,
6492 MVPP2_GMAC_FORCE_LINK_DOWN);
6493
6494 if (mvpp2_port_supports_xlg(port))
6495 mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6496 MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6497 MVPP22_XLG_CTRL0_FORCE_LINK_DOWN,
6498 MVPP22_XLG_CTRL0_FORCE_LINK_DOWN);
6499 }
6500
6501 /* Make sure the port is disabled when reconfiguring the mode */
6502 mvpp2_port_disable(port);
6503
6504 if (port->phy_interface != interface) {
6505 /* Place GMAC into reset */
6506 mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
6507 MVPP2_GMAC_PORT_RESET_MASK,
6508 MVPP2_GMAC_PORT_RESET_MASK);
6509
6510 if (port->priv->hw_version >= MVPP22) {
6511 mvpp22_gop_mask_irq(port);
6512
6513 phy_power_off(port->comphy);
6514
6515 /* Reconfigure the serdes lanes */
6516 mvpp22_mode_reconfigure(port, interface);
6517 }
6518 }
6519
6520 /* Select the appropriate PCS operations depending on the
6521 * configured interface mode. We will only switch to a mode
6522 * that the validate() checks have already passed.
6523 */
6524 if (mvpp2_is_xlg(interface))
6525 port->phylink_pcs.ops = &mvpp2_phylink_xlg_pcs_ops;
6526 else
6527 port->phylink_pcs.ops = &mvpp2_phylink_gmac_pcs_ops;
6528
6529 return 0;
6530 }
6531
mvpp2_mac_prepare(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6532 static int mvpp2_mac_prepare(struct phylink_config *config, unsigned int mode,
6533 phy_interface_t interface)
6534 {
6535 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6536 int ret;
6537
6538 ret = mvpp2__mac_prepare(config, mode, interface);
6539 if (ret == 0)
6540 phylink_set_pcs(port->phylink, &port->phylink_pcs);
6541
6542 return ret;
6543 }
6544
mvpp2_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)6545 static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode,
6546 const struct phylink_link_state *state)
6547 {
6548 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6549
6550 /* mac (re)configuration */
6551 if (mvpp2_is_xlg(state->interface))
6552 mvpp2_xlg_config(port, mode, state);
6553 else if (phy_interface_mode_is_rgmii(state->interface) ||
6554 phy_interface_mode_is_8023z(state->interface) ||
6555 state->interface == PHY_INTERFACE_MODE_SGMII)
6556 mvpp2_gmac_config(port, mode, state);
6557
6558 if (port->priv->hw_version == MVPP21 && port->flags & MVPP2_F_LOOPBACK)
6559 mvpp2_port_loopback_set(port, state);
6560 }
6561
mvpp2_mac_finish(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6562 static int mvpp2_mac_finish(struct phylink_config *config, unsigned int mode,
6563 phy_interface_t interface)
6564 {
6565 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6566
6567 if (port->priv->hw_version >= MVPP22 &&
6568 port->phy_interface != interface) {
6569 port->phy_interface = interface;
6570
6571 /* Unmask interrupts */
6572 mvpp22_gop_unmask_irq(port);
6573 }
6574
6575 if (!mvpp2_is_xlg(interface)) {
6576 /* Release GMAC reset and wait */
6577 mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
6578 MVPP2_GMAC_PORT_RESET_MASK, 0);
6579
6580 while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
6581 MVPP2_GMAC_PORT_RESET_MASK)
6582 continue;
6583 }
6584
6585 mvpp2_port_enable(port);
6586
6587 /* Allow the link to come up if in in-band mode, otherwise the
6588 * link is forced via mac_link_down()/mac_link_up()
6589 */
6590 if (phylink_autoneg_inband(mode)) {
6591 if (mvpp2_is_xlg(interface))
6592 mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6593 MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6594 MVPP22_XLG_CTRL0_FORCE_LINK_DOWN, 0);
6595 else
6596 mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6597 MVPP2_GMAC_FORCE_LINK_PASS |
6598 MVPP2_GMAC_FORCE_LINK_DOWN, 0);
6599 }
6600
6601 return 0;
6602 }
6603
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)6604 static void mvpp2_mac_link_up(struct phylink_config *config,
6605 struct phy_device *phy,
6606 unsigned int mode, phy_interface_t interface,
6607 int speed, int duplex,
6608 bool tx_pause, bool rx_pause)
6609 {
6610 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6611 u32 val;
6612 int i;
6613
6614 if (mvpp2_is_xlg(interface)) {
6615 if (!phylink_autoneg_inband(mode)) {
6616 val = MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6617 if (tx_pause)
6618 val |= MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN;
6619 if (rx_pause)
6620 val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
6621
6622 mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6623 MVPP22_XLG_CTRL0_FORCE_LINK_DOWN |
6624 MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6625 MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN |
6626 MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN, val);
6627 }
6628 } else {
6629 if (!phylink_autoneg_inband(mode)) {
6630 val = MVPP2_GMAC_FORCE_LINK_PASS;
6631
6632 if (speed == SPEED_1000 || speed == SPEED_2500)
6633 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6634 else if (speed == SPEED_100)
6635 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6636
6637 if (duplex == DUPLEX_FULL)
6638 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6639
6640 mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6641 MVPP2_GMAC_FORCE_LINK_DOWN |
6642 MVPP2_GMAC_FORCE_LINK_PASS |
6643 MVPP2_GMAC_CONFIG_MII_SPEED |
6644 MVPP2_GMAC_CONFIG_GMII_SPEED |
6645 MVPP2_GMAC_CONFIG_FULL_DUPLEX, val);
6646 }
6647
6648 /* We can always update the flow control enable bits;
6649 * these will only be effective if flow control AN
6650 * (MVPP2_GMAC_FLOW_CTRL_AUTONEG) is disabled.
6651 */
6652 val = 0;
6653 if (tx_pause)
6654 val |= MVPP22_CTRL4_TX_FC_EN;
6655 if (rx_pause)
6656 val |= MVPP22_CTRL4_RX_FC_EN;
6657
6658 mvpp2_modify(port->base + MVPP22_GMAC_CTRL_4_REG,
6659 MVPP22_CTRL4_RX_FC_EN | MVPP22_CTRL4_TX_FC_EN,
6660 val);
6661 }
6662
6663 if (port->priv->global_tx_fc) {
6664 port->tx_fc = tx_pause;
6665 if (tx_pause)
6666 mvpp2_rxq_enable_fc(port);
6667 else
6668 mvpp2_rxq_disable_fc(port);
6669 if (port->priv->percpu_pools) {
6670 for (i = 0; i < port->nrxqs; i++)
6671 mvpp2_bm_pool_update_fc(port, &port->priv->bm_pools[i], tx_pause);
6672 } else {
6673 mvpp2_bm_pool_update_fc(port, port->pool_long, tx_pause);
6674 mvpp2_bm_pool_update_fc(port, port->pool_short, tx_pause);
6675 }
6676 if (port->priv->hw_version == MVPP23)
6677 mvpp23_rx_fifo_fc_en(port->priv, port->id, tx_pause);
6678 }
6679
6680 mvpp2_port_enable(port);
6681
6682 mvpp2_egress_enable(port);
6683 mvpp2_ingress_enable(port);
6684 netif_tx_wake_all_queues(port->dev);
6685 }
6686
mvpp2_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6687 static void mvpp2_mac_link_down(struct phylink_config *config,
6688 unsigned int mode, phy_interface_t interface)
6689 {
6690 struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6691 u32 val;
6692
6693 if (!phylink_autoneg_inband(mode)) {
6694 if (mvpp2_is_xlg(interface)) {
6695 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6696 val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6697 val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
6698 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
6699 } else {
6700 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6701 val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
6702 val |= MVPP2_GMAC_FORCE_LINK_DOWN;
6703 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6704 }
6705 }
6706
6707 netif_tx_stop_all_queues(port->dev);
6708 mvpp2_egress_disable(port);
6709 mvpp2_ingress_disable(port);
6710
6711 mvpp2_port_disable(port);
6712 }
6713
6714 static const struct phylink_mac_ops mvpp2_phylink_ops = {
6715 .validate = mvpp2_phylink_validate,
6716 .mac_prepare = mvpp2_mac_prepare,
6717 .mac_config = mvpp2_mac_config,
6718 .mac_finish = mvpp2_mac_finish,
6719 .mac_link_up = mvpp2_mac_link_up,
6720 .mac_link_down = mvpp2_mac_link_down,
6721 };
6722
6723 /* Work-around for ACPI */
mvpp2_acpi_start(struct mvpp2_port * port)6724 static void mvpp2_acpi_start(struct mvpp2_port *port)
6725 {
6726 /* Phylink isn't used as of now for ACPI, so the MAC has to be
6727 * configured manually when the interface is started. This will
6728 * be removed as soon as the phylink ACPI support lands in.
6729 */
6730 struct phylink_link_state state = {
6731 .interface = port->phy_interface,
6732 };
6733 mvpp2__mac_prepare(&port->phylink_config, MLO_AN_INBAND,
6734 port->phy_interface);
6735 mvpp2_mac_config(&port->phylink_config, MLO_AN_INBAND, &state);
6736 port->phylink_pcs.ops->pcs_config(&port->phylink_pcs, MLO_AN_INBAND,
6737 port->phy_interface,
6738 state.advertising, false);
6739 mvpp2_mac_finish(&port->phylink_config, MLO_AN_INBAND,
6740 port->phy_interface);
6741 mvpp2_mac_link_up(&port->phylink_config, NULL,
6742 MLO_AN_INBAND, port->phy_interface,
6743 SPEED_UNKNOWN, DUPLEX_UNKNOWN, false, false);
6744 }
6745
6746 /* In order to ensure backward compatibility for ACPI, check if the port
6747 * firmware node comprises the necessary description allowing to use phylink.
6748 */
mvpp2_use_acpi_compat_mode(struct fwnode_handle * port_fwnode)6749 static bool mvpp2_use_acpi_compat_mode(struct fwnode_handle *port_fwnode)
6750 {
6751 if (!is_acpi_node(port_fwnode))
6752 return false;
6753
6754 return (!fwnode_property_present(port_fwnode, "phy-handle") &&
6755 !fwnode_property_present(port_fwnode, "managed") &&
6756 !fwnode_get_named_child_node(port_fwnode, "fixed-link"));
6757 }
6758
6759 /* Ports initialization */
mvpp2_port_probe(struct platform_device * pdev,struct fwnode_handle * port_fwnode,struct mvpp2 * priv)6760 static int mvpp2_port_probe(struct platform_device *pdev,
6761 struct fwnode_handle *port_fwnode,
6762 struct mvpp2 *priv)
6763 {
6764 struct phy *comphy = NULL;
6765 struct mvpp2_port *port;
6766 struct mvpp2_port_pcpu *port_pcpu;
6767 struct device_node *port_node = to_of_node(port_fwnode);
6768 netdev_features_t features;
6769 struct net_device *dev;
6770 struct phylink *phylink;
6771 char *mac_from = "";
6772 unsigned int ntxqs, nrxqs, thread;
6773 unsigned long flags = 0;
6774 bool has_tx_irqs;
6775 u32 id;
6776 int phy_mode;
6777 int err, i;
6778
6779 has_tx_irqs = mvpp2_port_has_irqs(priv, port_node, &flags);
6780 if (!has_tx_irqs && queue_mode == MVPP2_QDIST_MULTI_MODE) {
6781 dev_err(&pdev->dev,
6782 "not enough IRQs to support multi queue mode\n");
6783 return -EINVAL;
6784 }
6785
6786 ntxqs = MVPP2_MAX_TXQ;
6787 nrxqs = mvpp2_get_nrxqs(priv);
6788
6789 dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
6790 if (!dev)
6791 return -ENOMEM;
6792
6793 phy_mode = fwnode_get_phy_mode(port_fwnode);
6794 if (phy_mode < 0) {
6795 dev_err(&pdev->dev, "incorrect phy mode\n");
6796 err = phy_mode;
6797 goto err_free_netdev;
6798 }
6799
6800 /*
6801 * Rewrite 10GBASE-KR to 10GBASE-R for compatibility with existing DT.
6802 * Existing usage of 10GBASE-KR is not correct; no backplane
6803 * negotiation is done, and this driver does not actually support
6804 * 10GBASE-KR.
6805 */
6806 if (phy_mode == PHY_INTERFACE_MODE_10GKR)
6807 phy_mode = PHY_INTERFACE_MODE_10GBASER;
6808
6809 if (port_node) {
6810 comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
6811 if (IS_ERR(comphy)) {
6812 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
6813 err = -EPROBE_DEFER;
6814 goto err_free_netdev;
6815 }
6816 comphy = NULL;
6817 }
6818 }
6819
6820 if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
6821 err = -EINVAL;
6822 dev_err(&pdev->dev, "missing port-id value\n");
6823 goto err_free_netdev;
6824 }
6825
6826 dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
6827 dev->watchdog_timeo = 5 * HZ;
6828 dev->netdev_ops = &mvpp2_netdev_ops;
6829 dev->ethtool_ops = &mvpp2_eth_tool_ops;
6830
6831 port = netdev_priv(dev);
6832 port->dev = dev;
6833 port->fwnode = port_fwnode;
6834 port->ntxqs = ntxqs;
6835 port->nrxqs = nrxqs;
6836 port->priv = priv;
6837 port->has_tx_irqs = has_tx_irqs;
6838 port->flags = flags;
6839
6840 err = mvpp2_queue_vectors_init(port, port_node);
6841 if (err)
6842 goto err_free_netdev;
6843
6844 if (port_node)
6845 port->port_irq = of_irq_get_byname(port_node, "link");
6846 else
6847 port->port_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
6848 if (port->port_irq == -EPROBE_DEFER) {
6849 err = -EPROBE_DEFER;
6850 goto err_deinit_qvecs;
6851 }
6852 if (port->port_irq <= 0)
6853 /* the link irq is optional */
6854 port->port_irq = 0;
6855
6856 if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
6857 port->flags |= MVPP2_F_LOOPBACK;
6858
6859 port->id = id;
6860 if (priv->hw_version == MVPP21)
6861 port->first_rxq = port->id * port->nrxqs;
6862 else
6863 port->first_rxq = port->id * priv->max_port_rxqs;
6864
6865 port->of_node = port_node;
6866 port->phy_interface = phy_mode;
6867 port->comphy = comphy;
6868
6869 if (priv->hw_version == MVPP21) {
6870 port->base = devm_platform_ioremap_resource(pdev, 2 + id);
6871 if (IS_ERR(port->base)) {
6872 err = PTR_ERR(port->base);
6873 goto err_free_irq;
6874 }
6875
6876 port->stats_base = port->priv->lms_base +
6877 MVPP21_MIB_COUNTERS_OFFSET +
6878 port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
6879 } else {
6880 if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
6881 &port->gop_id)) {
6882 err = -EINVAL;
6883 dev_err(&pdev->dev, "missing gop-port-id value\n");
6884 goto err_deinit_qvecs;
6885 }
6886
6887 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
6888 port->stats_base = port->priv->iface_base +
6889 MVPP22_MIB_COUNTERS_OFFSET +
6890 port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
6891
6892 /* We may want a property to describe whether we should use
6893 * MAC hardware timestamping.
6894 */
6895 if (priv->tai)
6896 port->hwtstamp = true;
6897 }
6898
6899 /* Alloc per-cpu and ethtool stats */
6900 port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6901 if (!port->stats) {
6902 err = -ENOMEM;
6903 goto err_free_irq;
6904 }
6905
6906 port->ethtool_stats = devm_kcalloc(&pdev->dev,
6907 MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs),
6908 sizeof(u64), GFP_KERNEL);
6909 if (!port->ethtool_stats) {
6910 err = -ENOMEM;
6911 goto err_free_stats;
6912 }
6913
6914 mutex_init(&port->gather_stats_lock);
6915 INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
6916
6917 mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
6918
6919 port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
6920 port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
6921 SET_NETDEV_DEV(dev, &pdev->dev);
6922
6923 err = mvpp2_port_init(port);
6924 if (err < 0) {
6925 dev_err(&pdev->dev, "failed to init port %d\n", id);
6926 goto err_free_stats;
6927 }
6928
6929 mvpp2_port_periodic_xon_disable(port);
6930
6931 mvpp2_mac_reset_assert(port);
6932 mvpp22_pcs_reset_assert(port);
6933
6934 port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6935 if (!port->pcpu) {
6936 err = -ENOMEM;
6937 goto err_free_txq_pcpu;
6938 }
6939
6940 if (!port->has_tx_irqs) {
6941 for (thread = 0; thread < priv->nthreads; thread++) {
6942 port_pcpu = per_cpu_ptr(port->pcpu, thread);
6943
6944 hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6945 HRTIMER_MODE_REL_PINNED_SOFT);
6946 port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6947 port_pcpu->timer_scheduled = false;
6948 port_pcpu->dev = dev;
6949 }
6950 }
6951
6952 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
6953 NETIF_F_TSO;
6954 dev->features = features | NETIF_F_RXCSUM;
6955 dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
6956 NETIF_F_HW_VLAN_CTAG_FILTER;
6957
6958 if (mvpp22_rss_is_supported(port)) {
6959 dev->hw_features |= NETIF_F_RXHASH;
6960 dev->features |= NETIF_F_NTUPLE;
6961 }
6962
6963 if (!port->priv->percpu_pools)
6964 mvpp2_set_hw_csum(port, port->pool_long->id);
6965
6966 dev->vlan_features |= features;
6967 dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
6968 dev->priv_flags |= IFF_UNICAST_FLT;
6969
6970 /* MTU range: 68 - 9704 */
6971 dev->min_mtu = ETH_MIN_MTU;
6972 /* 9704 == 9728 - 20 and rounding to 8 */
6973 dev->max_mtu = MVPP2_BM_JUMBO_PKT_SIZE;
6974 dev->dev.of_node = port_node;
6975
6976 if (!mvpp2_use_acpi_compat_mode(port_fwnode)) {
6977 port->phylink_config.dev = &dev->dev;
6978 port->phylink_config.type = PHYLINK_NETDEV;
6979
6980 phylink = phylink_create(&port->phylink_config, port_fwnode,
6981 phy_mode, &mvpp2_phylink_ops);
6982 if (IS_ERR(phylink)) {
6983 err = PTR_ERR(phylink);
6984 goto err_free_port_pcpu;
6985 }
6986 port->phylink = phylink;
6987 } else {
6988 dev_warn(&pdev->dev, "Use link irqs for port#%d. FW update required\n", port->id);
6989 port->phylink = NULL;
6990 }
6991
6992 /* Cycle the comphy to power it down, saving 270mW per port -
6993 * don't worry about an error powering it up. When the comphy
6994 * driver does this, we can remove this code.
6995 */
6996 if (port->comphy) {
6997 err = mvpp22_comphy_init(port, port->phy_interface);
6998 if (err == 0)
6999 phy_power_off(port->comphy);
7000 }
7001
7002 err = register_netdev(dev);
7003 if (err < 0) {
7004 dev_err(&pdev->dev, "failed to register netdev\n");
7005 goto err_phylink;
7006 }
7007 netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7008
7009 priv->port_list[priv->port_count++] = port;
7010
7011 return 0;
7012
7013 err_phylink:
7014 if (port->phylink)
7015 phylink_destroy(port->phylink);
7016 err_free_port_pcpu:
7017 free_percpu(port->pcpu);
7018 err_free_txq_pcpu:
7019 for (i = 0; i < port->ntxqs; i++)
7020 free_percpu(port->txqs[i]->pcpu);
7021 err_free_stats:
7022 free_percpu(port->stats);
7023 err_free_irq:
7024 if (port->port_irq)
7025 irq_dispose_mapping(port->port_irq);
7026 err_deinit_qvecs:
7027 mvpp2_queue_vectors_deinit(port);
7028 err_free_netdev:
7029 free_netdev(dev);
7030 return err;
7031 }
7032
7033 /* Ports removal routine */
mvpp2_port_remove(struct mvpp2_port * port)7034 static void mvpp2_port_remove(struct mvpp2_port *port)
7035 {
7036 int i;
7037
7038 unregister_netdev(port->dev);
7039 if (port->phylink)
7040 phylink_destroy(port->phylink);
7041 free_percpu(port->pcpu);
7042 free_percpu(port->stats);
7043 for (i = 0; i < port->ntxqs; i++)
7044 free_percpu(port->txqs[i]->pcpu);
7045 mvpp2_queue_vectors_deinit(port);
7046 if (port->port_irq)
7047 irq_dispose_mapping(port->port_irq);
7048 free_netdev(port->dev);
7049 }
7050
7051 /* Initialize decoding windows */
mvpp2_conf_mbus_windows(const struct mbus_dram_target_info * dram,struct mvpp2 * priv)7052 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7053 struct mvpp2 *priv)
7054 {
7055 u32 win_enable;
7056 int i;
7057
7058 for (i = 0; i < 6; i++) {
7059 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7060 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7061
7062 if (i < 4)
7063 mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
7064 }
7065
7066 win_enable = 0;
7067
7068 for (i = 0; i < dram->num_cs; i++) {
7069 const struct mbus_dram_window *cs = dram->cs + i;
7070
7071 mvpp2_write(priv, MVPP2_WIN_BASE(i),
7072 (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
7073 dram->mbus_dram_target_id);
7074
7075 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
7076 (cs->size - 1) & 0xffff0000);
7077
7078 win_enable |= (1 << i);
7079 }
7080
7081 mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
7082 }
7083
7084 /* Initialize Rx FIFO's */
mvpp2_rx_fifo_init(struct mvpp2 * priv)7085 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
7086 {
7087 int port;
7088
7089 for (port = 0; port < MVPP2_MAX_PORTS; port++) {
7090 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
7091 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7092 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
7093 MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
7094 }
7095
7096 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7097 MVPP2_RX_FIFO_PORT_MIN_PKT);
7098 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7099 }
7100
mvpp22_rx_fifo_set_hw(struct mvpp2 * priv,int port,int data_size)7101 static void mvpp22_rx_fifo_set_hw(struct mvpp2 *priv, int port, int data_size)
7102 {
7103 int attr_size = MVPP2_RX_FIFO_PORT_ATTR_SIZE(data_size);
7104
7105 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port), data_size);
7106 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port), attr_size);
7107 }
7108
7109 /* Initialize TX FIFO's: the total FIFO size is 48kB on PPv2.2 and PPv2.3.
7110 * 4kB fixed space must be assigned for the loopback port.
7111 * Redistribute remaining avialable 44kB space among all active ports.
7112 * Guarantee minimum 32kB for 10G port and 8kB for port 1, capable of 2.5G
7113 * SGMII link.
7114 */
mvpp22_rx_fifo_init(struct mvpp2 * priv)7115 static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
7116 {
7117 int remaining_ports_count;
7118 unsigned long port_map;
7119 int size_remainder;
7120 int port, size;
7121
7122 /* The loopback requires fixed 4kB of the FIFO space assignment. */
7123 mvpp22_rx_fifo_set_hw(priv, MVPP2_LOOPBACK_PORT_INDEX,
7124 MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
7125 port_map = priv->port_map & ~BIT(MVPP2_LOOPBACK_PORT_INDEX);
7126
7127 /* Set RX FIFO size to 0 for inactive ports. */
7128 for_each_clear_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX)
7129 mvpp22_rx_fifo_set_hw(priv, port, 0);
7130
7131 /* Assign remaining RX FIFO space among all active ports. */
7132 size_remainder = MVPP2_RX_FIFO_PORT_DATA_SIZE_44KB;
7133 remaining_ports_count = hweight_long(port_map);
7134
7135 for_each_set_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX) {
7136 if (remaining_ports_count == 1)
7137 size = size_remainder;
7138 else if (port == 0)
7139 size = max(size_remainder / remaining_ports_count,
7140 MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
7141 else if (port == 1)
7142 size = max(size_remainder / remaining_ports_count,
7143 MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
7144 else
7145 size = size_remainder / remaining_ports_count;
7146
7147 size_remainder -= size;
7148 remaining_ports_count--;
7149
7150 mvpp22_rx_fifo_set_hw(priv, port, size);
7151 }
7152
7153 mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
7154 MVPP2_RX_FIFO_PORT_MIN_PKT);
7155 mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
7156 }
7157
7158 /* Configure Rx FIFO Flow control thresholds */
mvpp23_rx_fifo_fc_set_tresh(struct mvpp2 * priv)7159 static void mvpp23_rx_fifo_fc_set_tresh(struct mvpp2 *priv)
7160 {
7161 int port, val;
7162
7163 /* Port 0: maximum speed -10Gb/s port
7164 * required by spec RX FIFO threshold 9KB
7165 * Port 1: maximum speed -5Gb/s port
7166 * required by spec RX FIFO threshold 4KB
7167 * Port 2: maximum speed -1Gb/s port
7168 * required by spec RX FIFO threshold 2KB
7169 */
7170
7171 /* Without loopback port */
7172 for (port = 0; port < (MVPP2_MAX_PORTS - 1); port++) {
7173 if (port == 0) {
7174 val = (MVPP23_PORT0_FIFO_TRSH / MVPP2_RX_FC_TRSH_UNIT)
7175 << MVPP2_RX_FC_TRSH_OFFS;
7176 val &= MVPP2_RX_FC_TRSH_MASK;
7177 mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7178 } else if (port == 1) {
7179 val = (MVPP23_PORT1_FIFO_TRSH / MVPP2_RX_FC_TRSH_UNIT)
7180 << MVPP2_RX_FC_TRSH_OFFS;
7181 val &= MVPP2_RX_FC_TRSH_MASK;
7182 mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7183 } else {
7184 val = (MVPP23_PORT2_FIFO_TRSH / MVPP2_RX_FC_TRSH_UNIT)
7185 << MVPP2_RX_FC_TRSH_OFFS;
7186 val &= MVPP2_RX_FC_TRSH_MASK;
7187 mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7188 }
7189 }
7190 }
7191
7192 /* Configure Rx FIFO Flow control thresholds */
mvpp23_rx_fifo_fc_en(struct mvpp2 * priv,int port,bool en)7193 void mvpp23_rx_fifo_fc_en(struct mvpp2 *priv, int port, bool en)
7194 {
7195 int val;
7196
7197 val = mvpp2_read(priv, MVPP2_RX_FC_REG(port));
7198
7199 if (en)
7200 val |= MVPP2_RX_FC_EN;
7201 else
7202 val &= ~MVPP2_RX_FC_EN;
7203
7204 mvpp2_write(priv, MVPP2_RX_FC_REG(port), val);
7205 }
7206
mvpp22_tx_fifo_set_hw(struct mvpp2 * priv,int port,int size)7207 static void mvpp22_tx_fifo_set_hw(struct mvpp2 *priv, int port, int size)
7208 {
7209 int threshold = MVPP2_TX_FIFO_THRESHOLD(size);
7210
7211 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
7212 mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), threshold);
7213 }
7214
7215 /* Initialize TX FIFO's: the total FIFO size is 19kB on PPv2.2 and PPv2.3.
7216 * 1kB fixed space must be assigned for the loopback port.
7217 * Redistribute remaining avialable 18kB space among all active ports.
7218 * The 10G interface should use 10kB (which is maximum possible size
7219 * per single port).
7220 */
mvpp22_tx_fifo_init(struct mvpp2 * priv)7221 static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
7222 {
7223 int remaining_ports_count;
7224 unsigned long port_map;
7225 int size_remainder;
7226 int port, size;
7227
7228 /* The loopback requires fixed 1kB of the FIFO space assignment. */
7229 mvpp22_tx_fifo_set_hw(priv, MVPP2_LOOPBACK_PORT_INDEX,
7230 MVPP22_TX_FIFO_DATA_SIZE_1KB);
7231 port_map = priv->port_map & ~BIT(MVPP2_LOOPBACK_PORT_INDEX);
7232
7233 /* Set TX FIFO size to 0 for inactive ports. */
7234 for_each_clear_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX)
7235 mvpp22_tx_fifo_set_hw(priv, port, 0);
7236
7237 /* Assign remaining TX FIFO space among all active ports. */
7238 size_remainder = MVPP22_TX_FIFO_DATA_SIZE_18KB;
7239 remaining_ports_count = hweight_long(port_map);
7240
7241 for_each_set_bit(port, &port_map, MVPP2_LOOPBACK_PORT_INDEX) {
7242 if (remaining_ports_count == 1)
7243 size = min(size_remainder,
7244 MVPP22_TX_FIFO_DATA_SIZE_10KB);
7245 else if (port == 0)
7246 size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
7247 else
7248 size = size_remainder / remaining_ports_count;
7249
7250 size_remainder -= size;
7251 remaining_ports_count--;
7252
7253 mvpp22_tx_fifo_set_hw(priv, port, size);
7254 }
7255 }
7256
mvpp2_axi_init(struct mvpp2 * priv)7257 static void mvpp2_axi_init(struct mvpp2 *priv)
7258 {
7259 u32 val, rdval, wrval;
7260
7261 mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
7262
7263 /* AXI Bridge Configuration */
7264
7265 rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
7266 << MVPP22_AXI_ATTR_CACHE_OFFS;
7267 rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7268 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7269
7270 wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
7271 << MVPP22_AXI_ATTR_CACHE_OFFS;
7272 wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7273 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
7274
7275 /* BM */
7276 mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
7277 mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
7278
7279 /* Descriptors */
7280 mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
7281 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
7282 mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
7283 mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
7284
7285 /* Buffer Data */
7286 mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
7287 mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
7288
7289 val = MVPP22_AXI_CODE_CACHE_NON_CACHE
7290 << MVPP22_AXI_CODE_CACHE_OFFS;
7291 val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
7292 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7293 mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
7294 mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
7295
7296 val = MVPP22_AXI_CODE_CACHE_RD_CACHE
7297 << MVPP22_AXI_CODE_CACHE_OFFS;
7298 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7299 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7300
7301 mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
7302
7303 val = MVPP22_AXI_CODE_CACHE_WR_CACHE
7304 << MVPP22_AXI_CODE_CACHE_OFFS;
7305 val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
7306 << MVPP22_AXI_CODE_DOMAIN_OFFS;
7307
7308 mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
7309 }
7310
7311 /* Initialize network controller common part HW */
mvpp2_init(struct platform_device * pdev,struct mvpp2 * priv)7312 static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
7313 {
7314 const struct mbus_dram_target_info *dram_target_info;
7315 int err, i;
7316 u32 val;
7317
7318 /* MBUS windows configuration */
7319 dram_target_info = mv_mbus_dram_info();
7320 if (dram_target_info)
7321 mvpp2_conf_mbus_windows(dram_target_info, priv);
7322
7323 if (priv->hw_version >= MVPP22)
7324 mvpp2_axi_init(priv);
7325
7326 /* Disable HW PHY polling */
7327 if (priv->hw_version == MVPP21) {
7328 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7329 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
7330 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
7331 } else {
7332 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7333 val &= ~MVPP22_SMI_POLLING_EN;
7334 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
7335 }
7336
7337 /* Allocate and initialize aggregated TXQs */
7338 priv->aggr_txqs = devm_kcalloc(&pdev->dev, MVPP2_MAX_THREADS,
7339 sizeof(*priv->aggr_txqs),
7340 GFP_KERNEL);
7341 if (!priv->aggr_txqs)
7342 return -ENOMEM;
7343
7344 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7345 priv->aggr_txqs[i].id = i;
7346 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
7347 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
7348 if (err < 0)
7349 return err;
7350 }
7351
7352 /* Fifo Init */
7353 if (priv->hw_version == MVPP21) {
7354 mvpp2_rx_fifo_init(priv);
7355 } else {
7356 mvpp22_rx_fifo_init(priv);
7357 mvpp22_tx_fifo_init(priv);
7358 if (priv->hw_version == MVPP23)
7359 mvpp23_rx_fifo_fc_set_tresh(priv);
7360 }
7361
7362 if (priv->hw_version == MVPP21)
7363 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
7364 priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
7365
7366 /* Allow cache snoop when transmiting packets */
7367 mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
7368
7369 /* Buffer Manager initialization */
7370 err = mvpp2_bm_init(&pdev->dev, priv);
7371 if (err < 0)
7372 return err;
7373
7374 /* Parser default initialization */
7375 err = mvpp2_prs_default_init(pdev, priv);
7376 if (err < 0)
7377 return err;
7378
7379 /* Classifier default initialization */
7380 mvpp2_cls_init(priv);
7381
7382 return 0;
7383 }
7384
mvpp2_get_sram(struct platform_device * pdev,struct mvpp2 * priv)7385 static int mvpp2_get_sram(struct platform_device *pdev,
7386 struct mvpp2 *priv)
7387 {
7388 struct resource *res;
7389 void __iomem *base;
7390
7391 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
7392 if (!res) {
7393 if (has_acpi_companion(&pdev->dev))
7394 dev_warn(&pdev->dev, "ACPI is too old, Flow control not supported\n");
7395 else
7396 dev_warn(&pdev->dev, "DT is too old, Flow control not supported\n");
7397 return 0;
7398 }
7399
7400 base = devm_ioremap_resource(&pdev->dev, res);
7401 if (IS_ERR(base))
7402 return PTR_ERR(base);
7403
7404 priv->cm3_base = base;
7405 return 0;
7406 }
7407
mvpp2_probe(struct platform_device * pdev)7408 static int mvpp2_probe(struct platform_device *pdev)
7409 {
7410 struct fwnode_handle *fwnode = pdev->dev.fwnode;
7411 struct fwnode_handle *port_fwnode;
7412 struct mvpp2 *priv;
7413 struct resource *res;
7414 void __iomem *base;
7415 int i, shared;
7416 int err;
7417
7418 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
7419 if (!priv)
7420 return -ENOMEM;
7421
7422 priv->hw_version = (unsigned long)device_get_match_data(&pdev->dev);
7423
7424 /* multi queue mode isn't supported on PPV2.1, fallback to single
7425 * mode
7426 */
7427 if (priv->hw_version == MVPP21)
7428 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7429
7430 base = devm_platform_ioremap_resource(pdev, 0);
7431 if (IS_ERR(base))
7432 return PTR_ERR(base);
7433
7434 if (priv->hw_version == MVPP21) {
7435 priv->lms_base = devm_platform_ioremap_resource(pdev, 1);
7436 if (IS_ERR(priv->lms_base))
7437 return PTR_ERR(priv->lms_base);
7438 } else {
7439 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
7440 if (!res) {
7441 dev_err(&pdev->dev, "Invalid resource\n");
7442 return -EINVAL;
7443 }
7444 if (has_acpi_companion(&pdev->dev)) {
7445 /* In case the MDIO memory region is declared in
7446 * the ACPI, it can already appear as 'in-use'
7447 * in the OS. Because it is overlapped by second
7448 * region of the network controller, make
7449 * sure it is released, before requesting it again.
7450 * The care is taken by mvpp2 driver to avoid
7451 * concurrent access to this memory region.
7452 */
7453 release_resource(res);
7454 }
7455 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
7456 if (IS_ERR(priv->iface_base))
7457 return PTR_ERR(priv->iface_base);
7458
7459 /* Map CM3 SRAM */
7460 err = mvpp2_get_sram(pdev, priv);
7461 if (err)
7462 dev_warn(&pdev->dev, "Fail to alloc CM3 SRAM\n");
7463
7464 /* Enable global Flow Control only if handler to SRAM not NULL */
7465 if (priv->cm3_base)
7466 priv->global_tx_fc = true;
7467 }
7468
7469 if (priv->hw_version >= MVPP22 && dev_of_node(&pdev->dev)) {
7470 priv->sysctrl_base =
7471 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
7472 "marvell,system-controller");
7473 if (IS_ERR(priv->sysctrl_base))
7474 /* The system controller regmap is optional for dt
7475 * compatibility reasons. When not provided, the
7476 * configuration of the GoP relies on the
7477 * firmware/bootloader.
7478 */
7479 priv->sysctrl_base = NULL;
7480 }
7481
7482 if (priv->hw_version >= MVPP22 &&
7483 mvpp2_get_nrxqs(priv) * 2 <= MVPP2_BM_MAX_POOLS)
7484 priv->percpu_pools = 1;
7485
7486 mvpp2_setup_bm_pool();
7487
7488
7489 priv->nthreads = min_t(unsigned int, num_present_cpus(),
7490 MVPP2_MAX_THREADS);
7491
7492 shared = num_present_cpus() - priv->nthreads;
7493 if (shared > 0)
7494 bitmap_set(&priv->lock_map, 0,
7495 min_t(int, shared, MVPP2_MAX_THREADS));
7496
7497 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7498 u32 addr_space_sz;
7499
7500 addr_space_sz = (priv->hw_version == MVPP21 ?
7501 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
7502 priv->swth_base[i] = base + i * addr_space_sz;
7503 }
7504
7505 if (priv->hw_version == MVPP21)
7506 priv->max_port_rxqs = 8;
7507 else
7508 priv->max_port_rxqs = 32;
7509
7510 if (dev_of_node(&pdev->dev)) {
7511 priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
7512 if (IS_ERR(priv->pp_clk))
7513 return PTR_ERR(priv->pp_clk);
7514 err = clk_prepare_enable(priv->pp_clk);
7515 if (err < 0)
7516 return err;
7517
7518 priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
7519 if (IS_ERR(priv->gop_clk)) {
7520 err = PTR_ERR(priv->gop_clk);
7521 goto err_pp_clk;
7522 }
7523 err = clk_prepare_enable(priv->gop_clk);
7524 if (err < 0)
7525 goto err_pp_clk;
7526
7527 if (priv->hw_version >= MVPP22) {
7528 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
7529 if (IS_ERR(priv->mg_clk)) {
7530 err = PTR_ERR(priv->mg_clk);
7531 goto err_gop_clk;
7532 }
7533
7534 err = clk_prepare_enable(priv->mg_clk);
7535 if (err < 0)
7536 goto err_gop_clk;
7537
7538 priv->mg_core_clk = devm_clk_get_optional(&pdev->dev, "mg_core_clk");
7539 if (IS_ERR(priv->mg_core_clk)) {
7540 err = PTR_ERR(priv->mg_core_clk);
7541 goto err_mg_clk;
7542 }
7543
7544 err = clk_prepare_enable(priv->mg_core_clk);
7545 if (err < 0)
7546 goto err_mg_clk;
7547 }
7548
7549 priv->axi_clk = devm_clk_get_optional(&pdev->dev, "axi_clk");
7550 if (IS_ERR(priv->axi_clk)) {
7551 err = PTR_ERR(priv->axi_clk);
7552 goto err_mg_core_clk;
7553 }
7554
7555 err = clk_prepare_enable(priv->axi_clk);
7556 if (err < 0)
7557 goto err_mg_core_clk;
7558
7559 /* Get system's tclk rate */
7560 priv->tclk = clk_get_rate(priv->pp_clk);
7561 } else {
7562 err = device_property_read_u32(&pdev->dev, "clock-frequency", &priv->tclk);
7563 if (err) {
7564 dev_err(&pdev->dev, "missing clock-frequency value\n");
7565 return err;
7566 }
7567 }
7568
7569 if (priv->hw_version >= MVPP22) {
7570 err = dma_set_mask(&pdev->dev, MVPP2_DESC_DMA_MASK);
7571 if (err)
7572 goto err_axi_clk;
7573 /* Sadly, the BM pools all share the same register to
7574 * store the high 32 bits of their address. So they
7575 * must all have the same high 32 bits, which forces
7576 * us to restrict coherent memory to DMA_BIT_MASK(32).
7577 */
7578 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7579 if (err)
7580 goto err_axi_clk;
7581 }
7582
7583 /* Map DTS-active ports. Should be done before FIFO mvpp2_init */
7584 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7585 if (!fwnode_property_read_u32(port_fwnode, "port-id", &i))
7586 priv->port_map |= BIT(i);
7587 }
7588
7589 if (mvpp2_read(priv, MVPP2_VER_ID_REG) == MVPP2_VER_PP23)
7590 priv->hw_version = MVPP23;
7591
7592 /* Init mss lock */
7593 spin_lock_init(&priv->mss_spinlock);
7594
7595 /* Initialize network controller */
7596 err = mvpp2_init(pdev, priv);
7597 if (err < 0) {
7598 dev_err(&pdev->dev, "failed to initialize controller\n");
7599 goto err_axi_clk;
7600 }
7601
7602 err = mvpp22_tai_probe(&pdev->dev, priv);
7603 if (err < 0)
7604 goto err_axi_clk;
7605
7606 /* Initialize ports */
7607 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7608 err = mvpp2_port_probe(pdev, port_fwnode, priv);
7609 if (err < 0)
7610 goto err_port_probe;
7611 }
7612
7613 if (priv->port_count == 0) {
7614 dev_err(&pdev->dev, "no ports enabled\n");
7615 err = -ENODEV;
7616 goto err_axi_clk;
7617 }
7618
7619 /* Statistics must be gathered regularly because some of them (like
7620 * packets counters) are 32-bit registers and could overflow quite
7621 * quickly. For instance, a 10Gb link used at full bandwidth with the
7622 * smallest packets (64B) will overflow a 32-bit counter in less than
7623 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
7624 */
7625 snprintf(priv->queue_name, sizeof(priv->queue_name),
7626 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
7627 priv->port_count > 1 ? "+" : "");
7628 priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
7629 if (!priv->stats_queue) {
7630 err = -ENOMEM;
7631 goto err_port_probe;
7632 }
7633
7634 if (priv->global_tx_fc && priv->hw_version >= MVPP22) {
7635 err = mvpp2_enable_global_fc(priv);
7636 if (err)
7637 dev_warn(&pdev->dev, "Minimum of CM3 firmware 18.09 and chip revision B0 required for flow control\n");
7638 }
7639
7640 mvpp2_dbgfs_init(priv, pdev->name);
7641
7642 platform_set_drvdata(pdev, priv);
7643 return 0;
7644
7645 err_port_probe:
7646 fwnode_handle_put(port_fwnode);
7647
7648 i = 0;
7649 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7650 if (priv->port_list[i])
7651 mvpp2_port_remove(priv->port_list[i]);
7652 i++;
7653 }
7654 err_axi_clk:
7655 clk_disable_unprepare(priv->axi_clk);
7656 err_mg_core_clk:
7657 clk_disable_unprepare(priv->mg_core_clk);
7658 err_mg_clk:
7659 clk_disable_unprepare(priv->mg_clk);
7660 err_gop_clk:
7661 clk_disable_unprepare(priv->gop_clk);
7662 err_pp_clk:
7663 clk_disable_unprepare(priv->pp_clk);
7664 return err;
7665 }
7666
mvpp2_remove(struct platform_device * pdev)7667 static int mvpp2_remove(struct platform_device *pdev)
7668 {
7669 struct mvpp2 *priv = platform_get_drvdata(pdev);
7670 struct fwnode_handle *fwnode = pdev->dev.fwnode;
7671 int i = 0, poolnum = MVPP2_BM_POOLS_NUM;
7672 struct fwnode_handle *port_fwnode;
7673
7674 mvpp2_dbgfs_cleanup(priv);
7675
7676 fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7677 if (priv->port_list[i]) {
7678 mutex_destroy(&priv->port_list[i]->gather_stats_lock);
7679 mvpp2_port_remove(priv->port_list[i]);
7680 }
7681 i++;
7682 }
7683
7684 destroy_workqueue(priv->stats_queue);
7685
7686 if (priv->percpu_pools)
7687 poolnum = mvpp2_get_nrxqs(priv) * 2;
7688
7689 for (i = 0; i < poolnum; i++) {
7690 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7691
7692 mvpp2_bm_pool_destroy(&pdev->dev, priv, bm_pool);
7693 }
7694
7695 for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7696 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7697
7698 dma_free_coherent(&pdev->dev,
7699 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7700 aggr_txq->descs,
7701 aggr_txq->descs_dma);
7702 }
7703
7704 if (is_acpi_node(port_fwnode))
7705 return 0;
7706
7707 clk_disable_unprepare(priv->axi_clk);
7708 clk_disable_unprepare(priv->mg_core_clk);
7709 clk_disable_unprepare(priv->mg_clk);
7710 clk_disable_unprepare(priv->pp_clk);
7711 clk_disable_unprepare(priv->gop_clk);
7712
7713 return 0;
7714 }
7715
7716 static const struct of_device_id mvpp2_match[] = {
7717 {
7718 .compatible = "marvell,armada-375-pp2",
7719 .data = (void *)MVPP21,
7720 },
7721 {
7722 .compatible = "marvell,armada-7k-pp22",
7723 .data = (void *)MVPP22,
7724 },
7725 { }
7726 };
7727 MODULE_DEVICE_TABLE(of, mvpp2_match);
7728
7729 #ifdef CONFIG_ACPI
7730 static const struct acpi_device_id mvpp2_acpi_match[] = {
7731 { "MRVL0110", MVPP22 },
7732 { },
7733 };
7734 MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
7735 #endif
7736
7737 static struct platform_driver mvpp2_driver = {
7738 .probe = mvpp2_probe,
7739 .remove = mvpp2_remove,
7740 .driver = {
7741 .name = MVPP2_DRIVER_NAME,
7742 .of_match_table = mvpp2_match,
7743 .acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
7744 },
7745 };
7746
mvpp2_driver_init(void)7747 static int __init mvpp2_driver_init(void)
7748 {
7749 return platform_driver_register(&mvpp2_driver);
7750 }
7751 module_init(mvpp2_driver_init);
7752
mvpp2_driver_exit(void)7753 static void __exit mvpp2_driver_exit(void)
7754 {
7755 platform_driver_unregister(&mvpp2_driver);
7756 mvpp2_dbgfs_exit();
7757 }
7758 module_exit(mvpp2_driver_exit);
7759
7760 MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7761 MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
7762 MODULE_LICENSE("GPL v2");
7763