dimm_devs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/vmalloc.h>
  15. #include <linux/device.h>
  16. #include <linux/ndctl.h>
  17. #include <linux/slab.h>
  18. #include <linux/io.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include "nd-core.h"
  22. #include "label.h"
  23. #include "nd.h"
  24. static DEFINE_IDA(dimm_ida);
  25. /*
  26. * Retrieve bus and dimm handle and return if this bus supports
  27. * get_config_data commands
  28. */
  29. static int __validate_dimm(struct nvdimm_drvdata *ndd)
  30. {
  31. struct nvdimm *nvdimm;
  32. if (!ndd)
  33. return -EINVAL;
  34. nvdimm = to_nvdimm(ndd->dev);
  35. if (!nvdimm->dsm_mask)
  36. return -ENXIO;
  37. if (!test_bit(ND_CMD_GET_CONFIG_DATA, nvdimm->dsm_mask))
  38. return -ENXIO;
  39. return 0;
  40. }
  41. static int validate_dimm(struct nvdimm_drvdata *ndd)
  42. {
  43. int rc = __validate_dimm(ndd);
  44. if (rc && ndd)
  45. dev_dbg(ndd->dev, "%pf: %s error: %d\n",
  46. __builtin_return_address(0), __func__, rc);
  47. return rc;
  48. }
  49. /**
  50. * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
  51. * @nvdimm: dimm to initialize
  52. */
  53. int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
  54. {
  55. struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
  56. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  57. struct nvdimm_bus_descriptor *nd_desc;
  58. int rc = validate_dimm(ndd);
  59. if (rc)
  60. return rc;
  61. if (cmd->config_size)
  62. return 0; /* already valid */
  63. memset(cmd, 0, sizeof(*cmd));
  64. nd_desc = nvdimm_bus->nd_desc;
  65. return nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  66. ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd));
  67. }
  68. int nvdimm_init_config_data(struct nvdimm_drvdata *ndd)
  69. {
  70. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  71. struct nd_cmd_get_config_data_hdr *cmd;
  72. struct nvdimm_bus_descriptor *nd_desc;
  73. int rc = validate_dimm(ndd);
  74. u32 max_cmd_size, config_size;
  75. size_t offset;
  76. if (rc)
  77. return rc;
  78. if (ndd->data)
  79. return 0;
  80. if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0
  81. || ndd->nsarea.config_size < ND_LABEL_MIN_SIZE) {
  82. dev_dbg(ndd->dev, "failed to init config data area: (%d:%d)\n",
  83. ndd->nsarea.max_xfer, ndd->nsarea.config_size);
  84. return -ENXIO;
  85. }
  86. ndd->data = kmalloc(ndd->nsarea.config_size, GFP_KERNEL);
  87. if (!ndd->data)
  88. ndd->data = vmalloc(ndd->nsarea.config_size);
  89. if (!ndd->data)
  90. return -ENOMEM;
  91. max_cmd_size = min_t(u32, PAGE_SIZE, ndd->nsarea.max_xfer);
  92. cmd = kzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
  93. if (!cmd)
  94. return -ENOMEM;
  95. nd_desc = nvdimm_bus->nd_desc;
  96. for (config_size = ndd->nsarea.config_size, offset = 0;
  97. config_size; config_size -= cmd->in_length,
  98. offset += cmd->in_length) {
  99. cmd->in_length = min(config_size, max_cmd_size);
  100. cmd->in_offset = offset;
  101. rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  102. ND_CMD_GET_CONFIG_DATA, cmd,
  103. cmd->in_length + sizeof(*cmd));
  104. if (rc || cmd->status) {
  105. rc = -ENXIO;
  106. break;
  107. }
  108. memcpy(ndd->data + offset, cmd->out_buf, cmd->in_length);
  109. }
  110. dev_dbg(ndd->dev, "%s: len: %zu rc: %d\n", __func__, offset, rc);
  111. kfree(cmd);
  112. return rc;
  113. }
  114. int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
  115. void *buf, size_t len)
  116. {
  117. int rc = validate_dimm(ndd);
  118. size_t max_cmd_size, buf_offset;
  119. struct nd_cmd_set_config_hdr *cmd;
  120. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  121. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  122. if (rc)
  123. return rc;
  124. if (!ndd->data)
  125. return -ENXIO;
  126. if (offset + len > ndd->nsarea.config_size)
  127. return -ENXIO;
  128. max_cmd_size = min_t(u32, PAGE_SIZE, len);
  129. max_cmd_size = min_t(u32, max_cmd_size, ndd->nsarea.max_xfer);
  130. cmd = kzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
  131. if (!cmd)
  132. return -ENOMEM;
  133. for (buf_offset = 0; len; len -= cmd->in_length,
  134. buf_offset += cmd->in_length) {
  135. size_t cmd_size;
  136. u32 *status;
  137. cmd->in_offset = offset + buf_offset;
  138. cmd->in_length = min(max_cmd_size, len);
  139. memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length);
  140. /* status is output in the last 4-bytes of the command buffer */
  141. cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
  142. status = ((void *) cmd) + cmd_size - sizeof(u32);
  143. rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  144. ND_CMD_SET_CONFIG_DATA, cmd, cmd_size);
  145. if (rc || *status) {
  146. rc = rc ? rc : -ENXIO;
  147. break;
  148. }
  149. }
  150. kfree(cmd);
  151. return rc;
  152. }
  153. static void nvdimm_release(struct device *dev)
  154. {
  155. struct nvdimm *nvdimm = to_nvdimm(dev);
  156. ida_simple_remove(&dimm_ida, nvdimm->id);
  157. kfree(nvdimm);
  158. }
  159. static struct device_type nvdimm_device_type = {
  160. .name = "nvdimm",
  161. .release = nvdimm_release,
  162. };
  163. bool is_nvdimm(struct device *dev)
  164. {
  165. return dev->type == &nvdimm_device_type;
  166. }
  167. struct nvdimm *to_nvdimm(struct device *dev)
  168. {
  169. struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
  170. WARN_ON(!is_nvdimm(dev));
  171. return nvdimm;
  172. }
  173. EXPORT_SYMBOL_GPL(to_nvdimm);
  174. struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
  175. {
  176. struct nd_region *nd_region = &ndbr->nd_region;
  177. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  178. return nd_mapping->nvdimm;
  179. }
  180. EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
  181. struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
  182. {
  183. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  184. WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
  185. return dev_get_drvdata(&nvdimm->dev);
  186. }
  187. EXPORT_SYMBOL(to_ndd);
  188. void nvdimm_drvdata_release(struct kref *kref)
  189. {
  190. struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
  191. struct device *dev = ndd->dev;
  192. struct resource *res, *_r;
  193. dev_dbg(dev, "%s\n", __func__);
  194. nvdimm_bus_lock(dev);
  195. for_each_dpa_resource_safe(ndd, res, _r)
  196. nvdimm_free_dpa(ndd, res);
  197. nvdimm_bus_unlock(dev);
  198. if (ndd->data && is_vmalloc_addr(ndd->data))
  199. vfree(ndd->data);
  200. else
  201. kfree(ndd->data);
  202. kfree(ndd);
  203. put_device(dev);
  204. }
  205. void get_ndd(struct nvdimm_drvdata *ndd)
  206. {
  207. kref_get(&ndd->kref);
  208. }
  209. void put_ndd(struct nvdimm_drvdata *ndd)
  210. {
  211. if (ndd)
  212. kref_put(&ndd->kref, nvdimm_drvdata_release);
  213. }
  214. const char *nvdimm_name(struct nvdimm *nvdimm)
  215. {
  216. return dev_name(&nvdimm->dev);
  217. }
  218. EXPORT_SYMBOL_GPL(nvdimm_name);
  219. void *nvdimm_provider_data(struct nvdimm *nvdimm)
  220. {
  221. if (nvdimm)
  222. return nvdimm->provider_data;
  223. return NULL;
  224. }
  225. EXPORT_SYMBOL_GPL(nvdimm_provider_data);
  226. static ssize_t commands_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct nvdimm *nvdimm = to_nvdimm(dev);
  230. int cmd, len = 0;
  231. if (!nvdimm->dsm_mask)
  232. return sprintf(buf, "\n");
  233. for_each_set_bit(cmd, nvdimm->dsm_mask, BITS_PER_LONG)
  234. len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
  235. len += sprintf(buf + len, "\n");
  236. return len;
  237. }
  238. static DEVICE_ATTR_RO(commands);
  239. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  240. char *buf)
  241. {
  242. struct nvdimm *nvdimm = to_nvdimm(dev);
  243. /*
  244. * The state may be in the process of changing, userspace should
  245. * quiesce probing if it wants a static answer
  246. */
  247. nvdimm_bus_lock(dev);
  248. nvdimm_bus_unlock(dev);
  249. return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
  250. ? "active" : "idle");
  251. }
  252. static DEVICE_ATTR_RO(state);
  253. static ssize_t available_slots_show(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
  257. ssize_t rc;
  258. u32 nfree;
  259. if (!ndd)
  260. return -ENXIO;
  261. nvdimm_bus_lock(dev);
  262. nfree = nd_label_nfree(ndd);
  263. if (nfree - 1 > nfree) {
  264. dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
  265. nfree = 0;
  266. } else
  267. nfree--;
  268. rc = sprintf(buf, "%d\n", nfree);
  269. nvdimm_bus_unlock(dev);
  270. return rc;
  271. }
  272. static DEVICE_ATTR_RO(available_slots);
  273. static struct attribute *nvdimm_attributes[] = {
  274. &dev_attr_state.attr,
  275. &dev_attr_commands.attr,
  276. &dev_attr_available_slots.attr,
  277. NULL,
  278. };
  279. struct attribute_group nvdimm_attribute_group = {
  280. .attrs = nvdimm_attributes,
  281. };
  282. EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
  283. struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
  284. const struct attribute_group **groups, unsigned long flags,
  285. unsigned long *dsm_mask)
  286. {
  287. struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
  288. struct device *dev;
  289. if (!nvdimm)
  290. return NULL;
  291. nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
  292. if (nvdimm->id < 0) {
  293. kfree(nvdimm);
  294. return NULL;
  295. }
  296. nvdimm->provider_data = provider_data;
  297. nvdimm->flags = flags;
  298. nvdimm->dsm_mask = dsm_mask;
  299. atomic_set(&nvdimm->busy, 0);
  300. dev = &nvdimm->dev;
  301. dev_set_name(dev, "nmem%d", nvdimm->id);
  302. dev->parent = &nvdimm_bus->dev;
  303. dev->type = &nvdimm_device_type;
  304. dev->devt = MKDEV(nvdimm_major, nvdimm->id);
  305. dev->groups = groups;
  306. nd_device_register(dev);
  307. return nvdimm;
  308. }
  309. EXPORT_SYMBOL_GPL(nvdimm_create);
  310. /**
  311. * nd_blk_available_dpa - account the unused dpa of BLK region
  312. * @nd_mapping: container of dpa-resource-root + labels
  313. *
  314. * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges.
  315. */
  316. resource_size_t nd_blk_available_dpa(struct nd_mapping *nd_mapping)
  317. {
  318. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  319. resource_size_t map_end, busy = 0, available;
  320. struct resource *res;
  321. if (!ndd)
  322. return 0;
  323. map_end = nd_mapping->start + nd_mapping->size - 1;
  324. for_each_dpa_resource(ndd, res)
  325. if (res->start >= nd_mapping->start && res->start < map_end) {
  326. resource_size_t end = min(map_end, res->end);
  327. busy += end - res->start + 1;
  328. } else if (res->end >= nd_mapping->start
  329. && res->end <= map_end) {
  330. busy += res->end - nd_mapping->start;
  331. } else if (nd_mapping->start > res->start
  332. && nd_mapping->start < res->end) {
  333. /* total eclipse of the BLK region mapping */
  334. busy += nd_mapping->size;
  335. }
  336. available = map_end - nd_mapping->start + 1;
  337. if (busy < available)
  338. return available - busy;
  339. return 0;
  340. }
  341. /**
  342. * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
  343. * @nd_mapping: container of dpa-resource-root + labels
  344. * @nd_region: constrain available space check to this reference region
  345. * @overlap: calculate available space assuming this level of overlap
  346. *
  347. * Validate that a PMEM label, if present, aligns with the start of an
  348. * interleave set and truncate the available size at the lowest BLK
  349. * overlap point.
  350. *
  351. * The expectation is that this routine is called multiple times as it
  352. * probes for the largest BLK encroachment for any single member DIMM of
  353. * the interleave set. Once that value is determined the PMEM-limit for
  354. * the set can be established.
  355. */
  356. resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
  357. struct nd_mapping *nd_mapping, resource_size_t *overlap)
  358. {
  359. resource_size_t map_start, map_end, busy = 0, available, blk_start;
  360. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  361. struct resource *res;
  362. const char *reason;
  363. if (!ndd)
  364. return 0;
  365. map_start = nd_mapping->start;
  366. map_end = map_start + nd_mapping->size - 1;
  367. blk_start = max(map_start, map_end + 1 - *overlap);
  368. for_each_dpa_resource(ndd, res)
  369. if (res->start >= map_start && res->start < map_end) {
  370. if (strncmp(res->name, "blk", 3) == 0)
  371. blk_start = min(blk_start, res->start);
  372. else if (res->start != map_start) {
  373. reason = "misaligned to iset";
  374. goto err;
  375. } else {
  376. if (busy) {
  377. reason = "duplicate overlapping PMEM reservations?";
  378. goto err;
  379. }
  380. busy += resource_size(res);
  381. continue;
  382. }
  383. } else if (res->end >= map_start && res->end <= map_end) {
  384. if (strncmp(res->name, "blk", 3) == 0) {
  385. /*
  386. * If a BLK allocation overlaps the start of
  387. * PMEM the entire interleave set may now only
  388. * be used for BLK.
  389. */
  390. blk_start = map_start;
  391. } else {
  392. reason = "misaligned to iset";
  393. goto err;
  394. }
  395. } else if (map_start > res->start && map_start < res->end) {
  396. /* total eclipse of the mapping */
  397. busy += nd_mapping->size;
  398. blk_start = map_start;
  399. }
  400. *overlap = map_end + 1 - blk_start;
  401. available = blk_start - map_start;
  402. if (busy < available)
  403. return available - busy;
  404. return 0;
  405. err:
  406. /*
  407. * Something is wrong, PMEM must align with the start of the
  408. * interleave set, and there can only be one allocation per set.
  409. */
  410. nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
  411. return 0;
  412. }
  413. void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
  414. {
  415. WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  416. kfree(res->name);
  417. __release_region(&ndd->dpa, res->start, resource_size(res));
  418. }
  419. struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
  420. struct nd_label_id *label_id, resource_size_t start,
  421. resource_size_t n)
  422. {
  423. char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
  424. struct resource *res;
  425. if (!name)
  426. return NULL;
  427. WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  428. res = __request_region(&ndd->dpa, start, n, name, 0);
  429. if (!res)
  430. kfree(name);
  431. return res;
  432. }
  433. /**
  434. * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
  435. * @nvdimm: container of dpa-resource-root + labels
  436. * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
  437. */
  438. resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
  439. struct nd_label_id *label_id)
  440. {
  441. resource_size_t allocated = 0;
  442. struct resource *res;
  443. for_each_dpa_resource(ndd, res)
  444. if (strcmp(res->name, label_id->id) == 0)
  445. allocated += resource_size(res);
  446. return allocated;
  447. }
  448. static int count_dimms(struct device *dev, void *c)
  449. {
  450. int *count = c;
  451. if (is_nvdimm(dev))
  452. (*count)++;
  453. return 0;
  454. }
  455. int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
  456. {
  457. int count = 0;
  458. /* Flush any possible dimm registration failures */
  459. nd_synchronize();
  460. device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
  461. dev_dbg(&nvdimm_bus->dev, "%s: count: %d\n", __func__, count);
  462. if (count != dimm_count)
  463. return -ENXIO;
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);