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