12d5a2e48a9b575e139984a27ba2350d8771efc9
[dpdk.git] / drivers / net / i40e / i40e_vf_representor.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation.
3  */
4
5 #include <rte_bus_pci.h>
6 #include <rte_ethdev.h>
7 #include <rte_pci.h>
8 #include <rte_malloc.h>
9
10 #include "ethdev_driver.h"
11 #include "base/i40e_type.h"
12 #include "base/virtchnl.h"
13 #include "i40e_ethdev.h"
14 #include "i40e_rxtx.h"
15 #include "rte_pmd_i40e.h"
16
17 static int
18 i40e_vf_representor_link_update(struct rte_eth_dev *ethdev,
19         int wait_to_complete)
20 {
21         struct i40e_vf_representor *representor = ethdev->data->dev_private;
22         struct rte_eth_dev *dev =
23                 &rte_eth_devices[representor->adapter->pf.dev_data->port_id];
24
25         return i40e_dev_link_update(dev, wait_to_complete);
26 }
27 static int
28 i40e_vf_representor_dev_infos_get(struct rte_eth_dev *ethdev,
29         struct rte_eth_dev_info *dev_info)
30 {
31         struct i40e_vf_representor *representor = ethdev->data->dev_private;
32         struct rte_eth_dev_data *pf_dev_data =
33                 representor->adapter->pf.dev_data;
34
35         /* get dev info for the vdev */
36         dev_info->device = ethdev->device;
37
38         dev_info->max_rx_queues = ethdev->data->nb_rx_queues;
39         dev_info->max_tx_queues = ethdev->data->nb_tx_queues;
40
41         dev_info->min_rx_bufsize = I40E_BUF_SIZE_MIN;
42         dev_info->max_rx_pktlen = I40E_FRAME_SIZE_MAX;
43         dev_info->hash_key_size = (I40E_VFQF_HKEY_MAX_INDEX + 1) *
44                 sizeof(uint32_t);
45         dev_info->reta_size = ETH_RSS_RETA_SIZE_64;
46         dev_info->flow_type_rss_offloads = I40E_RSS_OFFLOAD_ALL;
47         dev_info->max_mac_addrs = I40E_NUM_MACADDR_MAX;
48         dev_info->rx_offload_capa =
49                 DEV_RX_OFFLOAD_VLAN_STRIP |
50                 DEV_RX_OFFLOAD_QINQ_STRIP |
51                 DEV_RX_OFFLOAD_IPV4_CKSUM |
52                 DEV_RX_OFFLOAD_UDP_CKSUM |
53                 DEV_RX_OFFLOAD_TCP_CKSUM |
54                 DEV_RX_OFFLOAD_VLAN_FILTER;
55         dev_info->tx_offload_capa =
56                 DEV_TX_OFFLOAD_MULTI_SEGS  |
57                 DEV_TX_OFFLOAD_VLAN_INSERT |
58                 DEV_TX_OFFLOAD_QINQ_INSERT |
59                 DEV_TX_OFFLOAD_IPV4_CKSUM |
60                 DEV_TX_OFFLOAD_UDP_CKSUM |
61                 DEV_TX_OFFLOAD_TCP_CKSUM |
62                 DEV_TX_OFFLOAD_SCTP_CKSUM |
63                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
64                 DEV_TX_OFFLOAD_TCP_TSO |
65                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
66                 DEV_TX_OFFLOAD_GRE_TNL_TSO |
67                 DEV_TX_OFFLOAD_IPIP_TNL_TSO |
68                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO;
69
70         dev_info->default_rxconf = (struct rte_eth_rxconf) {
71                 .rx_thresh = {
72                         .pthresh = I40E_DEFAULT_RX_PTHRESH,
73                         .hthresh = I40E_DEFAULT_RX_HTHRESH,
74                         .wthresh = I40E_DEFAULT_RX_WTHRESH,
75                 },
76                 .rx_free_thresh = I40E_DEFAULT_RX_FREE_THRESH,
77                 .rx_drop_en = 0,
78                 .offloads = 0,
79         };
80
81         dev_info->default_txconf = (struct rte_eth_txconf) {
82                 .tx_thresh = {
83                         .pthresh = I40E_DEFAULT_TX_PTHRESH,
84                         .hthresh = I40E_DEFAULT_TX_HTHRESH,
85                         .wthresh = I40E_DEFAULT_TX_WTHRESH,
86                 },
87                 .tx_free_thresh = I40E_DEFAULT_TX_FREE_THRESH,
88                 .tx_rs_thresh = I40E_DEFAULT_TX_RSBIT_THRESH,
89                 .offloads = 0,
90         };
91
92         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
93                 .nb_max = I40E_MAX_RING_DESC,
94                 .nb_min = I40E_MIN_RING_DESC,
95                 .nb_align = I40E_ALIGN_RING_DESC,
96         };
97
98         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
99                 .nb_max = I40E_MAX_RING_DESC,
100                 .nb_min = I40E_MIN_RING_DESC,
101                 .nb_align = I40E_ALIGN_RING_DESC,
102         };
103
104         dev_info->switch_info.name =
105                 rte_eth_devices[pf_dev_data->port_id].device->name;
106         dev_info->switch_info.domain_id = representor->switch_domain_id;
107         dev_info->switch_info.port_id = representor->vf_id;
108
109         return 0;
110 }
111
112 static int
113 i40e_vf_representor_dev_configure(__rte_unused struct rte_eth_dev *dev)
114 {
115         return 0;
116 }
117
118 static int
119 i40e_vf_representor_dev_start(__rte_unused struct rte_eth_dev *dev)
120 {
121         return 0;
122 }
123
124 static int
125 i40e_vf_representor_dev_stop(__rte_unused struct rte_eth_dev *dev)
126 {
127         return 0;
128 }
129
130 static int
131 i40e_vf_representor_rx_queue_setup(__rte_unused struct rte_eth_dev *dev,
132         __rte_unused uint16_t rx_queue_id,
133         __rte_unused uint16_t nb_rx_desc,
134         __rte_unused unsigned int socket_id,
135         __rte_unused const struct rte_eth_rxconf *rx_conf,
136         __rte_unused struct rte_mempool *mb_pool)
137 {
138         return 0;
139 }
140
141 static int
142 i40e_vf_representor_tx_queue_setup(__rte_unused struct rte_eth_dev *dev,
143         __rte_unused uint16_t rx_queue_id,
144         __rte_unused uint16_t nb_rx_desc,
145         __rte_unused unsigned int socket_id,
146         __rte_unused const struct rte_eth_txconf *tx_conf)
147 {
148         return 0;
149 }
150
151 static void
152 i40evf_stat_update_48(uint64_t *offset,
153                    uint64_t *stat)
154 {
155         if (*stat >= *offset)
156                 *stat = *stat - *offset;
157         else
158                 *stat = (uint64_t)((*stat +
159                         ((uint64_t)1 << I40E_48_BIT_WIDTH)) - *offset);
160
161         *stat &= I40E_48_BIT_MASK;
162 }
163
164 static void
165 i40evf_stat_update_32(uint64_t *offset,
166                    uint64_t *stat)
167 {
168         if (*stat >= *offset)
169                 *stat = (uint64_t)(*stat - *offset);
170         else
171                 *stat = (uint64_t)((*stat +
172                         ((uint64_t)1 << I40E_32_BIT_WIDTH)) - *offset);
173 }
174
175 static int
176 rte_pmd_i40e_get_vf_native_stats(uint16_t port,
177                           uint16_t vf_id,
178                           struct i40e_eth_stats *stats)
179 {
180         struct rte_eth_dev *dev;
181         struct i40e_pf *pf;
182         struct i40e_vsi *vsi;
183
184         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
185
186         dev = &rte_eth_devices[port];
187
188         if (!is_i40e_supported(dev))
189                 return -ENOTSUP;
190
191         pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
192
193         if (vf_id >= pf->vf_num || !pf->vfs) {
194                 PMD_DRV_LOG(ERR, "Invalid VF ID.");
195                 return -EINVAL;
196         }
197
198         vsi = pf->vfs[vf_id].vsi;
199         if (!vsi) {
200                 PMD_DRV_LOG(ERR, "Invalid VSI.");
201                 return -EINVAL;
202         }
203
204         i40e_update_vsi_stats(vsi);
205         memcpy(stats, &vsi->eth_stats, sizeof(vsi->eth_stats));
206
207         return 0;
208 }
209
210 static int
211 i40e_vf_representor_stats_get(struct rte_eth_dev *ethdev,
212                 struct rte_eth_stats *stats)
213 {
214         struct i40e_vf_representor *representor = ethdev->data->dev_private;
215         struct i40e_eth_stats native_stats;
216         int ret;
217
218         ret = rte_pmd_i40e_get_vf_native_stats(
219                 representor->adapter->pf.dev_data->port_id,
220                 representor->vf_id, &native_stats);
221         if (ret == 0) {
222                 i40evf_stat_update_48(
223                         &representor->stats_offset.rx_bytes,
224                         &native_stats.rx_bytes);
225                 i40evf_stat_update_48(
226                         &representor->stats_offset.rx_unicast,
227                         &native_stats.rx_unicast);
228                 i40evf_stat_update_48(
229                         &representor->stats_offset.rx_multicast,
230                         &native_stats.rx_multicast);
231                 i40evf_stat_update_48(
232                         &representor->stats_offset.rx_broadcast,
233                         &native_stats.rx_broadcast);
234                 i40evf_stat_update_32(
235                         &representor->stats_offset.rx_discards,
236                         &native_stats.rx_discards);
237                 i40evf_stat_update_32(
238                         &representor->stats_offset.rx_unknown_protocol,
239                         &native_stats.rx_unknown_protocol);
240                 i40evf_stat_update_48(
241                         &representor->stats_offset.tx_bytes,
242                         &native_stats.tx_bytes);
243                 i40evf_stat_update_48(
244                         &representor->stats_offset.tx_unicast,
245                         &native_stats.tx_unicast);
246                 i40evf_stat_update_48(
247                         &representor->stats_offset.tx_multicast,
248                         &native_stats.tx_multicast);
249                 i40evf_stat_update_48(
250                         &representor->stats_offset.tx_broadcast,
251                         &native_stats.tx_broadcast);
252                 i40evf_stat_update_32(
253                         &representor->stats_offset.tx_errors,
254                         &native_stats.tx_errors);
255                 i40evf_stat_update_32(
256                         &representor->stats_offset.tx_discards,
257                         &native_stats.tx_discards);
258
259                 stats->ipackets = native_stats.rx_unicast +
260                         native_stats.rx_multicast +
261                         native_stats.rx_broadcast;
262                 stats->opackets = native_stats.tx_unicast +
263                         native_stats.tx_multicast +
264                         native_stats.tx_broadcast;
265                 stats->ibytes   = native_stats.rx_bytes;
266                 stats->obytes   = native_stats.tx_bytes;
267                 stats->ierrors  = native_stats.rx_discards;
268                 stats->oerrors  = native_stats.tx_errors + native_stats.tx_discards;
269         }
270         return ret;
271 }
272
273 static int
274 i40e_vf_representor_stats_reset(struct rte_eth_dev *ethdev)
275 {
276         struct i40e_vf_representor *representor = ethdev->data->dev_private;
277
278         return rte_pmd_i40e_get_vf_native_stats(
279                 representor->adapter->pf.dev_data->port_id,
280                 representor->vf_id, &representor->stats_offset);
281 }
282
283 static int
284 i40e_vf_representor_promiscuous_enable(struct rte_eth_dev *ethdev)
285 {
286         struct i40e_vf_representor *representor = ethdev->data->dev_private;
287
288         return rte_pmd_i40e_set_vf_unicast_promisc(
289                 representor->adapter->pf.dev_data->port_id,
290                 representor->vf_id, 1);
291 }
292
293 static int
294 i40e_vf_representor_promiscuous_disable(struct rte_eth_dev *ethdev)
295 {
296         struct i40e_vf_representor *representor = ethdev->data->dev_private;
297
298         return rte_pmd_i40e_set_vf_unicast_promisc(
299                 representor->adapter->pf.dev_data->port_id,
300                 representor->vf_id, 0);
301 }
302
303 static int
304 i40e_vf_representor_allmulticast_enable(struct rte_eth_dev *ethdev)
305 {
306         struct i40e_vf_representor *representor = ethdev->data->dev_private;
307
308         return rte_pmd_i40e_set_vf_multicast_promisc(
309                 representor->adapter->pf.dev_data->port_id,
310                 representor->vf_id,  1);
311 }
312
313 static int
314 i40e_vf_representor_allmulticast_disable(struct rte_eth_dev *ethdev)
315 {
316         struct i40e_vf_representor *representor = ethdev->data->dev_private;
317
318         return rte_pmd_i40e_set_vf_multicast_promisc(
319                 representor->adapter->pf.dev_data->port_id,
320                 representor->vf_id,  0);
321 }
322
323 static void
324 i40e_vf_representor_mac_addr_remove(struct rte_eth_dev *ethdev, uint32_t index)
325 {
326         struct i40e_vf_representor *representor = ethdev->data->dev_private;
327
328         rte_pmd_i40e_remove_vf_mac_addr(
329                 representor->adapter->pf.dev_data->port_id,
330                 representor->vf_id, &ethdev->data->mac_addrs[index]);
331 }
332
333 static int
334 i40e_vf_representor_mac_addr_set(struct rte_eth_dev *ethdev,
335                 struct rte_ether_addr *mac_addr)
336 {
337         struct i40e_vf_representor *representor = ethdev->data->dev_private;
338
339         return rte_pmd_i40e_set_vf_mac_addr(
340                 representor->adapter->pf.dev_data->port_id,
341                 representor->vf_id, mac_addr);
342 }
343
344 static int
345 i40e_vf_representor_vlan_filter_set(struct rte_eth_dev *ethdev,
346                 uint16_t vlan_id, int on)
347 {
348         struct i40e_vf_representor *representor = ethdev->data->dev_private;
349         uint64_t vf_mask = 1ULL << representor->vf_id;
350
351         return rte_pmd_i40e_set_vf_vlan_filter(
352                 representor->adapter->pf.dev_data->port_id,
353                 vlan_id, vf_mask, on);
354 }
355
356 static int
357 i40e_vf_representor_vlan_offload_set(struct rte_eth_dev *ethdev, int mask)
358 {
359         struct i40e_vf_representor *representor = ethdev->data->dev_private;
360         struct rte_eth_dev *pdev;
361         struct i40e_pf_vf *vf;
362         struct i40e_vsi *vsi;
363         struct i40e_pf *pf;
364         uint32_t vfid;
365
366         pdev = &rte_eth_devices[representor->adapter->pf.dev_data->port_id];
367         vfid = representor->vf_id;
368
369         if (!is_i40e_supported(pdev)) {
370                 PMD_DRV_LOG(ERR, "Invalid PF dev.");
371                 return -EINVAL;
372         }
373
374         pf = I40E_DEV_PRIVATE_TO_PF(pdev->data->dev_private);
375
376         if (vfid >= pf->vf_num || !pf->vfs) {
377                 PMD_DRV_LOG(ERR, "Invalid VF ID.");
378                 return -EINVAL;
379         }
380
381         vf = &pf->vfs[vfid];
382         vsi = vf->vsi;
383         if (!vsi) {
384                 PMD_DRV_LOG(ERR, "Invalid VSI.");
385                 return -EINVAL;
386         }
387
388         if (mask & ETH_VLAN_FILTER_MASK) {
389                 /* Enable or disable VLAN filtering offload */
390                 if (ethdev->data->dev_conf.rxmode.offloads &
391                     DEV_RX_OFFLOAD_VLAN_FILTER)
392                         return i40e_vsi_config_vlan_filter(vsi, TRUE);
393                 else
394                         return i40e_vsi_config_vlan_filter(vsi, FALSE);
395         }
396
397         if (mask & ETH_VLAN_STRIP_MASK) {
398                 /* Enable or disable VLAN stripping offload */
399                 if (ethdev->data->dev_conf.rxmode.offloads &
400                     DEV_RX_OFFLOAD_VLAN_STRIP)
401                         return i40e_vsi_config_vlan_stripping(vsi, TRUE);
402                 else
403                         return i40e_vsi_config_vlan_stripping(vsi, FALSE);
404         }
405
406         return -EINVAL;
407 }
408
409 static void
410 i40e_vf_representor_vlan_strip_queue_set(struct rte_eth_dev *ethdev,
411         __rte_unused uint16_t rx_queue_id, int on)
412 {
413         struct i40e_vf_representor *representor = ethdev->data->dev_private;
414
415         rte_pmd_i40e_set_vf_vlan_stripq(
416                 representor->adapter->pf.dev_data->port_id,
417                 representor->vf_id, on);
418 }
419
420 static int
421 i40e_vf_representor_vlan_pvid_set(struct rte_eth_dev *ethdev, uint16_t vlan_id,
422         __rte_unused int on)
423 {
424         struct i40e_vf_representor *representor = ethdev->data->dev_private;
425
426         return rte_pmd_i40e_set_vf_vlan_insert(
427                 representor->adapter->pf.dev_data->port_id,
428                 representor->vf_id, vlan_id);
429 }
430
431 static const struct eth_dev_ops i40e_representor_dev_ops = {
432         .dev_infos_get        = i40e_vf_representor_dev_infos_get,
433
434         .dev_start            = i40e_vf_representor_dev_start,
435         .dev_configure        = i40e_vf_representor_dev_configure,
436         .dev_stop             = i40e_vf_representor_dev_stop,
437
438         .rx_queue_setup       = i40e_vf_representor_rx_queue_setup,
439         .tx_queue_setup       = i40e_vf_representor_tx_queue_setup,
440
441         .link_update          = i40e_vf_representor_link_update,
442
443         .stats_get            = i40e_vf_representor_stats_get,
444         .stats_reset          = i40e_vf_representor_stats_reset,
445
446         .promiscuous_enable   = i40e_vf_representor_promiscuous_enable,
447         .promiscuous_disable  = i40e_vf_representor_promiscuous_disable,
448
449         .allmulticast_enable  = i40e_vf_representor_allmulticast_enable,
450         .allmulticast_disable = i40e_vf_representor_allmulticast_disable,
451
452         .mac_addr_remove      = i40e_vf_representor_mac_addr_remove,
453         .mac_addr_set         = i40e_vf_representor_mac_addr_set,
454
455         .vlan_filter_set      = i40e_vf_representor_vlan_filter_set,
456         .vlan_offload_set     = i40e_vf_representor_vlan_offload_set,
457         .vlan_strip_queue_set = i40e_vf_representor_vlan_strip_queue_set,
458         .vlan_pvid_set        = i40e_vf_representor_vlan_pvid_set
459
460 };
461
462 static uint16_t
463 i40e_vf_representor_rx_burst(__rte_unused void *rx_queue,
464         __rte_unused struct rte_mbuf **rx_pkts, __rte_unused uint16_t nb_pkts)
465 {
466         return 0;
467 }
468
469 static uint16_t
470 i40e_vf_representor_tx_burst(__rte_unused void *tx_queue,
471         __rte_unused struct rte_mbuf **tx_pkts, __rte_unused uint16_t nb_pkts)
472 {
473         return 0;
474 }
475
476 int
477 i40e_vf_representor_init(struct rte_eth_dev *ethdev, void *init_params)
478 {
479         struct i40e_vf_representor *representor = ethdev->data->dev_private;
480
481         struct i40e_pf *pf;
482         struct i40e_pf_vf *vf;
483         struct rte_eth_link *link;
484
485         representor->vf_id =
486                 ((struct i40e_vf_representor *)init_params)->vf_id;
487         representor->switch_domain_id =
488                 ((struct i40e_vf_representor *)init_params)->switch_domain_id;
489         representor->adapter =
490                 ((struct i40e_vf_representor *)init_params)->adapter;
491
492         pf = I40E_DEV_PRIVATE_TO_PF(
493                 representor->adapter->pf.dev_data->dev_private);
494
495         if (representor->vf_id >= pf->vf_num)
496                 return -ENODEV;
497
498         /* Set representor device ops */
499         ethdev->dev_ops = &i40e_representor_dev_ops;
500
501         /* No data-path, but need stub Rx/Tx functions to avoid crash
502          * when testing with the likes of testpmd.
503          */
504         ethdev->rx_pkt_burst = i40e_vf_representor_rx_burst;
505         ethdev->tx_pkt_burst = i40e_vf_representor_tx_burst;
506
507         vf = &pf->vfs[representor->vf_id];
508
509         if (!vf->vsi) {
510                 PMD_DRV_LOG(ERR, "Invalid VSI.");
511                 return -ENODEV;
512         }
513
514         ethdev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
515         ethdev->data->representor_id = representor->vf_id;
516         ethdev->data->backer_port_id = pf->dev_data->port_id;
517
518         /* Setting the number queues allocated to the VF */
519         ethdev->data->nb_rx_queues = vf->vsi->nb_qps;
520         ethdev->data->nb_tx_queues = vf->vsi->nb_qps;
521
522         ethdev->data->mac_addrs = &vf->mac_addr;
523
524         /* Link state. Inherited from PF */
525         link = &representor->adapter->pf.dev_data->dev_link;
526
527         ethdev->data->dev_link.link_speed = link->link_speed;
528         ethdev->data->dev_link.link_duplex = link->link_duplex;
529         ethdev->data->dev_link.link_status = link->link_status;
530         ethdev->data->dev_link.link_autoneg = link->link_autoneg;
531
532         return 0;
533 }
534
535 int
536 i40e_vf_representor_uninit(struct rte_eth_dev *ethdev)
537 {
538         /* mac_addrs must not be freed because part of i40e_pf_vf */
539         ethdev->data->mac_addrs = NULL;
540
541         return 0;
542 }