dimm_devs.c 17 KB

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