1 /*
2  * Copyright (c) 2014-2016, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU Lesser General Public License,
6  * version 2.1, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
11  * more details.
12  */
13 #ifndef __NDCTL_H__
14 #define __NDCTL_H__
15 
16 #include <linux/types.h>
17 
18 struct nd_cmd_dimm_flags {
19 	__u32 status;
20 	__u32 flags;
21 } __packed;
22 
23 struct nd_cmd_get_config_size {
24 	__u32 status;
25 	__u32 config_size;
26 	__u32 max_xfer;
27 } __packed;
28 
29 struct nd_cmd_get_config_data_hdr {
30 	__u32 in_offset;
31 	__u32 in_length;
32 	__u32 status;
33 	__u8 out_buf[0];
34 } __packed;
35 
36 struct nd_cmd_set_config_hdr {
37 	__u32 in_offset;
38 	__u32 in_length;
39 	__u8 in_buf[0];
40 } __packed;
41 
42 struct nd_cmd_vendor_hdr {
43 	__u32 opcode;
44 	__u32 in_length;
45 	__u8 in_buf[0];
46 } __packed;
47 
48 struct nd_cmd_vendor_tail {
49 	__u32 status;
50 	__u32 out_length;
51 	__u8 out_buf[0];
52 } __packed;
53 
54 struct nd_cmd_ars_cap {
55 	__u64 address;
56 	__u64 length;
57 	__u32 status;
58 	__u32 max_ars_out;
59 	__u32 clear_err_unit;
60 	__u16 flags;
61 	__u16 reserved;
62 } __packed;
63 
64 struct nd_cmd_ars_start {
65 	__u64 address;
66 	__u64 length;
67 	__u16 type;
68 	__u8 flags;
69 	__u8 reserved[5];
70 	__u32 status;
71 	__u32 scrub_time;
72 } __packed;
73 
74 struct nd_cmd_ars_status {
75 	__u32 status;
76 	__u32 out_length;
77 	__u64 address;
78 	__u64 length;
79 	__u64 restart_address;
80 	__u64 restart_length;
81 	__u16 type;
82 	__u16 flags;
83 	__u32 num_records;
84 	struct nd_ars_record {
85 		__u32 handle;
86 		__u32 reserved;
87 		__u64 err_address;
88 		__u64 length;
89 	} __packed records[0];
90 } __packed;
91 
92 struct nd_cmd_clear_error {
93 	__u64 address;
94 	__u64 length;
95 	__u32 status;
96 	__u8 reserved[4];
97 	__u64 cleared;
98 } __packed;
99 
100 enum {
101 	ND_CMD_IMPLEMENTED = 0,
102 
103 	/* bus commands */
104 	ND_CMD_ARS_CAP = 1,
105 	ND_CMD_ARS_START = 2,
106 	ND_CMD_ARS_STATUS = 3,
107 	ND_CMD_CLEAR_ERROR = 4,
108 
109 	/* per-dimm commands */
110 	ND_CMD_SMART = 1,
111 	ND_CMD_SMART_THRESHOLD = 2,
112 	ND_CMD_DIMM_FLAGS = 3,
113 	ND_CMD_GET_CONFIG_SIZE = 4,
114 	ND_CMD_GET_CONFIG_DATA = 5,
115 	ND_CMD_SET_CONFIG_DATA = 6,
116 	ND_CMD_VENDOR_EFFECT_LOG_SIZE = 7,
117 	ND_CMD_VENDOR_EFFECT_LOG = 8,
118 	ND_CMD_VENDOR = 9,
119 	ND_CMD_CALL = 10,
120 };
121 
122 enum {
123 	ND_ARS_VOLATILE = 1,
124 	ND_ARS_PERSISTENT = 2,
125 	ND_ARS_RETURN_PREV_DATA = 1 << 1,
126 	ND_CONFIG_LOCKED = 1,
127 };
128 
nvdimm_bus_cmd_name(unsigned cmd)129 static inline const char *nvdimm_bus_cmd_name(unsigned cmd)
130 {
131 	static const char * const names[] = {
132 		[ND_CMD_ARS_CAP] = "ars_cap",
133 		[ND_CMD_ARS_START] = "ars_start",
134 		[ND_CMD_ARS_STATUS] = "ars_status",
135 		[ND_CMD_CLEAR_ERROR] = "clear_error",
136 		[ND_CMD_CALL] = "cmd_call",
137 	};
138 
139 	if (cmd < ARRAY_SIZE(names) && names[cmd])
140 		return names[cmd];
141 	return "unknown";
142 }
143 
nvdimm_cmd_name(unsigned cmd)144 static inline const char *nvdimm_cmd_name(unsigned cmd)
145 {
146 	static const char * const names[] = {
147 		[ND_CMD_SMART] = "smart",
148 		[ND_CMD_SMART_THRESHOLD] = "smart_thresh",
149 		[ND_CMD_DIMM_FLAGS] = "flags",
150 		[ND_CMD_GET_CONFIG_SIZE] = "get_size",
151 		[ND_CMD_GET_CONFIG_DATA] = "get_data",
152 		[ND_CMD_SET_CONFIG_DATA] = "set_data",
153 		[ND_CMD_VENDOR_EFFECT_LOG_SIZE] = "effect_size",
154 		[ND_CMD_VENDOR_EFFECT_LOG] = "effect_log",
155 		[ND_CMD_VENDOR] = "vendor",
156 		[ND_CMD_CALL] = "cmd_call",
157 	};
158 
159 	if (cmd < ARRAY_SIZE(names) && names[cmd])
160 		return names[cmd];
161 	return "unknown";
162 }
163 
164 #define ND_IOCTL 'N'
165 
166 #define ND_IOCTL_DIMM_FLAGS		_IOWR(ND_IOCTL, ND_CMD_DIMM_FLAGS,\
167 					struct nd_cmd_dimm_flags)
168 
169 #define ND_IOCTL_GET_CONFIG_SIZE	_IOWR(ND_IOCTL, ND_CMD_GET_CONFIG_SIZE,\
170 					struct nd_cmd_get_config_size)
171 
172 #define ND_IOCTL_GET_CONFIG_DATA	_IOWR(ND_IOCTL, ND_CMD_GET_CONFIG_DATA,\
173 					struct nd_cmd_get_config_data_hdr)
174 
175 #define ND_IOCTL_SET_CONFIG_DATA	_IOWR(ND_IOCTL, ND_CMD_SET_CONFIG_DATA,\
176 					struct nd_cmd_set_config_hdr)
177 
178 #define ND_IOCTL_VENDOR			_IOWR(ND_IOCTL, ND_CMD_VENDOR,\
179 					struct nd_cmd_vendor_hdr)
180 
181 #define ND_IOCTL_ARS_CAP		_IOWR(ND_IOCTL, ND_CMD_ARS_CAP,\
182 					struct nd_cmd_ars_cap)
183 
184 #define ND_IOCTL_ARS_START		_IOWR(ND_IOCTL, ND_CMD_ARS_START,\
185 					struct nd_cmd_ars_start)
186 
187 #define ND_IOCTL_ARS_STATUS		_IOWR(ND_IOCTL, ND_CMD_ARS_STATUS,\
188 					struct nd_cmd_ars_status)
189 
190 #define ND_IOCTL_CLEAR_ERROR		_IOWR(ND_IOCTL, ND_CMD_CLEAR_ERROR,\
191 					struct nd_cmd_clear_error)
192 
193 #define ND_DEVICE_DIMM 1            /* nd_dimm: container for "config data" */
194 #define ND_DEVICE_REGION_PMEM 2     /* nd_region: (parent of PMEM namespaces) */
195 #define ND_DEVICE_REGION_BLK 3      /* nd_region: (parent of BLK namespaces) */
196 #define ND_DEVICE_NAMESPACE_IO 4    /* legacy persistent memory */
197 #define ND_DEVICE_NAMESPACE_PMEM 5  /* PMEM namespace (may alias with BLK) */
198 #define ND_DEVICE_NAMESPACE_BLK 6   /* BLK namespace (may alias with PMEM) */
199 #define ND_DEVICE_DAX_PMEM 7        /* Device DAX interface to pmem */
200 
201 enum nd_driver_flags {
202 	ND_DRIVER_DIMM            = 1 << ND_DEVICE_DIMM,
203 	ND_DRIVER_REGION_PMEM     = 1 << ND_DEVICE_REGION_PMEM,
204 	ND_DRIVER_REGION_BLK      = 1 << ND_DEVICE_REGION_BLK,
205 	ND_DRIVER_NAMESPACE_IO    = 1 << ND_DEVICE_NAMESPACE_IO,
206 	ND_DRIVER_NAMESPACE_PMEM  = 1 << ND_DEVICE_NAMESPACE_PMEM,
207 	ND_DRIVER_NAMESPACE_BLK   = 1 << ND_DEVICE_NAMESPACE_BLK,
208 	ND_DRIVER_DAX_PMEM	  = 1 << ND_DEVICE_DAX_PMEM,
209 };
210 
211 enum {
212 	ND_MIN_NAMESPACE_SIZE = PAGE_SIZE,
213 };
214 
215 enum ars_masks {
216 	ARS_STATUS_MASK = 0x0000FFFF,
217 	ARS_EXT_STATUS_SHIFT = 16,
218 };
219 
220 /*
221  * struct nd_cmd_pkg
222  *
223  * is a wrapper to a quasi pass thru interface for invoking firmware
224  * associated with nvdimms.
225  *
226  * INPUT PARAMETERS
227  *
228  * nd_family corresponds to the firmware (e.g. DSM) interface.
229  *
230  * nd_command are the function index advertised by the firmware.
231  *
232  * nd_size_in is the size of the input parameters being passed to firmware
233  *
234  * OUTPUT PARAMETERS
235  *
236  * nd_fw_size is the size of the data firmware wants to return for
237  * the call.  If nd_fw_size is greater than size of nd_size_out, only
238  * the first nd_size_out bytes are returned.
239  */
240 
241 struct nd_cmd_pkg {
242 	__u64   nd_family;		/* family of commands */
243 	__u64   nd_command;
244 	__u32   nd_size_in;		/* INPUT: size of input args */
245 	__u32   nd_size_out;		/* INPUT: size of payload */
246 	__u32   nd_reserved2[9];	/* reserved must be zero */
247 	__u32   nd_fw_size;		/* OUTPUT: size fw wants to return */
248 	unsigned char nd_payload[];	/* Contents of call      */
249 };
250 
251 /* These NVDIMM families represent pre-standardization command sets */
252 #define NVDIMM_FAMILY_INTEL 0
253 #define NVDIMM_FAMILY_HPE1 1
254 #define NVDIMM_FAMILY_HPE2 2
255 #define NVDIMM_FAMILY_MSFT 3
256 
257 #define ND_IOCTL_CALL			_IOWR(ND_IOCTL, ND_CMD_CALL,\
258 					struct nd_cmd_pkg)
259 
260 #endif /* __NDCTL_H__ */
261