14d88a97aSDan Williams /* 24d88a97aSDan Williams * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. 34d88a97aSDan Williams * 44d88a97aSDan Williams * This program is free software; you can redistribute it and/or modify 54d88a97aSDan Williams * it under the terms of version 2 of the GNU General Public License as 64d88a97aSDan Williams * published by the Free Software Foundation. 74d88a97aSDan Williams * 84d88a97aSDan Williams * This program is distributed in the hope that it will be useful, but 94d88a97aSDan Williams * WITHOUT ANY WARRANTY; without even the implied warranty of 104d88a97aSDan Williams * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 114d88a97aSDan Williams * General Public License for more details. 124d88a97aSDan Williams */ 134d88a97aSDan Williams #ifndef __ND_H__ 144d88a97aSDan Williams #define __ND_H__ 151f7df6f8SDan Williams #include <linux/libnvdimm.h> 16*f0dc089cSDan Williams #include <linux/blkdev.h> 174d88a97aSDan Williams #include <linux/device.h> 184d88a97aSDan Williams #include <linux/mutex.h> 194d88a97aSDan Williams #include <linux/ndctl.h> 20bf9bccc1SDan Williams #include <linux/types.h> 214a826c83SDan Williams #include "label.h" 224d88a97aSDan Williams 238c2f7e86SDan Williams enum { 245212e11fSVishal Verma /* 255212e11fSVishal Verma * Limits the maximum number of block apertures a dimm can 265212e11fSVishal Verma * support and is an input to the geometry/on-disk-format of a 275212e11fSVishal Verma * BTT instance 285212e11fSVishal Verma */ 295212e11fSVishal Verma ND_MAX_LANES = 256, 308c2f7e86SDan Williams SECTOR_SHIFT = 9, 31fcae6957SVishal Verma INT_LBASIZE_ALIGNMENT = 64, 328c2f7e86SDan Williams }; 338c2f7e86SDan Williams 344d88a97aSDan Williams struct nvdimm_drvdata { 354d88a97aSDan Williams struct device *dev; 364a826c83SDan Williams int nsindex_size; 374d88a97aSDan Williams struct nd_cmd_get_config_size nsarea; 384d88a97aSDan Williams void *data; 394a826c83SDan Williams int ns_current, ns_next; 404a826c83SDan Williams struct resource dpa; 41bf9bccc1SDan Williams struct kref kref; 424d88a97aSDan Williams }; 434d88a97aSDan Williams 443d88002eSDan Williams struct nd_region_namespaces { 453d88002eSDan Williams int count; 463d88002eSDan Williams int active; 473d88002eSDan Williams }; 483d88002eSDan Williams 494a826c83SDan Williams static inline struct nd_namespace_index *to_namespace_index( 504a826c83SDan Williams struct nvdimm_drvdata *ndd, int i) 514a826c83SDan Williams { 524a826c83SDan Williams if (i < 0) 534a826c83SDan Williams return NULL; 544a826c83SDan Williams 554a826c83SDan Williams return ndd->data + sizeof_namespace_index(ndd) * i; 564a826c83SDan Williams } 574a826c83SDan Williams 584a826c83SDan Williams static inline struct nd_namespace_index *to_current_namespace_index( 594a826c83SDan Williams struct nvdimm_drvdata *ndd) 604a826c83SDan Williams { 614a826c83SDan Williams return to_namespace_index(ndd, ndd->ns_current); 624a826c83SDan Williams } 634a826c83SDan Williams 644a826c83SDan Williams static inline struct nd_namespace_index *to_next_namespace_index( 654a826c83SDan Williams struct nvdimm_drvdata *ndd) 664a826c83SDan Williams { 674a826c83SDan Williams return to_namespace_index(ndd, ndd->ns_next); 684a826c83SDan Williams } 694a826c83SDan Williams 704a826c83SDan Williams #define nd_dbg_dpa(r, d, res, fmt, arg...) \ 714a826c83SDan Williams dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \ 724a826c83SDan Williams (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \ 734a826c83SDan Williams (unsigned long long) (res ? resource_size(res) : 0), \ 744a826c83SDan Williams (unsigned long long) (res ? res->start : 0), ##arg) 754a826c83SDan Williams 76bf9bccc1SDan Williams #define for_each_label(l, label, labels) \ 77bf9bccc1SDan Williams for (l = 0; (label = labels ? labels[l] : NULL); l++) 78bf9bccc1SDan Williams 79bf9bccc1SDan Williams #define for_each_dpa_resource(ndd, res) \ 80bf9bccc1SDan Williams for (res = (ndd)->dpa.child; res; res = res->sibling) 81bf9bccc1SDan Williams 824a826c83SDan Williams #define for_each_dpa_resource_safe(ndd, res, next) \ 834a826c83SDan Williams for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \ 844a826c83SDan Williams res; res = next, next = next ? next->sibling : NULL) 854a826c83SDan Williams 865212e11fSVishal Verma struct nd_percpu_lane { 875212e11fSVishal Verma int count; 885212e11fSVishal Verma spinlock_t lock; 895212e11fSVishal Verma }; 905212e11fSVishal Verma 911f7df6f8SDan Williams struct nd_region { 921f7df6f8SDan Williams struct device dev; 931b40e09aSDan Williams struct ida ns_ida; 948c2f7e86SDan Williams struct ida btt_ida; 95bf9bccc1SDan Williams struct device *ns_seed; 968c2f7e86SDan Williams struct device *btt_seed; 971f7df6f8SDan Williams u16 ndr_mappings; 981f7df6f8SDan Williams u64 ndr_size; 991f7df6f8SDan Williams u64 ndr_start; 1005212e11fSVishal Verma int id, num_lanes; 1011f7df6f8SDan Williams void *provider_data; 102eaf96153SDan Williams struct nd_interleave_set *nd_set; 1035212e11fSVishal Verma struct nd_percpu_lane __percpu *lane; 1041f7df6f8SDan Williams struct nd_mapping mapping[0]; 1051f7df6f8SDan Williams }; 1061f7df6f8SDan Williams 107047fc8a1SRoss Zwisler struct nd_blk_region { 108047fc8a1SRoss Zwisler int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev); 109047fc8a1SRoss Zwisler void (*disable)(struct nvdimm_bus *nvdimm_bus, struct device *dev); 110047fc8a1SRoss Zwisler int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa, 111047fc8a1SRoss Zwisler void *iobuf, u64 len, int rw); 112047fc8a1SRoss Zwisler void *blk_provider_data; 113047fc8a1SRoss Zwisler struct nd_region nd_region; 114047fc8a1SRoss Zwisler }; 115047fc8a1SRoss Zwisler 1164a826c83SDan Williams /* 1174a826c83SDan Williams * Lookup next in the repeating sequence of 01, 10, and 11. 1184a826c83SDan Williams */ 1194a826c83SDan Williams static inline unsigned nd_inc_seq(unsigned seq) 1204a826c83SDan Williams { 1214a826c83SDan Williams static const unsigned next[] = { 0, 2, 3, 1 }; 1224a826c83SDan Williams 1234a826c83SDan Williams return next[seq & 3]; 1244a826c83SDan Williams } 125f524bf27SDan Williams 1265212e11fSVishal Verma struct btt; 1278c2f7e86SDan Williams struct nd_btt { 1288c2f7e86SDan Williams struct device dev; 1298c2f7e86SDan Williams struct nd_namespace_common *ndns; 1305212e11fSVishal Verma struct btt *btt; 1318c2f7e86SDan Williams unsigned long lbasize; 1328c2f7e86SDan Williams u8 *uuid; 1338c2f7e86SDan Williams int id; 1348c2f7e86SDan Williams }; 1358c2f7e86SDan Williams 1364d88a97aSDan Williams enum nd_async_mode { 1374d88a97aSDan Williams ND_SYNC, 1384d88a97aSDan Williams ND_ASYNC, 1394d88a97aSDan Williams }; 1404d88a97aSDan Williams 14141cd8b70SVishal Verma int nd_integrity_init(struct gendisk *disk, unsigned long meta_size); 142bf9bccc1SDan Williams void wait_nvdimm_bus_probe_idle(struct device *dev); 1434d88a97aSDan Williams void nd_device_register(struct device *dev); 1444d88a97aSDan Williams void nd_device_unregister(struct device *dev, enum nd_async_mode mode); 145bf9bccc1SDan Williams int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf, 146bf9bccc1SDan Williams size_t len); 1471b40e09aSDan Williams ssize_t nd_sector_size_show(unsigned long current_lbasize, 1481b40e09aSDan Williams const unsigned long *supported, char *buf); 1491b40e09aSDan Williams ssize_t nd_sector_size_store(struct device *dev, const char *buf, 1501b40e09aSDan Williams unsigned long *current_lbasize, const unsigned long *supported); 1514d88a97aSDan Williams int __init nvdimm_init(void); 1523d88002eSDan Williams int __init nd_region_init(void); 1534d88a97aSDan Williams void nvdimm_exit(void); 1543d88002eSDan Williams void nd_region_exit(void); 155bf9bccc1SDan Williams struct nvdimm; 156bf9bccc1SDan Williams struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping); 1574d88a97aSDan Williams int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd); 1584d88a97aSDan Williams int nvdimm_init_config_data(struct nvdimm_drvdata *ndd); 159f524bf27SDan Williams int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset, 160f524bf27SDan Williams void *buf, size_t len); 1618c2f7e86SDan Williams struct nd_btt *to_nd_btt(struct device *dev); 1628c2f7e86SDan Williams struct btt_sb; 1638c2f7e86SDan Williams u64 nd_btt_sb_checksum(struct btt_sb *btt_sb); 1648c2f7e86SDan Williams #if IS_ENABLED(CONFIG_BTT) 1658c2f7e86SDan Williams int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata); 1668c2f7e86SDan Williams bool is_nd_btt(struct device *dev); 1678c2f7e86SDan Williams struct device *nd_btt_create(struct nd_region *nd_region); 1688c2f7e86SDan Williams #else 1698c2f7e86SDan Williams static inline nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata) 1708c2f7e86SDan Williams { 1718c2f7e86SDan Williams return -ENODEV; 1728c2f7e86SDan Williams } 1738c2f7e86SDan Williams 1748c2f7e86SDan Williams static inline bool is_nd_btt(struct device *dev) 1758c2f7e86SDan Williams { 1768c2f7e86SDan Williams return false; 1778c2f7e86SDan Williams } 1788c2f7e86SDan Williams 1798c2f7e86SDan Williams static inline struct device *nd_btt_create(struct nd_region *nd_region) 1808c2f7e86SDan Williams { 1818c2f7e86SDan Williams return NULL; 1828c2f7e86SDan Williams } 1838c2f7e86SDan Williams 1848c2f7e86SDan Williams #endif 1853d88002eSDan Williams struct nd_region *to_nd_region(struct device *dev); 1863d88002eSDan Williams int nd_region_to_nstype(struct nd_region *nd_region); 1873d88002eSDan Williams int nd_region_register_namespaces(struct nd_region *nd_region, int *err); 188bf9bccc1SDan Williams u64 nd_region_interleave_set_cookie(struct nd_region *nd_region); 1893d88002eSDan Williams void nvdimm_bus_lock(struct device *dev); 1903d88002eSDan Williams void nvdimm_bus_unlock(struct device *dev); 1913d88002eSDan Williams bool is_nvdimm_bus_locked(struct device *dev); 192bf9bccc1SDan Williams void nvdimm_drvdata_release(struct kref *kref); 193bf9bccc1SDan Williams void put_ndd(struct nvdimm_drvdata *ndd); 1944a826c83SDan Williams int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd); 1954a826c83SDan Williams void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res); 1964a826c83SDan Williams struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd, 1974a826c83SDan Williams struct nd_label_id *label_id, resource_size_t start, 1984a826c83SDan Williams resource_size_t n); 1998c2f7e86SDan Williams resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns); 2008c2f7e86SDan Williams struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev); 2015212e11fSVishal Verma int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns); 2025212e11fSVishal Verma int nvdimm_namespace_detach_btt(struct nd_namespace_common *ndns); 2035212e11fSVishal Verma const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns, 2045212e11fSVishal Verma char *name); 205047fc8a1SRoss Zwisler int nd_blk_region_init(struct nd_region *nd_region); 206*f0dc089cSDan Williams void __nd_iostat_start(struct bio *bio, unsigned long *start); 207*f0dc089cSDan Williams static inline bool nd_iostat_start(struct bio *bio, unsigned long *start) 208*f0dc089cSDan Williams { 209*f0dc089cSDan Williams struct gendisk *disk = bio->bi_bdev->bd_disk; 210*f0dc089cSDan Williams 211*f0dc089cSDan Williams if (!blk_queue_io_stat(disk->queue)) 212*f0dc089cSDan Williams return false; 213*f0dc089cSDan Williams 214*f0dc089cSDan Williams __nd_iostat_start(bio, start); 215*f0dc089cSDan Williams return true; 216*f0dc089cSDan Williams } 217*f0dc089cSDan Williams void nd_iostat_end(struct bio *bio, unsigned long start); 218047fc8a1SRoss Zwisler resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk); 2194d88a97aSDan Williams #endif /* __ND_H__ */ 220