15b497af4SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24d88a97aSDan Williams /*
34d88a97aSDan Williams * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
44d88a97aSDan Williams */
54d88a97aSDan Williams #ifndef __ND_H__
64d88a97aSDan Williams #define __ND_H__
71f7df6f8SDan Williams #include <linux/libnvdimm.h>
8200c79daSDan Williams #include <linux/badblocks.h>
9f0dc089cSDan Williams #include <linux/blkdev.h>
104d88a97aSDan Williams #include <linux/device.h>
114d88a97aSDan Williams #include <linux/mutex.h>
124d88a97aSDan Williams #include <linux/ndctl.h>
13bf9bccc1SDan Williams #include <linux/types.h>
1471999466SDan Williams #include <linux/nd.h>
154a826c83SDan Williams #include "label.h"
164d88a97aSDan Williams
178c2f7e86SDan Williams enum {
185212e11fSVishal Verma /*
195212e11fSVishal Verma * Limits the maximum number of block apertures a dimm can
205212e11fSVishal Verma * support and is an input to the geometry/on-disk-format of a
215212e11fSVishal Verma * BTT instance
225212e11fSVishal Verma */
235212e11fSVishal Verma ND_MAX_LANES = 256,
24fcae6957SVishal Verma INT_LBASIZE_ALIGNMENT = 64,
253ae3d67bSVishal Verma NVDIMM_IO_ATOMIC = 1,
268c2f7e86SDan Williams };
278c2f7e86SDan Williams
284d88a97aSDan Williams struct nvdimm_drvdata {
294d88a97aSDan Williams struct device *dev;
3002881768SDan Williams int nslabel_size;
314d88a97aSDan Williams struct nd_cmd_get_config_size nsarea;
324d88a97aSDan Williams void *data;
335af96835SDan Williams bool cxl;
344a826c83SDan Williams int ns_current, ns_next;
354a826c83SDan Williams struct resource dpa;
36bf9bccc1SDan Williams struct kref kref;
374d88a97aSDan Williams };
384d88a97aSDan Williams
nsl_ref_name(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)39b4366a82SDan Williams static inline const u8 *nsl_ref_name(struct nvdimm_drvdata *ndd,
40b4366a82SDan Williams struct nd_namespace_label *nd_label)
41b4366a82SDan Williams {
425af96835SDan Williams if (ndd->cxl)
435af96835SDan Williams return nd_label->cxl.name;
445af96835SDan Williams return nd_label->efi.name;
45b4366a82SDan Williams }
46b4366a82SDan Williams
nsl_get_name(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u8 * name)47b4366a82SDan Williams static inline u8 *nsl_get_name(struct nvdimm_drvdata *ndd,
48b4366a82SDan Williams struct nd_namespace_label *nd_label, u8 *name)
49b4366a82SDan Williams {
505af96835SDan Williams if (ndd->cxl)
515af96835SDan Williams return memcpy(name, nd_label->cxl.name, NSLABEL_NAME_LEN);
525af96835SDan Williams return memcpy(name, nd_label->efi.name, NSLABEL_NAME_LEN);
53b4366a82SDan Williams }
54b4366a82SDan Williams
nsl_set_name(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u8 * name)558176f147SDan Williams static inline u8 *nsl_set_name(struct nvdimm_drvdata *ndd,
568176f147SDan Williams struct nd_namespace_label *nd_label, u8 *name)
578176f147SDan Williams {
588176f147SDan Williams if (!name)
598176f147SDan Williams return NULL;
605af96835SDan Williams if (ndd->cxl)
615af96835SDan Williams return memcpy(nd_label->cxl.name, name, NSLABEL_NAME_LEN);
625af96835SDan Williams return memcpy(nd_label->efi.name, name, NSLABEL_NAME_LEN);
638176f147SDan Williams }
648176f147SDan Williams
nsl_get_slot(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)65b4366a82SDan Williams static inline u32 nsl_get_slot(struct nvdimm_drvdata *ndd,
66b4366a82SDan Williams struct nd_namespace_label *nd_label)
67b4366a82SDan Williams {
685af96835SDan Williams if (ndd->cxl)
695af96835SDan Williams return __le32_to_cpu(nd_label->cxl.slot);
705af96835SDan Williams return __le32_to_cpu(nd_label->efi.slot);
71b4366a82SDan Williams }
72b4366a82SDan Williams
nsl_set_slot(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u32 slot)738176f147SDan Williams static inline void nsl_set_slot(struct nvdimm_drvdata *ndd,
748176f147SDan Williams struct nd_namespace_label *nd_label, u32 slot)
758176f147SDan Williams {
765af96835SDan Williams if (ndd->cxl)
775af96835SDan Williams nd_label->cxl.slot = __cpu_to_le32(slot);
785af96835SDan Williams else
795af96835SDan Williams nd_label->efi.slot = __cpu_to_le32(slot);
808176f147SDan Williams }
818176f147SDan Williams
nsl_get_checksum(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)82b4366a82SDan Williams static inline u64 nsl_get_checksum(struct nvdimm_drvdata *ndd,
83b4366a82SDan Williams struct nd_namespace_label *nd_label)
84b4366a82SDan Williams {
855af96835SDan Williams if (ndd->cxl)
865af96835SDan Williams return __le64_to_cpu(nd_label->cxl.checksum);
875af96835SDan Williams return __le64_to_cpu(nd_label->efi.checksum);
88b4366a82SDan Williams }
89b4366a82SDan Williams
nsl_set_checksum(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u64 checksum)908176f147SDan Williams static inline void nsl_set_checksum(struct nvdimm_drvdata *ndd,
918176f147SDan Williams struct nd_namespace_label *nd_label,
928176f147SDan Williams u64 checksum)
938176f147SDan Williams {
945af96835SDan Williams if (ndd->cxl)
955af96835SDan Williams nd_label->cxl.checksum = __cpu_to_le64(checksum);
965af96835SDan Williams else
975af96835SDan Williams nd_label->efi.checksum = __cpu_to_le64(checksum);
988176f147SDan Williams }
998176f147SDan Williams
nsl_get_flags(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)100b4366a82SDan Williams static inline u32 nsl_get_flags(struct nvdimm_drvdata *ndd,
101b4366a82SDan Williams struct nd_namespace_label *nd_label)
102b4366a82SDan Williams {
1035af96835SDan Williams if (ndd->cxl)
1045af96835SDan Williams return __le32_to_cpu(nd_label->cxl.flags);
1055af96835SDan Williams return __le32_to_cpu(nd_label->efi.flags);
106b4366a82SDan Williams }
107b4366a82SDan Williams
nsl_set_flags(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u32 flags)1088176f147SDan Williams static inline void nsl_set_flags(struct nvdimm_drvdata *ndd,
1098176f147SDan Williams struct nd_namespace_label *nd_label, u32 flags)
1108176f147SDan Williams {
1115af96835SDan Williams if (ndd->cxl)
1125af96835SDan Williams nd_label->cxl.flags = __cpu_to_le32(flags);
1135af96835SDan Williams else
1145af96835SDan Williams nd_label->efi.flags = __cpu_to_le32(flags);
1158176f147SDan Williams }
1168176f147SDan Williams
nsl_get_dpa(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)117b4366a82SDan Williams static inline u64 nsl_get_dpa(struct nvdimm_drvdata *ndd,
118b4366a82SDan Williams struct nd_namespace_label *nd_label)
119b4366a82SDan Williams {
1205af96835SDan Williams if (ndd->cxl)
1215af96835SDan Williams return __le64_to_cpu(nd_label->cxl.dpa);
1225af96835SDan Williams return __le64_to_cpu(nd_label->efi.dpa);
123b4366a82SDan Williams }
124b4366a82SDan Williams
nsl_set_dpa(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u64 dpa)1258176f147SDan Williams static inline void nsl_set_dpa(struct nvdimm_drvdata *ndd,
1268176f147SDan Williams struct nd_namespace_label *nd_label, u64 dpa)
1278176f147SDan Williams {
1285af96835SDan Williams if (ndd->cxl)
1295af96835SDan Williams nd_label->cxl.dpa = __cpu_to_le64(dpa);
1305af96835SDan Williams else
1315af96835SDan Williams nd_label->efi.dpa = __cpu_to_le64(dpa);
1328176f147SDan Williams }
1338176f147SDan Williams
nsl_get_rawsize(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)134b4366a82SDan Williams static inline u64 nsl_get_rawsize(struct nvdimm_drvdata *ndd,
135b4366a82SDan Williams struct nd_namespace_label *nd_label)
136b4366a82SDan Williams {
1375af96835SDan Williams if (ndd->cxl)
1385af96835SDan Williams return __le64_to_cpu(nd_label->cxl.rawsize);
1395af96835SDan Williams return __le64_to_cpu(nd_label->efi.rawsize);
140b4366a82SDan Williams }
141b4366a82SDan Williams
nsl_set_rawsize(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u64 rawsize)1428176f147SDan Williams static inline void nsl_set_rawsize(struct nvdimm_drvdata *ndd,
1438176f147SDan Williams struct nd_namespace_label *nd_label,
1448176f147SDan Williams u64 rawsize)
1458176f147SDan Williams {
1465af96835SDan Williams if (ndd->cxl)
1475af96835SDan Williams nd_label->cxl.rawsize = __cpu_to_le64(rawsize);
1485af96835SDan Williams else
1495af96835SDan Williams nd_label->efi.rawsize = __cpu_to_le64(rawsize);
1508176f147SDan Williams }
1518176f147SDan Williams
nsl_get_isetcookie(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)152b4366a82SDan Williams static inline u64 nsl_get_isetcookie(struct nvdimm_drvdata *ndd,
153b4366a82SDan Williams struct nd_namespace_label *nd_label)
154b4366a82SDan Williams {
1555af96835SDan Williams /* WARN future refactor attempts that break this assumption */
1565af96835SDan Williams if (dev_WARN_ONCE(ndd->dev, ndd->cxl,
1575af96835SDan Williams "CXL labels do not use the isetcookie concept\n"))
1585af96835SDan Williams return 0;
1595af96835SDan Williams return __le64_to_cpu(nd_label->efi.isetcookie);
160b4366a82SDan Williams }
161b4366a82SDan Williams
nsl_set_isetcookie(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u64 isetcookie)1628176f147SDan Williams static inline void nsl_set_isetcookie(struct nvdimm_drvdata *ndd,
1638176f147SDan Williams struct nd_namespace_label *nd_label,
1648176f147SDan Williams u64 isetcookie)
1658176f147SDan Williams {
1665af96835SDan Williams if (!ndd->cxl)
1675af96835SDan Williams nd_label->efi.isetcookie = __cpu_to_le64(isetcookie);
1688176f147SDan Williams }
1698176f147SDan Williams
nsl_validate_isetcookie(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u64 cookie)1709761b02dSDan Williams static inline bool nsl_validate_isetcookie(struct nvdimm_drvdata *ndd,
1719761b02dSDan Williams struct nd_namespace_label *nd_label,
1729761b02dSDan Williams u64 cookie)
1739761b02dSDan Williams {
1745af96835SDan Williams /*
1755af96835SDan Williams * Let the EFI and CXL validation comingle, where fields that
1765af96835SDan Williams * don't matter to CXL always validate.
1775af96835SDan Williams */
1785af96835SDan Williams if (ndd->cxl)
1795af96835SDan Williams return true;
1805af96835SDan Williams return cookie == __le64_to_cpu(nd_label->efi.isetcookie);
1819761b02dSDan Williams }
1829761b02dSDan Williams
nsl_get_position(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)183b4366a82SDan Williams static inline u16 nsl_get_position(struct nvdimm_drvdata *ndd,
184b4366a82SDan Williams struct nd_namespace_label *nd_label)
185b4366a82SDan Williams {
1865af96835SDan Williams if (ndd->cxl)
1875af96835SDan Williams return __le16_to_cpu(nd_label->cxl.position);
1885af96835SDan Williams return __le16_to_cpu(nd_label->efi.position);
189b4366a82SDan Williams }
190b4366a82SDan Williams
nsl_set_position(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u16 position)1918176f147SDan Williams static inline void nsl_set_position(struct nvdimm_drvdata *ndd,
1928176f147SDan Williams struct nd_namespace_label *nd_label,
1938176f147SDan Williams u16 position)
1948176f147SDan Williams {
1955af96835SDan Williams if (ndd->cxl)
1965af96835SDan Williams nd_label->cxl.position = __cpu_to_le16(position);
1975af96835SDan Williams else
1985af96835SDan Williams nd_label->efi.position = __cpu_to_le16(position);
1998176f147SDan Williams }
2008176f147SDan Williams
nsl_get_nlabel(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)201b4366a82SDan Williams static inline u16 nsl_get_nlabel(struct nvdimm_drvdata *ndd,
202b4366a82SDan Williams struct nd_namespace_label *nd_label)
203b4366a82SDan Williams {
2045af96835SDan Williams if (ndd->cxl)
2055af96835SDan Williams return 0;
2065af96835SDan Williams return __le16_to_cpu(nd_label->efi.nlabel);
207b4366a82SDan Williams }
208b4366a82SDan Williams
nsl_set_nlabel(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u16 nlabel)2098176f147SDan Williams static inline void nsl_set_nlabel(struct nvdimm_drvdata *ndd,
2108176f147SDan Williams struct nd_namespace_label *nd_label,
2118176f147SDan Williams u16 nlabel)
2128176f147SDan Williams {
2135af96835SDan Williams if (!ndd->cxl)
2145af96835SDan Williams nd_label->efi.nlabel = __cpu_to_le16(nlabel);
2158176f147SDan Williams }
2168176f147SDan Williams
nsl_get_nrange(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)21742e192aaSDan Williams static inline u16 nsl_get_nrange(struct nvdimm_drvdata *ndd,
21842e192aaSDan Williams struct nd_namespace_label *nd_label)
21942e192aaSDan Williams {
2205af96835SDan Williams if (ndd->cxl)
2215af96835SDan Williams return __le16_to_cpu(nd_label->cxl.nrange);
22242e192aaSDan Williams return 1;
22342e192aaSDan Williams }
22442e192aaSDan Williams
nsl_set_nrange(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u16 nrange)22542e192aaSDan Williams static inline void nsl_set_nrange(struct nvdimm_drvdata *ndd,
22642e192aaSDan Williams struct nd_namespace_label *nd_label,
22742e192aaSDan Williams u16 nrange)
22842e192aaSDan Williams {
2295af96835SDan Williams if (ndd->cxl)
2305af96835SDan Williams nd_label->cxl.nrange = __cpu_to_le16(nrange);
23142e192aaSDan Williams }
23242e192aaSDan Williams
nsl_get_lbasize(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)233b4366a82SDan Williams static inline u64 nsl_get_lbasize(struct nvdimm_drvdata *ndd,
234b4366a82SDan Williams struct nd_namespace_label *nd_label)
235b4366a82SDan Williams {
2365af96835SDan Williams /*
2375af96835SDan Williams * Yes, for some reason the EFI labels convey a massive 64-bit
2385af96835SDan Williams * lbasize, that got fixed for CXL.
2395af96835SDan Williams */
2405af96835SDan Williams if (ndd->cxl)
2415af96835SDan Williams return __le16_to_cpu(nd_label->cxl.lbasize);
2425af96835SDan Williams return __le64_to_cpu(nd_label->efi.lbasize);
243b4366a82SDan Williams }
244b4366a82SDan Williams
nsl_set_lbasize(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u64 lbasize)2458176f147SDan Williams static inline void nsl_set_lbasize(struct nvdimm_drvdata *ndd,
2468176f147SDan Williams struct nd_namespace_label *nd_label,
2478176f147SDan Williams u64 lbasize)
2488176f147SDan Williams {
2495af96835SDan Williams if (ndd->cxl)
2505af96835SDan Williams nd_label->cxl.lbasize = __cpu_to_le16(lbasize);
2515af96835SDan Williams else
2525af96835SDan Williams nd_label->efi.lbasize = __cpu_to_le64(lbasize);
2538176f147SDan Williams }
2548176f147SDan Williams
nsl_get_uuid(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,uuid_t * uuid)255d1c6e08eSDan Williams static inline const uuid_t *nsl_get_uuid(struct nvdimm_drvdata *ndd,
256d1c6e08eSDan Williams struct nd_namespace_label *nd_label,
257d1c6e08eSDan Williams uuid_t *uuid)
258d1c6e08eSDan Williams {
2595af96835SDan Williams if (ndd->cxl)
2605af96835SDan Williams import_uuid(uuid, nd_label->cxl.uuid);
2615af96835SDan Williams else
2625af96835SDan Williams import_uuid(uuid, nd_label->efi.uuid);
263d1c6e08eSDan Williams return uuid;
264d1c6e08eSDan Williams }
265d1c6e08eSDan Williams
nsl_set_uuid(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,const uuid_t * uuid)266d1c6e08eSDan Williams static inline const uuid_t *nsl_set_uuid(struct nvdimm_drvdata *ndd,
267d1c6e08eSDan Williams struct nd_namespace_label *nd_label,
268d1c6e08eSDan Williams const uuid_t *uuid)
269d1c6e08eSDan Williams {
2705af96835SDan Williams if (ndd->cxl)
2715af96835SDan Williams export_uuid(nd_label->cxl.uuid, uuid);
2725af96835SDan Williams else
2735af96835SDan Williams export_uuid(nd_label->efi.uuid, uuid);
274d1c6e08eSDan Williams return uuid;
275d1c6e08eSDan Williams }
276d1c6e08eSDan Williams
nsl_uuid_equal(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,const uuid_t * uuid)277d1c6e08eSDan Williams static inline bool nsl_uuid_equal(struct nvdimm_drvdata *ndd,
278d1c6e08eSDan Williams struct nd_namespace_label *nd_label,
279d1c6e08eSDan Williams const uuid_t *uuid)
280d1c6e08eSDan Williams {
281d1c6e08eSDan Williams uuid_t tmp;
282d1c6e08eSDan Williams
2835af96835SDan Williams if (ndd->cxl)
2845af96835SDan Williams import_uuid(&tmp, nd_label->cxl.uuid);
2855af96835SDan Williams else
2865af96835SDan Williams import_uuid(&tmp, nd_label->efi.uuid);
287d1c6e08eSDan Williams return uuid_equal(&tmp, uuid);
288d1c6e08eSDan Williams }
289d1c6e08eSDan Williams
nsl_uuid_raw(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)290d1c6e08eSDan Williams static inline const u8 *nsl_uuid_raw(struct nvdimm_drvdata *ndd,
291d1c6e08eSDan Williams struct nd_namespace_label *nd_label)
292d1c6e08eSDan Williams {
2935af96835SDan Williams if (ndd->cxl)
2945af96835SDan Williams return nd_label->cxl.uuid;
2955af96835SDan Williams return nd_label->efi.uuid;
296d1c6e08eSDan Williams }
297d1c6e08eSDan Williams
2988b03aa0eSDan Williams bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
2998b03aa0eSDan Williams struct nd_namespace_label *nd_label, guid_t *guid);
300a6e6d722SDan Williams enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
301a6e6d722SDan Williams struct nd_namespace_label *nd_label);
302f56541a7SDan Williams
303e5ae3b25SDan Williams struct nd_region_data {
304e5ae3b25SDan Williams int ns_count;
305e5ae3b25SDan Williams int ns_active;
306595c7307SDan Williams unsigned int hints_shift;
3079106137cSGustavo A. R. Silva void __iomem *flush_wpq[];
3083d88002eSDan Williams };
3093d88002eSDan Williams
ndrd_get_flush_wpq(struct nd_region_data * ndrd,int dimm,int hint)310595c7307SDan Williams static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
311595c7307SDan Williams int dimm, int hint)
312595c7307SDan Williams {
313595c7307SDan Williams unsigned int num = 1 << ndrd->hints_shift;
314595c7307SDan Williams unsigned int mask = num - 1;
315595c7307SDan Williams
316595c7307SDan Williams return ndrd->flush_wpq[dimm * num + (hint & mask)];
317595c7307SDan Williams }
318595c7307SDan Williams
ndrd_set_flush_wpq(struct nd_region_data * ndrd,int dimm,int hint,void __iomem * flush)319595c7307SDan Williams static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
320595c7307SDan Williams int hint, void __iomem *flush)
321595c7307SDan Williams {
322595c7307SDan Williams unsigned int num = 1 << ndrd->hints_shift;
323595c7307SDan Williams unsigned int mask = num - 1;
324595c7307SDan Williams
325595c7307SDan Williams ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
326595c7307SDan Williams }
327595c7307SDan Williams
to_namespace_index(struct nvdimm_drvdata * ndd,int i)3284a826c83SDan Williams static inline struct nd_namespace_index *to_namespace_index(
3294a826c83SDan Williams struct nvdimm_drvdata *ndd, int i)
3304a826c83SDan Williams {
3314a826c83SDan Williams if (i < 0)
3324a826c83SDan Williams return NULL;
3334a826c83SDan Williams
3344a826c83SDan Williams return ndd->data + sizeof_namespace_index(ndd) * i;
3354a826c83SDan Williams }
3364a826c83SDan Williams
to_current_namespace_index(struct nvdimm_drvdata * ndd)3374a826c83SDan Williams static inline struct nd_namespace_index *to_current_namespace_index(
3384a826c83SDan Williams struct nvdimm_drvdata *ndd)
3394a826c83SDan Williams {
3404a826c83SDan Williams return to_namespace_index(ndd, ndd->ns_current);
3414a826c83SDan Williams }
3424a826c83SDan Williams
to_next_namespace_index(struct nvdimm_drvdata * ndd)3434a826c83SDan Williams static inline struct nd_namespace_index *to_next_namespace_index(
3444a826c83SDan Williams struct nvdimm_drvdata *ndd)
3454a826c83SDan Williams {
3464a826c83SDan Williams return to_namespace_index(ndd, ndd->ns_next);
3474a826c83SDan Williams }
3484a826c83SDan Williams
349564e871aSDan Williams unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
350564e871aSDan Williams
3515af96835SDan Williams #define efi_namespace_label_has(ndd, field) \
3525af96835SDan Williams (!ndd->cxl && offsetof(struct nvdimm_efi_label, field) \
353564e871aSDan Williams < sizeof_namespace_label(ndd))
354564e871aSDan Williams
3554a826c83SDan Williams #define nd_dbg_dpa(r, d, res, fmt, arg...) \
3564a826c83SDan Williams dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
3574a826c83SDan Williams (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
3584a826c83SDan Williams (unsigned long long) (res ? resource_size(res) : 0), \
3594a826c83SDan Williams (unsigned long long) (res ? res->start : 0), ##arg)
3604a826c83SDan Williams
361bf9bccc1SDan Williams #define for_each_dpa_resource(ndd, res) \
362bf9bccc1SDan Williams for (res = (ndd)->dpa.child; res; res = res->sibling)
363bf9bccc1SDan Williams
3644a826c83SDan Williams #define for_each_dpa_resource_safe(ndd, res, next) \
3654a826c83SDan Williams for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
3664a826c83SDan Williams res; res = next, next = next ? next->sibling : NULL)
3674a826c83SDan Williams
3685212e11fSVishal Verma struct nd_percpu_lane {
3695212e11fSVishal Verma int count;
3705212e11fSVishal Verma spinlock_t lock;
3715212e11fSVishal Verma };
3725212e11fSVishal Verma
373c4703ce1SDan Williams enum nd_label_flags {
374c4703ce1SDan Williams ND_LABEL_REAP,
375c4703ce1SDan Williams };
376ae8219f1SDan Williams struct nd_label_ent {
377ae8219f1SDan Williams struct list_head list;
378c4703ce1SDan Williams unsigned long flags;
379ae8219f1SDan Williams struct nd_namespace_label *label;
380ae8219f1SDan Williams };
381ae8219f1SDan Williams
382ae8219f1SDan Williams enum nd_mapping_lock_class {
383ae8219f1SDan Williams ND_MAPPING_CLASS0,
384ae8219f1SDan Williams ND_MAPPING_UUID_SCAN,
385ae8219f1SDan Williams };
386ae8219f1SDan Williams
38744c462ebSDan Williams struct nd_mapping {
38844c462ebSDan Williams struct nvdimm *nvdimm;
38944c462ebSDan Williams u64 start;
39044c462ebSDan Williams u64 size;
391401c0a19SDan Williams int position;
392ae8219f1SDan Williams struct list_head labels;
393ae8219f1SDan Williams struct mutex lock;
39444c462ebSDan Williams /*
39544c462ebSDan Williams * @ndd is for private use at region enable / disable time for
39644c462ebSDan Williams * get_ndd() + put_ndd(), all other nd_mapping to ndd
39744c462ebSDan Williams * conversions use to_ndd() which respects enabled state of the
39844c462ebSDan Williams * nvdimm.
39944c462ebSDan Williams */
40044c462ebSDan Williams struct nvdimm_drvdata *ndd;
40144c462ebSDan Williams };
40244c462ebSDan Williams
4031f7df6f8SDan Williams struct nd_region {
4041f7df6f8SDan Williams struct device dev;
4051b40e09aSDan Williams struct ida ns_ida;
4068c2f7e86SDan Williams struct ida btt_ida;
407e1455744SDan Williams struct ida pfn_ida;
408cd03412aSDan Williams struct ida dax_ida;
409004f1afbSDan Williams unsigned long flags;
410bf9bccc1SDan Williams struct device *ns_seed;
4118c2f7e86SDan Williams struct device *btt_seed;
412e1455744SDan Williams struct device *pfn_seed;
413cd03412aSDan Williams struct device *dax_seed;
4142522afb8SDan Williams unsigned long align;
4151f7df6f8SDan Williams u16 ndr_mappings;
4161f7df6f8SDan Williams u64 ndr_size;
4171f7df6f8SDan Williams u64 ndr_start;
4188fc5c735SDan Williams int id, num_lanes, ro, numa_node, target_node;
4191f7df6f8SDan Williams void *provider_data;
420975750a9SToshi Kani struct kernfs_node *bb_state;
4216a6bef90SDave Jiang struct badblocks bb;
422eaf96153SDan Williams struct nd_interleave_set *nd_set;
4235212e11fSVishal Verma struct nd_percpu_lane __percpu *lane;
424c5d4355dSPankaj Gupta int (*flush)(struct nd_region *nd_region, struct bio *bio);
4259106137cSGustavo A. R. Silva struct nd_mapping mapping[];
4261f7df6f8SDan Williams };
4271f7df6f8SDan Williams
nsl_validate_nlabel(struct nd_region * nd_region,struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)4288172db92SDan Williams static inline bool nsl_validate_nlabel(struct nd_region *nd_region,
4298172db92SDan Williams struct nvdimm_drvdata *ndd,
4308172db92SDan Williams struct nd_namespace_label *nd_label)
4318172db92SDan Williams {
4325af96835SDan Williams if (ndd->cxl)
4335af96835SDan Williams return true;
4348172db92SDan Williams return nsl_get_nlabel(ndd, nd_label) == nd_region->ndr_mappings;
4358172db92SDan Williams }
4368172db92SDan Williams
4374a826c83SDan Williams /*
4384a826c83SDan Williams * Lookup next in the repeating sequence of 01, 10, and 11.
4394a826c83SDan Williams */
nd_inc_seq(unsigned seq)4404a826c83SDan Williams static inline unsigned nd_inc_seq(unsigned seq)
4414a826c83SDan Williams {
4424a826c83SDan Williams static const unsigned next[] = { 0, 2, 3, 1 };
4434a826c83SDan Williams
4444a826c83SDan Williams return next[seq & 3];
4454a826c83SDan Williams }
446f524bf27SDan Williams
4475212e11fSVishal Verma struct btt;
4488c2f7e86SDan Williams struct nd_btt {
4498c2f7e86SDan Williams struct device dev;
4508c2f7e86SDan Williams struct nd_namespace_common *ndns;
4515212e11fSVishal Verma struct btt *btt;
4528c2f7e86SDan Williams unsigned long lbasize;
453abe8b4e3SVishal Verma u64 size;
454d1c6e08eSDan Williams uuid_t *uuid;
4558c2f7e86SDan Williams int id;
45614e49454SVishal Verma int initial_offset;
45714e49454SVishal Verma u16 version_major;
45814e49454SVishal Verma u16 version_minor;
4598c2f7e86SDan Williams };
4608c2f7e86SDan Williams
461e1455744SDan Williams enum nd_pfn_mode {
462e1455744SDan Williams PFN_MODE_NONE,
463e1455744SDan Williams PFN_MODE_RAM,
464e1455744SDan Williams PFN_MODE_PMEM,
465e1455744SDan Williams };
466e1455744SDan Williams
467e1455744SDan Williams struct nd_pfn {
468e1455744SDan Williams int id;
469d1c6e08eSDan Williams uuid_t *uuid;
470e1455744SDan Williams struct device dev;
471315c5625SDan Williams unsigned long align;
472e1455744SDan Williams unsigned long npfns;
473e1455744SDan Williams enum nd_pfn_mode mode;
474e1455744SDan Williams struct nd_pfn_sb *pfn_sb;
475e1455744SDan Williams struct nd_namespace_common *ndns;
476e1455744SDan Williams };
477e1455744SDan Williams
478cd03412aSDan Williams struct nd_dax {
479cd03412aSDan Williams struct nd_pfn nd_pfn;
480cd03412aSDan Williams };
481cd03412aSDan Williams
nd_info_block_reserve(void)4828f4b01fcSAneesh Kumar K.V static inline u32 nd_info_block_reserve(void)
4838f4b01fcSAneesh Kumar K.V {
4848f4b01fcSAneesh Kumar K.V return ALIGN(SZ_8K, PAGE_SIZE);
4858f4b01fcSAneesh Kumar K.V }
4868f4b01fcSAneesh Kumar K.V
4874d88a97aSDan Williams enum nd_async_mode {
4884d88a97aSDan Williams ND_SYNC,
4894d88a97aSDan Williams ND_ASYNC,
4904d88a97aSDan Williams };
4914d88a97aSDan Williams
49241cd8b70SVishal Verma int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
493bf9bccc1SDan Williams void wait_nvdimm_bus_probe_idle(struct device *dev);
4944d88a97aSDan Williams void nd_device_register(struct device *dev);
4954d88a97aSDan Williams void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
49671999466SDan Williams void nd_device_notify(struct device *dev, enum nvdimm_event event);
497d1c6e08eSDan Williams int nd_uuid_store(struct device *dev, uuid_t **uuid_out, const char *buf,
498bf9bccc1SDan Williams size_t len);
499b2c48f9fSDan Williams ssize_t nd_size_select_show(unsigned long current_size,
5001b40e09aSDan Williams const unsigned long *supported, char *buf);
501b2c48f9fSDan Williams ssize_t nd_size_select_store(struct device *dev, const char *buf,
502b2c48f9fSDan Williams unsigned long *current_size, const unsigned long *supported);
5034d88a97aSDan Williams int __init nvdimm_init(void);
5043d88002eSDan Williams int __init nd_region_init(void);
505b3fde74eSDan Williams int __init nd_label_init(void);
5064d88a97aSDan Williams void nvdimm_exit(void);
5073d88002eSDan Williams void nd_region_exit(void);
508bf9bccc1SDan Williams struct nvdimm;
509adbb6829SDan Williams extern const struct attribute_group nd_device_attribute_group;
510e2f6a0e3SDan Williams extern const struct attribute_group nd_numa_attribute_group;
511e755799aSDan Williams extern const struct attribute_group *nvdimm_bus_attribute_groups[];
512bf9bccc1SDan Williams struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
513aee65987SToshi Kani int nvdimm_check_config_data(struct device *dev);
5144d88a97aSDan Williams int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
5154d88a97aSDan Williams int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
5162d657d17SAlexander Duyck int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
5172d657d17SAlexander Duyck size_t offset, size_t len);
518f524bf27SDan Williams int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
519f524bf27SDan Williams void *buf, size_t len);
52059e64739SDan Williams long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
52159e64739SDan Williams unsigned int len);
522a0e37452SDan Williams void nvdimm_set_labeling(struct device *dev);
5238f078b38SDan Williams void nvdimm_set_locked(struct device *dev);
524d34cb808SDan Williams void nvdimm_clear_locked(struct device *dev);
5251cd73865SDan Williams int nvdimm_security_setup_events(struct device *dev);
5264c6926a2SDave Jiang #if IS_ENABLED(CONFIG_NVDIMM_KEYS)
5274c6926a2SDave Jiang int nvdimm_security_unlock(struct device *dev);
5284c6926a2SDave Jiang #else
nvdimm_security_unlock(struct device * dev)5294c6926a2SDave Jiang static inline int nvdimm_security_unlock(struct device *dev)
5304c6926a2SDave Jiang {
5314c6926a2SDave Jiang return 0;
5324c6926a2SDave Jiang }
5334c6926a2SDave Jiang #endif
5348c2f7e86SDan Williams struct nd_btt *to_nd_btt(struct device *dev);
535e1455744SDan Williams
536e1455744SDan Williams struct nd_gen_sb {
537e1455744SDan Williams char reserved[SZ_4K - 8];
538e1455744SDan Williams __le64 checksum;
539e1455744SDan Williams };
540e1455744SDan Williams
541e1455744SDan Williams u64 nd_sb_checksum(struct nd_gen_sb *sb);
5428c2f7e86SDan Williams #if IS_ENABLED(CONFIG_BTT)
543200c79daSDan Williams int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
5448c2f7e86SDan Williams bool is_nd_btt(struct device *dev);
5458c2f7e86SDan Williams struct device *nd_btt_create(struct nd_region *nd_region);
5468c2f7e86SDan Williams #else
nd_btt_probe(struct device * dev,struct nd_namespace_common * ndns)547e32bc729SDan Williams static inline int nd_btt_probe(struct device *dev,
548200c79daSDan Williams struct nd_namespace_common *ndns)
5498c2f7e86SDan Williams {
5508c2f7e86SDan Williams return -ENODEV;
5518c2f7e86SDan Williams }
5528c2f7e86SDan Williams
is_nd_btt(struct device * dev)5538c2f7e86SDan Williams static inline bool is_nd_btt(struct device *dev)
5548c2f7e86SDan Williams {
5558c2f7e86SDan Williams return false;
5568c2f7e86SDan Williams }
5578c2f7e86SDan Williams
nd_btt_create(struct nd_region * nd_region)5588c2f7e86SDan Williams static inline struct device *nd_btt_create(struct nd_region *nd_region)
5598c2f7e86SDan Williams {
5608c2f7e86SDan Williams return NULL;
5618c2f7e86SDan Williams }
5628c2f7e86SDan Williams #endif
563e1455744SDan Williams
564e1455744SDan Williams struct nd_pfn *to_nd_pfn(struct device *dev);
565e1455744SDan Williams #if IS_ENABLED(CONFIG_NVDIMM_PFN)
5660dd69643SOliver O'Halloran
567f5376699SAneesh Kumar K.V #define MAX_NVDIMM_ALIGN 4
5680dd69643SOliver O'Halloran
569200c79daSDan Williams int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
570e1455744SDan Williams bool is_nd_pfn(struct device *dev);
571e1455744SDan Williams struct device *nd_pfn_create(struct nd_region *nd_region);
572cd03412aSDan Williams struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
573cd03412aSDan Williams struct nd_namespace_common *ndns);
574c5ed9268SDan Williams int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
57578c81cc8SDan Williams extern const struct attribute_group *nd_pfn_attribute_groups[];
576e1455744SDan Williams #else
nd_pfn_probe(struct device * dev,struct nd_namespace_common * ndns)577200c79daSDan Williams static inline int nd_pfn_probe(struct device *dev,
578200c79daSDan Williams struct nd_namespace_common *ndns)
579e1455744SDan Williams {
580e1455744SDan Williams return -ENODEV;
581e1455744SDan Williams }
582e1455744SDan Williams
is_nd_pfn(struct device * dev)583e1455744SDan Williams static inline bool is_nd_pfn(struct device *dev)
584e1455744SDan Williams {
585e1455744SDan Williams return false;
586e1455744SDan Williams }
587e1455744SDan Williams
nd_pfn_create(struct nd_region * nd_region)588e1455744SDan Williams static inline struct device *nd_pfn_create(struct nd_region *nd_region)
589e1455744SDan Williams {
590e1455744SDan Williams return NULL;
591e1455744SDan Williams }
59232ab0a3fSDan Williams
nd_pfn_validate(struct nd_pfn * nd_pfn,const char * sig)593c5ed9268SDan Williams static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
59432ab0a3fSDan Williams {
59532ab0a3fSDan Williams return -ENODEV;
59632ab0a3fSDan Williams }
597e1455744SDan Williams #endif
598e1455744SDan Williams
599cd03412aSDan Williams struct nd_dax *to_nd_dax(struct device *dev);
600cd03412aSDan Williams #if IS_ENABLED(CONFIG_NVDIMM_DAX)
601c5ed9268SDan Williams int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
6022a81ada3SGreg Kroah-Hartman bool is_nd_dax(const struct device *dev);
603cd03412aSDan Williams struct device *nd_dax_create(struct nd_region *nd_region);
nd_dax_devinit(struct nd_dax * nd_dax,struct nd_namespace_common * ndns)604*08ab71e0SYi Yang static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
605*08ab71e0SYi Yang struct nd_namespace_common *ndns)
606*08ab71e0SYi Yang {
607*08ab71e0SYi Yang if (!nd_dax)
608*08ab71e0SYi Yang return NULL;
609*08ab71e0SYi Yang return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
610*08ab71e0SYi Yang }
611cd03412aSDan Williams #else
nd_dax_probe(struct device * dev,struct nd_namespace_common * ndns)612c5ed9268SDan Williams static inline int nd_dax_probe(struct device *dev,
613c5ed9268SDan Williams struct nd_namespace_common *ndns)
614c5ed9268SDan Williams {
615c5ed9268SDan Williams return -ENODEV;
616c5ed9268SDan Williams }
617c5ed9268SDan Williams
is_nd_dax(const struct device * dev)6182a81ada3SGreg Kroah-Hartman static inline bool is_nd_dax(const struct device *dev)
619cd03412aSDan Williams {
620cd03412aSDan Williams return false;
621cd03412aSDan Williams }
622cd03412aSDan Williams
nd_dax_create(struct nd_region * nd_region)623cd03412aSDan Williams static inline struct device *nd_dax_create(struct nd_region *nd_region)
624cd03412aSDan Williams {
625cd03412aSDan Williams return NULL;
626cd03412aSDan Williams }
627cd03412aSDan Williams #endif
628cd03412aSDan Williams
6293d88002eSDan Williams int nd_region_to_nstype(struct nd_region *nd_region);
6303d88002eSDan Williams int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
631c12c48ceSDan Williams u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
632c12c48ceSDan Williams struct nd_namespace_index *nsindex);
63386ef58a4SDan Williams u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
6343d88002eSDan Williams void nvdimm_bus_lock(struct device *dev);
6353d88002eSDan Williams void nvdimm_bus_unlock(struct device *dev);
6363d88002eSDan Williams bool is_nvdimm_bus_locked(struct device *dev);
63732f61d67SChristoph Hellwig void nvdimm_check_and_set_ro(struct gendisk *disk);
638bf9bccc1SDan Williams void nvdimm_drvdata_release(struct kref *kref);
639bf9bccc1SDan Williams void put_ndd(struct nvdimm_drvdata *ndd);
6404a826c83SDan Williams int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
6414a826c83SDan Williams void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
6424a826c83SDan Williams struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
6434a826c83SDan Williams struct nd_label_id *label_id, resource_size_t start,
6444a826c83SDan Williams resource_size_t n);
6458c2f7e86SDan Williams resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
64608e6b3c6SDan Williams bool nvdimm_namespace_locked(struct nd_namespace_common *ndns);
6478c2f7e86SDan Williams struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
6485212e11fSVishal Verma int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
649298f2bc5SDan Williams int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
6505212e11fSVishal Verma const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
6515212e11fSVishal Verma char *name);
652f979b13cSDan Williams unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
653a4574f63SDan Williams struct range;
654a3901802SDan Williams void nvdimm_badblocks_populate(struct nd_region *nd_region,
655a4574f63SDan Williams struct badblocks *bb, const struct range *range);
6568f4b01fcSAneesh Kumar K.V int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
6578f4b01fcSAneesh Kumar K.V resource_size_t size);
6588f4b01fcSAneesh Kumar K.V void devm_namespace_disable(struct device *dev,
6598f4b01fcSAneesh Kumar K.V struct nd_namespace_common *ndns);
660200c79daSDan Williams #if IS_ENABLED(CONFIG_ND_CLAIM)
661e96f0bf2SAneesh Kumar K.V /* max struct page size independent of kernel config */
662c91d7136SDan Williams #define MAX_STRUCT_PAGE_SIZE 64
663e8d51348SChristoph Hellwig int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap);
664200c79daSDan Williams #else
nvdimm_setup_pfn(struct nd_pfn * nd_pfn,struct dev_pagemap * pgmap)665e8d51348SChristoph Hellwig static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
666e8d51348SChristoph Hellwig struct dev_pagemap *pgmap)
667ac515c08SDan Williams {
668e8d51348SChristoph Hellwig return -ENXIO;
669ac515c08SDan Williams }
670200c79daSDan Williams #endif
671e5ae3b25SDan Williams int nd_region_activate(struct nd_region *nd_region);
is_bad_pmem(struct badblocks * bb,sector_t sector,unsigned int len)672200c79daSDan Williams static inline bool is_bad_pmem(struct badblocks *bb, sector_t sector,
673200c79daSDan Williams unsigned int len)
674200c79daSDan Williams {
675200c79daSDan Williams if (bb->count) {
676200c79daSDan Williams sector_t first_bad;
677200c79daSDan Williams int num_bad;
678200c79daSDan Williams
679200c79daSDan Williams return !!badblocks_check(bb, sector, len / 512, &first_bad,
680200c79daSDan Williams &num_bad);
681200c79daSDan Williams }
682200c79daSDan Williams
683200c79daSDan Williams return false;
684200c79daSDan Williams }
685d1c6e08eSDan Williams const uuid_t *nd_dev_to_uuid(struct device *dev);
686004f1afbSDan Williams bool pmem_should_map_pages(struct device *dev);
6874d88a97aSDan Williams #endif /* __ND_H__ */
688