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