xref: /openbmc/linux/drivers/s390/crypto/ap_bus.h (revision 560e20e4)
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 struct ap_message {
237 	struct list_head list;		/* Request queueing. */
238 	unsigned long psmid;		/* Message id. */
239 	void *msg;			/* Pointer to message buffer. */
240 	size_t len;			/* actual msg len in msg buffer */
241 	size_t bufsize;			/* allocated msg buffer size */
242 	u16 flags;			/* Flags, see AP_MSG_FLAG_xxx */
243 	int rc;				/* Return code for this message */
244 	void *private;			/* ap driver private pointer. */
245 	/* receive is called from tasklet context */
246 	void (*receive)(struct ap_queue *, struct ap_message *,
247 			struct ap_message *);
248 };
249 
250 #define AP_MSG_FLAG_SPECIAL  0x0001	/* flag msg as 'special' with NQAP */
251 #define AP_MSG_FLAG_USAGE    0x0002	/* CCA, EP11: usage (no admin) msg */
252 #define AP_MSG_FLAG_ADMIN    0x0004	/* CCA, EP11: admin (=control) msg */
253 
254 /**
255  * ap_init_message() - Initialize ap_message.
256  * Initialize a message before using. Otherwise this might result in
257  * unexpected behaviour.
258  */
259 static inline void ap_init_message(struct ap_message *ap_msg)
260 {
261 	memset(ap_msg, 0, sizeof(*ap_msg));
262 }
263 
264 /**
265  * ap_release_message() - Release ap_message.
266  * Releases all memory used internal within the ap_message struct
267  * Currently this is the message and private field.
268  */
269 static inline void ap_release_message(struct ap_message *ap_msg)
270 {
271 	kfree_sensitive(ap_msg->msg);
272 	kfree_sensitive(ap_msg->private);
273 }
274 
275 /*
276  * Note: don't use ap_send/ap_recv after using ap_queue_message
277  * for the first time. Otherwise the ap message queue will get
278  * confused.
279  */
280 int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen);
281 int ap_recv(ap_qid_t qid, unsigned long *psmid, void *msg, size_t msglen);
282 
283 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
284 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);
285 
286 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg);
287 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg);
288 void ap_flush_queue(struct ap_queue *aq);
289 
290 void *ap_airq_ptr(void);
291 int ap_sb_available(void);
292 void ap_wait(enum ap_sm_wait wait);
293 void ap_request_timeout(struct timer_list *t);
294 void ap_bus_force_rescan(void);
295 
296 int ap_test_config_usage_domain(unsigned int domain);
297 int ap_test_config_ctrl_domain(unsigned int domain);
298 
299 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *ap_msg);
300 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type);
301 void ap_queue_prepare_remove(struct ap_queue *aq);
302 void ap_queue_remove(struct ap_queue *aq);
303 void ap_queue_init_state(struct ap_queue *aq);
304 
305 struct ap_card *ap_card_create(int id, int queue_depth, int raw_type,
306 			       int comp_type, unsigned int functions, int ml);
307 
308 #define APMASKSIZE (BITS_TO_LONGS(AP_DEVICES) * sizeof(unsigned long))
309 #define AQMASKSIZE (BITS_TO_LONGS(AP_DOMAINS) * sizeof(unsigned long))
310 
311 struct ap_perms {
312 	unsigned long ioctlm[BITS_TO_LONGS(AP_IOCTLS)];
313 	unsigned long apm[BITS_TO_LONGS(AP_DEVICES)];
314 	unsigned long aqm[BITS_TO_LONGS(AP_DOMAINS)];
315 	unsigned long adm[BITS_TO_LONGS(AP_DOMAINS)];
316 };
317 
318 extern struct ap_perms ap_perms;
319 extern struct mutex ap_perms_mutex;
320 
321 /*
322  * Get ap_queue device for this qid.
323  * Returns ptr to the struct ap_queue device or NULL if there
324  * was no ap_queue device with this qid found. When something is
325  * found, the reference count of the embedded device is increased.
326  * So the caller has to decrease the reference count after use
327  * with a call to put_device(&aq->ap_dev.device).
328  */
329 struct ap_queue *ap_get_qdev(ap_qid_t qid);
330 
331 /*
332  * check APQN for owned/reserved by ap bus and default driver(s).
333  * Checks if this APQN is or will be in use by the ap bus
334  * and the default set of drivers.
335  * If yes, returns 1, if not returns 0. On error a negative
336  * errno value is returned.
337  */
338 int ap_owned_by_def_drv(int card, int queue);
339 
340 /*
341  * check 'matrix' of APQNs for owned/reserved by ap bus and
342  * default driver(s).
343  * Checks if there is at least one APQN in the given 'matrix'
344  * marked as owned/reserved by the ap bus and default driver(s).
345  * If such an APQN is found the return value is 1, otherwise
346  * 0 is returned. On error a negative errno value is returned.
347  * The parameter apm is a bitmask which should be declared
348  * as DECLARE_BITMAP(apm, AP_DEVICES), the aqm parameter is
349  * similar, should be declared as DECLARE_BITMAP(aqm, AP_DOMAINS).
350  */
351 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
352 				       unsigned long *aqm);
353 
354 /*
355  * ap_parse_mask_str() - helper function to parse a bitmap string
356  * and clear/set the bits in the bitmap accordingly. The string may be
357  * given as absolute value, a hex string like 0x1F2E3D4C5B6A" simple
358  * overwriting the current content of the bitmap. Or as relative string
359  * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
360  * bits are cleared or set. Distinction is done based on the very
361  * first character which may be '+' or '-' for the relative string
362  * and otherwise assume to be an absolute value string. If parsing fails
363  * a negative errno value is returned. All arguments and bitmaps are
364  * big endian order.
365  */
366 int ap_parse_mask_str(const char *str,
367 		      unsigned long *bitmap, int bits,
368 		      struct mutex *lock);
369 
370 /*
371  * Interface to wait for the AP bus to have done one initial ap bus
372  * scan and all detected APQNs have been bound to device drivers.
373  * If these both conditions are not fulfilled, this function blocks
374  * on a condition with wait_for_completion_killable_timeout().
375  * If these both conditions are fulfilled (before the timeout hits)
376  * the return value is 0. If the timeout (in jiffies) hits instead
377  * -ETIME is returned. On failures negative return values are
378  * returned to the caller.
379  */
380 int ap_wait_init_apqn_bindings_complete(unsigned long timeout);
381 
382 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg);
383 void ap_send_online_uevent(struct ap_device *ap_dev, int online);
384 
385 #endif /* _AP_BUS_H_ */
386