From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 8B6BC42BF1; Mon, 5 Jun 2023 15:27:26 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EA0E6410EE; Mon, 5 Jun 2023 15:27:24 +0200 (CEST) Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by mails.dpdk.org (Postfix) with ESMTP id BED5940E2D for ; Mon, 5 Jun 2023 15:27:23 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eVqIgkCml2zCJkIjuk7GTmRn8XCimZldy2q3NvQgp1BF4uKXmdYwwy0X6gxwP201G2b4dzlRouPlYhnMXCqjMK9v18qQIfldA5V41/6tzAdi07I0mVp2BYrDDIXwZWPz0YeBjtdWQ6GYj6a/DMb2Y4kJC08sqeq03r+W3TBXRhdT/J80xGUQr+Hw6rIDMksRxWJAYZV15wT54QPxRdJDZxg7KIJb5KvjR/InsLs5hoGIID0UxvPmHJK3AK6yxsoFWiIxObww/mEBBGkskNJOkz9Tz/aBrvErt3od3YtBIlGcBubyUapLi9+WWMtIRcOnSTz9VxCjRxZJLeM0yIIqWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DcscENsSYUBIB3HgxMonliaoz/7ghGNRClVCIIOEew8=; b=G580qPmu7nCdCcoWriezMh0p6oKsUJv1C4WMqZhWRDqvYQrDC/i8FCL0S++I3ACoiwj4KW7ko9Yu5vpcMd8DsVM7NTcvZGqiog65yOcAEgETajEPaZtn55tDVlI7mNsW+Yo6NA0db1ijmgcBiCmNLy4QnBRhy3CaaUh/Pxlyvnb23nTomiijcgBjrp3/SGVjmIXdgvO2Sv66eVvmLgejOIAfrEewY5IyEW9ZY0Cq/JEgHIBByWC9z86rpV6hCtRAczkIfELPyrBrKPEu/R8m2ObHtr9CQXoHx1MWP3uxtCc+9qxs4WTwiU7LJ07kDIU9/vLw78Ong98GL4ap4VTJUg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=dpdk.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DcscENsSYUBIB3HgxMonliaoz/7ghGNRClVCIIOEew8=; b=WOgkh5Cc3u3sQLFdW5640rkgZ+dqm4unI0KfEoO512w4erPOYg3pZkA02+A+uoQILKit57Ippa4r38G3vaMeCsWAX5e2k6sc8AVKpvXuDZvIlITMyNum1pf7JTNRGaNr6quq3rt/ZFo/OzqwYEsTEsrmxNfFiVg5t/QQTnXw5Jc= Received: from DM6PR11CA0035.namprd11.prod.outlook.com (2603:10b6:5:190::48) by MW4PR12MB7262.namprd12.prod.outlook.com (2603:10b6:303:228::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Mon, 5 Jun 2023 13:27:21 +0000 Received: from DM6NAM11FT024.eop-nam11.prod.protection.outlook.com (2603:10b6:5:190:cafe::f0) by DM6PR11CA0035.outlook.office365.com (2603:10b6:5:190::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33 via Frontend Transport; Mon, 5 Jun 2023 13:27:20 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT024.mail.protection.outlook.com (10.13.172.159) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6455.33 via Frontend Transport; Mon, 5 Jun 2023 13:27:20 +0000 Received: from SATLEXMB03.amd.com (10.181.40.144) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Mon, 5 Jun 2023 08:27:19 -0500 Received: from xhdipdslab41.xilinx.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server id 15.1.2375.34 via Frontend Transport; Mon, 5 Jun 2023 08:27:16 -0500 From: Nipun Gupta To: , , , , , CC: , , , Nipun Gupta Subject: [PATCH v6 3/4] bus/cdx: add support for MSI Date: Mon, 5 Jun 2023 18:56:23 +0530 Message-ID: <20230605132624.8626-4-nipun.gupta@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230605132624.8626-1-nipun.gupta@amd.com> References: <20230124140746.594066-1-nipun.gupta@amd.com> <20230605132624.8626-1-nipun.gupta@amd.com> MIME-Version: 1.0 Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT024:EE_|MW4PR12MB7262:EE_ X-MS-Office365-Filtering-Correlation-Id: 72c7da14-8f15-4de2-a8cd-08db65c89737 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0eBIYYohYwVxXgZTZSxV7cskTosm9RotLcKzXvGl7248zcBuQSdEBfatjFqzL3Y/gOOdsjYLHC4vgl1W9nqz/toPCKwGhIPdfqaTSTAJ2McjT0Ur4nwYl3YAOTIOwxcogaM3CIJo2aMcPRjJlaPwji/cpNnt7FEf1AUp9FFnmIgaC6ecR8GhLheSRPAIUPw4CipbIoC5AoajEe/xnzHI2FbMQ3sl+7XyNPYEnS+n0GhyYomo8q1FUEC2TUADpowbEfOlGoXB9E8LOwB2ODtkrPh9Yq1sewLdHzpQILqdhGcJXq+8ajGzR5JvMAgtsv2OBwlskq6nxM/jsHUmOYbe+EWTYUsxDsfCVOpTMPUIygxMd/b1slYy6kN60dawvrv65w9QhXhwYQRHP7vRqWNYxr2qkYKcfTep0bipJHSRSEQwSiylqRiUoRzMKIcKpAKrMYEIii7aAoU98PjhiDP0BTgAr/frKlip3MIKYai+KvUiltlOVWK1LnVgK2DoDGf0W5F0ga3y4iqsnfDqOFc2zLxUkYyysyOiwT+9+8Q5n+JC9KkBEPGog3AzPMb/kJXc50b5Q0UcsQbIBhNM3zVkeQ4Bj0BLmJDItQr+bKafCUmROt9q3DG0+xb0/XBQ+g11ZzTLa9oU4oU9NqKAwIUc6N6aH6qoINFmzuRZ2cvax8i75Dx43gjgJR4SLqGsca/lh7iBgQ6zSdPq5+3BM9n6IyKuU58fw5/3PYf58VlkgshXEvtFcLJedk9Z436leJ+Bu++9yYVHWl1hgx8OWqNaLw== X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB03.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230028)(4636009)(346002)(39860400002)(396003)(136003)(376002)(451199021)(36840700001)(46966006)(40470700004)(8676002)(4326008)(8936002)(70586007)(70206006)(5660300002)(316002)(40480700001)(110136005)(41300700001)(54906003)(44832011)(186003)(2906002)(478600001)(40460700003)(82740400003)(356005)(81166007)(1076003)(26005)(86362001)(36756003)(336012)(47076005)(36860700001)(2616005)(83380400001)(82310400005)(426003)(6666004)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Jun 2023 13:27:20.7147 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 72c7da14-8f15-4de2-a8cd-08db65c89737 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT024.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7262 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org MSI's are exposed to the devices using VFIO (vfio-cdx). This patch uses the same to add support for MSI for the devices on the cdx bus. Signed-off-by: Nipun Gupta Acked-by: Ferruh Yigit --- drivers/bus/cdx/bus_cdx_driver.h | 25 +++++ drivers/bus/cdx/cdx.c | 11 ++ drivers/bus/cdx/cdx_vfio.c | 176 ++++++++++++++++++++++++++++++- drivers/bus/cdx/version.map | 2 + 4 files changed, 212 insertions(+), 2 deletions(-) diff --git a/drivers/bus/cdx/bus_cdx_driver.h b/drivers/bus/cdx/bus_cdx_driver.h index dbbd1bbdf2..5745429fa7 100644 --- a/drivers/bus/cdx/bus_cdx_driver.h +++ b/drivers/bus/cdx/bus_cdx_driver.h @@ -56,6 +56,7 @@ struct rte_cdx_device { struct rte_cdx_id id; /**< CDX ID. */ struct rte_mem_resource mem_resource[CDX_MAX_RESOURCE]; /**< CDX Memory Resource */ + struct rte_intr_handle *intr_handle; /**< Interrupt handle */ }; /** @@ -147,6 +148,30 @@ void rte_cdx_unmap_device(struct rte_cdx_device *dev); __rte_internal void rte_cdx_register(struct rte_cdx_driver *driver); +/** + * Enables VFIO Interrupts for CDX bus devices. + * + * @param intr_handle + * Pointer to the interrupt handle. + * + * @return + * 0 on success, -1 on error. + */ +__rte_internal +int rte_cdx_vfio_intr_enable(const struct rte_intr_handle *intr_handle); + +/** + * Disable VFIO Interrupts for CDX bus devices. + * + * @param intr_handle + * Pointer to the interrupt handle. + * + * @return + * 0 on success, -1 on error. + */ +__rte_internal +int rte_cdx_vfio_intr_disable(const struct rte_intr_handle *intr_handle); + /** * Helper for CDX device registration from driver (eth, crypto, raw) instance */ diff --git a/drivers/bus/cdx/cdx.c b/drivers/bus/cdx/cdx.c index 9ad8f73424..e5e6a665c3 100644 --- a/drivers/bus/cdx/cdx.c +++ b/drivers/bus/cdx/cdx.c @@ -202,6 +202,15 @@ cdx_scan_one(const char *dirname, const char *dev_name) goto err; } + /* Allocate interrupt instance for cdx device */ + dev->intr_handle = + rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE); + if (dev->intr_handle == NULL) { + CDX_BUS_ERR("Failed to create interrupt instance for %s", + dev->device.name); + return -ENOMEM; + } + /* * Check if device is bound to 'vfio-cdx' driver, so that user-space * can gracefully access the device. @@ -380,6 +389,8 @@ cdx_probe_one_driver(struct rte_cdx_driver *dr, return ret; error_probe: + rte_intr_instance_free(dev->intr_handle); + dev->intr_handle = NULL; cdx_vfio_unmap_resource(dev); error_map_device: return ret; diff --git a/drivers/bus/cdx/cdx_vfio.c b/drivers/bus/cdx/cdx_vfio.c index c545bcd9f5..e8ccf7fb94 100644 --- a/drivers/bus/cdx/cdx_vfio.c +++ b/drivers/bus/cdx/cdx_vfio.c @@ -51,6 +51,10 @@ struct mapped_cdx_resource { /** mapped cdx device list */ TAILQ_HEAD(mapped_cdx_res_list, mapped_cdx_resource); +/* IRQ set buffer length for MSI interrupts */ +#define MSI_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \ + sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1)) + static struct rte_tailq_elem cdx_vfio_tailq = { .name = "VFIO_CDX_RESOURCE_LIST", }; @@ -95,6 +99,27 @@ cdx_vfio_unmap_resource_primary(struct rte_cdx_device *dev) char cdx_addr[PATH_MAX] = {0}; struct mapped_cdx_resource *vfio_res = NULL; struct mapped_cdx_res_list *vfio_res_list; + int ret, vfio_dev_fd; + + if (rte_intr_fd_get(dev->intr_handle) < 0) + return -1; + + if (close(rte_intr_fd_get(dev->intr_handle)) < 0) { + CDX_BUS_ERR("Error when closing eventfd file descriptor for %s", + dev->device.name); + return -1; + } + + vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle); + if (vfio_dev_fd < 0) + return -1; + + ret = rte_vfio_release_device(CDX_BUS_DEVICES_PATH, dev->device.name, + vfio_dev_fd); + if (ret < 0) { + CDX_BUS_ERR("Cannot release VFIO device"); + return ret; + } vfio_res_list = RTE_TAILQ_CAST(cdx_vfio_tailq.head, mapped_cdx_res_list); @@ -117,6 +142,18 @@ cdx_vfio_unmap_resource_secondary(struct rte_cdx_device *dev) { struct mapped_cdx_resource *vfio_res = NULL; struct mapped_cdx_res_list *vfio_res_list; + int ret, vfio_dev_fd; + + vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle); + if (vfio_dev_fd < 0) + return -1; + + ret = rte_vfio_release_device(CDX_BUS_DEVICES_PATH, dev->device.name, + vfio_dev_fd); + if (ret < 0) { + CDX_BUS_ERR("Cannot release VFIO device"); + return ret; + } vfio_res_list = RTE_TAILQ_CAST(cdx_vfio_tailq.head, mapped_cdx_res_list); @@ -141,9 +178,74 @@ cdx_vfio_unmap_resource(struct rte_cdx_device *dev) return cdx_vfio_unmap_resource_secondary(dev); } +/* set up interrupt support (but not enable interrupts) */ static int -cdx_rte_vfio_setup_device(int vfio_dev_fd) +cdx_vfio_setup_interrupts(struct rte_cdx_device *dev, int vfio_dev_fd, + int num_irqs) { + int i, ret; + + if (num_irqs == 0) + return 0; + + /* start from MSI interrupt type */ + for (i = 0; i < num_irqs; i++) { + struct vfio_irq_info irq = { .argsz = sizeof(irq) }; + int fd = -1; + + irq.index = i; + + ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq); + if (ret < 0) { + CDX_BUS_ERR("Cannot get VFIO IRQ info, error %i (%s)", + errno, strerror(errno)); + return -1; + } + + /* if this vector cannot be used with eventfd, fail if we explicitly + * specified interrupt type, otherwise continue + */ + if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) + continue; + + /* Set nb_intr to the total number of interrupts */ + if (rte_intr_event_list_update(dev->intr_handle, irq.count)) + return -1; + + /* set up an eventfd for interrupts */ + fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); + if (fd < 0) { + CDX_BUS_ERR("Cannot set up eventfd, error %i (%s)", + errno, strerror(errno)); + return -1; + } + + if (rte_intr_fd_set(dev->intr_handle, fd)) + return -1; + + /* DPDK CDX bus currently supports only MSI-X */ + if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_VFIO_MSIX)) + return -1; + + if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd)) + return -1; + + return 0; + } + + /* if we're here, we haven't found a suitable interrupt vector */ + return -1; +} + +static int +cdx_vfio_setup_device(struct rte_cdx_device *dev, int vfio_dev_fd, + int num_irqs) +{ + if (cdx_vfio_setup_interrupts(dev, vfio_dev_fd, num_irqs) != 0) { + CDX_BUS_ERR("Error setting up interrupts!"); + return -1; + } + /* * Reset the device. If the device is not capable of resetting, * then it updates errno as EINVAL. @@ -279,6 +381,9 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev) struct cdx_map *maps; int vfio_dev_fd, i, ret; + if (rte_intr_fd_set(dev->intr_handle, -1)) + return -1; + ret = rte_vfio_setup_device(CDX_BUS_DEVICES_PATH, dev_name, &vfio_dev_fd, &device_info); if (ret) @@ -344,7 +449,7 @@ cdx_vfio_map_resource_primary(struct rte_cdx_device *dev) free(reg); } - if (cdx_rte_vfio_setup_device(vfio_dev_fd) < 0) { + if (cdx_vfio_setup_device(dev, vfio_dev_fd, device_info.num_irqs) < 0) { CDX_BUS_ERR("%s setup device failed", dev_name); goto err_vfio_res; } @@ -373,6 +478,9 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev) const char *dev_name = dev->device.name; struct cdx_map *maps; + if (rte_intr_fd_set(dev->intr_handle, -1)) + return -1; + /* if we're in a secondary process, just find our tailq entry */ TAILQ_FOREACH(vfio_res, vfio_res_list, next) { if (strcmp(vfio_res->name, dev_name)) @@ -406,6 +514,10 @@ cdx_vfio_map_resource_secondary(struct rte_cdx_device *dev) dev->mem_resource[i].len = maps[i].size; } + /* we need save vfio_dev_fd, so it can be used during release */ + if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd)) + goto err_vfio_dev_fd; + return 0; err_vfio_dev_fd: rte_vfio_release_device(CDX_BUS_DEVICES_PATH, cdx_addr, vfio_dev_fd); @@ -424,3 +536,63 @@ cdx_vfio_map_resource(struct rte_cdx_device *dev) else return cdx_vfio_map_resource_secondary(dev); } + +int +rte_cdx_vfio_intr_enable(const struct rte_intr_handle *intr_handle) +{ + char irq_set_buf[MSI_IRQ_SET_BUF_LEN]; + struct vfio_irq_set *irq_set; + int *fd_ptr, vfio_dev_fd, i; + int ret; + + irq_set = (struct vfio_irq_set *) irq_set_buf; + irq_set->count = rte_intr_nb_intr_get(intr_handle); + irq_set->argsz = sizeof(struct vfio_irq_set) + + (sizeof(int) * irq_set->count); + + irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index = 0; + irq_set->start = 0; + fd_ptr = (int *) &irq_set->data; + + for (i = 0; i < rte_intr_nb_efd_get(intr_handle); i++) + fd_ptr[i] = rte_intr_efds_index_get(intr_handle, i); + + vfio_dev_fd = rte_intr_dev_fd_get(intr_handle); + ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set); + + if (ret) { + CDX_BUS_ERR("Error enabling MSI interrupts for fd %d", + rte_intr_fd_get(intr_handle)); + return -1; + } + + return 0; +} + +/* disable MSI interrupts */ +int +rte_cdx_vfio_intr_disable(const struct rte_intr_handle *intr_handle) +{ + struct vfio_irq_set *irq_set; + char irq_set_buf[MSI_IRQ_SET_BUF_LEN]; + int len, ret, vfio_dev_fd; + + len = sizeof(struct vfio_irq_set); + + irq_set = (struct vfio_irq_set *) irq_set_buf; + irq_set->argsz = len; + irq_set->count = 0; + irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index = 0; + irq_set->start = 0; + + vfio_dev_fd = rte_intr_dev_fd_get(intr_handle); + ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set); + + if (ret) + CDX_BUS_ERR("Error disabling MSI interrupts for fd %d", + rte_intr_fd_get(intr_handle)); + + return ret; +} diff --git a/drivers/bus/cdx/version.map b/drivers/bus/cdx/version.map index 957fcab978..2f3d484ebd 100644 --- a/drivers/bus/cdx/version.map +++ b/drivers/bus/cdx/version.map @@ -6,6 +6,8 @@ INTERNAL { rte_cdx_register; rte_cdx_unmap_device; rte_cdx_unregister; + rte_cdx_vfio_intr_disable; + rte_cdx_vfio_intr_enable; local: *; }; -- 2.17.1