|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 3 | + |
| 4 | +#include <linux/platform_device.h> |
| 5 | +#include <linux/device.h> |
| 6 | +#include <linux/module.h> |
| 7 | +#include <linux/genalloc.h> |
| 8 | +#include <linux/vmalloc.h> |
| 9 | +#include <linux/dma-mapping.h> |
| 10 | +#include <linux/list_sort.h> |
| 11 | +#include <linux/libnvdimm.h> |
| 12 | +#include <linux/ndctl.h> |
| 13 | +#include <nd-core.h> |
| 14 | +#include <linux/printk.h> |
| 15 | +#include <linux/seq_buf.h> |
| 16 | + |
| 17 | +#include "../watermark.h" |
| 18 | +#include "nfit_test.h" |
| 19 | +#include "ndtest.h" |
| 20 | + |
| 21 | +enum { |
| 22 | + DIMM_SIZE = SZ_32M, |
| 23 | + LABEL_SIZE = SZ_128K, |
| 24 | + NUM_INSTANCES = 2, |
| 25 | + NUM_DCR = 4, |
| 26 | +}; |
| 27 | + |
| 28 | +static struct ndtest_priv *instances[NUM_INSTANCES]; |
| 29 | +static struct class *ndtest_dimm_class; |
| 30 | + |
| 31 | +static inline struct ndtest_priv *to_ndtest_priv(struct device *dev) |
| 32 | +{ |
| 33 | + struct platform_device *pdev = to_platform_device(dev); |
| 34 | + |
| 35 | + return container_of(pdev, struct ndtest_priv, pdev); |
| 36 | +} |
| 37 | + |
| 38 | +static int ndtest_ctl(struct nvdimm_bus_descriptor *nd_desc, |
| 39 | + struct nvdimm *nvdimm, unsigned int cmd, void *buf, |
| 40 | + unsigned int buf_len, int *cmd_rc) |
| 41 | +{ |
| 42 | + struct ndtest_dimm *dimm; |
| 43 | + int _cmd_rc; |
| 44 | + |
| 45 | + if (!cmd_rc) |
| 46 | + cmd_rc = &_cmd_rc; |
| 47 | + |
| 48 | + *cmd_rc = 0; |
| 49 | + |
| 50 | + if (!nvdimm) |
| 51 | + return -EINVAL; |
| 52 | + |
| 53 | + dimm = nvdimm_provider_data(nvdimm); |
| 54 | + if (!dimm) |
| 55 | + return -EINVAL; |
| 56 | + |
| 57 | + switch (cmd) { |
| 58 | + case ND_CMD_GET_CONFIG_SIZE: |
| 59 | + case ND_CMD_GET_CONFIG_DATA: |
| 60 | + case ND_CMD_SET_CONFIG_DATA: |
| 61 | + default: |
| 62 | + return -EINVAL; |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +static int ndtest_bus_register(struct ndtest_priv *p) |
| 69 | +{ |
| 70 | + p->bus_desc.ndctl = ndtest_ctl; |
| 71 | + p->bus_desc.module = THIS_MODULE; |
| 72 | + p->bus_desc.provider_name = NULL; |
| 73 | + |
| 74 | + p->bus = nvdimm_bus_register(&p->pdev.dev, &p->bus_desc); |
| 75 | + if (!p->bus) { |
| 76 | + dev_err(&p->pdev.dev, "Error creating nvdimm bus %pOF\n", p->dn); |
| 77 | + return -ENOMEM; |
| 78 | + } |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static int ndtest_remove(struct platform_device *pdev) |
| 84 | +{ |
| 85 | + struct ndtest_priv *p = to_ndtest_priv(&pdev->dev); |
| 86 | + |
| 87 | + nvdimm_bus_unregister(p->bus); |
| 88 | + return 0; |
| 89 | +} |
| 90 | + |
| 91 | +static int ndtest_probe(struct platform_device *pdev) |
| 92 | +{ |
| 93 | + struct ndtest_priv *p; |
| 94 | + |
| 95 | + p = to_ndtest_priv(&pdev->dev); |
| 96 | + if (ndtest_bus_register(p)) |
| 97 | + return -ENOMEM; |
| 98 | + |
| 99 | + platform_set_drvdata(pdev, p); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +static const struct platform_device_id ndtest_id[] = { |
| 105 | + { KBUILD_MODNAME }, |
| 106 | + { }, |
| 107 | +}; |
| 108 | + |
| 109 | +static struct platform_driver ndtest_driver = { |
| 110 | + .probe = ndtest_probe, |
| 111 | + .remove = ndtest_remove, |
| 112 | + .driver = { |
| 113 | + .name = KBUILD_MODNAME, |
| 114 | + }, |
| 115 | + .id_table = ndtest_id, |
| 116 | +}; |
| 117 | + |
| 118 | +static void ndtest_release(struct device *dev) |
| 119 | +{ |
| 120 | + struct ndtest_priv *p = to_ndtest_priv(dev); |
| 121 | + |
| 122 | + kfree(p); |
| 123 | +} |
| 124 | + |
| 125 | +static void cleanup_devices(void) |
| 126 | +{ |
| 127 | + int i; |
| 128 | + |
| 129 | + for (i = 0; i < NUM_INSTANCES; i++) |
| 130 | + if (instances[i]) |
| 131 | + platform_device_unregister(&instances[i]->pdev); |
| 132 | + |
| 133 | + nfit_test_teardown(); |
| 134 | + |
| 135 | + if (ndtest_dimm_class) |
| 136 | + class_destroy(ndtest_dimm_class); |
| 137 | +} |
| 138 | + |
| 139 | +static __init int ndtest_init(void) |
| 140 | +{ |
| 141 | + int rc, i; |
| 142 | + |
| 143 | + pmem_test(); |
| 144 | + libnvdimm_test(); |
| 145 | + device_dax_test(); |
| 146 | + dax_pmem_test(); |
| 147 | + dax_pmem_core_test(); |
| 148 | +#ifdef CONFIG_DEV_DAX_PMEM_COMPAT |
| 149 | + dax_pmem_compat_test(); |
| 150 | +#endif |
| 151 | + |
| 152 | + ndtest_dimm_class = class_create(THIS_MODULE, "nfit_test_dimm"); |
| 153 | + if (IS_ERR(ndtest_dimm_class)) { |
| 154 | + rc = PTR_ERR(ndtest_dimm_class); |
| 155 | + goto err_register; |
| 156 | + } |
| 157 | + |
| 158 | + /* Each instance can be taken as a bus, which can have multiple dimms */ |
| 159 | + for (i = 0; i < NUM_INSTANCES; i++) { |
| 160 | + struct ndtest_priv *priv; |
| 161 | + struct platform_device *pdev; |
| 162 | + |
| 163 | + priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 164 | + if (!priv) { |
| 165 | + rc = -ENOMEM; |
| 166 | + goto err_register; |
| 167 | + } |
| 168 | + |
| 169 | + INIT_LIST_HEAD(&priv->resources); |
| 170 | + pdev = &priv->pdev; |
| 171 | + pdev->name = KBUILD_MODNAME; |
| 172 | + pdev->id = i; |
| 173 | + pdev->dev.release = ndtest_release; |
| 174 | + rc = platform_device_register(pdev); |
| 175 | + if (rc) { |
| 176 | + put_device(&pdev->dev); |
| 177 | + goto err_register; |
| 178 | + } |
| 179 | + get_device(&pdev->dev); |
| 180 | + |
| 181 | + instances[i] = priv; |
| 182 | + } |
| 183 | + |
| 184 | + rc = platform_driver_register(&ndtest_driver); |
| 185 | + if (rc) |
| 186 | + goto err_register; |
| 187 | + |
| 188 | + return 0; |
| 189 | + |
| 190 | +err_register: |
| 191 | + pr_err("Error registering platform device\n"); |
| 192 | + cleanup_devices(); |
| 193 | + |
| 194 | + return rc; |
| 195 | +} |
| 196 | + |
| 197 | +static __exit void ndtest_exit(void) |
| 198 | +{ |
| 199 | + cleanup_devices(); |
| 200 | + platform_driver_unregister(&ndtest_driver); |
| 201 | +} |
| 202 | + |
| 203 | +module_init(ndtest_init); |
| 204 | +module_exit(ndtest_exit); |
| 205 | +MODULE_LICENSE("GPL"); |
| 206 | +MODULE_AUTHOR("IBM Corporation"); |
0 commit comments