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)))
363 static int ensure_nib_shared(unsigned long addr, struct gmap *gmap)
368 * The nib has to be located in shared storage since guest and
369 * host access it. vfio_pin_pages() will do a pin shared and
370 * if that fails (possibly because it's not a shared page) it
371 * calls export. We try to do a second pin shared here so that
372 * the UV gives us an error code if we try to pin a non-shared
375 * If the page is already pinned shared the UV will return a success.
377 ret = uv_pin_shared(addr);
379 /* vfio_pin_pages() likely exported the page so let's re-import */
380 gmap_convert_to_secure(gmap, addr);
386 * vfio_ap_irq_enable - Enable Interruption for a APQN
388 * @q: the vfio_ap_queue holding AQIC parameters
389 * @isc: the guest ISC to register with the GIB interface
390 * @vcpu: the vcpu object containing the registers specifying the parameters
391 * passed to the PQAP(AQIC) instruction.
393 * Pin the NIB saved in *q
394 * Register the guest ISC to GIB interface and retrieve the
395 * host ISC to issue the host side PQAP/AQIC
397 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
398 * vfio_pin_pages failed.
400 * Otherwise return the ap_queue_status returned by the ap_aqic(),
401 * all retry handling will be done by the guest.
403 * Return: &struct ap_queue_status
405 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
407 struct kvm_vcpu *vcpu)
409 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
410 struct ap_queue_status status = {};
411 struct kvm_s390_gisa *gisa;
419 /* Verify that the notification indicator byte address is valid */
420 if (vfio_ap_validate_nib(vcpu, &nib)) {
421 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
422 __func__, &nib, q->apqn);
424 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
428 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
429 IOMMU_READ | IOMMU_WRITE, &h_page);
434 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
435 "nib=%pad, apqn=%#04x\n",
436 __func__, ret, &nib, q->apqn);
438 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
442 kvm = q->matrix_mdev->kvm;
443 gisa = kvm->arch.gisa_int.origin;
445 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
446 aqic_gisa.gisc = isc;
448 /* NIB in non-shared storage is a rc 6 for PV guests */
449 if (kvm_s390_pv_cpu_is_protected(vcpu) &&
450 ensure_nib_shared(h_nib & PAGE_MASK, kvm->arch.gmap)) {
451 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
452 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
456 nisc = kvm_s390_gisc_register(kvm, isc);
458 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
459 __func__, nisc, isc, q->apqn);
461 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
462 status.response_code = AP_RESPONSE_INVALID_GISA;
466 aqic_gisa.isc = nisc;
468 aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
470 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
471 switch (status.response_code) {
472 case AP_RESPONSE_NORMAL:
473 /* See if we did clear older IRQ configuration */
474 vfio_ap_free_aqic_resources(q);
478 case AP_RESPONSE_OTHERWISE_CHANGED:
479 /* We could not modify IRQ settings: clear new configuration */
480 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
481 kvm_s390_gisc_unregister(kvm, isc);
484 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
485 status.response_code);
486 vfio_ap_irq_disable(q);
490 if (status.response_code != AP_RESPONSE_NORMAL) {
491 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
492 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
493 "gisa=%#x, isc=%#x, apqn=%#04x\n",
494 __func__, status.response_code,
495 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
496 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
504 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
505 * of big endian elements that can be passed by
506 * value to an s390dbf sprintf event function to
507 * format a UUID string.
509 * @guid: the object containing the little endian guid
510 * @uuid: a six-element array of long values that can be passed by value as
511 * arguments for a formatting string specifying a UUID.
513 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
514 * event functions if the memory for the passed string is available as long as
515 * the debug feature exists. Since a mediated device can be removed at any
516 * time, it's name can not be used because %s passes the reference to the string
517 * in memory and the reference will go stale once the device is removed .
519 * The s390dbf string formatting function allows a maximum of 9 arguments for a
520 * message to be displayed in the 'sprintf' view. In order to use the bytes
521 * comprising the mediated device's UUID to display the mediated device name,
522 * they will have to be converted into an array whose elements can be passed by
523 * value to sprintf. For example:
525 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
526 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
527 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
528 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
530 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
533 * The input guid is ordered in little endian, so it needs to be
534 * reordered for displaying a UUID as a string. This specifies the
535 * guid indices in proper order.
537 uuid[0] = le32_to_cpup((__le32 *)guid);
538 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
539 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
540 uuid[3] = *((__u16 *)&guid->b[8]);
541 uuid[4] = *((__u16 *)&guid->b[10]);
542 uuid[5] = *((__u32 *)&guid->b[12]);
546 * handle_pqap - PQAP instruction callback
548 * @vcpu: The vcpu on which we received the PQAP instruction
550 * Get the general register contents to initialize internal variables.
555 * Response.status may be set to following Response Code:
556 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
557 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
558 * - AP_RESPONSE_NORMAL (0) : in case of success
559 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
560 * We take the matrix_dev lock to ensure serialization on queues and
561 * mediated device access.
563 * Return: 0 if we could handle the request inside KVM.
564 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
566 static int handle_pqap(struct kvm_vcpu *vcpu)
570 unsigned long uuid[6];
571 struct vfio_ap_queue *q;
572 struct ap_queue_status qstatus = {
573 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
574 struct ap_matrix_mdev *matrix_mdev;
576 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
578 /* If we do not use the AIV facility just go to userland */
579 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
580 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
581 __func__, apqn, vcpu->arch.sie_block->eca);
586 mutex_lock(&matrix_dev->mdevs_lock);
588 if (!vcpu->kvm->arch.crypto.pqap_hook) {
589 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
595 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
596 struct ap_matrix_mdev, pqap_hook);
598 /* If the there is no guest using the mdev, there is nothing to do */
599 if (!matrix_mdev->kvm) {
600 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
601 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
602 __func__, uuid[0], uuid[1], uuid[2],
603 uuid[3], uuid[4], uuid[5], apqn);
607 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
609 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
610 __func__, AP_QID_CARD(apqn),
615 status = vcpu->run->s.regs.gprs[1];
617 /* If IR bit(16) is set we enable the interrupt */
618 if ((status >> (63 - 16)) & 0x01)
619 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
621 qstatus = vfio_ap_irq_disable(q);
624 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
625 vcpu->run->s.regs.gprs[1] >>= 32;
626 mutex_unlock(&matrix_dev->mdevs_lock);
630 static void vfio_ap_matrix_init(struct ap_config_info *info,
631 struct ap_matrix *matrix)
633 matrix->apm_max = info->apxa ? info->na : 63;
634 matrix->aqm_max = info->apxa ? info->nd : 15;
635 matrix->adm_max = info->apxa ? info->nd : 15;
638 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
640 if (matrix_mdev->kvm)
641 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
642 matrix_mdev->shadow_apcb.apm,
643 matrix_mdev->shadow_apcb.aqm,
644 matrix_mdev->shadow_apcb.adm);
647 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
649 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
651 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
652 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
653 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
655 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
660 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
661 * to ensure no queue devices are passed through to
662 * the guest that are not bound to the vfio_ap
665 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
666 * @apm_filtered: a 256-bit bitmap for storing the APIDs filtered from the
667 * guest's AP configuration that are still in the host's AP
670 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
671 * driver, its APID will be filtered from the guest's APCB. The matrix
672 * structure precludes filtering an individual APQN, so its APID will be
673 * filtered. Consequently, all queues associated with the adapter that
674 * are in the host's AP configuration must be reset. If queues are
675 * subsequently made available again to the guest, they should re-appear
678 * Return: a boolean value indicating whether the KVM guest's APCB was changed
679 * by the filtering or not.
681 static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
682 unsigned long *apm_filtered)
684 unsigned long apid, apqi, apqn;
685 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
686 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
687 struct vfio_ap_queue *q;
689 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
690 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
691 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
692 bitmap_clear(apm_filtered, 0, AP_DEVICES);
695 * Copy the adapters, domains and control domains to the shadow_apcb
696 * from the matrix mdev, but only those that are assigned to the host's
699 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
700 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
701 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
702 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
704 for_each_set_bit_inv(apid, matrix_mdev->shadow_apcb.apm, AP_DEVICES) {
705 for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
708 * If the APQN is not bound to the vfio_ap device
709 * driver, then we can't assign it to the guest's
710 * AP configuration. The AP architecture won't
711 * allow filtering of a single APQN, so let's filter
712 * the APID since an adapter represents a physical
715 apqn = AP_MKQID(apid, apqi);
716 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
717 if (!q || q->reset_status.response_code) {
718 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
721 * If the adapter was previously plugged into
722 * the guest, let's let the caller know that
723 * the APID was filtered.
725 if (test_bit_inv(apid, prev_shadow_apm))
726 set_bit_inv(apid, apm_filtered);
733 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
735 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
739 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
741 struct ap_matrix_mdev *matrix_mdev =
742 container_of(vdev, struct ap_matrix_mdev, vdev);
744 matrix_mdev->mdev = to_mdev_device(vdev->dev);
745 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
746 matrix_mdev->pqap_hook = handle_pqap;
747 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
748 hash_init(matrix_mdev->qtable.queues);
753 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
755 struct ap_matrix_mdev *matrix_mdev;
758 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
759 &vfio_ap_matrix_dev_ops);
760 if (IS_ERR(matrix_mdev))
761 return PTR_ERR(matrix_mdev);
763 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
766 matrix_mdev->req_trigger = NULL;
767 dev_set_drvdata(&mdev->dev, matrix_mdev);
768 mutex_lock(&matrix_dev->mdevs_lock);
769 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
770 mutex_unlock(&matrix_dev->mdevs_lock);
774 vfio_put_device(&matrix_mdev->vdev);
778 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
779 struct vfio_ap_queue *q)
782 q->matrix_mdev = matrix_mdev;
783 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
787 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
789 struct vfio_ap_queue *q;
791 q = vfio_ap_find_queue(apqn);
792 vfio_ap_mdev_link_queue(matrix_mdev, q);
795 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
797 hash_del(&q->mdev_qnode);
800 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
802 q->matrix_mdev = NULL;
805 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
807 struct vfio_ap_queue *q;
808 unsigned long apid, apqi;
810 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
811 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
813 q = vfio_ap_mdev_get_queue(matrix_mdev,
814 AP_MKQID(apid, apqi));
816 q->matrix_mdev = NULL;
821 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
823 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
825 vfio_unregister_group_dev(&matrix_mdev->vdev);
827 mutex_lock(&matrix_dev->guests_lock);
828 mutex_lock(&matrix_dev->mdevs_lock);
829 vfio_ap_mdev_reset_queues(matrix_mdev);
830 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
831 list_del(&matrix_mdev->node);
832 mutex_unlock(&matrix_dev->mdevs_lock);
833 mutex_unlock(&matrix_dev->guests_lock);
834 vfio_put_device(&matrix_mdev->vdev);
837 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
838 "already assigned to %s"
840 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
844 unsigned long apid, apqi;
845 const struct device *dev = mdev_dev(matrix_mdev->mdev);
846 const char *mdev_name = dev_name(dev);
848 for_each_set_bit_inv(apid, apm, AP_DEVICES)
849 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
850 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
854 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
856 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
857 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
859 * Verifies that each APQN derived from the Cartesian product of a bitmap of
860 * AP adapter IDs and AP queue indexes is not configured for any matrix
861 * mediated device. AP queue sharing is not allowed.
863 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
865 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
866 unsigned long *mdev_aqm)
868 struct ap_matrix_mdev *matrix_mdev;
869 DECLARE_BITMAP(apm, AP_DEVICES);
870 DECLARE_BITMAP(aqm, AP_DOMAINS);
872 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
874 * If the input apm and aqm are fields of the matrix_mdev
875 * object, then move on to the next matrix_mdev.
877 if (mdev_apm == matrix_mdev->matrix.apm &&
878 mdev_aqm == matrix_mdev->matrix.aqm)
881 memset(apm, 0, sizeof(apm));
882 memset(aqm, 0, sizeof(aqm));
885 * We work on full longs, as we can only exclude the leftover
886 * bits in non-inverse order. The leftover is all zeros.
888 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
892 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
896 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
905 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
906 * not reserved for the default zcrypt driver and
907 * are not assigned to another mdev.
909 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
911 * Return: One of the following values:
912 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
913 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
914 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
915 * zcrypt default driver.
916 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
917 * o A zero indicating validation succeeded.
919 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
921 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
922 matrix_mdev->matrix.aqm))
923 return -EADDRNOTAVAIL;
925 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
926 matrix_mdev->matrix.aqm);
929 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
934 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
935 vfio_ap_mdev_link_apqn(matrix_mdev,
936 AP_MKQID(apid, apqi));
939 static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
940 unsigned long *apm_reset)
942 struct vfio_ap_queue *q, *tmpq;
943 struct list_head qlist;
944 unsigned long apid, apqi;
947 if (bitmap_empty(apm_reset, AP_DEVICES))
950 INIT_LIST_HEAD(&qlist);
952 for_each_set_bit_inv(apid, apm_reset, AP_DEVICES) {
953 for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
956 * If the domain is not in the host's AP configuration,
957 * then resetting it will fail with response code 01
960 if (!test_bit_inv(apqi,
961 (unsigned long *)matrix_dev->info.aqm))
964 apqn = AP_MKQID(apid, apqi);
965 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
968 list_add_tail(&q->reset_qnode, &qlist);
972 ret = vfio_ap_mdev_reset_qlist(&qlist);
974 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode)
975 list_del(&q->reset_qnode);
981 * assign_adapter_store - parses the APID from @buf and sets the
982 * corresponding bit in the mediated matrix device's APM
984 * @dev: the matrix device
985 * @attr: the mediated matrix device's assign_adapter attribute
986 * @buf: a buffer containing the AP adapter number (APID) to
988 * @count: the number of bytes in @buf
990 * Return: the number of bytes processed if the APID is valid; otherwise,
991 * returns one of the following errors:
994 * The APID is not a valid number
997 * The APID exceeds the maximum value configured for the system
1000 * An APQN derived from the cross product of the APID being assigned
1001 * and the APQIs previously assigned is not bound to the vfio_ap device
1002 * driver; or, if no APQIs have yet been assigned, the APID is not
1003 * contained in an APQN bound to the vfio_ap device driver.
1006 * An APQN derived from the cross product of the APID being assigned
1007 * and the APQIs previously assigned is being used by another mediated
1011 * A lock required to validate the mdev's AP configuration could not
1014 static ssize_t assign_adapter_store(struct device *dev,
1015 struct device_attribute *attr,
1016 const char *buf, size_t count)
1020 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1021 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1023 mutex_lock(&ap_perms_mutex);
1024 get_update_locks_for_mdev(matrix_mdev);
1026 ret = kstrtoul(buf, 0, &apid);
1030 if (apid > matrix_mdev->matrix.apm_max) {
1035 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1040 set_bit_inv(apid, matrix_mdev->matrix.apm);
1042 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1044 clear_bit_inv(apid, matrix_mdev->matrix.apm);
1048 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
1050 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1051 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1052 reset_queues_for_apids(matrix_mdev, apm_filtered);
1057 release_update_locks_for_mdev(matrix_mdev);
1058 mutex_unlock(&ap_perms_mutex);
1062 static DEVICE_ATTR_WO(assign_adapter);
1064 static struct vfio_ap_queue
1065 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
1066 unsigned long apid, unsigned long apqi)
1068 struct vfio_ap_queue *q = NULL;
1070 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1071 /* If the queue is assigned to the matrix mdev, unlink it. */
1073 vfio_ap_unlink_queue_fr_mdev(q);
1079 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
1080 * adapter from the matrix mdev to which the
1081 * adapter was assigned.
1082 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
1083 * @apid: the APID of the unassigned adapter.
1084 * @qlist: list for storing queues associated with unassigned adapter that
1087 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1089 struct list_head *qlist)
1092 struct vfio_ap_queue *q;
1094 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1095 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1098 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1099 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1100 list_add_tail(&q->reset_qnode, qlist);
1105 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1108 struct vfio_ap_queue *q, *tmpq;
1109 struct list_head qlist;
1111 INIT_LIST_HEAD(&qlist);
1112 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
1114 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1115 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1116 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1119 vfio_ap_mdev_reset_qlist(&qlist);
1121 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1122 vfio_ap_unlink_mdev_fr_queue(q);
1123 list_del(&q->reset_qnode);
1128 * unassign_adapter_store - parses the APID from @buf and clears the
1129 * corresponding bit in the mediated matrix device's APM
1131 * @dev: the matrix device
1132 * @attr: the mediated matrix device's unassign_adapter attribute
1133 * @buf: a buffer containing the adapter number (APID) to be unassigned
1134 * @count: the number of bytes in @buf
1136 * Return: the number of bytes processed if the APID is valid; otherwise,
1137 * returns one of the following errors:
1138 * -EINVAL if the APID is not a number
1139 * -ENODEV if the APID it exceeds the maximum value configured for the
1142 static ssize_t unassign_adapter_store(struct device *dev,
1143 struct device_attribute *attr,
1144 const char *buf, size_t count)
1148 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1150 get_update_locks_for_mdev(matrix_mdev);
1152 ret = kstrtoul(buf, 0, &apid);
1156 if (apid > matrix_mdev->matrix.apm_max) {
1161 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1166 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1167 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1170 release_update_locks_for_mdev(matrix_mdev);
1173 static DEVICE_ATTR_WO(unassign_adapter);
1175 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1180 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1181 vfio_ap_mdev_link_apqn(matrix_mdev,
1182 AP_MKQID(apid, apqi));
1186 * assign_domain_store - parses the APQI from @buf and sets the
1187 * corresponding bit in the mediated matrix device's AQM
1189 * @dev: the matrix device
1190 * @attr: the mediated matrix device's assign_domain attribute
1191 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1193 * @count: the number of bytes in @buf
1195 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1196 * one of the following errors:
1199 * The APQI is not a valid number
1202 * The APQI exceeds the maximum value configured for the system
1205 * An APQN derived from the cross product of the APQI being assigned
1206 * and the APIDs previously assigned is not bound to the vfio_ap device
1207 * driver; or, if no APIDs have yet been assigned, the APQI is not
1208 * contained in an APQN bound to the vfio_ap device driver.
1211 * An APQN derived from the cross product of the APQI being assigned
1212 * and the APIDs previously assigned is being used by another mediated
1216 * The lock required to validate the mdev's AP configuration could not
1219 static ssize_t assign_domain_store(struct device *dev,
1220 struct device_attribute *attr,
1221 const char *buf, size_t count)
1225 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1226 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1228 mutex_lock(&ap_perms_mutex);
1229 get_update_locks_for_mdev(matrix_mdev);
1231 ret = kstrtoul(buf, 0, &apqi);
1235 if (apqi > matrix_mdev->matrix.aqm_max) {
1240 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1245 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1247 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1249 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1253 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1255 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1256 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1257 reset_queues_for_apids(matrix_mdev, apm_filtered);
1262 release_update_locks_for_mdev(matrix_mdev);
1263 mutex_unlock(&ap_perms_mutex);
1267 static DEVICE_ATTR_WO(assign_domain);
1269 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1271 struct list_head *qlist)
1274 struct vfio_ap_queue *q;
1276 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1277 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1280 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1281 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1282 list_add_tail(&q->reset_qnode, qlist);
1287 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1290 struct vfio_ap_queue *q, *tmpq;
1291 struct list_head qlist;
1293 INIT_LIST_HEAD(&qlist);
1294 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
1296 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1297 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1298 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1301 vfio_ap_mdev_reset_qlist(&qlist);
1303 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1304 vfio_ap_unlink_mdev_fr_queue(q);
1305 list_del(&q->reset_qnode);
1310 * unassign_domain_store - parses the APQI from @buf and clears the
1311 * corresponding bit in the mediated matrix device's AQM
1313 * @dev: the matrix device
1314 * @attr: the mediated matrix device's unassign_domain attribute
1315 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1317 * @count: the number of bytes in @buf
1319 * Return: the number of bytes processed if the APQI is valid; otherwise,
1320 * returns one of the following errors:
1321 * -EINVAL if the APQI is not a number
1322 * -ENODEV if the APQI exceeds the maximum value configured for the system
1324 static ssize_t unassign_domain_store(struct device *dev,
1325 struct device_attribute *attr,
1326 const char *buf, size_t count)
1330 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1332 get_update_locks_for_mdev(matrix_mdev);
1334 ret = kstrtoul(buf, 0, &apqi);
1338 if (apqi > matrix_mdev->matrix.aqm_max) {
1343 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1348 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1349 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1353 release_update_locks_for_mdev(matrix_mdev);
1356 static DEVICE_ATTR_WO(unassign_domain);
1359 * assign_control_domain_store - parses the domain ID from @buf and sets
1360 * the corresponding bit in the mediated matrix device's ADM
1362 * @dev: the matrix device
1363 * @attr: the mediated matrix device's assign_control_domain attribute
1364 * @buf: a buffer containing the domain ID to be assigned
1365 * @count: the number of bytes in @buf
1367 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1368 * returns one of the following errors:
1369 * -EINVAL if the ID is not a number
1370 * -ENODEV if the ID exceeds the maximum value configured for the system
1372 static ssize_t assign_control_domain_store(struct device *dev,
1373 struct device_attribute *attr,
1374 const char *buf, size_t count)
1378 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1380 get_update_locks_for_mdev(matrix_mdev);
1382 ret = kstrtoul(buf, 0, &id);
1386 if (id > matrix_mdev->matrix.adm_max) {
1391 if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1396 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1397 * domain number (id). The bits in the mask, from most significant to
1398 * least significant, correspond to IDs 0 up to the one less than the
1399 * number of control domains that can be assigned.
1401 set_bit_inv(id, matrix_mdev->matrix.adm);
1402 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1403 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1407 release_update_locks_for_mdev(matrix_mdev);
1410 static DEVICE_ATTR_WO(assign_control_domain);
1413 * unassign_control_domain_store - parses the domain ID from @buf and
1414 * clears the corresponding bit in the mediated matrix device's ADM
1416 * @dev: the matrix device
1417 * @attr: the mediated matrix device's unassign_control_domain attribute
1418 * @buf: a buffer containing the domain ID to be unassigned
1419 * @count: the number of bytes in @buf
1421 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1422 * returns one of the following errors:
1423 * -EINVAL if the ID is not a number
1424 * -ENODEV if the ID exceeds the maximum value configured for the system
1426 static ssize_t unassign_control_domain_store(struct device *dev,
1427 struct device_attribute *attr,
1428 const char *buf, size_t count)
1431 unsigned long domid;
1432 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1434 get_update_locks_for_mdev(matrix_mdev);
1436 ret = kstrtoul(buf, 0, &domid);
1440 if (domid > matrix_mdev->matrix.adm_max) {
1445 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1450 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1452 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1453 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1454 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1459 release_update_locks_for_mdev(matrix_mdev);
1462 static DEVICE_ATTR_WO(unassign_control_domain);
1464 static ssize_t control_domains_show(struct device *dev,
1465 struct device_attribute *dev_attr,
1472 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1473 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1475 mutex_lock(&matrix_dev->mdevs_lock);
1476 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1477 n = sprintf(bufpos, "%04lx\n", id);
1481 mutex_unlock(&matrix_dev->mdevs_lock);
1485 static DEVICE_ATTR_RO(control_domains);
1487 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1492 unsigned long apid1;
1493 unsigned long apqi1;
1494 unsigned long napm_bits = matrix->apm_max + 1;
1495 unsigned long naqm_bits = matrix->aqm_max + 1;
1499 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1500 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1502 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1503 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1504 for_each_set_bit_inv(apqi, matrix->aqm,
1506 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1512 } else if (apid1 < napm_bits) {
1513 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1514 n = sprintf(bufpos, "%02lx.\n", apid);
1518 } else if (apqi1 < naqm_bits) {
1519 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1520 n = sprintf(bufpos, ".%04lx\n", apqi);
1529 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1533 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1535 mutex_lock(&matrix_dev->mdevs_lock);
1536 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1537 mutex_unlock(&matrix_dev->mdevs_lock);
1541 static DEVICE_ATTR_RO(matrix);
1543 static ssize_t guest_matrix_show(struct device *dev,
1544 struct device_attribute *attr, char *buf)
1547 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1549 mutex_lock(&matrix_dev->mdevs_lock);
1550 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1551 mutex_unlock(&matrix_dev->mdevs_lock);
1555 static DEVICE_ATTR_RO(guest_matrix);
1557 static struct attribute *vfio_ap_mdev_attrs[] = {
1558 &dev_attr_assign_adapter.attr,
1559 &dev_attr_unassign_adapter.attr,
1560 &dev_attr_assign_domain.attr,
1561 &dev_attr_unassign_domain.attr,
1562 &dev_attr_assign_control_domain.attr,
1563 &dev_attr_unassign_control_domain.attr,
1564 &dev_attr_control_domains.attr,
1565 &dev_attr_matrix.attr,
1566 &dev_attr_guest_matrix.attr,
1570 static struct attribute_group vfio_ap_mdev_attr_group = {
1571 .attrs = vfio_ap_mdev_attrs
1574 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1575 &vfio_ap_mdev_attr_group,
1580 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1581 * to manage AP resources for the guest whose state is represented by @kvm
1583 * @matrix_mdev: a mediated matrix device
1584 * @kvm: reference to KVM instance
1586 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1587 * otherwise, returns an -EPERM.
1589 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1592 struct ap_matrix_mdev *m;
1594 if (kvm->arch.crypto.crycbd) {
1595 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1596 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1597 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1599 get_update_locks_for_kvm(kvm);
1601 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1602 if (m != matrix_mdev && m->kvm == kvm) {
1603 release_update_locks_for_kvm(kvm);
1609 matrix_mdev->kvm = kvm;
1610 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1612 release_update_locks_for_kvm(kvm);
1618 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1620 struct ap_queue_table *qtable = &matrix_mdev->qtable;
1621 struct vfio_ap_queue *q;
1624 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1625 if (q->saved_iova >= iova && q->saved_iova < iova + length)
1626 vfio_ap_irq_disable(q);
1630 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1633 struct ap_matrix_mdev *matrix_mdev =
1634 container_of(vdev, struct ap_matrix_mdev, vdev);
1636 mutex_lock(&matrix_dev->mdevs_lock);
1638 unmap_iova(matrix_mdev, iova, length);
1640 mutex_unlock(&matrix_dev->mdevs_lock);
1644 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1647 * @matrix_mdev: a matrix mediated device
1649 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1651 struct kvm *kvm = matrix_mdev->kvm;
1653 if (kvm && kvm->arch.crypto.crycbd) {
1654 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1655 kvm->arch.crypto.pqap_hook = NULL;
1656 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1658 get_update_locks_for_kvm(kvm);
1660 kvm_arch_crypto_clear_masks(kvm);
1661 vfio_ap_mdev_reset_queues(matrix_mdev);
1663 matrix_mdev->kvm = NULL;
1665 release_update_locks_for_kvm(kvm);
1669 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1671 struct ap_queue *queue;
1672 struct vfio_ap_queue *q = NULL;
1674 queue = ap_get_qdev(apqn);
1678 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1679 q = dev_get_drvdata(&queue->ap_dev.device);
1681 put_device(&queue->ap_dev.device);
1686 static int apq_status_check(int apqn, struct ap_queue_status *status)
1688 switch (status->response_code) {
1689 case AP_RESPONSE_NORMAL:
1690 case AP_RESPONSE_DECONFIGURED:
1692 case AP_RESPONSE_RESET_IN_PROGRESS:
1693 case AP_RESPONSE_BUSY:
1695 case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
1696 case AP_RESPONSE_ASSOC_FAILED:
1698 * These asynchronous response codes indicate a PQAP(AAPQ)
1699 * instruction to associate a secret with the guest failed. All
1700 * subsequent AP instructions will end with the asynchronous
1701 * response code until the AP queue is reset; so, let's return
1702 * a value indicating a reset needs to be performed again.
1707 "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1708 AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1709 status->response_code);
1714 #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
1716 static void apq_reset_check(struct work_struct *reset_work)
1718 int ret = -EBUSY, elapsed = 0;
1719 struct ap_queue_status status;
1720 struct vfio_ap_queue *q;
1722 q = container_of(reset_work, struct vfio_ap_queue, reset_work);
1723 memcpy(&status, &q->reset_status, sizeof(status));
1725 msleep(AP_RESET_INTERVAL);
1726 elapsed += AP_RESET_INTERVAL;
1727 status = ap_tapq(q->apqn, NULL);
1728 ret = apq_status_check(q->apqn, &status);
1731 if (ret == -EBUSY) {
1732 pr_notice_ratelimited(WAIT_MSG, elapsed,
1733 AP_QID_CARD(q->apqn),
1734 AP_QID_QUEUE(q->apqn),
1735 status.response_code,
1737 status.irq_enabled);
1739 if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
1740 q->reset_status.response_code == AP_RESPONSE_BUSY ||
1741 q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
1743 status = ap_zapq(q->apqn, 0);
1744 memcpy(&q->reset_status, &status, sizeof(status));
1748 * When an AP adapter is deconfigured, the
1749 * associated queues are reset, so let's set the
1750 * status response code to 0 so the queue may be
1751 * passed through (i.e., not filtered)
1753 if (status.response_code == AP_RESPONSE_DECONFIGURED)
1754 q->reset_status.response_code = 0;
1755 if (q->saved_isc != VFIO_AP_ISC_INVALID)
1756 vfio_ap_free_aqic_resources(q);
1762 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1764 struct ap_queue_status status;
1768 status = ap_zapq(q->apqn, 0);
1769 memcpy(&q->reset_status, &status, sizeof(status));
1770 switch (status.response_code) {
1771 case AP_RESPONSE_NORMAL:
1772 case AP_RESPONSE_RESET_IN_PROGRESS:
1773 case AP_RESPONSE_BUSY:
1774 case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
1776 * Let's verify whether the ZAPQ completed successfully on a work queue.
1778 queue_work(system_long_wq, &q->reset_work);
1780 case AP_RESPONSE_DECONFIGURED:
1782 * When an AP adapter is deconfigured, the associated
1783 * queues are reset, so let's set the status response code to 0
1784 * so the queue may be passed through (i.e., not filtered).
1786 q->reset_status.response_code = 0;
1787 vfio_ap_free_aqic_resources(q);
1791 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1792 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1793 status.response_code);
1797 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1799 int ret = 0, loop_cursor;
1800 struct vfio_ap_queue *q;
1802 hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode)
1803 vfio_ap_mdev_reset_queue(q);
1805 hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode) {
1806 flush_work(&q->reset_work);
1808 if (q->reset_status.response_code)
1815 static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
1818 struct vfio_ap_queue *q;
1820 list_for_each_entry(q, qlist, reset_qnode)
1821 vfio_ap_mdev_reset_queue(q);
1823 list_for_each_entry(q, qlist, reset_qnode) {
1824 flush_work(&q->reset_work);
1826 if (q->reset_status.response_code)
1833 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1835 struct ap_matrix_mdev *matrix_mdev =
1836 container_of(vdev, struct ap_matrix_mdev, vdev);
1841 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
1844 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1846 struct ap_matrix_mdev *matrix_mdev =
1847 container_of(vdev, struct ap_matrix_mdev, vdev);
1849 vfio_ap_mdev_unset_kvm(matrix_mdev);
1852 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
1854 struct device *dev = vdev->dev;
1855 struct ap_matrix_mdev *matrix_mdev;
1857 matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
1859 if (matrix_mdev->req_trigger) {
1861 dev_notice_ratelimited(dev,
1862 "Relaying device request to user (#%u)\n",
1865 eventfd_signal(matrix_mdev->req_trigger, 1);
1866 } else if (count == 0) {
1868 "No device request registered, blocked until released by user\n");
1872 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1874 unsigned long minsz;
1875 struct vfio_device_info info;
1877 minsz = offsetofend(struct vfio_device_info, num_irqs);
1879 if (copy_from_user(&info, (void __user *)arg, minsz))
1882 if (info.argsz < minsz)
1885 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1886 info.num_regions = 0;
1887 info.num_irqs = VFIO_AP_NUM_IRQS;
1889 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1892 static ssize_t vfio_ap_get_irq_info(unsigned long arg)
1894 unsigned long minsz;
1895 struct vfio_irq_info info;
1897 minsz = offsetofend(struct vfio_irq_info, count);
1899 if (copy_from_user(&info, (void __user *)arg, minsz))
1902 if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
1905 switch (info.index) {
1906 case VFIO_AP_REQ_IRQ_INDEX:
1908 info.flags = VFIO_IRQ_INFO_EVENTFD;
1914 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1917 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
1921 unsigned long minsz;
1923 minsz = offsetofend(struct vfio_irq_set, count);
1925 if (copy_from_user(irq_set, (void __user *)arg, minsz))
1928 ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
1933 if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
1939 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
1944 unsigned long minsz;
1945 struct eventfd_ctx *req_trigger;
1947 minsz = offsetofend(struct vfio_irq_set, count);
1948 data = (void __user *)(arg + minsz);
1950 if (get_user(fd, (s32 __user *)data))
1954 if (matrix_mdev->req_trigger)
1955 eventfd_ctx_put(matrix_mdev->req_trigger);
1956 matrix_mdev->req_trigger = NULL;
1957 } else if (fd >= 0) {
1958 req_trigger = eventfd_ctx_fdget(fd);
1959 if (IS_ERR(req_trigger))
1960 return PTR_ERR(req_trigger);
1962 if (matrix_mdev->req_trigger)
1963 eventfd_ctx_put(matrix_mdev->req_trigger);
1965 matrix_mdev->req_trigger = req_trigger;
1973 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
1977 struct vfio_irq_set irq_set;
1979 ret = vfio_ap_irq_set_init(&irq_set, arg);
1983 switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
1984 case VFIO_IRQ_SET_DATA_EVENTFD:
1985 switch (irq_set.index) {
1986 case VFIO_AP_REQ_IRQ_INDEX:
1987 return vfio_ap_set_request_irq(matrix_mdev, arg);
1996 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1997 unsigned int cmd, unsigned long arg)
1999 struct ap_matrix_mdev *matrix_mdev =
2000 container_of(vdev, struct ap_matrix_mdev, vdev);
2003 mutex_lock(&matrix_dev->mdevs_lock);
2005 case VFIO_DEVICE_GET_INFO:
2006 ret = vfio_ap_mdev_get_device_info(arg);
2008 case VFIO_DEVICE_RESET:
2009 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
2011 case VFIO_DEVICE_GET_IRQ_INFO:
2012 ret = vfio_ap_get_irq_info(arg);
2014 case VFIO_DEVICE_SET_IRQS:
2015 ret = vfio_ap_set_irqs(matrix_mdev, arg);
2021 mutex_unlock(&matrix_dev->mdevs_lock);
2026 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
2028 struct ap_matrix_mdev *matrix_mdev;
2029 unsigned long apid = AP_QID_CARD(q->apqn);
2030 unsigned long apqi = AP_QID_QUEUE(q->apqn);
2032 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2033 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
2034 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
2041 static ssize_t status_show(struct device *dev,
2042 struct device_attribute *attr,
2046 struct vfio_ap_queue *q;
2047 struct ap_matrix_mdev *matrix_mdev;
2048 struct ap_device *apdev = to_ap_dev(dev);
2050 mutex_lock(&matrix_dev->mdevs_lock);
2051 q = dev_get_drvdata(&apdev->device);
2052 matrix_mdev = vfio_ap_mdev_for_queue(q);
2055 if (matrix_mdev->kvm)
2056 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
2059 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
2062 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
2063 AP_QUEUE_UNASSIGNED);
2066 mutex_unlock(&matrix_dev->mdevs_lock);
2071 static DEVICE_ATTR_RO(status);
2073 static struct attribute *vfio_queue_attrs[] = {
2074 &dev_attr_status.attr,
2078 static const struct attribute_group vfio_queue_attr_group = {
2079 .attrs = vfio_queue_attrs,
2082 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
2083 .init = vfio_ap_mdev_init_dev,
2084 .open_device = vfio_ap_mdev_open_device,
2085 .close_device = vfio_ap_mdev_close_device,
2086 .ioctl = vfio_ap_mdev_ioctl,
2087 .dma_unmap = vfio_ap_mdev_dma_unmap,
2088 .bind_iommufd = vfio_iommufd_emulated_bind,
2089 .unbind_iommufd = vfio_iommufd_emulated_unbind,
2090 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
2091 .detach_ioas = vfio_iommufd_emulated_detach_ioas,
2092 .request = vfio_ap_mdev_request
2095 static struct mdev_driver vfio_ap_matrix_driver = {
2096 .device_api = VFIO_DEVICE_API_AP_STRING,
2097 .max_instances = MAX_ZDEV_ENTRIES_EXT,
2099 .name = "vfio_ap_mdev",
2100 .owner = THIS_MODULE,
2101 .mod_name = KBUILD_MODNAME,
2102 .dev_groups = vfio_ap_mdev_attr_groups,
2104 .probe = vfio_ap_mdev_probe,
2105 .remove = vfio_ap_mdev_remove,
2108 int vfio_ap_mdev_register(void)
2112 ret = mdev_register_driver(&vfio_ap_matrix_driver);
2116 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
2117 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
2118 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
2119 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2120 &vfio_ap_matrix_driver,
2121 matrix_dev->mdev_types, 1);
2127 mdev_unregister_driver(&vfio_ap_matrix_driver);
2131 void vfio_ap_mdev_unregister(void)
2133 mdev_unregister_parent(&matrix_dev->parent);
2134 mdev_unregister_driver(&vfio_ap_matrix_driver);
2137 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2140 struct vfio_ap_queue *q;
2141 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2142 struct ap_matrix_mdev *matrix_mdev;
2144 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2148 q = kzalloc(sizeof(*q), GFP_KERNEL);
2151 goto err_remove_group;
2154 q->apqn = to_ap_queue(&apdev->device)->qid;
2155 q->saved_isc = VFIO_AP_ISC_INVALID;
2156 memset(&q->reset_status, 0, sizeof(q->reset_status));
2157 INIT_WORK(&q->reset_work, apq_reset_check);
2158 matrix_mdev = get_update_locks_by_apqn(q->apqn);
2161 vfio_ap_mdev_link_queue(matrix_mdev, q);
2164 * If we're in the process of handling the adding of adapters or
2165 * domains to the host's AP configuration, then let the
2166 * vfio_ap device driver's on_scan_complete callback filter the
2167 * matrix and update the guest's AP configuration after all of
2168 * the new queue devices are probed.
2170 if (!bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) ||
2171 !bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS))
2174 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
2175 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2176 reset_queues_for_apids(matrix_mdev, apm_filtered);
2181 dev_set_drvdata(&apdev->device, q);
2182 release_update_locks_for_mdev(matrix_mdev);
2187 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2191 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2193 unsigned long apid, apqi;
2194 struct vfio_ap_queue *q;
2195 struct ap_matrix_mdev *matrix_mdev;
2197 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2198 q = dev_get_drvdata(&apdev->device);
2199 get_update_locks_for_queue(q);
2200 matrix_mdev = q->matrix_mdev;
2203 vfio_ap_unlink_queue_fr_mdev(q);
2205 apid = AP_QID_CARD(q->apqn);
2206 apqi = AP_QID_QUEUE(q->apqn);
2209 * If the queue is assigned to the guest's APCB, then remove
2210 * the adapter's APID from the APCB and hot it into the guest.
2212 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2213 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2214 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2215 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2219 vfio_ap_mdev_reset_queue(q);
2220 flush_work(&q->reset_work);
2221 dev_set_drvdata(&apdev->device, NULL);
2223 release_update_locks_for_mdev(matrix_mdev);
2227 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2228 * assigned to a mediated device under the control
2229 * of the vfio_ap device driver.
2231 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2232 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2235 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2236 * assigned to a mediated device under the control of the vfio_ap
2238 * * Otherwise, return 0.
2240 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2244 mutex_lock(&matrix_dev->guests_lock);
2245 mutex_lock(&matrix_dev->mdevs_lock);
2246 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2247 mutex_unlock(&matrix_dev->mdevs_lock);
2248 mutex_unlock(&matrix_dev->guests_lock);
2254 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2255 * domains that have been removed from the host's
2256 * AP configuration from a guest.
2258 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2259 * @aprem: the adapters that have been removed from the host's AP configuration
2260 * @aqrem: the domains that have been removed from the host's AP configuration
2261 * @cdrem: the control domains that have been removed from the host's AP
2264 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2265 unsigned long *aprem,
2266 unsigned long *aqrem,
2267 unsigned long *cdrem)
2271 if (!bitmap_empty(aprem, AP_DEVICES)) {
2272 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2273 matrix_mdev->shadow_apcb.apm,
2277 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2278 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2279 matrix_mdev->shadow_apcb.aqm,
2283 if (!bitmap_empty(cdrem, AP_DOMAINS))
2284 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2285 matrix_mdev->shadow_apcb.adm,
2289 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2293 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2294 * domains and control domains that have been removed
2295 * from the host AP configuration and unplugs them
2296 * from those guests.
2298 * @ap_remove: bitmap specifying which adapters have been removed from the host
2300 * @aq_remove: bitmap specifying which domains have been removed from the host
2302 * @cd_remove: bitmap specifying which control domains have been removed from
2305 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2306 unsigned long *aq_remove,
2307 unsigned long *cd_remove)
2309 struct ap_matrix_mdev *matrix_mdev;
2310 DECLARE_BITMAP(aprem, AP_DEVICES);
2311 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2312 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2315 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2316 mutex_lock(&matrix_mdev->kvm->lock);
2317 mutex_lock(&matrix_dev->mdevs_lock);
2319 do_remove |= bitmap_and(aprem, ap_remove,
2320 matrix_mdev->matrix.apm,
2322 do_remove |= bitmap_and(aqrem, aq_remove,
2323 matrix_mdev->matrix.aqm,
2325 do_remove |= bitmap_andnot(cdrem, cd_remove,
2326 matrix_mdev->matrix.adm,
2330 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2333 mutex_unlock(&matrix_dev->mdevs_lock);
2334 mutex_unlock(&matrix_mdev->kvm->lock);
2339 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2340 * control domains from the host AP configuration
2341 * by unplugging them from the guests that are
2343 * @cur_config_info: the current host AP configuration information
2344 * @prev_config_info: the previous host AP configuration information
2346 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2347 struct ap_config_info *prev_config_info)
2350 DECLARE_BITMAP(aprem, AP_DEVICES);
2351 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2352 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2354 do_remove = bitmap_andnot(aprem,
2355 (unsigned long *)prev_config_info->apm,
2356 (unsigned long *)cur_config_info->apm,
2358 do_remove |= bitmap_andnot(aqrem,
2359 (unsigned long *)prev_config_info->aqm,
2360 (unsigned long *)cur_config_info->aqm,
2362 do_remove |= bitmap_andnot(cdrem,
2363 (unsigned long *)prev_config_info->adm,
2364 (unsigned long *)cur_config_info->adm,
2368 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2372 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2373 * are older than AP type 10 (CEX4).
2374 * @apm: a bitmap of the APIDs to examine
2375 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2377 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2380 struct ap_queue_status status;
2381 unsigned long apid, apqi;
2382 struct ap_tapq_gr2 info;
2384 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2385 apid_cleared = false;
2387 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2388 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2389 switch (status.response_code) {
2391 * According to the architecture in each case
2392 * below, the queue's info should be filled.
2394 case AP_RESPONSE_NORMAL:
2395 case AP_RESPONSE_RESET_IN_PROGRESS:
2396 case AP_RESPONSE_DECONFIGURED:
2397 case AP_RESPONSE_CHECKSTOPPED:
2398 case AP_RESPONSE_BUSY:
2400 * The vfio_ap device driver only
2401 * supports CEX4 and newer adapters, so
2402 * remove the APID if the adapter is
2403 * older than a CEX4.
2405 if (info.at < AP_DEVICE_TYPE_CEX4) {
2406 clear_bit_inv(apid, apm);
2407 apid_cleared = true;
2414 * If we don't know the adapter type,
2415 * clear its APID since it can't be
2416 * determined whether the vfio_ap
2417 * device driver supports it.
2419 clear_bit_inv(apid, apm);
2420 apid_cleared = true;
2425 * If we've already cleared the APID from the apm, there
2426 * is no need to continue examining the remainin AP
2427 * queues to determine the type of the adapter.
2436 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2437 * control domains that have been added to the host's
2438 * AP configuration for each matrix mdev to which they
2441 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2443 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2445 * @adm_add: a bitmap specifying the control domains that have been added to the
2448 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2449 unsigned long *adm_add)
2451 struct ap_matrix_mdev *matrix_mdev;
2453 if (list_empty(&matrix_dev->mdev_list))
2456 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2458 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2459 bitmap_and(matrix_mdev->apm_add,
2460 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2461 bitmap_and(matrix_mdev->aqm_add,
2462 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2463 bitmap_and(matrix_mdev->adm_add,
2464 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2469 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2470 * control domains to the host AP configuration
2471 * by updating the bitmaps that specify what adapters,
2472 * domains and control domains have been added so they
2473 * can be hot plugged into the guest when the AP bus
2474 * scan completes (see vfio_ap_on_scan_complete
2476 * @cur_config_info: the current AP configuration information
2477 * @prev_config_info: the previous AP configuration information
2479 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2480 struct ap_config_info *prev_config_info)
2483 DECLARE_BITMAP(apm_add, AP_DEVICES);
2484 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2485 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2487 do_add = bitmap_andnot(apm_add,
2488 (unsigned long *)cur_config_info->apm,
2489 (unsigned long *)prev_config_info->apm,
2491 do_add |= bitmap_andnot(aqm_add,
2492 (unsigned long *)cur_config_info->aqm,
2493 (unsigned long *)prev_config_info->aqm,
2495 do_add |= bitmap_andnot(adm_add,
2496 (unsigned long *)cur_config_info->adm,
2497 (unsigned long *)prev_config_info->adm,
2501 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2505 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2508 * @cur_cfg_info: the current host AP configuration
2509 * @prev_cfg_info: the previous host AP configuration
2511 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2512 struct ap_config_info *prev_cfg_info)
2514 if (!cur_cfg_info || !prev_cfg_info)
2517 mutex_lock(&matrix_dev->guests_lock);
2519 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2520 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2521 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2523 mutex_unlock(&matrix_dev->guests_lock);
2526 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2528 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2529 bool filter_domains, filter_adapters, filter_cdoms, do_hotplug = false;
2531 mutex_lock(&matrix_mdev->kvm->lock);
2532 mutex_lock(&matrix_dev->mdevs_lock);
2534 filter_adapters = bitmap_intersects(matrix_mdev->matrix.apm,
2535 matrix_mdev->apm_add, AP_DEVICES);
2536 filter_domains = bitmap_intersects(matrix_mdev->matrix.aqm,
2537 matrix_mdev->aqm_add, AP_DOMAINS);
2538 filter_cdoms = bitmap_intersects(matrix_mdev->matrix.adm,
2539 matrix_mdev->adm_add, AP_DOMAINS);
2541 if (filter_adapters || filter_domains)
2542 do_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
2545 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2548 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2550 reset_queues_for_apids(matrix_mdev, apm_filtered);
2552 mutex_unlock(&matrix_dev->mdevs_lock);
2553 mutex_unlock(&matrix_mdev->kvm->lock);
2556 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2557 struct ap_config_info *old_config_info)
2559 struct ap_matrix_mdev *matrix_mdev;
2561 mutex_lock(&matrix_dev->guests_lock);
2563 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2564 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2565 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2566 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2569 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2570 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2571 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2572 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2575 mutex_unlock(&matrix_dev->guests_lock);