dimm_devs.c 14 KB

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