Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Adjunct processor matrix VFIO device driver callbacks.
4 *
5 * Copyright IBM Corp. 2018
6 *
7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8 * Halil Pasic <pasic@linux.ibm.com>
9 * Pierre Morel <pmorel@linux.ibm.com>
10 */
11#include <linux/string.h>
12#include <linux/vfio.h>
13#include <linux/device.h>
14#include <linux/list.h>
15#include <linux/ctype.h>
16#include <linux/bitops.h>
17#include <linux/kvm_host.h>
18#include <linux/module.h>
19#include <linux/uuid.h>
20#include <asm/kvm.h>
21#include <asm/zcrypt.h>
22
23#include "vfio_ap_private.h"
24#include "vfio_ap_debug.h"
25
26#define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
27#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
28
29#define AP_QUEUE_ASSIGNED "assigned"
30#define AP_QUEUE_UNASSIGNED "unassigned"
31#define AP_QUEUE_IN_USE "in use"
32
33static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable);
34static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
35static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
36static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q, unsigned int retry);
37
38/**
39 * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
40 * KVM guest's APCB in the proper order.
41 *
42 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
43 *
44 * The proper locking order is:
45 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
46 * guest's APCB.
47 * 2. kvm->lock: required to update a guest's APCB
48 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
49 *
50 * Note: If @kvm is NULL, the KVM lock will not be taken.
51 */
52static inline void get_update_locks_for_kvm(struct kvm *kvm)
53{
54 mutex_lock(&matrix_dev->guests_lock);
55 if (kvm)
56 mutex_lock(&kvm->lock);
57 mutex_lock(&matrix_dev->mdevs_lock);
58}
59
60/**
61 * release_update_locks_for_kvm: Release the locks used to dynamically update a
62 * KVM guest's APCB in the proper order.
63 *
64 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
65 *
66 * The proper unlocking order is:
67 * 1. matrix_dev->mdevs_lock
68 * 2. kvm->lock
69 * 3. matrix_dev->guests_lock
70 *
71 * Note: If @kvm is NULL, the KVM lock will not be released.
72 */
73static inline void release_update_locks_for_kvm(struct kvm *kvm)
74{
75 mutex_unlock(&matrix_dev->mdevs_lock);
76 if (kvm)
77 mutex_unlock(&kvm->lock);
78 mutex_unlock(&matrix_dev->guests_lock);
79}
80
81/**
82 * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
83 * KVM guest's APCB in the proper order.
84 *
85 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
86 * configuration data to use to update a KVM guest's APCB.
87 *
88 * The proper locking order is:
89 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
90 * guest's APCB.
91 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
92 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
93 *
94 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
95 * lock will not be taken.
96 */
97static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
98{
99 mutex_lock(&matrix_dev->guests_lock);
100 if (matrix_mdev && matrix_mdev->kvm)
101 mutex_lock(&matrix_mdev->kvm->lock);
102 mutex_lock(&matrix_dev->mdevs_lock);
103}
104
105/**
106 * release_update_locks_for_mdev: Release the locks used to dynamically update a
107 * KVM guest's APCB in the proper order.
108 *
109 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
110 * configuration data to use to update a KVM guest's APCB.
111 *
112 * The proper unlocking order is:
113 * 1. matrix_dev->mdevs_lock
114 * 2. matrix_mdev->kvm->lock
115 * 3. matrix_dev->guests_lock
116 *
117 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
118 * lock will not be released.
119 */
120static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
121{
122 mutex_unlock(&matrix_dev->mdevs_lock);
123 if (matrix_mdev && matrix_mdev->kvm)
124 mutex_unlock(&matrix_mdev->kvm->lock);
125 mutex_unlock(&matrix_dev->guests_lock);
126}
127
128/**
129 * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
130 * acquire the locks required to update the APCB of
131 * the KVM guest to which the mdev is attached.
132 *
133 * @apqn: the APQN of a queue device.
134 *
135 * The proper locking order is:
136 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
137 * guest's APCB.
138 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
139 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
140 *
141 * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
142 * will not be taken.
143 *
144 * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
145 * is not assigned to an ap_matrix_mdev.
146 */
147static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
148{
149 struct ap_matrix_mdev *matrix_mdev;
150
151 mutex_lock(&matrix_dev->guests_lock);
152
153 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
154 if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
155 test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
156 if (matrix_mdev->kvm)
157 mutex_lock(&matrix_mdev->kvm->lock);
158
159 mutex_lock(&matrix_dev->mdevs_lock);
160
161 return matrix_mdev;
162 }
163 }
164
165 mutex_lock(&matrix_dev->mdevs_lock);
166
167 return NULL;
168}
169
170/**
171 * get_update_locks_for_queue: get the locks required to update the APCB of the
172 * KVM guest to which the matrix mdev linked to a
173 * vfio_ap_queue object is attached.
174 *
175 * @q: a pointer to a vfio_ap_queue object.
176 *
177 * The proper locking order is:
178 * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
179 * KVM guest's APCB.
180 * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB
181 * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev
182 *
183 * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
184 * will not be taken.
185 */
186static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
187{
188 mutex_lock(&matrix_dev->guests_lock);
189 if (q->matrix_mdev && q->matrix_mdev->kvm)
190 mutex_lock(&q->matrix_mdev->kvm->lock);
191 mutex_lock(&matrix_dev->mdevs_lock);
192}
193
194/**
195 * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
196 * hash table of queues assigned to a matrix mdev
197 * @matrix_mdev: the matrix mdev
198 * @apqn: The APQN of a queue device
199 *
200 * Return: the pointer to the vfio_ap_queue struct representing the queue or
201 * NULL if the queue is not assigned to @matrix_mdev
202 */
203static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
204 struct ap_matrix_mdev *matrix_mdev,
205 int apqn)
206{
207 struct vfio_ap_queue *q;
208
209 hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
210 apqn) {
211 if (q && q->apqn == apqn)
212 return q;
213 }
214
215 return NULL;
216}
217
218/**
219 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
220 * @apqn: The AP Queue number
221 *
222 * Checks the IRQ bit for the status of this APQN using ap_tapq.
223 * Returns if the ap_tapq function succeeded and the bit is clear.
224 * Returns if ap_tapq function failed with invalid, deconfigured or
225 * checkstopped AP.
226 * Otherwise retries up to 5 times after waiting 20ms.
227 */
228static void vfio_ap_wait_for_irqclear(int apqn)
229{
230 struct ap_queue_status status;
231 int retry = 5;
232
233 do {
234 status = ap_tapq(apqn, NULL);
235 switch (status.response_code) {
236 case AP_RESPONSE_NORMAL:
237 case AP_RESPONSE_RESET_IN_PROGRESS:
238 if (!status.irq_enabled)
239 return;
240 fallthrough;
241 case AP_RESPONSE_BUSY:
242 msleep(20);
243 break;
244 case AP_RESPONSE_Q_NOT_AVAIL:
245 case AP_RESPONSE_DECONFIGURED:
246 case AP_RESPONSE_CHECKSTOPPED:
247 default:
248 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
249 status.response_code, apqn);
250 return;
251 }
252 } while (--retry);
253
254 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
255 __func__, status.response_code, apqn);
256}
257
258/**
259 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
260 * @q: The vfio_ap_queue
261 *
262 * Unregisters the ISC in the GIB when the saved ISC not invalid.
263 * Unpins the guest's page holding the NIB when it exists.
264 * Resets the saved_iova and saved_isc to invalid values.
265 */
266static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
267{
268 if (!q)
269 return;
270 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
271 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
272 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
273 q->saved_isc = VFIO_AP_ISC_INVALID;
274 }
275 if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
276 vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
277 q->saved_iova = 0;
278 }
279}
280
281/**
282 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
283 * @q: The vfio_ap_queue
284 *
285 * Uses ap_aqic to disable the interruption and in case of success, reset
286 * in progress or IRQ disable command already proceeded: calls
287 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
288 * and calls vfio_ap_free_aqic_resources() to free the resources associated
289 * with the AP interrupt handling.
290 *
291 * In the case the AP is busy, or a reset is in progress,
292 * retries after 20ms, up to 5 times.
293 *
294 * Returns if ap_aqic function failed with invalid, deconfigured or
295 * checkstopped AP.
296 *
297 * Return: &struct ap_queue_status
298 */
299static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
300{
301 struct ap_qirq_ctrl aqic_gisa = {};
302 struct ap_queue_status status;
303 int retries = 5;
304
305 do {
306 status = ap_aqic(q->apqn, aqic_gisa, 0);
307 switch (status.response_code) {
308 case AP_RESPONSE_OTHERWISE_CHANGED:
309 case AP_RESPONSE_NORMAL:
310 vfio_ap_wait_for_irqclear(q->apqn);
311 goto end_free;
312 case AP_RESPONSE_RESET_IN_PROGRESS:
313 case AP_RESPONSE_BUSY:
314 msleep(20);
315 break;
316 case AP_RESPONSE_Q_NOT_AVAIL:
317 case AP_RESPONSE_DECONFIGURED:
318 case AP_RESPONSE_CHECKSTOPPED:
319 case AP_RESPONSE_INVALID_ADDRESS:
320 default:
321 /* All cases in default means AP not operational */
322 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
323 status.response_code);
324 goto end_free;
325 }
326 } while (retries--);
327
328 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
329 status.response_code);
330end_free:
331 vfio_ap_free_aqic_resources(q);
332 return status;
333}
334
335/**
336 * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
337 *
338 * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
339 * @nib: the location for storing the nib address.
340 *
341 * When the PQAP(AQIC) instruction is executed, general register 2 contains the
342 * address of the notification indicator byte (nib) used for IRQ notification.
343 * This function parses and validates the nib from gr2.
344 *
345 * Return: returns zero if the nib address is a valid; otherwise, returns
346 * -EINVAL.
347 */
348static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
349{
350 *nib = vcpu->run->s.regs.gprs[2];
351
352 if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
353 return -EINVAL;
354
355 return 0;
356}
357
358/**
359 * vfio_ap_irq_enable - Enable Interruption for a APQN
360 *
361 * @q: the vfio_ap_queue holding AQIC parameters
362 * @isc: the guest ISC to register with the GIB interface
363 * @vcpu: the vcpu object containing the registers specifying the parameters
364 * passed to the PQAP(AQIC) instruction.
365 *
366 * Pin the NIB saved in *q
367 * Register the guest ISC to GIB interface and retrieve the
368 * host ISC to issue the host side PQAP/AQIC
369 *
370 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
371 * vfio_pin_pages failed.
372 *
373 * Otherwise return the ap_queue_status returned by the ap_aqic(),
374 * all retry handling will be done by the guest.
375 *
376 * Return: &struct ap_queue_status
377 */
378static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
379 int isc,
380 struct kvm_vcpu *vcpu)
381{
382 struct ap_qirq_ctrl aqic_gisa = {};
383 struct ap_queue_status status = {};
384 struct kvm_s390_gisa *gisa;
385 struct page *h_page;
386 int nisc;
387 struct kvm *kvm;
388 phys_addr_t h_nib;
389 dma_addr_t nib;
390 int ret;
391
392 /* Verify that the notification indicator byte address is valid */
393 if (vfio_ap_validate_nib(vcpu, &nib)) {
394 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
395 __func__, &nib, q->apqn);
396
397 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
398 return status;
399 }
400
401 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
402 IOMMU_READ | IOMMU_WRITE, &h_page);
403 switch (ret) {
404 case 1:
405 break;
406 default:
407 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
408 "nib=%pad, apqn=%#04x\n",
409 __func__, ret, &nib, q->apqn);
410
411 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
412 return status;
413 }
414
415 kvm = q->matrix_mdev->kvm;
416 gisa = kvm->arch.gisa_int.origin;
417
418 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
419 aqic_gisa.gisc = isc;
420
421 nisc = kvm_s390_gisc_register(kvm, isc);
422 if (nisc < 0) {
423 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
424 __func__, nisc, isc, q->apqn);
425
426 status.response_code = AP_RESPONSE_INVALID_GISA;
427 return status;
428 }
429
430 aqic_gisa.isc = nisc;
431 aqic_gisa.ir = 1;
432 aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
433
434 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
435 switch (status.response_code) {
436 case AP_RESPONSE_NORMAL:
437 /* See if we did clear older IRQ configuration */
438 vfio_ap_free_aqic_resources(q);
439 q->saved_iova = nib;
440 q->saved_isc = isc;
441 break;
442 case AP_RESPONSE_OTHERWISE_CHANGED:
443 /* We could not modify IRQ setings: clear new configuration */
444 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
445 kvm_s390_gisc_unregister(kvm, isc);
446 break;
447 default:
448 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
449 status.response_code);
450 vfio_ap_irq_disable(q);
451 break;
452 }
453
454 if (status.response_code != AP_RESPONSE_NORMAL) {
455 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
456 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
457 "gisa=%#x, isc=%#x, apqn=%#04x\n",
458 __func__, status.response_code,
459 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
460 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
461 q->apqn);
462 }
463
464 return status;
465}
466
467/**
468 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
469 * of big endian elements that can be passed by
470 * value to an s390dbf sprintf event function to
471 * format a UUID string.
472 *
473 * @guid: the object containing the little endian guid
474 * @uuid: a six-element array of long values that can be passed by value as
475 * arguments for a formatting string specifying a UUID.
476 *
477 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
478 * event functions if the memory for the passed string is available as long as
479 * the debug feature exists. Since a mediated device can be removed at any
480 * time, it's name can not be used because %s passes the reference to the string
481 * in memory and the reference will go stale once the device is removed .
482 *
483 * The s390dbf string formatting function allows a maximum of 9 arguments for a
484 * message to be displayed in the 'sprintf' view. In order to use the bytes
485 * comprising the mediated device's UUID to display the mediated device name,
486 * they will have to be converted into an array whose elements can be passed by
487 * value to sprintf. For example:
488 *
489 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
490 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
491 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
492 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
493 */
494static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
495{
496 /*
497 * The input guid is ordered in little endian, so it needs to be
498 * reordered for displaying a UUID as a string. This specifies the
499 * guid indices in proper order.
500 */
501 uuid[0] = le32_to_cpup((__le32 *)guid);
502 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
503 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
504 uuid[3] = *((__u16 *)&guid->b[8]);
505 uuid[4] = *((__u16 *)&guid->b[10]);
506 uuid[5] = *((__u32 *)&guid->b[12]);
507}
508
509/**
510 * handle_pqap - PQAP instruction callback
511 *
512 * @vcpu: The vcpu on which we received the PQAP instruction
513 *
514 * Get the general register contents to initialize internal variables.
515 * REG[0]: APQN
516 * REG[1]: IR and ISC
517 * REG[2]: NIB
518 *
519 * Response.status may be set to following Response Code:
520 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
521 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
522 * - AP_RESPONSE_NORMAL (0) : in case of successs
523 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
524 * We take the matrix_dev lock to ensure serialization on queues and
525 * mediated device access.
526 *
527 * Return: 0 if we could handle the request inside KVM.
528 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
529 */
530static int handle_pqap(struct kvm_vcpu *vcpu)
531{
532 uint64_t status;
533 uint16_t apqn;
534 unsigned long uuid[6];
535 struct vfio_ap_queue *q;
536 struct ap_queue_status qstatus = {
537 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
538 struct ap_matrix_mdev *matrix_mdev;
539
540 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
541
542 /* If we do not use the AIV facility just go to userland */
543 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
544 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
545 __func__, apqn, vcpu->arch.sie_block->eca);
546
547 return -EOPNOTSUPP;
548 }
549
550 mutex_lock(&matrix_dev->mdevs_lock);
551
552 if (!vcpu->kvm->arch.crypto.pqap_hook) {
553 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
554 __func__, apqn);
555
556 goto out_unlock;
557 }
558
559 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
560 struct ap_matrix_mdev, pqap_hook);
561
562 /* If the there is no guest using the mdev, there is nothing to do */
563 if (!matrix_mdev->kvm) {
564 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
565 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
566 __func__, uuid[0], uuid[1], uuid[2],
567 uuid[3], uuid[4], uuid[5], apqn);
568 goto out_unlock;
569 }
570
571 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
572 if (!q) {
573 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
574 __func__, AP_QID_CARD(apqn),
575 AP_QID_QUEUE(apqn));
576 goto out_unlock;
577 }
578
579 status = vcpu->run->s.regs.gprs[1];
580
581 /* If IR bit(16) is set we enable the interrupt */
582 if ((status >> (63 - 16)) & 0x01)
583 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
584 else
585 qstatus = vfio_ap_irq_disable(q);
586
587out_unlock:
588 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
589 vcpu->run->s.regs.gprs[1] >>= 32;
590 mutex_unlock(&matrix_dev->mdevs_lock);
591 return 0;
592}
593
594static void vfio_ap_matrix_init(struct ap_config_info *info,
595 struct ap_matrix *matrix)
596{
597 matrix->apm_max = info->apxa ? info->Na : 63;
598 matrix->aqm_max = info->apxa ? info->Nd : 15;
599 matrix->adm_max = info->apxa ? info->Nd : 15;
600}
601
602static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
603{
604 if (matrix_mdev->kvm)
605 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
606 matrix_mdev->shadow_apcb.apm,
607 matrix_mdev->shadow_apcb.aqm,
608 matrix_mdev->shadow_apcb.adm);
609}
610
611static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
612{
613 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
614
615 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
616 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
617 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
618
619 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
620 AP_DOMAINS);
621}
622
623/*
624 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
625 * to ensure no queue devices are passed through to
626 * the guest that are not bound to the vfio_ap
627 * device driver.
628 *
629 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
630 *
631 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
632 * driver, its APID will be filtered from the guest's APCB. The matrix
633 * structure precludes filtering an individual APQN, so its APID will be
634 * filtered.
635 *
636 * Return: a boolean value indicating whether the KVM guest's APCB was changed
637 * by the filtering or not.
638 */
639static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm,
640 struct ap_matrix_mdev *matrix_mdev)
641{
642 unsigned long apid, apqi, apqn;
643 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
644 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
645 struct vfio_ap_queue *q;
646
647 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
648 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
649 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
650
651 /*
652 * Copy the adapters, domains and control domains to the shadow_apcb
653 * from the matrix mdev, but only those that are assigned to the host's
654 * AP configuration.
655 */
656 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
657 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
658 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
659 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
660
661 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
662 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
663 /*
664 * If the APQN is not bound to the vfio_ap device
665 * driver, then we can't assign it to the guest's
666 * AP configuration. The AP architecture won't
667 * allow filtering of a single APQN, so let's filter
668 * the APID since an adapter represents a physical
669 * hardware device.
670 */
671 apqn = AP_MKQID(apid, apqi);
672 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
673 if (!q || q->reset_rc) {
674 clear_bit_inv(apid,
675 matrix_mdev->shadow_apcb.apm);
676 break;
677 }
678 }
679 }
680
681 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
682 AP_DEVICES) ||
683 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
684 AP_DOMAINS);
685}
686
687static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
688{
689 struct ap_matrix_mdev *matrix_mdev =
690 container_of(vdev, struct ap_matrix_mdev, vdev);
691
692 matrix_mdev->mdev = to_mdev_device(vdev->dev);
693 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
694 matrix_mdev->pqap_hook = handle_pqap;
695 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
696 hash_init(matrix_mdev->qtable.queues);
697
698 return 0;
699}
700
701static int vfio_ap_mdev_probe(struct mdev_device *mdev)
702{
703 struct ap_matrix_mdev *matrix_mdev;
704 int ret;
705
706 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
707 &vfio_ap_matrix_dev_ops);
708 if (IS_ERR(matrix_mdev))
709 return PTR_ERR(matrix_mdev);
710
711 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
712 if (ret)
713 goto err_put_vdev;
714 dev_set_drvdata(&mdev->dev, matrix_mdev);
715 mutex_lock(&matrix_dev->mdevs_lock);
716 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
717 mutex_unlock(&matrix_dev->mdevs_lock);
718 return 0;
719
720err_put_vdev:
721 vfio_put_device(&matrix_mdev->vdev);
722 return ret;
723}
724
725static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
726 struct vfio_ap_queue *q)
727{
728 if (q) {
729 q->matrix_mdev = matrix_mdev;
730 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
731 }
732}
733
734static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
735{
736 struct vfio_ap_queue *q;
737
738 q = vfio_ap_find_queue(apqn);
739 vfio_ap_mdev_link_queue(matrix_mdev, q);
740}
741
742static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
743{
744 hash_del(&q->mdev_qnode);
745}
746
747static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
748{
749 q->matrix_mdev = NULL;
750}
751
752static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
753{
754 struct vfio_ap_queue *q;
755 unsigned long apid, apqi;
756
757 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
758 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
759 AP_DOMAINS) {
760 q = vfio_ap_mdev_get_queue(matrix_mdev,
761 AP_MKQID(apid, apqi));
762 if (q)
763 q->matrix_mdev = NULL;
764 }
765 }
766}
767
768static void vfio_ap_mdev_remove(struct mdev_device *mdev)
769{
770 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
771
772 vfio_unregister_group_dev(&matrix_mdev->vdev);
773
774 mutex_lock(&matrix_dev->guests_lock);
775 mutex_lock(&matrix_dev->mdevs_lock);
776 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
777 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
778 list_del(&matrix_mdev->node);
779 mutex_unlock(&matrix_dev->mdevs_lock);
780 mutex_unlock(&matrix_dev->guests_lock);
781 vfio_put_device(&matrix_mdev->vdev);
782}
783
784#define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
785 "already assigned to %s"
786
787static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
788 unsigned long *apm,
789 unsigned long *aqm)
790{
791 unsigned long apid, apqi;
792 const struct device *dev = mdev_dev(matrix_mdev->mdev);
793 const char *mdev_name = dev_name(dev);
794
795 for_each_set_bit_inv(apid, apm, AP_DEVICES)
796 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
797 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
798}
799
800/**
801 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
802 *
803 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
804 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
805 *
806 * Verifies that each APQN derived from the Cartesian product of a bitmap of
807 * AP adapter IDs and AP queue indexes is not configured for any matrix
808 * mediated device. AP queue sharing is not allowed.
809 *
810 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
811 */
812static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
813 unsigned long *mdev_aqm)
814{
815 struct ap_matrix_mdev *matrix_mdev;
816 DECLARE_BITMAP(apm, AP_DEVICES);
817 DECLARE_BITMAP(aqm, AP_DOMAINS);
818
819 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
820 /*
821 * If the input apm and aqm are fields of the matrix_mdev
822 * object, then move on to the next matrix_mdev.
823 */
824 if (mdev_apm == matrix_mdev->matrix.apm &&
825 mdev_aqm == matrix_mdev->matrix.aqm)
826 continue;
827
828 memset(apm, 0, sizeof(apm));
829 memset(aqm, 0, sizeof(aqm));
830
831 /*
832 * We work on full longs, as we can only exclude the leftover
833 * bits in non-inverse order. The leftover is all zeros.
834 */
835 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
836 AP_DEVICES))
837 continue;
838
839 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
840 AP_DOMAINS))
841 continue;
842
843 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
844
845 return -EADDRINUSE;
846 }
847
848 return 0;
849}
850
851/**
852 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
853 * not reserved for the default zcrypt driver and
854 * are not assigned to another mdev.
855 *
856 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
857 *
858 * Return: One of the following values:
859 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
860 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
861 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
862 * zcrypt default driver.
863 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
864 * o A zero indicating validation succeeded.
865 */
866static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
867{
868 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
869 matrix_mdev->matrix.aqm))
870 return -EADDRNOTAVAIL;
871
872 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
873 matrix_mdev->matrix.aqm);
874}
875
876static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
877 unsigned long apid)
878{
879 unsigned long apqi;
880
881 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
882 vfio_ap_mdev_link_apqn(matrix_mdev,
883 AP_MKQID(apid, apqi));
884}
885
886/**
887 * assign_adapter_store - parses the APID from @buf and sets the
888 * corresponding bit in the mediated matrix device's APM
889 *
890 * @dev: the matrix device
891 * @attr: the mediated matrix device's assign_adapter attribute
892 * @buf: a buffer containing the AP adapter number (APID) to
893 * be assigned
894 * @count: the number of bytes in @buf
895 *
896 * Return: the number of bytes processed if the APID is valid; otherwise,
897 * returns one of the following errors:
898 *
899 * 1. -EINVAL
900 * The APID is not a valid number
901 *
902 * 2. -ENODEV
903 * The APID exceeds the maximum value configured for the system
904 *
905 * 3. -EADDRNOTAVAIL
906 * An APQN derived from the cross product of the APID being assigned
907 * and the APQIs previously assigned is not bound to the vfio_ap device
908 * driver; or, if no APQIs have yet been assigned, the APID is not
909 * contained in an APQN bound to the vfio_ap device driver.
910 *
911 * 4. -EADDRINUSE
912 * An APQN derived from the cross product of the APID being assigned
913 * and the APQIs previously assigned is being used by another mediated
914 * matrix device
915 *
916 * 5. -EAGAIN
917 * A lock required to validate the mdev's AP configuration could not
918 * be obtained.
919 */
920static ssize_t assign_adapter_store(struct device *dev,
921 struct device_attribute *attr,
922 const char *buf, size_t count)
923{
924 int ret;
925 unsigned long apid;
926 DECLARE_BITMAP(apm_delta, AP_DEVICES);
927 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
928
929 mutex_lock(&ap_perms_mutex);
930 get_update_locks_for_mdev(matrix_mdev);
931
932 ret = kstrtoul(buf, 0, &apid);
933 if (ret)
934 goto done;
935
936 if (apid > matrix_mdev->matrix.apm_max) {
937 ret = -ENODEV;
938 goto done;
939 }
940
941 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
942 ret = count;
943 goto done;
944 }
945
946 set_bit_inv(apid, matrix_mdev->matrix.apm);
947
948 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
949 if (ret) {
950 clear_bit_inv(apid, matrix_mdev->matrix.apm);
951 goto done;
952 }
953
954 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
955 memset(apm_delta, 0, sizeof(apm_delta));
956 set_bit_inv(apid, apm_delta);
957
958 if (vfio_ap_mdev_filter_matrix(apm_delta,
959 matrix_mdev->matrix.aqm, matrix_mdev))
960 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
961
962 ret = count;
963done:
964 release_update_locks_for_mdev(matrix_mdev);
965 mutex_unlock(&ap_perms_mutex);
966
967 return ret;
968}
969static DEVICE_ATTR_WO(assign_adapter);
970
971static struct vfio_ap_queue
972*vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
973 unsigned long apid, unsigned long apqi)
974{
975 struct vfio_ap_queue *q = NULL;
976
977 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
978 /* If the queue is assigned to the matrix mdev, unlink it. */
979 if (q)
980 vfio_ap_unlink_queue_fr_mdev(q);
981
982 return q;
983}
984
985/**
986 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
987 * adapter from the matrix mdev to which the
988 * adapter was assigned.
989 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
990 * @apid: the APID of the unassigned adapter.
991 * @qtable: table for storing queues associated with unassigned adapter.
992 */
993static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
994 unsigned long apid,
995 struct ap_queue_table *qtable)
996{
997 unsigned long apqi;
998 struct vfio_ap_queue *q;
999
1000 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1001 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1002
1003 if (q && qtable) {
1004 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1005 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1006 hash_add(qtable->queues, &q->mdev_qnode,
1007 q->apqn);
1008 }
1009 }
1010}
1011
1012static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1013 unsigned long apid)
1014{
1015 int loop_cursor;
1016 struct vfio_ap_queue *q;
1017 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1018
1019 hash_init(qtable->queues);
1020 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, qtable);
1021
1022 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1023 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1024 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1025 }
1026
1027 vfio_ap_mdev_reset_queues(qtable);
1028
1029 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1030 vfio_ap_unlink_mdev_fr_queue(q);
1031 hash_del(&q->mdev_qnode);
1032 }
1033
1034 kfree(qtable);
1035}
1036
1037/**
1038 * unassign_adapter_store - parses the APID from @buf and clears the
1039 * corresponding bit in the mediated matrix device's APM
1040 *
1041 * @dev: the matrix device
1042 * @attr: the mediated matrix device's unassign_adapter attribute
1043 * @buf: a buffer containing the adapter number (APID) to be unassigned
1044 * @count: the number of bytes in @buf
1045 *
1046 * Return: the number of bytes processed if the APID is valid; otherwise,
1047 * returns one of the following errors:
1048 * -EINVAL if the APID is not a number
1049 * -ENODEV if the APID it exceeds the maximum value configured for the
1050 * system
1051 */
1052static ssize_t unassign_adapter_store(struct device *dev,
1053 struct device_attribute *attr,
1054 const char *buf, size_t count)
1055{
1056 int ret;
1057 unsigned long apid;
1058 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1059
1060 get_update_locks_for_mdev(matrix_mdev);
1061
1062 ret = kstrtoul(buf, 0, &apid);
1063 if (ret)
1064 goto done;
1065
1066 if (apid > matrix_mdev->matrix.apm_max) {
1067 ret = -ENODEV;
1068 goto done;
1069 }
1070
1071 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1072 ret = count;
1073 goto done;
1074 }
1075
1076 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1077 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1078 ret = count;
1079done:
1080 release_update_locks_for_mdev(matrix_mdev);
1081 return ret;
1082}
1083static DEVICE_ATTR_WO(unassign_adapter);
1084
1085static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1086 unsigned long apqi)
1087{
1088 unsigned long apid;
1089
1090 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1091 vfio_ap_mdev_link_apqn(matrix_mdev,
1092 AP_MKQID(apid, apqi));
1093}
1094
1095/**
1096 * assign_domain_store - parses the APQI from @buf and sets the
1097 * corresponding bit in the mediated matrix device's AQM
1098 *
1099 * @dev: the matrix device
1100 * @attr: the mediated matrix device's assign_domain attribute
1101 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1102 * be assigned
1103 * @count: the number of bytes in @buf
1104 *
1105 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1106 * one of the following errors:
1107 *
1108 * 1. -EINVAL
1109 * The APQI is not a valid number
1110 *
1111 * 2. -ENODEV
1112 * The APQI exceeds the maximum value configured for the system
1113 *
1114 * 3. -EADDRNOTAVAIL
1115 * An APQN derived from the cross product of the APQI being assigned
1116 * and the APIDs previously assigned is not bound to the vfio_ap device
1117 * driver; or, if no APIDs have yet been assigned, the APQI is not
1118 * contained in an APQN bound to the vfio_ap device driver.
1119 *
1120 * 4. -EADDRINUSE
1121 * An APQN derived from the cross product of the APQI being assigned
1122 * and the APIDs previously assigned is being used by another mediated
1123 * matrix device
1124 *
1125 * 5. -EAGAIN
1126 * The lock required to validate the mdev's AP configuration could not
1127 * be obtained.
1128 */
1129static ssize_t assign_domain_store(struct device *dev,
1130 struct device_attribute *attr,
1131 const char *buf, size_t count)
1132{
1133 int ret;
1134 unsigned long apqi;
1135 DECLARE_BITMAP(aqm_delta, AP_DOMAINS);
1136 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1137
1138 mutex_lock(&ap_perms_mutex);
1139 get_update_locks_for_mdev(matrix_mdev);
1140
1141 ret = kstrtoul(buf, 0, &apqi);
1142 if (ret)
1143 goto done;
1144
1145 if (apqi > matrix_mdev->matrix.aqm_max) {
1146 ret = -ENODEV;
1147 goto done;
1148 }
1149
1150 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1151 ret = count;
1152 goto done;
1153 }
1154
1155 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1156
1157 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1158 if (ret) {
1159 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1160 goto done;
1161 }
1162
1163 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1164 memset(aqm_delta, 0, sizeof(aqm_delta));
1165 set_bit_inv(apqi, aqm_delta);
1166
1167 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, aqm_delta,
1168 matrix_mdev))
1169 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1170
1171 ret = count;
1172done:
1173 release_update_locks_for_mdev(matrix_mdev);
1174 mutex_unlock(&ap_perms_mutex);
1175
1176 return ret;
1177}
1178static DEVICE_ATTR_WO(assign_domain);
1179
1180static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1181 unsigned long apqi,
1182 struct ap_queue_table *qtable)
1183{
1184 unsigned long apid;
1185 struct vfio_ap_queue *q;
1186
1187 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1188 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1189
1190 if (q && qtable) {
1191 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1192 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1193 hash_add(qtable->queues, &q->mdev_qnode,
1194 q->apqn);
1195 }
1196 }
1197}
1198
1199static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1200 unsigned long apqi)
1201{
1202 int loop_cursor;
1203 struct vfio_ap_queue *q;
1204 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1205
1206 hash_init(qtable->queues);
1207 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, qtable);
1208
1209 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1210 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1211 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1212 }
1213
1214 vfio_ap_mdev_reset_queues(qtable);
1215
1216 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1217 vfio_ap_unlink_mdev_fr_queue(q);
1218 hash_del(&q->mdev_qnode);
1219 }
1220
1221 kfree(qtable);
1222}
1223
1224/**
1225 * unassign_domain_store - parses the APQI from @buf and clears the
1226 * corresponding bit in the mediated matrix device's AQM
1227 *
1228 * @dev: the matrix device
1229 * @attr: the mediated matrix device's unassign_domain attribute
1230 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1231 * be unassigned
1232 * @count: the number of bytes in @buf
1233 *
1234 * Return: the number of bytes processed if the APQI is valid; otherwise,
1235 * returns one of the following errors:
1236 * -EINVAL if the APQI is not a number
1237 * -ENODEV if the APQI exceeds the maximum value configured for the system
1238 */
1239static ssize_t unassign_domain_store(struct device *dev,
1240 struct device_attribute *attr,
1241 const char *buf, size_t count)
1242{
1243 int ret;
1244 unsigned long apqi;
1245 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1246
1247 get_update_locks_for_mdev(matrix_mdev);
1248
1249 ret = kstrtoul(buf, 0, &apqi);
1250 if (ret)
1251 goto done;
1252
1253 if (apqi > matrix_mdev->matrix.aqm_max) {
1254 ret = -ENODEV;
1255 goto done;
1256 }
1257
1258 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1259 ret = count;
1260 goto done;
1261 }
1262
1263 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1264 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1265 ret = count;
1266
1267done:
1268 release_update_locks_for_mdev(matrix_mdev);
1269 return ret;
1270}
1271static DEVICE_ATTR_WO(unassign_domain);
1272
1273/**
1274 * assign_control_domain_store - parses the domain ID from @buf and sets
1275 * the corresponding bit in the mediated matrix device's ADM
1276 *
1277 * @dev: the matrix device
1278 * @attr: the mediated matrix device's assign_control_domain attribute
1279 * @buf: a buffer containing the domain ID to be assigned
1280 * @count: the number of bytes in @buf
1281 *
1282 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1283 * returns one of the following errors:
1284 * -EINVAL if the ID is not a number
1285 * -ENODEV if the ID exceeds the maximum value configured for the system
1286 */
1287static ssize_t assign_control_domain_store(struct device *dev,
1288 struct device_attribute *attr,
1289 const char *buf, size_t count)
1290{
1291 int ret;
1292 unsigned long id;
1293 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1294
1295 get_update_locks_for_mdev(matrix_mdev);
1296
1297 ret = kstrtoul(buf, 0, &id);
1298 if (ret)
1299 goto done;
1300
1301 if (id > matrix_mdev->matrix.adm_max) {
1302 ret = -ENODEV;
1303 goto done;
1304 }
1305
1306 if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1307 ret = count;
1308 goto done;
1309 }
1310
1311 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1312 * domain number (id). The bits in the mask, from most significant to
1313 * least significant, correspond to IDs 0 up to the one less than the
1314 * number of control domains that can be assigned.
1315 */
1316 set_bit_inv(id, matrix_mdev->matrix.adm);
1317 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1318 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1319
1320 ret = count;
1321done:
1322 release_update_locks_for_mdev(matrix_mdev);
1323 return ret;
1324}
1325static DEVICE_ATTR_WO(assign_control_domain);
1326
1327/**
1328 * unassign_control_domain_store - parses the domain ID from @buf and
1329 * clears the corresponding bit in the mediated matrix device's ADM
1330 *
1331 * @dev: the matrix device
1332 * @attr: the mediated matrix device's unassign_control_domain attribute
1333 * @buf: a buffer containing the domain ID to be unassigned
1334 * @count: the number of bytes in @buf
1335 *
1336 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1337 * returns one of the following errors:
1338 * -EINVAL if the ID is not a number
1339 * -ENODEV if the ID exceeds the maximum value configured for the system
1340 */
1341static ssize_t unassign_control_domain_store(struct device *dev,
1342 struct device_attribute *attr,
1343 const char *buf, size_t count)
1344{
1345 int ret;
1346 unsigned long domid;
1347 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1348
1349 get_update_locks_for_mdev(matrix_mdev);
1350
1351 ret = kstrtoul(buf, 0, &domid);
1352 if (ret)
1353 goto done;
1354
1355 if (domid > matrix_mdev->matrix.adm_max) {
1356 ret = -ENODEV;
1357 goto done;
1358 }
1359
1360 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1361 ret = count;
1362 goto done;
1363 }
1364
1365 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1366
1367 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1368 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1369 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1370 }
1371
1372 ret = count;
1373done:
1374 release_update_locks_for_mdev(matrix_mdev);
1375 return ret;
1376}
1377static DEVICE_ATTR_WO(unassign_control_domain);
1378
1379static ssize_t control_domains_show(struct device *dev,
1380 struct device_attribute *dev_attr,
1381 char *buf)
1382{
1383 unsigned long id;
1384 int nchars = 0;
1385 int n;
1386 char *bufpos = buf;
1387 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1388 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1389
1390 mutex_lock(&matrix_dev->mdevs_lock);
1391 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1392 n = sprintf(bufpos, "%04lx\n", id);
1393 bufpos += n;
1394 nchars += n;
1395 }
1396 mutex_unlock(&matrix_dev->mdevs_lock);
1397
1398 return nchars;
1399}
1400static DEVICE_ATTR_RO(control_domains);
1401
1402static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1403{
1404 char *bufpos = buf;
1405 unsigned long apid;
1406 unsigned long apqi;
1407 unsigned long apid1;
1408 unsigned long apqi1;
1409 unsigned long napm_bits = matrix->apm_max + 1;
1410 unsigned long naqm_bits = matrix->aqm_max + 1;
1411 int nchars = 0;
1412 int n;
1413
1414 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1415 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1416
1417 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1418 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1419 for_each_set_bit_inv(apqi, matrix->aqm,
1420 naqm_bits) {
1421 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1422 apqi);
1423 bufpos += n;
1424 nchars += n;
1425 }
1426 }
1427 } else if (apid1 < napm_bits) {
1428 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1429 n = sprintf(bufpos, "%02lx.\n", apid);
1430 bufpos += n;
1431 nchars += n;
1432 }
1433 } else if (apqi1 < naqm_bits) {
1434 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1435 n = sprintf(bufpos, ".%04lx\n", apqi);
1436 bufpos += n;
1437 nchars += n;
1438 }
1439 }
1440
1441 return nchars;
1442}
1443
1444static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1445 char *buf)
1446{
1447 ssize_t nchars;
1448 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1449
1450 mutex_lock(&matrix_dev->mdevs_lock);
1451 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1452 mutex_unlock(&matrix_dev->mdevs_lock);
1453
1454 return nchars;
1455}
1456static DEVICE_ATTR_RO(matrix);
1457
1458static ssize_t guest_matrix_show(struct device *dev,
1459 struct device_attribute *attr, char *buf)
1460{
1461 ssize_t nchars;
1462 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1463
1464 mutex_lock(&matrix_dev->mdevs_lock);
1465 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1466 mutex_unlock(&matrix_dev->mdevs_lock);
1467
1468 return nchars;
1469}
1470static DEVICE_ATTR_RO(guest_matrix);
1471
1472static struct attribute *vfio_ap_mdev_attrs[] = {
1473 &dev_attr_assign_adapter.attr,
1474 &dev_attr_unassign_adapter.attr,
1475 &dev_attr_assign_domain.attr,
1476 &dev_attr_unassign_domain.attr,
1477 &dev_attr_assign_control_domain.attr,
1478 &dev_attr_unassign_control_domain.attr,
1479 &dev_attr_control_domains.attr,
1480 &dev_attr_matrix.attr,
1481 &dev_attr_guest_matrix.attr,
1482 NULL,
1483};
1484
1485static struct attribute_group vfio_ap_mdev_attr_group = {
1486 .attrs = vfio_ap_mdev_attrs
1487};
1488
1489static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1490 &vfio_ap_mdev_attr_group,
1491 NULL
1492};
1493
1494/**
1495 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1496 * to manage AP resources for the guest whose state is represented by @kvm
1497 *
1498 * @matrix_mdev: a mediated matrix device
1499 * @kvm: reference to KVM instance
1500 *
1501 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1502 * otherwise, returns an -EPERM.
1503 */
1504static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1505 struct kvm *kvm)
1506{
1507 struct ap_matrix_mdev *m;
1508
1509 if (kvm->arch.crypto.crycbd) {
1510 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1511 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1512 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1513
1514 get_update_locks_for_kvm(kvm);
1515
1516 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1517 if (m != matrix_mdev && m->kvm == kvm) {
1518 release_update_locks_for_kvm(kvm);
1519 return -EPERM;
1520 }
1521 }
1522
1523 kvm_get_kvm(kvm);
1524 matrix_mdev->kvm = kvm;
1525 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1526
1527 release_update_locks_for_kvm(kvm);
1528 }
1529
1530 return 0;
1531}
1532
1533static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1534{
1535 struct ap_queue_table *qtable = &matrix_mdev->qtable;
1536 struct vfio_ap_queue *q;
1537 int loop_cursor;
1538
1539 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1540 if (q->saved_iova >= iova && q->saved_iova < iova + length)
1541 vfio_ap_irq_disable(q);
1542 }
1543}
1544
1545static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1546 u64 length)
1547{
1548 struct ap_matrix_mdev *matrix_mdev =
1549 container_of(vdev, struct ap_matrix_mdev, vdev);
1550
1551 mutex_lock(&matrix_dev->mdevs_lock);
1552
1553 unmap_iova(matrix_mdev, iova, length);
1554
1555 mutex_unlock(&matrix_dev->mdevs_lock);
1556}
1557
1558/**
1559 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1560 * by @matrix_mdev.
1561 *
1562 * @matrix_mdev: a matrix mediated device
1563 */
1564static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1565{
1566 struct kvm *kvm = matrix_mdev->kvm;
1567
1568 if (kvm && kvm->arch.crypto.crycbd) {
1569 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1570 kvm->arch.crypto.pqap_hook = NULL;
1571 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1572
1573 get_update_locks_for_kvm(kvm);
1574
1575 kvm_arch_crypto_clear_masks(kvm);
1576 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1577 kvm_put_kvm(kvm);
1578 matrix_mdev->kvm = NULL;
1579
1580 release_update_locks_for_kvm(kvm);
1581 }
1582}
1583
1584static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1585{
1586 struct ap_queue *queue;
1587 struct vfio_ap_queue *q = NULL;
1588
1589 queue = ap_get_qdev(apqn);
1590 if (!queue)
1591 return NULL;
1592
1593 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1594 q = dev_get_drvdata(&queue->ap_dev.device);
1595
1596 put_device(&queue->ap_dev.device);
1597
1598 return q;
1599}
1600
1601static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1602 unsigned int retry)
1603{
1604 struct ap_queue_status status;
1605 int ret;
1606 int retry2 = 2;
1607
1608 if (!q)
1609 return 0;
1610retry_zapq:
1611 status = ap_zapq(q->apqn);
1612 q->reset_rc = status.response_code;
1613 switch (status.response_code) {
1614 case AP_RESPONSE_NORMAL:
1615 ret = 0;
1616 break;
1617 case AP_RESPONSE_RESET_IN_PROGRESS:
1618 if (retry--) {
1619 msleep(20);
1620 goto retry_zapq;
1621 }
1622 ret = -EBUSY;
1623 break;
1624 case AP_RESPONSE_Q_NOT_AVAIL:
1625 case AP_RESPONSE_DECONFIGURED:
1626 case AP_RESPONSE_CHECKSTOPPED:
1627 WARN_ONCE(status.irq_enabled,
1628 "PQAP/ZAPQ for %02x.%04x failed with rc=%u while IRQ enabled",
1629 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1630 status.response_code);
1631 ret = -EBUSY;
1632 goto free_resources;
1633 default:
1634 /* things are really broken, give up */
1635 WARN(true,
1636 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1637 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1638 status.response_code);
1639 return -EIO;
1640 }
1641
1642 /* wait for the reset to take effect */
1643 while (retry2--) {
1644 if (status.queue_empty && !status.irq_enabled)
1645 break;
1646 msleep(20);
1647 status = ap_tapq(q->apqn, NULL);
1648 }
1649 WARN_ONCE(retry2 <= 0, "unable to verify reset of queue %02x.%04x",
1650 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn));
1651
1652free_resources:
1653 vfio_ap_free_aqic_resources(q);
1654
1655 return ret;
1656}
1657
1658static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable)
1659{
1660 int ret, loop_cursor, rc = 0;
1661 struct vfio_ap_queue *q;
1662
1663 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1664 ret = vfio_ap_mdev_reset_queue(q, 1);
1665 /*
1666 * Regardless whether a queue turns out to be busy, or
1667 * is not operational, we need to continue resetting
1668 * the remaining queues.
1669 */
1670 if (ret)
1671 rc = ret;
1672 }
1673
1674 return rc;
1675}
1676
1677static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1678{
1679 struct ap_matrix_mdev *matrix_mdev =
1680 container_of(vdev, struct ap_matrix_mdev, vdev);
1681
1682 if (!vdev->kvm)
1683 return -EINVAL;
1684
1685 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
1686}
1687
1688static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1689{
1690 struct ap_matrix_mdev *matrix_mdev =
1691 container_of(vdev, struct ap_matrix_mdev, vdev);
1692
1693 vfio_ap_mdev_unset_kvm(matrix_mdev);
1694}
1695
1696static int vfio_ap_mdev_get_device_info(unsigned long arg)
1697{
1698 unsigned long minsz;
1699 struct vfio_device_info info;
1700
1701 minsz = offsetofend(struct vfio_device_info, num_irqs);
1702
1703 if (copy_from_user(&info, (void __user *)arg, minsz))
1704 return -EFAULT;
1705
1706 if (info.argsz < minsz)
1707 return -EINVAL;
1708
1709 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1710 info.num_regions = 0;
1711 info.num_irqs = 0;
1712
1713 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1714}
1715
1716static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1717 unsigned int cmd, unsigned long arg)
1718{
1719 struct ap_matrix_mdev *matrix_mdev =
1720 container_of(vdev, struct ap_matrix_mdev, vdev);
1721 int ret;
1722
1723 mutex_lock(&matrix_dev->mdevs_lock);
1724 switch (cmd) {
1725 case VFIO_DEVICE_GET_INFO:
1726 ret = vfio_ap_mdev_get_device_info(arg);
1727 break;
1728 case VFIO_DEVICE_RESET:
1729 ret = vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1730 break;
1731 default:
1732 ret = -EOPNOTSUPP;
1733 break;
1734 }
1735 mutex_unlock(&matrix_dev->mdevs_lock);
1736
1737 return ret;
1738}
1739
1740static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
1741{
1742 struct ap_matrix_mdev *matrix_mdev;
1743 unsigned long apid = AP_QID_CARD(q->apqn);
1744 unsigned long apqi = AP_QID_QUEUE(q->apqn);
1745
1746 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
1747 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
1748 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
1749 return matrix_mdev;
1750 }
1751
1752 return NULL;
1753}
1754
1755static ssize_t status_show(struct device *dev,
1756 struct device_attribute *attr,
1757 char *buf)
1758{
1759 ssize_t nchars = 0;
1760 struct vfio_ap_queue *q;
1761 struct ap_matrix_mdev *matrix_mdev;
1762 struct ap_device *apdev = to_ap_dev(dev);
1763
1764 mutex_lock(&matrix_dev->mdevs_lock);
1765 q = dev_get_drvdata(&apdev->device);
1766 matrix_mdev = vfio_ap_mdev_for_queue(q);
1767
1768 if (matrix_mdev) {
1769 if (matrix_mdev->kvm)
1770 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1771 AP_QUEUE_IN_USE);
1772 else
1773 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1774 AP_QUEUE_ASSIGNED);
1775 } else {
1776 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1777 AP_QUEUE_UNASSIGNED);
1778 }
1779
1780 mutex_unlock(&matrix_dev->mdevs_lock);
1781
1782 return nchars;
1783}
1784
1785static DEVICE_ATTR_RO(status);
1786
1787static struct attribute *vfio_queue_attrs[] = {
1788 &dev_attr_status.attr,
1789 NULL,
1790};
1791
1792static const struct attribute_group vfio_queue_attr_group = {
1793 .attrs = vfio_queue_attrs,
1794};
1795
1796static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1797 .init = vfio_ap_mdev_init_dev,
1798 .open_device = vfio_ap_mdev_open_device,
1799 .close_device = vfio_ap_mdev_close_device,
1800 .ioctl = vfio_ap_mdev_ioctl,
1801 .dma_unmap = vfio_ap_mdev_dma_unmap,
1802 .bind_iommufd = vfio_iommufd_emulated_bind,
1803 .unbind_iommufd = vfio_iommufd_emulated_unbind,
1804 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
1805};
1806
1807static struct mdev_driver vfio_ap_matrix_driver = {
1808 .device_api = VFIO_DEVICE_API_AP_STRING,
1809 .max_instances = MAX_ZDEV_ENTRIES_EXT,
1810 .driver = {
1811 .name = "vfio_ap_mdev",
1812 .owner = THIS_MODULE,
1813 .mod_name = KBUILD_MODNAME,
1814 .dev_groups = vfio_ap_mdev_attr_groups,
1815 },
1816 .probe = vfio_ap_mdev_probe,
1817 .remove = vfio_ap_mdev_remove,
1818};
1819
1820int vfio_ap_mdev_register(void)
1821{
1822 int ret;
1823
1824 ret = mdev_register_driver(&vfio_ap_matrix_driver);
1825 if (ret)
1826 return ret;
1827
1828 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
1829 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
1830 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
1831 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
1832 &vfio_ap_matrix_driver,
1833 matrix_dev->mdev_types, 1);
1834 if (ret)
1835 goto err_driver;
1836 return 0;
1837
1838err_driver:
1839 mdev_unregister_driver(&vfio_ap_matrix_driver);
1840 return ret;
1841}
1842
1843void vfio_ap_mdev_unregister(void)
1844{
1845 mdev_unregister_parent(&matrix_dev->parent);
1846 mdev_unregister_driver(&vfio_ap_matrix_driver);
1847}
1848
1849int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
1850{
1851 int ret;
1852 struct vfio_ap_queue *q;
1853 struct ap_matrix_mdev *matrix_mdev;
1854
1855 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
1856 if (ret)
1857 return ret;
1858
1859 q = kzalloc(sizeof(*q), GFP_KERNEL);
1860 if (!q)
1861 return -ENOMEM;
1862
1863 q->apqn = to_ap_queue(&apdev->device)->qid;
1864 q->saved_isc = VFIO_AP_ISC_INVALID;
1865 matrix_mdev = get_update_locks_by_apqn(q->apqn);
1866
1867 if (matrix_mdev) {
1868 vfio_ap_mdev_link_queue(matrix_mdev, q);
1869
1870 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm,
1871 matrix_mdev->matrix.aqm,
1872 matrix_mdev))
1873 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1874 }
1875 dev_set_drvdata(&apdev->device, q);
1876 release_update_locks_for_mdev(matrix_mdev);
1877
1878 return 0;
1879}
1880
1881void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
1882{
1883 unsigned long apid, apqi;
1884 struct vfio_ap_queue *q;
1885 struct ap_matrix_mdev *matrix_mdev;
1886
1887 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
1888 q = dev_get_drvdata(&apdev->device);
1889 get_update_locks_for_queue(q);
1890 matrix_mdev = q->matrix_mdev;
1891
1892 if (matrix_mdev) {
1893 vfio_ap_unlink_queue_fr_mdev(q);
1894
1895 apid = AP_QID_CARD(q->apqn);
1896 apqi = AP_QID_QUEUE(q->apqn);
1897
1898 /*
1899 * If the queue is assigned to the guest's APCB, then remove
1900 * the adapter's APID from the APCB and hot it into the guest.
1901 */
1902 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1903 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1904 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1905 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1906 }
1907 }
1908
1909 vfio_ap_mdev_reset_queue(q, 1);
1910 dev_set_drvdata(&apdev->device, NULL);
1911 kfree(q);
1912 release_update_locks_for_mdev(matrix_mdev);
1913}
1914
1915/**
1916 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
1917 * assigned to a mediated device under the control
1918 * of the vfio_ap device driver.
1919 *
1920 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
1921 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
1922 *
1923 * Return:
1924 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
1925 * assigned to a mediated device under the control of the vfio_ap
1926 * device driver.
1927 * * Otherwise, return 0.
1928 */
1929int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
1930{
1931 int ret;
1932
1933 mutex_lock(&matrix_dev->guests_lock);
1934 mutex_lock(&matrix_dev->mdevs_lock);
1935 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
1936 mutex_unlock(&matrix_dev->mdevs_lock);
1937 mutex_unlock(&matrix_dev->guests_lock);
1938
1939 return ret;
1940}
1941
1942/**
1943 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
1944 * domains that have been removed from the host's
1945 * AP configuration from a guest.
1946 *
1947 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
1948 * @aprem: the adapters that have been removed from the host's AP configuration
1949 * @aqrem: the domains that have been removed from the host's AP configuration
1950 * @cdrem: the control domains that have been removed from the host's AP
1951 * configuration.
1952 */
1953static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
1954 unsigned long *aprem,
1955 unsigned long *aqrem,
1956 unsigned long *cdrem)
1957{
1958 int do_hotplug = 0;
1959
1960 if (!bitmap_empty(aprem, AP_DEVICES)) {
1961 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
1962 matrix_mdev->shadow_apcb.apm,
1963 aprem, AP_DEVICES);
1964 }
1965
1966 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
1967 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
1968 matrix_mdev->shadow_apcb.aqm,
1969 aqrem, AP_DEVICES);
1970 }
1971
1972 if (!bitmap_empty(cdrem, AP_DOMAINS))
1973 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
1974 matrix_mdev->shadow_apcb.adm,
1975 cdrem, AP_DOMAINS);
1976
1977 if (do_hotplug)
1978 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1979}
1980
1981/**
1982 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
1983 * domains and control domains that have been removed
1984 * from the host AP configuration and unplugs them
1985 * from those guests.
1986 *
1987 * @ap_remove: bitmap specifying which adapters have been removed from the host
1988 * config.
1989 * @aq_remove: bitmap specifying which domains have been removed from the host
1990 * config.
1991 * @cd_remove: bitmap specifying which control domains have been removed from
1992 * the host config.
1993 */
1994static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
1995 unsigned long *aq_remove,
1996 unsigned long *cd_remove)
1997{
1998 struct ap_matrix_mdev *matrix_mdev;
1999 DECLARE_BITMAP(aprem, AP_DEVICES);
2000 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2001 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2002 int do_remove = 0;
2003
2004 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2005 mutex_lock(&matrix_mdev->kvm->lock);
2006 mutex_lock(&matrix_dev->mdevs_lock);
2007
2008 do_remove |= bitmap_and(aprem, ap_remove,
2009 matrix_mdev->matrix.apm,
2010 AP_DEVICES);
2011 do_remove |= bitmap_and(aqrem, aq_remove,
2012 matrix_mdev->matrix.aqm,
2013 AP_DOMAINS);
2014 do_remove |= bitmap_andnot(cdrem, cd_remove,
2015 matrix_mdev->matrix.adm,
2016 AP_DOMAINS);
2017
2018 if (do_remove)
2019 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2020 cdrem);
2021
2022 mutex_unlock(&matrix_dev->mdevs_lock);
2023 mutex_unlock(&matrix_mdev->kvm->lock);
2024 }
2025}
2026
2027/**
2028 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2029 * control domains from the host AP configuration
2030 * by unplugging them from the guests that are
2031 * using them.
2032 * @cur_config_info: the current host AP configuration information
2033 * @prev_config_info: the previous host AP configuration information
2034 */
2035static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2036 struct ap_config_info *prev_config_info)
2037{
2038 int do_remove;
2039 DECLARE_BITMAP(aprem, AP_DEVICES);
2040 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2041 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2042
2043 do_remove = bitmap_andnot(aprem,
2044 (unsigned long *)prev_config_info->apm,
2045 (unsigned long *)cur_config_info->apm,
2046 AP_DEVICES);
2047 do_remove |= bitmap_andnot(aqrem,
2048 (unsigned long *)prev_config_info->aqm,
2049 (unsigned long *)cur_config_info->aqm,
2050 AP_DEVICES);
2051 do_remove |= bitmap_andnot(cdrem,
2052 (unsigned long *)prev_config_info->adm,
2053 (unsigned long *)cur_config_info->adm,
2054 AP_DEVICES);
2055
2056 if (do_remove)
2057 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2058}
2059
2060/**
2061 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2062 * are older than AP type 10 (CEX4).
2063 * @apm: a bitmap of the APIDs to examine
2064 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2065 */
2066static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2067{
2068 bool apid_cleared;
2069 struct ap_queue_status status;
2070 unsigned long apid, apqi, info;
2071 int qtype, qtype_mask = 0xff000000;
2072
2073 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2074 apid_cleared = false;
2075
2076 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2077 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2078 switch (status.response_code) {
2079 /*
2080 * According to the architecture in each case
2081 * below, the queue's info should be filled.
2082 */
2083 case AP_RESPONSE_NORMAL:
2084 case AP_RESPONSE_RESET_IN_PROGRESS:
2085 case AP_RESPONSE_DECONFIGURED:
2086 case AP_RESPONSE_CHECKSTOPPED:
2087 case AP_RESPONSE_BUSY:
2088 qtype = info & qtype_mask;
2089
2090 /*
2091 * The vfio_ap device driver only
2092 * supports CEX4 and newer adapters, so
2093 * remove the APID if the adapter is
2094 * older than a CEX4.
2095 */
2096 if (qtype < AP_DEVICE_TYPE_CEX4) {
2097 clear_bit_inv(apid, apm);
2098 apid_cleared = true;
2099 }
2100
2101 break;
2102
2103 default:
2104 /*
2105 * If we don't know the adapter type,
2106 * clear its APID since it can't be
2107 * determined whether the vfio_ap
2108 * device driver supports it.
2109 */
2110 clear_bit_inv(apid, apm);
2111 apid_cleared = true;
2112 break;
2113 }
2114
2115 /*
2116 * If we've already cleared the APID from the apm, there
2117 * is no need to continue examining the remainin AP
2118 * queues to determine the type of the adapter.
2119 */
2120 if (apid_cleared)
2121 continue;
2122 }
2123 }
2124}
2125
2126/**
2127 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2128 * control domains that have been added to the host's
2129 * AP configuration for each matrix mdev to which they
2130 * are assigned.
2131 *
2132 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2133 * configuration.
2134 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2135 * configuration.
2136 * @adm_add: a bitmap specifying the control domains that have been added to the
2137 * AP configuration.
2138 */
2139static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2140 unsigned long *adm_add)
2141{
2142 struct ap_matrix_mdev *matrix_mdev;
2143
2144 if (list_empty(&matrix_dev->mdev_list))
2145 return;
2146
2147 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2148
2149 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2150 bitmap_and(matrix_mdev->apm_add,
2151 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2152 bitmap_and(matrix_mdev->aqm_add,
2153 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2154 bitmap_and(matrix_mdev->adm_add,
2155 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2156 }
2157}
2158
2159/**
2160 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2161 * control domains to the host AP configuration
2162 * by updating the bitmaps that specify what adapters,
2163 * domains and control domains have been added so they
2164 * can be hot plugged into the guest when the AP bus
2165 * scan completes (see vfio_ap_on_scan_complete
2166 * function).
2167 * @cur_config_info: the current AP configuration information
2168 * @prev_config_info: the previous AP configuration information
2169 */
2170static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2171 struct ap_config_info *prev_config_info)
2172{
2173 bool do_add;
2174 DECLARE_BITMAP(apm_add, AP_DEVICES);
2175 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2176 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2177
2178 do_add = bitmap_andnot(apm_add,
2179 (unsigned long *)cur_config_info->apm,
2180 (unsigned long *)prev_config_info->apm,
2181 AP_DEVICES);
2182 do_add |= bitmap_andnot(aqm_add,
2183 (unsigned long *)cur_config_info->aqm,
2184 (unsigned long *)prev_config_info->aqm,
2185 AP_DOMAINS);
2186 do_add |= bitmap_andnot(adm_add,
2187 (unsigned long *)cur_config_info->adm,
2188 (unsigned long *)prev_config_info->adm,
2189 AP_DOMAINS);
2190
2191 if (do_add)
2192 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2193}
2194
2195/**
2196 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2197 * configuration.
2198 *
2199 * @cur_cfg_info: the current host AP configuration
2200 * @prev_cfg_info: the previous host AP configuration
2201 */
2202void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2203 struct ap_config_info *prev_cfg_info)
2204{
2205 if (!cur_cfg_info || !prev_cfg_info)
2206 return;
2207
2208 mutex_lock(&matrix_dev->guests_lock);
2209
2210 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2211 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2212 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2213
2214 mutex_unlock(&matrix_dev->guests_lock);
2215}
2216
2217static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2218{
2219 bool do_hotplug = false;
2220 int filter_domains = 0;
2221 int filter_adapters = 0;
2222 DECLARE_BITMAP(apm, AP_DEVICES);
2223 DECLARE_BITMAP(aqm, AP_DOMAINS);
2224
2225 mutex_lock(&matrix_mdev->kvm->lock);
2226 mutex_lock(&matrix_dev->mdevs_lock);
2227
2228 filter_adapters = bitmap_and(apm, matrix_mdev->matrix.apm,
2229 matrix_mdev->apm_add, AP_DEVICES);
2230 filter_domains = bitmap_and(aqm, matrix_mdev->matrix.aqm,
2231 matrix_mdev->aqm_add, AP_DOMAINS);
2232
2233 if (filter_adapters && filter_domains)
2234 do_hotplug |= vfio_ap_mdev_filter_matrix(apm, aqm, matrix_mdev);
2235 else if (filter_adapters)
2236 do_hotplug |=
2237 vfio_ap_mdev_filter_matrix(apm,
2238 matrix_mdev->shadow_apcb.aqm,
2239 matrix_mdev);
2240 else
2241 do_hotplug |=
2242 vfio_ap_mdev_filter_matrix(matrix_mdev->shadow_apcb.apm,
2243 aqm, matrix_mdev);
2244
2245 if (bitmap_intersects(matrix_mdev->matrix.adm, matrix_mdev->adm_add,
2246 AP_DOMAINS))
2247 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2248
2249 if (do_hotplug)
2250 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2251
2252 mutex_unlock(&matrix_dev->mdevs_lock);
2253 mutex_unlock(&matrix_mdev->kvm->lock);
2254}
2255
2256void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2257 struct ap_config_info *old_config_info)
2258{
2259 struct ap_matrix_mdev *matrix_mdev;
2260
2261 mutex_lock(&matrix_dev->guests_lock);
2262
2263 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2264 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2265 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2266 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2267 continue;
2268
2269 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2270 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2271 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2272 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2273 }
2274
2275 mutex_unlock(&matrix_dev->guests_lock);
2276}
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Adjunct processor matrix VFIO device driver callbacks.
4 *
5 * Copyright IBM Corp. 2018
6 *
7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8 * Halil Pasic <pasic@linux.ibm.com>
9 * Pierre Morel <pmorel@linux.ibm.com>
10 */
11#include <linux/string.h>
12#include <linux/vfio.h>
13#include <linux/device.h>
14#include <linux/list.h>
15#include <linux/ctype.h>
16#include <linux/bitops.h>
17#include <linux/kvm_host.h>
18#include <linux/module.h>
19#include <linux/uuid.h>
20#include <asm/kvm.h>
21#include <asm/zcrypt.h>
22
23#include "vfio_ap_private.h"
24#include "vfio_ap_debug.h"
25
26#define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
27#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
28
29#define AP_QUEUE_ASSIGNED "assigned"
30#define AP_QUEUE_UNASSIGNED "unassigned"
31#define AP_QUEUE_IN_USE "in use"
32
33#define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */
34
35static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
36static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);
37static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
38static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
39static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
40
41/**
42 * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
43 * KVM guest's APCB in the proper order.
44 *
45 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
46 *
47 * The proper locking order is:
48 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
49 * guest's APCB.
50 * 2. kvm->lock: required to update a guest's APCB
51 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
52 *
53 * Note: If @kvm is NULL, the KVM lock will not be taken.
54 */
55static inline void get_update_locks_for_kvm(struct kvm *kvm)
56{
57 mutex_lock(&matrix_dev->guests_lock);
58 if (kvm)
59 mutex_lock(&kvm->lock);
60 mutex_lock(&matrix_dev->mdevs_lock);
61}
62
63/**
64 * release_update_locks_for_kvm: Release the locks used to dynamically update a
65 * KVM guest's APCB in the proper order.
66 *
67 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
68 *
69 * The proper unlocking order is:
70 * 1. matrix_dev->mdevs_lock
71 * 2. kvm->lock
72 * 3. matrix_dev->guests_lock
73 *
74 * Note: If @kvm is NULL, the KVM lock will not be released.
75 */
76static inline void release_update_locks_for_kvm(struct kvm *kvm)
77{
78 mutex_unlock(&matrix_dev->mdevs_lock);
79 if (kvm)
80 mutex_unlock(&kvm->lock);
81 mutex_unlock(&matrix_dev->guests_lock);
82}
83
84/**
85 * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
86 * KVM guest's APCB in the proper order.
87 *
88 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
89 * configuration data to use to update a KVM guest's APCB.
90 *
91 * The proper locking order is:
92 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
93 * guest's APCB.
94 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
95 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
96 *
97 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
98 * lock will not be taken.
99 */
100static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
101{
102 mutex_lock(&matrix_dev->guests_lock);
103 if (matrix_mdev && matrix_mdev->kvm)
104 mutex_lock(&matrix_mdev->kvm->lock);
105 mutex_lock(&matrix_dev->mdevs_lock);
106}
107
108/**
109 * release_update_locks_for_mdev: Release the locks used to dynamically update a
110 * KVM guest's APCB in the proper order.
111 *
112 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
113 * configuration data to use to update a KVM guest's APCB.
114 *
115 * The proper unlocking order is:
116 * 1. matrix_dev->mdevs_lock
117 * 2. matrix_mdev->kvm->lock
118 * 3. matrix_dev->guests_lock
119 *
120 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
121 * lock will not be released.
122 */
123static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
124{
125 mutex_unlock(&matrix_dev->mdevs_lock);
126 if (matrix_mdev && matrix_mdev->kvm)
127 mutex_unlock(&matrix_mdev->kvm->lock);
128 mutex_unlock(&matrix_dev->guests_lock);
129}
130
131/**
132 * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
133 * acquire the locks required to update the APCB of
134 * the KVM guest to which the mdev is attached.
135 *
136 * @apqn: the APQN of a queue device.
137 *
138 * The proper locking order is:
139 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
140 * guest's APCB.
141 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
142 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
143 *
144 * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
145 * will not be taken.
146 *
147 * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
148 * is not assigned to an ap_matrix_mdev.
149 */
150static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
151{
152 struct ap_matrix_mdev *matrix_mdev;
153
154 mutex_lock(&matrix_dev->guests_lock);
155
156 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
157 if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
158 test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
159 if (matrix_mdev->kvm)
160 mutex_lock(&matrix_mdev->kvm->lock);
161
162 mutex_lock(&matrix_dev->mdevs_lock);
163
164 return matrix_mdev;
165 }
166 }
167
168 mutex_lock(&matrix_dev->mdevs_lock);
169
170 return NULL;
171}
172
173/**
174 * get_update_locks_for_queue: get the locks required to update the APCB of the
175 * KVM guest to which the matrix mdev linked to a
176 * vfio_ap_queue object is attached.
177 *
178 * @q: a pointer to a vfio_ap_queue object.
179 *
180 * The proper locking order is:
181 * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
182 * KVM guest's APCB.
183 * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB
184 * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev
185 *
186 * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
187 * will not be taken.
188 */
189static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
190{
191 mutex_lock(&matrix_dev->guests_lock);
192 if (q->matrix_mdev && q->matrix_mdev->kvm)
193 mutex_lock(&q->matrix_mdev->kvm->lock);
194 mutex_lock(&matrix_dev->mdevs_lock);
195}
196
197/**
198 * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
199 * hash table of queues assigned to a matrix mdev
200 * @matrix_mdev: the matrix mdev
201 * @apqn: The APQN of a queue device
202 *
203 * Return: the pointer to the vfio_ap_queue struct representing the queue or
204 * NULL if the queue is not assigned to @matrix_mdev
205 */
206static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
207 struct ap_matrix_mdev *matrix_mdev,
208 int apqn)
209{
210 struct vfio_ap_queue *q;
211
212 hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
213 apqn) {
214 if (q && q->apqn == apqn)
215 return q;
216 }
217
218 return NULL;
219}
220
221/**
222 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
223 * @apqn: The AP Queue number
224 *
225 * Checks the IRQ bit for the status of this APQN using ap_tapq.
226 * Returns if the ap_tapq function succeeded and the bit is clear.
227 * Returns if ap_tapq function failed with invalid, deconfigured or
228 * checkstopped AP.
229 * Otherwise retries up to 5 times after waiting 20ms.
230 */
231static void vfio_ap_wait_for_irqclear(int apqn)
232{
233 struct ap_queue_status status;
234 int retry = 5;
235
236 do {
237 status = ap_tapq(apqn, NULL);
238 switch (status.response_code) {
239 case AP_RESPONSE_NORMAL:
240 case AP_RESPONSE_RESET_IN_PROGRESS:
241 if (!status.irq_enabled)
242 return;
243 fallthrough;
244 case AP_RESPONSE_BUSY:
245 msleep(20);
246 break;
247 case AP_RESPONSE_Q_NOT_AVAIL:
248 case AP_RESPONSE_DECONFIGURED:
249 case AP_RESPONSE_CHECKSTOPPED:
250 default:
251 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
252 status.response_code, apqn);
253 return;
254 }
255 } while (--retry);
256
257 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
258 __func__, status.response_code, apqn);
259}
260
261/**
262 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
263 * @q: The vfio_ap_queue
264 *
265 * Unregisters the ISC in the GIB when the saved ISC not invalid.
266 * Unpins the guest's page holding the NIB when it exists.
267 * Resets the saved_iova and saved_isc to invalid values.
268 */
269static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
270{
271 if (!q)
272 return;
273 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
274 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
275 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
276 q->saved_isc = VFIO_AP_ISC_INVALID;
277 }
278 if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
279 vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
280 q->saved_iova = 0;
281 }
282}
283
284/**
285 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
286 * @q: The vfio_ap_queue
287 *
288 * Uses ap_aqic to disable the interruption and in case of success, reset
289 * in progress or IRQ disable command already proceeded: calls
290 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
291 * and calls vfio_ap_free_aqic_resources() to free the resources associated
292 * with the AP interrupt handling.
293 *
294 * In the case the AP is busy, or a reset is in progress,
295 * retries after 20ms, up to 5 times.
296 *
297 * Returns if ap_aqic function failed with invalid, deconfigured or
298 * checkstopped AP.
299 *
300 * Return: &struct ap_queue_status
301 */
302static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
303{
304 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
305 struct ap_queue_status status;
306 int retries = 5;
307
308 do {
309 status = ap_aqic(q->apqn, aqic_gisa, 0);
310 switch (status.response_code) {
311 case AP_RESPONSE_OTHERWISE_CHANGED:
312 case AP_RESPONSE_NORMAL:
313 vfio_ap_wait_for_irqclear(q->apqn);
314 goto end_free;
315 case AP_RESPONSE_RESET_IN_PROGRESS:
316 case AP_RESPONSE_BUSY:
317 msleep(20);
318 break;
319 case AP_RESPONSE_Q_NOT_AVAIL:
320 case AP_RESPONSE_DECONFIGURED:
321 case AP_RESPONSE_CHECKSTOPPED:
322 case AP_RESPONSE_INVALID_ADDRESS:
323 default:
324 /* All cases in default means AP not operational */
325 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
326 status.response_code);
327 goto end_free;
328 }
329 } while (retries--);
330
331 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
332 status.response_code);
333end_free:
334 vfio_ap_free_aqic_resources(q);
335 return status;
336}
337
338/**
339 * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
340 *
341 * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
342 * @nib: the location for storing the nib address.
343 *
344 * When the PQAP(AQIC) instruction is executed, general register 2 contains the
345 * address of the notification indicator byte (nib) used for IRQ notification.
346 * This function parses and validates the nib from gr2.
347 *
348 * Return: returns zero if the nib address is a valid; otherwise, returns
349 * -EINVAL.
350 */
351static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
352{
353 *nib = vcpu->run->s.regs.gprs[2];
354
355 if (!*nib)
356 return -EINVAL;
357 if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
358 return -EINVAL;
359
360 return 0;
361}
362
363/**
364 * ensure_nib_shared() - Ensure the address of the NIB is secure and shared
365 * @addr: the physical (absolute) address of the NIB
366 *
367 * This function checks whether the NIB page, which has been pinned with
368 * vfio_pin_pages(), is a shared page belonging to a secure guest.
369 *
370 * It will call uv_pin_shared() on it; if the page was already pinned shared
371 * (i.e. if the NIB belongs to a secure guest and is shared), then 0
372 * (success) is returned. If the NIB was not shared, vfio_pin_pages() had
373 * exported it and now it does not belong to the secure guest anymore. In
374 * that case, an error is returned.
375 *
376 * Context: the NIB (at physical address @addr) has to be pinned with
377 * vfio_pin_pages() before calling this function.
378 *
379 * Return: 0 in case of success, otherwise an error < 0.
380 */
381static int ensure_nib_shared(unsigned long addr)
382{
383 /*
384 * The nib has to be located in shared storage since guest and
385 * host access it. vfio_pin_pages() will do a pin shared and
386 * if that fails (possibly because it's not a shared page) it
387 * calls export. We try to do a second pin shared here so that
388 * the UV gives us an error code if we try to pin a non-shared
389 * page.
390 *
391 * If the page is already pinned shared the UV will return a success.
392 */
393 return uv_pin_shared(addr);
394}
395
396/**
397 * vfio_ap_irq_enable - Enable Interruption for a APQN
398 *
399 * @q: the vfio_ap_queue holding AQIC parameters
400 * @isc: the guest ISC to register with the GIB interface
401 * @vcpu: the vcpu object containing the registers specifying the parameters
402 * passed to the PQAP(AQIC) instruction.
403 *
404 * Pin the NIB saved in *q
405 * Register the guest ISC to GIB interface and retrieve the
406 * host ISC to issue the host side PQAP/AQIC
407 *
408 * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
409 * vfio_pin_pages or kvm_s390_gisc_register failed.
410 *
411 * Otherwise return the ap_queue_status returned by the ap_aqic(),
412 * all retry handling will be done by the guest.
413 *
414 * Return: &struct ap_queue_status
415 */
416static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
417 int isc,
418 struct kvm_vcpu *vcpu)
419{
420 union ap_qirq_ctrl aqic_gisa = { .value = 0 };
421 struct ap_queue_status status = {};
422 struct kvm_s390_gisa *gisa;
423 struct page *h_page;
424 int nisc;
425 struct kvm *kvm;
426 phys_addr_t h_nib;
427 dma_addr_t nib;
428 int ret;
429
430 /* Verify that the notification indicator byte address is valid */
431 if (vfio_ap_validate_nib(vcpu, &nib)) {
432 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
433 __func__, &nib, q->apqn);
434
435 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
436 return status;
437 }
438
439 /* The pin will probably be successful even if the NIB was not shared */
440 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
441 IOMMU_READ | IOMMU_WRITE, &h_page);
442 switch (ret) {
443 case 1:
444 break;
445 default:
446 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
447 "nib=%pad, apqn=%#04x\n",
448 __func__, ret, &nib, q->apqn);
449
450 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
451 return status;
452 }
453
454 kvm = q->matrix_mdev->kvm;
455 gisa = kvm->arch.gisa_int.origin;
456
457 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
458 aqic_gisa.gisc = isc;
459
460 /* NIB in non-shared storage is a rc 6 for PV guests */
461 if (kvm_s390_pv_cpu_is_protected(vcpu) &&
462 ensure_nib_shared(h_nib & PAGE_MASK)) {
463 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
464 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
465 return status;
466 }
467
468 nisc = kvm_s390_gisc_register(kvm, isc);
469 if (nisc < 0) {
470 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
471 __func__, nisc, isc, q->apqn);
472
473 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
474 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
475 return status;
476 }
477
478 aqic_gisa.isc = nisc;
479 aqic_gisa.ir = 1;
480 aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
481
482 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
483 switch (status.response_code) {
484 case AP_RESPONSE_NORMAL:
485 /* See if we did clear older IRQ configuration */
486 vfio_ap_free_aqic_resources(q);
487 q->saved_iova = nib;
488 q->saved_isc = isc;
489 break;
490 case AP_RESPONSE_OTHERWISE_CHANGED:
491 /* We could not modify IRQ settings: clear new configuration */
492 ret = kvm_s390_gisc_unregister(kvm, isc);
493 if (ret)
494 VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n",
495 __func__, ret, isc, q->apqn);
496 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
497 break;
498 default:
499 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
500 status.response_code);
501 vfio_ap_irq_disable(q);
502 break;
503 }
504
505 if (status.response_code != AP_RESPONSE_NORMAL) {
506 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
507 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
508 "gisa=%#x, isc=%#x, apqn=%#04x\n",
509 __func__, status.response_code,
510 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
511 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
512 q->apqn);
513 }
514
515 return status;
516}
517
518/**
519 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
520 * of big endian elements that can be passed by
521 * value to an s390dbf sprintf event function to
522 * format a UUID string.
523 *
524 * @guid: the object containing the little endian guid
525 * @uuid: a six-element array of long values that can be passed by value as
526 * arguments for a formatting string specifying a UUID.
527 *
528 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
529 * event functions if the memory for the passed string is available as long as
530 * the debug feature exists. Since a mediated device can be removed at any
531 * time, it's name can not be used because %s passes the reference to the string
532 * in memory and the reference will go stale once the device is removed .
533 *
534 * The s390dbf string formatting function allows a maximum of 9 arguments for a
535 * message to be displayed in the 'sprintf' view. In order to use the bytes
536 * comprising the mediated device's UUID to display the mediated device name,
537 * they will have to be converted into an array whose elements can be passed by
538 * value to sprintf. For example:
539 *
540 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
541 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
542 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
543 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
544 */
545static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
546{
547 /*
548 * The input guid is ordered in little endian, so it needs to be
549 * reordered for displaying a UUID as a string. This specifies the
550 * guid indices in proper order.
551 */
552 uuid[0] = le32_to_cpup((__le32 *)guid);
553 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
554 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
555 uuid[3] = *((__u16 *)&guid->b[8]);
556 uuid[4] = *((__u16 *)&guid->b[10]);
557 uuid[5] = *((__u32 *)&guid->b[12]);
558}
559
560/**
561 * handle_pqap - PQAP instruction callback
562 *
563 * @vcpu: The vcpu on which we received the PQAP instruction
564 *
565 * Get the general register contents to initialize internal variables.
566 * REG[0]: APQN
567 * REG[1]: IR and ISC
568 * REG[2]: NIB
569 *
570 * Response.status may be set to following Response Code:
571 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
572 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
573 * - AP_RESPONSE_NORMAL (0) : in case of success
574 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
575 * We take the matrix_dev lock to ensure serialization on queues and
576 * mediated device access.
577 *
578 * Return: 0 if we could handle the request inside KVM.
579 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
580 */
581static int handle_pqap(struct kvm_vcpu *vcpu)
582{
583 uint64_t status;
584 uint16_t apqn;
585 unsigned long uuid[6];
586 struct vfio_ap_queue *q;
587 struct ap_queue_status qstatus = {
588 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
589 struct ap_matrix_mdev *matrix_mdev;
590
591 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
592
593 /* If we do not use the AIV facility just go to userland */
594 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
595 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
596 __func__, apqn, vcpu->arch.sie_block->eca);
597
598 return -EOPNOTSUPP;
599 }
600
601 mutex_lock(&matrix_dev->mdevs_lock);
602
603 if (!vcpu->kvm->arch.crypto.pqap_hook) {
604 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
605 __func__, apqn);
606
607 goto out_unlock;
608 }
609
610 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
611 struct ap_matrix_mdev, pqap_hook);
612
613 /* If the there is no guest using the mdev, there is nothing to do */
614 if (!matrix_mdev->kvm) {
615 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
616 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
617 __func__, uuid[0], uuid[1], uuid[2],
618 uuid[3], uuid[4], uuid[5], apqn);
619 goto out_unlock;
620 }
621
622 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
623 if (!q) {
624 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
625 __func__, AP_QID_CARD(apqn),
626 AP_QID_QUEUE(apqn));
627 goto out_unlock;
628 }
629
630 status = vcpu->run->s.regs.gprs[1];
631
632 /* If IR bit(16) is set we enable the interrupt */
633 if ((status >> (63 - 16)) & 0x01)
634 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
635 else
636 qstatus = vfio_ap_irq_disable(q);
637
638out_unlock:
639 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
640 vcpu->run->s.regs.gprs[1] >>= 32;
641 mutex_unlock(&matrix_dev->mdevs_lock);
642 return 0;
643}
644
645static void vfio_ap_matrix_init(struct ap_config_info *info,
646 struct ap_matrix *matrix)
647{
648 matrix->apm_max = info->apxa ? info->na : 63;
649 matrix->aqm_max = info->apxa ? info->nd : 15;
650 matrix->adm_max = info->apxa ? info->nd : 15;
651}
652
653static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
654{
655 if (matrix_mdev->kvm)
656 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
657 matrix_mdev->shadow_apcb.apm,
658 matrix_mdev->shadow_apcb.aqm,
659 matrix_mdev->shadow_apcb.adm);
660}
661
662static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
663{
664 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
665
666 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
667 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
668 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
669
670 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
671 AP_DOMAINS);
672}
673
674static bool _queue_passable(struct vfio_ap_queue *q)
675{
676 if (!q)
677 return false;
678
679 switch (q->reset_status.response_code) {
680 case AP_RESPONSE_NORMAL:
681 case AP_RESPONSE_DECONFIGURED:
682 case AP_RESPONSE_CHECKSTOPPED:
683 return true;
684 default:
685 return false;
686 }
687}
688
689/*
690 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
691 * to ensure no queue devices are passed through to
692 * the guest that are not bound to the vfio_ap
693 * device driver.
694 *
695 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
696 * @apm_filtered: a 256-bit bitmap for storing the APIDs filtered from the
697 * guest's AP configuration that are still in the host's AP
698 * configuration.
699 *
700 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
701 * driver, its APID will be filtered from the guest's APCB. The matrix
702 * structure precludes filtering an individual APQN, so its APID will be
703 * filtered. Consequently, all queues associated with the adapter that
704 * are in the host's AP configuration must be reset. If queues are
705 * subsequently made available again to the guest, they should re-appear
706 * in a reset state
707 *
708 * Return: a boolean value indicating whether the KVM guest's APCB was changed
709 * by the filtering or not.
710 */
711static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
712 unsigned long *apm_filtered)
713{
714 unsigned long apid, apqi, apqn;
715 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
716 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
717
718 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
719 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
720 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
721 bitmap_clear(apm_filtered, 0, AP_DEVICES);
722
723 /*
724 * Copy the adapters, domains and control domains to the shadow_apcb
725 * from the matrix mdev, but only those that are assigned to the host's
726 * AP configuration.
727 */
728 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
729 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
730 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
731 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
732
733 for_each_set_bit_inv(apid, matrix_mdev->shadow_apcb.apm, AP_DEVICES) {
734 for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
735 AP_DOMAINS) {
736 /*
737 * If the APQN is not bound to the vfio_ap device
738 * driver, then we can't assign it to the guest's
739 * AP configuration. The AP architecture won't
740 * allow filtering of a single APQN, so let's filter
741 * the APID since an adapter represents a physical
742 * hardware device.
743 */
744 apqn = AP_MKQID(apid, apqi);
745 if (!_queue_passable(vfio_ap_mdev_get_queue(matrix_mdev, apqn))) {
746 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
747
748 /*
749 * If the adapter was previously plugged into
750 * the guest, let's let the caller know that
751 * the APID was filtered.
752 */
753 if (test_bit_inv(apid, prev_shadow_apm))
754 set_bit_inv(apid, apm_filtered);
755
756 break;
757 }
758 }
759 }
760
761 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
762 AP_DEVICES) ||
763 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
764 AP_DOMAINS);
765}
766
767static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
768{
769 struct ap_matrix_mdev *matrix_mdev =
770 container_of(vdev, struct ap_matrix_mdev, vdev);
771
772 matrix_mdev->mdev = to_mdev_device(vdev->dev);
773 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
774 matrix_mdev->pqap_hook = handle_pqap;
775 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
776 hash_init(matrix_mdev->qtable.queues);
777
778 return 0;
779}
780
781static int vfio_ap_mdev_probe(struct mdev_device *mdev)
782{
783 struct ap_matrix_mdev *matrix_mdev;
784 int ret;
785
786 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
787 &vfio_ap_matrix_dev_ops);
788 if (IS_ERR(matrix_mdev))
789 return PTR_ERR(matrix_mdev);
790
791 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
792 if (ret)
793 goto err_put_vdev;
794 matrix_mdev->req_trigger = NULL;
795 dev_set_drvdata(&mdev->dev, matrix_mdev);
796 mutex_lock(&matrix_dev->mdevs_lock);
797 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
798 mutex_unlock(&matrix_dev->mdevs_lock);
799 return 0;
800
801err_put_vdev:
802 vfio_put_device(&matrix_mdev->vdev);
803 return ret;
804}
805
806static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
807 struct vfio_ap_queue *q)
808{
809 if (!q || vfio_ap_mdev_get_queue(matrix_mdev, q->apqn))
810 return;
811
812 q->matrix_mdev = matrix_mdev;
813 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
814}
815
816static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
817{
818 struct vfio_ap_queue *q;
819
820 q = vfio_ap_find_queue(apqn);
821 vfio_ap_mdev_link_queue(matrix_mdev, q);
822}
823
824static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
825{
826 hash_del(&q->mdev_qnode);
827}
828
829static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
830{
831 q->matrix_mdev = NULL;
832}
833
834static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
835{
836 struct vfio_ap_queue *q;
837 unsigned long apid, apqi;
838
839 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
840 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
841 AP_DOMAINS) {
842 q = vfio_ap_mdev_get_queue(matrix_mdev,
843 AP_MKQID(apid, apqi));
844 if (q)
845 q->matrix_mdev = NULL;
846 }
847 }
848}
849
850static void vfio_ap_mdev_remove(struct mdev_device *mdev)
851{
852 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
853
854 vfio_unregister_group_dev(&matrix_mdev->vdev);
855
856 mutex_lock(&matrix_dev->guests_lock);
857 mutex_lock(&matrix_dev->mdevs_lock);
858 vfio_ap_mdev_reset_queues(matrix_mdev);
859 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
860 list_del(&matrix_mdev->node);
861 mutex_unlock(&matrix_dev->mdevs_lock);
862 mutex_unlock(&matrix_dev->guests_lock);
863 vfio_put_device(&matrix_mdev->vdev);
864}
865
866#define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
867 "already assigned to %s"
868
869static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
870 unsigned long *apm,
871 unsigned long *aqm)
872{
873 unsigned long apid, apqi;
874 const struct device *dev = mdev_dev(matrix_mdev->mdev);
875 const char *mdev_name = dev_name(dev);
876
877 for_each_set_bit_inv(apid, apm, AP_DEVICES)
878 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
879 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
880}
881
882/**
883 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
884 *
885 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
886 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
887 *
888 * Verifies that each APQN derived from the Cartesian product of a bitmap of
889 * AP adapter IDs and AP queue indexes is not configured for any matrix
890 * mediated device. AP queue sharing is not allowed.
891 *
892 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
893 */
894static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
895 unsigned long *mdev_aqm)
896{
897 struct ap_matrix_mdev *matrix_mdev;
898 DECLARE_BITMAP(apm, AP_DEVICES);
899 DECLARE_BITMAP(aqm, AP_DOMAINS);
900
901 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
902 /*
903 * If the input apm and aqm are fields of the matrix_mdev
904 * object, then move on to the next matrix_mdev.
905 */
906 if (mdev_apm == matrix_mdev->matrix.apm &&
907 mdev_aqm == matrix_mdev->matrix.aqm)
908 continue;
909
910 memset(apm, 0, sizeof(apm));
911 memset(aqm, 0, sizeof(aqm));
912
913 /*
914 * We work on full longs, as we can only exclude the leftover
915 * bits in non-inverse order. The leftover is all zeros.
916 */
917 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
918 AP_DEVICES))
919 continue;
920
921 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
922 AP_DOMAINS))
923 continue;
924
925 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
926
927 return -EADDRINUSE;
928 }
929
930 return 0;
931}
932
933/**
934 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
935 * not reserved for the default zcrypt driver and
936 * are not assigned to another mdev.
937 *
938 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
939 *
940 * Return: One of the following values:
941 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
942 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
943 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
944 * zcrypt default driver.
945 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
946 * o A zero indicating validation succeeded.
947 */
948static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
949{
950 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
951 matrix_mdev->matrix.aqm))
952 return -EADDRNOTAVAIL;
953
954 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
955 matrix_mdev->matrix.aqm);
956}
957
958static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
959 unsigned long apid)
960{
961 unsigned long apqi;
962
963 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
964 vfio_ap_mdev_link_apqn(matrix_mdev,
965 AP_MKQID(apid, apqi));
966}
967
968static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,
969 unsigned long apid,
970 struct list_head *qlist)
971{
972 struct vfio_ap_queue *q;
973 unsigned long apqi;
974
975 for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS) {
976 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
977 if (q)
978 list_add_tail(&q->reset_qnode, qlist);
979 }
980}
981
982static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,
983 unsigned long apid)
984{
985 struct list_head qlist;
986
987 INIT_LIST_HEAD(&qlist);
988 collect_queues_to_reset(matrix_mdev, apid, &qlist);
989 vfio_ap_mdev_reset_qlist(&qlist);
990}
991
992static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
993 unsigned long *apm_reset)
994{
995 struct list_head qlist;
996 unsigned long apid;
997
998 if (bitmap_empty(apm_reset, AP_DEVICES))
999 return 0;
1000
1001 INIT_LIST_HEAD(&qlist);
1002
1003 for_each_set_bit_inv(apid, apm_reset, AP_DEVICES)
1004 collect_queues_to_reset(matrix_mdev, apid, &qlist);
1005
1006 return vfio_ap_mdev_reset_qlist(&qlist);
1007}
1008
1009/**
1010 * assign_adapter_store - parses the APID from @buf and sets the
1011 * corresponding bit in the mediated matrix device's APM
1012 *
1013 * @dev: the matrix device
1014 * @attr: the mediated matrix device's assign_adapter attribute
1015 * @buf: a buffer containing the AP adapter number (APID) to
1016 * be assigned
1017 * @count: the number of bytes in @buf
1018 *
1019 * Return: the number of bytes processed if the APID is valid; otherwise,
1020 * returns one of the following errors:
1021 *
1022 * 1. -EINVAL
1023 * The APID is not a valid number
1024 *
1025 * 2. -ENODEV
1026 * The APID exceeds the maximum value configured for the system
1027 *
1028 * 3. -EADDRNOTAVAIL
1029 * An APQN derived from the cross product of the APID being assigned
1030 * and the APQIs previously assigned is not bound to the vfio_ap device
1031 * driver; or, if no APQIs have yet been assigned, the APID is not
1032 * contained in an APQN bound to the vfio_ap device driver.
1033 *
1034 * 4. -EADDRINUSE
1035 * An APQN derived from the cross product of the APID being assigned
1036 * and the APQIs previously assigned is being used by another mediated
1037 * matrix device
1038 *
1039 * 5. -EAGAIN
1040 * A lock required to validate the mdev's AP configuration could not
1041 * be obtained.
1042 */
1043static ssize_t assign_adapter_store(struct device *dev,
1044 struct device_attribute *attr,
1045 const char *buf, size_t count)
1046{
1047 int ret;
1048 unsigned long apid;
1049 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1050 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1051
1052 mutex_lock(&ap_perms_mutex);
1053 get_update_locks_for_mdev(matrix_mdev);
1054
1055 ret = kstrtoul(buf, 0, &apid);
1056 if (ret)
1057 goto done;
1058
1059 if (apid > matrix_mdev->matrix.apm_max) {
1060 ret = -ENODEV;
1061 goto done;
1062 }
1063
1064 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1065 ret = count;
1066 goto done;
1067 }
1068
1069 set_bit_inv(apid, matrix_mdev->matrix.apm);
1070
1071 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1072 if (ret) {
1073 clear_bit_inv(apid, matrix_mdev->matrix.apm);
1074 goto done;
1075 }
1076
1077 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
1078
1079 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1080 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1081 reset_queues_for_apids(matrix_mdev, apm_filtered);
1082 }
1083
1084 ret = count;
1085done:
1086 release_update_locks_for_mdev(matrix_mdev);
1087 mutex_unlock(&ap_perms_mutex);
1088
1089 return ret;
1090}
1091static DEVICE_ATTR_WO(assign_adapter);
1092
1093static struct vfio_ap_queue
1094*vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
1095 unsigned long apid, unsigned long apqi)
1096{
1097 struct vfio_ap_queue *q = NULL;
1098
1099 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1100 /* If the queue is assigned to the matrix mdev, unlink it. */
1101 if (q)
1102 vfio_ap_unlink_queue_fr_mdev(q);
1103
1104 return q;
1105}
1106
1107/**
1108 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
1109 * adapter from the matrix mdev to which the
1110 * adapter was assigned.
1111 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
1112 * @apid: the APID of the unassigned adapter.
1113 * @qlist: list for storing queues associated with unassigned adapter that
1114 * need to be reset.
1115 */
1116static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1117 unsigned long apid,
1118 struct list_head *qlist)
1119{
1120 unsigned long apqi;
1121 struct vfio_ap_queue *q;
1122
1123 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1124 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1125
1126 if (q && qlist) {
1127 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1128 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1129 list_add_tail(&q->reset_qnode, qlist);
1130 }
1131 }
1132}
1133
1134static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,
1135 unsigned long *apids)
1136{
1137 struct vfio_ap_queue *q, *tmpq;
1138 struct list_head qlist;
1139 unsigned long apid;
1140 bool apcb_update = false;
1141
1142 INIT_LIST_HEAD(&qlist);
1143
1144 for_each_set_bit_inv(apid, apids, AP_DEVICES) {
1145 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
1146
1147 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1148 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1149 apcb_update = true;
1150 }
1151 }
1152
1153 /* Only update apcb if needed to avoid impacting guest */
1154 if (apcb_update)
1155 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1156
1157 vfio_ap_mdev_reset_qlist(&qlist);
1158
1159 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1160 vfio_ap_unlink_mdev_fr_queue(q);
1161 list_del(&q->reset_qnode);
1162 }
1163}
1164
1165static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1166 unsigned long apid)
1167{
1168 DECLARE_BITMAP(apids, AP_DEVICES);
1169
1170 bitmap_zero(apids, AP_DEVICES);
1171 set_bit_inv(apid, apids);
1172 vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, apids);
1173}
1174
1175/**
1176 * unassign_adapter_store - parses the APID from @buf and clears the
1177 * corresponding bit in the mediated matrix device's APM
1178 *
1179 * @dev: the matrix device
1180 * @attr: the mediated matrix device's unassign_adapter attribute
1181 * @buf: a buffer containing the adapter number (APID) to be unassigned
1182 * @count: the number of bytes in @buf
1183 *
1184 * Return: the number of bytes processed if the APID is valid; otherwise,
1185 * returns one of the following errors:
1186 * -EINVAL if the APID is not a number
1187 * -ENODEV if the APID it exceeds the maximum value configured for the
1188 * system
1189 */
1190static ssize_t unassign_adapter_store(struct device *dev,
1191 struct device_attribute *attr,
1192 const char *buf, size_t count)
1193{
1194 int ret;
1195 unsigned long apid;
1196 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1197
1198 get_update_locks_for_mdev(matrix_mdev);
1199
1200 ret = kstrtoul(buf, 0, &apid);
1201 if (ret)
1202 goto done;
1203
1204 if (apid > matrix_mdev->matrix.apm_max) {
1205 ret = -ENODEV;
1206 goto done;
1207 }
1208
1209 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
1210 ret = count;
1211 goto done;
1212 }
1213
1214 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1215 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1216 ret = count;
1217done:
1218 release_update_locks_for_mdev(matrix_mdev);
1219 return ret;
1220}
1221static DEVICE_ATTR_WO(unassign_adapter);
1222
1223static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1224 unsigned long apqi)
1225{
1226 unsigned long apid;
1227
1228 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1229 vfio_ap_mdev_link_apqn(matrix_mdev,
1230 AP_MKQID(apid, apqi));
1231}
1232
1233/**
1234 * assign_domain_store - parses the APQI from @buf and sets the
1235 * corresponding bit in the mediated matrix device's AQM
1236 *
1237 * @dev: the matrix device
1238 * @attr: the mediated matrix device's assign_domain attribute
1239 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1240 * be assigned
1241 * @count: the number of bytes in @buf
1242 *
1243 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1244 * one of the following errors:
1245 *
1246 * 1. -EINVAL
1247 * The APQI is not a valid number
1248 *
1249 * 2. -ENODEV
1250 * The APQI exceeds the maximum value configured for the system
1251 *
1252 * 3. -EADDRNOTAVAIL
1253 * An APQN derived from the cross product of the APQI being assigned
1254 * and the APIDs previously assigned is not bound to the vfio_ap device
1255 * driver; or, if no APIDs have yet been assigned, the APQI is not
1256 * contained in an APQN bound to the vfio_ap device driver.
1257 *
1258 * 4. -EADDRINUSE
1259 * An APQN derived from the cross product of the APQI being assigned
1260 * and the APIDs previously assigned is being used by another mediated
1261 * matrix device
1262 *
1263 * 5. -EAGAIN
1264 * The lock required to validate the mdev's AP configuration could not
1265 * be obtained.
1266 */
1267static ssize_t assign_domain_store(struct device *dev,
1268 struct device_attribute *attr,
1269 const char *buf, size_t count)
1270{
1271 int ret;
1272 unsigned long apqi;
1273 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1274 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1275
1276 mutex_lock(&ap_perms_mutex);
1277 get_update_locks_for_mdev(matrix_mdev);
1278
1279 ret = kstrtoul(buf, 0, &apqi);
1280 if (ret)
1281 goto done;
1282
1283 if (apqi > matrix_mdev->matrix.aqm_max) {
1284 ret = -ENODEV;
1285 goto done;
1286 }
1287
1288 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1289 ret = count;
1290 goto done;
1291 }
1292
1293 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1294
1295 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1296 if (ret) {
1297 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1298 goto done;
1299 }
1300
1301 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1302
1303 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
1304 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1305 reset_queues_for_apids(matrix_mdev, apm_filtered);
1306 }
1307
1308 ret = count;
1309done:
1310 release_update_locks_for_mdev(matrix_mdev);
1311 mutex_unlock(&ap_perms_mutex);
1312
1313 return ret;
1314}
1315static DEVICE_ATTR_WO(assign_domain);
1316
1317static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1318 unsigned long apqi,
1319 struct list_head *qlist)
1320{
1321 unsigned long apid;
1322 struct vfio_ap_queue *q;
1323
1324 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1325 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1326
1327 if (q && qlist) {
1328 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1329 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1330 list_add_tail(&q->reset_qnode, qlist);
1331 }
1332 }
1333}
1334
1335static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,
1336 unsigned long *apqis)
1337{
1338 struct vfio_ap_queue *q, *tmpq;
1339 struct list_head qlist;
1340 unsigned long apqi;
1341 bool apcb_update = false;
1342
1343 INIT_LIST_HEAD(&qlist);
1344
1345 for_each_set_bit_inv(apqi, apqis, AP_DOMAINS) {
1346 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
1347
1348 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1349 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1350 apcb_update = true;
1351 }
1352 }
1353
1354 /* Only update apcb if needed to avoid impacting guest */
1355 if (apcb_update)
1356 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1357
1358 vfio_ap_mdev_reset_qlist(&qlist);
1359
1360 list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
1361 vfio_ap_unlink_mdev_fr_queue(q);
1362 list_del(&q->reset_qnode);
1363 }
1364}
1365
1366static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1367 unsigned long apqi)
1368{
1369 DECLARE_BITMAP(apqis, AP_DOMAINS);
1370
1371 bitmap_zero(apqis, AP_DEVICES);
1372 set_bit_inv(apqi, apqis);
1373 vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis);
1374}
1375
1376/**
1377 * unassign_domain_store - parses the APQI from @buf and clears the
1378 * corresponding bit in the mediated matrix device's AQM
1379 *
1380 * @dev: the matrix device
1381 * @attr: the mediated matrix device's unassign_domain attribute
1382 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1383 * be unassigned
1384 * @count: the number of bytes in @buf
1385 *
1386 * Return: the number of bytes processed if the APQI is valid; otherwise,
1387 * returns one of the following errors:
1388 * -EINVAL if the APQI is not a number
1389 * -ENODEV if the APQI exceeds the maximum value configured for the system
1390 */
1391static ssize_t unassign_domain_store(struct device *dev,
1392 struct device_attribute *attr,
1393 const char *buf, size_t count)
1394{
1395 int ret;
1396 unsigned long apqi;
1397 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1398
1399 get_update_locks_for_mdev(matrix_mdev);
1400
1401 ret = kstrtoul(buf, 0, &apqi);
1402 if (ret)
1403 goto done;
1404
1405 if (apqi > matrix_mdev->matrix.aqm_max) {
1406 ret = -ENODEV;
1407 goto done;
1408 }
1409
1410 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
1411 ret = count;
1412 goto done;
1413 }
1414
1415 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1416 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1417 ret = count;
1418
1419done:
1420 release_update_locks_for_mdev(matrix_mdev);
1421 return ret;
1422}
1423static DEVICE_ATTR_WO(unassign_domain);
1424
1425/**
1426 * assign_control_domain_store - parses the domain ID from @buf and sets
1427 * the corresponding bit in the mediated matrix device's ADM
1428 *
1429 * @dev: the matrix device
1430 * @attr: the mediated matrix device's assign_control_domain attribute
1431 * @buf: a buffer containing the domain ID to be assigned
1432 * @count: the number of bytes in @buf
1433 *
1434 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1435 * returns one of the following errors:
1436 * -EINVAL if the ID is not a number
1437 * -ENODEV if the ID exceeds the maximum value configured for the system
1438 */
1439static ssize_t assign_control_domain_store(struct device *dev,
1440 struct device_attribute *attr,
1441 const char *buf, size_t count)
1442{
1443 int ret;
1444 unsigned long id;
1445 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1446
1447 get_update_locks_for_mdev(matrix_mdev);
1448
1449 ret = kstrtoul(buf, 0, &id);
1450 if (ret)
1451 goto done;
1452
1453 if (id > matrix_mdev->matrix.adm_max) {
1454 ret = -ENODEV;
1455 goto done;
1456 }
1457
1458 if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
1459 ret = count;
1460 goto done;
1461 }
1462
1463 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1464 * domain number (id). The bits in the mask, from most significant to
1465 * least significant, correspond to IDs 0 up to the one less than the
1466 * number of control domains that can be assigned.
1467 */
1468 set_bit_inv(id, matrix_mdev->matrix.adm);
1469 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1470 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1471
1472 ret = count;
1473done:
1474 release_update_locks_for_mdev(matrix_mdev);
1475 return ret;
1476}
1477static DEVICE_ATTR_WO(assign_control_domain);
1478
1479/**
1480 * unassign_control_domain_store - parses the domain ID from @buf and
1481 * clears the corresponding bit in the mediated matrix device's ADM
1482 *
1483 * @dev: the matrix device
1484 * @attr: the mediated matrix device's unassign_control_domain attribute
1485 * @buf: a buffer containing the domain ID to be unassigned
1486 * @count: the number of bytes in @buf
1487 *
1488 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1489 * returns one of the following errors:
1490 * -EINVAL if the ID is not a number
1491 * -ENODEV if the ID exceeds the maximum value configured for the system
1492 */
1493static ssize_t unassign_control_domain_store(struct device *dev,
1494 struct device_attribute *attr,
1495 const char *buf, size_t count)
1496{
1497 int ret;
1498 unsigned long domid;
1499 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1500
1501 get_update_locks_for_mdev(matrix_mdev);
1502
1503 ret = kstrtoul(buf, 0, &domid);
1504 if (ret)
1505 goto done;
1506
1507 if (domid > matrix_mdev->matrix.adm_max) {
1508 ret = -ENODEV;
1509 goto done;
1510 }
1511
1512 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
1513 ret = count;
1514 goto done;
1515 }
1516
1517 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1518
1519 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1520 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1521 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1522 }
1523
1524 ret = count;
1525done:
1526 release_update_locks_for_mdev(matrix_mdev);
1527 return ret;
1528}
1529static DEVICE_ATTR_WO(unassign_control_domain);
1530
1531static ssize_t control_domains_show(struct device *dev,
1532 struct device_attribute *dev_attr,
1533 char *buf)
1534{
1535 unsigned long id;
1536 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1537 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1538 int nchars = 0;
1539
1540 mutex_lock(&matrix_dev->mdevs_lock);
1541 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1)
1542 nchars += sysfs_emit_at(buf, nchars, "%04lx\n", id);
1543 mutex_unlock(&matrix_dev->mdevs_lock);
1544
1545 return nchars;
1546}
1547static DEVICE_ATTR_RO(control_domains);
1548
1549static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1550{
1551 unsigned long apid;
1552 unsigned long apqi;
1553 unsigned long apid1;
1554 unsigned long apqi1;
1555 unsigned long napm_bits = matrix->apm_max + 1;
1556 unsigned long naqm_bits = matrix->aqm_max + 1;
1557 int nchars = 0;
1558
1559 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1560 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1561
1562 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1563 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1564 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits)
1565 nchars += sysfs_emit_at(buf, nchars, "%02lx.%04lx\n", apid, apqi);
1566 }
1567 } else if (apid1 < napm_bits) {
1568 for_each_set_bit_inv(apid, matrix->apm, napm_bits)
1569 nchars += sysfs_emit_at(buf, nchars, "%02lx.\n", apid);
1570 } else if (apqi1 < naqm_bits) {
1571 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits)
1572 nchars += sysfs_emit_at(buf, nchars, ".%04lx\n", apqi);
1573 }
1574
1575 return nchars;
1576}
1577
1578static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1579 char *buf)
1580{
1581 ssize_t nchars;
1582 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1583
1584 mutex_lock(&matrix_dev->mdevs_lock);
1585 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1586 mutex_unlock(&matrix_dev->mdevs_lock);
1587
1588 return nchars;
1589}
1590static DEVICE_ATTR_RO(matrix);
1591
1592static ssize_t guest_matrix_show(struct device *dev,
1593 struct device_attribute *attr, char *buf)
1594{
1595 ssize_t nchars;
1596 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1597
1598 mutex_lock(&matrix_dev->mdevs_lock);
1599 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1600 mutex_unlock(&matrix_dev->mdevs_lock);
1601
1602 return nchars;
1603}
1604static DEVICE_ATTR_RO(guest_matrix);
1605
1606static ssize_t write_ap_bitmap(unsigned long *bitmap, char *buf, int offset, char sep)
1607{
1608 return sysfs_emit_at(buf, offset, "0x%016lx%016lx%016lx%016lx%c",
1609 bitmap[0], bitmap[1], bitmap[2], bitmap[3], sep);
1610}
1611
1612static ssize_t ap_config_show(struct device *dev, struct device_attribute *attr,
1613 char *buf)
1614{
1615 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1616 int idx = 0;
1617
1618 idx += write_ap_bitmap(matrix_mdev->matrix.apm, buf, idx, ',');
1619 idx += write_ap_bitmap(matrix_mdev->matrix.aqm, buf, idx, ',');
1620 idx += write_ap_bitmap(matrix_mdev->matrix.adm, buf, idx, '\n');
1621
1622 return idx;
1623}
1624
1625/* Number of characters needed for a complete hex mask representing the bits in .. */
1626#define AP_DEVICES_STRLEN (AP_DEVICES / 4 + 3)
1627#define AP_DOMAINS_STRLEN (AP_DOMAINS / 4 + 3)
1628#define AP_CONFIG_STRLEN (AP_DEVICES_STRLEN + 2 * AP_DOMAINS_STRLEN)
1629
1630static int parse_bitmap(char **strbufptr, unsigned long *bitmap, int nbits)
1631{
1632 char *curmask;
1633
1634 curmask = strsep(strbufptr, ",\n");
1635 if (!curmask)
1636 return -EINVAL;
1637
1638 bitmap_clear(bitmap, 0, nbits);
1639 return ap_hex2bitmap(curmask, bitmap, nbits);
1640}
1641
1642static int ap_matrix_overflow_check(struct ap_matrix_mdev *matrix_mdev)
1643{
1644 unsigned long bit;
1645
1646 for_each_set_bit_inv(bit, matrix_mdev->matrix.apm, AP_DEVICES) {
1647 if (bit > matrix_mdev->matrix.apm_max)
1648 return -ENODEV;
1649 }
1650
1651 for_each_set_bit_inv(bit, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1652 if (bit > matrix_mdev->matrix.aqm_max)
1653 return -ENODEV;
1654 }
1655
1656 for_each_set_bit_inv(bit, matrix_mdev->matrix.adm, AP_DOMAINS) {
1657 if (bit > matrix_mdev->matrix.adm_max)
1658 return -ENODEV;
1659 }
1660
1661 return 0;
1662}
1663
1664static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
1665{
1666 /* This check works around false positive gcc -Wstringop-overread */
1667 if (!src)
1668 return;
1669
1670 bitmap_copy(dst->apm, src->apm, AP_DEVICES);
1671 bitmap_copy(dst->aqm, src->aqm, AP_DOMAINS);
1672 bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
1673}
1674
1675static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
1676 const char *buf, size_t count)
1677{
1678 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1679 struct ap_matrix m_new, m_old, m_added, m_removed;
1680 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
1681 unsigned long newbit;
1682 char *newbuf, *rest;
1683 int rc = count;
1684 bool do_update;
1685
1686 newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
1687 if (!newbuf)
1688 return -ENOMEM;
1689 rest = newbuf;
1690
1691 mutex_lock(&ap_perms_mutex);
1692 get_update_locks_for_mdev(matrix_mdev);
1693
1694 /* Save old state */
1695 ap_matrix_copy(&m_old, &matrix_mdev->matrix);
1696 if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
1697 parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
1698 parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
1699 rc = -EINVAL;
1700 goto out;
1701 }
1702
1703 bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
1704 bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
1705 bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
1706 bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
1707
1708 /* Need new bitmaps in matrix_mdev for validation */
1709 ap_matrix_copy(&matrix_mdev->matrix, &m_new);
1710
1711 /* Ensure new state is valid, else undo new state */
1712 rc = vfio_ap_mdev_validate_masks(matrix_mdev);
1713 if (rc) {
1714 ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1715 goto out;
1716 }
1717 rc = ap_matrix_overflow_check(matrix_mdev);
1718 if (rc) {
1719 ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1720 goto out;
1721 }
1722 rc = count;
1723
1724 /* Need old bitmaps in matrix_mdev for unplug/unlink */
1725 ap_matrix_copy(&matrix_mdev->matrix, &m_old);
1726
1727 /* Unlink removed adapters/domains */
1728 vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, m_removed.apm);
1729 vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
1730
1731 /* Need new bitmaps in matrix_mdev for linking new adapters/domains */
1732 ap_matrix_copy(&matrix_mdev->matrix, &m_new);
1733
1734 /* Link newly added adapters */
1735 for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
1736 vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
1737
1738 for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
1739 vfio_ap_mdev_link_domain(matrix_mdev, newbit);
1740
1741 /* filter resources not bound to vfio-ap */
1742 do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
1743 do_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
1744
1745 /* Apply changes to shadow apbc if things changed */
1746 if (do_update) {
1747 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1748 reset_queues_for_apids(matrix_mdev, apm_filtered);
1749 }
1750out:
1751 release_update_locks_for_mdev(matrix_mdev);
1752 mutex_unlock(&ap_perms_mutex);
1753 kfree(newbuf);
1754 return rc;
1755}
1756static DEVICE_ATTR_RW(ap_config);
1757
1758static struct attribute *vfio_ap_mdev_attrs[] = {
1759 &dev_attr_assign_adapter.attr,
1760 &dev_attr_unassign_adapter.attr,
1761 &dev_attr_assign_domain.attr,
1762 &dev_attr_unassign_domain.attr,
1763 &dev_attr_assign_control_domain.attr,
1764 &dev_attr_unassign_control_domain.attr,
1765 &dev_attr_ap_config.attr,
1766 &dev_attr_control_domains.attr,
1767 &dev_attr_matrix.attr,
1768 &dev_attr_guest_matrix.attr,
1769 NULL,
1770};
1771
1772static struct attribute_group vfio_ap_mdev_attr_group = {
1773 .attrs = vfio_ap_mdev_attrs
1774};
1775
1776static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1777 &vfio_ap_mdev_attr_group,
1778 NULL
1779};
1780
1781/**
1782 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1783 * to manage AP resources for the guest whose state is represented by @kvm
1784 *
1785 * @matrix_mdev: a mediated matrix device
1786 * @kvm: reference to KVM instance
1787 *
1788 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1789 * otherwise, returns an -EPERM.
1790 */
1791static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1792 struct kvm *kvm)
1793{
1794 struct ap_matrix_mdev *m;
1795
1796 if (kvm->arch.crypto.crycbd) {
1797 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1798 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1799 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1800
1801 get_update_locks_for_kvm(kvm);
1802
1803 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1804 if (m != matrix_mdev && m->kvm == kvm) {
1805 release_update_locks_for_kvm(kvm);
1806 return -EPERM;
1807 }
1808 }
1809
1810 kvm_get_kvm(kvm);
1811 matrix_mdev->kvm = kvm;
1812 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1813
1814 release_update_locks_for_kvm(kvm);
1815 }
1816
1817 return 0;
1818}
1819
1820static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
1821{
1822 struct ap_queue_table *qtable = &matrix_mdev->qtable;
1823 struct vfio_ap_queue *q;
1824 int loop_cursor;
1825
1826 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1827 if (q->saved_iova >= iova && q->saved_iova < iova + length)
1828 vfio_ap_irq_disable(q);
1829 }
1830}
1831
1832static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1833 u64 length)
1834{
1835 struct ap_matrix_mdev *matrix_mdev =
1836 container_of(vdev, struct ap_matrix_mdev, vdev);
1837
1838 mutex_lock(&matrix_dev->mdevs_lock);
1839
1840 unmap_iova(matrix_mdev, iova, length);
1841
1842 mutex_unlock(&matrix_dev->mdevs_lock);
1843}
1844
1845/**
1846 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1847 * by @matrix_mdev.
1848 *
1849 * @matrix_mdev: a matrix mediated device
1850 */
1851static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1852{
1853 struct kvm *kvm = matrix_mdev->kvm;
1854
1855 if (kvm && kvm->arch.crypto.crycbd) {
1856 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1857 kvm->arch.crypto.pqap_hook = NULL;
1858 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1859
1860 get_update_locks_for_kvm(kvm);
1861
1862 kvm_arch_crypto_clear_masks(kvm);
1863 vfio_ap_mdev_reset_queues(matrix_mdev);
1864 kvm_put_kvm(kvm);
1865 matrix_mdev->kvm = NULL;
1866
1867 release_update_locks_for_kvm(kvm);
1868 }
1869}
1870
1871static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1872{
1873 struct ap_queue *queue;
1874 struct vfio_ap_queue *q = NULL;
1875
1876 queue = ap_get_qdev(apqn);
1877 if (!queue)
1878 return NULL;
1879
1880 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1881 q = dev_get_drvdata(&queue->ap_dev.device);
1882
1883 put_device(&queue->ap_dev.device);
1884
1885 return q;
1886}
1887
1888static int apq_status_check(int apqn, struct ap_queue_status *status)
1889{
1890 switch (status->response_code) {
1891 case AP_RESPONSE_NORMAL:
1892 case AP_RESPONSE_DECONFIGURED:
1893 case AP_RESPONSE_CHECKSTOPPED:
1894 return 0;
1895 case AP_RESPONSE_RESET_IN_PROGRESS:
1896 case AP_RESPONSE_BUSY:
1897 return -EBUSY;
1898 case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
1899 case AP_RESPONSE_ASSOC_FAILED:
1900 /*
1901 * These asynchronous response codes indicate a PQAP(AAPQ)
1902 * instruction to associate a secret with the guest failed. All
1903 * subsequent AP instructions will end with the asynchronous
1904 * response code until the AP queue is reset; so, let's return
1905 * a value indicating a reset needs to be performed again.
1906 */
1907 return -EAGAIN;
1908 default:
1909 WARN(true,
1910 "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
1911 AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
1912 status->response_code);
1913 return -EIO;
1914 }
1915}
1916
1917#define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
1918
1919static void apq_reset_check(struct work_struct *reset_work)
1920{
1921 int ret = -EBUSY, elapsed = 0;
1922 struct ap_queue_status status;
1923 struct vfio_ap_queue *q;
1924
1925 q = container_of(reset_work, struct vfio_ap_queue, reset_work);
1926 memcpy(&status, &q->reset_status, sizeof(status));
1927 while (true) {
1928 msleep(AP_RESET_INTERVAL);
1929 elapsed += AP_RESET_INTERVAL;
1930 status = ap_tapq(q->apqn, NULL);
1931 ret = apq_status_check(q->apqn, &status);
1932 if (ret == -EIO)
1933 return;
1934 if (ret == -EBUSY) {
1935 pr_notice_ratelimited(WAIT_MSG, elapsed,
1936 AP_QID_CARD(q->apqn),
1937 AP_QID_QUEUE(q->apqn),
1938 status.response_code,
1939 status.queue_empty,
1940 status.irq_enabled);
1941 } else {
1942 if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
1943 q->reset_status.response_code == AP_RESPONSE_BUSY ||
1944 q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
1945 ret == -EAGAIN) {
1946 status = ap_zapq(q->apqn, 0);
1947 memcpy(&q->reset_status, &status, sizeof(status));
1948 continue;
1949 }
1950 if (q->saved_isc != VFIO_AP_ISC_INVALID)
1951 vfio_ap_free_aqic_resources(q);
1952 break;
1953 }
1954 }
1955}
1956
1957static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
1958{
1959 struct ap_queue_status status;
1960
1961 if (!q)
1962 return;
1963 status = ap_zapq(q->apqn, 0);
1964 memcpy(&q->reset_status, &status, sizeof(status));
1965 switch (status.response_code) {
1966 case AP_RESPONSE_NORMAL:
1967 case AP_RESPONSE_RESET_IN_PROGRESS:
1968 case AP_RESPONSE_BUSY:
1969 case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
1970 /*
1971 * Let's verify whether the ZAPQ completed successfully on a work queue.
1972 */
1973 queue_work(system_long_wq, &q->reset_work);
1974 break;
1975 case AP_RESPONSE_DECONFIGURED:
1976 case AP_RESPONSE_CHECKSTOPPED:
1977 vfio_ap_free_aqic_resources(q);
1978 break;
1979 default:
1980 WARN(true,
1981 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1982 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1983 status.response_code);
1984 }
1985}
1986
1987static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1988{
1989 int ret = 0, loop_cursor;
1990 struct vfio_ap_queue *q;
1991
1992 hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode)
1993 vfio_ap_mdev_reset_queue(q);
1994
1995 hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode) {
1996 flush_work(&q->reset_work);
1997
1998 if (q->reset_status.response_code)
1999 ret = -EIO;
2000 }
2001
2002 return ret;
2003}
2004
2005static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
2006{
2007 int ret = 0;
2008 struct vfio_ap_queue *q;
2009
2010 list_for_each_entry(q, qlist, reset_qnode)
2011 vfio_ap_mdev_reset_queue(q);
2012
2013 list_for_each_entry(q, qlist, reset_qnode) {
2014 flush_work(&q->reset_work);
2015
2016 if (q->reset_status.response_code)
2017 ret = -EIO;
2018 }
2019
2020 return ret;
2021}
2022
2023static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
2024{
2025 struct ap_matrix_mdev *matrix_mdev =
2026 container_of(vdev, struct ap_matrix_mdev, vdev);
2027
2028 if (!vdev->kvm)
2029 return -EINVAL;
2030
2031 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
2032}
2033
2034static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
2035{
2036 struct ap_matrix_mdev *matrix_mdev =
2037 container_of(vdev, struct ap_matrix_mdev, vdev);
2038
2039 vfio_ap_mdev_unset_kvm(matrix_mdev);
2040}
2041
2042static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
2043{
2044 struct device *dev = vdev->dev;
2045 struct ap_matrix_mdev *matrix_mdev;
2046
2047 matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
2048
2049 if (matrix_mdev->req_trigger) {
2050 if (!(count % 10))
2051 dev_notice_ratelimited(dev,
2052 "Relaying device request to user (#%u)\n",
2053 count);
2054
2055 eventfd_signal(matrix_mdev->req_trigger);
2056 } else if (count == 0) {
2057 dev_notice(dev,
2058 "No device request registered, blocked until released by user\n");
2059 }
2060}
2061
2062static int vfio_ap_mdev_get_device_info(unsigned long arg)
2063{
2064 unsigned long minsz;
2065 struct vfio_device_info info;
2066
2067 minsz = offsetofend(struct vfio_device_info, num_irqs);
2068
2069 if (copy_from_user(&info, (void __user *)arg, minsz))
2070 return -EFAULT;
2071
2072 if (info.argsz < minsz)
2073 return -EINVAL;
2074
2075 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
2076 info.num_regions = 0;
2077 info.num_irqs = VFIO_AP_NUM_IRQS;
2078
2079 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
2080}
2081
2082static ssize_t vfio_ap_get_irq_info(unsigned long arg)
2083{
2084 unsigned long minsz;
2085 struct vfio_irq_info info;
2086
2087 minsz = offsetofend(struct vfio_irq_info, count);
2088
2089 if (copy_from_user(&info, (void __user *)arg, minsz))
2090 return -EFAULT;
2091
2092 if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
2093 return -EINVAL;
2094
2095 switch (info.index) {
2096 case VFIO_AP_REQ_IRQ_INDEX:
2097 info.count = 1;
2098 info.flags = VFIO_IRQ_INFO_EVENTFD;
2099 break;
2100 default:
2101 return -EINVAL;
2102 }
2103
2104 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
2105}
2106
2107static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
2108{
2109 int ret;
2110 size_t data_size;
2111 unsigned long minsz;
2112
2113 minsz = offsetofend(struct vfio_irq_set, count);
2114
2115 if (copy_from_user(irq_set, (void __user *)arg, minsz))
2116 return -EFAULT;
2117
2118 ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
2119 &data_size);
2120 if (ret)
2121 return ret;
2122
2123 if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
2124 return -EINVAL;
2125
2126 return 0;
2127}
2128
2129static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
2130 unsigned long arg)
2131{
2132 s32 fd;
2133 void __user *data;
2134 unsigned long minsz;
2135 struct eventfd_ctx *req_trigger;
2136
2137 minsz = offsetofend(struct vfio_irq_set, count);
2138 data = (void __user *)(arg + minsz);
2139
2140 if (get_user(fd, (s32 __user *)data))
2141 return -EFAULT;
2142
2143 if (fd == -1) {
2144 if (matrix_mdev->req_trigger)
2145 eventfd_ctx_put(matrix_mdev->req_trigger);
2146 matrix_mdev->req_trigger = NULL;
2147 } else if (fd >= 0) {
2148 req_trigger = eventfd_ctx_fdget(fd);
2149 if (IS_ERR(req_trigger))
2150 return PTR_ERR(req_trigger);
2151
2152 if (matrix_mdev->req_trigger)
2153 eventfd_ctx_put(matrix_mdev->req_trigger);
2154
2155 matrix_mdev->req_trigger = req_trigger;
2156 } else {
2157 return -EINVAL;
2158 }
2159
2160 return 0;
2161}
2162
2163static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
2164 unsigned long arg)
2165{
2166 int ret;
2167 struct vfio_irq_set irq_set;
2168
2169 ret = vfio_ap_irq_set_init(&irq_set, arg);
2170 if (ret)
2171 return ret;
2172
2173 switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
2174 case VFIO_IRQ_SET_DATA_EVENTFD:
2175 switch (irq_set.index) {
2176 case VFIO_AP_REQ_IRQ_INDEX:
2177 return vfio_ap_set_request_irq(matrix_mdev, arg);
2178 default:
2179 return -EINVAL;
2180 }
2181 default:
2182 return -EINVAL;
2183 }
2184}
2185
2186static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
2187 unsigned int cmd, unsigned long arg)
2188{
2189 struct ap_matrix_mdev *matrix_mdev =
2190 container_of(vdev, struct ap_matrix_mdev, vdev);
2191 int ret;
2192
2193 mutex_lock(&matrix_dev->mdevs_lock);
2194 switch (cmd) {
2195 case VFIO_DEVICE_GET_INFO:
2196 ret = vfio_ap_mdev_get_device_info(arg);
2197 break;
2198 case VFIO_DEVICE_RESET:
2199 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
2200 break;
2201 case VFIO_DEVICE_GET_IRQ_INFO:
2202 ret = vfio_ap_get_irq_info(arg);
2203 break;
2204 case VFIO_DEVICE_SET_IRQS:
2205 ret = vfio_ap_set_irqs(matrix_mdev, arg);
2206 break;
2207 default:
2208 ret = -EOPNOTSUPP;
2209 break;
2210 }
2211 mutex_unlock(&matrix_dev->mdevs_lock);
2212
2213 return ret;
2214}
2215
2216static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
2217{
2218 struct ap_matrix_mdev *matrix_mdev;
2219 unsigned long apid = AP_QID_CARD(q->apqn);
2220 unsigned long apqi = AP_QID_QUEUE(q->apqn);
2221
2222 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2223 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
2224 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
2225 return matrix_mdev;
2226 }
2227
2228 return NULL;
2229}
2230
2231static ssize_t status_show(struct device *dev,
2232 struct device_attribute *attr,
2233 char *buf)
2234{
2235 ssize_t nchars = 0;
2236 struct vfio_ap_queue *q;
2237 unsigned long apid, apqi;
2238 struct ap_matrix_mdev *matrix_mdev;
2239 struct ap_device *apdev = to_ap_dev(dev);
2240
2241 mutex_lock(&matrix_dev->mdevs_lock);
2242 q = dev_get_drvdata(&apdev->device);
2243 matrix_mdev = vfio_ap_mdev_for_queue(q);
2244
2245 /* If the queue is assigned to the matrix mediated device, then
2246 * determine whether it is passed through to a guest; otherwise,
2247 * indicate that it is unassigned.
2248 */
2249 if (matrix_mdev) {
2250 apid = AP_QID_CARD(q->apqn);
2251 apqi = AP_QID_QUEUE(q->apqn);
2252 /*
2253 * If the queue is passed through to the guest, then indicate
2254 * that it is in use; otherwise, indicate that it is
2255 * merely assigned to a matrix mediated device.
2256 */
2257 if (matrix_mdev->kvm &&
2258 test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2259 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
2260 nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_IN_USE);
2261 else
2262 nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_ASSIGNED);
2263 } else {
2264 nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_UNASSIGNED);
2265 }
2266
2267 mutex_unlock(&matrix_dev->mdevs_lock);
2268
2269 return nchars;
2270}
2271
2272static DEVICE_ATTR_RO(status);
2273
2274static struct attribute *vfio_queue_attrs[] = {
2275 &dev_attr_status.attr,
2276 NULL,
2277};
2278
2279static const struct attribute_group vfio_queue_attr_group = {
2280 .attrs = vfio_queue_attrs,
2281};
2282
2283static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
2284 .init = vfio_ap_mdev_init_dev,
2285 .open_device = vfio_ap_mdev_open_device,
2286 .close_device = vfio_ap_mdev_close_device,
2287 .ioctl = vfio_ap_mdev_ioctl,
2288 .dma_unmap = vfio_ap_mdev_dma_unmap,
2289 .bind_iommufd = vfio_iommufd_emulated_bind,
2290 .unbind_iommufd = vfio_iommufd_emulated_unbind,
2291 .attach_ioas = vfio_iommufd_emulated_attach_ioas,
2292 .detach_ioas = vfio_iommufd_emulated_detach_ioas,
2293 .request = vfio_ap_mdev_request
2294};
2295
2296static struct mdev_driver vfio_ap_matrix_driver = {
2297 .device_api = VFIO_DEVICE_API_AP_STRING,
2298 .max_instances = MAX_ZDEV_ENTRIES_EXT,
2299 .driver = {
2300 .name = "vfio_ap_mdev",
2301 .owner = THIS_MODULE,
2302 .mod_name = KBUILD_MODNAME,
2303 .dev_groups = vfio_ap_mdev_attr_groups,
2304 },
2305 .probe = vfio_ap_mdev_probe,
2306 .remove = vfio_ap_mdev_remove,
2307};
2308
2309int vfio_ap_mdev_register(void)
2310{
2311 int ret;
2312
2313 ret = mdev_register_driver(&vfio_ap_matrix_driver);
2314 if (ret)
2315 return ret;
2316
2317 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
2318 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
2319 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type;
2320 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
2321 &vfio_ap_matrix_driver,
2322 matrix_dev->mdev_types, 1);
2323 if (ret)
2324 goto err_driver;
2325 return 0;
2326
2327err_driver:
2328 mdev_unregister_driver(&vfio_ap_matrix_driver);
2329 return ret;
2330}
2331
2332void vfio_ap_mdev_unregister(void)
2333{
2334 mdev_unregister_parent(&matrix_dev->parent);
2335 mdev_unregister_driver(&vfio_ap_matrix_driver);
2336}
2337
2338int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2339{
2340 int ret;
2341 struct vfio_ap_queue *q;
2342 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2343 struct ap_matrix_mdev *matrix_mdev;
2344
2345 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
2346 if (ret)
2347 return ret;
2348
2349 q = kzalloc(sizeof(*q), GFP_KERNEL);
2350 if (!q) {
2351 ret = -ENOMEM;
2352 goto err_remove_group;
2353 }
2354
2355 q->apqn = to_ap_queue(&apdev->device)->qid;
2356 q->saved_isc = VFIO_AP_ISC_INVALID;
2357 memset(&q->reset_status, 0, sizeof(q->reset_status));
2358 INIT_WORK(&q->reset_work, apq_reset_check);
2359 matrix_mdev = get_update_locks_by_apqn(q->apqn);
2360
2361 if (matrix_mdev) {
2362 vfio_ap_mdev_link_queue(matrix_mdev, q);
2363
2364 /*
2365 * If we're in the process of handling the adding of adapters or
2366 * domains to the host's AP configuration, then let the
2367 * vfio_ap device driver's on_scan_complete callback filter the
2368 * matrix and update the guest's AP configuration after all of
2369 * the new queue devices are probed.
2370 */
2371 if (!bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) ||
2372 !bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS))
2373 goto done;
2374
2375 if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
2376 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2377 reset_queues_for_apids(matrix_mdev, apm_filtered);
2378 }
2379 }
2380
2381done:
2382 dev_set_drvdata(&apdev->device, q);
2383 release_update_locks_for_mdev(matrix_mdev);
2384
2385 return ret;
2386
2387err_remove_group:
2388 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2389 return ret;
2390}
2391
2392void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2393{
2394 unsigned long apid, apqi;
2395 struct vfio_ap_queue *q;
2396 struct ap_matrix_mdev *matrix_mdev;
2397
2398 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
2399 q = dev_get_drvdata(&apdev->device);
2400 get_update_locks_for_queue(q);
2401 matrix_mdev = q->matrix_mdev;
2402 apid = AP_QID_CARD(q->apqn);
2403 apqi = AP_QID_QUEUE(q->apqn);
2404
2405 if (matrix_mdev) {
2406 /* If the queue is assigned to the guest's AP configuration */
2407 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
2408 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
2409 /*
2410 * Since the queues are defined via a matrix of adapters
2411 * and domains, it is not possible to hot unplug a
2412 * single queue; so, let's unplug the adapter.
2413 */
2414 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
2415 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2416 reset_queues_for_apid(matrix_mdev, apid);
2417 goto done;
2418 }
2419 }
2420
2421 /*
2422 * If the queue is not in the host's AP configuration, then resetting
2423 * it will fail with response code 01, (APQN not valid); so, let's make
2424 * sure it is in the host's config.
2425 */
2426 if (test_bit_inv(apid, (unsigned long *)matrix_dev->info.apm) &&
2427 test_bit_inv(apqi, (unsigned long *)matrix_dev->info.aqm)) {
2428 vfio_ap_mdev_reset_queue(q);
2429 flush_work(&q->reset_work);
2430 }
2431
2432done:
2433 if (matrix_mdev)
2434 vfio_ap_unlink_queue_fr_mdev(q);
2435
2436 dev_set_drvdata(&apdev->device, NULL);
2437 kfree(q);
2438 release_update_locks_for_mdev(matrix_mdev);
2439}
2440
2441/**
2442 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
2443 * assigned to a mediated device under the control
2444 * of the vfio_ap device driver.
2445 *
2446 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
2447 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
2448 *
2449 * Return:
2450 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
2451 * assigned to a mediated device under the control of the vfio_ap
2452 * device driver.
2453 * * Otherwise, return 0.
2454 */
2455int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
2456{
2457 int ret;
2458
2459 mutex_lock(&matrix_dev->guests_lock);
2460 mutex_lock(&matrix_dev->mdevs_lock);
2461 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
2462 mutex_unlock(&matrix_dev->mdevs_lock);
2463 mutex_unlock(&matrix_dev->guests_lock);
2464
2465 return ret;
2466}
2467
2468/**
2469 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
2470 * domains that have been removed from the host's
2471 * AP configuration from a guest.
2472 *
2473 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
2474 * @aprem: the adapters that have been removed from the host's AP configuration
2475 * @aqrem: the domains that have been removed from the host's AP configuration
2476 * @cdrem: the control domains that have been removed from the host's AP
2477 * configuration.
2478 */
2479static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
2480 unsigned long *aprem,
2481 unsigned long *aqrem,
2482 unsigned long *cdrem)
2483{
2484 int do_hotplug = 0;
2485
2486 if (!bitmap_empty(aprem, AP_DEVICES)) {
2487 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
2488 matrix_mdev->shadow_apcb.apm,
2489 aprem, AP_DEVICES);
2490 }
2491
2492 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
2493 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
2494 matrix_mdev->shadow_apcb.aqm,
2495 aqrem, AP_DEVICES);
2496 }
2497
2498 if (!bitmap_empty(cdrem, AP_DOMAINS))
2499 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
2500 matrix_mdev->shadow_apcb.adm,
2501 cdrem, AP_DOMAINS);
2502
2503 if (do_hotplug)
2504 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2505}
2506
2507/**
2508 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
2509 * domains and control domains that have been removed
2510 * from the host AP configuration and unplugs them
2511 * from those guests.
2512 *
2513 * @ap_remove: bitmap specifying which adapters have been removed from the host
2514 * config.
2515 * @aq_remove: bitmap specifying which domains have been removed from the host
2516 * config.
2517 * @cd_remove: bitmap specifying which control domains have been removed from
2518 * the host config.
2519 */
2520static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
2521 unsigned long *aq_remove,
2522 unsigned long *cd_remove)
2523{
2524 struct ap_matrix_mdev *matrix_mdev;
2525 DECLARE_BITMAP(aprem, AP_DEVICES);
2526 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2527 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2528 int do_remove = 0;
2529
2530 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2531 mutex_lock(&matrix_mdev->kvm->lock);
2532 mutex_lock(&matrix_dev->mdevs_lock);
2533
2534 do_remove |= bitmap_and(aprem, ap_remove,
2535 matrix_mdev->matrix.apm,
2536 AP_DEVICES);
2537 do_remove |= bitmap_and(aqrem, aq_remove,
2538 matrix_mdev->matrix.aqm,
2539 AP_DOMAINS);
2540 do_remove |= bitmap_andnot(cdrem, cd_remove,
2541 matrix_mdev->matrix.adm,
2542 AP_DOMAINS);
2543
2544 if (do_remove)
2545 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2546 cdrem);
2547
2548 mutex_unlock(&matrix_dev->mdevs_lock);
2549 mutex_unlock(&matrix_mdev->kvm->lock);
2550 }
2551}
2552
2553/**
2554 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2555 * control domains from the host AP configuration
2556 * by unplugging them from the guests that are
2557 * using them.
2558 * @cur_config_info: the current host AP configuration information
2559 * @prev_config_info: the previous host AP configuration information
2560 */
2561static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2562 struct ap_config_info *prev_config_info)
2563{
2564 int do_remove;
2565 DECLARE_BITMAP(aprem, AP_DEVICES);
2566 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2567 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2568
2569 do_remove = bitmap_andnot(aprem,
2570 (unsigned long *)prev_config_info->apm,
2571 (unsigned long *)cur_config_info->apm,
2572 AP_DEVICES);
2573 do_remove |= bitmap_andnot(aqrem,
2574 (unsigned long *)prev_config_info->aqm,
2575 (unsigned long *)cur_config_info->aqm,
2576 AP_DEVICES);
2577 do_remove |= bitmap_andnot(cdrem,
2578 (unsigned long *)prev_config_info->adm,
2579 (unsigned long *)cur_config_info->adm,
2580 AP_DEVICES);
2581
2582 if (do_remove)
2583 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2584}
2585
2586/**
2587 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2588 * are older than AP type 10 (CEX4).
2589 * @apm: a bitmap of the APIDs to examine
2590 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2591 */
2592static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2593{
2594 bool apid_cleared;
2595 struct ap_queue_status status;
2596 unsigned long apid, apqi;
2597 struct ap_tapq_hwinfo info;
2598
2599 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2600 apid_cleared = false;
2601
2602 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2603 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2604 switch (status.response_code) {
2605 /*
2606 * According to the architecture in each case
2607 * below, the queue's info should be filled.
2608 */
2609 case AP_RESPONSE_NORMAL:
2610 case AP_RESPONSE_RESET_IN_PROGRESS:
2611 case AP_RESPONSE_DECONFIGURED:
2612 case AP_RESPONSE_CHECKSTOPPED:
2613 case AP_RESPONSE_BUSY:
2614 /*
2615 * The vfio_ap device driver only
2616 * supports CEX4 and newer adapters, so
2617 * remove the APID if the adapter is
2618 * older than a CEX4.
2619 */
2620 if (info.at < AP_DEVICE_TYPE_CEX4) {
2621 clear_bit_inv(apid, apm);
2622 apid_cleared = true;
2623 }
2624
2625 break;
2626
2627 default:
2628 /*
2629 * If we don't know the adapter type,
2630 * clear its APID since it can't be
2631 * determined whether the vfio_ap
2632 * device driver supports it.
2633 */
2634 clear_bit_inv(apid, apm);
2635 apid_cleared = true;
2636 break;
2637 }
2638
2639 /*
2640 * If we've already cleared the APID from the apm, there
2641 * is no need to continue examining the remainin AP
2642 * queues to determine the type of the adapter.
2643 */
2644 if (apid_cleared)
2645 continue;
2646 }
2647 }
2648}
2649
2650/**
2651 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2652 * control domains that have been added to the host's
2653 * AP configuration for each matrix mdev to which they
2654 * are assigned.
2655 *
2656 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2657 * configuration.
2658 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2659 * configuration.
2660 * @adm_add: a bitmap specifying the control domains that have been added to the
2661 * AP configuration.
2662 */
2663static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2664 unsigned long *adm_add)
2665{
2666 struct ap_matrix_mdev *matrix_mdev;
2667
2668 if (list_empty(&matrix_dev->mdev_list))
2669 return;
2670
2671 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2672
2673 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2674 bitmap_and(matrix_mdev->apm_add,
2675 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2676 bitmap_and(matrix_mdev->aqm_add,
2677 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2678 bitmap_and(matrix_mdev->adm_add,
2679 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2680 }
2681}
2682
2683/**
2684 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2685 * control domains to the host AP configuration
2686 * by updating the bitmaps that specify what adapters,
2687 * domains and control domains have been added so they
2688 * can be hot plugged into the guest when the AP bus
2689 * scan completes (see vfio_ap_on_scan_complete
2690 * function).
2691 * @cur_config_info: the current AP configuration information
2692 * @prev_config_info: the previous AP configuration information
2693 */
2694static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2695 struct ap_config_info *prev_config_info)
2696{
2697 bool do_add;
2698 DECLARE_BITMAP(apm_add, AP_DEVICES);
2699 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2700 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2701
2702 do_add = bitmap_andnot(apm_add,
2703 (unsigned long *)cur_config_info->apm,
2704 (unsigned long *)prev_config_info->apm,
2705 AP_DEVICES);
2706 do_add |= bitmap_andnot(aqm_add,
2707 (unsigned long *)cur_config_info->aqm,
2708 (unsigned long *)prev_config_info->aqm,
2709 AP_DOMAINS);
2710 do_add |= bitmap_andnot(adm_add,
2711 (unsigned long *)cur_config_info->adm,
2712 (unsigned long *)prev_config_info->adm,
2713 AP_DOMAINS);
2714
2715 if (do_add)
2716 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2717}
2718
2719/**
2720 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2721 * configuration.
2722 *
2723 * @cur_cfg_info: the current host AP configuration
2724 * @prev_cfg_info: the previous host AP configuration
2725 */
2726void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2727 struct ap_config_info *prev_cfg_info)
2728{
2729 if (!cur_cfg_info || !prev_cfg_info)
2730 return;
2731
2732 mutex_lock(&matrix_dev->guests_lock);
2733
2734 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2735 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2736 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2737
2738 mutex_unlock(&matrix_dev->guests_lock);
2739}
2740
2741static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2742{
2743 DECLARE_BITMAP(apm_filtered, AP_DEVICES);
2744 bool filter_domains, filter_adapters, filter_cdoms, do_hotplug = false;
2745
2746 mutex_lock(&matrix_mdev->kvm->lock);
2747 mutex_lock(&matrix_dev->mdevs_lock);
2748
2749 filter_adapters = bitmap_intersects(matrix_mdev->matrix.apm,
2750 matrix_mdev->apm_add, AP_DEVICES);
2751 filter_domains = bitmap_intersects(matrix_mdev->matrix.aqm,
2752 matrix_mdev->aqm_add, AP_DOMAINS);
2753 filter_cdoms = bitmap_intersects(matrix_mdev->matrix.adm,
2754 matrix_mdev->adm_add, AP_DOMAINS);
2755
2756 if (filter_adapters || filter_domains)
2757 do_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
2758
2759 if (filter_cdoms)
2760 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2761
2762 if (do_hotplug)
2763 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2764
2765 reset_queues_for_apids(matrix_mdev, apm_filtered);
2766
2767 mutex_unlock(&matrix_dev->mdevs_lock);
2768 mutex_unlock(&matrix_mdev->kvm->lock);
2769}
2770
2771void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2772 struct ap_config_info *old_config_info)
2773{
2774 struct ap_matrix_mdev *matrix_mdev;
2775
2776 mutex_lock(&matrix_dev->guests_lock);
2777
2778 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2779 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2780 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2781 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2782 continue;
2783
2784 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2785 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2786 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2787 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2788 }
2789
2790 mutex_unlock(&matrix_dev->guests_lock);
2791}