Dan Williams | e6dfb2d | 2015-04-25 03:56:17 -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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include "nd-core.h" |
| 20 | |
| 21 | static DEFINE_IDA(dimm_ida); |
| 22 | |
| 23 | static void nvdimm_release(struct device *dev) |
| 24 | { |
| 25 | struct nvdimm *nvdimm = to_nvdimm(dev); |
| 26 | |
| 27 | ida_simple_remove(&dimm_ida, nvdimm->id); |
| 28 | kfree(nvdimm); |
| 29 | } |
| 30 | |
| 31 | static struct device_type nvdimm_device_type = { |
| 32 | .name = "nvdimm", |
| 33 | .release = nvdimm_release, |
| 34 | }; |
| 35 | |
| 36 | static bool is_nvdimm(struct device *dev) |
| 37 | { |
| 38 | return dev->type == &nvdimm_device_type; |
| 39 | } |
| 40 | |
| 41 | struct nvdimm *to_nvdimm(struct device *dev) |
| 42 | { |
| 43 | struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev); |
| 44 | |
| 45 | WARN_ON(!is_nvdimm(dev)); |
| 46 | return nvdimm; |
| 47 | } |
| 48 | EXPORT_SYMBOL_GPL(to_nvdimm); |
| 49 | |
| 50 | const char *nvdimm_name(struct nvdimm *nvdimm) |
| 51 | { |
| 52 | return dev_name(&nvdimm->dev); |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(nvdimm_name); |
| 55 | |
| 56 | void *nvdimm_provider_data(struct nvdimm *nvdimm) |
| 57 | { |
| 58 | return nvdimm->provider_data; |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(nvdimm_provider_data); |
| 61 | |
| 62 | struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data, |
| 63 | const struct attribute_group **groups, unsigned long flags) |
| 64 | { |
| 65 | struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL); |
| 66 | struct device *dev; |
| 67 | |
| 68 | if (!nvdimm) |
| 69 | return NULL; |
| 70 | |
| 71 | nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL); |
| 72 | if (nvdimm->id < 0) { |
| 73 | kfree(nvdimm); |
| 74 | return NULL; |
| 75 | } |
| 76 | nvdimm->provider_data = provider_data; |
| 77 | nvdimm->flags = flags; |
| 78 | |
| 79 | dev = &nvdimm->dev; |
| 80 | dev_set_name(dev, "nmem%d", nvdimm->id); |
| 81 | dev->parent = &nvdimm_bus->dev; |
| 82 | dev->type = &nvdimm_device_type; |
| 83 | dev->bus = &nvdimm_bus_type; |
| 84 | dev->groups = groups; |
| 85 | if (device_register(dev) != 0) { |
| 86 | put_device(dev); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | return nvdimm; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(nvdimm_create); |