From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74291C2D0EC for ; Mon, 30 Mar 2020 05:53:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 302F82078B for ; Mon, 30 Mar 2020 05:53:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (4096-bit key) header.d=d-silva.org header.i=@d-silva.org header.b="DcslQM+q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 302F82078B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=d-silva.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D64BF6B0070; Mon, 30 Mar 2020 01:53:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D15886B0071; Mon, 30 Mar 2020 01:53:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2CA76B0072; Mon, 30 Mar 2020 01:53:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0008.hostedemail.com [216.40.44.8]) by kanga.kvack.org (Postfix) with ESMTP id AA8F66B0070 for ; Mon, 30 Mar 2020 01:53:16 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 6921E443F for ; Mon, 30 Mar 2020 05:53:16 +0000 (UTC) X-FDA: 76650960792.05.mice89_56bc1fcf2992b X-HE-Tag: mice89_56bc1fcf2992b X-Filterd-Recvd-Size: 8998 Received: from ushosting.nmnhosting.com (ushosting.nmnhosting.com [66.55.73.32]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Mon, 30 Mar 2020 05:53:15 +0000 (UTC) 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 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) Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 *ocxlpm= em) return device_register(&ocxlpmem->dev); } =20 +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 =3D MINOR(devno); + + mutex_lock(&minors_idr_lock); + ocxlpmem =3D 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 =3D find_and_get_ocxlpmem(inode->i_rdev); + if (!ocxlpmem) + return -ENODEV; + + file->private_data =3D ocxlpmem; + return 0; +} + +static int file_release(struct inode *inode, struct file *file) +{ + struct ocxlpmem *ocxlpmem =3D file->private_data; + + ocxlpmem_put(ocxlpmem); + return 0; +} + +static const struct file_operations fops =3D { + .owner =3D THIS_MODULE, + .open =3D file_open, + .release =3D 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); =20 + /* + * 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; } =20 - if (setup_command_metadata(ocxlpmem)) { + rc =3D setup_command_metadata(ocxlpmem); + if (rc) { dev_err(&pdev->dev, "Could not read command metadata\n"); goto err; } =20 + rc =3D create_cdev(ocxlpmem); + if (rc) { + dev_err(&pdev->dev, "Could not create character device\n"); + goto err; + } + elapsed =3D 0; timeout =3D ocxlpmem->readiness_timeout + ocxlpmem->memory_available_timeout; @@ -599,6 +675,36 @@ static struct pci_driver pci_driver =3D { .shutdown =3D remove, }; =20 +static int file_init(void) +{ + int rc; + + rc =3D 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 =3D 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); =20 - rc =3D pci_register_driver(&pci_driver); + rc =3D file_init(); if (rc) return rc; =20 + rc =3D pci_register_driver(&pci_driver); + if (rc) { + file_exit(); + return rc; + } + return 0; } =20 static void ocxlpmem_exit(void) { pci_unregister_driver(&pci_driver); + file_exit(); } =20 module_init(ocxlpmem_init); diff --git a/drivers/nvdimm/ocxl/ocxlpmem.h b/drivers/nvdimm/ocxl/ocxlpme= m.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. =20 #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; --=20 2.24.1