1 /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2 /*
3  *  include/asm-s390/zcrypt.h
4  *
5  *  zcrypt 2.2.1 (user-visible header)
6  *
7  *  Copyright IBM Corp. 2001, 2022
8  *  Author(s): Robert Burroughs
9  *	       Eric Rossman (edrossma@us.ibm.com)
10  *
11  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
12  */
13 
14 #ifndef __ASM_S390_ZCRYPT_H
15 #define __ASM_S390_ZCRYPT_H
16 
17 #define ZCRYPT_VERSION 2
18 #define ZCRYPT_RELEASE 2
19 #define ZCRYPT_VARIANT 1
20 
21 #include <linux/ioctl.h>
22 #include <linux/compiler.h>
23 #include <linux/types.h>
24 
25 /* Name of the zcrypt device driver. */
26 #define ZCRYPT_NAME "zcrypt"
27 
28 /**
29  * struct ica_rsa_modexpo
30  *
31  * Requirements:
32  * - outputdatalength is at least as large as inputdatalength.
33  * - All key parts are right justified in their fields, padded on
34  *   the left with zeroes.
35  * - length(b_key) = inputdatalength
36  * - length(n_modulus) = inputdatalength
37  */
38 struct ica_rsa_modexpo {
39 	__u8 __user  *inputdata;
40 	__u32	      inputdatalength;
41 	__u8 __user  *outputdata;
42 	__u32	      outputdatalength;
43 	__u8 __user  *b_key;
44 	__u8 __user  *n_modulus;
45 };
46 
47 /**
48  * struct ica_rsa_modexpo_crt
49  *
50  * Requirements:
51  * - inputdatalength is even.
52  * - outputdatalength is at least as large as inputdatalength.
53  * - All key parts are right justified in their fields, padded on
54  *   the left with zeroes.
55  * - length(bp_key)	= inputdatalength/2 + 8
56  * - length(bq_key)	= inputdatalength/2
57  * - length(np_key)	= inputdatalength/2 + 8
58  * - length(nq_key)	= inputdatalength/2
59  * - length(u_mult_inv) = inputdatalength/2 + 8
60  */
61 struct ica_rsa_modexpo_crt {
62 	__u8 __user  *inputdata;
63 	__u32	      inputdatalength;
64 	__u8 __user  *outputdata;
65 	__u32	      outputdatalength;
66 	__u8 __user  *bp_key;
67 	__u8 __user  *bq_key;
68 	__u8 __user  *np_prime;
69 	__u8 __user  *nq_prime;
70 	__u8 __user  *u_mult_inv;
71 };
72 
73 /**
74  * CPRBX
75  *	  Note that all shorts and ints are big-endian.
76  *	  All pointer fields are 16 bytes long, and mean nothing.
77  *
78  *	  A request CPRB is followed by a request_parameter_block.
79  *
80  *	  The request (or reply) parameter block is organized thus:
81  *	    function code
82  *	    VUD block
83  *	    key block
84  */
85 struct CPRBX {
86 	__u16	     cprb_len;		/* CPRB length	      220	 */
87 	__u8	     cprb_ver_id;	/* CPRB version id.   0x02	 */
88 	__u8	     _pad_000[3];	/* Alignment pad bytes		 */
89 	__u8	     func_id[2];	/* function id	      0x5432	 */
90 	__u8	     cprb_flags[4];	/* Flags			 */
91 	__u32	     req_parml;		/* request parameter buffer len	 */
92 	__u32	     req_datal;		/* request data buffer		 */
93 	__u32	     rpl_msgbl;		/* reply  message block length	 */
94 	__u32	     rpld_parml;	/* replied parameter block len	 */
95 	__u32	     rpl_datal;		/* reply data block len		 */
96 	__u32	     rpld_datal;	/* replied data block len	 */
97 	__u32	     req_extbl;		/* request extension block len	 */
98 	__u8	     _pad_001[4];	/* reserved			 */
99 	__u32	     rpld_extbl;	/* replied extension block len	 */
100 	__u8	     _pad_002[16 - sizeof(__u8 *)];
101 	__u8 __user *req_parmb;		/* request parm block 'address'	 */
102 	__u8	     _pad_003[16 - sizeof(__u8 *)];
103 	__u8 __user *req_datab;		/* request data block 'address'	 */
104 	__u8	     _pad_004[16 - sizeof(__u8 *)];
105 	__u8 __user *rpl_parmb;		/* reply parm block 'address'	 */
106 	__u8	     _pad_005[16 - sizeof(__u8 *)];
107 	__u8 __user *rpl_datab;		/* reply data block 'address'	 */
108 	__u8	     _pad_006[16 - sizeof(__u8 *)];
109 	__u8 __user *req_extb;		/* request extension block 'addr'*/
110 	__u8	     _pad_007[16 - sizeof(__u8 *)];
111 	__u8 __user *rpl_extb;		/* reply extension block 'address'*/
112 	__u16	     ccp_rtcode;	/* server return code		 */
113 	__u16	     ccp_rscode;	/* server reason code		 */
114 	__u32	     mac_data_len;	/* Mac Data Length		 */
115 	__u8	     logon_id[8];	/* Logon Identifier		 */
116 	__u8	     mac_value[8];	/* Mac Value			 */
117 	__u8	     mac_content_flgs;	/* Mac content flag byte	 */
118 	__u8	     _pad_008;		/* Alignment			 */
119 	__u16	     domain;		/* Domain			 */
120 	__u8	     _pad_009[12];	/* reserved, checked for zeros	 */
121 	__u8	     _pad_010[36];	/* reserved			 */
122 } __attribute__((packed));
123 
124 /**
125  * xcRB
126  */
127 struct ica_xcRB {
128 	__u16	      agent_ID;
129 	__u32	      user_defined;
130 	__u16	      request_ID;
131 	__u32	      request_control_blk_length;
132 	__u8	      _padding1[16 - sizeof(__u8 *)];
133 	__u8 __user  *request_control_blk_addr;
134 	__u32	      request_data_length;
135 	__u8	      _padding2[16 - sizeof(__u8 *)];
136 	__u8 __user  *request_data_address;
137 	__u32	      reply_control_blk_length;
138 	__u8	      _padding3[16 - sizeof(__u8 *)];
139 	__u8 __user  *reply_control_blk_addr;
140 	__u32	      reply_data_length;
141 	__u8	      __padding4[16 - sizeof(__u8 *)];
142 	__u8 __user  *reply_data_addr;
143 	__u16	      priority_window;
144 	__u32	      status;
145 } __attribute__((packed));
146 
147 /**
148  * struct ep11_cprb - EP11 connectivity programming request block
149  * @cprb_len:		CPRB header length [0x0020]
150  * @cprb_ver_id:	CPRB version id.   [0x04]
151  * @pad_000:		Alignment pad bytes
152  * @flags:		Admin bit [0x80], Special bit [0x20]
153  * @func_id:		Function id / subtype [0x5434] "T4"
154  * @source_id:		Source id [originator id]
155  * @target_id:		Target id [usage/ctrl domain id]
156  * @ret_code:		Return code
157  * @reserved1:		Reserved
158  * @reserved2:		Reserved
159  * @payload_len:	Payload length
160  */
161 struct ep11_cprb {
162 	__u16	cprb_len;
163 	__u8	cprb_ver_id;
164 	__u8	pad_000[2];
165 	__u8	flags;
166 	__u8	func_id[2];
167 	__u32	source_id;
168 	__u32	target_id;
169 	__u32	ret_code;
170 	__u32	reserved1;
171 	__u32	reserved2;
172 	__u32	payload_len;
173 } __attribute__((packed));
174 
175 /**
176  * struct ep11_target_dev - EP11 target device list
177  * @ap_id:	AP device id
178  * @dom_id:	Usage domain id
179  */
180 struct ep11_target_dev {
181 	__u16 ap_id;
182 	__u16 dom_id;
183 };
184 
185 /**
186  * struct ep11_urb - EP11 user request block
187  * @targets_num:	Number of target adapters
188  * @targets:		Addr to target adapter list
189  * @weight:		Level of request priority
190  * @req_no:		Request id/number
191  * @req_len:		Request length
192  * @req:		Addr to request block
193  * @resp_len:		Response length
194  * @resp:		Addr to response block
195  */
196 struct ep11_urb {
197 	__u16		targets_num;
198 	__u8 __user    *targets;
199 	__u64		weight;
200 	__u64		req_no;
201 	__u64		req_len;
202 	__u8 __user    *req;
203 	__u64		resp_len;
204 	__u8 __user    *resp;
205 } __attribute__((packed));
206 
207 /**
208  * struct zcrypt_device_status_ext
209  * @hwtype:		raw hardware type
210  * @qid:		8 bit device index, 8 bit domain
211  * @functions:		AP device function bit field 'abcdef'
212  *			a, b, c = reserved
213  *			d = CCA coprocessor
214  *			e = Accelerator
215  *			f = EP11 coprocessor
216  * @online		online status
217  * @reserved		reserved
218  */
219 struct zcrypt_device_status_ext {
220 	unsigned int hwtype:8;
221 	unsigned int qid:16;
222 	unsigned int online:1;
223 	unsigned int functions:6;
224 	unsigned int reserved:1;
225 };
226 
227 #define MAX_ZDEV_CARDIDS_EXT 256
228 #define MAX_ZDEV_DOMAINS_EXT 256
229 
230 /* Maximum number of zcrypt devices */
231 #define MAX_ZDEV_ENTRIES_EXT (MAX_ZDEV_CARDIDS_EXT * MAX_ZDEV_DOMAINS_EXT)
232 
233 /* Device matrix of all zcrypt devices */
234 struct zcrypt_device_matrix_ext {
235 	struct zcrypt_device_status_ext device[MAX_ZDEV_ENTRIES_EXT];
236 };
237 
238 #define AUTOSELECT  0xFFFFFFFF
239 #define AUTOSEL_AP  ((__u16)0xFFFF)
240 #define AUTOSEL_DOM ((__u16)0xFFFF)
241 
242 #define ZCRYPT_IOCTL_MAGIC 'z'
243 
244 /**
245  * Interface notes:
246  *
247  * The ioctl()s which are implemented (along with relevant details)
248  * are:
249  *
250  *   ICARSAMODEXPO
251  *     Perform an RSA operation using a Modulus-Exponent pair
252  *     This takes an ica_rsa_modexpo struct as its arg.
253  *
254  *     NOTE: please refer to the comments preceding this structure
255  *	     for the implementation details for the contents of the
256  *	     block
257  *
258  *   ICARSACRT
259  *     Perform an RSA operation using a Chinese-Remainder Theorem key
260  *     This takes an ica_rsa_modexpo_crt struct as its arg.
261  *
262  *     NOTE: please refer to the comments preceding this structure
263  *	     for the implementation details for the contents of the
264  *	     block
265  *
266  *   ZSECSENDCPRB
267  *     Send an arbitrary CPRB to a crypto card.
268  *
269  *   ZSENDEP11CPRB
270  *     Send an arbitrary EP11 CPRB to an EP11 coprocessor crypto card.
271  *
272  *   ZCRYPT_DEVICE_STATUS
273  *     The given struct zcrypt_device_matrix_ext is updated with
274  *     status information for each currently known apqn.
275  *
276  *   ZCRYPT_STATUS_MASK
277  *     Return an MAX_ZDEV_CARDIDS_EXT element array of unsigned chars for the
278  *     status of all devices.
279  *	 0x01: PCICA
280  *	 0x02: PCICC
281  *	 0x03: PCIXCC_MCL2
282  *	 0x04: PCIXCC_MCL3
283  *	 0x05: CEX2C
284  *	 0x06: CEX2A
285  *	 0x07: CEX3C
286  *	 0x08: CEX3A
287  *	 0x0a: CEX4
288  *	 0x0b: CEX5
289  *	 0x0c: CEX6, CEX7 or CEX8
290  *	 0x0d: device is disabled
291  *
292  *   ZCRYPT_QDEPTH_MASK
293  *     Return an MAX_ZDEV_CARDIDS_EXT element array of unsigned chars for the
294  *     queue depth of all devices.
295  *
296  *   ZCRYPT_PERDEV_REQCNT
297  *     Return an MAX_ZDEV_CARDIDS_EXT element array of unsigned integers for
298  *     the number of successfully completed requests per device since the
299  *     device was detected and made available.
300  *
301  */
302 
303 /**
304  * Supported ioctl calls
305  */
306 #define ICARSAMODEXPO  _IOC(_IOC_READ | _IOC_WRITE, ZCRYPT_IOCTL_MAGIC, 0x05, 0)
307 #define ICARSACRT      _IOC(_IOC_READ | _IOC_WRITE, ZCRYPT_IOCTL_MAGIC, 0x06, 0)
308 #define ZSECSENDCPRB   _IOC(_IOC_READ | _IOC_WRITE, ZCRYPT_IOCTL_MAGIC, 0x81, 0)
309 #define ZSENDEP11CPRB  _IOC(_IOC_READ | _IOC_WRITE, ZCRYPT_IOCTL_MAGIC, 0x04, 0)
310 
311 #define ZCRYPT_DEVICE_STATUS _IOC(_IOC_READ | _IOC_WRITE, ZCRYPT_IOCTL_MAGIC, 0x5f, 0)
312 #define ZCRYPT_STATUS_MASK   _IOR(ZCRYPT_IOCTL_MAGIC, 0x58, char[MAX_ZDEV_CARDIDS_EXT])
313 #define ZCRYPT_QDEPTH_MASK   _IOR(ZCRYPT_IOCTL_MAGIC, 0x59, char[MAX_ZDEV_CARDIDS_EXT])
314 #define ZCRYPT_PERDEV_REQCNT _IOR(ZCRYPT_IOCTL_MAGIC, 0x5a, int[MAX_ZDEV_CARDIDS_EXT])
315 
316 /*
317  * Support for multiple zcrypt device nodes.
318  */
319 
320 /* Nr of minor device node numbers to allocate. */
321 #define ZCRYPT_MAX_MINOR_NODES 256
322 
323 /* Max amount of possible ioctls */
324 #define MAX_ZDEV_IOCTLS (1 << _IOC_NRBITS)
325 
326 /*
327  * Only deprecated defines, structs and ioctls below this line.
328  */
329 
330 /* Deprecated: use MAX_ZDEV_CARDIDS_EXT */
331 #define MAX_ZDEV_CARDIDS 64
332 /* Deprecated: use MAX_ZDEV_DOMAINS_EXT */
333 #define MAX_ZDEV_DOMAINS 256
334 
335 /* Deprecated: use MAX_ZDEV_ENTRIES_EXT */
336 #define MAX_ZDEV_ENTRIES (MAX_ZDEV_CARDIDS * MAX_ZDEV_DOMAINS)
337 
338 /* Deprecated: use struct zcrypt_device_status_ext */
339 struct zcrypt_device_status {
340 	unsigned int hwtype:8;
341 	unsigned int qid:14;
342 	unsigned int online:1;
343 	unsigned int functions:6;
344 	unsigned int reserved:3;
345 };
346 
347 /* Deprecated: use struct zcrypt_device_matrix_ext */
348 struct zcrypt_device_matrix {
349 	struct zcrypt_device_status device[MAX_ZDEV_ENTRIES];
350 };
351 
352 /* Deprecated: use ZCRYPT_DEVICE_STATUS */
353 #define ZDEVICESTATUS _IOC(_IOC_READ | _IOC_WRITE, ZCRYPT_IOCTL_MAGIC, 0x4f, 0)
354 /* Deprecated: use ZCRYPT_STATUS_MASK */
355 #define Z90STAT_STATUS_MASK _IOR(ZCRYPT_IOCTL_MAGIC, 0x48, char[64])
356 /* Deprecated: use ZCRYPT_QDEPTH_MASK */
357 #define Z90STAT_QDEPTH_MASK _IOR(ZCRYPT_IOCTL_MAGIC, 0x49, char[64])
358 /* Deprecated: use ZCRYPT_PERDEV_REQCNT */
359 #define Z90STAT_PERDEV_REQCNT _IOR(ZCRYPT_IOCTL_MAGIC, 0x4a, int[64])
360 
361 /* Deprecated: use sysfs to query these values */
362 #define Z90STAT_REQUESTQ_COUNT	_IOR(ZCRYPT_IOCTL_MAGIC, 0x44, int)
363 #define Z90STAT_PENDINGQ_COUNT	_IOR(ZCRYPT_IOCTL_MAGIC, 0x45, int)
364 #define Z90STAT_TOTALOPEN_COUNT _IOR(ZCRYPT_IOCTL_MAGIC, 0x46, int)
365 #define Z90STAT_DOMAIN_INDEX	_IOR(ZCRYPT_IOCTL_MAGIC, 0x47, int)
366 
367 /*
368  * The ioctl number ranges 0x40 - 0x42 and 0x4b - 0x4e had been used in the
369  * past, don't assign new ioctls for these.
370  */
371 
372 #endif /* __ASM_S390_ZCRYPT_H */
373