region_devs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. #include <linux/scatterlist.h>
  14. #include <linux/highmem.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/sort.h>
  18. #include <linux/io.h>
  19. #include <linux/nd.h>
  20. #include "nd-core.h"
  21. #include "nd.h"
  22. static DEFINE_IDA(region_ida);
  23. static void nd_region_release(struct device *dev)
  24. {
  25. struct nd_region *nd_region = to_nd_region(dev);
  26. u16 i;
  27. for (i = 0; i < nd_region->ndr_mappings; i++) {
  28. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  29. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  30. put_device(&nvdimm->dev);
  31. }
  32. free_percpu(nd_region->lane);
  33. ida_simple_remove(&region_ida, nd_region->id);
  34. if (is_nd_blk(dev))
  35. kfree(to_nd_blk_region(dev));
  36. else
  37. kfree(nd_region);
  38. }
  39. static struct device_type nd_blk_device_type = {
  40. .name = "nd_blk",
  41. .release = nd_region_release,
  42. };
  43. static struct device_type nd_pmem_device_type = {
  44. .name = "nd_pmem",
  45. .release = nd_region_release,
  46. };
  47. static struct device_type nd_volatile_device_type = {
  48. .name = "nd_volatile",
  49. .release = nd_region_release,
  50. };
  51. bool is_nd_pmem(struct device *dev)
  52. {
  53. return dev ? dev->type == &nd_pmem_device_type : false;
  54. }
  55. bool is_nd_blk(struct device *dev)
  56. {
  57. return dev ? dev->type == &nd_blk_device_type : false;
  58. }
  59. struct nd_region *to_nd_region(struct device *dev)
  60. {
  61. struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
  62. WARN_ON(dev->type->release != nd_region_release);
  63. return nd_region;
  64. }
  65. EXPORT_SYMBOL_GPL(to_nd_region);
  66. struct nd_blk_region *to_nd_blk_region(struct device *dev)
  67. {
  68. struct nd_region *nd_region = to_nd_region(dev);
  69. WARN_ON(!is_nd_blk(dev));
  70. return container_of(nd_region, struct nd_blk_region, nd_region);
  71. }
  72. EXPORT_SYMBOL_GPL(to_nd_blk_region);
  73. void *nd_region_provider_data(struct nd_region *nd_region)
  74. {
  75. return nd_region->provider_data;
  76. }
  77. EXPORT_SYMBOL_GPL(nd_region_provider_data);
  78. void *nd_blk_region_provider_data(struct nd_blk_region *ndbr)
  79. {
  80. return ndbr->blk_provider_data;
  81. }
  82. EXPORT_SYMBOL_GPL(nd_blk_region_provider_data);
  83. void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data)
  84. {
  85. ndbr->blk_provider_data = data;
  86. }
  87. EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data);
  88. /**
  89. * nd_region_to_nstype() - region to an integer namespace type
  90. * @nd_region: region-device to interrogate
  91. *
  92. * This is the 'nstype' attribute of a region as well, an input to the
  93. * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
  94. * namespace devices with namespace drivers.
  95. */
  96. int nd_region_to_nstype(struct nd_region *nd_region)
  97. {
  98. if (is_nd_pmem(&nd_region->dev)) {
  99. u16 i, alias;
  100. for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) {
  101. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  102. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  103. if (nvdimm->flags & NDD_ALIASING)
  104. alias++;
  105. }
  106. if (alias)
  107. return ND_DEVICE_NAMESPACE_PMEM;
  108. else
  109. return ND_DEVICE_NAMESPACE_IO;
  110. } else if (is_nd_blk(&nd_region->dev)) {
  111. return ND_DEVICE_NAMESPACE_BLK;
  112. }
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(nd_region_to_nstype);
  116. static int is_uuid_busy(struct device *dev, void *data)
  117. {
  118. struct nd_region *nd_region = to_nd_region(dev->parent);
  119. u8 *uuid = data;
  120. switch (nd_region_to_nstype(nd_region)) {
  121. case ND_DEVICE_NAMESPACE_PMEM: {
  122. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  123. if (!nspm->uuid)
  124. break;
  125. if (memcmp(uuid, nspm->uuid, NSLABEL_UUID_LEN) == 0)
  126. return -EBUSY;
  127. break;
  128. }
  129. case ND_DEVICE_NAMESPACE_BLK: {
  130. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  131. if (!nsblk->uuid)
  132. break;
  133. if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) == 0)
  134. return -EBUSY;
  135. break;
  136. }
  137. default:
  138. break;
  139. }
  140. return 0;
  141. }
  142. static int is_namespace_uuid_busy(struct device *dev, void *data)
  143. {
  144. if (is_nd_pmem(dev) || is_nd_blk(dev))
  145. return device_for_each_child(dev, data, is_uuid_busy);
  146. return 0;
  147. }
  148. /**
  149. * nd_is_uuid_unique - verify that no other namespace has @uuid
  150. * @dev: any device on a nvdimm_bus
  151. * @uuid: uuid to check
  152. */
  153. bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
  154. {
  155. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  156. if (!nvdimm_bus)
  157. return false;
  158. WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
  159. if (device_for_each_child(&nvdimm_bus->dev, uuid,
  160. is_namespace_uuid_busy) != 0)
  161. return false;
  162. return true;
  163. }
  164. static ssize_t size_show(struct device *dev,
  165. struct device_attribute *attr, char *buf)
  166. {
  167. struct nd_region *nd_region = to_nd_region(dev);
  168. unsigned long long size = 0;
  169. if (is_nd_pmem(dev)) {
  170. size = nd_region->ndr_size;
  171. } else if (nd_region->ndr_mappings == 1) {
  172. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  173. size = nd_mapping->size;
  174. }
  175. return sprintf(buf, "%llu\n", size);
  176. }
  177. static DEVICE_ATTR_RO(size);
  178. static ssize_t mappings_show(struct device *dev,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. struct nd_region *nd_region = to_nd_region(dev);
  182. return sprintf(buf, "%d\n", nd_region->ndr_mappings);
  183. }
  184. static DEVICE_ATTR_RO(mappings);
  185. static ssize_t nstype_show(struct device *dev,
  186. struct device_attribute *attr, char *buf)
  187. {
  188. struct nd_region *nd_region = to_nd_region(dev);
  189. return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
  190. }
  191. static DEVICE_ATTR_RO(nstype);
  192. static ssize_t set_cookie_show(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. struct nd_region *nd_region = to_nd_region(dev);
  196. struct nd_interleave_set *nd_set = nd_region->nd_set;
  197. if (is_nd_pmem(dev) && nd_set)
  198. /* pass, should be precluded by region_visible */;
  199. else
  200. return -ENXIO;
  201. return sprintf(buf, "%#llx\n", nd_set->cookie);
  202. }
  203. static DEVICE_ATTR_RO(set_cookie);
  204. resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
  205. {
  206. resource_size_t blk_max_overlap = 0, available, overlap;
  207. int i;
  208. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  209. retry:
  210. available = 0;
  211. overlap = blk_max_overlap;
  212. for (i = 0; i < nd_region->ndr_mappings; i++) {
  213. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  214. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  215. /* if a dimm is disabled the available capacity is zero */
  216. if (!ndd)
  217. return 0;
  218. if (is_nd_pmem(&nd_region->dev)) {
  219. available += nd_pmem_available_dpa(nd_region,
  220. nd_mapping, &overlap);
  221. if (overlap > blk_max_overlap) {
  222. blk_max_overlap = overlap;
  223. goto retry;
  224. }
  225. } else if (is_nd_blk(&nd_region->dev)) {
  226. available += nd_blk_available_dpa(nd_mapping);
  227. }
  228. }
  229. return available;
  230. }
  231. static ssize_t available_size_show(struct device *dev,
  232. struct device_attribute *attr, char *buf)
  233. {
  234. struct nd_region *nd_region = to_nd_region(dev);
  235. unsigned long long available = 0;
  236. /*
  237. * Flush in-flight updates and grab a snapshot of the available
  238. * size. Of course, this value is potentially invalidated the
  239. * memory nvdimm_bus_lock() is dropped, but that's userspace's
  240. * problem to not race itself.
  241. */
  242. nvdimm_bus_lock(dev);
  243. wait_nvdimm_bus_probe_idle(dev);
  244. available = nd_region_available_dpa(nd_region);
  245. nvdimm_bus_unlock(dev);
  246. return sprintf(buf, "%llu\n", available);
  247. }
  248. static DEVICE_ATTR_RO(available_size);
  249. static ssize_t init_namespaces_show(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct nd_region_namespaces *num_ns = dev_get_drvdata(dev);
  253. ssize_t rc;
  254. nvdimm_bus_lock(dev);
  255. if (num_ns)
  256. rc = sprintf(buf, "%d/%d\n", num_ns->active, num_ns->count);
  257. else
  258. rc = -ENXIO;
  259. nvdimm_bus_unlock(dev);
  260. return rc;
  261. }
  262. static DEVICE_ATTR_RO(init_namespaces);
  263. static ssize_t namespace_seed_show(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct nd_region *nd_region = to_nd_region(dev);
  267. ssize_t rc;
  268. nvdimm_bus_lock(dev);
  269. if (nd_region->ns_seed)
  270. rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
  271. else
  272. rc = sprintf(buf, "\n");
  273. nvdimm_bus_unlock(dev);
  274. return rc;
  275. }
  276. static DEVICE_ATTR_RO(namespace_seed);
  277. static ssize_t btt_seed_show(struct device *dev,
  278. struct device_attribute *attr, char *buf)
  279. {
  280. struct nd_region *nd_region = to_nd_region(dev);
  281. ssize_t rc;
  282. nvdimm_bus_lock(dev);
  283. if (nd_region->btt_seed)
  284. rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
  285. else
  286. rc = sprintf(buf, "\n");
  287. nvdimm_bus_unlock(dev);
  288. return rc;
  289. }
  290. static DEVICE_ATTR_RO(btt_seed);
  291. static ssize_t pfn_seed_show(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. struct nd_region *nd_region = to_nd_region(dev);
  295. ssize_t rc;
  296. nvdimm_bus_lock(dev);
  297. if (nd_region->pfn_seed)
  298. rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
  299. else
  300. rc = sprintf(buf, "\n");
  301. nvdimm_bus_unlock(dev);
  302. return rc;
  303. }
  304. static DEVICE_ATTR_RO(pfn_seed);
  305. static ssize_t read_only_show(struct device *dev,
  306. struct device_attribute *attr, char *buf)
  307. {
  308. struct nd_region *nd_region = to_nd_region(dev);
  309. return sprintf(buf, "%d\n", nd_region->ro);
  310. }
  311. static ssize_t read_only_store(struct device *dev,
  312. struct device_attribute *attr, const char *buf, size_t len)
  313. {
  314. bool ro;
  315. int rc = strtobool(buf, &ro);
  316. struct nd_region *nd_region = to_nd_region(dev);
  317. if (rc)
  318. return rc;
  319. nd_region->ro = ro;
  320. return len;
  321. }
  322. static DEVICE_ATTR_RW(read_only);
  323. static struct attribute *nd_region_attributes[] = {
  324. &dev_attr_size.attr,
  325. &dev_attr_nstype.attr,
  326. &dev_attr_mappings.attr,
  327. &dev_attr_btt_seed.attr,
  328. &dev_attr_pfn_seed.attr,
  329. &dev_attr_read_only.attr,
  330. &dev_attr_set_cookie.attr,
  331. &dev_attr_available_size.attr,
  332. &dev_attr_namespace_seed.attr,
  333. &dev_attr_init_namespaces.attr,
  334. NULL,
  335. };
  336. static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
  337. {
  338. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  339. struct nd_region *nd_region = to_nd_region(dev);
  340. struct nd_interleave_set *nd_set = nd_region->nd_set;
  341. int type = nd_region_to_nstype(nd_region);
  342. if (a != &dev_attr_set_cookie.attr
  343. && a != &dev_attr_available_size.attr)
  344. return a->mode;
  345. if ((type == ND_DEVICE_NAMESPACE_PMEM
  346. || type == ND_DEVICE_NAMESPACE_BLK)
  347. && a == &dev_attr_available_size.attr)
  348. return a->mode;
  349. else if (is_nd_pmem(dev) && nd_set)
  350. return a->mode;
  351. return 0;
  352. }
  353. struct attribute_group nd_region_attribute_group = {
  354. .attrs = nd_region_attributes,
  355. .is_visible = region_visible,
  356. };
  357. EXPORT_SYMBOL_GPL(nd_region_attribute_group);
  358. u64 nd_region_interleave_set_cookie(struct nd_region *nd_region)
  359. {
  360. struct nd_interleave_set *nd_set = nd_region->nd_set;
  361. if (nd_set)
  362. return nd_set->cookie;
  363. return 0;
  364. }
  365. /*
  366. * Upon successful probe/remove, take/release a reference on the
  367. * associated interleave set (if present), and plant new btt + namespace
  368. * seeds. Also, on the removal of a BLK region, notify the provider to
  369. * disable the region.
  370. */
  371. static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
  372. struct device *dev, bool probe)
  373. {
  374. struct nd_region *nd_region;
  375. if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) {
  376. int i;
  377. nd_region = to_nd_region(dev);
  378. for (i = 0; i < nd_region->ndr_mappings; i++) {
  379. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  380. struct nvdimm_drvdata *ndd = nd_mapping->ndd;
  381. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  382. kfree(nd_mapping->labels);
  383. nd_mapping->labels = NULL;
  384. put_ndd(ndd);
  385. nd_mapping->ndd = NULL;
  386. if (ndd)
  387. atomic_dec(&nvdimm->busy);
  388. }
  389. if (is_nd_pmem(dev))
  390. return;
  391. to_nd_blk_region(dev)->disable(nvdimm_bus, dev);
  392. }
  393. if (dev->parent && is_nd_blk(dev->parent) && probe) {
  394. nd_region = to_nd_region(dev->parent);
  395. nvdimm_bus_lock(dev);
  396. if (nd_region->ns_seed == dev)
  397. nd_region_create_blk_seed(nd_region);
  398. nvdimm_bus_unlock(dev);
  399. }
  400. if (is_nd_btt(dev) && probe) {
  401. struct nd_btt *nd_btt = to_nd_btt(dev);
  402. nd_region = to_nd_region(dev->parent);
  403. nvdimm_bus_lock(dev);
  404. if (nd_region->btt_seed == dev)
  405. nd_region_create_btt_seed(nd_region);
  406. if (nd_region->ns_seed == &nd_btt->ndns->dev &&
  407. is_nd_blk(dev->parent))
  408. nd_region_create_blk_seed(nd_region);
  409. nvdimm_bus_unlock(dev);
  410. }
  411. }
  412. void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
  413. {
  414. nd_region_notify_driver_action(nvdimm_bus, dev, true);
  415. }
  416. void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
  417. {
  418. nd_region_notify_driver_action(nvdimm_bus, dev, false);
  419. }
  420. static ssize_t mappingN(struct device *dev, char *buf, int n)
  421. {
  422. struct nd_region *nd_region = to_nd_region(dev);
  423. struct nd_mapping *nd_mapping;
  424. struct nvdimm *nvdimm;
  425. if (n >= nd_region->ndr_mappings)
  426. return -ENXIO;
  427. nd_mapping = &nd_region->mapping[n];
  428. nvdimm = nd_mapping->nvdimm;
  429. return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev),
  430. nd_mapping->start, nd_mapping->size);
  431. }
  432. #define REGION_MAPPING(idx) \
  433. static ssize_t mapping##idx##_show(struct device *dev, \
  434. struct device_attribute *attr, char *buf) \
  435. { \
  436. return mappingN(dev, buf, idx); \
  437. } \
  438. static DEVICE_ATTR_RO(mapping##idx)
  439. /*
  440. * 32 should be enough for a while, even in the presence of socket
  441. * interleave a 32-way interleave set is a degenerate case.
  442. */
  443. REGION_MAPPING(0);
  444. REGION_MAPPING(1);
  445. REGION_MAPPING(2);
  446. REGION_MAPPING(3);
  447. REGION_MAPPING(4);
  448. REGION_MAPPING(5);
  449. REGION_MAPPING(6);
  450. REGION_MAPPING(7);
  451. REGION_MAPPING(8);
  452. REGION_MAPPING(9);
  453. REGION_MAPPING(10);
  454. REGION_MAPPING(11);
  455. REGION_MAPPING(12);
  456. REGION_MAPPING(13);
  457. REGION_MAPPING(14);
  458. REGION_MAPPING(15);
  459. REGION_MAPPING(16);
  460. REGION_MAPPING(17);
  461. REGION_MAPPING(18);
  462. REGION_MAPPING(19);
  463. REGION_MAPPING(20);
  464. REGION_MAPPING(21);
  465. REGION_MAPPING(22);
  466. REGION_MAPPING(23);
  467. REGION_MAPPING(24);
  468. REGION_MAPPING(25);
  469. REGION_MAPPING(26);
  470. REGION_MAPPING(27);
  471. REGION_MAPPING(28);
  472. REGION_MAPPING(29);
  473. REGION_MAPPING(30);
  474. REGION_MAPPING(31);
  475. static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
  476. {
  477. struct device *dev = container_of(kobj, struct device, kobj);
  478. struct nd_region *nd_region = to_nd_region(dev);
  479. if (n < nd_region->ndr_mappings)
  480. return a->mode;
  481. return 0;
  482. }
  483. static struct attribute *mapping_attributes[] = {
  484. &dev_attr_mapping0.attr,
  485. &dev_attr_mapping1.attr,
  486. &dev_attr_mapping2.attr,
  487. &dev_attr_mapping3.attr,
  488. &dev_attr_mapping4.attr,
  489. &dev_attr_mapping5.attr,
  490. &dev_attr_mapping6.attr,
  491. &dev_attr_mapping7.attr,
  492. &dev_attr_mapping8.attr,
  493. &dev_attr_mapping9.attr,
  494. &dev_attr_mapping10.attr,
  495. &dev_attr_mapping11.attr,
  496. &dev_attr_mapping12.attr,
  497. &dev_attr_mapping13.attr,
  498. &dev_attr_mapping14.attr,
  499. &dev_attr_mapping15.attr,
  500. &dev_attr_mapping16.attr,
  501. &dev_attr_mapping17.attr,
  502. &dev_attr_mapping18.attr,
  503. &dev_attr_mapping19.attr,
  504. &dev_attr_mapping20.attr,
  505. &dev_attr_mapping21.attr,
  506. &dev_attr_mapping22.attr,
  507. &dev_attr_mapping23.attr,
  508. &dev_attr_mapping24.attr,
  509. &dev_attr_mapping25.attr,
  510. &dev_attr_mapping26.attr,
  511. &dev_attr_mapping27.attr,
  512. &dev_attr_mapping28.attr,
  513. &dev_attr_mapping29.attr,
  514. &dev_attr_mapping30.attr,
  515. &dev_attr_mapping31.attr,
  516. NULL,
  517. };
  518. struct attribute_group nd_mapping_attribute_group = {
  519. .is_visible = mapping_visible,
  520. .attrs = mapping_attributes,
  521. };
  522. EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
  523. int nd_blk_region_init(struct nd_region *nd_region)
  524. {
  525. struct device *dev = &nd_region->dev;
  526. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  527. if (!is_nd_blk(dev))
  528. return 0;
  529. if (nd_region->ndr_mappings < 1) {
  530. dev_err(dev, "invalid BLK region\n");
  531. return -ENXIO;
  532. }
  533. return to_nd_blk_region(dev)->enable(nvdimm_bus, dev);
  534. }
  535. /**
  536. * nd_region_acquire_lane - allocate and lock a lane
  537. * @nd_region: region id and number of lanes possible
  538. *
  539. * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
  540. * We optimize for the common case where there are 256 lanes, one
  541. * per-cpu. For larger systems we need to lock to share lanes. For now
  542. * this implementation assumes the cost of maintaining an allocator for
  543. * free lanes is on the order of the lock hold time, so it implements a
  544. * static lane = cpu % num_lanes mapping.
  545. *
  546. * In the case of a BTT instance on top of a BLK namespace a lane may be
  547. * acquired recursively. We lock on the first instance.
  548. *
  549. * In the case of a BTT instance on top of PMEM, we only acquire a lane
  550. * for the BTT metadata updates.
  551. */
  552. unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
  553. {
  554. unsigned int cpu, lane;
  555. cpu = get_cpu();
  556. if (nd_region->num_lanes < nr_cpu_ids) {
  557. struct nd_percpu_lane *ndl_lock, *ndl_count;
  558. lane = cpu % nd_region->num_lanes;
  559. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  560. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  561. if (ndl_count->count++ == 0)
  562. spin_lock(&ndl_lock->lock);
  563. } else
  564. lane = cpu;
  565. return lane;
  566. }
  567. EXPORT_SYMBOL(nd_region_acquire_lane);
  568. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
  569. {
  570. if (nd_region->num_lanes < nr_cpu_ids) {
  571. unsigned int cpu = get_cpu();
  572. struct nd_percpu_lane *ndl_lock, *ndl_count;
  573. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  574. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  575. if (--ndl_count->count == 0)
  576. spin_unlock(&ndl_lock->lock);
  577. put_cpu();
  578. }
  579. put_cpu();
  580. }
  581. EXPORT_SYMBOL(nd_region_release_lane);
  582. static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
  583. struct nd_region_desc *ndr_desc, struct device_type *dev_type,
  584. const char *caller)
  585. {
  586. struct nd_region *nd_region;
  587. struct device *dev;
  588. void *region_buf;
  589. unsigned int i;
  590. int ro = 0;
  591. for (i = 0; i < ndr_desc->num_mappings; i++) {
  592. struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
  593. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  594. if ((nd_mapping->start | nd_mapping->size) % SZ_4K) {
  595. dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
  596. caller, dev_name(&nvdimm->dev), i);
  597. return NULL;
  598. }
  599. if (nvdimm->flags & NDD_UNARMED)
  600. ro = 1;
  601. }
  602. if (dev_type == &nd_blk_device_type) {
  603. struct nd_blk_region_desc *ndbr_desc;
  604. struct nd_blk_region *ndbr;
  605. ndbr_desc = to_blk_region_desc(ndr_desc);
  606. ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping)
  607. * ndr_desc->num_mappings,
  608. GFP_KERNEL);
  609. if (ndbr) {
  610. nd_region = &ndbr->nd_region;
  611. ndbr->enable = ndbr_desc->enable;
  612. ndbr->disable = ndbr_desc->disable;
  613. ndbr->do_io = ndbr_desc->do_io;
  614. }
  615. region_buf = ndbr;
  616. } else {
  617. nd_region = kzalloc(sizeof(struct nd_region)
  618. + sizeof(struct nd_mapping)
  619. * ndr_desc->num_mappings,
  620. GFP_KERNEL);
  621. region_buf = nd_region;
  622. }
  623. if (!region_buf)
  624. return NULL;
  625. nd_region->id = ida_simple_get(&region_ida, 0, 0, GFP_KERNEL);
  626. if (nd_region->id < 0)
  627. goto err_id;
  628. nd_region->lane = alloc_percpu(struct nd_percpu_lane);
  629. if (!nd_region->lane)
  630. goto err_percpu;
  631. for (i = 0; i < nr_cpu_ids; i++) {
  632. struct nd_percpu_lane *ndl;
  633. ndl = per_cpu_ptr(nd_region->lane, i);
  634. spin_lock_init(&ndl->lock);
  635. ndl->count = 0;
  636. }
  637. memcpy(nd_region->mapping, ndr_desc->nd_mapping,
  638. sizeof(struct nd_mapping) * ndr_desc->num_mappings);
  639. for (i = 0; i < ndr_desc->num_mappings; i++) {
  640. struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
  641. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  642. get_device(&nvdimm->dev);
  643. }
  644. nd_region->ndr_mappings = ndr_desc->num_mappings;
  645. nd_region->provider_data = ndr_desc->provider_data;
  646. nd_region->nd_set = ndr_desc->nd_set;
  647. nd_region->num_lanes = ndr_desc->num_lanes;
  648. nd_region->flags = ndr_desc->flags;
  649. nd_region->ro = ro;
  650. nd_region->numa_node = ndr_desc->numa_node;
  651. ida_init(&nd_region->ns_ida);
  652. ida_init(&nd_region->btt_ida);
  653. ida_init(&nd_region->pfn_ida);
  654. dev = &nd_region->dev;
  655. dev_set_name(dev, "region%d", nd_region->id);
  656. dev->parent = &nvdimm_bus->dev;
  657. dev->type = dev_type;
  658. dev->groups = ndr_desc->attr_groups;
  659. nd_region->ndr_size = resource_size(ndr_desc->res);
  660. nd_region->ndr_start = ndr_desc->res->start;
  661. nd_device_register(dev);
  662. return nd_region;
  663. err_percpu:
  664. ida_simple_remove(&region_ida, nd_region->id);
  665. err_id:
  666. kfree(region_buf);
  667. return NULL;
  668. }
  669. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  670. struct nd_region_desc *ndr_desc)
  671. {
  672. ndr_desc->num_lanes = ND_MAX_LANES;
  673. return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
  674. __func__);
  675. }
  676. EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
  677. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  678. struct nd_region_desc *ndr_desc)
  679. {
  680. if (ndr_desc->num_mappings > 1)
  681. return NULL;
  682. ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES);
  683. return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
  684. __func__);
  685. }
  686. EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
  687. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  688. struct nd_region_desc *ndr_desc)
  689. {
  690. ndr_desc->num_lanes = ND_MAX_LANES;
  691. return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
  692. __func__);
  693. }
  694. EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);