diff --git a/include/lib/ap.h b/include/lib/ap.h index e749f47a..3201fa63 100644 --- a/include/lib/ap.h +++ b/include/lib/ap.h @@ -88,6 +88,9 @@ bool ap_read_udev_masks(char *path, char *ap, char *aq, bool *read_ap, bool *read_aq); void ap_mask_to_list(char *mask, struct util_list *list); void ap_list_remove_all(struct util_list *list); +char *vfio_ap_device_get_adapter_mask(struct vfio_ap_device *dev, int *size); +char *vfio_ap_device_get_domain_mask(struct vfio_ap_device *dev, int *size); +char *vfio_ap_device_get_control_mask(struct vfio_ap_device *dev, int *size); /* Lock Functions */ int ap_get_lock(void); diff --git a/libap/ap.c b/libap/ap.c index d5116d8d..bb0fd66c 100644 --- a/libap/ap.c +++ b/libap/ap.c @@ -33,6 +33,9 @@ #include "lib/util_path.h" #include "lib/util_udev.h" +static const char default_mask[AP_MASK_SIZE] = + "0x0000000000000000000000000000000000000000000000000000000000000000"; + /* * Return sysfs path to a bus attribute * Note: caller is responsible for freeing the returned string @@ -737,6 +740,90 @@ static unsigned int random_delay(void) return AP_LOCK_DELAY_US + (rand() % AP_LOCK_VARIANCE_US); } +/** + * Return a mask of assigned adapters for the specified vfio_ap device. + * Note: caller is responsible for freeing the returned string + * + * @param[in] dev Vfio-ap struct to get adapter mask from + * @param[in, out] size Size of mask buffer returned + * + * @retval != 0 Adapter mask (hex string) + * @retval 0 Failed to generate a mask + */ +char *vfio_ap_device_get_adapter_mask(struct vfio_ap_device *dev, int *size) +{ + struct vfio_ap_node *node; + char *mask; + + if (!dev || !size) + return NULL; + + mask = util_strdup(default_mask); + *size = AP_MASK_SIZE; + + util_list_iterate(dev->adapters, node) { + ap_set_bit(node->id, mask, true); + } + + return mask; +} + +/** + * Return a mask of assigned domains for the specified vfio_ap device. + * Note: caller is responsible for freeing the returned string + * + * @param[in] dev Vfio-ap struct to get domain mask from + * @param[in, out] size Size of mask buffer returned + * + * @retval != 0 Domain mask (hex string) + * @retval 0 Failed to generate a mask + */ +char *vfio_ap_device_get_domain_mask(struct vfio_ap_device *dev, int *size) +{ + struct vfio_ap_node *node; + char *mask; + + if (!dev || !size) + return NULL; + + mask = util_strdup(default_mask); + *size = AP_MASK_SIZE; + + util_list_iterate(dev->domains, node) { + ap_set_bit(node->id, mask, true); + } + + return mask; +} + +/** + * Return a mask of assigned control domains for the specified vfio_ap device. + * Note: caller is responsible for freeing the returned string + * + * @param[in] dev Vfio-ap struct to get control domain mask from + * @param[in, out] size Size of mask buffer returned + * + * @retval != 0 Control domain mask (hex string) + * @retval 0 Failed to generate a mask + */ +char *vfio_ap_device_get_control_mask(struct vfio_ap_device *dev, int *size) +{ + struct vfio_ap_node *node; + char *mask; + + if (!dev || !size) + return NULL; + + mask = util_strdup(default_mask); + *size = AP_MASK_SIZE; + + util_list_iterate(dev->controls, node) { + ap_set_bit(node->id, mask, true); + } + + return mask; +} + /** * Acquire the ap config lock using this Process ID *