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