From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id C5D24A04A7;
	Tue, 25 Jan 2022 10:42:46 +0100 (CET)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 36FAD426D5;
	Tue, 25 Jan 2022 10:42:45 +0100 (CET)
Received: from mga18.intel.com (mga18.intel.com [134.134.136.126])
 by mails.dpdk.org (Postfix) with ESMTP id 82BEE4285F
 for <dev@dpdk.org>; Tue, 25 Jan 2022 10:42:43 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
 d=intel.com; i=@intel.com; q=dns/txt; s=Intel;
 t=1643103763; x=1674639763;
 h=from:to:cc:subject:date:message-id:in-reply-to: references;
 bh=rFUz/SVjhGgn20xUFjFFemaWcitUduNA2brEvZV/5+I=;
 b=e2dPbmIce7E39q4gzMExlHZwAUGFWldXN0xqA6bv4leMfCcZHbHctIIR
 KPmk+CS3CrLtHa6f0OHEXDzl9hsywnnKRMKi7wCAGnnMYy5JhWepaFwdS
 NlpzWZU3A5sOI7xmug+VBi9xwtez0mYy/ZfrhX9SZ/KGs4q02+5XQdhq2
 vzdrXnAIAD+fDLPnTByaJlTeQtUkSbmuea2E6osU6lz5hFhu3CUCKsrt8
 F+AWT950fvybqIHBnsVaaEku/mWbx/+RMP4GV0YNVFXEzt0jMe0YYnfbj
 8omampOmfkbdYxYH7V8T4iT9u6y1j/0bfQxFzAEvGe2a50KBZngnHhSgF w==;
X-IronPort-AV: E=McAfee;i="6200,9189,10237"; a="229840346"
X-IronPort-AV: E=Sophos;i="5.88,314,1635231600"; d="scan'208";a="229840346"
Received: from orsmga008.jf.intel.com ([10.7.209.65])
 by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 25 Jan 2022 01:42:43 -0800
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.88,314,1635231600"; d="scan'208";a="534656714"
Received: from dpdk-dipei.sh.intel.com ([10.67.111.91])
 by orsmga008.jf.intel.com with ESMTP; 25 Jan 2022 01:42:41 -0800
From: Andy Pei <andy.pei@intel.com>
To: dev@dpdk.org
Cc: chenbo.xia@intel.com, maxime.coquelin@redhat.com, gang.cao@intel.com,
 changpeng.liu@intel.com
Subject: [PATCH v2 01/15] vdpa/ifc: add support for virtio blk device
Date: Tue, 25 Jan 2022 17:37:03 +0800
Message-Id: <1643103437-118618-2-git-send-email-andy.pei@intel.com>
X-Mailer: git-send-email 1.8.3.1
In-Reply-To: <1643103437-118618-1-git-send-email-andy.pei@intel.com>
References: <1643093258-47258-2-git-send-email-andy.pei@intel.com>
 <1643103437-118618-1-git-send-email-andy.pei@intel.com>
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

Re-use the vdpa/ifc code, distinguish blk and net device by pci_device_id.
Blk and net device are implemented with proper feature and ops.

Signed-off-by: Andy Pei <andy.pei@intel.com>
---
 drivers/vdpa/ifc/base/ifcvf.h | 16 +++++++-
 drivers/vdpa/ifc/ifcvf_vdpa.c | 92 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 98 insertions(+), 10 deletions(-)

diff --git a/drivers/vdpa/ifc/base/ifcvf.h b/drivers/vdpa/ifc/base/ifcvf.h
index 573a35f..01522c6 100644
--- a/drivers/vdpa/ifc/base/ifcvf.h
+++ b/drivers/vdpa/ifc/base/ifcvf.h
@@ -5,8 +5,17 @@
 #ifndef _IFCVF_H_
 #define _IFCVF_H_
 
+#include <linux/virtio_blk.h>
 #include "ifcvf_osdep.h"
 
+#define IFCVF_NET	0
+#define IFCVF_BLK	1
+
+/* for BLK */
+#define IFCVF_BLK_TRANSITIONAL_DEVICE_ID    0x1001
+#define IFCVF_BLK_MODERN_DEVICE_ID          0x1042
+#define IFCVF_BLK_DEVICE_ID                 0x0002
+
 #define IFCVF_VENDOR_ID		0x1AF4
 #define IFCVF_DEVICE_ID		0x1041
 #define IFCVF_SUBSYS_VENDOR_ID	0x8086
@@ -57,7 +66,6 @@
 
 #define IFCVF_32_BIT_MASK		0xffffffff
 
-
 struct ifcvf_pci_cap {
 	u8 cap_vndr;            /* Generic PCI field: PCI_CAP_ID_VNDR */
 	u8 cap_next;            /* Generic PCI field: next ptr. */
@@ -126,7 +134,11 @@ struct ifcvf_hw {
 	u8     notify_region;
 	u32    notify_off_multiplier;
 	struct ifcvf_pci_common_cfg *common_cfg;
-	struct ifcvf_net_config *dev_cfg;
+	union {
+		struct ifcvf_net_config *net_cfg;
+		struct virtio_blk_config *blk_cfg;
+		void *dev_cfg;
+	};
 	u8     *isr;
 	u16    *notify_base;
 	u16    *notify_addr[IFCVF_MAX_QUEUES * 2];
diff --git a/drivers/vdpa/ifc/ifcvf_vdpa.c b/drivers/vdpa/ifc/ifcvf_vdpa.c
index 3853c4c..96b67dd 100644
--- a/drivers/vdpa/ifc/ifcvf_vdpa.c
+++ b/drivers/vdpa/ifc/ifcvf_vdpa.c
@@ -58,6 +58,7 @@ struct ifcvf_internal {
 	struct rte_vdpa_device *vdev;
 	uint16_t max_queues;
 	uint64_t features;
+	int device_type;
 	rte_atomic32_t started;
 	rte_atomic32_t dev_attached;
 	rte_atomic32_t running;
@@ -75,6 +76,12 @@ struct internal_list {
 	struct ifcvf_internal *internal;
 };
 
+/* vdpa device info includes device features and devcic operation. */
+struct rte_vdpa_dev_info {
+	uint64_t features;
+	struct rte_vdpa_dev_ops *ops;
+};
+
 TAILQ_HEAD(internal_list_head, internal_list);
 static struct internal_list_head internal_list =
 	TAILQ_HEAD_INITIALIZER(internal_list);
@@ -1170,6 +1177,48 @@ struct internal_list {
 	return 0;
 }
 
+static int16_t
+ifcvf_pci_get_device_type(struct rte_pci_device *pci_dev)
+{
+	uint16_t pci_device_id = pci_dev->id.device_id;
+	uint16_t device_id;
+
+	if (pci_device_id < 0x1000 || pci_device_id > 0x107f) {
+		DRV_LOG(ERR, "Probe device is not a virtio device\n");
+		return -1;
+	}
+
+	if (pci_device_id < 0x1040) {
+		/* Transitional devices: use the PCI subsystem device id as
+		 * virtio device id, same as legacy driver always did.
+		 */
+		device_id = pci_dev->id.subsystem_device_id;
+	} else {
+		/* Modern devices: simply use PCI device id,
+		 * but start from 0x1040.
+		 */
+		device_id = pci_device_id - 0x1040;
+	}
+
+	return device_id;
+}
+
+struct rte_vdpa_dev_info dev_info[] = {
+	{
+		.features = (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
+			    (1ULL << VIRTIO_NET_F_CTRL_VQ) |
+			    (1ULL << VIRTIO_NET_F_STATUS) |
+			    (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
+			    (1ULL << VHOST_F_LOG_ALL),
+		.ops = &ifcvf_ops,
+	},
+	{
+		.features = (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
+			    (1ULL << VHOST_F_LOG_ALL),
+		.ops = NULL,
+	},
+};
+
 static int
 ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
 		struct rte_pci_device *pci_dev)
@@ -1181,6 +1230,7 @@ struct internal_list {
 	int sw_fallback_lm = 0;
 	struct rte_kvargs *kvlist = NULL;
 	int ret = 0;
+	int16_t device_id;
 
 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
 		return 0;
@@ -1230,13 +1280,24 @@ struct internal_list {
 	internal->configured = 0;
 	internal->max_queues = IFCVF_MAX_QUEUES;
 	features = ifcvf_get_features(&internal->hw);
-	internal->features = (features &
-		~(1ULL << VIRTIO_F_IOMMU_PLATFORM)) |
-		(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
-		(1ULL << VIRTIO_NET_F_CTRL_VQ) |
-		(1ULL << VIRTIO_NET_F_STATUS) |
-		(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
-		(1ULL << VHOST_F_LOG_ALL);
+
+	device_id = ifcvf_pci_get_device_type(pci_dev);
+	if (device_id < 0) {
+		DRV_LOG(ERR, "failed to get device %s type", pci_dev->name);
+		goto error;
+	}
+
+	if (device_id == VIRTIO_ID_NET) {
+		internal->device_type = IFCVF_NET;
+		internal->features = features &
+					~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
+		internal->features |= dev_info[IFCVF_NET].features;
+	} else if (device_id == VIRTIO_ID_BLOCK) {
+		internal->device_type = IFCVF_BLK;
+		internal->features = features &
+					~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
+		internal->features |= dev_info[IFCVF_BLK].features;
+	}
 
 	list->internal = internal;
 
@@ -1248,7 +1309,8 @@ struct internal_list {
 	}
 	internal->sw_lm = sw_fallback_lm;
 
-	internal->vdev = rte_vdpa_register_device(&pci_dev->device, &ifcvf_ops);
+	internal->vdev = rte_vdpa_register_device(&pci_dev->device,
+				dev_info[internal->device_type].ops);
 	if (internal->vdev == NULL) {
 		DRV_LOG(ERR, "failed to register device %s", pci_dev->name);
 		goto error;
@@ -1316,6 +1378,20 @@ struct internal_list {
 	  .subsystem_device_id = IFCVF_SUBSYS_DEVICE_ID,
 	},
 
+	{ .class_id = RTE_CLASS_ANY_ID,
+	  .vendor_id = IFCVF_VENDOR_ID,
+	  .device_id = IFCVF_BLK_TRANSITIONAL_DEVICE_ID,
+	  .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
+	  .subsystem_device_id = IFCVF_BLK_DEVICE_ID,
+	},
+
+	{ .class_id = RTE_CLASS_ANY_ID,
+	  .vendor_id = IFCVF_VENDOR_ID,
+	  .device_id = IFCVF_BLK_MODERN_DEVICE_ID,
+	  .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
+	  .subsystem_device_id = IFCVF_BLK_DEVICE_ID,
+	},
+
 	{ .vendor_id = 0, /* sentinel */
 	},
 };
-- 
1.8.3.1