blob: f9d58c2b5341b4da29ec603b6c18b0a00499d4d3 [file] [log] [blame]
Dan Williams1f7df6f2015-06-09 20:13:14 -04001/*
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 */
Dan Williamseaf96152015-05-01 13:11:27 -040013#include <linux/scatterlist.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040014#include <linux/highmem.h>
Dan Williamseaf96152015-05-01 13:11:27 -040015#include <linux/sched.h>
Dan Williams1f7df6f2015-06-09 20:13:14 -040016#include <linux/slab.h>
Dan Williams0c27af62016-05-27 09:23:01 -070017#include <linux/hash.h>
Dan Williamsf284a4f2016-07-07 19:44:50 -070018#include <linux/pmem.h>
Dan Williamseaf96152015-05-01 13:11:27 -040019#include <linux/sort.h>
Dan Williams1f7df6f2015-06-09 20:13:14 -040020#include <linux/io.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040021#include <linux/nd.h>
Dan Williams1f7df6f2015-06-09 20:13:14 -040022#include "nd-core.h"
23#include "nd.h"
24
Dan Williamsf284a4f2016-07-07 19:44:50 -070025/*
26 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
27 * irrelevant.
28 */
29#include <linux/io-64-nonatomic-hi-lo.h>
30
Dan Williams1f7df6f2015-06-09 20:13:14 -040031static DEFINE_IDA(region_ida);
Dan Williams0c27af62016-05-27 09:23:01 -070032static DEFINE_PER_CPU(int, flush_idx);
Dan Williams1f7df6f2015-06-09 20:13:14 -040033
Dan Williamse5ae3b22016-06-07 17:00:04 -070034static int nvdimm_map_flush(struct device *dev, struct nvdimm *nvdimm, int dimm,
35 struct nd_region_data *ndrd)
36{
37 int i, j;
38
39 dev_dbg(dev, "%s: map %d flush address%s\n", nvdimm_name(nvdimm),
40 nvdimm->num_flush, nvdimm->num_flush == 1 ? "" : "es");
Dan Williams595c7302016-09-23 17:53:52 -070041 for (i = 0; i < (1 << ndrd->hints_shift); i++) {
Dan Williamse5ae3b22016-06-07 17:00:04 -070042 struct resource *res = &nvdimm->flush_wpq[i];
43 unsigned long pfn = PHYS_PFN(res->start);
44 void __iomem *flush_page;
45
46 /* check if flush hints share a page */
47 for (j = 0; j < i; j++) {
48 struct resource *res_j = &nvdimm->flush_wpq[j];
49 unsigned long pfn_j = PHYS_PFN(res_j->start);
50
51 if (pfn == pfn_j)
52 break;
53 }
54
55 if (j < i)
56 flush_page = (void __iomem *) ((unsigned long)
Dan Williams595c7302016-09-23 17:53:52 -070057 ndrd_get_flush_wpq(ndrd, dimm, j)
58 & PAGE_MASK);
Dan Williamse5ae3b22016-06-07 17:00:04 -070059 else
60 flush_page = devm_nvdimm_ioremap(dev,
Oliver O'Halloran480b6832016-09-19 20:19:00 +100061 PFN_PHYS(pfn), PAGE_SIZE);
Dan Williamse5ae3b22016-06-07 17:00:04 -070062 if (!flush_page)
63 return -ENXIO;
Dan Williams595c7302016-09-23 17:53:52 -070064 ndrd_set_flush_wpq(ndrd, dimm, i, flush_page
65 + (res->start & ~PAGE_MASK));
Dan Williamse5ae3b22016-06-07 17:00:04 -070066 }
67
68 return 0;
69}
70
71int nd_region_activate(struct nd_region *nd_region)
72{
Dave Jiangdb580282016-09-26 11:06:50 -070073 int i, j, num_flush = 0;
Dan Williamse5ae3b22016-06-07 17:00:04 -070074 struct nd_region_data *ndrd;
75 struct device *dev = &nd_region->dev;
76 size_t flush_data_size = sizeof(void *);
77
78 nvdimm_bus_lock(&nd_region->dev);
79 for (i = 0; i < nd_region->ndr_mappings; i++) {
80 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
81 struct nvdimm *nvdimm = nd_mapping->nvdimm;
82
83 /* at least one null hint slot per-dimm for the "no-hint" case */
84 flush_data_size += sizeof(void *);
Dan Williams0c27af62016-05-27 09:23:01 -070085 num_flush = min_not_zero(num_flush, nvdimm->num_flush);
Dan Williamse5ae3b22016-06-07 17:00:04 -070086 if (!nvdimm->num_flush)
87 continue;
88 flush_data_size += nvdimm->num_flush * sizeof(void *);
89 }
90 nvdimm_bus_unlock(&nd_region->dev);
91
92 ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL);
93 if (!ndrd)
94 return -ENOMEM;
95 dev_set_drvdata(dev, ndrd);
96
Dan Williams595c7302016-09-23 17:53:52 -070097 if (!num_flush)
98 return 0;
99
100 ndrd->hints_shift = ilog2(num_flush);
Dan Williamse5ae3b22016-06-07 17:00:04 -0700101 for (i = 0; i < nd_region->ndr_mappings; i++) {
102 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
103 struct nvdimm *nvdimm = nd_mapping->nvdimm;
104 int rc = nvdimm_map_flush(&nd_region->dev, nvdimm, i, ndrd);
105
106 if (rc)
107 return rc;
108 }
109
Dave Jiangdb580282016-09-26 11:06:50 -0700110 /*
111 * Clear out entries that are duplicates. This should prevent the
112 * extra flushings.
113 */
114 for (i = 0; i < nd_region->ndr_mappings - 1; i++) {
115 /* ignore if NULL already */
116 if (!ndrd_get_flush_wpq(ndrd, i, 0))
117 continue;
118
119 for (j = i + 1; j < nd_region->ndr_mappings; j++)
120 if (ndrd_get_flush_wpq(ndrd, i, 0) ==
121 ndrd_get_flush_wpq(ndrd, j, 0))
122 ndrd_set_flush_wpq(ndrd, j, 0, NULL);
123 }
124
Dan Williamse5ae3b22016-06-07 17:00:04 -0700125 return 0;
126}
127
Dan Williams1f7df6f2015-06-09 20:13:14 -0400128static void nd_region_release(struct device *dev)
129{
130 struct nd_region *nd_region = to_nd_region(dev);
131 u16 i;
132
133 for (i = 0; i < nd_region->ndr_mappings; i++) {
134 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
135 struct nvdimm *nvdimm = nd_mapping->nvdimm;
136
137 put_device(&nvdimm->dev);
138 }
Vishal Verma5212e112015-06-25 04:20:32 -0400139 free_percpu(nd_region->lane);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400140 ida_simple_remove(&region_ida, nd_region->id);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400141 if (is_nd_blk(dev))
142 kfree(to_nd_blk_region(dev));
143 else
144 kfree(nd_region);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400145}
146
147static struct device_type nd_blk_device_type = {
148 .name = "nd_blk",
149 .release = nd_region_release,
150};
151
152static struct device_type nd_pmem_device_type = {
153 .name = "nd_pmem",
154 .release = nd_region_release,
155};
156
157static struct device_type nd_volatile_device_type = {
158 .name = "nd_volatile",
159 .release = nd_region_release,
160};
161
Dan Williams3d880022015-05-31 15:02:11 -0400162bool is_nd_pmem(struct device *dev)
Dan Williams1f7df6f2015-06-09 20:13:14 -0400163{
164 return dev ? dev->type == &nd_pmem_device_type : false;
165}
166
Dan Williams3d880022015-05-31 15:02:11 -0400167bool is_nd_blk(struct device *dev)
168{
169 return dev ? dev->type == &nd_blk_device_type : false;
170}
171
Dan Williams1f7df6f2015-06-09 20:13:14 -0400172struct nd_region *to_nd_region(struct device *dev)
173{
174 struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
175
176 WARN_ON(dev->type->release != nd_region_release);
177 return nd_region;
178}
179EXPORT_SYMBOL_GPL(to_nd_region);
180
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400181struct nd_blk_region *to_nd_blk_region(struct device *dev)
182{
183 struct nd_region *nd_region = to_nd_region(dev);
184
185 WARN_ON(!is_nd_blk(dev));
186 return container_of(nd_region, struct nd_blk_region, nd_region);
187}
188EXPORT_SYMBOL_GPL(to_nd_blk_region);
189
190void *nd_region_provider_data(struct nd_region *nd_region)
191{
192 return nd_region->provider_data;
193}
194EXPORT_SYMBOL_GPL(nd_region_provider_data);
195
196void *nd_blk_region_provider_data(struct nd_blk_region *ndbr)
197{
198 return ndbr->blk_provider_data;
199}
200EXPORT_SYMBOL_GPL(nd_blk_region_provider_data);
201
202void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data)
203{
204 ndbr->blk_provider_data = data;
205}
206EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data);
207
Dan Williams3d880022015-05-31 15:02:11 -0400208/**
209 * nd_region_to_nstype() - region to an integer namespace type
210 * @nd_region: region-device to interrogate
211 *
212 * This is the 'nstype' attribute of a region as well, an input to the
213 * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
214 * namespace devices with namespace drivers.
215 */
216int nd_region_to_nstype(struct nd_region *nd_region)
217{
218 if (is_nd_pmem(&nd_region->dev)) {
219 u16 i, alias;
220
221 for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) {
222 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
223 struct nvdimm *nvdimm = nd_mapping->nvdimm;
224
225 if (nvdimm->flags & NDD_ALIASING)
226 alias++;
227 }
228 if (alias)
229 return ND_DEVICE_NAMESPACE_PMEM;
230 else
231 return ND_DEVICE_NAMESPACE_IO;
232 } else if (is_nd_blk(&nd_region->dev)) {
233 return ND_DEVICE_NAMESPACE_BLK;
234 }
235
236 return 0;
237}
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400238EXPORT_SYMBOL(nd_region_to_nstype);
239
Dan Williams1f7df6f2015-06-09 20:13:14 -0400240static ssize_t size_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
242{
243 struct nd_region *nd_region = to_nd_region(dev);
244 unsigned long long size = 0;
245
246 if (is_nd_pmem(dev)) {
247 size = nd_region->ndr_size;
248 } else if (nd_region->ndr_mappings == 1) {
249 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
250
251 size = nd_mapping->size;
252 }
253
254 return sprintf(buf, "%llu\n", size);
255}
256static DEVICE_ATTR_RO(size);
257
258static ssize_t mappings_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
260{
261 struct nd_region *nd_region = to_nd_region(dev);
262
263 return sprintf(buf, "%d\n", nd_region->ndr_mappings);
264}
265static DEVICE_ATTR_RO(mappings);
266
Dan Williams3d880022015-05-31 15:02:11 -0400267static ssize_t nstype_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
269{
270 struct nd_region *nd_region = to_nd_region(dev);
271
272 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
273}
274static DEVICE_ATTR_RO(nstype);
275
Dan Williamseaf96152015-05-01 13:11:27 -0400276static ssize_t set_cookie_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct nd_region *nd_region = to_nd_region(dev);
280 struct nd_interleave_set *nd_set = nd_region->nd_set;
281
282 if (is_nd_pmem(dev) && nd_set)
283 /* pass, should be precluded by region_visible */;
284 else
285 return -ENXIO;
286
287 return sprintf(buf, "%#llx\n", nd_set->cookie);
288}
289static DEVICE_ATTR_RO(set_cookie);
290
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400291resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
292{
293 resource_size_t blk_max_overlap = 0, available, overlap;
294 int i;
295
296 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
297
298 retry:
299 available = 0;
300 overlap = blk_max_overlap;
301 for (i = 0; i < nd_region->ndr_mappings; i++) {
302 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
303 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
304
305 /* if a dimm is disabled the available capacity is zero */
306 if (!ndd)
307 return 0;
308
309 if (is_nd_pmem(&nd_region->dev)) {
310 available += nd_pmem_available_dpa(nd_region,
311 nd_mapping, &overlap);
312 if (overlap > blk_max_overlap) {
313 blk_max_overlap = overlap;
314 goto retry;
315 }
316 } else if (is_nd_blk(&nd_region->dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400317 available += nd_blk_available_dpa(nd_mapping);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400318 }
319 }
320
321 return available;
322}
323
324static ssize_t available_size_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
327 struct nd_region *nd_region = to_nd_region(dev);
328 unsigned long long available = 0;
329
330 /*
331 * Flush in-flight updates and grab a snapshot of the available
332 * size. Of course, this value is potentially invalidated the
333 * memory nvdimm_bus_lock() is dropped, but that's userspace's
334 * problem to not race itself.
335 */
336 nvdimm_bus_lock(dev);
337 wait_nvdimm_bus_probe_idle(dev);
338 available = nd_region_available_dpa(nd_region);
339 nvdimm_bus_unlock(dev);
340
341 return sprintf(buf, "%llu\n", available);
342}
343static DEVICE_ATTR_RO(available_size);
344
Dan Williams3d880022015-05-31 15:02:11 -0400345static ssize_t init_namespaces_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
Dan Williamse5ae3b22016-06-07 17:00:04 -0700348 struct nd_region_data *ndrd = dev_get_drvdata(dev);
Dan Williams3d880022015-05-31 15:02:11 -0400349 ssize_t rc;
350
351 nvdimm_bus_lock(dev);
Dan Williamse5ae3b22016-06-07 17:00:04 -0700352 if (ndrd)
353 rc = sprintf(buf, "%d/%d\n", ndrd->ns_active, ndrd->ns_count);
Dan Williams3d880022015-05-31 15:02:11 -0400354 else
355 rc = -ENXIO;
356 nvdimm_bus_unlock(dev);
357
358 return rc;
359}
360static DEVICE_ATTR_RO(init_namespaces);
361
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400362static ssize_t namespace_seed_show(struct device *dev,
363 struct device_attribute *attr, char *buf)
364{
365 struct nd_region *nd_region = to_nd_region(dev);
366 ssize_t rc;
367
368 nvdimm_bus_lock(dev);
369 if (nd_region->ns_seed)
370 rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
371 else
372 rc = sprintf(buf, "\n");
373 nvdimm_bus_unlock(dev);
374 return rc;
375}
376static DEVICE_ATTR_RO(namespace_seed);
377
Dan Williams8c2f7e82015-06-25 04:20:04 -0400378static ssize_t btt_seed_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
381 struct nd_region *nd_region = to_nd_region(dev);
382 ssize_t rc;
383
384 nvdimm_bus_lock(dev);
385 if (nd_region->btt_seed)
386 rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
387 else
388 rc = sprintf(buf, "\n");
389 nvdimm_bus_unlock(dev);
390
391 return rc;
392}
393static DEVICE_ATTR_RO(btt_seed);
394
Dan Williamse1455742015-07-30 17:57:47 -0400395static ssize_t pfn_seed_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
397{
398 struct nd_region *nd_region = to_nd_region(dev);
399 ssize_t rc;
400
401 nvdimm_bus_lock(dev);
402 if (nd_region->pfn_seed)
403 rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
404 else
405 rc = sprintf(buf, "\n");
406 nvdimm_bus_unlock(dev);
407
408 return rc;
409}
410static DEVICE_ATTR_RO(pfn_seed);
411
Dan Williamscd034122016-03-11 10:15:36 -0800412static ssize_t dax_seed_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
415 struct nd_region *nd_region = to_nd_region(dev);
416 ssize_t rc;
417
418 nvdimm_bus_lock(dev);
419 if (nd_region->dax_seed)
420 rc = sprintf(buf, "%s\n", dev_name(nd_region->dax_seed));
421 else
422 rc = sprintf(buf, "\n");
423 nvdimm_bus_unlock(dev);
424
425 return rc;
426}
427static DEVICE_ATTR_RO(dax_seed);
428
Dan Williams58138822015-06-23 20:08:34 -0400429static ssize_t read_only_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
432 struct nd_region *nd_region = to_nd_region(dev);
433
434 return sprintf(buf, "%d\n", nd_region->ro);
435}
436
437static ssize_t read_only_store(struct device *dev,
438 struct device_attribute *attr, const char *buf, size_t len)
439{
440 bool ro;
441 int rc = strtobool(buf, &ro);
442 struct nd_region *nd_region = to_nd_region(dev);
443
444 if (rc)
445 return rc;
446
447 nd_region->ro = ro;
448 return len;
449}
450static DEVICE_ATTR_RW(read_only);
451
Dan Williams1f7df6f2015-06-09 20:13:14 -0400452static struct attribute *nd_region_attributes[] = {
453 &dev_attr_size.attr,
Dan Williams3d880022015-05-31 15:02:11 -0400454 &dev_attr_nstype.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400455 &dev_attr_mappings.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -0400456 &dev_attr_btt_seed.attr,
Dan Williamse1455742015-07-30 17:57:47 -0400457 &dev_attr_pfn_seed.attr,
Dan Williamscd034122016-03-11 10:15:36 -0800458 &dev_attr_dax_seed.attr,
Dan Williams58138822015-06-23 20:08:34 -0400459 &dev_attr_read_only.attr,
Dan Williamseaf96152015-05-01 13:11:27 -0400460 &dev_attr_set_cookie.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400461 &dev_attr_available_size.attr,
462 &dev_attr_namespace_seed.attr,
Dan Williams3d880022015-05-31 15:02:11 -0400463 &dev_attr_init_namespaces.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400464 NULL,
465};
466
Dan Williamseaf96152015-05-01 13:11:27 -0400467static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
468{
469 struct device *dev = container_of(kobj, typeof(*dev), kobj);
470 struct nd_region *nd_region = to_nd_region(dev);
471 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400472 int type = nd_region_to_nstype(nd_region);
Dan Williamseaf96152015-05-01 13:11:27 -0400473
Dmitry Krivenok6bb691a2015-12-02 09:39:29 +0300474 if (!is_nd_pmem(dev) && a == &dev_attr_pfn_seed.attr)
475 return 0;
476
Dan Williamscd034122016-03-11 10:15:36 -0800477 if (!is_nd_pmem(dev) && a == &dev_attr_dax_seed.attr)
478 return 0;
479
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400480 if (a != &dev_attr_set_cookie.attr
481 && a != &dev_attr_available_size.attr)
Dan Williamseaf96152015-05-01 13:11:27 -0400482 return a->mode;
483
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400484 if ((type == ND_DEVICE_NAMESPACE_PMEM
485 || type == ND_DEVICE_NAMESPACE_BLK)
486 && a == &dev_attr_available_size.attr)
487 return a->mode;
488 else if (is_nd_pmem(dev) && nd_set)
489 return a->mode;
Dan Williamseaf96152015-05-01 13:11:27 -0400490
491 return 0;
492}
493
Dan Williams1f7df6f2015-06-09 20:13:14 -0400494struct attribute_group nd_region_attribute_group = {
495 .attrs = nd_region_attributes,
Dan Williamseaf96152015-05-01 13:11:27 -0400496 .is_visible = region_visible,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400497};
498EXPORT_SYMBOL_GPL(nd_region_attribute_group);
499
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400500u64 nd_region_interleave_set_cookie(struct nd_region *nd_region)
501{
502 struct nd_interleave_set *nd_set = nd_region->nd_set;
503
504 if (nd_set)
505 return nd_set->cookie;
506 return 0;
507}
508
Dan Williamseaf96152015-05-01 13:11:27 -0400509/*
510 * Upon successful probe/remove, take/release a reference on the
Dan Williams8c2f7e82015-06-25 04:20:04 -0400511 * associated interleave set (if present), and plant new btt + namespace
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400512 * seeds. Also, on the removal of a BLK region, notify the provider to
513 * disable the region.
Dan Williamseaf96152015-05-01 13:11:27 -0400514 */
515static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
516 struct device *dev, bool probe)
517{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400518 struct nd_region *nd_region;
519
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400520 if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) {
Dan Williamseaf96152015-05-01 13:11:27 -0400521 int i;
522
Dan Williams8c2f7e82015-06-25 04:20:04 -0400523 nd_region = to_nd_region(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400524 for (i = 0; i < nd_region->ndr_mappings; i++) {
525 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400526 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
Dan Williamseaf96152015-05-01 13:11:27 -0400527 struct nvdimm *nvdimm = nd_mapping->nvdimm;
528
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400529 kfree(nd_mapping->labels);
530 nd_mapping->labels = NULL;
531 put_ndd(ndd);
532 nd_mapping->ndd = NULL;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400533 if (ndd)
534 atomic_dec(&nvdimm->busy);
Dan Williamseaf96152015-05-01 13:11:27 -0400535 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400536
537 if (is_nd_pmem(dev))
538 return;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400539 }
540 if (dev->parent && is_nd_blk(dev->parent) && probe) {
541 nd_region = to_nd_region(dev->parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400542 nvdimm_bus_lock(dev);
543 if (nd_region->ns_seed == dev)
544 nd_region_create_blk_seed(nd_region);
545 nvdimm_bus_unlock(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400546 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400547 if (is_nd_btt(dev) && probe) {
Dan Williams8ca24352015-07-24 23:42:34 -0400548 struct nd_btt *nd_btt = to_nd_btt(dev);
549
Dan Williams8c2f7e82015-06-25 04:20:04 -0400550 nd_region = to_nd_region(dev->parent);
551 nvdimm_bus_lock(dev);
552 if (nd_region->btt_seed == dev)
553 nd_region_create_btt_seed(nd_region);
Dan Williams8ca24352015-07-24 23:42:34 -0400554 if (nd_region->ns_seed == &nd_btt->ndns->dev &&
555 is_nd_blk(dev->parent))
556 nd_region_create_blk_seed(nd_region);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400557 nvdimm_bus_unlock(dev);
558 }
Dan Williams2dc43332015-12-13 11:41:36 -0800559 if (is_nd_pfn(dev) && probe) {
560 nd_region = to_nd_region(dev->parent);
561 nvdimm_bus_lock(dev);
562 if (nd_region->pfn_seed == dev)
563 nd_region_create_pfn_seed(nd_region);
564 nvdimm_bus_unlock(dev);
565 }
Dan Williamscd034122016-03-11 10:15:36 -0800566 if (is_nd_dax(dev) && probe) {
567 nd_region = to_nd_region(dev->parent);
568 nvdimm_bus_lock(dev);
569 if (nd_region->dax_seed == dev)
570 nd_region_create_dax_seed(nd_region);
571 nvdimm_bus_unlock(dev);
572 }
Dan Williamseaf96152015-05-01 13:11:27 -0400573}
574
575void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
576{
577 nd_region_notify_driver_action(nvdimm_bus, dev, true);
578}
579
580void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
581{
582 nd_region_notify_driver_action(nvdimm_bus, dev, false);
583}
584
Dan Williams1f7df6f2015-06-09 20:13:14 -0400585static ssize_t mappingN(struct device *dev, char *buf, int n)
586{
587 struct nd_region *nd_region = to_nd_region(dev);
588 struct nd_mapping *nd_mapping;
589 struct nvdimm *nvdimm;
590
591 if (n >= nd_region->ndr_mappings)
592 return -ENXIO;
593 nd_mapping = &nd_region->mapping[n];
594 nvdimm = nd_mapping->nvdimm;
595
596 return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev),
597 nd_mapping->start, nd_mapping->size);
598}
599
600#define REGION_MAPPING(idx) \
601static ssize_t mapping##idx##_show(struct device *dev, \
602 struct device_attribute *attr, char *buf) \
603{ \
604 return mappingN(dev, buf, idx); \
605} \
606static DEVICE_ATTR_RO(mapping##idx)
607
608/*
609 * 32 should be enough for a while, even in the presence of socket
610 * interleave a 32-way interleave set is a degenerate case.
611 */
612REGION_MAPPING(0);
613REGION_MAPPING(1);
614REGION_MAPPING(2);
615REGION_MAPPING(3);
616REGION_MAPPING(4);
617REGION_MAPPING(5);
618REGION_MAPPING(6);
619REGION_MAPPING(7);
620REGION_MAPPING(8);
621REGION_MAPPING(9);
622REGION_MAPPING(10);
623REGION_MAPPING(11);
624REGION_MAPPING(12);
625REGION_MAPPING(13);
626REGION_MAPPING(14);
627REGION_MAPPING(15);
628REGION_MAPPING(16);
629REGION_MAPPING(17);
630REGION_MAPPING(18);
631REGION_MAPPING(19);
632REGION_MAPPING(20);
633REGION_MAPPING(21);
634REGION_MAPPING(22);
635REGION_MAPPING(23);
636REGION_MAPPING(24);
637REGION_MAPPING(25);
638REGION_MAPPING(26);
639REGION_MAPPING(27);
640REGION_MAPPING(28);
641REGION_MAPPING(29);
642REGION_MAPPING(30);
643REGION_MAPPING(31);
644
645static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
646{
647 struct device *dev = container_of(kobj, struct device, kobj);
648 struct nd_region *nd_region = to_nd_region(dev);
649
650 if (n < nd_region->ndr_mappings)
651 return a->mode;
652 return 0;
653}
654
655static struct attribute *mapping_attributes[] = {
656 &dev_attr_mapping0.attr,
657 &dev_attr_mapping1.attr,
658 &dev_attr_mapping2.attr,
659 &dev_attr_mapping3.attr,
660 &dev_attr_mapping4.attr,
661 &dev_attr_mapping5.attr,
662 &dev_attr_mapping6.attr,
663 &dev_attr_mapping7.attr,
664 &dev_attr_mapping8.attr,
665 &dev_attr_mapping9.attr,
666 &dev_attr_mapping10.attr,
667 &dev_attr_mapping11.attr,
668 &dev_attr_mapping12.attr,
669 &dev_attr_mapping13.attr,
670 &dev_attr_mapping14.attr,
671 &dev_attr_mapping15.attr,
672 &dev_attr_mapping16.attr,
673 &dev_attr_mapping17.attr,
674 &dev_attr_mapping18.attr,
675 &dev_attr_mapping19.attr,
676 &dev_attr_mapping20.attr,
677 &dev_attr_mapping21.attr,
678 &dev_attr_mapping22.attr,
679 &dev_attr_mapping23.attr,
680 &dev_attr_mapping24.attr,
681 &dev_attr_mapping25.attr,
682 &dev_attr_mapping26.attr,
683 &dev_attr_mapping27.attr,
684 &dev_attr_mapping28.attr,
685 &dev_attr_mapping29.attr,
686 &dev_attr_mapping30.attr,
687 &dev_attr_mapping31.attr,
688 NULL,
689};
690
691struct attribute_group nd_mapping_attribute_group = {
692 .is_visible = mapping_visible,
693 .attrs = mapping_attributes,
694};
695EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
696
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400697int nd_blk_region_init(struct nd_region *nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -0400698{
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400699 struct device *dev = &nd_region->dev;
700 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
701
702 if (!is_nd_blk(dev))
703 return 0;
704
705 if (nd_region->ndr_mappings < 1) {
706 dev_err(dev, "invalid BLK region\n");
707 return -ENXIO;
708 }
709
710 return to_nd_blk_region(dev)->enable(nvdimm_bus, dev);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400711}
Dan Williams1f7df6f2015-06-09 20:13:14 -0400712
Vishal Verma5212e112015-06-25 04:20:32 -0400713/**
714 * nd_region_acquire_lane - allocate and lock a lane
715 * @nd_region: region id and number of lanes possible
716 *
717 * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
718 * We optimize for the common case where there are 256 lanes, one
719 * per-cpu. For larger systems we need to lock to share lanes. For now
720 * this implementation assumes the cost of maintaining an allocator for
721 * free lanes is on the order of the lock hold time, so it implements a
722 * static lane = cpu % num_lanes mapping.
723 *
724 * In the case of a BTT instance on top of a BLK namespace a lane may be
725 * acquired recursively. We lock on the first instance.
726 *
727 * In the case of a BTT instance on top of PMEM, we only acquire a lane
728 * for the BTT metadata updates.
729 */
730unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
731{
732 unsigned int cpu, lane;
733
734 cpu = get_cpu();
735 if (nd_region->num_lanes < nr_cpu_ids) {
736 struct nd_percpu_lane *ndl_lock, *ndl_count;
737
738 lane = cpu % nd_region->num_lanes;
739 ndl_count = per_cpu_ptr(nd_region->lane, cpu);
740 ndl_lock = per_cpu_ptr(nd_region->lane, lane);
741 if (ndl_count->count++ == 0)
742 spin_lock(&ndl_lock->lock);
743 } else
744 lane = cpu;
745
746 return lane;
747}
748EXPORT_SYMBOL(nd_region_acquire_lane);
749
750void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
751{
752 if (nd_region->num_lanes < nr_cpu_ids) {
753 unsigned int cpu = get_cpu();
754 struct nd_percpu_lane *ndl_lock, *ndl_count;
755
756 ndl_count = per_cpu_ptr(nd_region->lane, cpu);
757 ndl_lock = per_cpu_ptr(nd_region->lane, lane);
758 if (--ndl_count->count == 0)
759 spin_unlock(&ndl_lock->lock);
760 put_cpu();
761 }
762 put_cpu();
763}
764EXPORT_SYMBOL(nd_region_release_lane);
765
Dan Williams1f7df6f2015-06-09 20:13:14 -0400766static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
767 struct nd_region_desc *ndr_desc, struct device_type *dev_type,
768 const char *caller)
769{
770 struct nd_region *nd_region;
771 struct device *dev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400772 void *region_buf;
Vishal Verma5212e112015-06-25 04:20:32 -0400773 unsigned int i;
Dan Williams58138822015-06-23 20:08:34 -0400774 int ro = 0;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400775
776 for (i = 0; i < ndr_desc->num_mappings; i++) {
777 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
778 struct nvdimm *nvdimm = nd_mapping->nvdimm;
779
780 if ((nd_mapping->start | nd_mapping->size) % SZ_4K) {
781 dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
782 caller, dev_name(&nvdimm->dev), i);
783
784 return NULL;
785 }
Dan Williams58138822015-06-23 20:08:34 -0400786
787 if (nvdimm->flags & NDD_UNARMED)
788 ro = 1;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400789 }
790
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400791 if (dev_type == &nd_blk_device_type) {
792 struct nd_blk_region_desc *ndbr_desc;
793 struct nd_blk_region *ndbr;
794
795 ndbr_desc = to_blk_region_desc(ndr_desc);
796 ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping)
797 * ndr_desc->num_mappings,
798 GFP_KERNEL);
799 if (ndbr) {
800 nd_region = &ndbr->nd_region;
801 ndbr->enable = ndbr_desc->enable;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400802 ndbr->do_io = ndbr_desc->do_io;
803 }
804 region_buf = ndbr;
805 } else {
806 nd_region = kzalloc(sizeof(struct nd_region)
807 + sizeof(struct nd_mapping)
808 * ndr_desc->num_mappings,
809 GFP_KERNEL);
810 region_buf = nd_region;
811 }
812
813 if (!region_buf)
Dan Williams1f7df6f2015-06-09 20:13:14 -0400814 return NULL;
815 nd_region->id = ida_simple_get(&region_ida, 0, 0, GFP_KERNEL);
Vishal Verma5212e112015-06-25 04:20:32 -0400816 if (nd_region->id < 0)
817 goto err_id;
818
819 nd_region->lane = alloc_percpu(struct nd_percpu_lane);
820 if (!nd_region->lane)
821 goto err_percpu;
822
823 for (i = 0; i < nr_cpu_ids; i++) {
824 struct nd_percpu_lane *ndl;
825
826 ndl = per_cpu_ptr(nd_region->lane, i);
827 spin_lock_init(&ndl->lock);
828 ndl->count = 0;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400829 }
830
831 memcpy(nd_region->mapping, ndr_desc->nd_mapping,
832 sizeof(struct nd_mapping) * ndr_desc->num_mappings);
833 for (i = 0; i < ndr_desc->num_mappings; i++) {
834 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
835 struct nvdimm *nvdimm = nd_mapping->nvdimm;
836
837 get_device(&nvdimm->dev);
838 }
839 nd_region->ndr_mappings = ndr_desc->num_mappings;
840 nd_region->provider_data = ndr_desc->provider_data;
Dan Williamseaf96152015-05-01 13:11:27 -0400841 nd_region->nd_set = ndr_desc->nd_set;
Vishal Verma5212e112015-06-25 04:20:32 -0400842 nd_region->num_lanes = ndr_desc->num_lanes;
Dan Williams004f1af2015-08-24 19:20:23 -0400843 nd_region->flags = ndr_desc->flags;
Dan Williams58138822015-06-23 20:08:34 -0400844 nd_region->ro = ro;
Toshi Kani41d7a6d2015-06-19 12:18:33 -0600845 nd_region->numa_node = ndr_desc->numa_node;
Dan Williams1b40e092015-05-01 13:34:01 -0400846 ida_init(&nd_region->ns_ida);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400847 ida_init(&nd_region->btt_ida);
Dan Williamse1455742015-07-30 17:57:47 -0400848 ida_init(&nd_region->pfn_ida);
Dan Williamscd034122016-03-11 10:15:36 -0800849 ida_init(&nd_region->dax_ida);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400850 dev = &nd_region->dev;
851 dev_set_name(dev, "region%d", nd_region->id);
852 dev->parent = &nvdimm_bus->dev;
853 dev->type = dev_type;
854 dev->groups = ndr_desc->attr_groups;
855 nd_region->ndr_size = resource_size(ndr_desc->res);
856 nd_region->ndr_start = ndr_desc->res->start;
857 nd_device_register(dev);
858
859 return nd_region;
Vishal Verma5212e112015-06-25 04:20:32 -0400860
861 err_percpu:
862 ida_simple_remove(&region_ida, nd_region->id);
863 err_id:
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400864 kfree(region_buf);
Vishal Verma5212e112015-06-25 04:20:32 -0400865 return NULL;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400866}
867
868struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
869 struct nd_region_desc *ndr_desc)
870{
Vishal Verma5212e112015-06-25 04:20:32 -0400871 ndr_desc->num_lanes = ND_MAX_LANES;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400872 return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
873 __func__);
874}
875EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
876
877struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
878 struct nd_region_desc *ndr_desc)
879{
880 if (ndr_desc->num_mappings > 1)
881 return NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400882 ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400883 return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
884 __func__);
885}
886EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
887
888struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
889 struct nd_region_desc *ndr_desc)
890{
Vishal Verma5212e112015-06-25 04:20:32 -0400891 ndr_desc->num_lanes = ND_MAX_LANES;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400892 return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
893 __func__);
894}
895EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
Dan Williamsb354aba2016-05-17 20:24:16 -0700896
Dan Williamsf284a4f2016-07-07 19:44:50 -0700897/**
898 * nvdimm_flush - flush any posted write queues between the cpu and pmem media
899 * @nd_region: blk or interleaved pmem region
900 */
901void nvdimm_flush(struct nd_region *nd_region)
902{
903 struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
Dan Williams0c27af62016-05-27 09:23:01 -0700904 int i, idx;
905
906 /*
907 * Try to encourage some diversity in flush hint addresses
908 * across cpus assuming a limited number of flush hints.
909 */
910 idx = this_cpu_read(flush_idx);
911 idx = this_cpu_add_return(flush_idx, hash_32(current->pid + idx, 8));
Dan Williamsf284a4f2016-07-07 19:44:50 -0700912
913 /*
914 * The first wmb() is needed to 'sfence' all previous writes
915 * such that they are architecturally visible for the platform
916 * buffer flush. Note that we've already arranged for pmem
917 * writes to avoid the cache via arch_memcpy_to_pmem(). The
918 * final wmb() ensures ordering for the NVDIMM flush write.
919 */
920 wmb();
921 for (i = 0; i < nd_region->ndr_mappings; i++)
Dan Williams595c7302016-09-23 17:53:52 -0700922 if (ndrd_get_flush_wpq(ndrd, i, 0))
923 writeq(1, ndrd_get_flush_wpq(ndrd, i, idx));
Dan Williamsf284a4f2016-07-07 19:44:50 -0700924 wmb();
925}
926EXPORT_SYMBOL_GPL(nvdimm_flush);
927
928/**
929 * nvdimm_has_flush - determine write flushing requirements
930 * @nd_region: blk or interleaved pmem region
931 *
932 * Returns 1 if writes require flushing
933 * Returns 0 if writes do not require flushing
934 * Returns -ENXIO if flushing capability can not be determined
935 */
936int nvdimm_has_flush(struct nd_region *nd_region)
937{
938 struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
939 int i;
940
941 /* no nvdimm == flushing capability unknown */
942 if (nd_region->ndr_mappings == 0)
943 return -ENXIO;
944
945 for (i = 0; i < nd_region->ndr_mappings; i++)
946 /* flush hints present, flushing required */
Dan Williams595c7302016-09-23 17:53:52 -0700947 if (ndrd_get_flush_wpq(ndrd, i, 0))
Dan Williamsf284a4f2016-07-07 19:44:50 -0700948 return 1;
949
950 /*
951 * The platform defines dimm devices without hints, assume
952 * platform persistence mechanism like ADR
953 */
954 return 0;
955}
956EXPORT_SYMBOL_GPL(nvdimm_has_flush);
957
Dan Williamsb354aba2016-05-17 20:24:16 -0700958void __exit nd_region_devs_exit(void)
959{
960 ida_destroy(&region_ida);
961}