1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright IBM Corp. 2006, 2023
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
31extern int ap_domain_index;
32extern atomic_t ap_max_msg_size;
33
34extern DECLARE_HASHTABLE(ap_queues, 8);
35extern spinlock_t ap_queues_lock;
36
37static 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 * Supported AP device types
71 */
72#define AP_DEVICE_TYPE_CEX4 10
73#define AP_DEVICE_TYPE_CEX5 11
74#define AP_DEVICE_TYPE_CEX6 12
75#define AP_DEVICE_TYPE_CEX7 13
76#define AP_DEVICE_TYPE_CEX8 14
77
78/*
79 * Known function facilities
80 */
81#define AP_FUNC_MEX4K 1
82#define AP_FUNC_CRT4K 2
83#define AP_FUNC_COPRO 3
84#define AP_FUNC_ACCEL 4
85#define AP_FUNC_EP11 5
86#define AP_FUNC_APXA 6
87
88/*
89 * AP queue state machine states
90 */
91enum ap_sm_state {
92 AP_SM_STATE_RESET_START = 0,
93 AP_SM_STATE_RESET_WAIT,
94 AP_SM_STATE_SETIRQ_WAIT,
95 AP_SM_STATE_IDLE,
96 AP_SM_STATE_WORKING,
97 AP_SM_STATE_QUEUE_FULL,
98 AP_SM_STATE_ASSOC_WAIT,
99 NR_AP_SM_STATES
100};
101
102/*
103 * AP queue state machine events
104 */
105enum ap_sm_event {
106 AP_SM_EVENT_POLL,
107 AP_SM_EVENT_TIMEOUT,
108 NR_AP_SM_EVENTS
109};
110
111/*
112 * AP queue state wait behaviour
113 */
114enum ap_sm_wait {
115 AP_SM_WAIT_AGAIN = 0, /* retry immediately */
116 AP_SM_WAIT_HIGH_TIMEOUT, /* poll high freq, wait for timeout */
117 AP_SM_WAIT_LOW_TIMEOUT, /* poll low freq, wait for timeout */
118 AP_SM_WAIT_INTERRUPT, /* wait for thin interrupt (if available) */
119 AP_SM_WAIT_NONE, /* no wait */
120 NR_AP_SM_WAIT
121};
122
123/*
124 * AP queue device states
125 */
126enum ap_dev_state {
127 AP_DEV_STATE_UNINITIATED = 0, /* fresh and virgin, not touched */
128 AP_DEV_STATE_OPERATING, /* queue dev is working normal */
129 AP_DEV_STATE_SHUTDOWN, /* remove/unbind/shutdown in progress */
130 AP_DEV_STATE_ERROR, /* device is in error state */
131 NR_AP_DEV_STATES
132};
133
134struct ap_device;
135struct ap_message;
136
137/*
138 * The ap driver struct includes a flags field which holds some info for
139 * the ap bus about the driver. Currently only one flag is supported and
140 * used: The DEFAULT flag marks an ap driver as a default driver which is
141 * used together with the apmask and aqmask whitelisting of the ap bus.
142 */
143#define AP_DRIVER_FLAG_DEFAULT 0x0001
144
145struct ap_driver {
146 struct device_driver driver;
147 struct ap_device_id *ids;
148 unsigned int flags;
149
150 int (*probe)(struct ap_device *);
151 void (*remove)(struct ap_device *);
152 int (*in_use)(unsigned long *apm, unsigned long *aqm);
153 /*
154 * Called at the start of the ap bus scan function when
155 * the crypto config information (qci) has changed.
156 * This callback is not invoked if there is no AP
157 * QCI support available.
158 */
159 void (*on_config_changed)(struct ap_config_info *new_config_info,
160 struct ap_config_info *old_config_info);
161 /*
162 * Called at the end of the ap bus scan function when
163 * the crypto config information (qci) has changed.
164 * This callback is not invoked if there is no AP
165 * QCI support available.
166 */
167 void (*on_scan_complete)(struct ap_config_info *new_config_info,
168 struct ap_config_info *old_config_info);
169};
170
171#define to_ap_drv(x) container_of((x), struct ap_driver, driver)
172
173int ap_driver_register(struct ap_driver *, struct module *, char *);
174void ap_driver_unregister(struct ap_driver *);
175
176struct ap_device {
177 struct device device;
178 int device_type; /* AP device type. */
179};
180
181#define to_ap_dev(x) container_of((x), struct ap_device, device)
182
183struct ap_card {
184 struct ap_device ap_dev;
185 int raw_hwtype; /* AP raw hardware type. */
186 unsigned int functions; /* TAPQ GR2 upper 32 facility bits */
187 int queue_depth; /* AP queue depth.*/
188 int id; /* AP card number. */
189 unsigned int maxmsgsize; /* AP msg limit for this card */
190 bool config; /* configured state */
191 bool chkstop; /* checkstop state */
192 atomic64_t total_request_count; /* # requests ever for this AP device.*/
193};
194
195#define TAPQ_CARD_FUNC_CMP_MASK 0xFFFF0000
196#define ASSOC_IDX_INVALID 0x10000
197
198#define to_ap_card(x) container_of((x), struct ap_card, ap_dev.device)
199
200struct ap_queue {
201 struct ap_device ap_dev;
202 struct hlist_node hnode; /* Node for the ap_queues hashtable */
203 struct ap_card *card; /* Ptr to assoc. AP card. */
204 spinlock_t lock; /* Per device lock. */
205 enum ap_dev_state dev_state; /* queue device state */
206 bool config; /* configured state */
207 bool chkstop; /* checkstop state */
208 ap_qid_t qid; /* AP queue id. */
209 bool se_bound; /* SE bound state */
210 unsigned int assoc_idx; /* SE association index */
211 int queue_count; /* # messages currently on AP queue. */
212 int pendingq_count; /* # requests on pendingq list. */
213 int requestq_count; /* # requests on requestq list. */
214 u64 total_request_count; /* # requests ever for this AP device.*/
215 int request_timeout; /* Request timeout in jiffies. */
216 struct timer_list timeout; /* Timer for request timeouts. */
217 struct list_head pendingq; /* List of message sent to AP queue. */
218 struct list_head requestq; /* List of message yet to be sent. */
219 struct ap_message *reply; /* Per device reply message. */
220 enum ap_sm_state sm_state; /* ap queue state machine state */
221 int rapq_fbit; /* fbit arg for next rapq invocation */
222 int last_err_rc; /* last error state response code */
223};
224
225#define to_ap_queue(x) container_of((x), struct ap_queue, ap_dev.device)
226
227typedef enum ap_sm_wait (ap_func_t)(struct ap_queue *queue);
228
229struct ap_message {
230 struct list_head list; /* Request queueing. */
231 unsigned long psmid; /* Message id. */
232 void *msg; /* Pointer to message buffer. */
233 size_t len; /* actual msg len in msg buffer */
234 size_t bufsize; /* allocated msg buffer size */
235 u16 flags; /* Flags, see AP_MSG_FLAG_xxx */
236 int rc; /* Return code for this message */
237 void *private; /* ap driver private pointer. */
238 /* receive is called from tasklet context */
239 void (*receive)(struct ap_queue *, struct ap_message *,
240 struct ap_message *);
241};
242
243#define AP_MSG_FLAG_SPECIAL 0x0001 /* flag msg as 'special' with NQAP */
244#define AP_MSG_FLAG_USAGE 0x0002 /* CCA, EP11: usage (no admin) msg */
245#define AP_MSG_FLAG_ADMIN 0x0004 /* CCA, EP11: admin (=control) msg */
246
247/**
248 * ap_init_message() - Initialize ap_message.
249 * Initialize a message before using. Otherwise this might result in
250 * unexpected behaviour.
251 */
252static inline void ap_init_message(struct ap_message *ap_msg)
253{
254 memset(ap_msg, 0, sizeof(*ap_msg));
255}
256
257/**
258 * ap_release_message() - Release ap_message.
259 * Releases all memory used internal within the ap_message struct
260 * Currently this is the message and private field.
261 */
262static inline void ap_release_message(struct ap_message *ap_msg)
263{
264 kfree_sensitive(objp: ap_msg->msg);
265 kfree_sensitive(objp: ap_msg->private);
266}
267
268enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
269enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);
270
271int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg);
272void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg);
273void ap_flush_queue(struct ap_queue *aq);
274bool ap_queue_usable(struct ap_queue *aq);
275
276void *ap_airq_ptr(void);
277int ap_sb_available(void);
278bool ap_is_se_guest(void);
279void ap_wait(enum ap_sm_wait wait);
280void ap_request_timeout(struct timer_list *t);
281void ap_bus_force_rescan(void);
282
283int ap_test_config_usage_domain(unsigned int domain);
284int ap_test_config_ctrl_domain(unsigned int domain);
285
286void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *ap_msg);
287struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type);
288void ap_queue_prepare_remove(struct ap_queue *aq);
289void ap_queue_remove(struct ap_queue *aq);
290void ap_queue_init_state(struct ap_queue *aq);
291void _ap_queue_init_state(struct ap_queue *aq);
292
293struct ap_card *ap_card_create(int id, int queue_depth, int raw_type,
294 int comp_type, unsigned int functions, int ml);
295
296#define APMASKSIZE (BITS_TO_LONGS(AP_DEVICES) * sizeof(unsigned long))
297#define AQMASKSIZE (BITS_TO_LONGS(AP_DOMAINS) * sizeof(unsigned long))
298
299struct ap_perms {
300 unsigned long ioctlm[BITS_TO_LONGS(AP_IOCTLS)];
301 unsigned long apm[BITS_TO_LONGS(AP_DEVICES)];
302 unsigned long aqm[BITS_TO_LONGS(AP_DOMAINS)];
303 unsigned long adm[BITS_TO_LONGS(AP_DOMAINS)];
304};
305
306extern struct ap_perms ap_perms;
307extern struct mutex ap_perms_mutex;
308
309/*
310 * Get ap_queue device for this qid.
311 * Returns ptr to the struct ap_queue device or NULL if there
312 * was no ap_queue device with this qid found. When something is
313 * found, the reference count of the embedded device is increased.
314 * So the caller has to decrease the reference count after use
315 * with a call to put_device(&aq->ap_dev.device).
316 */
317struct ap_queue *ap_get_qdev(ap_qid_t qid);
318
319/*
320 * check APQN for owned/reserved by ap bus and default driver(s).
321 * Checks if this APQN is or will be in use by the ap bus
322 * and the default set of drivers.
323 * If yes, returns 1, if not returns 0. On error a negative
324 * errno value is returned.
325 */
326int ap_owned_by_def_drv(int card, int queue);
327
328/*
329 * check 'matrix' of APQNs for owned/reserved by ap bus and
330 * default driver(s).
331 * Checks if there is at least one APQN in the given 'matrix'
332 * marked as owned/reserved by the ap bus and default driver(s).
333 * If such an APQN is found the return value is 1, otherwise
334 * 0 is returned. On error a negative errno value is returned.
335 * The parameter apm is a bitmask which should be declared
336 * as DECLARE_BITMAP(apm, AP_DEVICES), the aqm parameter is
337 * similar, should be declared as DECLARE_BITMAP(aqm, AP_DOMAINS).
338 */
339int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
340 unsigned long *aqm);
341
342/*
343 * ap_parse_mask_str() - helper function to parse a bitmap string
344 * and clear/set the bits in the bitmap accordingly. The string may be
345 * given as absolute value, a hex string like 0x1F2E3D4C5B6A" simple
346 * overwriting the current content of the bitmap. Or as relative string
347 * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
348 * bits are cleared or set. Distinction is done based on the very
349 * first character which may be '+' or '-' for the relative string
350 * and otherwise assume to be an absolute value string. If parsing fails
351 * a negative errno value is returned. All arguments and bitmaps are
352 * big endian order.
353 */
354int ap_parse_mask_str(const char *str,
355 unsigned long *bitmap, int bits,
356 struct mutex *lock);
357
358/*
359 * Interface to wait for the AP bus to have done one initial ap bus
360 * scan and all detected APQNs have been bound to device drivers.
361 * If these both conditions are not fulfilled, this function blocks
362 * on a condition with wait_for_completion_killable_timeout().
363 * If these both conditions are fulfilled (before the timeout hits)
364 * the return value is 0. If the timeout (in jiffies) hits instead
365 * -ETIME is returned. On failures negative return values are
366 * returned to the caller.
367 */
368int ap_wait_init_apqn_bindings_complete(unsigned long timeout);
369
370void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg);
371void ap_send_online_uevent(struct ap_device *ap_dev, int online);
372
373#endif /* _AP_BUS_H_ */
374

source code of linux/drivers/s390/crypto/ap_bus.h