Content-Type: multipart/mixed; boundary="===============2803732426577947997==" MIME-Version: 1.0 From: Jiang, Dave Subject: [Accel-config] Re: [PATCH v1 05/11] accel-config: Implement create and remove mdev library API Date: Fri, 04 Dec 2020 23:59:26 +0000 Message-ID: <0bee6967e85946a9b9cf63331114bb31@intel.com> In-Reply-To: 20201202193057.299615-6-ramesh.thomas@intel.com To: accel-config@lists.01.org List-ID: --===============2803732426577947997== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable > -----Original Message----- > From: ramesh.thomas(a)intel.com > Sent: Wednesday, December 2, 2020 12:31 PM > To: accel-config(a)lists.01.org > Cc: Thomas, Ramesh ; Luck, Tony > ; Jiang, Dave > Subject: [PATCH v1 05/11] accel-config: Implement create and remove mdev > library API > = > From: Ramesh Thomas > = > Adds accfg_create_mdev and accfg_remove_mdev library functions. > = > Signed-off-by: Ramesh Thomas Reviewed-by: Dave Jiang > --- > 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 6a4648a..ce789e6 100644 > --- a/accfg/lib/libaccfg.c > +++ b/accfg/lib/libaccfg.c > @@ -978,6 +978,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 uuid) > +{ > + 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(struct 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 type, > + 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 --===============2803732426577947997==--