1
2
3
4
5
6
7
8
9
10
11#ifndef _ASM_S390_CPACF_H
12#define _ASM_S390_CPACF_H
13
14#include <asm/facility.h>
15
16
17
18
19#define CPACF_KMAC 0xb91e
20#define CPACF_KM 0xb92e
21#define CPACF_KMC 0xb92f
22#define CPACF_KIMD 0xb93e
23#define CPACF_KLMD 0xb93f
24#define CPACF_PCKMO 0xb928
25#define CPACF_KMF 0xb92a
26#define CPACF_KMO 0xb92b
27#define CPACF_PCC 0xb92c
28#define CPACF_KMCTR 0xb92d
29#define CPACF_PRNO 0xb93c
30#define CPACF_KMA 0xb929
31
32
33
34
35#define CPACF_ENCRYPT 0x00
36#define CPACF_DECRYPT 0x80
37
38
39
40
41#define CPACF_KM_QUERY 0x00
42#define CPACF_KM_DEA 0x01
43#define CPACF_KM_TDEA_128 0x02
44#define CPACF_KM_TDEA_192 0x03
45#define CPACF_KM_AES_128 0x12
46#define CPACF_KM_AES_192 0x13
47#define CPACF_KM_AES_256 0x14
48#define CPACF_KM_PAES_128 0x1a
49#define CPACF_KM_PAES_192 0x1b
50#define CPACF_KM_PAES_256 0x1c
51#define CPACF_KM_XTS_128 0x32
52#define CPACF_KM_XTS_256 0x34
53#define CPACF_KM_PXTS_128 0x3a
54#define CPACF_KM_PXTS_256 0x3c
55
56
57
58
59
60#define CPACF_KMC_QUERY 0x00
61#define CPACF_KMC_DEA 0x01
62#define CPACF_KMC_TDEA_128 0x02
63#define CPACF_KMC_TDEA_192 0x03
64#define CPACF_KMC_AES_128 0x12
65#define CPACF_KMC_AES_192 0x13
66#define CPACF_KMC_AES_256 0x14
67#define CPACF_KMC_PAES_128 0x1a
68#define CPACF_KMC_PAES_192 0x1b
69#define CPACF_KMC_PAES_256 0x1c
70#define CPACF_KMC_PRNG 0x43
71
72
73
74
75
76#define CPACF_KMCTR_QUERY 0x00
77#define CPACF_KMCTR_DEA 0x01
78#define CPACF_KMCTR_TDEA_128 0x02
79#define CPACF_KMCTR_TDEA_192 0x03
80#define CPACF_KMCTR_AES_128 0x12
81#define CPACF_KMCTR_AES_192 0x13
82#define CPACF_KMCTR_AES_256 0x14
83#define CPACF_KMCTR_PAES_128 0x1a
84#define CPACF_KMCTR_PAES_192 0x1b
85#define CPACF_KMCTR_PAES_256 0x1c
86
87
88
89
90
91#define CPACF_KIMD_QUERY 0x00
92#define CPACF_KIMD_SHA_1 0x01
93#define CPACF_KIMD_SHA_256 0x02
94#define CPACF_KIMD_SHA_512 0x03
95#define CPACF_KIMD_GHASH 0x41
96
97
98
99
100
101#define CPACF_KLMD_QUERY 0x00
102#define CPACF_KLMD_SHA_1 0x01
103#define CPACF_KLMD_SHA_256 0x02
104#define CPACF_KLMD_SHA_512 0x03
105
106
107
108
109
110#define CPACF_KMAC_QUERY 0x00
111#define CPACF_KMAC_DEA 0x01
112#define CPACF_KMAC_TDEA_128 0x02
113#define CPACF_KMAC_TDEA_192 0x03
114
115
116
117
118
119#define CPACF_PCKMO_QUERY 0x00
120#define CPACF_PCKMO_ENC_DES_KEY 0x01
121#define CPACF_PCKMO_ENC_TDES_128_KEY 0x02
122#define CPACF_PCKMO_ENC_TDES_192_KEY 0x03
123#define CPACF_PCKMO_ENC_AES_128_KEY 0x12
124#define CPACF_PCKMO_ENC_AES_192_KEY 0x13
125#define CPACF_PCKMO_ENC_AES_256_KEY 0x14
126
127
128
129
130
131#define CPACF_PRNO_QUERY 0x00
132#define CPACF_PRNO_SHA512_DRNG_GEN 0x03
133#define CPACF_PRNO_SHA512_DRNG_SEED 0x83
134#define CPACF_PRNO_TRNG_Q_R2C_RATIO 0x70
135#define CPACF_PRNO_TRNG 0x72
136
137typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
138
139
140
141
142
143
144
145
146
147
148
149static inline void __cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
150{
151 register unsigned long r0 asm("0") = 0;
152 register unsigned long r1 asm("1") = (unsigned long) mask;
153
154 asm volatile(
155 " spm 0\n"
156
157 "0: .insn rrf,%[opc] << 16,2,4,6,0\n"
158 " brc 1,0b\n"
159 : "=m" (*mask)
160 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
161 : "cc");
162}
163
164static inline int __cpacf_check_opcode(unsigned int opcode)
165{
166 switch (opcode) {
167 case CPACF_KMAC:
168 case CPACF_KM:
169 case CPACF_KMC:
170 case CPACF_KIMD:
171 case CPACF_KLMD:
172 return test_facility(17);
173 case CPACF_PCKMO:
174 return test_facility(76);
175 case CPACF_KMF:
176 case CPACF_KMO:
177 case CPACF_PCC:
178 case CPACF_KMCTR:
179 return test_facility(77);
180 case CPACF_PRNO:
181 return test_facility(57);
182 default:
183 BUG();
184 }
185}
186
187static inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
188{
189 if (__cpacf_check_opcode(opcode)) {
190 __cpacf_query(opcode, mask);
191 return 1;
192 }
193 memset(mask, 0, sizeof(*mask));
194 return 0;
195}
196
197static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
198{
199 return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
200}
201
202static inline int cpacf_query_func(unsigned int opcode, unsigned int func)
203{
204 cpacf_mask_t mask;
205
206 if (cpacf_query(opcode, &mask))
207 return cpacf_test_func(&mask, func);
208 return 0;
209}
210
211
212
213
214
215
216
217
218
219
220
221
222static inline int cpacf_km(unsigned long func, void *param,
223 u8 *dest, const u8 *src, long src_len)
224{
225 register unsigned long r0 asm("0") = (unsigned long) func;
226 register unsigned long r1 asm("1") = (unsigned long) param;
227 register unsigned long r2 asm("2") = (unsigned long) src;
228 register unsigned long r3 asm("3") = (unsigned long) src_len;
229 register unsigned long r4 asm("4") = (unsigned long) dest;
230
231 asm volatile(
232 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
233 " brc 1,0b\n"
234 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
235 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
236 : "cc", "memory");
237
238 return src_len - r3;
239}
240
241
242
243
244
245
246
247
248
249
250
251
252static inline int cpacf_kmc(unsigned long func, void *param,
253 u8 *dest, const u8 *src, long src_len)
254{
255 register unsigned long r0 asm("0") = (unsigned long) func;
256 register unsigned long r1 asm("1") = (unsigned long) param;
257 register unsigned long r2 asm("2") = (unsigned long) src;
258 register unsigned long r3 asm("3") = (unsigned long) src_len;
259 register unsigned long r4 asm("4") = (unsigned long) dest;
260
261 asm volatile(
262 "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
263 " brc 1,0b\n"
264 : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
265 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
266 : "cc", "memory");
267
268 return src_len - r3;
269}
270
271
272
273
274
275
276
277
278
279static inline void cpacf_kimd(unsigned long func, void *param,
280 const u8 *src, long src_len)
281{
282 register unsigned long r0 asm("0") = (unsigned long) func;
283 register unsigned long r1 asm("1") = (unsigned long) param;
284 register unsigned long r2 asm("2") = (unsigned long) src;
285 register unsigned long r3 asm("3") = (unsigned long) src_len;
286
287 asm volatile(
288 "0: .insn rre,%[opc] << 16,0,%[src]\n"
289 " brc 1,0b\n"
290 : [src] "+a" (r2), [len] "+d" (r3)
291 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
292 : "cc", "memory");
293}
294
295
296
297
298
299
300
301
302static inline void cpacf_klmd(unsigned long func, void *param,
303 const u8 *src, long src_len)
304{
305 register unsigned long r0 asm("0") = (unsigned long) func;
306 register unsigned long r1 asm("1") = (unsigned long) param;
307 register unsigned long r2 asm("2") = (unsigned long) src;
308 register unsigned long r3 asm("3") = (unsigned long) src_len;
309
310 asm volatile(
311 "0: .insn rre,%[opc] << 16,0,%[src]\n"
312 " brc 1,0b\n"
313 : [src] "+a" (r2), [len] "+d" (r3)
314 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
315 : "cc", "memory");
316}
317
318
319
320
321
322
323
324
325
326
327
328static inline int cpacf_kmac(unsigned long func, void *param,
329 const u8 *src, long src_len)
330{
331 register unsigned long r0 asm("0") = (unsigned long) func;
332 register unsigned long r1 asm("1") = (unsigned long) param;
333 register unsigned long r2 asm("2") = (unsigned long) src;
334 register unsigned long r3 asm("3") = (unsigned long) src_len;
335
336 asm volatile(
337 "0: .insn rre,%[opc] << 16,0,%[src]\n"
338 " brc 1,0b\n"
339 : [src] "+a" (r2), [len] "+d" (r3)
340 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
341 : "cc", "memory");
342
343 return src_len - r3;
344}
345
346
347
348
349
350
351
352
353
354
355
356
357
358static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
359 const u8 *src, long src_len, u8 *counter)
360{
361 register unsigned long r0 asm("0") = (unsigned long) func;
362 register unsigned long r1 asm("1") = (unsigned long) param;
363 register unsigned long r2 asm("2") = (unsigned long) src;
364 register unsigned long r3 asm("3") = (unsigned long) src_len;
365 register unsigned long r4 asm("4") = (unsigned long) dest;
366 register unsigned long r6 asm("6") = (unsigned long) counter;
367
368 asm volatile(
369 "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
370 " brc 1,0b\n"
371 : [src] "+a" (r2), [len] "+d" (r3),
372 [dst] "+a" (r4), [ctr] "+a" (r6)
373 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
374 : "cc", "memory");
375
376 return src_len - r3;
377}
378
379
380
381
382
383
384
385
386
387
388
389static inline void cpacf_prno(unsigned long func, void *param,
390 u8 *dest, unsigned long dest_len,
391 const u8 *seed, unsigned long seed_len)
392{
393 register unsigned long r0 asm("0") = (unsigned long) func;
394 register unsigned long r1 asm("1") = (unsigned long) param;
395 register unsigned long r2 asm("2") = (unsigned long) dest;
396 register unsigned long r3 asm("3") = (unsigned long) dest_len;
397 register unsigned long r4 asm("4") = (unsigned long) seed;
398 register unsigned long r5 asm("5") = (unsigned long) seed_len;
399
400 asm volatile (
401 "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
402 " brc 1,0b\n"
403 : [dst] "+a" (r2), [dlen] "+d" (r3)
404 : [fc] "d" (r0), [pba] "a" (r1),
405 [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PRNO)
406 : "cc", "memory");
407}
408
409
410
411
412
413
414
415
416static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len,
417 u8 *cbuf, unsigned long cbuf_len)
418{
419 register unsigned long r0 asm("0") = (unsigned long) CPACF_PRNO_TRNG;
420 register unsigned long r2 asm("2") = (unsigned long) ucbuf;
421 register unsigned long r3 asm("3") = (unsigned long) ucbuf_len;
422 register unsigned long r4 asm("4") = (unsigned long) cbuf;
423 register unsigned long r5 asm("5") = (unsigned long) cbuf_len;
424
425 asm volatile (
426 "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
427 " brc 1,0b\n"
428 : [ucbuf] "+a" (r2), [ucbuflen] "+d" (r3),
429 [cbuf] "+a" (r4), [cbuflen] "+d" (r5)
430 : [fc] "d" (r0), [opc] "i" (CPACF_PRNO)
431 : "cc", "memory");
432}
433
434
435
436
437
438
439
440static inline void cpacf_pcc(unsigned long func, void *param)
441{
442 register unsigned long r0 asm("0") = (unsigned long) func;
443 register unsigned long r1 asm("1") = (unsigned long) param;
444
445 asm volatile(
446 "0: .insn rre,%[opc] << 16,0,0\n"
447 " brc 1,0b\n"
448 :
449 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
450 : "cc", "memory");
451}
452
453
454
455
456
457
458
459
460
461static inline void cpacf_pckmo(long func, void *param)
462{
463 register unsigned long r0 asm("0") = (unsigned long) func;
464 register unsigned long r1 asm("1") = (unsigned long) param;
465
466 asm volatile(
467 " .insn rre,%[opc] << 16,0,0\n"
468 :
469 : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCKMO)
470 : "cc", "memory");
471}
472
473#endif
474