Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2109697ybb; Sun, 29 Mar 2020 22:54:30 -0700 (PDT) X-Google-Smtp-Source: ADFU+vs2L3weeVhNCTicdtBrSFL9FJKXfMffcMEyDegca6aHhQyICl+XYHqjUg35AuuG7rwqFtes X-Received: by 2002:a9d:4f0:: with SMTP id 103mr7813525otm.336.1585547670001; Sun, 29 Mar 2020 22:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585547669; cv=none; d=google.com; s=arc-20160816; b=u6Ue6csCKt22e+0q5ZOLeAhyCFWO5XeDPUnxbovROStsNBZMaYS7uYt+iVgBvf5xxI jSNS2vc+KlfKpcnALHksuI7d3TSSBaCXrdT2s1BmhVi+vMNoKpoORDStiZpXNZzVA5ml L36wR/1gFQS56Nhe1lVCxrWDg92nMRgRICLUhSi8f+eo6/KFbVE24DusIxsKGYrEgmD4 vgfrF7tYN+/sWt5LnJjULL9YpCvYvPm3YvpaWgFt3hZtf9Mw6d6M3ld+mBkBjjDBQlyA Hd4K9D7UpQqM0AGBzT5t6N5dYuWMX4Wn2/EF0yxp31JBCfzYQoO5STXtJt9x5qUufu4F 7gVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tznaYg9La+sKgb/0ecNfYID8HVwEYaqs5KvqkHhXNY0=; b=cLCEsyDlawKjKdNKvJ3UrXPWwPy4sPluQnsJXOY0W+PHwaMYdniZLcNEJASZBQxF56 upb8c3LqQlWiwN+tXMV8/CXuSrUT7w/fXJmGdL3nt7Zv22Nkb7bymCmrzJ70rCsKAEix kn1X2ENYAmzuCJylb+m0Rq6JW7BmNe94FFkkuS0K2NI4B/6KPMINejhn9ua1GU2/oF7W G9EOePJt6yL44QX+ZQvcesLHVvPSetMnkS0RpPcMSkT0K0Vz4IzoFVz/aOjOUP2drwvx N8NhaqlzptksyNedRvNSspgQeuLqqtZ5utrocC2ZbwaX2or7Ylhms2gXaDxb6l4BFYiq L8bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@d-silva.org header.s=201810a header.b=DcslQM+q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 193si5272983oie.51.2020.03.29.22.54.17; Sun, 29 Mar 2020 22:54:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@d-silva.org header.s=201810a header.b=DcslQM+q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728922AbgC3FxS (ORCPT + 99 others); Mon, 30 Mar 2020 01:53:18 -0400 Received: from ushosting.nmnhosting.com ([66.55.73.32]:37666 "EHLO ushosting.nmnhosting.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728492AbgC3FxR (ORCPT ); Mon, 30 Mar 2020 01:53:17 -0400 Received: from mail2.nmnhosting.com (unknown [202.169.106.97]) by ushosting.nmnhosting.com (Postfix) with ESMTPS id 32B862DC6860; Mon, 30 Mar 2020 16:52:54 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=d-silva.org; s=201810a; t=1585547574; bh=UtTeCR+k2Y01sGISBjlacY9KQsMxyTaomboXrU4wwyo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DcslQM+qGci9qChZilaQA/DfS7pIdT/bNqHpbmQhKkju0pgXMUz8JD90Cz+H1bOZ/ amHXQtG9t9iPoAPZuinrZD7AwVpmAv1jXyLxSdfobhFwpgpUM0K6qXy+IAmaP+HmlE TNOYm4ybNsk99pct5O+F75N7mUe3GbKAMkrk8zM0bR7/KJdH31MXZ2m7M39Aa0aERC hhBsydX+iRUw3CN/F63pYnhDAkKdFRLC7xbyoEEdnEDLEc7Igxlv0Of84H5ld7cps0 V0O9+JZZ2QIz8gyRAvAlapeKh5K8RWoV3B9FGc30JME4hgJGuzP427kc5vGzXjEH5K owUqeLtXwXVJDdQmdwFJCqkbEpULxf9gwx8QvS5clbc3TJNV4IZ6zQDxSjW7H/+yni p7+SHPCkLZZZhNXt04DvM8qX26uChSVK6PmzZI5tnv7hQSlx4XtQxD11dePDdL42r1 fDYadY3krb5KndhtNhvCfr1WBs7e97qb4o94/aLW6iqXGP3/FzACm/xRJT/ctSx5kc y/c+jC1pb1s+PJWQOXQuxvUmL1yb7VAP+LmMsMHoSQ9prJ0i+wFPws4FtxKpAkFTyi 0meqZfMRKeTShg5TSQuGOSjNVPBiPoAXGTDoW4TqYnZ+7azYSrqrMhWVYgnLWN69xR 8KL4NoIcEOzKu9p7busd2CuE= Received: from localhost.lan ([10.0.1.179]) by mail2.nmnhosting.com (8.15.2/8.15.2) with ESMTP id 02R7C4Ao045934; Fri, 27 Mar 2020 18:12:18 +1100 (AEDT) (envelope-from alastair@d-silva.org) From: "Alastair D'Silva" To: alastair@d-silva.org Cc: "Aneesh Kumar K . V" , "Oliver O'Halloran" , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Frederic Barrat , Andrew Donnellan , Arnd Bergmann , Greg Kroah-Hartman , Dan Williams , Vishal Verma , Dave Jiang , Ira Weiny , Andrew Morton , Mauro Carvalho Chehab , "David S. Miller" , Rob Herring , Anton Blanchard , Krzysztof Kozlowski , Mahesh Salgaonkar , Madhavan Srinivasan , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Anju T Sudhakar , Hari Bathini , Thomas Gleixner , Greg Kurz , Nicholas Piggin , Masahiro Yamada , Alexey Kardashevskiy , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-nvdimm@lists.01.org, linux-mm@kvack.org Subject: [PATCH v4 15/25] nvdimm/ocxl: Register a character device for userspace to interact with Date: Fri, 27 Mar 2020 18:11:52 +1100 Message-Id: <20200327071202.2159885-16-alastair@d-silva.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200327071202.2159885-1-alastair@d-silva.org> References: <20200327071202.2159885-1-alastair@d-silva.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mail2.nmnhosting.com [10.0.1.20]); Fri, 27 Mar 2020 18:12:18 +1100 (AEDT) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces a character device (/dev/ocxlpmemX) which further patches will use to interact with userspace, such as error logs, controller stats and card debug functionality. Signed-off-by: Alastair D'Silva --- drivers/nvdimm/ocxl/main.c | 117 ++++++++++++++++++++++++++++++++- drivers/nvdimm/ocxl/ocxlpmem.h | 2 + 2 files changed, 117 insertions(+), 2 deletions(-) diff --git a/drivers/nvdimm/ocxl/main.c b/drivers/nvdimm/ocxl/main.c index 8db573036423..9b85fcd3f1c9 100644 --- a/drivers/nvdimm/ocxl/main.c +++ b/drivers/nvdimm/ocxl/main.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include "ocxlpmem.h" @@ -356,6 +357,67 @@ static int ocxlpmem_register(struct ocxlpmem *ocxlpmem) return device_register(&ocxlpmem->dev); } +static void ocxlpmem_put(struct ocxlpmem *ocxlpmem) +{ + put_device(&ocxlpmem->dev); +} + +static struct ocxlpmem *ocxlpmem_get(struct ocxlpmem *ocxlpmem) +{ + return (!get_device(&ocxlpmem->dev)) ? NULL : ocxlpmem; +} + +static struct ocxlpmem *find_and_get_ocxlpmem(dev_t devno) +{ + struct ocxlpmem *ocxlpmem; + int minor = MINOR(devno); + + mutex_lock(&minors_idr_lock); + ocxlpmem = idr_find(&minors_idr, minor); + if (ocxlpmem) + ocxlpmem_get(ocxlpmem); + mutex_unlock(&minors_idr_lock); + + return ocxlpmem; +} + +static int file_open(struct inode *inode, struct file *file) +{ + struct ocxlpmem *ocxlpmem; + + ocxlpmem = find_and_get_ocxlpmem(inode->i_rdev); + if (!ocxlpmem) + return -ENODEV; + + file->private_data = ocxlpmem; + return 0; +} + +static int file_release(struct inode *inode, struct file *file) +{ + struct ocxlpmem *ocxlpmem = file->private_data; + + ocxlpmem_put(ocxlpmem); + return 0; +} + +static const struct file_operations fops = { + .owner = THIS_MODULE, + .open = file_open, + .release = file_release, +}; + +/** + * create_cdev() - Create the chardev in /dev for the device + * @ocxlpmem: the SCM metadata + * Return: 0 on success, negative on failure + */ +static int create_cdev(struct ocxlpmem *ocxlpmem) +{ + cdev_init(&ocxlpmem->cdev, &fops); + return cdev_add(&ocxlpmem->cdev, ocxlpmem->dev.devt, 1); +} + /** * ocxlpmem_remove() - Free an OpenCAPI persistent memory device * @pdev: the PCI device information struct @@ -376,6 +438,13 @@ static void remove(struct pci_dev *pdev) if (ocxlpmem->nvdimm_bus) nvdimm_bus_unregister(ocxlpmem->nvdimm_bus); + /* + * Remove the cdev early to prevent a race against userspace + * via the char dev + */ + if (ocxlpmem->cdev.owner) + cdev_del(&ocxlpmem->cdev); + device_unregister(&ocxlpmem->dev); } } @@ -527,11 +596,18 @@ static int probe(struct pci_dev *pdev, const struct pci_device_id *ent) goto err; } - if (setup_command_metadata(ocxlpmem)) { + rc = setup_command_metadata(ocxlpmem); + if (rc) { dev_err(&pdev->dev, "Could not read command metadata\n"); goto err; } + rc = create_cdev(ocxlpmem); + if (rc) { + dev_err(&pdev->dev, "Could not create character device\n"); + goto err; + } + elapsed = 0; timeout = ocxlpmem->readiness_timeout + ocxlpmem->memory_available_timeout; @@ -599,6 +675,36 @@ static struct pci_driver pci_driver = { .shutdown = remove, }; +static int file_init(void) +{ + int rc; + + rc = alloc_chrdev_region(&ocxlpmem_dev, 0, NUM_MINORS, "ocxlpmem"); + if (rc) { + idr_destroy(&minors_idr); + pr_err("Unable to allocate OpenCAPI persistent memory major number: %d\n", + rc); + return rc; + } + + ocxlpmem_class = class_create(THIS_MODULE, "ocxlpmem"); + if (IS_ERR(ocxlpmem_class)) { + idr_destroy(&minors_idr); + pr_err("Unable to create ocxlpmem class\n"); + unregister_chrdev_region(ocxlpmem_dev, NUM_MINORS); + return PTR_ERR(ocxlpmem_class); + } + + return 0; +} + +static void file_exit(void) +{ + class_destroy(ocxlpmem_class); + unregister_chrdev_region(ocxlpmem_dev, NUM_MINORS); + idr_destroy(&minors_idr); +} + static int __init ocxlpmem_init(void) { int rc; @@ -606,16 +712,23 @@ static int __init ocxlpmem_init(void) mutex_init(&minors_idr_lock); idr_init(&minors_idr); - rc = pci_register_driver(&pci_driver); + rc = file_init(); if (rc) return rc; + rc = pci_register_driver(&pci_driver); + if (rc) { + file_exit(); + return rc; + } + return 0; } static void ocxlpmem_exit(void) { pci_unregister_driver(&pci_driver); + file_exit(); } module_init(ocxlpmem_init); diff --git a/drivers/nvdimm/ocxl/ocxlpmem.h b/drivers/nvdimm/ocxl/ocxlpmem.h index b72b3f909fc3..ee3bd651f254 100644 --- a/drivers/nvdimm/ocxl/ocxlpmem.h +++ b/drivers/nvdimm/ocxl/ocxlpmem.h @@ -2,6 +2,7 @@ // Copyright 2020 IBM Corp. #include +#include #include #include #include @@ -103,6 +104,7 @@ struct command_metadata { struct ocxlpmem { struct device dev; struct pci_dev *pdev; + struct cdev cdev; struct ocxl_fn *ocxl_fn; struct nd_interleave_set nd_set; struct nvdimm_bus_descriptor bus_desc; -- 2.24.1