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 MAX_RESET_CHECK_WAIT	200	/* Sleep max 200ms for reset check	*/
34 #define AP_RESET_INTERVAL		20	/* Reset sleep interval (20ms)		*/
35 
36 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable);
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 int 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 /**
364  * vfio_ap_irq_enable - Enable Interruption for a APQN
365  *
366  * @q:	 the vfio_ap_queue holding AQIC parameters
367  * @isc: the guest ISC to register with the GIB interface
368  * @vcpu: the vcpu object containing the registers specifying the parameters
369  *	  passed to the PQAP(AQIC) instruction.
370  *
371  * Pin the NIB saved in *q
372  * Register the guest ISC to GIB interface and retrieve the
373  * host ISC to issue the host side PQAP/AQIC
374  *
375  * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
376  * vfio_pin_pages failed.
377  *
378  * Otherwise return the ap_queue_status returned by the ap_aqic(),
379  * all retry handling will be done by the guest.
380  *
381  * Return: &struct ap_queue_status
382  */
383 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
384 						 int isc,
385 						 struct kvm_vcpu *vcpu)
386 {
387 	union ap_qirq_ctrl aqic_gisa = { .value = 0 };
388 	struct ap_queue_status status = {};
389 	struct kvm_s390_gisa *gisa;
390 	struct page *h_page;
391 	int nisc;
392 	struct kvm *kvm;
393 	phys_addr_t h_nib;
394 	dma_addr_t nib;
395 	int ret;
396 
397 	/* Verify that the notification indicator byte address is valid */
398 	if (vfio_ap_validate_nib(vcpu, &nib)) {
399 		VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
400 				 __func__, &nib, q->apqn);
401 
402 		status.response_code = AP_RESPONSE_INVALID_ADDRESS;
403 		return status;
404 	}
405 
406 	ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
407 			     IOMMU_READ | IOMMU_WRITE, &h_page);
408 	switch (ret) {
409 	case 1:
410 		break;
411 	default:
412 		VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
413 				 "nib=%pad, apqn=%#04x\n",
414 				 __func__, ret, &nib, q->apqn);
415 
416 		status.response_code = AP_RESPONSE_INVALID_ADDRESS;
417 		return status;
418 	}
419 
420 	kvm = q->matrix_mdev->kvm;
421 	gisa = kvm->arch.gisa_int.origin;
422 
423 	h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
424 	aqic_gisa.gisc = isc;
425 
426 	nisc = kvm_s390_gisc_register(kvm, isc);
427 	if (nisc < 0) {
428 		VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
429 				 __func__, nisc, isc, q->apqn);
430 
431 		status.response_code = AP_RESPONSE_INVALID_GISA;
432 		return status;
433 	}
434 
435 	aqic_gisa.isc = nisc;
436 	aqic_gisa.ir = 1;
437 	aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
438 
439 	status = ap_aqic(q->apqn, aqic_gisa, h_nib);
440 	switch (status.response_code) {
441 	case AP_RESPONSE_NORMAL:
442 		/* See if we did clear older IRQ configuration */
443 		vfio_ap_free_aqic_resources(q);
444 		q->saved_iova = nib;
445 		q->saved_isc = isc;
446 		break;
447 	case AP_RESPONSE_OTHERWISE_CHANGED:
448 		/* We could not modify IRQ settings: clear new configuration */
449 		vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
450 		kvm_s390_gisc_unregister(kvm, isc);
451 		break;
452 	default:
453 		pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
454 			status.response_code);
455 		vfio_ap_irq_disable(q);
456 		break;
457 	}
458 
459 	if (status.response_code != AP_RESPONSE_NORMAL) {
460 		VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
461 				 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
462 				 "gisa=%#x, isc=%#x, apqn=%#04x\n",
463 				 __func__, status.response_code,
464 				 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
465 				 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
466 				 q->apqn);
467 	}
468 
469 	return status;
470 }
471 
472 /**
473  * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
474  *				of big endian elements that can be passed by
475  *				value to an s390dbf sprintf event function to
476  *				format a UUID string.
477  *
478  * @guid: the object containing the little endian guid
479  * @uuid: a six-element array of long values that can be passed by value as
480  *	  arguments for a formatting string specifying a UUID.
481  *
482  * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
483  * event functions if the memory for the passed string is available as long as
484  * the debug feature exists. Since a mediated device can be removed at any
485  * time, it's name can not be used because %s passes the reference to the string
486  * in memory and the reference will go stale once the device is removed .
487  *
488  * The s390dbf string formatting function allows a maximum of 9 arguments for a
489  * message to be displayed in the 'sprintf' view. In order to use the bytes
490  * comprising the mediated device's UUID to display the mediated device name,
491  * they will have to be converted into an array whose elements can be passed by
492  * value to sprintf. For example:
493  *
494  * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
495  * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
496  * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
497  * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
498  */
499 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
500 {
501 	/*
502 	 * The input guid is ordered in little endian, so it needs to be
503 	 * reordered for displaying a UUID as a string. This specifies the
504 	 * guid indices in proper order.
505 	 */
506 	uuid[0] = le32_to_cpup((__le32 *)guid);
507 	uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
508 	uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
509 	uuid[3] = *((__u16 *)&guid->b[8]);
510 	uuid[4] = *((__u16 *)&guid->b[10]);
511 	uuid[5] = *((__u32 *)&guid->b[12]);
512 }
513 
514 /**
515  * handle_pqap - PQAP instruction callback
516  *
517  * @vcpu: The vcpu on which we received the PQAP instruction
518  *
519  * Get the general register contents to initialize internal variables.
520  * REG[0]: APQN
521  * REG[1]: IR and ISC
522  * REG[2]: NIB
523  *
524  * Response.status may be set to following Response Code:
525  * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
526  * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
527  * - AP_RESPONSE_NORMAL (0) : in case of success
528  *   Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
529  * We take the matrix_dev lock to ensure serialization on queues and
530  * mediated device access.
531  *
532  * Return: 0 if we could handle the request inside KVM.
533  * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
534  */
535 static int handle_pqap(struct kvm_vcpu *vcpu)
536 {
537 	uint64_t status;
538 	uint16_t apqn;
539 	unsigned long uuid[6];
540 	struct vfio_ap_queue *q;
541 	struct ap_queue_status qstatus = {
542 			       .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
543 	struct ap_matrix_mdev *matrix_mdev;
544 
545 	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
546 
547 	/* If we do not use the AIV facility just go to userland */
548 	if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
549 		VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
550 				 __func__, apqn, vcpu->arch.sie_block->eca);
551 
552 		return -EOPNOTSUPP;
553 	}
554 
555 	mutex_lock(&matrix_dev->mdevs_lock);
556 
557 	if (!vcpu->kvm->arch.crypto.pqap_hook) {
558 		VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
559 				 __func__, apqn);
560 
561 		goto out_unlock;
562 	}
563 
564 	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
565 				   struct ap_matrix_mdev, pqap_hook);
566 
567 	/* If the there is no guest using the mdev, there is nothing to do */
568 	if (!matrix_mdev->kvm) {
569 		vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
570 		VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
571 				 __func__, uuid[0],  uuid[1], uuid[2],
572 				 uuid[3], uuid[4], uuid[5], apqn);
573 		goto out_unlock;
574 	}
575 
576 	q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
577 	if (!q) {
578 		VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
579 				 __func__, AP_QID_CARD(apqn),
580 				 AP_QID_QUEUE(apqn));
581 		goto out_unlock;
582 	}
583 
584 	status = vcpu->run->s.regs.gprs[1];
585 
586 	/* If IR bit(16) is set we enable the interrupt */
587 	if ((status >> (63 - 16)) & 0x01)
588 		qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
589 	else
590 		qstatus = vfio_ap_irq_disable(q);
591 
592 out_unlock:
593 	memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
594 	vcpu->run->s.regs.gprs[1] >>= 32;
595 	mutex_unlock(&matrix_dev->mdevs_lock);
596 	return 0;
597 }
598 
599 static void vfio_ap_matrix_init(struct ap_config_info *info,
600 				struct ap_matrix *matrix)
601 {
602 	matrix->apm_max = info->apxa ? info->na : 63;
603 	matrix->aqm_max = info->apxa ? info->nd : 15;
604 	matrix->adm_max = info->apxa ? info->nd : 15;
605 }
606 
607 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
608 {
609 	if (matrix_mdev->kvm)
610 		kvm_arch_crypto_set_masks(matrix_mdev->kvm,
611 					  matrix_mdev->shadow_apcb.apm,
612 					  matrix_mdev->shadow_apcb.aqm,
613 					  matrix_mdev->shadow_apcb.adm);
614 }
615 
616 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
617 {
618 	DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
619 
620 	bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
621 	bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
622 		   (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
623 
624 	return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
625 			     AP_DOMAINS);
626 }
627 
628 /*
629  * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
630  *				to ensure no queue devices are passed through to
631  *				the guest that are not bound to the vfio_ap
632  *				device driver.
633  *
634  * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
635  *
636  * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
637  *	 driver, its APID will be filtered from the guest's APCB. The matrix
638  *	 structure precludes filtering an individual APQN, so its APID will be
639  *	 filtered.
640  *
641  * Return: a boolean value indicating whether the KVM guest's APCB was changed
642  *	   by the filtering or not.
643  */
644 static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm,
645 				       struct ap_matrix_mdev *matrix_mdev)
646 {
647 	unsigned long apid, apqi, apqn;
648 	DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
649 	DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
650 	struct vfio_ap_queue *q;
651 
652 	bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
653 	bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
654 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
655 
656 	/*
657 	 * Copy the adapters, domains and control domains to the shadow_apcb
658 	 * from the matrix mdev, but only those that are assigned to the host's
659 	 * AP configuration.
660 	 */
661 	bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
662 		   (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
663 	bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
664 		   (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
665 
666 	for_each_set_bit_inv(apid, apm, AP_DEVICES) {
667 		for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
668 			/*
669 			 * If the APQN is not bound to the vfio_ap device
670 			 * driver, then we can't assign it to the guest's
671 			 * AP configuration. The AP architecture won't
672 			 * allow filtering of a single APQN, so let's filter
673 			 * the APID since an adapter represents a physical
674 			 * hardware device.
675 			 */
676 			apqn = AP_MKQID(apid, apqi);
677 			q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
678 			if (!q || q->reset_rc) {
679 				clear_bit_inv(apid,
680 					      matrix_mdev->shadow_apcb.apm);
681 				break;
682 			}
683 		}
684 	}
685 
686 	return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
687 			     AP_DEVICES) ||
688 	       !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
689 			     AP_DOMAINS);
690 }
691 
692 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
693 {
694 	struct ap_matrix_mdev *matrix_mdev =
695 		container_of(vdev, struct ap_matrix_mdev, vdev);
696 
697 	matrix_mdev->mdev = to_mdev_device(vdev->dev);
698 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
699 	matrix_mdev->pqap_hook = handle_pqap;
700 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
701 	hash_init(matrix_mdev->qtable.queues);
702 
703 	return 0;
704 }
705 
706 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
707 {
708 	struct ap_matrix_mdev *matrix_mdev;
709 	int ret;
710 
711 	matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
712 					&vfio_ap_matrix_dev_ops);
713 	if (IS_ERR(matrix_mdev))
714 		return PTR_ERR(matrix_mdev);
715 
716 	ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
717 	if (ret)
718 		goto err_put_vdev;
719 	matrix_mdev->req_trigger = NULL;
720 	dev_set_drvdata(&mdev->dev, matrix_mdev);
721 	mutex_lock(&matrix_dev->mdevs_lock);
722 	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
723 	mutex_unlock(&matrix_dev->mdevs_lock);
724 	return 0;
725 
726 err_put_vdev:
727 	vfio_put_device(&matrix_mdev->vdev);
728 	return ret;
729 }
730 
731 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
732 				    struct vfio_ap_queue *q)
733 {
734 	if (q) {
735 		q->matrix_mdev = matrix_mdev;
736 		hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
737 	}
738 }
739 
740 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
741 {
742 	struct vfio_ap_queue *q;
743 
744 	q = vfio_ap_find_queue(apqn);
745 	vfio_ap_mdev_link_queue(matrix_mdev, q);
746 }
747 
748 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
749 {
750 	hash_del(&q->mdev_qnode);
751 }
752 
753 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
754 {
755 	q->matrix_mdev = NULL;
756 }
757 
758 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
759 {
760 	struct vfio_ap_queue *q;
761 	unsigned long apid, apqi;
762 
763 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
764 		for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
765 				     AP_DOMAINS) {
766 			q = vfio_ap_mdev_get_queue(matrix_mdev,
767 						   AP_MKQID(apid, apqi));
768 			if (q)
769 				q->matrix_mdev = NULL;
770 		}
771 	}
772 }
773 
774 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
775 {
776 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
777 
778 	vfio_unregister_group_dev(&matrix_mdev->vdev);
779 
780 	mutex_lock(&matrix_dev->guests_lock);
781 	mutex_lock(&matrix_dev->mdevs_lock);
782 	vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
783 	vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
784 	list_del(&matrix_mdev->node);
785 	mutex_unlock(&matrix_dev->mdevs_lock);
786 	mutex_unlock(&matrix_dev->guests_lock);
787 	vfio_put_device(&matrix_mdev->vdev);
788 }
789 
790 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
791 			 "already assigned to %s"
792 
793 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
794 					 unsigned long *apm,
795 					 unsigned long *aqm)
796 {
797 	unsigned long apid, apqi;
798 	const struct device *dev = mdev_dev(matrix_mdev->mdev);
799 	const char *mdev_name = dev_name(dev);
800 
801 	for_each_set_bit_inv(apid, apm, AP_DEVICES)
802 		for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
803 			dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
804 }
805 
806 /**
807  * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
808  *
809  * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
810  * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
811  *
812  * Verifies that each APQN derived from the Cartesian product of a bitmap of
813  * AP adapter IDs and AP queue indexes is not configured for any matrix
814  * mediated device. AP queue sharing is not allowed.
815  *
816  * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
817  */
818 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
819 					  unsigned long *mdev_aqm)
820 {
821 	struct ap_matrix_mdev *matrix_mdev;
822 	DECLARE_BITMAP(apm, AP_DEVICES);
823 	DECLARE_BITMAP(aqm, AP_DOMAINS);
824 
825 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
826 		/*
827 		 * If the input apm and aqm are fields of the matrix_mdev
828 		 * object, then move on to the next matrix_mdev.
829 		 */
830 		if (mdev_apm == matrix_mdev->matrix.apm &&
831 		    mdev_aqm == matrix_mdev->matrix.aqm)
832 			continue;
833 
834 		memset(apm, 0, sizeof(apm));
835 		memset(aqm, 0, sizeof(aqm));
836 
837 		/*
838 		 * We work on full longs, as we can only exclude the leftover
839 		 * bits in non-inverse order. The leftover is all zeros.
840 		 */
841 		if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
842 				AP_DEVICES))
843 			continue;
844 
845 		if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
846 				AP_DOMAINS))
847 			continue;
848 
849 		vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
850 
851 		return -EADDRINUSE;
852 	}
853 
854 	return 0;
855 }
856 
857 /**
858  * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
859  *				 not reserved for the default zcrypt driver and
860  *				 are not assigned to another mdev.
861  *
862  * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
863  *
864  * Return: One of the following values:
865  * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
866  *   most likely -EBUSY indicating the ap_perms_mutex lock is already held.
867  * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
868  *		   zcrypt default driver.
869  * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
870  * o A zero indicating validation succeeded.
871  */
872 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
873 {
874 	if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
875 					       matrix_mdev->matrix.aqm))
876 		return -EADDRNOTAVAIL;
877 
878 	return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
879 					      matrix_mdev->matrix.aqm);
880 }
881 
882 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
883 				      unsigned long apid)
884 {
885 	unsigned long apqi;
886 
887 	for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
888 		vfio_ap_mdev_link_apqn(matrix_mdev,
889 				       AP_MKQID(apid, apqi));
890 }
891 
892 /**
893  * assign_adapter_store - parses the APID from @buf and sets the
894  * corresponding bit in the mediated matrix device's APM
895  *
896  * @dev:	the matrix device
897  * @attr:	the mediated matrix device's assign_adapter attribute
898  * @buf:	a buffer containing the AP adapter number (APID) to
899  *		be assigned
900  * @count:	the number of bytes in @buf
901  *
902  * Return: the number of bytes processed if the APID is valid; otherwise,
903  * returns one of the following errors:
904  *
905  *	1. -EINVAL
906  *	   The APID is not a valid number
907  *
908  *	2. -ENODEV
909  *	   The APID exceeds the maximum value configured for the system
910  *
911  *	3. -EADDRNOTAVAIL
912  *	   An APQN derived from the cross product of the APID being assigned
913  *	   and the APQIs previously assigned is not bound to the vfio_ap device
914  *	   driver; or, if no APQIs have yet been assigned, the APID is not
915  *	   contained in an APQN bound to the vfio_ap device driver.
916  *
917  *	4. -EADDRINUSE
918  *	   An APQN derived from the cross product of the APID being assigned
919  *	   and the APQIs previously assigned is being used by another mediated
920  *	   matrix device
921  *
922  *	5. -EAGAIN
923  *	   A lock required to validate the mdev's AP configuration could not
924  *	   be obtained.
925  */
926 static ssize_t assign_adapter_store(struct device *dev,
927 				    struct device_attribute *attr,
928 				    const char *buf, size_t count)
929 {
930 	int ret;
931 	unsigned long apid;
932 	DECLARE_BITMAP(apm_delta, AP_DEVICES);
933 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
934 
935 	mutex_lock(&ap_perms_mutex);
936 	get_update_locks_for_mdev(matrix_mdev);
937 
938 	ret = kstrtoul(buf, 0, &apid);
939 	if (ret)
940 		goto done;
941 
942 	if (apid > matrix_mdev->matrix.apm_max) {
943 		ret = -ENODEV;
944 		goto done;
945 	}
946 
947 	if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
948 		ret = count;
949 		goto done;
950 	}
951 
952 	set_bit_inv(apid, matrix_mdev->matrix.apm);
953 
954 	ret = vfio_ap_mdev_validate_masks(matrix_mdev);
955 	if (ret) {
956 		clear_bit_inv(apid, matrix_mdev->matrix.apm);
957 		goto done;
958 	}
959 
960 	vfio_ap_mdev_link_adapter(matrix_mdev, apid);
961 	memset(apm_delta, 0, sizeof(apm_delta));
962 	set_bit_inv(apid, apm_delta);
963 
964 	if (vfio_ap_mdev_filter_matrix(apm_delta,
965 				       matrix_mdev->matrix.aqm, matrix_mdev))
966 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
967 
968 	ret = count;
969 done:
970 	release_update_locks_for_mdev(matrix_mdev);
971 	mutex_unlock(&ap_perms_mutex);
972 
973 	return ret;
974 }
975 static DEVICE_ATTR_WO(assign_adapter);
976 
977 static struct vfio_ap_queue
978 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
979 			     unsigned long apid, unsigned long apqi)
980 {
981 	struct vfio_ap_queue *q = NULL;
982 
983 	q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
984 	/* If the queue is assigned to the matrix mdev, unlink it. */
985 	if (q)
986 		vfio_ap_unlink_queue_fr_mdev(q);
987 
988 	return q;
989 }
990 
991 /**
992  * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
993  *				 adapter from the matrix mdev to which the
994  *				 adapter was assigned.
995  * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
996  * @apid: the APID of the unassigned adapter.
997  * @qtable: table for storing queues associated with unassigned adapter.
998  */
999 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1000 					unsigned long apid,
1001 					struct ap_queue_table *qtable)
1002 {
1003 	unsigned long apqi;
1004 	struct vfio_ap_queue *q;
1005 
1006 	for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1007 		q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1008 
1009 		if (q && qtable) {
1010 			if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1011 			    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1012 				hash_add(qtable->queues, &q->mdev_qnode,
1013 					 q->apqn);
1014 		}
1015 	}
1016 }
1017 
1018 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1019 					    unsigned long apid)
1020 {
1021 	int loop_cursor;
1022 	struct vfio_ap_queue *q;
1023 	struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1024 
1025 	hash_init(qtable->queues);
1026 	vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, qtable);
1027 
1028 	if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1029 		clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1030 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1031 	}
1032 
1033 	vfio_ap_mdev_reset_queues(qtable);
1034 
1035 	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1036 		vfio_ap_unlink_mdev_fr_queue(q);
1037 		hash_del(&q->mdev_qnode);
1038 	}
1039 
1040 	kfree(qtable);
1041 }
1042 
1043 /**
1044  * unassign_adapter_store - parses the APID from @buf and clears the
1045  * corresponding bit in the mediated matrix device's APM
1046  *
1047  * @dev:	the matrix device
1048  * @attr:	the mediated matrix device's unassign_adapter attribute
1049  * @buf:	a buffer containing the adapter number (APID) to be unassigned
1050  * @count:	the number of bytes in @buf
1051  *
1052  * Return: the number of bytes processed if the APID is valid; otherwise,
1053  * returns one of the following errors:
1054  *	-EINVAL if the APID is not a number
1055  *	-ENODEV if the APID it exceeds the maximum value configured for the
1056  *		system
1057  */
1058 static ssize_t unassign_adapter_store(struct device *dev,
1059 				      struct device_attribute *attr,
1060 				      const char *buf, size_t count)
1061 {
1062 	int ret;
1063 	unsigned long apid;
1064 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1065 
1066 	get_update_locks_for_mdev(matrix_mdev);
1067 
1068 	ret = kstrtoul(buf, 0, &apid);
1069 	if (ret)
1070 		goto done;
1071 
1072 	if (apid > matrix_mdev->matrix.apm_max) {
1073 		ret = -ENODEV;
1074 		goto done;
1075 	}
1076 
1077 	if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1078 		ret = count;
1079 		goto done;
1080 	}
1081 
1082 	clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1083 	vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1084 	ret = count;
1085 done:
1086 	release_update_locks_for_mdev(matrix_mdev);
1087 	return ret;
1088 }
1089 static DEVICE_ATTR_WO(unassign_adapter);
1090 
1091 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1092 				     unsigned long apqi)
1093 {
1094 	unsigned long apid;
1095 
1096 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1097 		vfio_ap_mdev_link_apqn(matrix_mdev,
1098 				       AP_MKQID(apid, apqi));
1099 }
1100 
1101 /**
1102  * assign_domain_store - parses the APQI from @buf and sets the
1103  * corresponding bit in the mediated matrix device's AQM
1104  *
1105  * @dev:	the matrix device
1106  * @attr:	the mediated matrix device's assign_domain attribute
1107  * @buf:	a buffer containing the AP queue index (APQI) of the domain to
1108  *		be assigned
1109  * @count:	the number of bytes in @buf
1110  *
1111  * Return: the number of bytes processed if the APQI is valid; otherwise returns
1112  * one of the following errors:
1113  *
1114  *	1. -EINVAL
1115  *	   The APQI is not a valid number
1116  *
1117  *	2. -ENODEV
1118  *	   The APQI exceeds the maximum value configured for the system
1119  *
1120  *	3. -EADDRNOTAVAIL
1121  *	   An APQN derived from the cross product of the APQI being assigned
1122  *	   and the APIDs previously assigned is not bound to the vfio_ap device
1123  *	   driver; or, if no APIDs have yet been assigned, the APQI is not
1124  *	   contained in an APQN bound to the vfio_ap device driver.
1125  *
1126  *	4. -EADDRINUSE
1127  *	   An APQN derived from the cross product of the APQI being assigned
1128  *	   and the APIDs previously assigned is being used by another mediated
1129  *	   matrix device
1130  *
1131  *	5. -EAGAIN
1132  *	   The lock required to validate the mdev's AP configuration could not
1133  *	   be obtained.
1134  */
1135 static ssize_t assign_domain_store(struct device *dev,
1136 				   struct device_attribute *attr,
1137 				   const char *buf, size_t count)
1138 {
1139 	int ret;
1140 	unsigned long apqi;
1141 	DECLARE_BITMAP(aqm_delta, AP_DOMAINS);
1142 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1143 
1144 	mutex_lock(&ap_perms_mutex);
1145 	get_update_locks_for_mdev(matrix_mdev);
1146 
1147 	ret = kstrtoul(buf, 0, &apqi);
1148 	if (ret)
1149 		goto done;
1150 
1151 	if (apqi > matrix_mdev->matrix.aqm_max) {
1152 		ret = -ENODEV;
1153 		goto done;
1154 	}
1155 
1156 	if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1157 		ret = count;
1158 		goto done;
1159 	}
1160 
1161 	set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1162 
1163 	ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1164 	if (ret) {
1165 		clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1166 		goto done;
1167 	}
1168 
1169 	vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1170 	memset(aqm_delta, 0, sizeof(aqm_delta));
1171 	set_bit_inv(apqi, aqm_delta);
1172 
1173 	if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, aqm_delta,
1174 				       matrix_mdev))
1175 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1176 
1177 	ret = count;
1178 done:
1179 	release_update_locks_for_mdev(matrix_mdev);
1180 	mutex_unlock(&ap_perms_mutex);
1181 
1182 	return ret;
1183 }
1184 static DEVICE_ATTR_WO(assign_domain);
1185 
1186 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1187 				       unsigned long apqi,
1188 				       struct ap_queue_table *qtable)
1189 {
1190 	unsigned long apid;
1191 	struct vfio_ap_queue *q;
1192 
1193 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1194 		q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1195 
1196 		if (q && qtable) {
1197 			if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1198 			    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1199 				hash_add(qtable->queues, &q->mdev_qnode,
1200 					 q->apqn);
1201 		}
1202 	}
1203 }
1204 
1205 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1206 					   unsigned long apqi)
1207 {
1208 	int loop_cursor;
1209 	struct vfio_ap_queue *q;
1210 	struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1211 
1212 	hash_init(qtable->queues);
1213 	vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, qtable);
1214 
1215 	if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1216 		clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1217 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1218 	}
1219 
1220 	vfio_ap_mdev_reset_queues(qtable);
1221 
1222 	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1223 		vfio_ap_unlink_mdev_fr_queue(q);
1224 		hash_del(&q->mdev_qnode);
1225 	}
1226 
1227 	kfree(qtable);
1228 }
1229 
1230 /**
1231  * unassign_domain_store - parses the APQI from @buf and clears the
1232  * corresponding bit in the mediated matrix device's AQM
1233  *
1234  * @dev:	the matrix device
1235  * @attr:	the mediated matrix device's unassign_domain attribute
1236  * @buf:	a buffer containing the AP queue index (APQI) of the domain to
1237  *		be unassigned
1238  * @count:	the number of bytes in @buf
1239  *
1240  * Return: the number of bytes processed if the APQI is valid; otherwise,
1241  * returns one of the following errors:
1242  *	-EINVAL if the APQI is not a number
1243  *	-ENODEV if the APQI exceeds the maximum value configured for the system
1244  */
1245 static ssize_t unassign_domain_store(struct device *dev,
1246 				     struct device_attribute *attr,
1247 				     const char *buf, size_t count)
1248 {
1249 	int ret;
1250 	unsigned long apqi;
1251 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1252 
1253 	get_update_locks_for_mdev(matrix_mdev);
1254 
1255 	ret = kstrtoul(buf, 0, &apqi);
1256 	if (ret)
1257 		goto done;
1258 
1259 	if (apqi > matrix_mdev->matrix.aqm_max) {
1260 		ret = -ENODEV;
1261 		goto done;
1262 	}
1263 
1264 	if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1265 		ret = count;
1266 		goto done;
1267 	}
1268 
1269 	clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1270 	vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1271 	ret = count;
1272 
1273 done:
1274 	release_update_locks_for_mdev(matrix_mdev);
1275 	return ret;
1276 }
1277 static DEVICE_ATTR_WO(unassign_domain);
1278 
1279 /**
1280  * assign_control_domain_store - parses the domain ID from @buf and sets
1281  * the corresponding bit in the mediated matrix device's ADM
1282  *
1283  * @dev:	the matrix device
1284  * @attr:	the mediated matrix device's assign_control_domain attribute
1285  * @buf:	a buffer containing the domain ID to be assigned
1286  * @count:	the number of bytes in @buf
1287  *
1288  * Return: the number of bytes processed if the domain ID is valid; otherwise,
1289  * returns one of the following errors:
1290  *	-EINVAL if the ID is not a number
1291  *	-ENODEV if the ID exceeds the maximum value configured for the system
1292  */
1293 static ssize_t assign_control_domain_store(struct device *dev,
1294 					   struct device_attribute *attr,
1295 					   const char *buf, size_t count)
1296 {
1297 	int ret;
1298 	unsigned long id;
1299 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1300 
1301 	get_update_locks_for_mdev(matrix_mdev);
1302 
1303 	ret = kstrtoul(buf, 0, &id);
1304 	if (ret)
1305 		goto done;
1306 
1307 	if (id > matrix_mdev->matrix.adm_max) {
1308 		ret = -ENODEV;
1309 		goto done;
1310 	}
1311 
1312 	if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1313 		ret = count;
1314 		goto done;
1315 	}
1316 
1317 	/* Set the bit in the ADM (bitmask) corresponding to the AP control
1318 	 * domain number (id). The bits in the mask, from most significant to
1319 	 * least significant, correspond to IDs 0 up to the one less than the
1320 	 * number of control domains that can be assigned.
1321 	 */
1322 	set_bit_inv(id, matrix_mdev->matrix.adm);
1323 	if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1324 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1325 
1326 	ret = count;
1327 done:
1328 	release_update_locks_for_mdev(matrix_mdev);
1329 	return ret;
1330 }
1331 static DEVICE_ATTR_WO(assign_control_domain);
1332 
1333 /**
1334  * unassign_control_domain_store - parses the domain ID from @buf and
1335  * clears the corresponding bit in the mediated matrix device's ADM
1336  *
1337  * @dev:	the matrix device
1338  * @attr:	the mediated matrix device's unassign_control_domain attribute
1339  * @buf:	a buffer containing the domain ID to be unassigned
1340  * @count:	the number of bytes in @buf
1341  *
1342  * Return: the number of bytes processed if the domain ID is valid; otherwise,
1343  * returns one of the following errors:
1344  *	-EINVAL if the ID is not a number
1345  *	-ENODEV if the ID exceeds the maximum value configured for the system
1346  */
1347 static ssize_t unassign_control_domain_store(struct device *dev,
1348 					     struct device_attribute *attr,
1349 					     const char *buf, size_t count)
1350 {
1351 	int ret;
1352 	unsigned long domid;
1353 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1354 
1355 	get_update_locks_for_mdev(matrix_mdev);
1356 
1357 	ret = kstrtoul(buf, 0, &domid);
1358 	if (ret)
1359 		goto done;
1360 
1361 	if (domid > matrix_mdev->matrix.adm_max) {
1362 		ret = -ENODEV;
1363 		goto done;
1364 	}
1365 
1366 	if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1367 		ret = count;
1368 		goto done;
1369 	}
1370 
1371 	clear_bit_inv(domid, matrix_mdev->matrix.adm);
1372 
1373 	if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1374 		clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1375 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1376 	}
1377 
1378 	ret = count;
1379 done:
1380 	release_update_locks_for_mdev(matrix_mdev);
1381 	return ret;
1382 }
1383 static DEVICE_ATTR_WO(unassign_control_domain);
1384 
1385 static ssize_t control_domains_show(struct device *dev,
1386 				    struct device_attribute *dev_attr,
1387 				    char *buf)
1388 {
1389 	unsigned long id;
1390 	int nchars = 0;
1391 	int n;
1392 	char *bufpos = buf;
1393 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1394 	unsigned long max_domid = matrix_mdev->matrix.adm_max;
1395 
1396 	mutex_lock(&matrix_dev->mdevs_lock);
1397 	for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1398 		n = sprintf(bufpos, "%04lx\n", id);
1399 		bufpos += n;
1400 		nchars += n;
1401 	}
1402 	mutex_unlock(&matrix_dev->mdevs_lock);
1403 
1404 	return nchars;
1405 }
1406 static DEVICE_ATTR_RO(control_domains);
1407 
1408 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1409 {
1410 	char *bufpos = buf;
1411 	unsigned long apid;
1412 	unsigned long apqi;
1413 	unsigned long apid1;
1414 	unsigned long apqi1;
1415 	unsigned long napm_bits = matrix->apm_max + 1;
1416 	unsigned long naqm_bits = matrix->aqm_max + 1;
1417 	int nchars = 0;
1418 	int n;
1419 
1420 	apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1421 	apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1422 
1423 	if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1424 		for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1425 			for_each_set_bit_inv(apqi, matrix->aqm,
1426 					     naqm_bits) {
1427 				n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1428 					    apqi);
1429 				bufpos += n;
1430 				nchars += n;
1431 			}
1432 		}
1433 	} else if (apid1 < napm_bits) {
1434 		for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1435 			n = sprintf(bufpos, "%02lx.\n", apid);
1436 			bufpos += n;
1437 			nchars += n;
1438 		}
1439 	} else if (apqi1 < naqm_bits) {
1440 		for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1441 			n = sprintf(bufpos, ".%04lx\n", apqi);
1442 			bufpos += n;
1443 			nchars += n;
1444 		}
1445 	}
1446 
1447 	return nchars;
1448 }
1449 
1450 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1451 			   char *buf)
1452 {
1453 	ssize_t nchars;
1454 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1455 
1456 	mutex_lock(&matrix_dev->mdevs_lock);
1457 	nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1458 	mutex_unlock(&matrix_dev->mdevs_lock);
1459 
1460 	return nchars;
1461 }
1462 static DEVICE_ATTR_RO(matrix);
1463 
1464 static ssize_t guest_matrix_show(struct device *dev,
1465 				 struct device_attribute *attr, char *buf)
1466 {
1467 	ssize_t nchars;
1468 	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1469 
1470 	mutex_lock(&matrix_dev->mdevs_lock);
1471 	nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1472 	mutex_unlock(&matrix_dev->mdevs_lock);
1473 
1474 	return nchars;
1475 }
1476 static DEVICE_ATTR_RO(guest_matrix);
1477 
1478 static struct attribute *vfio_ap_mdev_attrs[] = {
1479 	&dev_attr_assign_adapter.attr,
1480 	&dev_attr_unassign_adapter.attr,
1481 	&dev_attr_assign_domain.attr,
1482 	&dev_attr_unassign_domain.attr,
1483 	&dev_attr_assign_control_domain.attr,
1484 	&dev_attr_unassign_control_domain.attr,
1485 	&dev_attr_control_domains.attr,
1486 	&dev_attr_matrix.attr,
1487 	&dev_attr_guest_matrix.attr,
1488 	NULL,
1489 };
1490 
1491 static struct attribute_group vfio_ap_mdev_attr_group = {
1492 	.attrs = vfio_ap_mdev_attrs
1493 };
1494 
1495 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1496 	&vfio_ap_mdev_attr_group,
1497 	NULL
1498 };
1499 
1500 /**
1501  * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1502  * to manage AP resources for the guest whose state is represented by @kvm
1503  *
1504  * @matrix_mdev: a mediated matrix device
1505  * @kvm: reference to KVM instance
1506  *
1507  * Return: 0 if no other mediated matrix device has a reference to @kvm;
1508  * otherwise, returns an -EPERM.
1509  */
1510 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1511 				struct kvm *kvm)
1512 {
1513 	struct ap_matrix_mdev *m;
1514 
1515 	if (kvm->arch.crypto.crycbd) {
1516 		down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1517 		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1518 		up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1519 
1520 		get_update_locks_for_kvm(kvm);
1521 
1522 		list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1523 			if (m != matrix_mdev && m->kvm == kvm) {
1524 				release_update_locks_for_kvm(kvm);
1525 				return -EPERM;
1526 			}
1527 		}
1528 
1529 		kvm_get_kvm(kvm);
1530 		matrix_mdev->kvm = kvm;
1531 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1532 
1533 		release_update_locks_for_kvm(kvm);
1534 	}
1535 
1536 	return 0;
1537 }
1538 
1539 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1540 {
1541 	struct ap_queue_table *qtable = &matrix_mdev->qtable;
1542 	struct vfio_ap_queue *q;
1543 	int loop_cursor;
1544 
1545 	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1546 		if (q->saved_iova >= iova && q->saved_iova < iova + length)
1547 			vfio_ap_irq_disable(q);
1548 	}
1549 }
1550 
1551 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1552 				   u64 length)
1553 {
1554 	struct ap_matrix_mdev *matrix_mdev =
1555 		container_of(vdev, struct ap_matrix_mdev, vdev);
1556 
1557 	mutex_lock(&matrix_dev->mdevs_lock);
1558 
1559 	unmap_iova(matrix_mdev, iova, length);
1560 
1561 	mutex_unlock(&matrix_dev->mdevs_lock);
1562 }
1563 
1564 /**
1565  * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1566  * by @matrix_mdev.
1567  *
1568  * @matrix_mdev: a matrix mediated device
1569  */
1570 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1571 {
1572 	struct kvm *kvm = matrix_mdev->kvm;
1573 
1574 	if (kvm && kvm->arch.crypto.crycbd) {
1575 		down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1576 		kvm->arch.crypto.pqap_hook = NULL;
1577 		up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1578 
1579 		get_update_locks_for_kvm(kvm);
1580 
1581 		kvm_arch_crypto_clear_masks(kvm);
1582 		vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1583 		kvm_put_kvm(kvm);
1584 		matrix_mdev->kvm = NULL;
1585 
1586 		release_update_locks_for_kvm(kvm);
1587 	}
1588 }
1589 
1590 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1591 {
1592 	struct ap_queue *queue;
1593 	struct vfio_ap_queue *q = NULL;
1594 
1595 	queue = ap_get_qdev(apqn);
1596 	if (!queue)
1597 		return NULL;
1598 
1599 	if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1600 		q = dev_get_drvdata(&queue->ap_dev.device);
1601 
1602 	put_device(&queue->ap_dev.device);
1603 
1604 	return q;
1605 }
1606 
1607 static int apq_status_check(int apqn, struct ap_queue_status *status)
1608 {
1609 	switch (status->response_code) {
1610 	case AP_RESPONSE_NORMAL:
1611 	case AP_RESPONSE_DECONFIGURED:
1612 		return 0;
1613 	case AP_RESPONSE_RESET_IN_PROGRESS:
1614 		return -EBUSY;
1615 	default:
1616 		WARN(true,
1617 		     "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1618 		     AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1619 		     status->response_code);
1620 		return -EIO;
1621 	}
1622 }
1623 
1624 static int apq_reset_check(struct vfio_ap_queue *q)
1625 {
1626 	int ret;
1627 	int iters = MAX_RESET_CHECK_WAIT / AP_RESET_INTERVAL;
1628 	struct ap_queue_status status;
1629 
1630 	for (; iters > 0; iters--) {
1631 		msleep(AP_RESET_INTERVAL);
1632 		status = ap_tapq(q->apqn, NULL);
1633 		ret = apq_status_check(q->apqn, &status);
1634 		if (ret != -EBUSY)
1635 			return ret;
1636 	}
1637 	WARN_ONCE(iters <= 0,
1638 		  "timeout verifying reset of queue %02x.%04x (%u, %u, %u)",
1639 		  AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1640 		  status.queue_empty, status.irq_enabled, status.response_code);
1641 	return ret;
1642 }
1643 
1644 static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1645 {
1646 	struct ap_queue_status status;
1647 	int ret;
1648 
1649 	if (!q)
1650 		return 0;
1651 retry_zapq:
1652 	status = ap_zapq(q->apqn, 0);
1653 	q->reset_rc = status.response_code;
1654 	switch (status.response_code) {
1655 	case AP_RESPONSE_NORMAL:
1656 		ret = 0;
1657 		if (!status.irq_enabled)
1658 			vfio_ap_free_aqic_resources(q);
1659 		if (!status.queue_empty || status.irq_enabled) {
1660 			ret = apq_reset_check(q);
1661 			if (status.irq_enabled && ret == 0)
1662 				vfio_ap_free_aqic_resources(q);
1663 		}
1664 		break;
1665 	case AP_RESPONSE_RESET_IN_PROGRESS:
1666 		/*
1667 		 * There is a reset issued by another process in progress. Let's wait
1668 		 * for that to complete. Since we have no idea whether it was a RAPQ or
1669 		 * ZAPQ, then if it completes successfully, let's issue the ZAPQ.
1670 		 */
1671 		ret = apq_reset_check(q);
1672 		if (ret)
1673 			break;
1674 		goto retry_zapq;
1675 	case AP_RESPONSE_DECONFIGURED:
1676 		/*
1677 		 * When an AP adapter is deconfigured, the associated
1678 		 * queues are reset, so let's return a value indicating the reset
1679 		 * completed successfully.
1680 		 */
1681 		ret = 0;
1682 		vfio_ap_free_aqic_resources(q);
1683 		break;
1684 	default:
1685 		WARN(true,
1686 		     "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1687 		     AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1688 		     status.response_code);
1689 		return -EIO;
1690 	}
1691 
1692 	return ret;
1693 }
1694 
1695 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable)
1696 {
1697 	int ret, loop_cursor, rc = 0;
1698 	struct vfio_ap_queue *q;
1699 
1700 	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1701 		ret = vfio_ap_mdev_reset_queue(q);
1702 		/*
1703 		 * Regardless whether a queue turns out to be busy, or
1704 		 * is not operational, we need to continue resetting
1705 		 * the remaining queues.
1706 		 */
1707 		if (ret)
1708 			rc = ret;
1709 	}
1710 
1711 	return rc;
1712 }
1713 
1714 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1715 {
1716 	struct ap_matrix_mdev *matrix_mdev =
1717 		container_of(vdev, struct ap_matrix_mdev, vdev);
1718 
1719 	if (!vdev->kvm)
1720 		return -EINVAL;
1721 
1722 	return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
1723 }
1724 
1725 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1726 {
1727 	struct ap_matrix_mdev *matrix_mdev =
1728 		container_of(vdev, struct ap_matrix_mdev, vdev);
1729 
1730 	vfio_ap_mdev_unset_kvm(matrix_mdev);
1731 }
1732 
1733 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
1734 {
1735 	struct device *dev = vdev->dev;
1736 	struct ap_matrix_mdev *matrix_mdev;
1737 
1738 	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
1739 
1740 	if (matrix_mdev->req_trigger) {
1741 		if (!(count % 10))
1742 			dev_notice_ratelimited(dev,
1743 					       "Relaying device request to user (#%u)\n",
1744 					       count);
1745 
1746 		eventfd_signal(matrix_mdev->req_trigger, 1);
1747 	} else if (count == 0) {
1748 		dev_notice(dev,
1749 			   "No device request registered, blocked until released by user\n");
1750 	}
1751 }
1752 
1753 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1754 {
1755 	unsigned long minsz;
1756 	struct vfio_device_info info;
1757 
1758 	minsz = offsetofend(struct vfio_device_info, num_irqs);
1759 
1760 	if (copy_from_user(&info, (void __user *)arg, minsz))
1761 		return -EFAULT;
1762 
1763 	if (info.argsz < minsz)
1764 		return -EINVAL;
1765 
1766 	info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1767 	info.num_regions = 0;
1768 	info.num_irqs = VFIO_AP_NUM_IRQS;
1769 
1770 	return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1771 }
1772 
1773 static ssize_t vfio_ap_get_irq_info(unsigned long arg)
1774 {
1775 	unsigned long minsz;
1776 	struct vfio_irq_info info;
1777 
1778 	minsz = offsetofend(struct vfio_irq_info, count);
1779 
1780 	if (copy_from_user(&info, (void __user *)arg, minsz))
1781 		return -EFAULT;
1782 
1783 	if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
1784 		return -EINVAL;
1785 
1786 	switch (info.index) {
1787 	case VFIO_AP_REQ_IRQ_INDEX:
1788 		info.count = 1;
1789 		info.flags = VFIO_IRQ_INFO_EVENTFD;
1790 		break;
1791 	default:
1792 		return -EINVAL;
1793 	}
1794 
1795 	return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1796 }
1797 
1798 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
1799 {
1800 	int ret;
1801 	size_t data_size;
1802 	unsigned long minsz;
1803 
1804 	minsz = offsetofend(struct vfio_irq_set, count);
1805 
1806 	if (copy_from_user(irq_set, (void __user *)arg, minsz))
1807 		return -EFAULT;
1808 
1809 	ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
1810 						 &data_size);
1811 	if (ret)
1812 		return ret;
1813 
1814 	if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
1815 		return -EINVAL;
1816 
1817 	return 0;
1818 }
1819 
1820 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
1821 				   unsigned long arg)
1822 {
1823 	s32 fd;
1824 	void __user *data;
1825 	unsigned long minsz;
1826 	struct eventfd_ctx *req_trigger;
1827 
1828 	minsz = offsetofend(struct vfio_irq_set, count);
1829 	data = (void __user *)(arg + minsz);
1830 
1831 	if (get_user(fd, (s32 __user *)data))
1832 		return -EFAULT;
1833 
1834 	if (fd == -1) {
1835 		if (matrix_mdev->req_trigger)
1836 			eventfd_ctx_put(matrix_mdev->req_trigger);
1837 		matrix_mdev->req_trigger = NULL;
1838 	} else if (fd >= 0) {
1839 		req_trigger = eventfd_ctx_fdget(fd);
1840 		if (IS_ERR(req_trigger))
1841 			return PTR_ERR(req_trigger);
1842 
1843 		if (matrix_mdev->req_trigger)
1844 			eventfd_ctx_put(matrix_mdev->req_trigger);
1845 
1846 		matrix_mdev->req_trigger = req_trigger;
1847 	} else {
1848 		return -EINVAL;
1849 	}
1850 
1851 	return 0;
1852 }
1853 
1854 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
1855 			    unsigned long arg)
1856 {
1857 	int ret;
1858 	struct vfio_irq_set irq_set;
1859 
1860 	ret = vfio_ap_irq_set_init(&irq_set, arg);
1861 	if (ret)
1862 		return ret;
1863 
1864 	switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
1865 	case VFIO_IRQ_SET_DATA_EVENTFD:
1866 		switch (irq_set.index) {
1867 		case VFIO_AP_REQ_IRQ_INDEX:
1868 			return vfio_ap_set_request_irq(matrix_mdev, arg);
1869 		default:
1870 			return -EINVAL;
1871 		}
1872 	default:
1873 		return -EINVAL;
1874 	}
1875 }
1876 
1877 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1878 				    unsigned int cmd, unsigned long arg)
1879 {
1880 	struct ap_matrix_mdev *matrix_mdev =
1881 		container_of(vdev, struct ap_matrix_mdev, vdev);
1882 	int ret;
1883 
1884 	mutex_lock(&matrix_dev->mdevs_lock);
1885 	switch (cmd) {
1886 	case VFIO_DEVICE_GET_INFO:
1887 		ret = vfio_ap_mdev_get_device_info(arg);
1888 		break;
1889 	case VFIO_DEVICE_RESET:
1890 		ret = vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1891 		break;
1892 	case VFIO_DEVICE_GET_IRQ_INFO:
1893 			ret = vfio_ap_get_irq_info(arg);
1894 			break;
1895 	case VFIO_DEVICE_SET_IRQS:
1896 		ret = vfio_ap_set_irqs(matrix_mdev, arg);
1897 		break;
1898 	default:
1899 		ret = -EOPNOTSUPP;
1900 		break;
1901 	}
1902 	mutex_unlock(&matrix_dev->mdevs_lock);
1903 
1904 	return ret;
1905 }
1906 
1907 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
1908 {
1909 	struct ap_matrix_mdev *matrix_mdev;
1910 	unsigned long apid = AP_QID_CARD(q->apqn);
1911 	unsigned long apqi = AP_QID_QUEUE(q->apqn);
1912 
1913 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
1914 		if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
1915 		    test_bit_inv(apqi, matrix_mdev->matrix.aqm))
1916 			return matrix_mdev;
1917 	}
1918 
1919 	return NULL;
1920 }
1921 
1922 static ssize_t status_show(struct device *dev,
1923 			   struct device_attribute *attr,
1924 			   char *buf)
1925 {
1926 	ssize_t nchars = 0;
1927 	struct vfio_ap_queue *q;
1928 	struct ap_matrix_mdev *matrix_mdev;
1929 	struct ap_device *apdev = to_ap_dev(dev);
1930 
1931 	mutex_lock(&matrix_dev->mdevs_lock);
1932 	q = dev_get_drvdata(&apdev->device);
1933 	matrix_mdev = vfio_ap_mdev_for_queue(q);
1934 
1935 	if (matrix_mdev) {
1936 		if (matrix_mdev->kvm)
1937 			nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1938 					   AP_QUEUE_IN_USE);
1939 		else
1940 			nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1941 					   AP_QUEUE_ASSIGNED);
1942 	} else {
1943 		nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1944 				   AP_QUEUE_UNASSIGNED);
1945 	}
1946 
1947 	mutex_unlock(&matrix_dev->mdevs_lock);
1948 
1949 	return nchars;
1950 }
1951 
1952 static DEVICE_ATTR_RO(status);
1953 
1954 static struct attribute *vfio_queue_attrs[] = {
1955 	&dev_attr_status.attr,
1956 	NULL,
1957 };
1958 
1959 static const struct attribute_group vfio_queue_attr_group = {
1960 	.attrs = vfio_queue_attrs,
1961 };
1962 
1963 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1964 	.init = vfio_ap_mdev_init_dev,
1965 	.open_device = vfio_ap_mdev_open_device,
1966 	.close_device = vfio_ap_mdev_close_device,
1967 	.ioctl = vfio_ap_mdev_ioctl,
1968 	.dma_unmap = vfio_ap_mdev_dma_unmap,
1969 	.bind_iommufd = vfio_iommufd_emulated_bind,
1970 	.unbind_iommufd = vfio_iommufd_emulated_unbind,
1971 	.attach_ioas = vfio_iommufd_emulated_attach_ioas,
1972 	.request = vfio_ap_mdev_request
1973 };
1974 
1975 static struct mdev_driver vfio_ap_matrix_driver = {
1976 	.device_api = VFIO_DEVICE_API_AP_STRING,
1977 	.max_instances = MAX_ZDEV_ENTRIES_EXT,
1978 	.driver = {
1979 		.name = "vfio_ap_mdev",
1980 		.owner = THIS_MODULE,
1981 		.mod_name = KBUILD_MODNAME,
1982 		.dev_groups = vfio_ap_mdev_attr_groups,
1983 	},
1984 	.probe = vfio_ap_mdev_probe,
1985 	.remove = vfio_ap_mdev_remove,
1986 };
1987 
1988 int vfio_ap_mdev_register(void)
1989 {
1990 	int ret;
1991 
1992 	ret = mdev_register_driver(&vfio_ap_matrix_driver);
1993 	if (ret)
1994 		return ret;
1995 
1996 	matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
1997 	matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
1998 	matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
1999 	ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2000 				   &vfio_ap_matrix_driver,
2001 				   matrix_dev->mdev_types, 1);
2002 	if (ret)
2003 		goto err_driver;
2004 	return 0;
2005 
2006 err_driver:
2007 	mdev_unregister_driver(&vfio_ap_matrix_driver);
2008 	return ret;
2009 }
2010 
2011 void vfio_ap_mdev_unregister(void)
2012 {
2013 	mdev_unregister_parent(&matrix_dev->parent);
2014 	mdev_unregister_driver(&vfio_ap_matrix_driver);
2015 }
2016 
2017 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2018 {
2019 	int ret;
2020 	struct vfio_ap_queue *q;
2021 	struct ap_matrix_mdev *matrix_mdev;
2022 
2023 	ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2024 	if (ret)
2025 		return ret;
2026 
2027 	q = kzalloc(sizeof(*q), GFP_KERNEL);
2028 	if (!q) {
2029 		ret = -ENOMEM;
2030 		goto err_remove_group;
2031 	}
2032 
2033 	q->apqn = to_ap_queue(&apdev->device)->qid;
2034 	q->saved_isc = VFIO_AP_ISC_INVALID;
2035 	matrix_mdev = get_update_locks_by_apqn(q->apqn);
2036 
2037 	if (matrix_mdev) {
2038 		vfio_ap_mdev_link_queue(matrix_mdev, q);
2039 
2040 		if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm,
2041 					       matrix_mdev->matrix.aqm,
2042 					       matrix_mdev))
2043 			vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2044 	}
2045 	dev_set_drvdata(&apdev->device, q);
2046 	release_update_locks_for_mdev(matrix_mdev);
2047 
2048 	return 0;
2049 
2050 err_remove_group:
2051 	sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2052 	return ret;
2053 }
2054 
2055 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2056 {
2057 	unsigned long apid, apqi;
2058 	struct vfio_ap_queue *q;
2059 	struct ap_matrix_mdev *matrix_mdev;
2060 
2061 	sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2062 	q = dev_get_drvdata(&apdev->device);
2063 	get_update_locks_for_queue(q);
2064 	matrix_mdev = q->matrix_mdev;
2065 
2066 	if (matrix_mdev) {
2067 		vfio_ap_unlink_queue_fr_mdev(q);
2068 
2069 		apid = AP_QID_CARD(q->apqn);
2070 		apqi = AP_QID_QUEUE(q->apqn);
2071 
2072 		/*
2073 		 * If the queue is assigned to the guest's APCB, then remove
2074 		 * the adapter's APID from the APCB and hot it into the guest.
2075 		 */
2076 		if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2077 		    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2078 			clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2079 			vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2080 		}
2081 	}
2082 
2083 	vfio_ap_mdev_reset_queue(q);
2084 	dev_set_drvdata(&apdev->device, NULL);
2085 	kfree(q);
2086 	release_update_locks_for_mdev(matrix_mdev);
2087 }
2088 
2089 /**
2090  * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2091  *				 assigned to a mediated device under the control
2092  *				 of the vfio_ap device driver.
2093  *
2094  * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2095  * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2096  *
2097  * Return:
2098  *	* -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2099  *	  assigned to a mediated device under the control of the vfio_ap
2100  *	  device driver.
2101  *	* Otherwise, return 0.
2102  */
2103 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2104 {
2105 	int ret;
2106 
2107 	mutex_lock(&matrix_dev->guests_lock);
2108 	mutex_lock(&matrix_dev->mdevs_lock);
2109 	ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2110 	mutex_unlock(&matrix_dev->mdevs_lock);
2111 	mutex_unlock(&matrix_dev->guests_lock);
2112 
2113 	return ret;
2114 }
2115 
2116 /**
2117  * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2118  *				 domains that have been removed from the host's
2119  *				 AP configuration from a guest.
2120  *
2121  * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2122  * @aprem: the adapters that have been removed from the host's AP configuration
2123  * @aqrem: the domains that have been removed from the host's AP configuration
2124  * @cdrem: the control domains that have been removed from the host's AP
2125  *	   configuration.
2126  */
2127 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2128 					unsigned long *aprem,
2129 					unsigned long *aqrem,
2130 					unsigned long *cdrem)
2131 {
2132 	int do_hotplug = 0;
2133 
2134 	if (!bitmap_empty(aprem, AP_DEVICES)) {
2135 		do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2136 					    matrix_mdev->shadow_apcb.apm,
2137 					    aprem, AP_DEVICES);
2138 	}
2139 
2140 	if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2141 		do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2142 					    matrix_mdev->shadow_apcb.aqm,
2143 					    aqrem, AP_DEVICES);
2144 	}
2145 
2146 	if (!bitmap_empty(cdrem, AP_DOMAINS))
2147 		do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2148 					    matrix_mdev->shadow_apcb.adm,
2149 					    cdrem, AP_DOMAINS);
2150 
2151 	if (do_hotplug)
2152 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2153 }
2154 
2155 /**
2156  * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2157  *			     domains and control domains that have been removed
2158  *			     from the host AP configuration and unplugs them
2159  *			     from those guests.
2160  *
2161  * @ap_remove:	bitmap specifying which adapters have been removed from the host
2162  *		config.
2163  * @aq_remove:	bitmap specifying which domains have been removed from the host
2164  *		config.
2165  * @cd_remove:	bitmap specifying which control domains have been removed from
2166  *		the host config.
2167  */
2168 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2169 				    unsigned long *aq_remove,
2170 				    unsigned long *cd_remove)
2171 {
2172 	struct ap_matrix_mdev *matrix_mdev;
2173 	DECLARE_BITMAP(aprem, AP_DEVICES);
2174 	DECLARE_BITMAP(aqrem, AP_DOMAINS);
2175 	DECLARE_BITMAP(cdrem, AP_DOMAINS);
2176 	int do_remove = 0;
2177 
2178 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2179 		mutex_lock(&matrix_mdev->kvm->lock);
2180 		mutex_lock(&matrix_dev->mdevs_lock);
2181 
2182 		do_remove |= bitmap_and(aprem, ap_remove,
2183 					  matrix_mdev->matrix.apm,
2184 					  AP_DEVICES);
2185 		do_remove |= bitmap_and(aqrem, aq_remove,
2186 					  matrix_mdev->matrix.aqm,
2187 					  AP_DOMAINS);
2188 		do_remove |= bitmap_andnot(cdrem, cd_remove,
2189 					     matrix_mdev->matrix.adm,
2190 					     AP_DOMAINS);
2191 
2192 		if (do_remove)
2193 			vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2194 						    cdrem);
2195 
2196 		mutex_unlock(&matrix_dev->mdevs_lock);
2197 		mutex_unlock(&matrix_mdev->kvm->lock);
2198 	}
2199 }
2200 
2201 /**
2202  * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2203  *				control domains from the host AP configuration
2204  *				by unplugging them from the guests that are
2205  *				using them.
2206  * @cur_config_info: the current host AP configuration information
2207  * @prev_config_info: the previous host AP configuration information
2208  */
2209 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2210 				       struct ap_config_info *prev_config_info)
2211 {
2212 	int do_remove;
2213 	DECLARE_BITMAP(aprem, AP_DEVICES);
2214 	DECLARE_BITMAP(aqrem, AP_DOMAINS);
2215 	DECLARE_BITMAP(cdrem, AP_DOMAINS);
2216 
2217 	do_remove = bitmap_andnot(aprem,
2218 				  (unsigned long *)prev_config_info->apm,
2219 				  (unsigned long *)cur_config_info->apm,
2220 				  AP_DEVICES);
2221 	do_remove |= bitmap_andnot(aqrem,
2222 				   (unsigned long *)prev_config_info->aqm,
2223 				   (unsigned long *)cur_config_info->aqm,
2224 				   AP_DEVICES);
2225 	do_remove |= bitmap_andnot(cdrem,
2226 				   (unsigned long *)prev_config_info->adm,
2227 				   (unsigned long *)cur_config_info->adm,
2228 				   AP_DEVICES);
2229 
2230 	if (do_remove)
2231 		vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2232 }
2233 
2234 /**
2235  * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2236  *				 are older than AP type 10 (CEX4).
2237  * @apm: a bitmap of the APIDs to examine
2238  * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2239  */
2240 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2241 {
2242 	bool apid_cleared;
2243 	struct ap_queue_status status;
2244 	unsigned long apid, apqi;
2245 	struct ap_tapq_gr2 info;
2246 
2247 	for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2248 		apid_cleared = false;
2249 
2250 		for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2251 			status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2252 			switch (status.response_code) {
2253 			/*
2254 			 * According to the architecture in each case
2255 			 * below, the queue's info should be filled.
2256 			 */
2257 			case AP_RESPONSE_NORMAL:
2258 			case AP_RESPONSE_RESET_IN_PROGRESS:
2259 			case AP_RESPONSE_DECONFIGURED:
2260 			case AP_RESPONSE_CHECKSTOPPED:
2261 			case AP_RESPONSE_BUSY:
2262 				/*
2263 				 * The vfio_ap device driver only
2264 				 * supports CEX4 and newer adapters, so
2265 				 * remove the APID if the adapter is
2266 				 * older than a CEX4.
2267 				 */
2268 				if (info.at < AP_DEVICE_TYPE_CEX4) {
2269 					clear_bit_inv(apid, apm);
2270 					apid_cleared = true;
2271 				}
2272 
2273 				break;
2274 
2275 			default:
2276 				/*
2277 				 * If we don't know the adapter type,
2278 				 * clear its APID since it can't be
2279 				 * determined whether the vfio_ap
2280 				 * device driver supports it.
2281 				 */
2282 				clear_bit_inv(apid, apm);
2283 				apid_cleared = true;
2284 				break;
2285 			}
2286 
2287 			/*
2288 			 * If we've already cleared the APID from the apm, there
2289 			 * is no need to continue examining the remainin AP
2290 			 * queues to determine the type of the adapter.
2291 			 */
2292 			if (apid_cleared)
2293 				continue;
2294 		}
2295 	}
2296 }
2297 
2298 /**
2299  * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2300  *			  control domains that have been added to the host's
2301  *			  AP configuration for each matrix mdev to which they
2302  *			  are assigned.
2303  *
2304  * @apm_add: a bitmap specifying the adapters that have been added to the AP
2305  *	     configuration.
2306  * @aqm_add: a bitmap specifying the domains that have been added to the AP
2307  *	     configuration.
2308  * @adm_add: a bitmap specifying the control domains that have been added to the
2309  *	     AP configuration.
2310  */
2311 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2312 				 unsigned long *adm_add)
2313 {
2314 	struct ap_matrix_mdev *matrix_mdev;
2315 
2316 	if (list_empty(&matrix_dev->mdev_list))
2317 		return;
2318 
2319 	vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2320 
2321 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2322 		bitmap_and(matrix_mdev->apm_add,
2323 			   matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2324 		bitmap_and(matrix_mdev->aqm_add,
2325 			   matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2326 		bitmap_and(matrix_mdev->adm_add,
2327 			   matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2328 	}
2329 }
2330 
2331 /**
2332  * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2333  *			     control domains to the host AP configuration
2334  *			     by updating the bitmaps that specify what adapters,
2335  *			     domains and control domains have been added so they
2336  *			     can be hot plugged into the guest when the AP bus
2337  *			     scan completes (see vfio_ap_on_scan_complete
2338  *			     function).
2339  * @cur_config_info: the current AP configuration information
2340  * @prev_config_info: the previous AP configuration information
2341  */
2342 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2343 				    struct ap_config_info *prev_config_info)
2344 {
2345 	bool do_add;
2346 	DECLARE_BITMAP(apm_add, AP_DEVICES);
2347 	DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2348 	DECLARE_BITMAP(adm_add, AP_DOMAINS);
2349 
2350 	do_add = bitmap_andnot(apm_add,
2351 			       (unsigned long *)cur_config_info->apm,
2352 			       (unsigned long *)prev_config_info->apm,
2353 			       AP_DEVICES);
2354 	do_add |= bitmap_andnot(aqm_add,
2355 				(unsigned long *)cur_config_info->aqm,
2356 				(unsigned long *)prev_config_info->aqm,
2357 				AP_DOMAINS);
2358 	do_add |= bitmap_andnot(adm_add,
2359 				(unsigned long *)cur_config_info->adm,
2360 				(unsigned long *)prev_config_info->adm,
2361 				AP_DOMAINS);
2362 
2363 	if (do_add)
2364 		vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2365 }
2366 
2367 /**
2368  * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2369  *			    configuration.
2370  *
2371  * @cur_cfg_info: the current host AP configuration
2372  * @prev_cfg_info: the previous host AP configuration
2373  */
2374 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2375 			    struct ap_config_info *prev_cfg_info)
2376 {
2377 	if (!cur_cfg_info || !prev_cfg_info)
2378 		return;
2379 
2380 	mutex_lock(&matrix_dev->guests_lock);
2381 
2382 	vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2383 	vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2384 	memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2385 
2386 	mutex_unlock(&matrix_dev->guests_lock);
2387 }
2388 
2389 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2390 {
2391 	bool do_hotplug = false;
2392 	int filter_domains = 0;
2393 	int filter_adapters = 0;
2394 	DECLARE_BITMAP(apm, AP_DEVICES);
2395 	DECLARE_BITMAP(aqm, AP_DOMAINS);
2396 
2397 	mutex_lock(&matrix_mdev->kvm->lock);
2398 	mutex_lock(&matrix_dev->mdevs_lock);
2399 
2400 	filter_adapters = bitmap_and(apm, matrix_mdev->matrix.apm,
2401 				     matrix_mdev->apm_add, AP_DEVICES);
2402 	filter_domains = bitmap_and(aqm, matrix_mdev->matrix.aqm,
2403 				    matrix_mdev->aqm_add, AP_DOMAINS);
2404 
2405 	if (filter_adapters && filter_domains)
2406 		do_hotplug |= vfio_ap_mdev_filter_matrix(apm, aqm, matrix_mdev);
2407 	else if (filter_adapters)
2408 		do_hotplug |=
2409 			vfio_ap_mdev_filter_matrix(apm,
2410 						   matrix_mdev->shadow_apcb.aqm,
2411 						   matrix_mdev);
2412 	else
2413 		do_hotplug |=
2414 			vfio_ap_mdev_filter_matrix(matrix_mdev->shadow_apcb.apm,
2415 						   aqm, matrix_mdev);
2416 
2417 	if (bitmap_intersects(matrix_mdev->matrix.adm, matrix_mdev->adm_add,
2418 			      AP_DOMAINS))
2419 		do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2420 
2421 	if (do_hotplug)
2422 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2423 
2424 	mutex_unlock(&matrix_dev->mdevs_lock);
2425 	mutex_unlock(&matrix_mdev->kvm->lock);
2426 }
2427 
2428 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2429 			      struct ap_config_info *old_config_info)
2430 {
2431 	struct ap_matrix_mdev *matrix_mdev;
2432 
2433 	mutex_lock(&matrix_dev->guests_lock);
2434 
2435 	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2436 		if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2437 		    bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2438 		    bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2439 			continue;
2440 
2441 		vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2442 		bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2443 		bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2444 		bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2445 	}
2446 
2447 	mutex_unlock(&matrix_dev->guests_lock);
2448 }
2449