1 // SPDX-License-Identifier: GPL-2.0+
3 * Adjunct processor matrix VFIO device driver callbacks.
5 * Copyright IBM Corp. 2018
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_queue_table *qtable);
36 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
37 static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
38 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
41 * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
42 * KVM guest's APCB in the proper order.
44 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
46 * The proper locking order is:
47 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
49 * 2. kvm->lock: required to update a guest's APCB
50 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
52 * Note: If @kvm is NULL, the KVM lock will not be taken.
54 static inline void get_update_locks_for_kvm(struct kvm *kvm)
56 mutex_lock(&matrix_dev->guests_lock);
58 mutex_lock(&kvm->lock);
59 mutex_lock(&matrix_dev->mdevs_lock);
63 * release_update_locks_for_kvm: Release the locks used to dynamically update a
64 * KVM guest's APCB in the proper order.
66 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
68 * The proper unlocking order is:
69 * 1. matrix_dev->mdevs_lock
71 * 3. matrix_dev->guests_lock
73 * Note: If @kvm is NULL, the KVM lock will not be released.
75 static inline void release_update_locks_for_kvm(struct kvm *kvm)
77 mutex_unlock(&matrix_dev->mdevs_lock);
79 mutex_unlock(&kvm->lock);
80 mutex_unlock(&matrix_dev->guests_lock);
84 * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
85 * KVM guest's APCB in the proper order.
87 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
88 * configuration data to use to update a KVM guest's APCB.
90 * The proper locking order is:
91 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
93 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
94 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
96 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
97 * lock will not be taken.
99 static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
101 mutex_lock(&matrix_dev->guests_lock);
102 if (matrix_mdev && matrix_mdev->kvm)
103 mutex_lock(&matrix_mdev->kvm->lock);
104 mutex_lock(&matrix_dev->mdevs_lock);
108 * release_update_locks_for_mdev: Release the locks used to dynamically update a
109 * KVM guest's APCB in the proper order.
111 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
112 * configuration data to use to update a KVM guest's APCB.
114 * The proper unlocking order is:
115 * 1. matrix_dev->mdevs_lock
116 * 2. matrix_mdev->kvm->lock
117 * 3. matrix_dev->guests_lock
119 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
120 * lock will not be released.
122 static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
124 mutex_unlock(&matrix_dev->mdevs_lock);
125 if (matrix_mdev && matrix_mdev->kvm)
126 mutex_unlock(&matrix_mdev->kvm->lock);
127 mutex_unlock(&matrix_dev->guests_lock);
131 * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
132 * acquire the locks required to update the APCB of
133 * the KVM guest to which the mdev is attached.
135 * @apqn: the APQN of a queue device.
137 * The proper locking order is:
138 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
140 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
141 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
143 * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
146 * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
147 * is not assigned to an ap_matrix_mdev.
149 static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
151 struct ap_matrix_mdev *matrix_mdev;
153 mutex_lock(&matrix_dev->guests_lock);
155 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
156 if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
157 test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
158 if (matrix_mdev->kvm)
159 mutex_lock(&matrix_mdev->kvm->lock);
161 mutex_lock(&matrix_dev->mdevs_lock);
167 mutex_lock(&matrix_dev->mdevs_lock);
173 * get_update_locks_for_queue: get the locks required to update the APCB of the
174 * KVM guest to which the matrix mdev linked to a
175 * vfio_ap_queue object is attached.
177 * @q: a pointer to a vfio_ap_queue object.
179 * The proper locking order is:
180 * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
182 * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB
183 * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev
185 * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
188 static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
190 mutex_lock(&matrix_dev->guests_lock);
191 if (q->matrix_mdev && q->matrix_mdev->kvm)
192 mutex_lock(&q->matrix_mdev->kvm->lock);
193 mutex_lock(&matrix_dev->mdevs_lock);
197 * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
198 * hash table of queues assigned to a matrix mdev
199 * @matrix_mdev: the matrix mdev
200 * @apqn: The APQN of a queue device
202 * Return: the pointer to the vfio_ap_queue struct representing the queue or
203 * NULL if the queue is not assigned to @matrix_mdev
205 static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
206 struct ap_matrix_mdev *matrix_mdev,
209 struct vfio_ap_queue *q;
211 hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
213 if (q && q->apqn == apqn)
221 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
222 * @apqn: The AP Queue number
224 * Checks the IRQ bit for the status of this APQN using ap_tapq.
225 * Returns if the ap_tapq function succeeded and the bit is clear.
226 * Returns if ap_tapq function failed with invalid, deconfigured or
228 * Otherwise retries up to 5 times after waiting 20ms.
230 static void vfio_ap_wait_for_irqclear(int apqn)
232 struct ap_queue_status status;
236 status = ap_tapq(apqn, NULL);
237 switch (status.response_code) {
238 case AP_RESPONSE_NORMAL:
239 case AP_RESPONSE_RESET_IN_PROGRESS:
240 if (!status.irq_enabled)
243 case AP_RESPONSE_BUSY:
246 case AP_RESPONSE_Q_NOT_AVAIL:
247 case AP_RESPONSE_DECONFIGURED:
248 case AP_RESPONSE_CHECKSTOPPED:
250 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
251 status.response_code, apqn);
256 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
257 __func__, status.response_code, apqn);
261 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
262 * @q: The vfio_ap_queue
264 * Unregisters the ISC in the GIB when the saved ISC not invalid.
265 * Unpins the guest's page holding the NIB when it exists.
266 * Resets the saved_iova and saved_isc to invalid values.
268 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
272 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
273 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
274 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
275 q->saved_isc = VFIO_AP_ISC_INVALID;
277 if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
278 vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
284 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
285 * @q: The vfio_ap_queue
287 * Uses ap_aqic to disable the interruption and in case of success, reset
288 * in progress or IRQ disable command already proceeded: calls
289 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
290 * and calls vfio_ap_free_aqic_resources() to free the resources associated
291 * with the AP interrupt handling.
293 * In the case the AP is busy, or a reset is in progress,
294 * retries after 20ms, up to 5 times.
296 * Returns if ap_aqic function failed with invalid, deconfigured or
299 * Return: &struct ap_queue_status
301 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
303 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
304 struct ap_queue_status status;
308 status = ap_aqic(q->apqn, aqic_gisa, 0);
309 switch (status.response_code) {
310 case AP_RESPONSE_OTHERWISE_CHANGED:
311 case AP_RESPONSE_NORMAL:
312 vfio_ap_wait_for_irqclear(q->apqn);
314 case AP_RESPONSE_RESET_IN_PROGRESS:
315 case AP_RESPONSE_BUSY:
318 case AP_RESPONSE_Q_NOT_AVAIL:
319 case AP_RESPONSE_DECONFIGURED:
320 case AP_RESPONSE_CHECKSTOPPED:
321 case AP_RESPONSE_INVALID_ADDRESS:
323 /* All cases in default means AP not operational */
324 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
325 status.response_code);
330 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
331 status.response_code);
333 vfio_ap_free_aqic_resources(q);
338 * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
340 * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
341 * @nib: the location for storing the nib address.
343 * When the PQAP(AQIC) instruction is executed, general register 2 contains the
344 * address of the notification indicator byte (nib) used for IRQ notification.
345 * This function parses and validates the nib from gr2.
347 * Return: returns zero if the nib address is a valid; otherwise, returns
350 static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
352 *nib = vcpu->run->s.regs.gprs[2];
356 if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
362 static int ensure_nib_shared(unsigned long addr, struct gmap *gmap)
367 * The nib has to be located in shared storage since guest and
368 * host access it. vfio_pin_pages() will do a pin shared and
369 * if that fails (possibly because it's not a shared page) it
370 * calls export. We try to do a second pin shared here so that
371 * the UV gives us an error code if we try to pin a non-shared
374 * If the page is already pinned shared the UV will return a success.
376 ret = uv_pin_shared(addr);
378 /* vfio_pin_pages() likely exported the page so let's re-import */
379 gmap_convert_to_secure(gmap, addr);
385 * vfio_ap_irq_enable - Enable Interruption for a APQN
387 * @q: the vfio_ap_queue holding AQIC parameters
388 * @isc: the guest ISC to register with the GIB interface
389 * @vcpu: the vcpu object containing the registers specifying the parameters
390 * passed to the PQAP(AQIC) instruction.
392 * Pin the NIB saved in *q
393 * Register the guest ISC to GIB interface and retrieve the
394 * host ISC to issue the host side PQAP/AQIC
396 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
397 * vfio_pin_pages failed.
399 * Otherwise return the ap_queue_status returned by the ap_aqic(),
400 * all retry handling will be done by the guest.
402 * Return: &struct ap_queue_status
404 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
406 struct kvm_vcpu *vcpu)
408 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
409 struct ap_queue_status status = {};
410 struct kvm_s390_gisa *gisa;
418 /* Verify that the notification indicator byte address is valid */
419 if (vfio_ap_validate_nib(vcpu, &nib)) {
420 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
421 __func__, &nib, q->apqn);
423 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
427 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
428 IOMMU_READ | IOMMU_WRITE, &h_page);
433 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
434 "nib=%pad, apqn=%#04x\n",
435 __func__, ret, &nib, q->apqn);
437 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
441 kvm = q->matrix_mdev->kvm;
442 gisa = kvm->arch.gisa_int.origin;
444 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
445 aqic_gisa.gisc = isc;
447 /* NIB in non-shared storage is a rc 6 for PV guests */
448 if (kvm_s390_pv_cpu_is_protected(vcpu) &&
449 ensure_nib_shared(h_nib & PAGE_MASK, kvm->arch.gmap)) {
450 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
451 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
455 nisc = kvm_s390_gisc_register(kvm, isc);
457 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
458 __func__, nisc, isc, q->apqn);
460 status.response_code = AP_RESPONSE_INVALID_GISA;
464 aqic_gisa.isc = nisc;
466 aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
468 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
469 switch (status.response_code) {
470 case AP_RESPONSE_NORMAL:
471 /* See if we did clear older IRQ configuration */
472 vfio_ap_free_aqic_resources(q);
476 case AP_RESPONSE_OTHERWISE_CHANGED:
477 /* We could not modify IRQ settings: clear new configuration */
478 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
479 kvm_s390_gisc_unregister(kvm, isc);
482 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
483 status.response_code);
484 vfio_ap_irq_disable(q);
488 if (status.response_code != AP_RESPONSE_NORMAL) {
489 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
490 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
491 "gisa=%#x, isc=%#x, apqn=%#04x\n",
492 __func__, status.response_code,
493 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
494 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
502 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
503 * of big endian elements that can be passed by
504 * value to an s390dbf sprintf event function to
505 * format a UUID string.
507 * @guid: the object containing the little endian guid
508 * @uuid: a six-element array of long values that can be passed by value as
509 * arguments for a formatting string specifying a UUID.
511 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
512 * event functions if the memory for the passed string is available as long as
513 * the debug feature exists. Since a mediated device can be removed at any
514 * time, it's name can not be used because %s passes the reference to the string
515 * in memory and the reference will go stale once the device is removed .
517 * The s390dbf string formatting function allows a maximum of 9 arguments for a
518 * message to be displayed in the 'sprintf' view. In order to use the bytes
519 * comprising the mediated device's UUID to display the mediated device name,
520 * they will have to be converted into an array whose elements can be passed by
521 * value to sprintf. For example:
523 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
524 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
525 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
526 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
528 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
531 * The input guid is ordered in little endian, so it needs to be
532 * reordered for displaying a UUID as a string. This specifies the
533 * guid indices in proper order.
535 uuid[0] = le32_to_cpup((__le32 *)guid);
536 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
537 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
538 uuid[3] = *((__u16 *)&guid->b[8]);
539 uuid[4] = *((__u16 *)&guid->b[10]);
540 uuid[5] = *((__u32 *)&guid->b[12]);
544 * handle_pqap - PQAP instruction callback
546 * @vcpu: The vcpu on which we received the PQAP instruction
548 * Get the general register contents to initialize internal variables.
553 * Response.status may be set to following Response Code:
554 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
555 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
556 * - AP_RESPONSE_NORMAL (0) : in case of success
557 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
558 * We take the matrix_dev lock to ensure serialization on queues and
559 * mediated device access.
561 * Return: 0 if we could handle the request inside KVM.
562 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
564 static int handle_pqap(struct kvm_vcpu *vcpu)
568 unsigned long uuid[6];
569 struct vfio_ap_queue *q;
570 struct ap_queue_status qstatus = {
571 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
572 struct ap_matrix_mdev *matrix_mdev;
574 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
576 /* If we do not use the AIV facility just go to userland */
577 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
578 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
579 __func__, apqn, vcpu->arch.sie_block->eca);
584 mutex_lock(&matrix_dev->mdevs_lock);
586 if (!vcpu->kvm->arch.crypto.pqap_hook) {
587 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
593 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
594 struct ap_matrix_mdev, pqap_hook);
596 /* If the there is no guest using the mdev, there is nothing to do */
597 if (!matrix_mdev->kvm) {
598 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
599 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
600 __func__, uuid[0], uuid[1], uuid[2],
601 uuid[3], uuid[4], uuid[5], apqn);
605 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
607 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
608 __func__, AP_QID_CARD(apqn),
613 status = vcpu->run->s.regs.gprs[1];
615 /* If IR bit(16) is set we enable the interrupt */
616 if ((status >> (63 - 16)) & 0x01)
617 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
619 qstatus = vfio_ap_irq_disable(q);
622 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
623 vcpu->run->s.regs.gprs[1] >>= 32;
624 mutex_unlock(&matrix_dev->mdevs_lock);
628 static void vfio_ap_matrix_init(struct ap_config_info *info,
629 struct ap_matrix *matrix)
631 matrix->apm_max = info->apxa ? info->na : 63;
632 matrix->aqm_max = info->apxa ? info->nd : 15;
633 matrix->adm_max = info->apxa ? info->nd : 15;
636 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
638 if (matrix_mdev->kvm)
639 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
640 matrix_mdev->shadow_apcb.apm,
641 matrix_mdev->shadow_apcb.aqm,
642 matrix_mdev->shadow_apcb.adm);
645 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
647 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
649 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
650 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
651 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
653 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
658 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
659 * to ensure no queue devices are passed through to
660 * the guest that are not bound to the vfio_ap
663 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
665 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
666 * driver, its APID will be filtered from the guest's APCB. The matrix
667 * structure precludes filtering an individual APQN, so its APID will be
670 * Return: a boolean value indicating whether the KVM guest's APCB was changed
671 * by the filtering or not.
673 static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm,
674 struct ap_matrix_mdev *matrix_mdev)
676 unsigned long apid, apqi, apqn;
677 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
678 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
679 struct vfio_ap_queue *q;
681 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
682 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
683 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
686 * Copy the adapters, domains and control domains to the shadow_apcb
687 * from the matrix mdev, but only those that are assigned to the host's
690 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
691 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
692 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
693 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
695 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
696 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
698 * If the APQN is not bound to the vfio_ap device
699 * driver, then we can't assign it to the guest's
700 * AP configuration. The AP architecture won't
701 * allow filtering of a single APQN, so let's filter
702 * the APID since an adapter represents a physical
705 apqn = AP_MKQID(apid, apqi);
706 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
707 if (!q || q->reset_status.response_code) {
709 matrix_mdev->shadow_apcb.apm);
715 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
717 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
721 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
723 struct ap_matrix_mdev *matrix_mdev =
724 container_of(vdev, struct ap_matrix_mdev, vdev);
726 matrix_mdev->mdev = to_mdev_device(vdev->dev);
727 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
728 matrix_mdev->pqap_hook = handle_pqap;
729 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
730 hash_init(matrix_mdev->qtable.queues);
735 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
737 struct ap_matrix_mdev *matrix_mdev;
740 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
741 &vfio_ap_matrix_dev_ops);
742 if (IS_ERR(matrix_mdev))
743 return PTR_ERR(matrix_mdev);
745 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
748 matrix_mdev->req_trigger = NULL;
749 dev_set_drvdata(&mdev->dev, matrix_mdev);
750 mutex_lock(&matrix_dev->mdevs_lock);
751 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
752 mutex_unlock(&matrix_dev->mdevs_lock);
756 vfio_put_device(&matrix_mdev->vdev);
760 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
761 struct vfio_ap_queue *q)
764 q->matrix_mdev = matrix_mdev;
765 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
769 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
771 struct vfio_ap_queue *q;
773 q = vfio_ap_find_queue(apqn);
774 vfio_ap_mdev_link_queue(matrix_mdev, q);
777 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
779 hash_del(&q->mdev_qnode);
782 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
784 q->matrix_mdev = NULL;
787 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
789 struct vfio_ap_queue *q;
790 unsigned long apid, apqi;
792 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
793 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
795 q = vfio_ap_mdev_get_queue(matrix_mdev,
796 AP_MKQID(apid, apqi));
798 q->matrix_mdev = NULL;
803 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
805 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
807 vfio_unregister_group_dev(&matrix_mdev->vdev);
809 mutex_lock(&matrix_dev->guests_lock);
810 mutex_lock(&matrix_dev->mdevs_lock);
811 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
812 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
813 list_del(&matrix_mdev->node);
814 mutex_unlock(&matrix_dev->mdevs_lock);
815 mutex_unlock(&matrix_dev->guests_lock);
816 vfio_put_device(&matrix_mdev->vdev);
819 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
820 "already assigned to %s"
822 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
826 unsigned long apid, apqi;
827 const struct device *dev = mdev_dev(matrix_mdev->mdev);
828 const char *mdev_name = dev_name(dev);
830 for_each_set_bit_inv(apid, apm, AP_DEVICES)
831 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
832 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
836 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
838 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
839 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
841 * Verifies that each APQN derived from the Cartesian product of a bitmap of
842 * AP adapter IDs and AP queue indexes is not configured for any matrix
843 * mediated device. AP queue sharing is not allowed.
845 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
847 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
848 unsigned long *mdev_aqm)
850 struct ap_matrix_mdev *matrix_mdev;
851 DECLARE_BITMAP(apm, AP_DEVICES);
852 DECLARE_BITMAP(aqm, AP_DOMAINS);
854 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
856 * If the input apm and aqm are fields of the matrix_mdev
857 * object, then move on to the next matrix_mdev.
859 if (mdev_apm == matrix_mdev->matrix.apm &&
860 mdev_aqm == matrix_mdev->matrix.aqm)
863 memset(apm, 0, sizeof(apm));
864 memset(aqm, 0, sizeof(aqm));
867 * We work on full longs, as we can only exclude the leftover
868 * bits in non-inverse order. The leftover is all zeros.
870 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
874 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
878 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
887 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
888 * not reserved for the default zcrypt driver and
889 * are not assigned to another mdev.
891 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
893 * Return: One of the following values:
894 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
895 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
896 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
897 * zcrypt default driver.
898 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
899 * o A zero indicating validation succeeded.
901 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
903 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
904 matrix_mdev->matrix.aqm))
905 return -EADDRNOTAVAIL;
907 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
908 matrix_mdev->matrix.aqm);
911 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
916 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
917 vfio_ap_mdev_link_apqn(matrix_mdev,
918 AP_MKQID(apid, apqi));
922 * assign_adapter_store - parses the APID from @buf and sets the
923 * corresponding bit in the mediated matrix device's APM
925 * @dev: the matrix device
926 * @attr: the mediated matrix device's assign_adapter attribute
927 * @buf: a buffer containing the AP adapter number (APID) to
929 * @count: the number of bytes in @buf
931 * Return: the number of bytes processed if the APID is valid; otherwise,
932 * returns one of the following errors:
935 * The APID is not a valid number
938 * The APID exceeds the maximum value configured for the system
941 * An APQN derived from the cross product of the APID being assigned
942 * and the APQIs previously assigned is not bound to the vfio_ap device
943 * driver; or, if no APQIs have yet been assigned, the APID is not
944 * contained in an APQN bound to the vfio_ap device driver.
947 * An APQN derived from the cross product of the APID being assigned
948 * and the APQIs previously assigned is being used by another mediated
952 * A lock required to validate the mdev's AP configuration could not
955 static ssize_t assign_adapter_store(struct device *dev,
956 struct device_attribute *attr,
957 const char *buf, size_t count)
961 DECLARE_BITMAP(apm_delta, AP_DEVICES);
962 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
964 mutex_lock(&ap_perms_mutex);
965 get_update_locks_for_mdev(matrix_mdev);
967 ret = kstrtoul(buf, 0, &apid);
971 if (apid > matrix_mdev->matrix.apm_max) {
976 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
981 set_bit_inv(apid, matrix_mdev->matrix.apm);
983 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
985 clear_bit_inv(apid, matrix_mdev->matrix.apm);
989 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
990 memset(apm_delta, 0, sizeof(apm_delta));
991 set_bit_inv(apid, apm_delta);
993 if (vfio_ap_mdev_filter_matrix(apm_delta,
994 matrix_mdev->matrix.aqm, matrix_mdev))
995 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
999 release_update_locks_for_mdev(matrix_mdev);
1000 mutex_unlock(&ap_perms_mutex);
1004 static DEVICE_ATTR_WO(assign_adapter);
1006 static struct vfio_ap_queue
1007 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
1008 unsigned long apid, unsigned long apqi)
1010 struct vfio_ap_queue *q = NULL;
1012 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1013 /* If the queue is assigned to the matrix mdev, unlink it. */
1015 vfio_ap_unlink_queue_fr_mdev(q);
1021 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
1022 * adapter from the matrix mdev to which the
1023 * adapter was assigned.
1024 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
1025 * @apid: the APID of the unassigned adapter.
1026 * @qtable: table for storing queues associated with unassigned adapter.
1028 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1030 struct ap_queue_table *qtable)
1033 struct vfio_ap_queue *q;
1035 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1036 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1039 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1040 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1041 hash_add(qtable->queues, &q->mdev_qnode,
1047 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1051 struct vfio_ap_queue *q;
1052 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1054 hash_init(qtable->queues);
1055 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, qtable);
1057 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1058 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1059 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1062 vfio_ap_mdev_reset_queues(qtable);
1064 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1065 vfio_ap_unlink_mdev_fr_queue(q);
1066 hash_del(&q->mdev_qnode);
1073 * unassign_adapter_store - parses the APID from @buf and clears the
1074 * corresponding bit in the mediated matrix device's APM
1076 * @dev: the matrix device
1077 * @attr: the mediated matrix device's unassign_adapter attribute
1078 * @buf: a buffer containing the adapter number (APID) to be unassigned
1079 * @count: the number of bytes in @buf
1081 * Return: the number of bytes processed if the APID is valid; otherwise,
1082 * returns one of the following errors:
1083 * -EINVAL if the APID is not a number
1084 * -ENODEV if the APID it exceeds the maximum value configured for the
1087 static ssize_t unassign_adapter_store(struct device *dev,
1088 struct device_attribute *attr,
1089 const char *buf, size_t count)
1093 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1095 get_update_locks_for_mdev(matrix_mdev);
1097 ret = kstrtoul(buf, 0, &apid);
1101 if (apid > matrix_mdev->matrix.apm_max) {
1106 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1111 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1112 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1115 release_update_locks_for_mdev(matrix_mdev);
1118 static DEVICE_ATTR_WO(unassign_adapter);
1120 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1125 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1126 vfio_ap_mdev_link_apqn(matrix_mdev,
1127 AP_MKQID(apid, apqi));
1131 * assign_domain_store - parses the APQI from @buf and sets the
1132 * corresponding bit in the mediated matrix device's AQM
1134 * @dev: the matrix device
1135 * @attr: the mediated matrix device's assign_domain attribute
1136 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1138 * @count: the number of bytes in @buf
1140 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1141 * one of the following errors:
1144 * The APQI is not a valid number
1147 * The APQI exceeds the maximum value configured for the system
1150 * An APQN derived from the cross product of the APQI being assigned
1151 * and the APIDs previously assigned is not bound to the vfio_ap device
1152 * driver; or, if no APIDs have yet been assigned, the APQI is not
1153 * contained in an APQN bound to the vfio_ap device driver.
1156 * An APQN derived from the cross product of the APQI being assigned
1157 * and the APIDs previously assigned is being used by another mediated
1161 * The lock required to validate the mdev's AP configuration could not
1164 static ssize_t assign_domain_store(struct device *dev,
1165 struct device_attribute *attr,
1166 const char *buf, size_t count)
1170 DECLARE_BITMAP(aqm_delta, AP_DOMAINS);
1171 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1173 mutex_lock(&ap_perms_mutex);
1174 get_update_locks_for_mdev(matrix_mdev);
1176 ret = kstrtoul(buf, 0, &apqi);
1180 if (apqi > matrix_mdev->matrix.aqm_max) {
1185 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1190 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1192 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1194 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1198 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1199 memset(aqm_delta, 0, sizeof(aqm_delta));
1200 set_bit_inv(apqi, aqm_delta);
1202 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, aqm_delta,
1204 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1208 release_update_locks_for_mdev(matrix_mdev);
1209 mutex_unlock(&ap_perms_mutex);
1213 static DEVICE_ATTR_WO(assign_domain);
1215 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1217 struct ap_queue_table *qtable)
1220 struct vfio_ap_queue *q;
1222 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1223 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1226 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1227 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1228 hash_add(qtable->queues, &q->mdev_qnode,
1234 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1238 struct vfio_ap_queue *q;
1239 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1241 hash_init(qtable->queues);
1242 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, qtable);
1244 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1245 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1246 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1249 vfio_ap_mdev_reset_queues(qtable);
1251 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1252 vfio_ap_unlink_mdev_fr_queue(q);
1253 hash_del(&q->mdev_qnode);
1260 * unassign_domain_store - parses the APQI from @buf and clears the
1261 * corresponding bit in the mediated matrix device's AQM
1263 * @dev: the matrix device
1264 * @attr: the mediated matrix device's unassign_domain attribute
1265 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1267 * @count: the number of bytes in @buf
1269 * Return: the number of bytes processed if the APQI is valid; otherwise,
1270 * returns one of the following errors:
1271 * -EINVAL if the APQI is not a number
1272 * -ENODEV if the APQI exceeds the maximum value configured for the system
1274 static ssize_t unassign_domain_store(struct device *dev,
1275 struct device_attribute *attr,
1276 const char *buf, size_t count)
1280 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1282 get_update_locks_for_mdev(matrix_mdev);
1284 ret = kstrtoul(buf, 0, &apqi);
1288 if (apqi > matrix_mdev->matrix.aqm_max) {
1293 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1298 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1299 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1303 release_update_locks_for_mdev(matrix_mdev);
1306 static DEVICE_ATTR_WO(unassign_domain);
1309 * assign_control_domain_store - parses the domain ID from @buf and sets
1310 * the corresponding bit in the mediated matrix device's ADM
1312 * @dev: the matrix device
1313 * @attr: the mediated matrix device's assign_control_domain attribute
1314 * @buf: a buffer containing the domain ID to be assigned
1315 * @count: the number of bytes in @buf
1317 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1318 * returns one of the following errors:
1319 * -EINVAL if the ID is not a number
1320 * -ENODEV if the ID exceeds the maximum value configured for the system
1322 static ssize_t assign_control_domain_store(struct device *dev,
1323 struct device_attribute *attr,
1324 const char *buf, size_t count)
1328 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1330 get_update_locks_for_mdev(matrix_mdev);
1332 ret = kstrtoul(buf, 0, &id);
1336 if (id > matrix_mdev->matrix.adm_max) {
1341 if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1346 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1347 * domain number (id). The bits in the mask, from most significant to
1348 * least significant, correspond to IDs 0 up to the one less than the
1349 * number of control domains that can be assigned.
1351 set_bit_inv(id, matrix_mdev->matrix.adm);
1352 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1353 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1357 release_update_locks_for_mdev(matrix_mdev);
1360 static DEVICE_ATTR_WO(assign_control_domain);
1363 * unassign_control_domain_store - parses the domain ID from @buf and
1364 * clears the corresponding bit in the mediated matrix device's ADM
1366 * @dev: the matrix device
1367 * @attr: the mediated matrix device's unassign_control_domain attribute
1368 * @buf: a buffer containing the domain ID to be unassigned
1369 * @count: the number of bytes in @buf
1371 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1372 * returns one of the following errors:
1373 * -EINVAL if the ID is not a number
1374 * -ENODEV if the ID exceeds the maximum value configured for the system
1376 static ssize_t unassign_control_domain_store(struct device *dev,
1377 struct device_attribute *attr,
1378 const char *buf, size_t count)
1381 unsigned long domid;
1382 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1384 get_update_locks_for_mdev(matrix_mdev);
1386 ret = kstrtoul(buf, 0, &domid);
1390 if (domid > matrix_mdev->matrix.adm_max) {
1395 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1400 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1402 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1403 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1404 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1409 release_update_locks_for_mdev(matrix_mdev);
1412 static DEVICE_ATTR_WO(unassign_control_domain);
1414 static ssize_t control_domains_show(struct device *dev,
1415 struct device_attribute *dev_attr,
1422 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1423 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1425 mutex_lock(&matrix_dev->mdevs_lock);
1426 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1427 n = sprintf(bufpos, "%04lx\n", id);
1431 mutex_unlock(&matrix_dev->mdevs_lock);
1435 static DEVICE_ATTR_RO(control_domains);
1437 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1442 unsigned long apid1;
1443 unsigned long apqi1;
1444 unsigned long napm_bits = matrix->apm_max + 1;
1445 unsigned long naqm_bits = matrix->aqm_max + 1;
1449 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1450 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1452 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1453 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1454 for_each_set_bit_inv(apqi, matrix->aqm,
1456 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1462 } else if (apid1 < napm_bits) {
1463 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1464 n = sprintf(bufpos, "%02lx.\n", apid);
1468 } else if (apqi1 < naqm_bits) {
1469 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1470 n = sprintf(bufpos, ".%04lx\n", apqi);
1479 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1483 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1485 mutex_lock(&matrix_dev->mdevs_lock);
1486 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1487 mutex_unlock(&matrix_dev->mdevs_lock);
1491 static DEVICE_ATTR_RO(matrix);
1493 static ssize_t guest_matrix_show(struct device *dev,
1494 struct device_attribute *attr, char *buf)
1497 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1499 mutex_lock(&matrix_dev->mdevs_lock);
1500 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1501 mutex_unlock(&matrix_dev->mdevs_lock);
1505 static DEVICE_ATTR_RO(guest_matrix);
1507 static struct attribute *vfio_ap_mdev_attrs[] = {
1508 &dev_attr_assign_adapter.attr,
1509 &dev_attr_unassign_adapter.attr,
1510 &dev_attr_assign_domain.attr,
1511 &dev_attr_unassign_domain.attr,
1512 &dev_attr_assign_control_domain.attr,
1513 &dev_attr_unassign_control_domain.attr,
1514 &dev_attr_control_domains.attr,
1515 &dev_attr_matrix.attr,
1516 &dev_attr_guest_matrix.attr,
1520 static struct attribute_group vfio_ap_mdev_attr_group = {
1521 .attrs = vfio_ap_mdev_attrs
1524 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1525 &vfio_ap_mdev_attr_group,
1530 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1531 * to manage AP resources for the guest whose state is represented by @kvm
1533 * @matrix_mdev: a mediated matrix device
1534 * @kvm: reference to KVM instance
1536 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1537 * otherwise, returns an -EPERM.
1539 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1542 struct ap_matrix_mdev *m;
1544 if (kvm->arch.crypto.crycbd) {
1545 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1546 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1547 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1549 get_update_locks_for_kvm(kvm);
1551 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1552 if (m != matrix_mdev && m->kvm == kvm) {
1553 release_update_locks_for_kvm(kvm);
1559 matrix_mdev->kvm = kvm;
1560 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1562 release_update_locks_for_kvm(kvm);
1568 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1570 struct ap_queue_table *qtable = &matrix_mdev->qtable;
1571 struct vfio_ap_queue *q;
1574 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1575 if (q->saved_iova >= iova && q->saved_iova < iova + length)
1576 vfio_ap_irq_disable(q);
1580 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1583 struct ap_matrix_mdev *matrix_mdev =
1584 container_of(vdev, struct ap_matrix_mdev, vdev);
1586 mutex_lock(&matrix_dev->mdevs_lock);
1588 unmap_iova(matrix_mdev, iova, length);
1590 mutex_unlock(&matrix_dev->mdevs_lock);
1594 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1597 * @matrix_mdev: a matrix mediated device
1599 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1601 struct kvm *kvm = matrix_mdev->kvm;
1603 if (kvm && kvm->arch.crypto.crycbd) {
1604 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1605 kvm->arch.crypto.pqap_hook = NULL;
1606 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1608 get_update_locks_for_kvm(kvm);
1610 kvm_arch_crypto_clear_masks(kvm);
1611 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1613 matrix_mdev->kvm = NULL;
1615 release_update_locks_for_kvm(kvm);
1619 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1621 struct ap_queue *queue;
1622 struct vfio_ap_queue *q = NULL;
1624 queue = ap_get_qdev(apqn);
1628 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1629 q = dev_get_drvdata(&queue->ap_dev.device);
1631 put_device(&queue->ap_dev.device);
1636 static int apq_status_check(int apqn, struct ap_queue_status *status)
1638 switch (status->response_code) {
1639 case AP_RESPONSE_NORMAL:
1640 case AP_RESPONSE_DECONFIGURED:
1642 case AP_RESPONSE_RESET_IN_PROGRESS:
1643 case AP_RESPONSE_BUSY:
1645 case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
1646 case AP_RESPONSE_ASSOC_FAILED:
1648 * These asynchronous response codes indicate a PQAP(AAPQ)
1649 * instruction to associate a secret with the guest failed. All
1650 * subsequent AP instructions will end with the asynchronous
1651 * response code until the AP queue is reset; so, let's return
1652 * a value indicating a reset needs to be performed again.
1657 "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1658 AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1659 status->response_code);
1664 #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
1666 static void apq_reset_check(struct work_struct *reset_work)
1668 int ret = -EBUSY, elapsed = 0;
1669 struct ap_queue_status status;
1670 struct vfio_ap_queue *q;
1672 q = container_of(reset_work, struct vfio_ap_queue, reset_work);
1673 memcpy(&status, &q->reset_status, sizeof(status));
1675 msleep(AP_RESET_INTERVAL);
1676 elapsed += AP_RESET_INTERVAL;
1677 status = ap_tapq(q->apqn, NULL);
1678 ret = apq_status_check(q->apqn, &status);
1681 if (ret == -EBUSY) {
1682 pr_notice_ratelimited(WAIT_MSG, elapsed,
1683 AP_QID_CARD(q->apqn),
1684 AP_QID_QUEUE(q->apqn),
1685 status.response_code,
1687 status.irq_enabled);
1689 if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
1690 q->reset_status.response_code == AP_RESPONSE_BUSY ||
1691 q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
1693 status = ap_zapq(q->apqn, 0);
1694 memcpy(&q->reset_status, &status, sizeof(status));
1698 * When an AP adapter is deconfigured, the
1699 * associated queues are reset, so let's set the
1700 * status response code to 0 so the queue may be
1701 * passed through (i.e., not filtered)
1703 if (status.response_code == AP_RESPONSE_DECONFIGURED)
1704 q->reset_status.response_code = 0;
1705 if (q->saved_isc != VFIO_AP_ISC_INVALID)
1706 vfio_ap_free_aqic_resources(q);
1712 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1714 struct ap_queue_status status;
1718 status = ap_zapq(q->apqn, 0);
1719 memcpy(&q->reset_status, &status, sizeof(status));
1720 switch (status.response_code) {
1721 case AP_RESPONSE_NORMAL:
1722 case AP_RESPONSE_RESET_IN_PROGRESS:
1723 case AP_RESPONSE_BUSY:
1724 case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
1726 * Let's verify whether the ZAPQ completed successfully on a work queue.
1728 queue_work(system_long_wq, &q->reset_work);
1730 case AP_RESPONSE_DECONFIGURED:
1732 * When an AP adapter is deconfigured, the associated
1733 * queues are reset, so let's set the status response code to 0
1734 * so the queue may be passed through (i.e., not filtered).
1736 q->reset_status.response_code = 0;
1737 vfio_ap_free_aqic_resources(q);
1741 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1742 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1743 status.response_code);
1747 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable)
1749 int ret = 0, loop_cursor;
1750 struct vfio_ap_queue *q;
1752 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode)
1753 vfio_ap_mdev_reset_queue(q);
1755 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1756 flush_work(&q->reset_work);
1758 if (q->reset_status.response_code)
1765 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1767 struct ap_matrix_mdev *matrix_mdev =
1768 container_of(vdev, struct ap_matrix_mdev, vdev);
1773 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
1776 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1778 struct ap_matrix_mdev *matrix_mdev =
1779 container_of(vdev, struct ap_matrix_mdev, vdev);
1781 vfio_ap_mdev_unset_kvm(matrix_mdev);
1784 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
1786 struct device *dev = vdev->dev;
1787 struct ap_matrix_mdev *matrix_mdev;
1789 matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
1791 if (matrix_mdev->req_trigger) {
1793 dev_notice_ratelimited(dev,
1794 "Relaying device request to user (#%u)\n",
1797 eventfd_signal(matrix_mdev->req_trigger, 1);
1798 } else if (count == 0) {
1800 "No device request registered, blocked until released by user\n");
1804 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1806 unsigned long minsz;
1807 struct vfio_device_info info;
1809 minsz = offsetofend(struct vfio_device_info, num_irqs);
1811 if (copy_from_user(&info, (void __user *)arg, minsz))
1814 if (info.argsz < minsz)
1817 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1818 info.num_regions = 0;
1819 info.num_irqs = VFIO_AP_NUM_IRQS;
1821 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1824 static ssize_t vfio_ap_get_irq_info(unsigned long arg)
1826 unsigned long minsz;
1827 struct vfio_irq_info info;
1829 minsz = offsetofend(struct vfio_irq_info, count);
1831 if (copy_from_user(&info, (void __user *)arg, minsz))
1834 if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
1837 switch (info.index) {
1838 case VFIO_AP_REQ_IRQ_INDEX:
1840 info.flags = VFIO_IRQ_INFO_EVENTFD;
1846 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1849 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
1853 unsigned long minsz;
1855 minsz = offsetofend(struct vfio_irq_set, count);
1857 if (copy_from_user(irq_set, (void __user *)arg, minsz))
1860 ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
1865 if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
1871 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
1876 unsigned long minsz;
1877 struct eventfd_ctx *req_trigger;
1879 minsz = offsetofend(struct vfio_irq_set, count);
1880 data = (void __user *)(arg + minsz);
1882 if (get_user(fd, (s32 __user *)data))
1886 if (matrix_mdev->req_trigger)
1887 eventfd_ctx_put(matrix_mdev->req_trigger);
1888 matrix_mdev->req_trigger = NULL;
1889 } else if (fd >= 0) {
1890 req_trigger = eventfd_ctx_fdget(fd);
1891 if (IS_ERR(req_trigger))
1892 return PTR_ERR(req_trigger);
1894 if (matrix_mdev->req_trigger)
1895 eventfd_ctx_put(matrix_mdev->req_trigger);
1897 matrix_mdev->req_trigger = req_trigger;
1905 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
1909 struct vfio_irq_set irq_set;
1911 ret = vfio_ap_irq_set_init(&irq_set, arg);
1915 switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
1916 case VFIO_IRQ_SET_DATA_EVENTFD:
1917 switch (irq_set.index) {
1918 case VFIO_AP_REQ_IRQ_INDEX:
1919 return vfio_ap_set_request_irq(matrix_mdev, arg);
1928 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1929 unsigned int cmd, unsigned long arg)
1931 struct ap_matrix_mdev *matrix_mdev =
1932 container_of(vdev, struct ap_matrix_mdev, vdev);
1935 mutex_lock(&matrix_dev->mdevs_lock);
1937 case VFIO_DEVICE_GET_INFO:
1938 ret = vfio_ap_mdev_get_device_info(arg);
1940 case VFIO_DEVICE_RESET:
1941 ret = vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1943 case VFIO_DEVICE_GET_IRQ_INFO:
1944 ret = vfio_ap_get_irq_info(arg);
1946 case VFIO_DEVICE_SET_IRQS:
1947 ret = vfio_ap_set_irqs(matrix_mdev, arg);
1953 mutex_unlock(&matrix_dev->mdevs_lock);
1958 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
1960 struct ap_matrix_mdev *matrix_mdev;
1961 unsigned long apid = AP_QID_CARD(q->apqn);
1962 unsigned long apqi = AP_QID_QUEUE(q->apqn);
1964 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
1965 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
1966 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
1973 static ssize_t status_show(struct device *dev,
1974 struct device_attribute *attr,
1978 struct vfio_ap_queue *q;
1979 struct ap_matrix_mdev *matrix_mdev;
1980 struct ap_device *apdev = to_ap_dev(dev);
1982 mutex_lock(&matrix_dev->mdevs_lock);
1983 q = dev_get_drvdata(&apdev->device);
1984 matrix_mdev = vfio_ap_mdev_for_queue(q);
1987 if (matrix_mdev->kvm)
1988 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1991 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1994 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1995 AP_QUEUE_UNASSIGNED);
1998 mutex_unlock(&matrix_dev->mdevs_lock);
2003 static DEVICE_ATTR_RO(status);
2005 static struct attribute *vfio_queue_attrs[] = {
2006 &dev_attr_status.attr,
2010 static const struct attribute_group vfio_queue_attr_group = {
2011 .attrs = vfio_queue_attrs,
2014 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
2015 .init = vfio_ap_mdev_init_dev,
2016 .open_device = vfio_ap_mdev_open_device,
2017 .close_device = vfio_ap_mdev_close_device,
2018 .ioctl = vfio_ap_mdev_ioctl,
2019 .dma_unmap = vfio_ap_mdev_dma_unmap,
2020 .bind_iommufd = vfio_iommufd_emulated_bind,
2021 .unbind_iommufd = vfio_iommufd_emulated_unbind,
2022 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
2023 .detach_ioas = vfio_iommufd_emulated_detach_ioas,
2024 .request = vfio_ap_mdev_request
2027 static struct mdev_driver vfio_ap_matrix_driver = {
2028 .device_api = VFIO_DEVICE_API_AP_STRING,
2029 .max_instances = MAX_ZDEV_ENTRIES_EXT,
2031 .name = "vfio_ap_mdev",
2032 .owner = THIS_MODULE,
2033 .mod_name = KBUILD_MODNAME,
2034 .dev_groups = vfio_ap_mdev_attr_groups,
2036 .probe = vfio_ap_mdev_probe,
2037 .remove = vfio_ap_mdev_remove,
2040 int vfio_ap_mdev_register(void)
2044 ret = mdev_register_driver(&vfio_ap_matrix_driver);
2048 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
2049 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
2050 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
2051 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2052 &vfio_ap_matrix_driver,
2053 matrix_dev->mdev_types, 1);
2059 mdev_unregister_driver(&vfio_ap_matrix_driver);
2063 void vfio_ap_mdev_unregister(void)
2065 mdev_unregister_parent(&matrix_dev->parent);
2066 mdev_unregister_driver(&vfio_ap_matrix_driver);
2069 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2072 struct vfio_ap_queue *q;
2073 struct ap_matrix_mdev *matrix_mdev;
2075 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2079 q = kzalloc(sizeof(*q), GFP_KERNEL);
2082 goto err_remove_group;
2085 q->apqn = to_ap_queue(&apdev->device)->qid;
2086 q->saved_isc = VFIO_AP_ISC_INVALID;
2087 memset(&q->reset_status, 0, sizeof(q->reset_status));
2088 INIT_WORK(&q->reset_work, apq_reset_check);
2089 matrix_mdev = get_update_locks_by_apqn(q->apqn);
2092 vfio_ap_mdev_link_queue(matrix_mdev, q);
2094 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm,
2095 matrix_mdev->matrix.aqm,
2097 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2099 dev_set_drvdata(&apdev->device, q);
2100 release_update_locks_for_mdev(matrix_mdev);
2105 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2109 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2111 unsigned long apid, apqi;
2112 struct vfio_ap_queue *q;
2113 struct ap_matrix_mdev *matrix_mdev;
2115 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2116 q = dev_get_drvdata(&apdev->device);
2117 get_update_locks_for_queue(q);
2118 matrix_mdev = q->matrix_mdev;
2121 vfio_ap_unlink_queue_fr_mdev(q);
2123 apid = AP_QID_CARD(q->apqn);
2124 apqi = AP_QID_QUEUE(q->apqn);
2127 * If the queue is assigned to the guest's APCB, then remove
2128 * the adapter's APID from the APCB and hot it into the guest.
2130 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2131 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2132 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2133 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2137 vfio_ap_mdev_reset_queue(q);
2138 flush_work(&q->reset_work);
2139 dev_set_drvdata(&apdev->device, NULL);
2141 release_update_locks_for_mdev(matrix_mdev);
2145 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2146 * assigned to a mediated device under the control
2147 * of the vfio_ap device driver.
2149 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2150 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2153 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2154 * assigned to a mediated device under the control of the vfio_ap
2156 * * Otherwise, return 0.
2158 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2162 mutex_lock(&matrix_dev->guests_lock);
2163 mutex_lock(&matrix_dev->mdevs_lock);
2164 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2165 mutex_unlock(&matrix_dev->mdevs_lock);
2166 mutex_unlock(&matrix_dev->guests_lock);
2172 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2173 * domains that have been removed from the host's
2174 * AP configuration from a guest.
2176 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2177 * @aprem: the adapters that have been removed from the host's AP configuration
2178 * @aqrem: the domains that have been removed from the host's AP configuration
2179 * @cdrem: the control domains that have been removed from the host's AP
2182 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2183 unsigned long *aprem,
2184 unsigned long *aqrem,
2185 unsigned long *cdrem)
2189 if (!bitmap_empty(aprem, AP_DEVICES)) {
2190 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2191 matrix_mdev->shadow_apcb.apm,
2195 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2196 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2197 matrix_mdev->shadow_apcb.aqm,
2201 if (!bitmap_empty(cdrem, AP_DOMAINS))
2202 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2203 matrix_mdev->shadow_apcb.adm,
2207 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2211 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2212 * domains and control domains that have been removed
2213 * from the host AP configuration and unplugs them
2214 * from those guests.
2216 * @ap_remove: bitmap specifying which adapters have been removed from the host
2218 * @aq_remove: bitmap specifying which domains have been removed from the host
2220 * @cd_remove: bitmap specifying which control domains have been removed from
2223 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2224 unsigned long *aq_remove,
2225 unsigned long *cd_remove)
2227 struct ap_matrix_mdev *matrix_mdev;
2228 DECLARE_BITMAP(aprem, AP_DEVICES);
2229 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2230 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2233 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2234 mutex_lock(&matrix_mdev->kvm->lock);
2235 mutex_lock(&matrix_dev->mdevs_lock);
2237 do_remove |= bitmap_and(aprem, ap_remove,
2238 matrix_mdev->matrix.apm,
2240 do_remove |= bitmap_and(aqrem, aq_remove,
2241 matrix_mdev->matrix.aqm,
2243 do_remove |= bitmap_andnot(cdrem, cd_remove,
2244 matrix_mdev->matrix.adm,
2248 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2251 mutex_unlock(&matrix_dev->mdevs_lock);
2252 mutex_unlock(&matrix_mdev->kvm->lock);
2257 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2258 * control domains from the host AP configuration
2259 * by unplugging them from the guests that are
2261 * @cur_config_info: the current host AP configuration information
2262 * @prev_config_info: the previous host AP configuration information
2264 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2265 struct ap_config_info *prev_config_info)
2268 DECLARE_BITMAP(aprem, AP_DEVICES);
2269 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2270 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2272 do_remove = bitmap_andnot(aprem,
2273 (unsigned long *)prev_config_info->apm,
2274 (unsigned long *)cur_config_info->apm,
2276 do_remove |= bitmap_andnot(aqrem,
2277 (unsigned long *)prev_config_info->aqm,
2278 (unsigned long *)cur_config_info->aqm,
2280 do_remove |= bitmap_andnot(cdrem,
2281 (unsigned long *)prev_config_info->adm,
2282 (unsigned long *)cur_config_info->adm,
2286 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2290 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2291 * are older than AP type 10 (CEX4).
2292 * @apm: a bitmap of the APIDs to examine
2293 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2295 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2298 struct ap_queue_status status;
2299 unsigned long apid, apqi;
2300 struct ap_tapq_gr2 info;
2302 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2303 apid_cleared = false;
2305 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2306 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2307 switch (status.response_code) {
2309 * According to the architecture in each case
2310 * below, the queue's info should be filled.
2312 case AP_RESPONSE_NORMAL:
2313 case AP_RESPONSE_RESET_IN_PROGRESS:
2314 case AP_RESPONSE_DECONFIGURED:
2315 case AP_RESPONSE_CHECKSTOPPED:
2316 case AP_RESPONSE_BUSY:
2318 * The vfio_ap device driver only
2319 * supports CEX4 and newer adapters, so
2320 * remove the APID if the adapter is
2321 * older than a CEX4.
2323 if (info.at < AP_DEVICE_TYPE_CEX4) {
2324 clear_bit_inv(apid, apm);
2325 apid_cleared = true;
2332 * If we don't know the adapter type,
2333 * clear its APID since it can't be
2334 * determined whether the vfio_ap
2335 * device driver supports it.
2337 clear_bit_inv(apid, apm);
2338 apid_cleared = true;
2343 * If we've already cleared the APID from the apm, there
2344 * is no need to continue examining the remainin AP
2345 * queues to determine the type of the adapter.
2354 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2355 * control domains that have been added to the host's
2356 * AP configuration for each matrix mdev to which they
2359 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2361 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2363 * @adm_add: a bitmap specifying the control domains that have been added to the
2366 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2367 unsigned long *adm_add)
2369 struct ap_matrix_mdev *matrix_mdev;
2371 if (list_empty(&matrix_dev->mdev_list))
2374 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2376 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2377 bitmap_and(matrix_mdev->apm_add,
2378 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2379 bitmap_and(matrix_mdev->aqm_add,
2380 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2381 bitmap_and(matrix_mdev->adm_add,
2382 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2387 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2388 * control domains to the host AP configuration
2389 * by updating the bitmaps that specify what adapters,
2390 * domains and control domains have been added so they
2391 * can be hot plugged into the guest when the AP bus
2392 * scan completes (see vfio_ap_on_scan_complete
2394 * @cur_config_info: the current AP configuration information
2395 * @prev_config_info: the previous AP configuration information
2397 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2398 struct ap_config_info *prev_config_info)
2401 DECLARE_BITMAP(apm_add, AP_DEVICES);
2402 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2403 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2405 do_add = bitmap_andnot(apm_add,
2406 (unsigned long *)cur_config_info->apm,
2407 (unsigned long *)prev_config_info->apm,
2409 do_add |= bitmap_andnot(aqm_add,
2410 (unsigned long *)cur_config_info->aqm,
2411 (unsigned long *)prev_config_info->aqm,
2413 do_add |= bitmap_andnot(adm_add,
2414 (unsigned long *)cur_config_info->adm,
2415 (unsigned long *)prev_config_info->adm,
2419 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2423 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2426 * @cur_cfg_info: the current host AP configuration
2427 * @prev_cfg_info: the previous host AP configuration
2429 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2430 struct ap_config_info *prev_cfg_info)
2432 if (!cur_cfg_info || !prev_cfg_info)
2435 mutex_lock(&matrix_dev->guests_lock);
2437 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2438 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2439 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2441 mutex_unlock(&matrix_dev->guests_lock);
2444 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2446 bool do_hotplug = false;
2447 int filter_domains = 0;
2448 int filter_adapters = 0;
2449 DECLARE_BITMAP(apm, AP_DEVICES);
2450 DECLARE_BITMAP(aqm, AP_DOMAINS);
2452 mutex_lock(&matrix_mdev->kvm->lock);
2453 mutex_lock(&matrix_dev->mdevs_lock);
2455 filter_adapters = bitmap_and(apm, matrix_mdev->matrix.apm,
2456 matrix_mdev->apm_add, AP_DEVICES);
2457 filter_domains = bitmap_and(aqm, matrix_mdev->matrix.aqm,
2458 matrix_mdev->aqm_add, AP_DOMAINS);
2460 if (filter_adapters && filter_domains)
2461 do_hotplug |= vfio_ap_mdev_filter_matrix(apm, aqm, matrix_mdev);
2462 else if (filter_adapters)
2464 vfio_ap_mdev_filter_matrix(apm,
2465 matrix_mdev->shadow_apcb.aqm,
2469 vfio_ap_mdev_filter_matrix(matrix_mdev->shadow_apcb.apm,
2472 if (bitmap_intersects(matrix_mdev->matrix.adm, matrix_mdev->adm_add,
2474 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2477 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2479 mutex_unlock(&matrix_dev->mdevs_lock);
2480 mutex_unlock(&matrix_mdev->kvm->lock);
2483 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2484 struct ap_config_info *old_config_info)
2486 struct ap_matrix_mdev *matrix_mdev;
2488 mutex_lock(&matrix_dev->guests_lock);
2490 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2491 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2492 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2493 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2496 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2497 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2498 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2499 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2502 mutex_unlock(&matrix_dev->guests_lock);