2347808fa3e4277b42ea449ff40ec4be5e60c552
[linux-2.6-microblaze.git] / drivers / s390 / crypto / vfio_ap_ops.c
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 <asm/kvm.h>
20 #include <asm/zcrypt.h>
21
22 #include "vfio_ap_private.h"
23
24 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
25 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
26
27 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
28 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
29 static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
30
31 static int match_apqn(struct device *dev, const void *data)
32 {
33         struct vfio_ap_queue *q = dev_get_drvdata(dev);
34
35         return (q->apqn == *(int *)(data)) ? 1 : 0;
36 }
37
38 /**
39  * vfio_ap_get_queue: Retrieve a queue with a specific APQN from a list
40  * @matrix_mdev: the associated mediated matrix
41  * @apqn: The queue APQN
42  *
43  * Retrieve a queue with a specific APQN from the list of the
44  * devices of the vfio_ap_drv.
45  * Verify that the APID and the APQI are set in the matrix.
46  *
47  * Returns the pointer to the associated vfio_ap_queue
48  */
49 static struct vfio_ap_queue *vfio_ap_get_queue(
50                                         struct ap_matrix_mdev *matrix_mdev,
51                                         int apqn)
52 {
53         struct vfio_ap_queue *q;
54
55         if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
56                 return NULL;
57         if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
58                 return NULL;
59
60         q = vfio_ap_find_queue(apqn);
61         if (q)
62                 q->matrix_mdev = matrix_mdev;
63
64         return q;
65 }
66
67 /**
68  * vfio_ap_wait_for_irqclear
69  * @apqn: The AP Queue number
70  *
71  * Checks the IRQ bit for the status of this APQN using ap_tapq.
72  * Returns if the ap_tapq function succeeded and the bit is clear.
73  * Returns if ap_tapq function failed with invalid, deconfigured or
74  * checkstopped AP.
75  * Otherwise retries up to 5 times after waiting 20ms.
76  *
77  */
78 static void vfio_ap_wait_for_irqclear(int apqn)
79 {
80         struct ap_queue_status status;
81         int retry = 5;
82
83         do {
84                 status = ap_tapq(apqn, NULL);
85                 switch (status.response_code) {
86                 case AP_RESPONSE_NORMAL:
87                 case AP_RESPONSE_RESET_IN_PROGRESS:
88                         if (!status.irq_enabled)
89                                 return;
90                         fallthrough;
91                 case AP_RESPONSE_BUSY:
92                         msleep(20);
93                         break;
94                 case AP_RESPONSE_Q_NOT_AVAIL:
95                 case AP_RESPONSE_DECONFIGURED:
96                 case AP_RESPONSE_CHECKSTOPPED:
97                 default:
98                         WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
99                                   status.response_code, apqn);
100                         return;
101                 }
102         } while (--retry);
103
104         WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
105                   __func__, status.response_code, apqn);
106 }
107
108 /**
109  * vfio_ap_free_aqic_resources
110  * @q: The vfio_ap_queue
111  *
112  * Unregisters the ISC in the GIB when the saved ISC not invalid.
113  * Unpin the guest's page holding the NIB when it exist.
114  * Reset the saved_pfn and saved_isc to invalid values.
115  *
116  */
117 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
118 {
119         if (!q)
120                 return;
121         if (q->saved_isc != VFIO_AP_ISC_INVALID &&
122             !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
123                 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
124                 q->saved_isc = VFIO_AP_ISC_INVALID;
125         }
126         if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
127                 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
128                                  &q->saved_pfn, 1);
129                 q->saved_pfn = 0;
130         }
131 }
132
133 /**
134  * vfio_ap_irq_disable
135  * @q: The vfio_ap_queue
136  *
137  * Uses ap_aqic to disable the interruption and in case of success, reset
138  * in progress or IRQ disable command already proceeded: calls
139  * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
140  * and calls vfio_ap_free_aqic_resources() to free the resources associated
141  * with the AP interrupt handling.
142  *
143  * In the case the AP is busy, or a reset is in progress,
144  * retries after 20ms, up to 5 times.
145  *
146  * Returns if ap_aqic function failed with invalid, deconfigured or
147  * checkstopped AP.
148  */
149 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
150 {
151         struct ap_qirq_ctrl aqic_gisa = {};
152         struct ap_queue_status status;
153         int retries = 5;
154
155         do {
156                 status = ap_aqic(q->apqn, aqic_gisa, NULL);
157                 switch (status.response_code) {
158                 case AP_RESPONSE_OTHERWISE_CHANGED:
159                 case AP_RESPONSE_NORMAL:
160                         vfio_ap_wait_for_irqclear(q->apqn);
161                         goto end_free;
162                 case AP_RESPONSE_RESET_IN_PROGRESS:
163                 case AP_RESPONSE_BUSY:
164                         msleep(20);
165                         break;
166                 case AP_RESPONSE_Q_NOT_AVAIL:
167                 case AP_RESPONSE_DECONFIGURED:
168                 case AP_RESPONSE_CHECKSTOPPED:
169                 case AP_RESPONSE_INVALID_ADDRESS:
170                 default:
171                         /* All cases in default means AP not operational */
172                         WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
173                                   status.response_code);
174                         goto end_free;
175                 }
176         } while (retries--);
177
178         WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
179                   status.response_code);
180 end_free:
181         vfio_ap_free_aqic_resources(q);
182         q->matrix_mdev = NULL;
183         return status;
184 }
185
186 /**
187  * vfio_ap_setirq: Enable Interruption for a APQN
188  *
189  * @dev: the device associated with the ap_queue
190  * @q:   the vfio_ap_queue holding AQIC parameters
191  *
192  * Pin the NIB saved in *q
193  * Register the guest ISC to GIB interface and retrieve the
194  * host ISC to issue the host side PQAP/AQIC
195  *
196  * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
197  * vfio_pin_pages failed.
198  *
199  * Otherwise return the ap_queue_status returned by the ap_aqic(),
200  * all retry handling will be done by the guest.
201  */
202 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
203                                                  int isc,
204                                                  unsigned long nib)
205 {
206         struct ap_qirq_ctrl aqic_gisa = {};
207         struct ap_queue_status status = {};
208         struct kvm_s390_gisa *gisa;
209         struct kvm *kvm;
210         unsigned long h_nib, g_pfn, h_pfn;
211         int ret;
212
213         g_pfn = nib >> PAGE_SHIFT;
214         ret = vfio_pin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1,
215                              IOMMU_READ | IOMMU_WRITE, &h_pfn);
216         switch (ret) {
217         case 1:
218                 break;
219         default:
220                 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
221                 return status;
222         }
223
224         kvm = q->matrix_mdev->kvm;
225         gisa = kvm->arch.gisa_int.origin;
226
227         h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK);
228         aqic_gisa.gisc = isc;
229         aqic_gisa.isc = kvm_s390_gisc_register(kvm, isc);
230         aqic_gisa.ir = 1;
231         aqic_gisa.gisa = (uint64_t)gisa >> 4;
232
233         status = ap_aqic(q->apqn, aqic_gisa, (void *)h_nib);
234         switch (status.response_code) {
235         case AP_RESPONSE_NORMAL:
236                 /* See if we did clear older IRQ configuration */
237                 vfio_ap_free_aqic_resources(q);
238                 q->saved_pfn = g_pfn;
239                 q->saved_isc = isc;
240                 break;
241         case AP_RESPONSE_OTHERWISE_CHANGED:
242                 /* We could not modify IRQ setings: clear new configuration */
243                 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1);
244                 kvm_s390_gisc_unregister(kvm, isc);
245                 break;
246         default:
247                 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
248                         status.response_code);
249                 vfio_ap_irq_disable(q);
250                 break;
251         }
252
253         return status;
254 }
255
256 /**
257  * handle_pqap: PQAP instruction callback
258  *
259  * @vcpu: The vcpu on which we received the PQAP instruction
260  *
261  * Get the general register contents to initialize internal variables.
262  * REG[0]: APQN
263  * REG[1]: IR and ISC
264  * REG[2]: NIB
265  *
266  * Response.status may be set to following Response Code:
267  * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
268  * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
269  * - AP_RESPONSE_NORMAL (0) : in case of successs
270  *   Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
271  * We take the matrix_dev lock to ensure serialization on queues and
272  * mediated device access.
273  *
274  * Return 0 if we could handle the request inside KVM.
275  * otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
276  */
277 static int handle_pqap(struct kvm_vcpu *vcpu)
278 {
279         uint64_t status;
280         uint16_t apqn;
281         struct vfio_ap_queue *q;
282         struct ap_queue_status qstatus = {
283                                .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
284         struct ap_matrix_mdev *matrix_mdev;
285
286         /* If we do not use the AIV facility just go to userland */
287         if (!(vcpu->arch.sie_block->eca & ECA_AIV))
288                 return -EOPNOTSUPP;
289
290         apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
291         mutex_lock(&matrix_dev->lock);
292
293         if (!vcpu->kvm->arch.crypto.pqap_hook)
294                 goto out_unlock;
295         matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
296                                    struct ap_matrix_mdev, pqap_hook);
297
298         /* If the there is no guest using the mdev, there is nothing to do */
299         if (!matrix_mdev->kvm)
300                 goto out_unlock;
301
302         q = vfio_ap_get_queue(matrix_mdev, apqn);
303         if (!q)
304                 goto out_unlock;
305
306         status = vcpu->run->s.regs.gprs[1];
307
308         /* If IR bit(16) is set we enable the interrupt */
309         if ((status >> (63 - 16)) & 0x01)
310                 qstatus = vfio_ap_irq_enable(q, status & 0x07,
311                                              vcpu->run->s.regs.gprs[2]);
312         else
313                 qstatus = vfio_ap_irq_disable(q);
314
315 out_unlock:
316         memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
317         vcpu->run->s.regs.gprs[1] >>= 32;
318         mutex_unlock(&matrix_dev->lock);
319         return 0;
320 }
321
322 static void vfio_ap_matrix_init(struct ap_config_info *info,
323                                 struct ap_matrix *matrix)
324 {
325         matrix->apm_max = info->apxa ? info->Na : 63;
326         matrix->aqm_max = info->apxa ? info->Nd : 15;
327         matrix->adm_max = info->apxa ? info->Nd : 15;
328 }
329
330 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
331 {
332         struct ap_matrix_mdev *matrix_mdev;
333         int ret;
334
335         if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
336                 return -EPERM;
337
338         matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
339         if (!matrix_mdev) {
340                 ret = -ENOMEM;
341                 goto err_dec_available;
342         }
343         vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev,
344                             &vfio_ap_matrix_dev_ops);
345
346         matrix_mdev->mdev = mdev;
347         vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
348         matrix_mdev->pqap_hook = handle_pqap;
349         mutex_lock(&matrix_dev->lock);
350         list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
351         mutex_unlock(&matrix_dev->lock);
352
353         ret = vfio_register_group_dev(&matrix_mdev->vdev);
354         if (ret)
355                 goto err_list;
356         dev_set_drvdata(&mdev->dev, matrix_mdev);
357         return 0;
358
359 err_list:
360         mutex_lock(&matrix_dev->lock);
361         list_del(&matrix_mdev->node);
362         mutex_unlock(&matrix_dev->lock);
363         kfree(matrix_mdev);
364 err_dec_available:
365         atomic_inc(&matrix_dev->available_instances);
366         return ret;
367 }
368
369 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
370 {
371         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
372
373         vfio_unregister_group_dev(&matrix_mdev->vdev);
374
375         mutex_lock(&matrix_dev->lock);
376         vfio_ap_mdev_reset_queues(matrix_mdev);
377         list_del(&matrix_mdev->node);
378         kfree(matrix_mdev);
379         atomic_inc(&matrix_dev->available_instances);
380         mutex_unlock(&matrix_dev->lock);
381 }
382
383 static ssize_t name_show(struct mdev_type *mtype,
384                          struct mdev_type_attribute *attr, char *buf)
385 {
386         return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
387 }
388
389 static MDEV_TYPE_ATTR_RO(name);
390
391 static ssize_t available_instances_show(struct mdev_type *mtype,
392                                         struct mdev_type_attribute *attr,
393                                         char *buf)
394 {
395         return sprintf(buf, "%d\n",
396                        atomic_read(&matrix_dev->available_instances));
397 }
398
399 static MDEV_TYPE_ATTR_RO(available_instances);
400
401 static ssize_t device_api_show(struct mdev_type *mtype,
402                                struct mdev_type_attribute *attr, char *buf)
403 {
404         return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
405 }
406
407 static MDEV_TYPE_ATTR_RO(device_api);
408
409 static struct attribute *vfio_ap_mdev_type_attrs[] = {
410         &mdev_type_attr_name.attr,
411         &mdev_type_attr_device_api.attr,
412         &mdev_type_attr_available_instances.attr,
413         NULL,
414 };
415
416 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
417         .name = VFIO_AP_MDEV_TYPE_HWVIRT,
418         .attrs = vfio_ap_mdev_type_attrs,
419 };
420
421 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
422         &vfio_ap_mdev_hwvirt_type_group,
423         NULL,
424 };
425
426 struct vfio_ap_queue_reserved {
427         unsigned long *apid;
428         unsigned long *apqi;
429         bool reserved;
430 };
431
432 /**
433  * vfio_ap_has_queue
434  *
435  * @dev: an AP queue device
436  * @data: a struct vfio_ap_queue_reserved reference
437  *
438  * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
439  * apid or apqi specified in @data:
440  *
441  * - If @data contains both an apid and apqi value, then @data will be flagged
442  *   as reserved if the APID and APQI fields for the AP queue device matches
443  *
444  * - If @data contains only an apid value, @data will be flagged as
445  *   reserved if the APID field in the AP queue device matches
446  *
447  * - If @data contains only an apqi value, @data will be flagged as
448  *   reserved if the APQI field in the AP queue device matches
449  *
450  * Returns 0 to indicate the input to function succeeded. Returns -EINVAL if
451  * @data does not contain either an apid or apqi.
452  */
453 static int vfio_ap_has_queue(struct device *dev, void *data)
454 {
455         struct vfio_ap_queue_reserved *qres = data;
456         struct ap_queue *ap_queue = to_ap_queue(dev);
457         ap_qid_t qid;
458         unsigned long id;
459
460         if (qres->apid && qres->apqi) {
461                 qid = AP_MKQID(*qres->apid, *qres->apqi);
462                 if (qid == ap_queue->qid)
463                         qres->reserved = true;
464         } else if (qres->apid && !qres->apqi) {
465                 id = AP_QID_CARD(ap_queue->qid);
466                 if (id == *qres->apid)
467                         qres->reserved = true;
468         } else if (!qres->apid && qres->apqi) {
469                 id = AP_QID_QUEUE(ap_queue->qid);
470                 if (id == *qres->apqi)
471                         qres->reserved = true;
472         } else {
473                 return -EINVAL;
474         }
475
476         return 0;
477 }
478
479 /**
480  * vfio_ap_verify_queue_reserved
481  *
482  * @matrix_dev: a mediated matrix device
483  * @apid: an AP adapter ID
484  * @apqi: an AP queue index
485  *
486  * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
487  * driver according to the following rules:
488  *
489  * - If both @apid and @apqi are not NULL, then there must be an AP queue
490  *   device bound to the vfio_ap driver with the APQN identified by @apid and
491  *   @apqi
492  *
493  * - If only @apid is not NULL, then there must be an AP queue device bound
494  *   to the vfio_ap driver with an APQN containing @apid
495  *
496  * - If only @apqi is not NULL, then there must be an AP queue device bound
497  *   to the vfio_ap driver with an APQN containing @apqi
498  *
499  * Returns 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
500  */
501 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
502                                          unsigned long *apqi)
503 {
504         int ret;
505         struct vfio_ap_queue_reserved qres;
506
507         qres.apid = apid;
508         qres.apqi = apqi;
509         qres.reserved = false;
510
511         ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
512                                      &qres, vfio_ap_has_queue);
513         if (ret)
514                 return ret;
515
516         if (qres.reserved)
517                 return 0;
518
519         return -EADDRNOTAVAIL;
520 }
521
522 static int
523 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
524                                              unsigned long apid)
525 {
526         int ret;
527         unsigned long apqi;
528         unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
529
530         if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
531                 return vfio_ap_verify_queue_reserved(&apid, NULL);
532
533         for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
534                 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
535                 if (ret)
536                         return ret;
537         }
538
539         return 0;
540 }
541
542 /**
543  * vfio_ap_mdev_verify_no_sharing
544  *
545  * Verifies that the APQNs derived from the cross product of the AP adapter IDs
546  * and AP queue indexes comprising the AP matrix are not configured for another
547  * mediated device. AP queue sharing is not allowed.
548  *
549  * @matrix_mdev: the mediated matrix device
550  *
551  * Returns 0 if the APQNs are not shared, otherwise; returns -EADDRINUSE.
552  */
553 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
554 {
555         struct ap_matrix_mdev *lstdev;
556         DECLARE_BITMAP(apm, AP_DEVICES);
557         DECLARE_BITMAP(aqm, AP_DOMAINS);
558
559         list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
560                 if (matrix_mdev == lstdev)
561                         continue;
562
563                 memset(apm, 0, sizeof(apm));
564                 memset(aqm, 0, sizeof(aqm));
565
566                 /*
567                  * We work on full longs, as we can only exclude the leftover
568                  * bits in non-inverse order. The leftover is all zeros.
569                  */
570                 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
571                                 lstdev->matrix.apm, AP_DEVICES))
572                         continue;
573
574                 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
575                                 lstdev->matrix.aqm, AP_DOMAINS))
576                         continue;
577
578                 return -EADDRINUSE;
579         }
580
581         return 0;
582 }
583
584 /**
585  * assign_adapter_store
586  *
587  * @dev:        the matrix device
588  * @attr:       the mediated matrix device's assign_adapter attribute
589  * @buf:        a buffer containing the AP adapter number (APID) to
590  *              be assigned
591  * @count:      the number of bytes in @buf
592  *
593  * Parses the APID from @buf and sets the corresponding bit in the mediated
594  * matrix device's APM.
595  *
596  * Returns the number of bytes processed if the APID is valid; otherwise,
597  * returns one of the following errors:
598  *
599  *      1. -EINVAL
600  *         The APID is not a valid number
601  *
602  *      2. -ENODEV
603  *         The APID exceeds the maximum value configured for the system
604  *
605  *      3. -EADDRNOTAVAIL
606  *         An APQN derived from the cross product of the APID being assigned
607  *         and the APQIs previously assigned is not bound to the vfio_ap device
608  *         driver; or, if no APQIs have yet been assigned, the APID is not
609  *         contained in an APQN bound to the vfio_ap device driver.
610  *
611  *      4. -EADDRINUSE
612  *         An APQN derived from the cross product of the APID being assigned
613  *         and the APQIs previously assigned is being used by another mediated
614  *         matrix device
615  */
616 static ssize_t assign_adapter_store(struct device *dev,
617                                     struct device_attribute *attr,
618                                     const char *buf, size_t count)
619 {
620         int ret;
621         unsigned long apid;
622         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
623
624         mutex_lock(&matrix_dev->lock);
625
626         /* If the KVM guest is running, disallow assignment of adapter */
627         if (matrix_mdev->kvm) {
628                 ret = -EBUSY;
629                 goto done;
630         }
631
632         ret = kstrtoul(buf, 0, &apid);
633         if (ret)
634                 goto done;
635
636         if (apid > matrix_mdev->matrix.apm_max) {
637                 ret = -ENODEV;
638                 goto done;
639         }
640
641         /*
642          * Set the bit in the AP mask (APM) corresponding to the AP adapter
643          * number (APID). The bits in the mask, from most significant to least
644          * significant bit, correspond to APIDs 0-255.
645          */
646         ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
647         if (ret)
648                 goto done;
649
650         set_bit_inv(apid, matrix_mdev->matrix.apm);
651
652         ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
653         if (ret)
654                 goto share_err;
655
656         ret = count;
657         goto done;
658
659 share_err:
660         clear_bit_inv(apid, matrix_mdev->matrix.apm);
661 done:
662         mutex_unlock(&matrix_dev->lock);
663
664         return ret;
665 }
666 static DEVICE_ATTR_WO(assign_adapter);
667
668 /**
669  * unassign_adapter_store
670  *
671  * @dev:        the matrix device
672  * @attr:       the mediated matrix device's unassign_adapter attribute
673  * @buf:        a buffer containing the adapter number (APID) to be unassigned
674  * @count:      the number of bytes in @buf
675  *
676  * Parses the APID from @buf and clears the corresponding bit in the mediated
677  * matrix device's APM.
678  *
679  * Returns the number of bytes processed if the APID is valid; otherwise,
680  * returns one of the following errors:
681  *      -EINVAL if the APID is not a number
682  *      -ENODEV if the APID it exceeds the maximum value configured for the
683  *              system
684  */
685 static ssize_t unassign_adapter_store(struct device *dev,
686                                       struct device_attribute *attr,
687                                       const char *buf, size_t count)
688 {
689         int ret;
690         unsigned long apid;
691         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
692
693         mutex_lock(&matrix_dev->lock);
694
695         /* If the KVM guest is running, disallow unassignment of adapter */
696         if (matrix_mdev->kvm) {
697                 ret = -EBUSY;
698                 goto done;
699         }
700
701         ret = kstrtoul(buf, 0, &apid);
702         if (ret)
703                 goto done;
704
705         if (apid > matrix_mdev->matrix.apm_max) {
706                 ret = -ENODEV;
707                 goto done;
708         }
709
710         clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
711         ret = count;
712 done:
713         mutex_unlock(&matrix_dev->lock);
714         return ret;
715 }
716 static DEVICE_ATTR_WO(unassign_adapter);
717
718 static int
719 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
720                                              unsigned long apqi)
721 {
722         int ret;
723         unsigned long apid;
724         unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
725
726         if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
727                 return vfio_ap_verify_queue_reserved(NULL, &apqi);
728
729         for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
730                 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
731                 if (ret)
732                         return ret;
733         }
734
735         return 0;
736 }
737
738 /**
739  * assign_domain_store
740  *
741  * @dev:        the matrix device
742  * @attr:       the mediated matrix device's assign_domain attribute
743  * @buf:        a buffer containing the AP queue index (APQI) of the domain to
744  *              be assigned
745  * @count:      the number of bytes in @buf
746  *
747  * Parses the APQI from @buf and sets the corresponding bit in the mediated
748  * matrix device's AQM.
749  *
750  * Returns the number of bytes processed if the APQI is valid; otherwise returns
751  * one of the following errors:
752  *
753  *      1. -EINVAL
754  *         The APQI is not a valid number
755  *
756  *      2. -ENODEV
757  *         The APQI exceeds the maximum value configured for the system
758  *
759  *      3. -EADDRNOTAVAIL
760  *         An APQN derived from the cross product of the APQI being assigned
761  *         and the APIDs previously assigned is not bound to the vfio_ap device
762  *         driver; or, if no APIDs have yet been assigned, the APQI is not
763  *         contained in an APQN bound to the vfio_ap device driver.
764  *
765  *      4. -EADDRINUSE
766  *         An APQN derived from the cross product of the APQI being assigned
767  *         and the APIDs previously assigned is being used by another mediated
768  *         matrix device
769  */
770 static ssize_t assign_domain_store(struct device *dev,
771                                    struct device_attribute *attr,
772                                    const char *buf, size_t count)
773 {
774         int ret;
775         unsigned long apqi;
776         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
777         unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
778
779         mutex_lock(&matrix_dev->lock);
780
781         /* If the KVM guest is running, disallow assignment of domain */
782         if (matrix_mdev->kvm) {
783                 ret = -EBUSY;
784                 goto done;
785         }
786
787         ret = kstrtoul(buf, 0, &apqi);
788         if (ret)
789                 goto done;
790         if (apqi > max_apqi) {
791                 ret = -ENODEV;
792                 goto done;
793         }
794
795         ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
796         if (ret)
797                 goto done;
798
799         set_bit_inv(apqi, matrix_mdev->matrix.aqm);
800
801         ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
802         if (ret)
803                 goto share_err;
804
805         ret = count;
806         goto done;
807
808 share_err:
809         clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
810 done:
811         mutex_unlock(&matrix_dev->lock);
812
813         return ret;
814 }
815 static DEVICE_ATTR_WO(assign_domain);
816
817
818 /**
819  * unassign_domain_store
820  *
821  * @dev:        the matrix device
822  * @attr:       the mediated matrix device's unassign_domain attribute
823  * @buf:        a buffer containing the AP queue index (APQI) of the domain to
824  *              be unassigned
825  * @count:      the number of bytes in @buf
826  *
827  * Parses the APQI from @buf and clears the corresponding bit in the
828  * mediated matrix device's AQM.
829  *
830  * Returns the number of bytes processed if the APQI is valid; otherwise,
831  * returns one of the following errors:
832  *      -EINVAL if the APQI is not a number
833  *      -ENODEV if the APQI exceeds the maximum value configured for the system
834  */
835 static ssize_t unassign_domain_store(struct device *dev,
836                                      struct device_attribute *attr,
837                                      const char *buf, size_t count)
838 {
839         int ret;
840         unsigned long apqi;
841         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
842
843         mutex_lock(&matrix_dev->lock);
844
845         /* If the KVM guest is running, disallow unassignment of domain */
846         if (matrix_mdev->kvm) {
847                 ret = -EBUSY;
848                 goto done;
849         }
850
851         ret = kstrtoul(buf, 0, &apqi);
852         if (ret)
853                 goto done;
854
855         if (apqi > matrix_mdev->matrix.aqm_max) {
856                 ret = -ENODEV;
857                 goto done;
858         }
859
860         clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
861         ret = count;
862
863 done:
864         mutex_unlock(&matrix_dev->lock);
865         return ret;
866 }
867 static DEVICE_ATTR_WO(unassign_domain);
868
869 /**
870  * assign_control_domain_store
871  *
872  * @dev:        the matrix device
873  * @attr:       the mediated matrix device's assign_control_domain attribute
874  * @buf:        a buffer containing the domain ID to be assigned
875  * @count:      the number of bytes in @buf
876  *
877  * Parses the domain ID from @buf and sets the corresponding bit in the mediated
878  * matrix device's ADM.
879  *
880  * Returns the number of bytes processed if the domain ID is valid; otherwise,
881  * returns one of the following errors:
882  *      -EINVAL if the ID is not a number
883  *      -ENODEV if the ID exceeds the maximum value configured for the system
884  */
885 static ssize_t assign_control_domain_store(struct device *dev,
886                                            struct device_attribute *attr,
887                                            const char *buf, size_t count)
888 {
889         int ret;
890         unsigned long id;
891         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
892
893         mutex_lock(&matrix_dev->lock);
894
895         /* If the KVM guest is running, disallow assignment of control domain */
896         if (matrix_mdev->kvm) {
897                 ret = -EBUSY;
898                 goto done;
899         }
900
901         ret = kstrtoul(buf, 0, &id);
902         if (ret)
903                 goto done;
904
905         if (id > matrix_mdev->matrix.adm_max) {
906                 ret = -ENODEV;
907                 goto done;
908         }
909
910         /* Set the bit in the ADM (bitmask) corresponding to the AP control
911          * domain number (id). The bits in the mask, from most significant to
912          * least significant, correspond to IDs 0 up to the one less than the
913          * number of control domains that can be assigned.
914          */
915         set_bit_inv(id, matrix_mdev->matrix.adm);
916         ret = count;
917 done:
918         mutex_unlock(&matrix_dev->lock);
919         return ret;
920 }
921 static DEVICE_ATTR_WO(assign_control_domain);
922
923 /**
924  * unassign_control_domain_store
925  *
926  * @dev:        the matrix device
927  * @attr:       the mediated matrix device's unassign_control_domain attribute
928  * @buf:        a buffer containing the domain ID to be unassigned
929  * @count:      the number of bytes in @buf
930  *
931  * Parses the domain ID from @buf and clears the corresponding bit in the
932  * mediated matrix device's ADM.
933  *
934  * Returns the number of bytes processed if the domain ID is valid; otherwise,
935  * returns one of the following errors:
936  *      -EINVAL if the ID is not a number
937  *      -ENODEV if the ID exceeds the maximum value configured for the system
938  */
939 static ssize_t unassign_control_domain_store(struct device *dev,
940                                              struct device_attribute *attr,
941                                              const char *buf, size_t count)
942 {
943         int ret;
944         unsigned long domid;
945         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
946         unsigned long max_domid =  matrix_mdev->matrix.adm_max;
947
948         mutex_lock(&matrix_dev->lock);
949
950         /* If a KVM guest is running, disallow unassignment of control domain */
951         if (matrix_mdev->kvm) {
952                 ret = -EBUSY;
953                 goto done;
954         }
955
956         ret = kstrtoul(buf, 0, &domid);
957         if (ret)
958                 goto done;
959         if (domid > max_domid) {
960                 ret = -ENODEV;
961                 goto done;
962         }
963
964         clear_bit_inv(domid, matrix_mdev->matrix.adm);
965         ret = count;
966 done:
967         mutex_unlock(&matrix_dev->lock);
968         return ret;
969 }
970 static DEVICE_ATTR_WO(unassign_control_domain);
971
972 static ssize_t control_domains_show(struct device *dev,
973                                     struct device_attribute *dev_attr,
974                                     char *buf)
975 {
976         unsigned long id;
977         int nchars = 0;
978         int n;
979         char *bufpos = buf;
980         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
981         unsigned long max_domid = matrix_mdev->matrix.adm_max;
982
983         mutex_lock(&matrix_dev->lock);
984         for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
985                 n = sprintf(bufpos, "%04lx\n", id);
986                 bufpos += n;
987                 nchars += n;
988         }
989         mutex_unlock(&matrix_dev->lock);
990
991         return nchars;
992 }
993 static DEVICE_ATTR_RO(control_domains);
994
995 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
996                            char *buf)
997 {
998         struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
999         char *bufpos = buf;
1000         unsigned long apid;
1001         unsigned long apqi;
1002         unsigned long apid1;
1003         unsigned long apqi1;
1004         unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
1005         unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
1006         int nchars = 0;
1007         int n;
1008
1009         apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
1010         apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
1011
1012         mutex_lock(&matrix_dev->lock);
1013
1014         if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1015                 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1016                         for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1017                                              naqm_bits) {
1018                                 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1019                                             apqi);
1020                                 bufpos += n;
1021                                 nchars += n;
1022                         }
1023                 }
1024         } else if (apid1 < napm_bits) {
1025                 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1026                         n = sprintf(bufpos, "%02lx.\n", apid);
1027                         bufpos += n;
1028                         nchars += n;
1029                 }
1030         } else if (apqi1 < naqm_bits) {
1031                 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
1032                         n = sprintf(bufpos, ".%04lx\n", apqi);
1033                         bufpos += n;
1034                         nchars += n;
1035                 }
1036         }
1037
1038         mutex_unlock(&matrix_dev->lock);
1039
1040         return nchars;
1041 }
1042 static DEVICE_ATTR_RO(matrix);
1043
1044 static struct attribute *vfio_ap_mdev_attrs[] = {
1045         &dev_attr_assign_adapter.attr,
1046         &dev_attr_unassign_adapter.attr,
1047         &dev_attr_assign_domain.attr,
1048         &dev_attr_unassign_domain.attr,
1049         &dev_attr_assign_control_domain.attr,
1050         &dev_attr_unassign_control_domain.attr,
1051         &dev_attr_control_domains.attr,
1052         &dev_attr_matrix.attr,
1053         NULL,
1054 };
1055
1056 static struct attribute_group vfio_ap_mdev_attr_group = {
1057         .attrs = vfio_ap_mdev_attrs
1058 };
1059
1060 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1061         &vfio_ap_mdev_attr_group,
1062         NULL
1063 };
1064
1065 /**
1066  * vfio_ap_mdev_set_kvm
1067  *
1068  * @matrix_mdev: a mediated matrix device
1069  * @kvm: reference to KVM instance
1070  *
1071  * Sets all data for @matrix_mdev that are needed to manage AP resources
1072  * for the guest whose state is represented by @kvm.
1073  *
1074  * Note: The matrix_dev->lock must be taken prior to calling
1075  * this function; however, the lock will be temporarily released while the
1076  * guest's AP configuration is set to avoid a potential lockdep splat.
1077  * The kvm->lock is taken to set the guest's AP configuration which, under
1078  * certain circumstances, will result in a circular lock dependency if this is
1079  * done under the @matrix_mdev->lock.
1080  *
1081  * Return 0 if no other mediated matrix device has a reference to @kvm;
1082  * otherwise, returns an -EPERM.
1083  */
1084 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1085                                 struct kvm *kvm)
1086 {
1087         struct ap_matrix_mdev *m;
1088
1089         if (kvm->arch.crypto.crycbd) {
1090                 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1091                 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1092                 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1093
1094                 mutex_lock(&kvm->lock);
1095                 mutex_lock(&matrix_dev->lock);
1096
1097                 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1098                         if (m != matrix_mdev && m->kvm == kvm) {
1099                                 mutex_unlock(&kvm->lock);
1100                                 mutex_unlock(&matrix_dev->lock);
1101                                 return -EPERM;
1102                         }
1103                 }
1104
1105                 kvm_get_kvm(kvm);
1106                 matrix_mdev->kvm = kvm;
1107                 kvm_arch_crypto_set_masks(kvm,
1108                                           matrix_mdev->matrix.apm,
1109                                           matrix_mdev->matrix.aqm,
1110                                           matrix_mdev->matrix.adm);
1111
1112                 mutex_unlock(&kvm->lock);
1113                 mutex_unlock(&matrix_dev->lock);
1114         }
1115
1116         return 0;
1117 }
1118
1119 /*
1120  * vfio_ap_mdev_iommu_notifier: IOMMU notifier callback
1121  *
1122  * @nb: The notifier block
1123  * @action: Action to be taken
1124  * @data: data associated with the request
1125  *
1126  * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1127  * pinned before). Other requests are ignored.
1128  *
1129  */
1130 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1131                                        unsigned long action, void *data)
1132 {
1133         struct ap_matrix_mdev *matrix_mdev;
1134
1135         matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1136
1137         if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1138                 struct vfio_iommu_type1_dma_unmap *unmap = data;
1139                 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1140
1141                 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1142                 return NOTIFY_OK;
1143         }
1144
1145         return NOTIFY_DONE;
1146 }
1147
1148 /**
1149  * vfio_ap_mdev_unset_kvm
1150  *
1151  * @matrix_mdev: a matrix mediated device
1152  *
1153  * Performs clean-up of resources no longer needed by @matrix_mdev.
1154  *
1155  * Note: The matrix_dev->lock must be taken prior to calling
1156  * this function; however, the lock will be temporarily released while the
1157  * guest's AP configuration is cleared to avoid a potential lockdep splat.
1158  * The kvm->lock is taken to clear the guest's AP configuration which, under
1159  * certain circumstances, will result in a circular lock dependency if this is
1160  * done under the @matrix_mdev->lock.
1161  *
1162  */
1163 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev,
1164                                    struct kvm *kvm)
1165 {
1166         if (kvm && kvm->arch.crypto.crycbd) {
1167                 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1168                 kvm->arch.crypto.pqap_hook = NULL;
1169                 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1170
1171                 mutex_lock(&kvm->lock);
1172                 mutex_lock(&matrix_dev->lock);
1173
1174                 kvm_arch_crypto_clear_masks(kvm);
1175                 vfio_ap_mdev_reset_queues(matrix_mdev);
1176                 kvm_put_kvm(kvm);
1177                 matrix_mdev->kvm = NULL;
1178
1179                 mutex_unlock(&kvm->lock);
1180                 mutex_unlock(&matrix_dev->lock);
1181         }
1182 }
1183
1184 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1185                                        unsigned long action, void *data)
1186 {
1187         int notify_rc = NOTIFY_OK;
1188         struct ap_matrix_mdev *matrix_mdev;
1189
1190         if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1191                 return NOTIFY_OK;
1192
1193         matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1194
1195         if (!data)
1196                 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1197         else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1198                 notify_rc = NOTIFY_DONE;
1199
1200         return notify_rc;
1201 }
1202
1203 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1204 {
1205         struct device *dev;
1206         struct vfio_ap_queue *q = NULL;
1207
1208         dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1209                                  &apqn, match_apqn);
1210         if (dev) {
1211                 q = dev_get_drvdata(dev);
1212                 put_device(dev);
1213         }
1214
1215         return q;
1216 }
1217
1218 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1219                              unsigned int retry)
1220 {
1221         struct ap_queue_status status;
1222         int ret;
1223         int retry2 = 2;
1224
1225         if (!q)
1226                 return 0;
1227
1228 retry_zapq:
1229         status = ap_zapq(q->apqn);
1230         switch (status.response_code) {
1231         case AP_RESPONSE_NORMAL:
1232                 ret = 0;
1233                 break;
1234         case AP_RESPONSE_RESET_IN_PROGRESS:
1235                 if (retry--) {
1236                         msleep(20);
1237                         goto retry_zapq;
1238                 }
1239                 ret = -EBUSY;
1240                 break;
1241         case AP_RESPONSE_Q_NOT_AVAIL:
1242         case AP_RESPONSE_DECONFIGURED:
1243         case AP_RESPONSE_CHECKSTOPPED:
1244                 WARN_ON_ONCE(status.irq_enabled);
1245                 ret = -EBUSY;
1246                 goto free_resources;
1247         default:
1248                 /* things are really broken, give up */
1249                 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1250                      status.response_code);
1251                 return -EIO;
1252         }
1253
1254         /* wait for the reset to take effect */
1255         while (retry2--) {
1256                 if (status.queue_empty && !status.irq_enabled)
1257                         break;
1258                 msleep(20);
1259                 status = ap_tapq(q->apqn, NULL);
1260         }
1261         WARN_ON_ONCE(retry2 <= 0);
1262
1263 free_resources:
1264         vfio_ap_free_aqic_resources(q);
1265
1266         return ret;
1267 }
1268
1269 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1270 {
1271         int ret;
1272         int rc = 0;
1273         unsigned long apid, apqi;
1274         struct vfio_ap_queue *q;
1275
1276         for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1277                              matrix_mdev->matrix.apm_max + 1) {
1278                 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1279                                      matrix_mdev->matrix.aqm_max + 1) {
1280                         q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1281                         ret = vfio_ap_mdev_reset_queue(q, 1);
1282                         /*
1283                          * Regardless whether a queue turns out to be busy, or
1284                          * is not operational, we need to continue resetting
1285                          * the remaining queues.
1286                          */
1287                         if (ret)
1288                                 rc = ret;
1289                 }
1290         }
1291
1292         return rc;
1293 }
1294
1295 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1296 {
1297         struct ap_matrix_mdev *matrix_mdev =
1298                 container_of(vdev, struct ap_matrix_mdev, vdev);
1299         unsigned long events;
1300         int ret;
1301
1302         matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1303         events = VFIO_GROUP_NOTIFY_SET_KVM;
1304
1305         ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1306                                      &events, &matrix_mdev->group_notifier);
1307         if (ret)
1308                 return ret;
1309
1310         matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1311         events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1312         ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1313                                      &events, &matrix_mdev->iommu_notifier);
1314         if (ret)
1315                 goto out_unregister_group;
1316         return 0;
1317
1318 out_unregister_group:
1319         vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1320                                  &matrix_mdev->group_notifier);
1321         return ret;
1322 }
1323
1324 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1325 {
1326         struct ap_matrix_mdev *matrix_mdev =
1327                 container_of(vdev, struct ap_matrix_mdev, vdev);
1328
1329         vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1330                                  &matrix_mdev->iommu_notifier);
1331         vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1332                                  &matrix_mdev->group_notifier);
1333         vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1334 }
1335
1336 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1337 {
1338         unsigned long minsz;
1339         struct vfio_device_info info;
1340
1341         minsz = offsetofend(struct vfio_device_info, num_irqs);
1342
1343         if (copy_from_user(&info, (void __user *)arg, minsz))
1344                 return -EFAULT;
1345
1346         if (info.argsz < minsz)
1347                 return -EINVAL;
1348
1349         info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1350         info.num_regions = 0;
1351         info.num_irqs = 0;
1352
1353         return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1354 }
1355
1356 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1357                                     unsigned int cmd, unsigned long arg)
1358 {
1359         struct ap_matrix_mdev *matrix_mdev =
1360                 container_of(vdev, struct ap_matrix_mdev, vdev);
1361         int ret;
1362
1363         mutex_lock(&matrix_dev->lock);
1364         switch (cmd) {
1365         case VFIO_DEVICE_GET_INFO:
1366                 ret = vfio_ap_mdev_get_device_info(arg);
1367                 break;
1368         case VFIO_DEVICE_RESET:
1369                 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
1370                 break;
1371         default:
1372                 ret = -EOPNOTSUPP;
1373                 break;
1374         }
1375         mutex_unlock(&matrix_dev->lock);
1376
1377         return ret;
1378 }
1379
1380 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1381         .open_device = vfio_ap_mdev_open_device,
1382         .close_device = vfio_ap_mdev_close_device,
1383         .ioctl = vfio_ap_mdev_ioctl,
1384 };
1385
1386 static struct mdev_driver vfio_ap_matrix_driver = {
1387         .driver = {
1388                 .name = "vfio_ap_mdev",
1389                 .owner = THIS_MODULE,
1390                 .mod_name = KBUILD_MODNAME,
1391                 .dev_groups = vfio_ap_mdev_attr_groups,
1392         },
1393         .probe = vfio_ap_mdev_probe,
1394         .remove = vfio_ap_mdev_remove,
1395 };
1396
1397 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1398         .owner                  = THIS_MODULE,
1399         .device_driver          = &vfio_ap_matrix_driver,
1400         .supported_type_groups  = vfio_ap_mdev_type_groups,
1401 };
1402
1403 int vfio_ap_mdev_register(void)
1404 {
1405         int ret;
1406
1407         atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1408
1409         ret = mdev_register_driver(&vfio_ap_matrix_driver);
1410         if (ret)
1411                 return ret;
1412
1413         ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1414         if (ret)
1415                 goto err_driver;
1416         return 0;
1417
1418 err_driver:
1419         mdev_unregister_driver(&vfio_ap_matrix_driver);
1420         return ret;
1421 }
1422
1423 void vfio_ap_mdev_unregister(void)
1424 {
1425         mdev_unregister_device(&matrix_dev->device);
1426         mdev_unregister_driver(&vfio_ap_matrix_driver);
1427 }