net/cnxk: support flow RSS
[dpdk.git] / drivers / net / cnxk / cn10k_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 #include "cn10k_ethdev.h"
5 #include "cn10k_rte_flow.h"
6 #include "cn10k_rx.h"
7 #include "cn10k_tx.h"
8
9 static uint16_t
10 nix_rx_offload_flags(struct rte_eth_dev *eth_dev)
11 {
12         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
13         struct rte_eth_dev_data *data = eth_dev->data;
14         struct rte_eth_conf *conf = &data->dev_conf;
15         struct rte_eth_rxmode *rxmode = &conf->rxmode;
16         uint16_t flags = 0;
17
18         if (rxmode->mq_mode == ETH_MQ_RX_RSS &&
19             (dev->rx_offloads & DEV_RX_OFFLOAD_RSS_HASH))
20                 flags |= NIX_RX_OFFLOAD_RSS_F;
21
22         if (dev->rx_offloads &
23             (DEV_RX_OFFLOAD_TCP_CKSUM | DEV_RX_OFFLOAD_UDP_CKSUM))
24                 flags |= NIX_RX_OFFLOAD_CHECKSUM_F;
25
26         if (dev->rx_offloads &
27             (DEV_RX_OFFLOAD_IPV4_CKSUM | DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM))
28                 flags |= NIX_RX_OFFLOAD_CHECKSUM_F;
29
30         if (dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER)
31                 flags |= NIX_RX_MULTI_SEG_F;
32
33         if (!dev->ptype_disable)
34                 flags |= NIX_RX_OFFLOAD_PTYPE_F;
35
36         return flags;
37 }
38
39 static uint16_t
40 nix_tx_offload_flags(struct rte_eth_dev *eth_dev)
41 {
42         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
43         uint64_t conf = dev->tx_offloads;
44         uint16_t flags = 0;
45
46         /* Fastpath is dependent on these enums */
47         RTE_BUILD_BUG_ON(PKT_TX_TCP_CKSUM != (1ULL << 52));
48         RTE_BUILD_BUG_ON(PKT_TX_SCTP_CKSUM != (2ULL << 52));
49         RTE_BUILD_BUG_ON(PKT_TX_UDP_CKSUM != (3ULL << 52));
50         RTE_BUILD_BUG_ON(PKT_TX_IP_CKSUM != (1ULL << 54));
51         RTE_BUILD_BUG_ON(PKT_TX_IPV4 != (1ULL << 55));
52         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IP_CKSUM != (1ULL << 58));
53         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IPV4 != (1ULL << 59));
54         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IPV6 != (1ULL << 60));
55         RTE_BUILD_BUG_ON(PKT_TX_OUTER_UDP_CKSUM != (1ULL << 41));
56         RTE_BUILD_BUG_ON(RTE_MBUF_L2_LEN_BITS != 7);
57         RTE_BUILD_BUG_ON(RTE_MBUF_L3_LEN_BITS != 9);
58         RTE_BUILD_BUG_ON(RTE_MBUF_OUTL2_LEN_BITS != 7);
59         RTE_BUILD_BUG_ON(RTE_MBUF_OUTL3_LEN_BITS != 9);
60         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) !=
61                          offsetof(struct rte_mbuf, buf_iova) + 8);
62         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
63                          offsetof(struct rte_mbuf, buf_iova) + 16);
64         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
65                          offsetof(struct rte_mbuf, ol_flags) + 12);
66         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, tx_offload) !=
67                          offsetof(struct rte_mbuf, pool) + 2 * sizeof(void *));
68
69         if (conf & DEV_TX_OFFLOAD_VLAN_INSERT ||
70             conf & DEV_TX_OFFLOAD_QINQ_INSERT)
71                 flags |= NIX_TX_OFFLOAD_VLAN_QINQ_F;
72
73         if (conf & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM ||
74             conf & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM)
75                 flags |= NIX_TX_OFFLOAD_OL3_OL4_CSUM_F;
76
77         if (conf & DEV_TX_OFFLOAD_IPV4_CKSUM ||
78             conf & DEV_TX_OFFLOAD_TCP_CKSUM ||
79             conf & DEV_TX_OFFLOAD_UDP_CKSUM || conf & DEV_TX_OFFLOAD_SCTP_CKSUM)
80                 flags |= NIX_TX_OFFLOAD_L3_L4_CSUM_F;
81
82         if (!(conf & DEV_TX_OFFLOAD_MBUF_FAST_FREE))
83                 flags |= NIX_TX_OFFLOAD_MBUF_NOFF_F;
84
85         if (conf & DEV_TX_OFFLOAD_MULTI_SEGS)
86                 flags |= NIX_TX_MULTI_SEG_F;
87
88         /* Enable Inner checksum for TSO */
89         if (conf & DEV_TX_OFFLOAD_TCP_TSO)
90                 flags |= (NIX_TX_OFFLOAD_TSO_F | NIX_TX_OFFLOAD_L3_L4_CSUM_F);
91
92         /* Enable Inner and Outer checksum for Tunnel TSO */
93         if (conf & (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
94                     DEV_TX_OFFLOAD_GENEVE_TNL_TSO | DEV_TX_OFFLOAD_GRE_TNL_TSO))
95                 flags |= (NIX_TX_OFFLOAD_TSO_F | NIX_TX_OFFLOAD_OL3_OL4_CSUM_F |
96                           NIX_TX_OFFLOAD_L3_L4_CSUM_F);
97
98         return flags;
99 }
100
101 static int
102 cn10k_nix_ptypes_set(struct rte_eth_dev *eth_dev, uint32_t ptype_mask)
103 {
104         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
105
106         if (ptype_mask) {
107                 dev->rx_offload_flags |= NIX_RX_OFFLOAD_PTYPE_F;
108                 dev->ptype_disable = 0;
109         } else {
110                 dev->rx_offload_flags &= ~NIX_RX_OFFLOAD_PTYPE_F;
111                 dev->ptype_disable = 1;
112         }
113
114         cn10k_eth_set_rx_function(eth_dev);
115         return 0;
116 }
117
118 static void
119 nix_form_default_desc(struct cnxk_eth_dev *dev, struct cn10k_eth_txq *txq,
120                       uint16_t qid)
121 {
122         struct nix_send_ext_s *send_hdr_ext;
123         union nix_send_hdr_w0_u send_hdr_w0;
124         union nix_send_sg_s sg_w0;
125
126         RTE_SET_USED(dev);
127
128         /* Initialize the fields based on basic single segment packet */
129         memset(&txq->cmd, 0, sizeof(txq->cmd));
130         send_hdr_w0.u = 0;
131         sg_w0.u = 0;
132
133         if (dev->tx_offload_flags & NIX_TX_NEED_EXT_HDR) {
134                 /* 2(HDR) + 2(EXT_HDR) + 1(SG) + 1(IOVA) = 6/2 - 1 = 2 */
135                 send_hdr_w0.sizem1 = 2;
136
137                 send_hdr_ext = (struct nix_send_ext_s *)&txq->cmd[0];
138                 send_hdr_ext->w0.subdc = NIX_SUBDC_EXT;
139         } else {
140                 /* 2(HDR) + 1(SG) + 1(IOVA) = 4/2 - 1 = 1 */
141                 send_hdr_w0.sizem1 = 1;
142         }
143
144         send_hdr_w0.sq = qid;
145         sg_w0.subdc = NIX_SUBDC_SG;
146         sg_w0.segs = 1;
147         sg_w0.ld_type = NIX_SENDLDTYPE_LDD;
148
149         txq->send_hdr_w0 = send_hdr_w0.u;
150         txq->sg_w0 = sg_w0.u;
151
152         rte_wmb();
153 }
154
155 static int
156 cn10k_nix_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t qid,
157                          uint16_t nb_desc, unsigned int socket,
158                          const struct rte_eth_txconf *tx_conf)
159 {
160         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
161         struct cn10k_eth_txq *txq;
162         struct roc_nix_sq *sq;
163         int rc;
164
165         RTE_SET_USED(socket);
166
167         /* Common Tx queue setup */
168         rc = cnxk_nix_tx_queue_setup(eth_dev, qid, nb_desc,
169                                      sizeof(struct cn10k_eth_txq), tx_conf);
170         if (rc)
171                 return rc;
172
173         sq = &dev->sqs[qid];
174         /* Update fast path queue */
175         txq = eth_dev->data->tx_queues[qid];
176         txq->fc_mem = sq->fc;
177         /* Store lmt base in tx queue for easy access */
178         txq->lmt_base = dev->nix.lmt_base;
179         txq->io_addr = sq->io_addr;
180         txq->nb_sqb_bufs_adj = sq->nb_sqb_bufs_adj;
181         txq->sqes_per_sqb_log2 = sq->sqes_per_sqb_log2;
182
183         nix_form_default_desc(dev, txq, qid);
184         txq->lso_tun_fmt = dev->lso_tun_fmt;
185         return 0;
186 }
187
188 static int
189 cn10k_nix_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t qid,
190                          uint16_t nb_desc, unsigned int socket,
191                          const struct rte_eth_rxconf *rx_conf,
192                          struct rte_mempool *mp)
193 {
194         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
195         struct cn10k_eth_rxq *rxq;
196         struct roc_nix_rq *rq;
197         struct roc_nix_cq *cq;
198         int rc;
199
200         RTE_SET_USED(socket);
201
202         /* CQ Errata needs min 4K ring */
203         if (dev->cq_min_4k && nb_desc < 4096)
204                 nb_desc = 4096;
205
206         /* Common Rx queue setup */
207         rc = cnxk_nix_rx_queue_setup(eth_dev, qid, nb_desc,
208                                      sizeof(struct cn10k_eth_rxq), rx_conf, mp);
209         if (rc)
210                 return rc;
211
212         rq = &dev->rqs[qid];
213         cq = &dev->cqs[qid];
214
215         /* Update fast path queue */
216         rxq = eth_dev->data->rx_queues[qid];
217         rxq->rq = qid;
218         rxq->desc = (uintptr_t)cq->desc_base;
219         rxq->cq_door = cq->door;
220         rxq->cq_status = cq->status;
221         rxq->wdata = cq->wdata;
222         rxq->head = cq->head;
223         rxq->qmask = cq->qmask;
224
225         /* Data offset from data to start of mbuf is first_skip */
226         rxq->data_off = rq->first_skip;
227         rxq->mbuf_initializer = cnxk_nix_rxq_mbuf_setup(dev);
228
229         /* Lookup mem */
230         rxq->lookup_mem = cnxk_nix_fastpath_lookup_mem_get();
231         return 0;
232 }
233
234 static int
235 cn10k_nix_tx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qidx)
236 {
237         struct cn10k_eth_txq *txq = eth_dev->data->tx_queues[qidx];
238         int rc;
239
240         rc = cnxk_nix_tx_queue_stop(eth_dev, qidx);
241         if (rc)
242                 return rc;
243
244         /* Clear fc cache pkts to trigger worker stop */
245         txq->fc_cache_pkts = 0;
246         return 0;
247 }
248
249 static int
250 cn10k_nix_configure(struct rte_eth_dev *eth_dev)
251 {
252         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
253         int rc;
254
255         /* Common nix configure */
256         rc = cnxk_nix_configure(eth_dev);
257         if (rc)
258                 return rc;
259
260         /* Update offload flags */
261         dev->rx_offload_flags = nix_rx_offload_flags(eth_dev);
262         dev->tx_offload_flags = nix_tx_offload_flags(eth_dev);
263
264         plt_nix_dbg("Configured port%d platform specific rx_offload_flags=%x"
265                     " tx_offload_flags=0x%x",
266                     eth_dev->data->port_id, dev->rx_offload_flags,
267                     dev->tx_offload_flags);
268         return 0;
269 }
270
271 static int
272 cn10k_nix_dev_start(struct rte_eth_dev *eth_dev)
273 {
274         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
275         int rc;
276
277         /* Common eth dev start */
278         rc = cnxk_nix_dev_start(eth_dev);
279         if (rc)
280                 return rc;
281
282         /* Setting up the rx[tx]_offload_flags due to change
283          * in rx[tx]_offloads.
284          */
285         dev->rx_offload_flags |= nix_rx_offload_flags(eth_dev);
286         dev->tx_offload_flags |= nix_tx_offload_flags(eth_dev);
287
288         cn10k_eth_set_tx_function(eth_dev);
289         cn10k_eth_set_rx_function(eth_dev);
290         return 0;
291 }
292
293 /* Update platform specific eth dev ops */
294 static void
295 nix_eth_dev_ops_override(void)
296 {
297         static int init_once;
298
299         if (init_once)
300                 return;
301         init_once = 1;
302
303         /* Update platform specific ops */
304         cnxk_eth_dev_ops.dev_configure = cn10k_nix_configure;
305         cnxk_eth_dev_ops.tx_queue_setup = cn10k_nix_tx_queue_setup;
306         cnxk_eth_dev_ops.rx_queue_setup = cn10k_nix_rx_queue_setup;
307         cnxk_eth_dev_ops.tx_queue_stop = cn10k_nix_tx_queue_stop;
308         cnxk_eth_dev_ops.dev_start = cn10k_nix_dev_start;
309         cnxk_eth_dev_ops.dev_ptypes_set = cn10k_nix_ptypes_set;
310 }
311
312 static void
313 npc_flow_ops_override(void)
314 {
315         static int init_once;
316
317         if (init_once)
318                 return;
319         init_once = 1;
320
321         /* Update platform specific ops */
322         cnxk_flow_ops.create = cn10k_flow_create;
323         cnxk_flow_ops.destroy = cn10k_flow_destroy;
324 }
325
326 static int
327 cn10k_nix_remove(struct rte_pci_device *pci_dev)
328 {
329         return cnxk_nix_remove(pci_dev);
330 }
331
332 static int
333 cn10k_nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
334 {
335         struct rte_eth_dev *eth_dev;
336         int rc;
337
338         if (RTE_CACHE_LINE_SIZE != 64) {
339                 plt_err("Driver not compiled for CN10K");
340                 return -EFAULT;
341         }
342
343         rc = roc_plt_init();
344         if (rc) {
345                 plt_err("Failed to initialize platform model, rc=%d", rc);
346                 return rc;
347         }
348
349         nix_eth_dev_ops_override();
350         npc_flow_ops_override();
351
352         /* Common probe */
353         rc = cnxk_nix_probe(pci_drv, pci_dev);
354         if (rc)
355                 return rc;
356
357         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
358                 eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
359                 if (!eth_dev)
360                         return -ENOENT;
361
362                 /* Setup callbacks for secondary process */
363                 cn10k_eth_set_tx_function(eth_dev);
364                 cn10k_eth_set_rx_function(eth_dev);
365         }
366         return 0;
367 }
368
369 static const struct rte_pci_id cn10k_pci_nix_map[] = {
370         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KA, PCI_DEVID_CNXK_RVU_PF),
371         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KAS, PCI_DEVID_CNXK_RVU_PF),
372         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KA, PCI_DEVID_CNXK_RVU_VF),
373         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KAS, PCI_DEVID_CNXK_RVU_VF),
374         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KA, PCI_DEVID_CNXK_RVU_AF_VF),
375         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KAS, PCI_DEVID_CNXK_RVU_AF_VF),
376         {
377                 .vendor_id = 0,
378         },
379 };
380
381 static struct rte_pci_driver cn10k_pci_nix = {
382         .id_table = cn10k_pci_nix_map,
383         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA |
384                      RTE_PCI_DRV_INTR_LSC,
385         .probe = cn10k_nix_probe,
386         .remove = cn10k_nix_remove,
387 };
388
389 RTE_PMD_REGISTER_PCI(net_cn10k, cn10k_pci_nix);
390 RTE_PMD_REGISTER_PCI_TABLE(net_cn10k, cn10k_pci_nix_map);
391 RTE_PMD_REGISTER_KMOD_DEP(net_cn10k, "vfio-pci");