region_devs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 read_only_show(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. struct nd_region *nd_region = to_nd_region(dev);
  295. return sprintf(buf, "%d\n", nd_region->ro);
  296. }
  297. static ssize_t read_only_store(struct device *dev,
  298. struct device_attribute *attr, const char *buf, size_t len)
  299. {
  300. bool ro;
  301. int rc = strtobool(buf, &ro);
  302. struct nd_region *nd_region = to_nd_region(dev);
  303. if (rc)
  304. return rc;
  305. nd_region->ro = ro;
  306. return len;
  307. }
  308. static DEVICE_ATTR_RW(read_only);
  309. static struct attribute *nd_region_attributes[] = {
  310. &dev_attr_size.attr,
  311. &dev_attr_nstype.attr,
  312. &dev_attr_mappings.attr,
  313. &dev_attr_btt_seed.attr,
  314. &dev_attr_read_only.attr,
  315. &dev_attr_set_cookie.attr,
  316. &dev_attr_available_size.attr,
  317. &dev_attr_namespace_seed.attr,
  318. &dev_attr_init_namespaces.attr,
  319. NULL,
  320. };
  321. static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
  322. {
  323. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  324. struct nd_region *nd_region = to_nd_region(dev);
  325. struct nd_interleave_set *nd_set = nd_region->nd_set;
  326. int type = nd_region_to_nstype(nd_region);
  327. if (a != &dev_attr_set_cookie.attr
  328. && a != &dev_attr_available_size.attr)
  329. return a->mode;
  330. if ((type == ND_DEVICE_NAMESPACE_PMEM
  331. || type == ND_DEVICE_NAMESPACE_BLK)
  332. && a == &dev_attr_available_size.attr)
  333. return a->mode;
  334. else if (is_nd_pmem(dev) && nd_set)
  335. return a->mode;
  336. return 0;
  337. }
  338. struct attribute_group nd_region_attribute_group = {
  339. .attrs = nd_region_attributes,
  340. .is_visible = region_visible,
  341. };
  342. EXPORT_SYMBOL_GPL(nd_region_attribute_group);
  343. u64 nd_region_interleave_set_cookie(struct nd_region *nd_region)
  344. {
  345. struct nd_interleave_set *nd_set = nd_region->nd_set;
  346. if (nd_set)
  347. return nd_set->cookie;
  348. return 0;
  349. }
  350. /*
  351. * Upon successful probe/remove, take/release a reference on the
  352. * associated interleave set (if present), and plant new btt + namespace
  353. * seeds. Also, on the removal of a BLK region, notify the provider to
  354. * disable the region.
  355. */
  356. static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
  357. struct device *dev, bool probe)
  358. {
  359. struct nd_region *nd_region;
  360. if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) {
  361. int i;
  362. nd_region = to_nd_region(dev);
  363. for (i = 0; i < nd_region->ndr_mappings; i++) {
  364. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  365. struct nvdimm_drvdata *ndd = nd_mapping->ndd;
  366. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  367. kfree(nd_mapping->labels);
  368. nd_mapping->labels = NULL;
  369. put_ndd(ndd);
  370. nd_mapping->ndd = NULL;
  371. if (ndd)
  372. atomic_dec(&nvdimm->busy);
  373. }
  374. if (is_nd_pmem(dev))
  375. return;
  376. to_nd_blk_region(dev)->disable(nvdimm_bus, dev);
  377. }
  378. if (dev->parent && is_nd_blk(dev->parent) && probe) {
  379. nd_region = to_nd_region(dev->parent);
  380. nvdimm_bus_lock(dev);
  381. if (nd_region->ns_seed == dev)
  382. nd_region_create_blk_seed(nd_region);
  383. nvdimm_bus_unlock(dev);
  384. }
  385. if (is_nd_btt(dev) && probe) {
  386. nd_region = to_nd_region(dev->parent);
  387. nvdimm_bus_lock(dev);
  388. if (nd_region->btt_seed == dev)
  389. nd_region_create_btt_seed(nd_region);
  390. nvdimm_bus_unlock(dev);
  391. }
  392. }
  393. void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
  394. {
  395. nd_region_notify_driver_action(nvdimm_bus, dev, true);
  396. }
  397. void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
  398. {
  399. nd_region_notify_driver_action(nvdimm_bus, dev, false);
  400. }
  401. static ssize_t mappingN(struct device *dev, char *buf, int n)
  402. {
  403. struct nd_region *nd_region = to_nd_region(dev);
  404. struct nd_mapping *nd_mapping;
  405. struct nvdimm *nvdimm;
  406. if (n >= nd_region->ndr_mappings)
  407. return -ENXIO;
  408. nd_mapping = &nd_region->mapping[n];
  409. nvdimm = nd_mapping->nvdimm;
  410. return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev),
  411. nd_mapping->start, nd_mapping->size);
  412. }
  413. #define REGION_MAPPING(idx) \
  414. static ssize_t mapping##idx##_show(struct device *dev, \
  415. struct device_attribute *attr, char *buf) \
  416. { \
  417. return mappingN(dev, buf, idx); \
  418. } \
  419. static DEVICE_ATTR_RO(mapping##idx)
  420. /*
  421. * 32 should be enough for a while, even in the presence of socket
  422. * interleave a 32-way interleave set is a degenerate case.
  423. */
  424. REGION_MAPPING(0);
  425. REGION_MAPPING(1);
  426. REGION_MAPPING(2);
  427. REGION_MAPPING(3);
  428. REGION_MAPPING(4);
  429. REGION_MAPPING(5);
  430. REGION_MAPPING(6);
  431. REGION_MAPPING(7);
  432. REGION_MAPPING(8);
  433. REGION_MAPPING(9);
  434. REGION_MAPPING(10);
  435. REGION_MAPPING(11);
  436. REGION_MAPPING(12);
  437. REGION_MAPPING(13);
  438. REGION_MAPPING(14);
  439. REGION_MAPPING(15);
  440. REGION_MAPPING(16);
  441. REGION_MAPPING(17);
  442. REGION_MAPPING(18);
  443. REGION_MAPPING(19);
  444. REGION_MAPPING(20);
  445. REGION_MAPPING(21);
  446. REGION_MAPPING(22);
  447. REGION_MAPPING(23);
  448. REGION_MAPPING(24);
  449. REGION_MAPPING(25);
  450. REGION_MAPPING(26);
  451. REGION_MAPPING(27);
  452. REGION_MAPPING(28);
  453. REGION_MAPPING(29);
  454. REGION_MAPPING(30);
  455. REGION_MAPPING(31);
  456. static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
  457. {
  458. struct device *dev = container_of(kobj, struct device, kobj);
  459. struct nd_region *nd_region = to_nd_region(dev);
  460. if (n < nd_region->ndr_mappings)
  461. return a->mode;
  462. return 0;
  463. }
  464. static struct attribute *mapping_attributes[] = {
  465. &dev_attr_mapping0.attr,
  466. &dev_attr_mapping1.attr,
  467. &dev_attr_mapping2.attr,
  468. &dev_attr_mapping3.attr,
  469. &dev_attr_mapping4.attr,
  470. &dev_attr_mapping5.attr,
  471. &dev_attr_mapping6.attr,
  472. &dev_attr_mapping7.attr,
  473. &dev_attr_mapping8.attr,
  474. &dev_attr_mapping9.attr,
  475. &dev_attr_mapping10.attr,
  476. &dev_attr_mapping11.attr,
  477. &dev_attr_mapping12.attr,
  478. &dev_attr_mapping13.attr,
  479. &dev_attr_mapping14.attr,
  480. &dev_attr_mapping15.attr,
  481. &dev_attr_mapping16.attr,
  482. &dev_attr_mapping17.attr,
  483. &dev_attr_mapping18.attr,
  484. &dev_attr_mapping19.attr,
  485. &dev_attr_mapping20.attr,
  486. &dev_attr_mapping21.attr,
  487. &dev_attr_mapping22.attr,
  488. &dev_attr_mapping23.attr,
  489. &dev_attr_mapping24.attr,
  490. &dev_attr_mapping25.attr,
  491. &dev_attr_mapping26.attr,
  492. &dev_attr_mapping27.attr,
  493. &dev_attr_mapping28.attr,
  494. &dev_attr_mapping29.attr,
  495. &dev_attr_mapping30.attr,
  496. &dev_attr_mapping31.attr,
  497. NULL,
  498. };
  499. struct attribute_group nd_mapping_attribute_group = {
  500. .is_visible = mapping_visible,
  501. .attrs = mapping_attributes,
  502. };
  503. EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
  504. int nd_blk_region_init(struct nd_region *nd_region)
  505. {
  506. struct device *dev = &nd_region->dev;
  507. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  508. if (!is_nd_blk(dev))
  509. return 0;
  510. if (nd_region->ndr_mappings < 1) {
  511. dev_err(dev, "invalid BLK region\n");
  512. return -ENXIO;
  513. }
  514. return to_nd_blk_region(dev)->enable(nvdimm_bus, dev);
  515. }
  516. /**
  517. * nd_region_acquire_lane - allocate and lock a lane
  518. * @nd_region: region id and number of lanes possible
  519. *
  520. * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
  521. * We optimize for the common case where there are 256 lanes, one
  522. * per-cpu. For larger systems we need to lock to share lanes. For now
  523. * this implementation assumes the cost of maintaining an allocator for
  524. * free lanes is on the order of the lock hold time, so it implements a
  525. * static lane = cpu % num_lanes mapping.
  526. *
  527. * In the case of a BTT instance on top of a BLK namespace a lane may be
  528. * acquired recursively. We lock on the first instance.
  529. *
  530. * In the case of a BTT instance on top of PMEM, we only acquire a lane
  531. * for the BTT metadata updates.
  532. */
  533. unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
  534. {
  535. unsigned int cpu, lane;
  536. cpu = get_cpu();
  537. if (nd_region->num_lanes < nr_cpu_ids) {
  538. struct nd_percpu_lane *ndl_lock, *ndl_count;
  539. lane = cpu % nd_region->num_lanes;
  540. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  541. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  542. if (ndl_count->count++ == 0)
  543. spin_lock(&ndl_lock->lock);
  544. } else
  545. lane = cpu;
  546. return lane;
  547. }
  548. EXPORT_SYMBOL(nd_region_acquire_lane);
  549. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
  550. {
  551. if (nd_region->num_lanes < nr_cpu_ids) {
  552. unsigned int cpu = get_cpu();
  553. struct nd_percpu_lane *ndl_lock, *ndl_count;
  554. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  555. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  556. if (--ndl_count->count == 0)
  557. spin_unlock(&ndl_lock->lock);
  558. put_cpu();
  559. }
  560. put_cpu();
  561. }
  562. EXPORT_SYMBOL(nd_region_release_lane);
  563. static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
  564. struct nd_region_desc *ndr_desc, struct device_type *dev_type,
  565. const char *caller)
  566. {
  567. struct nd_region *nd_region;
  568. struct device *dev;
  569. void *region_buf;
  570. unsigned int i;
  571. int ro = 0;
  572. for (i = 0; i < ndr_desc->num_mappings; i++) {
  573. struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
  574. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  575. if ((nd_mapping->start | nd_mapping->size) % SZ_4K) {
  576. dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
  577. caller, dev_name(&nvdimm->dev), i);
  578. return NULL;
  579. }
  580. if (nvdimm->flags & NDD_UNARMED)
  581. ro = 1;
  582. }
  583. if (dev_type == &nd_blk_device_type) {
  584. struct nd_blk_region_desc *ndbr_desc;
  585. struct nd_blk_region *ndbr;
  586. ndbr_desc = to_blk_region_desc(ndr_desc);
  587. ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping)
  588. * ndr_desc->num_mappings,
  589. GFP_KERNEL);
  590. if (ndbr) {
  591. nd_region = &ndbr->nd_region;
  592. ndbr->enable = ndbr_desc->enable;
  593. ndbr->disable = ndbr_desc->disable;
  594. ndbr->do_io = ndbr_desc->do_io;
  595. }
  596. region_buf = ndbr;
  597. } else {
  598. nd_region = kzalloc(sizeof(struct nd_region)
  599. + sizeof(struct nd_mapping)
  600. * ndr_desc->num_mappings,
  601. GFP_KERNEL);
  602. region_buf = nd_region;
  603. }
  604. if (!region_buf)
  605. return NULL;
  606. nd_region->id = ida_simple_get(&region_ida, 0, 0, GFP_KERNEL);
  607. if (nd_region->id < 0)
  608. goto err_id;
  609. nd_region->lane = alloc_percpu(struct nd_percpu_lane);
  610. if (!nd_region->lane)
  611. goto err_percpu;
  612. for (i = 0; i < nr_cpu_ids; i++) {
  613. struct nd_percpu_lane *ndl;
  614. ndl = per_cpu_ptr(nd_region->lane, i);
  615. spin_lock_init(&ndl->lock);
  616. ndl->count = 0;
  617. }
  618. memcpy(nd_region->mapping, ndr_desc->nd_mapping,
  619. sizeof(struct nd_mapping) * ndr_desc->num_mappings);
  620. for (i = 0; i < ndr_desc->num_mappings; i++) {
  621. struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
  622. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  623. get_device(&nvdimm->dev);
  624. }
  625. nd_region->ndr_mappings = ndr_desc->num_mappings;
  626. nd_region->provider_data = ndr_desc->provider_data;
  627. nd_region->nd_set = ndr_desc->nd_set;
  628. nd_region->num_lanes = ndr_desc->num_lanes;
  629. nd_region->ro = ro;
  630. nd_region->numa_node = ndr_desc->numa_node;
  631. ida_init(&nd_region->ns_ida);
  632. ida_init(&nd_region->btt_ida);
  633. dev = &nd_region->dev;
  634. dev_set_name(dev, "region%d", nd_region->id);
  635. dev->parent = &nvdimm_bus->dev;
  636. dev->type = dev_type;
  637. dev->groups = ndr_desc->attr_groups;
  638. nd_region->ndr_size = resource_size(ndr_desc->res);
  639. nd_region->ndr_start = ndr_desc->res->start;
  640. nd_device_register(dev);
  641. return nd_region;
  642. err_percpu:
  643. ida_simple_remove(&region_ida, nd_region->id);
  644. err_id:
  645. kfree(region_buf);
  646. return NULL;
  647. }
  648. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  649. struct nd_region_desc *ndr_desc)
  650. {
  651. ndr_desc->num_lanes = ND_MAX_LANES;
  652. return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
  653. __func__);
  654. }
  655. EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
  656. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  657. struct nd_region_desc *ndr_desc)
  658. {
  659. if (ndr_desc->num_mappings > 1)
  660. return NULL;
  661. ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES);
  662. return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
  663. __func__);
  664. }
  665. EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
  666. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  667. struct nd_region_desc *ndr_desc)
  668. {
  669. ndr_desc->num_lanes = ND_MAX_LANES;
  670. return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
  671. __func__);
  672. }
  673. EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);