From patchwork Mon Jun 3 15:53:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683949 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2046.outbound.protection.outlook.com [40.107.92.46]) (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 C71311369B9; Mon, 3 Jun 2024 15:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430021; cv=fail; b=p66+ky5Zz89DBZO3Hk5dOsXA26hZqWolOOJYdXjW6TUnpShLOoa9ZauDu7/87LxytwCWFbyhTh4VX2JC4d4vrr730bNOYoQkBCcClTWzlk9p55cVN+oouM7+NK9SxHfpvS991pyrpiLM9mMTK4DvcdYTuxNfxK8AivCzeqH+6yw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430021; c=relaxed/simple; bh=B/w8Xxl6ahlkPyMmOt3teiLon1egwfwQDSUl7nT3IhM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=JQAh42wCCmaQAySAnurzpcdgKCxxKO3jkzhqFLiAlN/8jSr9aaT3CeURqjDx+61CtcDpbzfAhlaF5F5t32eB07DuG/9YPRswNVFt1pOOhKl8OtA/n9i355ZfQiwSKTapNN77YUY7qC/aueDxPeDaoXNTX8zCOeOYB64GPI8YwH8= 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=Ez8a4Gm0; arc=fail smtp.client-ip=40.107.92.46 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="Ez8a4Gm0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K8YjDSsmzUcLX6dz4thHvaXm+ERC1xcRZnSCLHBYbjEL/onGycf08j4jAr/VGM9BJ8Ff8LTVCHhCXAIW45FRf5AeDjU090kqCfagC2//b7i3SMDi6W0BPGhLPKWw+A9TE+vR6gcjuDFjQuYp2wQkkeh5NwFdCuilj8fr2bi7tuYL152Jm+udH+0J4Y38QnnDEAVeRBaNOXAPQmrd0blvMU4f4jzjei4EkI9a2yoBowlcxVuHvPJg+JRAcUcf6czqLLaKkJBXlD0aBfOCZOwd9VseFOqrML3Zl2sIZE4dAu/+4wnUYTbs/NlrrpZOSz0vqLIfFx4Td3GysuDvyT2Y7A== 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=OXbAZ+GPpPi0It5Sp5wSafN7Ssgy5hSKQTFJVJhh69o=; b=gJWQVlieq+Cp2GNrgoIaGFQSGZubZbbhUKHVoWe8ULBmhnGxcR7vUN1MZ6EazvChyaXFtUPwhUQsUYb0fOpNSu6Nz3OGtT2ss0zjHntZaekbQaKt95xOnEQO71Ccm+ZMHufj+mw9hTS2U9wasBe+8AEnhygvR16mYdo0qt6H6zgnKgW0S71RgvnmJp9OpKhiog5NbhQ/xTBEfYj26dd8lcdyIfWZlbtepHXrdWPO1IRnp3HYuXRuOxYffQNJRowDUfKLHcSqyuif/hYpFtVXbKj5tC1RrRA2JtZgN5xfI3OJcKZ02HK6IqLoB3wcKoHUcQvyzYTuwtpj1cy+pPjsCw== 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=OXbAZ+GPpPi0It5Sp5wSafN7Ssgy5hSKQTFJVJhh69o=; b=Ez8a4Gm0iTtizx8AtvRkmSUvImq903IpGqmjh9YVVh0OI/4FpJmmww8S4YvcQyHeahm+ebaANPZKkDbiFmc4uhh9EDIzLN2/9Enl/0kLqvAwqTcsSP0gw2U0H42vsX38l0YzXPRS3HDktQKPrESUSgesxK/J3DMKtmXKUwLqQD23N7/CxbX2Lqu2OwxDPgbtND1GWWL2VmVzgM24okJ6NEcgTaA9MY8Y5XIXTJqx20Sv8ThfWxClPmUW81+04d+eU1XdDfpjH6biI/ugVWzHgZ4EB0qdD9Faar69AznKIwHj3yNYAqnfRNT+aTNeWSYjTirbsxdx9qEK//vRzCArsA== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:32 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:32 +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 1/8] fwctl: Add basic structure for a class subsystem with a cdev Date: Mon, 3 Jun 2024 12:53:17 -0300 Message-ID: <1-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0121.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d5::9) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: 859840b4-0825-460b-e86d-08dc83e54fca X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: m6v81c33ERpi4PD3LJOddQjE0ipoO+moaUxgt16Hb2N0rlMCx2d/vl/u3gIPGDHIb4vT0dEXyqlEWQE5GwIc5vDB801Cr3vW/yZxU+SwD7Ep+YCKwfkxZCGmdPo+Q083EACQGW3644Dxu3OWcGLOA0roQFiuwvU/FuOXRuDzYZHQaupf8VLy9rJcrX76xRbIa1/bUNnL2K4JRgexooHx8hYyVw9AXXeyIl1FLJQDG5KPUP9ifke3Tcc8XO2iivOn1KJVAxHabrjR0ziogqZRQdG5hnUVsJI880DweIfi8VZjF3N1fH+NooRc6pU16bM24uSTm4Dxq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CL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SHzBHd+hpb6Ku52iVsTWOxLK1rfL4pYwmBce7kESFKxuzBjcLB7JXxThKdqnoVqueCwweILFLVHE3JET6ZuujatIpO/fSVLh1pHb/bcORffz3E5UymSmQt8TwQD0vBhOXoYr0XLQoNIPQWIBT2FGkWUmEieC+nxmMjiUC6dPlU1Q+lFK6Oq9wQGHOYKo43z7iW4ydR2m/CU5U+0qGH62YrX4+1yqZNbJluENI0pa1G6/XQctKkCBK4EYCZS0uYPWqoy9bqfa+GMEIMXepbXMN5+qtfANfAxCo1TMuXbcDsn9H3E59wOumdxlTJKZbnPv3DWFxykLGGl6dwYczNyDIdTGmMr0vDW7Lx/bBsTmB4J10EARjdwMEd41Q4kfFUfAsP5nJO89cv4JsUXcHqkHwbpa5sEZclCqOCDpitF+4OmAxUz+BaxksyP27R7jhUBVXNEsrfLd7ZJQ2S/TQd/1IpWkqKi7gXKuoQOZbJhsTN+n00mm1rEBreO10jMjDyOUj5DRD1dh+9RvRyc0unLRBkPca7E2rv1Uo31ex7EnDQki3vLnJHmmjTU7yOcclH9XIdMULNtD+GFhgAx9YvP2BRnQmGhczvOD8W3M+svjeJMh8jpwOA0oO8/hRDkprFtQ31eR2O3sciF0jECTnnTsOVAUI6hSkcDtOR0+SPamFLI5Cdt7b/zUSz79HqU3ujwX+Tx4C0nB82IHb78piKb74X3KQ2pmTNZPOVNdSUkCYpTvt0PJ3wPEQSAxfBTkw5KjuHS060W57jwWZtETYAPR0qpZn13Ct+dAQ7YXt2C2zdj6x6VN/fefVPDMTUVdKLPvagF/eVp9Y7rfGKa0H0YpGWjk/JbMMLm1xa05QUV7byU0W53MKiMunVZjq6XxVzO90LhQ+uFINHLXcXNg9fP7yVdFzj3kw9KbtY9jq0N4mgbmjRg2A2VIaV6FBsQE531DrSWcVS/dQoXfptn4pZ5giXWntHzD099cnuGh+UkkmjMHRHpbwrLpOYVqal/iHaeaUITABSzpKzsI9sNo8zUuNAKHda5cxR6O66cl0p+AI96+w6yV5Y+HbWB02scHMkavvJCR6j4eG++BJMuxvhAbkAjPuvmOGOtyHbL5ra9w0Kbm9Oew2DgL8mSst+WNfhN7oybG4rDxVYT/1YGk5X7YZg28Edg9Y9CcBezEIq/Z56LdmsKwazBoYR5x8QgAyYFg2dzS/so34i9e4IvUOl0EdFCf+d3/aOECY/vGEGdEc/ahcXSufYjEUf5Ay/VfbE62SrnosJZQgv5W8WVwSavV0I1xGa2mGDiQE3x3T8OUKY+f14dfjnDHt2D3Pk80juc5wo3rbPkIdPsDEfw3hKEACznkj8UI3SSn3+jq4hah34fKZONgKV7jQNE738nEdRYmKhLBJUhaTzRiBNOaPYxfXGZcvlm9LUUlGTGb+xbX8oT7KRfJlY86lOJady2QbKcDm4rqWQ+Rm6u+czcyRFQGkss0CPTmhQb0zuzajwJ5KNXOymjG30sesqNEmD+eu4a60h+5jRMLvFVfOkxf4UTQiTDDBzmgv15JyK5bIDXTtpb0/icbBlEJFfU1lRi9GlYJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 859840b4-0825-460b-e86d-08dc83e54fca X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:29.4671 (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: o3aSmVgjEj5MpSVSCpLqrFHBgJc1HZIQQ0dZg4xF5hqjP5oF5A9g7cIeKCA2Tdpo X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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 | 174 +++++++++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 68 ++++++++++++++++ 7 files changed, 266 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 8754ac2c259dc9..833b853808421e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9077,6 +9077,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..6ceee3347ae642 --- /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..ff9b7bad5a2b0d --- /dev/null +++ b/drivers/fwctl/main.c @@ -0,0 +1,174 @@ +// 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); + + if (fwctl->dev.devt) + 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); + + if (!fwctl) + return NULL; + fwctl->dev.class = &fwctl_class; + fwctl->dev.parent = parent; + 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); + int devnum; + + devnum = ida_alloc_max(&fwctl_ida, FWCTL_MAX_DEVICES - 1, GFP_KERNEL); + if (devnum < 0) + return NULL; + fwctl->dev.devt = fwctl_dev + devnum; + + 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 3 15:53:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683947 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2046.outbound.protection.outlook.com [40.107.92.46]) (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 B057813665A; Mon, 3 Jun 2024 15:53:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430019; cv=fail; b=igcwcfRZYFLza0ACxkZQGdr9a8W4KopgxzS0WbBMcVrxN9OxP31AYAJPVBgiPWM0l/wezBcJ1gpCCI1uJRQQ19Pd8i/A7HmFcfbKDqOugb3qd0hhJ/lu4sch1fTtTE9+romzjFbwg4Oj9+NcmjWnh6BTipng3Oen/aVcDA3CxFQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430019; c=relaxed/simple; bh=X16kVzG2cdReR3SAG+0d93wDvcaggVw9r52ivFCFr9o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=OCdW6lxPfDn33QPWln5768mPdpeEUjAILRpwaOBB8wT/E3OTfpojGIhvUTtZb7XLwUNk8rwC+Fx+hOpxuMMOZice2kLLTLfSAaitRebumMydTBluRI2IXsCEbcq43NPw0xEHkL16Q64cb8O4/EkfUK8jCMS9K8nrVRzKJiWJ2dU= 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=YpSB7wC1; arc=fail smtp.client-ip=40.107.92.46 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="YpSB7wC1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Xib9Np2+nImaCW5AhrqCBMWKutbdwYSdmwGOLTkgDdxETiJlPUHGMAz3Ha+swSbikgNHGVnCdKDVZcQZGIR+RxUtAPlMhuuZ58lJ+eBuhJmQ6sPK5bqPKT1TyKcxPL/777vJXT079mNkN0Am4ku4qQi4fMb7ilBz8oAiZ4mgxHiwQ4uZNwKVk7BzL3DnqmhY+ACNaeeCHpI0Heq2f8HsvCNpOtRmk0qgIcnd7ei4f5+thHoJxGgTeelD2zLUchTmh9STvIL2/TzBreTM82HoryiUIefA2kCOWRW/vs7fOOVPzs5Q8PzrnC2uCDNfA2eHalW06dE8ZNk5P+Y7GzC0Yg== 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=K6sEPwWFly2zmU3fxNKK8dKKfoblwv9qH/vtv4HC76A=; b=Bs59E0XEp0HFqvaLa039p0hCkqDAvNYr5wTdN7NVZaHewnToklDIKUiVja8JPbJKmIgtk4GinMRiWIJnlFYzv1ZaTy3H2v+bNN38bN4GRAr2DbJfeet+tzFQkPHRqjVwloMjEWj0gwB7TkCbG8aq5vrKi5ZtfWx/0v5fCyeFded0uGFn/KAQ/91m9SEHE7pBkB1pyoye9f4sfn6C03T7rFd56PcE4WY04jzsOkj81vqH8OPtfJ+12zN/Qw8K3cXaujGcfN34oioGcR9YtLzjhpNb6Rbbcsck/NU5sFLr0pc3vZ+53iJjxnDTaxsod4fFym2Jaiz+8AIs3+QSiW0unw== 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=K6sEPwWFly2zmU3fxNKK8dKKfoblwv9qH/vtv4HC76A=; b=YpSB7wC1EL5GmcSuRe38qCJjQJWH1zlX7LiGlDAeWaVNStlJP3WkWKHrTD1O5U/slZZku16JLyYxVMIWs23f0TNYtGdyaGSTIm01vKmgcRsjRbtrUpFixNs7xxZboRMMnL+M/1Tgqlp/V4+suZZBgoSAowVxermj6dVSOvQSD/iA1OQWv72Lp4alNDW7VvnFe2CarEPH0VmsEPTmKctnfQZMBiKl6/zPUnuGwQ8MIFdUWATbaeLbNnNsh/pJvDu5gSowvW97b/ju5xo/47PMnrNlLaCctSmI7sU2iJyGbZWNqR1ja+kV87OvbRAZDZF9+soMImBsKFvnSpS/IByrnA== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:28 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:28 +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 2/8] fwctl: Basic ioctl dispatch for the character device Date: Mon, 3 Jun 2024 12:53:18 -0300 Message-ID: <2-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT3PR01CA0023.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:86::28) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f0e1e8c-6e51-47f4-8004-08dc83e54ee3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: OVAYSdzz6t9ffV7VUEaLFQnLRg2zeE9FOId+zAhYBh06EHRkvIhLRLWNpulzDR7jyejaEE5fyEAovqudZ3cga9b+y6b6MpuSg9p2AHg+NJC5RguLTUCz7bp0jKxK7nCEJshlYkzftUcWX6wy6r43G6hRSrLLn3XTNA4bLzxQQq2TJfzFiVKc5lxEHcJuOv3HSCUl68CyEhRJDOYFookhqDrCR8uuewugJaSmYD+m0avH+jha112X/g9LuBX0Q9H2v9cHDN3JSvtju+lt6N0P/zFEbF1WtFV1juyKT3qn9SOz/VXImtZMOERL6hKxPSmE3y+i4djoqAEgsCHEsSg8ZSN2TxVSWv20nf0N0wCv9psblWIRO4cpS2jKRM8l/FR3gBeTLBr2J2FiDWS4yUt4gxIgCvCQ5hB8ajUgKjl69WbhaU9hQcIrYT1HAaFH6xnJzIbZN/BtpaSrh7S45ur220mYWHLO0FzenFeFPFUrwloa7p73uOU5o2bwQ9yHE0aj1/ES6TTz7wx7+v3EU64xYf03BBKrV/S1cifbTz13uv08cfPtyFo8fTnh+31nFodJfVc7cIMedh4lZ7uSMDIX0mBDqmHNuuum9nnZMo7aln73eiN9z6jSQS6sJcw7Syyt3ac3Y+jlYtuI0tiQnufW5kcRrsvp/4ZITMAksP0rEtIW2LJYh/0hltNLWF1eLV9DZyvYutXewYU7slbwMm//F4vCEzF1iIqiDFNRb42oVPPFWv/D7883WDCYC1KLK/AKnOMwkNRlewn3ljwNFQiJrKtfMawN/Em9eZTzeSJMF7p5chzYjmTCqGK7tk1ugY+UiJLT8cUSHaGoCe5GwqSEOqbjr4+4mpcPBq+n62j2GOGkVFBRdwFXGw0GAOlRMwXdMafkGL1U9D5TUMNQ2NUQ5jc2HqmwH1rpdJ+YTKJ0a4KGXjgJHMrAbucVUeQnomHc7pNYLLFNm6gYSmtbf/L3LoclhZ0YsrC/SXHq9csWF28NJPskJlc5PHkHodP7aJ2YQIqBP5i4ftmuOvrsnbdUcle3qKnL2TZiAiVw2ftLIJJ84Sb2zpdBtNdYiYSiU4h+RFyoFqPJvrVCwxAHexRFfLYM15kJnVuYKZcxUv8k9BtKeayvWoj4dYqfrRAr3fDDYSz2HlTxwJMph2l75v2CQ5qcxog9RCGlqZdJEh4RuNtErKU1qCLrGiVE7BTp5xE9TYuuX3uqghVUPcZlFItwbBf/u0WIMX3ZYjvyNuWnxXksGjgqexIxd1AN6CwAP3j/4n/md0X6PV74K0KbUDZmSiy95VsepYYOdkjzO/YkeaFuYiLd7yUSdXQ3ht00r+6a 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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-Mess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LarK6F88yoVAGTIgmmRnMHPrPFACrfTTMpuJY5eDVFFEmgA3QC1YVnF/nh+167hVL5XZ2+v2mtZmSw2ZKifkRYQbUZAGyJL0pfc/9H4kDuEOopMNCikPA2G+yc1u8W6sQC8AbGyGmA5C+df0IAI19UTdZ9MHRkrBeUSHJppXh8FxZvSH/3fnIw+cxZMvWgclsbvAJWr9ON9eXwuOmt7uUoVVynfuxoh59Yx1e+x9jwhTzVsx68RP6jWSv/RouuJuveiNQl9HVXqI4EK0lJJaOnT+KAZgulYn1XC3g+lOqeNFBxMxPQvDCGP93ClvyaMj4hkVKm+bTil8Y/lSVnfDMeBQF04bLdPQrpWhEqHaBKSy5yPfmqmZqMDwHK766YkTArSh1GA4JklomhJi6uiDI/K+h6B0J/K7gR2OaadaAhhXrKCQLbE4X4+0jpK99s/NHXCJ2AYp+BlfTSaDxR0bLqpdtYOMGItSSLJRGbOjdzgBRtONnU45E44oWRo2i2kq X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f0e1e8c-6e51-47f4-8004-08dc83e54ee3 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:27.5990 (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: 0Aj4GYdusCJKHiV/n+ZcM7irOJRHijwXfWPjT24uvqlZ4vE1HCndkC3bSS1qRRne X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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 833b853808421e..94062161e9c4d7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9084,6 +9084,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 ff9b7bad5a2b0d..7ecdabdd9dcb1e 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); + struct fwctl_uctx *uctx __free(kfree) = NULL; + int ret; + + guard(rwsem_read)(&fwctl->registration_lock); + if (!fwctl->ops) + return -ENODEV; + + uctx = 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) @@ -46,6 +152,7 @@ static void fwctl_device_release(struct device *device) if (fwctl->dev.devt) 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); device_initialize(&fwctl->dev); return_ptr(fwctl); } @@ -134,8 +244,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 3 15:53:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683942 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 42E892576F; Mon, 3 Jun 2024 15:53:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430014; cv=fail; b=LIQMOKihFfH9Key2q7dM3Vw7O2tsPahv8ztXcc12gXM/wUnnkU/JVjb5SoKmkXeSlBlYOJuyb8oHszqRO5ItNmUGap1CKiz32COLfO2GzlGSwbrt7jvMhgaDPIpOSf9nV3kCws10OgD6P15wOxxtq3RvE/4EtIKWmwg4AzvDPEI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430014; c=relaxed/simple; bh=AEYCTJ65gy0+wqFPeTtMg5SoCFWMgKAi9rCsp2EmSAo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=CMPmR8+1Ftt/yWQgeWoM8mHBzzs7fJbHTHl35rwt5itD9Vq+u59nPY2Ky3zvIAH9CNOwxn2M2PElNj77hg4+IfBBOtyeXsmQBbmNs4dXXF9NPxW4S3O/2szPmOq42ek4jASRxwiLUd3xlpDXhWm/0EXe7Ky/tA4COIMBTK/yUiM= 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=Z0a1JyPk; arc=fail smtp.client-ip=40.107.220.68 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="Z0a1JyPk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HYZ0BC338H3J+9jZsXWLYSasdixwGKeeXV4SigvRTJ9jlgilrdabVmxGkRizPyEe81uHLWo01zbmR0AQyseEgdC3zBj/M4ceCp9IgPqctRsQ/b+pRTc/lIQ56b4K0d/rLazwkaWCv2422AhZjkXdhti2Yc6qEc967FDn/CXbEt7iGH4S5FYLsk9zIe9esTM2PTeUN+nyxyWf9HFHevGLjwDT20Y7gTGo/IfMpMzOOMYPiLTb7O5JC9LUc9x5d8hk5ZiybTsPvm12pieqBBfERLQCcXusAID3n4vIk9RfbIDZ+xPMB1yPO7Cve7y8+DI3Ry/gFCHzAlTx7niQ15mkUw== 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=8a66Ch5a6DnRuLMQiJuqgluaOzuZl4aSIf4u2RZsEhw=; b=D0V9qMtPTyxfar+9ix2tHg/jJ/7XY1XS+o/uDC8YNE7+UMu/CaJ7Fb9FDYONkZVkzwM/LH2Y4YDG8g8JHdsTqjSwhxmtHUru/K05C+YUaPfu//63W9akanhtVdp0wJzraCbimZ3qG0WJhAf+/yn2OlArzgVcmfUwGsqcV5cRKE2QZkIevx+ewriM0AEBmAFwGM0Ge8EogmQ8mTfW6MghHawqiqBisxp99kqVwgXmu/O7OC2KA9q3r3JD+bzjyVwXaVslbTKQIkqiYJ5tMgEqOGfNua5DGrD2q0+FsvOxrWSh1p1i8Ukbx3TZi3oViNkeg8d3JwbXq3eEyp3gh3O1Tg== 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=8a66Ch5a6DnRuLMQiJuqgluaOzuZl4aSIf4u2RZsEhw=; b=Z0a1JyPk4JNK+U4FldqMbgn9B8PWuk3uzx/l/R22G8UAphkex1IhFDSeBDViyzmwCj7FK+/I4bjtC0Ca5vYt2EonC2dZHi37f5fmsUX5cWIunyAsuNYd/p1c9ATjXyrTKTR9AvlgjeOsD2dvH/uHwbnpiTPhpjCcljaTxHyKtrkXDguQ2LV+QSViBMkVFjBVZX5wc7Zlbs3WzM6NlHOrOoc8bb3QGgjOCfB0uTfsbthfHDeubhZQPHKNOdnJLXQmQBmn++vjJjw3JChyhCy9W7kFpztkhkU0PYBrRV9RaaeAcNTTndvY6DxCk/Bpz3PuYIeMCmm0v8t5GnPxZlhiGA== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:26 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:26 +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 3/8] fwctl: FWCTL_INFO to return basic information about the device Date: Mon, 3 Jun 2024 12:53:19 -0300 Message-ID: <3-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT3PR01CA0009.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:86::21) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: c0280e73-9959-4ffb-d015-08dc83e54e17 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: nIghvLiuVPhYq/o4sOz9W1WPwHsxoMHp7L7KtI70mavoXU39bGC/f6nbItxdVGZgmVoEO1jNN6q/XcLqcami5hbKOoCb89SjPW59rVAZj+EyQeGJ2xAUpOEShMylTfRSjfVjm5xHeYpmfUJZTled4BWlq8+xjqWOsv++nOu2O/IbHSIvx3L23keeSY1UbzAnSzrfsls4iHtnGEVq9dBcAFXRRYyIVJ4HpnwLm1G4bPtqmtDRJhgSVvNDBL40efVqIWVDyxuQsBkUl/LRLLVBZVcHX/sYlvVRC04KmyWYxt2PcRMd/pY9vEF2AUBTTIRPPdzDpldF6vLvXMeA2Bq4wgWfiro2oafrpfvKk8F+oSUILQPEhqTUjATCqaBNQIazb5jTax3/f/kh+L0xsLE62xSMH4TkOpBce95HrOIKItMIZ+zMRDIZFzK219DviDlxi28wiOu8CZ+Izuegjle5igXqxQj1umhD9tc8T6x8+E4Z6eQXb82o04NW8nVFBDXEZgsdJY9LqxvN/+S3vUQRMGsukI1PsWr0Xf54MXg5Qkz4/80gJlLbfdWibQ2JxA6brPpAveXOAc3nwjV99RwgSdzI4HSrscWi0g2jYSSzig+r+44jJ7KuRU5CqA2ZQ/TOmr+nn1yERmT733NgHUG6r4GL6rlO2/yTWGJ4EgWMuMa1E6h+QGmI1PFcJVfBNYdlsDrZgAw5z8Z0fBQv5gMZfgY3pW2RaW83t7pA/9nIuBHWiYddLvPJHfc7pQa8XeWPloR0RA1wFwWhhDJnhr3zZ2ukBB0TP/hDto2tBrQUgMZ356SFSQcdKd/xbQVbbozvG+/fYGcriT+XK39a0L7chKujGBpvl8Q8sdXEZsMLHsHvZBhNvKXAJVKcv+ggsGpg6t5dC+Hm/0PfLEOyPUEp0ilfQ2NcK7PtRu6L6JSR8fYe3BH2LIRh3y16NWtdqc+8YAnIBbgn72ZpEjfN/jwVTzPhjeK8DMi3/INHXLHlKHMgXYD4Kddp8kMPXxIVeOTyRW85q5Cwm0A3OE8bICYlocoyaxTJdGo2Q93QxOHrLfpXqgFL4C9UXvCpMcvEHq7xvRjtiaG2V3VyMUP9uhPd727AwP3r11gPrvl5cjy1B/YcZuwdS2U3UhNI0knjDkN+LHMtJMR7rsNdwbEa1ABP+nPjoILf9VbYDoqPH/bU1qwPV+gxEHtcvHgxI5X5asGuATbzpCnzVMHoHAcFzcF8PhC5jjSbXf70QDSB8913kvVqdb66lgHdUo53KPew7Doj0LU6OAcHM8X9JUimpjp79xWE+5tXpHHUS81gyjImKc7mCNQYCik+nHtRBbMgSlAo 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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hknLPk9z35a6QPTymck7wWebUmeybQhztn+AWMzGd9+IkKHtC2t5CQvCZ90bppxIAzj0YdbYazvTx0LfZYo1TLBBJCsDCof4Hygh86QMzo5HULpnzPgmTdaxrnaZQmCaeZgdXzyX7cMrKyAri/ZDNOKIHVfdigiJOAL1b64/Z39jIhVbBWmZ5NK47cDqXxrEdC1jk4siijMI2eeg4Y5tUnp9DlavBhiqVcGZKT+Rt7oQ+EItndn2HhGSFUycLBhsNtkgvn2dZMq7H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ERFPMJF0frGVB+2a5mQgP0CROOgBH0+dy7m/Hg8m4HfDPrzjm1APEo7iG152YbN4omkvRJpxnqI2h+V0s5pP9ErKWPAX214DBxGwOtNoA+5Xpc++qF3NGcdY1pQWI30stjF1UsAn4JY19B0pv2IkqvwFkz2dfKiv1+ss0jFQolqyJtRL8x6hPOxTlvTzgLMpOHSzQ9twxab6SfG+ZJgJLCOpKniCvEK2KC/wBQsRw4bU/+8v64wN6 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c0280e73-9959-4ffb-d015-08dc83e54e17 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:26.3005 (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: T+hksErZcJH0PrWJ8hqTyvQ2+xh5UswJNkFY4PIb6Hn6JAWmduKecTbgMaeujcAk X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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 | 54 ++++++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 8 ++++++ include/uapi/fwctl/fwctl.h | 29 ++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c index 7ecdabdd9dcb1e..10e3f504893892 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,59 @@ 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) = NULL; + + driver_info = 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 +98,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 3 15:53:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683946 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 B3FC9131BAF; Mon, 3 Jun 2024 15:53:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430019; cv=fail; b=Z+yQZnSJtpm1DDZNxGVD+evaU+0eR4OZ7y0b6TfB1XA8q56H7RfQKYG/BOqQgvAa9aa/rpwdeAGtsoeMSVwPIzKkMqM4tKjfuSQ5mJnwcE4zuATCJwd6Oc2cV6oXSrokyIoQkK3jRUqL8AULGCJRpGsV/ZLjXfDGwomjDBQGCf0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430019; c=relaxed/simple; bh=INwHB+8zHtT8++2o5KHq8P3RBx2L3oZmu6Ceo7xBjUE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=dqkwsW7eznDvrxGw1qXwzd6tsQjIdrbEI3qPrcMLYMiQ+0jJiaydknHvAXLX3bZo/PebtD8uahEU21xcgzozoxkP/yRIsLHJ6bpKkA+rMo+4FYH35sSITJR2SHui+UrtcJIrYhcHY8SWw8/X9C4LSLa3NyakgUkVTRemr+IKkzU= 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=OEgVS2Ue; arc=fail smtp.client-ip=40.107.220.68 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="OEgVS2Ue" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hjLrbIYj5cJoxOYDJdE9t/57W55AZTEmepa5qtsYnffy7nthTFVbGYrGpCVQI4yOe3gIcxHwwflECzasgP/5PcIhKqBypX8DbzJQSt7Lh5V684Io0bFztfOZPIp8nQhif0O1tHoIkJpbAs/AiBSgQYvorRTKxWw2MGhE6EF6QNQIy9QhFNMBPrvNgBpE9R/uguWSs+X8coWgkrBMcDB7wSdvFfZZt4ZfnC/AEMUwxmcUowBoOr9MuQLVeymQhFc/MKQt99LZJGWL+ivu5MoG2XZAo8KSl2LUuFBEhSIahLjeQKtcAoOXHL6MQJRt6M5FbWizK79J9/ZxYYW93HtPgw== 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=GEMh+DoZPliQW9Sfc6rELGDgT/kare6XBBKjEW4Ckz8WiYPVz52Fg+ZCfT/rcXEjMsvIe8z+9jCrdMnLuc5wCZRu5VgYn4pLgKT3oJu9E9yjCdJqeu2qi3Hj8SU4cusGUnKupWomgcYWw5/dmpQ+Gx9o4Hre2XpL57fSYEbHXn1ZUOFZAjQi7pBlJY31+B60oM2U5HbT3/0Ws1gTqlCyJhwu02iEtEth9UGCc1ioMzJqYS40pBtxq+qdC7WEpx1WiLLUpigIrZA9GqpxlxVyjV36QE3WepkI/VUI9uC0PR27oIA1fdIEqLNBIXZhg7Rvy6F1dnc/MoY94SHw/88V3g== 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=OEgVS2UesqcgArnipCUJCqggEKffB7QL6SyBdk0lyx6v5bGbeshajOxJMZBVgVO13Mg64lK99v1oWGo3mFBRK9KmMgpmxmvCndMSFZAQABPFpd3NfSyzMBvg1i6jWe+g5/CuGaXH3VcYkgLPWRFu6Phb2vLYipQQfvuAHvziBJYNqymWrW9oGL7UiyuuJ6CWK4Do5SUUZtJXgZLlnSSWTSH1yqOE2FITZbIOB1mgMAXs720BAMEQs1EcFGdxfD/S8SMolrurzMRuhBCE7Rg6U2d+QgZ5Fm5XOaBBehp8+ZE1hjJAu3YxFS8QiW7L2a0Tnwz0g1pCh7yzLvJc802i4g== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:29 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:29 +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 4/8] taint: Add TAINT_FWCTL Date: Mon, 3 Jun 2024 12:53:20 -0300 Message-ID: <4-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0008.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d1::16) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: e48047c0-2b96-4ebe-2755-08dc83e54ef1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: +WkeBJvTRcPsjKLKZb4KmWOezEFfy6+fiuA6jSzY1mZ2uHF6gBlJ4yQ8RfYmb8gsOFRetPpNSS1xN/xuuw7FuwQIQ86m1NBDhrvtzZyCNasxvZoafZlZjiCUeZLyeAEq6tWktvmwmdw2siiUQ62OdYmvNb5Be9oqelsejU/Zsixv7ZGLXlzoDD5weoh8xSyWyEVJsMVJCWqNYUxGw5h7yavL+m/+PN1WYMyS/D8qikN1sL0AxGcGC0kCklVvhFKNG6z7LUkMICiA91/U1YdrjB1cJLci8z0varZiaL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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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UnAKC9FYC7lySKirO5V2kpk5abKZ8NYrxbplDQrbuQMXXwRa4F9v30Mk5sOZxWTmpcycTAYIgruYAZTMPGe2X39UMot6qYb3kC2pfvTlNKwHHwoiAeuzTwfo4mZxke45P0Ngj+3DP570LyoQzYOID+q8LVUIfBHX+GaTB5M+9XYwxyAwDGQhkrnqRJKlCQDCb0BT4MoZ1f5LImZksC6D4HbrPHsOURFfEQ+DBUnMXdF90neZ3iaCAHc68DvHWe9ggHJlJxM+8OUn72Y8UMF/l1ul6N0ij5e/UBzg68sJsLTKtlTuZOUWlunUiTTHikZlrx68MQSwP/VyIqalQePzycZS5uK3D2hWcXnnZJQHbZj016eLhj/eC8goZzykGAY5uC/mYe9WWR7gZHX5k4uZUJj7gLZKhcFhcLTs7xatmfmO6/4WK5uY8wqPxPY1PJ9wfMkORIlbSqO44QdO8NpDKMKstAb8hvUwaSxFylslsZyscY9jheai0DXjldYrLbZxAqrr/o+6IM8RKKpowtS/NN6xuRu+WB/s6A7yBYQ9ra8k18XLyC6Q7/qbtO1nn136owixWY0RaXV8cCv0WFr5F9p1m5oR9jIbuH3EzpV1A8YfBXvvCUV7jvTMKAOUxgSyojS+v7Bb09sz8cnV/YrOf395CuHPU98kuKcSSap513cxjTJUUkE4B7iHsNGxDD9q/+2/KIbM+KhtBlsdAZPwfMmz1PXYmR18vCfG+fhBLGjLzRSbt7Lgn2/r6JD/8KG+6onRxFhUn112tIynN5wU1M67X/ErNbj0B+/GRDM9ydw1XKt09gpghBwqkjU0JwBwkFbBnDs14oSxmPxfof5gpLjjOvtGIvM98GdWzyJGFut5u0eYu2Rhx0dIJ19o4tqeJzJXOn8KQ9UrYtD03IxGJzm2UK7IbDch9a1LUp49YLZeqa3QpN0kYPVKQOUNCVkaJKRhkv93OXzruvc/Wv45MwOVJgek4AgKm2Qv8XSgpacpXLw5Dyujw8zsp25JrLfzJwYukeqTrFsrkgCe8dCE4Ia9sD/De7Gye53ngdxHAl151/pSQaPR+p+puyZaUR8iee5AiU6oNY0+mCZnmPW8KWuJu4VZr67y6o+hwM6ytJzPoUO6nO70uAGjIC7xuAVdnZGqoKrIg/aTvwsu0eSL7bHMAs/mNJQgY4m9O9tBPYI+JT8UOcugJbUTGroLET5EtJloipRI2TJlTNbQ9JdlBmvH8NLzLxbPY4Ac44Q5rtC34Vw1RUMtTCre0RIc0SUc3Rsi4FLYj5sc7iNYrEFYFu2aeEy0pYy61s8dOjw+hyRWJ+/Wk2q8xtJ6RG5UDKLkUwa3LGWT5i4O9Vshpofr5OWhcIkl2aXQmKJV9GthFlmrNVUSPL/fQNNs7XanP5eO0GozH2iqJkuX2XIfjTnFlWGjdwYruhlRHrw4wlx9NVZzfhQJJOTf+9K1GOZWj5gLL3dYNnIVZfnLHHy3ZNweyufFpYSnEmZoVGnWu9nq0AZPeToa7jRkiuwF5YOx7nsiCUWsM4zQEcbJcA2veICqe9CWt/Y9t5/UnBjtLBTm07Zh/o0IB4LTk1G1NzcCNPJ4 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e48047c0-2b96-4ebe-2755-08dc83e54ef1 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:27.6277 (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: elta4oGpNdB+Kys8ELO8uaXv4qJge07gHmJlZp8fiwr2GdVauTFVL4l+g1uMmrw1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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 3 15:53:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683943 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 37843131E33; Mon, 3 Jun 2024 15:53:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430015; cv=fail; b=FzLEc8V25ZscOf0zNfd79MmYre1bWq5KHfdk608lsy5Cd+895mEIfcczo9DEgmhnRGQoRV+8RyMJ+dRQFYuSnKCwRjU/r3NDYXB7Zp89kzaIgbrPNTxfmctn5gcamwltTKwhn0mFSsxVNXqKKAkDFOVX8QMlwcMnrETGZ0SxxCw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430015; c=relaxed/simple; bh=fbgX+Nyt7kY71w9xcORixppx9MgoGB69W098xJk/EAI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZHLaT8k7YLdsI8BIeuVpVjCVKUUhM110MvlyLvGBALxU7BmeVTxDLvw+EU87txGa2FKPzFZWBVnlO3sbE3ojGLC1rTvli5+8ftn8Pt2utnRQTixvWpkFKP3eC8cs+katNWtqlfqbHPNmQexumre7kif25u/3qKH+p5xmr9BqgcM= 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=dRaiPf60; arc=fail smtp.client-ip=40.107.220.68 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="dRaiPf60" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ittivMfQdKEBg7TCE+Z64gKTzOt5jtaFVwvqIA86nlC7u/AiP+xHSbfTTUqQjvnq7JIpxU5eKKzifsjbO4zAbqfLpL580zESagc+xF24/39XIp7hhp2kQh2za0bnuh6YD31erhYJfihNRcZSSxyb57TrW5Sh4XO+A5PkUt9m3gVTiDDQqUeOIvbgNNw/6OwYlhpNZkI2z8m/02Ql+FMG+v+kKkU9Y5IuWyhUoxVTYj1Ts26CcLKkNl8ks2QWMBeK/lccSNUs7pH3rg/oian/BTJKKe9zOXJTMWKwNYUxzi/SD99jVZede9iZTGOsYW88gFc3INT3lK6BkesxEZd0TQ== 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=tQOymlnr7phvCmJdHapc5xBAi9V1NAAiP03KmhkrAC8=; b=mmbnoOQdtti+UXuLBRK4VLpQ/iqN2UqWPckUiBcku8BZ0tVPgwG98PeDReEoe2odTYb4wv9yI8ySzFuKktwyEIo83E8jPrtyZ9PJ7soITQNymKEIUm0FaG8VNEOT07scKUuQAzp98S9iRrMzsWihQ9DtNb/65eCV5i/rk3sIuz9y4xIVEdgEn2pjFrkEvIzbVHV1vFK3GF9Qg9MGM9DlgOxeYHXCJdHL/bx8mr4ZR5L5jvSI3pthS20pVb7GcWEC0cYN7lBgJhU9n4fcqYtTfrrR9ImhWJOeaUnwyBK/mZo0WcGO+HDH+czX4o4MwhiPWvedwq7TFD3T+cTmJJZzyA== 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=tQOymlnr7phvCmJdHapc5xBAi9V1NAAiP03KmhkrAC8=; b=dRaiPf6082yc+IZamm1Zsifr8Q1ksRZrzkMPpzT0xjNwbhlcxx0MC6P5elhxsXQGHQZkkjejuqS1l5yRwA7md37iaHpqlpjkmCHo+vZ7Jc22v55pxw4oYQFoBkK3lJyiaNkc+Obr1HrIjkgKejsKDpmbtpSQrZvYu1NnEfVcbOBhmlxXvBWsTJCquAf6OmYX2WVfrcnwoPit8zYSo05BZtaUPDB42/vJ8c0P+QSVIAnaLFgze8QK3wO6ibR3cbLKQRWJP7QR8nq0vXgWBbbTY88qPbKpH4yITB7J8LZTuKMYKXz66aBvxWbctsZVFG6sgH+K7ku0mRewqBELIlF8UQ== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:27 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:27 +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 5/8] fwctl: FWCTL_RPC to execute a Remote Procedure Call to device firmware Date: Mon, 3 Jun 2024 12:53:21 -0300 Message-ID: <5-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0002.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d1::15) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: b146e37b-838e-4a4f-5d46-08dc83e54e05 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: kArUCnQsRr64GjWROLLA83UT5ggf9iMYC8fcX9vVhD9GbR/P9VEw3cIBibwtUhYf7wpHZGFDAKr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AQzGytv4250aaOmGDEI4bY9hIGf/mA74CfnvUIJjDaTU5JbFSWy6uWpkcM+5tKkJboUYJ5fYEC2akTJNEM/LM6jTS2pfV/4+3slHmz+HBj2GOtWcQ2LOpaWJFkSOh0JSkDaI4lTGYvFTnkXXfxxLTJ8xvZBT7HVFGiXO5gFov50FF08Ai6VjARGIsRNZdgD 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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: H8H9hMOK39MV6SQjfMhusmgrbKtKJPP9kghMqentDDgmiatpJH3txtQqEYc6lXjE20eVnX8LVPcfwWr3MikGOu+U7DKlvopfXg3FARmW2cRQBW29xcINNF781ouWYI7k4jFHMb/mR6yx3O0Jp14sefThesfku+RARGQ2lE5MG4Lv3V1G7CfQAvlrOjqki39BBUA9N/Xe9h1NgdTEBW3ay+VMQwu6Nn2kfLpf8Spe+bZmPxnNtMsN8MUY79UDPXYzgbfEIpBzDnoIuYYfkjNtlkhK9F42QczihaMvVrHhaoxCwoCpH8JH502eQn4SQwT732o2/1su8alM0akwqhhSL6tclgcTYCYkhMofG9f/QTcW5GhWGu59jukez6/p+zkW5JIQNgxWLMvF3E3pQ6AqZi3f4RtAZf+3vG3IXr+QNCTAg8z3AdhETM7aFoSbqKyi3muc5l+za72MTlAOQxJ3gspkb9G+oVbnmb5RjCLgsFrznhtjYSEKavSxv8xDHAILI5E0fxSQdRXK9A9mfaOo9qRVISes5VBMMJV2Gh+IkVgXioFQAhk7zFl6ENpBln6nwgCOmdlPrnG+VmSDte7x5SL6vAM/u3e74l0WPbe8TZp2j2kbCnNiBia78JRqN4/cK7u5To2R3BZHUNq3mkjyYf7ZoYnEtB6MabehyBj3jldsvrmd1cOlLHoaM3J2HSUFs93ezuhQLumWlbkA5fFYDMZgDEh3xcIrWVfbR4zgagafZfKyE5Lv09NwRmoavvMAKNFo0L0gz0m92O/fIMxo6Oi4miq4D5VRIi2k+P/NHHxKqv7ac1GVW0enWyPye+XvsiVUxR3IqF2F9dMTsHGGaqlvJ9608KLBR9oG3B4RkAI1jg8n4qJgjnn8WhYRFdqET6kQ8Th9kEx/gfWNMkoP/g7BwZUEHYehIvPUCaewJBKhu3cEsfEj+ddVyTppPp0hClTsOtMvbumiqgHkkzt0wY3lPzij0X8dtNWZZ61s8nVSkKxhqslmmC7QiWqYb0HjI6nVuw795qu3E7uBQAXG+7zNBJujNuJnzPB/XyEu6Qg5uIMFZ58T3a3nQ32+mbmtwgidHIYRXgOCt7iCG/MvUv2jqlyyO5rhSl9l8vAtc51BDM0Rms5Wt7lByn+Ce9ySIdsKfC5XPGsOyza7/ofeu9He/qN0m+I5BFGnsAtOhLqwQopaubcLIR9Z54bq6kEOgHc+UkruWXGfEsWzFOBhCsFt8k910u8BK0Ww4uixxoI57m/uGLyZbwGQ+wiX646oyXivVEA5Xe0edBBF9cQXN2egwIY8373At6tq612x6zJ1f/NTN7mJGhKqFzdbhA3UsBxCBbceMopkJqrNcQzAS7ZmfeP4OP6ud1Jsql89V9DjU4crBUBnGPZRqJWPQjMAMC9pZbv7P2dXCN5fhog46sIEK/WSPWt2fLMN9saujguGaWdVt1kNTmdRQlFVJWjfA5JH0EhcY/EVcFDx2EVR3EjY79mztJ9CGMtxv5+5mCCoNDL/sNTPXHXmtMD/QCBptotr/jAO22T/PoXCNwtABNdMhrqlLTa5KaEsIAL/mLguE2o/c3I+q7wf+o9xY6H6 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b146e37b-838e-4a4f-5d46-08dc83e54e05 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:26.4874 (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: h6MYPO4sL3xCsd5mn6ETXcSaOAcvpdgSdyusEb7RYjp4InfJKZJuHMJ3doMVEcnN X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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 | 63 ++++++++++++++++++++++++++++++++++++ include/linux/fwctl.h | 5 +++ include/uapi/fwctl/fwctl.h | 66 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) diff --git a/drivers/fwctl/main.c b/drivers/fwctl/main.c index 10e3f504893892..a9d4b764832bb8 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; @@ -76,9 +80,67 @@ 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; + void *outbuf __free(kvfree_errptr) = NULL; + void *inbuf __free(kvfree) = NULL; + 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; + }; + + inbuf = 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; + outbuf = 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 { @@ -99,6 +161,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 3 15:53:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683950 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 A40A6136E3F; Mon, 3 Jun 2024 15:53:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430023; cv=fail; b=eUEsrySmAf4flK3kuytSjXguulrQy292t1hEuDeVlPrWP2SK9y3281JNP/WEoORf7XS5Vo7auycKLjTRJA2B2RgUxzGKw9vMAyN7cUQx/TGMRJAHnDeAmAS8TY6E9cn+qPNZ8a3dfcPl+NiDBq55t+iTvqneOK2T9DIAiUfbZhI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430023; c=relaxed/simple; bh=yzd9LFChuvNGxQtPPK1v3a5VDRZ/rTTXvAaWOf/u1bk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=hLWHjKpnnNzAn6172lYhl3GBWjBq4ZvcsT84d+oCLQ7m540+RX+Bb9qIakARGTN9PpXFOqj8LjbUK0heUgFSG1gO1EBhw+yJCOk1SM/pkWa2g1oLz/PqPgx3EMGUSbr2mYAbDoCS1yPL7Yfip8gvz+2raowZ58tfo9R0z7TwSxw= 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=Hc2i7TyM; arc=fail smtp.client-ip=40.107.220.68 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="Hc2i7TyM" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DAXuw43w+k9oGsT8gtDDa13ypBXfHRjVk0dOp3ADjEhGstVYgMtuMPZ2q4ezvkNOM6FhA5saAna+Mq/W8hBNRgDibuHFWS2yAXjx1WXu8hgqhN7dbqmsDtKQMebBpxwObIVoHg9w/uROaIRXIfiAafIbaDIrLjUrzK5QRRCVEmohVMubDv4OwJaIj1PVZqEBK1uDR1uHq+MfJcTr/+2Nr7isGeNheaQOsIdNoOCSdl1lg8WgdVgzMqCK96IjoTwewfwrpr/Zj0Arn8q92hG34Jt+YyaPCdmEdb/3OU6bs4T0ZizgMITyXR1/nhRklXsTMhJaFjjN+dtfvdRkjX/YFA== 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=uGAnx4YWzQEavI8LVDlznh8j1reXAQpI6iiRXSb7ilk=; b=HbCRcFnelAqpeP6BNXrG94pbPwuT4SJ6SlhCHjDn4T2b6jP5T2ByadJJzLVaUvzciDRB3WyUbxJZ40LuPZe8+VPNCXAp5GeIeNZ5aCLYR9HU5YDTca8/DIGF7F7V4/zW686UuaUS5OXzoBxy/1Zn8GrGbX/dKOyWdK8aOWXDYXzlG2ZS0Trvr1O8Vv/tVApf8FwFnNB5F77g2FQy3z6Yh0wFNTgn5x9+Xdccfo3i0yWDKLZbwGIni5JlNk1ILK/vw6k8qx4p9F1G8BbQpSHoeeLhrBpop94Yt/9z2F1+01twiSRYcY04N7fMmaQdYmAZJ82ZqDyZ71BR92r0yn0/8A== 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=uGAnx4YWzQEavI8LVDlznh8j1reXAQpI6iiRXSb7ilk=; b=Hc2i7TyMTuu1Tdl/c/rGI8DKDGFs/DHb3PrUAjfB5AB6/0z7cTTSwJfr3Mbt4uuxFnoRnJO47NFsexRrSAAIDZAZIUFhQ7CvGxpfosnUtaI3uOHFGCK7R+Et97EwrA3+gT6Ld9CG8gjvv6tbSVNgsEkRVp31x4rbdN6FlHRvS0EvyDLsIFyWNmHeAiGnye0eA/2NQgUSBPegFv/ytAMBK6eCIDC1lAKhJtnqEQETlNKAqMHgr33pmmOP/52SUmJAeQYuDGYeD9vJPFz4MhUA8cYBJ7JNUnxojXIcf+SiAQzJ7+7y5uJYeKXyMvRqXfOWuCdyCbzndTpSC149nq9I5w== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:31 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:31 +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 6/8] fwctl: Add documentation Date: Mon, 3 Jun 2024 12:53:22 -0300 Message-ID: <6-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT3PR01CA0007.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:86::19) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: 593812fc-76e6-4d96-6956-08dc83e54fbe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: =?utf-8?q?2cCeelPBO2hFs/SyXDYiSS39GxfsKDq?= =?utf-8?q?4RukGXk2JjSIfCdJ2xlZFAoWl2Y3cVvMtiVtwuCS/Idejlpwlqsx8MuUqK9R8spUy?= =?utf-8?q?wSNglcIFVLp+Tj4pN1HY7Z3QPy8S44VLPirgqRWKDDG7/BVm32/nBRqyTUT3EoJlC?= =?utf-8?q?HhmoJl8JI/rto7kFvCdxM9iGCVoema4iCtAzkukmrNwDYjgL8s2Xtc0ErA4rb2Bnz?= =?utf-8?q?vvIxE2txOTMflQI354Iuh9zlePkVoxnrGa/oxgew9rCjHjMbJqA1w6+MnOMgUzSyq?= =?utf-8?q?DcQ6webAdNMg8ausKtag67SRqGXVjOjYNG9sbXzD5hK/7H4Kub9WAz0uXHQCYoLLB?= =?utf-8?q?JI81j4M2FPQ+ecwV8fzxSHGSl/Gor/Pb0bmglZ13iWxpQVXPRqHTV+LvDM7qPmkqZ?= =?utf-8?q?2EAfL7aaKGuJjXQQw3s4tffUFP7hZwCFp2aisIZ5M7BkbTWAscf581SN+8Ki/m/oM?= =?utf-8?q?zokAgQ3BDEg6alZk0XmqwqZI2bZrzep0zSyAKgSJ+ryNj0pJEsggTzlWiBwfo0eEJ?= =?utf-8?q?X9v++UgW9iHDiIxos5woGpfzXXPnB/wG1SG05vI26yzSL4GUAU2rLOJxJJPSVqZ1R?= =?utf-8?q?ANmqEeB9e8l2ZfTD+T76XSrvuPNXE73mzad/iPN7Q+gDv2ONXt7ksn3nedBisFDD4?= =?utf-8?q?LEvbbWNoVt8inhhp7vc//Y1KludTymzez3Hw80MV8udj5fouCm5u0Jpm94HATl5PE?= =?utf-8?q?rBtGaW8IKQVcBWEOJPzzBNn3ROgqAZPEpCGs+800dzn0auYgnghKDgNNXoZxk7UpU?= =?utf-8?q?UrmWWv4QEYHCveEce8vEo/4M+m4DxqxuKR7F4O7HDBCNKBdqwO5+Ni6CsbMtkiWUq?= =?utf-8?q?uL/Jf5GEFy2DeShIvz0ueicj5iUIKSm74RC/P/N0h/cPrctqI+CBovDn4xaiIRrIV?= =?utf-8?q?k/QZlf/b2Ahny9sDYh3uqfv7j3R/XwjrqBkVwSn5fjLMLUUhU+FRKVRR1w9EMeN/T?= =?utf-8?q?kXj/vIpHrw4ZvB3k7Mnr4OzfF+nglEHYIPAKXAZj+xoSoYU/AuHhJsnw8N5vV/zM3?= =?utf-8?q?GdpW6Sd6SMbat1cyq4vCp6qMpztppzw5giLkJQjMXOjGpeei7h9ICrc18D2G00HN8?= =?utf-8?q?iR8Yn7kOdypqwDoSnPCo65/ccIx/1au7JPd7sm8sVf7MdPcnlBgqvovd+jI3lG5wY?= =?utf-8?q?WhfqjdJ3ivG3R+QrIqLUbVnq7coBYdBGjax1JO3OgDyC2EhnGfdCuMUPY7k7sRFQ6?= =?utf-8?q?fvLPslkyW2i4J6lSrcxnJaIJANDgZB8rax77A/soexziRzcmO4jfsuQRyLr+GM21x?= =?utf-8?q?pHBwYzQos8SY/?= 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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?EZpgjPwjfMVv0/aTgo3n/oebb3DC?=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QiXVPOAH/z2KWJ/Lxpa2q7eyfE+b23BCVIOEKb95Bv6PRC/UQu5pgsJlPoK?= =?utf-8?q?w5DH9F6uc+6VyUgCx63Q72t9JKksvrDkovPcU9h1Oh7jqlCHOAqU2iO3L06nD9wRA?= =?utf-8?q?p3+DgdaHqfX/Tz158kbGGUKns5kyqrVWWALG97x68oV/E4OINNFN5ZTmIoeWIRcK/?= =?utf-8?q?xjf+MDr/grboIWwD/VARSITBgQYoDdKxba+k7vZgD1UJjdML6yaTFCxuJkYdKgvQF?= =?utf-8?q?EbPESYHGEALodgrmYUW+GUWAKTwpFTsIl10lcOkeEq2YdZiBkKbnlZE5iwNKIIL/h?= =?utf-8?q?KlctFObi+ARMvZRyKE39+CgggRHAEEmeNbVdQnDPSWqt8PlwlxJbsV9duIF4Yah36?= =?utf-8?q?yS5COUDNadFw1kOc1ERAEF2s90SRX9YopZYaT/hL7k1iKDQLnhz5reGW890I6LXK4?= =?utf-8?q?xBq6XSlhO/R1DaFFFp+3ExW/C3wwBVY0SMFVllDy95a2qAQMYfRumTGpDBk7JHnzQ?= =?utf-8?q?qJbIxbDuF99u+g6CIaDOo6EhXgV0vIUeRxZjD0naEhwPcnUAJo9unrvIkCdX6LuWK?= =?utf-8?q?6Mv7JaRFg7stGx4T3QNi9jd4nnwhpM7GqrZioN8g8DpWeC01u7W7TOyT8YNvrg/b/?= =?utf-8?q?zHRh3RptYf4G?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 593812fc-76e6-4d96-6956-08dc83e54fbe X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:29.0570 (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: zuNUOgAn9lQMLWeV3fNUDmaRoTkorbcV9V5IiaRpjTBtONkjb3EZCJaEAlN+3ocJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 Document the purpose and rules for the fwctl subsystem. Link in kdocs to the doc tree. 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..630e75a91838f0 --- /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 stretches 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 co-operation 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 co-operate 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, or 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 would be 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 5926115ec0ed86..9685942fc8a21f 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -43,6 +43,7 @@ Devices and I/O accelerators/ocxl dma-buf-alloc-exchange + fwctl gpio/index iommu iommufd From patchwork Mon Jun 3 15:53:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683948 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 603161369A4; Mon, 3 Jun 2024 15:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430021; cv=fail; b=AtuQjDmY1duI8srBjvp3z5H6zb07X/L8+o2nQSoEmp+gKG72eFQb6NhE2fyW1PGM7M3LO//b4DtM632kqg1iGwVDNyKrDBbP6gOclUwS8QPyA+PjXPFVHyJ5UvAUB5ifVz71J/URPtDd1sLKByBexpXp3rTIL0SNYKnchGJ9A5Q= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430021; c=relaxed/simple; bh=fwbsMauJGBpqqC6/rfXsMvepiLWAjmtVZN1uirWxISo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Jw6f0sdAYYvHrEhlsjHa8MGomgSxeTUlPZFdKXAQHjT4w4gqYNEpSVzAWzvYdt9Z9P9y+zMtyqr/a5NDqYPQsh5DemQ/gavMs+yLlT2R4J4GRnF15V+njNmRXBCPAKx6uFLnD7vtkuRQllcLTS12r0ArsvNVMvKWHjccavTnKws= 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=gRrv+CDG; arc=fail smtp.client-ip=40.107.220.68 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="gRrv+CDG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Wy43SvdgifFahXqp9rf20B2YtgrfsoR40Zn8dwQyxd/7FTLNUE5OYBS2IZaWz86qcN+v4LLduqPsULU+M35/gynCptrWFT8O+1Aja+v11HxG5P99oD1CBVFFwrLrtlbdpIngV/ukZSmb9JTXt3zga5/udkRaKOHUGgGiP7u3/Shenj5BphsYrfXiCryqDOeyPqLSm6ySu6YOk5ET5xiPSOvjhnAAM8tLb5pPQOcK+PXqiqraT9oY8qADKFJX8+9TClC9aPUE/hIp6fCFjhseoHfim1qCGO8bzCO2vh1KlcDXebUoNGCEs0RpW0laGoJGVB1klYBuLWSZYqEFs+PdEw== 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=MBsVt4IHfkuZMkd24mTDHfp9+FpVCtg5DVR1sp3YVx8=; b=jw0Psy8ffiv5DdSbCJys1wh8fzYy9MZCR3iG/s0HVzK+ZrEyPPk0Ay6K9HV7Yc2COKGb7162NulVjfHgAbYLghnaQ+TJt4DhaFiHJJko7GP3FZiuO8UccR6tBKFL2U8ikhQSX3/IKrSqy0zmypPWAUODnJouz2uVW4cxzKHDoAZjgjrpM/raFOucYE55YSbn1bvhelTghZ2HTlkxjZ0wTnt69WTaJNoVplAOdwii6WK2gqYN6Vk8lEs6KXMQw69zxLvRISm0Yey9PYeo4OzV5zAG1s4jGL+bcucBnI0v6qz0K0ibZHJPDzokJwgFLQ0Zc0LyDlg5UBmU00PGXLaN0w== 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=MBsVt4IHfkuZMkd24mTDHfp9+FpVCtg5DVR1sp3YVx8=; b=gRrv+CDGuAUdL55oNHYbTMefOvKMvsF1rq4EHqgnnN9vaiEaXfiwTJKrewdwswU9YaS0mebUZBxBhJMAgQQ5nkWWsrnjuZwZdEwyQejgWgxQawm9ba1V70opNdz63roMsd3Kh8CSM+HpzYzF+Mil6TYEZEF7WxUN3eg88bisDlcikUMCLkdtzPIYTnmn9tfa8hBmk5H0yyHy4R/HaCy+hYMiZwbI0SO3plbLiqTdoMv64/pZjgFCpXyM087eCyND0EptIW4HuQCv40bP2CgHZsuLfWThdjHTDDbPBZgaAIbfr9dRoUbAECmsiXL6TqKRZLrLFLgMdVDhBHBYrWteIA== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:30 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:30 +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 7/8] fwctl/mlx5: Support for communicating with mlx5 fw Date: Mon, 3 Jun 2024 12:53:23 -0300 Message-ID: <7-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0005.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d1::18) 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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: fdb93356-f8e5-4960-644d-08dc83e54fab X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366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hNGgk8hRA6FvFFCkYlG21sqOsoFy6HSTsBwijV+eD+t55KshqmHe5Z5gtUeUcfzqHyfbUlvknqgllAajXjCbcE/JyWYZFA38PMGoBDCtU7tjuAlu9hWntbQr3sD4g672AyXg0+zDYCtuB8HRpW4GgPfg+Tb2bhENAfYMdXH+ep4nV7XXhQUk3p1QMYMOcaPofyiF4ju//9j79TSujRIz/OQkpcMusAVkG9BAH3LxVowZF+NlmTzufKAY/2SrH+KYfh3Z5Pw7ob9nxEjNQ1yN9YzQ7+O 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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: EBYxErPTW9geptB0ksIacpt4O6xlYl4xsFSSyib8df0FdrOUM4AGVb6JqTJ/pQg0dAGAp/aXtrfE2Fuce1Y8lmDP88kZT9NgcssOwDtSmPu7t/dYckoT8m18N9UQFctsQW8krbvmL/lk3x9pqKvMkGQ6XiX+00SDoIgvFhdRhzJtadI8xHwiL9G5GYRmZc1Felfb41ooVRfVIvynUnnUW9F9KdgUy1wdKD7eh5Kpa1LLkoFIpP2I6KRiOhlqvk1yj2IAH7wNHMCeU0dEEW+0XFGb1peB5F9BvNyVGleTFafnKAotEOOz1bOXt/b1uE1GeOUlOIaMERrgOcm1LseT9TSekjn2jl1SxbhK68IPY3r4ttOE5q4ojUunUBNfqaUe77izFYT0/7fU9IXyPpXGhvnc0UBC/YlcL/gS/RWP7lfdAq8QieF8vrbW5YMHTxtv8WwLHo3lBX+KiaI35UEkd6/zLBWkg4JfvNaNhz45TGat13XYh9pWObbEjuJvGaJgDY3z5kxkCMJ0iYoKV1/3HAsKKQiYnjZX3ynbY2GSQrRDQRnwG5MY8TU2LdHojo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Network-Message-Id: fdb93356-f8e5-4960-644d-08dc83e54fab X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:28.8908 (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: qdwiCZqbCBS9BEyQfffXXg7mAR8B/TGcRIA7x6DAKHrp41ivzxUvds0mu25asjkc X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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 | 333 ++++++++++++++++++++++++++++++++++++ include/uapi/fwctl/fwctl.h | 1 + include/uapi/fwctl/mlx5.h | 36 ++++ 7 files changed, 396 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 94062161e9c4d7..3bd74656d73d5d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9086,6 +9086,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 6ceee3347ae642..d7aa64710146ce 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..d5b751f1e98486 --- /dev/null +++ b/drivers/fwctl/mlx5/main.c @@ -0,0 +1,333 @@ +// 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, +}; + +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; + 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_QUERY_ADAPTER: + case MLX5_CMD_OP_QUERY_ESW_FUNCTIONS: + case MLX5_CMD_OP_QUERY_HCA_CAP: + case MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT: + 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 3 15:53:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13683945 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2068.outbound.protection.outlook.com [40.107.220.68]) (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 14D25132124; Mon, 3 Jun 2024 15:53:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430017; cv=fail; b=HyGZ1nwX+e/WlPmYU0sIHo04fJPYTRnabg05NfKNAV6yCbh6Q3PR+ILXsRyZQZHliyyZO2T5AWrHPj7/hoxB4qSSAOa8LtBoGY+sa49IeiMmuxN1Y4cdWlT9mYoHWegPx5dv6TO9tLfhlDQE/BjH2pUVBpJwrDpvmRsxKEDXIBA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430017; c=relaxed/simple; bh=8OD2Hfx/2G14AFgglBk1QAzWsKVoply2tS4MrhiZpkU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=jvnRkTdVchFg0J3YU4Xtjd/cltMmSp9PYcadtAeS3aL69lCwQUZNW4EBK/RBQDEZvKIQCI+rnuGltHj1i6y25Nv1BtIImRGHm3DORo0xn1NGTEerjfRx6/XJD2vjJYeZkkz6IHQnORL5js0FCZ33tIe35bc2h330GNJBDZXvQKM= 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=hnQdhHdK; arc=fail smtp.client-ip=40.107.220.68 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="hnQdhHdK" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IVqPlOxP5f13fl+dwbu6Ro32C98W0v+cSlCr+pW88CJ4b2WK3nvgd2JU2A7NRtFWEG7zzjCbEXnWbjm01Doi6p84qMkd7TaLA584sc4xtxn55CrSJCxmemHZl/+05t1TgVEQhW3WBywmdGgrydxHVyVtmNN1q6hECYMnrJ026PuXjLUy8ODF/yU5c/uibu18xv4W93UWZgCrTyBzL7DmPBGFV9U881QdYjBwPWbFTJCmwiPxsNpi7vXC8ayhCOe55VZp8NmDqzbZWKn2bsZPhLce6yzpiY364Z4LzkuRGqfBQFTNIVgp+Nwk7V8mPBVrsAvioyGkHRKq43Bkqs0RCw== 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=OqzQ/xnveD/cEx1OFtAfUuX9PkEJiJCqdDsVrFLY4w2S++058lC0MdNd+hq2Q3HiALqZWg1oqHEIdWXCe0DPr7yosGJP2UvjtvByY/ufQa0ZwhuqcA6P5gPzvmXyy6T7KW4hHPI4/BiIKAD+GxYqGCFKAnw/ZnlCeRke1xRm+wzHhhU67qyL1uc4EgfCzO9gpCuQXH8wxVz0Z+YrFUek04LPHVd80jBN0AAHWeCAxb7abX1bK/9xT53DGmmrRHXpS6L65wNdPRNyK5uYicIfoqyU8a24DY1vaE/5OL95rNKL+xbVPHmnHjks06LihxSCEbrgEvsvleZzD6TUeBi6yQ== 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=hnQdhHdK2/aZBRnfJ3fEpNpyIhuQMOmvYOCj1ulPTazgu7XkkdJ9mN9ij7ycqsJHI98OJgKPSiLb0fAF5m4fqpPj2xgc12Ete4kGkChaAHWaEb+SUUN2PxmyOlJ7QRvB7+2RdBg60Opz+CD2hY/KMs97wnif2QiQ0Wui4qrjErM5U0rBKaud0IZsPbyt/UbCdvshfdJ5oez1k/zxPCl1SNaSQhpKzLZnzAXil51e7lN1SvK14Bo/ztCx15cLBjL1re34+PvwXJDy6mc46bBmloh1LEUZ4T8OBV6/FK8qrT+pGebqQrvwjC+IGQpPNGDGw0ev7SdX3rX+enLNORy3Ig== 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 MN0PR12MB6197.namprd12.prod.outlook.com (2603:10b6:208:3c6::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Mon, 3 Jun 2024 15:53:28 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Mon, 3 Jun 2024 15:53:28 +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 8/8] mlx5: Create an auxiliary device for fwctl_mlx5 Date: Mon, 3 Jun 2024 12:53:24 -0300 Message-ID: <8-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> In-Reply-To: <0-v1-9912f1a11620+2a-fwctl_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0132.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d5::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_|MN0PR12MB6197:EE_ X-MS-Office365-Filtering-Correlation-Id: caa30098-1afe-4d44-39db-08dc83e54edf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|366007|376005|921011; X-Microsoft-Antispam-Message-Info: VMgqRTUyGMR98NSXW40ZtN3QbjJLZo7IcJ2Go4ySeMQ6iXiuFbCYKV2OpXhIsvoimvyjgD05uyCqE1o66Cyg4H7aP+QImh7vEYjpVrZsJxsD+DBsaZq1aMfw3AYkTzAAflAbtjjQ6qoDshocbs+RKNnbyIAk69OV15Rk1aY13EwOXU4dNYfX2Wfh8DO8E2lme+v2RrGrOSkre7CsD6X+laFCiZPp4ZQREJm9O3lZUg9nzMBy7OYIR2o14rc9dZfuXLkRQg4UT888izSCNxx3rEdyApXVCOk+RQijTYCakw5lzKdkWfGPY8u5/v8bs70ioxM8BzyHjb+NlrFn1Tc6V/LIq/WL2C2/XVlUJUduJPaKMRUL9ALCQAfzYeU+mxhNIkUGP1GQjGdgm3OeRgkyHq0WfhCYwRMnQ4ypF/fPwFZ7ptmj2AA0URLm/a/nAvptcdszxY1jGbXeLbedF8XCceh6+WEB/TBxV79e5slPuU473W7r1mI6EJL/UCukrMQtI1rbqrYRtCLf4962mT6Xa5aoBHWD6r7TWj5DqGMM0/dkSqp9/azL1Pu/OYmMch9UxZ3NMu22/yjRcBizhpBJU5JYD7Tt7P1n9SAP5viEVhFytDit34XNsw+RwPXUitm/tfQnDv7o9bjFQbNncbu4bH6Wua5O2WwUyMDDmHGmCWpaBP8AeWoVUZzQmwh5eBZnBLR3VsgyOK1k9VfqKlO0yA9d6cHAyT4K9zTofjhSGZjOE2w3eS70b9AKdPc0HOQgSH2FGEL6YcSDyVg9PnG2XxjlH5R1FBjyqQxTl8HN+SBAuT/5PwIRDWAv9YcVLur2zJ5XhnkbVVjbVdQSZKKSfKQdzQZT2iaV2EaX8THhVwzh23Mmr5yxQNLX4S2R+OLjRFrqnlXPRmFgi22MrXpqGEDmEHL/2Po+WZFHzpk+KX+UOZ0KIVKpb0dfpK7xd7s4jFTv6G0QfVHWA8YtXSoGKyvs2K1H3P0jv2PMsVeKOhxRrE9qAIjpZl1Odn1vX+nkm0itJp5UrYVym853KuUutEX1ZMaWLT6Zhgqp9M6SYvFTAVHjeK0qVZs8I2eAdXlRdo01+Tia8g6BdKJUCayI1opG1nRTHmKhgJw+03wtuTodk68Wes7+BzdgXEYHLWW4Oo0E8dJQrxdB4o4aV/aAXmsDilJ4nDOHY7WP92kbzXWRIqQBuRI/Ht2tEpkizdF4Wl1vdkq7GZJ83g1NkPcgJ2s6kCbdSHoO8+477yb+sqkhCKL3S2K57Tr6j93DgphhtV9viMn/dCf/i5vVE35r2eaHF3+5h3Ind2jtlVKtFoBcbY7rQ5XuChwmY0jZuU/u 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:(13230031)(1800799015)(7416005)(366007)(376005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WCCcGwGYT7RZJ9C2vHsHOYK/fJ132pdSsREKbLfI2uIxDJyxOyQN/i1pPqsJYrDnCfSRTk1+4ItXPKMn/0GKAWOJTkRJDcXuBdZxMQHzUIFHJIxkUs4cN1ADcojf3uF0u/lwzGUETxsgul+LUZ0GaLvQVEMg/X5aYNhfvE+yqnsUNQ4tHv1yZoWvMRCg7/CrqH6N7mDMRofpc9fpOK34UZL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O3i5PPqz64aK5fpZOwxXuXS2yuvwlwJAAFgpM7s7uGSaLU+CAOCQJiAtQPADbHttsEzDFi6We6zBN2QnCLiVy/PlZez5Oit8MtPmzcPDu1er3lBjy9QKhs5n1xK822nKjkINFxrBU05eWDv5LM6Jw367SGcbSB7KG6OwYuSldhSUrsTBcMkeKc11Eu8x/TNpwiLvhICW4EWqeA363ZSPKo/mudGP5mbR7LpXHwlBaVPo8g4CTWEFEZMyYLiW6f+F53lK+zCsfuRZAzMTYI+CL8W9iT97WW97Wer2nF X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: caa30098-1afe-4d44-39db-08dc83e54edf X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 15:53:27.4995 (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: 2k9YKqF/oop0NEkdguYs28VumxJonegN0Z5CiEUzlUlQE79VoUzXXaZjCa5vjgZ0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6197 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)