net/octeontx2: support TSO offload
[dpdk.git] / drivers / net / octeontx2 / otx2_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <inttypes.h>
6
7 #include <rte_ethdev_pci.h>
8 #include <rte_io.h>
9 #include <rte_malloc.h>
10 #include <rte_mbuf.h>
11 #include <rte_mbuf_pool_ops.h>
12 #include <rte_mempool.h>
13
14 #include "otx2_ethdev.h"
15
16 static inline uint64_t
17 nix_get_rx_offload_capa(struct otx2_eth_dev *dev)
18 {
19         uint64_t capa = NIX_RX_OFFLOAD_CAPA;
20
21         if (otx2_dev_is_vf(dev))
22                 capa &= ~DEV_RX_OFFLOAD_TIMESTAMP;
23
24         return capa;
25 }
26
27 static inline uint64_t
28 nix_get_tx_offload_capa(struct otx2_eth_dev *dev)
29 {
30         uint64_t capa = NIX_TX_OFFLOAD_CAPA;
31
32         /* TSO not supported for earlier chip revisions */
33         if (otx2_dev_is_96xx_A0(dev) || otx2_dev_is_95xx_Ax(dev))
34                 capa &= ~(DEV_TX_OFFLOAD_TCP_TSO |
35                           DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
36                           DEV_TX_OFFLOAD_GENEVE_TNL_TSO);
37         return capa;
38 }
39
40 static const struct otx2_dev_ops otx2_dev_ops = {
41         .link_status_update = otx2_eth_dev_link_status_update,
42         .ptp_info_update = otx2_eth_dev_ptp_info_update
43 };
44
45 static int
46 nix_lf_alloc(struct otx2_eth_dev *dev, uint32_t nb_rxq, uint32_t nb_txq)
47 {
48         struct otx2_mbox *mbox = dev->mbox;
49         struct nix_lf_alloc_req *req;
50         struct nix_lf_alloc_rsp *rsp;
51         int rc;
52
53         req = otx2_mbox_alloc_msg_nix_lf_alloc(mbox);
54         req->rq_cnt = nb_rxq;
55         req->sq_cnt = nb_txq;
56         req->cq_cnt = nb_rxq;
57         /* XQE_SZ should be in Sync with NIX_CQ_ENTRY_SZ */
58         RTE_BUILD_BUG_ON(NIX_CQ_ENTRY_SZ != 128);
59         req->xqe_sz = NIX_XQESZ_W16;
60         req->rss_sz = dev->rss_info.rss_size;
61         req->rss_grps = NIX_RSS_GRPS;
62         req->npa_func = otx2_npa_pf_func_get();
63         req->sso_func = otx2_sso_pf_func_get();
64         req->rx_cfg = BIT_ULL(35 /* DIS_APAD */);
65         if (dev->rx_offloads & (DEV_RX_OFFLOAD_TCP_CKSUM |
66                          DEV_RX_OFFLOAD_UDP_CKSUM)) {
67                 req->rx_cfg |= BIT_ULL(37 /* CSUM_OL4 */);
68                 req->rx_cfg |= BIT_ULL(36 /* CSUM_IL4 */);
69         }
70         req->rx_cfg |= BIT_ULL(32 /* DROP_RE */);
71
72         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
73         if (rc)
74                 return rc;
75
76         dev->sqb_size = rsp->sqb_size;
77         dev->tx_chan_base = rsp->tx_chan_base;
78         dev->rx_chan_base = rsp->rx_chan_base;
79         dev->rx_chan_cnt = rsp->rx_chan_cnt;
80         dev->tx_chan_cnt = rsp->tx_chan_cnt;
81         dev->lso_tsov4_idx = rsp->lso_tsov4_idx;
82         dev->lso_tsov6_idx = rsp->lso_tsov6_idx;
83         dev->lf_tx_stats = rsp->lf_tx_stats;
84         dev->lf_rx_stats = rsp->lf_rx_stats;
85         dev->cints = rsp->cints;
86         dev->qints = rsp->qints;
87         dev->npc_flow.channel = dev->rx_chan_base;
88
89         return 0;
90 }
91
92 static int
93 nix_lf_free(struct otx2_eth_dev *dev)
94 {
95         struct otx2_mbox *mbox = dev->mbox;
96         struct nix_lf_free_req *req;
97         struct ndc_sync_op *ndc_req;
98         int rc;
99
100         /* Sync NDC-NIX for LF */
101         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
102         ndc_req->nix_lf_tx_sync = 1;
103         ndc_req->nix_lf_rx_sync = 1;
104         rc = otx2_mbox_process(mbox);
105         if (rc)
106                 otx2_err("Error on NDC-NIX-[TX, RX] LF sync, rc %d", rc);
107
108         req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
109         /* Let AF driver free all this nix lf's
110          * NPC entries allocated using NPC MBOX.
111          */
112         req->flags = 0;
113
114         return otx2_mbox_process(mbox);
115 }
116
117 int
118 otx2_cgx_rxtx_start(struct otx2_eth_dev *dev)
119 {
120         struct otx2_mbox *mbox = dev->mbox;
121
122         if (otx2_dev_is_vf(dev))
123                 return 0;
124
125         otx2_mbox_alloc_msg_cgx_start_rxtx(mbox);
126
127         return otx2_mbox_process(mbox);
128 }
129
130 int
131 otx2_cgx_rxtx_stop(struct otx2_eth_dev *dev)
132 {
133         struct otx2_mbox *mbox = dev->mbox;
134
135         if (otx2_dev_is_vf(dev))
136                 return 0;
137
138         otx2_mbox_alloc_msg_cgx_stop_rxtx(mbox);
139
140         return otx2_mbox_process(mbox);
141 }
142
143 static int
144 npc_rx_enable(struct otx2_eth_dev *dev)
145 {
146         struct otx2_mbox *mbox = dev->mbox;
147
148         otx2_mbox_alloc_msg_nix_lf_start_rx(mbox);
149
150         return otx2_mbox_process(mbox);
151 }
152
153 static int
154 npc_rx_disable(struct otx2_eth_dev *dev)
155 {
156         struct otx2_mbox *mbox = dev->mbox;
157
158         otx2_mbox_alloc_msg_nix_lf_stop_rx(mbox);
159
160         return otx2_mbox_process(mbox);
161 }
162
163 static int
164 nix_cgx_start_link_event(struct otx2_eth_dev *dev)
165 {
166         struct otx2_mbox *mbox = dev->mbox;
167
168         if (otx2_dev_is_vf(dev))
169                 return 0;
170
171         otx2_mbox_alloc_msg_cgx_start_linkevents(mbox);
172
173         return otx2_mbox_process(mbox);
174 }
175
176 static int
177 cgx_intlbk_enable(struct otx2_eth_dev *dev, bool en)
178 {
179         struct otx2_mbox *mbox = dev->mbox;
180
181         if (otx2_dev_is_vf(dev))
182                 return 0;
183
184         if (en)
185                 otx2_mbox_alloc_msg_cgx_intlbk_enable(mbox);
186         else
187                 otx2_mbox_alloc_msg_cgx_intlbk_disable(mbox);
188
189         return otx2_mbox_process(mbox);
190 }
191
192 static int
193 nix_cgx_stop_link_event(struct otx2_eth_dev *dev)
194 {
195         struct otx2_mbox *mbox = dev->mbox;
196
197         if (otx2_dev_is_vf(dev))
198                 return 0;
199
200         otx2_mbox_alloc_msg_cgx_stop_linkevents(mbox);
201
202         return otx2_mbox_process(mbox);
203 }
204
205 static inline void
206 nix_rx_queue_reset(struct otx2_eth_rxq *rxq)
207 {
208         rxq->head = 0;
209         rxq->available = 0;
210 }
211
212 static inline uint32_t
213 nix_qsize_to_val(enum nix_q_size_e qsize)
214 {
215         return (16UL << (qsize * 2));
216 }
217
218 static inline enum nix_q_size_e
219 nix_qsize_clampup_get(struct otx2_eth_dev *dev, uint32_t val)
220 {
221         int i;
222
223         if (otx2_ethdev_fixup_is_min_4k_q(dev))
224                 i = nix_q_size_4K;
225         else
226                 i = nix_q_size_16;
227
228         for (; i < nix_q_size_max; i++)
229                 if (val <= nix_qsize_to_val(i))
230                         break;
231
232         if (i >= nix_q_size_max)
233                 i = nix_q_size_max - 1;
234
235         return i;
236 }
237
238 static int
239 nix_cq_rq_init(struct rte_eth_dev *eth_dev, struct otx2_eth_dev *dev,
240                uint16_t qid, struct otx2_eth_rxq *rxq, struct rte_mempool *mp)
241 {
242         struct otx2_mbox *mbox = dev->mbox;
243         const struct rte_memzone *rz;
244         uint32_t ring_size, cq_size;
245         struct nix_aq_enq_req *aq;
246         uint16_t first_skip;
247         int rc;
248
249         cq_size = rxq->qlen;
250         ring_size = cq_size * NIX_CQ_ENTRY_SZ;
251         rz = rte_eth_dma_zone_reserve(eth_dev, "cq", qid, ring_size,
252                                       NIX_CQ_ALIGN, dev->node);
253         if (rz == NULL) {
254                 otx2_err("Failed to allocate mem for cq hw ring");
255                 rc = -ENOMEM;
256                 goto fail;
257         }
258         memset(rz->addr, 0, rz->len);
259         rxq->desc = (uintptr_t)rz->addr;
260         rxq->qmask = cq_size - 1;
261
262         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
263         aq->qidx = qid;
264         aq->ctype = NIX_AQ_CTYPE_CQ;
265         aq->op = NIX_AQ_INSTOP_INIT;
266
267         aq->cq.ena = 1;
268         aq->cq.caching = 1;
269         aq->cq.qsize = rxq->qsize;
270         aq->cq.base = rz->iova;
271         aq->cq.avg_level = 0xff;
272         aq->cq.cq_err_int_ena = BIT(NIX_CQERRINT_CQE_FAULT);
273         aq->cq.cq_err_int_ena |= BIT(NIX_CQERRINT_DOOR_ERR);
274
275         /* Many to one reduction */
276         aq->cq.qint_idx = qid % dev->qints;
277         /* Map CQ0 [RQ0] to CINT0 and so on till max 64 irqs */
278         aq->cq.cint_idx = qid;
279
280         if (otx2_ethdev_fixup_is_limit_cq_full(dev)) {
281                 const float rx_cq_skid = NIX_CQ_FULL_ERRATA_SKID;
282                 uint16_t min_rx_drop;
283
284                 min_rx_drop = ceil(rx_cq_skid / (float)cq_size);
285                 aq->cq.drop = min_rx_drop;
286                 aq->cq.drop_ena = 1;
287                 rxq->cq_drop = min_rx_drop;
288         } else {
289                 rxq->cq_drop = NIX_CQ_THRESH_LEVEL;
290                 aq->cq.drop = rxq->cq_drop;
291                 aq->cq.drop_ena = 1;
292         }
293
294         /* TX pause frames enable flowctrl on RX side */
295         if (dev->fc_info.tx_pause) {
296                 /* Single bpid is allocated for all rx channels for now */
297                 aq->cq.bpid = dev->fc_info.bpid[0];
298                 aq->cq.bp = rxq->cq_drop;
299                 aq->cq.bp_ena = 1;
300         }
301
302         rc = otx2_mbox_process(mbox);
303         if (rc) {
304                 otx2_err("Failed to init cq context");
305                 goto fail;
306         }
307
308         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
309         aq->qidx = qid;
310         aq->ctype = NIX_AQ_CTYPE_RQ;
311         aq->op = NIX_AQ_INSTOP_INIT;
312
313         aq->rq.sso_ena = 0;
314         aq->rq.cq = qid; /* RQ to CQ 1:1 mapped */
315         aq->rq.spb_ena = 0;
316         aq->rq.lpb_aura = npa_lf_aura_handle_to_aura(mp->pool_id);
317         first_skip = (sizeof(struct rte_mbuf));
318         first_skip += RTE_PKTMBUF_HEADROOM;
319         first_skip += rte_pktmbuf_priv_size(mp);
320         rxq->data_off = first_skip;
321
322         first_skip /= 8; /* Expressed in number of dwords */
323         aq->rq.first_skip = first_skip;
324         aq->rq.later_skip = (sizeof(struct rte_mbuf) / 8);
325         aq->rq.flow_tagw = 32; /* 32-bits */
326         aq->rq.lpb_sizem1 = rte_pktmbuf_data_room_size(mp);
327         aq->rq.lpb_sizem1 += rte_pktmbuf_priv_size(mp);
328         aq->rq.lpb_sizem1 += sizeof(struct rte_mbuf);
329         aq->rq.lpb_sizem1 /= 8;
330         aq->rq.lpb_sizem1 -= 1; /* Expressed in size minus one */
331         aq->rq.ena = 1;
332         aq->rq.pb_caching = 0x2; /* First cache aligned block to LLC */
333         aq->rq.xqe_imm_size = 0; /* No pkt data copy to CQE */
334         aq->rq.rq_int_ena = 0;
335         /* Many to one reduction */
336         aq->rq.qint_idx = qid % dev->qints;
337
338         aq->rq.xqe_drop_ena = 1;
339
340         rc = otx2_mbox_process(mbox);
341         if (rc) {
342                 otx2_err("Failed to init rq context");
343                 goto fail;
344         }
345
346         return 0;
347 fail:
348         return rc;
349 }
350
351 static int
352 nix_rq_enb_dis(struct rte_eth_dev *eth_dev,
353                struct otx2_eth_rxq *rxq, const bool enb)
354 {
355         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
356         struct otx2_mbox *mbox = dev->mbox;
357         struct nix_aq_enq_req *aq;
358
359         /* Pkts will be dropped silently if RQ is disabled */
360         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
361         aq->qidx = rxq->rq;
362         aq->ctype = NIX_AQ_CTYPE_RQ;
363         aq->op = NIX_AQ_INSTOP_WRITE;
364
365         aq->rq.ena = enb;
366         aq->rq_mask.ena = ~(aq->rq_mask.ena);
367
368         return otx2_mbox_process(mbox);
369 }
370
371 static int
372 nix_cq_rq_uninit(struct rte_eth_dev *eth_dev, struct otx2_eth_rxq *rxq)
373 {
374         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
375         struct otx2_mbox *mbox = dev->mbox;
376         struct nix_aq_enq_req *aq;
377         int rc;
378
379         /* RQ is already disabled */
380         /* Disable CQ */
381         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
382         aq->qidx = rxq->rq;
383         aq->ctype = NIX_AQ_CTYPE_CQ;
384         aq->op = NIX_AQ_INSTOP_WRITE;
385
386         aq->cq.ena = 0;
387         aq->cq_mask.ena = ~(aq->cq_mask.ena);
388
389         rc = otx2_mbox_process(mbox);
390         if (rc < 0) {
391                 otx2_err("Failed to disable cq context");
392                 return rc;
393         }
394
395         return 0;
396 }
397
398 static inline int
399 nix_get_data_off(struct otx2_eth_dev *dev)
400 {
401         return otx2_ethdev_is_ptp_en(dev) ? NIX_TIMESYNC_RX_OFFSET : 0;
402 }
403
404 uint64_t
405 otx2_nix_rxq_mbuf_setup(struct otx2_eth_dev *dev, uint16_t port_id)
406 {
407         struct rte_mbuf mb_def;
408         uint64_t *tmp;
409
410         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) % 8 != 0);
411         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, refcnt) -
412                                 offsetof(struct rte_mbuf, data_off) != 2);
413         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, nb_segs) -
414                                 offsetof(struct rte_mbuf, data_off) != 4);
415         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, port) -
416                                 offsetof(struct rte_mbuf, data_off) != 6);
417         mb_def.nb_segs = 1;
418         mb_def.data_off = RTE_PKTMBUF_HEADROOM + nix_get_data_off(dev);
419         mb_def.port = port_id;
420         rte_mbuf_refcnt_set(&mb_def, 1);
421
422         /* Prevent compiler reordering: rearm_data covers previous fields */
423         rte_compiler_barrier();
424         tmp = (uint64_t *)&mb_def.rearm_data;
425
426         return *tmp;
427 }
428
429 static void
430 otx2_nix_rx_queue_release(void *rx_queue)
431 {
432         struct otx2_eth_rxq *rxq = rx_queue;
433
434         if (!rxq)
435                 return;
436
437         otx2_nix_dbg("Releasing rxq %u", rxq->rq);
438         nix_cq_rq_uninit(rxq->eth_dev, rxq);
439         rte_free(rx_queue);
440 }
441
442 static int
443 otx2_nix_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t rq,
444                         uint16_t nb_desc, unsigned int socket,
445                         const struct rte_eth_rxconf *rx_conf,
446                         struct rte_mempool *mp)
447 {
448         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
449         struct rte_mempool_ops *ops;
450         struct otx2_eth_rxq *rxq;
451         const char *platform_ops;
452         enum nix_q_size_e qsize;
453         uint64_t offloads;
454         int rc;
455
456         rc = -EINVAL;
457
458         /* Compile time check to make sure all fast path elements in a CL */
459         RTE_BUILD_BUG_ON(offsetof(struct otx2_eth_rxq, slow_path_start) >= 128);
460
461         /* Sanity checks */
462         if (rx_conf->rx_deferred_start == 1) {
463                 otx2_err("Deferred Rx start is not supported");
464                 goto fail;
465         }
466
467         platform_ops = rte_mbuf_platform_mempool_ops();
468         /* This driver needs octeontx2_npa mempool ops to work */
469         ops = rte_mempool_get_ops(mp->ops_index);
470         if (strncmp(ops->name, platform_ops, RTE_MEMPOOL_OPS_NAMESIZE)) {
471                 otx2_err("mempool ops should be of octeontx2_npa type");
472                 goto fail;
473         }
474
475         if (mp->pool_id == 0) {
476                 otx2_err("Invalid pool_id");
477                 goto fail;
478         }
479
480         /* Free memory prior to re-allocation if needed */
481         if (eth_dev->data->rx_queues[rq] != NULL) {
482                 otx2_nix_dbg("Freeing memory prior to re-allocation %d", rq);
483                 otx2_nix_rx_queue_release(eth_dev->data->rx_queues[rq]);
484                 eth_dev->data->rx_queues[rq] = NULL;
485         }
486
487         offloads = rx_conf->offloads | eth_dev->data->dev_conf.rxmode.offloads;
488         dev->rx_offloads |= offloads;
489
490         /* Find the CQ queue size */
491         qsize = nix_qsize_clampup_get(dev, nb_desc);
492         /* Allocate rxq memory */
493         rxq = rte_zmalloc_socket("otx2 rxq", sizeof(*rxq), OTX2_ALIGN, socket);
494         if (rxq == NULL) {
495                 otx2_err("Failed to allocate rq=%d", rq);
496                 rc = -ENOMEM;
497                 goto fail;
498         }
499
500         rxq->eth_dev = eth_dev;
501         rxq->rq = rq;
502         rxq->cq_door = dev->base + NIX_LF_CQ_OP_DOOR;
503         rxq->cq_status = (int64_t *)(dev->base + NIX_LF_CQ_OP_STATUS);
504         rxq->wdata = (uint64_t)rq << 32;
505         rxq->aura = npa_lf_aura_handle_to_aura(mp->pool_id);
506         rxq->mbuf_initializer = otx2_nix_rxq_mbuf_setup(dev,
507                                                         eth_dev->data->port_id);
508         rxq->offloads = offloads;
509         rxq->pool = mp;
510         rxq->qlen = nix_qsize_to_val(qsize);
511         rxq->qsize = qsize;
512         rxq->lookup_mem = otx2_nix_fastpath_lookup_mem_get();
513         rxq->tstamp = &dev->tstamp;
514
515         /* Alloc completion queue */
516         rc = nix_cq_rq_init(eth_dev, dev, rq, rxq, mp);
517         if (rc) {
518                 otx2_err("Failed to allocate rxq=%u", rq);
519                 goto free_rxq;
520         }
521
522         rxq->qconf.socket_id = socket;
523         rxq->qconf.nb_desc = nb_desc;
524         rxq->qconf.mempool = mp;
525         memcpy(&rxq->qconf.conf.rx, rx_conf, sizeof(struct rte_eth_rxconf));
526
527         nix_rx_queue_reset(rxq);
528         otx2_nix_dbg("rq=%d pool=%s qsize=%d nb_desc=%d->%d",
529                      rq, mp->name, qsize, nb_desc, rxq->qlen);
530
531         eth_dev->data->rx_queues[rq] = rxq;
532         eth_dev->data->rx_queue_state[rq] = RTE_ETH_QUEUE_STATE_STOPPED;
533
534         /* Calculating delta and freq mult between PTP HI clock and tsc.
535          * These are needed in deriving raw clock value from tsc counter.
536          * read_clock eth op returns raw clock value.
537          */
538         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP) ||
539             otx2_ethdev_is_ptp_en(dev)) {
540                 rc = otx2_nix_raw_clock_tsc_conv(dev);
541                 if (rc) {
542                         otx2_err("Failed to calculate delta and freq mult");
543                         goto fail;
544                 }
545         }
546
547         return 0;
548
549 free_rxq:
550         otx2_nix_rx_queue_release(rxq);
551 fail:
552         return rc;
553 }
554
555 static inline uint8_t
556 nix_sq_max_sqe_sz(struct otx2_eth_txq *txq)
557 {
558         /*
559          * Maximum three segments can be supported with W8, Choose
560          * NIX_MAXSQESZ_W16 for multi segment offload.
561          */
562         if (txq->offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
563                 return NIX_MAXSQESZ_W16;
564         else
565                 return NIX_MAXSQESZ_W8;
566 }
567
568 static uint16_t
569 nix_rx_offload_flags(struct rte_eth_dev *eth_dev)
570 {
571         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
572         struct rte_eth_dev_data *data = eth_dev->data;
573         struct rte_eth_conf *conf = &data->dev_conf;
574         struct rte_eth_rxmode *rxmode = &conf->rxmode;
575         uint16_t flags = 0;
576
577         if (rxmode->mq_mode == ETH_MQ_RX_RSS)
578                 flags |= NIX_RX_OFFLOAD_RSS_F;
579
580         if (dev->rx_offloads & (DEV_RX_OFFLOAD_TCP_CKSUM |
581                          DEV_RX_OFFLOAD_UDP_CKSUM))
582                 flags |= NIX_RX_OFFLOAD_CHECKSUM_F;
583
584         if (dev->rx_offloads & (DEV_RX_OFFLOAD_IPV4_CKSUM |
585                                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM))
586                 flags |= NIX_RX_OFFLOAD_CHECKSUM_F;
587
588         if (dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER)
589                 flags |= NIX_RX_MULTI_SEG_F;
590
591         if (dev->rx_offloads & (DEV_RX_OFFLOAD_VLAN_STRIP |
592                                 DEV_RX_OFFLOAD_QINQ_STRIP))
593                 flags |= NIX_RX_OFFLOAD_VLAN_STRIP_F;
594
595         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP))
596                 flags |= NIX_RX_OFFLOAD_TSTAMP_F;
597
598         return flags;
599 }
600
601 static uint16_t
602 nix_tx_offload_flags(struct rte_eth_dev *eth_dev)
603 {
604         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
605         uint64_t conf = dev->tx_offloads;
606         uint16_t flags = 0;
607
608         /* Fastpath is dependent on these enums */
609         RTE_BUILD_BUG_ON(PKT_TX_TCP_CKSUM != (1ULL << 52));
610         RTE_BUILD_BUG_ON(PKT_TX_SCTP_CKSUM != (2ULL << 52));
611         RTE_BUILD_BUG_ON(PKT_TX_UDP_CKSUM != (3ULL << 52));
612         RTE_BUILD_BUG_ON(PKT_TX_IP_CKSUM != (1ULL << 54));
613         RTE_BUILD_BUG_ON(PKT_TX_IPV4 != (1ULL << 55));
614         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IP_CKSUM != (1ULL << 58));
615         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IPV4 != (1ULL << 59));
616         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IPV6 != (1ULL << 60));
617         RTE_BUILD_BUG_ON(PKT_TX_OUTER_UDP_CKSUM != (1ULL << 41));
618         RTE_BUILD_BUG_ON(RTE_MBUF_L2_LEN_BITS != 7);
619         RTE_BUILD_BUG_ON(RTE_MBUF_L3_LEN_BITS != 9);
620         RTE_BUILD_BUG_ON(RTE_MBUF_OUTL2_LEN_BITS != 7);
621         RTE_BUILD_BUG_ON(RTE_MBUF_OUTL3_LEN_BITS != 9);
622         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) !=
623                          offsetof(struct rte_mbuf, buf_iova) + 8);
624         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
625                          offsetof(struct rte_mbuf, buf_iova) + 16);
626         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
627                          offsetof(struct rte_mbuf, ol_flags) + 12);
628         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, tx_offload) !=
629                          offsetof(struct rte_mbuf, pool) + 2 * sizeof(void *));
630
631         if (conf & DEV_TX_OFFLOAD_VLAN_INSERT ||
632             conf & DEV_TX_OFFLOAD_QINQ_INSERT)
633                 flags |= NIX_TX_OFFLOAD_VLAN_QINQ_F;
634
635         if (conf & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM ||
636             conf & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM)
637                 flags |= NIX_TX_OFFLOAD_OL3_OL4_CSUM_F;
638
639         if (conf & DEV_TX_OFFLOAD_IPV4_CKSUM ||
640             conf & DEV_TX_OFFLOAD_TCP_CKSUM ||
641             conf & DEV_TX_OFFLOAD_UDP_CKSUM ||
642             conf & DEV_TX_OFFLOAD_SCTP_CKSUM)
643                 flags |= NIX_TX_OFFLOAD_L3_L4_CSUM_F;
644
645         if (!(conf & DEV_TX_OFFLOAD_MBUF_FAST_FREE))
646                 flags |= NIX_TX_OFFLOAD_MBUF_NOFF_F;
647
648         if (conf & DEV_TX_OFFLOAD_MULTI_SEGS)
649                 flags |= NIX_TX_MULTI_SEG_F;
650
651         /* Enable Inner checksum for TSO */
652         if (conf & DEV_TX_OFFLOAD_TCP_TSO)
653                 flags |= (NIX_TX_OFFLOAD_TSO_F |
654                           NIX_TX_OFFLOAD_L3_L4_CSUM_F);
655
656         /* Enable Inner and Outer checksum for Tunnel TSO */
657         if (conf & (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
658                     DEV_TX_OFFLOAD_GENEVE_TNL_TSO))
659                 flags |= (NIX_TX_OFFLOAD_TSO_F |
660                           NIX_TX_OFFLOAD_OL3_OL4_CSUM_F |
661                           NIX_TX_OFFLOAD_L3_L4_CSUM_F);
662
663         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP))
664                 flags |= NIX_TX_OFFLOAD_TSTAMP_F;
665
666         return flags;
667 }
668
669 static int
670 nix_sq_init(struct otx2_eth_txq *txq)
671 {
672         struct otx2_eth_dev *dev = txq->dev;
673         struct otx2_mbox *mbox = dev->mbox;
674         struct nix_aq_enq_req *sq;
675         uint32_t rr_quantum;
676         uint16_t smq;
677         int rc;
678
679         if (txq->sqb_pool->pool_id == 0)
680                 return -EINVAL;
681
682         rc = otx2_nix_tm_get_leaf_data(dev, txq->sq, &rr_quantum, &smq);
683         if (rc) {
684                 otx2_err("Failed to get sq->smq(leaf node), rc=%d", rc);
685                 return rc;
686         }
687
688         sq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
689         sq->qidx = txq->sq;
690         sq->ctype = NIX_AQ_CTYPE_SQ;
691         sq->op = NIX_AQ_INSTOP_INIT;
692         sq->sq.max_sqe_size = nix_sq_max_sqe_sz(txq);
693
694         sq->sq.smq = smq;
695         sq->sq.smq_rr_quantum = rr_quantum;
696         sq->sq.default_chan = dev->tx_chan_base;
697         sq->sq.sqe_stype = NIX_STYPE_STF;
698         sq->sq.ena = 1;
699         if (sq->sq.max_sqe_size == NIX_MAXSQESZ_W8)
700                 sq->sq.sqe_stype = NIX_STYPE_STP;
701         sq->sq.sqb_aura =
702                 npa_lf_aura_handle_to_aura(txq->sqb_pool->pool_id);
703         sq->sq.sq_int_ena = BIT(NIX_SQINT_LMT_ERR);
704         sq->sq.sq_int_ena |= BIT(NIX_SQINT_SQB_ALLOC_FAIL);
705         sq->sq.sq_int_ena |= BIT(NIX_SQINT_SEND_ERR);
706         sq->sq.sq_int_ena |= BIT(NIX_SQINT_MNQ_ERR);
707
708         /* Many to one reduction */
709         sq->sq.qint_idx = txq->sq % dev->qints;
710
711         return otx2_mbox_process(mbox);
712 }
713
714 static int
715 nix_sq_uninit(struct otx2_eth_txq *txq)
716 {
717         struct otx2_eth_dev *dev = txq->dev;
718         struct otx2_mbox *mbox = dev->mbox;
719         struct ndc_sync_op *ndc_req;
720         struct nix_aq_enq_rsp *rsp;
721         struct nix_aq_enq_req *aq;
722         uint16_t sqes_per_sqb;
723         void *sqb_buf;
724         int rc, count;
725
726         otx2_nix_dbg("Cleaning up sq %u", txq->sq);
727
728         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
729         aq->qidx = txq->sq;
730         aq->ctype = NIX_AQ_CTYPE_SQ;
731         aq->op = NIX_AQ_INSTOP_READ;
732
733         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
734         if (rc)
735                 return rc;
736
737         /* Check if sq is already cleaned up */
738         if (!rsp->sq.ena)
739                 return 0;
740
741         /* Disable sq */
742         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
743         aq->qidx = txq->sq;
744         aq->ctype = NIX_AQ_CTYPE_SQ;
745         aq->op = NIX_AQ_INSTOP_WRITE;
746
747         aq->sq_mask.ena = ~aq->sq_mask.ena;
748         aq->sq.ena = 0;
749
750         rc = otx2_mbox_process(mbox);
751         if (rc)
752                 return rc;
753
754         /* Read SQ and free sqb's */
755         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
756         aq->qidx = txq->sq;
757         aq->ctype = NIX_AQ_CTYPE_SQ;
758         aq->op = NIX_AQ_INSTOP_READ;
759
760         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
761         if (rc)
762                 return rc;
763
764         if (aq->sq.smq_pend)
765                 otx2_err("SQ has pending sqe's");
766
767         count = aq->sq.sqb_count;
768         sqes_per_sqb = 1 << txq->sqes_per_sqb_log2;
769         /* Free SQB's that are used */
770         sqb_buf = (void *)rsp->sq.head_sqb;
771         while (count) {
772                 void *next_sqb;
773
774                 next_sqb = *(void **)((uintptr_t)sqb_buf + (uint32_t)
775                                       ((sqes_per_sqb - 1) *
776                                       nix_sq_max_sqe_sz(txq)));
777                 npa_lf_aura_op_free(txq->sqb_pool->pool_id, 1,
778                                     (uint64_t)sqb_buf);
779                 sqb_buf = next_sqb;
780                 count--;
781         }
782
783         /* Free next to use sqb */
784         if (rsp->sq.next_sqb)
785                 npa_lf_aura_op_free(txq->sqb_pool->pool_id, 1,
786                                     rsp->sq.next_sqb);
787
788         /* Sync NDC-NIX-TX for LF */
789         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
790         ndc_req->nix_lf_tx_sync = 1;
791         rc = otx2_mbox_process(mbox);
792         if (rc)
793                 otx2_err("Error on NDC-NIX-TX LF sync, rc %d", rc);
794
795         return rc;
796 }
797
798 static int
799 nix_sqb_aura_limit_cfg(struct rte_mempool *mp, uint16_t nb_sqb_bufs)
800 {
801         struct otx2_npa_lf *npa_lf = otx2_intra_dev_get_cfg()->npa_lf;
802         struct npa_aq_enq_req *aura_req;
803
804         aura_req = otx2_mbox_alloc_msg_npa_aq_enq(npa_lf->mbox);
805         aura_req->aura_id = npa_lf_aura_handle_to_aura(mp->pool_id);
806         aura_req->ctype = NPA_AQ_CTYPE_AURA;
807         aura_req->op = NPA_AQ_INSTOP_WRITE;
808
809         aura_req->aura.limit = nb_sqb_bufs;
810         aura_req->aura_mask.limit = ~(aura_req->aura_mask.limit);
811
812         return otx2_mbox_process(npa_lf->mbox);
813 }
814
815 static int
816 nix_alloc_sqb_pool(int port, struct otx2_eth_txq *txq, uint16_t nb_desc)
817 {
818         struct otx2_eth_dev *dev = txq->dev;
819         uint16_t sqes_per_sqb, nb_sqb_bufs;
820         char name[RTE_MEMPOOL_NAMESIZE];
821         struct rte_mempool_objsz sz;
822         struct npa_aura_s *aura;
823         uint32_t tmp, blk_sz;
824
825         aura = (struct npa_aura_s *)((uintptr_t)txq->fc_mem + OTX2_ALIGN);
826         snprintf(name, sizeof(name), "otx2_sqb_pool_%d_%d", port, txq->sq);
827         blk_sz = dev->sqb_size;
828
829         if (nix_sq_max_sqe_sz(txq) == NIX_MAXSQESZ_W16)
830                 sqes_per_sqb = (dev->sqb_size / 8) / 16;
831         else
832                 sqes_per_sqb = (dev->sqb_size / 8) / 8;
833
834         nb_sqb_bufs = nb_desc / sqes_per_sqb;
835         /* Clamp up to devarg passed SQB count */
836         nb_sqb_bufs =  RTE_MIN(dev->max_sqb_count, RTE_MAX(NIX_DEF_SQB,
837                               nb_sqb_bufs + NIX_SQB_LIST_SPACE));
838
839         txq->sqb_pool = rte_mempool_create_empty(name, NIX_MAX_SQB, blk_sz,
840                                                  0, 0, dev->node,
841                                                  MEMPOOL_F_NO_SPREAD);
842         txq->nb_sqb_bufs = nb_sqb_bufs;
843         txq->sqes_per_sqb_log2 = (uint16_t)rte_log2_u32(sqes_per_sqb);
844         txq->nb_sqb_bufs_adj = nb_sqb_bufs -
845                 RTE_ALIGN_MUL_CEIL(nb_sqb_bufs, sqes_per_sqb) / sqes_per_sqb;
846         txq->nb_sqb_bufs_adj =
847                 (NIX_SQB_LOWER_THRESH * txq->nb_sqb_bufs_adj) / 100;
848
849         if (txq->sqb_pool == NULL) {
850                 otx2_err("Failed to allocate sqe mempool");
851                 goto fail;
852         }
853
854         memset(aura, 0, sizeof(*aura));
855         aura->fc_ena = 1;
856         aura->fc_addr = txq->fc_iova;
857         aura->fc_hyst_bits = 0; /* Store count on all updates */
858         if (rte_mempool_set_ops_byname(txq->sqb_pool, "octeontx2_npa", aura)) {
859                 otx2_err("Failed to set ops for sqe mempool");
860                 goto fail;
861         }
862         if (rte_mempool_populate_default(txq->sqb_pool) < 0) {
863                 otx2_err("Failed to populate sqe mempool");
864                 goto fail;
865         }
866
867         tmp = rte_mempool_calc_obj_size(blk_sz, MEMPOOL_F_NO_SPREAD, &sz);
868         if (dev->sqb_size != sz.elt_size) {
869                 otx2_err("sqe pool block size is not expected %d != %d",
870                          dev->sqb_size, tmp);
871                 goto fail;
872         }
873
874         nix_sqb_aura_limit_cfg(txq->sqb_pool, txq->nb_sqb_bufs);
875
876         return 0;
877 fail:
878         return -ENOMEM;
879 }
880
881 void
882 otx2_nix_form_default_desc(struct otx2_eth_txq *txq)
883 {
884         struct nix_send_ext_s *send_hdr_ext;
885         struct nix_send_hdr_s *send_hdr;
886         struct nix_send_mem_s *send_mem;
887         union nix_send_sg_s *sg;
888
889         /* Initialize the fields based on basic single segment packet */
890         memset(&txq->cmd, 0, sizeof(txq->cmd));
891
892         if (txq->dev->tx_offload_flags & NIX_TX_NEED_EXT_HDR) {
893                 send_hdr = (struct nix_send_hdr_s *)&txq->cmd[0];
894                 /* 2(HDR) + 2(EXT_HDR) + 1(SG) + 1(IOVA) = 6/2 - 1 = 2 */
895                 send_hdr->w0.sizem1 = 2;
896
897                 send_hdr_ext = (struct nix_send_ext_s *)&txq->cmd[2];
898                 send_hdr_ext->w0.subdc = NIX_SUBDC_EXT;
899                 if (txq->dev->tx_offload_flags & NIX_TX_OFFLOAD_TSTAMP_F) {
900                         /* Default: one seg packet would have:
901                          * 2(HDR) + 2(EXT) + 1(SG) + 1(IOVA) + 2(MEM)
902                          * => 8/2 - 1 = 3
903                          */
904                         send_hdr->w0.sizem1 = 3;
905                         send_hdr_ext->w0.tstmp = 1;
906
907                         /* To calculate the offset for send_mem,
908                          * send_hdr->w0.sizem1 * 2
909                          */
910                         send_mem = (struct nix_send_mem_s *)(txq->cmd +
911                                                 (send_hdr->w0.sizem1 << 1));
912                         send_mem->subdc = NIX_SUBDC_MEM;
913                         send_mem->alg = NIX_SENDMEMALG_SETTSTMP;
914                         send_mem->addr = txq->dev->tstamp.tx_tstamp_iova;
915                 }
916                 sg = (union nix_send_sg_s *)&txq->cmd[4];
917         } else {
918                 send_hdr = (struct nix_send_hdr_s *)&txq->cmd[0];
919                 /* 2(HDR) + 1(SG) + 1(IOVA) = 4/2 - 1 = 1 */
920                 send_hdr->w0.sizem1 = 1;
921                 sg = (union nix_send_sg_s *)&txq->cmd[2];
922         }
923
924         send_hdr->w0.sq = txq->sq;
925         sg->subdc = NIX_SUBDC_SG;
926         sg->segs = 1;
927         sg->ld_type = NIX_SENDLDTYPE_LDD;
928
929         rte_smp_wmb();
930 }
931
932 static void
933 otx2_nix_tx_queue_release(void *_txq)
934 {
935         struct otx2_eth_txq *txq = _txq;
936         struct rte_eth_dev *eth_dev;
937
938         if (!txq)
939                 return;
940
941         eth_dev = txq->dev->eth_dev;
942
943         otx2_nix_dbg("Releasing txq %u", txq->sq);
944
945         /* Flush and disable tm */
946         otx2_nix_tm_sw_xoff(txq, eth_dev->data->dev_started);
947
948         /* Free sqb's and disable sq */
949         nix_sq_uninit(txq);
950
951         if (txq->sqb_pool) {
952                 rte_mempool_free(txq->sqb_pool);
953                 txq->sqb_pool = NULL;
954         }
955         rte_free(txq);
956 }
957
958
959 static int
960 otx2_nix_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t sq,
961                         uint16_t nb_desc, unsigned int socket_id,
962                         const struct rte_eth_txconf *tx_conf)
963 {
964         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
965         const struct rte_memzone *fc;
966         struct otx2_eth_txq *txq;
967         uint64_t offloads;
968         int rc;
969
970         rc = -EINVAL;
971
972         /* Compile time check to make sure all fast path elements in a CL */
973         RTE_BUILD_BUG_ON(offsetof(struct otx2_eth_txq, slow_path_start) >= 128);
974
975         if (tx_conf->tx_deferred_start) {
976                 otx2_err("Tx deferred start is not supported");
977                 goto fail;
978         }
979
980         /* Free memory prior to re-allocation if needed. */
981         if (eth_dev->data->tx_queues[sq] != NULL) {
982                 otx2_nix_dbg("Freeing memory prior to re-allocation %d", sq);
983                 otx2_nix_tx_queue_release(eth_dev->data->tx_queues[sq]);
984                 eth_dev->data->tx_queues[sq] = NULL;
985         }
986
987         /* Find the expected offloads for this queue */
988         offloads = tx_conf->offloads | eth_dev->data->dev_conf.txmode.offloads;
989
990         /* Allocating tx queue data structure */
991         txq = rte_zmalloc_socket("otx2_ethdev TX queue", sizeof(*txq),
992                                  OTX2_ALIGN, socket_id);
993         if (txq == NULL) {
994                 otx2_err("Failed to alloc txq=%d", sq);
995                 rc = -ENOMEM;
996                 goto fail;
997         }
998         txq->sq = sq;
999         txq->dev = dev;
1000         txq->sqb_pool = NULL;
1001         txq->offloads = offloads;
1002         dev->tx_offloads |= offloads;
1003
1004         /*
1005          * Allocate memory for flow control updates from HW.
1006          * Alloc one cache line, so that fits all FC_STYPE modes.
1007          */
1008         fc = rte_eth_dma_zone_reserve(eth_dev, "fcmem", sq,
1009                                       OTX2_ALIGN + sizeof(struct npa_aura_s),
1010                                       OTX2_ALIGN, dev->node);
1011         if (fc == NULL) {
1012                 otx2_err("Failed to allocate mem for fcmem");
1013                 rc = -ENOMEM;
1014                 goto free_txq;
1015         }
1016         txq->fc_iova = fc->iova;
1017         txq->fc_mem = fc->addr;
1018
1019         /* Initialize the aura sqb pool */
1020         rc = nix_alloc_sqb_pool(eth_dev->data->port_id, txq, nb_desc);
1021         if (rc) {
1022                 otx2_err("Failed to alloc sqe pool rc=%d", rc);
1023                 goto free_txq;
1024         }
1025
1026         /* Initialize the SQ */
1027         rc = nix_sq_init(txq);
1028         if (rc) {
1029                 otx2_err("Failed to init sq=%d context", sq);
1030                 goto free_txq;
1031         }
1032
1033         txq->fc_cache_pkts = 0;
1034         txq->io_addr = dev->base + NIX_LF_OP_SENDX(0);
1035         /* Evenly distribute LMT slot for each sq */
1036         txq->lmt_addr = (void *)(dev->lmt_addr + ((sq & LMT_SLOT_MASK) << 12));
1037
1038         txq->qconf.socket_id = socket_id;
1039         txq->qconf.nb_desc = nb_desc;
1040         memcpy(&txq->qconf.conf.tx, tx_conf, sizeof(struct rte_eth_txconf));
1041
1042         otx2_nix_form_default_desc(txq);
1043
1044         otx2_nix_dbg("sq=%d fc=%p offload=0x%" PRIx64 " sqb=0x%" PRIx64 ""
1045                      " lmt_addr=%p nb_sqb_bufs=%d sqes_per_sqb_log2=%d", sq,
1046                      fc->addr, offloads, txq->sqb_pool->pool_id, txq->lmt_addr,
1047                      txq->nb_sqb_bufs, txq->sqes_per_sqb_log2);
1048         eth_dev->data->tx_queues[sq] = txq;
1049         eth_dev->data->tx_queue_state[sq] = RTE_ETH_QUEUE_STATE_STOPPED;
1050         return 0;
1051
1052 free_txq:
1053         otx2_nix_tx_queue_release(txq);
1054 fail:
1055         return rc;
1056 }
1057
1058 static int
1059 nix_store_queue_cfg_and_then_release(struct rte_eth_dev *eth_dev)
1060 {
1061         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1062         struct otx2_eth_qconf *tx_qconf = NULL;
1063         struct otx2_eth_qconf *rx_qconf = NULL;
1064         struct otx2_eth_txq **txq;
1065         struct otx2_eth_rxq **rxq;
1066         int i, nb_rxq, nb_txq;
1067
1068         nb_rxq = RTE_MIN(dev->configured_nb_rx_qs, eth_dev->data->nb_rx_queues);
1069         nb_txq = RTE_MIN(dev->configured_nb_tx_qs, eth_dev->data->nb_tx_queues);
1070
1071         tx_qconf = malloc(nb_txq * sizeof(*tx_qconf));
1072         if (tx_qconf == NULL) {
1073                 otx2_err("Failed to allocate memory for tx_qconf");
1074                 goto fail;
1075         }
1076
1077         rx_qconf = malloc(nb_rxq * sizeof(*rx_qconf));
1078         if (rx_qconf == NULL) {
1079                 otx2_err("Failed to allocate memory for rx_qconf");
1080                 goto fail;
1081         }
1082
1083         txq = (struct otx2_eth_txq **)eth_dev->data->tx_queues;
1084         for (i = 0; i < nb_txq; i++) {
1085                 if (txq[i] == NULL) {
1086                         otx2_err("txq[%d] is already released", i);
1087                         goto fail;
1088                 }
1089                 memcpy(&tx_qconf[i], &txq[i]->qconf, sizeof(*tx_qconf));
1090                 otx2_nix_tx_queue_release(txq[i]);
1091                 eth_dev->data->tx_queues[i] = NULL;
1092         }
1093
1094         rxq = (struct otx2_eth_rxq **)eth_dev->data->rx_queues;
1095         for (i = 0; i < nb_rxq; i++) {
1096                 if (rxq[i] == NULL) {
1097                         otx2_err("rxq[%d] is already released", i);
1098                         goto fail;
1099                 }
1100                 memcpy(&rx_qconf[i], &rxq[i]->qconf, sizeof(*rx_qconf));
1101                 otx2_nix_rx_queue_release(rxq[i]);
1102                 eth_dev->data->rx_queues[i] = NULL;
1103         }
1104
1105         dev->tx_qconf = tx_qconf;
1106         dev->rx_qconf = rx_qconf;
1107         return 0;
1108
1109 fail:
1110         if (tx_qconf)
1111                 free(tx_qconf);
1112         if (rx_qconf)
1113                 free(rx_qconf);
1114
1115         return -ENOMEM;
1116 }
1117
1118 static int
1119 nix_restore_queue_cfg(struct rte_eth_dev *eth_dev)
1120 {
1121         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1122         struct otx2_eth_qconf *tx_qconf = dev->tx_qconf;
1123         struct otx2_eth_qconf *rx_qconf = dev->rx_qconf;
1124         struct otx2_eth_txq **txq;
1125         struct otx2_eth_rxq **rxq;
1126         int rc, i, nb_rxq, nb_txq;
1127
1128         nb_rxq = RTE_MIN(dev->configured_nb_rx_qs, eth_dev->data->nb_rx_queues);
1129         nb_txq = RTE_MIN(dev->configured_nb_tx_qs, eth_dev->data->nb_tx_queues);
1130
1131         rc = -ENOMEM;
1132         /* Setup tx & rx queues with previous configuration so
1133          * that the queues can be functional in cases like ports
1134          * are started without re configuring queues.
1135          *
1136          * Usual re config sequence is like below:
1137          * port_configure() {
1138          *      if(reconfigure) {
1139          *              queue_release()
1140          *              queue_setup()
1141          *      }
1142          *      queue_configure() {
1143          *              queue_release()
1144          *              queue_setup()
1145          *      }
1146          * }
1147          * port_start()
1148          *
1149          * In some application's control path, queue_configure() would
1150          * NOT be invoked for TXQs/RXQs in port_configure().
1151          * In such cases, queues can be functional after start as the
1152          * queues are already setup in port_configure().
1153          */
1154         for (i = 0; i < nb_txq; i++) {
1155                 rc = otx2_nix_tx_queue_setup(eth_dev, i, tx_qconf[i].nb_desc,
1156                                              tx_qconf[i].socket_id,
1157                                              &tx_qconf[i].conf.tx);
1158                 if (rc) {
1159                         otx2_err("Failed to setup tx queue rc=%d", rc);
1160                         txq = (struct otx2_eth_txq **)eth_dev->data->tx_queues;
1161                         for (i -= 1; i >= 0; i--)
1162                                 otx2_nix_tx_queue_release(txq[i]);
1163                         goto fail;
1164                 }
1165         }
1166
1167         free(tx_qconf); tx_qconf = NULL;
1168
1169         for (i = 0; i < nb_rxq; i++) {
1170                 rc = otx2_nix_rx_queue_setup(eth_dev, i, rx_qconf[i].nb_desc,
1171                                              rx_qconf[i].socket_id,
1172                                              &rx_qconf[i].conf.rx,
1173                                              rx_qconf[i].mempool);
1174                 if (rc) {
1175                         otx2_err("Failed to setup rx queue rc=%d", rc);
1176                         rxq = (struct otx2_eth_rxq **)eth_dev->data->rx_queues;
1177                         for (i -= 1; i >= 0; i--)
1178                                 otx2_nix_rx_queue_release(rxq[i]);
1179                         goto release_tx_queues;
1180                 }
1181         }
1182
1183         free(rx_qconf); rx_qconf = NULL;
1184
1185         return 0;
1186
1187 release_tx_queues:
1188         txq = (struct otx2_eth_txq **)eth_dev->data->tx_queues;
1189         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1190                 otx2_nix_tx_queue_release(txq[i]);
1191 fail:
1192         if (tx_qconf)
1193                 free(tx_qconf);
1194         if (rx_qconf)
1195                 free(rx_qconf);
1196
1197         return rc;
1198 }
1199
1200 static uint16_t
1201 nix_eth_nop_burst(void *queue, struct rte_mbuf **mbufs, uint16_t pkts)
1202 {
1203         RTE_SET_USED(queue);
1204         RTE_SET_USED(mbufs);
1205         RTE_SET_USED(pkts);
1206
1207         return 0;
1208 }
1209
1210 static void
1211 nix_set_nop_rxtx_function(struct rte_eth_dev *eth_dev)
1212 {
1213         /* These dummy functions are required for supporting
1214          * some applications which reconfigure queues without
1215          * stopping tx burst and rx burst threads(eg kni app)
1216          * When the queues context is saved, txq/rxqs are released
1217          * which caused app crash since rx/tx burst is still
1218          * on different lcores
1219          */
1220         eth_dev->tx_pkt_burst = nix_eth_nop_burst;
1221         eth_dev->rx_pkt_burst = nix_eth_nop_burst;
1222         rte_mb();
1223 }
1224
1225 static void
1226 nix_lso_tcp(struct nix_lso_format_cfg *req, bool v4)
1227 {
1228         volatile struct nix_lso_format *field;
1229
1230         /* Format works only with TCP packet marked by OL3/OL4 */
1231         field = (volatile struct nix_lso_format *)&req->fields[0];
1232         req->field_mask = NIX_LSO_FIELD_MASK;
1233         /* Outer IPv4/IPv6 */
1234         field->layer = NIX_TXLAYER_OL3;
1235         field->offset = v4 ? 2 : 4;
1236         field->sizem1 = 1; /* 2B */
1237         field->alg = NIX_LSOALG_ADD_PAYLEN;
1238         field++;
1239         if (v4) {
1240                 /* IPID field */
1241                 field->layer = NIX_TXLAYER_OL3;
1242                 field->offset = 4;
1243                 field->sizem1 = 1;
1244                 /* Incremented linearly per segment */
1245                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1246                 field++;
1247         }
1248
1249         /* TCP sequence number update */
1250         field->layer = NIX_TXLAYER_OL4;
1251         field->offset = 4;
1252         field->sizem1 = 3; /* 4 bytes */
1253         field->alg = NIX_LSOALG_ADD_OFFSET;
1254         field++;
1255         /* TCP flags field */
1256         field->layer = NIX_TXLAYER_OL4;
1257         field->offset = 12;
1258         field->sizem1 = 1;
1259         field->alg = NIX_LSOALG_TCP_FLAGS;
1260         field++;
1261 }
1262
1263 static void
1264 nix_lso_udp_tun_tcp(struct nix_lso_format_cfg *req,
1265                     bool outer_v4, bool inner_v4)
1266 {
1267         volatile struct nix_lso_format *field;
1268
1269         field = (volatile struct nix_lso_format *)&req->fields[0];
1270         req->field_mask = NIX_LSO_FIELD_MASK;
1271         /* Outer IPv4/IPv6 len */
1272         field->layer = NIX_TXLAYER_OL3;
1273         field->offset = outer_v4 ? 2 : 4;
1274         field->sizem1 = 1; /* 2B */
1275         field->alg = NIX_LSOALG_ADD_PAYLEN;
1276         field++;
1277         if (outer_v4) {
1278                 /* IPID */
1279                 field->layer = NIX_TXLAYER_OL3;
1280                 field->offset = 4;
1281                 field->sizem1 = 1;
1282                 /* Incremented linearly per segment */
1283                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1284                 field++;
1285         }
1286
1287         /* Outer UDP length */
1288         field->layer = NIX_TXLAYER_OL4;
1289         field->offset = 4;
1290         field->sizem1 = 1;
1291         field->alg = NIX_LSOALG_ADD_PAYLEN;
1292         field++;
1293
1294         /* Inner IPv4/IPv6 */
1295         field->layer = NIX_TXLAYER_IL3;
1296         field->offset = inner_v4 ? 2 : 4;
1297         field->sizem1 = 1; /* 2B */
1298         field->alg = NIX_LSOALG_ADD_PAYLEN;
1299         field++;
1300         if (inner_v4) {
1301                 /* IPID field */
1302                 field->layer = NIX_TXLAYER_IL3;
1303                 field->offset = 4;
1304                 field->sizem1 = 1;
1305                 /* Incremented linearly per segment */
1306                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1307                 field++;
1308         }
1309
1310         /* TCP sequence number update */
1311         field->layer = NIX_TXLAYER_IL4;
1312         field->offset = 4;
1313         field->sizem1 = 3; /* 4 bytes */
1314         field->alg = NIX_LSOALG_ADD_OFFSET;
1315         field++;
1316
1317         /* TCP flags field */
1318         field->layer = NIX_TXLAYER_IL4;
1319         field->offset = 12;
1320         field->sizem1 = 1;
1321         field->alg = NIX_LSOALG_TCP_FLAGS;
1322         field++;
1323 }
1324
1325 static int
1326 nix_setup_lso_formats(struct otx2_eth_dev *dev)
1327 {
1328         struct otx2_mbox *mbox = dev->mbox;
1329         struct nix_lso_format_cfg_rsp *rsp;
1330         struct nix_lso_format_cfg *req;
1331         uint8_t base;
1332         int rc;
1333
1334         /* Skip if TSO was not requested */
1335         if (!(dev->tx_offload_flags & NIX_TX_OFFLOAD_TSO_F))
1336                 return 0;
1337         /*
1338          * IPv4/TCP LSO
1339          */
1340         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1341         nix_lso_tcp(req, true);
1342         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1343         if (rc)
1344                 return rc;
1345
1346         base = rsp->lso_format_idx;
1347         if (base != NIX_LSO_FORMAT_IDX_TSOV4)
1348                 return -EFAULT;
1349         dev->lso_base_idx = base;
1350         otx2_nix_dbg("tcpv4 lso fmt=%u", base);
1351
1352
1353         /*
1354          * IPv6/TCP LSO
1355          */
1356         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1357         nix_lso_tcp(req, false);
1358         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1359         if (rc)
1360                 return rc;
1361
1362         if (rsp->lso_format_idx != base + 1)
1363                 return -EFAULT;
1364         otx2_nix_dbg("tcpv6 lso fmt=%u\n", base + 1);
1365
1366         /*
1367          * IPv4/UDP/TUN HDR/IPv4/TCP LSO
1368          */
1369         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1370         nix_lso_udp_tun_tcp(req, true, true);
1371         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1372         if (rc)
1373                 return rc;
1374
1375         if (rsp->lso_format_idx != base + 2)
1376                 return -EFAULT;
1377         otx2_nix_dbg("udp tun v4v4 fmt=%u\n", base + 2);
1378
1379         /*
1380          * IPv4/UDP/TUN HDR/IPv6/TCP LSO
1381          */
1382         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1383         nix_lso_udp_tun_tcp(req, true, false);
1384         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1385         if (rc)
1386                 return rc;
1387
1388         if (rsp->lso_format_idx != base + 3)
1389                 return -EFAULT;
1390         otx2_nix_dbg("udp tun v4v6 fmt=%u\n", base + 3);
1391
1392         /*
1393          * IPv6/UDP/TUN HDR/IPv4/TCP LSO
1394          */
1395         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1396         nix_lso_udp_tun_tcp(req, false, true);
1397         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1398         if (rc)
1399                 return rc;
1400
1401         if (rsp->lso_format_idx != base + 4)
1402                 return -EFAULT;
1403         otx2_nix_dbg("udp tun v6v4 fmt=%u\n", base + 4);
1404
1405         /*
1406          * IPv6/UDP/TUN HDR/IPv6/TCP LSO
1407          */
1408         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1409         nix_lso_udp_tun_tcp(req, false, false);
1410         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1411         if (rc)
1412                 return rc;
1413         if (rsp->lso_format_idx != base + 5)
1414                 return -EFAULT;
1415         otx2_nix_dbg("udp tun v6v6 fmt=%u\n", base + 5);
1416
1417         return 0;
1418 }
1419
1420 static int
1421 otx2_nix_configure(struct rte_eth_dev *eth_dev)
1422 {
1423         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1424         struct rte_eth_dev_data *data = eth_dev->data;
1425         struct rte_eth_conf *conf = &data->dev_conf;
1426         struct rte_eth_rxmode *rxmode = &conf->rxmode;
1427         struct rte_eth_txmode *txmode = &conf->txmode;
1428         char ea_fmt[RTE_ETHER_ADDR_FMT_SIZE];
1429         struct rte_ether_addr *ea;
1430         uint8_t nb_rxq, nb_txq;
1431         int rc;
1432
1433         rc = -EINVAL;
1434
1435         /* Sanity checks */
1436         if (rte_eal_has_hugepages() == 0) {
1437                 otx2_err("Huge page is not configured");
1438                 goto fail_configure;
1439         }
1440
1441         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
1442                 otx2_err("Setting link speed/duplex not supported");
1443                 goto fail_configure;
1444         }
1445
1446         if (conf->dcb_capability_en == 1) {
1447                 otx2_err("dcb enable is not supported");
1448                 goto fail_configure;
1449         }
1450
1451         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
1452                 otx2_err("Flow director is not supported");
1453                 goto fail_configure;
1454         }
1455
1456         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
1457             rxmode->mq_mode != ETH_MQ_RX_RSS) {
1458                 otx2_err("Unsupported mq rx mode %d", rxmode->mq_mode);
1459                 goto fail_configure;
1460         }
1461
1462         if (txmode->mq_mode != ETH_MQ_TX_NONE) {
1463                 otx2_err("Unsupported mq tx mode %d", txmode->mq_mode);
1464                 goto fail_configure;
1465         }
1466
1467         if (otx2_dev_is_Ax(dev) &&
1468             (txmode->offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
1469             ((txmode->offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1470             (txmode->offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM))) {
1471                 otx2_err("Outer IP and SCTP checksum unsupported");
1472                 goto fail_configure;
1473         }
1474
1475         /* Free the resources allocated from the previous configure */
1476         if (dev->configured == 1) {
1477                 otx2_nix_rxchan_bpid_cfg(eth_dev, false);
1478                 otx2_nix_vlan_fini(eth_dev);
1479                 otx2_flow_free_all_resources(dev);
1480                 oxt2_nix_unregister_queue_irqs(eth_dev);
1481                 if (eth_dev->data->dev_conf.intr_conf.rxq)
1482                         oxt2_nix_unregister_cq_irqs(eth_dev);
1483                 nix_set_nop_rxtx_function(eth_dev);
1484                 rc = nix_store_queue_cfg_and_then_release(eth_dev);
1485                 if (rc)
1486                         goto fail_configure;
1487                 otx2_nix_tm_fini(eth_dev);
1488                 nix_lf_free(dev);
1489         }
1490
1491         dev->rx_offloads = rxmode->offloads;
1492         dev->tx_offloads = txmode->offloads;
1493         dev->rx_offload_flags |= nix_rx_offload_flags(eth_dev);
1494         dev->tx_offload_flags |= nix_tx_offload_flags(eth_dev);
1495         dev->rss_info.rss_grps = NIX_RSS_GRPS;
1496
1497         nb_rxq = RTE_MAX(data->nb_rx_queues, 1);
1498         nb_txq = RTE_MAX(data->nb_tx_queues, 1);
1499
1500         /* Alloc a nix lf */
1501         rc = nix_lf_alloc(dev, nb_rxq, nb_txq);
1502         if (rc) {
1503                 otx2_err("Failed to init nix_lf rc=%d", rc);
1504                 goto fail_offloads;
1505         }
1506
1507         rc = nix_setup_lso_formats(dev);
1508         if (rc) {
1509                 otx2_err("failed to setup nix lso format fields, rc=%d", rc);
1510                 goto free_nix_lf;
1511         }
1512
1513         /* Configure RSS */
1514         rc = otx2_nix_rss_config(eth_dev);
1515         if (rc) {
1516                 otx2_err("Failed to configure rss rc=%d", rc);
1517                 goto free_nix_lf;
1518         }
1519
1520         /* Init the default TM scheduler hierarchy */
1521         rc = otx2_nix_tm_init_default(eth_dev);
1522         if (rc) {
1523                 otx2_err("Failed to init traffic manager rc=%d", rc);
1524                 goto free_nix_lf;
1525         }
1526
1527         rc = otx2_nix_vlan_offload_init(eth_dev);
1528         if (rc) {
1529                 otx2_err("Failed to init vlan offload rc=%d", rc);
1530                 goto tm_fini;
1531         }
1532
1533         /* Register queue IRQs */
1534         rc = oxt2_nix_register_queue_irqs(eth_dev);
1535         if (rc) {
1536                 otx2_err("Failed to register queue interrupts rc=%d", rc);
1537                 goto vlan_fini;
1538         }
1539
1540         /* Register cq IRQs */
1541         if (eth_dev->data->dev_conf.intr_conf.rxq) {
1542                 if (eth_dev->data->nb_rx_queues > dev->cints) {
1543                         otx2_err("Rx interrupt cannot be enabled, rxq > %d",
1544                                  dev->cints);
1545                         goto q_irq_fini;
1546                 }
1547                 /* Rx interrupt feature cannot work with vector mode because,
1548                  * vector mode doesn't process packets unless min 4 pkts are
1549                  * received, while cq interrupts are generated even for 1 pkt
1550                  * in the CQ.
1551                  */
1552                 dev->scalar_ena = true;
1553
1554                 rc = oxt2_nix_register_cq_irqs(eth_dev);
1555                 if (rc) {
1556                         otx2_err("Failed to register CQ interrupts rc=%d", rc);
1557                         goto q_irq_fini;
1558                 }
1559         }
1560
1561         /* Configure loop back mode */
1562         rc = cgx_intlbk_enable(dev, eth_dev->data->dev_conf.lpbk_mode);
1563         if (rc) {
1564                 otx2_err("Failed to configure cgx loop back mode rc=%d", rc);
1565                 goto q_irq_fini;
1566         }
1567
1568         rc = otx2_nix_rxchan_bpid_cfg(eth_dev, true);
1569         if (rc) {
1570                 otx2_err("Failed to configure nix rx chan bpid cfg rc=%d", rc);
1571                 goto q_irq_fini;
1572         }
1573
1574         /*
1575          * Restore queue config when reconfigure followed by
1576          * reconfigure and no queue configure invoked from application case.
1577          */
1578         if (dev->configured == 1) {
1579                 rc = nix_restore_queue_cfg(eth_dev);
1580                 if (rc)
1581                         goto cq_fini;
1582         }
1583
1584         /* Update the mac address */
1585         ea = eth_dev->data->mac_addrs;
1586         memcpy(ea, dev->mac_addr, RTE_ETHER_ADDR_LEN);
1587         if (rte_is_zero_ether_addr(ea))
1588                 rte_eth_random_addr((uint8_t *)ea);
1589
1590         rte_ether_format_addr(ea_fmt, RTE_ETHER_ADDR_FMT_SIZE, ea);
1591
1592         otx2_nix_dbg("Configured port%d mac=%s nb_rxq=%d nb_txq=%d"
1593                 " rx_offloads=0x%" PRIx64 " tx_offloads=0x%" PRIx64 ""
1594                 " rx_flags=0x%x tx_flags=0x%x",
1595                 eth_dev->data->port_id, ea_fmt, nb_rxq,
1596                 nb_txq, dev->rx_offloads, dev->tx_offloads,
1597                 dev->rx_offload_flags, dev->tx_offload_flags);
1598
1599         /* All good */
1600         dev->configured = 1;
1601         dev->configured_nb_rx_qs = data->nb_rx_queues;
1602         dev->configured_nb_tx_qs = data->nb_tx_queues;
1603         return 0;
1604
1605 cq_fini:
1606         oxt2_nix_unregister_cq_irqs(eth_dev);
1607 q_irq_fini:
1608         oxt2_nix_unregister_queue_irqs(eth_dev);
1609 vlan_fini:
1610         otx2_nix_vlan_fini(eth_dev);
1611 tm_fini:
1612         otx2_nix_tm_fini(eth_dev);
1613 free_nix_lf:
1614         nix_lf_free(dev);
1615 fail_offloads:
1616         dev->rx_offload_flags &= ~nix_rx_offload_flags(eth_dev);
1617         dev->tx_offload_flags &= ~nix_tx_offload_flags(eth_dev);
1618 fail_configure:
1619         dev->configured = 0;
1620         return rc;
1621 }
1622
1623 int
1624 otx2_nix_tx_queue_start(struct rte_eth_dev *eth_dev, uint16_t qidx)
1625 {
1626         struct rte_eth_dev_data *data = eth_dev->data;
1627         struct otx2_eth_txq *txq;
1628         int rc = -EINVAL;
1629
1630         txq = eth_dev->data->tx_queues[qidx];
1631
1632         if (data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
1633                 return 0;
1634
1635         rc = otx2_nix_sq_sqb_aura_fc(txq, true);
1636         if (rc) {
1637                 otx2_err("Failed to enable sqb aura fc, txq=%u, rc=%d",
1638                          qidx, rc);
1639                 goto done;
1640         }
1641
1642         data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
1643
1644 done:
1645         return rc;
1646 }
1647
1648 int
1649 otx2_nix_tx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qidx)
1650 {
1651         struct rte_eth_dev_data *data = eth_dev->data;
1652         struct otx2_eth_txq *txq;
1653         int rc;
1654
1655         txq = eth_dev->data->tx_queues[qidx];
1656
1657         if (data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
1658                 return 0;
1659
1660         txq->fc_cache_pkts = 0;
1661
1662         rc = otx2_nix_sq_sqb_aura_fc(txq, false);
1663         if (rc) {
1664                 otx2_err("Failed to disable sqb aura fc, txq=%u, rc=%d",
1665                          qidx, rc);
1666                 goto done;
1667         }
1668
1669         data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
1670
1671 done:
1672         return rc;
1673 }
1674
1675 static int
1676 otx2_nix_rx_queue_start(struct rte_eth_dev *eth_dev, uint16_t qidx)
1677 {
1678         struct otx2_eth_rxq *rxq = eth_dev->data->rx_queues[qidx];
1679         struct rte_eth_dev_data *data = eth_dev->data;
1680         int rc;
1681
1682         if (data->rx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
1683                 return 0;
1684
1685         rc = nix_rq_enb_dis(rxq->eth_dev, rxq, true);
1686         if (rc) {
1687                 otx2_err("Failed to enable rxq=%u, rc=%d", qidx, rc);
1688                 goto done;
1689         }
1690
1691         data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
1692
1693 done:
1694         return rc;
1695 }
1696
1697 static int
1698 otx2_nix_rx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qidx)
1699 {
1700         struct otx2_eth_rxq *rxq = eth_dev->data->rx_queues[qidx];
1701         struct rte_eth_dev_data *data = eth_dev->data;
1702         int rc;
1703
1704         if (data->rx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
1705                 return 0;
1706
1707         rc = nix_rq_enb_dis(rxq->eth_dev, rxq, false);
1708         if (rc) {
1709                 otx2_err("Failed to disable rxq=%u, rc=%d", qidx, rc);
1710                 goto done;
1711         }
1712
1713         data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
1714
1715 done:
1716         return rc;
1717 }
1718
1719 static void
1720 otx2_nix_dev_stop(struct rte_eth_dev *eth_dev)
1721 {
1722         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1723         struct rte_mbuf *rx_pkts[32];
1724         struct otx2_eth_rxq *rxq;
1725         int count, i, j, rc;
1726
1727         nix_cgx_stop_link_event(dev);
1728         npc_rx_disable(dev);
1729
1730         /* Stop rx queues and free up pkts pending */
1731         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1732                 rc = otx2_nix_rx_queue_stop(eth_dev, i);
1733                 if (rc)
1734                         continue;
1735
1736                 rxq = eth_dev->data->rx_queues[i];
1737                 count = dev->rx_pkt_burst_no_offload(rxq, rx_pkts, 32);
1738                 while (count) {
1739                         for (j = 0; j < count; j++)
1740                                 rte_pktmbuf_free(rx_pkts[j]);
1741                         count = dev->rx_pkt_burst_no_offload(rxq, rx_pkts, 32);
1742                 }
1743         }
1744
1745         /* Stop tx queues  */
1746         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1747                 otx2_nix_tx_queue_stop(eth_dev, i);
1748 }
1749
1750 static int
1751 otx2_nix_dev_start(struct rte_eth_dev *eth_dev)
1752 {
1753         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1754         int rc, i;
1755
1756         if (eth_dev->data->nb_rx_queues != 0) {
1757                 rc = otx2_nix_recalc_mtu(eth_dev);
1758                 if (rc)
1759                         return rc;
1760         }
1761
1762         /* Start rx queues */
1763         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1764                 rc = otx2_nix_rx_queue_start(eth_dev, i);
1765                 if (rc)
1766                         return rc;
1767         }
1768
1769         /* Start tx queues  */
1770         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
1771                 rc = otx2_nix_tx_queue_start(eth_dev, i);
1772                 if (rc)
1773                         return rc;
1774         }
1775
1776         rc = otx2_nix_update_flow_ctrl_mode(eth_dev);
1777         if (rc) {
1778                 otx2_err("Failed to update flow ctrl mode %d", rc);
1779                 return rc;
1780         }
1781
1782         /* Enable PTP if it was requested by the app or if it is already
1783          * enabled in PF owning this VF
1784          */
1785         memset(&dev->tstamp, 0, sizeof(struct otx2_timesync_info));
1786         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP) ||
1787             otx2_ethdev_is_ptp_en(dev))
1788                 otx2_nix_timesync_enable(eth_dev);
1789         else
1790                 otx2_nix_timesync_disable(eth_dev);
1791
1792         rc = npc_rx_enable(dev);
1793         if (rc) {
1794                 otx2_err("Failed to enable NPC rx %d", rc);
1795                 return rc;
1796         }
1797
1798         otx2_nix_toggle_flag_link_cfg(dev, true);
1799
1800         rc = nix_cgx_start_link_event(dev);
1801         if (rc) {
1802                 otx2_err("Failed to start cgx link event %d", rc);
1803                 goto rx_disable;
1804         }
1805
1806         otx2_nix_toggle_flag_link_cfg(dev, false);
1807         otx2_eth_set_tx_function(eth_dev);
1808         otx2_eth_set_rx_function(eth_dev);
1809
1810         return 0;
1811
1812 rx_disable:
1813         npc_rx_disable(dev);
1814         otx2_nix_toggle_flag_link_cfg(dev, false);
1815         return rc;
1816 }
1817
1818 static int otx2_nix_dev_reset(struct rte_eth_dev *eth_dev);
1819 static void otx2_nix_dev_close(struct rte_eth_dev *eth_dev);
1820
1821 /* Initialize and register driver with DPDK Application */
1822 static const struct eth_dev_ops otx2_eth_dev_ops = {
1823         .dev_infos_get            = otx2_nix_info_get,
1824         .dev_configure            = otx2_nix_configure,
1825         .link_update              = otx2_nix_link_update,
1826         .tx_queue_setup           = otx2_nix_tx_queue_setup,
1827         .tx_queue_release         = otx2_nix_tx_queue_release,
1828         .rx_queue_setup           = otx2_nix_rx_queue_setup,
1829         .rx_queue_release         = otx2_nix_rx_queue_release,
1830         .dev_start                = otx2_nix_dev_start,
1831         .dev_stop                 = otx2_nix_dev_stop,
1832         .dev_close                = otx2_nix_dev_close,
1833         .tx_queue_start           = otx2_nix_tx_queue_start,
1834         .tx_queue_stop            = otx2_nix_tx_queue_stop,
1835         .rx_queue_start           = otx2_nix_rx_queue_start,
1836         .rx_queue_stop            = otx2_nix_rx_queue_stop,
1837         .dev_set_link_up          = otx2_nix_dev_set_link_up,
1838         .dev_set_link_down        = otx2_nix_dev_set_link_down,
1839         .dev_supported_ptypes_get = otx2_nix_supported_ptypes_get,
1840         .dev_reset                = otx2_nix_dev_reset,
1841         .stats_get                = otx2_nix_dev_stats_get,
1842         .stats_reset              = otx2_nix_dev_stats_reset,
1843         .get_reg                  = otx2_nix_dev_get_reg,
1844         .mtu_set                  = otx2_nix_mtu_set,
1845         .mac_addr_add             = otx2_nix_mac_addr_add,
1846         .mac_addr_remove          = otx2_nix_mac_addr_del,
1847         .mac_addr_set             = otx2_nix_mac_addr_set,
1848         .promiscuous_enable       = otx2_nix_promisc_enable,
1849         .promiscuous_disable      = otx2_nix_promisc_disable,
1850         .allmulticast_enable      = otx2_nix_allmulticast_enable,
1851         .allmulticast_disable     = otx2_nix_allmulticast_disable,
1852         .queue_stats_mapping_set  = otx2_nix_queue_stats_mapping,
1853         .reta_update              = otx2_nix_dev_reta_update,
1854         .reta_query               = otx2_nix_dev_reta_query,
1855         .rss_hash_update          = otx2_nix_rss_hash_update,
1856         .rss_hash_conf_get        = otx2_nix_rss_hash_conf_get,
1857         .xstats_get               = otx2_nix_xstats_get,
1858         .xstats_get_names         = otx2_nix_xstats_get_names,
1859         .xstats_reset             = otx2_nix_xstats_reset,
1860         .xstats_get_by_id         = otx2_nix_xstats_get_by_id,
1861         .xstats_get_names_by_id   = otx2_nix_xstats_get_names_by_id,
1862         .rxq_info_get             = otx2_nix_rxq_info_get,
1863         .txq_info_get             = otx2_nix_txq_info_get,
1864         .rx_queue_count           = otx2_nix_rx_queue_count,
1865         .rx_descriptor_done       = otx2_nix_rx_descriptor_done,
1866         .rx_descriptor_status     = otx2_nix_rx_descriptor_status,
1867         .tx_descriptor_status     = otx2_nix_tx_descriptor_status,
1868         .tx_done_cleanup          = otx2_nix_tx_done_cleanup,
1869         .pool_ops_supported       = otx2_nix_pool_ops_supported,
1870         .filter_ctrl              = otx2_nix_dev_filter_ctrl,
1871         .get_module_info          = otx2_nix_get_module_info,
1872         .get_module_eeprom        = otx2_nix_get_module_eeprom,
1873         .fw_version_get           = otx2_nix_fw_version_get,
1874         .flow_ctrl_get            = otx2_nix_flow_ctrl_get,
1875         .flow_ctrl_set            = otx2_nix_flow_ctrl_set,
1876         .timesync_enable          = otx2_nix_timesync_enable,
1877         .timesync_disable         = otx2_nix_timesync_disable,
1878         .timesync_read_rx_timestamp = otx2_nix_timesync_read_rx_timestamp,
1879         .timesync_read_tx_timestamp = otx2_nix_timesync_read_tx_timestamp,
1880         .timesync_adjust_time     = otx2_nix_timesync_adjust_time,
1881         .timesync_read_time       = otx2_nix_timesync_read_time,
1882         .timesync_write_time      = otx2_nix_timesync_write_time,
1883         .vlan_offload_set         = otx2_nix_vlan_offload_set,
1884         .vlan_filter_set          = otx2_nix_vlan_filter_set,
1885         .vlan_strip_queue_set     = otx2_nix_vlan_strip_queue_set,
1886         .vlan_tpid_set            = otx2_nix_vlan_tpid_set,
1887         .vlan_pvid_set            = otx2_nix_vlan_pvid_set,
1888         .rx_queue_intr_enable     = otx2_nix_rx_queue_intr_enable,
1889         .rx_queue_intr_disable    = otx2_nix_rx_queue_intr_disable,
1890         .read_clock               = otx2_nix_read_clock,
1891 };
1892
1893 static inline int
1894 nix_lf_attach(struct otx2_eth_dev *dev)
1895 {
1896         struct otx2_mbox *mbox = dev->mbox;
1897         struct rsrc_attach_req *req;
1898
1899         /* Attach NIX(lf) */
1900         req = otx2_mbox_alloc_msg_attach_resources(mbox);
1901         req->modify = true;
1902         req->nixlf = true;
1903
1904         return otx2_mbox_process(mbox);
1905 }
1906
1907 static inline int
1908 nix_lf_get_msix_offset(struct otx2_eth_dev *dev)
1909 {
1910         struct otx2_mbox *mbox = dev->mbox;
1911         struct msix_offset_rsp *msix_rsp;
1912         int rc;
1913
1914         /* Get NPA and NIX MSIX vector offsets */
1915         otx2_mbox_alloc_msg_msix_offset(mbox);
1916
1917         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
1918
1919         dev->nix_msixoff = msix_rsp->nix_msixoff;
1920
1921         return rc;
1922 }
1923
1924 static inline int
1925 otx2_eth_dev_lf_detach(struct otx2_mbox *mbox)
1926 {
1927         struct rsrc_detach_req *req;
1928
1929         req = otx2_mbox_alloc_msg_detach_resources(mbox);
1930
1931         /* Detach all except npa lf */
1932         req->partial = true;
1933         req->nixlf = true;
1934         req->sso = true;
1935         req->ssow = true;
1936         req->timlfs = true;
1937         req->cptlfs = true;
1938
1939         return otx2_mbox_process(mbox);
1940 }
1941
1942 static int
1943 otx2_eth_dev_init(struct rte_eth_dev *eth_dev)
1944 {
1945         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1946         struct rte_pci_device *pci_dev;
1947         int rc, max_entries;
1948
1949         eth_dev->dev_ops = &otx2_eth_dev_ops;
1950
1951         /* For secondary processes, the primary has done all the work */
1952         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1953                 /* Setup callbacks for secondary process */
1954                 otx2_eth_set_tx_function(eth_dev);
1955                 otx2_eth_set_rx_function(eth_dev);
1956                 return 0;
1957         }
1958
1959         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1960
1961         rte_eth_copy_pci_info(eth_dev, pci_dev);
1962         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1963
1964         /* Zero out everything after OTX2_DEV to allow proper dev_reset() */
1965         memset(&dev->otx2_eth_dev_data_start, 0, sizeof(*dev) -
1966                 offsetof(struct otx2_eth_dev, otx2_eth_dev_data_start));
1967
1968         /* Parse devargs string */
1969         rc = otx2_ethdev_parse_devargs(eth_dev->device->devargs, dev);
1970         if (rc) {
1971                 otx2_err("Failed to parse devargs rc=%d", rc);
1972                 goto error;
1973         }
1974
1975         if (!dev->mbox_active) {
1976                 /* Initialize the base otx2_dev object
1977                  * only if already present
1978                  */
1979                 rc = otx2_dev_init(pci_dev, dev);
1980                 if (rc) {
1981                         otx2_err("Failed to initialize otx2_dev rc=%d", rc);
1982                         goto error;
1983                 }
1984         }
1985         /* Device generic callbacks */
1986         dev->ops = &otx2_dev_ops;
1987         dev->eth_dev = eth_dev;
1988
1989         /* Grab the NPA LF if required */
1990         rc = otx2_npa_lf_init(pci_dev, dev);
1991         if (rc)
1992                 goto otx2_dev_uninit;
1993
1994         dev->configured = 0;
1995         dev->drv_inited = true;
1996         dev->base = dev->bar2 + (RVU_BLOCK_ADDR_NIX0 << 20);
1997         dev->lmt_addr = dev->bar2 + (RVU_BLOCK_ADDR_LMT << 20);
1998
1999         /* Attach NIX LF */
2000         rc = nix_lf_attach(dev);
2001         if (rc)
2002                 goto otx2_npa_uninit;
2003
2004         /* Get NIX MSIX offset */
2005         rc = nix_lf_get_msix_offset(dev);
2006         if (rc)
2007                 goto otx2_npa_uninit;
2008
2009         /* Register LF irq handlers */
2010         rc = otx2_nix_register_irqs(eth_dev);
2011         if (rc)
2012                 goto mbox_detach;
2013
2014         /* Get maximum number of supported MAC entries */
2015         max_entries = otx2_cgx_mac_max_entries_get(dev);
2016         if (max_entries < 0) {
2017                 otx2_err("Failed to get max entries for mac addr");
2018                 rc = -ENOTSUP;
2019                 goto unregister_irq;
2020         }
2021
2022         /* For VFs, returned max_entries will be 0. But to keep default MAC
2023          * address, one entry must be allocated. So setting up to 1.
2024          */
2025         if (max_entries == 0)
2026                 max_entries = 1;
2027
2028         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", max_entries *
2029                                                RTE_ETHER_ADDR_LEN, 0);
2030         if (eth_dev->data->mac_addrs == NULL) {
2031                 otx2_err("Failed to allocate memory for mac addr");
2032                 rc = -ENOMEM;
2033                 goto unregister_irq;
2034         }
2035
2036         dev->max_mac_entries = max_entries;
2037
2038         rc = otx2_nix_mac_addr_get(eth_dev, dev->mac_addr);
2039         if (rc)
2040                 goto free_mac_addrs;
2041
2042         /* Update the mac address */
2043         memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
2044
2045         /* Also sync same MAC address to CGX table */
2046         otx2_cgx_mac_addr_set(eth_dev, &eth_dev->data->mac_addrs[0]);
2047
2048         /* Initialize the tm data structures */
2049         otx2_nix_tm_conf_init(eth_dev);
2050
2051         dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
2052         dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
2053
2054         if (otx2_dev_is_96xx_A0(dev) ||
2055             otx2_dev_is_95xx_Ax(dev)) {
2056                 dev->hwcap |= OTX2_FIXUP_F_MIN_4K_Q;
2057                 dev->hwcap |= OTX2_FIXUP_F_LIMIT_CQ_FULL;
2058         }
2059
2060         /* Initialize rte-flow */
2061         rc = otx2_flow_init(dev);
2062         if (rc)
2063                 goto free_mac_addrs;
2064
2065         otx2_nix_dbg("Port=%d pf=%d vf=%d ver=%s msix_off=%d hwcap=0x%" PRIx64
2066                      " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
2067                      eth_dev->data->port_id, dev->pf, dev->vf,
2068                      OTX2_ETH_DEV_PMD_VERSION, dev->nix_msixoff, dev->hwcap,
2069                      dev->rx_offload_capa, dev->tx_offload_capa);
2070         return 0;
2071
2072 free_mac_addrs:
2073         rte_free(eth_dev->data->mac_addrs);
2074 unregister_irq:
2075         otx2_nix_unregister_irqs(eth_dev);
2076 mbox_detach:
2077         otx2_eth_dev_lf_detach(dev->mbox);
2078 otx2_npa_uninit:
2079         otx2_npa_lf_fini();
2080 otx2_dev_uninit:
2081         otx2_dev_fini(pci_dev, dev);
2082 error:
2083         otx2_err("Failed to init nix eth_dev rc=%d", rc);
2084         return rc;
2085 }
2086
2087 static int
2088 otx2_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
2089 {
2090         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
2091         struct rte_pci_device *pci_dev;
2092         int rc, i;
2093
2094         /* Nothing to be done for secondary processes */
2095         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2096                 return 0;
2097
2098         /* Clear the flag since we are closing down */
2099         dev->configured = 0;
2100
2101         /* Disable nix bpid config */
2102         otx2_nix_rxchan_bpid_cfg(eth_dev, false);
2103
2104         npc_rx_disable(dev);
2105
2106         /* Disable vlan offloads */
2107         otx2_nix_vlan_fini(eth_dev);
2108
2109         /* Disable other rte_flow entries */
2110         otx2_flow_fini(dev);
2111
2112         /* Disable PTP if already enabled */
2113         if (otx2_ethdev_is_ptp_en(dev))
2114                 otx2_nix_timesync_disable(eth_dev);
2115
2116         nix_cgx_stop_link_event(dev);
2117
2118         /* Free up SQs */
2119         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
2120                 otx2_nix_tx_queue_release(eth_dev->data->tx_queues[i]);
2121                 eth_dev->data->tx_queues[i] = NULL;
2122         }
2123         eth_dev->data->nb_tx_queues = 0;
2124
2125         /* Free up RQ's and CQ's */
2126         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
2127                 otx2_nix_rx_queue_release(eth_dev->data->rx_queues[i]);
2128                 eth_dev->data->rx_queues[i] = NULL;
2129         }
2130         eth_dev->data->nb_rx_queues = 0;
2131
2132         /* Free tm resources */
2133         rc = otx2_nix_tm_fini(eth_dev);
2134         if (rc)
2135                 otx2_err("Failed to cleanup tm, rc=%d", rc);
2136
2137         /* Unregister queue irqs */
2138         oxt2_nix_unregister_queue_irqs(eth_dev);
2139
2140         /* Unregister cq irqs */
2141         if (eth_dev->data->dev_conf.intr_conf.rxq)
2142                 oxt2_nix_unregister_cq_irqs(eth_dev);
2143
2144         rc = nix_lf_free(dev);
2145         if (rc)
2146                 otx2_err("Failed to free nix lf, rc=%d", rc);
2147
2148         rc = otx2_npa_lf_fini();
2149         if (rc)
2150                 otx2_err("Failed to cleanup npa lf, rc=%d", rc);
2151
2152         rte_free(eth_dev->data->mac_addrs);
2153         eth_dev->data->mac_addrs = NULL;
2154         dev->drv_inited = false;
2155
2156         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2157         otx2_nix_unregister_irqs(eth_dev);
2158
2159         rc = otx2_eth_dev_lf_detach(dev->mbox);
2160         if (rc)
2161                 otx2_err("Failed to detach resources, rc=%d", rc);
2162
2163         /* Check if mbox close is needed */
2164         if (!mbox_close)
2165                 return 0;
2166
2167         if (otx2_npa_lf_active(dev) || otx2_dev_active_vfs(dev)) {
2168                 /* Will be freed later by PMD */
2169                 eth_dev->data->dev_private = NULL;
2170                 return 0;
2171         }
2172
2173         otx2_dev_fini(pci_dev, dev);
2174         return 0;
2175 }
2176
2177 static void
2178 otx2_nix_dev_close(struct rte_eth_dev *eth_dev)
2179 {
2180         otx2_eth_dev_uninit(eth_dev, true);
2181 }
2182
2183 static int
2184 otx2_nix_dev_reset(struct rte_eth_dev *eth_dev)
2185 {
2186         int rc;
2187
2188         rc = otx2_eth_dev_uninit(eth_dev, false);
2189         if (rc)
2190                 return rc;
2191
2192         return otx2_eth_dev_init(eth_dev);
2193 }
2194
2195 static int
2196 nix_remove(struct rte_pci_device *pci_dev)
2197 {
2198         struct rte_eth_dev *eth_dev;
2199         struct otx2_idev_cfg *idev;
2200         struct otx2_dev *otx2_dev;
2201         int rc;
2202
2203         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
2204         if (eth_dev) {
2205                 /* Cleanup eth dev */
2206                 rc = otx2_eth_dev_uninit(eth_dev, true);
2207                 if (rc)
2208                         return rc;
2209
2210                 rte_eth_dev_pci_release(eth_dev);
2211         }
2212
2213         /* Nothing to be done for secondary processes */
2214         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2215                 return 0;
2216
2217         /* Check for common resources */
2218         idev = otx2_intra_dev_get_cfg();
2219         if (!idev || !idev->npa_lf || idev->npa_lf->pci_dev != pci_dev)
2220                 return 0;
2221
2222         otx2_dev = container_of(idev->npa_lf, struct otx2_dev, npalf);
2223
2224         if (otx2_npa_lf_active(otx2_dev) || otx2_dev_active_vfs(otx2_dev))
2225                 goto exit;
2226
2227         /* Safe to cleanup mbox as no more users */
2228         otx2_dev_fini(pci_dev, otx2_dev);
2229         rte_free(otx2_dev);
2230         return 0;
2231
2232 exit:
2233         otx2_info("%s: common resource in use by other devices", pci_dev->name);
2234         return -EAGAIN;
2235 }
2236
2237 static int
2238 nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
2239 {
2240         int rc;
2241
2242         RTE_SET_USED(pci_drv);
2243
2244         rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct otx2_eth_dev),
2245                                            otx2_eth_dev_init);
2246
2247         /* On error on secondary, recheck if port exists in primary or
2248          * in mid of detach state.
2249          */
2250         if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
2251                 if (!rte_eth_dev_allocated(pci_dev->device.name))
2252                         return 0;
2253         return rc;
2254 }
2255
2256 static const struct rte_pci_id pci_nix_map[] = {
2257         {
2258                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF)
2259         },
2260         {
2261                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_VF)
2262         },
2263         {
2264                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
2265                                PCI_DEVID_OCTEONTX2_RVU_AF_VF)
2266         },
2267         {
2268                 .vendor_id = 0,
2269         },
2270 };
2271
2272 static struct rte_pci_driver pci_nix = {
2273         .id_table = pci_nix_map,
2274         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA |
2275                         RTE_PCI_DRV_INTR_LSC,
2276         .probe = nix_probe,
2277         .remove = nix_remove,
2278 };
2279
2280 RTE_PMD_REGISTER_PCI(net_octeontx2, pci_nix);
2281 RTE_PMD_REGISTER_PCI_TABLE(net_octeontx2, pci_nix_map);
2282 RTE_PMD_REGISTER_KMOD_DEP(net_octeontx2, "vfio-pci");