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