1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * CXL IOCTLs for Memory Devices
4  */
5 
6 #ifndef _UAPI_CXL_MEM_H_
7 #define _UAPI_CXL_MEM_H_
8 
9 #include <linux/types.h>
10 
11 /**
12  * DOC: UAPI
13  *
14  * Not all of the commands that the driver supports are available for use by
15  * userspace at all times.  Userspace can check the result of the QUERY command
16  * to determine the live set of commands.  Alternatively, it can issue the
17  * command and check for failure.
18  */
19 
20 #define CXL_MEM_QUERY_COMMANDS _IOR(0xCE, 1, struct cxl_mem_query_commands)
21 #define CXL_MEM_SEND_COMMAND _IOWR(0xCE, 2, struct cxl_send_command)
22 
23 /*
24  * NOTE: New defines must be added to the end of the list to preserve
25  * compatibility because this enum is exported to user space.
26  */
27 #define CXL_CMDS                                                          \
28 	___C(INVALID, "Invalid Command"),                                 \
29 	___C(IDENTIFY, "Identify Command"),                               \
30 	___C(RAW, "Raw device command"),                                  \
31 	___C(GET_SUPPORTED_LOGS, "Get Supported Logs"),                   \
32 	___C(GET_FW_INFO, "Get FW Info"),                                 \
33 	___C(GET_PARTITION_INFO, "Get Partition Information"),            \
34 	___C(GET_LSA, "Get Label Storage Area"),                          \
35 	___C(GET_HEALTH_INFO, "Get Health Info"),                         \
36 	___C(GET_LOG, "Get Log"),                                         \
37 	___C(SET_PARTITION_INFO, "Set Partition Information"),            \
38 	___C(SET_LSA, "Set Label Storage Area"),                          \
39 	___C(GET_ALERT_CONFIG, "Get Alert Configuration"),                \
40 	___C(SET_ALERT_CONFIG, "Set Alert Configuration"),                \
41 	___C(GET_SHUTDOWN_STATE, "Get Shutdown State"),                   \
42 	___C(SET_SHUTDOWN_STATE, "Set Shutdown State"),                   \
43 	___C(GET_POISON, "Get Poison List"),                              \
44 	___C(INJECT_POISON, "Inject Poison"),                             \
45 	___C(CLEAR_POISON, "Clear Poison"),                               \
46 	___C(GET_SCAN_MEDIA_CAPS, "Get Scan Media Capabilities"),         \
47 	___C(SCAN_MEDIA, "Scan Media"),                                   \
48 	___C(GET_SCAN_MEDIA, "Get Scan Media Results"),                   \
49 	___C(MAX, "invalid / last command")
50 
51 #define ___C(a, b) CXL_MEM_COMMAND_ID_##a
52 enum { CXL_CMDS };
53 
54 #undef ___C
55 #define ___C(a, b) { b }
56 static const struct {
57 	const char *name;
58 } cxl_command_names[] __attribute__((__unused__)) = { CXL_CMDS };
59 
60 /*
61  * Here's how this actually breaks out:
62  * cxl_command_names[] = {
63  *	[CXL_MEM_COMMAND_ID_INVALID] = { "Invalid Command" },
64  *	[CXL_MEM_COMMAND_ID_IDENTIFY] = { "Identify Command" },
65  *	...
66  *	[CXL_MEM_COMMAND_ID_MAX] = { "invalid / last command" },
67  * };
68  */
69 
70 #undef ___C
71 
72 /**
73  * struct cxl_command_info - Command information returned from a query.
74  * @id: ID number for the command.
75  * @flags: Flags that specify command behavior.
76  *
77  *         CXL_MEM_COMMAND_FLAG_USER_ENABLED
78  *
79  *         The given command id is supported by the driver and is supported by
80  *         a related opcode on the device.
81  *
82  *         CXL_MEM_COMMAND_FLAG_EXCLUSIVE
83  *
84  *         Requests with the given command id will terminate with EBUSY as the
85  *         kernel actively owns management of the given resource. For example,
86  *         the label-storage-area can not be written while the kernel is
87  *         actively managing that space.
88  *
89  * @size_in: Expected input size, or ~0 if variable length.
90  * @size_out: Expected output size, or ~0 if variable length.
91  *
92  * Represents a single command that is supported by both the driver and the
93  * hardware. This is returned as part of an array from the query ioctl. The
94  * following would be a command that takes a variable length input and returns 0
95  * bytes of output.
96  *
97  *  - @id = 10
98  *  - @flags = CXL_MEM_COMMAND_FLAG_ENABLED
99  *  - @size_in = ~0
100  *  - @size_out = 0
101  *
102  * See struct cxl_mem_query_commands.
103  */
104 struct cxl_command_info {
105 	__u32 id;
106 
107 	__u32 flags;
108 #define CXL_MEM_COMMAND_FLAG_MASK		GENMASK(1, 0)
109 #define CXL_MEM_COMMAND_FLAG_ENABLED		BIT(0)
110 #define CXL_MEM_COMMAND_FLAG_EXCLUSIVE		BIT(1)
111 
112 	__u32 size_in;
113 	__u32 size_out;
114 };
115 
116 /**
117  * struct cxl_mem_query_commands - Query supported commands.
118  * @n_commands: In/out parameter. When @n_commands is > 0, the driver will
119  *		return min(num_support_commands, n_commands). When @n_commands
120  *		is 0, driver will return the number of total supported commands.
121  * @rsvd: Reserved for future use.
122  * @commands: Output array of supported commands. This array must be allocated
123  *            by userspace to be at least min(num_support_commands, @n_commands)
124  *
125  * Allow userspace to query the available commands supported by both the driver,
126  * and the hardware. Commands that aren't supported by either the driver, or the
127  * hardware are not returned in the query.
128  *
129  * Examples:
130  *
131  *  - { .n_commands = 0 } // Get number of supported commands
132  *  - { .n_commands = 15, .commands = buf } // Return first 15 (or less)
133  *    supported commands
134  *
135  *  See struct cxl_command_info.
136  */
137 struct cxl_mem_query_commands {
138 	/*
139 	 * Input: Number of commands to return (space allocated by user)
140 	 * Output: Number of commands supported by the driver/hardware
141 	 *
142 	 * If n_commands is 0, kernel will only return number of commands and
143 	 * not try to populate commands[], thus allowing userspace to know how
144 	 * much space to allocate
145 	 */
146 	__u32 n_commands;
147 	__u32 rsvd;
148 
149 	struct cxl_command_info __user commands[]; /* out: supported commands */
150 };
151 
152 /**
153  * struct cxl_send_command - Send a command to a memory device.
154  * @id: The command to send to the memory device. This must be one of the
155  *	commands returned by the query command.
156  * @flags: Flags for the command (input).
157  * @raw: Special fields for raw commands
158  * @raw.opcode: Opcode passed to hardware when using the RAW command.
159  * @raw.rsvd: Must be zero.
160  * @rsvd: Must be zero.
161  * @retval: Return value from the memory device (output).
162  * @in: Parameters associated with input payload.
163  * @in.size: Size of the payload to provide to the device (input).
164  * @in.rsvd: Must be zero.
165  * @in.payload: Pointer to memory for payload input, payload is little endian.
166  * @out: Parameters associated with output payload.
167  * @out.size: Size of the payload received from the device (input/output). This
168  *	      field is filled in by userspace to let the driver know how much
169  *	      space was allocated for output. It is populated by the driver to
170  *	      let userspace know how large the output payload actually was.
171  * @out.rsvd: Must be zero.
172  * @out.payload: Pointer to memory for payload output, payload is little endian.
173  *
174  * Mechanism for userspace to send a command to the hardware for processing. The
175  * driver will do basic validation on the command sizes. In some cases even the
176  * payload may be introspected. Userspace is required to allocate large enough
177  * buffers for size_out which can be variable length in certain situations.
178  */
179 struct cxl_send_command {
180 	__u32 id;
181 	__u32 flags;
182 	union {
183 		struct {
184 			__u16 opcode;
185 			__u16 rsvd;
186 		} raw;
187 		__u32 rsvd;
188 	};
189 	__u32 retval;
190 
191 	struct {
192 		__u32 size;
193 		__u32 rsvd;
194 		__u64 payload;
195 	} in;
196 
197 	struct {
198 		__u32 size;
199 		__u32 rsvd;
200 		__u64 payload;
201 	} out;
202 };
203 
204 #endif
205