xref: /openbmc/linux/drivers/s390/crypto/ap_bus.h (revision 84b9b44b)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright IBM Corp. 2006, 2019
4  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
6  *	      Ralph Wuerthner <rwuerthn@de.ibm.com>
7  *	      Felix Beck <felix.beck@de.ibm.com>
8  *	      Holger Dengler <hd@linux.vnet.ibm.com>
9  *
10  * Adjunct processor bus header file.
11  */
12 
13 #ifndef _AP_BUS_H_
14 #define _AP_BUS_H_
15 
16 #include <linux/device.h>
17 #include <linux/types.h>
18 #include <linux/hashtable.h>
19 #include <asm/isc.h>
20 #include <asm/ap.h>
21 
22 #define AP_DEVICES 256		/* Number of AP devices. */
23 #define AP_DOMAINS 256		/* Number of AP domains. */
24 #define AP_IOCTLS  256		/* Number of ioctls. */
25 #define AP_RESET_TIMEOUT (HZ*0.7)	/* Time in ticks for reset timeouts. */
26 #define AP_CONFIG_TIME 30	/* Time in seconds between AP bus rescans. */
27 #define AP_POLL_TIME 1		/* Time in ticks between receive polls. */
28 #define AP_DEFAULT_MAX_MSG_SIZE (12 * 1024)
29 #define AP_TAPQ_ML_FIELD_CHUNK_SIZE (4096)
30 
31 extern int ap_domain_index;
32 extern atomic_t ap_max_msg_size;
33 
34 extern DECLARE_HASHTABLE(ap_queues, 8);
35 extern spinlock_t ap_queues_lock;
36 
37 static inline int ap_test_bit(unsigned int *ptr, unsigned int nr)
38 {
39 	return (*ptr & (0x80000000u >> nr)) != 0;
40 }
41 
42 #define AP_RESPONSE_NORMAL		     0x00
43 #define AP_RESPONSE_Q_NOT_AVAIL		     0x01
44 #define AP_RESPONSE_RESET_IN_PROGRESS	     0x02
45 #define AP_RESPONSE_DECONFIGURED	     0x03
46 #define AP_RESPONSE_CHECKSTOPPED	     0x04
47 #define AP_RESPONSE_BUSY		     0x05
48 #define AP_RESPONSE_INVALID_ADDRESS	     0x06
49 #define AP_RESPONSE_OTHERWISE_CHANGED	     0x07
50 #define AP_RESPONSE_INVALID_GISA	     0x08
51 #define AP_RESPONSE_Q_BOUND_TO_ANOTHER	     0x09
52 #define AP_RESPONSE_STATE_CHANGE_IN_PROGRESS 0x0A
53 #define AP_RESPONSE_Q_NOT_BOUND		     0x0B
54 #define AP_RESPONSE_Q_FULL		     0x10
55 #define AP_RESPONSE_NO_PENDING_REPLY	     0x10
56 #define AP_RESPONSE_INDEX_TOO_BIG	     0x11
57 #define AP_RESPONSE_NO_FIRST_PART	     0x13
58 #define AP_RESPONSE_MESSAGE_TOO_BIG	     0x15
59 #define AP_RESPONSE_REQ_FAC_NOT_INST	     0x16
60 #define AP_RESPONSE_Q_BIND_ERROR	     0x30
61 #define AP_RESPONSE_Q_NOT_AVAIL_FOR_ASSOC    0x31
62 #define AP_RESPONSE_Q_NOT_EMPTY		     0x32
63 #define AP_RESPONSE_BIND_LIMIT_EXCEEDED	     0x33
64 #define AP_RESPONSE_INVALID_ASSOC_SECRET     0x34
65 #define AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE  0x35
66 #define AP_RESPONSE_ASSOC_FAILED	     0x36
67 #define AP_RESPONSE_INVALID_DOMAIN	     0x42
68 
69 /*
70  * Known device types
71  */
72 #define AP_DEVICE_TYPE_PCICC	3
73 #define AP_DEVICE_TYPE_PCICA	4
74 #define AP_DEVICE_TYPE_PCIXCC	5
75 #define AP_DEVICE_TYPE_CEX2A	6
76 #define AP_DEVICE_TYPE_CEX2C	7
77 #define AP_DEVICE_TYPE_CEX3A	8
78 #define AP_DEVICE_TYPE_CEX3C	9
79 #define AP_DEVICE_TYPE_CEX4	10
80 #define AP_DEVICE_TYPE_CEX5	11
81 #define AP_DEVICE_TYPE_CEX6	12
82 #define AP_DEVICE_TYPE_CEX7	13
83 #define AP_DEVICE_TYPE_CEX8	14
84 
85 /*
86  * Known function facilities
87  */
88 #define AP_FUNC_MEX4K 1
89 #define AP_FUNC_CRT4K 2
90 #define AP_FUNC_COPRO 3
91 #define AP_FUNC_ACCEL 4
92 #define AP_FUNC_EP11  5
93 #define AP_FUNC_APXA  6
94 
95 /*
96  * AP queue state machine states
97  */
98 enum ap_sm_state {
99 	AP_SM_STATE_RESET_START = 0,
100 	AP_SM_STATE_RESET_WAIT,
101 	AP_SM_STATE_SETIRQ_WAIT,
102 	AP_SM_STATE_IDLE,
103 	AP_SM_STATE_WORKING,
104 	AP_SM_STATE_QUEUE_FULL,
105 	AP_SM_STATE_ASSOC_WAIT,
106 	NR_AP_SM_STATES
107 };
108 
109 /*
110  * AP queue state machine events
111  */
112 enum ap_sm_event {
113 	AP_SM_EVENT_POLL,
114 	AP_SM_EVENT_TIMEOUT,
115 	NR_AP_SM_EVENTS
116 };
117 
118 /*
119  * AP queue state wait behaviour
120  */
121 enum ap_sm_wait {
122 	AP_SM_WAIT_AGAIN = 0,	 /* retry immediately */
123 	AP_SM_WAIT_HIGH_TIMEOUT, /* poll high freq, wait for timeout */
124 	AP_SM_WAIT_LOW_TIMEOUT,	 /* poll low freq, wait for timeout */
125 	AP_SM_WAIT_INTERRUPT,	 /* wait for thin interrupt (if available) */
126 	AP_SM_WAIT_NONE,	 /* no wait */
127 	NR_AP_SM_WAIT
128 };
129 
130 /*
131  * AP queue device states
132  */
133 enum ap_dev_state {
134 	AP_DEV_STATE_UNINITIATED = 0,	/* fresh and virgin, not touched */
135 	AP_DEV_STATE_OPERATING,		/* queue dev is working normal */
136 	AP_DEV_STATE_SHUTDOWN,		/* remove/unbind/shutdown in progress */
137 	AP_DEV_STATE_ERROR,		/* device is in error state */
138 	NR_AP_DEV_STATES
139 };
140 
141 struct ap_device;
142 struct ap_message;
143 
144 /*
145  * The ap driver struct includes a flags field which holds some info for
146  * the ap bus about the driver. Currently only one flag is supported and
147  * used: The DEFAULT flag marks an ap driver as a default driver which is
148  * used together with the apmask and aqmask whitelisting of the ap bus.
149  */
150 #define AP_DRIVER_FLAG_DEFAULT 0x0001
151 
152 struct ap_driver {
153 	struct device_driver driver;
154 	struct ap_device_id *ids;
155 	unsigned int flags;
156 
157 	int (*probe)(struct ap_device *);
158 	void (*remove)(struct ap_device *);
159 	int (*in_use)(unsigned long *apm, unsigned long *aqm);
160 	/*
161 	 * Called at the start of the ap bus scan function when
162 	 * the crypto config information (qci) has changed.
163 	 * This callback is not invoked if there is no AP
164 	 * QCI support available.
165 	 */
166 	void (*on_config_changed)(struct ap_config_info *new_config_info,
167 				  struct ap_config_info *old_config_info);
168 	/*
169 	 * Called at the end of the ap bus scan function when
170 	 * the crypto config information (qci) has changed.
171 	 * This callback is not invoked if there is no AP
172 	 * QCI support available.
173 	 */
174 	void (*on_scan_complete)(struct ap_config_info *new_config_info,
175 				 struct ap_config_info *old_config_info);
176 };
177 
178 #define to_ap_drv(x) container_of((x), struct ap_driver, driver)
179 
180 int ap_driver_register(struct ap_driver *, struct module *, char *);
181 void ap_driver_unregister(struct ap_driver *);
182 
183 struct ap_device {
184 	struct device device;
185 	int device_type;		/* AP device type. */
186 };
187 
188 #define to_ap_dev(x) container_of((x), struct ap_device, device)
189 
190 struct ap_card {
191 	struct ap_device ap_dev;
192 	int raw_hwtype;			/* AP raw hardware type. */
193 	unsigned int functions;		/* TAPQ GR2 upper 32 facility bits */
194 	int queue_depth;		/* AP queue depth.*/
195 	int id;				/* AP card number. */
196 	unsigned int maxmsgsize;	/* AP msg limit for this card */
197 	bool config;			/* configured state */
198 	bool chkstop;			/* checkstop state */
199 	atomic64_t total_request_count;	/* # requests ever for this AP device.*/
200 };
201 
202 #define TAPQ_CARD_FUNC_CMP_MASK 0xFFFF0000
203 #define ASSOC_IDX_INVALID 0x10000
204 
205 #define to_ap_card(x) container_of((x), struct ap_card, ap_dev.device)
206 
207 struct ap_queue {
208 	struct ap_device ap_dev;
209 	struct hlist_node hnode;	/* Node for the ap_queues hashtable */
210 	struct ap_card *card;		/* Ptr to assoc. AP card. */
211 	spinlock_t lock;		/* Per device lock. */
212 	enum ap_dev_state dev_state;	/* queue device state */
213 	bool config;			/* configured state */
214 	bool chkstop;			/* checkstop state */
215 	ap_qid_t qid;			/* AP queue id. */
216 	bool interrupt;			/* indicate if interrupts are enabled */
217 	unsigned int assoc_idx;		/* SE association index */
218 	int queue_count;		/* # messages currently on AP queue. */
219 	int pendingq_count;		/* # requests on pendingq list. */
220 	int requestq_count;		/* # requests on requestq list. */
221 	u64 total_request_count;	/* # requests ever for this AP device.*/
222 	int request_timeout;		/* Request timeout in jiffies. */
223 	struct timer_list timeout;	/* Timer for request timeouts. */
224 	struct list_head pendingq;	/* List of message sent to AP queue. */
225 	struct list_head requestq;	/* List of message yet to be sent. */
226 	struct ap_message *reply;	/* Per device reply message. */
227 	enum ap_sm_state sm_state;	/* ap queue state machine state */
228 	int rapq_fbit;			/* fbit arg for next rapq invocation */
229 	int last_err_rc;		/* last error state response code */
230 };
231 
232 #define to_ap_queue(x) container_of((x), struct ap_queue, ap_dev.device)
233 
234 typedef enum ap_sm_wait (ap_func_t)(struct ap_queue *queue);
235 
236 /* failure injection cmd struct */
237 struct ap_fi {
238 	union {
239 		u16 cmd;		/* fi flags + action */
240 		struct {
241 			u8 flags;	/* fi flags only */
242 			u8 action;	/* fi action only */
243 		};
244 	};
245 };
246 
247 /* all currently known fi actions */
248 enum ap_fi_actions {
249 	AP_FI_ACTION_CCA_AGENT_FF   = 0x01,
250 	AP_FI_ACTION_CCA_DOM_INVAL  = 0x02,
251 	AP_FI_ACTION_NQAP_QID_INVAL = 0x03,
252 };
253 
254 /* all currently known fi flags */
255 enum ap_fi_flags {
256 	AP_FI_FLAG_NO_RETRY	  = 0x01,
257 	AP_FI_FLAG_TOGGLE_SPECIAL = 0x02,
258 };
259 
260 struct ap_message {
261 	struct list_head list;		/* Request queueing. */
262 	unsigned long psmid;		/* Message id. */
263 	void *msg;			/* Pointer to message buffer. */
264 	size_t len;			/* actual msg len in msg buffer */
265 	size_t bufsize;			/* allocated msg buffer size */
266 	u16 flags;			/* Flags, see AP_MSG_FLAG_xxx */
267 	struct ap_fi fi;		/* Failure Injection cmd */
268 	int rc;				/* Return code for this message */
269 	void *private;			/* ap driver private pointer. */
270 	/* receive is called from tasklet context */
271 	void (*receive)(struct ap_queue *, struct ap_message *,
272 			struct ap_message *);
273 };
274 
275 #define AP_MSG_FLAG_SPECIAL  0x0001	/* flag msg as 'special' with NQAP */
276 #define AP_MSG_FLAG_USAGE    0x0002	/* CCA, EP11: usage (no admin) msg */
277 #define AP_MSG_FLAG_ADMIN    0x0004	/* CCA, EP11: admin (=control) msg */
278 
279 /**
280  * ap_init_message() - Initialize ap_message.
281  * Initialize a message before using. Otherwise this might result in
282  * unexpected behaviour.
283  */
284 static inline void ap_init_message(struct ap_message *ap_msg)
285 {
286 	memset(ap_msg, 0, sizeof(*ap_msg));
287 }
288 
289 /**
290  * ap_release_message() - Release ap_message.
291  * Releases all memory used internal within the ap_message struct
292  * Currently this is the message and private field.
293  */
294 static inline void ap_release_message(struct ap_message *ap_msg)
295 {
296 	kfree_sensitive(ap_msg->msg);
297 	kfree_sensitive(ap_msg->private);
298 }
299 
300 /*
301  * Note: don't use ap_send/ap_recv after using ap_queue_message
302  * for the first time. Otherwise the ap message queue will get
303  * confused.
304  */
305 int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen);
306 int ap_recv(ap_qid_t qid, unsigned long *psmid, void *msg, size_t msglen);
307 
308 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
309 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);
310 
311 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg);
312 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg);
313 void ap_flush_queue(struct ap_queue *aq);
314 
315 void *ap_airq_ptr(void);
316 int ap_sb_available(void);
317 void ap_wait(enum ap_sm_wait wait);
318 void ap_request_timeout(struct timer_list *t);
319 void ap_bus_force_rescan(void);
320 
321 int ap_test_config_usage_domain(unsigned int domain);
322 int ap_test_config_ctrl_domain(unsigned int domain);
323 
324 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *ap_msg);
325 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type);
326 void ap_queue_prepare_remove(struct ap_queue *aq);
327 void ap_queue_remove(struct ap_queue *aq);
328 void ap_queue_init_state(struct ap_queue *aq);
329 
330 struct ap_card *ap_card_create(int id, int queue_depth, int raw_type,
331 			       int comp_type, unsigned int functions, int ml);
332 
333 #define APMASKSIZE (BITS_TO_LONGS(AP_DEVICES) * sizeof(unsigned long))
334 #define AQMASKSIZE (BITS_TO_LONGS(AP_DOMAINS) * sizeof(unsigned long))
335 
336 struct ap_perms {
337 	unsigned long ioctlm[BITS_TO_LONGS(AP_IOCTLS)];
338 	unsigned long apm[BITS_TO_LONGS(AP_DEVICES)];
339 	unsigned long aqm[BITS_TO_LONGS(AP_DOMAINS)];
340 	unsigned long adm[BITS_TO_LONGS(AP_DOMAINS)];
341 };
342 
343 extern struct ap_perms ap_perms;
344 extern struct mutex ap_perms_mutex;
345 
346 /*
347  * Get ap_queue device for this qid.
348  * Returns ptr to the struct ap_queue device or NULL if there
349  * was no ap_queue device with this qid found. When something is
350  * found, the reference count of the embedded device is increased.
351  * So the caller has to decrease the reference count after use
352  * with a call to put_device(&aq->ap_dev.device).
353  */
354 struct ap_queue *ap_get_qdev(ap_qid_t qid);
355 
356 /*
357  * check APQN for owned/reserved by ap bus and default driver(s).
358  * Checks if this APQN is or will be in use by the ap bus
359  * and the default set of drivers.
360  * If yes, returns 1, if not returns 0. On error a negative
361  * errno value is returned.
362  */
363 int ap_owned_by_def_drv(int card, int queue);
364 
365 /*
366  * check 'matrix' of APQNs for owned/reserved by ap bus and
367  * default driver(s).
368  * Checks if there is at least one APQN in the given 'matrix'
369  * marked as owned/reserved by the ap bus and default driver(s).
370  * If such an APQN is found the return value is 1, otherwise
371  * 0 is returned. On error a negative errno value is returned.
372  * The parameter apm is a bitmask which should be declared
373  * as DECLARE_BITMAP(apm, AP_DEVICES), the aqm parameter is
374  * similar, should be declared as DECLARE_BITMAP(aqm, AP_DOMAINS).
375  */
376 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
377 				       unsigned long *aqm);
378 
379 /*
380  * ap_parse_mask_str() - helper function to parse a bitmap string
381  * and clear/set the bits in the bitmap accordingly. The string may be
382  * given as absolute value, a hex string like 0x1F2E3D4C5B6A" simple
383  * overwriting the current content of the bitmap. Or as relative string
384  * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
385  * bits are cleared or set. Distinction is done based on the very
386  * first character which may be '+' or '-' for the relative string
387  * and othewise assume to be an absolute value string. If parsing fails
388  * a negative errno value is returned. All arguments and bitmaps are
389  * big endian order.
390  */
391 int ap_parse_mask_str(const char *str,
392 		      unsigned long *bitmap, int bits,
393 		      struct mutex *lock);
394 
395 /*
396  * Interface to wait for the AP bus to have done one initial ap bus
397  * scan and all detected APQNs have been bound to device drivers.
398  * If these both conditions are not fulfilled, this function blocks
399  * on a condition with wait_for_completion_killable_timeout().
400  * If these both conditions are fulfilled (before the timeout hits)
401  * the return value is 0. If the timeout (in jiffies) hits instead
402  * -ETIME is returned. On failures negative return values are
403  * returned to the caller.
404  */
405 int ap_wait_init_apqn_bindings_complete(unsigned long timeout);
406 
407 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg);
408 void ap_send_online_uevent(struct ap_device *ap_dev, int online);
409 
410 #endif /* _AP_BUS_H_ */
411