Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/vmalloc.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/sizes.h> |
| 17 | #include <linux/ndctl.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/nd.h> |
| 21 | #include "nd.h" |
| 22 | |
| 23 | static void free_data(struct nvdimm_drvdata *ndd) |
| 24 | { |
| 25 | if (!ndd) |
| 26 | return; |
| 27 | |
| 28 | if (ndd->data && is_vmalloc_addr(ndd->data)) |
| 29 | vfree(ndd->data); |
| 30 | else |
| 31 | kfree(ndd->data); |
| 32 | kfree(ndd); |
| 33 | } |
| 34 | |
| 35 | static int nvdimm_probe(struct device *dev) |
| 36 | { |
| 37 | struct nvdimm_drvdata *ndd; |
| 38 | int rc; |
| 39 | |
| 40 | ndd = kzalloc(sizeof(*ndd), GFP_KERNEL); |
| 41 | if (!ndd) |
| 42 | return -ENOMEM; |
| 43 | |
| 44 | dev_set_drvdata(dev, ndd); |
| 45 | ndd->dev = dev; |
| 46 | |
| 47 | rc = nvdimm_init_nsarea(ndd); |
| 48 | if (rc) |
| 49 | goto err; |
| 50 | |
| 51 | rc = nvdimm_init_config_data(ndd); |
| 52 | if (rc) |
| 53 | goto err; |
| 54 | |
| 55 | dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size); |
| 56 | |
| 57 | return 0; |
| 58 | |
| 59 | err: |
| 60 | free_data(ndd); |
| 61 | return rc; |
| 62 | } |
| 63 | |
| 64 | static int nvdimm_remove(struct device *dev) |
| 65 | { |
| 66 | struct nvdimm_drvdata *ndd = dev_get_drvdata(dev); |
| 67 | |
| 68 | free_data(ndd); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static struct nd_device_driver nvdimm_driver = { |
| 74 | .probe = nvdimm_probe, |
| 75 | .remove = nvdimm_remove, |
| 76 | .drv = { |
| 77 | .name = "nvdimm", |
| 78 | }, |
| 79 | .type = ND_DRIVER_DIMM, |
| 80 | }; |
| 81 | |
| 82 | int __init nvdimm_init(void) |
| 83 | { |
| 84 | return nd_driver_register(&nvdimm_driver); |
| 85 | } |
| 86 | |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame^] | 87 | void nvdimm_exit(void) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 88 | { |
| 89 | driver_unregister(&nvdimm_driver.drv); |
| 90 | } |
| 91 | |
| 92 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM); |