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 /* If VF is present, then cascade configuration down */
366 #define VF_ETHDEV_FUNC_RET_STATUS(dev, func) \
368 struct hn_data *hv = (dev)->data->dev_private; \
369 struct rte_eth_dev *vf_dev; \
371 rte_spinlock_lock(&hv->vf_lock); \
372 vf_dev = hn_get_vf_dev(hv); \
374 ret = func(vf_dev->data->port_id); \
375 rte_spinlock_unlock(&hv->vf_lock); \
379 void hn_vf_reset(struct rte_eth_dev *dev)
381 VF_ETHDEV_FUNC(dev, rte_eth_dev_reset);
384 void hn_vf_close(struct rte_eth_dev *dev)
386 struct hn_data *hv = dev->data->dev_private;
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);
394 hv->vf_port = HN_INVALID_PORT;
395 rte_spinlock_unlock(&hv->vf_lock);
398 int hn_vf_stats_reset(struct rte_eth_dev *dev)
400 VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_stats_reset);
403 int hn_vf_allmulticast_enable(struct rte_eth_dev *dev)
405 VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_allmulticast_enable);
408 int hn_vf_allmulticast_disable(struct rte_eth_dev *dev)
410 VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_allmulticast_disable);
413 int hn_vf_promiscuous_enable(struct rte_eth_dev *dev)
415 VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_enable);
418 int hn_vf_promiscuous_disable(struct rte_eth_dev *dev)
420 VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_disable);
423 int hn_vf_mc_addr_list(struct rte_eth_dev *dev,
424 struct rte_ether_addr *mc_addr_set,
427 struct hn_data *hv = dev->data->dev_private;
428 struct rte_eth_dev *vf_dev;
431 rte_spinlock_lock(&hv->vf_lock);
432 vf_dev = hn_get_vf_dev(hv);
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);
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)
445 struct hn_data *hv = dev->data->dev_private;
446 struct rte_eth_dev *vf_dev;
449 rte_spinlock_lock(&hv->vf_lock);
450 vf_dev = hn_get_vf_dev(hv);
452 ret = rte_eth_tx_queue_setup(vf_dev->data->port_id,
455 rte_spinlock_unlock(&hv->vf_lock);
459 void hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id)
461 struct rte_eth_dev *vf_dev;
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];
468 (*vf_dev->dev_ops->tx_queue_release)(subq);
471 rte_spinlock_unlock(&hv->vf_lock);
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)
480 struct hn_data *hv = dev->data->dev_private;
481 struct rte_eth_dev *vf_dev;
484 rte_spinlock_lock(&hv->vf_lock);
485 vf_dev = hn_get_vf_dev(hv);
487 ret = rte_eth_rx_queue_setup(vf_dev->data->port_id,
489 socket_id, rx_conf, mp);
490 rte_spinlock_unlock(&hv->vf_lock);
494 void hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id)
496 struct rte_eth_dev *vf_dev;
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];
503 (*vf_dev->dev_ops->rx_queue_release)(subq);
505 rte_spinlock_unlock(&hv->vf_lock);
508 int hn_vf_stats_get(struct rte_eth_dev *dev,
509 struct rte_eth_stats *stats)
511 struct hn_data *hv = dev->data->dev_private;
512 struct rte_eth_dev *vf_dev;
515 rte_spinlock_lock(&hv->vf_lock);
516 vf_dev = hn_get_vf_dev(hv);
518 ret = rte_eth_stats_get(vf_dev->data->port_id, stats);
519 rte_spinlock_unlock(&hv->vf_lock);
523 int hn_vf_xstats_get_names(struct rte_eth_dev *dev,
524 struct rte_eth_xstat_name *names,
527 struct hn_data *hv = dev->data->dev_private;
528 struct rte_eth_dev *vf_dev;
531 rte_spinlock_lock(&hv->vf_lock);
532 vf_dev = hn_get_vf_dev(hv);
534 count = rte_eth_xstats_get_names(vf_dev->data->port_id,
536 rte_spinlock_unlock(&hv->vf_lock);
538 /* add vf_ prefix to xstat names */
540 for (i = 0; i < count; i++) {
541 char tmp[RTE_ETH_XSTATS_NAME_SIZE];
543 snprintf(tmp, sizeof(tmp), "vf_%s", names[i].name);
544 strlcpy(names[i].name, tmp, sizeof(names[i].name));
551 int hn_vf_xstats_get(struct rte_eth_dev *dev,
552 struct rte_eth_xstat *xstats,
556 struct hn_data *hv = dev->data->dev_private;
557 struct rte_eth_dev *vf_dev;
560 rte_spinlock_lock(&hv->vf_lock);
561 vf_dev = hn_get_vf_dev(hv);
563 count = rte_eth_xstats_get(vf_dev->data->port_id,
564 xstats + offset, n - offset);
565 rte_spinlock_unlock(&hv->vf_lock);
567 /* Offset id's for VF stats */
569 for (i = 0; i < count; i++)
570 xstats[i + offset].id += offset;
576 int hn_vf_xstats_reset(struct rte_eth_dev *dev)
578 struct hn_data *hv = dev->data->dev_private;
579 struct rte_eth_dev *vf_dev;
582 rte_spinlock_lock(&hv->vf_lock);
583 vf_dev = hn_get_vf_dev(hv);
585 ret = rte_eth_xstats_reset(vf_dev->data->port_id);
588 rte_spinlock_unlock(&hv->vf_lock);
593 int hn_vf_rss_hash_update(struct rte_eth_dev *dev,
594 struct rte_eth_rss_conf *rss_conf)
596 struct hn_data *hv = dev->data->dev_private;
597 struct rte_eth_dev *vf_dev;
600 rte_spinlock_lock(&hv->vf_lock);
601 vf_dev = hn_get_vf_dev(hv);
602 if (vf_dev && vf_dev->dev_ops->rss_hash_update)
603 ret = vf_dev->dev_ops->rss_hash_update(vf_dev, rss_conf);
604 rte_spinlock_unlock(&hv->vf_lock);
609 int hn_vf_reta_hash_update(struct rte_eth_dev *dev,
610 struct rte_eth_rss_reta_entry64 *reta_conf,
613 struct hn_data *hv = dev->data->dev_private;
614 struct rte_eth_dev *vf_dev;
617 rte_spinlock_lock(&hv->vf_lock);
618 vf_dev = hn_get_vf_dev(hv);
619 if (vf_dev && vf_dev->dev_ops->reta_update)
620 ret = vf_dev->dev_ops->reta_update(vf_dev,
621 reta_conf, reta_size);
622 rte_spinlock_unlock(&hv->vf_lock);