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