806e95f064c79e33b198a198f1bfb8c33446d2be
[dpdk.git] / drivers / net / cnxk / cn9k_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 #include "cn9k_ethdev.h"
5 #include "cn9k_rx.h"
6 #include "cn9k_tx.h"
7
8 static int
9 cn9k_nix_ptypes_set(struct rte_eth_dev *eth_dev, uint32_t ptype_mask)
10 {
11         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
12
13         if (ptype_mask) {
14                 dev->rx_offload_flags |= NIX_RX_OFFLOAD_PTYPE_F;
15                 dev->ptype_disable = 0;
16         } else {
17                 dev->rx_offload_flags &= ~NIX_RX_OFFLOAD_PTYPE_F;
18                 dev->ptype_disable = 1;
19         }
20
21         return 0;
22 }
23
24 static void
25 nix_form_default_desc(struct cnxk_eth_dev *dev, struct cn9k_eth_txq *txq,
26                       uint16_t qid)
27 {
28         struct nix_send_ext_s *send_hdr_ext;
29         struct nix_send_hdr_s *send_hdr;
30         union nix_send_sg_s *sg;
31
32         RTE_SET_USED(dev);
33
34         /* Initialize the fields based on basic single segment packet */
35         memset(&txq->cmd, 0, sizeof(txq->cmd));
36
37         if (dev->tx_offload_flags & NIX_TX_NEED_EXT_HDR) {
38                 send_hdr = (struct nix_send_hdr_s *)&txq->cmd[0];
39                 /* 2(HDR) + 2(EXT_HDR) + 1(SG) + 1(IOVA) = 6/2 - 1 = 2 */
40                 send_hdr->w0.sizem1 = 2;
41
42                 send_hdr_ext = (struct nix_send_ext_s *)&txq->cmd[2];
43                 send_hdr_ext->w0.subdc = NIX_SUBDC_EXT;
44                 sg = (union nix_send_sg_s *)&txq->cmd[4];
45         } else {
46                 send_hdr = (struct nix_send_hdr_s *)&txq->cmd[0];
47                 /* 2(HDR) + 1(SG) + 1(IOVA) = 4/2 - 1 = 1 */
48                 send_hdr->w0.sizem1 = 1;
49                 sg = (union nix_send_sg_s *)&txq->cmd[2];
50         }
51
52         send_hdr->w0.sq = qid;
53         sg->subdc = NIX_SUBDC_SG;
54         sg->segs = 1;
55         sg->ld_type = NIX_SENDLDTYPE_LDD;
56
57         rte_wmb();
58 }
59
60 static int
61 cn9k_nix_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t qid,
62                         uint16_t nb_desc, unsigned int socket,
63                         const struct rte_eth_txconf *tx_conf)
64 {
65         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
66         struct cn9k_eth_txq *txq;
67         struct roc_nix_sq *sq;
68         int rc;
69
70         RTE_SET_USED(socket);
71
72         /* Common Tx queue setup */
73         rc = cnxk_nix_tx_queue_setup(eth_dev, qid, nb_desc,
74                                      sizeof(struct cn9k_eth_txq), tx_conf);
75         if (rc)
76                 return rc;
77
78         sq = &dev->sqs[qid];
79         /* Update fast path queue */
80         txq = eth_dev->data->tx_queues[qid];
81         txq->fc_mem = sq->fc;
82         txq->lmt_addr = sq->lmt_addr;
83         txq->io_addr = sq->io_addr;
84         txq->nb_sqb_bufs_adj = sq->nb_sqb_bufs_adj;
85         txq->sqes_per_sqb_log2 = sq->sqes_per_sqb_log2;
86
87         nix_form_default_desc(dev, txq, qid);
88         txq->lso_tun_fmt = dev->lso_tun_fmt;
89         return 0;
90 }
91
92 static int
93 cn9k_nix_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t qid,
94                         uint16_t nb_desc, unsigned int socket,
95                         const struct rte_eth_rxconf *rx_conf,
96                         struct rte_mempool *mp)
97 {
98         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
99         struct cn9k_eth_rxq *rxq;
100         struct roc_nix_rq *rq;
101         struct roc_nix_cq *cq;
102         int rc;
103
104         RTE_SET_USED(socket);
105
106         /* CQ Errata needs min 4K ring */
107         if (dev->cq_min_4k && nb_desc < 4096)
108                 nb_desc = 4096;
109
110         /* Common Rx queue setup */
111         rc = cnxk_nix_rx_queue_setup(eth_dev, qid, nb_desc,
112                                      sizeof(struct cn9k_eth_rxq), rx_conf, mp);
113         if (rc)
114                 return rc;
115
116         rq = &dev->rqs[qid];
117         cq = &dev->cqs[qid];
118
119         /* Update fast path queue */
120         rxq = eth_dev->data->rx_queues[qid];
121         rxq->rq = qid;
122         rxq->desc = (uintptr_t)cq->desc_base;
123         rxq->cq_door = cq->door;
124         rxq->cq_status = cq->status;
125         rxq->wdata = cq->wdata;
126         rxq->head = cq->head;
127         rxq->qmask = cq->qmask;
128
129         /* Data offset from data to start of mbuf is first_skip */
130         rxq->data_off = rq->first_skip;
131         rxq->mbuf_initializer = cnxk_nix_rxq_mbuf_setup(dev);
132
133         /* Lookup mem */
134         rxq->lookup_mem = cnxk_nix_fastpath_lookup_mem_get();
135         return 0;
136 }
137
138 static int
139 cn9k_nix_tx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qidx)
140 {
141         struct cn9k_eth_txq *txq = eth_dev->data->tx_queues[qidx];
142         int rc;
143
144         rc = cnxk_nix_tx_queue_stop(eth_dev, qidx);
145         if (rc)
146                 return rc;
147
148         /* Clear fc cache pkts to trigger worker stop */
149         txq->fc_cache_pkts = 0;
150         return 0;
151 }
152
153 static int
154 cn9k_nix_configure(struct rte_eth_dev *eth_dev)
155 {
156         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
157         struct rte_eth_conf *conf = &eth_dev->data->dev_conf;
158         struct rte_eth_txmode *txmode = &conf->txmode;
159         int rc;
160
161         /* Platform specific checks */
162         if ((roc_model_is_cn96_a0() || roc_model_is_cn95_a0()) &&
163             (txmode->offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
164             ((txmode->offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
165              (txmode->offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM))) {
166                 plt_err("Outer IP and SCTP checksum unsupported");
167                 return -EINVAL;
168         }
169
170         /* Common nix configure */
171         rc = cnxk_nix_configure(eth_dev);
172         if (rc)
173                 return rc;
174
175         plt_nix_dbg("Configured port%d platform specific rx_offload_flags=%x"
176                     " tx_offload_flags=0x%x",
177                     eth_dev->data->port_id, dev->rx_offload_flags,
178                     dev->tx_offload_flags);
179         return 0;
180 }
181
182 /* Update platform specific eth dev ops */
183 static void
184 nix_eth_dev_ops_override(void)
185 {
186         static int init_once;
187
188         if (init_once)
189                 return;
190         init_once = 1;
191
192         /* Update platform specific ops */
193         cnxk_eth_dev_ops.dev_configure = cn9k_nix_configure;
194         cnxk_eth_dev_ops.tx_queue_setup = cn9k_nix_tx_queue_setup;
195         cnxk_eth_dev_ops.rx_queue_setup = cn9k_nix_rx_queue_setup;
196         cnxk_eth_dev_ops.tx_queue_stop = cn9k_nix_tx_queue_stop;
197         cnxk_eth_dev_ops.dev_ptypes_set = cn9k_nix_ptypes_set;
198 }
199
200 static int
201 cn9k_nix_remove(struct rte_pci_device *pci_dev)
202 {
203         return cnxk_nix_remove(pci_dev);
204 }
205
206 static int
207 cn9k_nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
208 {
209         struct rte_eth_dev *eth_dev;
210         struct cnxk_eth_dev *dev;
211         int rc;
212
213         if (RTE_CACHE_LINE_SIZE != 128) {
214                 plt_err("Driver not compiled for CN9K");
215                 return -EFAULT;
216         }
217
218         rc = roc_plt_init();
219         if (rc) {
220                 plt_err("Failed to initialize platform model, rc=%d", rc);
221                 return rc;
222         }
223
224         nix_eth_dev_ops_override();
225
226         /* Common probe */
227         rc = cnxk_nix_probe(pci_drv, pci_dev);
228         if (rc)
229                 return rc;
230
231         /* Find eth dev allocated */
232         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
233         if (!eth_dev)
234                 return -ENOENT;
235
236         dev = cnxk_eth_pmd_priv(eth_dev);
237         /* Update capabilities already set for TSO.
238          * TSO not supported for earlier chip revisions
239          */
240         if (roc_model_is_cn96_a0() || roc_model_is_cn95_a0())
241                 dev->tx_offload_capa &= ~(DEV_TX_OFFLOAD_TCP_TSO |
242                                           DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
243                                           DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
244                                           DEV_TX_OFFLOAD_GRE_TNL_TSO);
245
246         /* 50G and 100G to be supported for board version C0
247          * and above of CN9K.
248          */
249         if (roc_model_is_cn96_a0() || roc_model_is_cn95_a0()) {
250                 dev->speed_capa &= ~(uint64_t)ETH_LINK_SPEED_50G;
251                 dev->speed_capa &= ~(uint64_t)ETH_LINK_SPEED_100G;
252         }
253
254         dev->hwcap = 0;
255
256         /* Update HW erratas */
257         if (roc_model_is_cn96_a0() || roc_model_is_cn95_a0())
258                 dev->cq_min_4k = 1;
259         return 0;
260 }
261
262 static const struct rte_pci_id cn9k_pci_nix_map[] = {
263         {
264                 .vendor_id = 0,
265         },
266 };
267
268 static struct rte_pci_driver cn9k_pci_nix = {
269         .id_table = cn9k_pci_nix_map,
270         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA |
271                      RTE_PCI_DRV_INTR_LSC,
272         .probe = cn9k_nix_probe,
273         .remove = cn9k_nix_remove,
274 };
275
276 RTE_PMD_REGISTER_PCI(net_cn9k, cn9k_pci_nix);
277 RTE_PMD_REGISTER_PCI_TABLE(net_cn9k, cn9k_pci_nix_map);
278 RTE_PMD_REGISTER_KMOD_DEP(net_cn9k, "vfio-pci");