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