1 // SPDX-License-Identifier: GPL-2.0+
3 * Adjunct processor matrix VFIO device driver callbacks.
5 * Copyright IBM Corp. 2018
7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8 * Halil Pasic <pasic@linux.ibm.com>
9 * Pierre Morel <pmorel@linux.ibm.com>
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>
21 #include <asm/zcrypt.h>
23 #include "vfio_ap_private.h"
24 #include "vfio_ap_debug.h"
26 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
27 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
29 #define AP_QUEUE_ASSIGNED "assigned"
30 #define AP_QUEUE_UNASSIGNED "unassigned"
31 #define AP_QUEUE_IN_USE "in use"
33 #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */
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);
42 * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
43 * KVM guest's APCB in the proper order.
45 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
47 * The proper locking order is:
48 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
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
53 * Note: If @kvm is NULL, the KVM lock will not be taken.
55 static inline void get_update_locks_for_kvm(struct kvm *kvm)
57 mutex_lock(&matrix_dev->guests_lock);
59 mutex_lock(&kvm->lock);
60 mutex_lock(&matrix_dev->mdevs_lock);
64 * release_update_locks_for_kvm: Release the locks used to dynamically update a
65 * KVM guest's APCB in the proper order.
67 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
69 * The proper unlocking order is:
70 * 1. matrix_dev->mdevs_lock
72 * 3. matrix_dev->guests_lock
74 * Note: If @kvm is NULL, the KVM lock will not be released.
76 static inline void release_update_locks_for_kvm(struct kvm *kvm)
78 mutex_unlock(&matrix_dev->mdevs_lock);
80 mutex_unlock(&kvm->lock);
81 mutex_unlock(&matrix_dev->guests_lock);
85 * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
86 * KVM guest's APCB in the proper order.
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.
91 * The proper locking order is:
92 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
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
97 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
98 * lock will not be taken.
100 static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
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);
109 * release_update_locks_for_mdev: Release the locks used to dynamically update a
110 * KVM guest's APCB in the proper order.
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.
115 * The proper unlocking order is:
116 * 1. matrix_dev->mdevs_lock
117 * 2. matrix_mdev->kvm->lock
118 * 3. matrix_dev->guests_lock
120 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
121 * lock will not be released.
123 static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
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);
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.
136 * @apqn: the APQN of a queue device.
138 * The proper locking order is:
139 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
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
144 * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
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.
150 static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
152 struct ap_matrix_mdev *matrix_mdev;
154 mutex_lock(&matrix_dev->guests_lock);
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);
162 mutex_lock(&matrix_dev->mdevs_lock);
168 mutex_lock(&matrix_dev->mdevs_lock);
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.
178 * @q: a pointer to a vfio_ap_queue object.
180 * The proper locking order is:
181 * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
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
186 * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
189 static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
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);
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
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
206 static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
207 struct ap_matrix_mdev *matrix_mdev,
210 struct vfio_ap_queue *q;
212 hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
214 if (q && q->apqn == apqn)
222 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
223 * @apqn: The AP Queue number
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
229 * Otherwise retries up to 5 times after waiting 20ms.
231 static void vfio_ap_wait_for_irqclear(int apqn)
233 struct ap_queue_status status;
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)
244 case AP_RESPONSE_BUSY:
247 case AP_RESPONSE_Q_NOT_AVAIL:
248 case AP_RESPONSE_DECONFIGURED:
249 case AP_RESPONSE_CHECKSTOPPED:
251 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
252 status.response_code, apqn);
257 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
258 __func__, status.response_code, apqn);
262 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
263 * @q: The vfio_ap_queue
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.
269 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
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;
278 if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
279 vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
285 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
286 * @q: The vfio_ap_queue
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.
294 * In the case the AP is busy, or a reset is in progress,
295 * retries after 20ms, up to 5 times.
297 * Returns if ap_aqic function failed with invalid, deconfigured or
300 * Return: &struct ap_queue_status
302 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
304 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
305 struct ap_queue_status status;
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);
315 case AP_RESPONSE_RESET_IN_PROGRESS:
316 case AP_RESPONSE_BUSY:
319 case AP_RESPONSE_Q_NOT_AVAIL:
320 case AP_RESPONSE_DECONFIGURED:
321 case AP_RESPONSE_CHECKSTOPPED:
322 case AP_RESPONSE_INVALID_ADDRESS:
324 /* All cases in default means AP not operational */
325 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
326 status.response_code);
331 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
332 status.response_code);
334 vfio_ap_free_aqic_resources(q);
339 * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
341 * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
342 * @nib: the location for storing the nib address.
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.
348 * Return: returns zero if the nib address is a valid; otherwise, returns
351 static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
353 *nib = vcpu->run->s.regs.gprs[2];
357 if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
364 * ensure_nib_shared() - Ensure the address of the NIB is secure and shared
365 * @addr: the physical (absolute) address of the NIB
367 * This function checks whether the NIB page, which has been pinned with
368 * vfio_pin_pages(), is a shared page belonging to a secure guest.
370 * It will call uv_pin_shared() on it; if the page was already pinned shared
371 * (i.e. if the NIB belongs to a secure guest and is shared), then 0
372 * (success) is returned. If the NIB was not shared, vfio_pin_pages() had
373 * exported it and now it does not belong to the secure guest anymore. In
374 * that case, an error is returned.
376 * Context: the NIB (at physical address @addr) has to be pinned with
377 * vfio_pin_pages() before calling this function.
379 * Return: 0 in case of success, otherwise an error < 0.
381 static int ensure_nib_shared(unsigned long addr)
384 * The nib has to be located in shared storage since guest and
385 * host access it. vfio_pin_pages() will do a pin shared and
386 * if that fails (possibly because it's not a shared page) it
387 * calls export. We try to do a second pin shared here so that
388 * the UV gives us an error code if we try to pin a non-shared
391 * If the page is already pinned shared the UV will return a success.
393 return uv_pin_shared(addr);
397 * vfio_ap_irq_enable - Enable Interruption for a APQN
399 * @q: the vfio_ap_queue holding AQIC parameters
400 * @isc: the guest ISC to register with the GIB interface
401 * @vcpu: the vcpu object containing the registers specifying the parameters
402 * passed to the PQAP(AQIC) instruction.
404 * Pin the NIB saved in *q
405 * Register the guest ISC to GIB interface and retrieve the
406 * host ISC to issue the host side PQAP/AQIC
408 * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
409 * vfio_pin_pages or kvm_s390_gisc_register failed.
411 * Otherwise return the ap_queue_status returned by the ap_aqic(),
412 * all retry handling will be done by the guest.
414 * Return: &struct ap_queue_status
416 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
418 struct kvm_vcpu *vcpu)
420 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
421 struct ap_queue_status status = {};
422 struct kvm_s390_gisa *gisa;
430 /* Verify that the notification indicator byte address is valid */
431 if (vfio_ap_validate_nib(vcpu, &nib)) {
432 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
433 __func__, &nib, q->apqn);
435 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
439 /* The pin will probably be successful even if the NIB was not shared */
440 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
441 IOMMU_READ | IOMMU_WRITE, &h_page);
446 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
447 "nib=%pad, apqn=%#04x\n",
448 __func__, ret, &nib, q->apqn);
450 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
454 kvm = q->matrix_mdev->kvm;
455 gisa = kvm->arch.gisa_int.origin;
457 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
458 aqic_gisa.gisc = isc;
460 /* NIB in non-shared storage is a rc 6 for PV guests */
461 if (kvm_s390_pv_cpu_is_protected(vcpu) &&
462 ensure_nib_shared(h_nib & PAGE_MASK)) {
463 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
464 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
468 nisc = kvm_s390_gisc_register(kvm, isc);
470 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
471 __func__, nisc, isc, q->apqn);
473 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
474 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
478 aqic_gisa.isc = nisc;
480 aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
482 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
483 switch (status.response_code) {
484 case AP_RESPONSE_NORMAL:
485 /* See if we did clear older IRQ configuration */
486 vfio_ap_free_aqic_resources(q);
490 case AP_RESPONSE_OTHERWISE_CHANGED:
491 /* We could not modify IRQ settings: clear new configuration */
492 ret = kvm_s390_gisc_unregister(kvm, isc);
494 VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n",
495 __func__, ret, isc, q->apqn);
496 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
499 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
500 status.response_code);
501 vfio_ap_irq_disable(q);
505 if (status.response_code != AP_RESPONSE_NORMAL) {
506 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
507 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
508 "gisa=%#x, isc=%#x, apqn=%#04x\n",
509 __func__, status.response_code,
510 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
511 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
519 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
520 * of big endian elements that can be passed by
521 * value to an s390dbf sprintf event function to
522 * format a UUID string.
524 * @guid: the object containing the little endian guid
525 * @uuid: a six-element array of long values that can be passed by value as
526 * arguments for a formatting string specifying a UUID.
528 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
529 * event functions if the memory for the passed string is available as long as
530 * the debug feature exists. Since a mediated device can be removed at any
531 * time, it's name can not be used because %s passes the reference to the string
532 * in memory and the reference will go stale once the device is removed .
534 * The s390dbf string formatting function allows a maximum of 9 arguments for a
535 * message to be displayed in the 'sprintf' view. In order to use the bytes
536 * comprising the mediated device's UUID to display the mediated device name,
537 * they will have to be converted into an array whose elements can be passed by
538 * value to sprintf. For example:
540 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
541 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
542 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
543 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
545 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
548 * The input guid is ordered in little endian, so it needs to be
549 * reordered for displaying a UUID as a string. This specifies the
550 * guid indices in proper order.
552 uuid[0] = le32_to_cpup((__le32 *)guid);
553 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
554 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
555 uuid[3] = *((__u16 *)&guid->b[8]);
556 uuid[4] = *((__u16 *)&guid->b[10]);
557 uuid[5] = *((__u32 *)&guid->b[12]);
561 * handle_pqap - PQAP instruction callback
563 * @vcpu: The vcpu on which we received the PQAP instruction
565 * Get the general register contents to initialize internal variables.
570 * Response.status may be set to following Response Code:
571 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
572 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
573 * - AP_RESPONSE_NORMAL (0) : in case of success
574 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
575 * We take the matrix_dev lock to ensure serialization on queues and
576 * mediated device access.
578 * Return: 0 if we could handle the request inside KVM.
579 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
581 static int handle_pqap(struct kvm_vcpu *vcpu)
585 unsigned long uuid[6];
586 struct vfio_ap_queue *q;
587 struct ap_queue_status qstatus = {
588 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
589 struct ap_matrix_mdev *matrix_mdev;
591 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
593 /* If we do not use the AIV facility just go to userland */
594 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
595 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
596 __func__, apqn, vcpu->arch.sie_block->eca);
601 mutex_lock(&matrix_dev->mdevs_lock);
603 if (!vcpu->kvm->arch.crypto.pqap_hook) {
604 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
610 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
611 struct ap_matrix_mdev, pqap_hook);
613 /* If the there is no guest using the mdev, there is nothing to do */
614 if (!matrix_mdev->kvm) {
615 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
616 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
617 __func__, uuid[0], uuid[1], uuid[2],
618 uuid[3], uuid[4], uuid[5], apqn);
622 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
624 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
625 __func__, AP_QID_CARD(apqn),
630 status = vcpu->run->s.regs.gprs[1];
632 /* If IR bit(16) is set we enable the interrupt */
633 if ((status >> (63 - 16)) & 0x01)
634 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
636 qstatus = vfio_ap_irq_disable(q);
639 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
640 vcpu->run->s.regs.gprs[1] >>= 32;
641 mutex_unlock(&matrix_dev->mdevs_lock);
645 static void vfio_ap_matrix_init(struct ap_config_info *info,
646 struct ap_matrix *matrix)
648 matrix->apm_max = info->apxa ? info->na : 63;
649 matrix->aqm_max = info->apxa ? info->nd : 15;
650 matrix->adm_max = info->apxa ? info->nd : 15;
653 static void signal_guest_ap_cfg_changed(struct ap_matrix_mdev *matrix_mdev)
655 if (matrix_mdev->cfg_chg_trigger)
656 eventfd_signal(matrix_mdev->cfg_chg_trigger);
659 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
661 if (matrix_mdev->kvm) {
662 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
663 matrix_mdev->shadow_apcb.apm,
664 matrix_mdev->shadow_apcb.aqm,
665 matrix_mdev->shadow_apcb.adm);
667 signal_guest_ap_cfg_changed(matrix_mdev);
671 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
673 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
675 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
676 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
677 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
679 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
683 static bool _queue_passable(struct vfio_ap_queue *q)
688 switch (q->reset_status.response_code) {
689 case AP_RESPONSE_NORMAL:
690 case AP_RESPONSE_DECONFIGURED:
691 case AP_RESPONSE_CHECKSTOPPED:
699 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
700 * to ensure no queue devices are passed through to
701 * the guest that are not bound to the vfio_ap
704 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
705 * @apm_filtered: a 256-bit bitmap for storing the APIDs filtered from the
706 * guest's AP configuration that are still in the host's AP
709 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
710 * driver, its APID will be filtered from the guest's APCB. The matrix
711 * structure precludes filtering an individual APQN, so its APID will be
712 * filtered. Consequently, all queues associated with the adapter that
713 * are in the host's AP configuration must be reset. If queues are
714 * subsequently made available again to the guest, they should re-appear
717 * Return: a boolean value indicating whether the KVM guest's APCB was changed
718 * by the filtering or not.
720 static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
721 unsigned long *apm_filtered)
723 unsigned long apid, apqi, apqn;
724 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
725 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
727 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
728 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
729 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
730 bitmap_clear(apm_filtered, 0, AP_DEVICES);
733 * Copy the adapters, domains and control domains to the shadow_apcb
734 * from the matrix mdev, but only those that are assigned to the host's
737 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
738 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
739 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
740 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
742 for_each_set_bit_inv(apid, matrix_mdev->shadow_apcb.apm, AP_DEVICES) {
743 for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
746 * If the APQN is not bound to the vfio_ap device
747 * driver, then we can't assign it to the guest's
748 * AP configuration. The AP architecture won't
749 * allow filtering of a single APQN, so let's filter
750 * the APID since an adapter represents a physical
753 apqn = AP_MKQID(apid, apqi);
754 if (!_queue_passable(vfio_ap_mdev_get_queue(matrix_mdev, apqn))) {
755 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
758 * If the adapter was previously plugged into
759 * the guest, let's let the caller know that
760 * the APID was filtered.
762 if (test_bit_inv(apid, prev_shadow_apm))
763 set_bit_inv(apid, apm_filtered);
770 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
772 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
776 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
778 struct ap_matrix_mdev *matrix_mdev =
779 container_of(vdev, struct ap_matrix_mdev, vdev);
781 matrix_mdev->mdev = to_mdev_device(vdev->dev);
782 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
783 matrix_mdev->pqap_hook = handle_pqap;
784 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
785 hash_init(matrix_mdev->qtable.queues);
790 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
792 struct ap_matrix_mdev *matrix_mdev;
795 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
796 &vfio_ap_matrix_dev_ops);
797 if (IS_ERR(matrix_mdev))
798 return PTR_ERR(matrix_mdev);
800 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
803 matrix_mdev->req_trigger = NULL;
804 matrix_mdev->cfg_chg_trigger = NULL;
805 dev_set_drvdata(&mdev->dev, matrix_mdev);
806 mutex_lock(&matrix_dev->mdevs_lock);
807 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
808 mutex_unlock(&matrix_dev->mdevs_lock);
812 vfio_put_device(&matrix_mdev->vdev);
816 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
817 struct vfio_ap_queue *q)
819 if (!q || vfio_ap_mdev_get_queue(matrix_mdev, q->apqn))
822 q->matrix_mdev = matrix_mdev;
823 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
826 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
828 struct vfio_ap_queue *q;
830 q = vfio_ap_find_queue(apqn);
831 vfio_ap_mdev_link_queue(matrix_mdev, q);
834 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
836 hash_del(&q->mdev_qnode);
839 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
841 q->matrix_mdev = NULL;
844 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
846 struct vfio_ap_queue *q;
847 unsigned long apid, apqi;
849 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
850 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
852 q = vfio_ap_mdev_get_queue(matrix_mdev,
853 AP_MKQID(apid, apqi));
855 q->matrix_mdev = NULL;
860 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
862 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
864 vfio_unregister_group_dev(&matrix_mdev->vdev);
866 mutex_lock(&matrix_dev->guests_lock);
867 mutex_lock(&matrix_dev->mdevs_lock);
868 vfio_ap_mdev_reset_queues(matrix_mdev);
869 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
870 list_del(&matrix_mdev->node);
871 mutex_unlock(&matrix_dev->mdevs_lock);
872 mutex_unlock(&matrix_dev->guests_lock);
873 vfio_put_device(&matrix_mdev->vdev);
876 #define MDEV_SHARING_ERR "Userspace may not assign queue %02lx.%04lx to mdev: already assigned to %s"
878 #define MDEV_IN_USE_ERR "Can not reserve queue %02lx.%04lx for host driver: in use by mdev"
880 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *assignee,
881 struct ap_matrix_mdev *assigned_to,
882 unsigned long *apm, unsigned long *aqm)
884 unsigned long apid, apqi;
886 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
887 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
888 dev_warn(mdev_dev(assignee->mdev), MDEV_SHARING_ERR,
889 apid, apqi, dev_name(mdev_dev(assigned_to->mdev)));
894 static void vfio_ap_mdev_log_in_use_err(struct ap_matrix_mdev *assignee,
895 unsigned long *apm, unsigned long *aqm)
897 unsigned long apid, apqi;
899 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
900 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
901 dev_warn(mdev_dev(assignee->mdev), MDEV_IN_USE_ERR, apid, apqi);
906 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
908 * @assignee: the matrix mdev to which @mdev_apm and @mdev_aqm are being
909 * assigned; or, NULL if this function was called by the AP bus
910 * driver in_use callback to verify none of the APQNs being reserved
911 * for the host device driver are in use by a vfio_ap mediated device
912 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
913 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
915 * Verifies that each APQN derived from the Cartesian product of APIDs
916 * represented by the bits set in @mdev_apm and the APQIs of the bits set in
917 * @mdev_aqm is not assigned to a mediated device other than the mdev to which
918 * the APQN is being assigned (@assignee). AP queue sharing is not allowed.
920 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
922 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *assignee,
923 unsigned long *mdev_apm,
924 unsigned long *mdev_aqm)
926 struct ap_matrix_mdev *assigned_to;
927 DECLARE_BITMAP(apm, AP_DEVICES);
928 DECLARE_BITMAP(aqm, AP_DOMAINS);
930 list_for_each_entry(assigned_to, &matrix_dev->mdev_list, node) {
932 * If the mdev to which the mdev_apm and mdev_aqm is being
933 * assigned is the same as the mdev being verified
935 if (assignee == assigned_to)
938 memset(apm, 0, sizeof(apm));
939 memset(aqm, 0, sizeof(aqm));
942 * We work on full longs, as we can only exclude the leftover
943 * bits in non-inverse order. The leftover is all zeros.
945 if (!bitmap_and(apm, mdev_apm, assigned_to->matrix.apm, AP_DEVICES))
948 if (!bitmap_and(aqm, mdev_aqm, assigned_to->matrix.aqm, AP_DOMAINS))
952 vfio_ap_mdev_log_sharing_err(assignee, assigned_to, apm, aqm);
954 vfio_ap_mdev_log_in_use_err(assigned_to, apm, aqm);
963 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
964 * not reserved for the default zcrypt driver and
965 * are not assigned to another mdev.
967 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
969 * Return: One of the following values:
970 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
971 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
972 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
973 * zcrypt default driver.
974 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
975 * o A zero indicating validation succeeded.
977 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
979 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
980 matrix_mdev->matrix.aqm))
981 return -EADDRNOTAVAIL;
983 return vfio_ap_mdev_verify_no_sharing(matrix_mdev,
984 matrix_mdev->matrix.apm,
985 matrix_mdev->matrix.aqm);
988 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
993 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
994 vfio_ap_mdev_link_apqn(matrix_mdev,
995 AP_MKQID(apid, apqi));
998 static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,
1000 struct list_head *qlist)
1002 struct vfio_ap_queue *q;
1005 for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS) {
1006 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1008 list_add_tail(&q->reset_qnode, qlist);
1012 static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,
1015 struct list_head qlist;
1017 INIT_LIST_HEAD(&qlist);
1018 collect_queues_to_reset(matrix_mdev, apid, &qlist);
1019 vfio_ap_mdev_reset_qlist(&qlist);
1022 static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
1023 unsigned long *apm_reset)
1025 struct list_head qlist;
1028 if (bitmap_empty(apm_reset, AP_DEVICES))
1031 INIT_LIST_HEAD(&qlist);
1033 for_each_set_bit_inv(apid, apm_reset, AP_DEVICES)
1034 collect_queues_to_reset(matrix_mdev, apid, &qlist);
1036 return vfio_ap_mdev_reset_qlist(&qlist);
1040 * assign_adapter_store - parses the APID from @buf and sets the
1041 * corresponding bit in the mediated matrix device's APM
1043 * @dev: the matrix device
1044 * @attr: the mediated matrix device's assign_adapter attribute
1045 * @buf: a buffer containing the AP adapter number (APID) to
1047 * @count: the number of bytes in @buf
1049 * Return: the number of bytes processed if the APID is valid; otherwise,
1050 * returns one of the following errors:
1053 * The APID is not a valid number
1056 * The APID exceeds the maximum value configured for the system
1059 * An APQN derived from the cross product of the APID being assigned
1060 * and the APQIs previously assigned is not bound to the vfio_ap device
1061 * driver; or, if no APQIs have yet been assigned, the APID is not
1062 * contained in an APQN bound to the vfio_ap device driver.
1065 * An APQN derived from the cross product of the APID being assigned
1066 * and the APQIs previously assigned is being used by another mediated
1070 * A lock required to validate the mdev's AP configuration could not
1073 static ssize_t assign_adapter_store(struct device *dev,
1074 struct device_attribute *attr,
1075 const char *buf, size_t count)
1079 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1080 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1082 mutex_lock(&ap_perms_mutex);
1083 get_update_locks_for_mdev(matrix_mdev);
1085 ret = kstrtoul(buf, 0, &apid);
1089 if (apid > matrix_mdev->matrix.apm_max) {
1094 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1099 set_bit_inv(apid, matrix_mdev->matrix.apm);
1101 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1103 clear_bit_inv(apid, matrix_mdev->matrix.apm);
1107 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
1109 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1110 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1111 reset_queues_for_apids(matrix_mdev, apm_filtered);
1116 release_update_locks_for_mdev(matrix_mdev);
1117 mutex_unlock(&ap_perms_mutex);
1121 static DEVICE_ATTR_WO(assign_adapter);
1123 static struct vfio_ap_queue
1124 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
1125 unsigned long apid, unsigned long apqi)
1127 struct vfio_ap_queue *q = NULL;
1129 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1130 /* If the queue is assigned to the matrix mdev, unlink it. */
1132 vfio_ap_unlink_queue_fr_mdev(q);
1138 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
1139 * adapter from the matrix mdev to which the
1140 * adapter was assigned.
1141 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
1142 * @apid: the APID of the unassigned adapter.
1143 * @qlist: list for storing queues associated with unassigned adapter that
1146 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1148 struct list_head *qlist)
1151 struct vfio_ap_queue *q;
1153 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1154 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1157 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1158 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1159 list_add_tail(&q->reset_qnode, qlist);
1164 static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,
1165 unsigned long *apids)
1167 struct vfio_ap_queue *q, *tmpq;
1168 struct list_head qlist;
1170 bool apcb_update = false;
1172 INIT_LIST_HEAD(&qlist);
1174 for_each_set_bit_inv(apid, apids, AP_DEVICES) {
1175 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
1177 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1178 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1183 /* Only update apcb if needed to avoid impacting guest */
1185 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1187 vfio_ap_mdev_reset_qlist(&qlist);
1189 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1190 vfio_ap_unlink_mdev_fr_queue(q);
1191 list_del(&q->reset_qnode);
1195 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1198 DECLARE_BITMAP(apids, AP_DEVICES);
1200 bitmap_zero(apids, AP_DEVICES);
1201 set_bit_inv(apid, apids);
1202 vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, apids);
1206 * unassign_adapter_store - parses the APID from @buf and clears the
1207 * corresponding bit in the mediated matrix device's APM
1209 * @dev: the matrix device
1210 * @attr: the mediated matrix device's unassign_adapter attribute
1211 * @buf: a buffer containing the adapter number (APID) to be unassigned
1212 * @count: the number of bytes in @buf
1214 * Return: the number of bytes processed if the APID is valid; otherwise,
1215 * returns one of the following errors:
1216 * -EINVAL if the APID is not a number
1217 * -ENODEV if the APID it exceeds the maximum value configured for the
1220 static ssize_t unassign_adapter_store(struct device *dev,
1221 struct device_attribute *attr,
1222 const char *buf, size_t count)
1226 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1228 get_update_locks_for_mdev(matrix_mdev);
1230 ret = kstrtoul(buf, 0, &apid);
1234 if (apid > matrix_mdev->matrix.apm_max) {
1239 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1244 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1245 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1248 release_update_locks_for_mdev(matrix_mdev);
1251 static DEVICE_ATTR_WO(unassign_adapter);
1253 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1258 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1259 vfio_ap_mdev_link_apqn(matrix_mdev,
1260 AP_MKQID(apid, apqi));
1264 * assign_domain_store - parses the APQI from @buf and sets the
1265 * corresponding bit in the mediated matrix device's AQM
1267 * @dev: the matrix device
1268 * @attr: the mediated matrix device's assign_domain attribute
1269 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1271 * @count: the number of bytes in @buf
1273 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1274 * one of the following errors:
1277 * The APQI is not a valid number
1280 * The APQI exceeds the maximum value configured for the system
1283 * An APQN derived from the cross product of the APQI being assigned
1284 * and the APIDs previously assigned is not bound to the vfio_ap device
1285 * driver; or, if no APIDs have yet been assigned, the APQI is not
1286 * contained in an APQN bound to the vfio_ap device driver.
1289 * An APQN derived from the cross product of the APQI being assigned
1290 * and the APIDs previously assigned is being used by another mediated
1294 * The lock required to validate the mdev's AP configuration could not
1297 static ssize_t assign_domain_store(struct device *dev,
1298 struct device_attribute *attr,
1299 const char *buf, size_t count)
1303 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1304 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1306 mutex_lock(&ap_perms_mutex);
1307 get_update_locks_for_mdev(matrix_mdev);
1309 ret = kstrtoul(buf, 0, &apqi);
1313 if (apqi > matrix_mdev->matrix.aqm_max) {
1318 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1323 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1325 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1327 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1331 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1333 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1334 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1335 reset_queues_for_apids(matrix_mdev, apm_filtered);
1340 release_update_locks_for_mdev(matrix_mdev);
1341 mutex_unlock(&ap_perms_mutex);
1345 static DEVICE_ATTR_WO(assign_domain);
1347 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1349 struct list_head *qlist)
1352 struct vfio_ap_queue *q;
1354 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1355 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1358 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1359 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1360 list_add_tail(&q->reset_qnode, qlist);
1365 static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,
1366 unsigned long *apqis)
1368 struct vfio_ap_queue *q, *tmpq;
1369 struct list_head qlist;
1371 bool apcb_update = false;
1373 INIT_LIST_HEAD(&qlist);
1375 for_each_set_bit_inv(apqi, apqis, AP_DOMAINS) {
1376 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
1378 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1379 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1384 /* Only update apcb if needed to avoid impacting guest */
1386 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1388 vfio_ap_mdev_reset_qlist(&qlist);
1390 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1391 vfio_ap_unlink_mdev_fr_queue(q);
1392 list_del(&q->reset_qnode);
1396 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1399 DECLARE_BITMAP(apqis, AP_DOMAINS);
1401 bitmap_zero(apqis, AP_DEVICES);
1402 set_bit_inv(apqi, apqis);
1403 vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis);
1407 * unassign_domain_store - parses the APQI from @buf and clears the
1408 * corresponding bit in the mediated matrix device's AQM
1410 * @dev: the matrix device
1411 * @attr: the mediated matrix device's unassign_domain attribute
1412 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1414 * @count: the number of bytes in @buf
1416 * Return: the number of bytes processed if the APQI is valid; otherwise,
1417 * returns one of the following errors:
1418 * -EINVAL if the APQI is not a number
1419 * -ENODEV if the APQI exceeds the maximum value configured for the system
1421 static ssize_t unassign_domain_store(struct device *dev,
1422 struct device_attribute *attr,
1423 const char *buf, size_t count)
1427 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1429 get_update_locks_for_mdev(matrix_mdev);
1431 ret = kstrtoul(buf, 0, &apqi);
1435 if (apqi > matrix_mdev->matrix.aqm_max) {
1440 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1445 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1446 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1450 release_update_locks_for_mdev(matrix_mdev);
1453 static DEVICE_ATTR_WO(unassign_domain);
1456 * assign_control_domain_store - parses the domain ID from @buf and sets
1457 * the corresponding bit in the mediated matrix device's ADM
1459 * @dev: the matrix device
1460 * @attr: the mediated matrix device's assign_control_domain attribute
1461 * @buf: a buffer containing the domain ID to be assigned
1462 * @count: the number of bytes in @buf
1464 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1465 * returns one of the following errors:
1466 * -EINVAL if the ID is not a number
1467 * -ENODEV if the ID exceeds the maximum value configured for the system
1469 static ssize_t assign_control_domain_store(struct device *dev,
1470 struct device_attribute *attr,
1471 const char *buf, size_t count)
1475 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1477 get_update_locks_for_mdev(matrix_mdev);
1479 ret = kstrtoul(buf, 0, &id);
1483 if (id > matrix_mdev->matrix.adm_max) {
1488 if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1493 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1494 * domain number (id). The bits in the mask, from most significant to
1495 * least significant, correspond to IDs 0 up to the one less than the
1496 * number of control domains that can be assigned.
1498 set_bit_inv(id, matrix_mdev->matrix.adm);
1499 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1500 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1504 release_update_locks_for_mdev(matrix_mdev);
1507 static DEVICE_ATTR_WO(assign_control_domain);
1510 * unassign_control_domain_store - parses the domain ID from @buf and
1511 * clears the corresponding bit in the mediated matrix device's ADM
1513 * @dev: the matrix device
1514 * @attr: the mediated matrix device's unassign_control_domain attribute
1515 * @buf: a buffer containing the domain ID to be unassigned
1516 * @count: the number of bytes in @buf
1518 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1519 * returns one of the following errors:
1520 * -EINVAL if the ID is not a number
1521 * -ENODEV if the ID exceeds the maximum value configured for the system
1523 static ssize_t unassign_control_domain_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t count)
1528 unsigned long domid;
1529 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1531 get_update_locks_for_mdev(matrix_mdev);
1533 ret = kstrtoul(buf, 0, &domid);
1537 if (domid > matrix_mdev->matrix.adm_max) {
1542 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1547 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1549 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1550 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1551 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1556 release_update_locks_for_mdev(matrix_mdev);
1559 static DEVICE_ATTR_WO(unassign_control_domain);
1561 static ssize_t control_domains_show(struct device *dev,
1562 struct device_attribute *dev_attr,
1566 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1567 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1570 mutex_lock(&matrix_dev->mdevs_lock);
1571 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1)
1572 nchars += sysfs_emit_at(buf, nchars, "%04lx\n", id);
1573 mutex_unlock(&matrix_dev->mdevs_lock);
1577 static DEVICE_ATTR_RO(control_domains);
1579 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1583 unsigned long apid1;
1584 unsigned long apqi1;
1585 unsigned long napm_bits = matrix->apm_max + 1;
1586 unsigned long naqm_bits = matrix->aqm_max + 1;
1589 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1590 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1592 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1593 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1594 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits)
1595 nchars += sysfs_emit_at(buf, nchars, "%02lx.%04lx\n", apid, apqi);
1597 } else if (apid1 < napm_bits) {
1598 for_each_set_bit_inv(apid, matrix->apm, napm_bits)
1599 nchars += sysfs_emit_at(buf, nchars, "%02lx.\n", apid);
1600 } else if (apqi1 < naqm_bits) {
1601 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits)
1602 nchars += sysfs_emit_at(buf, nchars, ".%04lx\n", apqi);
1608 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1612 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1614 mutex_lock(&matrix_dev->mdevs_lock);
1615 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1616 mutex_unlock(&matrix_dev->mdevs_lock);
1620 static DEVICE_ATTR_RO(matrix);
1622 static ssize_t guest_matrix_show(struct device *dev,
1623 struct device_attribute *attr, char *buf)
1626 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1628 mutex_lock(&matrix_dev->mdevs_lock);
1629 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1630 mutex_unlock(&matrix_dev->mdevs_lock);
1634 static DEVICE_ATTR_RO(guest_matrix);
1636 static ssize_t write_ap_bitmap(unsigned long *bitmap, char *buf, int offset, char sep)
1638 return sysfs_emit_at(buf, offset, "0x%016lx%016lx%016lx%016lx%c",
1639 bitmap[0], bitmap[1], bitmap[2], bitmap[3], sep);
1642 static ssize_t ap_config_show(struct device *dev, struct device_attribute *attr,
1645 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1648 idx += write_ap_bitmap(matrix_mdev->matrix.apm, buf, idx, ',');
1649 idx += write_ap_bitmap(matrix_mdev->matrix.aqm, buf, idx, ',');
1650 idx += write_ap_bitmap(matrix_mdev->matrix.adm, buf, idx, '\n');
1655 /* Number of characters needed for a complete hex mask representing the bits in .. */
1656 #define AP_DEVICES_STRLEN (AP_DEVICES / 4 + 3)
1657 #define AP_DOMAINS_STRLEN (AP_DOMAINS / 4 + 3)
1658 #define AP_CONFIG_STRLEN (AP_DEVICES_STRLEN + 2 * AP_DOMAINS_STRLEN)
1660 static int parse_bitmap(char **strbufptr, unsigned long *bitmap, int nbits)
1664 curmask = strsep(strbufptr, ",\n");
1668 bitmap_clear(bitmap, 0, nbits);
1669 return ap_hex2bitmap(curmask, bitmap, nbits);
1672 static int ap_matrix_overflow_check(struct ap_matrix_mdev *matrix_mdev)
1676 for_each_set_bit_inv(bit, matrix_mdev->matrix.apm, AP_DEVICES) {
1677 if (bit > matrix_mdev->matrix.apm_max)
1681 for_each_set_bit_inv(bit, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1682 if (bit > matrix_mdev->matrix.aqm_max)
1686 for_each_set_bit_inv(bit, matrix_mdev->matrix.adm, AP_DOMAINS) {
1687 if (bit > matrix_mdev->matrix.adm_max)
1694 static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
1696 /* This check works around false positive gcc -Wstringop-overread */
1700 bitmap_copy(dst->apm, src->apm, AP_DEVICES);
1701 bitmap_copy(dst->aqm, src->aqm, AP_DOMAINS);
1702 bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
1705 static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
1706 const char *buf, size_t count)
1708 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1709 struct ap_matrix m_new, m_old, m_added, m_removed;
1710 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1711 unsigned long newbit;
1712 char *newbuf, *rest;
1716 newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
1721 mutex_lock(&ap_perms_mutex);
1722 get_update_locks_for_mdev(matrix_mdev);
1724 /* Save old state */
1725 ap_matrix_copy(&m_old, &matrix_mdev->matrix);
1726 if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
1727 parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
1728 parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
1733 bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
1734 bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
1735 bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
1736 bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
1738 /* Need new bitmaps in matrix_mdev for validation */
1739 ap_matrix_copy(&matrix_mdev->matrix, &m_new);
1741 /* Ensure new state is valid, else undo new state */
1742 rc = vfio_ap_mdev_validate_masks(matrix_mdev);
1744 ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1747 rc = ap_matrix_overflow_check(matrix_mdev);
1749 ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1754 /* Need old bitmaps in matrix_mdev for unplug/unlink */
1755 ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1757 /* Unlink removed adapters/domains */
1758 vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, m_removed.apm);
1759 vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
1761 /* Need new bitmaps in matrix_mdev for linking new adapters/domains */
1762 ap_matrix_copy(&matrix_mdev->matrix, &m_new);
1764 /* Link newly added adapters */
1765 for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
1766 vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
1768 for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
1769 vfio_ap_mdev_link_domain(matrix_mdev, newbit);
1771 /* filter resources not bound to vfio-ap */
1772 do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
1773 do_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
1775 /* Apply changes to shadow apbc if things changed */
1777 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1778 reset_queues_for_apids(matrix_mdev, apm_filtered);
1781 release_update_locks_for_mdev(matrix_mdev);
1782 mutex_unlock(&ap_perms_mutex);
1786 static DEVICE_ATTR_RW(ap_config);
1788 static struct attribute *vfio_ap_mdev_attrs[] = {
1789 &dev_attr_assign_adapter.attr,
1790 &dev_attr_unassign_adapter.attr,
1791 &dev_attr_assign_domain.attr,
1792 &dev_attr_unassign_domain.attr,
1793 &dev_attr_assign_control_domain.attr,
1794 &dev_attr_unassign_control_domain.attr,
1795 &dev_attr_ap_config.attr,
1796 &dev_attr_control_domains.attr,
1797 &dev_attr_matrix.attr,
1798 &dev_attr_guest_matrix.attr,
1802 static struct attribute_group vfio_ap_mdev_attr_group = {
1803 .attrs = vfio_ap_mdev_attrs
1806 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1807 &vfio_ap_mdev_attr_group,
1812 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1813 * to manage AP resources for the guest whose state is represented by @kvm
1815 * @matrix_mdev: a mediated matrix device
1816 * @kvm: reference to KVM instance
1818 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1819 * otherwise, returns an -EPERM.
1821 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1824 struct ap_matrix_mdev *m;
1826 if (kvm->arch.crypto.crycbd) {
1827 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1828 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1829 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1831 get_update_locks_for_kvm(kvm);
1833 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1834 if (m != matrix_mdev && m->kvm == kvm) {
1835 release_update_locks_for_kvm(kvm);
1841 matrix_mdev->kvm = kvm;
1842 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1844 release_update_locks_for_kvm(kvm);
1850 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1852 struct ap_queue_table *qtable = &matrix_mdev->qtable;
1853 struct vfio_ap_queue *q;
1856 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1857 if (q->saved_iova >= iova && q->saved_iova < iova + length)
1858 vfio_ap_irq_disable(q);
1862 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1865 struct ap_matrix_mdev *matrix_mdev =
1866 container_of(vdev, struct ap_matrix_mdev, vdev);
1868 mutex_lock(&matrix_dev->mdevs_lock);
1870 unmap_iova(matrix_mdev, iova, length);
1872 mutex_unlock(&matrix_dev->mdevs_lock);
1876 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1879 * @matrix_mdev: a matrix mediated device
1881 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1883 struct kvm *kvm = matrix_mdev->kvm;
1885 if (kvm && kvm->arch.crypto.crycbd) {
1886 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1887 kvm->arch.crypto.pqap_hook = NULL;
1888 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1890 get_update_locks_for_kvm(kvm);
1892 kvm_arch_crypto_clear_masks(kvm);
1893 vfio_ap_mdev_reset_queues(matrix_mdev);
1895 matrix_mdev->kvm = NULL;
1897 release_update_locks_for_kvm(kvm);
1901 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1903 struct ap_queue *queue;
1904 struct vfio_ap_queue *q = NULL;
1906 queue = ap_get_qdev(apqn);
1910 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1911 q = dev_get_drvdata(&queue->ap_dev.device);
1913 put_device(&queue->ap_dev.device);
1918 static int apq_status_check(int apqn, struct ap_queue_status *status)
1920 switch (status->response_code) {
1921 case AP_RESPONSE_NORMAL:
1922 case AP_RESPONSE_DECONFIGURED:
1923 case AP_RESPONSE_CHECKSTOPPED:
1925 case AP_RESPONSE_RESET_IN_PROGRESS:
1926 case AP_RESPONSE_BUSY:
1928 case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
1929 case AP_RESPONSE_ASSOC_FAILED:
1931 * These asynchronous response codes indicate a PQAP(AAPQ)
1932 * instruction to associate a secret with the guest failed. All
1933 * subsequent AP instructions will end with the asynchronous
1934 * response code until the AP queue is reset; so, let's return
1935 * a value indicating a reset needs to be performed again.
1940 "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1941 AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1942 status->response_code);
1947 #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
1949 static void apq_reset_check(struct work_struct *reset_work)
1951 int ret = -EBUSY, elapsed = 0;
1952 struct ap_queue_status status;
1953 struct vfio_ap_queue *q;
1955 q = container_of(reset_work, struct vfio_ap_queue, reset_work);
1956 memcpy(&status, &q->reset_status, sizeof(status));
1958 msleep(AP_RESET_INTERVAL);
1959 elapsed += AP_RESET_INTERVAL;
1960 status = ap_tapq(q->apqn, NULL);
1961 ret = apq_status_check(q->apqn, &status);
1964 if (ret == -EBUSY) {
1965 pr_notice_ratelimited(WAIT_MSG, elapsed,
1966 AP_QID_CARD(q->apqn),
1967 AP_QID_QUEUE(q->apqn),
1968 status.response_code,
1970 status.irq_enabled);
1972 if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
1973 q->reset_status.response_code == AP_RESPONSE_BUSY ||
1974 q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
1976 status = ap_zapq(q->apqn, 0);
1977 memcpy(&q->reset_status, &status, sizeof(status));
1980 if (q->saved_isc != VFIO_AP_ISC_INVALID)
1981 vfio_ap_free_aqic_resources(q);
1987 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1989 struct ap_queue_status status;
1993 status = ap_zapq(q->apqn, 0);
1994 memcpy(&q->reset_status, &status, sizeof(status));
1995 switch (status.response_code) {
1996 case AP_RESPONSE_NORMAL:
1997 case AP_RESPONSE_RESET_IN_PROGRESS:
1998 case AP_RESPONSE_BUSY:
1999 case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
2001 * Let's verify whether the ZAPQ completed successfully on a work queue.
2003 queue_work(system_long_wq, &q->reset_work);
2005 case AP_RESPONSE_DECONFIGURED:
2006 case AP_RESPONSE_CHECKSTOPPED:
2007 vfio_ap_free_aqic_resources(q);
2011 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
2012 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
2013 status.response_code);
2017 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
2019 int ret = 0, loop_cursor;
2020 struct vfio_ap_queue *q;
2022 hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode)
2023 vfio_ap_mdev_reset_queue(q);
2025 hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode) {
2026 flush_work(&q->reset_work);
2028 if (q->reset_status.response_code)
2035 static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
2038 struct vfio_ap_queue *q;
2040 list_for_each_entry(q, qlist, reset_qnode)
2041 vfio_ap_mdev_reset_queue(q);
2043 list_for_each_entry(q, qlist, reset_qnode) {
2044 flush_work(&q->reset_work);
2046 if (q->reset_status.response_code)
2053 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
2055 struct ap_matrix_mdev *matrix_mdev =
2056 container_of(vdev, struct ap_matrix_mdev, vdev);
2061 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
2064 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
2066 struct ap_matrix_mdev *matrix_mdev =
2067 container_of(vdev, struct ap_matrix_mdev, vdev);
2069 vfio_ap_mdev_unset_kvm(matrix_mdev);
2072 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
2074 struct device *dev = vdev->dev;
2075 struct ap_matrix_mdev *matrix_mdev;
2077 matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
2079 get_update_locks_for_mdev(matrix_mdev);
2081 if (matrix_mdev->kvm) {
2082 kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
2083 signal_guest_ap_cfg_changed(matrix_mdev);
2086 if (matrix_mdev->req_trigger) {
2088 dev_notice_ratelimited(dev,
2089 "Relaying device request to user (#%u)\n",
2092 eventfd_signal(matrix_mdev->req_trigger);
2093 } else if (count == 0) {
2095 "No device request registered, blocked until released by user\n");
2098 release_update_locks_for_mdev(matrix_mdev);
2101 static int vfio_ap_mdev_get_device_info(unsigned long arg)
2103 unsigned long minsz;
2104 struct vfio_device_info info;
2106 minsz = offsetofend(struct vfio_device_info, num_irqs);
2108 if (copy_from_user(&info, (void __user *)arg, minsz))
2111 if (info.argsz < minsz)
2114 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
2115 info.num_regions = 0;
2116 info.num_irqs = VFIO_AP_NUM_IRQS;
2118 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
2121 static ssize_t vfio_ap_get_irq_info(unsigned long arg)
2123 unsigned long minsz;
2124 struct vfio_irq_info info;
2126 minsz = offsetofend(struct vfio_irq_info, count);
2128 if (copy_from_user(&info, (void __user *)arg, minsz))
2131 if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
2134 switch (info.index) {
2135 case VFIO_AP_REQ_IRQ_INDEX:
2137 info.flags = VFIO_IRQ_INFO_EVENTFD;
2139 case VFIO_AP_CFG_CHG_IRQ_INDEX:
2141 info.flags = VFIO_IRQ_INFO_EVENTFD;
2147 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
2150 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
2154 unsigned long minsz;
2156 minsz = offsetofend(struct vfio_irq_set, count);
2158 if (copy_from_user(irq_set, (void __user *)arg, minsz))
2161 ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
2166 if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
2172 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
2177 unsigned long minsz;
2178 struct eventfd_ctx *req_trigger;
2180 minsz = offsetofend(struct vfio_irq_set, count);
2181 data = (void __user *)(arg + minsz);
2183 if (get_user(fd, (s32 __user *)data))
2187 if (matrix_mdev->req_trigger)
2188 eventfd_ctx_put(matrix_mdev->req_trigger);
2189 matrix_mdev->req_trigger = NULL;
2190 } else if (fd >= 0) {
2191 req_trigger = eventfd_ctx_fdget(fd);
2192 if (IS_ERR(req_trigger))
2193 return PTR_ERR(req_trigger);
2195 if (matrix_mdev->req_trigger)
2196 eventfd_ctx_put(matrix_mdev->req_trigger);
2198 matrix_mdev->req_trigger = req_trigger;
2206 static int vfio_ap_set_cfg_change_irq(struct ap_matrix_mdev *matrix_mdev, unsigned long arg)
2210 unsigned long minsz;
2211 struct eventfd_ctx *cfg_chg_trigger;
2213 minsz = offsetofend(struct vfio_irq_set, count);
2214 data = (void __user *)(arg + minsz);
2216 if (get_user(fd, (s32 __user *)data))
2220 if (matrix_mdev->cfg_chg_trigger)
2221 eventfd_ctx_put(matrix_mdev->cfg_chg_trigger);
2222 matrix_mdev->cfg_chg_trigger = NULL;
2223 } else if (fd >= 0) {
2224 cfg_chg_trigger = eventfd_ctx_fdget(fd);
2225 if (IS_ERR(cfg_chg_trigger))
2226 return PTR_ERR(cfg_chg_trigger);
2228 if (matrix_mdev->cfg_chg_trigger)
2229 eventfd_ctx_put(matrix_mdev->cfg_chg_trigger);
2231 matrix_mdev->cfg_chg_trigger = cfg_chg_trigger;
2239 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
2243 struct vfio_irq_set irq_set;
2245 ret = vfio_ap_irq_set_init(&irq_set, arg);
2249 switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
2250 case VFIO_IRQ_SET_DATA_EVENTFD:
2251 switch (irq_set.index) {
2252 case VFIO_AP_REQ_IRQ_INDEX:
2253 return vfio_ap_set_request_irq(matrix_mdev, arg);
2254 case VFIO_AP_CFG_CHG_IRQ_INDEX:
2255 return vfio_ap_set_cfg_change_irq(matrix_mdev, arg);
2264 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
2265 unsigned int cmd, unsigned long arg)
2267 struct ap_matrix_mdev *matrix_mdev =
2268 container_of(vdev, struct ap_matrix_mdev, vdev);
2271 mutex_lock(&matrix_dev->mdevs_lock);
2273 case VFIO_DEVICE_GET_INFO:
2274 ret = vfio_ap_mdev_get_device_info(arg);
2276 case VFIO_DEVICE_RESET:
2277 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
2279 case VFIO_DEVICE_GET_IRQ_INFO:
2280 ret = vfio_ap_get_irq_info(arg);
2282 case VFIO_DEVICE_SET_IRQS:
2283 ret = vfio_ap_set_irqs(matrix_mdev, arg);
2289 mutex_unlock(&matrix_dev->mdevs_lock);
2294 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
2296 struct ap_matrix_mdev *matrix_mdev;
2297 unsigned long apid = AP_QID_CARD(q->apqn);
2298 unsigned long apqi = AP_QID_QUEUE(q->apqn);
2300 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2301 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
2302 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
2309 static ssize_t status_show(struct device *dev,
2310 struct device_attribute *attr,
2314 struct vfio_ap_queue *q;
2315 unsigned long apid, apqi;
2316 struct ap_matrix_mdev *matrix_mdev;
2317 struct ap_device *apdev = to_ap_dev(dev);
2319 mutex_lock(&matrix_dev->mdevs_lock);
2320 q = dev_get_drvdata(&apdev->device);
2321 matrix_mdev = vfio_ap_mdev_for_queue(q);
2323 /* If the queue is assigned to the matrix mediated device, then
2324 * determine whether it is passed through to a guest; otherwise,
2325 * indicate that it is unassigned.
2328 apid = AP_QID_CARD(q->apqn);
2329 apqi = AP_QID_QUEUE(q->apqn);
2331 * If the queue is passed through to the guest, then indicate
2332 * that it is in use; otherwise, indicate that it is
2333 * merely assigned to a matrix mediated device.
2335 if (matrix_mdev->kvm &&
2336 test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2337 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
2338 nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_IN_USE);
2340 nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_ASSIGNED);
2342 nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_UNASSIGNED);
2345 mutex_unlock(&matrix_dev->mdevs_lock);
2350 static DEVICE_ATTR_RO(status);
2352 static struct attribute *vfio_queue_attrs[] = {
2353 &dev_attr_status.attr,
2357 static const struct attribute_group vfio_queue_attr_group = {
2358 .attrs = vfio_queue_attrs,
2361 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
2362 .init = vfio_ap_mdev_init_dev,
2363 .open_device = vfio_ap_mdev_open_device,
2364 .close_device = vfio_ap_mdev_close_device,
2365 .ioctl = vfio_ap_mdev_ioctl,
2366 .dma_unmap = vfio_ap_mdev_dma_unmap,
2367 .bind_iommufd = vfio_iommufd_emulated_bind,
2368 .unbind_iommufd = vfio_iommufd_emulated_unbind,
2369 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
2370 .detach_ioas = vfio_iommufd_emulated_detach_ioas,
2371 .request = vfio_ap_mdev_request
2374 static struct mdev_driver vfio_ap_matrix_driver = {
2375 .device_api = VFIO_DEVICE_API_AP_STRING,
2376 .max_instances = MAX_ZDEV_ENTRIES_EXT,
2378 .name = "vfio_ap_mdev",
2379 .owner = THIS_MODULE,
2380 .mod_name = KBUILD_MODNAME,
2381 .dev_groups = vfio_ap_mdev_attr_groups,
2383 .probe = vfio_ap_mdev_probe,
2384 .remove = vfio_ap_mdev_remove,
2387 int vfio_ap_mdev_register(void)
2391 ret = mdev_register_driver(&vfio_ap_matrix_driver);
2395 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
2396 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
2397 matrix_dev->mdev_types = &matrix_dev->mdev_type;
2398 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2399 &vfio_ap_matrix_driver,
2400 &matrix_dev->mdev_types, 1);
2406 mdev_unregister_driver(&vfio_ap_matrix_driver);
2410 void vfio_ap_mdev_unregister(void)
2412 mdev_unregister_parent(&matrix_dev->parent);
2413 mdev_unregister_driver(&vfio_ap_matrix_driver);
2416 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2419 struct vfio_ap_queue *q;
2420 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2421 struct ap_matrix_mdev *matrix_mdev;
2423 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2427 q = kzalloc(sizeof(*q), GFP_KERNEL);
2430 goto err_remove_group;
2433 q->apqn = to_ap_queue(&apdev->device)->qid;
2434 q->saved_isc = VFIO_AP_ISC_INVALID;
2435 memset(&q->reset_status, 0, sizeof(q->reset_status));
2436 INIT_WORK(&q->reset_work, apq_reset_check);
2437 matrix_mdev = get_update_locks_by_apqn(q->apqn);
2440 vfio_ap_mdev_link_queue(matrix_mdev, q);
2443 * If we're in the process of handling the adding of adapters or
2444 * domains to the host's AP configuration, then let the
2445 * vfio_ap device driver's on_scan_complete callback filter the
2446 * matrix and update the guest's AP configuration after all of
2447 * the new queue devices are probed.
2449 if (!bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) ||
2450 !bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS))
2453 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
2454 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2455 reset_queues_for_apids(matrix_mdev, apm_filtered);
2460 dev_set_drvdata(&apdev->device, q);
2461 release_update_locks_for_mdev(matrix_mdev);
2466 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2470 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2472 unsigned long apid, apqi;
2473 struct vfio_ap_queue *q;
2474 struct ap_matrix_mdev *matrix_mdev;
2476 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2477 q = dev_get_drvdata(&apdev->device);
2478 get_update_locks_for_queue(q);
2479 matrix_mdev = q->matrix_mdev;
2480 apid = AP_QID_CARD(q->apqn);
2481 apqi = AP_QID_QUEUE(q->apqn);
2484 /* If the queue is assigned to the guest's AP configuration */
2485 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2486 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2488 * Since the queues are defined via a matrix of adapters
2489 * and domains, it is not possible to hot unplug a
2490 * single queue; so, let's unplug the adapter.
2492 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2493 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2494 reset_queues_for_apid(matrix_mdev, apid);
2500 * If the queue is not in the host's AP configuration, then resetting
2501 * it will fail with response code 01, (APQN not valid); so, let's make
2502 * sure it is in the host's config.
2504 if (test_bit_inv(apid, (unsigned long *)matrix_dev->info.apm) &&
2505 test_bit_inv(apqi, (unsigned long *)matrix_dev->info.aqm)) {
2506 vfio_ap_mdev_reset_queue(q);
2507 flush_work(&q->reset_work);
2512 vfio_ap_unlink_queue_fr_mdev(q);
2514 dev_set_drvdata(&apdev->device, NULL);
2516 release_update_locks_for_mdev(matrix_mdev);
2520 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2521 * assigned to a mediated device under the control
2522 * of the vfio_ap device driver.
2524 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2525 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2528 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2529 * assigned to a mediated device under the control of the vfio_ap
2531 * * Otherwise, return 0.
2533 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2537 mutex_lock(&matrix_dev->guests_lock);
2538 mutex_lock(&matrix_dev->mdevs_lock);
2539 ret = vfio_ap_mdev_verify_no_sharing(NULL, apm, aqm);
2540 mutex_unlock(&matrix_dev->mdevs_lock);
2541 mutex_unlock(&matrix_dev->guests_lock);
2547 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2548 * domains that have been removed from the host's
2549 * AP configuration from a guest.
2551 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2552 * @aprem: the adapters that have been removed from the host's AP configuration
2553 * @aqrem: the domains that have been removed from the host's AP configuration
2554 * @cdrem: the control domains that have been removed from the host's AP
2557 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2558 unsigned long *aprem,
2559 unsigned long *aqrem,
2560 unsigned long *cdrem)
2564 if (!bitmap_empty(aprem, AP_DEVICES)) {
2565 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2566 matrix_mdev->shadow_apcb.apm,
2570 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2571 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2572 matrix_mdev->shadow_apcb.aqm,
2576 if (!bitmap_empty(cdrem, AP_DOMAINS))
2577 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2578 matrix_mdev->shadow_apcb.adm,
2582 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2586 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2587 * domains and control domains that have been removed
2588 * from the host AP configuration and unplugs them
2589 * from those guests.
2591 * @ap_remove: bitmap specifying which adapters have been removed from the host
2593 * @aq_remove: bitmap specifying which domains have been removed from the host
2595 * @cd_remove: bitmap specifying which control domains have been removed from
2598 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2599 unsigned long *aq_remove,
2600 unsigned long *cd_remove)
2602 struct ap_matrix_mdev *matrix_mdev;
2603 DECLARE_BITMAP(aprem, AP_DEVICES);
2604 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2605 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2608 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2609 mutex_lock(&matrix_mdev->kvm->lock);
2610 mutex_lock(&matrix_dev->mdevs_lock);
2612 do_remove |= bitmap_and(aprem, ap_remove,
2613 matrix_mdev->matrix.apm,
2615 do_remove |= bitmap_and(aqrem, aq_remove,
2616 matrix_mdev->matrix.aqm,
2618 do_remove |= bitmap_andnot(cdrem, cd_remove,
2619 matrix_mdev->matrix.adm,
2623 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2626 mutex_unlock(&matrix_dev->mdevs_lock);
2627 mutex_unlock(&matrix_mdev->kvm->lock);
2632 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2633 * control domains from the host AP configuration
2634 * by unplugging them from the guests that are
2636 * @cur_config_info: the current host AP configuration information
2637 * @prev_config_info: the previous host AP configuration information
2639 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2640 struct ap_config_info *prev_config_info)
2643 DECLARE_BITMAP(aprem, AP_DEVICES);
2644 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2645 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2647 do_remove = bitmap_andnot(aprem,
2648 (unsigned long *)prev_config_info->apm,
2649 (unsigned long *)cur_config_info->apm,
2651 do_remove |= bitmap_andnot(aqrem,
2652 (unsigned long *)prev_config_info->aqm,
2653 (unsigned long *)cur_config_info->aqm,
2655 do_remove |= bitmap_andnot(cdrem,
2656 (unsigned long *)prev_config_info->adm,
2657 (unsigned long *)cur_config_info->adm,
2661 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2665 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2666 * are older than AP type 10 (CEX4).
2667 * @apm: a bitmap of the APIDs to examine
2668 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2670 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2673 struct ap_queue_status status;
2674 unsigned long apid, apqi;
2675 struct ap_tapq_hwinfo info;
2677 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2678 apid_cleared = false;
2680 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2681 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2682 switch (status.response_code) {
2684 * According to the architecture in each case
2685 * below, the queue's info should be filled.
2687 case AP_RESPONSE_NORMAL:
2688 case AP_RESPONSE_RESET_IN_PROGRESS:
2689 case AP_RESPONSE_DECONFIGURED:
2690 case AP_RESPONSE_CHECKSTOPPED:
2691 case AP_RESPONSE_BUSY:
2693 * The vfio_ap device driver only
2694 * supports CEX4 and newer adapters, so
2695 * remove the APID if the adapter is
2696 * older than a CEX4.
2698 if (info.at < AP_DEVICE_TYPE_CEX4) {
2699 clear_bit_inv(apid, apm);
2700 apid_cleared = true;
2707 * If we don't know the adapter type,
2708 * clear its APID since it can't be
2709 * determined whether the vfio_ap
2710 * device driver supports it.
2712 clear_bit_inv(apid, apm);
2713 apid_cleared = true;
2718 * If we've already cleared the APID from the apm, there
2719 * is no need to continue examining the remainin AP
2720 * queues to determine the type of the adapter.
2729 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2730 * control domains that have been added to the host's
2731 * AP configuration for each matrix mdev to which they
2734 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2736 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2738 * @adm_add: a bitmap specifying the control domains that have been added to the
2741 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2742 unsigned long *adm_add)
2744 struct ap_matrix_mdev *matrix_mdev;
2746 if (list_empty(&matrix_dev->mdev_list))
2749 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2751 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2752 bitmap_and(matrix_mdev->apm_add,
2753 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2754 bitmap_and(matrix_mdev->aqm_add,
2755 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2756 bitmap_and(matrix_mdev->adm_add,
2757 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2762 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2763 * control domains to the host AP configuration
2764 * by updating the bitmaps that specify what adapters,
2765 * domains and control domains have been added so they
2766 * can be hot plugged into the guest when the AP bus
2767 * scan completes (see vfio_ap_on_scan_complete
2769 * @cur_config_info: the current AP configuration information
2770 * @prev_config_info: the previous AP configuration information
2772 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2773 struct ap_config_info *prev_config_info)
2776 DECLARE_BITMAP(apm_add, AP_DEVICES);
2777 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2778 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2780 do_add = bitmap_andnot(apm_add,
2781 (unsigned long *)cur_config_info->apm,
2782 (unsigned long *)prev_config_info->apm,
2784 do_add |= bitmap_andnot(aqm_add,
2785 (unsigned long *)cur_config_info->aqm,
2786 (unsigned long *)prev_config_info->aqm,
2788 do_add |= bitmap_andnot(adm_add,
2789 (unsigned long *)cur_config_info->adm,
2790 (unsigned long *)prev_config_info->adm,
2794 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2798 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2801 * @cur_cfg_info: the current host AP configuration
2802 * @prev_cfg_info: the previous host AP configuration
2804 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2805 struct ap_config_info *prev_cfg_info)
2807 if (!cur_cfg_info || !prev_cfg_info)
2810 mutex_lock(&matrix_dev->guests_lock);
2812 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2813 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2814 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2816 mutex_unlock(&matrix_dev->guests_lock);
2819 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2821 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2822 bool filter_domains, filter_adapters, filter_cdoms, do_hotplug = false;
2824 mutex_lock(&matrix_mdev->kvm->lock);
2825 mutex_lock(&matrix_dev->mdevs_lock);
2827 filter_adapters = bitmap_intersects(matrix_mdev->matrix.apm,
2828 matrix_mdev->apm_add, AP_DEVICES);
2829 filter_domains = bitmap_intersects(matrix_mdev->matrix.aqm,
2830 matrix_mdev->aqm_add, AP_DOMAINS);
2831 filter_cdoms = bitmap_intersects(matrix_mdev->matrix.adm,
2832 matrix_mdev->adm_add, AP_DOMAINS);
2834 if (filter_adapters || filter_domains)
2835 do_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
2838 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2841 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2843 reset_queues_for_apids(matrix_mdev, apm_filtered);
2845 mutex_unlock(&matrix_dev->mdevs_lock);
2846 mutex_unlock(&matrix_mdev->kvm->lock);
2849 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2850 struct ap_config_info *old_config_info)
2852 struct ap_matrix_mdev *matrix_mdev;
2854 mutex_lock(&matrix_dev->guests_lock);
2856 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2857 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2858 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2859 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2862 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2863 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2864 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2865 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2868 mutex_unlock(&matrix_dev->guests_lock);