From patchwork Mon Jun 24 22:47:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710210 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2060.outbound.protection.outlook.com [40.107.237.60]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 618701A2C0D; Mon, 24 Jun 2024 22:47:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269268; cv=fail; b=XGEVGZy0wkA9Yy4xhNdJjYxgBtgKmNsMfQpYnHpLBM09XopHWK+76CKm5DuHNc5tUtZi7k4K0LJfb1nstV5Bhg+zyjt8LT3oxPOcDbhNcYaECsoZXBdz61HqGRlt9hk8eErYHuTahtNzFZDtCRX73C7ythfmE7Vfgky+z9cRpj0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269268; c=relaxed/simple; bh=VwewmplYDVp+G8uPxuU84wYJGKqGDPz+ZZRBBjyeLdE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=TjF9tvP9i8N7Fir5zZqy5APBdsPJmXkMEXyhjkJLol0/+tbY+412/JkiqrIjuQr+laEM+I7IBqiJ9tG1Tra5QOIGf5m2AJzMSLY9X6ZesbhP+5sJWO/G4274hWbjWepYOBeX1YA/Q5kVgeAIDhvV1rkXwghmcBf2vvEYAKxcHGQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=jD0ppvrn; arc=fail smtp.client-ip=40.107.237.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="jD0ppvrn" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZBH/zAtOYsDmjggMYM9FrbWkCt9888mJWQpp6a/jQzEWODtFENA0MnLJ9bU7LbmnxgRVqqE91c0VW2eBKZ2Hw6K/ABFzXvaUb9qhKL8NmsFmCi4dFSUuVLNJ/18ZOeGdPuU27QLi7sfyFMb5ll6bE6fKMV67ff6b85Ui8lf5ns1OpHrZmw6wxz6lT+XJi9gZkSbCXUmkoq0fgxbg/Qm/DpaSmqzlBfOwoNSX/Xq1X2ot95fnzkVa4bN1fyS+EjACWMlrPbxrTJuOZq2XHjqO2knUod3bgqNT45f55rrpK11WzBjIXWQ6N+kQWcf45Cal/e3qSguXRscZkD4xVeFjjA== 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=apam43stmI6A8HxJdS7uki9gbqUgu640LtMjI4qCpLE=; b=hzOGVY2AjMIzSih9SMaR/b+ytI20fNvuPuaLqjEu8H0MRkydx5xmDcDZMjJAlxKUVu4WxXJ2pqTiH5L/oR+5U1JPZdzMhL8pwWJXc1/CTB6YizszzOkz7b5zuLSwGj3ROKPMUwboGyHi/Gkcl4Lf/9A/HXHSOsxJUgbN1cmwHicUXmnYxpHPME/vvIBuNRfvNEg57i9ct1Bmr1ZPxfVUkEZ4rk2ckNHVQo5KTxU1j0EEGHcSPRH0mm50gfal5RW5F2HQacnGBU5Xa5VJsZVjAfmU9ZLHlBKeqC6txeEokfBmQs+urzT8PaCd8ZW8N5Q8Zszcxw5cKx4xGhk0pyhuqg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=apam43stmI6A8HxJdS7uki9gbqUgu640LtMjI4qCpLE=; b=jD0ppvrnVhg/1EwFxajKdwA13Zqqo4ir5I7fRyRhXXrLu7oINibVn+Iall23GoieJ71dpokayWQB8geLHDOES3es6S0q5xsVopA8a0wHGK9RsNleZ9u13X3OyJe8XxTc+nBh/nsomm/4opgDoPu5IezJtBUDU3pTfgMMk/e/0lG7EKlsh91H0YaKu5mtI6rKJZP31SlwvspmpFk3+O7lp1jb3DwBRhGei9Buu0wfkHpKrgniE3byNmY8BuWaMy3DUud2pUOKOVoSZJZraGCG2CwkX0tFvlXFQPRC35/YFWKvWOssw9p+XouHyYD578lNGDX8laQ9Kt8fxCcOP7wyFw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BY5PR12MB4147.namprd12.prod.outlook.com (2603:10b6:a03:205::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.29; Mon, 24 Jun 2024 22:47:38 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:37 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 1/8] fwctl: Add basic structure for a class subsystem with a cdev Date: Mon, 24 Jun 2024 19:47:25 -0300 Message-ID: <1-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0330.namprd13.prod.outlook.com (2603:10b6:208:2c1::35) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BY5PR12MB4147:EE_ X-MS-Office365-Filtering-Correlation-Id: 28fbe3bb-8ece-400b-aed1-08dc949fa32a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|366013|7416011|1800799021|921017; X-Microsoft-Antispam-Message-Info: edtw2lwwmzbqZNhcmH8gGE305NYMI/WG8dMAy7WifcPFUTH5pivmIxUny3Ys3H3S8bBGbvyxs3NP2fVy7AwGu86SJqCYdV63E7NdQVfBqW0i3AyrBRwiWO9NnT2JXfv31kAg53ZQhF8UJ9i+hVHEtmEAieZYjLxgku5wQGNY4amHsb3Hs2pKeM6rGFgEb+8Y2+pL77vPUFc9WiRhQ7gUQHoW29ivcPwKJWus22G7Q0dmjPu4vuOnWDXZ2Zeiv32bCyAXuDZOrvbVYo744b8qRL0iXPwmpjlf9GAV2c2V0Yg9OQGzvssAjFJuquso6qjGMZhx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am-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(366013)(7416011)(1800799021)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: K8Mu0LO/XtjcbUOSTPGbXM/BTRzm2xCB1oD69R8ujR61J38x+x3nQPes8OQNnIJ4kKGAFkQ9SUdePHIMeUW5Bpy+Rm9vgbrIe3mOIoc20RPGBi96/IG5VSVQpZxciqmMPAihpiXaCOSwDLFEOwF/ruI5BPQXVnq07JdnBexPecKyA8kMa321iga00asdvXkxm/3R3JKVVDqdKqBjPFUQlsp43DHABvElJD1y80epQPYxNOOV1P+++Y+t4kzenapBxHNavDj58ujncOE5HL/A2lrC9XHS2wdOd6QJvZUHrmNy79v4O3Wm30v+7QZIlj5RpGGLsS26yoAzvQuGdwaVr96fW0Xr7uIedkZmdy1ic/cDmuqqkAshHy2k4VkA233vn+VqbpIZSvlTkx+zEV/UKghWEAJs6fiIyPU1OEmwladCYx0CuFHniH6aj3ypKkd7AYNyDtpCdCGaAdojZSjFP3v3EIbXCPTliR/gwyGujQ8ba+pXYA7t/z7qsQRJuyfdNir3KsxGkeQFRaclZowYOKQjwLcvdE+AyDHTnFJ2WXlXID3P1f3SzFDFGaXZawlAMREsRxPIZK0Po3IswvQoM3RHttGM/fdD26qr1dkcwHw04Vw1XLGHdy+8vkQK+rO5i6XuHv3DrtiyBh63UaqwOTTGeAMLgi9tpUi5waUCyLhalJE5rvMjd8ALhNI6xoOMF5Pg/DlR6v/W25RPP6ui9MM1vLGkzh0OvbeFfbJzaNq1l+GNh0G4snZqRKyC3q9RO99A4PEZhg2KUKpV85XwYHEJMT+Iju+MpFxf9AKZSziB2Z/6WK4Zla+9N9oC/pUZ4HLQ8Zs+zcQWhw0rxqdSow0n/H0YV+9mnfAHLpfML/kHQkSTjLnOQH7RCxZxajAX8jgXOx+AG80EJI+99iHBsAM/04YecFSmnXDB1hEkzNNgBUCFNv55u1M+gsRnUf7ktARJMd5TDfaA63QeItPrrOinp8NEz4HIG2d9oPHrqwQPWYw9Wc2TEtQ2z0lUkMYpMhjl00JId7+IxktikG00UqzAZSpTkLNzzCbfE/wgDw5gnybPfRGyYjU/OYsLoEIdy9bj8BMJyZrgZCx8v7Huy88ggUuafORbkEo5C5tlx22ex/dlVU7HQdG2ByZl04LL6cYeM3vXb76H79OtgHG36fVSbqSOgrK2gMbkbG9Oqujqc1JHt+JBUdNtZGb6mOG9J/U+50D7e8yjCtaU6tkmaxVr6ocr11sWGTEaO4K8I5rJCFWH8MZNmzlX69xm/H4v8l3jyrgi36+gGMvg3ccvnMFyEYw/dawlld15cqWIb4x1IVA9Cme5w7XgMyYEmtEu/xpiZL3YDtGdbJokfmv30lQWAIZm5Xz5XtLFEOBFWA+/GbPZT1AXv2iW5imiWCrpGf/Sqj5TBXKmt5Y6ipMq6E1kwGI9X6ope1mVaK3EKOy+12aIqp+4o9ys06/UPEVxowBTqhk6qQPRRNo+nhW9n2CKqv5Ms0zrLSxZZE4wFGVOGXNkN9bN2OYUVAxxID2o5RBTWw7mdXWWOyu288PLUx+xKlJy3/Ip36j5jyEquKpLg9cHBShzHJavdOj7ejdz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 28fbe3bb-8ece-400b-aed1-08dc949fa32a X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:34.0082 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: rYGo8PYaX0/y4ejT/GQ3ljIcCI3qe8gznGIXmWLOk6y+2gcZtI7421/fWW9quPR6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4147 Create the class, character device and functions for a fwctl driver to un/register to the subsystem. A typical fwctl driver has a sysfs presence like: $ ls -l /dev/fwctl/fwctl0 crw------- 1 root root 250, 0 Apr 25 19:16 /dev/fwctl/fwctl0 $ ls /sys/class/fwctl/fwctl0 dev device power subsystem uevent $ ls /sys/class/fwctl/fwctl0/device/infiniband/ ibp0s10f0 $ ls /sys/class/infiniband/ibp0s10f0/device/fwctl/ fwctl0/ $ ls /sys/devices/pci0000:00/0000:00:0a.0/fwctl/fwctl0 dev device power subsystem uevent Which allows userspace to link all the multi-subsystem driver components together and learn the subsystem specific names for the device's components. Signed-off-by: Jason Gunthorpe --- MAINTAINERS | 8 ++ drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/fwctl/Kconfig | 9 +++ drivers/fwctl/Makefile | 4 + drivers/fwctl/main.c | 177 +++++++++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 68 ++++++++++++++++ 7 files changed, 269 insertions(+) create mode 100644 drivers/fwctl/Kconfig create mode 100644 drivers/fwctl/Makefile create mode 100644 drivers/fwctl/main.c create mode 100644 include/linux/fwctl.h diff --git a/MAINTAINERS b/MAINTAINERS index 2ca8f35dfe0399..aa7a760d12f8ef 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9076,6 +9076,14 @@ F: kernel/futex/* F: tools/perf/bench/futex* F: tools/testing/selftests/futex/ +FWCTL SUBSYSTEM +M: Jason Gunthorpe +M: Saeed Mahameed +S: Maintained +F: Documentation/userspace-api/fwctl.rst +F: drivers/fwctl/ +F: include/linux/fwctl.h + GALAXYCORE GC0308 CAMERA SENSOR DRIVER M: Sebastian Reichel L: linux-media@vger.kernel.org diff --git a/drivers/Kconfig b/drivers/Kconfig index 7bdad836fc6207..7c556c5ac4fddc 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -21,6 +21,8 @@ source "drivers/connector/Kconfig" source "drivers/firmware/Kconfig" +source "drivers/fwctl/Kconfig" + source "drivers/gnss/Kconfig" source "drivers/mtd/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index fe9ceb0d2288ad..f6a241b747b29c 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -133,6 +133,7 @@ obj-$(CONFIG_MEMSTICK) += memstick/ obj-y += leds/ obj-$(CONFIG_INFINIBAND) += infiniband/ obj-y += firmware/ +obj-$(CONFIG_FWCTL) += fwctl/ obj-$(CONFIG_CRYPTO) += crypto/ obj-$(CONFIG_SUPERH) += sh/ obj-y += clocksource/ diff --git a/drivers/fwctl/Kconfig b/drivers/fwctl/Kconfig new file mode 100644 index 00000000000000..37147a695add9a --- /dev/null +++ b/drivers/fwctl/Kconfig @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +menuconfig FWCTL + tristate "fwctl device firmware access framework" + help + fwctl provides a userspace API for restricted access to communicate + with on-device firmware. The communication channel is intended to + support a wide range of lockdown compatible device behaviors including + manipulating device FLASH, debugging, and other activities that don't + fit neatly into an existing subsystem. diff --git a/drivers/fwctl/Makefile b/drivers/fwctl/Makefile new file mode 100644 index 00000000000000..1cad210f6ba580 --- /dev/null +++ b/drivers/fwctl/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_FWCTL) += fwctl.o + +fwctl-y += main.o diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c new file mode 100644 index 00000000000000..6e9bf15c743b5c --- /dev/null +++ b/drivers/fwctl/main.c @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + */ +#define pr_fmt(fmt) "fwctl: " fmt +#include +#include +#include +#include +#include + +enum { + FWCTL_MAX_DEVICES = 256, +}; +static dev_t fwctl_dev; +static DEFINE_IDA(fwctl_ida); + +static int fwctl_fops_open(struct inode *inode, struct file *filp) +{ + struct fwctl_device *fwctl = + container_of(inode->i_cdev, struct fwctl_device, cdev); + + get_device(&fwctl->dev); + filp->private_data = fwctl; + return 0; +} + +static int fwctl_fops_release(struct inode *inode, struct file *filp) +{ + struct fwctl_device *fwctl = filp->private_data; + + fwctl_put(fwctl); + return 0; +} + +static const struct file_operations fwctl_fops = { + .owner = THIS_MODULE, + .open = fwctl_fops_open, + .release = fwctl_fops_release, +}; + +static void fwctl_device_release(struct device *device) +{ + struct fwctl_device *fwctl = + container_of(device, struct fwctl_device, dev); + + ida_free(&fwctl_ida, fwctl->dev.devt - fwctl_dev); + kfree(fwctl); +} + +static char *fwctl_devnode(const struct device *dev, umode_t *mode) +{ + return kasprintf(GFP_KERNEL, "fwctl/%s", dev_name(dev)); +} + +static struct class fwctl_class = { + .name = "fwctl", + .dev_release = fwctl_device_release, + .devnode = fwctl_devnode, +}; + +static struct fwctl_device * +_alloc_device(struct device *parent, const struct fwctl_ops *ops, size_t size) +{ + struct fwctl_device *fwctl __free(kfree) = kzalloc(size, GFP_KERNEL); + int devnum; + + if (!fwctl) + return NULL; + fwctl->dev.class = &fwctl_class; + fwctl->dev.parent = parent; + + devnum = ida_alloc_max(&fwctl_ida, FWCTL_MAX_DEVICES - 1, GFP_KERNEL); + if (devnum < 0) + return NULL; + fwctl->dev.devt = fwctl_dev + devnum; + + device_initialize(&fwctl->dev); + return_ptr(fwctl); +} + +/* Drivers use the fwctl_alloc_device() wrapper */ +struct fwctl_device *_fwctl_alloc_device(struct device *parent, + const struct fwctl_ops *ops, + size_t size) +{ + struct fwctl_device *fwctl __free(fwctl) = + _alloc_device(parent, ops, size); + + if (!fwctl) + return NULL; + + cdev_init(&fwctl->cdev, &fwctl_fops); + fwctl->cdev.owner = THIS_MODULE; + + if (dev_set_name(&fwctl->dev, "fwctl%d", fwctl->dev.devt - fwctl_dev)) + return NULL; + + fwctl->ops = ops; + return_ptr(fwctl); +} +EXPORT_SYMBOL_NS_GPL(_fwctl_alloc_device, FWCTL); + +/** + * fwctl_register - Register a new device to the subsystem + * @fwctl: Previously allocated fwctl_device + * + * On return the device is visible through sysfs and /dev, driver ops may be + * called. + */ +int fwctl_register(struct fwctl_device *fwctl) +{ + int ret; + + ret = cdev_device_add(&fwctl->cdev, &fwctl->dev); + if (ret) + return ret; + return 0; +} +EXPORT_SYMBOL_NS_GPL(fwctl_register, FWCTL); + +/** + * fwctl_unregister - Unregister a device from the subsystem + * @fwctl: Previously allocated and registered fwctl_device + * + * Undoes fwctl_register(). On return no driver ops will be called. The + * caller must still call fwctl_put() to free the fwctl. + * + * Unregister will return even if userspace still has file descriptors open. + * This will call ops->close_uctx() on any open FDs and after return no driver + * op will be called. The FDs remain open but all fops will return -ENODEV. + * + * The design of fwctl allows this sort of disassociation of the driver from the + * subsystem primarily by keeping memory allocations owned by the core subsytem. + * The fwctl_device and fwctl_uctx can both be freed without requiring a driver + * callback. This allows the module to remain unlocked while FDs are open. + */ +void fwctl_unregister(struct fwctl_device *fwctl) +{ + cdev_device_del(&fwctl->cdev, &fwctl->dev); + + /* + * The driver module may unload after this returns, the op pointer will + * not be valid. + */ + fwctl->ops = NULL; +} +EXPORT_SYMBOL_NS_GPL(fwctl_unregister, FWCTL); + +static int __init fwctl_init(void) +{ + int ret; + + ret = alloc_chrdev_region(&fwctl_dev, 0, FWCTL_MAX_DEVICES, "fwctl"); + if (ret) + return ret; + + ret = class_register(&fwctl_class); + if (ret) + goto err_chrdev; + return 0; + +err_chrdev: + unregister_chrdev_region(fwctl_dev, FWCTL_MAX_DEVICES); + return ret; +} + +static void __exit fwctl_exit(void) +{ + class_unregister(&fwctl_class); + unregister_chrdev_region(fwctl_dev, FWCTL_MAX_DEVICES); +} + +module_init(fwctl_init); +module_exit(fwctl_exit); +MODULE_DESCRIPTION("fwctl device firmware access framework"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/fwctl.h b/include/linux/fwctl.h new file mode 100644 index 00000000000000..ef4eaa87c945e4 --- /dev/null +++ b/include/linux/fwctl.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + */ +#ifndef __LINUX_FWCTL_H +#define __LINUX_FWCTL_H +#include +#include +#include + +struct fwctl_device; +struct fwctl_uctx; + +struct fwctl_ops { +}; + +/** + * struct fwctl_device - Per-driver registration struct + * @dev: The sysfs (class/fwctl/fwctlXX) device + * + * Each driver instance will have one of these structs with the driver + * private data following immeidately after. This struct is refcounted, + * it is freed by calling fwctl_put(). + */ +struct fwctl_device { + struct device dev; + /* private: */ + struct cdev cdev; + const struct fwctl_ops *ops; +}; + +struct fwctl_device *_fwctl_alloc_device(struct device *parent, + const struct fwctl_ops *ops, + size_t size); +/** + * fwctl_alloc_device - Allocate a fwctl + * @parent: Physical device that provides the FW interface + * @ops: Driver ops to register + * @drv_struct: 'struct driver_fwctl' that holds the struct fwctl_device + * @member: Name of the struct fwctl_device in @drv_struct + * + * This allocates and initializes the fwctl_device embedded in the drv_struct. + * Upon success the pointer must be freed via fwctl_put(). Returns NULL on + * failure. Returns a 'drv_struct *' on success, NULL on error. + */ +#define fwctl_alloc_device(parent, ops, drv_struct, member) \ + container_of(_fwctl_alloc_device( \ + parent, ops, \ + sizeof(drv_struct) + \ + BUILD_BUG_ON_ZERO( \ + offsetof(drv_struct, member))), \ + drv_struct, member) + +static inline struct fwctl_device *fwctl_get(struct fwctl_device *fwctl) +{ + get_device(&fwctl->dev); + return fwctl; +} +static inline void fwctl_put(struct fwctl_device *fwctl) +{ + put_device(&fwctl->dev); +} +DEFINE_FREE(fwctl, struct fwctl_device *, if (_T) fwctl_put(_T)); + +int fwctl_register(struct fwctl_device *fwctl); +void fwctl_unregister(struct fwctl_device *fwctl); + +#endif From patchwork Mon Jun 24 22:47:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710212 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2089.outbound.protection.outlook.com [40.107.94.89]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 155E61A2573; Mon, 24 Jun 2024 22:47:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.89 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269268; cv=fail; b=QhMwph0wWZuE6z4+Weo853LYo5/nwoQ/SLvSBL7WhaJGqaS/PsWiUY55k6O9QlRLEpsoxGy8JOHojI197QQu7jQLLyug3y6JEQmSWeeWDN0G/szcUevdqMJ0oO1UwKm0F4zezZeTzdwOM0dwvbVo5fP1OkAGs8VB/PHd2jfyktk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269268; c=relaxed/simple; bh=9LL8FTTeqBAcEeNsr/jEn26HrKYFsCnLecYZqYjqnAE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=FTKcUiVXxYug6fXQoDgdfvyn71UUXHSkWV0UpK7CkT/j6/PdqPDbZl7zi4tA40zIMX4KBL2dY0LRQkJ0XzPjZNN40K6b5HlQQ1OAtY+Q50SEyNr4tda4b7YRbzysItbb011AWBdWsnSxSYPyRkTF3qBCzjvkg898s96QSSk7Qkc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=R+d0+vss; arc=fail smtp.client-ip=40.107.94.89 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="R+d0+vss" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=itYpGD7n8xVAiQO7sTGztQ6gs44qNEQmLskJ+bnjupWEBXsGfZHcoIzEHsZz0tiLoDRz+ciVo2An1Js//NYlEP3XG/hO+7KmW88iztqfggmajchBksVp3smFpVdUQPznfQ9IrMzsDDghsPbPxfJj3X+wcAmc2lbYnXTMwrVvcLuERpws4r9/gUhTqnFW7i2oz8MNgwveq1vJaa6kO+whTh9uoHllhrtPRAY7jD4untpZqAHlwwB627j7WrVHsKnUeuIwfdBOYyjufjEN3f9PvYXxr07gecsQbjhFtfP/g1a3cZmCt7udx91t/eScC2M3xASZeRmTtH4mgrYLQ55qRA== 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=4TBNPrZNu2noX0hR3hUW2WU/nT7Y3Zwpb5vhJl0vwi0=; b=MCMsTexjxJ87Tg71WhlUvyHcMNREauVlSMB7kjkSDBntHO+jUGJAJ2TeIU91yN98dCi0T65XTRPVjPySEBH4QO0LEPu+CU3yZHXfjsLOzeN72pxkr2VKmhppfVqj0lnXlmUmRaA5sFFk4pkOp7AC13lEWi4S0YJuhpQ7As6nSvWvSZBtX6EB+trVII5Bm2Zql8WxgSdO7HTqrp5VIlEK629gm88BmKz5pQGxuTdyXu27+7welZvhKOQyCbN+2DpB26sZnqiJEKEB6iqdv1vCw8fP3fG/2Wj2bhnQc1cAinbgVwPAXeFQRWhUC3dRhpnF38QIGfWoZWI1pQBTnYELAQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4TBNPrZNu2noX0hR3hUW2WU/nT7Y3Zwpb5vhJl0vwi0=; b=R+d0+vsspCzKkhXkqiQwo86gNJ2Mw+fjvuUCXtEP1XKoTLJWn0Wj0DfkBsQdq8n/z5wnGE+PdEdd4Di8fnY5JOrp0PYeVYVrOTZ9dILDc2Z+UyBM8+6COOJFA6DoT6SjAUZqpzL+TDy+Cq0qTbW5MqnkbNVP++G1sXW8EsGCW1u1kKAlT1OMep1MZ8FU1AeicpXqLOWCyhs5C/iw7naWOWDCDPFfSewHOaYxjUYLtfwji/HWp/u/5qKKpd3eAODIh000+qXiL6Nkz8EAOmt4S5OvVuD5LVXFmgO1cM4opJWlOce/mYVel04eLhhQZk1IdKpi0ymC1SV6BZY7nmuqtQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by DS7PR12MB6024.namprd12.prod.outlook.com (2603:10b6:8:84::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.30; Mon, 24 Jun 2024 22:47:41 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:40 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 2/8] fwctl: Basic ioctl dispatch for the character device Date: Mon, 24 Jun 2024 19:47:26 -0300 Message-ID: <2-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: BL1P221CA0021.NAMP221.PROD.OUTLOOK.COM (2603:10b6:208:2c5::17) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|DS7PR12MB6024:EE_ X-MS-Office365-Filtering-Correlation-Id: 6209cb50-091b-4700-864d-08dc949fa417 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|1800799021|7416011|366013|921017; X-Microsoft-Antispam-Message-Info: qDmf3lf+10UZtCJ0ONtxHIrKZN7VSX4I5g7rNvEYv/A7gWJVe+ezWGVdXQXe/DnTVZ5Tjb9ehXXIpHth4RMHidEq9bYX1eElCrGIHcTLkiehhIxtB8QgOBYcEBCFhsb8++r/KIokjdOGsoKWc3hDRHKn90VC2Vcio+JcGCu7meC/WGpbz2OFPEBScUn4tWzkVoVkAkpaxLZUFwMPiOSqaWXRn0qdPvuOEb3tATjH3XYDFu+nAQmxqUgf7N5XSSK8bHP6f98t8OjwAJeb7nt/qpKk9drV0bnczJttgV41LnzEPVNi02Ea7r9wuQ8rgzbroYBPENeSYHkqYgJDpniszquLXQH/FaX4YvwC8VBegYt/BDgT8jlOPsZBfTQ0ihXwpQzhR5txomeahaPa6GkAZlD447pNtu6662gco3sMgtr6OcF+x5N/TZ8BH6b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WW2xQMY3/43/QD9BcwaMFQZjFzTKtgr3Fc9SYRGFixEqqR+DPRBnm067wh6yRHs4LLxPb4IQz1i9UHAVT4q4mNbweUgvwYTajRvEzaEJfkJm0d9YONAVsL1MiuErpjQIk/nJzL8LkjgmcooemKzsRy6x2XKyuoJOCj7uJE3pI35NDYAvgLgr7cZ4191Zkyc2S9YGXhe3gHRGhLZeTgdQ86ZRpx+r3Wrtsdhnt3KRJBXghboTj4vkGC973mszzwdRR13JGaXdE2yEaGf8kicxbUzYtDw69WFMufDa5zT3F25Gvyz9gpthcgoLUI0jGlqgZBq6JiSY2N9SM4xOoreESksFDka3Pjf2jqKLIeckx4/5XWHtb4PrRLvnbI7+Rr+uhcpPwtXOidhLKBzRaJ8N5NNcyQUUfBn4Kyr9q0PI39d7WWRP9DdS1Eb5GAKb5NMRCDB6/gHIFsB394iHzC6f23nkEU0y9uWpBriSU2dMszqHVVnpDjUdzvVZLyia7AHWHpvtJMrKN+SAB1S+SkDgVvv7BVnmge1Iyvdi+R9BHG5B/K74x5Q4W6jeYcokKGfWoqzfLHeYZEA0HC76Puz+xu4N5eIshiL/2xnBbZOmrFkxKZId X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6209cb50-091b-4700-864d-08dc949fa417 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:35.5847 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: CLLIMgMBVmFM+eotuQP3yybmQXhwvuMv3p8z+zc8GO9gE8ws2omkC6snF1gzgOjK X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6024 Each file descriptor gets a chunk of per-FD driver specific context that allows the driver to attach a device specific struct to. The core code takes care of the memory lifetime for this structure. The ioctl dispatch and design is based on what was built for iommufd. The ioctls have a struct which has a combined in/out behavior with a typical 'zero pad' scheme for future extension and backwards compatibility. Like iommufd some shared logic does most of the ioctl marshalling and compatibility work and tables diatches to some function pointers for each unique iotcl. This approach has proven to work quite well in the iommufd and rdma subsystems. Allocate an ioctl number space for the subsystem. Signed-off-by: Jason Gunthorpe --- .../userspace-api/ioctl/ioctl-number.rst | 1 + MAINTAINERS | 1 + drivers/fwctl/main.c | 124 +++++++++++++++++- include/linux/fwctl.h | 31 +++++ include/uapi/fwctl/fwctl.h | 41 ++++++ 5 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 include/uapi/fwctl/fwctl.h diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst index a141e8e65c5d3a..4d91c5a20b98c8 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -324,6 +324,7 @@ Code Seq# Include File Comments 0x97 00-7F fs/ceph/ioctl.h Ceph file system 0x99 00-0F 537-Addinboard driver +0x9A 00-0F include/uapi/fwctl/fwctl.h 0xA0 all linux/sdp/sdp.h Industrial Device Project 0xA1 0 linux/vtpm_proxy.h TPM Emulator Proxy Driver diff --git a/MAINTAINERS b/MAINTAINERS index aa7a760d12f8ef..2090009a6ae98a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9083,6 +9083,7 @@ S: Maintained F: Documentation/userspace-api/fwctl.rst F: drivers/fwctl/ F: include/linux/fwctl.h +F: include/uapi/fwctl/ GALAXYCORE GC0308 CAMERA SENSOR DRIVER M: Sebastian Reichel diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c index 6e9bf15c743b5c..6872c01d5c62e8 100644 --- a/drivers/fwctl/main.c +++ b/drivers/fwctl/main.c @@ -9,26 +9,131 @@ #include #include +#include + enum { FWCTL_MAX_DEVICES = 256, }; static dev_t fwctl_dev; static DEFINE_IDA(fwctl_ida); +struct fwctl_ucmd { + struct fwctl_uctx *uctx; + void __user *ubuffer; + void *cmd; + u32 user_size; +}; + +/* On stack memory for the ioctl structs */ +union ucmd_buffer { +}; + +struct fwctl_ioctl_op { + unsigned int size; + unsigned int min_size; + unsigned int ioctl_num; + int (*execute)(struct fwctl_ucmd *ucmd); +}; + +#define IOCTL_OP(_ioctl, _fn, _struct, _last) \ + [_IOC_NR(_ioctl) - FWCTL_CMD_BASE] = { \ + .size = sizeof(_struct) + \ + BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \ + sizeof(_struct)), \ + .min_size = offsetofend(_struct, _last), \ + .ioctl_num = _ioctl, \ + .execute = _fn, \ + } +static const struct fwctl_ioctl_op fwctl_ioctl_ops[] = { +}; + +static long fwctl_fops_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + struct fwctl_uctx *uctx = filp->private_data; + const struct fwctl_ioctl_op *op; + struct fwctl_ucmd ucmd = {}; + union ucmd_buffer buf; + unsigned int nr; + int ret; + + nr = _IOC_NR(cmd); + if ((nr - FWCTL_CMD_BASE) >= ARRAY_SIZE(fwctl_ioctl_ops)) + return -ENOIOCTLCMD; + op = &fwctl_ioctl_ops[nr - FWCTL_CMD_BASE]; + if (op->ioctl_num != cmd) + return -ENOIOCTLCMD; + + ucmd.uctx = uctx; + ucmd.cmd = &buf; + ucmd.ubuffer = (void __user *)arg; + ret = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer); + if (ret) + return ret; + + if (ucmd.user_size < op->min_size) + return -EINVAL; + + ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer, + ucmd.user_size); + if (ret) + return ret; + + guard(rwsem_read)(&uctx->fwctl->registration_lock); + if (!uctx->fwctl->ops) + return -ENODEV; + return op->execute(&ucmd); +} + static int fwctl_fops_open(struct inode *inode, struct file *filp) { struct fwctl_device *fwctl = container_of(inode->i_cdev, struct fwctl_device, cdev); + int ret; + + guard(rwsem_read)(&fwctl->registration_lock); + if (!fwctl->ops) + return -ENODEV; + + struct fwctl_uctx *uctx __free(kfree) = + kzalloc(fwctl->ops->uctx_size, GFP_KERNEL | GFP_KERNEL_ACCOUNT); + if (!uctx) + return -ENOMEM; + + uctx->fwctl = fwctl; + ret = fwctl->ops->open_uctx(uctx); + if (ret) + return ret; + + scoped_guard(mutex, &fwctl->uctx_list_lock) { + list_add_tail(&uctx->uctx_list_entry, &fwctl->uctx_list); + } get_device(&fwctl->dev); - filp->private_data = fwctl; + filp->private_data = no_free_ptr(uctx); return 0; } +static void fwctl_destroy_uctx(struct fwctl_uctx *uctx) +{ + lockdep_assert_held(&uctx->fwctl->uctx_list_lock); + list_del(&uctx->uctx_list_entry); + uctx->fwctl->ops->close_uctx(uctx); +} + static int fwctl_fops_release(struct inode *inode, struct file *filp) { - struct fwctl_device *fwctl = filp->private_data; + struct fwctl_uctx *uctx = filp->private_data; + struct fwctl_device *fwctl = uctx->fwctl; + scoped_guard(rwsem_read, &fwctl->registration_lock) { + if (fwctl->ops) { + guard(mutex)(&fwctl->uctx_list_lock); + fwctl_destroy_uctx(uctx); + } + } + + kfree(uctx); fwctl_put(fwctl); return 0; } @@ -37,6 +142,7 @@ static const struct file_operations fwctl_fops = { .owner = THIS_MODULE, .open = fwctl_fops_open, .release = fwctl_fops_release, + .unlocked_ioctl = fwctl_fops_ioctl, }; static void fwctl_device_release(struct device *device) @@ -45,6 +151,7 @@ static void fwctl_device_release(struct device *device) container_of(device, struct fwctl_device, dev); ida_free(&fwctl_ida, fwctl->dev.devt - fwctl_dev); + mutex_destroy(&fwctl->uctx_list_lock); kfree(fwctl); } @@ -69,6 +176,9 @@ _alloc_device(struct device *parent, const struct fwctl_ops *ops, size_t size) return NULL; fwctl->dev.class = &fwctl_class; fwctl->dev.parent = parent; + init_rwsem(&fwctl->registration_lock); + mutex_init(&fwctl->uctx_list_lock); + INIT_LIST_HEAD(&fwctl->uctx_list); devnum = ida_alloc_max(&fwctl_ida, FWCTL_MAX_DEVICES - 1, GFP_KERNEL); if (devnum < 0) @@ -137,8 +247,18 @@ EXPORT_SYMBOL_NS_GPL(fwctl_register, FWCTL); */ void fwctl_unregister(struct fwctl_device *fwctl) { + struct fwctl_uctx *uctx; + cdev_device_del(&fwctl->cdev, &fwctl->dev); + /* Disable and free the driver's resources for any still open FDs. */ + guard(rwsem_write)(&fwctl->registration_lock); + guard(mutex)(&fwctl->uctx_list_lock); + while ((uctx = list_first_entry_or_null(&fwctl->uctx_list, + struct fwctl_uctx, + uctx_list_entry))) + fwctl_destroy_uctx(uctx); + /* * The driver module may unload after this returns, the op pointer will * not be valid. diff --git a/include/linux/fwctl.h b/include/linux/fwctl.h index ef4eaa87c945e4..1d9651de92fc19 100644 --- a/include/linux/fwctl.h +++ b/include/linux/fwctl.h @@ -11,7 +11,20 @@ struct fwctl_device; struct fwctl_uctx; +/** + * struct fwctl_ops - Driver provided operations + * @uctx_size: The size of the fwctl_uctx struct to allocate. The first + * bytes of this memory will be a fwctl_uctx. The driver can use the + * remaining bytes as its private memory. + * @open_uctx: Called when a file descriptor is opened before the uctx is ever + * used. + * @close_uctx: Called when the uctx is destroyed, usually when the FD is + * closed. + */ struct fwctl_ops { + size_t uctx_size; + int (*open_uctx)(struct fwctl_uctx *uctx); + void (*close_uctx)(struct fwctl_uctx *uctx); }; /** @@ -26,6 +39,10 @@ struct fwctl_device { struct device dev; /* private: */ struct cdev cdev; + + struct rw_semaphore registration_lock; + struct mutex uctx_list_lock; + struct list_head uctx_list; const struct fwctl_ops *ops; }; @@ -65,4 +82,18 @@ DEFINE_FREE(fwctl, struct fwctl_device *, if (_T) fwctl_put(_T)); int fwctl_register(struct fwctl_device *fwctl); void fwctl_unregister(struct fwctl_device *fwctl); +/** + * struct fwctl_uctx - Per user FD context + * @fwctl: fwctl instance that owns the context + * + * Every FD opened by userspace will get a unique context allocation. Any driver + * private data will follow immediately after. + */ +struct fwctl_uctx { + struct fwctl_device *fwctl; + /* private: */ + /* Head at fwctl_device::uctx_list */ + struct list_head uctx_list_entry; +}; + #endif diff --git a/include/uapi/fwctl/fwctl.h b/include/uapi/fwctl/fwctl.h new file mode 100644 index 00000000000000..0bdce95b6d69d9 --- /dev/null +++ b/include/uapi/fwctl/fwctl.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. + */ +#ifndef _UAPI_FWCTL_H +#define _UAPI_FWCTL_H + +#include +#include + +#define FWCTL_TYPE 0x9A + +/** + * DOC: General ioctl format + * + * The ioctl interface follows a general format to allow for extensibility. Each + * ioctl is passed in a structure pointer as the argument providing the size of + * the structure in the first u32. The kernel checks that any structure space + * beyond what it understands is 0. This allows userspace to use the backward + * compatible portion while consistently using the newer, larger, structures. + * + * ioctls use a standard meaning for common errnos: + * + * - ENOTTY: The IOCTL number itself is not supported at all + * - E2BIG: The IOCTL number is supported, but the provided structure has + * non-zero in a part the kernel does not understand. + * - EOPNOTSUPP: The IOCTL number is supported, and the structure is + * understood, however a known field has a value the kernel does not + * understand or support. + * - EINVAL: Everything about the IOCTL was understood, but a field is not + * correct. + * - ENOMEM: Out of memory. + * - ENODEV: The underlying device has been hot-unplugged and the FD is + * orphaned. + * + * As well as additional errnos, within specific ioctls. + */ +enum { + FWCTL_CMD_BASE = 0, +}; + +#endif From patchwork Mon Jun 24 22:47:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710213 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2060.outbound.protection.outlook.com [40.107.237.60]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF5921A2556; Mon, 24 Jun 2024 22:47:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269270; cv=fail; b=ptia3Tsy17khtCsOy7FBxK7YAoK438UFGb5oKWLIuxu64TevgWqmVnJmpLGRkXKt9rHLPgO52fvAW1U4SoLCtSohMs504SXbTlG5zgWdvwBFmtMS3V9TJprqoWcrfPIKcHcb0o0PPvotcZO20H+oQ32Ttvv7xr/Wm77mwandBWk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269270; c=relaxed/simple; bh=xNTNqHqZ1cG9cvVIU83R7MWCqKHpanuuBkyZ9DSwFUg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=nfj/5hY0TxfOLu915GDRBkuRLLVZb/iz+u7judOaRnabIMiS63kqA59XdOU2JPVvoYfBYj/3LjSw5zwDcF3R8MqoctcsmyWp5GQb7INpEYRJVN1Uxm2mgDN3elcJiBnJsLYifeO+2xdDsFMQiceNz1/ovb6E2AP3KIC9EREu0CQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Kv5uJLlf; arc=fail smtp.client-ip=40.107.237.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Kv5uJLlf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F+dhGEGBogjJIO1lSN9H9K3Kydf9Y6v3/txNaHXLs3DdCFuFGg4UWLY2w5/35pfE/jj71eNoPSLq41SSwIncjtk3DJcGK3Gy7JV/tUoCskAWQzPOENbj67PwvHub0M9dmIuP2fmQfFhaQEGYqFVlaHztv1zo1IhCApI0zZBQS2avo4yRDA2sR8P5YMw9J7WWC3LVmbonBAHwlYMRmsen3g7UuFg9Up7yCkHOmbvNLoStR1YEEcoEQojaRWakPiViiPil1Kik3FmLQFgYC1Ngvw9Vjtli9Uwwm0wU3co1pYrJx+VTynZoBTPLfTbjFd9+jU+P4T6vMEJccZVcFl/XmA== 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=6LkXHMOLTxsLzvj6tVxRa5+wTgzmi3dZW8K+OEZv8So=; b=HRFi5XSdph2GNcFUpQwNx1aJKCuiL4NkeJoOc5IEVRiXMZ3tSUOD76PAh7rU8gWUE/e1+Pu05UJ4E5vAlQCJj5cQuSoq5iSqRwovbAOSNVpLlxD/0b9bi82EoVTgMc+cmlqLszoj8lCDp5rycoEClyXE8e9uRza1cJzKIyfFMye9DnGSsnyyWTag5lUUnjj+HqVhN3DAzbWnxn58txBxB9aXXir/FHc2iO3Eyhq9NL0JlkOhZ37EKXuZ5oqQQ7Fjbf/egS2hc13bSn92BawN17mfrH4Hb1tiTZUAYcrQ7S+bPWj9F/GAeihFmgDPSbUz0Ip0q/A5YuVgYtL/QAhW4g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6LkXHMOLTxsLzvj6tVxRa5+wTgzmi3dZW8K+OEZv8So=; b=Kv5uJLlftwnhfOBozDZs4is63FoBfD82PfKkPv9XoIJ+tAzlqxsNA34EczwGOMY+9Np5frGecm3YymNpLIYti9O6ibWOKJcvt3GIgDQwk2IUFk4YcnMtCZQmsgndKGHc+z8gz0KhXlQcPF0bANqzvHaiJ5x33RYnRZzUyEQCsBx2HKYXeawyXKbZTckapfUTjtWjJVWP7s8G+SJ/aRiTjDzmQFJvRYlRIHiZ9MQ84K9XjID3JMofe4Klz5f7tGwOzMJu1/oTnTKah3OjIDfoIar3Bg6m7p4oNQWFxbVXG4CYhRxc6obVK+kh64lOjP1v/sv9lIu/PvGQHyU8iKYbMA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BY5PR12MB4147.namprd12.prod.outlook.com (2603:10b6:a03:205::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.29; Mon, 24 Jun 2024 22:47:39 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:39 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 3/8] fwctl: FWCTL_INFO to return basic information about the device Date: Mon, 24 Jun 2024 19:47:27 -0300 Message-ID: <3-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0308.namprd13.prod.outlook.com (2603:10b6:208:2c1::13) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BY5PR12MB4147:EE_ X-MS-Office365-Filtering-Correlation-Id: dee19c90-208b-48b7-c16f-08dc949fa3d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|366013|7416011|1800799021|921017; X-Microsoft-Antispam-Message-Info: VX0qZGfzpGbOvhUZ1bsvpoSVytS+feUo8ZO/50PzsxBp36BbmlRu4x1rA30EuPvln+OeZyIhNYLcGBT+oj0l2aikLEHklsTcqczMC6hr+nldzxlrGhA+8/WhwB0pi1vCc69+y+qhjvpC3jUBdBxf+fyXdMHQbiFSHga37pzLq4T5AuF3p+dKWecOdEoUILlOYQOP1a5cdIQbHOPQTuJJT/cC0USXbdzoX6hVzC/Wd1TKq5i81jUkh4tXFetzrvRqtbFWYvLeOES/d8Hq5qUohZ1N13cjv9hI39ebjkABoo3fmmRbuBUO5K8EsjnjZwTdEbODmzkgE3q8DoonHsf2ynC0n1uEwqQnG/AR6Zfn35rAGxMW20MZDD3M3AO5in/KGFS/bwXnsWZE4xqNemU7tiYk0VXpwTy5uiLkCHP6bpr6SCKPAdWUZQRlVTIEi8rEO0EfU8XI+VFBEnVZEXun9od4a0i+r/apWNvASmElBrnHE+rNRnUXHwsyuqQv5YRxgLsNy6yOyhCZGBfQurBK43OMaiRVAvolRRE1KIUtT1LTaxLtxH9P9nydiTjCcetqwkjOK8ea8MVp8ooQp3NqihbQLGAb/s4grrJumbX7rFlPy7NB87msQub0egPAmE2cXJ3HdpSvk7kdXlsQzZ6/aBDecT33DaSKoOeBECB0XVJhVnsN3tMvKPmzBcVqy6b//jUH6lRK9zYyynyzKLdfnHjCw6WR3Cxhxu09GhFM/tVZMagEhMA46d0FXID2Phal81MJpMU3cItyFJxR3kFxe0T9SIagFQGX51Eujlg9liUSCimvj+PmN3sKlrOQIwO5z7FfRk4AoxxRPFg2r+omq3OpBjhQu6SIUVzf6IkfUeLQ2Vnl8TQoTeYre22asOsJpqnxTrAThTlOI7tirWG3QB6ogQw4R3BsrzqpkeHzo5ydJ5xCNHSTrIvzP5VPKaOoDnPrhoXUtDhpP0wnBnAoG3cSIWv/o3VziWBP7F6FBOC+LnNP69jEpZEHGRm+9PgQHmeDXpT4J1wZ1gRahgkuQH0lw0H0iJLIDNlx2CCASKWT2wPpSfRaiMQMsMp2e3jSA5RJIGc5b2GSqAXXF+vaJj7VWpjUt0rmIDNcgxZIHldfZRt4gIO2bi+T2t40ON+2NPI9ImjXt4GLiaaAaxfYj29QYNNx4gBte+1kSFE6aB6/0S27COn3px9j97mg7rHB1l5so3nzk4/P577M+XhinP8mfpmK/IqsRUBteX0VGCkcSsp0KSfN5gqkoWVEPUhcYq14i2SU/t2WCD/PqOaOcy3moXKssCS38BXjShsvKn9U3rJkSYS8QEA2q8sv2JFuBiMxZnMsqyjM0epGPXrTpjMnhG4dUkeGh28VlcjHWQo= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(366013)(7416011)(1800799021)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GyCLSz2xSEJDvUIuP1/TcOLs3p2MVFbm2MeAoKYTcz5wJQPC8YI6buIXAjRnmA6LwWkvWif5ya/IcnZeQihplPjtb58WXUOoW0VefjRRsweqSTudzd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et5P71WppCptptKctHdyjBUc1Iu/7M/V7phcT1Dvvc8evCOSbFg4sqa7QgO2yYDWXtb0/eaMnNpKpWx+iHf44oeIJYmeamO2WANVBQqVWwMAF5ezdFilOIR/s9OFNPkLh+kG3Bsp6o2Oqx2XjTtwlkd8St7pjweSOFrc+3Hb+12zgrMqV90cKpEhpLYcRSdY5LcwMN0zLzDDON1XcoKz/z+9HweDDKTsSx52dyVuw0o+hYKw9iTxqIWltwzhJVCOsRsTTNHyRGwmt8Cqvt6wADiovQxdoAkePcYz+ZWKRT957XUKbxDXhvALPXVHfo4kc1BHjYNxHfUI6SeLW+OJTjD5tQpeUZvKEjayaK61O/yU8SAKEAgmfuvWvH2Djp/bUj4QnVR3n/FGkDaf2 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: dee19c90-208b-48b7-c16f-08dc949fa3d3 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:35.0572 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8c3tP5iWa1z/0nJbEZB+djCunc55eBMKZw7ROdmTD6XwGiIJt6kI4xCgWdJNg4OK X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4147 Userspace will need to know some details about the fwctl interface being used to locate the correct userspace code to communicate with the kernel. Provide a simple device_type enum indicating what the kernel driver is. Allow the device to provide a device specific info struct that contains any additional information that the driver may need to provide to userspace. Signed-off-by: Jason Gunthorpe --- drivers/fwctl/main.c | 53 ++++++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 8 ++++++ include/uapi/fwctl/fwctl.h | 29 +++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c index 6872c01d5c62e8..f1dec0b590aee4 100644 --- a/drivers/fwctl/main.c +++ b/drivers/fwctl/main.c @@ -17,6 +17,8 @@ enum { static dev_t fwctl_dev; static DEFINE_IDA(fwctl_ida); +DEFINE_FREE(kfree_errptr, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T)); + struct fwctl_ucmd { struct fwctl_uctx *uctx; void __user *ubuffer; @@ -24,8 +26,58 @@ struct fwctl_ucmd { u32 user_size; }; +static int ucmd_respond(struct fwctl_ucmd *ucmd, size_t cmd_len) +{ + if (copy_to_user(ucmd->ubuffer, ucmd->cmd, + min_t(size_t, ucmd->user_size, cmd_len))) + return -EFAULT; + return 0; +} + +static int copy_to_user_zero_pad(void __user *to, const void *from, + size_t from_len, size_t user_len) +{ + size_t copy_len; + + copy_len = min(from_len, user_len); + if (copy_to_user(to, from, copy_len)) + return -EFAULT; + if (copy_len < user_len) { + if (clear_user(to + copy_len, user_len - copy_len)) + return -EFAULT; + } + return 0; +} + +static int fwctl_cmd_info(struct fwctl_ucmd *ucmd) +{ + struct fwctl_device *fwctl = ucmd->uctx->fwctl; + struct fwctl_info *cmd = ucmd->cmd; + size_t driver_info_len = 0; + + if (cmd->flags) + return -EOPNOTSUPP; + + if (cmd->device_data_len) { + void *driver_info __free(kfree_errptr) = + fwctl->ops->info(ucmd->uctx, &driver_info_len); + if (IS_ERR(driver_info)) + return PTR_ERR(driver_info); + + if (copy_to_user_zero_pad(u64_to_user_ptr(cmd->out_device_data), + driver_info, driver_info_len, + cmd->device_data_len)) + return -EFAULT; + } + + cmd->out_device_type = fwctl->ops->device_type; + cmd->device_data_len = driver_info_len; + return ucmd_respond(ucmd, sizeof(*cmd)); +} + /* On stack memory for the ioctl structs */ union ucmd_buffer { + struct fwctl_info info; }; struct fwctl_ioctl_op { @@ -45,6 +97,7 @@ struct fwctl_ioctl_op { .execute = _fn, \ } static const struct fwctl_ioctl_op fwctl_ioctl_ops[] = { + IOCTL_OP(FWCTL_INFO, fwctl_cmd_info, struct fwctl_info, out_device_data), }; static long fwctl_fops_ioctl(struct file *filp, unsigned int cmd, diff --git a/include/linux/fwctl.h b/include/linux/fwctl.h index 1d9651de92fc19..9a906b861acf3a 100644 --- a/include/linux/fwctl.h +++ b/include/linux/fwctl.h @@ -7,12 +7,14 @@ #include #include #include +#include struct fwctl_device; struct fwctl_uctx; /** * struct fwctl_ops - Driver provided operations + * @device_type: The drivers assigned device_type number. This is uABI * @uctx_size: The size of the fwctl_uctx struct to allocate. The first * bytes of this memory will be a fwctl_uctx. The driver can use the * remaining bytes as its private memory. @@ -20,11 +22,17 @@ struct fwctl_uctx; * used. * @close_uctx: Called when the uctx is destroyed, usually when the FD is * closed. + * @info: Implement FWCTL_INFO. Return a kmalloc() memory that is copied to + * out_device_data. On input length indicates the size of the user buffer + * on output it indicates the size of the memory. The driver can ignore + * length on input, the core code will handle everything. */ struct fwctl_ops { + enum fwctl_device_type device_type; size_t uctx_size; int (*open_uctx)(struct fwctl_uctx *uctx); void (*close_uctx)(struct fwctl_uctx *uctx); + void *(*info)(struct fwctl_uctx *uctx, size_t *length); }; /** diff --git a/include/uapi/fwctl/fwctl.h b/include/uapi/fwctl/fwctl.h index 0bdce95b6d69d9..39db9f09f8068e 100644 --- a/include/uapi/fwctl/fwctl.h +++ b/include/uapi/fwctl/fwctl.h @@ -36,6 +36,35 @@ */ enum { FWCTL_CMD_BASE = 0, + FWCTL_CMD_INFO = 0, + FWCTL_CMD_RPC = 1, }; +enum fwctl_device_type { + FWCTL_DEVICE_TYPE_ERROR = 0, +}; + +/** + * struct fwctl_info - ioctl(FWCTL_INFO) + * @size: sizeof(struct fwctl_info) + * @flags: Must be 0 + * @out_device_type: Returns the type of the device from enum fwctl_device_type + * @device_data_len: On input the length of the out_device_data memory. On + * output the size of the kernel's device_data which may be larger or + * smaller than the input. Maybe 0 on input. + * @out_device_data: Pointer to a memory of device_data_len bytes. Kernel will + * fill the entire memory, zeroing as required. + * + * Returns basic information about this fwctl instance, particularly what driver + * is being used to define the device_data format. + */ +struct fwctl_info { + __u32 size; + __u32 flags; + __u32 out_device_type; + __u32 device_data_len; + __aligned_u64 out_device_data; +}; +#define FWCTL_INFO _IO(FWCTL_TYPE, FWCTL_CMD_INFO) + #endif From patchwork Mon Jun 24 22:47:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710207 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2089.outbound.protection.outlook.com [40.107.94.89]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C21F1A254F; Mon, 24 Jun 2024 22:47:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.89 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269265; cv=fail; b=nfEwdsWUugJ0iOoYBusdZ7ULOUT+rwZSQ1v/R9LBP8ydThscOoagxFNtZ+ofS/Epv3yk/jVYGf/AzcZBOIUy3/vUn6ZlcngCSNTTU/BDld99SAxArkq8pCRceBPPqlFqewLv/oa83AReduTUgTTbfTfWeQyMCFHChW1OgI2oKaI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269265; c=relaxed/simple; bh=INwHB+8zHtT8++2o5KHq8P3RBx2L3oZmu6Ceo7xBjUE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=YbdMbqiWSNEFjhcowYw84jm6GCRR3R9nbCusWQCINMYCdSYfXmat6pSivJdeKGaKpDgjFa87nJ67f4LOMxbkJ+2qzvC3g8b+4CcjE6h7itKcJBwG3/NjBxb5REBiGedFFP8CatKoNBcleRxQAbb8TUxZUngmydEmj/IjV4upXJ8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=RaU8uSGX; arc=fail smtp.client-ip=40.107.94.89 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="RaU8uSGX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MZ+axd7F04x+a4nylIFBes30JLsvkqU+2Crv0POhZrw36WLb1lUrqcMRW7fPUyuSZeChF90xMMnRqVougJkCODUuMxnlPntotU/BB2mLmsN79EBvpcK9884nLNqvqB3u0RUUM6Lu9FBGBqwlZSiIg51Dd8BGa7xN4xyZf/TsmjTxWK5CnGHbshESqEm9xuDQI/5GC2pV/Oi0nZPEhftXFi+aG4KgGJ4zLwCWZbgPg1tqm/tjROKeA7FZIeztQE5HiUu69eG5cvSV+Bsfys9t5t+ThoMGi/BiisPuO8+7RLyTRAX4I871H1itoVbU9xkQ3Iyn8dTS9MKc/rL9xi5qag== 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=5EJ40bburIyBhpGzbZEasBszAf0xIC7aH42lNUBqrJE=; b=cGkZPsA7K1FIL+EYEffdv8iMC09vRUohBCCqkc72hQFRxsnMZLpMOEpfzL494Qnpqvgutao+IgfdVK4hUR4EMcwvPL0wFvHNpCo+/cmc/hzFtq3RM4e+m0Dpq6AFP1DlT17xNRN+tfDfxBWNdxlmh7aFRC/dABN3+TJ7EnBC4b8Jca/4hA3nQnJx/6v/3DBUKmHHftegGzw3cp7H347PjoFm0USGK9fw9U++6RQMpXkqBti3WDoR6N+lRKYIPkMVD0Labs3GNsxSIYdlfHVzqq/wtuHxcZiQe2fVoGcAVkhV8SHuwf21MQgs1c1iEFYP4dFX8PlinN0n5Ez+d1NBMw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5EJ40bburIyBhpGzbZEasBszAf0xIC7aH42lNUBqrJE=; b=RaU8uSGXvrCsUgnUkZtS3P4WmemA7LqxWl9nnYbUj/b8dqWNCnfnNMG/q/DhaMY9QOKn3x2l81cwCO2Q8nQh+Z1iKkSqXjvfq/fTgRV+S9KJkeyRNyPQ2z5fBFplFr44aclNuI/N9fwi8dwKMx8Aycjc/TPYfsGaICAdDe8ctooIsRKH7yNIhIt/KsM6uH6vYnTynxCOC2FO7g+zHdFIE1HsIgBwJUW4kMY75AmDHt+dkyJ5iECZiTAeVtH3jNj4zxSJpOdx22r5QQYkHpFbZKnKiPZ3E33YR+sVe71E1M6HyH7i9mCYkQgQSFGUBqlLX4mP0H0+vAzCnwjKK7hjhg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by DS7PR12MB6024.namprd12.prod.outlook.com (2603:10b6:8:84::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.30; Mon, 24 Jun 2024 22:47:40 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:39 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 4/8] taint: Add TAINT_FWCTL Date: Mon, 24 Jun 2024 19:47:28 -0300 Message-ID: <4-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0065.namprd05.prod.outlook.com (2603:10b6:208:236::34) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|DS7PR12MB6024:EE_ X-MS-Office365-Filtering-Correlation-Id: 5046168f-17e6-461b-0daf-08dc949fa412 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|1800799021|7416011|366013|921017; X-Microsoft-Antispam-Message-Info: F/7ELUV5AUv3RRj7b3ObmLKncdoPRkHEihmZgF4cfiZRscJAI95RKJC2HCGl0y7LbU7NorRgqZquQzoVAWVoib6kYdZj1tGxTWwCm5RAMI+a4w/4wJJEKWPBo636vCPxWgonAqNzUJXg9pcmCJI3xOE0S/eoVyvbmrAUeYijbOinlN6Xn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9yc5foacNHTJi6Ie3bck2dxJzOAAusnsSZ0abNXtgBWYIIhvEyQXXPZXAm1QP42nklpLB56Xv/QISbr/Fi0932F/GGkbV/xhZhUDZxsabhS73i+fJoojCmjbg= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(1800799021)(7416011)(366013)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3tk9XbElGYVJXiFFCkNXc4rNDgPyhbegIOVR9/XzoZDUTitoA8+OSrquG7sSBRs4AahpCs7nYh86pfFCQLA1B8UD2VlWKg9oA1bucrsyT1L8CscZMghzdHk6UEMGRwbVf0kSZaMjy/dP8BMkq8gtGsUqgl6F/3lKd8IxlnEA+fe8C/un/JcdzIO+N2FIp+WLp8VRVObtGmDkA4v7GihMJ8T/6bPMzAloVPq41mZbVJZFVCkmNNn/1KHvQAu/WPbOe/hfBJqPVtRuBl1kOzmmccE/izRNsDLPLKJ0c4PwC0RNVTjYn6TpYbBi3AA80F0ZlxjPagz6TXufJWQrsHmaa623grbsLBfk2A4Riui6YbMMEtvMmV1gwnpvAaW4WMJ8fcrogPLEiFaVeuWiSi2ScHDPwj881GNtQaGXtTzCXENRIYoUJb8ntDvTiY9KPSkPqjjAp7rj0OgfWhxIsqv3MGfyS/FYZzJ2Sa1yHnK/Xwm3MDij5YzHoGfLTHymE7122/wYdad4JWxin21WuQX+XXkzLiMYBNt0aecWrhDYIuoZwtaMjwOIKQahX+HirmgjD1AT/ClrTxzT0svxqh3gWgPCCJ+uIZGcu64GHE9T+2PjZs7dG+FaTMEGWtpU7IbD/ELfSNAYxSIMhblMhAjR8DmZ9fTPeINyUx5Of9GwY9+qEzbjQW7hTs4JEF4dSv5iKgUFHjeIvwSY2mbHLVYbqJ3ftQ5g90I3d6JtZddf34Wq71KCIpmjEqqvBBKJG78U92jZ7WE+MftJJFzTJPnOYuhYbHg8FJ5EYX/iUF3zF4HH9uihosGQ01gHbbj6o7gflCEtlR63/kNy6OOIy7Lrivkfbt43CNmQBVlnQ4qhiN84aO89laFBPZnMGNsdTl78pSJwZLGiSTdXZzq8HLr5EkkQK71SWUxfVuxD9Ur60G05757ZhTrrhy1y97uxJMwebwO5StL9z6CvUB/ehq+g80knhK07C8MY5SBnf4hi80T8fxwUzW0mAcKbEiDu/P+5sLh1Sj56nMr2axrF7vrTBLw7l3XnPbxkPp6uwa89hnm7P1ayFZ8Lp3mTGEXN/VgTyQqK4VblwUP7uYIEzxa+cU/XDcw5Y0pzo/ZKyBtaNmgQVSOBWxK6zmN8v/p9gv2m4wHv67LMU9P1x0IwmI584JTTLGpyYyPxUv48sLUw6sQdjkmx12ApYLEsot/Q473D3qiIpvTtp+qO4w58JsRPV//85L2+l4c1o2eC//V9+TwhV6R/Sxpxg5i8EQ4TFLIeJjN02rYLe1tErx/JgYjykqsayu1OEgyG0c7+LFHAQp78RF7IoV3PCUnUFcZ3F24m8KLHL5o+N21ERj/wfEi+Lrwjf0yQOBIYaUAYinUz48Js4yO4udcL1sRKi4LpKUvoLGt2yS6plGYMLedHs8hGvR3fd3PIm/8iQy19zwewx7nM5PuU8p27bcrvEeBzUMIq+e90NWjSCxJ7PNIcSn1aLSplZWpcMqTNvsm8P5B08GCM95Y1F6INZeXetVoZfMu0ngFDG8qbqUvoyMbkMluGRmtHo/ukmhNw/Rcx/SzKE3lSoscp58g+7SsZmiODlM9Q X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5046168f-17e6-461b-0daf-08dc949fa412 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:35.4593 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uoPo291rP/g9YpGQD0F2y3dcUPeCXdpq+RAL/5Y7O0oVnKJD29+WqXn6GURAYnqV X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6024 Requesting a fwctl scope of access that includes mutating device debug data will cause the kernel to be tainted. Changing the device operation through things in the debug scope may cause the device to malfunction in undefined ways. This should be reflected in the TAINT flags to help any debuggers understand that something has been done. Signed-off-by: Jason Gunthorpe --- Documentation/admin-guide/tainted-kernels.rst | 5 +++++ include/linux/panic.h | 3 ++- kernel/panic.c | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/tainted-kernels.rst b/Documentation/admin-guide/tainted-kernels.rst index f92551539e8a66..f91a54966a9719 100644 --- a/Documentation/admin-guide/tainted-kernels.rst +++ b/Documentation/admin-guide/tainted-kernels.rst @@ -101,6 +101,7 @@ Bit Log Number Reason that got the kernel tainted 16 _/X 65536 auxiliary taint, defined for and used by distros 17 _/T 131072 kernel was built with the struct randomization plugin 18 _/N 262144 an in-kernel test has been run + 19 _/J 524288 userspace used a mutating debug operation in fwctl === === ====== ======================================================== Note: The character ``_`` is representing a blank in this table to make reading @@ -182,3 +183,7 @@ More detailed explanation for tainting produce extremely unusual kernel structure layouts (even performance pathological ones), which is important to know when debugging. Set at build time. + + 18) ``J`` if userpace opened /dev/fwctl/* and performed a FWTCL_RPC_DEBUG_WRITE + to use the devices debugging features. Device debugging features could + cause the device to malfunction in undefined ways. diff --git a/include/linux/panic.h b/include/linux/panic.h index 6717b15e798c38..5dfd5295effd40 100644 --- a/include/linux/panic.h +++ b/include/linux/panic.h @@ -73,7 +73,8 @@ static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) #define TAINT_AUX 16 #define TAINT_RANDSTRUCT 17 #define TAINT_TEST 18 -#define TAINT_FLAGS_COUNT 19 +#define TAINT_FWCTL 19 +#define TAINT_FLAGS_COUNT 20 #define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) struct taint_flag { diff --git a/kernel/panic.c b/kernel/panic.c index 8bff183d6180e7..b71f573ec7c5fc 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -494,6 +494,7 @@ const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { [ TAINT_AUX ] = { 'X', ' ', true }, [ TAINT_RANDSTRUCT ] = { 'T', ' ', true }, [ TAINT_TEST ] = { 'N', ' ', true }, + [ TAINT_FWCTL ] = { 'J', ' ', true }, }; /** From patchwork Mon Jun 24 22:47:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710209 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 733D31A2556; Mon, 24 Jun 2024 22:47:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269266; cv=fail; b=fDwGBRC3sQtASnzQfEEFTyyLNJn6dJzK++EHdTC4bzX98QMhDX0XpielCPKJ7Ex1UlKWYbqNfs7d/EaU7IUwI19EehHR5/rxizUE1Mvlqghz864tQtmEShaQ9IfT8A4KmemNabws3eGdsH181x7FUgetZbamTUjH7U0uwsk2NB0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269266; c=relaxed/simple; bh=M+Sp6BgrRIack3VPn5wtmaTUXuPES3c0KPMUpzuQgnw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=dcMO+imFFS/hD0IEpSESBrM+XetKJajMkvpSc1mvQCEVnD4BDYUBuxNn/dysHb4fDrgnrNSNxeQgylQhRdtnruQlOVz+jPtshDgVBF5kd8I8kTkC8Oc55n8YUq5RmVo/3KX8JmXOsR802TX5fbN1YNf9WBRhLcHakCbaSO46RWE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=LJfHQwzk; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="LJfHQwzk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G9TNmJ/Yj9iNh3pnvJDeQU9ddLJBSrchtNix43DwAv/l/LzqxZ4IzgquBBf7jWc9Z+TkP7v3a4vr9RqaVeVHzwyaCAKSLORB+cL61a/HAsXb7VANm4Jn8HGrTrRYvxBZUnOazn9wspjDDGmPVUTFFQChMcCnzrYraLPcJyx95naDFl/PsaLcSWqB/Psr5X2w49wzUuzmEihxEFTwO3Ez+OO11wjPogBMGaeAVptx8SstT7Y/OKgg0S1bWkI2HARrK51e40HoAECN4FhSPj//qfVkfROzORG2sCGnExGr/5Q8cgNuisYHClSXoU1lcyF7YLXWrGGSXcuply/BL7e6QQ== 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=ahCe+PFiQm/4yjM3tCQ2bSPwGrCMGl0a4eA7ToK9U2M=; b=nBwmXMy4CIKzl5f7yaNRKVeh7aysNO39CQmK6GzfZKhgfTymC8vNi+lqVvlCxDkuirHP+hNFmptjDt9IwM/2+XtVO2WrK1SJ6Jb2EEflz4XmlqBbAuMESSKBFoKpOzOyGidneem0ZbB3m2wwunqQ1t15PcMn8/5pOWKjf3MeXUMqQ7ORsSF5TlRd9Q+C+8MNZ3/jjU6JdKOMW8g8u3M/hzs5A1UNMJBE3y0Q6MCMQKzonIFz3ebhp0BpKomUcBotEmE/kWSFlwvgpIhwiioYb8ceNAbH7hgyMUAxOPvZ1mGLVAfl7doyrhuR+DWLVPTrneJpyIvDxIaQDXgLHDywiw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ahCe+PFiQm/4yjM3tCQ2bSPwGrCMGl0a4eA7ToK9U2M=; b=LJfHQwzkrNGMSOhJIT+A+RoMXHprjwASO1rABMFy5jJl+26HwtTBavqg5f/XwdlAuJzjNIgraFU6YZn9QoTFxeaqbMD1Sy0+dTdEUCEmJh1ZIPbxtjs69syNxBrvN4QoptJNc9Wyu+mnW2eXScWe5xCBQhyVY5Jt9Biizz1hY18u2ViLnz26Efrm/ktI8EyE8fpFycUF0bN/OSscnb9hjYoodyDpu+AiTOwi+dgBR3dabk7qOZ2T4J67keSqhOK/kgiTL9Iw5Cr6Sh5PRi+iB5fIqBsuiPXJNU0v0UgutRliQnp5bmx/MbCwzzcv6mKyaGp6Bv3j/xVeQVA2LOzEdg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BY5PR12MB4147.namprd12.prod.outlook.com (2603:10b6:a03:205::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.29; Mon, 24 Jun 2024 22:47:37 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:37 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 5/8] fwctl: FWCTL_RPC to execute a Remote Procedure Call to device firmware Date: Mon, 24 Jun 2024 19:47:29 -0300 Message-ID: <5-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0012.namprd07.prod.outlook.com (2603:10b6:208:1a0::22) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BY5PR12MB4147:EE_ X-MS-Office365-Filtering-Correlation-Id: fa15bd64-3495-4fe9-1ffc-08dc949fa2fa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|366013|7416011|1800799021|921017; X-Microsoft-Antispam-Message-Info: Or43nQCznv+xqDd8sAMtdPKAb+y8DN8yI3Hu7lk5tlglLMRT5A26MgZ7tICnpFzQY8rT0vlkhhauyh7Er8teKvsIiTL50unrdfv+MNcXNYa5unAyA11x0klXaZl8XI+87G5Wp4Zp8+nAuPQ43qsWO2Hom85i6etAdX2sZA0ZrgA8WF/vgJr0eobmjf++/E4+6rrhguKEDCoVAUoAetYQd1xEwIuWIyD8zrfcSz0J1IiAgMAFkyY+K/jdJyvDPGFhDv6CbrpeZQpAsvIBTv0JROXldxN2I+NUBdbElXRmrXX8+L7R0V1NsKG3gY3yavpAVdVDKCoMZ94wiE6VXI2aFE7/ch+1PgDFvA2OfU+SPKzvuenqI/6rl7RuZXLOTyh2+sOBvs9bFVoMGdhZrG0Pokx0Hz2VVU2UeM19juV3vJYhH4NeGltMqGanKqcG9iyZhi3u/k779Cer0scez8/Q7JyRYdZcmOiIM9vYSmT8YTcu32xK30wx9maa0MBQAz/Si5Np3ltnFA0P50fcSr/8meCpSi28jYVGoKkmclA9Vs3GG/xhjqSI7ierm8VnCpiPdO38gpDkyCjGJn+Y5Eh+gtBrJIWUX3SQcviNBEf9TkKo/BSZJtF2Oh3hZXWUPk+ZlCDF5zK3JzphpNZnu8QM+MBloISlc1MdtlmUO5+0UID4gon/M3UKeOXwKcwoTYwh0SKStT0g7eo8UyJ0ejRTumBPxHLDnWv/1pxci5ertDiXXAuwgzqmmIgHb14c4Udt2STlqvioi0DTdco51lWCmhtpdVZZU++IIr3YfovU4AIhaTg7cf7aItbuaKldc8SjciNhbJunO3rGWCeiGzoDxZaZgHOT61YRjGI080rihDvhhii1JIWyi2P+MgSTaPdRHnJnqt9jgoSxd6cE9gDGDuIlJ3WG36RLtGk1mhfRcNAZlN5vxR9epOBhLSgq2yTF68Ipwp13xsmfaOqni0xbG7KOopEKBR6xVmkbiP5KxmwNPXV5a7yxq3dMzgGSj1Z5TkTALjILhYyu5JOYnbxfEnvRmSKKGMolsTdRJsjXPxgr6zYw6mW2SXDdpBaVB87ZQXa+4hWPg/98kdv0mwssmC6YIA7qVG6zDnLbC8IhRq/skvfKnQg6rJsy1i/ogVzWtYYP8jZ5Bsfw6qvY3C0OdXun8K16DwSVTqw4AsJcHKS2wJNBem+1OAHchlgptF+RypeG9g5+WJ4BkcdO811AQ44WQS9AA3KNycNcybDJ3P2oIh86tD6OthmtWlDb1y4qLM55ZfFmHFlR81etslUGGaZFSQoXobCy6KwMmb71c6/zb1dUvAIcKqKvd6NvtLauKFaEusaDAQ/8+IVDjME5/eNEnoQRiAUeu45Lixo6Cr8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(366013)(7416011)(1800799021)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: zrQjO1wx6SWXScm46wXppHFkBxhsCVxBgaiTRWeStsUoi0I8pnu9UgeWnh9WeHvs8mi83XiDvXkcZ9mG1KWjgLJcf/svVkAUZ+Jbdks7dR2FmvVbW4msCe5839DtiLFSzwai1LO2mcnmFs/50oHI0ScsJG7wIt0sLtOetT/uiC3qZ1dUb2mdLQE4ygjhcTi/QDMIqqhh4h4UlHpj7uZBlSAPgesA0t7HrWEL4B2nq+xGJ/33SVQ8CyjGXTR34dSx1UeWVQhQLg9dXTKHtILnlj7pKGMBIAkkLqQ5Ug4jyhtkiZMSrgsWq6ELnTaE/Gchqz9sTaOROotsRab3ovXjYDBOfwqBP3vTfNjaEGCz44EXQtMGyhpOir5K/Wt0quqOdoFPMZZzvHkfzGfRE5aadNY2NFVgkUQCWsL+1ding+bUXuulN52L3VdwM+N3WNvIDJhmRTdAEfNshE+8bpNxnH5aARnS3+oEz7n/kFtV97bKs471y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1uIFsCLUG4VI73CPW X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fa15bd64-3495-4fe9-1ffc-08dc949fa2fa X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:33.7629 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Xhd8pyVmhCnEcwQmRGA2vzWT4J1JBRmKa3zvHEvxy2+saHzBQ0ZUbPeo+za7rW1H X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4147 Add the FWCTL_RPC ioctl which allows a request/response RPC call to device firmware. Drivers implementing this call must follow the security guidelines under Documentation/userspace-api/fwctl.rst The core code provides some memory management helpers to get the messages copied from and back to userspace. The driver is responsible for allocating the output message memory and delivering the message to the device. Signed-off-by: Jason Gunthorpe --- drivers/fwctl/main.c | 62 +++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 5 +++ include/uapi/fwctl/fwctl.h | 66 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c index f1dec0b590aee4..9506b993a1a56d 100644 --- a/drivers/fwctl/main.c +++ b/drivers/fwctl/main.c @@ -8,16 +8,20 @@ #include #include #include +#include #include enum { FWCTL_MAX_DEVICES = 256, + MAX_RPC_LEN = SZ_2M, }; static dev_t fwctl_dev; static DEFINE_IDA(fwctl_ida); +static unsigned long fwctl_tainted; DEFINE_FREE(kfree_errptr, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T)); +DEFINE_FREE(kvfree_errptr, void *, if (!IS_ERR_OR_NULL(_T)) kvfree(_T)); struct fwctl_ucmd { struct fwctl_uctx *uctx; @@ -75,9 +79,66 @@ static int fwctl_cmd_info(struct fwctl_ucmd *ucmd) return ucmd_respond(ucmd, sizeof(*cmd)); } +static int fwctl_cmd_rpc(struct fwctl_ucmd *ucmd) +{ + struct fwctl_device *fwctl = ucmd->uctx->fwctl; + struct fwctl_rpc *cmd = ucmd->cmd; + size_t out_len; + + if (cmd->in_len > MAX_RPC_LEN || cmd->out_len > MAX_RPC_LEN) + return -EMSGSIZE; + + switch (cmd->scope) { + case FWCTL_RPC_CONFIGURATION: + case FWCTL_RPC_DEBUG_READ_ONLY: + break; + + case FWCTL_RPC_DEBUG_WRITE_FULL: + if (!capable(CAP_SYS_RAWIO)) + return -EPERM; + fallthrough; + case FWCTL_RPC_DEBUG_WRITE: + if (!test_and_set_bit(0, &fwctl_tainted)) { + dev_warn( + &fwctl->dev, + "%s(%d): has requested full access to the physical device device", + current->comm, task_pid_nr(current)); + add_taint(TAINT_FWCTL, LOCKDEP_STILL_OK); + } + break; + default: + return -EOPNOTSUPP; + }; + + void *inbuf __free(kvfree) = + kvzalloc(cmd->in_len, GFP_KERNEL | GFP_KERNEL_ACCOUNT); + if (!inbuf) + return -ENOMEM; + if (copy_from_user(inbuf, u64_to_user_ptr(cmd->in), cmd->in_len)) + return -EFAULT; + + out_len = cmd->out_len; + void *outbuf __free(kvfree_errptr) = fwctl->ops->fw_rpc( + ucmd->uctx, cmd->scope, inbuf, cmd->in_len, &out_len); + if (IS_ERR(outbuf)) + return PTR_ERR(outbuf); + if (outbuf == inbuf) { + /* The driver can re-use inbuf as outbuf */ + inbuf = NULL; + } + + if (copy_to_user(u64_to_user_ptr(cmd->out), outbuf, + min(cmd->out_len, out_len))) + return -EFAULT; + + cmd->out_len = out_len; + return ucmd_respond(ucmd, sizeof(*cmd)); +} + /* On stack memory for the ioctl structs */ union ucmd_buffer { struct fwctl_info info; + struct fwctl_rpc rpc; }; struct fwctl_ioctl_op { @@ -98,6 +159,7 @@ struct fwctl_ioctl_op { } static const struct fwctl_ioctl_op fwctl_ioctl_ops[] = { IOCTL_OP(FWCTL_INFO, fwctl_cmd_info, struct fwctl_info, out_device_data), + IOCTL_OP(FWCTL_RPC, fwctl_cmd_rpc, struct fwctl_rpc, out), }; static long fwctl_fops_ioctl(struct file *filp, unsigned int cmd, diff --git a/include/linux/fwctl.h b/include/linux/fwctl.h index 9a906b861acf3a..294cfbf63306a2 100644 --- a/include/linux/fwctl.h +++ b/include/linux/fwctl.h @@ -26,6 +26,9 @@ struct fwctl_uctx; * out_device_data. On input length indicates the size of the user buffer * on output it indicates the size of the memory. The driver can ignore * length on input, the core code will handle everything. + * @fw_rpc: Implement FWCTL_RPC. Deliver rpc_in/in_len to the FW and return + * the response and set out_len. rpc_in can be returned as the response + * pointer. Otherwise the returned pointer is freed with kvfree(). */ struct fwctl_ops { enum fwctl_device_type device_type; @@ -33,6 +36,8 @@ struct fwctl_ops { int (*open_uctx)(struct fwctl_uctx *uctx); void (*close_uctx)(struct fwctl_uctx *uctx); void *(*info)(struct fwctl_uctx *uctx, size_t *length); + void *(*fw_rpc)(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope, + void *rpc_in, size_t in_len, size_t *out_len); }; /** diff --git a/include/uapi/fwctl/fwctl.h b/include/uapi/fwctl/fwctl.h index 39db9f09f8068e..8bde0d4416fd55 100644 --- a/include/uapi/fwctl/fwctl.h +++ b/include/uapi/fwctl/fwctl.h @@ -67,4 +67,70 @@ struct fwctl_info { }; #define FWCTL_INFO _IO(FWCTL_TYPE, FWCTL_CMD_INFO) +/** + * enum fwctl_rpc_scope - Scope of access for the RPC + */ +enum fwctl_rpc_scope { + /** + * @FWCTL_RPC_CONFIGURATION: Device configuration access scope + * + * Read/write access to device configuration. When configuration + * is written to the device remains in a fully supported state. + */ + FWCTL_RPC_CONFIGURATION = 0, + /** + * @FWCTL_RPC_DEBUG_READ_ONLY: Read only access to debug information + * + * Readable debug information. Debug information is compatible with + * kernel lockdown, and does not disclose any sensitive information. For + * instance exposing any encryption secrets from this information is + * forbidden. + */ + FWCTL_RPC_DEBUG_READ_ONLY = 1, + /** + * @FWCTL_RPC_DEBUG_WRITE: Writable access to lockdown compatible debug information + * + * Allows write access to data in the device which may leave a fully + * supported state. This is intended to permit intensive and possibly + * invasive debugging. This scope will taint the kernel. + */ + FWCTL_RPC_DEBUG_WRITE = 2, + /** + * @FWCTL_RPC_DEBUG_WRITE_FULL: Writable access to all debug information + * + * Allows read/write access to everything. Requires CAP_SYS_RAW_IO, so + * it is not required to follow lockdown principals. If in doubt + * debugging should be placed in this scope. This scope will taint the + * kernel. + */ + FWCTL_RPC_DEBUG_WRITE_FULL = 3, +}; + +/** + * struct fwctl_rpc - ioctl(FWCTL_RPC) + * @size: sizeof(struct fwctl_rpc) + * @scope: One of enum fwctl_rpc_scope, required scope for the RPC + * @in_len: Length of the in memory + * @out_len: Length of the out memory + * @in: Request message in device specific format + * @out: Response message in device specific format + * + * Deliver a Remote Procedure Call to the device FW and return the response. The + * call's parameters and return are marshaled into linear buffers of memory. Any + * errno indicates that delivery of the RPC to the device failed. Return status + * originating in the device during a successful delivery must be encoded into + * out. + * + * The format of the buffers matches the out_device_type from FWCTL_INFO. + */ +struct fwctl_rpc { + __u32 size; + __u32 scope; + __u32 in_len; + __u32 out_len; + __aligned_u64 in; + __aligned_u64 out; +}; +#define FWCTL_RPC _IO(FWCTL_TYPE, FWCTL_CMD_RPC) + #endif From patchwork Mon Jun 24 22:47:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710211 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BE4F1A2C20; Mon, 24 Jun 2024 22:47:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269268; cv=fail; b=EgMBnbMq5vF3cbVF99rEjvFYskViBd1uc6aIO49QDBSVAg23uM11xXb4nBa3ut1RIiXqRX/51WilnUDY/abkVqrCvbBeSS2Tl2//n+njJo4b9/NacF3R6TxOX5rgN+CnfA9pY8c5L8UY05y2WS9mNGYJPwUAw3BjaP8KZYcF1eI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269268; c=relaxed/simple; bh=IeoLhhb+zoCxeiiOKUdFjzyHuZEVNC+Oq8b2bHfPr6E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=T8UHu7IYfLdO4A635ZK7vQQL+DCZRF9dlP2X5rFQEjdM7ykLWuRkPtXQLR4TO3NkpsFleSFNaNtE61OkUaUvJXZTw7qTczCjmFZBhW8mRBYQQztczr0nrNqB15HY4arieoU8SubIrL8QYjFhcCYVc1gLdan+FtVpGDR2vIt2a4A= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=oXhgEVaL; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="oXhgEVaL" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FFymqRFUATqhmK6AA90H3SMcDCjrAvIkbaPGByXHEblruMI16DBQhrOodCAE1NDj7UJV275givfqoZDH4u+bF+W617Zc7ue0unvhmU0bp5/LPDb4K4Eq92eWzFDCYaaIXSfd5h15WRVQs/pqzIo7ASHmHHki71+tHWz023auq8D+ZynI//2H6zB2WAuxSl6nHRwqwWFglUCx8Ma8yTUTRMKP54tQWIUI1SKhP0yP76xIH8yMXbGnZLy9VhOScIyfR/mBSEyK4sBxsIVS5EppsGY8JxLscERjB9a+0b+Seqx0LFLuqrjhcNJlDyA7Hzheyq7aXw/MpKXQ0KKhXU08kg== 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=QsIA2qdFHiTn26g0sQU23Rb8YT422wDFfTLZZk4P7cg=; b=FzTmGA1iksmCF6zxDKAGUY/+1MyGFQVilv4mTV7+u4elXHb/OkJLajFZRGkbyNvPpGpLizPHZq9xjAuyERvpWR9reMk+MRGi5ar25MEG7X7Ar1AnLXrvrkl0/VFIRhEtQfyFK7CIJgIhin0T7GSwuRf7cehQw9oaiVvRb5aVTY7Pvbk9XyWstQiieWIYP8TJIiOak8JmH+D2eMlGF/4u3E6gD+4oFjO2Mshvs5oDxmMZ9xRktBJTncAR95rR66m/8vT2Gn7/CaHLSsqa6MfqXbc/JktB0GxhNs3LL8lVl8JwvQ1HZCSG+xrJsTC6g4rJ3P+ZppVDqQl76+fem9sjFA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QsIA2qdFHiTn26g0sQU23Rb8YT422wDFfTLZZk4P7cg=; b=oXhgEVaLsjyv1RpYLFODe6V14iJXvaN6NEZG1Cl5eNV1il1+j7iGP61K22h2an4XrapXsT4I00rZn9AAeRODjwpLPWIuoiRBSEF3fvZd0Z8JrAkDb9ndWMa9h1mBkcznmTS/6NFkf4PJjHVD9rBHMTvMRF6i1NKNY1bBZ4LnxRNyaz9RLbhKiUn3mhvZ9oB42FzuJb97gyaOBQkjBqynFTxqtbCvBQbZzD5UZn9ORpC9MlIYic99UM5CHs93fw7eQweYeFT+ug4hDPTfTu2lUkHuvqWzKSjOIaK32YfhWMYg01d1g55aEcA6eJXMQc85KbZsDg87/BcWfxIHrhWM9A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BY5PR12MB4147.namprd12.prod.outlook.com (2603:10b6:a03:205::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.29; Mon, 24 Jun 2024 22:47:38 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:38 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 6/8] fwctl: Add documentation Date: Mon, 24 Jun 2024 19:47:30 -0300 Message-ID: <6-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0302.namprd13.prod.outlook.com (2603:10b6:208:2c1::7) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BY5PR12MB4147:EE_ X-MS-Office365-Filtering-Correlation-Id: f945b807-24fa-47aa-3b53-08dc949fa334 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|366013|7416011|1800799021|921017; X-Microsoft-Antispam-Message-Info: =?utf-8?q?izA3T9tCghLWRcOXee19G0Elh2MtPau?= =?utf-8?q?pRCivdL2AKI9+9ojqLfvfUBIrU3C70PvYq7VjYtNdiI2HaH5GbiMJi4j8ss89ym/e?= =?utf-8?q?U7z0mlRDOD5RdqbAP7VrB9Gx9rfPsTxCXrczk1hHdrX5yod0iETDj41QV+BGc4vwl?= =?utf-8?q?ky6JJqJSHj+xjNVhX7eMrSJ8ZsOjzVR8KL/CyJ5T04rpeHFQNPYjb6paxRjSc1zYF?= =?utf-8?q?4IJ4cbiada5tg8Ueu+DVuzMuS71yN2SYJMHCPAnC+XnMyRR8nSy5oP0SMP06nnYLU?= =?utf-8?q?mb54hm4NPcDIGNDQgDA9WAex4gfhH2rcpSdZrY54edKz/0PcSrKtjiBvJ7J6rjDfC?= =?utf-8?q?7JeZJBArN3x34bhy+iQ/yjJbRkf5fy9eZyKdS7uuYr7iJ8qLuY02SPvNq3XpMtrbz?= =?utf-8?q?cj5ogIO/o9QZpKP05AdtoVObb0EXK8pkrznlPtqnPgWTtBUlaZW/VT+qIMZupsxdZ?= =?utf-8?q?SmuPMSlsIa7aiOTy2dKAv2YlGyMISZm3MQTG+vAWMJ2LTvPrXdgrtFa1A3x5FFWu8?= =?ut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od.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(366013)(7416011)(1800799021)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?/mh8AKFvlgafEUifP/1vFbR68cer?= =?utf-8?q?POHm6JxVNGpRqzaumdHkKSPde6HGg+7wbxZdyw4y4NQjm0TZzNNZyfDs8Gk4rSgE9?= =?utf-8?q?fT2536kU3wHvAnxWI2KUFGcOFPgNuQ+HxtSPUah9PRPoKDwH9RofJ214I+SFqUjwB?= =?utf-8?q?JBm9jagA4HkIXw94MJbHH+LOMdradhGvziJ5TsPlTv3gvIrmfv1TbYA0vMVR1Nxbv?= =?utf-8?q?JIWFlIYzDwAAb/mdCWbXaxgOi9FYsu8akeu+/Z9A/8E+K9KNM4pNpCw2kulZTs0vF?= =?utf-8?q?EN7JkEcCJ1bocy9sZloLMD0jwouFNKZqc/juZEjSW1ihYXKZcD3m5PO9u3AoWvugZ?= =?utf-8?q?Aw73P9RlReV07xPbYZLdFa/ffLvYXq7SsLewKNRRgYJgyguY++7xkUkaW7l6q158j?= =?utf-8?q?bwUhH1EIScuTimJNmlbl4diQqZDQ0UUvV8hIakSWrAuJ6NqDQ0en/tcDIeBfiQTcc?= =?utf-8?q?ILtKkp5ca773lNwybbTMGJqLvDqRYRPdb47FW/xM2eJ09U8PtF/1RJ9Fja4rnhIxw?= =?utf-8?q?r4nY3Lg4CN7gzCmjjgKF3O8ZYqR9O9j+wWhG+ItDKJURv5/htX8IqSzIcdc0Gz63z?= =?utf-8?q?fmS+VcQn0IKvFdDZ+RNAbbYJWv71uZAnZY8Wad/RYIgreZOBiYA4ocOlvJUHFERp2?= =?utf-8?q?q7DeSeBjYoAXEl5c/476NiRFZCNkbccOA3IMjXAmn303r7UpmanNuJO8BZ7WmAWWZ?= =?utf-8?q?uHjyaBTz5xdJtYl50pxZWoWdD1THaC8fGssVdV7UH7eu6MJnsLW6XO2EJSbqyOM1B?= =?utf-8?q?T2UQ74+hkorP+2wsFTq1YjBQyCuqD2Okz6BFWmRLnPbIxV4LpjW2512+bQYslbyAo?= =?utf-8?q?zNGCtfZmMNtxxYHVOoiISq9WuEXVKcb0+329jx+6lfbWwnVEaiQetJwG9f41ZLukm?= =?utf-8?q?vGti1ziffX8BRG+gKYkxqxJrK/yAuHwtM/MpHOrA62KUOOBS6VBl6srZr3Ih3evCi?= =?utf-8?q?Q/9U7uDh65yl9LufDCTtw+YPV1loqbnFcuzrBDDu/c5VrR+BCVd4PRQ6eq/UXixmt?= =?utf-8?q?S/SxbzAcgt58eFxl9WZ94yP4N4Ma11umk7ClD1k6MEIevUjAwNyMiCXRMVpG6tRSP?= =?utf-8?q?MWwsInKmCkZtUgKEYSNG7u6oejZf+it9V3ymuEihr9H7vdlwjzUlcUYls6EgYxYLT?= =?utf-8?q?1dSonchM94OyE1B/jdCY/MWOM14WZutRbvtAC+HURRXpU9/FMx1NPNnYHt7Y2u7P6?= =?utf-8?q?G0jYOn7K+5lN376IGwNXvVEQFy+BkJGGEe3Sw9tCWnfRiFL5Vj3MnMPyMmpUJkZOb?= =?utf-8?q?TFXyxtp0YJlIwnZUoTIxjgcYtRPPm8McAnacCIUxgAkbqKBVFwwqLohVw/r9S2nJa?= =?utf-8?q?R9ixtgr3r+gvSGfHPLD8c9xe5xGUvwhfsCMLxkZQ4J+kD1bYR0ao7s9LyBU32r2Ho?= =?utf-8?q?U1C1DnuOEYEjeu5NrFHxLNgDBBRB/57U8qouV45zYl3qUxAf4Xm/Mg1guwSHt9UUs?= =?utf-8?q?zlqLGrixW7E0od4UnFrcqz9Hc5iZ5S0zuwEgxvoLXHz6sqOyGjxBGVZ0XYjVXeICr?= =?utf-8?q?UGZ1ZwICJk0K?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f945b807-24fa-47aa-3b53-08dc949fa334 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:34.1735 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: W+B6lyDDhRwwB9G+EPv5n4u+PXIz72/khjStsld+NT3Vl+TC/NqLsSxjw9yLxMXj X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4147 Document the purpose and rules for the fwctl subsystem. Link in kdocs to the doc tree. Nacked-by: Jakub Kicinski Link: https://lore.kernel.org/r/20240603114250.5325279c@kernel.org Signed-off-by: Jason Gunthorpe --- Documentation/userspace-api/fwctl.rst | 269 ++++++++++++++++++++++++++ Documentation/userspace-api/index.rst | 1 + 2 files changed, 270 insertions(+) create mode 100644 Documentation/userspace-api/fwctl.rst diff --git a/Documentation/userspace-api/fwctl.rst b/Documentation/userspace-api/fwctl.rst new file mode 100644 index 00000000000000..ece2db2530502f --- /dev/null +++ b/Documentation/userspace-api/fwctl.rst @@ -0,0 +1,269 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============== +fwctl subsystem +=============== + +:Author: Jason Gunthorpe + +Overview +======== + +Modern devices contain extensive amounts of FW, and in many cases, are largely +software-defined pieces of hardware. The evolution of this approach is largely a +reaction to Moore's Law where a chip tape out is now highly expensive, and the +chip design is extremely large. Replacing fixed HW logic with a flexible and +tightly coupled FW/HW combination is an effective risk mitigation against chip +respin. Problems in the HW design can be counteracted in device FW. This is +especially true for devices which present a stable and backwards compatible +interface to the operating system driver (such as NVMe). + +The FW layer in devices has grown to incredible sizes and devices frequently +integrate clusters of fast processors to run it. For example, mlx5 devices have +over 30MB of FW code, and big configurations operate with over 1GB of FW managed +runtime state. + +The availability of such a flexible layer has created quite a variety in the +industry where single pieces of silicon are now configurable software-defined +devices and can operate in substantially different ways depending on the need. +Further, we often see cases where specific sites wish to operate devices in ways +that are highly specialized and require applications that have been tailored to +their unique configuration. + +Further, devices have become multi-functional and integrated to the point they +no longer fit neatly into the kernel's division of subsystems. Modern +multi-functional devices have drivers, such as bnxt/ice/mlx5/pds, that span many +subsystems while sharing the underlying hardware using the auxiliary device +system. + +All together this creates a challenge for the operating system, where devices +have an expansive FW environment that needs robust device-specific debugging +support, and FW-driven functionality that is not well suited to “generic” +interfaces. fwctl seeks to allow access to the full device functionality from +user space in the areas of debuggability, management, and first-boot/nth-boot +provisioning. + +fwctl is aimed at the common device design pattern where the OS and FW +communicate via an RPC message layer constructed with a queue or mailbox scheme. +In this case the driver will typically have some layer to deliver RPC messages +and collect RPC responses from device FW. The in-kernel subsystem drivers that +operate the device for its primary purposes will use these RPCs to build their +drivers, but devices also usually have a set of ancillary RPCs that don't really +fit into any specific subsystem. For example, a HW RAID controller is primarily +operated by the block layer but also comes with a set of RPCs to administer the +construction of drives within the HW RAID. + +In the past when devices were more single function, individual subsystems would +grow different approaches to solving some of these common problems. For instance +monitoring device health, manipulating its FLASH, debugging the FW, +provisioning, all have various unique interfaces across the kernel. + +fwctl's purpose is to define a common set of limited rules, described below, +that allow user space to securely construct and execute RPCs inside device FW. +The rules serve as an agreement between the operating system and FW on how to +correctly design the RPC interface. As a uAPI the subsystem provides a thin +layer of discovery and a generic uAPI to deliver the RPCs and collect the +response. It supports a system of user space libraries and tools which will +use this interface to control the device using the device native protocols. + +Scope of Action +--------------- + +fwctl drivers are strictly restricted to being a way to operate the device FW. +It is not an avenue to access random kernel internals, or other operating system +SW states. + +fwctl instances must operate on a well-defined device function, and the device +should have a well-defined security model for what scope within the physical +device the function is permitted to access. For instance, the most complex PCIe +device today may broadly have several function-level scopes: + + 1. A privileged function with full access to the on-device global state and + configuration + + 2. Multiple hypervisor functions with control over itself and child functions + used with VMs + + 3. Multiple VM functions tightly scoped within the VM + +The device may create a logical parent/child relationship between these scopes. +For instance a child VM's FW may be within the scope of the hypervisor FW. It is +quite common in the VFIO world that the hypervisor environment has a complex +provisioning/profiling/configuration responsibility for the function VFIO +assigns to the VM. + +Further, within the function, devices often have RPC commands that fall within +some general scopes of action: + + 1. Access to function & child configuration, FLASH, etc/ that becomes live at a + function reset. + + 2. Access to function & child runtime configuration that kernel drivers can + discover at runtime. + + 3. Read only access to function debug information that may report on FW objects + in the function & child, including FW objects owned by other kernel + subsystems. + + 4. Write access to function & child debug information strictly compatible with + the principles of kernel lockdown and kernel integrity protection. Triggers + a kernel Taint. + + 5. Full debug device access. Triggers a kernel Taint, requires CAP_SYS_RAWIO. + +Userspace will provide a scope label on each RPC and the kernel must enforce the +above CAP's and taints based on that scope. A combination of kernel and FW can +enforce that RPCs are placed in the correct scope by userspace. + +Denied behavior +--------------- + +There are many things this interface must not allow user space to do (without a +Taint or CAP), broadly derived from the principles of kernel lockdown. Some +examples: + + 1. DMA to/from arbitrary memory, hang the system, run code in the device, or + otherwise compromise device or system security and integrity. + + 2. Provide an abnormal “back door” to kernel drivers. No manipulation of kernel + objects owned by kernel drivers. + + 3. Directly configure or otherwise control kernel drivers. A subsystem kernel + driver can react to the device configuration at function reset/driver load + time, but otherwise should not be coupled to fwctl. + + 4. Operate the HW in a way that overlaps with the core purpose of another + primary kernel subsystem, such as read/write to LBAs, send/receive of + network packets, or operate an accelerator's data plane. + +fwctl is not a replacement for device direct access subsystems like uacce or +VFIO. + +fwctl User API +============== + +.. kernel-doc:: include/uapi/fwctl/fwctl.h +.. kernel-doc:: include/uapi/fwctl/mlx5.h + +sysfs Class +----------- + +fwctl has a sysfs class (/sys/class/fwctl/fwctlNN/) and character devices +(/dev/fwctl/fwctlNN) with a simple numbered scheme. The character device +operates the iotcl uAPI described above. + +fwctl devices can be related to driver components in other subsystems through +sysfs:: + + $ ls /sys/class/fwctl/fwctl0/device/infiniband/ + ibp0s10f0 + + $ ls /sys/class/infiniband/ibp0s10f0/device/fwctl/ + fwctl0/ + + $ ls /sys/devices/pci0000:00/0000:00:0a.0/fwctl/fwctl0 + dev device power subsystem uevent + +User space Community +-------------------- + +Drawing inspiration from nvme-cli, participating in the kernel side must come +with a user space in a common TBD git tree, at a minimum to usefully operate the +kernel driver. Providing such an implementation is a pre-condition to merging a +kernel driver. + +The goal is to build user space community around some of the shared problems +we all have, and ideally develop some common user space programs with some +starting themes of: + + - Device in-field debugging + + - HW provisioning + + - VFIO child device profiling before VM boot + + - Confidential Compute topics (attestation, secure provisioning) + +That stretch across all subsystems in the kernel. fwupd is a great example of +how an excellent user space experience can emerge out of kernel-side diversity. + +fwctl Kernel API +================ + +.. kernel-doc:: drivers/fwctl/main.c + :export: +.. kernel-doc:: include/linux/fwctl.h + +fwctl Driver design +------------------- + +In many cases a fwctl driver is going to be part of a larger cross-subsystem +device possibly using the auxiliary_device mechanism. In that case several +subsystems are going to be sharing the same device and FW interface layer so the +device design must already provide for isolation and cooperation between kernel +subsystems. fwctl should fit into that same model. + +Part of the driver should include a description of how its scope restrictions +and security model work. The driver and FW together must ensure that RPCs +provided by user space are mapped to the appropriate scope. If the validation is +done in the driver then the validation can read a 'command effects' report from +the device, or hardwire the enforcement. If the validation is done in the FW, +then the driver should pass the fwctl_rpc_scope to the FW along with the command. + +The driver and FW must cooperate to ensure that either fwctl cannot allocate +any FW resources, or any resources it does allocate are freed on FD closure. A +driver primarily constructed around FW RPCs may find that its core PCI function +and RPC layer belongs under fwctl with auxiliary devices connecting to other +subsystems. + +Each device type must represent a stable FW ABI, such that the userspace +components have the same general stability we expect from the kernel. FW upgrade +should not break the userspace tools. + +Security Response +================= + +The kernel remains the gatekeeper for this interface. If violations of the +scopes, security or isolation principles are found, we have options to let +devices fix them with a FW update, push a kernel patch to parse and block RPC +commands or push a kernel patch to block entire firmware versions/devices. + +While the kernel can always directly parse and restrict RPCs, it is expected +that the existing kernel pattern of allowing drivers to delegate validation to +FW to be a useful design. + +Existing Similar Examples +========================= + +The approach described in this document is not a new idea. Direct, or near +direct device access has been offered by the kernel in different areas for +decades. With more devices wanting to follow this design pattern it is becoming +clear that it is not entirely well understood and, more importantly, the +security considerations are not well defined or agreed upon. + +Some examples: + + - HW RAID controllers. This includes RPCs to do things like compose drives into + a RAID volume, configure RAID parameters, monitor the HW and more. + + - Baseboard managers. RPCs for configuring settings in the device and more + + - NVMe vendor command capsules. nvme-cli provides access to some monitoring + functions that different products have defined, but more exists. + + - CXL also has a NVMe-like vendor command system. + + - DRM allows user space drivers to send commands to the device via kernel + mediation + + - RDMA allows user space drivers to directly push commands to the device + without kernel involvement + + - Various “raw” APIs, raw HID (SDL2), raw USB, NVMe Generic Interface, etc + +The first 4 are examples of areas that fwctl intends to cover. + +Some key lessons learned from these past efforts are the importance of having a +common user space project to use as a pre-condition for obtaining a kernel +driver. Developing good community around useful software in user space is key to +getting companies to fund participation to enable their products. diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst index 8a251d71fa6e14..990b4c0710c99e 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -44,6 +44,7 @@ Devices and I/O accelerators/ocxl dma-buf-alloc-exchange + fwctl gpio/index iommu iommufd From patchwork Mon Jun 24 22:47:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710208 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2060.outbound.protection.outlook.com [40.107.237.60]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01E171A2540; Mon, 24 Jun 2024 22:47:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269266; cv=fail; b=RHWf9qHy3v/83TCyB5Gyq1SRyMc13/HmdRGm7gaUpHi+YaPhAnKGr1lDGNid6UMJ85PDhtpAAJhMvFxMNix3Ybbn2filrNoQ8b0MieKOXYst7n/aAdEKPhzlMb/roNOT2XVc0AQKsUBcUzlVQdbM08XoZLrm0emjA5qPdHzy6dI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269266; c=relaxed/simple; bh=XCeF/2caKxUj6vXzDDb46aXwynhWaqaQ8Fm/UsBqgbU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=TH1Z0G99oDDXkGxAlzjR+8dMnjolZCnzSgzTeIUNNWnIe/GespbKJB3ctQOzJhvstU0gOfserv/SGUCOhK1z1DMaTPbHXE4TQpJZEv/vvmzMGqUQ2VauxKTgJBJYB7p9RnSpwrD1csVbh/TbkMh3y/fZONRUpnjt5SW3ouZ7EVQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Uh5xYb9V; arc=fail smtp.client-ip=40.107.237.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Uh5xYb9V" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QuvwcqEJj6+YswPRiaLFu1GHEx9SBMj8/BthmHzIufVx25Q4BF6DgAmHnt4gCzK+iRQf3Om53DnNx2ouN00kgW0gvqVbiiY5NrMLKraEvR5XuaONiC5eQ495vQ13rjXjBlnsC6QPRImcT/L6zA+tjD8KNxOnB/qzTka6Pb9iLXSyyRoKm3A8rqsJ49hyp78971SznySEk3VxaeWTGFbG6pI24bWlj3k/Y+f/l5jPATokeKluA4G0u2nxbK4U/t0Tl/UZth6jOuBdiCRGBD6xMTh1gQe6ySJ+AdLMYjo0LJXS0hZgickhWo1DfnAKHzp6sviE8TwaVhvFWINDkdcqxQ== 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=GsaVyhrn2fpluRJdINOcBUH/9hti28c5b2oPJwmXZyk=; b=j+ksN6eOZJPN1uPewuf5lw6yv7NZw3wBNjTaEtfThpJlYtTrTS3HShF883gAlWyMNjfMEOIx2oWVUza3gLacL8yhM8VIjN/7H9d1X23MUENDHnDKNF8xalB6nTrWw7E77wlqXspr/HuMazTJbT1iFA5qLq1IZvUvninEbxdlMCcGLrU3Su7mQT0k95cCfgRuCWzpF3pkBsyjO+OAYO+Kmd85dQAZPZG+3AulEIagegFYKsCBXFzpe8frySqlF2QzHlJ9IqHaLj8c6zz2/46D/HMP1er2/BYoaAsQ85atfBHsZrR3PXqfCKf+mN6yrbfMSntczHCc/dvvZHnW4yuOaQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GsaVyhrn2fpluRJdINOcBUH/9hti28c5b2oPJwmXZyk=; b=Uh5xYb9Vpoh9uEfFUxZc5+mQChYHn5O4f9H+wJABS/l//gUrrADBnJXlaKDQzuO824Pi4f2+ZBoSNoloz2GUiNGn0UkF+VQCL928g/gB94s+/IljNrEagTN8OT98/zrKUi4ouMggBA1/aFsEgebpsTTGriGkMvTEJ1kIETJNA241nVaFQq78MWVWLX2BDRIq/bUujCl9zl1/QsZh4ri5po8yRrwNlscSORY2hUAxrtOChRkeN7DBYOIa2fhWEfW6eWCeuSgYtKYBJNdsBESgqd+P14HFVrVZf5+oVVAiVKvUjlgLGOE5wVhytvy0CZ9Lf5nsld+ml7dWqJwCvk0a6A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BY5PR12MB4147.namprd12.prod.outlook.com (2603:10b6:a03:205::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.29; Mon, 24 Jun 2024 22:47:37 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:37 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 7/8] fwctl/mlx5: Support for communicating with mlx5 fw Date: Mon, 24 Jun 2024 19:47:31 -0300 Message-ID: <7-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0029.namprd07.prod.outlook.com (2603:10b6:208:1a0::39) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BY5PR12MB4147:EE_ X-MS-Office365-Filtering-Correlation-Id: 9eceff69-f2ef-4673-5c94-08dc949fa302 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|366013|7416011|1800799021|921017; X-Microsoft-Antispam-Message-Info: C3ppm2uzphacipk1BIMXdktwtwitnCY8JynB4YuepEPNBZjtM7ZxKa/P2da0n89aHJsNkNuU16P97RJNE/I5/GQ1C0UDpY/6aM/nh001t9oyKfX9fFTJ67muQgMqVRpCUasF3lUkcZXsDHa6imMpwJKtoXbH/ZnGBnnzs40Pwto/gDPtsCXGuMhSrLuRTtN5Zb1pUBuBF7xDcZHB42JmQXAJGluRwo8vQXErGcngqYIXW2AyKKy5kBaiZyGwGzTuX8QVoJa1124x7iC8CsrLgIH4hd/kDm/WkzGdozX1uvtQvItN3Adp3D/sFbvzI7YI7KmXNh3XLICX15P1xJVkbQSgeRKzIJQ3tlpX+pIcY6vfqM2G1GDh2lvqC0yw7Q7sppi6/OLI0GIyYxpCr82ekB98NJsKmn/r+okWPx+Rlcr+DY2qDhbpyx3+6Sgm5ktvCXZKstuVH9O1rmr7o/AEZC/BerIj/Q0l50sr9/7qBrlzhPeLVmReI61rnwX/BdVkca+xJ8cLSEMcve81DnRE9iOm8+Q8HCFeuOQofOrNGGzYEosbyDCSsYWW4x1skNkIZrTP3HBcRqCmthKksaO+Dm+UREN0ea+mBlu5ZcIKoMSO86QBZX1yxtYv5OOs5YcCsdznZ4I4TXyLOvwY3iysHukeKMsiWX/PMDyWdOCPPzkbCOdFSvGgUVuGRLCO0FeY86WgtfycE38aSqTLHnc298qlaKUWrFVvxudXtwbs96K1wwq5tL73Tj3dY5Orp8+fTLYsCKIl6zTMkudvY9EJwmUcBVmfyqVSz8nEMDjrmG/thQnvpwmpDCSVcsrhpgVy5Nk3vEaZ43krd217D+PCXgyDFxDSR+rrfGOX3HVC9ARkCfZfachqEP35wTTj0xgeYXx40VuXppeLySmg9jaPrQzf+XQtm+DDWNiSFjTAMzg4kgXNfHkERfBztbpqZUav2l0pdeR40NK05quzy3VIdvBfOmlTUod3eQnxPtlpJXGwVzVu2exRP5irxahTyPSw0Kz2yXqEeOl4SYMF8uyOAE+R7FrLgs8Jd9TrBxl7iK7WktHMMHXrloqX6VTheGfbl5xR0Sg61iJh5c4rh22BSl/oDfj0QXJy0vITm79X4fkKGSy90qWQEmdrt+Ahlr4G+ooflvebpjyZaahz9+7JkUJImzz5t1M24W2aAKGO4GJXDIdnXGWWxAGXqEWbUrga7V0RLqVV1ibKBHrE214DMvYdNpyuAZ10I6EdShC7iODVSYt+fbK047oSIghpDBZHUisy5pHGp3hl/x3KOBh4+NrY9gjXqcpXhS+LqKHdR0ET6prx+EnJzbJqZv0ABssf4PMIFxDjwYOUuu2OHFeCOielAunfTMukvIDpEp84u4E= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(366013)(7416011)(1800799021)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xioNFhZxLxvIH/DuMpXnmM/WW96JPsgLCrTNekBoGBcnU3w9EB/SIF2b2ugoqgFamGJlNYl8czksCT+fE21xe+ccgqTzhqhHNYM7TiGjGGzPCWiJAvK5cayyk34cr50VrJ271asbHBaXPAbm+E4pkQ3lhahZxgiwe4sO9A2g3bIlXkdp3A7ikwkLTUoaZs4/qw8NlA1kY7d8Jri3eIr7yi8xFV8I1Os0bJXuQ9ZEV+GoxvJ7prHsxHw27cctWbY6al/Gov2cE9JS5aO9MjxJ5PC3zvUVn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e1qrUoaoFq+Pvel3Y17P16tc2JRSusJCHKwiwuy/fJQqJmeIpLlDNrtNoPpLk6e68b7lDv7eeXKowOVFOTyLht1zyVXsSUL7Wtj6EBCN+MYYyewSgdqi+mdM7cVIhLLZEilj/9XAppg3egF+83aWMPsswWv680V3C5HGrkppEiWzRWDifj63slgNn1kdgrmVj11FbbiiJwSpabQfaoI8FT49 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9eceff69-f2ef-4673-5c94-08dc949fa302 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:33.7977 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: g11XXo5ns5dLPOVYYf85YFsxg6YDLsfvhZSxz3MyBPI4pm+ZAn/+zNLisZt8MFBa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4147 From: Saeed Mahameed mlx5's fw has long provided a User Context concept. This has a long history in RDMA as part of the devx extended verbs programming interface. A User Context is a security envelope that contains objects and controls access. It contains the Protection Domain object from the InfiniBand Architecture and both togther provide the OS with the necessary tools to bind a security context like a process to the device. The security context is restricted to not be able to touch the kernel or other processes. In the RDMA verbs case it is also restricted to not touch global device resources. The fwctl_mlx5 takes this approach and builds a User Context per fwctl file descriptor and uses a FW security capability on the User Context to enable access to global device resources. This makes the context useful for provisioning and debugging the global device state. mlx5 already has a robust infrastructure for delivering RPC messages to fw. Trivially connect fwctl's RPC mechanism to mlx5_cmd_do(). Enforce the User Context ID in every RPC header so the FW knows the security context of the issuing ID. Signed-off-by: Saeed Mahameed Signed-off-by: Jason Gunthorpe --- MAINTAINERS | 7 + drivers/fwctl/Kconfig | 14 ++ drivers/fwctl/Makefile | 1 + drivers/fwctl/mlx5/Makefile | 4 + drivers/fwctl/mlx5/main.c | 337 ++++++++++++++++++++++++++++++++++++ include/uapi/fwctl/fwctl.h | 1 + include/uapi/fwctl/mlx5.h | 36 ++++ 7 files changed, 400 insertions(+) create mode 100644 drivers/fwctl/mlx5/Makefile create mode 100644 drivers/fwctl/mlx5/main.c create mode 100644 include/uapi/fwctl/mlx5.h diff --git a/MAINTAINERS b/MAINTAINERS index 2090009a6ae98a..f0689e510510b3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9085,6 +9085,13 @@ F: drivers/fwctl/ F: include/linux/fwctl.h F: include/uapi/fwctl/ +FWCTL MLX5 DRIVER +M: Saeed Mahameed +R: Itay Avraham +L: linux-kernel@vger.kernel.org +S: Maintained +F: drivers/fwctl/mlx5/ + GALAXYCORE GC0308 CAMERA SENSOR DRIVER M: Sebastian Reichel L: linux-media@vger.kernel.org diff --git a/drivers/fwctl/Kconfig b/drivers/fwctl/Kconfig index 37147a695add9a..e5ee2d46d43126 100644 --- a/drivers/fwctl/Kconfig +++ b/drivers/fwctl/Kconfig @@ -7,3 +7,17 @@ menuconfig FWCTL support a wide range of lockdown compatible device behaviors including manipulating device FLASH, debugging, and other activities that don't fit neatly into an existing subsystem. + +if FWCTL +config FWCTL_MLX5 + tristate "mlx5 ConnectX control fwctl driver" + depends on MLX5_CORE + help + MLX5CTL provides interface for the user process to access the debug and + configuration registers of the ConnectX hardware family + (NICs, PCI switches and SmartNIC SoCs). + This will allow configuration and debug tools to work out of the box on + mainstream kernel. + + If you don't know what to do here, say N. +endif diff --git a/drivers/fwctl/Makefile b/drivers/fwctl/Makefile index 1cad210f6ba580..1c535f694d7fe4 100644 --- a/drivers/fwctl/Makefile +++ b/drivers/fwctl/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_FWCTL) += fwctl.o +obj-$(CONFIG_FWCTL_MLX5) += mlx5/ fwctl-y += main.o diff --git a/drivers/fwctl/mlx5/Makefile b/drivers/fwctl/mlx5/Makefile new file mode 100644 index 00000000000000..139a23e3c7c517 --- /dev/null +++ b/drivers/fwctl/mlx5/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_FWCTL_MLX5) += mlx5_fwctl.o + +mlx5_fwctl-y += main.o diff --git a/drivers/fwctl/mlx5/main.c b/drivers/fwctl/mlx5/main.c new file mode 100644 index 00000000000000..5e64371d7e5508 --- /dev/null +++ b/drivers/fwctl/mlx5/main.c @@ -0,0 +1,337 @@ +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 +/* + * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + */ +#include +#include +#include +#include +#include + +#define mlx5ctl_err(mcdev, format, ...) \ + dev_err(&mcdev->fwctl.dev, format, ##__VA_ARGS__) + +#define mlx5ctl_dbg(mcdev, format, ...) \ + dev_dbg(&mcdev->fwctl.dev, "PID %u: " format, current->pid, \ + ##__VA_ARGS__) + +struct mlx5ctl_uctx { + struct fwctl_uctx uctx; + u32 uctx_caps; + u32 uctx_uid; +}; + +struct mlx5ctl_dev { + struct fwctl_device fwctl; + struct mlx5_core_dev *mdev; +}; +DEFINE_FREE(mlx5ctl, struct mlx5ctl_dev *, if (_T) fwctl_put(&_T->fwctl)); + +struct mlx5_ifc_mbox_in_hdr_bits { + u8 opcode[0x10]; + u8 uid[0x10]; + + u8 reserved_at_20[0x10]; + u8 op_mod[0x10]; + + u8 reserved_at_40[0x40]; +}; + +struct mlx5_ifc_mbox_out_hdr_bits { + u8 status[0x8]; + u8 reserved_at_8[0x18]; + + u8 syndrome[0x20]; + + u8 reserved_at_40[0x40]; +}; + +enum { + MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES = 0x4, +}; + +enum { + MLX5_CMD_OP_QUERY_DIAGNOSTIC_PARAMS = 0x819, + MLX5_CMD_OP_SET_DIAGNOSTIC_PARAMS = 0x820, + MLX5_CMD_OP_QUERY_DIAGNOSTIC_COUNTERS = 0x821, + MLX5_CMD_OP_POSTPONE_CONNECTED_QP_TIMEOUT = 0xb2e, +}; + +static int mlx5ctl_alloc_uid(struct mlx5ctl_dev *mcdev, u32 cap) +{ + u32 out[MLX5_ST_SZ_DW(create_uctx_out)] = {}; + u32 in[MLX5_ST_SZ_DW(create_uctx_in)] = {}; + void *uctx; + int ret; + u16 uid; + + uctx = MLX5_ADDR_OF(create_uctx_in, in, uctx); + + mlx5ctl_dbg(mcdev, "%s: caps 0x%x\n", __func__, cap); + MLX5_SET(create_uctx_in, in, opcode, MLX5_CMD_OP_CREATE_UCTX); + MLX5_SET(uctx, uctx, cap, cap); + + ret = mlx5_cmd_exec(mcdev->mdev, in, sizeof(in), out, sizeof(out)); + if (ret) + return ret; + + uid = MLX5_GET(create_uctx_out, out, uid); + mlx5ctl_dbg(mcdev, "allocated uid %u with caps 0x%x\n", uid, cap); + return uid; +} + +static void mlx5ctl_release_uid(struct mlx5ctl_dev *mcdev, u16 uid) +{ + u32 in[MLX5_ST_SZ_DW(destroy_uctx_in)] = {}; + struct mlx5_core_dev *mdev = mcdev->mdev; + int ret; + + MLX5_SET(destroy_uctx_in, in, opcode, MLX5_CMD_OP_DESTROY_UCTX); + MLX5_SET(destroy_uctx_in, in, uid, uid); + + ret = mlx5_cmd_exec_in(mdev, destroy_uctx, in); + mlx5ctl_dbg(mcdev, "released uid %u %pe\n", uid, ERR_PTR(ret)); +} + +static int mlx5ctl_open_uctx(struct fwctl_uctx *uctx) +{ + struct mlx5ctl_uctx *mfd = + container_of(uctx, struct mlx5ctl_uctx, uctx); + struct mlx5ctl_dev *mcdev = + container_of(uctx->fwctl, struct mlx5ctl_dev, fwctl); + int uid; + + /* + * New FW supports the TOOLS_RESOURCES uid security label + * which allows commands to manipulate the global device state. + * Otherwise only basic existing RDMA devx privilege are allowed. + */ + if (MLX5_CAP_GEN(mcdev->mdev, uctx_cap) & + MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES) + mfd->uctx_caps |= MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES; + + uid = mlx5ctl_alloc_uid(mcdev, mfd->uctx_caps); + if (uid < 0) + return uid; + + mfd->uctx_uid = uid; + return 0; +} + +static void mlx5ctl_close_uctx(struct fwctl_uctx *uctx) +{ + struct mlx5ctl_dev *mcdev = + container_of(uctx->fwctl, struct mlx5ctl_dev, fwctl); + struct mlx5ctl_uctx *mfd = + container_of(uctx, struct mlx5ctl_uctx, uctx); + + mlx5ctl_release_uid(mcdev, mfd->uctx_uid); +} + +static void *mlx5ctl_info(struct fwctl_uctx *uctx, size_t *length) +{ + struct mlx5ctl_uctx *mfd = + container_of(uctx, struct mlx5ctl_uctx, uctx); + struct fwctl_info_mlx5 *info; + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return ERR_PTR(-ENOMEM); + + info->uid = mfd->uctx_uid; + info->uctx_caps = mfd->uctx_caps; + *length = sizeof(*info); + return info; +} + +static bool mlx5ctl_validate_rpc(const void *in, enum fwctl_rpc_scope scope) +{ + u16 opcode = MLX5_GET(mbox_in_hdr, in, opcode); + + /* + * Currently the driver can't keep track of commands that allocate + * objects in the FW, these commands are safe from a security + * perspective but nothing will free the memory when the FD is closed. + * For now permit only query commands. Also the caps for the scope have + * not been defined yet, filter commands manually for now. + */ + switch (opcode) { + case MLX5_CMD_OP_POSTPONE_CONNECTED_QP_TIMEOUT: + case MLX5_CMD_OP_QUERY_ADAPTER: + case MLX5_CMD_OP_QUERY_ESW_FUNCTIONS: + case MLX5_CMD_OP_QUERY_HCA_CAP: + case MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT: + case MLX5_CMD_OP_QUERY_ROCE_ADDRESS: + return scope <= FWCTL_RPC_CONFIGURATION; + + case MLX5_CMD_OP_QUERY_CONG_PARAMS: + case MLX5_CMD_OP_QUERY_CONG_STATISTICS: + case MLX5_CMD_OP_QUERY_CONG_STATUS: + case MLX5_CMD_OP_QUERY_CQ: + case MLX5_CMD_OP_QUERY_DCT: + case MLX5_CMD_OP_QUERY_DIAGNOSTIC_COUNTERS: + case MLX5_CMD_OP_QUERY_DIAGNOSTIC_PARAMS: + case MLX5_CMD_OP_QUERY_EQ: + case MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT: + case MLX5_CMD_OP_QUERY_FLOW_COUNTER: + case MLX5_CMD_OP_QUERY_FLOW_GROUP: + case MLX5_CMD_OP_QUERY_FLOW_TABLE_ENTRY: + case MLX5_CMD_OP_QUERY_FLOW_TABLE: + case MLX5_CMD_OP_QUERY_GENERAL_OBJECT: + case MLX5_CMD_OP_QUERY_ISSI: + case MLX5_CMD_OP_QUERY_L2_TABLE_ENTRY: + case MLX5_CMD_OP_QUERY_LAG: + case MLX5_CMD_OP_QUERY_MAD_DEMUX: + case MLX5_CMD_OP_QUERY_MKEY: + case MLX5_CMD_OP_QUERY_MODIFY_HEADER_CONTEXT: + case MLX5_CMD_OP_QUERY_PACKET_REFORMAT_CONTEXT: + case MLX5_CMD_OP_QUERY_PAGES: + case MLX5_CMD_OP_QUERY_Q_COUNTER: + case MLX5_CMD_OP_QUERY_QP: + case MLX5_CMD_OP_QUERY_RMP: + case MLX5_CMD_OP_QUERY_RQ: + case MLX5_CMD_OP_QUERY_RQT: + case MLX5_CMD_OP_QUERY_SCHEDULING_ELEMENT: + case MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS: + case MLX5_CMD_OP_QUERY_SQ: + case MLX5_CMD_OP_QUERY_SRQ: + case MLX5_CMD_OP_QUERY_TIR: + case MLX5_CMD_OP_QUERY_TIS: + case MLX5_CMD_OP_QUERY_VHCA_MIGRATION_STATE: + case MLX5_CMD_OP_QUERY_VNIC_ENV: + case MLX5_CMD_OP_QUERY_VPORT_COUNTER: + case MLX5_CMD_OP_QUERY_VPORT_STATE: + case MLX5_CMD_OP_QUERY_WOL_ROL: + case MLX5_CMD_OP_QUERY_XRC_SRQ: + case MLX5_CMD_OP_QUERY_XRQ_DC_PARAMS_ENTRY: + case MLX5_CMD_OP_QUERY_XRQ_ERROR_PARAMS: + case MLX5_CMD_OP_QUERY_XRQ: + return scope <= FWCTL_RPC_DEBUG_READ_ONLY; + + case MLX5_CMD_OP_SET_DIAGNOSTIC_PARAMS: + return scope <= FWCTL_RPC_DEBUG_WRITE; + + case MLX5_CMD_OP_ACCESS_REG: + return scope <= FWCTL_RPC_DEBUG_WRITE_FULL; + default: + return false; + } +} + +static void *mlx5ctl_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope, + void *rpc_in, size_t in_len, size_t *out_len) +{ + struct mlx5ctl_dev *mcdev = + container_of(uctx->fwctl, struct mlx5ctl_dev, fwctl); + struct mlx5ctl_uctx *mfd = + container_of(uctx, struct mlx5ctl_uctx, uctx); + void *rpc_alloc __free(kvfree) = NULL; + void *rpc_out; + int ret; + + if (in_len < MLX5_ST_SZ_BYTES(mbox_in_hdr) || + *out_len < MLX5_ST_SZ_BYTES(mbox_out_hdr)) + return ERR_PTR(-EMSGSIZE); + + /* FIXME: Requires device support for more scopes */ + if (scope != FWCTL_RPC_CONFIGURATION && + scope != FWCTL_RPC_DEBUG_READ_ONLY) + return ERR_PTR(-EOPNOTSUPP); + + mlx5ctl_dbg(mcdev, "[UID %d] cmdif: opcode 0x%x inlen %zu outlen %zu\n", + mfd->uctx_uid, MLX5_GET(mbox_in_hdr, rpc_in, opcode), + in_len, *out_len); + + if (!mlx5ctl_validate_rpc(rpc_in, scope)) + return ERR_PTR(-EBADMSG); + + /* + * mlx5_cmd_do() copies the input message to its own buffer before + * executing it, so we can reuse the allocation for the output. + */ + if (*out_len <= in_len) { + rpc_out = rpc_in; + } else { + rpc_out = rpc_alloc = kvzalloc(*out_len, GFP_KERNEL); + if (!rpc_alloc) + return ERR_PTR(-ENOMEM); + } + + /* Enforce the user context for the command */ + MLX5_SET(mbox_in_hdr, rpc_in, uid, mfd->uctx_uid); + ret = mlx5_cmd_do(mcdev->mdev, rpc_in, in_len, rpc_out, *out_len); + + mlx5ctl_dbg(mcdev, + "[UID %d] cmdif: opcode 0x%x status 0x%x retval %pe\n", + mfd->uctx_uid, MLX5_GET(mbox_in_hdr, rpc_in, opcode), + MLX5_GET(mbox_out_hdr, rpc_out, status), ERR_PTR(ret)); + + /* + * -EREMOTEIO means execution succeeded and the out is valid, + * but an error code was returned inside out. Everything else + * means the RPC did not make it to the device. + */ + if (ret && ret != -EREMOTEIO) + return ERR_PTR(ret); + if (rpc_out == rpc_in) + return rpc_in; + return_ptr(rpc_alloc); +} + +static const struct fwctl_ops mlx5ctl_ops = { + .device_type = FWCTL_DEVICE_TYPE_MLX5, + .uctx_size = sizeof(struct mlx5ctl_uctx), + .open_uctx = mlx5ctl_open_uctx, + .close_uctx = mlx5ctl_close_uctx, + .info = mlx5ctl_info, + .fw_rpc = mlx5ctl_fw_rpc, +}; + +static int mlx5ctl_probe(struct auxiliary_device *adev, + const struct auxiliary_device_id *id) + +{ + struct mlx5_adev *madev = container_of(adev, struct mlx5_adev, adev); + struct mlx5_core_dev *mdev = madev->mdev; + struct mlx5ctl_dev *mcdev __free(mlx5ctl) = fwctl_alloc_device( + &mdev->pdev->dev, &mlx5ctl_ops, struct mlx5ctl_dev, fwctl); + int ret; + + if (!mcdev) + return -ENOMEM; + + mcdev->mdev = mdev; + + ret = fwctl_register(&mcdev->fwctl); + if (ret) + return ret; + auxiliary_set_drvdata(adev, no_free_ptr(mcdev)); + return 0; +} + +static void mlx5ctl_remove(struct auxiliary_device *adev) +{ + struct mlx5ctl_dev *mcdev __free(mlx5ctl) = auxiliary_get_drvdata(adev); + + fwctl_unregister(&mcdev->fwctl); +} + +static const struct auxiliary_device_id mlx5ctl_id_table[] = { + {.name = MLX5_ADEV_NAME ".fwctl",}, + {}, +}; +MODULE_DEVICE_TABLE(auxiliary, mlx5ctl_id_table); + +static struct auxiliary_driver mlx5ctl_driver = { + .name = "mlx5_fwctl", + .probe = mlx5ctl_probe, + .remove = mlx5ctl_remove, + .id_table = mlx5ctl_id_table, +}; + +module_auxiliary_driver(mlx5ctl_driver); + +MODULE_IMPORT_NS(FWCTL); +MODULE_DESCRIPTION("mlx5 ConnectX fwctl driver"); +MODULE_AUTHOR("Saeed Mahameed "); +MODULE_LICENSE("Dual BSD/GPL"); diff --git a/include/uapi/fwctl/fwctl.h b/include/uapi/fwctl/fwctl.h index 8bde0d4416fd55..49a357e1bc713f 100644 --- a/include/uapi/fwctl/fwctl.h +++ b/include/uapi/fwctl/fwctl.h @@ -42,6 +42,7 @@ enum { enum fwctl_device_type { FWCTL_DEVICE_TYPE_ERROR = 0, + FWCTL_DEVICE_TYPE_MLX5 = 1, }; /** diff --git a/include/uapi/fwctl/mlx5.h b/include/uapi/fwctl/mlx5.h new file mode 100644 index 00000000000000..bcb4602ffdeee4 --- /dev/null +++ b/include/uapi/fwctl/mlx5.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + * + * These are definitions for the command interface for mlx5 HW. mlx5 FW has a + * User Context mechanism which allows the FW to understand a security scope. + * FWCTL binds each FD to a FW user context and then places the User Context ID + * (UID) in each command header. The created User Context has a capability set + * that is appropriate for FWCTL's security model. + * + * Command formation should use a copy of the structs in mlx5_ifc.h following + * the Programmers Reference Manual. A open release is available here: + * + * https://network.nvidia.com/files/doc-2020/ethernet-adapters-programming-manual.pdf + * + * The device_type for this file is FWCTL_DEVICE_TYPE_MLX5. + */ +#ifndef _UAPI_FWCTL_MLX5_H +#define _UAPI_FWCTL_MLX5_H + +#include + +/** + * struct fwctl_info_mlx5 - ioctl(FWCTL_INFO) out_device_data + * @uid: The FW UID this FD is bound to. Each command header will force + * this value. + * @uctx_caps: The FW capabilities that are enabled for the uid. + * + * Return basic information about the FW interface available. + */ +struct fwctl_info_mlx5 { + __u32 uid; + __u32 uctx_caps; +}; + +#endif From patchwork Mon Jun 24 22:47:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13710214 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05E4D1A2FC1; Mon, 24 Jun 2024 22:47:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269270; cv=fail; b=KReeM6BfqKJE+D8vou0UDd726nMNZ+HQ7hfxE296SJqWZXUtUWOd3CP5s2kOYE4ZgjMbLFyR0fjZ5foYOC0T/EJbJyuqNf+q3lDS/2x6Cn83KsZ6J/mw6rGyGaza9ZY7uZo9l8wZhb+rDn7FQ1ZBwbri9nB/5M6B2CbStomxxBM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719269270; c=relaxed/simple; bh=8OD2Hfx/2G14AFgglBk1QAzWsKVoply2tS4MrhiZpkU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=aTacyTCRuV/f+N1lwoP+BJ+Ev+DFmBsQ7xzwwEUCu3sHUBsvU0xZld77mz4fn/TKZ+eYUd4afPE09S8cdP0A2Dbwtpc10T4nFYyfDxGw5MfTjoDoEMYw1iAI75qCnz/uStI5EqxgKyXjhcdTZNYsnxuXtWKxmzaMuowXAwE+oFs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=AEPFunku; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="AEPFunku" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D9nBXT3lp6mrakV9OWtqBE4L8A2JrVKaCCN1HUlK57boFBLtAbocn9AFzy+N6dQzPjxHf8CtkpVW/eQzEBknGsW+SMprtBcQ2vC2mFJ2drwDybZ/hbl25WstLusKaUHaem8wa17WLfvELb/Zk7KQn6H/ZJjcYncCV5iLECDyva57ODMqfP9uLhUQ7BoOqJba4AoFZ2ghUtjjDKQb1XQYbTQLR+jQyiCza+Buw3q1s7xJ1K8PZJBXi9kEcSN7K7tcUH7qJcISlPQe5Axr+69bua6Rc39LK87/qmyjXCsGWe7L0bdmIImb031jraW4tUwo9K0ep7A/x3eQStdQgdUx7g== 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=eCI4chK7kbXXkpq7Y0mXZ5pHELGxM+ZuWnWxVCW7XP8=; b=Z8+B31TuFS76PzoyGzCm95oBzrN6bfjb7UHFqt59ZLQ6/P7Tzq1hguBwgO8zXW12PMzkUARpIxOZD2sBOqLld9XXzjwEGIR4D52iED93OraLj4sAKOQvviZWIO93wHdHhmsugPwv2o037NWXMqrp31AhBeyE4l3Hx7og8UzKjFHwPg21sBJyBLrp9nk6BcgTvCHEzzWD1c1jkffZkiUOKSrZvfC6pwkCMzKucNi80mjUqj2va12iA5cPCvtZ7mHgPza1Ode58aHkI9ROHZNZADkBs2LTneNTrfGQY7nOIJ5X5x1n2NkRmQ270rUZNzmjMEG4Kg/D3YBHpBVxuf9wOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=eCI4chK7kbXXkpq7Y0mXZ5pHELGxM+ZuWnWxVCW7XP8=; b=AEPFunkuyYpnEZDlRAFfTKpJ5+PKULq8Bh5R8rv92PaEd1pyKQkc9T2Wuyqh0lHd7YLij2hJZXAhgdaXHJU4SkrtqWISDF33NvBlMM8xtdsUQt+yHMKCxQdaxzt3UKaudzkWrAXm45/B1NeqwsuxQ2S3+aJ7P3nyKFgLI2MJT+2rhSvtx9mIFESnUQDJsBO22/8YTZcIXvLYf9naqPt8wkwMPELBzc1HgiFnwOVvvt4TtBE5CoPXkJgpOuJsjnPZ0mGPpM7YG6RycjGBLA1cy1IrG8ityqfzpduIHPwP6qCaseRrFHJou8MbS5eyfaiUI4a5GaeVX9kc83YIsxyc/A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by BY5PR12MB4147.namprd12.prod.outlook.com (2603:10b6:a03:205::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.29; Mon, 24 Jun 2024 22:47:38 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Mon, 24 Jun 2024 22:47:38 +0000 From: Jason Gunthorpe To: Jonathan Corbet , Itay Avraham , Jakub Kicinski , Leon Romanovsky , linux-doc@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, Paolo Abeni , Saeed Mahameed , Tariq Toukan Cc: Andy Gospodarek , Aron Silverton , Dan Williams , David Ahern , Christoph Hellwig , Jiri Pirko , Leonid Bloch , Leon Romanovsky , linux-cxl@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 8/8] mlx5: Create an auxiliary device for fwctl_mlx5 Date: Mon, 24 Jun 2024 19:47:32 -0300 Message-ID: <8-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> In-Reply-To: <0-v2-940e479ceba9+3821-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0043.namprd05.prod.outlook.com (2603:10b6:208:236::12) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) Precedence: bulk X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|BY5PR12MB4147:EE_ X-MS-Office365-Filtering-Correlation-Id: f201e901-df8d-42d4-176d-08dc949fa34a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|376011|366013|7416011|1800799021|921017; X-Microsoft-Antispam-Message-Info: j5qqx8Af2Z4wK5YbT91++zpdljiy1xSm3XiujLtRf7cUJl1ueUQtkKkEzTlGNhjGzDB277f+dmdd2WceMFuzuS7AEC4x5PB2EVC22/LcEsxPj0GrBPIw0WMZjWLm0vKu43SSN1PQ9JljHTg/1yXUR8WQsAqhD2bvNVA3D24xlou3cAlE5lfDrr0q45yhWw0SgdIfW4vGi9RI88/XsTnw2cCtww4gV7FljUWeLHEBwUBMd1xgi9eKOQ6TWS3+rSvshpg/5W9amachIY4DO1dXyxjgqv2kbY1ztkvPfolj8kuionzrwWDbPaOmHjmP2kAVhtZIG4awYtMZEGjiJVxtfoG0qDSnKbFKEaW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I;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230037)(376011)(366013)(7416011)(1800799021)(921017);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mFVGX2tjKRw26bFmxWZ4o1xiRUz5iJQuYO8fDdaq3BLTJIBB9CGviBSJVI0eziRNBW6C3m7aJ1zdH70g3sYz4l101Zk2QQwpBFO1CaZ5x4aagTnaUcsL85rc8XvzDIDnbrcU+eeV+ON01hJCRq09SOYRj0ABBZy7wXVhjM3QqdUBD9AY8M18aVYHSP+v2TJoC4Rc7qY0sICcdnhMYjFbeITEm+plACsvbotiqmxUVTcIYnmawzI+vKjvrMuANCMS/KuBRz1kaO9o5/og7KDpbsYWAfj22ffWx1vYqFxKpMHzoJN/sjWZ5ICFMJ6hbqQC3yT5D7kJTtAzTn+xjT0ActwpENClQFeL+qjiGpEJhiJGdJlMuDnbaozXQCGwSK1qVvJHjWS1LWqDYOi1LiKqY0L2/Q7LzvSXoPqutmC7wxd78JTnzxo3IEyBuUt81qR92dk6Aj4CSaIag/UcJyOhwJjroV75letAgzLGMJJcSfiie5hXkYk+6NHUfWVjEl8ibNWD+aQp5GWlyZjbRYdNj5dMDtA9+LTSbbLsfM+KY3psc7ybA1i5FI/miEgqtDV2zXpdlVXbapSHLlxco8tfnM0Z3xBXDwPREX6ZoeEQUV5oDXuM9i1PAHlIgPEH5+n82Rnt6dGQqlJVexl54CzSUDuG23FXva1WTUgiR9mE+gOUa+XmOKz6xbpTRsocl9X7Bo/69YKwxDPnyUgMtPQ1mEvmSqOArTM/yMZ6s/c4Ko9SO4u8rqs6CUz7xtAHl2ZECUXFsG4TP5Dp0OxjsdV/QYHb11ZqKJcfMtlNxuKLpGtJyXwudUUlSRUMrQdrxJu1MLVtKluGxWgVr2PePMrA8k5k/vwxZOvNrO/IlRCosvfg9CC/Eecg4PhrgWJK3gjcUExlXnQP8uIt6ObiqJMJM5NRqkk590T7QeVl3iuhJqi6xg6elD/SQJnx+Kvs/bWumGajxdeUsGtypV02dRCR2K6wxM0qbWdL/yCljSG2Ig9jPIrSn9l4Nmz4g8+RyS2uc7y7ohKJDLeU3fbIojLWT/NgN3s3uDAB0qu56AF3J6+NHxYr2hSXMAuTRUkrORNAqKqEQFoOPGDYw7PzntPjKIQuk5Ez/EqhjUBDMW6kdMUmFsSmnb2OlYkLCKJYcWyiMjhfbWCkNwXCCLo9ZthjfFMX0/8ehbb7gNWmNArMQ2aRsrNKHooL7EbUNYRUc8UK9QHAOBy2yyBM9iM4+HeuSmsJ22zyUXYQabmcy/6GFKwgvh1ponduJJaysPFx3bm9zLcmYg2WaEmYlVXGf397O00xbRDjzZD47TBRiFCeVC3oTm8nLUaoHzGOPe1laKTGr/QAvFylcHwuMk3NObGecoULSMqoyWB9EDaqbvjqmfepFDY/k/ny6W/BN065G0r2CoMxcCShFut+/ChY1zRRuWKTwi1qvw5NuPmdx6Cd2erwt+Sn+cBdNEopYieN0RJZxqZyLv/UXUjT47Vni80yKU8kHSk03ZyUugauwkvFH5DNsivR2spP+Ea+1BwKpBiQzzw/Vg3FLqpz/XfJk9y/FSXzfFoLCeWyb+I0OvYGY/jIIgeVwpN4EH2MQeK1CPm5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f201e901-df8d-42d4-176d-08dc949fa34a X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2024 22:47:34.2478 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FtBr1mZRKP7YzAVc3mlyK1SVNGS6lgG66hWgRi5AaA76H97t+oi0jINQtgiDkNl5 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4147 From: Saeed Mahameed If the device supports User Context then it can support fwctl. Create an auxiliary device to allow fwctl to bind to it. Create a sysfs like: $ ls /sys/devices/pci0000:00/0000:00:0a.0/mlx5_core.fwctl.0/driver -l lrwxrwxrwx 1 root root 0 Apr 25 19:46 /sys/devices/pci0000:00/0000:00:0a.0/mlx5_core.fwctl.0/driver -> ../../../../bus/auxiliary/drivers/mlx5_fwctl.mlx5_fwctl Signed-off-by: Saeed Mahameed Signed-off-by: Jason Gunthorpe --- drivers/net/ethernet/mellanox/mlx5/core/dev.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/dev.c b/drivers/net/ethernet/mellanox/mlx5/core/dev.c index 47e7c2639774fd..6781ddb090c475 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/dev.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/dev.c @@ -228,8 +228,14 @@ enum { MLX5_INTERFACE_PROTOCOL_VNET, MLX5_INTERFACE_PROTOCOL_DPLL, + MLX5_INTERFACE_PROTOCOL_FWCTL, }; +static bool is_fwctl_supported(struct mlx5_core_dev *dev) +{ + return MLX5_CAP_GEN(dev, uctx_cap); +} + static const struct mlx5_adev_device { const char *suffix; bool (*is_supported)(struct mlx5_core_dev *dev); @@ -252,6 +258,8 @@ static const struct mlx5_adev_device { .is_supported = &is_mp_supported }, [MLX5_INTERFACE_PROTOCOL_DPLL] = { .suffix = "dpll", .is_supported = &is_dpll_supported }, + [MLX5_INTERFACE_PROTOCOL_FWCTL] = { .suffix = "fwctl", + .is_supported = &is_fwctl_supported }, }; int mlx5_adev_idx_alloc(void)