Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
0004  */
0005 #include <linux/vmalloc.h>
0006 #include <linux/module.h>
0007 #include <linux/device.h>
0008 #include <linux/sizes.h>
0009 #include <linux/ndctl.h>
0010 #include <linux/slab.h>
0011 #include <linux/mm.h>
0012 #include <linux/nd.h>
0013 #include "label.h"
0014 #include "nd.h"
0015 
0016 static int nvdimm_probe(struct device *dev)
0017 {
0018     struct nvdimm_drvdata *ndd;
0019     int rc;
0020 
0021     rc = nvdimm_security_setup_events(dev);
0022     if (rc < 0) {
0023         dev_err(dev, "security event setup failed: %d\n", rc);
0024         return rc;
0025     }
0026 
0027     rc = nvdimm_check_config_data(dev);
0028     if (rc) {
0029         /* not required for non-aliased nvdimm, ex. NVDIMM-N */
0030         if (rc == -ENOTTY)
0031             rc = 0;
0032         return rc;
0033     }
0034 
0035     /*
0036      * The locked status bit reflects explicit status codes from the
0037      * label reading commands, revalidate it each time the driver is
0038      * activated and re-reads the label area.
0039      */
0040     nvdimm_clear_locked(dev);
0041 
0042     ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
0043     if (!ndd)
0044         return -ENOMEM;
0045 
0046     dev_set_drvdata(dev, ndd);
0047     ndd->dpa.name = dev_name(dev);
0048     ndd->ns_current = -1;
0049     ndd->ns_next = -1;
0050     ndd->dpa.start = 0;
0051     ndd->dpa.end = -1;
0052     ndd->dev = dev;
0053     get_device(dev);
0054     kref_init(&ndd->kref);
0055 
0056     /*
0057      * Attempt to unlock, if the DIMM supports security commands,
0058      * otherwise the locked indication is determined by explicit
0059      * status codes from the label reading commands.
0060      */
0061     rc = nvdimm_security_unlock(dev);
0062     if (rc < 0)
0063         dev_dbg(dev, "failed to unlock dimm: %d\n", rc);
0064 
0065 
0066     /*
0067      * EACCES failures reading the namespace label-area-properties
0068      * are interpreted as the DIMM capacity being locked but the
0069      * namespace labels themselves being accessible.
0070      */
0071     rc = nvdimm_init_nsarea(ndd);
0072     if (rc == -EACCES) {
0073         /*
0074          * See nvdimm_namespace_common_probe() where we fail to
0075          * allow namespaces to probe while the DIMM is locked,
0076          * but we do allow for namespace enumeration.
0077          */
0078         nvdimm_set_locked(dev);
0079         rc = 0;
0080     }
0081     if (rc)
0082         goto err;
0083 
0084     /*
0085      * EACCES failures reading the namespace label-data are
0086      * interpreted as the label area being locked in addition to the
0087      * DIMM capacity. We fail the dimm probe to prevent regions from
0088      * attempting to parse the label area.
0089      */
0090     rc = nd_label_data_init(ndd);
0091     if (rc == -EACCES)
0092         nvdimm_set_locked(dev);
0093     if (rc)
0094         goto err;
0095 
0096     dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
0097 
0098     nvdimm_bus_lock(dev);
0099     if (ndd->ns_current >= 0) {
0100         rc = nd_label_reserve_dpa(ndd);
0101         if (rc == 0)
0102             nvdimm_set_labeling(dev);
0103     }
0104     nvdimm_bus_unlock(dev);
0105 
0106     if (rc)
0107         goto err;
0108 
0109     return 0;
0110 
0111  err:
0112     put_ndd(ndd);
0113     return rc;
0114 }
0115 
0116 static void nvdimm_remove(struct device *dev)
0117 {
0118     struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
0119 
0120     nvdimm_bus_lock(dev);
0121     dev_set_drvdata(dev, NULL);
0122     nvdimm_bus_unlock(dev);
0123     put_ndd(ndd);
0124 }
0125 
0126 static struct nd_device_driver nvdimm_driver = {
0127     .probe = nvdimm_probe,
0128     .remove = nvdimm_remove,
0129     .drv = {
0130         .name = "nvdimm",
0131     },
0132     .type = ND_DRIVER_DIMM,
0133 };
0134 
0135 int __init nvdimm_init(void)
0136 {
0137     return nd_driver_register(&nvdimm_driver);
0138 }
0139 
0140 void nvdimm_exit(void)
0141 {
0142     driver_unregister(&nvdimm_driver.drv);
0143 }
0144 
0145 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);