region_devs.c 20 KB

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