xref: /linux/drivers/s390/crypto/vfio_ap_ops.c (revision 0c8ea05e9b3d8e5287e2a968f2a2e744dfd31b99)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Adjunct processor matrix VFIO device driver callbacks.
4  *
5  * Copyright IBM Corp. 2018
6  *
7  * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8  *	      Halil Pasic <pasic@linux.ibm.com>
9  *	      Pierre Morel <pmorel@linux.ibm.com>
10  */
11 #include <linux/string.h>
12 #include <linux/vfio.h>
13 #include <linux/device.h>
14 #include <linux/list.h>
15 #include <linux/ctype.h>
16 #include <linux/bitops.h>
17 #include <linux/kvm_host.h>
18 #include <linux/module.h>
19 #include <linux/uuid.h>
20 #include <asm/kvm.h>
21 #include <asm/zcrypt.h>
22 
23 #include "vfio_ap_private.h"
24 #include "vfio_ap_debug.h"
25 
26 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
27 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
28 
29 #define AP_QUEUE_ASSIGNED "assigned"
30 #define AP_QUEUE_UNASSIGNED "unassigned"
31 #define AP_QUEUE_IN_USE "in use"
32 
33 #define AP_RESET_INTERVAL		20	/* Reset sleep interval (20ms)		*/
34 
35 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
36 static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);
37 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
38 static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
39 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
40 
41 /**
42  * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
43  *			     KVM guest's APCB in the proper order.
44  *
45  * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
46  *
47  * The proper locking order is:
48  * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
49  *			       guest's APCB.
50  * 2. kvm->lock:	       required to update a guest's APCB
51  * 3. matrix_dev->mdevs_lock:  required to access data stored in a matrix_mdev
52  *
53  * Note: If @kvm is NULL, the KVM lock will not be taken.
54  */
55 static inline void get_update_locks_for_kvm(struct kvm *kvm)
56 {
57 	mutex_lock(&matrix_dev->guests_lock);
58 	if (kvm)
59 		mutex_lock(&kvm->lock);
60 	mutex_lock(&matrix_dev->mdevs_lock);
61 }
62 
63 /**
64  * release_update_locks_for_kvm: Release the locks used to dynamically update a
65  *				 KVM guest's APCB in the proper order.
66  *
67  * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
68  *
69  * The proper unlocking order is:
70  * 1. matrix_dev->mdevs_lock
71  * 2. kvm->lock
72  * 3. matrix_dev->guests_lock
73  *
74  * Note: If @kvm is NULL, the KVM lock will not be released.
75  */
76 static inline void release_update_locks_for_kvm(struct kvm *kvm)
77 {
78 	mutex_unlock(&matrix_dev->mdevs_lock);
79 	if (kvm)
80 		mutex_unlock(&kvm->lock);
81 	mutex_unlock(&matrix_dev->guests_lock);
82 }
83 
84 /**
85  * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
86  *			      KVM guest's APCB in the proper order.
87  *
88  * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
89  *		 configuration data to use to update a KVM guest's APCB.
90  *
91  * The proper locking order is:
92  * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
93  *			       guest's APCB.
94  * 2. matrix_mdev->kvm->lock:  required to update a guest's APCB
95  * 3. matrix_dev->mdevs_lock:  required to access data stored in a matrix_mdev
96  *
97  * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
98  *	 lock will not be taken.
99  */
100 static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
101 {
102 	mutex_lock(&matrix_dev->guests_lock);
103 	if (matrix_mdev && matrix_mdev->kvm)
104 		mutex_lock(&matrix_mdev->kvm->lock);
105 	mutex_lock(&matrix_dev->mdevs_lock);
106 }
107 
108 /**
109  * release_update_locks_for_mdev: Release the locks used to dynamically update a
110  *				  KVM guest's APCB in the proper order.
111  *
112  * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
113  *		 configuration data to use to update a KVM guest's APCB.
114  *
115  * The proper unlocking order is:
116  * 1. matrix_dev->mdevs_lock
117  * 2. matrix_mdev->kvm->lock
118  * 3. matrix_dev->guests_lock
119  *
120  * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
121  *	 lock will not be released.
122  */
123 static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
124 {
125 	mutex_unlock(&matrix_dev->mdevs_lock);
126 	if (matrix_mdev && matrix_mdev->kvm)
127 		mutex_unlock(&matrix_mdev->kvm->lock);
128 	mutex_unlock(&matrix_dev->guests_lock);
129 }
130 
131 /**
132  * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
133  *			     acquire the locks required to update the APCB of
134  *			     the KVM guest to which the mdev is attached.
135  *
136  * @apqn: the APQN of a queue device.
137  *
138  * The proper locking order is:
139  * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
140  *			       guest's APCB.
141  * 2. matrix_mdev->kvm->lock:  required to update a guest's APCB
142  * 3. matrix_dev->mdevs_lock:  required to access data stored in a matrix_mdev
143  *
144  * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
145  *	 will not be taken.
146  *
147  * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
148  *	   is not assigned to an ap_matrix_mdev.
149  */
150 static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
151 {
152 	struct ap_matrix_mdev *matrix_mdev;
153 
154 	mutex_lock(&matrix_dev->guests_lock);
155 
156 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
157 		if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
158 		    test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
159 			if (matrix_mdev->kvm)
160 				mutex_lock(&matrix_mdev->kvm->lock);
161 
162 			mutex_lock(&matrix_dev->mdevs_lock);
163 
164 			return matrix_mdev;
165 		}
166 	}
167 
168 	mutex_lock(&matrix_dev->mdevs_lock);
169 
170 	return NULL;
171 }
172 
173 /**
174  * get_update_locks_for_queue: get the locks required to update the APCB of the
175  *			       KVM guest to which the matrix mdev linked to a
176  *			       vfio_ap_queue object is attached.
177  *
178  * @q: a pointer to a vfio_ap_queue object.
179  *
180  * The proper locking order is:
181  * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
182  *				  KVM guest's APCB.
183  * 2. q->matrix_mdev->kvm->lock:  required to update a guest's APCB
184  * 3. matrix_dev->mdevs_lock:	  required to access data stored in matrix_mdev
185  *
186  * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
187  *	  will not be taken.
188  */
189 static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
190 {
191 	mutex_lock(&matrix_dev->guests_lock);
192 	if (q->matrix_mdev && q->matrix_mdev->kvm)
193 		mutex_lock(&q->matrix_mdev->kvm->lock);
194 	mutex_lock(&matrix_dev->mdevs_lock);
195 }
196 
197 /**
198  * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
199  *			    hash table of queues assigned to a matrix mdev
200  * @matrix_mdev: the matrix mdev
201  * @apqn: The APQN of a queue device
202  *
203  * Return: the pointer to the vfio_ap_queue struct representing the queue or
204  *	   NULL if the queue is not assigned to @matrix_mdev
205  */
206 static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
207 					struct ap_matrix_mdev *matrix_mdev,
208 					int apqn)
209 {
210 	struct vfio_ap_queue *q;
211 
212 	hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
213 			       apqn) {
214 		if (q && q->apqn == apqn)
215 			return q;
216 	}
217 
218 	return NULL;
219 }
220 
221 /**
222  * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
223  * @apqn: The AP Queue number
224  *
225  * Checks the IRQ bit for the status of this APQN using ap_tapq.
226  * Returns if the ap_tapq function succeeded and the bit is clear.
227  * Returns if ap_tapq function failed with invalid, deconfigured or
228  * checkstopped AP.
229  * Otherwise retries up to 5 times after waiting 20ms.
230  */
231 static void vfio_ap_wait_for_irqclear(int apqn)
232 {
233 	struct ap_queue_status status;
234 	int retry = 5;
235 
236 	do {
237 		status = ap_tapq(apqn, NULL);
238 		switch (status.response_code) {
239 		case AP_RESPONSE_NORMAL:
240 		case AP_RESPONSE_RESET_IN_PROGRESS:
241 			if (!status.irq_enabled)
242 				return;
243 			fallthrough;
244 		case AP_RESPONSE_BUSY:
245 			msleep(20);
246 			break;
247 		case AP_RESPONSE_Q_NOT_AVAIL:
248 		case AP_RESPONSE_DECONFIGURED:
249 		case AP_RESPONSE_CHECKSTOPPED:
250 		default:
251 			WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
252 				  status.response_code, apqn);
253 			return;
254 		}
255 	} while (--retry);
256 
257 	WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
258 		  __func__, status.response_code, apqn);
259 }
260 
261 /**
262  * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
263  * @q: The vfio_ap_queue
264  *
265  * Unregisters the ISC in the GIB when the saved ISC not invalid.
266  * Unpins the guest's page holding the NIB when it exists.
267  * Resets the saved_iova and saved_isc to invalid values.
268  */
269 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
270 {
271 	if (!q)
272 		return;
273 	if (q->saved_isc != VFIO_AP_ISC_INVALID &&
274 	    !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
275 		kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
276 		q->saved_isc = VFIO_AP_ISC_INVALID;
277 	}
278 	if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
279 		vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
280 		q->saved_iova = 0;
281 	}
282 }
283 
284 /**
285  * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
286  * @q: The vfio_ap_queue
287  *
288  * Uses ap_aqic to disable the interruption and in case of success, reset
289  * in progress or IRQ disable command already proceeded: calls
290  * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
291  * and calls vfio_ap_free_aqic_resources() to free the resources associated
292  * with the AP interrupt handling.
293  *
294  * In the case the AP is busy, or a reset is in progress,
295  * retries after 20ms, up to 5 times.
296  *
297  * Returns if ap_aqic function failed with invalid, deconfigured or
298  * checkstopped AP.
299  *
300  * Return: &struct ap_queue_status
301  */
302 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
303 {
304 	union ap_qirq_ctrl aqic_gisa = { .value = 0 };
305 	struct ap_queue_status status;
306 	int retries = 5;
307 
308 	do {
309 		status = ap_aqic(q->apqn, aqic_gisa, 0);
310 		switch (status.response_code) {
311 		case AP_RESPONSE_OTHERWISE_CHANGED:
312 		case AP_RESPONSE_NORMAL:
313 			vfio_ap_wait_for_irqclear(q->apqn);
314 			goto end_free;
315 		case AP_RESPONSE_RESET_IN_PROGRESS:
316 		case AP_RESPONSE_BUSY:
317 			msleep(20);
318 			break;
319 		case AP_RESPONSE_Q_NOT_AVAIL:
320 		case AP_RESPONSE_DECONFIGURED:
321 		case AP_RESPONSE_CHECKSTOPPED:
322 		case AP_RESPONSE_INVALID_ADDRESS:
323 		default:
324 			/* All cases in default means AP not operational */
325 			WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
326 				  status.response_code);
327 			goto end_free;
328 		}
329 	} while (retries--);
330 
331 	WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
332 		  status.response_code);
333 end_free:
334 	vfio_ap_free_aqic_resources(q);
335 	return status;
336 }
337 
338 /**
339  * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
340  *
341  * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
342  * @nib: the location for storing the nib address.
343  *
344  * When the PQAP(AQIC) instruction is executed, general register 2 contains the
345  * address of the notification indicator byte (nib) used for IRQ notification.
346  * This function parses and validates the nib from gr2.
347  *
348  * Return: returns zero if the nib address is a valid; otherwise, returns
349  *	   -EINVAL.
350  */
351 static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
352 {
353 	*nib = vcpu->run->s.regs.gprs[2];
354 
355 	if (!*nib)
356 		return -EINVAL;
357 	if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
358 		return -EINVAL;
359 
360 	return 0;
361 }
362 
363 static int ensure_nib_shared(unsigned long addr, struct gmap *gmap)
364 {
365 	int ret;
366 
367 	/*
368 	 * The nib has to be located in shared storage since guest and
369 	 * host access it. vfio_pin_pages() will do a pin shared and
370 	 * if that fails (possibly because it's not a shared page) it
371 	 * calls export. We try to do a second pin shared here so that
372 	 * the UV gives us an error code if we try to pin a non-shared
373 	 * page.
374 	 *
375 	 * If the page is already pinned shared the UV will return a success.
376 	 */
377 	ret = uv_pin_shared(addr);
378 	if (ret) {
379 		/* vfio_pin_pages() likely exported the page so let's re-import */
380 		gmap_convert_to_secure(gmap, addr);
381 	}
382 	return ret;
383 }
384 
385 /**
386  * vfio_ap_irq_enable - Enable Interruption for a APQN
387  *
388  * @q:	 the vfio_ap_queue holding AQIC parameters
389  * @isc: the guest ISC to register with the GIB interface
390  * @vcpu: the vcpu object containing the registers specifying the parameters
391  *	  passed to the PQAP(AQIC) instruction.
392  *
393  * Pin the NIB saved in *q
394  * Register the guest ISC to GIB interface and retrieve the
395  * host ISC to issue the host side PQAP/AQIC
396  *
397  * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
398  * vfio_pin_pages or kvm_s390_gisc_register failed.
399  *
400  * Otherwise return the ap_queue_status returned by the ap_aqic(),
401  * all retry handling will be done by the guest.
402  *
403  * Return: &struct ap_queue_status
404  */
405 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
406 						 int isc,
407 						 struct kvm_vcpu *vcpu)
408 {
409 	union ap_qirq_ctrl aqic_gisa = { .value = 0 };
410 	struct ap_queue_status status = {};
411 	struct kvm_s390_gisa *gisa;
412 	struct page *h_page;
413 	int nisc;
414 	struct kvm *kvm;
415 	phys_addr_t h_nib;
416 	dma_addr_t nib;
417 	int ret;
418 
419 	/* Verify that the notification indicator byte address is valid */
420 	if (vfio_ap_validate_nib(vcpu, &nib)) {
421 		VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
422 				 __func__, &nib, q->apqn);
423 
424 		status.response_code = AP_RESPONSE_INVALID_ADDRESS;
425 		return status;
426 	}
427 
428 	ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
429 			     IOMMU_READ | IOMMU_WRITE, &h_page);
430 	switch (ret) {
431 	case 1:
432 		break;
433 	default:
434 		VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
435 				 "nib=%pad, apqn=%#04x\n",
436 				 __func__, ret, &nib, q->apqn);
437 
438 		status.response_code = AP_RESPONSE_INVALID_ADDRESS;
439 		return status;
440 	}
441 
442 	kvm = q->matrix_mdev->kvm;
443 	gisa = kvm->arch.gisa_int.origin;
444 
445 	h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
446 	aqic_gisa.gisc = isc;
447 
448 	/* NIB in non-shared storage is a rc 6 for PV guests */
449 	if (kvm_s390_pv_cpu_is_protected(vcpu) &&
450 	    ensure_nib_shared(h_nib & PAGE_MASK, kvm->arch.gmap)) {
451 		vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
452 		status.response_code = AP_RESPONSE_INVALID_ADDRESS;
453 		return status;
454 	}
455 
456 	nisc = kvm_s390_gisc_register(kvm, isc);
457 	if (nisc < 0) {
458 		VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
459 				 __func__, nisc, isc, q->apqn);
460 
461 		vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
462 		status.response_code = AP_RESPONSE_INVALID_ADDRESS;
463 		return status;
464 	}
465 
466 	aqic_gisa.isc = nisc;
467 	aqic_gisa.ir = 1;
468 	aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
469 
470 	status = ap_aqic(q->apqn, aqic_gisa, h_nib);
471 	switch (status.response_code) {
472 	case AP_RESPONSE_NORMAL:
473 		/* See if we did clear older IRQ configuration */
474 		vfio_ap_free_aqic_resources(q);
475 		q->saved_iova = nib;
476 		q->saved_isc = isc;
477 		break;
478 	case AP_RESPONSE_OTHERWISE_CHANGED:
479 		/* We could not modify IRQ settings: clear new configuration */
480 		ret = kvm_s390_gisc_unregister(kvm, isc);
481 		if (ret)
482 			VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n",
483 					 __func__, ret, isc, q->apqn);
484 		vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
485 		break;
486 	default:
487 		pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
488 			status.response_code);
489 		vfio_ap_irq_disable(q);
490 		break;
491 	}
492 
493 	if (status.response_code != AP_RESPONSE_NORMAL) {
494 		VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
495 				 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
496 				 "gisa=%#x, isc=%#x, apqn=%#04x\n",
497 				 __func__, status.response_code,
498 				 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
499 				 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
500 				 q->apqn);
501 	}
502 
503 	return status;
504 }
505 
506 /**
507  * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
508  *				of big endian elements that can be passed by
509  *				value to an s390dbf sprintf event function to
510  *				format a UUID string.
511  *
512  * @guid: the object containing the little endian guid
513  * @uuid: a six-element array of long values that can be passed by value as
514  *	  arguments for a formatting string specifying a UUID.
515  *
516  * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
517  * event functions if the memory for the passed string is available as long as
518  * the debug feature exists. Since a mediated device can be removed at any
519  * time, it's name can not be used because %s passes the reference to the string
520  * in memory and the reference will go stale once the device is removed .
521  *
522  * The s390dbf string formatting function allows a maximum of 9 arguments for a
523  * message to be displayed in the 'sprintf' view. In order to use the bytes
524  * comprising the mediated device's UUID to display the mediated device name,
525  * they will have to be converted into an array whose elements can be passed by
526  * value to sprintf. For example:
527  *
528  * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
529  * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
530  * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
531  * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
532  */
533 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
534 {
535 	/*
536 	 * The input guid is ordered in little endian, so it needs to be
537 	 * reordered for displaying a UUID as a string. This specifies the
538 	 * guid indices in proper order.
539 	 */
540 	uuid[0] = le32_to_cpup((__le32 *)guid);
541 	uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
542 	uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
543 	uuid[3] = *((__u16 *)&guid->b[8]);
544 	uuid[4] = *((__u16 *)&guid->b[10]);
545 	uuid[5] = *((__u32 *)&guid->b[12]);
546 }
547 
548 /**
549  * handle_pqap - PQAP instruction callback
550  *
551  * @vcpu: The vcpu on which we received the PQAP instruction
552  *
553  * Get the general register contents to initialize internal variables.
554  * REG[0]: APQN
555  * REG[1]: IR and ISC
556  * REG[2]: NIB
557  *
558  * Response.status may be set to following Response Code:
559  * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
560  * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
561  * - AP_RESPONSE_NORMAL (0) : in case of success
562  *   Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
563  * We take the matrix_dev lock to ensure serialization on queues and
564  * mediated device access.
565  *
566  * Return: 0 if we could handle the request inside KVM.
567  * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
568  */
569 static int handle_pqap(struct kvm_vcpu *vcpu)
570 {
571 	uint64_t status;
572 	uint16_t apqn;
573 	unsigned long uuid[6];
574 	struct vfio_ap_queue *q;
575 	struct ap_queue_status qstatus = {
576 			       .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
577 	struct ap_matrix_mdev *matrix_mdev;
578 
579 	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
580 
581 	/* If we do not use the AIV facility just go to userland */
582 	if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
583 		VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
584 				 __func__, apqn, vcpu->arch.sie_block->eca);
585 
586 		return -EOPNOTSUPP;
587 	}
588 
589 	mutex_lock(&matrix_dev->mdevs_lock);
590 
591 	if (!vcpu->kvm->arch.crypto.pqap_hook) {
592 		VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
593 				 __func__, apqn);
594 
595 		goto out_unlock;
596 	}
597 
598 	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
599 				   struct ap_matrix_mdev, pqap_hook);
600 
601 	/* If the there is no guest using the mdev, there is nothing to do */
602 	if (!matrix_mdev->kvm) {
603 		vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
604 		VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
605 				 __func__, uuid[0],  uuid[1], uuid[2],
606 				 uuid[3], uuid[4], uuid[5], apqn);
607 		goto out_unlock;
608 	}
609 
610 	q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
611 	if (!q) {
612 		VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
613 				 __func__, AP_QID_CARD(apqn),
614 				 AP_QID_QUEUE(apqn));
615 		goto out_unlock;
616 	}
617 
618 	status = vcpu->run->s.regs.gprs[1];
619 
620 	/* If IR bit(16) is set we enable the interrupt */
621 	if ((status >> (63 - 16)) & 0x01)
622 		qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
623 	else
624 		qstatus = vfio_ap_irq_disable(q);
625 
626 out_unlock:
627 	memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
628 	vcpu->run->s.regs.gprs[1] >>= 32;
629 	mutex_unlock(&matrix_dev->mdevs_lock);
630 	return 0;
631 }
632 
633 static void vfio_ap_matrix_init(struct ap_config_info *info,
634 				struct ap_matrix *matrix)
635 {
636 	matrix->apm_max = info->apxa ? info->na : 63;
637 	matrix->aqm_max = info->apxa ? info->nd : 15;
638 	matrix->adm_max = info->apxa ? info->nd : 15;
639 }
640 
641 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
642 {
643 	if (matrix_mdev->kvm)
644 		kvm_arch_crypto_set_masks(matrix_mdev->kvm,
645 					  matrix_mdev->shadow_apcb.apm,
646 					  matrix_mdev->shadow_apcb.aqm,
647 					  matrix_mdev->shadow_apcb.adm);
648 }
649 
650 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
651 {
652 	DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
653 
654 	bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
655 	bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
656 		   (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
657 
658 	return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
659 			     AP_DOMAINS);
660 }
661 
662 static bool _queue_passable(struct vfio_ap_queue *q)
663 {
664 	if (!q)
665 		return false;
666 
667 	switch (q->reset_status.response_code) {
668 	case AP_RESPONSE_NORMAL:
669 	case AP_RESPONSE_DECONFIGURED:
670 	case AP_RESPONSE_CHECKSTOPPED:
671 		return true;
672 	default:
673 		return false;
674 	}
675 }
676 
677 /*
678  * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
679  *				to ensure no queue devices are passed through to
680  *				the guest that are not bound to the vfio_ap
681  *				device driver.
682  *
683  * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
684  * @apm_filtered: a 256-bit bitmap for storing the APIDs filtered from the
685  *		  guest's AP configuration that are still in the host's AP
686  *		  configuration.
687  *
688  * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
689  *	 driver, its APID will be filtered from the guest's APCB. The matrix
690  *	 structure precludes filtering an individual APQN, so its APID will be
691  *	 filtered. Consequently, all queues associated with the adapter that
692  *	 are in the host's AP configuration must be reset. If queues are
693  *	 subsequently made available again to the guest, they should re-appear
694  *	 in a reset state
695  *
696  * Return: a boolean value indicating whether the KVM guest's APCB was changed
697  *	   by the filtering or not.
698  */
699 static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
700 				       unsigned long *apm_filtered)
701 {
702 	unsigned long apid, apqi, apqn;
703 	DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
704 	DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
705 
706 	bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
707 	bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
708 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
709 	bitmap_clear(apm_filtered, 0, AP_DEVICES);
710 
711 	/*
712 	 * Copy the adapters, domains and control domains to the shadow_apcb
713 	 * from the matrix mdev, but only those that are assigned to the host's
714 	 * AP configuration.
715 	 */
716 	bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
717 		   (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
718 	bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
719 		   (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
720 
721 	for_each_set_bit_inv(apid, matrix_mdev->shadow_apcb.apm, AP_DEVICES) {
722 		for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
723 				     AP_DOMAINS) {
724 			/*
725 			 * If the APQN is not bound to the vfio_ap device
726 			 * driver, then we can't assign it to the guest's
727 			 * AP configuration. The AP architecture won't
728 			 * allow filtering of a single APQN, so let's filter
729 			 * the APID since an adapter represents a physical
730 			 * hardware device.
731 			 */
732 			apqn = AP_MKQID(apid, apqi);
733 			if (!_queue_passable(vfio_ap_mdev_get_queue(matrix_mdev, apqn))) {
734 				clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
735 
736 				/*
737 				 * If the adapter was previously plugged into
738 				 * the guest, let's let the caller know that
739 				 * the APID was filtered.
740 				 */
741 				if (test_bit_inv(apid, prev_shadow_apm))
742 					set_bit_inv(apid, apm_filtered);
743 
744 				break;
745 			}
746 		}
747 	}
748 
749 	return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
750 			     AP_DEVICES) ||
751 	       !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
752 			     AP_DOMAINS);
753 }
754 
755 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
756 {
757 	struct ap_matrix_mdev *matrix_mdev =
758 		container_of(vdev, struct ap_matrix_mdev, vdev);
759 
760 	matrix_mdev->mdev = to_mdev_device(vdev->dev);
761 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
762 	matrix_mdev->pqap_hook = handle_pqap;
763 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
764 	hash_init(matrix_mdev->qtable.queues);
765 
766 	return 0;
767 }
768 
769 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
770 {
771 	struct ap_matrix_mdev *matrix_mdev;
772 	int ret;
773 
774 	matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
775 					&vfio_ap_matrix_dev_ops);
776 	if (IS_ERR(matrix_mdev))
777 		return PTR_ERR(matrix_mdev);
778 
779 	ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
780 	if (ret)
781 		goto err_put_vdev;
782 	matrix_mdev->req_trigger = NULL;
783 	dev_set_drvdata(&mdev->dev, matrix_mdev);
784 	mutex_lock(&matrix_dev->mdevs_lock);
785 	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
786 	mutex_unlock(&matrix_dev->mdevs_lock);
787 	return 0;
788 
789 err_put_vdev:
790 	vfio_put_device(&matrix_mdev->vdev);
791 	return ret;
792 }
793 
794 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
795 				    struct vfio_ap_queue *q)
796 {
797 	if (!q || vfio_ap_mdev_get_queue(matrix_mdev, q->apqn))
798 		return;
799 
800 	q->matrix_mdev = matrix_mdev;
801 	hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
802 }
803 
804 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
805 {
806 	struct vfio_ap_queue *q;
807 
808 	q = vfio_ap_find_queue(apqn);
809 	vfio_ap_mdev_link_queue(matrix_mdev, q);
810 }
811 
812 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
813 {
814 	hash_del(&q->mdev_qnode);
815 }
816 
817 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
818 {
819 	q->matrix_mdev = NULL;
820 }
821 
822 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
823 {
824 	struct vfio_ap_queue *q;
825 	unsigned long apid, apqi;
826 
827 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
828 		for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
829 				     AP_DOMAINS) {
830 			q = vfio_ap_mdev_get_queue(matrix_mdev,
831 						   AP_MKQID(apid, apqi));
832 			if (q)
833 				q->matrix_mdev = NULL;
834 		}
835 	}
836 }
837 
838 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
839 {
840 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
841 
842 	vfio_unregister_group_dev(&matrix_mdev->vdev);
843 
844 	mutex_lock(&matrix_dev->guests_lock);
845 	mutex_lock(&matrix_dev->mdevs_lock);
846 	vfio_ap_mdev_reset_queues(matrix_mdev);
847 	vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
848 	list_del(&matrix_mdev->node);
849 	mutex_unlock(&matrix_dev->mdevs_lock);
850 	mutex_unlock(&matrix_dev->guests_lock);
851 	vfio_put_device(&matrix_mdev->vdev);
852 }
853 
854 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
855 			 "already assigned to %s"
856 
857 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
858 					 unsigned long *apm,
859 					 unsigned long *aqm)
860 {
861 	unsigned long apid, apqi;
862 	const struct device *dev = mdev_dev(matrix_mdev->mdev);
863 	const char *mdev_name = dev_name(dev);
864 
865 	for_each_set_bit_inv(apid, apm, AP_DEVICES)
866 		for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
867 			dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
868 }
869 
870 /**
871  * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
872  *
873  * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
874  * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
875  *
876  * Verifies that each APQN derived from the Cartesian product of a bitmap of
877  * AP adapter IDs and AP queue indexes is not configured for any matrix
878  * mediated device. AP queue sharing is not allowed.
879  *
880  * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
881  */
882 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
883 					  unsigned long *mdev_aqm)
884 {
885 	struct ap_matrix_mdev *matrix_mdev;
886 	DECLARE_BITMAP(apm, AP_DEVICES);
887 	DECLARE_BITMAP(aqm, AP_DOMAINS);
888 
889 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
890 		/*
891 		 * If the input apm and aqm are fields of the matrix_mdev
892 		 * object, then move on to the next matrix_mdev.
893 		 */
894 		if (mdev_apm == matrix_mdev->matrix.apm &&
895 		    mdev_aqm == matrix_mdev->matrix.aqm)
896 			continue;
897 
898 		memset(apm, 0, sizeof(apm));
899 		memset(aqm, 0, sizeof(aqm));
900 
901 		/*
902 		 * We work on full longs, as we can only exclude the leftover
903 		 * bits in non-inverse order. The leftover is all zeros.
904 		 */
905 		if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
906 				AP_DEVICES))
907 			continue;
908 
909 		if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
910 				AP_DOMAINS))
911 			continue;
912 
913 		vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
914 
915 		return -EADDRINUSE;
916 	}
917 
918 	return 0;
919 }
920 
921 /**
922  * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
923  *				 not reserved for the default zcrypt driver and
924  *				 are not assigned to another mdev.
925  *
926  * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
927  *
928  * Return: One of the following values:
929  * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
930  *   most likely -EBUSY indicating the ap_perms_mutex lock is already held.
931  * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
932  *		   zcrypt default driver.
933  * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
934  * o A zero indicating validation succeeded.
935  */
936 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
937 {
938 	if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
939 					       matrix_mdev->matrix.aqm))
940 		return -EADDRNOTAVAIL;
941 
942 	return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
943 					      matrix_mdev->matrix.aqm);
944 }
945 
946 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
947 				      unsigned long apid)
948 {
949 	unsigned long apqi;
950 
951 	for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
952 		vfio_ap_mdev_link_apqn(matrix_mdev,
953 				       AP_MKQID(apid, apqi));
954 }
955 
956 static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,
957 				    unsigned long apid,
958 				    struct list_head *qlist)
959 {
960 	struct vfio_ap_queue *q;
961 	unsigned long  apqi;
962 
963 	for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS) {
964 		q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
965 		if (q)
966 			list_add_tail(&q->reset_qnode, qlist);
967 	}
968 }
969 
970 static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,
971 				  unsigned long apid)
972 {
973 	struct list_head qlist;
974 
975 	INIT_LIST_HEAD(&qlist);
976 	collect_queues_to_reset(matrix_mdev, apid, &qlist);
977 	vfio_ap_mdev_reset_qlist(&qlist);
978 }
979 
980 static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
981 				  unsigned long *apm_reset)
982 {
983 	struct list_head qlist;
984 	unsigned long apid;
985 
986 	if (bitmap_empty(apm_reset, AP_DEVICES))
987 		return 0;
988 
989 	INIT_LIST_HEAD(&qlist);
990 
991 	for_each_set_bit_inv(apid, apm_reset, AP_DEVICES)
992 		collect_queues_to_reset(matrix_mdev, apid, &qlist);
993 
994 	return vfio_ap_mdev_reset_qlist(&qlist);
995 }
996 
997 /**
998  * assign_adapter_store - parses the APID from @buf and sets the
999  * corresponding bit in the mediated matrix device's APM
1000  *
1001  * @dev:	the matrix device
1002  * @attr:	the mediated matrix device's assign_adapter attribute
1003  * @buf:	a buffer containing the AP adapter number (APID) to
1004  *		be assigned
1005  * @count:	the number of bytes in @buf
1006  *
1007  * Return: the number of bytes processed if the APID is valid; otherwise,
1008  * returns one of the following errors:
1009  *
1010  *	1. -EINVAL
1011  *	   The APID is not a valid number
1012  *
1013  *	2. -ENODEV
1014  *	   The APID exceeds the maximum value configured for the system
1015  *
1016  *	3. -EADDRNOTAVAIL
1017  *	   An APQN derived from the cross product of the APID being assigned
1018  *	   and the APQIs previously assigned is not bound to the vfio_ap device
1019  *	   driver; or, if no APQIs have yet been assigned, the APID is not
1020  *	   contained in an APQN bound to the vfio_ap device driver.
1021  *
1022  *	4. -EADDRINUSE
1023  *	   An APQN derived from the cross product of the APID being assigned
1024  *	   and the APQIs previously assigned is being used by another mediated
1025  *	   matrix device
1026  *
1027  *	5. -EAGAIN
1028  *	   A lock required to validate the mdev's AP configuration could not
1029  *	   be obtained.
1030  */
1031 static ssize_t assign_adapter_store(struct device *dev,
1032 				    struct device_attribute *attr,
1033 				    const char *buf, size_t count)
1034 {
1035 	int ret;
1036 	unsigned long apid;
1037 	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1038 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1039 
1040 	mutex_lock(&ap_perms_mutex);
1041 	get_update_locks_for_mdev(matrix_mdev);
1042 
1043 	ret = kstrtoul(buf, 0, &apid);
1044 	if (ret)
1045 		goto done;
1046 
1047 	if (apid > matrix_mdev->matrix.apm_max) {
1048 		ret = -ENODEV;
1049 		goto done;
1050 	}
1051 
1052 	if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1053 		ret = count;
1054 		goto done;
1055 	}
1056 
1057 	set_bit_inv(apid, matrix_mdev->matrix.apm);
1058 
1059 	ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1060 	if (ret) {
1061 		clear_bit_inv(apid, matrix_mdev->matrix.apm);
1062 		goto done;
1063 	}
1064 
1065 	vfio_ap_mdev_link_adapter(matrix_mdev, apid);
1066 
1067 	if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1068 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1069 		reset_queues_for_apids(matrix_mdev, apm_filtered);
1070 	}
1071 
1072 	ret = count;
1073 done:
1074 	release_update_locks_for_mdev(matrix_mdev);
1075 	mutex_unlock(&ap_perms_mutex);
1076 
1077 	return ret;
1078 }
1079 static DEVICE_ATTR_WO(assign_adapter);
1080 
1081 static struct vfio_ap_queue
1082 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
1083 			     unsigned long apid, unsigned long apqi)
1084 {
1085 	struct vfio_ap_queue *q = NULL;
1086 
1087 	q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1088 	/* If the queue is assigned to the matrix mdev, unlink it. */
1089 	if (q)
1090 		vfio_ap_unlink_queue_fr_mdev(q);
1091 
1092 	return q;
1093 }
1094 
1095 /**
1096  * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
1097  *				 adapter from the matrix mdev to which the
1098  *				 adapter was assigned.
1099  * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
1100  * @apid: the APID of the unassigned adapter.
1101  * @qlist: list for storing queues associated with unassigned adapter that
1102  *	   need to be reset.
1103  */
1104 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1105 					unsigned long apid,
1106 					struct list_head *qlist)
1107 {
1108 	unsigned long apqi;
1109 	struct vfio_ap_queue *q;
1110 
1111 	for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1112 		q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1113 
1114 		if (q && qlist) {
1115 			if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1116 			    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1117 				list_add_tail(&q->reset_qnode, qlist);
1118 		}
1119 	}
1120 }
1121 
1122 static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,
1123 					     unsigned long *apids)
1124 {
1125 	struct vfio_ap_queue *q, *tmpq;
1126 	struct list_head qlist;
1127 	unsigned long apid;
1128 	bool apcb_update = false;
1129 
1130 	INIT_LIST_HEAD(&qlist);
1131 
1132 	for_each_set_bit_inv(apid, apids, AP_DEVICES) {
1133 		vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
1134 
1135 		if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1136 			clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1137 			apcb_update = true;
1138 		}
1139 	}
1140 
1141 	/* Only update apcb if needed to avoid impacting guest */
1142 	if (apcb_update)
1143 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1144 
1145 	vfio_ap_mdev_reset_qlist(&qlist);
1146 
1147 	list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1148 		vfio_ap_unlink_mdev_fr_queue(q);
1149 		list_del(&q->reset_qnode);
1150 	}
1151 }
1152 
1153 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1154 					    unsigned long apid)
1155 {
1156 	DECLARE_BITMAP(apids, AP_DEVICES);
1157 
1158 	bitmap_zero(apids, AP_DEVICES);
1159 	set_bit_inv(apid, apids);
1160 	vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, apids);
1161 }
1162 
1163 /**
1164  * unassign_adapter_store - parses the APID from @buf and clears the
1165  * corresponding bit in the mediated matrix device's APM
1166  *
1167  * @dev:	the matrix device
1168  * @attr:	the mediated matrix device's unassign_adapter attribute
1169  * @buf:	a buffer containing the adapter number (APID) to be unassigned
1170  * @count:	the number of bytes in @buf
1171  *
1172  * Return: the number of bytes processed if the APID is valid; otherwise,
1173  * returns one of the following errors:
1174  *	-EINVAL if the APID is not a number
1175  *	-ENODEV if the APID it exceeds the maximum value configured for the
1176  *		system
1177  */
1178 static ssize_t unassign_adapter_store(struct device *dev,
1179 				      struct device_attribute *attr,
1180 				      const char *buf, size_t count)
1181 {
1182 	int ret;
1183 	unsigned long apid;
1184 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1185 
1186 	get_update_locks_for_mdev(matrix_mdev);
1187 
1188 	ret = kstrtoul(buf, 0, &apid);
1189 	if (ret)
1190 		goto done;
1191 
1192 	if (apid > matrix_mdev->matrix.apm_max) {
1193 		ret = -ENODEV;
1194 		goto done;
1195 	}
1196 
1197 	if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1198 		ret = count;
1199 		goto done;
1200 	}
1201 
1202 	clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1203 	vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1204 	ret = count;
1205 done:
1206 	release_update_locks_for_mdev(matrix_mdev);
1207 	return ret;
1208 }
1209 static DEVICE_ATTR_WO(unassign_adapter);
1210 
1211 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1212 				     unsigned long apqi)
1213 {
1214 	unsigned long apid;
1215 
1216 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1217 		vfio_ap_mdev_link_apqn(matrix_mdev,
1218 				       AP_MKQID(apid, apqi));
1219 }
1220 
1221 /**
1222  * assign_domain_store - parses the APQI from @buf and sets the
1223  * corresponding bit in the mediated matrix device's AQM
1224  *
1225  * @dev:	the matrix device
1226  * @attr:	the mediated matrix device's assign_domain attribute
1227  * @buf:	a buffer containing the AP queue index (APQI) of the domain to
1228  *		be assigned
1229  * @count:	the number of bytes in @buf
1230  *
1231  * Return: the number of bytes processed if the APQI is valid; otherwise returns
1232  * one of the following errors:
1233  *
1234  *	1. -EINVAL
1235  *	   The APQI is not a valid number
1236  *
1237  *	2. -ENODEV
1238  *	   The APQI exceeds the maximum value configured for the system
1239  *
1240  *	3. -EADDRNOTAVAIL
1241  *	   An APQN derived from the cross product of the APQI being assigned
1242  *	   and the APIDs previously assigned is not bound to the vfio_ap device
1243  *	   driver; or, if no APIDs have yet been assigned, the APQI is not
1244  *	   contained in an APQN bound to the vfio_ap device driver.
1245  *
1246  *	4. -EADDRINUSE
1247  *	   An APQN derived from the cross product of the APQI being assigned
1248  *	   and the APIDs previously assigned is being used by another mediated
1249  *	   matrix device
1250  *
1251  *	5. -EAGAIN
1252  *	   The lock required to validate the mdev's AP configuration could not
1253  *	   be obtained.
1254  */
1255 static ssize_t assign_domain_store(struct device *dev,
1256 				   struct device_attribute *attr,
1257 				   const char *buf, size_t count)
1258 {
1259 	int ret;
1260 	unsigned long apqi;
1261 	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1262 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1263 
1264 	mutex_lock(&ap_perms_mutex);
1265 	get_update_locks_for_mdev(matrix_mdev);
1266 
1267 	ret = kstrtoul(buf, 0, &apqi);
1268 	if (ret)
1269 		goto done;
1270 
1271 	if (apqi > matrix_mdev->matrix.aqm_max) {
1272 		ret = -ENODEV;
1273 		goto done;
1274 	}
1275 
1276 	if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1277 		ret = count;
1278 		goto done;
1279 	}
1280 
1281 	set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1282 
1283 	ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1284 	if (ret) {
1285 		clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1286 		goto done;
1287 	}
1288 
1289 	vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1290 
1291 	if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1292 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1293 		reset_queues_for_apids(matrix_mdev, apm_filtered);
1294 	}
1295 
1296 	ret = count;
1297 done:
1298 	release_update_locks_for_mdev(matrix_mdev);
1299 	mutex_unlock(&ap_perms_mutex);
1300 
1301 	return ret;
1302 }
1303 static DEVICE_ATTR_WO(assign_domain);
1304 
1305 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1306 				       unsigned long apqi,
1307 				       struct list_head *qlist)
1308 {
1309 	unsigned long apid;
1310 	struct vfio_ap_queue *q;
1311 
1312 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1313 		q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1314 
1315 		if (q && qlist) {
1316 			if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1317 			    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1318 				list_add_tail(&q->reset_qnode, qlist);
1319 		}
1320 	}
1321 }
1322 
1323 static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,
1324 					    unsigned long *apqis)
1325 {
1326 	struct vfio_ap_queue *q, *tmpq;
1327 	struct list_head qlist;
1328 	unsigned long apqi;
1329 	bool apcb_update = false;
1330 
1331 	INIT_LIST_HEAD(&qlist);
1332 
1333 	for_each_set_bit_inv(apqi, apqis, AP_DOMAINS) {
1334 		vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
1335 
1336 		if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1337 			clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1338 			apcb_update = true;
1339 		}
1340 	}
1341 
1342 	/* Only update apcb if needed to avoid impacting guest */
1343 	if (apcb_update)
1344 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1345 
1346 	vfio_ap_mdev_reset_qlist(&qlist);
1347 
1348 	list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1349 		vfio_ap_unlink_mdev_fr_queue(q);
1350 		list_del(&q->reset_qnode);
1351 	}
1352 }
1353 
1354 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1355 					   unsigned long apqi)
1356 {
1357 	DECLARE_BITMAP(apqis, AP_DOMAINS);
1358 
1359 	bitmap_zero(apqis, AP_DEVICES);
1360 	set_bit_inv(apqi, apqis);
1361 	vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis);
1362 }
1363 
1364 /**
1365  * unassign_domain_store - parses the APQI from @buf and clears the
1366  * corresponding bit in the mediated matrix device's AQM
1367  *
1368  * @dev:	the matrix device
1369  * @attr:	the mediated matrix device's unassign_domain attribute
1370  * @buf:	a buffer containing the AP queue index (APQI) of the domain to
1371  *		be unassigned
1372  * @count:	the number of bytes in @buf
1373  *
1374  * Return: the number of bytes processed if the APQI is valid; otherwise,
1375  * returns one of the following errors:
1376  *	-EINVAL if the APQI is not a number
1377  *	-ENODEV if the APQI exceeds the maximum value configured for the system
1378  */
1379 static ssize_t unassign_domain_store(struct device *dev,
1380 				     struct device_attribute *attr,
1381 				     const char *buf, size_t count)
1382 {
1383 	int ret;
1384 	unsigned long apqi;
1385 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1386 
1387 	get_update_locks_for_mdev(matrix_mdev);
1388 
1389 	ret = kstrtoul(buf, 0, &apqi);
1390 	if (ret)
1391 		goto done;
1392 
1393 	if (apqi > matrix_mdev->matrix.aqm_max) {
1394 		ret = -ENODEV;
1395 		goto done;
1396 	}
1397 
1398 	if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1399 		ret = count;
1400 		goto done;
1401 	}
1402 
1403 	clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1404 	vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1405 	ret = count;
1406 
1407 done:
1408 	release_update_locks_for_mdev(matrix_mdev);
1409 	return ret;
1410 }
1411 static DEVICE_ATTR_WO(unassign_domain);
1412 
1413 /**
1414  * assign_control_domain_store - parses the domain ID from @buf and sets
1415  * the corresponding bit in the mediated matrix device's ADM
1416  *
1417  * @dev:	the matrix device
1418  * @attr:	the mediated matrix device's assign_control_domain attribute
1419  * @buf:	a buffer containing the domain ID to be assigned
1420  * @count:	the number of bytes in @buf
1421  *
1422  * Return: the number of bytes processed if the domain ID is valid; otherwise,
1423  * returns one of the following errors:
1424  *	-EINVAL if the ID is not a number
1425  *	-ENODEV if the ID exceeds the maximum value configured for the system
1426  */
1427 static ssize_t assign_control_domain_store(struct device *dev,
1428 					   struct device_attribute *attr,
1429 					   const char *buf, size_t count)
1430 {
1431 	int ret;
1432 	unsigned long id;
1433 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1434 
1435 	get_update_locks_for_mdev(matrix_mdev);
1436 
1437 	ret = kstrtoul(buf, 0, &id);
1438 	if (ret)
1439 		goto done;
1440 
1441 	if (id > matrix_mdev->matrix.adm_max) {
1442 		ret = -ENODEV;
1443 		goto done;
1444 	}
1445 
1446 	if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1447 		ret = count;
1448 		goto done;
1449 	}
1450 
1451 	/* Set the bit in the ADM (bitmask) corresponding to the AP control
1452 	 * domain number (id). The bits in the mask, from most significant to
1453 	 * least significant, correspond to IDs 0 up to the one less than the
1454 	 * number of control domains that can be assigned.
1455 	 */
1456 	set_bit_inv(id, matrix_mdev->matrix.adm);
1457 	if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1458 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1459 
1460 	ret = count;
1461 done:
1462 	release_update_locks_for_mdev(matrix_mdev);
1463 	return ret;
1464 }
1465 static DEVICE_ATTR_WO(assign_control_domain);
1466 
1467 /**
1468  * unassign_control_domain_store - parses the domain ID from @buf and
1469  * clears the corresponding bit in the mediated matrix device's ADM
1470  *
1471  * @dev:	the matrix device
1472  * @attr:	the mediated matrix device's unassign_control_domain attribute
1473  * @buf:	a buffer containing the domain ID to be unassigned
1474  * @count:	the number of bytes in @buf
1475  *
1476  * Return: the number of bytes processed if the domain ID is valid; otherwise,
1477  * returns one of the following errors:
1478  *	-EINVAL if the ID is not a number
1479  *	-ENODEV if the ID exceeds the maximum value configured for the system
1480  */
1481 static ssize_t unassign_control_domain_store(struct device *dev,
1482 					     struct device_attribute *attr,
1483 					     const char *buf, size_t count)
1484 {
1485 	int ret;
1486 	unsigned long domid;
1487 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1488 
1489 	get_update_locks_for_mdev(matrix_mdev);
1490 
1491 	ret = kstrtoul(buf, 0, &domid);
1492 	if (ret)
1493 		goto done;
1494 
1495 	if (domid > matrix_mdev->matrix.adm_max) {
1496 		ret = -ENODEV;
1497 		goto done;
1498 	}
1499 
1500 	if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1501 		ret = count;
1502 		goto done;
1503 	}
1504 
1505 	clear_bit_inv(domid, matrix_mdev->matrix.adm);
1506 
1507 	if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1508 		clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1509 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1510 	}
1511 
1512 	ret = count;
1513 done:
1514 	release_update_locks_for_mdev(matrix_mdev);
1515 	return ret;
1516 }
1517 static DEVICE_ATTR_WO(unassign_control_domain);
1518 
1519 static ssize_t control_domains_show(struct device *dev,
1520 				    struct device_attribute *dev_attr,
1521 				    char *buf)
1522 {
1523 	unsigned long id;
1524 	int nchars = 0;
1525 	int n;
1526 	char *bufpos = buf;
1527 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1528 	unsigned long max_domid = matrix_mdev->matrix.adm_max;
1529 
1530 	mutex_lock(&matrix_dev->mdevs_lock);
1531 	for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1532 		n = sprintf(bufpos, "%04lx\n", id);
1533 		bufpos += n;
1534 		nchars += n;
1535 	}
1536 	mutex_unlock(&matrix_dev->mdevs_lock);
1537 
1538 	return nchars;
1539 }
1540 static DEVICE_ATTR_RO(control_domains);
1541 
1542 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1543 {
1544 	char *bufpos = buf;
1545 	unsigned long apid;
1546 	unsigned long apqi;
1547 	unsigned long apid1;
1548 	unsigned long apqi1;
1549 	unsigned long napm_bits = matrix->apm_max + 1;
1550 	unsigned long naqm_bits = matrix->aqm_max + 1;
1551 	int nchars = 0;
1552 	int n;
1553 
1554 	apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1555 	apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1556 
1557 	if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1558 		for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1559 			for_each_set_bit_inv(apqi, matrix->aqm,
1560 					     naqm_bits) {
1561 				n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1562 					    apqi);
1563 				bufpos += n;
1564 				nchars += n;
1565 			}
1566 		}
1567 	} else if (apid1 < napm_bits) {
1568 		for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1569 			n = sprintf(bufpos, "%02lx.\n", apid);
1570 			bufpos += n;
1571 			nchars += n;
1572 		}
1573 	} else if (apqi1 < naqm_bits) {
1574 		for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1575 			n = sprintf(bufpos, ".%04lx\n", apqi);
1576 			bufpos += n;
1577 			nchars += n;
1578 		}
1579 	}
1580 
1581 	return nchars;
1582 }
1583 
1584 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1585 			   char *buf)
1586 {
1587 	ssize_t nchars;
1588 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1589 
1590 	mutex_lock(&matrix_dev->mdevs_lock);
1591 	nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1592 	mutex_unlock(&matrix_dev->mdevs_lock);
1593 
1594 	return nchars;
1595 }
1596 static DEVICE_ATTR_RO(matrix);
1597 
1598 static ssize_t guest_matrix_show(struct device *dev,
1599 				 struct device_attribute *attr, char *buf)
1600 {
1601 	ssize_t nchars;
1602 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1603 
1604 	mutex_lock(&matrix_dev->mdevs_lock);
1605 	nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1606 	mutex_unlock(&matrix_dev->mdevs_lock);
1607 
1608 	return nchars;
1609 }
1610 static DEVICE_ATTR_RO(guest_matrix);
1611 
1612 static ssize_t write_ap_bitmap(unsigned long *bitmap, char *buf, int offset, char sep)
1613 {
1614 	return sysfs_emit_at(buf, offset, "0x%016lx%016lx%016lx%016lx%c",
1615 			 bitmap[0], bitmap[1], bitmap[2], bitmap[3], sep);
1616 }
1617 
1618 static ssize_t ap_config_show(struct device *dev, struct device_attribute *attr,
1619 			      char *buf)
1620 {
1621 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1622 	int idx = 0;
1623 
1624 	idx += write_ap_bitmap(matrix_mdev->matrix.apm, buf, idx, ',');
1625 	idx += write_ap_bitmap(matrix_mdev->matrix.aqm, buf, idx, ',');
1626 	idx += write_ap_bitmap(matrix_mdev->matrix.adm, buf, idx, '\n');
1627 
1628 	return idx;
1629 }
1630 
1631 /* Number of characters needed for a complete hex mask representing the bits in ..  */
1632 #define AP_DEVICES_STRLEN	(AP_DEVICES / 4 + 3)
1633 #define AP_DOMAINS_STRLEN	(AP_DOMAINS / 4 + 3)
1634 #define AP_CONFIG_STRLEN	(AP_DEVICES_STRLEN + 2 * AP_DOMAINS_STRLEN)
1635 
1636 static int parse_bitmap(char **strbufptr, unsigned long *bitmap, int nbits)
1637 {
1638 	char *curmask;
1639 
1640 	curmask = strsep(strbufptr, ",\n");
1641 	if (!curmask)
1642 		return -EINVAL;
1643 
1644 	bitmap_clear(bitmap, 0, nbits);
1645 	return ap_hex2bitmap(curmask, bitmap, nbits);
1646 }
1647 
1648 static int ap_matrix_overflow_check(struct ap_matrix_mdev *matrix_mdev)
1649 {
1650 	unsigned long bit;
1651 
1652 	for_each_set_bit_inv(bit, matrix_mdev->matrix.apm, AP_DEVICES) {
1653 		if (bit > matrix_mdev->matrix.apm_max)
1654 			return -ENODEV;
1655 	}
1656 
1657 	for_each_set_bit_inv(bit, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1658 		if (bit > matrix_mdev->matrix.aqm_max)
1659 			return -ENODEV;
1660 	}
1661 
1662 	for_each_set_bit_inv(bit, matrix_mdev->matrix.adm, AP_DOMAINS) {
1663 		if (bit > matrix_mdev->matrix.adm_max)
1664 			return -ENODEV;
1665 	}
1666 
1667 	return 0;
1668 }
1669 
1670 static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
1671 {
1672 	/* This check works around false positive gcc -Wstringop-overread */
1673 	if (!src)
1674 		return;
1675 
1676 	bitmap_copy(dst->apm, src->apm, AP_DEVICES);
1677 	bitmap_copy(dst->aqm, src->aqm, AP_DOMAINS);
1678 	bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
1679 }
1680 
1681 static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
1682 			       const char *buf, size_t count)
1683 {
1684 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1685 	struct ap_matrix m_new, m_old, m_added, m_removed;
1686 	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1687 	unsigned long newbit;
1688 	char *newbuf, *rest;
1689 	int rc = count;
1690 	bool do_update;
1691 
1692 	newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
1693 	if (!newbuf)
1694 		return -ENOMEM;
1695 	rest = newbuf;
1696 
1697 	mutex_lock(&ap_perms_mutex);
1698 	get_update_locks_for_mdev(matrix_mdev);
1699 
1700 	/* Save old state */
1701 	ap_matrix_copy(&m_old, &matrix_mdev->matrix);
1702 	if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
1703 	    parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
1704 	    parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
1705 		rc = -EINVAL;
1706 		goto out;
1707 	}
1708 
1709 	bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
1710 	bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
1711 	bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
1712 	bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
1713 
1714 	/* Need new bitmaps in matrix_mdev for validation */
1715 	ap_matrix_copy(&matrix_mdev->matrix, &m_new);
1716 
1717 	/* Ensure new state is valid, else undo new state */
1718 	rc = vfio_ap_mdev_validate_masks(matrix_mdev);
1719 	if (rc) {
1720 		ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1721 		goto out;
1722 	}
1723 	rc = ap_matrix_overflow_check(matrix_mdev);
1724 	if (rc) {
1725 		ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1726 		goto out;
1727 	}
1728 	rc = count;
1729 
1730 	/* Need old bitmaps in matrix_mdev for unplug/unlink */
1731 	ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1732 
1733 	/* Unlink removed adapters/domains */
1734 	vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, m_removed.apm);
1735 	vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
1736 
1737 	/* Need new bitmaps in matrix_mdev for linking new adapters/domains */
1738 	ap_matrix_copy(&matrix_mdev->matrix, &m_new);
1739 
1740 	/* Link newly added adapters */
1741 	for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
1742 		vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
1743 
1744 	for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
1745 		vfio_ap_mdev_link_domain(matrix_mdev, newbit);
1746 
1747 	/* filter resources not bound to vfio-ap */
1748 	do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
1749 	do_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
1750 
1751 	/* Apply changes to shadow apbc if things changed */
1752 	if (do_update) {
1753 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1754 		reset_queues_for_apids(matrix_mdev, apm_filtered);
1755 	}
1756 out:
1757 	release_update_locks_for_mdev(matrix_mdev);
1758 	mutex_unlock(&ap_perms_mutex);
1759 	kfree(newbuf);
1760 	return rc;
1761 }
1762 static DEVICE_ATTR_RW(ap_config);
1763 
1764 static struct attribute *vfio_ap_mdev_attrs[] = {
1765 	&dev_attr_assign_adapter.attr,
1766 	&dev_attr_unassign_adapter.attr,
1767 	&dev_attr_assign_domain.attr,
1768 	&dev_attr_unassign_domain.attr,
1769 	&dev_attr_assign_control_domain.attr,
1770 	&dev_attr_unassign_control_domain.attr,
1771 	&dev_attr_ap_config.attr,
1772 	&dev_attr_control_domains.attr,
1773 	&dev_attr_matrix.attr,
1774 	&dev_attr_guest_matrix.attr,
1775 	NULL,
1776 };
1777 
1778 static struct attribute_group vfio_ap_mdev_attr_group = {
1779 	.attrs = vfio_ap_mdev_attrs
1780 };
1781 
1782 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1783 	&vfio_ap_mdev_attr_group,
1784 	NULL
1785 };
1786 
1787 /**
1788  * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1789  * to manage AP resources for the guest whose state is represented by @kvm
1790  *
1791  * @matrix_mdev: a mediated matrix device
1792  * @kvm: reference to KVM instance
1793  *
1794  * Return: 0 if no other mediated matrix device has a reference to @kvm;
1795  * otherwise, returns an -EPERM.
1796  */
1797 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1798 				struct kvm *kvm)
1799 {
1800 	struct ap_matrix_mdev *m;
1801 
1802 	if (kvm->arch.crypto.crycbd) {
1803 		down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1804 		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1805 		up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1806 
1807 		get_update_locks_for_kvm(kvm);
1808 
1809 		list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1810 			if (m != matrix_mdev && m->kvm == kvm) {
1811 				release_update_locks_for_kvm(kvm);
1812 				return -EPERM;
1813 			}
1814 		}
1815 
1816 		kvm_get_kvm(kvm);
1817 		matrix_mdev->kvm = kvm;
1818 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1819 
1820 		release_update_locks_for_kvm(kvm);
1821 	}
1822 
1823 	return 0;
1824 }
1825 
1826 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1827 {
1828 	struct ap_queue_table *qtable = &matrix_mdev->qtable;
1829 	struct vfio_ap_queue *q;
1830 	int loop_cursor;
1831 
1832 	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1833 		if (q->saved_iova >= iova && q->saved_iova < iova + length)
1834 			vfio_ap_irq_disable(q);
1835 	}
1836 }
1837 
1838 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1839 				   u64 length)
1840 {
1841 	struct ap_matrix_mdev *matrix_mdev =
1842 		container_of(vdev, struct ap_matrix_mdev, vdev);
1843 
1844 	mutex_lock(&matrix_dev->mdevs_lock);
1845 
1846 	unmap_iova(matrix_mdev, iova, length);
1847 
1848 	mutex_unlock(&matrix_dev->mdevs_lock);
1849 }
1850 
1851 /**
1852  * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1853  * by @matrix_mdev.
1854  *
1855  * @matrix_mdev: a matrix mediated device
1856  */
1857 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1858 {
1859 	struct kvm *kvm = matrix_mdev->kvm;
1860 
1861 	if (kvm && kvm->arch.crypto.crycbd) {
1862 		down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1863 		kvm->arch.crypto.pqap_hook = NULL;
1864 		up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1865 
1866 		get_update_locks_for_kvm(kvm);
1867 
1868 		kvm_arch_crypto_clear_masks(kvm);
1869 		vfio_ap_mdev_reset_queues(matrix_mdev);
1870 		kvm_put_kvm(kvm);
1871 		matrix_mdev->kvm = NULL;
1872 
1873 		release_update_locks_for_kvm(kvm);
1874 	}
1875 }
1876 
1877 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1878 {
1879 	struct ap_queue *queue;
1880 	struct vfio_ap_queue *q = NULL;
1881 
1882 	queue = ap_get_qdev(apqn);
1883 	if (!queue)
1884 		return NULL;
1885 
1886 	if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1887 		q = dev_get_drvdata(&queue->ap_dev.device);
1888 
1889 	put_device(&queue->ap_dev.device);
1890 
1891 	return q;
1892 }
1893 
1894 static int apq_status_check(int apqn, struct ap_queue_status *status)
1895 {
1896 	switch (status->response_code) {
1897 	case AP_RESPONSE_NORMAL:
1898 	case AP_RESPONSE_DECONFIGURED:
1899 	case AP_RESPONSE_CHECKSTOPPED:
1900 		return 0;
1901 	case AP_RESPONSE_RESET_IN_PROGRESS:
1902 	case AP_RESPONSE_BUSY:
1903 		return -EBUSY;
1904 	case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
1905 	case AP_RESPONSE_ASSOC_FAILED:
1906 		/*
1907 		 * These asynchronous response codes indicate a PQAP(AAPQ)
1908 		 * instruction to associate a secret with the guest failed. All
1909 		 * subsequent AP instructions will end with the asynchronous
1910 		 * response code until the AP queue is reset; so, let's return
1911 		 * a value indicating a reset needs to be performed again.
1912 		 */
1913 		return -EAGAIN;
1914 	default:
1915 		WARN(true,
1916 		     "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1917 		     AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1918 		     status->response_code);
1919 		return -EIO;
1920 	}
1921 }
1922 
1923 #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
1924 
1925 static void apq_reset_check(struct work_struct *reset_work)
1926 {
1927 	int ret = -EBUSY, elapsed = 0;
1928 	struct ap_queue_status status;
1929 	struct vfio_ap_queue *q;
1930 
1931 	q = container_of(reset_work, struct vfio_ap_queue, reset_work);
1932 	memcpy(&status, &q->reset_status, sizeof(status));
1933 	while (true) {
1934 		msleep(AP_RESET_INTERVAL);
1935 		elapsed += AP_RESET_INTERVAL;
1936 		status = ap_tapq(q->apqn, NULL);
1937 		ret = apq_status_check(q->apqn, &status);
1938 		if (ret == -EIO)
1939 			return;
1940 		if (ret == -EBUSY) {
1941 			pr_notice_ratelimited(WAIT_MSG, elapsed,
1942 					      AP_QID_CARD(q->apqn),
1943 					      AP_QID_QUEUE(q->apqn),
1944 					      status.response_code,
1945 					      status.queue_empty,
1946 					      status.irq_enabled);
1947 		} else {
1948 			if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
1949 			    q->reset_status.response_code == AP_RESPONSE_BUSY ||
1950 			    q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
1951 			    ret == -EAGAIN) {
1952 				status = ap_zapq(q->apqn, 0);
1953 				memcpy(&q->reset_status, &status, sizeof(status));
1954 				continue;
1955 			}
1956 			if (q->saved_isc != VFIO_AP_ISC_INVALID)
1957 				vfio_ap_free_aqic_resources(q);
1958 			break;
1959 		}
1960 	}
1961 }
1962 
1963 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1964 {
1965 	struct ap_queue_status status;
1966 
1967 	if (!q)
1968 		return;
1969 	status = ap_zapq(q->apqn, 0);
1970 	memcpy(&q->reset_status, &status, sizeof(status));
1971 	switch (status.response_code) {
1972 	case AP_RESPONSE_NORMAL:
1973 	case AP_RESPONSE_RESET_IN_PROGRESS:
1974 	case AP_RESPONSE_BUSY:
1975 	case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
1976 		/*
1977 		 * Let's verify whether the ZAPQ completed successfully on a work queue.
1978 		 */
1979 		queue_work(system_long_wq, &q->reset_work);
1980 		break;
1981 	case AP_RESPONSE_DECONFIGURED:
1982 	case AP_RESPONSE_CHECKSTOPPED:
1983 		vfio_ap_free_aqic_resources(q);
1984 		break;
1985 	default:
1986 		WARN(true,
1987 		     "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1988 		     AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1989 		     status.response_code);
1990 	}
1991 }
1992 
1993 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1994 {
1995 	int ret = 0, loop_cursor;
1996 	struct vfio_ap_queue *q;
1997 
1998 	hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode)
1999 		vfio_ap_mdev_reset_queue(q);
2000 
2001 	hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode) {
2002 		flush_work(&q->reset_work);
2003 
2004 		if (q->reset_status.response_code)
2005 			ret = -EIO;
2006 	}
2007 
2008 	return ret;
2009 }
2010 
2011 static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
2012 {
2013 	int ret = 0;
2014 	struct vfio_ap_queue *q;
2015 
2016 	list_for_each_entry(q, qlist, reset_qnode)
2017 		vfio_ap_mdev_reset_queue(q);
2018 
2019 	list_for_each_entry(q, qlist, reset_qnode) {
2020 		flush_work(&q->reset_work);
2021 
2022 		if (q->reset_status.response_code)
2023 			ret = -EIO;
2024 	}
2025 
2026 	return ret;
2027 }
2028 
2029 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
2030 {
2031 	struct ap_matrix_mdev *matrix_mdev =
2032 		container_of(vdev, struct ap_matrix_mdev, vdev);
2033 
2034 	if (!vdev->kvm)
2035 		return -EINVAL;
2036 
2037 	return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
2038 }
2039 
2040 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
2041 {
2042 	struct ap_matrix_mdev *matrix_mdev =
2043 		container_of(vdev, struct ap_matrix_mdev, vdev);
2044 
2045 	vfio_ap_mdev_unset_kvm(matrix_mdev);
2046 }
2047 
2048 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
2049 {
2050 	struct device *dev = vdev->dev;
2051 	struct ap_matrix_mdev *matrix_mdev;
2052 
2053 	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
2054 
2055 	if (matrix_mdev->req_trigger) {
2056 		if (!(count % 10))
2057 			dev_notice_ratelimited(dev,
2058 					       "Relaying device request to user (#%u)\n",
2059 					       count);
2060 
2061 		eventfd_signal(matrix_mdev->req_trigger);
2062 	} else if (count == 0) {
2063 		dev_notice(dev,
2064 			   "No device request registered, blocked until released by user\n");
2065 	}
2066 }
2067 
2068 static int vfio_ap_mdev_get_device_info(unsigned long arg)
2069 {
2070 	unsigned long minsz;
2071 	struct vfio_device_info info;
2072 
2073 	minsz = offsetofend(struct vfio_device_info, num_irqs);
2074 
2075 	if (copy_from_user(&info, (void __user *)arg, minsz))
2076 		return -EFAULT;
2077 
2078 	if (info.argsz < minsz)
2079 		return -EINVAL;
2080 
2081 	info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
2082 	info.num_regions = 0;
2083 	info.num_irqs = VFIO_AP_NUM_IRQS;
2084 
2085 	return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
2086 }
2087 
2088 static ssize_t vfio_ap_get_irq_info(unsigned long arg)
2089 {
2090 	unsigned long minsz;
2091 	struct vfio_irq_info info;
2092 
2093 	minsz = offsetofend(struct vfio_irq_info, count);
2094 
2095 	if (copy_from_user(&info, (void __user *)arg, minsz))
2096 		return -EFAULT;
2097 
2098 	if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
2099 		return -EINVAL;
2100 
2101 	switch (info.index) {
2102 	case VFIO_AP_REQ_IRQ_INDEX:
2103 		info.count = 1;
2104 		info.flags = VFIO_IRQ_INFO_EVENTFD;
2105 		break;
2106 	default:
2107 		return -EINVAL;
2108 	}
2109 
2110 	return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
2111 }
2112 
2113 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
2114 {
2115 	int ret;
2116 	size_t data_size;
2117 	unsigned long minsz;
2118 
2119 	minsz = offsetofend(struct vfio_irq_set, count);
2120 
2121 	if (copy_from_user(irq_set, (void __user *)arg, minsz))
2122 		return -EFAULT;
2123 
2124 	ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
2125 						 &data_size);
2126 	if (ret)
2127 		return ret;
2128 
2129 	if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
2130 		return -EINVAL;
2131 
2132 	return 0;
2133 }
2134 
2135 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
2136 				   unsigned long arg)
2137 {
2138 	s32 fd;
2139 	void __user *data;
2140 	unsigned long minsz;
2141 	struct eventfd_ctx *req_trigger;
2142 
2143 	minsz = offsetofend(struct vfio_irq_set, count);
2144 	data = (void __user *)(arg + minsz);
2145 
2146 	if (get_user(fd, (s32 __user *)data))
2147 		return -EFAULT;
2148 
2149 	if (fd == -1) {
2150 		if (matrix_mdev->req_trigger)
2151 			eventfd_ctx_put(matrix_mdev->req_trigger);
2152 		matrix_mdev->req_trigger = NULL;
2153 	} else if (fd >= 0) {
2154 		req_trigger = eventfd_ctx_fdget(fd);
2155 		if (IS_ERR(req_trigger))
2156 			return PTR_ERR(req_trigger);
2157 
2158 		if (matrix_mdev->req_trigger)
2159 			eventfd_ctx_put(matrix_mdev->req_trigger);
2160 
2161 		matrix_mdev->req_trigger = req_trigger;
2162 	} else {
2163 		return -EINVAL;
2164 	}
2165 
2166 	return 0;
2167 }
2168 
2169 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
2170 			    unsigned long arg)
2171 {
2172 	int ret;
2173 	struct vfio_irq_set irq_set;
2174 
2175 	ret = vfio_ap_irq_set_init(&irq_set, arg);
2176 	if (ret)
2177 		return ret;
2178 
2179 	switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
2180 	case VFIO_IRQ_SET_DATA_EVENTFD:
2181 		switch (irq_set.index) {
2182 		case VFIO_AP_REQ_IRQ_INDEX:
2183 			return vfio_ap_set_request_irq(matrix_mdev, arg);
2184 		default:
2185 			return -EINVAL;
2186 		}
2187 	default:
2188 		return -EINVAL;
2189 	}
2190 }
2191 
2192 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
2193 				    unsigned int cmd, unsigned long arg)
2194 {
2195 	struct ap_matrix_mdev *matrix_mdev =
2196 		container_of(vdev, struct ap_matrix_mdev, vdev);
2197 	int ret;
2198 
2199 	mutex_lock(&matrix_dev->mdevs_lock);
2200 	switch (cmd) {
2201 	case VFIO_DEVICE_GET_INFO:
2202 		ret = vfio_ap_mdev_get_device_info(arg);
2203 		break;
2204 	case VFIO_DEVICE_RESET:
2205 		ret = vfio_ap_mdev_reset_queues(matrix_mdev);
2206 		break;
2207 	case VFIO_DEVICE_GET_IRQ_INFO:
2208 			ret = vfio_ap_get_irq_info(arg);
2209 			break;
2210 	case VFIO_DEVICE_SET_IRQS:
2211 		ret = vfio_ap_set_irqs(matrix_mdev, arg);
2212 		break;
2213 	default:
2214 		ret = -EOPNOTSUPP;
2215 		break;
2216 	}
2217 	mutex_unlock(&matrix_dev->mdevs_lock);
2218 
2219 	return ret;
2220 }
2221 
2222 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
2223 {
2224 	struct ap_matrix_mdev *matrix_mdev;
2225 	unsigned long apid = AP_QID_CARD(q->apqn);
2226 	unsigned long apqi = AP_QID_QUEUE(q->apqn);
2227 
2228 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2229 		if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
2230 		    test_bit_inv(apqi, matrix_mdev->matrix.aqm))
2231 			return matrix_mdev;
2232 	}
2233 
2234 	return NULL;
2235 }
2236 
2237 static ssize_t status_show(struct device *dev,
2238 			   struct device_attribute *attr,
2239 			   char *buf)
2240 {
2241 	ssize_t nchars = 0;
2242 	struct vfio_ap_queue *q;
2243 	unsigned long apid, apqi;
2244 	struct ap_matrix_mdev *matrix_mdev;
2245 	struct ap_device *apdev = to_ap_dev(dev);
2246 
2247 	mutex_lock(&matrix_dev->mdevs_lock);
2248 	q = dev_get_drvdata(&apdev->device);
2249 	matrix_mdev = vfio_ap_mdev_for_queue(q);
2250 
2251 	/* If the queue is assigned to the matrix mediated device, then
2252 	 * determine whether it is passed through to a guest; otherwise,
2253 	 * indicate that it is unassigned.
2254 	 */
2255 	if (matrix_mdev) {
2256 		apid = AP_QID_CARD(q->apqn);
2257 		apqi = AP_QID_QUEUE(q->apqn);
2258 		/*
2259 		 * If the queue is passed through to the guest, then indicate
2260 		 * that it is in use; otherwise, indicate that it is
2261 		 * merely assigned to a matrix mediated device.
2262 		 */
2263 		if (matrix_mdev->kvm &&
2264 		    test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2265 		    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
2266 			nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
2267 					   AP_QUEUE_IN_USE);
2268 		else
2269 			nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
2270 					   AP_QUEUE_ASSIGNED);
2271 	} else {
2272 		nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
2273 				   AP_QUEUE_UNASSIGNED);
2274 	}
2275 
2276 	mutex_unlock(&matrix_dev->mdevs_lock);
2277 
2278 	return nchars;
2279 }
2280 
2281 static DEVICE_ATTR_RO(status);
2282 
2283 static struct attribute *vfio_queue_attrs[] = {
2284 	&dev_attr_status.attr,
2285 	NULL,
2286 };
2287 
2288 static const struct attribute_group vfio_queue_attr_group = {
2289 	.attrs = vfio_queue_attrs,
2290 };
2291 
2292 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
2293 	.init = vfio_ap_mdev_init_dev,
2294 	.open_device = vfio_ap_mdev_open_device,
2295 	.close_device = vfio_ap_mdev_close_device,
2296 	.ioctl = vfio_ap_mdev_ioctl,
2297 	.dma_unmap = vfio_ap_mdev_dma_unmap,
2298 	.bind_iommufd = vfio_iommufd_emulated_bind,
2299 	.unbind_iommufd = vfio_iommufd_emulated_unbind,
2300 	.attach_ioas = vfio_iommufd_emulated_attach_ioas,
2301 	.detach_ioas = vfio_iommufd_emulated_detach_ioas,
2302 	.request = vfio_ap_mdev_request
2303 };
2304 
2305 static struct mdev_driver vfio_ap_matrix_driver = {
2306 	.device_api = VFIO_DEVICE_API_AP_STRING,
2307 	.max_instances = MAX_ZDEV_ENTRIES_EXT,
2308 	.driver = {
2309 		.name = "vfio_ap_mdev",
2310 		.owner = THIS_MODULE,
2311 		.mod_name = KBUILD_MODNAME,
2312 		.dev_groups = vfio_ap_mdev_attr_groups,
2313 	},
2314 	.probe = vfio_ap_mdev_probe,
2315 	.remove = vfio_ap_mdev_remove,
2316 };
2317 
2318 int vfio_ap_mdev_register(void)
2319 {
2320 	int ret;
2321 
2322 	ret = mdev_register_driver(&vfio_ap_matrix_driver);
2323 	if (ret)
2324 		return ret;
2325 
2326 	matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
2327 	matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
2328 	matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
2329 	ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2330 				   &vfio_ap_matrix_driver,
2331 				   matrix_dev->mdev_types, 1);
2332 	if (ret)
2333 		goto err_driver;
2334 	return 0;
2335 
2336 err_driver:
2337 	mdev_unregister_driver(&vfio_ap_matrix_driver);
2338 	return ret;
2339 }
2340 
2341 void vfio_ap_mdev_unregister(void)
2342 {
2343 	mdev_unregister_parent(&matrix_dev->parent);
2344 	mdev_unregister_driver(&vfio_ap_matrix_driver);
2345 }
2346 
2347 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2348 {
2349 	int ret;
2350 	struct vfio_ap_queue *q;
2351 	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2352 	struct ap_matrix_mdev *matrix_mdev;
2353 
2354 	ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2355 	if (ret)
2356 		return ret;
2357 
2358 	q = kzalloc(sizeof(*q), GFP_KERNEL);
2359 	if (!q) {
2360 		ret = -ENOMEM;
2361 		goto err_remove_group;
2362 	}
2363 
2364 	q->apqn = to_ap_queue(&apdev->device)->qid;
2365 	q->saved_isc = VFIO_AP_ISC_INVALID;
2366 	memset(&q->reset_status, 0, sizeof(q->reset_status));
2367 	INIT_WORK(&q->reset_work, apq_reset_check);
2368 	matrix_mdev = get_update_locks_by_apqn(q->apqn);
2369 
2370 	if (matrix_mdev) {
2371 		vfio_ap_mdev_link_queue(matrix_mdev, q);
2372 
2373 		/*
2374 		 * If we're in the process of handling the adding of adapters or
2375 		 * domains to the host's AP configuration, then let the
2376 		 * vfio_ap device driver's on_scan_complete callback filter the
2377 		 * matrix and update the guest's AP configuration after all of
2378 		 * the new queue devices are probed.
2379 		 */
2380 		if (!bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) ||
2381 		    !bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS))
2382 			goto done;
2383 
2384 		if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
2385 			vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2386 			reset_queues_for_apids(matrix_mdev, apm_filtered);
2387 		}
2388 	}
2389 
2390 done:
2391 	dev_set_drvdata(&apdev->device, q);
2392 	release_update_locks_for_mdev(matrix_mdev);
2393 
2394 	return ret;
2395 
2396 err_remove_group:
2397 	sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2398 	return ret;
2399 }
2400 
2401 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2402 {
2403 	unsigned long apid, apqi;
2404 	struct vfio_ap_queue *q;
2405 	struct ap_matrix_mdev *matrix_mdev;
2406 
2407 	sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2408 	q = dev_get_drvdata(&apdev->device);
2409 	get_update_locks_for_queue(q);
2410 	matrix_mdev = q->matrix_mdev;
2411 	apid = AP_QID_CARD(q->apqn);
2412 	apqi = AP_QID_QUEUE(q->apqn);
2413 
2414 	if (matrix_mdev) {
2415 		/* If the queue is assigned to the guest's AP configuration */
2416 		if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2417 		    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2418 			/*
2419 			 * Since the queues are defined via a matrix of adapters
2420 			 * and domains, it is not possible to hot unplug a
2421 			 * single queue; so, let's unplug the adapter.
2422 			 */
2423 			clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2424 			vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2425 			reset_queues_for_apid(matrix_mdev, apid);
2426 			goto done;
2427 		}
2428 	}
2429 
2430 	/*
2431 	 * If the queue is not in the host's AP configuration, then resetting
2432 	 * it will fail with response code 01, (APQN not valid); so, let's make
2433 	 * sure it is in the host's config.
2434 	 */
2435 	if (test_bit_inv(apid, (unsigned long *)matrix_dev->info.apm) &&
2436 	    test_bit_inv(apqi, (unsigned long *)matrix_dev->info.aqm)) {
2437 		vfio_ap_mdev_reset_queue(q);
2438 		flush_work(&q->reset_work);
2439 	}
2440 
2441 done:
2442 	if (matrix_mdev)
2443 		vfio_ap_unlink_queue_fr_mdev(q);
2444 
2445 	dev_set_drvdata(&apdev->device, NULL);
2446 	kfree(q);
2447 	release_update_locks_for_mdev(matrix_mdev);
2448 }
2449 
2450 /**
2451  * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2452  *				 assigned to a mediated device under the control
2453  *				 of the vfio_ap device driver.
2454  *
2455  * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2456  * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2457  *
2458  * Return:
2459  *	* -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2460  *	  assigned to a mediated device under the control of the vfio_ap
2461  *	  device driver.
2462  *	* Otherwise, return 0.
2463  */
2464 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2465 {
2466 	int ret;
2467 
2468 	mutex_lock(&matrix_dev->guests_lock);
2469 	mutex_lock(&matrix_dev->mdevs_lock);
2470 	ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2471 	mutex_unlock(&matrix_dev->mdevs_lock);
2472 	mutex_unlock(&matrix_dev->guests_lock);
2473 
2474 	return ret;
2475 }
2476 
2477 /**
2478  * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2479  *				 domains that have been removed from the host's
2480  *				 AP configuration from a guest.
2481  *
2482  * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2483  * @aprem: the adapters that have been removed from the host's AP configuration
2484  * @aqrem: the domains that have been removed from the host's AP configuration
2485  * @cdrem: the control domains that have been removed from the host's AP
2486  *	   configuration.
2487  */
2488 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2489 					unsigned long *aprem,
2490 					unsigned long *aqrem,
2491 					unsigned long *cdrem)
2492 {
2493 	int do_hotplug = 0;
2494 
2495 	if (!bitmap_empty(aprem, AP_DEVICES)) {
2496 		do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2497 					    matrix_mdev->shadow_apcb.apm,
2498 					    aprem, AP_DEVICES);
2499 	}
2500 
2501 	if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2502 		do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2503 					    matrix_mdev->shadow_apcb.aqm,
2504 					    aqrem, AP_DEVICES);
2505 	}
2506 
2507 	if (!bitmap_empty(cdrem, AP_DOMAINS))
2508 		do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2509 					    matrix_mdev->shadow_apcb.adm,
2510 					    cdrem, AP_DOMAINS);
2511 
2512 	if (do_hotplug)
2513 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2514 }
2515 
2516 /**
2517  * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2518  *			     domains and control domains that have been removed
2519  *			     from the host AP configuration and unplugs them
2520  *			     from those guests.
2521  *
2522  * @ap_remove:	bitmap specifying which adapters have been removed from the host
2523  *		config.
2524  * @aq_remove:	bitmap specifying which domains have been removed from the host
2525  *		config.
2526  * @cd_remove:	bitmap specifying which control domains have been removed from
2527  *		the host config.
2528  */
2529 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2530 				    unsigned long *aq_remove,
2531 				    unsigned long *cd_remove)
2532 {
2533 	struct ap_matrix_mdev *matrix_mdev;
2534 	DECLARE_BITMAP(aprem, AP_DEVICES);
2535 	DECLARE_BITMAP(aqrem, AP_DOMAINS);
2536 	DECLARE_BITMAP(cdrem, AP_DOMAINS);
2537 	int do_remove = 0;
2538 
2539 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2540 		mutex_lock(&matrix_mdev->kvm->lock);
2541 		mutex_lock(&matrix_dev->mdevs_lock);
2542 
2543 		do_remove |= bitmap_and(aprem, ap_remove,
2544 					  matrix_mdev->matrix.apm,
2545 					  AP_DEVICES);
2546 		do_remove |= bitmap_and(aqrem, aq_remove,
2547 					  matrix_mdev->matrix.aqm,
2548 					  AP_DOMAINS);
2549 		do_remove |= bitmap_andnot(cdrem, cd_remove,
2550 					     matrix_mdev->matrix.adm,
2551 					     AP_DOMAINS);
2552 
2553 		if (do_remove)
2554 			vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2555 						    cdrem);
2556 
2557 		mutex_unlock(&matrix_dev->mdevs_lock);
2558 		mutex_unlock(&matrix_mdev->kvm->lock);
2559 	}
2560 }
2561 
2562 /**
2563  * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2564  *				control domains from the host AP configuration
2565  *				by unplugging them from the guests that are
2566  *				using them.
2567  * @cur_config_info: the current host AP configuration information
2568  * @prev_config_info: the previous host AP configuration information
2569  */
2570 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2571 				       struct ap_config_info *prev_config_info)
2572 {
2573 	int do_remove;
2574 	DECLARE_BITMAP(aprem, AP_DEVICES);
2575 	DECLARE_BITMAP(aqrem, AP_DOMAINS);
2576 	DECLARE_BITMAP(cdrem, AP_DOMAINS);
2577 
2578 	do_remove = bitmap_andnot(aprem,
2579 				  (unsigned long *)prev_config_info->apm,
2580 				  (unsigned long *)cur_config_info->apm,
2581 				  AP_DEVICES);
2582 	do_remove |= bitmap_andnot(aqrem,
2583 				   (unsigned long *)prev_config_info->aqm,
2584 				   (unsigned long *)cur_config_info->aqm,
2585 				   AP_DEVICES);
2586 	do_remove |= bitmap_andnot(cdrem,
2587 				   (unsigned long *)prev_config_info->adm,
2588 				   (unsigned long *)cur_config_info->adm,
2589 				   AP_DEVICES);
2590 
2591 	if (do_remove)
2592 		vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2593 }
2594 
2595 /**
2596  * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2597  *				 are older than AP type 10 (CEX4).
2598  * @apm: a bitmap of the APIDs to examine
2599  * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2600  */
2601 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2602 {
2603 	bool apid_cleared;
2604 	struct ap_queue_status status;
2605 	unsigned long apid, apqi;
2606 	struct ap_tapq_hwinfo info;
2607 
2608 	for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2609 		apid_cleared = false;
2610 
2611 		for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2612 			status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2613 			switch (status.response_code) {
2614 			/*
2615 			 * According to the architecture in each case
2616 			 * below, the queue's info should be filled.
2617 			 */
2618 			case AP_RESPONSE_NORMAL:
2619 			case AP_RESPONSE_RESET_IN_PROGRESS:
2620 			case AP_RESPONSE_DECONFIGURED:
2621 			case AP_RESPONSE_CHECKSTOPPED:
2622 			case AP_RESPONSE_BUSY:
2623 				/*
2624 				 * The vfio_ap device driver only
2625 				 * supports CEX4 and newer adapters, so
2626 				 * remove the APID if the adapter is
2627 				 * older than a CEX4.
2628 				 */
2629 				if (info.at < AP_DEVICE_TYPE_CEX4) {
2630 					clear_bit_inv(apid, apm);
2631 					apid_cleared = true;
2632 				}
2633 
2634 				break;
2635 
2636 			default:
2637 				/*
2638 				 * If we don't know the adapter type,
2639 				 * clear its APID since it can't be
2640 				 * determined whether the vfio_ap
2641 				 * device driver supports it.
2642 				 */
2643 				clear_bit_inv(apid, apm);
2644 				apid_cleared = true;
2645 				break;
2646 			}
2647 
2648 			/*
2649 			 * If we've already cleared the APID from the apm, there
2650 			 * is no need to continue examining the remainin AP
2651 			 * queues to determine the type of the adapter.
2652 			 */
2653 			if (apid_cleared)
2654 				continue;
2655 		}
2656 	}
2657 }
2658 
2659 /**
2660  * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2661  *			  control domains that have been added to the host's
2662  *			  AP configuration for each matrix mdev to which they
2663  *			  are assigned.
2664  *
2665  * @apm_add: a bitmap specifying the adapters that have been added to the AP
2666  *	     configuration.
2667  * @aqm_add: a bitmap specifying the domains that have been added to the AP
2668  *	     configuration.
2669  * @adm_add: a bitmap specifying the control domains that have been added to the
2670  *	     AP configuration.
2671  */
2672 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2673 				 unsigned long *adm_add)
2674 {
2675 	struct ap_matrix_mdev *matrix_mdev;
2676 
2677 	if (list_empty(&matrix_dev->mdev_list))
2678 		return;
2679 
2680 	vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2681 
2682 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2683 		bitmap_and(matrix_mdev->apm_add,
2684 			   matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2685 		bitmap_and(matrix_mdev->aqm_add,
2686 			   matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2687 		bitmap_and(matrix_mdev->adm_add,
2688 			   matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2689 	}
2690 }
2691 
2692 /**
2693  * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2694  *			     control domains to the host AP configuration
2695  *			     by updating the bitmaps that specify what adapters,
2696  *			     domains and control domains have been added so they
2697  *			     can be hot plugged into the guest when the AP bus
2698  *			     scan completes (see vfio_ap_on_scan_complete
2699  *			     function).
2700  * @cur_config_info: the current AP configuration information
2701  * @prev_config_info: the previous AP configuration information
2702  */
2703 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2704 				    struct ap_config_info *prev_config_info)
2705 {
2706 	bool do_add;
2707 	DECLARE_BITMAP(apm_add, AP_DEVICES);
2708 	DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2709 	DECLARE_BITMAP(adm_add, AP_DOMAINS);
2710 
2711 	do_add = bitmap_andnot(apm_add,
2712 			       (unsigned long *)cur_config_info->apm,
2713 			       (unsigned long *)prev_config_info->apm,
2714 			       AP_DEVICES);
2715 	do_add |= bitmap_andnot(aqm_add,
2716 				(unsigned long *)cur_config_info->aqm,
2717 				(unsigned long *)prev_config_info->aqm,
2718 				AP_DOMAINS);
2719 	do_add |= bitmap_andnot(adm_add,
2720 				(unsigned long *)cur_config_info->adm,
2721 				(unsigned long *)prev_config_info->adm,
2722 				AP_DOMAINS);
2723 
2724 	if (do_add)
2725 		vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2726 }
2727 
2728 /**
2729  * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2730  *			    configuration.
2731  *
2732  * @cur_cfg_info: the current host AP configuration
2733  * @prev_cfg_info: the previous host AP configuration
2734  */
2735 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2736 			    struct ap_config_info *prev_cfg_info)
2737 {
2738 	if (!cur_cfg_info || !prev_cfg_info)
2739 		return;
2740 
2741 	mutex_lock(&matrix_dev->guests_lock);
2742 
2743 	vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2744 	vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2745 	memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2746 
2747 	mutex_unlock(&matrix_dev->guests_lock);
2748 }
2749 
2750 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2751 {
2752 	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2753 	bool filter_domains, filter_adapters, filter_cdoms, do_hotplug = false;
2754 
2755 	mutex_lock(&matrix_mdev->kvm->lock);
2756 	mutex_lock(&matrix_dev->mdevs_lock);
2757 
2758 	filter_adapters = bitmap_intersects(matrix_mdev->matrix.apm,
2759 					    matrix_mdev->apm_add, AP_DEVICES);
2760 	filter_domains = bitmap_intersects(matrix_mdev->matrix.aqm,
2761 					   matrix_mdev->aqm_add, AP_DOMAINS);
2762 	filter_cdoms = bitmap_intersects(matrix_mdev->matrix.adm,
2763 					 matrix_mdev->adm_add, AP_DOMAINS);
2764 
2765 	if (filter_adapters || filter_domains)
2766 		do_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
2767 
2768 	if (filter_cdoms)
2769 		do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2770 
2771 	if (do_hotplug)
2772 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2773 
2774 	reset_queues_for_apids(matrix_mdev, apm_filtered);
2775 
2776 	mutex_unlock(&matrix_dev->mdevs_lock);
2777 	mutex_unlock(&matrix_mdev->kvm->lock);
2778 }
2779 
2780 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2781 			      struct ap_config_info *old_config_info)
2782 {
2783 	struct ap_matrix_mdev *matrix_mdev;
2784 
2785 	mutex_lock(&matrix_dev->guests_lock);
2786 
2787 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2788 		if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2789 		    bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2790 		    bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2791 			continue;
2792 
2793 		vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2794 		bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2795 		bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2796 		bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2797 	}
2798 
2799 	mutex_unlock(&matrix_dev->guests_lock);
2800 }
2801