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 MAX_RESET_CHECK_WAIT 200 /* Sleep max 200ms for reset check */
34 #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */
36 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable);
37 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
38 static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
39 static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
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 * vfio_ap_irq_enable - Enable Interruption for a APQN
366 * @q: the vfio_ap_queue holding AQIC parameters
367 * @isc: the guest ISC to register with the GIB interface
368 * @vcpu: the vcpu object containing the registers specifying the parameters
369 * passed to the PQAP(AQIC) instruction.
371 * Pin the NIB saved in *q
372 * Register the guest ISC to GIB interface and retrieve the
373 * host ISC to issue the host side PQAP/AQIC
375 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
376 * vfio_pin_pages failed.
378 * Otherwise return the ap_queue_status returned by the ap_aqic(),
379 * all retry handling will be done by the guest.
381 * Return: &struct ap_queue_status
383 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
385 struct kvm_vcpu *vcpu)
387 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
388 struct ap_queue_status status = {};
389 struct kvm_s390_gisa *gisa;
397 /* Verify that the notification indicator byte address is valid */
398 if (vfio_ap_validate_nib(vcpu, &nib)) {
399 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
400 __func__, &nib, q->apqn);
402 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
406 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
407 IOMMU_READ | IOMMU_WRITE, &h_page);
412 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
413 "nib=%pad, apqn=%#04x\n",
414 __func__, ret, &nib, q->apqn);
416 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
420 kvm = q->matrix_mdev->kvm;
421 gisa = kvm->arch.gisa_int.origin;
423 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
424 aqic_gisa.gisc = isc;
426 nisc = kvm_s390_gisc_register(kvm, isc);
428 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
429 __func__, nisc, isc, q->apqn);
431 status.response_code = AP_RESPONSE_INVALID_GISA;
435 aqic_gisa.isc = nisc;
437 aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
439 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
440 switch (status.response_code) {
441 case AP_RESPONSE_NORMAL:
442 /* See if we did clear older IRQ configuration */
443 vfio_ap_free_aqic_resources(q);
447 case AP_RESPONSE_OTHERWISE_CHANGED:
448 /* We could not modify IRQ setings: clear new configuration */
449 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
450 kvm_s390_gisc_unregister(kvm, isc);
453 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
454 status.response_code);
455 vfio_ap_irq_disable(q);
459 if (status.response_code != AP_RESPONSE_NORMAL) {
460 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
461 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
462 "gisa=%#x, isc=%#x, apqn=%#04x\n",
463 __func__, status.response_code,
464 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
465 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
473 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
474 * of big endian elements that can be passed by
475 * value to an s390dbf sprintf event function to
476 * format a UUID string.
478 * @guid: the object containing the little endian guid
479 * @uuid: a six-element array of long values that can be passed by value as
480 * arguments for a formatting string specifying a UUID.
482 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
483 * event functions if the memory for the passed string is available as long as
484 * the debug feature exists. Since a mediated device can be removed at any
485 * time, it's name can not be used because %s passes the reference to the string
486 * in memory and the reference will go stale once the device is removed .
488 * The s390dbf string formatting function allows a maximum of 9 arguments for a
489 * message to be displayed in the 'sprintf' view. In order to use the bytes
490 * comprising the mediated device's UUID to display the mediated device name,
491 * they will have to be converted into an array whose elements can be passed by
492 * value to sprintf. For example:
494 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
495 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
496 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
497 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
499 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
502 * The input guid is ordered in little endian, so it needs to be
503 * reordered for displaying a UUID as a string. This specifies the
504 * guid indices in proper order.
506 uuid[0] = le32_to_cpup((__le32 *)guid);
507 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
508 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
509 uuid[3] = *((__u16 *)&guid->b[8]);
510 uuid[4] = *((__u16 *)&guid->b[10]);
511 uuid[5] = *((__u32 *)&guid->b[12]);
515 * handle_pqap - PQAP instruction callback
517 * @vcpu: The vcpu on which we received the PQAP instruction
519 * Get the general register contents to initialize internal variables.
524 * Response.status may be set to following Response Code:
525 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
526 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
527 * - AP_RESPONSE_NORMAL (0) : in case of successs
528 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
529 * We take the matrix_dev lock to ensure serialization on queues and
530 * mediated device access.
532 * Return: 0 if we could handle the request inside KVM.
533 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
535 static int handle_pqap(struct kvm_vcpu *vcpu)
539 unsigned long uuid[6];
540 struct vfio_ap_queue *q;
541 struct ap_queue_status qstatus = {
542 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
543 struct ap_matrix_mdev *matrix_mdev;
545 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
547 /* If we do not use the AIV facility just go to userland */
548 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
549 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
550 __func__, apqn, vcpu->arch.sie_block->eca);
555 mutex_lock(&matrix_dev->mdevs_lock);
557 if (!vcpu->kvm->arch.crypto.pqap_hook) {
558 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
564 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
565 struct ap_matrix_mdev, pqap_hook);
567 /* If the there is no guest using the mdev, there is nothing to do */
568 if (!matrix_mdev->kvm) {
569 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
570 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
571 __func__, uuid[0], uuid[1], uuid[2],
572 uuid[3], uuid[4], uuid[5], apqn);
576 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
578 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
579 __func__, AP_QID_CARD(apqn),
584 status = vcpu->run->s.regs.gprs[1];
586 /* If IR bit(16) is set we enable the interrupt */
587 if ((status >> (63 - 16)) & 0x01)
588 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
590 qstatus = vfio_ap_irq_disable(q);
593 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
594 vcpu->run->s.regs.gprs[1] >>= 32;
595 mutex_unlock(&matrix_dev->mdevs_lock);
599 static void vfio_ap_matrix_init(struct ap_config_info *info,
600 struct ap_matrix *matrix)
602 matrix->apm_max = info->apxa ? info->na : 63;
603 matrix->aqm_max = info->apxa ? info->nd : 15;
604 matrix->adm_max = info->apxa ? info->nd : 15;
607 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
609 if (matrix_mdev->kvm)
610 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
611 matrix_mdev->shadow_apcb.apm,
612 matrix_mdev->shadow_apcb.aqm,
613 matrix_mdev->shadow_apcb.adm);
616 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
618 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
620 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
621 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
622 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
624 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
629 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
630 * to ensure no queue devices are passed through to
631 * the guest that are not bound to the vfio_ap
634 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
636 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
637 * driver, its APID will be filtered from the guest's APCB. The matrix
638 * structure precludes filtering an individual APQN, so its APID will be
641 * Return: a boolean value indicating whether the KVM guest's APCB was changed
642 * by the filtering or not.
644 static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm,
645 struct ap_matrix_mdev *matrix_mdev)
647 unsigned long apid, apqi, apqn;
648 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
649 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
650 struct vfio_ap_queue *q;
652 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
653 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
654 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
657 * Copy the adapters, domains and control domains to the shadow_apcb
658 * from the matrix mdev, but only those that are assigned to the host's
661 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
662 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
663 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
664 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
666 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
667 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
669 * If the APQN is not bound to the vfio_ap device
670 * driver, then we can't assign it to the guest's
671 * AP configuration. The AP architecture won't
672 * allow filtering of a single APQN, so let's filter
673 * the APID since an adapter represents a physical
676 apqn = AP_MKQID(apid, apqi);
677 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
678 if (!q || q->reset_rc) {
680 matrix_mdev->shadow_apcb.apm);
686 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
688 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
692 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
694 struct ap_matrix_mdev *matrix_mdev =
695 container_of(vdev, struct ap_matrix_mdev, vdev);
697 matrix_mdev->mdev = to_mdev_device(vdev->dev);
698 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
699 matrix_mdev->pqap_hook = handle_pqap;
700 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
701 hash_init(matrix_mdev->qtable.queues);
706 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
708 struct ap_matrix_mdev *matrix_mdev;
711 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
712 &vfio_ap_matrix_dev_ops);
713 if (IS_ERR(matrix_mdev))
714 return PTR_ERR(matrix_mdev);
716 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
719 matrix_mdev->req_trigger = NULL;
720 dev_set_drvdata(&mdev->dev, matrix_mdev);
721 mutex_lock(&matrix_dev->mdevs_lock);
722 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
723 mutex_unlock(&matrix_dev->mdevs_lock);
727 vfio_put_device(&matrix_mdev->vdev);
731 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
732 struct vfio_ap_queue *q)
735 q->matrix_mdev = matrix_mdev;
736 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
740 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
742 struct vfio_ap_queue *q;
744 q = vfio_ap_find_queue(apqn);
745 vfio_ap_mdev_link_queue(matrix_mdev, q);
748 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
750 hash_del(&q->mdev_qnode);
753 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
755 q->matrix_mdev = NULL;
758 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
760 struct vfio_ap_queue *q;
761 unsigned long apid, apqi;
763 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
764 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
766 q = vfio_ap_mdev_get_queue(matrix_mdev,
767 AP_MKQID(apid, apqi));
769 q->matrix_mdev = NULL;
774 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
776 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
778 vfio_unregister_group_dev(&matrix_mdev->vdev);
780 mutex_lock(&matrix_dev->guests_lock);
781 mutex_lock(&matrix_dev->mdevs_lock);
782 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
783 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
784 list_del(&matrix_mdev->node);
785 mutex_unlock(&matrix_dev->mdevs_lock);
786 mutex_unlock(&matrix_dev->guests_lock);
787 vfio_put_device(&matrix_mdev->vdev);
790 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
791 "already assigned to %s"
793 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
797 unsigned long apid, apqi;
798 const struct device *dev = mdev_dev(matrix_mdev->mdev);
799 const char *mdev_name = dev_name(dev);
801 for_each_set_bit_inv(apid, apm, AP_DEVICES)
802 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
803 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
807 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
809 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
810 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
812 * Verifies that each APQN derived from the Cartesian product of a bitmap of
813 * AP adapter IDs and AP queue indexes is not configured for any matrix
814 * mediated device. AP queue sharing is not allowed.
816 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
818 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
819 unsigned long *mdev_aqm)
821 struct ap_matrix_mdev *matrix_mdev;
822 DECLARE_BITMAP(apm, AP_DEVICES);
823 DECLARE_BITMAP(aqm, AP_DOMAINS);
825 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
827 * If the input apm and aqm are fields of the matrix_mdev
828 * object, then move on to the next matrix_mdev.
830 if (mdev_apm == matrix_mdev->matrix.apm &&
831 mdev_aqm == matrix_mdev->matrix.aqm)
834 memset(apm, 0, sizeof(apm));
835 memset(aqm, 0, sizeof(aqm));
838 * We work on full longs, as we can only exclude the leftover
839 * bits in non-inverse order. The leftover is all zeros.
841 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
845 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
849 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
858 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
859 * not reserved for the default zcrypt driver and
860 * are not assigned to another mdev.
862 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
864 * Return: One of the following values:
865 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
866 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
867 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
868 * zcrypt default driver.
869 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
870 * o A zero indicating validation succeeded.
872 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
874 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
875 matrix_mdev->matrix.aqm))
876 return -EADDRNOTAVAIL;
878 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
879 matrix_mdev->matrix.aqm);
882 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
887 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
888 vfio_ap_mdev_link_apqn(matrix_mdev,
889 AP_MKQID(apid, apqi));
893 * assign_adapter_store - parses the APID from @buf and sets the
894 * corresponding bit in the mediated matrix device's APM
896 * @dev: the matrix device
897 * @attr: the mediated matrix device's assign_adapter attribute
898 * @buf: a buffer containing the AP adapter number (APID) to
900 * @count: the number of bytes in @buf
902 * Return: the number of bytes processed if the APID is valid; otherwise,
903 * returns one of the following errors:
906 * The APID is not a valid number
909 * The APID exceeds the maximum value configured for the system
912 * An APQN derived from the cross product of the APID being assigned
913 * and the APQIs previously assigned is not bound to the vfio_ap device
914 * driver; or, if no APQIs have yet been assigned, the APID is not
915 * contained in an APQN bound to the vfio_ap device driver.
918 * An APQN derived from the cross product of the APID being assigned
919 * and the APQIs previously assigned is being used by another mediated
923 * A lock required to validate the mdev's AP configuration could not
926 static ssize_t assign_adapter_store(struct device *dev,
927 struct device_attribute *attr,
928 const char *buf, size_t count)
932 DECLARE_BITMAP(apm_delta, AP_DEVICES);
933 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
935 mutex_lock(&ap_perms_mutex);
936 get_update_locks_for_mdev(matrix_mdev);
938 ret = kstrtoul(buf, 0, &apid);
942 if (apid > matrix_mdev->matrix.apm_max) {
947 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
952 set_bit_inv(apid, matrix_mdev->matrix.apm);
954 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
956 clear_bit_inv(apid, matrix_mdev->matrix.apm);
960 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
961 memset(apm_delta, 0, sizeof(apm_delta));
962 set_bit_inv(apid, apm_delta);
964 if (vfio_ap_mdev_filter_matrix(apm_delta,
965 matrix_mdev->matrix.aqm, matrix_mdev))
966 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
970 release_update_locks_for_mdev(matrix_mdev);
971 mutex_unlock(&ap_perms_mutex);
975 static DEVICE_ATTR_WO(assign_adapter);
977 static struct vfio_ap_queue
978 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
979 unsigned long apid, unsigned long apqi)
981 struct vfio_ap_queue *q = NULL;
983 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
984 /* If the queue is assigned to the matrix mdev, unlink it. */
986 vfio_ap_unlink_queue_fr_mdev(q);
992 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
993 * adapter from the matrix mdev to which the
994 * adapter was assigned.
995 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
996 * @apid: the APID of the unassigned adapter.
997 * @qtable: table for storing queues associated with unassigned adapter.
999 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1001 struct ap_queue_table *qtable)
1004 struct vfio_ap_queue *q;
1006 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1007 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1010 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1011 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1012 hash_add(qtable->queues, &q->mdev_qnode,
1018 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1022 struct vfio_ap_queue *q;
1023 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1025 hash_init(qtable->queues);
1026 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, qtable);
1028 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1029 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1030 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1033 vfio_ap_mdev_reset_queues(qtable);
1035 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1036 vfio_ap_unlink_mdev_fr_queue(q);
1037 hash_del(&q->mdev_qnode);
1044 * unassign_adapter_store - parses the APID from @buf and clears the
1045 * corresponding bit in the mediated matrix device's APM
1047 * @dev: the matrix device
1048 * @attr: the mediated matrix device's unassign_adapter attribute
1049 * @buf: a buffer containing the adapter number (APID) to be unassigned
1050 * @count: the number of bytes in @buf
1052 * Return: the number of bytes processed if the APID is valid; otherwise,
1053 * returns one of the following errors:
1054 * -EINVAL if the APID is not a number
1055 * -ENODEV if the APID it exceeds the maximum value configured for the
1058 static ssize_t unassign_adapter_store(struct device *dev,
1059 struct device_attribute *attr,
1060 const char *buf, size_t count)
1064 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1066 get_update_locks_for_mdev(matrix_mdev);
1068 ret = kstrtoul(buf, 0, &apid);
1072 if (apid > matrix_mdev->matrix.apm_max) {
1077 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1082 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1083 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1086 release_update_locks_for_mdev(matrix_mdev);
1089 static DEVICE_ATTR_WO(unassign_adapter);
1091 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1096 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1097 vfio_ap_mdev_link_apqn(matrix_mdev,
1098 AP_MKQID(apid, apqi));
1102 * assign_domain_store - parses the APQI from @buf and sets the
1103 * corresponding bit in the mediated matrix device's AQM
1105 * @dev: the matrix device
1106 * @attr: the mediated matrix device's assign_domain attribute
1107 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1109 * @count: the number of bytes in @buf
1111 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1112 * one of the following errors:
1115 * The APQI is not a valid number
1118 * The APQI exceeds the maximum value configured for the system
1121 * An APQN derived from the cross product of the APQI being assigned
1122 * and the APIDs previously assigned is not bound to the vfio_ap device
1123 * driver; or, if no APIDs have yet been assigned, the APQI is not
1124 * contained in an APQN bound to the vfio_ap device driver.
1127 * An APQN derived from the cross product of the APQI being assigned
1128 * and the APIDs previously assigned is being used by another mediated
1132 * The lock required to validate the mdev's AP configuration could not
1135 static ssize_t assign_domain_store(struct device *dev,
1136 struct device_attribute *attr,
1137 const char *buf, size_t count)
1141 DECLARE_BITMAP(aqm_delta, AP_DOMAINS);
1142 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1144 mutex_lock(&ap_perms_mutex);
1145 get_update_locks_for_mdev(matrix_mdev);
1147 ret = kstrtoul(buf, 0, &apqi);
1151 if (apqi > matrix_mdev->matrix.aqm_max) {
1156 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1161 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1163 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1165 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1169 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1170 memset(aqm_delta, 0, sizeof(aqm_delta));
1171 set_bit_inv(apqi, aqm_delta);
1173 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, aqm_delta,
1175 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1179 release_update_locks_for_mdev(matrix_mdev);
1180 mutex_unlock(&ap_perms_mutex);
1184 static DEVICE_ATTR_WO(assign_domain);
1186 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1188 struct ap_queue_table *qtable)
1191 struct vfio_ap_queue *q;
1193 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1194 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1197 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1198 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1199 hash_add(qtable->queues, &q->mdev_qnode,
1205 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1209 struct vfio_ap_queue *q;
1210 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1212 hash_init(qtable->queues);
1213 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, qtable);
1215 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1216 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1217 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1220 vfio_ap_mdev_reset_queues(qtable);
1222 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1223 vfio_ap_unlink_mdev_fr_queue(q);
1224 hash_del(&q->mdev_qnode);
1231 * unassign_domain_store - parses the APQI from @buf and clears the
1232 * corresponding bit in the mediated matrix device's AQM
1234 * @dev: the matrix device
1235 * @attr: the mediated matrix device's unassign_domain attribute
1236 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1238 * @count: the number of bytes in @buf
1240 * Return: the number of bytes processed if the APQI is valid; otherwise,
1241 * returns one of the following errors:
1242 * -EINVAL if the APQI is not a number
1243 * -ENODEV if the APQI exceeds the maximum value configured for the system
1245 static ssize_t unassign_domain_store(struct device *dev,
1246 struct device_attribute *attr,
1247 const char *buf, size_t count)
1251 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1253 get_update_locks_for_mdev(matrix_mdev);
1255 ret = kstrtoul(buf, 0, &apqi);
1259 if (apqi > matrix_mdev->matrix.aqm_max) {
1264 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1269 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1270 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1274 release_update_locks_for_mdev(matrix_mdev);
1277 static DEVICE_ATTR_WO(unassign_domain);
1280 * assign_control_domain_store - parses the domain ID from @buf and sets
1281 * the corresponding bit in the mediated matrix device's ADM
1283 * @dev: the matrix device
1284 * @attr: the mediated matrix device's assign_control_domain attribute
1285 * @buf: a buffer containing the domain ID to be assigned
1286 * @count: the number of bytes in @buf
1288 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1289 * returns one of the following errors:
1290 * -EINVAL if the ID is not a number
1291 * -ENODEV if the ID exceeds the maximum value configured for the system
1293 static ssize_t assign_control_domain_store(struct device *dev,
1294 struct device_attribute *attr,
1295 const char *buf, size_t count)
1299 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1301 get_update_locks_for_mdev(matrix_mdev);
1303 ret = kstrtoul(buf, 0, &id);
1307 if (id > matrix_mdev->matrix.adm_max) {
1312 if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1317 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1318 * domain number (id). The bits in the mask, from most significant to
1319 * least significant, correspond to IDs 0 up to the one less than the
1320 * number of control domains that can be assigned.
1322 set_bit_inv(id, matrix_mdev->matrix.adm);
1323 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1324 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1328 release_update_locks_for_mdev(matrix_mdev);
1331 static DEVICE_ATTR_WO(assign_control_domain);
1334 * unassign_control_domain_store - parses the domain ID from @buf and
1335 * clears the corresponding bit in the mediated matrix device's ADM
1337 * @dev: the matrix device
1338 * @attr: the mediated matrix device's unassign_control_domain attribute
1339 * @buf: a buffer containing the domain ID to be unassigned
1340 * @count: the number of bytes in @buf
1342 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1343 * returns one of the following errors:
1344 * -EINVAL if the ID is not a number
1345 * -ENODEV if the ID exceeds the maximum value configured for the system
1347 static ssize_t unassign_control_domain_store(struct device *dev,
1348 struct device_attribute *attr,
1349 const char *buf, size_t count)
1352 unsigned long domid;
1353 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1355 get_update_locks_for_mdev(matrix_mdev);
1357 ret = kstrtoul(buf, 0, &domid);
1361 if (domid > matrix_mdev->matrix.adm_max) {
1366 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1371 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1373 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1374 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1375 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1380 release_update_locks_for_mdev(matrix_mdev);
1383 static DEVICE_ATTR_WO(unassign_control_domain);
1385 static ssize_t control_domains_show(struct device *dev,
1386 struct device_attribute *dev_attr,
1393 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1394 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1396 mutex_lock(&matrix_dev->mdevs_lock);
1397 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1398 n = sprintf(bufpos, "%04lx\n", id);
1402 mutex_unlock(&matrix_dev->mdevs_lock);
1406 static DEVICE_ATTR_RO(control_domains);
1408 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1413 unsigned long apid1;
1414 unsigned long apqi1;
1415 unsigned long napm_bits = matrix->apm_max + 1;
1416 unsigned long naqm_bits = matrix->aqm_max + 1;
1420 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1421 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1423 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1424 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1425 for_each_set_bit_inv(apqi, matrix->aqm,
1427 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1433 } else if (apid1 < napm_bits) {
1434 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1435 n = sprintf(bufpos, "%02lx.\n", apid);
1439 } else if (apqi1 < naqm_bits) {
1440 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1441 n = sprintf(bufpos, ".%04lx\n", apqi);
1450 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1454 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1456 mutex_lock(&matrix_dev->mdevs_lock);
1457 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1458 mutex_unlock(&matrix_dev->mdevs_lock);
1462 static DEVICE_ATTR_RO(matrix);
1464 static ssize_t guest_matrix_show(struct device *dev,
1465 struct device_attribute *attr, char *buf)
1468 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1470 mutex_lock(&matrix_dev->mdevs_lock);
1471 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1472 mutex_unlock(&matrix_dev->mdevs_lock);
1476 static DEVICE_ATTR_RO(guest_matrix);
1478 static struct attribute *vfio_ap_mdev_attrs[] = {
1479 &dev_attr_assign_adapter.attr,
1480 &dev_attr_unassign_adapter.attr,
1481 &dev_attr_assign_domain.attr,
1482 &dev_attr_unassign_domain.attr,
1483 &dev_attr_assign_control_domain.attr,
1484 &dev_attr_unassign_control_domain.attr,
1485 &dev_attr_control_domains.attr,
1486 &dev_attr_matrix.attr,
1487 &dev_attr_guest_matrix.attr,
1491 static struct attribute_group vfio_ap_mdev_attr_group = {
1492 .attrs = vfio_ap_mdev_attrs
1495 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1496 &vfio_ap_mdev_attr_group,
1501 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1502 * to manage AP resources for the guest whose state is represented by @kvm
1504 * @matrix_mdev: a mediated matrix device
1505 * @kvm: reference to KVM instance
1507 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1508 * otherwise, returns an -EPERM.
1510 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1513 struct ap_matrix_mdev *m;
1515 if (kvm->arch.crypto.crycbd) {
1516 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1517 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1518 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1520 get_update_locks_for_kvm(kvm);
1522 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1523 if (m != matrix_mdev && m->kvm == kvm) {
1524 release_update_locks_for_kvm(kvm);
1530 matrix_mdev->kvm = kvm;
1531 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1533 release_update_locks_for_kvm(kvm);
1539 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1541 struct ap_queue_table *qtable = &matrix_mdev->qtable;
1542 struct vfio_ap_queue *q;
1545 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1546 if (q->saved_iova >= iova && q->saved_iova < iova + length)
1547 vfio_ap_irq_disable(q);
1551 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1554 struct ap_matrix_mdev *matrix_mdev =
1555 container_of(vdev, struct ap_matrix_mdev, vdev);
1557 mutex_lock(&matrix_dev->mdevs_lock);
1559 unmap_iova(matrix_mdev, iova, length);
1561 mutex_unlock(&matrix_dev->mdevs_lock);
1565 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1568 * @matrix_mdev: a matrix mediated device
1570 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1572 struct kvm *kvm = matrix_mdev->kvm;
1574 if (kvm && kvm->arch.crypto.crycbd) {
1575 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1576 kvm->arch.crypto.pqap_hook = NULL;
1577 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1579 get_update_locks_for_kvm(kvm);
1581 kvm_arch_crypto_clear_masks(kvm);
1582 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1584 matrix_mdev->kvm = NULL;
1586 release_update_locks_for_kvm(kvm);
1590 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1592 struct ap_queue *queue;
1593 struct vfio_ap_queue *q = NULL;
1595 queue = ap_get_qdev(apqn);
1599 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1600 q = dev_get_drvdata(&queue->ap_dev.device);
1602 put_device(&queue->ap_dev.device);
1607 static int apq_status_check(int apqn, struct ap_queue_status *status)
1609 switch (status->response_code) {
1610 case AP_RESPONSE_NORMAL:
1611 case AP_RESPONSE_RESET_IN_PROGRESS:
1612 if (status->queue_empty && !status->irq_enabled)
1615 case AP_RESPONSE_DECONFIGURED:
1617 * If the AP queue is deconfigured, any subsequent AP command
1618 * targeting the queue will fail with the same response code. On the
1619 * other hand, when an AP adapter is deconfigured, the associated
1620 * queues are reset, so let's return a value indicating the reset
1621 * for which we're waiting completed successfully.
1626 "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1627 AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1628 status->response_code);
1633 static int apq_reset_check(struct vfio_ap_queue *q)
1636 int iters = MAX_RESET_CHECK_WAIT / AP_RESET_INTERVAL;
1637 struct ap_queue_status status;
1639 for (; iters > 0; iters--) {
1640 msleep(AP_RESET_INTERVAL);
1641 status = ap_tapq(q->apqn, NULL);
1642 ret = apq_status_check(q->apqn, &status);
1646 WARN_ONCE(iters <= 0,
1647 "timeout verifying reset of queue %02x.%04x (%u, %u, %u)",
1648 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1649 status.queue_empty, status.irq_enabled, status.response_code);
1653 static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1655 struct ap_queue_status status;
1661 status = ap_zapq(q->apqn, 0);
1662 q->reset_rc = status.response_code;
1663 switch (status.response_code) {
1664 case AP_RESPONSE_NORMAL:
1666 /* if the reset has not completed, wait for it to take effect */
1667 if (!status.queue_empty || status.irq_enabled)
1668 ret = apq_reset_check(q);
1670 case AP_RESPONSE_RESET_IN_PROGRESS:
1672 * There is a reset issued by another process in progress. Let's wait
1673 * for that to complete. Since we have no idea whether it was a RAPQ or
1674 * ZAPQ, then if it completes successfully, let's issue the ZAPQ.
1676 ret = apq_reset_check(q);
1680 case AP_RESPONSE_DECONFIGURED:
1682 * When an AP adapter is deconfigured, the associated
1683 * queues are reset, so let's return a value indicating the reset
1684 * completed successfully.
1690 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1691 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1692 status.response_code);
1696 vfio_ap_free_aqic_resources(q);
1701 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable)
1703 int ret, loop_cursor, rc = 0;
1704 struct vfio_ap_queue *q;
1706 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1707 ret = vfio_ap_mdev_reset_queue(q);
1709 * Regardless whether a queue turns out to be busy, or
1710 * is not operational, we need to continue resetting
1711 * the remaining queues.
1720 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1722 struct ap_matrix_mdev *matrix_mdev =
1723 container_of(vdev, struct ap_matrix_mdev, vdev);
1728 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
1731 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1733 struct ap_matrix_mdev *matrix_mdev =
1734 container_of(vdev, struct ap_matrix_mdev, vdev);
1736 vfio_ap_mdev_unset_kvm(matrix_mdev);
1739 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
1741 struct device *dev = vdev->dev;
1742 struct ap_matrix_mdev *matrix_mdev;
1744 matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
1746 if (matrix_mdev->req_trigger) {
1748 dev_notice_ratelimited(dev,
1749 "Relaying device request to user (#%u)\n",
1752 eventfd_signal(matrix_mdev->req_trigger, 1);
1753 } else if (count == 0) {
1755 "No device request registered, blocked until released by user\n");
1759 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1761 unsigned long minsz;
1762 struct vfio_device_info info;
1764 minsz = offsetofend(struct vfio_device_info, num_irqs);
1766 if (copy_from_user(&info, (void __user *)arg, minsz))
1769 if (info.argsz < minsz)
1772 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1773 info.num_regions = 0;
1774 info.num_irqs = VFIO_AP_NUM_IRQS;
1776 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1779 static ssize_t vfio_ap_get_irq_info(unsigned long arg)
1781 unsigned long minsz;
1782 struct vfio_irq_info info;
1784 minsz = offsetofend(struct vfio_irq_info, count);
1786 if (copy_from_user(&info, (void __user *)arg, minsz))
1789 if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
1792 switch (info.index) {
1793 case VFIO_AP_REQ_IRQ_INDEX:
1795 info.flags = VFIO_IRQ_INFO_EVENTFD;
1801 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1804 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
1808 unsigned long minsz;
1810 minsz = offsetofend(struct vfio_irq_set, count);
1812 if (copy_from_user(irq_set, (void __user *)arg, minsz))
1815 ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
1820 if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
1826 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
1831 unsigned long minsz;
1832 struct eventfd_ctx *req_trigger;
1834 minsz = offsetofend(struct vfio_irq_set, count);
1835 data = (void __user *)(arg + minsz);
1837 if (get_user(fd, (s32 __user *)data))
1841 if (matrix_mdev->req_trigger)
1842 eventfd_ctx_put(matrix_mdev->req_trigger);
1843 matrix_mdev->req_trigger = NULL;
1844 } else if (fd >= 0) {
1845 req_trigger = eventfd_ctx_fdget(fd);
1846 if (IS_ERR(req_trigger))
1847 return PTR_ERR(req_trigger);
1849 if (matrix_mdev->req_trigger)
1850 eventfd_ctx_put(matrix_mdev->req_trigger);
1852 matrix_mdev->req_trigger = req_trigger;
1860 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
1864 struct vfio_irq_set irq_set;
1866 ret = vfio_ap_irq_set_init(&irq_set, arg);
1870 switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
1871 case VFIO_IRQ_SET_DATA_EVENTFD:
1872 switch (irq_set.index) {
1873 case VFIO_AP_REQ_IRQ_INDEX:
1874 return vfio_ap_set_request_irq(matrix_mdev, arg);
1883 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1884 unsigned int cmd, unsigned long arg)
1886 struct ap_matrix_mdev *matrix_mdev =
1887 container_of(vdev, struct ap_matrix_mdev, vdev);
1890 mutex_lock(&matrix_dev->mdevs_lock);
1892 case VFIO_DEVICE_GET_INFO:
1893 ret = vfio_ap_mdev_get_device_info(arg);
1895 case VFIO_DEVICE_RESET:
1896 ret = vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1898 case VFIO_DEVICE_GET_IRQ_INFO:
1899 ret = vfio_ap_get_irq_info(arg);
1901 case VFIO_DEVICE_SET_IRQS:
1902 ret = vfio_ap_set_irqs(matrix_mdev, arg);
1908 mutex_unlock(&matrix_dev->mdevs_lock);
1913 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
1915 struct ap_matrix_mdev *matrix_mdev;
1916 unsigned long apid = AP_QID_CARD(q->apqn);
1917 unsigned long apqi = AP_QID_QUEUE(q->apqn);
1919 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
1920 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
1921 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
1928 static ssize_t status_show(struct device *dev,
1929 struct device_attribute *attr,
1933 struct vfio_ap_queue *q;
1934 struct ap_matrix_mdev *matrix_mdev;
1935 struct ap_device *apdev = to_ap_dev(dev);
1937 mutex_lock(&matrix_dev->mdevs_lock);
1938 q = dev_get_drvdata(&apdev->device);
1939 matrix_mdev = vfio_ap_mdev_for_queue(q);
1942 if (matrix_mdev->kvm)
1943 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1946 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1949 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1950 AP_QUEUE_UNASSIGNED);
1953 mutex_unlock(&matrix_dev->mdevs_lock);
1958 static DEVICE_ATTR_RO(status);
1960 static struct attribute *vfio_queue_attrs[] = {
1961 &dev_attr_status.attr,
1965 static const struct attribute_group vfio_queue_attr_group = {
1966 .attrs = vfio_queue_attrs,
1969 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1970 .init = vfio_ap_mdev_init_dev,
1971 .open_device = vfio_ap_mdev_open_device,
1972 .close_device = vfio_ap_mdev_close_device,
1973 .ioctl = vfio_ap_mdev_ioctl,
1974 .dma_unmap = vfio_ap_mdev_dma_unmap,
1975 .bind_iommufd = vfio_iommufd_emulated_bind,
1976 .unbind_iommufd = vfio_iommufd_emulated_unbind,
1977 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
1978 .request = vfio_ap_mdev_request
1981 static struct mdev_driver vfio_ap_matrix_driver = {
1982 .device_api = VFIO_DEVICE_API_AP_STRING,
1983 .max_instances = MAX_ZDEV_ENTRIES_EXT,
1985 .name = "vfio_ap_mdev",
1986 .owner = THIS_MODULE,
1987 .mod_name = KBUILD_MODNAME,
1988 .dev_groups = vfio_ap_mdev_attr_groups,
1990 .probe = vfio_ap_mdev_probe,
1991 .remove = vfio_ap_mdev_remove,
1994 int vfio_ap_mdev_register(void)
1998 ret = mdev_register_driver(&vfio_ap_matrix_driver);
2002 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
2003 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
2004 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
2005 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2006 &vfio_ap_matrix_driver,
2007 matrix_dev->mdev_types, 1);
2013 mdev_unregister_driver(&vfio_ap_matrix_driver);
2017 void vfio_ap_mdev_unregister(void)
2019 mdev_unregister_parent(&matrix_dev->parent);
2020 mdev_unregister_driver(&vfio_ap_matrix_driver);
2023 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2026 struct vfio_ap_queue *q;
2027 struct ap_matrix_mdev *matrix_mdev;
2029 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2033 q = kzalloc(sizeof(*q), GFP_KERNEL);
2036 goto err_remove_group;
2039 q->apqn = to_ap_queue(&apdev->device)->qid;
2040 q->saved_isc = VFIO_AP_ISC_INVALID;
2041 matrix_mdev = get_update_locks_by_apqn(q->apqn);
2044 vfio_ap_mdev_link_queue(matrix_mdev, q);
2046 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm,
2047 matrix_mdev->matrix.aqm,
2049 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2051 dev_set_drvdata(&apdev->device, q);
2052 release_update_locks_for_mdev(matrix_mdev);
2057 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2061 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2063 unsigned long apid, apqi;
2064 struct vfio_ap_queue *q;
2065 struct ap_matrix_mdev *matrix_mdev;
2067 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2068 q = dev_get_drvdata(&apdev->device);
2069 get_update_locks_for_queue(q);
2070 matrix_mdev = q->matrix_mdev;
2073 vfio_ap_unlink_queue_fr_mdev(q);
2075 apid = AP_QID_CARD(q->apqn);
2076 apqi = AP_QID_QUEUE(q->apqn);
2079 * If the queue is assigned to the guest's APCB, then remove
2080 * the adapter's APID from the APCB and hot it into the guest.
2082 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2083 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2084 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2085 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2089 vfio_ap_mdev_reset_queue(q);
2090 dev_set_drvdata(&apdev->device, NULL);
2092 release_update_locks_for_mdev(matrix_mdev);
2096 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2097 * assigned to a mediated device under the control
2098 * of the vfio_ap device driver.
2100 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2101 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2104 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2105 * assigned to a mediated device under the control of the vfio_ap
2107 * * Otherwise, return 0.
2109 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2113 mutex_lock(&matrix_dev->guests_lock);
2114 mutex_lock(&matrix_dev->mdevs_lock);
2115 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2116 mutex_unlock(&matrix_dev->mdevs_lock);
2117 mutex_unlock(&matrix_dev->guests_lock);
2123 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2124 * domains that have been removed from the host's
2125 * AP configuration from a guest.
2127 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2128 * @aprem: the adapters that have been removed from the host's AP configuration
2129 * @aqrem: the domains that have been removed from the host's AP configuration
2130 * @cdrem: the control domains that have been removed from the host's AP
2133 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2134 unsigned long *aprem,
2135 unsigned long *aqrem,
2136 unsigned long *cdrem)
2140 if (!bitmap_empty(aprem, AP_DEVICES)) {
2141 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2142 matrix_mdev->shadow_apcb.apm,
2146 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2147 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2148 matrix_mdev->shadow_apcb.aqm,
2152 if (!bitmap_empty(cdrem, AP_DOMAINS))
2153 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2154 matrix_mdev->shadow_apcb.adm,
2158 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2162 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2163 * domains and control domains that have been removed
2164 * from the host AP configuration and unplugs them
2165 * from those guests.
2167 * @ap_remove: bitmap specifying which adapters have been removed from the host
2169 * @aq_remove: bitmap specifying which domains have been removed from the host
2171 * @cd_remove: bitmap specifying which control domains have been removed from
2174 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2175 unsigned long *aq_remove,
2176 unsigned long *cd_remove)
2178 struct ap_matrix_mdev *matrix_mdev;
2179 DECLARE_BITMAP(aprem, AP_DEVICES);
2180 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2181 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2184 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2185 mutex_lock(&matrix_mdev->kvm->lock);
2186 mutex_lock(&matrix_dev->mdevs_lock);
2188 do_remove |= bitmap_and(aprem, ap_remove,
2189 matrix_mdev->matrix.apm,
2191 do_remove |= bitmap_and(aqrem, aq_remove,
2192 matrix_mdev->matrix.aqm,
2194 do_remove |= bitmap_andnot(cdrem, cd_remove,
2195 matrix_mdev->matrix.adm,
2199 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2202 mutex_unlock(&matrix_dev->mdevs_lock);
2203 mutex_unlock(&matrix_mdev->kvm->lock);
2208 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2209 * control domains from the host AP configuration
2210 * by unplugging them from the guests that are
2212 * @cur_config_info: the current host AP configuration information
2213 * @prev_config_info: the previous host AP configuration information
2215 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2216 struct ap_config_info *prev_config_info)
2219 DECLARE_BITMAP(aprem, AP_DEVICES);
2220 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2221 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2223 do_remove = bitmap_andnot(aprem,
2224 (unsigned long *)prev_config_info->apm,
2225 (unsigned long *)cur_config_info->apm,
2227 do_remove |= bitmap_andnot(aqrem,
2228 (unsigned long *)prev_config_info->aqm,
2229 (unsigned long *)cur_config_info->aqm,
2231 do_remove |= bitmap_andnot(cdrem,
2232 (unsigned long *)prev_config_info->adm,
2233 (unsigned long *)cur_config_info->adm,
2237 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2241 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2242 * are older than AP type 10 (CEX4).
2243 * @apm: a bitmap of the APIDs to examine
2244 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2246 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2249 struct ap_queue_status status;
2250 unsigned long apid, apqi;
2251 struct ap_tapq_gr2 info;
2253 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2254 apid_cleared = false;
2256 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2257 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2258 switch (status.response_code) {
2260 * According to the architecture in each case
2261 * below, the queue's info should be filled.
2263 case AP_RESPONSE_NORMAL:
2264 case AP_RESPONSE_RESET_IN_PROGRESS:
2265 case AP_RESPONSE_DECONFIGURED:
2266 case AP_RESPONSE_CHECKSTOPPED:
2267 case AP_RESPONSE_BUSY:
2269 * The vfio_ap device driver only
2270 * supports CEX4 and newer adapters, so
2271 * remove the APID if the adapter is
2272 * older than a CEX4.
2274 if (info.at < AP_DEVICE_TYPE_CEX4) {
2275 clear_bit_inv(apid, apm);
2276 apid_cleared = true;
2283 * If we don't know the adapter type,
2284 * clear its APID since it can't be
2285 * determined whether the vfio_ap
2286 * device driver supports it.
2288 clear_bit_inv(apid, apm);
2289 apid_cleared = true;
2294 * If we've already cleared the APID from the apm, there
2295 * is no need to continue examining the remainin AP
2296 * queues to determine the type of the adapter.
2305 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2306 * control domains that have been added to the host's
2307 * AP configuration for each matrix mdev to which they
2310 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2312 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2314 * @adm_add: a bitmap specifying the control domains that have been added to the
2317 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2318 unsigned long *adm_add)
2320 struct ap_matrix_mdev *matrix_mdev;
2322 if (list_empty(&matrix_dev->mdev_list))
2325 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2327 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2328 bitmap_and(matrix_mdev->apm_add,
2329 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2330 bitmap_and(matrix_mdev->aqm_add,
2331 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2332 bitmap_and(matrix_mdev->adm_add,
2333 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2338 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2339 * control domains to the host AP configuration
2340 * by updating the bitmaps that specify what adapters,
2341 * domains and control domains have been added so they
2342 * can be hot plugged into the guest when the AP bus
2343 * scan completes (see vfio_ap_on_scan_complete
2345 * @cur_config_info: the current AP configuration information
2346 * @prev_config_info: the previous AP configuration information
2348 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2349 struct ap_config_info *prev_config_info)
2352 DECLARE_BITMAP(apm_add, AP_DEVICES);
2353 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2354 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2356 do_add = bitmap_andnot(apm_add,
2357 (unsigned long *)cur_config_info->apm,
2358 (unsigned long *)prev_config_info->apm,
2360 do_add |= bitmap_andnot(aqm_add,
2361 (unsigned long *)cur_config_info->aqm,
2362 (unsigned long *)prev_config_info->aqm,
2364 do_add |= bitmap_andnot(adm_add,
2365 (unsigned long *)cur_config_info->adm,
2366 (unsigned long *)prev_config_info->adm,
2370 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2374 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2377 * @cur_cfg_info: the current host AP configuration
2378 * @prev_cfg_info: the previous host AP configuration
2380 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2381 struct ap_config_info *prev_cfg_info)
2383 if (!cur_cfg_info || !prev_cfg_info)
2386 mutex_lock(&matrix_dev->guests_lock);
2388 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2389 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2390 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2392 mutex_unlock(&matrix_dev->guests_lock);
2395 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2397 bool do_hotplug = false;
2398 int filter_domains = 0;
2399 int filter_adapters = 0;
2400 DECLARE_BITMAP(apm, AP_DEVICES);
2401 DECLARE_BITMAP(aqm, AP_DOMAINS);
2403 mutex_lock(&matrix_mdev->kvm->lock);
2404 mutex_lock(&matrix_dev->mdevs_lock);
2406 filter_adapters = bitmap_and(apm, matrix_mdev->matrix.apm,
2407 matrix_mdev->apm_add, AP_DEVICES);
2408 filter_domains = bitmap_and(aqm, matrix_mdev->matrix.aqm,
2409 matrix_mdev->aqm_add, AP_DOMAINS);
2411 if (filter_adapters && filter_domains)
2412 do_hotplug |= vfio_ap_mdev_filter_matrix(apm, aqm, matrix_mdev);
2413 else if (filter_adapters)
2415 vfio_ap_mdev_filter_matrix(apm,
2416 matrix_mdev->shadow_apcb.aqm,
2420 vfio_ap_mdev_filter_matrix(matrix_mdev->shadow_apcb.apm,
2423 if (bitmap_intersects(matrix_mdev->matrix.adm, matrix_mdev->adm_add,
2425 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2428 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2430 mutex_unlock(&matrix_dev->mdevs_lock);
2431 mutex_unlock(&matrix_mdev->kvm->lock);
2434 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2435 struct ap_config_info *old_config_info)
2437 struct ap_matrix_mdev *matrix_mdev;
2439 mutex_lock(&matrix_dev->guests_lock);
2441 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2442 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2443 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2444 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2447 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2448 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2449 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2450 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2453 mutex_unlock(&matrix_dev->guests_lock);