18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef __LABEL_H__
68c2ecf20Sopenharmony_ci#define __LABEL_H__
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/ndctl.h>
98c2ecf20Sopenharmony_ci#include <linux/sizes.h>
108c2ecf20Sopenharmony_ci#include <linux/uuid.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cienum {
148c2ecf20Sopenharmony_ci	NSINDEX_SIG_LEN = 16,
158c2ecf20Sopenharmony_ci	NSINDEX_ALIGN = 256,
168c2ecf20Sopenharmony_ci	NSINDEX_SEQ_MASK = 0x3,
178c2ecf20Sopenharmony_ci	NSLABEL_UUID_LEN = 16,
188c2ecf20Sopenharmony_ci	NSLABEL_NAME_LEN = 64,
198c2ecf20Sopenharmony_ci	NSLABEL_FLAG_ROLABEL = 0x1,  /* read-only label */
208c2ecf20Sopenharmony_ci	NSLABEL_FLAG_LOCAL = 0x2,    /* DIMM-local namespace */
218c2ecf20Sopenharmony_ci	NSLABEL_FLAG_BTT = 0x4,      /* namespace contains a BTT */
228c2ecf20Sopenharmony_ci	NSLABEL_FLAG_UPDATING = 0x8, /* label being updated */
238c2ecf20Sopenharmony_ci	BTT_ALIGN = 4096,            /* all btt structures */
248c2ecf20Sopenharmony_ci	BTTINFO_SIG_LEN = 16,
258c2ecf20Sopenharmony_ci	BTTINFO_UUID_LEN = 16,
268c2ecf20Sopenharmony_ci	BTTINFO_FLAG_ERROR = 0x1,    /* error state (read-only) */
278c2ecf20Sopenharmony_ci	BTTINFO_MAJOR_VERSION = 1,
288c2ecf20Sopenharmony_ci	ND_LABEL_MIN_SIZE = 256 * 4, /* see sizeof_namespace_index() */
298c2ecf20Sopenharmony_ci	ND_LABEL_ID_SIZE = 50,
308c2ecf20Sopenharmony_ci	ND_NSINDEX_INIT = 0x1,
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * struct nd_namespace_index - label set superblock
358c2ecf20Sopenharmony_ci * @sig: NAMESPACE_INDEX\0
368c2ecf20Sopenharmony_ci * @flags: placeholder
378c2ecf20Sopenharmony_ci * @seq: sequence number for this index
388c2ecf20Sopenharmony_ci * @myoff: offset of this index in label area
398c2ecf20Sopenharmony_ci * @mysize: size of this index struct
408c2ecf20Sopenharmony_ci * @otheroff: offset of other index
418c2ecf20Sopenharmony_ci * @labeloff: offset of first label slot
428c2ecf20Sopenharmony_ci * @nslot: total number of label slots
438c2ecf20Sopenharmony_ci * @major: label area major version
448c2ecf20Sopenharmony_ci * @minor: label area minor version
458c2ecf20Sopenharmony_ci * @checksum: fletcher64 of all fields
468c2ecf20Sopenharmony_ci * @free[0]: bitmap, nlabel bits
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * The size of free[] is rounded up so the total struct size is a
498c2ecf20Sopenharmony_ci * multiple of NSINDEX_ALIGN bytes.  Any bits this allocates beyond
508c2ecf20Sopenharmony_ci * nlabel bits must be zero.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistruct nd_namespace_index {
538c2ecf20Sopenharmony_ci	u8 sig[NSINDEX_SIG_LEN];
548c2ecf20Sopenharmony_ci	u8 flags[3];
558c2ecf20Sopenharmony_ci	u8 labelsize;
568c2ecf20Sopenharmony_ci	__le32 seq;
578c2ecf20Sopenharmony_ci	__le64 myoff;
588c2ecf20Sopenharmony_ci	__le64 mysize;
598c2ecf20Sopenharmony_ci	__le64 otheroff;
608c2ecf20Sopenharmony_ci	__le64 labeloff;
618c2ecf20Sopenharmony_ci	__le32 nslot;
628c2ecf20Sopenharmony_ci	__le16 major;
638c2ecf20Sopenharmony_ci	__le16 minor;
648c2ecf20Sopenharmony_ci	__le64 checksum;
658c2ecf20Sopenharmony_ci	u8 free[];
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/**
698c2ecf20Sopenharmony_ci * struct nd_namespace_label - namespace superblock
708c2ecf20Sopenharmony_ci * @uuid: UUID per RFC 4122
718c2ecf20Sopenharmony_ci * @name: optional name (NULL-terminated)
728c2ecf20Sopenharmony_ci * @flags: see NSLABEL_FLAG_*
738c2ecf20Sopenharmony_ci * @nlabel: num labels to describe this ns
748c2ecf20Sopenharmony_ci * @position: labels position in set
758c2ecf20Sopenharmony_ci * @isetcookie: interleave set cookie
768c2ecf20Sopenharmony_ci * @lbasize: LBA size in bytes or 0 for pmem
778c2ecf20Sopenharmony_ci * @dpa: DPA of NVM range on this DIMM
788c2ecf20Sopenharmony_ci * @rawsize: size of namespace
798c2ecf20Sopenharmony_ci * @slot: slot of this label in label area
808c2ecf20Sopenharmony_ci * @unused: must be zero
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cistruct nd_namespace_label {
838c2ecf20Sopenharmony_ci	u8 uuid[NSLABEL_UUID_LEN];
848c2ecf20Sopenharmony_ci	u8 name[NSLABEL_NAME_LEN];
858c2ecf20Sopenharmony_ci	__le32 flags;
868c2ecf20Sopenharmony_ci	__le16 nlabel;
878c2ecf20Sopenharmony_ci	__le16 position;
888c2ecf20Sopenharmony_ci	__le64 isetcookie;
898c2ecf20Sopenharmony_ci	__le64 lbasize;
908c2ecf20Sopenharmony_ci	__le64 dpa;
918c2ecf20Sopenharmony_ci	__le64 rawsize;
928c2ecf20Sopenharmony_ci	__le32 slot;
938c2ecf20Sopenharmony_ci	/*
948c2ecf20Sopenharmony_ci	 * Accessing fields past this point should be gated by a
958c2ecf20Sopenharmony_ci	 * namespace_label_has() check.
968c2ecf20Sopenharmony_ci	 */
978c2ecf20Sopenharmony_ci	u8 align;
988c2ecf20Sopenharmony_ci	u8 reserved[3];
998c2ecf20Sopenharmony_ci	guid_t type_guid;
1008c2ecf20Sopenharmony_ci	guid_t abstraction_guid;
1018c2ecf20Sopenharmony_ci	u8 reserved2[88];
1028c2ecf20Sopenharmony_ci	__le64 checksum;
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define NVDIMM_BTT_GUID "8aed63a2-29a2-4c66-8b12-f05d15d3922a"
1068c2ecf20Sopenharmony_ci#define NVDIMM_BTT2_GUID "18633bfc-1735-4217-8ac9-17239282d3f8"
1078c2ecf20Sopenharmony_ci#define NVDIMM_PFN_GUID "266400ba-fb9f-4677-bcb0-968f11d0d225"
1088c2ecf20Sopenharmony_ci#define NVDIMM_DAX_GUID "97a86d9c-3cdd-4eda-986f-5068b4f80088"
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/**
1118c2ecf20Sopenharmony_ci * struct nd_label_id - identifier string for dpa allocation
1128c2ecf20Sopenharmony_ci * @id: "{blk|pmem}-<namespace uuid>"
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_cistruct nd_label_id {
1158c2ecf20Sopenharmony_ci	char id[ND_LABEL_ID_SIZE];
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/*
1198c2ecf20Sopenharmony_ci * If the 'best' index is invalid, so is the 'next' index.  Otherwise,
1208c2ecf20Sopenharmony_ci * the next index is MOD(index+1, 2)
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cistatic inline int nd_label_next_nsindex(int index)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	if (index < 0)
1258c2ecf20Sopenharmony_ci		return -1;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return (index + 1) % 2;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct nvdimm_drvdata;
1318c2ecf20Sopenharmony_ciint nd_label_data_init(struct nvdimm_drvdata *ndd);
1328c2ecf20Sopenharmony_cisize_t sizeof_namespace_index(struct nvdimm_drvdata *ndd);
1338c2ecf20Sopenharmony_ciint nd_label_active_count(struct nvdimm_drvdata *ndd);
1348c2ecf20Sopenharmony_cistruct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n);
1358c2ecf20Sopenharmony_ciu32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd);
1368c2ecf20Sopenharmony_cibool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot);
1378c2ecf20Sopenharmony_ciu32 nd_label_nfree(struct nvdimm_drvdata *ndd);
1388c2ecf20Sopenharmony_cienum nvdimm_claim_class to_nvdimm_cclass(guid_t *guid);
1398c2ecf20Sopenharmony_cistruct nd_region;
1408c2ecf20Sopenharmony_cistruct nd_namespace_pmem;
1418c2ecf20Sopenharmony_cistruct nd_namespace_blk;
1428c2ecf20Sopenharmony_ciint nd_pmem_namespace_label_update(struct nd_region *nd_region,
1438c2ecf20Sopenharmony_ci		struct nd_namespace_pmem *nspm, resource_size_t size);
1448c2ecf20Sopenharmony_ciint nd_blk_namespace_label_update(struct nd_region *nd_region,
1458c2ecf20Sopenharmony_ci		struct nd_namespace_blk *nsblk, resource_size_t size);
1468c2ecf20Sopenharmony_ci#endif /* __LABEL_H__ */
147