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