Commit 49354bde authored by Yizhen Fan's avatar Yizhen Fan Committed by fanyizhen1995
Browse files

ub: uburma support query vf status and write in cdev



driver inclusion
category: feature
bugzilla: NA
CVE: NA

--------------------------------

Uburma support query vf status and write in cdev.
Uburma will query vf status when uburma device creates, and write port
related attrs under /dev/uburma/<ubc_dev->dev_name>/vf*

Signed-off-by: default avatarGuoxin Qian <qianguoxin@huawei.com>
Signed-off-by: default avatarYizhen Fan <fanyizhen@huawei.com>
parent 0ede0194
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@
#include "ubcore_log.h"
#include "ubcore_netlink.h"
#include <urma/ubcore_types.h>
#include <urma/ubcore_api.h>
#include <urma/ubcore_uapi.h>
#include "ubcore_priv.h"

+63 −0
Original line number Diff line number Diff line
@@ -37,6 +37,10 @@ typedef ssize_t (*uburma_show_attr_cb)(const struct ubcore_device *ubc_dev, char
typedef ssize_t (*uburma_store_attr_cb)(struct ubcore_device *ubc_dev, const char *buf, size_t len);
typedef ssize_t (*uburma_show_port_attr_cb)(const struct ubcore_device *ubc_dev, char *buf,
					    uint8_t port_num);
typedef ssize_t (*uburma_show_vf_attr_cb)(const struct ubcore_device *ubc_dev, char *buf,
					  uint16_t vf_num);
typedef ssize_t (*uburma_store_vf_attr_cb)(struct ubcore_device *ubc_dev, const char *buf,
					   size_t len, uint16_t vf_num);

static ssize_t uburma_show_dev_attr(struct device *dev, struct device_attribute *attr, char *buf,
				    uburma_show_attr_cb show_cb)
@@ -565,6 +569,48 @@ static struct kobj_type uburma_port_type = { .release = uburma_port_release,
					     .default_attrs = uburma_port_attrs
};

static struct attribute *uburma_vf_attrs[] = {
	NULL,
};

static ssize_t uburma_vf_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
	struct uburma_vf_attribute *vf_attr = container_of(attr, struct uburma_vf_attribute, attr);
	struct uburma_vf *vf = container_of(kobj, struct uburma_vf, kobj);

	if (!vf_attr->show)
		return -EIO;

	return vf_attr->show(vf, vf_attr, buf);
}

static ssize_t uburma_vf_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf,
				    size_t count)
{
	struct uburma_vf_attribute *vf_attr = container_of(attr, struct uburma_vf_attribute, attr);
	struct uburma_vf *vf = container_of(kobj, struct uburma_vf, kobj);

	if (!vf_attr->store)
		return -EIO;
	return vf_attr->store(vf, vf_attr, buf, count);
}

static const struct sysfs_ops uburma_vf_sysfs_ops = { .show = uburma_vf_attr_show,
						      .store = uburma_vf_attr_store };

static void uburma_vf_release(struct kobject *kobj)
{
}

static const struct attribute_group uburma_vf_groups = {
	.attrs = uburma_vf_attrs,
};

static struct kobj_type uburma_vf_type = { .release = uburma_vf_release,
					   .sysfs_ops = &uburma_vf_sysfs_ops,
					   .default_attrs = uburma_vf_attrs
};

int uburma_create_port_attr_files(struct uburma_device *ubu_dev, uint8_t port_num)
{
	struct uburma_port *p;
@@ -577,6 +623,18 @@ int uburma_create_port_attr_files(struct uburma_device *ubu_dev, uint8_t port_nu
				    port_num);
}

int uburma_create_vf_attr_files(struct uburma_device *ubu_dev, uint32_t vf_num)
{
	struct uburma_vf *vf;

	vf = &ubu_dev->vf[vf_num];
	vf->ubu_dev = ubu_dev;
	vf->vf_idx = vf_num;

	return kobject_init_and_add(&vf->kobj, &uburma_vf_type, &ubu_dev->dev->kobj, "vf%u",
				    vf_num);
}

int uburma_create_dev_attr_files(struct uburma_device *ubu_dev)
{
	int ret;
@@ -595,6 +653,11 @@ void uburma_remove_port_attr_files(struct uburma_device *ubu_dev, uint8_t port_n
	kobject_put(&ubu_dev->port[port_num].kobj);
}

void uburma_remove_vf_attr_files(struct uburma_device *ubu_dev, uint32_t vf_num)
{
	kobject_put(&ubu_dev->vf[vf_num].kobj);
}

void uburma_remove_dev_attr_files(struct uburma_device *ubu_dev)
{
	sysfs_remove_group(&ubu_dev->dev->kobj, &uburma_dev_attr_group);
+14 −0
Original line number Diff line number Diff line
@@ -35,9 +35,23 @@ struct uburma_port_attribute {

#define PORT_ATTR_RO(_name) struct uburma_port_attribute port_attr_##_name = __ATTR_RO(_name)

struct uburma_vf_attribute {
	struct attribute attr;
	ssize_t (*show)(struct uburma_vf *vf, struct uburma_vf_attribute *attr, char *buf);
	ssize_t (*store)(struct uburma_vf *vf, struct uburma_vf_attribute *attr, const char *buf,
			 size_t count);
};

#define VF_ATTR(_name, _mode, _show, _store)	\
	struct uburma_vf_attribute vf_attr_##_name = __ATTR(_name, _mode, _show, _store)

#define VF_ATTR_RO(_name) struct uburma_vf_attribute vf_attr_##_name = __ATTR_RO(_name)

int uburma_create_port_attr_files(struct uburma_device *ubu_dev, uint8_t port_num);
int uburma_create_vf_attr_files(struct uburma_device *ubu_dev, uint32_t vf_num);
int uburma_create_dev_attr_files(struct uburma_device *ubu_dev);
void uburma_remove_port_attr_files(struct uburma_device *ubu_dev, uint8_t port_num);
void uburma_remove_vf_attr_files(struct uburma_device *ubu_dev, uint32_t vf_num);
void uburma_remove_dev_attr_files(struct uburma_device *ubu_dev);

#endif /* UBURMA_CDEV_FILE_H */
+54 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include "uburma_types.h"
#include "uburma_file_ops.h"
#include "uburma_cdev_file.h"
#include "uburma_uobj.h"
#include "uburma_cmd.h"

#define UBURMA_MAX_DEVICE 1024
@@ -95,7 +96,7 @@ static int uburma_get_devt(dev_t *devt)

static int uburma_device_create(struct uburma_device *ubu_dev, struct ubcore_device *ubc_dev)
{
	uint8_t i, j;
	uint8_t i, j, k;

	/* create /dev/uburma/<ubc_dev->dev_name> */
	ubu_dev->dev = device_create(g_uburma_class, ubc_dev->dev.parent, ubu_dev->cdev.dev,
@@ -116,8 +117,17 @@ static int uburma_device_create(struct uburma_device *ubu_dev, struct ubcore_dev
			goto err_port_attr;
	}

	/* create /dev/uburma/<ubc_dev->dev_name>/vf* */
	for (k = 0; k < ubc_dev->attr.vf_cnt; k++) {
		if (uburma_create_vf_attr_files(ubu_dev, k) != 0)
			goto err_vf_attr;
	}

	return 0;

err_vf_attr:
	for (j = 0; j < k; j++)
		uburma_remove_vf_attr_files(ubu_dev, j);
err_port_attr:
	for (j = 0; j < i; j++)
		uburma_remove_port_attr_files(ubu_dev, j);
@@ -131,6 +141,15 @@ static int uburma_device_create(struct uburma_device *ubu_dev, struct ubcore_dev
static void uburma_device_destroy(struct uburma_device *ubu_dev,
				  const struct ubcore_device *ubc_dev)
{
	uint8_t i;

	for (i = 0; i < ubc_dev->attr.vf_cnt; i++)
		uburma_remove_vf_attr_files(ubu_dev, i);

	for (i = 0; i < ubc_dev->attr.port_cnt; i++)
		uburma_remove_port_attr_files(ubu_dev, i);

	uburma_remove_dev_attr_files(ubu_dev);
	device_destroy(g_uburma_class, ubu_dev->cdev.dev);
}

@@ -213,6 +232,38 @@ static int uburma_add_device(struct ubcore_device *ubc_dev)
	return -EPERM;
}

static void uburma_free_ucontext(struct uburma_device *ubu_dev, struct ubcore_device *ubc_dev)
{
	struct uburma_file *file;

	rcu_assign_pointer(ubu_dev->ubc_dev, NULL);
	synchronize_srcu(&ubu_dev->ubc_dev_srcu);

	mutex_lock(&ubu_dev->lists_mutex);
	while (list_empty(&ubu_dev->uburma_file_list) == false) {
		struct ubcore_ucontext *ucontext;

		file = list_first_entry(&ubu_dev->uburma_file_list, struct uburma_file, list);
		file->is_closed = true;
		list_del(&file->list);
		kref_get(&file->ref);
		mutex_unlock(&ubu_dev->lists_mutex);

		mutex_lock(&file->mutex);
		uburma_cleanup_uobjs(file, UBURMA_REMOVE_DRIVER_REMOVE);
		ucontext = file->ucontext;
		file->ucontext = NULL;
		if (ucontext != NULL)
			ubcore_free_ucontext(ubc_dev, ucontext);

		mutex_unlock(&file->mutex);

		mutex_lock(&ubu_dev->lists_mutex);
		(void)kref_put(&file->ref, uburma_release_file);
	}
	mutex_unlock(&ubu_dev->lists_mutex);
}

static void uburma_remove_device(struct ubcore_device *ubc_dev, void *client_ctx)
{
	struct uburma_device *ubu_dev = client_ctx;
@@ -224,6 +275,8 @@ static void uburma_remove_device(struct ubcore_device *ubc_dev, void *client_ctx
	cdev_del(&ubu_dev->cdev);
	clear_bit(ubu_dev->devnum, g_dev_bitmap);

	uburma_free_ucontext(ubu_dev, ubc_dev);

	if (atomic_dec_and_test(&ubu_dev->refcnt))
		complete(&ubu_dev->comp);

+8 −0
Original line number Diff line number Diff line
@@ -65,6 +65,13 @@ struct uburma_port {
	uint8_t port_num;
};

struct uburma_vf {
	struct kobject kobj;
	struct uburma_device *ubu_dev;
	uint32_t vf_idx;
};


struct uburma_device {
	atomic_t refcnt;
	struct completion comp; /* When refcnt becomes 0, it will wake up */
@@ -75,6 +82,7 @@ struct uburma_device {
	struct cdev cdev;
	struct device *dev;
	struct uburma_port port[UBCORE_MAX_PORT_CNT];
	struct uburma_vf vf[UBCORE_MAX_VF_CNT];
	struct ubcore_device *__rcu ubc_dev;
	struct srcu_struct ubc_dev_srcu; /* protect ubc_dev */
	struct kobject kobj; /* when equal to 0 , free uburma_device. */