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