dimm_devs.c 18 KB

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