Content-Type: multipart/mixed; boundary="===============2993628212941828013==" MIME-Version: 1.0 From: ramesh.thomas at intel.com Subject: [Accel-config] [PATCH v3 05/14] accel-config: Implement create and remove mdev library API Date: Sat, 05 Dec 2020 15:20:17 -0500 Message-ID: <20201205202025.315186-6-ramesh.thomas@intel.com> In-Reply-To: 20201205202025.315186-1-ramesh.thomas@intel.com To: accel-config@lists.01.org List-ID: --===============2993628212941828013== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Ramesh Thomas Adds accfg_create_mdev and accfg_remove_mdev library functions. Signed-off-by: Ramesh Thomas --- accfg/lib/libaccel-config.sym | 2 + accfg/lib/libaccfg.c | 104 ++++++++++++++++++++++++++++++++++ accfg/libaccel_config.h | 3 + 3 files changed, 109 insertions(+) diff --git a/accfg/lib/libaccel-config.sym b/accfg/lib/libaccel-config.sym index a857152..73c8417 100644 --- a/accfg/lib/libaccel-config.sym +++ b/accfg/lib/libaccel-config.sym @@ -141,4 +141,6 @@ global: accfg_mdev_get_uuid; accfg_mdev_get_type; accfg_mdev_basenames; + accfg_create_mdev; + accfg_remove_mdev; } LIBACCFG_7; diff --git a/accfg/lib/libaccfg.c b/accfg/lib/libaccfg.c index 2cd4e3c..106e21e 100644 --- a/accfg/lib/libaccfg.c +++ b/accfg/lib/libaccfg.c @@ -979,6 +979,110 @@ ACCFG_EXPORT enum accfg_mdev_type accfg_mdev_get_type= (struct accfg_device_mdev * return mdev->type; } = +static void accfg_gen_uuid(struct accfg_device *device, uuid_t uuid) +{ + struct accfg_device_mdev *entry, *next; + + uuid_clear(uuid); + while (1) { + uuid_generate(uuid); + if (list_empty(&device->mdev_list)) + return; + list_for_each_safe(&device->mdev_list, entry, next, list) { + if (uuid_compare(uuid, entry->uuid) !=3D 0) + return; + } + } +} + +ACCFG_EXPORT int accfg_create_mdev(struct accfg_device *device, + enum accfg_mdev_type type, uuid_t uuid) +{ + struct accfg_ctx *ctx =3D accfg_device_get_ctx(device); + struct accfg_device_mdev *mdev; + char uuid_str[UUID_STR_LEN]; + char mdev_path[PATH_MAX]; + int rc; + + if (type >=3D ACCFG_MDEV_TYPE_UNKNOWN || type < 0) + return -EINVAL; + + mdev =3D calloc(1, sizeof(struct accfg_device_mdev)); + if (!mdev) { + err(ctx, "mdev allocation failed\n"); + return -ENOMEM; + } + accfg_gen_uuid(device, mdev->uuid); + mdev->type =3D type; + + uuid_unparse(mdev->uuid, uuid_str); + sprintf(mdev_path, "%s/%s/idxd-%s/create", device->mdev_path, + MDEV_POSTFIX, accfg_mdev_basenames[type]); + rc =3D sysfs_write_attr(ctx, mdev_path, uuid_str); + if (rc < 0) { + err(ctx, "create mdev failed %d\n", rc); + goto create_err; + } + + list_add_tail(&device->mdev_list, &mdev->list); + uuid_copy(uuid, mdev->uuid); + return 0; + +create_err: + free(mdev); + return rc; +} + +static int accfg_device_mdev_remove(struct accfg_device *device, + struct accfg_device_mdev *mdev) +{ + struct accfg_ctx *ctx =3D accfg_device_get_ctx(device); + char uuid_str[UUID_STR_LEN]; + char mdev_path[PATH_MAX]; + int rc; + + uuid_unparse(mdev->uuid, uuid_str); + sprintf(mdev_path, "%s/%s/remove", device->mdev_path, uuid_str); + rc =3D sysfs_write_attr(ctx, mdev_path, "1"); + if (rc < 0) + return rc; + + list_del(&mdev->list); + free(mdev); + + return 0; +} + +ACCFG_EXPORT int accfg_remove_mdev(struct accfg_device *device, uuid_t uui= d) +{ + struct accfg_ctx *ctx =3D accfg_device_get_ctx(device); + struct accfg_device_mdev *entry, *next; + int rc, all; + + /* remove all mdevs if null uuid is passed */ + all =3D uuid_is_null(uuid); + list_for_each_safe(&device->mdev_list, entry, next, list) { + if (all || !uuid_compare(entry->uuid, uuid)) { + rc =3D accfg_device_mdev_remove(device, entry); + if (rc < 0) + goto remove_err; + if (!all) + return 0; + } + } + + if (!all) { + err(ctx, "mdev uuid not found\n"); + return -EINVAL; + } + + return 0; + +remove_err: + err(ctx, "remove mdev failed %d\n", rc); + return rc; +} + /** * accfg_device_get_first - retrieve first device in the system * @ctx: context established by accfg_new diff --git a/accfg/libaccel_config.h b/accfg/libaccel_config.h index a55bfde..95678fb 100644 --- a/accfg/libaccel_config.h +++ b/accfg/libaccel_config.h @@ -189,6 +189,9 @@ struct accfg_device_mdev *accfg_device_first_mdev(struc= t accfg_device *device); struct accfg_device_mdev *accfg_device_next_mdev(struct accfg_device_mdev = *mdev); void accfg_mdev_get_uuid(struct accfg_device_mdev *mdev, uuid_t uuid); enum accfg_mdev_type accfg_mdev_get_type(struct accfg_device_mdev *mdev); +int accfg_create_mdev(struct accfg_device *device, enum accfg_mdev_type ty= pe, + uuid_t uuid); +int accfg_remove_mdev(struct accfg_device *device, uuid_t uuid); = #define accfg_device_mdev_foreach(device, mdev) \ for (mdev =3D accfg_device_first_mdev(device); \ -- = 2.26.2 --===============2993628212941828013==--