1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018 Microsoft Corp.
14 #include <sys/types.h>
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>
24 #include <rte_bus_pci.h>
26 #include <rte_string_fns.h>
32 /* Search for VF with matching MAC address, return port id */
33 static int hn_vf_match(const struct rte_eth_dev *dev)
35 const struct rte_ether_addr *mac = dev->data->mac_addrs;
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;
45 if (rte_is_same_ether_addr(mac, vf_mac))
53 * Attach new PCI VF device and return the port_id
55 static int hn_vf_attach(struct hn_data *hv, uint16_t port_id)
57 struct rte_eth_dev_owner owner = { .id = RTE_ETH_DEV_NO_OWNER };
60 if (hn_vf_attached(hv)) {
61 PMD_DRV_LOG(ERR, "VF already attached");
65 ret = rte_eth_dev_owner_get(port_id, &owner);
67 PMD_DRV_LOG(ERR, "Can not find owner for port %d", port_id);
71 if (owner.id != RTE_ETH_DEV_NO_OWNER) {
72 PMD_DRV_LOG(ERR, "Port %u already owned by other device %s",
77 ret = rte_eth_dev_owner_set(port_id, &hv->owner);
79 PMD_DRV_LOG(ERR, "Can set owner for port %d", port_id);
83 PMD_DRV_LOG(DEBUG, "Attach VF device %u", port_id);
84 hv->vf_port = port_id;
90 /* Add new VF device to synthetic device */
91 int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv)
95 port = hn_vf_match(dev);
97 PMD_DRV_LOG(NOTICE, "No matching MAC found");
101 rte_spinlock_lock(&hv->vf_lock);
102 err = hn_vf_attach(hv, port);
105 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
106 hv->vf_intr = (struct rte_intr_handle) {
108 .type = RTE_INTR_HANDLE_EXT,
110 dev->intr_handle = &hv->vf_intr;
111 hn_nvs_set_datapath(hv, NVS_DATAPATH_VF);
113 rte_spinlock_unlock(&hv->vf_lock);
118 /* Remove new VF device */
119 static void hn_vf_remove(struct hn_data *hv)
122 rte_spinlock_lock(&hv->vf_lock);
124 if (!hn_vf_attached(hv)) {
125 PMD_DRV_LOG(ERR, "VF path not active");
127 /* Stop incoming packets from arriving on VF */
128 hn_nvs_set_datapath(hv, NVS_DATAPATH_SYNTHETIC);
130 /* Stop transmission over VF */
131 hv->vf_port = HN_INVALID_PORT;
134 /* Give back ownership */
135 rte_eth_dev_owner_unset(hv->vf_port, hv->owner.id);
137 rte_spinlock_unlock(&hv->vf_lock);
140 /* Handle VF association message from host */
142 hn_nvs_handle_vfassoc(struct rte_eth_dev *dev,
143 const struct vmbus_chanpkt_hdr *hdr,
146 struct hn_data *hv = dev->data->dev_private;
147 const struct hn_nvs_vf_association *vf_assoc = data;
149 if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*vf_assoc))) {
150 PMD_DRV_LOG(ERR, "invalid vf association NVS");
154 PMD_DRV_LOG(DEBUG, "VF serial %u %s port %u",
156 vf_assoc->allocated ? "add to" : "remove from",
159 hv->vf_present = vf_assoc->allocated;
161 if (dev->state != RTE_ETH_DEV_ATTACHED)
164 if (vf_assoc->allocated)
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.
175 static int hn_vf_info_merge(struct rte_eth_dev *vf_dev,
176 struct rte_eth_dev_info *info)
178 struct rte_eth_dev_info vf_info;
181 ret = rte_eth_dev_info_get(vf_dev->data->port_id, &vf_info);
185 info->speed_capa = vf_info.speed_capa;
186 info->default_rxportconf = vf_info.default_rxportconf;
187 info->default_txportconf = vf_info.default_txportconf;
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;
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;
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);
208 int hn_vf_info_get(struct hn_data *hv, struct rte_eth_dev_info *info)
210 struct rte_eth_dev *vf_dev;
213 rte_spinlock_lock(&hv->vf_lock);
214 vf_dev = hn_get_vf_dev(hv);
216 ret = hn_vf_info_merge(vf_dev, info);
217 rte_spinlock_unlock(&hv->vf_lock);
221 int hn_vf_link_update(struct rte_eth_dev *dev,
222 int wait_to_complete)
224 struct hn_data *hv = dev->data->dev_private;
225 struct rte_eth_dev *vf_dev;
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);
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)
242 struct rte_eth_dev *dev = cb_arg;
244 if (event != RTE_ETH_EVENT_INTR_LSC)
247 /* if link state has changed pass on */
248 if (hn_dev_link_update(dev, 0) == 0)
249 return 0; /* no change */
251 return _rte_eth_dev_callback_process(dev,
252 RTE_ETH_EVENT_INTR_LSC,
256 static int _hn_vf_configure(struct rte_eth_dev *dev,
258 const struct rte_eth_conf *dev_conf)
260 struct rte_eth_conf vf_conf = *dev_conf;
261 struct rte_eth_dev *vf_dev;
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",
269 vf_conf.intr_conf.lsc = 1;
271 PMD_DRV_LOG(DEBUG, "disabling LSC for VF %u",
273 vf_conf.intr_conf.lsc = 0;
276 ret = rte_eth_dev_configure(vf_port,
277 dev->data->nb_rx_queues,
278 dev->data->nb_tx_queues,
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);
289 "Failed to register LSC callback for VF %u",
296 * Configure VF if present.
297 * Force VF to have same number of queues as synthetic device
299 int hn_vf_configure(struct rte_eth_dev *dev,
300 const struct rte_eth_conf *dev_conf)
302 struct hn_data *hv = dev->data->dev_private;
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);
312 const uint32_t *hn_vf_supported_ptypes(struct rte_eth_dev *dev)
314 struct hn_data *hv = dev->data->dev_private;
315 struct rte_eth_dev *vf_dev;
316 const uint32_t *ptypes = NULL;
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);
327 int hn_vf_start(struct rte_eth_dev *dev)
329 struct hn_data *hv = dev->data->dev_private;
330 struct rte_eth_dev *vf_dev;
333 rte_spinlock_lock(&hv->vf_lock);
334 vf_dev = hn_get_vf_dev(hv);
336 ret = rte_eth_dev_start(vf_dev->data->port_id);
337 rte_spinlock_unlock(&hv->vf_lock);
341 void hn_vf_stop(struct rte_eth_dev *dev)
343 struct hn_data *hv = dev->data->dev_private;
344 struct rte_eth_dev *vf_dev;
346 rte_spinlock_lock(&hv->vf_lock);
347 vf_dev = hn_get_vf_dev(hv);
349 rte_eth_dev_stop(vf_dev->data->port_id);
350 rte_spinlock_unlock(&hv->vf_lock);
353 /* If VF is present, then cascade configuration down */
354 #define VF_ETHDEV_FUNC(dev, func) \
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); \
361 func(vf_dev->data->port_id); \
362 rte_spinlock_unlock(&hv->vf_lock); \
365 void hn_vf_reset(struct rte_eth_dev *dev)
367 VF_ETHDEV_FUNC(dev, rte_eth_dev_reset);
370 void hn_vf_close(struct rte_eth_dev *dev)
372 struct hn_data *hv = dev->data->dev_private;
375 rte_spinlock_lock(&hv->vf_lock);
376 vf_port = hv->vf_port;
377 if (vf_port != HN_INVALID_PORT)
378 rte_eth_dev_close(vf_port);
380 hv->vf_port = HN_INVALID_PORT;
381 rte_spinlock_unlock(&hv->vf_lock);
384 void hn_vf_stats_reset(struct rte_eth_dev *dev)
386 VF_ETHDEV_FUNC(dev, rte_eth_stats_reset);
389 void hn_vf_allmulticast_enable(struct rte_eth_dev *dev)
391 VF_ETHDEV_FUNC(dev, rte_eth_allmulticast_enable);
394 void hn_vf_allmulticast_disable(struct rte_eth_dev *dev)
396 VF_ETHDEV_FUNC(dev, rte_eth_allmulticast_disable);
399 void hn_vf_promiscuous_enable(struct rte_eth_dev *dev)
401 VF_ETHDEV_FUNC(dev, rte_eth_promiscuous_enable);
404 void hn_vf_promiscuous_disable(struct rte_eth_dev *dev)
406 VF_ETHDEV_FUNC(dev, rte_eth_promiscuous_disable);
409 int hn_vf_mc_addr_list(struct rte_eth_dev *dev,
410 struct rte_ether_addr *mc_addr_set,
413 struct hn_data *hv = dev->data->dev_private;
414 struct rte_eth_dev *vf_dev;
417 rte_spinlock_lock(&hv->vf_lock);
418 vf_dev = hn_get_vf_dev(hv);
420 ret = rte_eth_dev_set_mc_addr_list(vf_dev->data->port_id,
421 mc_addr_set, nb_mc_addr);
422 rte_spinlock_unlock(&hv->vf_lock);
426 int hn_vf_tx_queue_setup(struct rte_eth_dev *dev,
427 uint16_t queue_idx, uint16_t nb_desc,
428 unsigned int socket_id,
429 const struct rte_eth_txconf *tx_conf)
431 struct hn_data *hv = dev->data->dev_private;
432 struct rte_eth_dev *vf_dev;
435 rte_spinlock_lock(&hv->vf_lock);
436 vf_dev = hn_get_vf_dev(hv);
438 ret = rte_eth_tx_queue_setup(vf_dev->data->port_id,
441 rte_spinlock_unlock(&hv->vf_lock);
445 void hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id)
447 struct rte_eth_dev *vf_dev;
449 rte_spinlock_lock(&hv->vf_lock);
450 vf_dev = hn_get_vf_dev(hv);
451 if (vf_dev && vf_dev->dev_ops->tx_queue_release) {
452 void *subq = vf_dev->data->tx_queues[queue_id];
454 (*vf_dev->dev_ops->tx_queue_release)(subq);
457 rte_spinlock_unlock(&hv->vf_lock);
460 int hn_vf_rx_queue_setup(struct rte_eth_dev *dev,
461 uint16_t queue_idx, uint16_t nb_desc,
462 unsigned int socket_id,
463 const struct rte_eth_rxconf *rx_conf,
464 struct rte_mempool *mp)
466 struct hn_data *hv = dev->data->dev_private;
467 struct rte_eth_dev *vf_dev;
470 rte_spinlock_lock(&hv->vf_lock);
471 vf_dev = hn_get_vf_dev(hv);
473 ret = rte_eth_rx_queue_setup(vf_dev->data->port_id,
475 socket_id, rx_conf, mp);
476 rte_spinlock_unlock(&hv->vf_lock);
480 void hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id)
482 struct rte_eth_dev *vf_dev;
484 rte_spinlock_lock(&hv->vf_lock);
485 vf_dev = hn_get_vf_dev(hv);
486 if (vf_dev && vf_dev->dev_ops->rx_queue_release) {
487 void *subq = vf_dev->data->rx_queues[queue_id];
489 (*vf_dev->dev_ops->rx_queue_release)(subq);
491 rte_spinlock_unlock(&hv->vf_lock);
494 int hn_vf_stats_get(struct rte_eth_dev *dev,
495 struct rte_eth_stats *stats)
497 struct hn_data *hv = dev->data->dev_private;
498 struct rte_eth_dev *vf_dev;
501 rte_spinlock_lock(&hv->vf_lock);
502 vf_dev = hn_get_vf_dev(hv);
504 ret = rte_eth_stats_get(vf_dev->data->port_id, stats);
505 rte_spinlock_unlock(&hv->vf_lock);
509 int hn_vf_xstats_get_names(struct rte_eth_dev *dev,
510 struct rte_eth_xstat_name *names,
513 struct hn_data *hv = dev->data->dev_private;
514 struct rte_eth_dev *vf_dev;
517 rte_spinlock_lock(&hv->vf_lock);
518 vf_dev = hn_get_vf_dev(hv);
520 count = rte_eth_xstats_get_names(vf_dev->data->port_id,
522 rte_spinlock_unlock(&hv->vf_lock);
524 /* add vf_ prefix to xstat names */
526 for (i = 0; i < count; i++) {
527 char tmp[RTE_ETH_XSTATS_NAME_SIZE];
529 snprintf(tmp, sizeof(tmp), "vf_%s", names[i].name);
530 strlcpy(names[i].name, tmp, sizeof(names[i].name));
537 int hn_vf_xstats_get(struct rte_eth_dev *dev,
538 struct rte_eth_xstat *xstats,
542 struct hn_data *hv = dev->data->dev_private;
543 struct rte_eth_dev *vf_dev;
546 rte_spinlock_lock(&hv->vf_lock);
547 vf_dev = hn_get_vf_dev(hv);
549 count = rte_eth_xstats_get(vf_dev->data->port_id,
550 xstats + offset, n - offset);
551 rte_spinlock_unlock(&hv->vf_lock);
553 /* Offset id's for VF stats */
555 for (i = 0; i < count; i++)
556 xstats[i + offset].id += offset;
562 void hn_vf_xstats_reset(struct rte_eth_dev *dev)
564 struct hn_data *hv = dev->data->dev_private;
565 struct rte_eth_dev *vf_dev;
567 rte_spinlock_lock(&hv->vf_lock);
568 vf_dev = hn_get_vf_dev(hv);
570 rte_eth_xstats_reset(vf_dev->data->port_id);
571 rte_spinlock_unlock(&hv->vf_lock);
574 int hn_vf_rss_hash_update(struct rte_eth_dev *dev,
575 struct rte_eth_rss_conf *rss_conf)
577 struct hn_data *hv = dev->data->dev_private;
578 struct rte_eth_dev *vf_dev;
581 rte_spinlock_lock(&hv->vf_lock);
582 vf_dev = hn_get_vf_dev(hv);
583 if (vf_dev && vf_dev->dev_ops->rss_hash_update)
584 ret = vf_dev->dev_ops->rss_hash_update(vf_dev, rss_conf);
585 rte_spinlock_unlock(&hv->vf_lock);
590 int hn_vf_reta_hash_update(struct rte_eth_dev *dev,
591 struct rte_eth_rss_reta_entry64 *reta_conf,
594 struct hn_data *hv = dev->data->dev_private;
595 struct rte_eth_dev *vf_dev;
598 rte_spinlock_lock(&hv->vf_lock);
599 vf_dev = hn_get_vf_dev(hv);
600 if (vf_dev && vf_dev->dev_ops->reta_update)
601 ret = vf_dev->dev_ops->reta_update(vf_dev,
602 reta_conf, reta_size);
603 rte_spinlock_unlock(&hv->vf_lock);