1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  Copyright IBM Corp. 2001, 2006
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  */
10 
11 #ifndef _ZCRYPT_ERROR_H_
12 #define _ZCRYPT_ERROR_H_
13 
14 #include <linux/atomic.h>
15 #include "zcrypt_debug.h"
16 #include "zcrypt_api.h"
17 #include "zcrypt_msgtype6.h"
18 
19 /**
20  * Reply Messages
21  *
22  * Error reply messages are of two types:
23  *    82:  Error (see below)
24  *    88:  Error (see below)
25  * Both type 82 and type 88 have the same structure in the header.
26  *
27  * Request reply messages are of three known types:
28  *    80:  Reply from a Type 50 Request (see CEX2A-RELATED STRUCTS)
29  *    84:  Reply from a Type 4 Request (see PCICA-RELATED STRUCTS)
30  *    86:  Reply from a Type 6 Request (see PCICC/PCIXCC/CEX2C-RELATED STRUCTS)
31  *
32  */
33 struct error_hdr {
34 	unsigned char reserved1;	/* 0x00			*/
35 	unsigned char type;		/* 0x82 or 0x88		*/
36 	unsigned char reserved2[2];	/* 0x0000		*/
37 	unsigned char reply_code;	/* reply code		*/
38 	unsigned char reserved3[3];	/* 0x000000		*/
39 };
40 
41 #define TYPE82_RSP_CODE 0x82
42 #define TYPE88_RSP_CODE 0x88
43 
44 #define REP82_ERROR_MACHINE_FAILURE	    0x10
45 #define REP82_ERROR_PREEMPT_FAILURE	    0x12
46 #define REP82_ERROR_CHECKPT_FAILURE	    0x14
47 #define REP82_ERROR_MESSAGE_TYPE	    0x20
48 #define REP82_ERROR_INVALID_COMM_CD	    0x21 /* Type 84	*/
49 #define REP82_ERROR_INVALID_MSG_LEN	    0x23
50 #define REP82_ERROR_RESERVD_FIELD	    0x24 /* was 0x50	*/
51 #define REP82_ERROR_FORMAT_FIELD	    0x29
52 #define REP82_ERROR_INVALID_COMMAND	    0x30
53 #define REP82_ERROR_MALFORMED_MSG	    0x40
54 #define REP82_ERROR_INVALID_SPECIAL_CMD	    0x41
55 #define REP82_ERROR_INVALID_DOMAIN_PRECHECK 0x42
56 #define REP82_ERROR_RESERVED_FIELDO	    0x50 /* old value	*/
57 #define REP82_ERROR_WORD_ALIGNMENT	    0x60
58 #define REP82_ERROR_MESSAGE_LENGTH	    0x80
59 #define REP82_ERROR_OPERAND_INVALID	    0x82
60 #define REP82_ERROR_OPERAND_SIZE	    0x84
61 #define REP82_ERROR_EVEN_MOD_IN_OPND	    0x85
62 #define REP82_ERROR_RESERVED_FIELD	    0x88
63 #define REP82_ERROR_INVALID_DOMAIN_PENDING  0x8A
64 #define REP82_ERROR_TRANSPORT_FAIL	    0x90
65 #define REP82_ERROR_PACKET_TRUNCATED	    0xA0
66 #define REP82_ERROR_ZERO_BUFFER_LEN	    0xB0
67 
68 #define REP88_ERROR_MODULE_FAILURE	    0x10
69 
70 #define REP88_ERROR_MESSAGE_TYPE	    0x20
71 #define REP88_ERROR_MESSAGE_MALFORMD	    0x22
72 #define REP88_ERROR_MESSAGE_LENGTH	    0x23
73 #define REP88_ERROR_RESERVED_FIELD	    0x24
74 #define REP88_ERROR_KEY_TYPE		    0x34
75 #define REP88_ERROR_INVALID_KEY	    0x82 /* CEX2A	*/
76 #define REP88_ERROR_OPERAND		    0x84 /* CEX2A	*/
77 #define REP88_ERROR_OPERAND_EVEN_MOD	    0x85 /* CEX2A	*/
78 
79 static inline int convert_error(struct zcrypt_queue *zq,
80 				struct ap_message *reply)
81 {
82 	struct error_hdr *ehdr = reply->message;
83 	int card = AP_QID_CARD(zq->queue->qid);
84 	int queue = AP_QID_QUEUE(zq->queue->qid);
85 
86 	switch (ehdr->reply_code) {
87 	case REP82_ERROR_OPERAND_INVALID:
88 	case REP82_ERROR_OPERAND_SIZE:
89 	case REP82_ERROR_EVEN_MOD_IN_OPND:
90 	case REP88_ERROR_MESSAGE_MALFORMD:
91 	case REP82_ERROR_INVALID_DOMAIN_PRECHECK:
92 	case REP82_ERROR_INVALID_DOMAIN_PENDING:
93 	case REP82_ERROR_INVALID_SPECIAL_CMD:
94 	//   REP88_ERROR_INVALID_KEY		// '82' CEX2A
95 	//   REP88_ERROR_OPERAND		// '84' CEX2A
96 	//   REP88_ERROR_OPERAND_EVEN_MOD	// '85' CEX2A
97 		/* Invalid input data. */
98 		ZCRYPT_DBF(DBF_WARN,
99 			   "device=%02x.%04x reply=0x%02x => rc=EINVAL\n",
100 			   card, queue, ehdr->reply_code);
101 		return -EINVAL;
102 	case REP82_ERROR_MESSAGE_TYPE:
103 	//   REP88_ERROR_MESSAGE_TYPE		// '20' CEX2A
104 		/*
105 		 * To sent a message of the wrong type is a bug in the
106 		 * device driver. Send error msg, disable the device
107 		 * and then repeat the request.
108 		 */
109 		atomic_set(&zcrypt_rescan_req, 1);
110 		zq->online = 0;
111 		pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
112 		       card, queue);
113 		ZCRYPT_DBF(DBF_ERR,
114 			   "device=%02x.%04x reply=0x%02x => online=0 rc=EAGAIN\n",
115 			   card, queue, ehdr->reply_code);
116 		return -EAGAIN;
117 	case REP82_ERROR_TRANSPORT_FAIL:
118 		/* Card or infrastructure failure, disable card */
119 		atomic_set(&zcrypt_rescan_req, 1);
120 		zq->online = 0;
121 		pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
122 		       card, queue);
123 		/* For type 86 response show the apfs value (failure reason) */
124 		if (ehdr->type == TYPE86_RSP_CODE) {
125 			struct {
126 				struct type86_hdr hdr;
127 				struct type86_fmt2_ext fmt2;
128 			} __packed * head = reply->message;
129 			unsigned int apfs = *((u32 *)head->fmt2.apfs);
130 
131 			ZCRYPT_DBF(DBF_ERR,
132 				   "device=%02x.%04x reply=0x%02x apfs=0x%x => online=0 rc=EAGAIN\n",
133 				   card, queue, apfs, ehdr->reply_code);
134 		} else
135 			ZCRYPT_DBF(DBF_ERR,
136 				   "device=%02x.%04x reply=0x%02x => online=0 rc=EAGAIN\n",
137 				   card, queue, ehdr->reply_code);
138 		return -EAGAIN;
139 	case REP82_ERROR_MACHINE_FAILURE:
140 	//   REP88_ERROR_MODULE_FAILURE		// '10' CEX2A
141 		/* If a card fails disable it and repeat the request. */
142 		atomic_set(&zcrypt_rescan_req, 1);
143 		zq->online = 0;
144 		pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
145 		       card, queue);
146 		ZCRYPT_DBF(DBF_ERR,
147 			   "device=%02x.%04x reply=0x%02x => online=0 rc=EAGAIN\n",
148 			   card, queue, ehdr->reply_code);
149 		return -EAGAIN;
150 	default:
151 		zq->online = 0;
152 		pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
153 		       card, queue);
154 		ZCRYPT_DBF(DBF_ERR,
155 			   "device=%02x.%04x reply=0x%02x => online=0 rc=EAGAIN\n",
156 			   card, queue, ehdr->reply_code);
157 		return -EAGAIN;	/* repeat the request on a different device. */
158 	}
159 }
160 
161 #endif /* _ZCRYPT_ERROR_H_ */
162