dimm_devs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. int nvdimm_check_config_data(struct device *dev)
  30. {
  31. struct nvdimm *nvdimm = to_nvdimm(dev);
  32. if (!nvdimm->cmd_mask ||
  33. !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) {
  34. if (nvdimm->flags & NDD_ALIASING)
  35. return -ENXIO;
  36. else
  37. return -ENOTTY;
  38. }
  39. return 0;
  40. }
  41. static int validate_dimm(struct nvdimm_drvdata *ndd)
  42. {
  43. int rc;
  44. if (!ndd)
  45. return -EINVAL;
  46. rc = nvdimm_check_config_data(ndd->dev);
  47. if (rc)
  48. dev_dbg(ndd->dev, "%pf: %s error: %d\n",
  49. __builtin_return_address(0), __func__, rc);
  50. return rc;
  51. }
  52. /**
  53. * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
  54. * @nvdimm: dimm to initialize
  55. */
  56. int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
  57. {
  58. struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
  59. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  60. struct nvdimm_bus_descriptor *nd_desc;
  61. int rc = validate_dimm(ndd);
  62. if (rc)
  63. return rc;
  64. if (cmd->config_size)
  65. return 0; /* already valid */
  66. memset(cmd, 0, sizeof(*cmd));
  67. nd_desc = nvdimm_bus->nd_desc;
  68. return nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  69. ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd), NULL);
  70. }
  71. int nvdimm_init_config_data(struct nvdimm_drvdata *ndd)
  72. {
  73. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  74. struct nd_cmd_get_config_data_hdr *cmd;
  75. struct nvdimm_bus_descriptor *nd_desc;
  76. int rc = validate_dimm(ndd);
  77. u32 max_cmd_size, config_size;
  78. size_t offset;
  79. if (rc)
  80. return rc;
  81. if (ndd->data)
  82. return 0;
  83. if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0
  84. || ndd->nsarea.config_size < ND_LABEL_MIN_SIZE) {
  85. dev_dbg(ndd->dev, "failed to init config data area: (%d:%d)\n",
  86. ndd->nsarea.max_xfer, ndd->nsarea.config_size);
  87. return -ENXIO;
  88. }
  89. ndd->data = kmalloc(ndd->nsarea.config_size, GFP_KERNEL);
  90. if (!ndd->data)
  91. ndd->data = vmalloc(ndd->nsarea.config_size);
  92. if (!ndd->data)
  93. return -ENOMEM;
  94. max_cmd_size = min_t(u32, PAGE_SIZE, ndd->nsarea.max_xfer);
  95. cmd = kzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
  96. if (!cmd)
  97. return -ENOMEM;
  98. nd_desc = nvdimm_bus->nd_desc;
  99. for (config_size = ndd->nsarea.config_size, offset = 0;
  100. config_size; config_size -= cmd->in_length,
  101. offset += cmd->in_length) {
  102. cmd->in_length = min(config_size, max_cmd_size);
  103. cmd->in_offset = offset;
  104. rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  105. ND_CMD_GET_CONFIG_DATA, cmd,
  106. cmd->in_length + sizeof(*cmd), NULL);
  107. if (rc || cmd->status) {
  108. rc = -ENXIO;
  109. break;
  110. }
  111. memcpy(ndd->data + offset, cmd->out_buf, cmd->in_length);
  112. }
  113. dev_dbg(ndd->dev, "%s: len: %zu rc: %d\n", __func__, offset, rc);
  114. kfree(cmd);
  115. return rc;
  116. }
  117. int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
  118. void *buf, size_t len)
  119. {
  120. int rc = validate_dimm(ndd);
  121. size_t max_cmd_size, buf_offset;
  122. struct nd_cmd_set_config_hdr *cmd;
  123. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
  124. struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
  125. if (rc)
  126. return rc;
  127. if (!ndd->data)
  128. return -ENXIO;
  129. if (offset + len > ndd->nsarea.config_size)
  130. return -ENXIO;
  131. max_cmd_size = min_t(u32, PAGE_SIZE, len);
  132. max_cmd_size = min_t(u32, max_cmd_size, ndd->nsarea.max_xfer);
  133. cmd = kzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
  134. if (!cmd)
  135. return -ENOMEM;
  136. for (buf_offset = 0; len; len -= cmd->in_length,
  137. buf_offset += cmd->in_length) {
  138. size_t cmd_size;
  139. u32 *status;
  140. cmd->in_offset = offset + buf_offset;
  141. cmd->in_length = min(max_cmd_size, len);
  142. memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length);
  143. /* status is output in the last 4-bytes of the command buffer */
  144. cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
  145. status = ((void *) cmd) + cmd_size - sizeof(u32);
  146. rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
  147. ND_CMD_SET_CONFIG_DATA, cmd, cmd_size, NULL);
  148. if (rc || *status) {
  149. rc = rc ? rc : -ENXIO;
  150. break;
  151. }
  152. }
  153. kfree(cmd);
  154. return rc;
  155. }
  156. void nvdimm_set_aliasing(struct device *dev)
  157. {
  158. struct nvdimm *nvdimm = to_nvdimm(dev);
  159. nvdimm->flags |= NDD_ALIASING;
  160. }
  161. static void nvdimm_release(struct device *dev)
  162. {
  163. struct nvdimm *nvdimm = to_nvdimm(dev);
  164. ida_simple_remove(&dimm_ida, nvdimm->id);
  165. kfree(nvdimm);
  166. }
  167. static struct device_type nvdimm_device_type = {
  168. .name = "nvdimm",
  169. .release = nvdimm_release,
  170. };
  171. bool is_nvdimm(struct device *dev)
  172. {
  173. return dev->type == &nvdimm_device_type;
  174. }
  175. struct nvdimm *to_nvdimm(struct device *dev)
  176. {
  177. struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
  178. WARN_ON(!is_nvdimm(dev));
  179. return nvdimm;
  180. }
  181. EXPORT_SYMBOL_GPL(to_nvdimm);
  182. struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
  183. {
  184. struct nd_region *nd_region = &ndbr->nd_region;
  185. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  186. return nd_mapping->nvdimm;
  187. }
  188. EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
  189. struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
  190. {
  191. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  192. WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
  193. return dev_get_drvdata(&nvdimm->dev);
  194. }
  195. EXPORT_SYMBOL(to_ndd);
  196. void nvdimm_drvdata_release(struct kref *kref)
  197. {
  198. struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
  199. struct device *dev = ndd->dev;
  200. struct resource *res, *_r;
  201. dev_dbg(dev, "%s\n", __func__);
  202. nvdimm_bus_lock(dev);
  203. for_each_dpa_resource_safe(ndd, res, _r)
  204. nvdimm_free_dpa(ndd, res);
  205. nvdimm_bus_unlock(dev);
  206. kvfree(ndd->data);
  207. kfree(ndd);
  208. put_device(dev);
  209. }
  210. void get_ndd(struct nvdimm_drvdata *ndd)
  211. {
  212. kref_get(&ndd->kref);
  213. }
  214. void put_ndd(struct nvdimm_drvdata *ndd)
  215. {
  216. if (ndd)
  217. kref_put(&ndd->kref, nvdimm_drvdata_release);
  218. }
  219. const char *nvdimm_name(struct nvdimm *nvdimm)
  220. {
  221. return dev_name(&nvdimm->dev);
  222. }
  223. EXPORT_SYMBOL_GPL(nvdimm_name);
  224. struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
  225. {
  226. return &nvdimm->dev.kobj;
  227. }
  228. EXPORT_SYMBOL_GPL(nvdimm_kobj);
  229. unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
  230. {
  231. return nvdimm->cmd_mask;
  232. }
  233. EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
  234. void *nvdimm_provider_data(struct nvdimm *nvdimm)
  235. {
  236. if (nvdimm)
  237. return nvdimm->provider_data;
  238. return NULL;
  239. }
  240. EXPORT_SYMBOL_GPL(nvdimm_provider_data);
  241. static ssize_t commands_show(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. struct nvdimm *nvdimm = to_nvdimm(dev);
  245. int cmd, len = 0;
  246. if (!nvdimm->cmd_mask)
  247. return sprintf(buf, "\n");
  248. for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
  249. len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
  250. len += sprintf(buf + len, "\n");
  251. return len;
  252. }
  253. static DEVICE_ATTR_RO(commands);
  254. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  255. char *buf)
  256. {
  257. struct nvdimm *nvdimm = to_nvdimm(dev);
  258. /*
  259. * The state may be in the process of changing, userspace should
  260. * quiesce probing if it wants a static answer
  261. */
  262. nvdimm_bus_lock(dev);
  263. nvdimm_bus_unlock(dev);
  264. return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
  265. ? "active" : "idle");
  266. }
  267. static DEVICE_ATTR_RO(state);
  268. static ssize_t available_slots_show(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
  272. ssize_t rc;
  273. u32 nfree;
  274. if (!ndd)
  275. return -ENXIO;
  276. nvdimm_bus_lock(dev);
  277. nfree = nd_label_nfree(ndd);
  278. if (nfree - 1 > nfree) {
  279. dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
  280. nfree = 0;
  281. } else
  282. nfree--;
  283. rc = sprintf(buf, "%d\n", nfree);
  284. nvdimm_bus_unlock(dev);
  285. return rc;
  286. }
  287. static DEVICE_ATTR_RO(available_slots);
  288. static struct attribute *nvdimm_attributes[] = {
  289. &dev_attr_state.attr,
  290. &dev_attr_commands.attr,
  291. &dev_attr_available_slots.attr,
  292. NULL,
  293. };
  294. struct attribute_group nvdimm_attribute_group = {
  295. .attrs = nvdimm_attributes,
  296. };
  297. EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
  298. struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
  299. const struct attribute_group **groups, unsigned long flags,
  300. unsigned long cmd_mask, int num_flush,
  301. struct resource *flush_wpq)
  302. {
  303. struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
  304. struct device *dev;
  305. if (!nvdimm)
  306. return NULL;
  307. nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
  308. if (nvdimm->id < 0) {
  309. kfree(nvdimm);
  310. return NULL;
  311. }
  312. nvdimm->provider_data = provider_data;
  313. nvdimm->flags = flags;
  314. nvdimm->cmd_mask = cmd_mask;
  315. nvdimm->num_flush = num_flush;
  316. nvdimm->flush_wpq = flush_wpq;
  317. atomic_set(&nvdimm->busy, 0);
  318. dev = &nvdimm->dev;
  319. dev_set_name(dev, "nmem%d", nvdimm->id);
  320. dev->parent = &nvdimm_bus->dev;
  321. dev->type = &nvdimm_device_type;
  322. dev->devt = MKDEV(nvdimm_major, nvdimm->id);
  323. dev->groups = groups;
  324. nd_device_register(dev);
  325. return nvdimm;
  326. }
  327. EXPORT_SYMBOL_GPL(nvdimm_create);
  328. int alias_dpa_busy(struct device *dev, void *data)
  329. {
  330. resource_size_t map_end, blk_start, new, busy;
  331. struct blk_alloc_info *info = data;
  332. struct nd_mapping *nd_mapping;
  333. struct nd_region *nd_region;
  334. struct nvdimm_drvdata *ndd;
  335. struct resource *res;
  336. int i;
  337. if (!is_nd_pmem(dev))
  338. return 0;
  339. nd_region = to_nd_region(dev);
  340. for (i = 0; i < nd_region->ndr_mappings; i++) {
  341. nd_mapping = &nd_region->mapping[i];
  342. if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
  343. break;
  344. }
  345. if (i >= nd_region->ndr_mappings)
  346. return 0;
  347. ndd = to_ndd(nd_mapping);
  348. map_end = nd_mapping->start + nd_mapping->size - 1;
  349. blk_start = nd_mapping->start;
  350. /*
  351. * In the allocation case ->res is set to free space that we are
  352. * looking to validate against PMEM aliasing collision rules
  353. * (i.e. BLK is allocated after all aliased PMEM).
  354. */
  355. if (info->res) {
  356. if (info->res->start >= nd_mapping->start
  357. && info->res->start < map_end)
  358. /* pass */;
  359. else
  360. return 0;
  361. }
  362. retry:
  363. /*
  364. * Find the free dpa from the end of the last pmem allocation to
  365. * the end of the interleave-set mapping that is not already
  366. * covered by a blk allocation.
  367. */
  368. busy = 0;
  369. for_each_dpa_resource(ndd, res) {
  370. if ((res->start >= blk_start && res->start < map_end)
  371. || (res->end >= blk_start
  372. && res->end <= map_end)) {
  373. if (strncmp(res->name, "pmem", 4) == 0) {
  374. new = max(blk_start, min(map_end + 1,
  375. res->end + 1));
  376. if (new != blk_start) {
  377. blk_start = new;
  378. goto retry;
  379. }
  380. } else
  381. busy += min(map_end, res->end)
  382. - max(nd_mapping->start, res->start) + 1;
  383. } else if (nd_mapping->start > res->start
  384. && map_end < res->end) {
  385. /* total eclipse of the PMEM region mapping */
  386. busy += nd_mapping->size;
  387. break;
  388. }
  389. }
  390. /* update the free space range with the probed blk_start */
  391. if (info->res && blk_start > info->res->start) {
  392. info->res->start = max(info->res->start, blk_start);
  393. if (info->res->start > info->res->end)
  394. info->res->end = info->res->start - 1;
  395. return 1;
  396. }
  397. info->available -= blk_start - nd_mapping->start + busy;
  398. return 0;
  399. }
  400. static int blk_dpa_busy(struct device *dev, void *data)
  401. {
  402. struct blk_alloc_info *info = data;
  403. struct nd_mapping *nd_mapping;
  404. struct nd_region *nd_region;
  405. resource_size_t map_end;
  406. int i;
  407. if (!is_nd_pmem(dev))
  408. return 0;
  409. nd_region = to_nd_region(dev);
  410. for (i = 0; i < nd_region->ndr_mappings; i++) {
  411. nd_mapping = &nd_region->mapping[i];
  412. if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
  413. break;
  414. }
  415. if (i >= nd_region->ndr_mappings)
  416. return 0;
  417. map_end = nd_mapping->start + nd_mapping->size - 1;
  418. if (info->res->start >= nd_mapping->start
  419. && info->res->start < map_end) {
  420. if (info->res->end <= map_end) {
  421. info->busy = 0;
  422. return 1;
  423. } else {
  424. info->busy -= info->res->end - map_end;
  425. return 0;
  426. }
  427. } else if (info->res->end >= nd_mapping->start
  428. && info->res->end <= map_end) {
  429. info->busy -= nd_mapping->start - info->res->start;
  430. return 0;
  431. } else {
  432. info->busy -= nd_mapping->size;
  433. return 0;
  434. }
  435. }
  436. /**
  437. * nd_blk_available_dpa - account the unused dpa of BLK region
  438. * @nd_mapping: container of dpa-resource-root + labels
  439. *
  440. * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
  441. * we arrange for them to never start at an lower dpa than the last
  442. * PMEM allocation in an aliased region.
  443. */
  444. resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
  445. {
  446. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  447. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  448. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  449. struct blk_alloc_info info = {
  450. .nd_mapping = nd_mapping,
  451. .available = nd_mapping->size,
  452. .res = NULL,
  453. };
  454. struct resource *res;
  455. if (!ndd)
  456. return 0;
  457. device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
  458. /* now account for busy blk allocations in unaliased dpa */
  459. for_each_dpa_resource(ndd, res) {
  460. if (strncmp(res->name, "blk", 3) != 0)
  461. continue;
  462. info.res = res;
  463. info.busy = resource_size(res);
  464. device_for_each_child(&nvdimm_bus->dev, &info, blk_dpa_busy);
  465. info.available -= info.busy;
  466. }
  467. return info.available;
  468. }
  469. /**
  470. * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
  471. * @nd_mapping: container of dpa-resource-root + labels
  472. * @nd_region: constrain available space check to this reference region
  473. * @overlap: calculate available space assuming this level of overlap
  474. *
  475. * Validate that a PMEM label, if present, aligns with the start of an
  476. * interleave set and truncate the available size at the lowest BLK
  477. * overlap point.
  478. *
  479. * The expectation is that this routine is called multiple times as it
  480. * probes for the largest BLK encroachment for any single member DIMM of
  481. * the interleave set. Once that value is determined the PMEM-limit for
  482. * the set can be established.
  483. */
  484. resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
  485. struct nd_mapping *nd_mapping, resource_size_t *overlap)
  486. {
  487. resource_size_t map_start, map_end, busy = 0, available, blk_start;
  488. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  489. struct resource *res;
  490. const char *reason;
  491. if (!ndd)
  492. return 0;
  493. map_start = nd_mapping->start;
  494. map_end = map_start + nd_mapping->size - 1;
  495. blk_start = max(map_start, map_end + 1 - *overlap);
  496. for_each_dpa_resource(ndd, res) {
  497. if (res->start >= map_start && res->start < map_end) {
  498. if (strncmp(res->name, "blk", 3) == 0)
  499. blk_start = min(blk_start,
  500. max(map_start, res->start));
  501. else if (res->end > map_end) {
  502. reason = "misaligned to iset";
  503. goto err;
  504. } else
  505. busy += resource_size(res);
  506. } else if (res->end >= map_start && res->end <= map_end) {
  507. if (strncmp(res->name, "blk", 3) == 0) {
  508. /*
  509. * If a BLK allocation overlaps the start of
  510. * PMEM the entire interleave set may now only
  511. * be used for BLK.
  512. */
  513. blk_start = map_start;
  514. } else
  515. busy += resource_size(res);
  516. } else if (map_start > res->start && map_start < res->end) {
  517. /* total eclipse of the mapping */
  518. busy += nd_mapping->size;
  519. blk_start = map_start;
  520. }
  521. }
  522. *overlap = map_end + 1 - blk_start;
  523. available = blk_start - map_start;
  524. if (busy < available)
  525. return available - busy;
  526. return 0;
  527. err:
  528. nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
  529. return 0;
  530. }
  531. void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
  532. {
  533. WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  534. kfree(res->name);
  535. __release_region(&ndd->dpa, res->start, resource_size(res));
  536. }
  537. struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
  538. struct nd_label_id *label_id, resource_size_t start,
  539. resource_size_t n)
  540. {
  541. char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
  542. struct resource *res;
  543. if (!name)
  544. return NULL;
  545. WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  546. res = __request_region(&ndd->dpa, start, n, name, 0);
  547. if (!res)
  548. kfree(name);
  549. return res;
  550. }
  551. /**
  552. * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
  553. * @nvdimm: container of dpa-resource-root + labels
  554. * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
  555. */
  556. resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
  557. struct nd_label_id *label_id)
  558. {
  559. resource_size_t allocated = 0;
  560. struct resource *res;
  561. for_each_dpa_resource(ndd, res)
  562. if (strcmp(res->name, label_id->id) == 0)
  563. allocated += resource_size(res);
  564. return allocated;
  565. }
  566. static int count_dimms(struct device *dev, void *c)
  567. {
  568. int *count = c;
  569. if (is_nvdimm(dev))
  570. (*count)++;
  571. return 0;
  572. }
  573. int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
  574. {
  575. int count = 0;
  576. /* Flush any possible dimm registration failures */
  577. nd_synchronize();
  578. device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
  579. dev_dbg(&nvdimm_bus->dev, "%s: count: %d\n", __func__, count);
  580. if (count != dimm_count)
  581. return -ENXIO;
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
  585. void __exit nvdimm_devs_exit(void)
  586. {
  587. ida_destroy(&dimm_ida);
  588. }