Merge tag 'backlight-next-5.14' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / s390 / crypto / zcrypt_msgtype6.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright IBM Corp. 2001, 2012
4  *  Author(s): Robert Burroughs
5  *             Eric Rossman (edrossma@us.ibm.com)
6  *
7  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
8  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
9  *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
10  *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
11  */
12
13 #define KMSG_COMPONENT "zcrypt"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/atomic.h>
22 #include <linux/uaccess.h>
23
24 #include "ap_bus.h"
25 #include "zcrypt_api.h"
26 #include "zcrypt_error.h"
27 #include "zcrypt_msgtype6.h"
28 #include "zcrypt_cca_key.h"
29
30 #define CEXXC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply       */
31
32 #define CEIL4(x) ((((x)+3)/4)*4)
33
34 struct response_type {
35         struct completion work;
36         int type;
37 };
38 #define CEXXC_RESPONSE_TYPE_ICA  0
39 #define CEXXC_RESPONSE_TYPE_XCRB 1
40 #define CEXXC_RESPONSE_TYPE_EP11 2
41
42 MODULE_AUTHOR("IBM Corporation");
43 MODULE_DESCRIPTION("Cryptographic Coprocessor (message type 6), " \
44                    "Copyright IBM Corp. 2001, 2012");
45 MODULE_LICENSE("GPL");
46
47 /**
48  * CPRB
49  *        Note that all shorts, ints and longs are little-endian.
50  *        All pointer fields are 32-bits long, and mean nothing
51  *
52  *        A request CPRB is followed by a request_parameter_block.
53  *
54  *        The request (or reply) parameter block is organized thus:
55  *          function code
56  *          VUD block
57  *          key block
58  */
59 struct CPRB {
60         unsigned short cprb_len;        /* CPRB length                   */
61         unsigned char cprb_ver_id;      /* CPRB version id.              */
62         unsigned char pad_000;          /* Alignment pad byte.           */
63         unsigned char srpi_rtcode[4];   /* SRPI return code LELONG       */
64         unsigned char srpi_verb;        /* SRPI verb type                */
65         unsigned char flags;            /* flags                         */
66         unsigned char func_id[2];       /* function id                   */
67         unsigned char checkpoint_flag;  /*                               */
68         unsigned char resv2;            /* reserved                      */
69         unsigned short req_parml;       /* request parameter buffer      */
70                                         /* length 16-bit little endian   */
71         unsigned char req_parmp[4];     /* request parameter buffer      *
72                                          * pointer (means nothing: the   *
73                                          * parameter buffer follows      *
74                                          * the CPRB).                    */
75         unsigned char req_datal[4];     /* request data buffer           */
76                                         /* length         ULELONG        */
77         unsigned char req_datap[4];     /* request data buffer           */
78                                         /* pointer                       */
79         unsigned short rpl_parml;       /* reply  parameter buffer       */
80                                         /* length 16-bit little endian   */
81         unsigned char pad_001[2];       /* Alignment pad bytes. ULESHORT */
82         unsigned char rpl_parmp[4];     /* reply parameter buffer        *
83                                          * pointer (means nothing: the   *
84                                          * parameter buffer follows      *
85                                          * the CPRB).                    */
86         unsigned char rpl_datal[4];     /* reply data buffer len ULELONG */
87         unsigned char rpl_datap[4];     /* reply data buffer             */
88                                         /* pointer                       */
89         unsigned short ccp_rscode;      /* server reason code   ULESHORT */
90         unsigned short ccp_rtcode;      /* server return code   ULESHORT */
91         unsigned char repd_parml[2];    /* replied parameter len ULESHORT*/
92         unsigned char mac_data_len[2];  /* Mac Data Length      ULESHORT */
93         unsigned char repd_datal[4];    /* replied data length  ULELONG  */
94         unsigned char req_pc[2];        /* PC identifier                 */
95         unsigned char res_origin[8];    /* resource origin               */
96         unsigned char mac_value[8];     /* Mac Value                     */
97         unsigned char logon_id[8];      /* Logon Identifier              */
98         unsigned char usage_domain[2];  /* cdx                           */
99         unsigned char resv3[18];        /* reserved for requestor        */
100         unsigned short svr_namel;       /* server name length  ULESHORT  */
101         unsigned char svr_name[8];      /* server name                   */
102 } __packed;
103
104 struct function_and_rules_block {
105         unsigned char function_code[2];
106         unsigned short ulen;
107         unsigned char only_rule[8];
108 } __packed;
109
110 /**
111  * The following is used to initialize the CPRBX passed to the CEXxC/CEXxP
112  * card in a type6 message. The 3 fields that must be filled in at execution
113  * time are  req_parml, rpl_parml and usage_domain.
114  * Everything about this interface is ascii/big-endian, since the
115  * device does *not* have 'Intel inside'.
116  *
117  * The CPRBX is followed immediately by the parm block.
118  * The parm block contains:
119  * - function code ('PD' 0x5044 or 'PK' 0x504B)
120  * - rule block (one of:)
121  *   + 0x000A 'PKCS-1.2' (MCL2 'PD')
122  *   + 0x000A 'ZERO-PAD' (MCL2 'PK')
123  *   + 0x000A 'ZERO-PAD' (MCL3 'PD' or CEX2C 'PD')
124  *   + 0x000A 'MRP     ' (MCL3 'PK' or CEX2C 'PK')
125  * - VUD block
126  */
127 static const struct CPRBX static_cprbx = {
128         .cprb_len       =  0x00DC,
129         .cprb_ver_id    =  0x02,
130         .func_id        = {0x54, 0x32},
131 };
132
133 int speed_idx_cca(int req_type)
134 {
135         switch (req_type) {
136         case 0x4142:
137         case 0x4149:
138         case 0x414D:
139         case 0x4341:
140         case 0x4344:
141         case 0x4354:
142         case 0x4358:
143         case 0x444B:
144         case 0x4558:
145         case 0x4643:
146         case 0x4651:
147         case 0x4C47:
148         case 0x4C4B:
149         case 0x4C51:
150         case 0x4F48:
151         case 0x504F:
152         case 0x5053:
153         case 0x5058:
154         case 0x5343:
155         case 0x5344:
156         case 0x5345:
157         case 0x5350:
158                 return LOW;
159         case 0x414B:
160         case 0x4345:
161         case 0x4349:
162         case 0x434D:
163         case 0x4847:
164         case 0x4849:
165         case 0x484D:
166         case 0x4850:
167         case 0x4851:
168         case 0x4954:
169         case 0x4958:
170         case 0x4B43:
171         case 0x4B44:
172         case 0x4B45:
173         case 0x4B47:
174         case 0x4B48:
175         case 0x4B49:
176         case 0x4B4E:
177         case 0x4B50:
178         case 0x4B52:
179         case 0x4B54:
180         case 0x4B58:
181         case 0x4D50:
182         case 0x4D53:
183         case 0x4D56:
184         case 0x4D58:
185         case 0x5044:
186         case 0x5045:
187         case 0x5046:
188         case 0x5047:
189         case 0x5049:
190         case 0x504B:
191         case 0x504D:
192         case 0x5254:
193         case 0x5347:
194         case 0x5349:
195         case 0x534B:
196         case 0x534D:
197         case 0x5356:
198         case 0x5358:
199         case 0x5443:
200         case 0x544B:
201         case 0x5647:
202                 return HIGH;
203         default:
204                 return MEDIUM;
205         }
206 }
207
208 int speed_idx_ep11(int req_type)
209 {
210         switch (req_type) {
211         case  1:
212         case  2:
213         case 36:
214         case 37:
215         case 38:
216         case 39:
217         case 40:
218                 return LOW;
219         case 17:
220         case 18:
221         case 19:
222         case 20:
223         case 21:
224         case 22:
225         case 26:
226         case 30:
227         case 31:
228         case 32:
229         case 33:
230         case 34:
231         case 35:
232                 return HIGH;
233         default:
234                 return MEDIUM;
235         }
236 }
237
238
239 /**
240  * Convert a ICAMEX message to a type6 MEX message.
241  *
242  * @zq: crypto device pointer
243  * @ap_msg: pointer to AP message
244  * @mex: pointer to user input data
245  *
246  * Returns 0 on success or negative errno value.
247  */
248 static int ICAMEX_msg_to_type6MEX_msgX(struct zcrypt_queue *zq,
249                                        struct ap_message *ap_msg,
250                                        struct ica_rsa_modexpo *mex)
251 {
252         static struct type6_hdr static_type6_hdrX = {
253                 .type           =  0x06,
254                 .offset1        =  0x00000058,
255                 .agent_id       = {'C', 'A',},
256                 .function_code  = {'P', 'K'},
257         };
258         static struct function_and_rules_block static_pke_fnr = {
259                 .function_code  = {'P', 'K'},
260                 .ulen           = 10,
261                 .only_rule      = {'M', 'R', 'P', ' ', ' ', ' ', ' ', ' '}
262         };
263         struct {
264                 struct type6_hdr hdr;
265                 struct CPRBX cprbx;
266                 struct function_and_rules_block fr;
267                 unsigned short length;
268                 char text[0];
269         } __packed * msg = ap_msg->msg;
270         int size;
271
272         /*
273          * The inputdatalength was a selection criteria in the dispatching
274          * function zcrypt_rsa_modexpo(). However, make sure the following
275          * copy_from_user() never exceeds the allocated buffer space.
276          */
277         if (WARN_ON_ONCE(mex->inputdatalength > PAGE_SIZE))
278                 return -EINVAL;
279
280         /* VUD.ciphertext */
281         msg->length = mex->inputdatalength + 2;
282         if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
283                 return -EFAULT;
284
285         /* Set up key which is located after the variable length text. */
286         size = zcrypt_type6_mex_key_en(mex, msg->text+mex->inputdatalength);
287         if (size < 0)
288                 return size;
289         size += sizeof(*msg) + mex->inputdatalength;
290
291         /* message header, cprbx and f&r */
292         msg->hdr = static_type6_hdrX;
293         msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
294         msg->hdr.FromCardLen1 = CEXXC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
295
296         msg->cprbx = static_cprbx;
297         msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
298         msg->cprbx.rpl_msgbl = msg->hdr.FromCardLen1;
299
300         msg->fr = static_pke_fnr;
301
302         msg->cprbx.req_parml = size - sizeof(msg->hdr) - sizeof(msg->cprbx);
303
304         ap_msg->len = size;
305         return 0;
306 }
307
308 /**
309  * Convert a ICACRT message to a type6 CRT message.
310  *
311  * @zq: crypto device pointer
312  * @ap_msg: pointer to AP message
313  * @crt: pointer to user input data
314  *
315  * Returns 0 on success or negative errno value.
316  */
317 static int ICACRT_msg_to_type6CRT_msgX(struct zcrypt_queue *zq,
318                                        struct ap_message *ap_msg,
319                                        struct ica_rsa_modexpo_crt *crt)
320 {
321         static struct type6_hdr static_type6_hdrX = {
322                 .type           =  0x06,
323                 .offset1        =  0x00000058,
324                 .agent_id       = {'C', 'A',},
325                 .function_code  = {'P', 'D'},
326         };
327         static struct function_and_rules_block static_pkd_fnr = {
328                 .function_code  = {'P', 'D'},
329                 .ulen           = 10,
330                 .only_rule      = {'Z', 'E', 'R', 'O', '-', 'P', 'A', 'D'}
331         };
332
333         struct {
334                 struct type6_hdr hdr;
335                 struct CPRBX cprbx;
336                 struct function_and_rules_block fr;
337                 unsigned short length;
338                 char text[0];
339         } __packed * msg = ap_msg->msg;
340         int size;
341
342         /*
343          * The inputdatalength was a selection criteria in the dispatching
344          * function zcrypt_rsa_crt(). However, make sure the following
345          * copy_from_user() never exceeds the allocated buffer space.
346          */
347         if (WARN_ON_ONCE(crt->inputdatalength > PAGE_SIZE))
348                 return -EINVAL;
349
350         /* VUD.ciphertext */
351         msg->length = crt->inputdatalength + 2;
352         if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
353                 return -EFAULT;
354
355         /* Set up key which is located after the variable length text. */
356         size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength);
357         if (size < 0)
358                 return size;
359         size += sizeof(*msg) + crt->inputdatalength;    /* total size of msg */
360
361         /* message header, cprbx and f&r */
362         msg->hdr = static_type6_hdrX;
363         msg->hdr.ToCardLen1 = size -  sizeof(msg->hdr);
364         msg->hdr.FromCardLen1 = CEXXC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
365
366         msg->cprbx = static_cprbx;
367         msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
368         msg->cprbx.req_parml = msg->cprbx.rpl_msgbl =
369                 size - sizeof(msg->hdr) - sizeof(msg->cprbx);
370
371         msg->fr = static_pkd_fnr;
372
373         ap_msg->len = size;
374         return 0;
375 }
376
377 /**
378  * Convert a XCRB message to a type6 CPRB message.
379  *
380  * @zq: crypto device pointer
381  * @ap_msg: pointer to AP message
382  * @xcRB: pointer to user input data
383  *
384  * Returns 0 on success or -EFAULT, -EINVAL.
385  */
386 struct type86_fmt2_msg {
387         struct type86_hdr hdr;
388         struct type86_fmt2_ext fmt2;
389 } __packed;
390
391 static int XCRB_msg_to_type6CPRB_msgX(bool userspace, struct ap_message *ap_msg,
392                                       struct ica_xcRB *xcRB,
393                                       unsigned int *fcode,
394                                       unsigned short **dom)
395 {
396         static struct type6_hdr static_type6_hdrX = {
397                 .type           =  0x06,
398                 .offset1        =  0x00000058,
399         };
400         struct {
401                 struct type6_hdr hdr;
402                 struct CPRBX cprbx;
403         } __packed * msg = ap_msg->msg;
404
405         int rcblen = CEIL4(xcRB->request_control_blk_length);
406         int replylen, req_sumlen, resp_sumlen;
407         char *req_data = ap_msg->msg + sizeof(struct type6_hdr) + rcblen;
408         char *function_code;
409
410         if (CEIL4(xcRB->request_control_blk_length) <
411                         xcRB->request_control_blk_length)
412                 return -EINVAL; /* overflow after alignment*/
413
414         /* length checks */
415         ap_msg->len = sizeof(struct type6_hdr) +
416                 CEIL4(xcRB->request_control_blk_length) +
417                 xcRB->request_data_length;
418         if (ap_msg->len > MSGTYPE06_MAX_MSG_SIZE)
419                 return -EINVAL;
420
421         /*
422          * Overflow check
423          * sum must be greater (or equal) than the largest operand
424          */
425         req_sumlen = CEIL4(xcRB->request_control_blk_length) +
426                         xcRB->request_data_length;
427         if ((CEIL4(xcRB->request_control_blk_length) <=
428                                                 xcRB->request_data_length) ?
429                 (req_sumlen < xcRB->request_data_length) :
430                 (req_sumlen < CEIL4(xcRB->request_control_blk_length))) {
431                 return -EINVAL;
432         }
433
434         if (CEIL4(xcRB->reply_control_blk_length) <
435                         xcRB->reply_control_blk_length)
436                 return -EINVAL; /* overflow after alignment*/
437
438         replylen = sizeof(struct type86_fmt2_msg) +
439                 CEIL4(xcRB->reply_control_blk_length) +
440                 xcRB->reply_data_length;
441         if (replylen > MSGTYPE06_MAX_MSG_SIZE)
442                 return -EINVAL;
443
444         /*
445          * Overflow check
446          * sum must be greater (or equal) than the largest operand
447          */
448         resp_sumlen = CEIL4(xcRB->reply_control_blk_length) +
449                         xcRB->reply_data_length;
450         if ((CEIL4(xcRB->reply_control_blk_length) <= xcRB->reply_data_length) ?
451                 (resp_sumlen < xcRB->reply_data_length) :
452                 (resp_sumlen < CEIL4(xcRB->reply_control_blk_length))) {
453                 return -EINVAL;
454         }
455
456         /* prepare type6 header */
457         msg->hdr = static_type6_hdrX;
458         memcpy(msg->hdr.agent_id, &(xcRB->agent_ID), sizeof(xcRB->agent_ID));
459         msg->hdr.ToCardLen1 = xcRB->request_control_blk_length;
460         if (xcRB->request_data_length) {
461                 msg->hdr.offset2 = msg->hdr.offset1 + rcblen;
462                 msg->hdr.ToCardLen2 = xcRB->request_data_length;
463         }
464         msg->hdr.FromCardLen1 = xcRB->reply_control_blk_length;
465         msg->hdr.FromCardLen2 = xcRB->reply_data_length;
466
467         /* prepare CPRB */
468         if (z_copy_from_user(userspace, &(msg->cprbx), xcRB->request_control_blk_addr,
469                              xcRB->request_control_blk_length))
470                 return -EFAULT;
471         if (msg->cprbx.cprb_len + sizeof(msg->hdr.function_code) >
472             xcRB->request_control_blk_length)
473                 return -EINVAL;
474         function_code = ((unsigned char *)&msg->cprbx) + msg->cprbx.cprb_len;
475         memcpy(msg->hdr.function_code, function_code,
476                sizeof(msg->hdr.function_code));
477
478         *fcode = (msg->hdr.function_code[0] << 8) | msg->hdr.function_code[1];
479         *dom = (unsigned short *)&msg->cprbx.domain;
480
481         if (memcmp(function_code, "US", 2) == 0
482             || memcmp(function_code, "AU", 2) == 0)
483                 ap_msg->flags |= AP_MSG_FLAG_SPECIAL;
484
485 #ifdef CONFIG_ZCRYPT_DEBUG
486         if (ap_msg->fi.flags & AP_FI_FLAG_TOGGLE_SPECIAL)
487                 ap_msg->flags ^= AP_MSG_FLAG_SPECIAL;
488 #endif
489
490         /* copy data block */
491         if (xcRB->request_data_length &&
492             z_copy_from_user(userspace, req_data, xcRB->request_data_address,
493                              xcRB->request_data_length))
494                 return -EFAULT;
495
496         return 0;
497 }
498
499 static int xcrb_msg_to_type6_ep11cprb_msgx(bool userspace, struct ap_message *ap_msg,
500                                            struct ep11_urb *xcRB,
501                                            unsigned int *fcode)
502 {
503         unsigned int lfmt;
504         static struct type6_hdr static_type6_ep11_hdr = {
505                 .type           =  0x06,
506                 .rqid           = {0x00, 0x01},
507                 .function_code  = {0x00, 0x00},
508                 .agent_id[0]    =  0x58,        /* {'X'} */
509                 .agent_id[1]    =  0x43,        /* {'C'} */
510                 .offset1        =  0x00000058,
511         };
512
513         struct {
514                 struct type6_hdr hdr;
515                 struct ep11_cprb cprbx;
516                 unsigned char   pld_tag;        /* fixed value 0x30 */
517                 unsigned char   pld_lenfmt;     /* payload length format */
518         } __packed * msg = ap_msg->msg;
519
520         struct pld_hdr {
521                 unsigned char   func_tag;       /* fixed value 0x4 */
522                 unsigned char   func_len;       /* fixed value 0x4 */
523                 unsigned int    func_val;       /* function ID     */
524                 unsigned char   dom_tag;        /* fixed value 0x4 */
525                 unsigned char   dom_len;        /* fixed value 0x4 */
526                 unsigned int    dom_val;        /* domain id       */
527         } __packed * payload_hdr = NULL;
528
529         if (CEIL4(xcRB->req_len) < xcRB->req_len)
530                 return -EINVAL; /* overflow after alignment*/
531
532         /* length checks */
533         ap_msg->len = sizeof(struct type6_hdr) + xcRB->req_len;
534         if (CEIL4(xcRB->req_len) > MSGTYPE06_MAX_MSG_SIZE -
535                                    (sizeof(struct type6_hdr)))
536                 return -EINVAL;
537
538         if (CEIL4(xcRB->resp_len) < xcRB->resp_len)
539                 return -EINVAL; /* overflow after alignment*/
540
541         if (CEIL4(xcRB->resp_len) > MSGTYPE06_MAX_MSG_SIZE -
542                                     (sizeof(struct type86_fmt2_msg)))
543                 return -EINVAL;
544
545         /* prepare type6 header */
546         msg->hdr = static_type6_ep11_hdr;
547         msg->hdr.ToCardLen1   = xcRB->req_len;
548         msg->hdr.FromCardLen1 = xcRB->resp_len;
549
550         /* Import CPRB data from the ioctl input parameter */
551         if (z_copy_from_user(userspace, &(msg->cprbx.cprb_len),
552                              (char __force __user *)xcRB->req, xcRB->req_len)) {
553                 return -EFAULT;
554         }
555
556         if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
557                 switch (msg->pld_lenfmt & 0x03) {
558                 case 1:
559                         lfmt = 2;
560                         break;
561                 case 2:
562                         lfmt = 3;
563                         break;
564                 default:
565                         return -EINVAL;
566                 }
567         } else {
568                 lfmt = 1; /* length format #1 */
569         }
570         payload_hdr = (struct pld_hdr *)((&(msg->pld_lenfmt))+lfmt);
571         *fcode = payload_hdr->func_val & 0xFFFF;
572
573         /* enable special processing based on the cprbs flags special bit */
574         if (msg->cprbx.flags & 0x20)
575                 ap_msg->flags |= AP_MSG_FLAG_SPECIAL;
576
577 #ifdef CONFIG_ZCRYPT_DEBUG
578         if (ap_msg->fi.flags & AP_FI_FLAG_TOGGLE_SPECIAL)
579                 ap_msg->flags ^= AP_MSG_FLAG_SPECIAL;
580 #endif
581
582         return 0;
583 }
584
585 /**
586  * Copy results from a type 86 ICA reply message back to user space.
587  *
588  * @zq: crypto device pointer
589  * @reply: reply AP message.
590  * @data: pointer to user output data
591  * @length: size of user output data
592  *
593  * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
594  */
595 struct type86x_reply {
596         struct type86_hdr hdr;
597         struct type86_fmt2_ext fmt2;
598         struct CPRBX cprbx;
599         unsigned char pad[4];   /* 4 byte function code/rules block ? */
600         unsigned short length;
601         char text[];
602 } __packed;
603
604 struct type86_ep11_reply {
605         struct type86_hdr hdr;
606         struct type86_fmt2_ext fmt2;
607         struct ep11_cprb cprbx;
608 } __packed;
609
610 static int convert_type86_ica(struct zcrypt_queue *zq,
611                           struct ap_message *reply,
612                           char __user *outputdata,
613                           unsigned int outputdatalength)
614 {
615         static unsigned char static_pad[] = {
616                 0x00, 0x02,
617                 0x1B, 0x7B, 0x5D, 0xB5, 0x75, 0x01, 0x3D, 0xFD,
618                 0x8D, 0xD1, 0xC7, 0x03, 0x2D, 0x09, 0x23, 0x57,
619                 0x89, 0x49, 0xB9, 0x3F, 0xBB, 0x99, 0x41, 0x5B,
620                 0x75, 0x21, 0x7B, 0x9D, 0x3B, 0x6B, 0x51, 0x39,
621                 0xBB, 0x0D, 0x35, 0xB9, 0x89, 0x0F, 0x93, 0xA5,
622                 0x0B, 0x47, 0xF1, 0xD3, 0xBB, 0xCB, 0xF1, 0x9D,
623                 0x23, 0x73, 0x71, 0xFF, 0xF3, 0xF5, 0x45, 0xFB,
624                 0x61, 0x29, 0x23, 0xFD, 0xF1, 0x29, 0x3F, 0x7F,
625                 0x17, 0xB7, 0x1B, 0xA9, 0x19, 0xBD, 0x57, 0xA9,
626                 0xD7, 0x95, 0xA3, 0xCB, 0xED, 0x1D, 0xDB, 0x45,
627                 0x7D, 0x11, 0xD1, 0x51, 0x1B, 0xED, 0x71, 0xE9,
628                 0xB1, 0xD1, 0xAB, 0xAB, 0x21, 0x2B, 0x1B, 0x9F,
629                 0x3B, 0x9F, 0xF7, 0xF7, 0xBD, 0x63, 0xEB, 0xAD,
630                 0xDF, 0xB3, 0x6F, 0x5B, 0xDB, 0x8D, 0xA9, 0x5D,
631                 0xE3, 0x7D, 0x77, 0x49, 0x47, 0xF5, 0xA7, 0xFD,
632                 0xAB, 0x2F, 0x27, 0x35, 0x77, 0xD3, 0x49, 0xC9,
633                 0x09, 0xEB, 0xB1, 0xF9, 0xBF, 0x4B, 0xCB, 0x2B,
634                 0xEB, 0xEB, 0x05, 0xFF, 0x7D, 0xC7, 0x91, 0x8B,
635                 0x09, 0x83, 0xB9, 0xB9, 0x69, 0x33, 0x39, 0x6B,
636                 0x79, 0x75, 0x19, 0xBF, 0xBB, 0x07, 0x1D, 0xBD,
637                 0x29, 0xBF, 0x39, 0x95, 0x93, 0x1D, 0x35, 0xC7,
638                 0xC9, 0x4D, 0xE5, 0x97, 0x0B, 0x43, 0x9B, 0xF1,
639                 0x16, 0x93, 0x03, 0x1F, 0xA5, 0xFB, 0xDB, 0xF3,
640                 0x27, 0x4F, 0x27, 0x61, 0x05, 0x1F, 0xB9, 0x23,
641                 0x2F, 0xC3, 0x81, 0xA9, 0x23, 0x71, 0x55, 0x55,
642                 0xEB, 0xED, 0x41, 0xE5, 0xF3, 0x11, 0xF1, 0x43,
643                 0x69, 0x03, 0xBD, 0x0B, 0x37, 0x0F, 0x51, 0x8F,
644                 0x0B, 0xB5, 0x89, 0x5B, 0x67, 0xA9, 0xD9, 0x4F,
645                 0x01, 0xF9, 0x21, 0x77, 0x37, 0x73, 0x79, 0xC5,
646                 0x7F, 0x51, 0xC1, 0xCF, 0x97, 0xA1, 0x75, 0xAD,
647                 0x35, 0x9D, 0xD3, 0xD3, 0xA7, 0x9D, 0x5D, 0x41,
648                 0x6F, 0x65, 0x1B, 0xCF, 0xA9, 0x87, 0x91, 0x09
649         };
650         struct type86x_reply *msg = reply->msg;
651         unsigned short service_rc, service_rs;
652         unsigned int reply_len, pad_len;
653         char *data;
654
655         service_rc = msg->cprbx.ccp_rtcode;
656         if (unlikely(service_rc != 0)) {
657                 service_rs = msg->cprbx.ccp_rscode;
658                 if ((service_rc == 8 && service_rs == 66) ||
659                     (service_rc == 8 && service_rs == 65) ||
660                     (service_rc == 8 && service_rs == 72) ||
661                     (service_rc == 8 && service_rs == 770) ||
662                     (service_rc == 12 && service_rs == 769)) {
663                         ZCRYPT_DBF_WARN("dev=%02x.%04x rc/rs=%d/%d => rc=EINVAL\n",
664                                         AP_QID_CARD(zq->queue->qid),
665                                         AP_QID_QUEUE(zq->queue->qid),
666                                         (int) service_rc, (int) service_rs);
667                         return -EINVAL;
668                 }
669                 zq->online = 0;
670                 pr_err("Crypto dev=%02x.%04x rc/rs=%d/%d online=0 rc=EAGAIN\n",
671                        AP_QID_CARD(zq->queue->qid),
672                        AP_QID_QUEUE(zq->queue->qid),
673                        (int) service_rc, (int) service_rs);
674                 ZCRYPT_DBF_ERR("dev=%02x.%04x rc/rs=%d/%d => online=0 rc=EAGAIN\n",
675                                AP_QID_CARD(zq->queue->qid),
676                                AP_QID_QUEUE(zq->queue->qid),
677                                (int) service_rc, (int) service_rs);
678                 ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
679                 return -EAGAIN;
680         }
681         data = msg->text;
682         reply_len = msg->length - 2;
683         if (reply_len > outputdatalength)
684                 return -EINVAL;
685         /*
686          * For all encipher requests, the length of the ciphertext (reply_len)
687          * will always equal the modulus length. For MEX decipher requests
688          * the output needs to get padded. Minimum pad size is 10.
689          *
690          * Currently, the cases where padding will be added is for:
691          * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
692          *   ZERO-PAD and CRT is only supported for PKD requests)
693          * - PCICC, always
694          */
695         pad_len = outputdatalength - reply_len;
696         if (pad_len > 0) {
697                 if (pad_len < 10)
698                         return -EINVAL;
699                 /* 'restore' padding left in the CEXXC card. */
700                 if (copy_to_user(outputdata, static_pad, pad_len - 1))
701                         return -EFAULT;
702                 if (put_user(0, outputdata + pad_len - 1))
703                         return -EFAULT;
704         }
705         /* Copy the crypto response to user space. */
706         if (copy_to_user(outputdata + pad_len, data, reply_len))
707                 return -EFAULT;
708         return 0;
709 }
710
711 /**
712  * Copy results from a type 86 XCRB reply message back to user space.
713  *
714  * @zq: crypto device pointer
715  * @reply: reply AP message.
716  * @xcRB: pointer to XCRB
717  *
718  * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
719  */
720 static int convert_type86_xcrb(bool userspace, struct zcrypt_queue *zq,
721                                struct ap_message *reply,
722                                struct ica_xcRB *xcRB)
723 {
724         struct type86_fmt2_msg *msg = reply->msg;
725         char *data = reply->msg;
726
727         /* Copy CPRB to user */
728         if (z_copy_to_user(userspace, xcRB->reply_control_blk_addr,
729                            data + msg->fmt2.offset1, msg->fmt2.count1))
730                 return -EFAULT;
731         xcRB->reply_control_blk_length = msg->fmt2.count1;
732
733         /* Copy data buffer to user */
734         if (msg->fmt2.count2)
735                 if (z_copy_to_user(userspace, xcRB->reply_data_addr,
736                                    data + msg->fmt2.offset2, msg->fmt2.count2))
737                         return -EFAULT;
738         xcRB->reply_data_length = msg->fmt2.count2;
739         return 0;
740 }
741
742 /**
743  * Copy results from a type 86 EP11 XCRB reply message back to user space.
744  *
745  * @zq: crypto device pointer
746  * @reply: reply AP message.
747  * @xcRB: pointer to EP11 user request block
748  *
749  * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
750  */
751 static int convert_type86_ep11_xcrb(bool userspace, struct zcrypt_queue *zq,
752                                     struct ap_message *reply,
753                                     struct ep11_urb *xcRB)
754 {
755         struct type86_fmt2_msg *msg = reply->msg;
756         char *data = reply->msg;
757
758         if (xcRB->resp_len < msg->fmt2.count1)
759                 return -EINVAL;
760
761         /* Copy response CPRB to user */
762         if (z_copy_to_user(userspace, (char __force __user *)xcRB->resp,
763                            data + msg->fmt2.offset1, msg->fmt2.count1))
764                 return -EFAULT;
765         xcRB->resp_len = msg->fmt2.count1;
766         return 0;
767 }
768
769 static int convert_type86_rng(struct zcrypt_queue *zq,
770                           struct ap_message *reply,
771                           char *buffer)
772 {
773         struct {
774                 struct type86_hdr hdr;
775                 struct type86_fmt2_ext fmt2;
776                 struct CPRBX cprbx;
777         } __packed * msg = reply->msg;
778         char *data = reply->msg;
779
780         if (msg->cprbx.ccp_rtcode != 0 || msg->cprbx.ccp_rscode != 0)
781                 return -EINVAL;
782         memcpy(buffer, data + msg->fmt2.offset2, msg->fmt2.count2);
783         return msg->fmt2.count2;
784 }
785
786 static int convert_response_ica(struct zcrypt_queue *zq,
787                             struct ap_message *reply,
788                             char __user *outputdata,
789                             unsigned int outputdatalength)
790 {
791         struct type86x_reply *msg = reply->msg;
792
793         switch (msg->hdr.type) {
794         case TYPE82_RSP_CODE:
795         case TYPE88_RSP_CODE:
796                 return convert_error(zq, reply);
797         case TYPE86_RSP_CODE:
798                 if (msg->cprbx.ccp_rtcode &&
799                    (msg->cprbx.ccp_rscode == 0x14f) &&
800                    (outputdatalength > 256)) {
801                         if (zq->zcard->max_exp_bit_length <= 17) {
802                                 zq->zcard->max_exp_bit_length = 17;
803                                 return -EAGAIN;
804                         } else
805                                 return -EINVAL;
806                 }
807                 if (msg->hdr.reply_code)
808                         return convert_error(zq, reply);
809                 if (msg->cprbx.cprb_ver_id == 0x02)
810                         return convert_type86_ica(zq, reply,
811                                                   outputdata, outputdatalength);
812                 fallthrough;    /* wrong cprb version is an unknown response */
813         default:
814                 /* Unknown response type, this should NEVER EVER happen */
815                 zq->online = 0;
816                 pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
817                        AP_QID_CARD(zq->queue->qid),
818                        AP_QID_QUEUE(zq->queue->qid),
819                        (int) msg->hdr.type);
820                 ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
821                                AP_QID_CARD(zq->queue->qid),
822                                AP_QID_QUEUE(zq->queue->qid),
823                                (int) msg->hdr.type);
824                 ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
825                 return -EAGAIN;
826         }
827 }
828
829 static int convert_response_xcrb(bool userspace, struct zcrypt_queue *zq,
830                                  struct ap_message *reply,
831                                  struct ica_xcRB *xcRB)
832 {
833         struct type86x_reply *msg = reply->msg;
834
835         switch (msg->hdr.type) {
836         case TYPE82_RSP_CODE:
837         case TYPE88_RSP_CODE:
838                 xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
839                 return convert_error(zq, reply);
840         case TYPE86_RSP_CODE:
841                 if (msg->hdr.reply_code) {
842                         memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
843                         return convert_error(zq, reply);
844                 }
845                 if (msg->cprbx.cprb_ver_id == 0x02)
846                         return convert_type86_xcrb(userspace, zq, reply, xcRB);
847                 fallthrough;    /* wrong cprb version is an unknown response */
848         default: /* Unknown response type, this should NEVER EVER happen */
849                 xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
850                 zq->online = 0;
851                 pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
852                        AP_QID_CARD(zq->queue->qid),
853                        AP_QID_QUEUE(zq->queue->qid),
854                        (int) msg->hdr.type);
855                 ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
856                                AP_QID_CARD(zq->queue->qid),
857                                AP_QID_QUEUE(zq->queue->qid),
858                                (int) msg->hdr.type);
859                 ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
860                 return -EAGAIN;
861         }
862 }
863
864 static int convert_response_ep11_xcrb(bool userspace, struct zcrypt_queue *zq,
865                                       struct ap_message *reply, struct ep11_urb *xcRB)
866 {
867         struct type86_ep11_reply *msg = reply->msg;
868
869         switch (msg->hdr.type) {
870         case TYPE82_RSP_CODE:
871         case TYPE87_RSP_CODE:
872                 return convert_error(zq, reply);
873         case TYPE86_RSP_CODE:
874                 if (msg->hdr.reply_code)
875                         return convert_error(zq, reply);
876                 if (msg->cprbx.cprb_ver_id == 0x04)
877                         return convert_type86_ep11_xcrb(userspace, zq, reply, xcRB);
878                 fallthrough;    /* wrong cprb version is an unknown resp */
879         default: /* Unknown response type, this should NEVER EVER happen */
880                 zq->online = 0;
881                 pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
882                        AP_QID_CARD(zq->queue->qid),
883                        AP_QID_QUEUE(zq->queue->qid),
884                        (int) msg->hdr.type);
885                 ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
886                                AP_QID_CARD(zq->queue->qid),
887                                AP_QID_QUEUE(zq->queue->qid),
888                                (int) msg->hdr.type);
889                 ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
890                 return -EAGAIN;
891         }
892 }
893
894 static int convert_response_rng(struct zcrypt_queue *zq,
895                                  struct ap_message *reply,
896                                  char *data)
897 {
898         struct type86x_reply *msg = reply->msg;
899
900         switch (msg->hdr.type) {
901         case TYPE82_RSP_CODE:
902         case TYPE88_RSP_CODE:
903                 return -EINVAL;
904         case TYPE86_RSP_CODE:
905                 if (msg->hdr.reply_code)
906                         return -EINVAL;
907                 if (msg->cprbx.cprb_ver_id == 0x02)
908                         return convert_type86_rng(zq, reply, data);
909                 fallthrough;    /* wrong cprb version is an unknown response */
910         default: /* Unknown response type, this should NEVER EVER happen */
911                 zq->online = 0;
912                 pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
913                        AP_QID_CARD(zq->queue->qid),
914                        AP_QID_QUEUE(zq->queue->qid),
915                        (int) msg->hdr.type);
916                 ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
917                                AP_QID_CARD(zq->queue->qid),
918                                AP_QID_QUEUE(zq->queue->qid),
919                                (int) msg->hdr.type);
920                 ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
921                 return -EAGAIN;
922         }
923 }
924
925 /**
926  * This function is called from the AP bus code after a crypto request
927  * "msg" has finished with the reply message "reply".
928  * It is called from tasklet context.
929  * @aq: pointer to the AP queue
930  * @msg: pointer to the AP message
931  * @reply: pointer to the AP reply message
932  */
933 static void zcrypt_msgtype6_receive(struct ap_queue *aq,
934                                   struct ap_message *msg,
935                                   struct ap_message *reply)
936 {
937         static struct error_hdr error_reply = {
938                 .type = TYPE82_RSP_CODE,
939                 .reply_code = REP82_ERROR_MACHINE_FAILURE,
940         };
941         struct response_type *resp_type =
942                 (struct response_type *) msg->private;
943         struct type86x_reply *t86r;
944         int len;
945
946         /* Copy the reply message to the request message buffer. */
947         if (!reply)
948                 goto out;       /* ap_msg->rc indicates the error */
949         t86r = reply->msg;
950         if (t86r->hdr.type == TYPE86_RSP_CODE &&
951                  t86r->cprbx.cprb_ver_id == 0x02) {
952                 switch (resp_type->type) {
953                 case CEXXC_RESPONSE_TYPE_ICA:
954                         len = sizeof(struct type86x_reply) + t86r->length - 2;
955                         len = min_t(int, CEXXC_MAX_ICA_RESPONSE_SIZE, len);
956                         memcpy(msg->msg, reply->msg, len);
957                         break;
958                 case CEXXC_RESPONSE_TYPE_XCRB:
959                         len = t86r->fmt2.offset2 + t86r->fmt2.count2;
960                         len = min_t(int, MSGTYPE06_MAX_MSG_SIZE, len);
961                         memcpy(msg->msg, reply->msg, len);
962                         break;
963                 default:
964                         memcpy(msg->msg, &error_reply, sizeof(error_reply));
965                 }
966         } else
967                 memcpy(msg->msg, reply->msg, sizeof(error_reply));
968 out:
969         complete(&(resp_type->work));
970 }
971
972 /**
973  * This function is called from the AP bus code after a crypto request
974  * "msg" has finished with the reply message "reply".
975  * It is called from tasklet context.
976  * @aq: pointer to the AP queue
977  * @msg: pointer to the AP message
978  * @reply: pointer to the AP reply message
979  */
980 static void zcrypt_msgtype6_receive_ep11(struct ap_queue *aq,
981                                          struct ap_message *msg,
982                                          struct ap_message *reply)
983 {
984         static struct error_hdr error_reply = {
985                 .type = TYPE82_RSP_CODE,
986                 .reply_code = REP82_ERROR_MACHINE_FAILURE,
987         };
988         struct response_type *resp_type =
989                 (struct response_type *)msg->private;
990         struct type86_ep11_reply *t86r;
991         int len;
992
993         /* Copy the reply message to the request message buffer. */
994         if (!reply)
995                 goto out;       /* ap_msg->rc indicates the error */
996         t86r = reply->msg;
997         if (t86r->hdr.type == TYPE86_RSP_CODE &&
998             t86r->cprbx.cprb_ver_id == 0x04) {
999                 switch (resp_type->type) {
1000                 case CEXXC_RESPONSE_TYPE_EP11:
1001                         len = t86r->fmt2.offset1 + t86r->fmt2.count1;
1002                         len = min_t(int, MSGTYPE06_MAX_MSG_SIZE, len);
1003                         memcpy(msg->msg, reply->msg, len);
1004                         break;
1005                 default:
1006                         memcpy(msg->msg, &error_reply, sizeof(error_reply));
1007                 }
1008         } else {
1009                 memcpy(msg->msg, reply->msg, sizeof(error_reply));
1010         }
1011 out:
1012         complete(&(resp_type->work));
1013 }
1014
1015 static atomic_t zcrypt_step = ATOMIC_INIT(0);
1016
1017 /**
1018  * The request distributor calls this function if it picked the CEXxC
1019  * device to handle a modexpo request.
1020  * @zq: pointer to zcrypt_queue structure that identifies the
1021  *      CEXxC device to the request distributor
1022  * @mex: pointer to the modexpo request buffer
1023  */
1024 static long zcrypt_msgtype6_modexpo(struct zcrypt_queue *zq,
1025                                     struct ica_rsa_modexpo *mex,
1026                                     struct ap_message *ap_msg)
1027 {
1028         struct response_type resp_type = {
1029                 .type = CEXXC_RESPONSE_TYPE_ICA,
1030         };
1031         int rc;
1032
1033         ap_msg->msg = (void *) get_zeroed_page(GFP_KERNEL);
1034         if (!ap_msg->msg)
1035                 return -ENOMEM;
1036         ap_msg->receive = zcrypt_msgtype6_receive;
1037         ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1038                 atomic_inc_return(&zcrypt_step);
1039         ap_msg->private = &resp_type;
1040         rc = ICAMEX_msg_to_type6MEX_msgX(zq, ap_msg, mex);
1041         if (rc)
1042                 goto out_free;
1043         init_completion(&resp_type.work);
1044         rc = ap_queue_message(zq->queue, ap_msg);
1045         if (rc)
1046                 goto out_free;
1047         rc = wait_for_completion_interruptible(&resp_type.work);
1048         if (rc == 0) {
1049                 rc = ap_msg->rc;
1050                 if (rc == 0)
1051                         rc = convert_response_ica(zq, ap_msg,
1052                                                   mex->outputdata,
1053                                                   mex->outputdatalength);
1054         } else
1055                 /* Signal pending. */
1056                 ap_cancel_message(zq->queue, ap_msg);
1057 out_free:
1058         free_page((unsigned long) ap_msg->msg);
1059         ap_msg->private = NULL;
1060         ap_msg->msg = NULL;
1061         return rc;
1062 }
1063
1064 /**
1065  * The request distributor calls this function if it picked the CEXxC
1066  * device to handle a modexpo_crt request.
1067  * @zq: pointer to zcrypt_queue structure that identifies the
1068  *      CEXxC device to the request distributor
1069  * @crt: pointer to the modexpoc_crt request buffer
1070  */
1071 static long zcrypt_msgtype6_modexpo_crt(struct zcrypt_queue *zq,
1072                                         struct ica_rsa_modexpo_crt *crt,
1073                                         struct ap_message *ap_msg)
1074 {
1075         struct response_type resp_type = {
1076                 .type = CEXXC_RESPONSE_TYPE_ICA,
1077         };
1078         int rc;
1079
1080         ap_msg->msg = (void *) get_zeroed_page(GFP_KERNEL);
1081         if (!ap_msg->msg)
1082                 return -ENOMEM;
1083         ap_msg->receive = zcrypt_msgtype6_receive;
1084         ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1085                 atomic_inc_return(&zcrypt_step);
1086         ap_msg->private = &resp_type;
1087         rc = ICACRT_msg_to_type6CRT_msgX(zq, ap_msg, crt);
1088         if (rc)
1089                 goto out_free;
1090         init_completion(&resp_type.work);
1091         rc = ap_queue_message(zq->queue, ap_msg);
1092         if (rc)
1093                 goto out_free;
1094         rc = wait_for_completion_interruptible(&resp_type.work);
1095         if (rc == 0) {
1096                 rc = ap_msg->rc;
1097                 if (rc == 0)
1098                         rc = convert_response_ica(zq, ap_msg,
1099                                                   crt->outputdata,
1100                                                   crt->outputdatalength);
1101         } else {
1102                 /* Signal pending. */
1103                 ap_cancel_message(zq->queue, ap_msg);
1104         }
1105 out_free:
1106         free_page((unsigned long) ap_msg->msg);
1107         ap_msg->private = NULL;
1108         ap_msg->msg = NULL;
1109         return rc;
1110 }
1111
1112 /**
1113  * Fetch function code from cprb.
1114  * Extracting the fc requires to copy the cprb from userspace.
1115  * So this function allocates memory and needs an ap_msg prepared
1116  * by the caller with ap_init_message(). Also the caller has to
1117  * make sure ap_release_message() is always called even on failure.
1118  */
1119 unsigned int get_cprb_fc(bool userspace, struct ica_xcRB *xcRB,
1120                          struct ap_message *ap_msg,
1121                          unsigned int *func_code, unsigned short **dom)
1122 {
1123         struct response_type resp_type = {
1124                 .type = CEXXC_RESPONSE_TYPE_XCRB,
1125         };
1126
1127         ap_msg->msg = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
1128         if (!ap_msg->msg)
1129                 return -ENOMEM;
1130         ap_msg->receive = zcrypt_msgtype6_receive;
1131         ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1132                                 atomic_inc_return(&zcrypt_step);
1133         ap_msg->private = kmemdup(&resp_type, sizeof(resp_type), GFP_KERNEL);
1134         if (!ap_msg->private)
1135                 return -ENOMEM;
1136         return XCRB_msg_to_type6CPRB_msgX(userspace, ap_msg, xcRB, func_code, dom);
1137 }
1138
1139 /**
1140  * The request distributor calls this function if it picked the CEXxC
1141  * device to handle a send_cprb request.
1142  * @zq: pointer to zcrypt_queue structure that identifies the
1143  *      CEXxC device to the request distributor
1144  * @xcRB: pointer to the send_cprb request buffer
1145  */
1146 static long zcrypt_msgtype6_send_cprb(bool userspace, struct zcrypt_queue *zq,
1147                                       struct ica_xcRB *xcRB,
1148                                       struct ap_message *ap_msg)
1149 {
1150         int rc;
1151         struct response_type *rtype = (struct response_type *)(ap_msg->private);
1152
1153         init_completion(&rtype->work);
1154         rc = ap_queue_message(zq->queue, ap_msg);
1155         if (rc)
1156                 goto out;
1157         rc = wait_for_completion_interruptible(&rtype->work);
1158         if (rc == 0) {
1159                 rc = ap_msg->rc;
1160                 if (rc == 0)
1161                         rc = convert_response_xcrb(userspace, zq, ap_msg, xcRB);
1162         } else
1163                 /* Signal pending. */
1164                 ap_cancel_message(zq->queue, ap_msg);
1165 out:
1166         return rc;
1167 }
1168
1169 /**
1170  * Fetch function code from ep11 cprb.
1171  * Extracting the fc requires to copy the ep11 cprb from userspace.
1172  * So this function allocates memory and needs an ap_msg prepared
1173  * by the caller with ap_init_message(). Also the caller has to
1174  * make sure ap_release_message() is always called even on failure.
1175  */
1176 unsigned int get_ep11cprb_fc(bool userspace, struct ep11_urb *xcrb,
1177                              struct ap_message *ap_msg,
1178                              unsigned int *func_code)
1179 {
1180         struct response_type resp_type = {
1181                 .type = CEXXC_RESPONSE_TYPE_EP11,
1182         };
1183
1184         ap_msg->msg = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
1185         if (!ap_msg->msg)
1186                 return -ENOMEM;
1187         ap_msg->receive = zcrypt_msgtype6_receive_ep11;
1188         ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1189                                 atomic_inc_return(&zcrypt_step);
1190         ap_msg->private = kmemdup(&resp_type, sizeof(resp_type), GFP_KERNEL);
1191         if (!ap_msg->private)
1192                 return -ENOMEM;
1193         return xcrb_msg_to_type6_ep11cprb_msgx(userspace, ap_msg, xcrb, func_code);
1194 }
1195
1196 /**
1197  * The request distributor calls this function if it picked the CEX4P
1198  * device to handle a send_ep11_cprb request.
1199  * @zq: pointer to zcrypt_queue structure that identifies the
1200  *        CEX4P device to the request distributor
1201  * @xcRB: pointer to the ep11 user request block
1202  */
1203 static long zcrypt_msgtype6_send_ep11_cprb(bool userspace, struct zcrypt_queue *zq,
1204                                            struct ep11_urb *xcrb,
1205                                            struct ap_message *ap_msg)
1206 {
1207         int rc;
1208         unsigned int lfmt;
1209         struct response_type *rtype = (struct response_type *)(ap_msg->private);
1210         struct {
1211                 struct type6_hdr hdr;
1212                 struct ep11_cprb cprbx;
1213                 unsigned char   pld_tag;        /* fixed value 0x30 */
1214                 unsigned char   pld_lenfmt;     /* payload length format */
1215         } __packed * msg = ap_msg->msg;
1216         struct pld_hdr {
1217                 unsigned char   func_tag;       /* fixed value 0x4 */
1218                 unsigned char   func_len;       /* fixed value 0x4 */
1219                 unsigned int    func_val;       /* function ID     */
1220                 unsigned char   dom_tag;        /* fixed value 0x4 */
1221                 unsigned char   dom_len;        /* fixed value 0x4 */
1222                 unsigned int    dom_val;        /* domain id       */
1223         } __packed * payload_hdr = NULL;
1224
1225
1226         /**
1227          * The target domain field within the cprb body/payload block will be
1228          * replaced by the usage domain for non-management commands only.
1229          * Therefore we check the first bit of the 'flags' parameter for
1230          * management command indication.
1231          *   0 - non management command
1232          *   1 - management command
1233          */
1234         if (!((msg->cprbx.flags & 0x80) == 0x80)) {
1235                 msg->cprbx.target_id = (unsigned int)
1236                                         AP_QID_QUEUE(zq->queue->qid);
1237
1238                 if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
1239                         switch (msg->pld_lenfmt & 0x03) {
1240                         case 1:
1241                                 lfmt = 2;
1242                                 break;
1243                         case 2:
1244                                 lfmt = 3;
1245                                 break;
1246                         default:
1247                                 return -EINVAL;
1248                         }
1249                 } else {
1250                         lfmt = 1; /* length format #1 */
1251                 }
1252                 payload_hdr = (struct pld_hdr *)((&(msg->pld_lenfmt))+lfmt);
1253                 payload_hdr->dom_val = (unsigned int)
1254                                         AP_QID_QUEUE(zq->queue->qid);
1255         }
1256
1257         init_completion(&rtype->work);
1258         rc = ap_queue_message(zq->queue, ap_msg);
1259         if (rc)
1260                 goto out;
1261         rc = wait_for_completion_interruptible(&rtype->work);
1262         if (rc == 0) {
1263                 rc = ap_msg->rc;
1264                 if (rc == 0)
1265                         rc = convert_response_ep11_xcrb(userspace, zq, ap_msg, xcrb);
1266         } else
1267                 /* Signal pending. */
1268                 ap_cancel_message(zq->queue, ap_msg);
1269 out:
1270         return rc;
1271 }
1272
1273 unsigned int get_rng_fc(struct ap_message *ap_msg, int *func_code,
1274                                                    unsigned int *domain)
1275 {
1276         struct response_type resp_type = {
1277                 .type = CEXXC_RESPONSE_TYPE_XCRB,
1278         };
1279
1280         ap_msg->msg = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
1281         if (!ap_msg->msg)
1282                 return -ENOMEM;
1283         ap_msg->receive = zcrypt_msgtype6_receive;
1284         ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1285                                 atomic_inc_return(&zcrypt_step);
1286         ap_msg->private = kmemdup(&resp_type, sizeof(resp_type), GFP_KERNEL);
1287         if (!ap_msg->private)
1288                 return -ENOMEM;
1289
1290         rng_type6CPRB_msgX(ap_msg, ZCRYPT_RNG_BUFFER_SIZE, domain);
1291
1292         *func_code = HWRNG;
1293         return 0;
1294 }
1295
1296 /**
1297  * The request distributor calls this function if it picked the CEXxC
1298  * device to generate random data.
1299  * @zq: pointer to zcrypt_queue structure that identifies the
1300  *      CEXxC device to the request distributor
1301  * @buffer: pointer to a memory page to return random data
1302  */
1303 static long zcrypt_msgtype6_rng(struct zcrypt_queue *zq,
1304                                 char *buffer, struct ap_message *ap_msg)
1305 {
1306         struct {
1307                 struct type6_hdr hdr;
1308                 struct CPRBX cprbx;
1309                 char function_code[2];
1310                 short int rule_length;
1311                 char rule[8];
1312                 short int verb_length;
1313                 short int key_length;
1314         } __packed * msg = ap_msg->msg;
1315         struct response_type *rtype = (struct response_type *)(ap_msg->private);
1316         int rc;
1317
1318         msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
1319
1320         init_completion(&rtype->work);
1321         rc = ap_queue_message(zq->queue, ap_msg);
1322         if (rc)
1323                 goto out;
1324         rc = wait_for_completion_interruptible(&rtype->work);
1325         if (rc == 0) {
1326                 rc = ap_msg->rc;
1327                 if (rc == 0)
1328                         rc = convert_response_rng(zq, ap_msg, buffer);
1329         } else
1330                 /* Signal pending. */
1331                 ap_cancel_message(zq->queue, ap_msg);
1332 out:
1333         return rc;
1334 }
1335
1336 /**
1337  * The crypto operations for a CEXxC card.
1338  */
1339 static struct zcrypt_ops zcrypt_msgtype6_norng_ops = {
1340         .owner = THIS_MODULE,
1341         .name = MSGTYPE06_NAME,
1342         .variant = MSGTYPE06_VARIANT_NORNG,
1343         .rsa_modexpo = zcrypt_msgtype6_modexpo,
1344         .rsa_modexpo_crt = zcrypt_msgtype6_modexpo_crt,
1345         .send_cprb = zcrypt_msgtype6_send_cprb,
1346 };
1347
1348 static struct zcrypt_ops zcrypt_msgtype6_ops = {
1349         .owner = THIS_MODULE,
1350         .name = MSGTYPE06_NAME,
1351         .variant = MSGTYPE06_VARIANT_DEFAULT,
1352         .rsa_modexpo = zcrypt_msgtype6_modexpo,
1353         .rsa_modexpo_crt = zcrypt_msgtype6_modexpo_crt,
1354         .send_cprb = zcrypt_msgtype6_send_cprb,
1355         .rng = zcrypt_msgtype6_rng,
1356 };
1357
1358 static struct zcrypt_ops zcrypt_msgtype6_ep11_ops = {
1359         .owner = THIS_MODULE,
1360         .name = MSGTYPE06_NAME,
1361         .variant = MSGTYPE06_VARIANT_EP11,
1362         .rsa_modexpo = NULL,
1363         .rsa_modexpo_crt = NULL,
1364         .send_ep11_cprb = zcrypt_msgtype6_send_ep11_cprb,
1365 };
1366
1367 void __init zcrypt_msgtype6_init(void)
1368 {
1369         zcrypt_msgtype_register(&zcrypt_msgtype6_norng_ops);
1370         zcrypt_msgtype_register(&zcrypt_msgtype6_ops);
1371         zcrypt_msgtype_register(&zcrypt_msgtype6_ep11_ops);
1372 }
1373
1374 void __exit zcrypt_msgtype6_exit(void)
1375 {
1376         zcrypt_msgtype_unregister(&zcrypt_msgtype6_norng_ops);
1377         zcrypt_msgtype_unregister(&zcrypt_msgtype6_ops);
1378         zcrypt_msgtype_unregister(&zcrypt_msgtype6_ep11_ops);
1379 }