region_devs.c 20 KB

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