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