dimm_devs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. kvfree(ndd->data);
  199. kfree(ndd);
  200. put_device(dev);
  201. }
  202. void get_ndd(struct nvdimm_drvdata *ndd)
  203. {
  204. kref_get(&ndd->kref);
  205. }
  206. void put_ndd(struct nvdimm_drvdata *ndd)
  207. {
  208. if (ndd)
  209. kref_put(&ndd->kref, nvdimm_drvdata_release);
  210. }
  211. const char *nvdimm_name(struct nvdimm *nvdimm)
  212. {
  213. return dev_name(&nvdimm->dev);
  214. }
  215. EXPORT_SYMBOL_GPL(nvdimm_name);
  216. void *nvdimm_provider_data(struct nvdimm *nvdimm)
  217. {
  218. if (nvdimm)
  219. return nvdimm->provider_data;
  220. return NULL;
  221. }
  222. EXPORT_SYMBOL_GPL(nvdimm_provider_data);
  223. static ssize_t commands_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct nvdimm *nvdimm = to_nvdimm(dev);
  227. int cmd, len = 0;
  228. if (!nvdimm->dsm_mask)
  229. return sprintf(buf, "\n");
  230. for_each_set_bit(cmd, nvdimm->dsm_mask, BITS_PER_LONG)
  231. len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
  232. len += sprintf(buf + len, "\n");
  233. return len;
  234. }
  235. static DEVICE_ATTR_RO(commands);
  236. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  237. char *buf)
  238. {
  239. struct nvdimm *nvdimm = to_nvdimm(dev);
  240. /*
  241. * The state may be in the process of changing, userspace should
  242. * quiesce probing if it wants a static answer
  243. */
  244. nvdimm_bus_lock(dev);
  245. nvdimm_bus_unlock(dev);
  246. return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
  247. ? "active" : "idle");
  248. }
  249. static DEVICE_ATTR_RO(state);
  250. static ssize_t available_slots_show(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
  254. ssize_t rc;
  255. u32 nfree;
  256. if (!ndd)
  257. return -ENXIO;
  258. nvdimm_bus_lock(dev);
  259. nfree = nd_label_nfree(ndd);
  260. if (nfree - 1 > nfree) {
  261. dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
  262. nfree = 0;
  263. } else
  264. nfree--;
  265. rc = sprintf(buf, "%d\n", nfree);
  266. nvdimm_bus_unlock(dev);
  267. return rc;
  268. }
  269. static DEVICE_ATTR_RO(available_slots);
  270. static struct attribute *nvdimm_attributes[] = {
  271. &dev_attr_state.attr,
  272. &dev_attr_commands.attr,
  273. &dev_attr_available_slots.attr,
  274. NULL,
  275. };
  276. struct attribute_group nvdimm_attribute_group = {
  277. .attrs = nvdimm_attributes,
  278. };
  279. EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
  280. struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
  281. const struct attribute_group **groups, unsigned long flags,
  282. unsigned long *dsm_mask)
  283. {
  284. struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
  285. struct device *dev;
  286. if (!nvdimm)
  287. return NULL;
  288. nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
  289. if (nvdimm->id < 0) {
  290. kfree(nvdimm);
  291. return NULL;
  292. }
  293. nvdimm->provider_data = provider_data;
  294. nvdimm->flags = flags;
  295. nvdimm->dsm_mask = dsm_mask;
  296. atomic_set(&nvdimm->busy, 0);
  297. dev = &nvdimm->dev;
  298. dev_set_name(dev, "nmem%d", nvdimm->id);
  299. dev->parent = &nvdimm_bus->dev;
  300. dev->type = &nvdimm_device_type;
  301. dev->devt = MKDEV(nvdimm_major, nvdimm->id);
  302. dev->groups = groups;
  303. nd_device_register(dev);
  304. return nvdimm;
  305. }
  306. EXPORT_SYMBOL_GPL(nvdimm_create);
  307. /**
  308. * nd_blk_available_dpa - account the unused dpa of BLK region
  309. * @nd_mapping: container of dpa-resource-root + labels
  310. *
  311. * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges.
  312. */
  313. resource_size_t nd_blk_available_dpa(struct nd_mapping *nd_mapping)
  314. {
  315. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  316. resource_size_t map_end, busy = 0, available;
  317. struct resource *res;
  318. if (!ndd)
  319. return 0;
  320. map_end = nd_mapping->start + nd_mapping->size - 1;
  321. for_each_dpa_resource(ndd, res)
  322. if (res->start >= nd_mapping->start && res->start < map_end) {
  323. resource_size_t end = min(map_end, res->end);
  324. busy += end - res->start + 1;
  325. } else if (res->end >= nd_mapping->start
  326. && res->end <= map_end) {
  327. busy += res->end - nd_mapping->start;
  328. } else if (nd_mapping->start > res->start
  329. && nd_mapping->start < res->end) {
  330. /* total eclipse of the BLK region mapping */
  331. busy += nd_mapping->size;
  332. }
  333. available = map_end - nd_mapping->start + 1;
  334. if (busy < available)
  335. return available - busy;
  336. return 0;
  337. }
  338. /**
  339. * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
  340. * @nd_mapping: container of dpa-resource-root + labels
  341. * @nd_region: constrain available space check to this reference region
  342. * @overlap: calculate available space assuming this level of overlap
  343. *
  344. * Validate that a PMEM label, if present, aligns with the start of an
  345. * interleave set and truncate the available size at the lowest BLK
  346. * overlap point.
  347. *
  348. * The expectation is that this routine is called multiple times as it
  349. * probes for the largest BLK encroachment for any single member DIMM of
  350. * the interleave set. Once that value is determined the PMEM-limit for
  351. * the set can be established.
  352. */
  353. resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
  354. struct nd_mapping *nd_mapping, resource_size_t *overlap)
  355. {
  356. resource_size_t map_start, map_end, busy = 0, available, blk_start;
  357. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  358. struct resource *res;
  359. const char *reason;
  360. if (!ndd)
  361. return 0;
  362. map_start = nd_mapping->start;
  363. map_end = map_start + nd_mapping->size - 1;
  364. blk_start = max(map_start, map_end + 1 - *overlap);
  365. for_each_dpa_resource(ndd, res)
  366. if (res->start >= map_start && res->start < map_end) {
  367. if (strncmp(res->name, "blk", 3) == 0)
  368. blk_start = min(blk_start, res->start);
  369. else if (res->start != map_start) {
  370. reason = "misaligned to iset";
  371. goto err;
  372. } else {
  373. if (busy) {
  374. reason = "duplicate overlapping PMEM reservations?";
  375. goto err;
  376. }
  377. busy += resource_size(res);
  378. continue;
  379. }
  380. } else if (res->end >= map_start && res->end <= map_end) {
  381. if (strncmp(res->name, "blk", 3) == 0) {
  382. /*
  383. * If a BLK allocation overlaps the start of
  384. * PMEM the entire interleave set may now only
  385. * be used for BLK.
  386. */
  387. blk_start = map_start;
  388. } else {
  389. reason = "misaligned to iset";
  390. goto err;
  391. }
  392. } else if (map_start > res->start && map_start < res->end) {
  393. /* total eclipse of the mapping */
  394. busy += nd_mapping->size;
  395. blk_start = map_start;
  396. }
  397. *overlap = map_end + 1 - blk_start;
  398. available = blk_start - map_start;
  399. if (busy < available)
  400. return available - busy;
  401. return 0;
  402. err:
  403. /*
  404. * Something is wrong, PMEM must align with the start of the
  405. * interleave set, and there can only be one allocation per set.
  406. */
  407. nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
  408. return 0;
  409. }
  410. void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
  411. {
  412. WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  413. kfree(res->name);
  414. __release_region(&ndd->dpa, res->start, resource_size(res));
  415. }
  416. struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
  417. struct nd_label_id *label_id, resource_size_t start,
  418. resource_size_t n)
  419. {
  420. char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
  421. struct resource *res;
  422. if (!name)
  423. return NULL;
  424. WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
  425. res = __request_region(&ndd->dpa, start, n, name, 0);
  426. if (!res)
  427. kfree(name);
  428. return res;
  429. }
  430. /**
  431. * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
  432. * @nvdimm: container of dpa-resource-root + labels
  433. * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
  434. */
  435. resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
  436. struct nd_label_id *label_id)
  437. {
  438. resource_size_t allocated = 0;
  439. struct resource *res;
  440. for_each_dpa_resource(ndd, res)
  441. if (strcmp(res->name, label_id->id) == 0)
  442. allocated += resource_size(res);
  443. return allocated;
  444. }
  445. static int count_dimms(struct device *dev, void *c)
  446. {
  447. int *count = c;
  448. if (is_nvdimm(dev))
  449. (*count)++;
  450. return 0;
  451. }
  452. int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
  453. {
  454. int count = 0;
  455. /* Flush any possible dimm registration failures */
  456. nd_synchronize();
  457. device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
  458. dev_dbg(&nvdimm_bus->dev, "%s: count: %d\n", __func__, count);
  459. if (count != dimm_count)
  460. return -ENXIO;
  461. return 0;
  462. }
  463. EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);