net/octeontx2: support SDP interface
[dpdk.git] / drivers / net / octeontx2 / otx2_ethdev_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_mbuf_pool_ops.h>
6
7 #include "otx2_ethdev.h"
8
9 int
10 otx2_nix_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
11 {
12         uint32_t buffsz, frame_size = mtu + NIX_L2_OVERHEAD;
13         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
14         struct rte_eth_dev_data *data = eth_dev->data;
15         struct otx2_mbox *mbox = dev->mbox;
16         struct nix_frs_cfg *req;
17         int rc;
18
19         /* Check if MTU is within the allowed range */
20         if (frame_size < NIX_MIN_FRS || frame_size > NIX_MAX_FRS)
21                 return -EINVAL;
22
23         buffsz = data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
24
25         /* Refuse MTU that requires the support of scattered packets
26          * when this feature has not been enabled before.
27          */
28         if (data->dev_started && frame_size > buffsz &&
29             !(dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER))
30                 return -EINVAL;
31
32         /* Check <seg size> * <max_seg>  >= max_frame */
33         if ((dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER) &&
34             (frame_size > buffsz * NIX_RX_NB_SEG_MAX))
35                 return -EINVAL;
36
37         req = otx2_mbox_alloc_msg_nix_set_hw_frs(mbox);
38         req->update_smq = true;
39         if (otx2_dev_is_sdp(dev))
40                 req->sdp_link = true;
41         /* FRS HW config should exclude FCS but include NPC VTAG insert size */
42         req->maxlen = frame_size - RTE_ETHER_CRC_LEN + NIX_MAX_VTAG_ACT_SIZE;
43
44         rc = otx2_mbox_process(mbox);
45         if (rc)
46                 return rc;
47
48         /* Now just update Rx MAXLEN */
49         req = otx2_mbox_alloc_msg_nix_set_hw_frs(mbox);
50         req->maxlen = frame_size - RTE_ETHER_CRC_LEN;
51         if (otx2_dev_is_sdp(dev))
52                 req->sdp_link = true;
53
54         rc = otx2_mbox_process(mbox);
55         if (rc)
56                 return rc;
57
58         if (frame_size > RTE_ETHER_MAX_LEN)
59                 dev->rx_offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
60         else
61                 dev->rx_offloads &= ~DEV_RX_OFFLOAD_JUMBO_FRAME;
62
63         /* Update max_rx_pkt_len */
64         data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
65
66         return rc;
67 }
68
69 int
70 otx2_nix_recalc_mtu(struct rte_eth_dev *eth_dev)
71 {
72         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
73         struct rte_eth_dev_data *data = eth_dev->data;
74         struct rte_pktmbuf_pool_private *mbp_priv;
75         struct otx2_eth_rxq *rxq;
76         uint32_t buffsz;
77         uint16_t mtu;
78         int rc;
79
80         /* Get rx buffer size */
81         rxq = data->rx_queues[0];
82         mbp_priv = rte_mempool_get_priv(rxq->pool);
83         buffsz = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
84
85         /* Setup scatter mode if needed by jumbo */
86         if (data->dev_conf.rxmode.max_rx_pkt_len > buffsz)
87                 dev->rx_offloads |= DEV_RX_OFFLOAD_SCATTER;
88
89         /* Setup MTU based on max_rx_pkt_len */
90         mtu = data->dev_conf.rxmode.max_rx_pkt_len - NIX_L2_OVERHEAD;
91
92         rc = otx2_nix_mtu_set(eth_dev, mtu);
93         if (rc)
94                 otx2_err("Failed to set default MTU size %d", rc);
95
96         return rc;
97 }
98
99 static void
100 nix_cgx_promisc_config(struct rte_eth_dev *eth_dev, int en)
101 {
102         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
103         struct otx2_mbox *mbox = dev->mbox;
104
105         if (otx2_dev_is_vf_or_sdp(dev))
106                 return;
107
108         if (en)
109                 otx2_mbox_alloc_msg_cgx_promisc_enable(mbox);
110         else
111                 otx2_mbox_alloc_msg_cgx_promisc_disable(mbox);
112
113         otx2_mbox_process(mbox);
114 }
115
116 void
117 otx2_nix_promisc_config(struct rte_eth_dev *eth_dev, int en)
118 {
119         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
120         struct otx2_mbox *mbox = dev->mbox;
121         struct nix_rx_mode *req;
122
123         if (otx2_dev_is_vf(dev))
124                 return;
125
126         req = otx2_mbox_alloc_msg_nix_set_rx_mode(mbox);
127
128         if (en)
129                 req->mode = NIX_RX_MODE_UCAST | NIX_RX_MODE_PROMISC;
130
131         otx2_mbox_process(mbox);
132         eth_dev->data->promiscuous = en;
133         otx2_nix_vlan_update_promisc(eth_dev, en);
134 }
135
136 int
137 otx2_nix_promisc_enable(struct rte_eth_dev *eth_dev)
138 {
139         otx2_nix_promisc_config(eth_dev, 1);
140         nix_cgx_promisc_config(eth_dev, 1);
141
142         return 0;
143 }
144
145 int
146 otx2_nix_promisc_disable(struct rte_eth_dev *eth_dev)
147 {
148         otx2_nix_promisc_config(eth_dev, 0);
149         nix_cgx_promisc_config(eth_dev, 0);
150
151         return 0;
152 }
153
154 static void
155 nix_allmulticast_config(struct rte_eth_dev *eth_dev, int en)
156 {
157         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
158         struct otx2_mbox *mbox = dev->mbox;
159         struct nix_rx_mode *req;
160
161         if (otx2_dev_is_vf(dev))
162                 return;
163
164         req = otx2_mbox_alloc_msg_nix_set_rx_mode(mbox);
165
166         if (en)
167                 req->mode = NIX_RX_MODE_UCAST | NIX_RX_MODE_ALLMULTI;
168         else if (eth_dev->data->promiscuous)
169                 req->mode = NIX_RX_MODE_UCAST | NIX_RX_MODE_PROMISC;
170
171         otx2_mbox_process(mbox);
172 }
173
174 int
175 otx2_nix_allmulticast_enable(struct rte_eth_dev *eth_dev)
176 {
177         nix_allmulticast_config(eth_dev, 1);
178
179         return 0;
180 }
181
182 int
183 otx2_nix_allmulticast_disable(struct rte_eth_dev *eth_dev)
184 {
185         nix_allmulticast_config(eth_dev, 0);
186
187         return 0;
188 }
189
190 void
191 otx2_nix_rxq_info_get(struct rte_eth_dev *eth_dev, uint16_t queue_id,
192                       struct rte_eth_rxq_info *qinfo)
193 {
194         struct otx2_eth_rxq *rxq;
195
196         rxq = eth_dev->data->rx_queues[queue_id];
197
198         qinfo->mp = rxq->pool;
199         qinfo->scattered_rx = eth_dev->data->scattered_rx;
200         qinfo->nb_desc = rxq->qconf.nb_desc;
201
202         qinfo->conf.rx_free_thresh = 0;
203         qinfo->conf.rx_drop_en = 0;
204         qinfo->conf.rx_deferred_start = 0;
205         qinfo->conf.offloads = rxq->offloads;
206 }
207
208 void
209 otx2_nix_txq_info_get(struct rte_eth_dev *eth_dev, uint16_t queue_id,
210                       struct rte_eth_txq_info *qinfo)
211 {
212         struct otx2_eth_txq *txq;
213
214         txq = eth_dev->data->tx_queues[queue_id];
215
216         qinfo->nb_desc = txq->qconf.nb_desc;
217
218         qinfo->conf.tx_thresh.pthresh = 0;
219         qinfo->conf.tx_thresh.hthresh = 0;
220         qinfo->conf.tx_thresh.wthresh = 0;
221
222         qinfo->conf.tx_free_thresh = 0;
223         qinfo->conf.tx_rs_thresh = 0;
224         qinfo->conf.offloads = txq->offloads;
225         qinfo->conf.tx_deferred_start = 0;
226 }
227
228 static void
229 nix_rx_head_tail_get(struct otx2_eth_dev *dev,
230                      uint32_t *head, uint32_t *tail, uint16_t queue_idx)
231 {
232         uint64_t reg, val;
233
234         if (head == NULL || tail == NULL)
235                 return;
236
237         reg = (((uint64_t)queue_idx) << 32);
238         val = otx2_atomic64_add_nosync(reg, (int64_t *)
239                                        (dev->base + NIX_LF_CQ_OP_STATUS));
240         if (val & (OP_ERR | CQ_ERR))
241                 val = 0;
242
243         *tail = (uint32_t)(val & 0xFFFFF);
244         *head = (uint32_t)((val >> 20) & 0xFFFFF);
245 }
246
247 uint32_t
248 otx2_nix_rx_queue_count(struct rte_eth_dev *eth_dev, uint16_t queue_idx)
249 {
250         struct otx2_eth_rxq *rxq = eth_dev->data->rx_queues[queue_idx];
251         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
252         uint32_t head, tail;
253
254         nix_rx_head_tail_get(dev, &head, &tail, queue_idx);
255         return (tail - head) % rxq->qlen;
256 }
257
258 static inline int
259 nix_offset_has_packet(uint32_t head, uint32_t tail, uint16_t offset)
260 {
261         /* Check given offset(queue index) has packet filled by HW */
262         if (tail > head && offset <= tail && offset >= head)
263                 return 1;
264         /* Wrap around case */
265         if (head > tail && (offset >= head || offset <= tail))
266                 return 1;
267
268         return 0;
269 }
270
271 int
272 otx2_nix_rx_descriptor_done(void *rx_queue, uint16_t offset)
273 {
274         struct otx2_eth_rxq *rxq = rx_queue;
275         uint32_t head, tail;
276
277         nix_rx_head_tail_get(otx2_eth_pmd_priv(rxq->eth_dev),
278                              &head, &tail, rxq->rq);
279
280         return nix_offset_has_packet(head, tail, offset);
281 }
282
283 int
284 otx2_nix_rx_descriptor_status(void *rx_queue, uint16_t offset)
285 {
286         struct otx2_eth_rxq *rxq = rx_queue;
287         uint32_t head, tail;
288
289         if (rxq->qlen <= offset)
290                 return -EINVAL;
291
292         nix_rx_head_tail_get(otx2_eth_pmd_priv(rxq->eth_dev),
293                              &head, &tail, rxq->rq);
294
295         if (nix_offset_has_packet(head, tail, offset))
296                 return RTE_ETH_RX_DESC_DONE;
297         else
298                 return RTE_ETH_RX_DESC_AVAIL;
299 }
300
301 static void
302 nix_tx_head_tail_get(struct otx2_eth_dev *dev,
303                      uint32_t *head, uint32_t *tail, uint16_t queue_idx)
304 {
305         uint64_t reg, val;
306
307         if (head == NULL || tail == NULL)
308                 return;
309
310         reg = (((uint64_t)queue_idx) << 32);
311         val = otx2_atomic64_add_nosync(reg, (int64_t *)
312                                        (dev->base + NIX_LF_SQ_OP_STATUS));
313         if (val & OP_ERR)
314                 val = 0;
315
316         *tail = (uint32_t)((val >> 28) & 0x3F);
317         *head = (uint32_t)((val >> 20) & 0x3F);
318 }
319
320 int
321 otx2_nix_tx_descriptor_status(void *tx_queue, uint16_t offset)
322 {
323         struct otx2_eth_txq *txq = tx_queue;
324         uint32_t head, tail;
325
326         if (txq->qconf.nb_desc <= offset)
327                 return -EINVAL;
328
329         nix_tx_head_tail_get(txq->dev, &head, &tail, txq->sq);
330
331         if (nix_offset_has_packet(head, tail, offset))
332                 return RTE_ETH_TX_DESC_DONE;
333         else
334                 return RTE_ETH_TX_DESC_FULL;
335 }
336
337 /* It is a NOP for octeontx2 as HW frees the buffer on xmit */
338 int
339 otx2_nix_tx_done_cleanup(void *txq, uint32_t free_cnt)
340 {
341         RTE_SET_USED(txq);
342         RTE_SET_USED(free_cnt);
343
344         return 0;
345 }
346
347 int
348 otx2_nix_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
349                         size_t fw_size)
350 {
351         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
352         int rc = (int)fw_size;
353
354         if (fw_size > sizeof(dev->mkex_pfl_name))
355                 rc = sizeof(dev->mkex_pfl_name);
356
357         rc = strlcpy(fw_version, (char *)dev->mkex_pfl_name, rc);
358
359         rc += 1; /* Add the size of '\0' */
360         if (fw_size < (uint32_t)rc)
361                 return rc;
362
363         return 0;
364 }
365
366 int
367 otx2_nix_pool_ops_supported(struct rte_eth_dev *eth_dev, const char *pool)
368 {
369         RTE_SET_USED(eth_dev);
370
371         if (!strcmp(pool, rte_mbuf_platform_mempool_ops()))
372                 return 0;
373
374         return -ENOTSUP;
375 }
376
377 int
378 otx2_nix_dev_filter_ctrl(struct rte_eth_dev *eth_dev,
379                          enum rte_filter_type filter_type,
380                          enum rte_filter_op filter_op, void *arg)
381 {
382         RTE_SET_USED(eth_dev);
383
384         if (filter_type != RTE_ETH_FILTER_GENERIC) {
385                 otx2_err("Unsupported filter type %d", filter_type);
386                 return -ENOTSUP;
387         }
388
389         if (filter_op == RTE_ETH_FILTER_GET) {
390                 *(const void **)arg = &otx2_flow_ops;
391                 return 0;
392         }
393
394         otx2_err("Invalid filter_op %d", filter_op);
395         return -EINVAL;
396 }
397
398 static struct cgx_fw_data *
399 nix_get_fwdata(struct otx2_eth_dev *dev)
400 {
401         struct otx2_mbox *mbox = dev->mbox;
402         struct cgx_fw_data *rsp = NULL;
403         int rc;
404
405         otx2_mbox_alloc_msg_cgx_get_aux_link_info(mbox);
406
407         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
408         if (rc) {
409                 otx2_err("Failed to get fw data: %d", rc);
410                 return NULL;
411         }
412
413         return rsp;
414 }
415
416 int
417 otx2_nix_get_module_info(struct rte_eth_dev *eth_dev,
418                          struct rte_eth_dev_module_info *modinfo)
419 {
420         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
421         struct cgx_fw_data *rsp;
422
423         rsp = nix_get_fwdata(dev);
424         if (rsp == NULL)
425                 return -EIO;
426
427         modinfo->type = rsp->fwdata.sfp_eeprom.sff_id;
428         modinfo->eeprom_len = SFP_EEPROM_SIZE;
429
430         return 0;
431 }
432
433 int
434 otx2_nix_get_module_eeprom(struct rte_eth_dev *eth_dev,
435                            struct rte_dev_eeprom_info *info)
436 {
437         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
438         struct cgx_fw_data *rsp;
439
440         if (!info->data || !info->length ||
441             (info->offset + info->length > SFP_EEPROM_SIZE))
442                 return -EINVAL;
443
444         rsp = nix_get_fwdata(dev);
445         if (rsp == NULL)
446                 return -EIO;
447
448         otx2_mbox_memcpy(info->data, rsp->fwdata.sfp_eeprom.buf + info->offset,
449                          info->length);
450
451         return 0;
452 }
453
454 int
455 otx2_nix_info_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *devinfo)
456 {
457         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
458         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
459
460         devinfo->min_rx_bufsize = NIX_MIN_FRS;
461         devinfo->max_rx_pktlen = NIX_MAX_FRS;
462         devinfo->max_rx_queues = RTE_MAX_QUEUES_PER_PORT;
463         devinfo->max_tx_queues = RTE_MAX_QUEUES_PER_PORT;
464         devinfo->max_mac_addrs = dev->max_mac_entries;
465         devinfo->max_vfs = pci_dev->max_vfs;
466         devinfo->max_mtu = devinfo->max_rx_pktlen - NIX_L2_OVERHEAD;
467         devinfo->min_mtu = devinfo->min_rx_bufsize - NIX_L2_OVERHEAD;
468
469         devinfo->rx_offload_capa = dev->rx_offload_capa;
470         devinfo->tx_offload_capa = dev->tx_offload_capa;
471         devinfo->rx_queue_offload_capa = 0;
472         devinfo->tx_queue_offload_capa = 0;
473
474         devinfo->reta_size = dev->rss_info.rss_size;
475         devinfo->hash_key_size = NIX_HASH_KEY_SIZE;
476         devinfo->flow_type_rss_offloads = NIX_RSS_OFFLOAD;
477
478         devinfo->default_rxconf = (struct rte_eth_rxconf) {
479                 .rx_drop_en = 0,
480                 .offloads = 0,
481         };
482
483         devinfo->default_txconf = (struct rte_eth_txconf) {
484                 .offloads = 0,
485         };
486
487         devinfo->default_rxportconf = (struct rte_eth_dev_portconf) {
488                 .ring_size = NIX_RX_DEFAULT_RING_SZ,
489         };
490
491         devinfo->rx_desc_lim = (struct rte_eth_desc_lim) {
492                 .nb_max = UINT16_MAX,
493                 .nb_min = NIX_RX_MIN_DESC,
494                 .nb_align = NIX_RX_MIN_DESC_ALIGN,
495                 .nb_seg_max = NIX_RX_NB_SEG_MAX,
496                 .nb_mtu_seg_max = NIX_RX_NB_SEG_MAX,
497         };
498         devinfo->rx_desc_lim.nb_max =
499                 RTE_ALIGN_MUL_FLOOR(devinfo->rx_desc_lim.nb_max,
500                                     NIX_RX_MIN_DESC_ALIGN);
501
502         devinfo->tx_desc_lim = (struct rte_eth_desc_lim) {
503                 .nb_max = UINT16_MAX,
504                 .nb_min = 1,
505                 .nb_align = 1,
506                 .nb_seg_max = NIX_TX_NB_SEG_MAX,
507                 .nb_mtu_seg_max = NIX_TX_NB_SEG_MAX,
508         };
509
510         /* Auto negotiation disabled */
511         devinfo->speed_capa = ETH_LINK_SPEED_FIXED;
512         devinfo->speed_capa |= ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
513                                 ETH_LINK_SPEED_25G | ETH_LINK_SPEED_40G |
514                                 ETH_LINK_SPEED_50G | ETH_LINK_SPEED_100G;
515
516         devinfo->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
517                                 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
518
519         return 0;
520 }