Re: [PATCH 2/2] IFC VDPA layer

From: Zhu Lingshan
Date: Tue Nov 12 2019 - 03:54:13 EST



On 11/8/2019 8:46 PM, Zhu Lingshan wrote:

On 11/6/2019 6:39 PM, Jason Wang wrote:

----- Original Message -----
This commit introduced IFC operations for vdpa, which complys to
virtio_mdev and vhost_mdev interfaces, handles IFC VF
initialization, configuration and removal.

Signed-off-by: Zhu Lingshan <lingshan.zhu@xxxxxxxxx>
---
 drivers/vhost/ifcvf/ifcvf_main.c | 605
 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 605 insertions(+)
 create mode 100644 drivers/vhost/ifcvf/ifcvf_main.c

diff --git a/drivers/vhost/ifcvf/ifcvf_main.c
b/drivers/vhost/ifcvf/ifcvf_main.c
new file mode 100644
index 0000000..7165457
--- /dev/null
+++ b/drivers/vhost/ifcvf/ifcvf_main.c
@@ -0,0 +1,605 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2019 Intel Corporation.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/mdev.h>
+#include <linux/pci.h>
+#include <linux/sysfs.h>
+#include "ifcvf_base.h"
+
+#define VERSION_STRINGÂÂÂ "0.1"
+#define DRIVER_AUTHORÂÂÂ "Intel Corporation"
+#define IFCVF_DRIVER_NAMEÂÂÂ "ifcvf"
+
+static struct ifcvf_hw *mdev_to_vf(struct mdev_device *mdev)
+{
+ÂÂÂ struct ifcvf_asapter *adapter = mdev_get_drvdata(mdev);
+ÂÂÂ struct ifcvf_hw *vf = IFC_PRIVATE_TO_VF(adapter);
+
+ÂÂÂ return vf;
+}
+
+static irqreturn_t ifcvf_intr_handler(int irq, void *arg)
+{
+ÂÂÂ struct vring_info *vring = arg;
+
+ÂÂÂ if (vring->cb.callback)
+ÂÂÂÂÂÂÂ return vring->cb.callback(vring->cb.private);
+
+ÂÂÂ return IRQ_HANDLED;
+}
+
+static u64 ifcvf_mdev_get_features(struct mdev_device *mdev)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ return ifcvf_get_features(vf);
+}
+
+static int ifcvf_mdev_set_features(struct mdev_device *mdev, u64 features)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ vf->req_features = features;
+
+ÂÂÂ return 0;
+}
+
+static u64 ifcvf_mdev_get_vq_state(struct mdev_device *mdev, u16 qid)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+ÂÂÂ u16 last_avail_idx;
+
+ÂÂÂ last_avail_idx = *(u16 *)(vf->lm_cfg + IFCVF_LM_RING_STATE_OFFSET +
+ÂÂÂÂÂÂÂÂÂÂÂÂ (qid / 2) * IFCVF_LM_CFG_SIZE + (qid % 2) * 4);
+
Similar to the comment of previous patch, it's better to have a
structure for lm_cfg.

Hello Jason,

Thanks for your comments!

Now I use an variable for the address, and iowrite() for portable purpose.

+ÂÂÂ return last_avail_idx;
+}
+
+static int ifcvf_mdev_set_vq_state(struct mdev_device *mdev, u16 qid, u64
num)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ vf->vring[qid].last_avail_idx = num;
+
+ÂÂÂ return 0;
+}
+
+static int ifcvf_mdev_set_vq_address(struct mdev_device *mdev, u16 idx,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u64 desc_area, u64 driver_area,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u64 device_area)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ vf->vring[idx].desc = desc_area;
+ÂÂÂ vf->vring[idx].avail = driver_area;
+ÂÂÂ vf->vring[idx].used = device_area;
+
+ÂÂÂ return 0;
+}
+
+static void ifcvf_mdev_set_vq_num(struct mdev_device *mdev, u16 qid, u32
num)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ vf->vring[qid].size = num;
+}
+
+static void ifcvf_mdev_set_vq_ready(struct mdev_device *mdev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u16 qid, bool ready)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ vf->vring[qid].ready = ready;
There should be a "iowrite16(1, &cfg->queue_enable)" here. And there's
probably no need to store ready in vring in this case.
Yes

+}
+
+static bool ifcvf_mdev_get_vq_ready(struct mdev_device *mdev, u16 qid)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ return vf->vring[qid].ready;
And the status should be read from cfg->queue_enable.
Yes

+}
+
+static void ifcvf_mdev_set_vq_cb(struct mdev_device *mdev, u16 idx,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct virtio_mdev_callback *cb)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ vf->vring[idx].cb = *cb;
+}
+
+static void ifcvf_mdev_kick_vq(struct mdev_device *mdev, u16 idx)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ ifcvf_notify_queue(vf, idx);
+}
+
+static u8 ifcvf_mdev_get_status(struct mdev_device *mdev)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ return ifcvf_get_status(vf);
+}
+
+static u32 ifcvf_mdev_get_generation(struct mdev_device *mdev)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ return ioread8(&vf->common_cfg->config_generation);
+}
+
+static u32 ifcvf_mdev_get_device_id(struct mdev_device *mdev)
+{
+ÂÂÂ return VIRTIO_ID_NET;
+}
+
+static u32 ifcvf_mdev_get_vendor_id(struct mdev_device *mdev)
+{
+ÂÂÂ return IFCVF_VENDOR_ID;
+}
+
+static u16 ifcvf_mdev_get_vq_align(struct mdev_device *mdev)
+{
+ÂÂÂ return IFCVF_QUEUE_ALIGNMENT;
+}
+
+static u64 ifcvf_mdev_get_mdev_features(struct mdev_device *mdev)
+{
+ÂÂÂ return VIRTIO_MDEV_F_VERSION_1;
+}
We've decide to remove this API.
Removed.

+
+static int ifcvf_start_datapath(void *private)
+{
+ÂÂÂ struct ifcvf_hw *vf = IFC_PRIVATE_TO_VF(private);
+ÂÂÂ struct ifcvf_adapter *ifcvf;
+ÂÂÂ int i, ret = 0;
+
+ÂÂÂ ifcvf = container_of(vf, struct ifcvf_adapter, vf);
+
+ÂÂÂ for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) {
+ÂÂÂÂÂÂÂ if (!vf->vring[i].ready) {
+ÂÂÂÂÂÂÂÂÂÂÂ IFC_ERR(ifcvf->dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "Failed to start datapath, vring %d not ready.\n", i);
+ÂÂÂÂÂÂÂÂÂÂÂ return -EINVAL;
+ÂÂÂÂÂÂÂ }
This should be not related. Driver can choose to not start a virtqueue.
removed the codes.

+
+ÂÂÂÂÂÂÂ if (!vf->vring[i].size) {
+ÂÂÂÂÂÂÂÂÂÂÂ IFC_ERR(ifcvf->dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "Failed to start datapath, vring %d size is zero.\n", i);
+ÂÂÂÂÂÂÂÂÂÂÂ return -EINVAL;
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ if (!vf->vring[i].desc || !vf->vring[i].avail ||
+ÂÂÂÂÂÂÂÂÂÂÂ !vf->vring[i].used) {
+ÂÂÂÂÂÂÂÂÂÂÂ IFC_ERR(ifcvf->dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "Failed to start datapath, "
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "invaild value for vring %d desc,"
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "avail_idx or usex_idx.\n", i);
+ÂÂÂÂÂÂÂÂÂÂÂ return -EINVAL;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ vf->nr_vring = i;
+ÂÂÂ ret = ifcvf_start_hw(vf);
So basically there's no need for ifcvf_start_hw() to care about vq
enablement, virtio core will take care of that through set_vq_ready().
Agreed, however if we don't enable the queue, I observe the hardware will not allow access to the queue, even reading something.

Hello Jason,

IMHO, I think maybe we can keep using vring.ready. If upper layer want to enable the queue, it can call set_vq_ready to mark it as "ready", then ifcvf_hw_enable() will enable all queues marked as "ready"(I added some code there), this can give control to upper layer as well.

THanks!



+
+ÂÂÂ return ret;
+}
+
+static int ifcvf_stop_datapath(void *private)
+{
+ÂÂÂ struct ifcvf_hw *vf = IFC_PRIVATE_TO_VF(private);
+ÂÂÂ int i;
+
+ÂÂÂ for (i = 0; i < IFCVF_MAX_QUEUES; i++)
+ÂÂÂÂÂÂÂ vf->vring[i].cb.callback = NULL;
+
+ÂÂÂ ifcvf_stop_hw(vf);
+
+ÂÂÂ return 0;
+}
+
+static void ifcvf_reset_vring(struct ifcvf_adapter *adapter)
+{
+ÂÂÂ struct ifcvf_hw *vf = IFC_PRIVATE_TO_VF(adapter);
+ÂÂÂ struct virtio_pci_common_cfg *cfg;
+ÂÂÂ u8 *lm_cfg;
+ÂÂÂ int i;
+
+ÂÂÂ cfg = vf->common_cfg;
+ÂÂÂ lm_cfg = vf->lm_cfg;
+
+ÂÂÂ for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) {
+ÂÂÂÂÂÂÂ vf->vring[i].last_used_idx = 0;
+ÂÂÂÂÂÂÂ vf->vring[i].last_avail_idx = 0;
+ÂÂÂÂÂÂÂ vf->vring[i].desc = 0;
+ÂÂÂÂÂÂÂ vf->vring[i].avail = 0;
+ÂÂÂÂÂÂÂ vf->vring[i].used = 0;
+ÂÂÂÂÂÂÂ vf->vring[i].ready = 0;
+ÂÂÂÂÂÂÂ vf->vring->cb.callback = NULL;
+ÂÂÂÂÂÂÂ vf->vring->cb.private = NULL;
+
+ÂÂÂ }
+
+ÂÂÂ ifcvf_reset(vf);
So virtio-pci call vp_synchornize_vectors(), do need someting similar
here (I mean in ifcvf_reset())?
Hardware handles most of the reset stuff, when reset the VF, we did not free irqs, the handler still working, so the on-flight irqs can be handled.

+}
+
+static void ifcvf_mdev_set_status(struct mdev_device *mdev, u8 status)
+{
+ÂÂÂ struct ifcvf_adapter *adapter = mdev_get_drvdata(mdev);
+ÂÂÂ struct ifcvf_hw *vf = IFC_PRIVATE_TO_VF(adapter);
+ÂÂÂ int ret = 0;
+
+ÂÂÂ if (status == 0) {
+ÂÂÂÂÂÂÂ ifcvf_stop_datapath(adapter);
+ÂÂÂÂÂÂÂ ifcvf_reset_vring(adapter);
+ÂÂÂÂÂÂÂ return;
+ÂÂÂ }
+
+ÂÂÂ if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
+ÂÂÂÂÂÂÂ ret = ifcvf_start_datapath(adapter);
If device support VIRTIO_CONFIG_S_DRIVER_OK, having something like
start_datapath here looks wired.

If it just to setup the virtqueue etc, can we simply move them to e.g
set_vq_num, set_vq_address, etc?
IMHO, it does not just setup vqs, it also do some config and enabling works for the whole VF.

+
+ÂÂÂÂÂÂÂ if (ret)
+ÂÂÂÂÂÂÂÂÂÂÂ IFC_ERR(adapter->dev, "Failed to set mdev status %u.\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ status);
+ÂÂÂ }
+
+ÂÂÂ ifcvf_set_status(vf, status);
+}
+
+static u16 ifcvf_mdev_get_vq_num_max(struct mdev_device *mdev)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ return vf->vring[0].size;
It looks to me the only case that size is set is from
ifcvf_mdev_set_vq_num()? So I don't get how is this supposed to
work. I belive this should be a query for the hardware or a at least a
macro?
Fixed.

+}
+static void ifcvf_mdev_get_config(struct mdev_device *mdev, unsigned int
offset,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ void *buf, unsigned int len)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ WARN_ON(offset + len > sizeof(struct ifcvf_net_config));
+ÂÂÂ ifcvf_read_net_config(vf, offset, buf, len);
+}
+
+static void ifcvf_mdev_set_config(struct mdev_device *mdev, unsigned int
offset,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const void *buf, unsigned int len)
+{
+ÂÂÂ struct ifcvf_hw *vf = mdev_to_vf(mdev);
+
+ÂÂÂ WARN_ON(offset + len > sizeof(struct ifcvf_net_config));
+ÂÂÂ ifcvf_write_net_config(vf, offset, buf, len);
+}
+
+static struct virtio_mdev_device_ops ifc_mdev_ops = {
+ .get_features = ifcvf_mdev_get_features,
+ .set_features = ifcvf_mdev_set_features,
+ÂÂÂ .get_statusÂÂÂ = ifcvf_mdev_get_status,
+ÂÂÂ .set_statusÂÂÂ = ifcvf_mdev_set_status,
+ÂÂÂ .get_vq_num_max = ifcvf_mdev_get_vq_num_max,
+ÂÂÂ .get_vq_stateÂÂ = ifcvf_mdev_get_vq_state,
+ÂÂÂ .set_vq_stateÂÂ = ifcvf_mdev_set_vq_state,
+ÂÂÂ .set_vq_cbÂÂÂÂÂ = ifcvf_mdev_set_vq_cb,
+ÂÂÂ .set_vq_readyÂÂ = ifcvf_mdev_set_vq_ready,
+ÂÂÂ .get_vq_readyÂÂÂ = ifcvf_mdev_get_vq_ready,
+ÂÂÂ .set_vq_numÂÂÂÂ = ifcvf_mdev_set_vq_num,
+ÂÂÂ .set_vq_address = ifcvf_mdev_set_vq_address,
+ÂÂÂ .kick_vqÂÂÂÂÂÂÂ = ifcvf_mdev_kick_vq,
+ÂÂÂ .get_generationÂÂÂ = ifcvf_mdev_get_generation,
+ÂÂÂ .get_device_idÂÂÂ = ifcvf_mdev_get_device_id,
+ÂÂÂ .get_vendor_idÂÂÂ = ifcvf_mdev_get_vendor_id,
+ÂÂÂ .get_vq_alignÂÂÂ = ifcvf_mdev_get_vq_align,
+ÂÂÂ .get_configÂÂÂ = ifcvf_mdev_get_config,
+ÂÂÂ .set_configÂÂÂ = ifcvf_mdev_set_config,
+ÂÂÂ .get_mdev_features = ifcvf_mdev_get_mdev_features,
set_config_cb needs to be implemented since you claim to support VIRTIO_NET_F_STATUS.
control_vq feature bit is removed.

+};
+
+static int ifcvf_init_msix(struct ifcvf_adapter *adapter)
+{
+ÂÂÂ struct pci_dev *pdev = to_pci_dev(adapter->dev);
+ÂÂÂ struct ifcvf_hw *vf = &adapter->vf;
+ÂÂÂ int vector, i, ret, irq;
+
+ÂÂÂ ret = pci_alloc_irq_vectors(pdev, IFCVF_MAX_INTR,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ IFCVF_MAX_INTR, PCI_IRQ_MSIX);
+ÂÂÂ if (ret < 0) {
+ÂÂÂÂÂÂÂ IFC_ERR(adapter->dev, "Failed to alloc irq vectors.\n");
+ÂÂÂÂÂÂÂ return ret;
+ÂÂÂ }
+
+ÂÂÂ for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) {
+ÂÂÂÂÂÂÂ vector = i + IFCVF_MSI_QUEUE_OFF;
+ÂÂÂÂÂÂÂ irq = pci_irq_vector(pdev, vector);
+ÂÂÂÂÂÂÂ ret = request_irq(irq, ifcvf_intr_handler, 0,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ pci_name(pdev), &vf->vring[i]);
+ÂÂÂÂÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂÂÂÂÂ IFC_ERR(adapter->dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "Failed to request irq for vq %d.\n", i);
+ÂÂÂÂÂÂÂÂÂÂÂ return ret;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
Need allocate config interrupt here as well.

+
+ÂÂÂ return 0;
+}
+
+static void ifcvf_destroy_adapter(struct ifcvf_adapter *adapter)
+{
+ÂÂÂ struct ifcvf_hw *vf = IFC_PRIVATE_TO_VF(adapter);
+ÂÂÂ struct pci_dev *pdev = to_pci_dev(adapter->dev);
+ÂÂÂ int i, vector, irq;
+
+ÂÂÂ for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) {
+ÂÂÂÂÂÂÂ vector = i + IFCVF_MSI_QUEUE_OFF;
+ÂÂÂÂÂÂÂ irq = pci_irq_vector(pdev, vector);
+ÂÂÂÂÂÂÂ free_irq(irq, &vf->vring[i]);
+ÂÂÂ }
+}
+
+static ssize_t name_show(struct kobject *kobj, struct device *dev, char
*buf)
+{
+ÂÂÂ const char *name = "vhost accelerator (virtio ring compatible)";
+
I believe something like "IFCVF vhost/virtio accelerator" is better?
Agreed.

+ÂÂÂ return sprintf(buf, "%s\n", name);
+}
+MDEV_TYPE_ATTR_RO(name);
+
+static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ char *buf)
+{
+ÂÂÂ return sprintf(buf, "%s\n", VIRTIO_MDEV_DEVICE_API_STRING);
+}
+MDEV_TYPE_ATTR_RO(device_api);
+
+static ssize_t available_instances_show(struct kobject *kobj,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct device *dev, char *buf)
+{
+ÂÂÂ struct pci_dev *pdev;
+ÂÂÂ struct ifcvf_adapter *adapter;
+
+ÂÂÂ pdev = to_pci_dev(dev);
+ÂÂÂ adapter = pci_get_drvdata(pdev);
+
+ÂÂÂ return sprintf(buf, "%d\n", adapter->mdev_count);
+}
+
+MDEV_TYPE_ATTR_RO(available_instances);
+
+static ssize_t type_show(struct kobject *kobj,
+ÂÂÂÂÂÂÂÂÂÂÂ struct device *dev, char *buf)
+{
+ÂÂÂ return sprintf(buf, "%s\n", "net");
+}
+
+MDEV_TYPE_ATTR_RO(type);
+
+
+static struct attribute *mdev_types_attrs[] = {
+ÂÂÂ &mdev_type_attr_name.attr,
+ÂÂÂ &mdev_type_attr_device_api.attr,
+ÂÂÂ &mdev_type_attr_available_instances.attr,
+ÂÂÂ &mdev_type_attr_type.attr,
+ÂÂÂ NULL,
+};
+
+static struct attribute_group mdev_type_group_virtio = {
+ .name = "virtio_mdev",
+ÂÂÂ .attrs = mdev_types_attrs,
+};
+
+static struct attribute_group mdev_type_group_vhost = {
+ .name = "vhost_mdev",
+ÂÂÂ .attrs = mdev_types_attrs,
+};
+
+static struct attribute_group *mdev_type_groups[] = {
+ÂÂÂ &mdev_type_group_virtio,
+ÂÂÂ &mdev_type_group_vhost,
+ÂÂÂ NULL,
+};
+
+const struct attribute_group *mdev_dev_groups[] = {
+ÂÂÂ NULL,
+};
+
+static int ifcvf_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
+{
+ÂÂÂ struct device *dev = mdev_parent_dev(mdev);
+ÂÂÂ struct pci_dev *pdev = to_pci_dev(dev);
+ÂÂÂ struct ifcvf_adapter *adapter = pci_get_drvdata(pdev);
+ÂÂÂ int ret = 0;
+
+ÂÂÂ mutex_lock(&adapter->mdev_lock);
+
+ÂÂÂ if (adapter->mdev_count < IFCVF_MDEV_LIMIT) {
+ÂÂÂÂÂÂÂ IFC_ERR(&pdev->dev,
+ÂÂÂÂÂÂÂÂÂÂÂ "Can not create mdev, reached limitation %d.\n",
+ÂÂÂÂÂÂÂÂÂÂÂ IFCVF_MDEV_LIMIT);
+ÂÂÂÂÂÂÂ ret = -EINVAL;
+ÂÂÂÂÂÂÂ goto out;
+ÂÂÂ }
+
+ÂÂÂ if (!strcmp(kobj->name, "ifcvf-virtio_mdev"))
+ÂÂÂÂÂÂÂ mdev_set_virtio_ops(mdev, &ifc_mdev_ops);
+
+ÂÂÂ if (!strcmp(kobj->name, "ifcvf-vhost_mdev"))
+ÂÂÂÂÂÂÂ mdev_set_vhost_ops(mdev, &ifc_mdev_ops);
+
+ÂÂÂ mdev_set_drvdata(mdev, adapter);
+ÂÂÂ mdev_set_iommu_device(mdev_dev(mdev), dev);
+ÂÂÂ adapter->mdev_count--;
+
+out:
+ÂÂÂ mutex_unlock(&adapter->mdev_lock);
+ÂÂÂ return ret;
+}
+
+static int ifcvf_mdev_remove(struct mdev_device *mdev)
+{
+ÂÂÂ struct device *dev = mdev_parent_dev(mdev);
+ÂÂÂ struct pci_dev *pdev = to_pci_dev(dev);
+ÂÂÂ struct ifcvf_adapter *adapter = pci_get_drvdata(pdev);
+
+ÂÂÂ mutex_lock(&adapter->mdev_lock);
+ÂÂÂ adapter->mdev_count++;
+ÂÂÂ mutex_unlock(&adapter->mdev_lock);
+
+ÂÂÂ return 0;
+}
+
+static struct mdev_parent_ops ifcvf_mdev_fops = {
+ÂÂÂ .ownerÂÂÂÂÂÂÂÂÂÂÂ = THIS_MODULE,
+ÂÂÂ .supported_type_groupsÂÂÂ = mdev_type_groups,
+ÂÂÂ .mdev_attr_groupsÂÂÂ = mdev_dev_groups,
+ÂÂÂ .createÂÂÂÂÂÂÂÂÂÂÂ = ifcvf_mdev_create,
+ÂÂÂ .removeÂÂÂÂÂÂÂÂÂÂÂ = ifcvf_mdev_remove,
+};
+
+static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ÂÂÂ struct device *dev = &pdev->dev;
+ÂÂÂ struct ifcvf_adapter *adapter;
+ÂÂÂ struct ifcvf_hw *vf;
+ÂÂÂ int ret, i;
+
+ÂÂÂ adapter = kzalloc(sizeof(struct ifcvf_adapter), GFP_KERNEL);
+
+ÂÂÂ if (adapter == NULL) {
+ÂÂÂÂÂÂÂ ret = -ENOMEM;
+ÂÂÂÂÂÂÂ goto fail;
+ÂÂÂ }
+
+ÂÂÂ mutex_init(&adapter->mdev_lock);
+ÂÂÂ adapter->mdev_count = IFCVF_MDEV_LIMIT;
+ÂÂÂ adapter->dev = dev;
+ÂÂÂ pci_set_drvdata(pdev, adapter);
+ÂÂÂ ret = pci_enable_device(pdev);
+
+ÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂ IFC_ERR(adapter->dev, "Failed to enable device.\n");
+ÂÂÂÂÂÂÂ goto free_adapter;
+ÂÂÂ }
+
+ÂÂÂ ret = pci_request_regions(pdev, IFCVF_DRIVER_NAME);
+
+ÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂ IFC_ERR(adapter->dev, "Failed to request MMIO region.\n");
+ÂÂÂÂÂÂÂ goto disable_device;
+ÂÂÂ }
+
+ÂÂÂ pci_set_master(pdev);
+ÂÂÂ ret = ifcvf_init_msix(adapter);
+
+ÂÂÂ if (ret) {
+ÂÂÂÂÂÂÂ IFC_ERR(adapter->dev, "Failed to initialize MSIX.\n");
+ÂÂÂÂÂÂÂ goto free_msix;
+ÂÂÂ }
+
+ÂÂÂ vf = &adapter->vf;
+
+ÂÂÂ for (i = 0; i < IFCVF_PCI_MAX_RESOURCE; i++) {
+ÂÂÂÂÂÂÂ vf->mem_resource[i].phys_addr = pci_resource_start(pdev, i);
+ÂÂÂÂÂÂÂ vf->mem_resource[i].len = pci_resource_len(pdev, i);
+ÂÂÂÂÂÂÂ if (!vf->mem_resource[i].len) {
+ÂÂÂÂÂÂÂÂÂÂÂ vf->mem_resource[i].addr = NULL;
+ÂÂÂÂÂÂÂÂÂÂÂ continue;
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ vf->mem_resource[i].addr = pci_iomap_range(pdev, i, 0,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ vf->mem_resource[i].len);
+ÂÂÂÂÂÂÂ if (!vf->mem_resource[i].addr) {
+ÂÂÂÂÂÂÂÂÂÂÂ IFC_ERR(adapter->dev, "Failed to map IO resource %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ i);
+ÂÂÂÂÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂÂÂÂÂ goto free_msix;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ if (ifcvf_init_hw(vf, pdev) < 0) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ goto destroy_adapter;
+ÂÂÂ }
+
+ÂÂÂ ret = mdev_register_device(dev, &ifcvf_mdev_fops);
+
+ÂÂÂ if (ret) {
+ IFC_ERR(adapter->dev, "Failed to register mdev device\n");
+ÂÂÂÂÂÂÂ goto destroy_adapter;
+ÂÂÂ }
+
+ÂÂÂ return 0;
+
+destroy_adapter:
+ÂÂÂ ifcvf_destroy_adapter(adapter);
+free_msix:
+ÂÂÂ pci_free_irq_vectors(pdev);
+ÂÂÂ pci_release_regions(pdev);
+disable_device:
+ÂÂÂ pci_disable_device(pdev);
+free_adapter:
+ÂÂÂ kfree(adapter);
+fail:
+ÂÂÂ return ret;
+}
+
+static void ifcvf_remove(struct pci_dev *pdev)
+{
+ÂÂÂ struct ifcvf_adapter *adapter = pci_get_drvdata(pdev);
+ÂÂÂ struct device *dev = &pdev->dev;
+ÂÂÂ struct ifcvf_hw *vf;
+ÂÂÂ int i;
+
+ÂÂÂ mdev_unregister_device(dev);
+
+ÂÂÂ vf = &adapter->vf;
+ÂÂÂ for (i = 0; i < IFCVF_PCI_MAX_RESOURCE; i++) {
+ÂÂÂÂÂÂÂ if (vf->mem_resource[i].addr) {
+ÂÂÂÂÂÂÂÂÂÂÂ pci_iounmap(pdev, vf->mem_resource[i].addr);
+ÂÂÂÂÂÂÂÂÂÂÂ vf->mem_resource[i].addr = NULL;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ ifcvf_destroy_adapter(adapter);
+ÂÂÂ pci_free_irq_vectors(pdev);
+ÂÂÂ pci_release_regions(pdev);
+ÂÂÂ pci_disable_device(pdev);
+ÂÂÂ kfree(adapter);
+}
+
+static struct pci_device_id ifcvf_pci_ids[] = {
+ÂÂÂ { PCI_DEVICE_SUB(IFCVF_VENDOR_ID,
+ÂÂÂÂÂÂÂÂÂÂÂ IFCVF_DEVICE_ID,
+ÂÂÂÂÂÂÂÂÂÂÂ IFCVF_SUBSYS_VENDOR_ID,
+ÂÂÂÂÂÂÂÂÂÂÂ IFCVF_SUBSYS_DEVICE_ID) },
+ÂÂÂ { 0 },
+};
+MODULE_DEVICE_TABLE(pci, ifcvf_pci_ids);
+
+static struct pci_driver ifcvf_driver = {
+ÂÂÂ .nameÂÂÂÂ = IFCVF_DRIVER_NAME,
+ÂÂÂ .id_table = ifcvf_pci_ids,
+ÂÂÂ .probeÂÂÂ = ifcvf_probe,
+ÂÂÂ .removeÂÂ = ifcvf_remove,
+};
+
+static int __init ifcvf_init_module(void)
+{
+ÂÂÂ int ret;
+
+ÂÂÂ ret = pci_register_driver(&ifcvf_driver);
+ÂÂÂ return ret;
+}
+
+static void __exit ifcvf_exit_module(void)
+{
+ÂÂÂ pci_unregister_driver(&ifcvf_driver);
+}
You probably can do something simpler thorugh module_pci_driver().

Thanks

+
+module_init(ifcvf_init_module);
+module_exit(ifcvf_exit_module);
+
+MODULE_LICENSE("GPL v2");
+MODULE_VERSION(VERSION_STRING);
+MODULE_AUTHOR(DRIVER_AUTHOR);
--
1.8.3.1