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