ethdev: change promiscuous callbacks to return status
[dpdk.git] / drivers / net / netvsc / hn_vf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2018 Microsoft Corp.
3  * All rights reserved.
4  */
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <stdbool.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include <dirent.h>
13 #include <fcntl.h>
14 #include <sys/types.h>
15 #include <sys/uio.h>
16
17 #include <rte_ether.h>
18 #include <rte_ethdev.h>
19 #include <rte_ethdev_driver.h>
20 #include <rte_lcore.h>
21 #include <rte_memory.h>
22 #include <rte_bus_vmbus.h>
23 #include <rte_pci.h>
24 #include <rte_bus_pci.h>
25 #include <rte_log.h>
26 #include <rte_string_fns.h>
27
28 #include "hn_logs.h"
29 #include "hn_var.h"
30 #include "hn_nvs.h"
31
32 /* Search for VF with matching MAC address, return port id */
33 static int hn_vf_match(const struct rte_eth_dev *dev)
34 {
35         const struct rte_ether_addr *mac = dev->data->mac_addrs;
36         int i;
37
38         RTE_ETH_FOREACH_DEV(i) {
39                 const struct rte_eth_dev *vf_dev = &rte_eth_devices[i];
40                 const struct rte_ether_addr *vf_mac = vf_dev->data->mac_addrs;
41
42                 if (vf_dev == dev)
43                         continue;
44
45                 if (rte_is_same_ether_addr(mac, vf_mac))
46                         return i;
47         }
48         return -ENOENT;
49 }
50
51
52 /*
53  * Attach new PCI VF device and return the port_id
54  */
55 static int hn_vf_attach(struct hn_data *hv, uint16_t port_id)
56 {
57         struct rte_eth_dev_owner owner = { .id = RTE_ETH_DEV_NO_OWNER };
58         int ret;
59
60         if (hn_vf_attached(hv)) {
61                 PMD_DRV_LOG(ERR, "VF already attached");
62                 return -EEXIST;
63         }
64
65         ret = rte_eth_dev_owner_get(port_id, &owner);
66         if (ret < 0) {
67                 PMD_DRV_LOG(ERR, "Can not find owner for port %d", port_id);
68                 return ret;
69         }
70
71         if (owner.id != RTE_ETH_DEV_NO_OWNER) {
72                 PMD_DRV_LOG(ERR, "Port %u already owned by other device %s",
73                             port_id, owner.name);
74                 return -EBUSY;
75         }
76
77         ret = rte_eth_dev_owner_set(port_id, &hv->owner);
78         if (ret < 0) {
79                 PMD_DRV_LOG(ERR, "Can set owner for port %d", port_id);
80                 return ret;
81         }
82
83         PMD_DRV_LOG(DEBUG, "Attach VF device %u", port_id);
84         hv->vf_port = port_id;
85         rte_smp_wmb();
86
87         return 0;
88 }
89
90 /* Add new VF device to synthetic device */
91 int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv)
92 {
93         int port, err;
94
95         port = hn_vf_match(dev);
96         if (port < 0) {
97                 PMD_DRV_LOG(NOTICE, "No matching MAC found");
98                 return port;
99         }
100
101         rte_spinlock_lock(&hv->vf_lock);
102         err = hn_vf_attach(hv, port);
103
104         if (err == 0) {
105                 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
106                 hv->vf_intr = (struct rte_intr_handle) {
107                         .fd = -1,
108                         .type = RTE_INTR_HANDLE_EXT,
109                 };
110                 dev->intr_handle = &hv->vf_intr;
111                 hn_nvs_set_datapath(hv, NVS_DATAPATH_VF);
112         }
113         rte_spinlock_unlock(&hv->vf_lock);
114
115         return err;
116 }
117
118 /* Remove new VF device */
119 static void hn_vf_remove(struct hn_data *hv)
120 {
121
122         rte_spinlock_lock(&hv->vf_lock);
123
124         if (!hn_vf_attached(hv)) {
125                 PMD_DRV_LOG(ERR, "VF path not active");
126         } else {
127                 /* Stop incoming packets from arriving on VF */
128                 hn_nvs_set_datapath(hv, NVS_DATAPATH_SYNTHETIC);
129
130                 /* Stop transmission over VF */
131                 hv->vf_port = HN_INVALID_PORT;
132                 rte_smp_wmb();
133
134                 /* Give back ownership */
135                 rte_eth_dev_owner_unset(hv->vf_port, hv->owner.id);
136         }
137         rte_spinlock_unlock(&hv->vf_lock);
138 }
139
140 /* Handle VF association message from host */
141 void
142 hn_nvs_handle_vfassoc(struct rte_eth_dev *dev,
143                       const struct vmbus_chanpkt_hdr *hdr,
144                       const void *data)
145 {
146         struct hn_data *hv = dev->data->dev_private;
147         const struct hn_nvs_vf_association *vf_assoc = data;
148
149         if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*vf_assoc))) {
150                 PMD_DRV_LOG(ERR, "invalid vf association NVS");
151                 return;
152         }
153
154         PMD_DRV_LOG(DEBUG, "VF serial %u %s port %u",
155                     vf_assoc->serial,
156                     vf_assoc->allocated ? "add to" : "remove from",
157                     dev->data->port_id);
158
159         hv->vf_present = vf_assoc->allocated;
160
161         if (dev->state != RTE_ETH_DEV_ATTACHED)
162                 return;
163
164         if (vf_assoc->allocated)
165                 hn_vf_add(dev, hv);
166         else
167                 hn_vf_remove(hv);
168 }
169
170 /*
171  * Merge the info from the VF and synthetic path.
172  * use the default config of the VF
173  * and the minimum number of queues and buffer sizes.
174  */
175 static int hn_vf_info_merge(struct rte_eth_dev *vf_dev,
176                              struct rte_eth_dev_info *info)
177 {
178         struct rte_eth_dev_info vf_info;
179         int ret;
180
181         ret = rte_eth_dev_info_get(vf_dev->data->port_id, &vf_info);
182         if (ret != 0)
183                 return ret;
184
185         info->speed_capa = vf_info.speed_capa;
186         info->default_rxportconf = vf_info.default_rxportconf;
187         info->default_txportconf = vf_info.default_txportconf;
188
189         info->max_rx_queues = RTE_MIN(vf_info.max_rx_queues,
190                                       info->max_rx_queues);
191         info->rx_offload_capa &= vf_info.rx_offload_capa;
192         info->rx_queue_offload_capa &= vf_info.rx_queue_offload_capa;
193         info->flow_type_rss_offloads &= vf_info.flow_type_rss_offloads;
194
195         info->max_tx_queues = RTE_MIN(vf_info.max_tx_queues,
196                                       info->max_tx_queues);
197         info->tx_offload_capa &= vf_info.tx_offload_capa;
198         info->tx_queue_offload_capa &= vf_info.tx_queue_offload_capa;
199
200         info->min_rx_bufsize = RTE_MAX(vf_info.min_rx_bufsize,
201                                        info->min_rx_bufsize);
202         info->max_rx_pktlen  = RTE_MAX(vf_info.max_rx_pktlen,
203                                        info->max_rx_pktlen);
204
205         return 0;
206 }
207
208 int hn_vf_info_get(struct hn_data *hv, struct rte_eth_dev_info *info)
209 {
210         struct rte_eth_dev *vf_dev;
211         int ret = 0;
212
213         rte_spinlock_lock(&hv->vf_lock);
214         vf_dev = hn_get_vf_dev(hv);
215         if (vf_dev)
216                 ret = hn_vf_info_merge(vf_dev, info);
217         rte_spinlock_unlock(&hv->vf_lock);
218         return ret;
219 }
220
221 int hn_vf_link_update(struct rte_eth_dev *dev,
222                       int wait_to_complete)
223 {
224         struct hn_data *hv = dev->data->dev_private;
225         struct rte_eth_dev *vf_dev;
226         int ret = 0;
227
228         rte_spinlock_lock(&hv->vf_lock);
229         vf_dev = hn_get_vf_dev(hv);
230         if (vf_dev && vf_dev->dev_ops->link_update)
231                 ret = (*vf_dev->dev_ops->link_update)(vf_dev, wait_to_complete);
232         rte_spinlock_unlock(&hv->vf_lock);
233
234         return ret;
235 }
236
237 /* called when VF has link state interrupts enabled */
238 static int hn_vf_lsc_event(uint16_t port_id __rte_unused,
239                            enum rte_eth_event_type event,
240                            void *cb_arg, void *out __rte_unused)
241 {
242         struct rte_eth_dev *dev = cb_arg;
243
244         if (event != RTE_ETH_EVENT_INTR_LSC)
245                 return 0;
246
247         /* if link state has changed pass on */
248         if (hn_dev_link_update(dev, 0) == 0)
249                 return 0; /* no change */
250
251         return _rte_eth_dev_callback_process(dev,
252                                              RTE_ETH_EVENT_INTR_LSC,
253                                              NULL);
254 }
255
256 static int _hn_vf_configure(struct rte_eth_dev *dev,
257                             uint16_t vf_port,
258                             const struct rte_eth_conf *dev_conf)
259 {
260         struct rte_eth_conf vf_conf = *dev_conf;
261         struct rte_eth_dev *vf_dev;
262         int ret;
263
264         vf_dev = &rte_eth_devices[vf_port];
265         if (dev_conf->intr_conf.lsc &&
266             (vf_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
267                 PMD_DRV_LOG(DEBUG, "enabling LSC for VF %u",
268                             vf_port);
269                 vf_conf.intr_conf.lsc = 1;
270         } else {
271                 PMD_DRV_LOG(DEBUG, "disabling LSC for VF %u",
272                             vf_port);
273                 vf_conf.intr_conf.lsc = 0;
274         }
275
276         ret = rte_eth_dev_configure(vf_port,
277                                     dev->data->nb_rx_queues,
278                                     dev->data->nb_tx_queues,
279                                     &vf_conf);
280         if (ret) {
281                 PMD_DRV_LOG(ERR,
282                             "VF configuration failed: %d", ret);
283         } else if (vf_conf.intr_conf.lsc) {
284                 ret = rte_eth_dev_callback_register(vf_port,
285                                                     RTE_ETH_DEV_INTR_LSC,
286                                                     hn_vf_lsc_event, dev);
287                 if (ret)
288                         PMD_DRV_LOG(ERR,
289                                     "Failed to register LSC callback for VF %u",
290                                     vf_port);
291         }
292         return ret;
293 }
294
295 /*
296  * Configure VF if present.
297  * Force VF to have same number of queues as synthetic device
298  */
299 int hn_vf_configure(struct rte_eth_dev *dev,
300                     const struct rte_eth_conf *dev_conf)
301 {
302         struct hn_data *hv = dev->data->dev_private;
303         int ret = 0;
304
305         rte_spinlock_lock(&hv->vf_lock);
306         if (hv->vf_port != HN_INVALID_PORT)
307                 ret = _hn_vf_configure(dev, hv->vf_port, dev_conf);
308         rte_spinlock_unlock(&hv->vf_lock);
309         return ret;
310 }
311
312 const uint32_t *hn_vf_supported_ptypes(struct rte_eth_dev *dev)
313 {
314         struct hn_data *hv = dev->data->dev_private;
315         struct rte_eth_dev *vf_dev;
316         const uint32_t *ptypes = NULL;
317
318         rte_spinlock_lock(&hv->vf_lock);
319         vf_dev = hn_get_vf_dev(hv);
320         if (vf_dev && vf_dev->dev_ops->dev_supported_ptypes_get)
321                 ptypes = (*vf_dev->dev_ops->dev_supported_ptypes_get)(vf_dev);
322         rte_spinlock_unlock(&hv->vf_lock);
323
324         return ptypes;
325 }
326
327 int hn_vf_start(struct rte_eth_dev *dev)
328 {
329         struct hn_data *hv = dev->data->dev_private;
330         struct rte_eth_dev *vf_dev;
331         int ret = 0;
332
333         rte_spinlock_lock(&hv->vf_lock);
334         vf_dev = hn_get_vf_dev(hv);
335         if (vf_dev)
336                 ret = rte_eth_dev_start(vf_dev->data->port_id);
337         rte_spinlock_unlock(&hv->vf_lock);
338         return ret;
339 }
340
341 void hn_vf_stop(struct rte_eth_dev *dev)
342 {
343         struct hn_data *hv = dev->data->dev_private;
344         struct rte_eth_dev *vf_dev;
345
346         rte_spinlock_lock(&hv->vf_lock);
347         vf_dev = hn_get_vf_dev(hv);
348         if (vf_dev)
349                 rte_eth_dev_stop(vf_dev->data->port_id);
350         rte_spinlock_unlock(&hv->vf_lock);
351 }
352
353 /* If VF is present, then cascade configuration down */
354 #define VF_ETHDEV_FUNC(dev, func)                               \
355         {                                                       \
356                 struct hn_data *hv = (dev)->data->dev_private;  \
357                 struct rte_eth_dev *vf_dev;                     \
358                 rte_spinlock_lock(&hv->vf_lock);                \
359                 vf_dev = hn_get_vf_dev(hv);                     \
360                 if (vf_dev)                                     \
361                         func(vf_dev->data->port_id);            \
362                 rte_spinlock_unlock(&hv->vf_lock);              \
363         }
364
365 /* If VF is present, then cascade configuration down */
366 #define VF_ETHDEV_FUNC_RET_STATUS(dev, func)                    \
367         {                                                       \
368                 struct hn_data *hv = (dev)->data->dev_private;  \
369                 struct rte_eth_dev *vf_dev;                     \
370                 int ret = 0;                                    \
371                 rte_spinlock_lock(&hv->vf_lock);                \
372                 vf_dev = hn_get_vf_dev(hv);                     \
373                 if (vf_dev)                                     \
374                         ret = func(vf_dev->data->port_id);      \
375                 rte_spinlock_unlock(&hv->vf_lock);              \
376                 return ret;                                     \
377         }
378
379 void hn_vf_reset(struct rte_eth_dev *dev)
380 {
381         VF_ETHDEV_FUNC(dev, rte_eth_dev_reset);
382 }
383
384 void hn_vf_close(struct rte_eth_dev *dev)
385 {
386         struct hn_data *hv = dev->data->dev_private;
387         uint16_t vf_port;
388
389         rte_spinlock_lock(&hv->vf_lock);
390         vf_port = hv->vf_port;
391         if (vf_port != HN_INVALID_PORT)
392                 rte_eth_dev_close(vf_port);
393
394         hv->vf_port = HN_INVALID_PORT;
395         rte_spinlock_unlock(&hv->vf_lock);
396 }
397
398 void hn_vf_stats_reset(struct rte_eth_dev *dev)
399 {
400         VF_ETHDEV_FUNC(dev, rte_eth_stats_reset);
401 }
402
403 void hn_vf_allmulticast_enable(struct rte_eth_dev *dev)
404 {
405         VF_ETHDEV_FUNC(dev, rte_eth_allmulticast_enable);
406 }
407
408 void hn_vf_allmulticast_disable(struct rte_eth_dev *dev)
409 {
410         VF_ETHDEV_FUNC(dev, rte_eth_allmulticast_disable);
411 }
412
413 int hn_vf_promiscuous_enable(struct rte_eth_dev *dev)
414 {
415         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_enable);
416 }
417
418 int hn_vf_promiscuous_disable(struct rte_eth_dev *dev)
419 {
420         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_disable);
421 }
422
423 int hn_vf_mc_addr_list(struct rte_eth_dev *dev,
424                         struct rte_ether_addr *mc_addr_set,
425                         uint32_t nb_mc_addr)
426 {
427         struct hn_data *hv = dev->data->dev_private;
428         struct rte_eth_dev *vf_dev;
429         int ret = 0;
430
431         rte_spinlock_lock(&hv->vf_lock);
432         vf_dev = hn_get_vf_dev(hv);
433         if (vf_dev)
434                 ret = rte_eth_dev_set_mc_addr_list(vf_dev->data->port_id,
435                                                    mc_addr_set, nb_mc_addr);
436         rte_spinlock_unlock(&hv->vf_lock);
437         return ret;
438 }
439
440 int hn_vf_tx_queue_setup(struct rte_eth_dev *dev,
441                          uint16_t queue_idx, uint16_t nb_desc,
442                          unsigned int socket_id,
443                          const struct rte_eth_txconf *tx_conf)
444 {
445         struct hn_data *hv = dev->data->dev_private;
446         struct rte_eth_dev *vf_dev;
447         int ret = 0;
448
449         rte_spinlock_lock(&hv->vf_lock);
450         vf_dev = hn_get_vf_dev(hv);
451         if (vf_dev)
452                 ret = rte_eth_tx_queue_setup(vf_dev->data->port_id,
453                                              queue_idx, nb_desc,
454                                              socket_id, tx_conf);
455         rte_spinlock_unlock(&hv->vf_lock);
456         return ret;
457 }
458
459 void hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id)
460 {
461         struct rte_eth_dev *vf_dev;
462
463         rte_spinlock_lock(&hv->vf_lock);
464         vf_dev = hn_get_vf_dev(hv);
465         if (vf_dev && vf_dev->dev_ops->tx_queue_release) {
466                 void *subq = vf_dev->data->tx_queues[queue_id];
467
468                 (*vf_dev->dev_ops->tx_queue_release)(subq);
469         }
470
471         rte_spinlock_unlock(&hv->vf_lock);
472 }
473
474 int hn_vf_rx_queue_setup(struct rte_eth_dev *dev,
475                          uint16_t queue_idx, uint16_t nb_desc,
476                          unsigned int socket_id,
477                          const struct rte_eth_rxconf *rx_conf,
478                          struct rte_mempool *mp)
479 {
480         struct hn_data *hv = dev->data->dev_private;
481         struct rte_eth_dev *vf_dev;
482         int ret = 0;
483
484         rte_spinlock_lock(&hv->vf_lock);
485         vf_dev = hn_get_vf_dev(hv);
486         if (vf_dev)
487                 ret = rte_eth_rx_queue_setup(vf_dev->data->port_id,
488                                              queue_idx, nb_desc,
489                                              socket_id, rx_conf, mp);
490         rte_spinlock_unlock(&hv->vf_lock);
491         return ret;
492 }
493
494 void hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id)
495 {
496         struct rte_eth_dev *vf_dev;
497
498         rte_spinlock_lock(&hv->vf_lock);
499         vf_dev = hn_get_vf_dev(hv);
500         if (vf_dev && vf_dev->dev_ops->rx_queue_release) {
501                 void *subq = vf_dev->data->rx_queues[queue_id];
502
503                 (*vf_dev->dev_ops->rx_queue_release)(subq);
504         }
505         rte_spinlock_unlock(&hv->vf_lock);
506 }
507
508 int hn_vf_stats_get(struct rte_eth_dev *dev,
509                     struct rte_eth_stats *stats)
510 {
511         struct hn_data *hv = dev->data->dev_private;
512         struct rte_eth_dev *vf_dev;
513         int ret = 0;
514
515         rte_spinlock_lock(&hv->vf_lock);
516         vf_dev = hn_get_vf_dev(hv);
517         if (vf_dev)
518                 ret = rte_eth_stats_get(vf_dev->data->port_id, stats);
519         rte_spinlock_unlock(&hv->vf_lock);
520         return ret;
521 }
522
523 int hn_vf_xstats_get_names(struct rte_eth_dev *dev,
524                            struct rte_eth_xstat_name *names,
525                            unsigned int n)
526 {
527         struct hn_data *hv = dev->data->dev_private;
528         struct rte_eth_dev *vf_dev;
529         int i, count = 0;
530
531         rte_spinlock_lock(&hv->vf_lock);
532         vf_dev = hn_get_vf_dev(hv);
533         if (vf_dev)
534                 count = rte_eth_xstats_get_names(vf_dev->data->port_id,
535                                                  names, n);
536         rte_spinlock_unlock(&hv->vf_lock);
537
538         /* add vf_ prefix to xstat names */
539         if (names) {
540                 for (i = 0; i < count; i++) {
541                         char tmp[RTE_ETH_XSTATS_NAME_SIZE];
542
543                         snprintf(tmp, sizeof(tmp), "vf_%s", names[i].name);
544                         strlcpy(names[i].name, tmp, sizeof(names[i].name));
545                 }
546         }
547
548         return count;
549 }
550
551 int hn_vf_xstats_get(struct rte_eth_dev *dev,
552                      struct rte_eth_xstat *xstats,
553                      unsigned int offset,
554                      unsigned int n)
555 {
556         struct hn_data *hv = dev->data->dev_private;
557         struct rte_eth_dev *vf_dev;
558         int i, count = 0;
559
560         rte_spinlock_lock(&hv->vf_lock);
561         vf_dev = hn_get_vf_dev(hv);
562         if (vf_dev)
563                 count = rte_eth_xstats_get(vf_dev->data->port_id,
564                                            xstats + offset, n - offset);
565         rte_spinlock_unlock(&hv->vf_lock);
566
567         /* Offset id's for VF stats */
568         if (count > 0) {
569                 for (i = 0; i < count; i++)
570                         xstats[i + offset].id += offset;
571         }
572
573         return count;
574 }
575
576 void hn_vf_xstats_reset(struct rte_eth_dev *dev)
577 {
578         struct hn_data *hv = dev->data->dev_private;
579         struct rte_eth_dev *vf_dev;
580
581         rte_spinlock_lock(&hv->vf_lock);
582         vf_dev = hn_get_vf_dev(hv);
583         if (vf_dev)
584                 rte_eth_xstats_reset(vf_dev->data->port_id);
585         rte_spinlock_unlock(&hv->vf_lock);
586 }
587
588 int hn_vf_rss_hash_update(struct rte_eth_dev *dev,
589                           struct rte_eth_rss_conf *rss_conf)
590 {
591         struct hn_data *hv = dev->data->dev_private;
592         struct rte_eth_dev *vf_dev;
593         int ret = 0;
594
595         rte_spinlock_lock(&hv->vf_lock);
596         vf_dev = hn_get_vf_dev(hv);
597         if (vf_dev && vf_dev->dev_ops->rss_hash_update)
598                 ret = vf_dev->dev_ops->rss_hash_update(vf_dev, rss_conf);
599         rte_spinlock_unlock(&hv->vf_lock);
600
601         return ret;
602 }
603
604 int hn_vf_reta_hash_update(struct rte_eth_dev *dev,
605                            struct rte_eth_rss_reta_entry64 *reta_conf,
606                            uint16_t reta_size)
607 {
608         struct hn_data *hv = dev->data->dev_private;
609         struct rte_eth_dev *vf_dev;
610         int ret = 0;
611
612         rte_spinlock_lock(&hv->vf_lock);
613         vf_dev = hn_get_vf_dev(hv);
614         if (vf_dev && vf_dev->dev_ops->reta_update)
615                 ret = vf_dev->dev_ops->reta_update(vf_dev,
616                                                    reta_conf, reta_size);
617         rte_spinlock_unlock(&hv->vf_lock);
618
619         return ret;
620 }