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> 164d88a97aSDan Williams #include <linux/device.h> 174d88a97aSDan Williams #include <linux/mutex.h> 184d88a97aSDan Williams #include <linux/ndctl.h> 19bf9bccc1SDan Williams #include <linux/types.h> 204a826c83SDan Williams #include "label.h" 214d88a97aSDan Williams 224d88a97aSDan Williams struct nvdimm_drvdata { 234d88a97aSDan Williams struct device *dev; 244a826c83SDan Williams int nsindex_size; 254d88a97aSDan Williams struct nd_cmd_get_config_size nsarea; 264d88a97aSDan Williams void *data; 274a826c83SDan Williams int ns_current, ns_next; 284a826c83SDan Williams struct resource dpa; 29bf9bccc1SDan Williams struct kref kref; 304d88a97aSDan Williams }; 314d88a97aSDan Williams 323d88002eSDan Williams struct nd_region_namespaces { 333d88002eSDan Williams int count; 343d88002eSDan Williams int active; 353d88002eSDan Williams }; 363d88002eSDan Williams 374a826c83SDan Williams static inline struct nd_namespace_index *to_namespace_index( 384a826c83SDan Williams struct nvdimm_drvdata *ndd, int i) 394a826c83SDan Williams { 404a826c83SDan Williams if (i < 0) 414a826c83SDan Williams return NULL; 424a826c83SDan Williams 434a826c83SDan Williams return ndd->data + sizeof_namespace_index(ndd) * i; 444a826c83SDan Williams } 454a826c83SDan Williams 464a826c83SDan Williams static inline struct nd_namespace_index *to_current_namespace_index( 474a826c83SDan Williams struct nvdimm_drvdata *ndd) 484a826c83SDan Williams { 494a826c83SDan Williams return to_namespace_index(ndd, ndd->ns_current); 504a826c83SDan Williams } 514a826c83SDan Williams 524a826c83SDan Williams static inline struct nd_namespace_index *to_next_namespace_index( 534a826c83SDan Williams struct nvdimm_drvdata *ndd) 544a826c83SDan Williams { 554a826c83SDan Williams return to_namespace_index(ndd, ndd->ns_next); 564a826c83SDan Williams } 574a826c83SDan Williams 584a826c83SDan Williams #define nd_dbg_dpa(r, d, res, fmt, arg...) \ 594a826c83SDan Williams dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \ 604a826c83SDan Williams (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \ 614a826c83SDan Williams (unsigned long long) (res ? resource_size(res) : 0), \ 624a826c83SDan Williams (unsigned long long) (res ? res->start : 0), ##arg) 634a826c83SDan Williams 64bf9bccc1SDan Williams #define for_each_label(l, label, labels) \ 65bf9bccc1SDan Williams for (l = 0; (label = labels ? labels[l] : NULL); l++) 66bf9bccc1SDan Williams 67bf9bccc1SDan Williams #define for_each_dpa_resource(ndd, res) \ 68bf9bccc1SDan Williams for (res = (ndd)->dpa.child; res; res = res->sibling) 69bf9bccc1SDan Williams 704a826c83SDan Williams #define for_each_dpa_resource_safe(ndd, res, next) \ 714a826c83SDan Williams for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \ 724a826c83SDan Williams res; res = next, next = next ? next->sibling : NULL) 734a826c83SDan Williams 741f7df6f8SDan Williams struct nd_region { 751f7df6f8SDan Williams struct device dev; 76*1b40e09aSDan Williams struct ida ns_ida; 77bf9bccc1SDan Williams struct device *ns_seed; 781f7df6f8SDan Williams u16 ndr_mappings; 791f7df6f8SDan Williams u64 ndr_size; 801f7df6f8SDan Williams u64 ndr_start; 811f7df6f8SDan Williams int id; 821f7df6f8SDan Williams void *provider_data; 83eaf96153SDan Williams struct nd_interleave_set *nd_set; 841f7df6f8SDan Williams struct nd_mapping mapping[0]; 851f7df6f8SDan Williams }; 861f7df6f8SDan Williams 874a826c83SDan Williams /* 884a826c83SDan Williams * Lookup next in the repeating sequence of 01, 10, and 11. 894a826c83SDan Williams */ 904a826c83SDan Williams static inline unsigned nd_inc_seq(unsigned seq) 914a826c83SDan Williams { 924a826c83SDan Williams static const unsigned next[] = { 0, 2, 3, 1 }; 934a826c83SDan Williams 944a826c83SDan Williams return next[seq & 3]; 954a826c83SDan Williams } 964d88a97aSDan Williams enum nd_async_mode { 974d88a97aSDan Williams ND_SYNC, 984d88a97aSDan Williams ND_ASYNC, 994d88a97aSDan Williams }; 1004d88a97aSDan Williams 101bf9bccc1SDan Williams void wait_nvdimm_bus_probe_idle(struct device *dev); 1024d88a97aSDan Williams void nd_device_register(struct device *dev); 1034d88a97aSDan Williams void nd_device_unregister(struct device *dev, enum nd_async_mode mode); 104bf9bccc1SDan Williams int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf, 105bf9bccc1SDan Williams size_t len); 106*1b40e09aSDan Williams ssize_t nd_sector_size_show(unsigned long current_lbasize, 107*1b40e09aSDan Williams const unsigned long *supported, char *buf); 108*1b40e09aSDan Williams ssize_t nd_sector_size_store(struct device *dev, const char *buf, 109*1b40e09aSDan Williams unsigned long *current_lbasize, const unsigned long *supported); 1104d88a97aSDan Williams int __init nvdimm_init(void); 1113d88002eSDan Williams int __init nd_region_init(void); 1124d88a97aSDan Williams void nvdimm_exit(void); 1133d88002eSDan Williams void nd_region_exit(void); 114bf9bccc1SDan Williams struct nvdimm; 115bf9bccc1SDan Williams struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping); 1164d88a97aSDan Williams int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd); 1174d88a97aSDan Williams int nvdimm_init_config_data(struct nvdimm_drvdata *ndd); 1183d88002eSDan Williams struct nd_region *to_nd_region(struct device *dev); 1193d88002eSDan Williams int nd_region_to_nstype(struct nd_region *nd_region); 1203d88002eSDan Williams int nd_region_register_namespaces(struct nd_region *nd_region, int *err); 121bf9bccc1SDan Williams u64 nd_region_interleave_set_cookie(struct nd_region *nd_region); 1223d88002eSDan Williams void nvdimm_bus_lock(struct device *dev); 1233d88002eSDan Williams void nvdimm_bus_unlock(struct device *dev); 1243d88002eSDan Williams bool is_nvdimm_bus_locked(struct device *dev); 125bf9bccc1SDan Williams void nvdimm_drvdata_release(struct kref *kref); 126bf9bccc1SDan Williams void put_ndd(struct nvdimm_drvdata *ndd); 1274a826c83SDan Williams int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd); 1284a826c83SDan Williams void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res); 1294a826c83SDan Williams struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd, 1304a826c83SDan Williams struct nd_label_id *label_id, resource_size_t start, 1314a826c83SDan Williams resource_size_t n); 1324d88a97aSDan Williams #endif /* __ND_H__ */ 133