net/octeontx2: support VLAN offloads
[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 static void
10 nix_cgx_promisc_config(struct rte_eth_dev *eth_dev, int en)
11 {
12         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
13         struct otx2_mbox *mbox = dev->mbox;
14
15         if (otx2_dev_is_vf(dev))
16                 return;
17
18         if (en)
19                 otx2_mbox_alloc_msg_cgx_promisc_enable(mbox);
20         else
21                 otx2_mbox_alloc_msg_cgx_promisc_disable(mbox);
22
23         otx2_mbox_process(mbox);
24 }
25
26 void
27 otx2_nix_promisc_config(struct rte_eth_dev *eth_dev, int en)
28 {
29         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
30         struct otx2_mbox *mbox = dev->mbox;
31         struct nix_rx_mode *req;
32
33         if (otx2_dev_is_vf(dev))
34                 return;
35
36         req = otx2_mbox_alloc_msg_nix_set_rx_mode(mbox);
37
38         if (en)
39                 req->mode = NIX_RX_MODE_UCAST | NIX_RX_MODE_PROMISC;
40
41         otx2_mbox_process(mbox);
42         eth_dev->data->promiscuous = en;
43         otx2_nix_vlan_update_promisc(eth_dev, en);
44 }
45
46 void
47 otx2_nix_promisc_enable(struct rte_eth_dev *eth_dev)
48 {
49         otx2_nix_promisc_config(eth_dev, 1);
50         nix_cgx_promisc_config(eth_dev, 1);
51 }
52
53 void
54 otx2_nix_promisc_disable(struct rte_eth_dev *eth_dev)
55 {
56         otx2_nix_promisc_config(eth_dev, 0);
57         nix_cgx_promisc_config(eth_dev, 0);
58 }
59
60 static void
61 nix_allmulticast_config(struct rte_eth_dev *eth_dev, int en)
62 {
63         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
64         struct otx2_mbox *mbox = dev->mbox;
65         struct nix_rx_mode *req;
66
67         if (otx2_dev_is_vf(dev))
68                 return;
69
70         req = otx2_mbox_alloc_msg_nix_set_rx_mode(mbox);
71
72         if (en)
73                 req->mode = NIX_RX_MODE_UCAST | NIX_RX_MODE_ALLMULTI;
74         else if (eth_dev->data->promiscuous)
75                 req->mode = NIX_RX_MODE_UCAST | NIX_RX_MODE_PROMISC;
76
77         otx2_mbox_process(mbox);
78 }
79
80 void
81 otx2_nix_allmulticast_enable(struct rte_eth_dev *eth_dev)
82 {
83         nix_allmulticast_config(eth_dev, 1);
84 }
85
86 void
87 otx2_nix_allmulticast_disable(struct rte_eth_dev *eth_dev)
88 {
89         nix_allmulticast_config(eth_dev, 0);
90 }
91
92 void
93 otx2_nix_rxq_info_get(struct rte_eth_dev *eth_dev, uint16_t queue_id,
94                       struct rte_eth_rxq_info *qinfo)
95 {
96         struct otx2_eth_rxq *rxq;
97
98         rxq = eth_dev->data->rx_queues[queue_id];
99
100         qinfo->mp = rxq->pool;
101         qinfo->scattered_rx = eth_dev->data->scattered_rx;
102         qinfo->nb_desc = rxq->qconf.nb_desc;
103
104         qinfo->conf.rx_free_thresh = 0;
105         qinfo->conf.rx_drop_en = 0;
106         qinfo->conf.rx_deferred_start = 0;
107         qinfo->conf.offloads = rxq->offloads;
108 }
109
110 void
111 otx2_nix_txq_info_get(struct rte_eth_dev *eth_dev, uint16_t queue_id,
112                       struct rte_eth_txq_info *qinfo)
113 {
114         struct otx2_eth_txq *txq;
115
116         txq = eth_dev->data->tx_queues[queue_id];
117
118         qinfo->nb_desc = txq->qconf.nb_desc;
119
120         qinfo->conf.tx_thresh.pthresh = 0;
121         qinfo->conf.tx_thresh.hthresh = 0;
122         qinfo->conf.tx_thresh.wthresh = 0;
123
124         qinfo->conf.tx_free_thresh = 0;
125         qinfo->conf.tx_rs_thresh = 0;
126         qinfo->conf.offloads = txq->offloads;
127         qinfo->conf.tx_deferred_start = 0;
128 }
129
130 static void
131 nix_rx_head_tail_get(struct otx2_eth_dev *dev,
132                      uint32_t *head, uint32_t *tail, uint16_t queue_idx)
133 {
134         uint64_t reg, val;
135
136         if (head == NULL || tail == NULL)
137                 return;
138
139         reg = (((uint64_t)queue_idx) << 32);
140         val = otx2_atomic64_add_nosync(reg, (int64_t *)
141                                        (dev->base + NIX_LF_CQ_OP_STATUS));
142         if (val & (OP_ERR | CQ_ERR))
143                 val = 0;
144
145         *tail = (uint32_t)(val & 0xFFFFF);
146         *head = (uint32_t)((val >> 20) & 0xFFFFF);
147 }
148
149 uint32_t
150 otx2_nix_rx_queue_count(struct rte_eth_dev *eth_dev, uint16_t queue_idx)
151 {
152         struct otx2_eth_rxq *rxq = eth_dev->data->rx_queues[queue_idx];
153         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
154         uint32_t head, tail;
155
156         nix_rx_head_tail_get(dev, &head, &tail, queue_idx);
157         return (tail - head) % rxq->qlen;
158 }
159
160 static inline int
161 nix_offset_has_packet(uint32_t head, uint32_t tail, uint16_t offset)
162 {
163         /* Check given offset(queue index) has packet filled by HW */
164         if (tail > head && offset <= tail && offset >= head)
165                 return 1;
166         /* Wrap around case */
167         if (head > tail && (offset >= head || offset <= tail))
168                 return 1;
169
170         return 0;
171 }
172
173 int
174 otx2_nix_rx_descriptor_done(void *rx_queue, uint16_t offset)
175 {
176         struct otx2_eth_rxq *rxq = rx_queue;
177         uint32_t head, tail;
178
179         nix_rx_head_tail_get(otx2_eth_pmd_priv(rxq->eth_dev),
180                              &head, &tail, rxq->rq);
181
182         return nix_offset_has_packet(head, tail, offset);
183 }
184
185 int
186 otx2_nix_rx_descriptor_status(void *rx_queue, uint16_t offset)
187 {
188         struct otx2_eth_rxq *rxq = rx_queue;
189         uint32_t head, tail;
190
191         if (rxq->qlen >= offset)
192                 return -EINVAL;
193
194         nix_rx_head_tail_get(otx2_eth_pmd_priv(rxq->eth_dev),
195                              &head, &tail, rxq->rq);
196
197         if (nix_offset_has_packet(head, tail, offset))
198                 return RTE_ETH_RX_DESC_DONE;
199         else
200                 return RTE_ETH_RX_DESC_AVAIL;
201 }
202
203 /* It is a NOP for octeontx2 as HW frees the buffer on xmit */
204 int
205 otx2_nix_tx_done_cleanup(void *txq, uint32_t free_cnt)
206 {
207         RTE_SET_USED(txq);
208         RTE_SET_USED(free_cnt);
209
210         return 0;
211 }
212
213 int
214 otx2_nix_pool_ops_supported(struct rte_eth_dev *eth_dev, const char *pool)
215 {
216         RTE_SET_USED(eth_dev);
217
218         if (!strcmp(pool, rte_mbuf_platform_mempool_ops()))
219                 return 0;
220
221         return -ENOTSUP;
222 }
223
224 int
225 otx2_nix_dev_filter_ctrl(struct rte_eth_dev *eth_dev,
226                          enum rte_filter_type filter_type,
227                          enum rte_filter_op filter_op, void *arg)
228 {
229         RTE_SET_USED(eth_dev);
230
231         if (filter_type != RTE_ETH_FILTER_GENERIC) {
232                 otx2_err("Unsupported filter type %d", filter_type);
233                 return -ENOTSUP;
234         }
235
236         if (filter_op == RTE_ETH_FILTER_GET) {
237                 *(const void **)arg = &otx2_flow_ops;
238                 return 0;
239         }
240
241         otx2_err("Invalid filter_op %d", filter_op);
242         return -EINVAL;
243 }
244
245 static struct cgx_fw_data *
246 nix_get_fwdata(struct otx2_eth_dev *dev)
247 {
248         struct otx2_mbox *mbox = dev->mbox;
249         struct cgx_fw_data *rsp = NULL;
250
251         otx2_mbox_alloc_msg_cgx_get_aux_link_info(mbox);
252
253         otx2_mbox_process_msg(mbox, (void *)&rsp);
254
255         return rsp;
256 }
257
258 int
259 otx2_nix_get_module_info(struct rte_eth_dev *eth_dev,
260                          struct rte_eth_dev_module_info *modinfo)
261 {
262         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
263         struct cgx_fw_data *rsp;
264
265         rsp = nix_get_fwdata(dev);
266         if (rsp == NULL)
267                 return -EIO;
268
269         modinfo->type = rsp->fwdata.sfp_eeprom.sff_id;
270         modinfo->eeprom_len = SFP_EEPROM_SIZE;
271
272         return 0;
273 }
274
275 int
276 otx2_nix_get_module_eeprom(struct rte_eth_dev *eth_dev,
277                            struct rte_dev_eeprom_info *info)
278 {
279         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
280         struct cgx_fw_data *rsp;
281
282         if (!info->data || !info->length ||
283             (info->offset + info->length > SFP_EEPROM_SIZE))
284                 return -EINVAL;
285
286         rsp = nix_get_fwdata(dev);
287         if (rsp == NULL)
288                 return -EIO;
289
290         otx2_mbox_memcpy(info->data, rsp->fwdata.sfp_eeprom.buf + info->offset,
291                          info->length);
292
293         return 0;
294 }
295
296 void
297 otx2_nix_info_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *devinfo)
298 {
299         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
300         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
301
302         devinfo->min_rx_bufsize = NIX_MIN_FRS;
303         devinfo->max_rx_pktlen = NIX_MAX_FRS;
304         devinfo->max_rx_queues = RTE_MAX_QUEUES_PER_PORT;
305         devinfo->max_tx_queues = RTE_MAX_QUEUES_PER_PORT;
306         devinfo->max_mac_addrs = dev->max_mac_entries;
307         devinfo->max_vfs = pci_dev->max_vfs;
308         devinfo->max_mtu = devinfo->max_rx_pktlen - NIX_L2_OVERHEAD;
309         devinfo->min_mtu = devinfo->min_rx_bufsize - NIX_L2_OVERHEAD;
310
311         devinfo->rx_offload_capa = dev->rx_offload_capa;
312         devinfo->tx_offload_capa = dev->tx_offload_capa;
313         devinfo->rx_queue_offload_capa = 0;
314         devinfo->tx_queue_offload_capa = 0;
315
316         devinfo->reta_size = dev->rss_info.rss_size;
317         devinfo->hash_key_size = NIX_HASH_KEY_SIZE;
318         devinfo->flow_type_rss_offloads = NIX_RSS_OFFLOAD;
319
320         devinfo->default_rxconf = (struct rte_eth_rxconf) {
321                 .rx_drop_en = 0,
322                 .offloads = 0,
323         };
324
325         devinfo->default_txconf = (struct rte_eth_txconf) {
326                 .offloads = 0,
327         };
328
329         devinfo->rx_desc_lim = (struct rte_eth_desc_lim) {
330                 .nb_max = UINT16_MAX,
331                 .nb_min = NIX_RX_MIN_DESC,
332                 .nb_align = NIX_RX_MIN_DESC_ALIGN,
333                 .nb_seg_max = NIX_RX_NB_SEG_MAX,
334                 .nb_mtu_seg_max = NIX_RX_NB_SEG_MAX,
335         };
336         devinfo->rx_desc_lim.nb_max =
337                 RTE_ALIGN_MUL_FLOOR(devinfo->rx_desc_lim.nb_max,
338                                     NIX_RX_MIN_DESC_ALIGN);
339
340         devinfo->tx_desc_lim = (struct rte_eth_desc_lim) {
341                 .nb_max = UINT16_MAX,
342                 .nb_min = 1,
343                 .nb_align = 1,
344                 .nb_seg_max = NIX_TX_NB_SEG_MAX,
345                 .nb_mtu_seg_max = NIX_TX_NB_SEG_MAX,
346         };
347
348         /* Auto negotiation disabled */
349         devinfo->speed_capa = ETH_LINK_SPEED_FIXED;
350         devinfo->speed_capa |= ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
351                                 ETH_LINK_SPEED_25G | ETH_LINK_SPEED_40G |
352                                 ETH_LINK_SPEED_50G | ETH_LINK_SPEED_100G;
353
354         devinfo->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
355                                 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
356 }