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 <linux/uuid.h> 20 #include <asm/kvm.h> 21 #include <asm/zcrypt.h> 22 23 #include "vfio_ap_private.h" 24 #include "vfio_ap_debug.h" 25 26 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough" 27 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device" 28 29 #define AP_QUEUE_ASSIGNED "assigned" 30 #define AP_QUEUE_UNASSIGNED "unassigned" 31 #define AP_QUEUE_IN_USE "in use" 32 33 #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */ 34 35 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable); 36 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn); 37 static const struct vfio_device_ops vfio_ap_matrix_dev_ops; 38 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q); 39 40 /** 41 * get_update_locks_for_kvm: Acquire the locks required to dynamically update a 42 * KVM guest's APCB in the proper order. 43 * 44 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB. 45 * 46 * The proper locking order is: 47 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM 48 * guest's APCB. 49 * 2. kvm->lock: required to update a guest's APCB 50 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev 51 * 52 * Note: If @kvm is NULL, the KVM lock will not be taken. 53 */ 54 static inline void get_update_locks_for_kvm(struct kvm *kvm) 55 { 56 mutex_lock(&matrix_dev->guests_lock); 57 if (kvm) 58 mutex_lock(&kvm->lock); 59 mutex_lock(&matrix_dev->mdevs_lock); 60 } 61 62 /** 63 * release_update_locks_for_kvm: Release the locks used to dynamically update a 64 * KVM guest's APCB in the proper order. 65 * 66 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB. 67 * 68 * The proper unlocking order is: 69 * 1. matrix_dev->mdevs_lock 70 * 2. kvm->lock 71 * 3. matrix_dev->guests_lock 72 * 73 * Note: If @kvm is NULL, the KVM lock will not be released. 74 */ 75 static inline void release_update_locks_for_kvm(struct kvm *kvm) 76 { 77 mutex_unlock(&matrix_dev->mdevs_lock); 78 if (kvm) 79 mutex_unlock(&kvm->lock); 80 mutex_unlock(&matrix_dev->guests_lock); 81 } 82 83 /** 84 * get_update_locks_for_mdev: Acquire the locks required to dynamically update a 85 * KVM guest's APCB in the proper order. 86 * 87 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP 88 * configuration data to use to update a KVM guest's APCB. 89 * 90 * The proper locking order is: 91 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM 92 * guest's APCB. 93 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB 94 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev 95 * 96 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM 97 * lock will not be taken. 98 */ 99 static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev) 100 { 101 mutex_lock(&matrix_dev->guests_lock); 102 if (matrix_mdev && matrix_mdev->kvm) 103 mutex_lock(&matrix_mdev->kvm->lock); 104 mutex_lock(&matrix_dev->mdevs_lock); 105 } 106 107 /** 108 * release_update_locks_for_mdev: Release the locks used to dynamically update a 109 * KVM guest's APCB in the proper order. 110 * 111 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP 112 * configuration data to use to update a KVM guest's APCB. 113 * 114 * The proper unlocking order is: 115 * 1. matrix_dev->mdevs_lock 116 * 2. matrix_mdev->kvm->lock 117 * 3. matrix_dev->guests_lock 118 * 119 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM 120 * lock will not be released. 121 */ 122 static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev) 123 { 124 mutex_unlock(&matrix_dev->mdevs_lock); 125 if (matrix_mdev && matrix_mdev->kvm) 126 mutex_unlock(&matrix_mdev->kvm->lock); 127 mutex_unlock(&matrix_dev->guests_lock); 128 } 129 130 /** 131 * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and 132 * acquire the locks required to update the APCB of 133 * the KVM guest to which the mdev is attached. 134 * 135 * @apqn: the APQN of a queue device. 136 * 137 * The proper locking order is: 138 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM 139 * guest's APCB. 140 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB 141 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev 142 * 143 * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock 144 * will not be taken. 145 * 146 * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn 147 * is not assigned to an ap_matrix_mdev. 148 */ 149 static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn) 150 { 151 struct ap_matrix_mdev *matrix_mdev; 152 153 mutex_lock(&matrix_dev->guests_lock); 154 155 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) { 156 if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) && 157 test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) { 158 if (matrix_mdev->kvm) 159 mutex_lock(&matrix_mdev->kvm->lock); 160 161 mutex_lock(&matrix_dev->mdevs_lock); 162 163 return matrix_mdev; 164 } 165 } 166 167 mutex_lock(&matrix_dev->mdevs_lock); 168 169 return NULL; 170 } 171 172 /** 173 * get_update_locks_for_queue: get the locks required to update the APCB of the 174 * KVM guest to which the matrix mdev linked to a 175 * vfio_ap_queue object is attached. 176 * 177 * @q: a pointer to a vfio_ap_queue object. 178 * 179 * The proper locking order is: 180 * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a 181 * KVM guest's APCB. 182 * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB 183 * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev 184 * 185 * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock 186 * will not be taken. 187 */ 188 static inline void get_update_locks_for_queue(struct vfio_ap_queue *q) 189 { 190 mutex_lock(&matrix_dev->guests_lock); 191 if (q->matrix_mdev && q->matrix_mdev->kvm) 192 mutex_lock(&q->matrix_mdev->kvm->lock); 193 mutex_lock(&matrix_dev->mdevs_lock); 194 } 195 196 /** 197 * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a 198 * hash table of queues assigned to a matrix mdev 199 * @matrix_mdev: the matrix mdev 200 * @apqn: The APQN of a queue device 201 * 202 * Return: the pointer to the vfio_ap_queue struct representing the queue or 203 * NULL if the queue is not assigned to @matrix_mdev 204 */ 205 static struct vfio_ap_queue *vfio_ap_mdev_get_queue( 206 struct ap_matrix_mdev *matrix_mdev, 207 int apqn) 208 { 209 struct vfio_ap_queue *q; 210 211 hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode, 212 apqn) { 213 if (q && q->apqn == apqn) 214 return q; 215 } 216 217 return NULL; 218 } 219 220 /** 221 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries 222 * @apqn: The AP Queue number 223 * 224 * Checks the IRQ bit for the status of this APQN using ap_tapq. 225 * Returns if the ap_tapq function succeeded and the bit is clear. 226 * Returns if ap_tapq function failed with invalid, deconfigured or 227 * checkstopped AP. 228 * Otherwise retries up to 5 times after waiting 20ms. 229 */ 230 static void vfio_ap_wait_for_irqclear(int apqn) 231 { 232 struct ap_queue_status status; 233 int retry = 5; 234 235 do { 236 status = ap_tapq(apqn, NULL); 237 switch (status.response_code) { 238 case AP_RESPONSE_NORMAL: 239 case AP_RESPONSE_RESET_IN_PROGRESS: 240 if (!status.irq_enabled) 241 return; 242 fallthrough; 243 case AP_RESPONSE_BUSY: 244 msleep(20); 245 break; 246 case AP_RESPONSE_Q_NOT_AVAIL: 247 case AP_RESPONSE_DECONFIGURED: 248 case AP_RESPONSE_CHECKSTOPPED: 249 default: 250 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__, 251 status.response_code, apqn); 252 return; 253 } 254 } while (--retry); 255 256 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n", 257 __func__, status.response_code, apqn); 258 } 259 260 /** 261 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources 262 * @q: The vfio_ap_queue 263 * 264 * Unregisters the ISC in the GIB when the saved ISC not invalid. 265 * Unpins the guest's page holding the NIB when it exists. 266 * Resets the saved_iova and saved_isc to invalid values. 267 */ 268 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q) 269 { 270 if (!q) 271 return; 272 if (q->saved_isc != VFIO_AP_ISC_INVALID && 273 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) { 274 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc); 275 q->saved_isc = VFIO_AP_ISC_INVALID; 276 } 277 if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) { 278 vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1); 279 q->saved_iova = 0; 280 } 281 } 282 283 /** 284 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt 285 * @q: The vfio_ap_queue 286 * 287 * Uses ap_aqic to disable the interruption and in case of success, reset 288 * in progress or IRQ disable command already proceeded: calls 289 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear 290 * and calls vfio_ap_free_aqic_resources() to free the resources associated 291 * with the AP interrupt handling. 292 * 293 * In the case the AP is busy, or a reset is in progress, 294 * retries after 20ms, up to 5 times. 295 * 296 * Returns if ap_aqic function failed with invalid, deconfigured or 297 * checkstopped AP. 298 * 299 * Return: &struct ap_queue_status 300 */ 301 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q) 302 { 303 union ap_qirq_ctrl aqic_gisa = { .value = 0 }; 304 struct ap_queue_status status; 305 int retries = 5; 306 307 do { 308 status = ap_aqic(q->apqn, aqic_gisa, 0); 309 switch (status.response_code) { 310 case AP_RESPONSE_OTHERWISE_CHANGED: 311 case AP_RESPONSE_NORMAL: 312 vfio_ap_wait_for_irqclear(q->apqn); 313 goto end_free; 314 case AP_RESPONSE_RESET_IN_PROGRESS: 315 case AP_RESPONSE_BUSY: 316 msleep(20); 317 break; 318 case AP_RESPONSE_Q_NOT_AVAIL: 319 case AP_RESPONSE_DECONFIGURED: 320 case AP_RESPONSE_CHECKSTOPPED: 321 case AP_RESPONSE_INVALID_ADDRESS: 322 default: 323 /* All cases in default means AP not operational */ 324 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__, 325 status.response_code); 326 goto end_free; 327 } 328 } while (retries--); 329 330 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__, 331 status.response_code); 332 end_free: 333 vfio_ap_free_aqic_resources(q); 334 return status; 335 } 336 337 /** 338 * vfio_ap_validate_nib - validate a notification indicator byte (nib) address. 339 * 340 * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction. 341 * @nib: the location for storing the nib address. 342 * 343 * When the PQAP(AQIC) instruction is executed, general register 2 contains the 344 * address of the notification indicator byte (nib) used for IRQ notification. 345 * This function parses and validates the nib from gr2. 346 * 347 * Return: returns zero if the nib address is a valid; otherwise, returns 348 * -EINVAL. 349 */ 350 static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib) 351 { 352 *nib = vcpu->run->s.regs.gprs[2]; 353 354 if (!*nib) 355 return -EINVAL; 356 if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT))) 357 return -EINVAL; 358 359 return 0; 360 } 361 362 static int ensure_nib_shared(unsigned long addr, struct gmap *gmap) 363 { 364 int ret; 365 366 /* 367 * The nib has to be located in shared storage since guest and 368 * host access it. vfio_pin_pages() will do a pin shared and 369 * if that fails (possibly because it's not a shared page) it 370 * calls export. We try to do a second pin shared here so that 371 * the UV gives us an error code if we try to pin a non-shared 372 * page. 373 * 374 * If the page is already pinned shared the UV will return a success. 375 */ 376 ret = uv_pin_shared(addr); 377 if (ret) { 378 /* vfio_pin_pages() likely exported the page so let's re-import */ 379 gmap_convert_to_secure(gmap, addr); 380 } 381 return ret; 382 } 383 384 /** 385 * vfio_ap_irq_enable - Enable Interruption for a APQN 386 * 387 * @q: the vfio_ap_queue holding AQIC parameters 388 * @isc: the guest ISC to register with the GIB interface 389 * @vcpu: the vcpu object containing the registers specifying the parameters 390 * passed to the PQAP(AQIC) instruction. 391 * 392 * Pin the NIB saved in *q 393 * Register the guest ISC to GIB interface and retrieve the 394 * host ISC to issue the host side PQAP/AQIC 395 * 396 * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the 397 * vfio_pin_pages or kvm_s390_gisc_register failed. 398 * 399 * Otherwise return the ap_queue_status returned by the ap_aqic(), 400 * all retry handling will be done by the guest. 401 * 402 * Return: &struct ap_queue_status 403 */ 404 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q, 405 int isc, 406 struct kvm_vcpu *vcpu) 407 { 408 union ap_qirq_ctrl aqic_gisa = { .value = 0 }; 409 struct ap_queue_status status = {}; 410 struct kvm_s390_gisa *gisa; 411 struct page *h_page; 412 int nisc; 413 struct kvm *kvm; 414 phys_addr_t h_nib; 415 dma_addr_t nib; 416 int ret; 417 418 /* Verify that the notification indicator byte address is valid */ 419 if (vfio_ap_validate_nib(vcpu, &nib)) { 420 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n", 421 __func__, &nib, q->apqn); 422 423 status.response_code = AP_RESPONSE_INVALID_ADDRESS; 424 return status; 425 } 426 427 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1, 428 IOMMU_READ | IOMMU_WRITE, &h_page); 429 switch (ret) { 430 case 1: 431 break; 432 default: 433 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d," 434 "nib=%pad, apqn=%#04x\n", 435 __func__, ret, &nib, q->apqn); 436 437 status.response_code = AP_RESPONSE_INVALID_ADDRESS; 438 return status; 439 } 440 441 kvm = q->matrix_mdev->kvm; 442 gisa = kvm->arch.gisa_int.origin; 443 444 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK); 445 aqic_gisa.gisc = isc; 446 447 /* NIB in non-shared storage is a rc 6 for PV guests */ 448 if (kvm_s390_pv_cpu_is_protected(vcpu) && 449 ensure_nib_shared(h_nib & PAGE_MASK, kvm->arch.gmap)) { 450 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1); 451 status.response_code = AP_RESPONSE_INVALID_ADDRESS; 452 return status; 453 } 454 455 nisc = kvm_s390_gisc_register(kvm, isc); 456 if (nisc < 0) { 457 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n", 458 __func__, nisc, isc, q->apqn); 459 460 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1); 461 status.response_code = AP_RESPONSE_INVALID_ADDRESS; 462 return status; 463 } 464 465 aqic_gisa.isc = nisc; 466 aqic_gisa.ir = 1; 467 aqic_gisa.gisa = virt_to_phys(gisa) >> 4; 468 469 status = ap_aqic(q->apqn, aqic_gisa, h_nib); 470 switch (status.response_code) { 471 case AP_RESPONSE_NORMAL: 472 /* See if we did clear older IRQ configuration */ 473 vfio_ap_free_aqic_resources(q); 474 q->saved_iova = nib; 475 q->saved_isc = isc; 476 break; 477 case AP_RESPONSE_OTHERWISE_CHANGED: 478 /* We could not modify IRQ settings: clear new configuration */ 479 ret = kvm_s390_gisc_unregister(kvm, isc); 480 if (ret) 481 VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n", 482 __func__, ret, isc, q->apqn); 483 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1); 484 break; 485 default: 486 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn, 487 status.response_code); 488 vfio_ap_irq_disable(q); 489 break; 490 } 491 492 if (status.response_code != AP_RESPONSE_NORMAL) { 493 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: " 494 "zone=%#x, ir=%#x, gisc=%#x, f=%#x," 495 "gisa=%#x, isc=%#x, apqn=%#04x\n", 496 __func__, status.response_code, 497 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc, 498 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc, 499 q->apqn); 500 } 501 502 return status; 503 } 504 505 /** 506 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array 507 * of big endian elements that can be passed by 508 * value to an s390dbf sprintf event function to 509 * format a UUID string. 510 * 511 * @guid: the object containing the little endian guid 512 * @uuid: a six-element array of long values that can be passed by value as 513 * arguments for a formatting string specifying a UUID. 514 * 515 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf 516 * event functions if the memory for the passed string is available as long as 517 * the debug feature exists. Since a mediated device can be removed at any 518 * time, it's name can not be used because %s passes the reference to the string 519 * in memory and the reference will go stale once the device is removed . 520 * 521 * The s390dbf string formatting function allows a maximum of 9 arguments for a 522 * message to be displayed in the 'sprintf' view. In order to use the bytes 523 * comprising the mediated device's UUID to display the mediated device name, 524 * they will have to be converted into an array whose elements can be passed by 525 * value to sprintf. For example: 526 * 527 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 } 528 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804 529 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 } 530 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx" 531 */ 532 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid) 533 { 534 /* 535 * The input guid is ordered in little endian, so it needs to be 536 * reordered for displaying a UUID as a string. This specifies the 537 * guid indices in proper order. 538 */ 539 uuid[0] = le32_to_cpup((__le32 *)guid); 540 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]); 541 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]); 542 uuid[3] = *((__u16 *)&guid->b[8]); 543 uuid[4] = *((__u16 *)&guid->b[10]); 544 uuid[5] = *((__u32 *)&guid->b[12]); 545 } 546 547 /** 548 * handle_pqap - PQAP instruction callback 549 * 550 * @vcpu: The vcpu on which we received the PQAP instruction 551 * 552 * Get the general register contents to initialize internal variables. 553 * REG[0]: APQN 554 * REG[1]: IR and ISC 555 * REG[2]: NIB 556 * 557 * Response.status may be set to following Response Code: 558 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available 559 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured 560 * - AP_RESPONSE_NORMAL (0) : in case of success 561 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC. 562 * We take the matrix_dev lock to ensure serialization on queues and 563 * mediated device access. 564 * 565 * Return: 0 if we could handle the request inside KVM. 566 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault. 567 */ 568 static int handle_pqap(struct kvm_vcpu *vcpu) 569 { 570 uint64_t status; 571 uint16_t apqn; 572 unsigned long uuid[6]; 573 struct vfio_ap_queue *q; 574 struct ap_queue_status qstatus = { 575 .response_code = AP_RESPONSE_Q_NOT_AVAIL, }; 576 struct ap_matrix_mdev *matrix_mdev; 577 578 apqn = vcpu->run->s.regs.gprs[0] & 0xffff; 579 580 /* If we do not use the AIV facility just go to userland */ 581 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) { 582 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n", 583 __func__, apqn, vcpu->arch.sie_block->eca); 584 585 return -EOPNOTSUPP; 586 } 587 588 mutex_lock(&matrix_dev->mdevs_lock); 589 590 if (!vcpu->kvm->arch.crypto.pqap_hook) { 591 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n", 592 __func__, apqn); 593 594 goto out_unlock; 595 } 596 597 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook, 598 struct ap_matrix_mdev, pqap_hook); 599 600 /* If the there is no guest using the mdev, there is nothing to do */ 601 if (!matrix_mdev->kvm) { 602 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid); 603 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n", 604 __func__, uuid[0], uuid[1], uuid[2], 605 uuid[3], uuid[4], uuid[5], apqn); 606 goto out_unlock; 607 } 608 609 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn); 610 if (!q) { 611 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n", 612 __func__, AP_QID_CARD(apqn), 613 AP_QID_QUEUE(apqn)); 614 goto out_unlock; 615 } 616 617 status = vcpu->run->s.regs.gprs[1]; 618 619 /* If IR bit(16) is set we enable the interrupt */ 620 if ((status >> (63 - 16)) & 0x01) 621 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu); 622 else 623 qstatus = vfio_ap_irq_disable(q); 624 625 out_unlock: 626 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus)); 627 vcpu->run->s.regs.gprs[1] >>= 32; 628 mutex_unlock(&matrix_dev->mdevs_lock); 629 return 0; 630 } 631 632 static void vfio_ap_matrix_init(struct ap_config_info *info, 633 struct ap_matrix *matrix) 634 { 635 matrix->apm_max = info->apxa ? info->na : 63; 636 matrix->aqm_max = info->apxa ? info->nd : 15; 637 matrix->adm_max = info->apxa ? info->nd : 15; 638 } 639 640 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev) 641 { 642 if (matrix_mdev->kvm) 643 kvm_arch_crypto_set_masks(matrix_mdev->kvm, 644 matrix_mdev->shadow_apcb.apm, 645 matrix_mdev->shadow_apcb.aqm, 646 matrix_mdev->shadow_apcb.adm); 647 } 648 649 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev) 650 { 651 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS); 652 653 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS); 654 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm, 655 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS); 656 657 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, 658 AP_DOMAINS); 659 } 660 661 /* 662 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev 663 * to ensure no queue devices are passed through to 664 * the guest that are not bound to the vfio_ap 665 * device driver. 666 * 667 * @matrix_mdev: the matrix mdev whose matrix is to be filtered. 668 * 669 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap 670 * driver, its APID will be filtered from the guest's APCB. The matrix 671 * structure precludes filtering an individual APQN, so its APID will be 672 * filtered. 673 * 674 * Return: a boolean value indicating whether the KVM guest's APCB was changed 675 * by the filtering or not. 676 */ 677 static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm, 678 struct ap_matrix_mdev *matrix_mdev) 679 { 680 unsigned long apid, apqi, apqn; 681 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES); 682 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS); 683 struct vfio_ap_queue *q; 684 685 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES); 686 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS); 687 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb); 688 689 /* 690 * Copy the adapters, domains and control domains to the shadow_apcb 691 * from the matrix mdev, but only those that are assigned to the host's 692 * AP configuration. 693 */ 694 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm, 695 (unsigned long *)matrix_dev->info.apm, AP_DEVICES); 696 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm, 697 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS); 698 699 for_each_set_bit_inv(apid, apm, AP_DEVICES) { 700 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) { 701 /* 702 * If the APQN is not bound to the vfio_ap device 703 * driver, then we can't assign it to the guest's 704 * AP configuration. The AP architecture won't 705 * allow filtering of a single APQN, so let's filter 706 * the APID since an adapter represents a physical 707 * hardware device. 708 */ 709 apqn = AP_MKQID(apid, apqi); 710 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn); 711 if (!q || q->reset_status.response_code) { 712 clear_bit_inv(apid, 713 matrix_mdev->shadow_apcb.apm); 714 break; 715 } 716 } 717 } 718 719 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, 720 AP_DEVICES) || 721 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, 722 AP_DOMAINS); 723 } 724 725 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev) 726 { 727 struct ap_matrix_mdev *matrix_mdev = 728 container_of(vdev, struct ap_matrix_mdev, vdev); 729 730 matrix_mdev->mdev = to_mdev_device(vdev->dev); 731 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix); 732 matrix_mdev->pqap_hook = handle_pqap; 733 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb); 734 hash_init(matrix_mdev->qtable.queues); 735 736 return 0; 737 } 738 739 static int vfio_ap_mdev_probe(struct mdev_device *mdev) 740 { 741 struct ap_matrix_mdev *matrix_mdev; 742 int ret; 743 744 matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev, 745 &vfio_ap_matrix_dev_ops); 746 if (IS_ERR(matrix_mdev)) 747 return PTR_ERR(matrix_mdev); 748 749 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev); 750 if (ret) 751 goto err_put_vdev; 752 matrix_mdev->req_trigger = NULL; 753 dev_set_drvdata(&mdev->dev, matrix_mdev); 754 mutex_lock(&matrix_dev->mdevs_lock); 755 list_add(&matrix_mdev->node, &matrix_dev->mdev_list); 756 mutex_unlock(&matrix_dev->mdevs_lock); 757 return 0; 758 759 err_put_vdev: 760 vfio_put_device(&matrix_mdev->vdev); 761 return ret; 762 } 763 764 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev, 765 struct vfio_ap_queue *q) 766 { 767 if (q) { 768 q->matrix_mdev = matrix_mdev; 769 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn); 770 } 771 } 772 773 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn) 774 { 775 struct vfio_ap_queue *q; 776 777 q = vfio_ap_find_queue(apqn); 778 vfio_ap_mdev_link_queue(matrix_mdev, q); 779 } 780 781 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q) 782 { 783 hash_del(&q->mdev_qnode); 784 } 785 786 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q) 787 { 788 q->matrix_mdev = NULL; 789 } 790 791 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev) 792 { 793 struct vfio_ap_queue *q; 794 unsigned long apid, apqi; 795 796 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) { 797 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, 798 AP_DOMAINS) { 799 q = vfio_ap_mdev_get_queue(matrix_mdev, 800 AP_MKQID(apid, apqi)); 801 if (q) 802 q->matrix_mdev = NULL; 803 } 804 } 805 } 806 807 static void vfio_ap_mdev_remove(struct mdev_device *mdev) 808 { 809 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev); 810 811 vfio_unregister_group_dev(&matrix_mdev->vdev); 812 813 mutex_lock(&matrix_dev->guests_lock); 814 mutex_lock(&matrix_dev->mdevs_lock); 815 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable); 816 vfio_ap_mdev_unlink_fr_queues(matrix_mdev); 817 list_del(&matrix_mdev->node); 818 mutex_unlock(&matrix_dev->mdevs_lock); 819 mutex_unlock(&matrix_dev->guests_lock); 820 vfio_put_device(&matrix_mdev->vdev); 821 } 822 823 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \ 824 "already assigned to %s" 825 826 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev, 827 unsigned long *apm, 828 unsigned long *aqm) 829 { 830 unsigned long apid, apqi; 831 const struct device *dev = mdev_dev(matrix_mdev->mdev); 832 const char *mdev_name = dev_name(dev); 833 834 for_each_set_bit_inv(apid, apm, AP_DEVICES) 835 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) 836 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name); 837 } 838 839 /** 840 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs 841 * 842 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified 843 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified 844 * 845 * Verifies that each APQN derived from the Cartesian product of a bitmap of 846 * AP adapter IDs and AP queue indexes is not configured for any matrix 847 * mediated device. AP queue sharing is not allowed. 848 * 849 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE. 850 */ 851 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm, 852 unsigned long *mdev_aqm) 853 { 854 struct ap_matrix_mdev *matrix_mdev; 855 DECLARE_BITMAP(apm, AP_DEVICES); 856 DECLARE_BITMAP(aqm, AP_DOMAINS); 857 858 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) { 859 /* 860 * If the input apm and aqm are fields of the matrix_mdev 861 * object, then move on to the next matrix_mdev. 862 */ 863 if (mdev_apm == matrix_mdev->matrix.apm && 864 mdev_aqm == matrix_mdev->matrix.aqm) 865 continue; 866 867 memset(apm, 0, sizeof(apm)); 868 memset(aqm, 0, sizeof(aqm)); 869 870 /* 871 * We work on full longs, as we can only exclude the leftover 872 * bits in non-inverse order. The leftover is all zeros. 873 */ 874 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm, 875 AP_DEVICES)) 876 continue; 877 878 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm, 879 AP_DOMAINS)) 880 continue; 881 882 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm); 883 884 return -EADDRINUSE; 885 } 886 887 return 0; 888 } 889 890 /** 891 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are 892 * not reserved for the default zcrypt driver and 893 * are not assigned to another mdev. 894 * 895 * @matrix_mdev: the mdev to which the APQNs being validated are assigned. 896 * 897 * Return: One of the following values: 898 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function, 899 * most likely -EBUSY indicating the ap_perms_mutex lock is already held. 900 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the 901 * zcrypt default driver. 902 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev 903 * o A zero indicating validation succeeded. 904 */ 905 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev) 906 { 907 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm, 908 matrix_mdev->matrix.aqm)) 909 return -EADDRNOTAVAIL; 910 911 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm, 912 matrix_mdev->matrix.aqm); 913 } 914 915 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev, 916 unsigned long apid) 917 { 918 unsigned long apqi; 919 920 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) 921 vfio_ap_mdev_link_apqn(matrix_mdev, 922 AP_MKQID(apid, apqi)); 923 } 924 925 /** 926 * assign_adapter_store - parses the APID from @buf and sets the 927 * corresponding bit in the mediated matrix device's APM 928 * 929 * @dev: the matrix device 930 * @attr: the mediated matrix device's assign_adapter attribute 931 * @buf: a buffer containing the AP adapter number (APID) to 932 * be assigned 933 * @count: the number of bytes in @buf 934 * 935 * Return: the number of bytes processed if the APID is valid; otherwise, 936 * returns one of the following errors: 937 * 938 * 1. -EINVAL 939 * The APID is not a valid number 940 * 941 * 2. -ENODEV 942 * The APID exceeds the maximum value configured for the system 943 * 944 * 3. -EADDRNOTAVAIL 945 * An APQN derived from the cross product of the APID being assigned 946 * and the APQIs previously assigned is not bound to the vfio_ap device 947 * driver; or, if no APQIs have yet been assigned, the APID is not 948 * contained in an APQN bound to the vfio_ap device driver. 949 * 950 * 4. -EADDRINUSE 951 * An APQN derived from the cross product of the APID being assigned 952 * and the APQIs previously assigned is being used by another mediated 953 * matrix device 954 * 955 * 5. -EAGAIN 956 * A lock required to validate the mdev's AP configuration could not 957 * be obtained. 958 */ 959 static ssize_t assign_adapter_store(struct device *dev, 960 struct device_attribute *attr, 961 const char *buf, size_t count) 962 { 963 int ret; 964 unsigned long apid; 965 DECLARE_BITMAP(apm_delta, AP_DEVICES); 966 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 967 968 mutex_lock(&ap_perms_mutex); 969 get_update_locks_for_mdev(matrix_mdev); 970 971 ret = kstrtoul(buf, 0, &apid); 972 if (ret) 973 goto done; 974 975 if (apid > matrix_mdev->matrix.apm_max) { 976 ret = -ENODEV; 977 goto done; 978 } 979 980 if (test_bit_inv(apid, matrix_mdev->matrix.apm)) { 981 ret = count; 982 goto done; 983 } 984 985 set_bit_inv(apid, matrix_mdev->matrix.apm); 986 987 ret = vfio_ap_mdev_validate_masks(matrix_mdev); 988 if (ret) { 989 clear_bit_inv(apid, matrix_mdev->matrix.apm); 990 goto done; 991 } 992 993 vfio_ap_mdev_link_adapter(matrix_mdev, apid); 994 memset(apm_delta, 0, sizeof(apm_delta)); 995 set_bit_inv(apid, apm_delta); 996 997 if (vfio_ap_mdev_filter_matrix(apm_delta, 998 matrix_mdev->matrix.aqm, matrix_mdev)) 999 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1000 1001 ret = count; 1002 done: 1003 release_update_locks_for_mdev(matrix_mdev); 1004 mutex_unlock(&ap_perms_mutex); 1005 1006 return ret; 1007 } 1008 static DEVICE_ATTR_WO(assign_adapter); 1009 1010 static struct vfio_ap_queue 1011 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev, 1012 unsigned long apid, unsigned long apqi) 1013 { 1014 struct vfio_ap_queue *q = NULL; 1015 1016 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi)); 1017 /* If the queue is assigned to the matrix mdev, unlink it. */ 1018 if (q) 1019 vfio_ap_unlink_queue_fr_mdev(q); 1020 1021 return q; 1022 } 1023 1024 /** 1025 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned 1026 * adapter from the matrix mdev to which the 1027 * adapter was assigned. 1028 * @matrix_mdev: the matrix mediated device to which the adapter was assigned. 1029 * @apid: the APID of the unassigned adapter. 1030 * @qtable: table for storing queues associated with unassigned adapter. 1031 */ 1032 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev, 1033 unsigned long apid, 1034 struct ap_queue_table *qtable) 1035 { 1036 unsigned long apqi; 1037 struct vfio_ap_queue *q; 1038 1039 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) { 1040 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi); 1041 1042 if (q && qtable) { 1043 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) && 1044 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) 1045 hash_add(qtable->queues, &q->mdev_qnode, 1046 q->apqn); 1047 } 1048 } 1049 } 1050 1051 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev, 1052 unsigned long apid) 1053 { 1054 int loop_cursor; 1055 struct vfio_ap_queue *q; 1056 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL); 1057 1058 hash_init(qtable->queues); 1059 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, qtable); 1060 1061 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) { 1062 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm); 1063 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1064 } 1065 1066 vfio_ap_mdev_reset_queues(qtable); 1067 1068 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) { 1069 vfio_ap_unlink_mdev_fr_queue(q); 1070 hash_del(&q->mdev_qnode); 1071 } 1072 1073 kfree(qtable); 1074 } 1075 1076 /** 1077 * unassign_adapter_store - parses the APID from @buf and clears the 1078 * corresponding bit in the mediated matrix device's APM 1079 * 1080 * @dev: the matrix device 1081 * @attr: the mediated matrix device's unassign_adapter attribute 1082 * @buf: a buffer containing the adapter number (APID) to be unassigned 1083 * @count: the number of bytes in @buf 1084 * 1085 * Return: the number of bytes processed if the APID is valid; otherwise, 1086 * returns one of the following errors: 1087 * -EINVAL if the APID is not a number 1088 * -ENODEV if the APID it exceeds the maximum value configured for the 1089 * system 1090 */ 1091 static ssize_t unassign_adapter_store(struct device *dev, 1092 struct device_attribute *attr, 1093 const char *buf, size_t count) 1094 { 1095 int ret; 1096 unsigned long apid; 1097 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1098 1099 get_update_locks_for_mdev(matrix_mdev); 1100 1101 ret = kstrtoul(buf, 0, &apid); 1102 if (ret) 1103 goto done; 1104 1105 if (apid > matrix_mdev->matrix.apm_max) { 1106 ret = -ENODEV; 1107 goto done; 1108 } 1109 1110 if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) { 1111 ret = count; 1112 goto done; 1113 } 1114 1115 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm); 1116 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid); 1117 ret = count; 1118 done: 1119 release_update_locks_for_mdev(matrix_mdev); 1120 return ret; 1121 } 1122 static DEVICE_ATTR_WO(unassign_adapter); 1123 1124 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev, 1125 unsigned long apqi) 1126 { 1127 unsigned long apid; 1128 1129 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) 1130 vfio_ap_mdev_link_apqn(matrix_mdev, 1131 AP_MKQID(apid, apqi)); 1132 } 1133 1134 /** 1135 * assign_domain_store - parses the APQI from @buf and sets the 1136 * corresponding bit in the mediated matrix device's AQM 1137 * 1138 * @dev: the matrix device 1139 * @attr: the mediated matrix device's assign_domain attribute 1140 * @buf: a buffer containing the AP queue index (APQI) of the domain to 1141 * be assigned 1142 * @count: the number of bytes in @buf 1143 * 1144 * Return: the number of bytes processed if the APQI is valid; otherwise returns 1145 * one of the following errors: 1146 * 1147 * 1. -EINVAL 1148 * The APQI is not a valid number 1149 * 1150 * 2. -ENODEV 1151 * The APQI exceeds the maximum value configured for the system 1152 * 1153 * 3. -EADDRNOTAVAIL 1154 * An APQN derived from the cross product of the APQI being assigned 1155 * and the APIDs previously assigned is not bound to the vfio_ap device 1156 * driver; or, if no APIDs have yet been assigned, the APQI is not 1157 * contained in an APQN bound to the vfio_ap device driver. 1158 * 1159 * 4. -EADDRINUSE 1160 * An APQN derived from the cross product of the APQI being assigned 1161 * and the APIDs previously assigned is being used by another mediated 1162 * matrix device 1163 * 1164 * 5. -EAGAIN 1165 * The lock required to validate the mdev's AP configuration could not 1166 * be obtained. 1167 */ 1168 static ssize_t assign_domain_store(struct device *dev, 1169 struct device_attribute *attr, 1170 const char *buf, size_t count) 1171 { 1172 int ret; 1173 unsigned long apqi; 1174 DECLARE_BITMAP(aqm_delta, AP_DOMAINS); 1175 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1176 1177 mutex_lock(&ap_perms_mutex); 1178 get_update_locks_for_mdev(matrix_mdev); 1179 1180 ret = kstrtoul(buf, 0, &apqi); 1181 if (ret) 1182 goto done; 1183 1184 if (apqi > matrix_mdev->matrix.aqm_max) { 1185 ret = -ENODEV; 1186 goto done; 1187 } 1188 1189 if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) { 1190 ret = count; 1191 goto done; 1192 } 1193 1194 set_bit_inv(apqi, matrix_mdev->matrix.aqm); 1195 1196 ret = vfio_ap_mdev_validate_masks(matrix_mdev); 1197 if (ret) { 1198 clear_bit_inv(apqi, matrix_mdev->matrix.aqm); 1199 goto done; 1200 } 1201 1202 vfio_ap_mdev_link_domain(matrix_mdev, apqi); 1203 memset(aqm_delta, 0, sizeof(aqm_delta)); 1204 set_bit_inv(apqi, aqm_delta); 1205 1206 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, aqm_delta, 1207 matrix_mdev)) 1208 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1209 1210 ret = count; 1211 done: 1212 release_update_locks_for_mdev(matrix_mdev); 1213 mutex_unlock(&ap_perms_mutex); 1214 1215 return ret; 1216 } 1217 static DEVICE_ATTR_WO(assign_domain); 1218 1219 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev, 1220 unsigned long apqi, 1221 struct ap_queue_table *qtable) 1222 { 1223 unsigned long apid; 1224 struct vfio_ap_queue *q; 1225 1226 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) { 1227 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi); 1228 1229 if (q && qtable) { 1230 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) && 1231 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) 1232 hash_add(qtable->queues, &q->mdev_qnode, 1233 q->apqn); 1234 } 1235 } 1236 } 1237 1238 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev, 1239 unsigned long apqi) 1240 { 1241 int loop_cursor; 1242 struct vfio_ap_queue *q; 1243 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL); 1244 1245 hash_init(qtable->queues); 1246 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, qtable); 1247 1248 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) { 1249 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm); 1250 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1251 } 1252 1253 vfio_ap_mdev_reset_queues(qtable); 1254 1255 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) { 1256 vfio_ap_unlink_mdev_fr_queue(q); 1257 hash_del(&q->mdev_qnode); 1258 } 1259 1260 kfree(qtable); 1261 } 1262 1263 /** 1264 * unassign_domain_store - parses the APQI from @buf and clears the 1265 * corresponding bit in the mediated matrix device's AQM 1266 * 1267 * @dev: the matrix device 1268 * @attr: the mediated matrix device's unassign_domain attribute 1269 * @buf: a buffer containing the AP queue index (APQI) of the domain to 1270 * be unassigned 1271 * @count: the number of bytes in @buf 1272 * 1273 * Return: the number of bytes processed if the APQI is valid; otherwise, 1274 * returns one of the following errors: 1275 * -EINVAL if the APQI is not a number 1276 * -ENODEV if the APQI exceeds the maximum value configured for the system 1277 */ 1278 static ssize_t unassign_domain_store(struct device *dev, 1279 struct device_attribute *attr, 1280 const char *buf, size_t count) 1281 { 1282 int ret; 1283 unsigned long apqi; 1284 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1285 1286 get_update_locks_for_mdev(matrix_mdev); 1287 1288 ret = kstrtoul(buf, 0, &apqi); 1289 if (ret) 1290 goto done; 1291 1292 if (apqi > matrix_mdev->matrix.aqm_max) { 1293 ret = -ENODEV; 1294 goto done; 1295 } 1296 1297 if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) { 1298 ret = count; 1299 goto done; 1300 } 1301 1302 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm); 1303 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi); 1304 ret = count; 1305 1306 done: 1307 release_update_locks_for_mdev(matrix_mdev); 1308 return ret; 1309 } 1310 static DEVICE_ATTR_WO(unassign_domain); 1311 1312 /** 1313 * assign_control_domain_store - parses the domain ID from @buf and sets 1314 * the corresponding bit in the mediated matrix device's ADM 1315 * 1316 * @dev: the matrix device 1317 * @attr: the mediated matrix device's assign_control_domain attribute 1318 * @buf: a buffer containing the domain ID to be assigned 1319 * @count: the number of bytes in @buf 1320 * 1321 * Return: the number of bytes processed if the domain ID is valid; otherwise, 1322 * returns one of the following errors: 1323 * -EINVAL if the ID is not a number 1324 * -ENODEV if the ID exceeds the maximum value configured for the system 1325 */ 1326 static ssize_t assign_control_domain_store(struct device *dev, 1327 struct device_attribute *attr, 1328 const char *buf, size_t count) 1329 { 1330 int ret; 1331 unsigned long id; 1332 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1333 1334 get_update_locks_for_mdev(matrix_mdev); 1335 1336 ret = kstrtoul(buf, 0, &id); 1337 if (ret) 1338 goto done; 1339 1340 if (id > matrix_mdev->matrix.adm_max) { 1341 ret = -ENODEV; 1342 goto done; 1343 } 1344 1345 if (test_bit_inv(id, matrix_mdev->matrix.adm)) { 1346 ret = count; 1347 goto done; 1348 } 1349 1350 /* Set the bit in the ADM (bitmask) corresponding to the AP control 1351 * domain number (id). The bits in the mask, from most significant to 1352 * least significant, correspond to IDs 0 up to the one less than the 1353 * number of control domains that can be assigned. 1354 */ 1355 set_bit_inv(id, matrix_mdev->matrix.adm); 1356 if (vfio_ap_mdev_filter_cdoms(matrix_mdev)) 1357 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1358 1359 ret = count; 1360 done: 1361 release_update_locks_for_mdev(matrix_mdev); 1362 return ret; 1363 } 1364 static DEVICE_ATTR_WO(assign_control_domain); 1365 1366 /** 1367 * unassign_control_domain_store - parses the domain ID from @buf and 1368 * clears the corresponding bit in the mediated matrix device's ADM 1369 * 1370 * @dev: the matrix device 1371 * @attr: the mediated matrix device's unassign_control_domain attribute 1372 * @buf: a buffer containing the domain ID to be unassigned 1373 * @count: the number of bytes in @buf 1374 * 1375 * Return: the number of bytes processed if the domain ID is valid; otherwise, 1376 * returns one of the following errors: 1377 * -EINVAL if the ID is not a number 1378 * -ENODEV if the ID exceeds the maximum value configured for the system 1379 */ 1380 static ssize_t unassign_control_domain_store(struct device *dev, 1381 struct device_attribute *attr, 1382 const char *buf, size_t count) 1383 { 1384 int ret; 1385 unsigned long domid; 1386 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1387 1388 get_update_locks_for_mdev(matrix_mdev); 1389 1390 ret = kstrtoul(buf, 0, &domid); 1391 if (ret) 1392 goto done; 1393 1394 if (domid > matrix_mdev->matrix.adm_max) { 1395 ret = -ENODEV; 1396 goto done; 1397 } 1398 1399 if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) { 1400 ret = count; 1401 goto done; 1402 } 1403 1404 clear_bit_inv(domid, matrix_mdev->matrix.adm); 1405 1406 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) { 1407 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm); 1408 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1409 } 1410 1411 ret = count; 1412 done: 1413 release_update_locks_for_mdev(matrix_mdev); 1414 return ret; 1415 } 1416 static DEVICE_ATTR_WO(unassign_control_domain); 1417 1418 static ssize_t control_domains_show(struct device *dev, 1419 struct device_attribute *dev_attr, 1420 char *buf) 1421 { 1422 unsigned long id; 1423 int nchars = 0; 1424 int n; 1425 char *bufpos = buf; 1426 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1427 unsigned long max_domid = matrix_mdev->matrix.adm_max; 1428 1429 mutex_lock(&matrix_dev->mdevs_lock); 1430 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) { 1431 n = sprintf(bufpos, "%04lx\n", id); 1432 bufpos += n; 1433 nchars += n; 1434 } 1435 mutex_unlock(&matrix_dev->mdevs_lock); 1436 1437 return nchars; 1438 } 1439 static DEVICE_ATTR_RO(control_domains); 1440 1441 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf) 1442 { 1443 char *bufpos = buf; 1444 unsigned long apid; 1445 unsigned long apqi; 1446 unsigned long apid1; 1447 unsigned long apqi1; 1448 unsigned long napm_bits = matrix->apm_max + 1; 1449 unsigned long naqm_bits = matrix->aqm_max + 1; 1450 int nchars = 0; 1451 int n; 1452 1453 apid1 = find_first_bit_inv(matrix->apm, napm_bits); 1454 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits); 1455 1456 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) { 1457 for_each_set_bit_inv(apid, matrix->apm, napm_bits) { 1458 for_each_set_bit_inv(apqi, matrix->aqm, 1459 naqm_bits) { 1460 n = sprintf(bufpos, "%02lx.%04lx\n", apid, 1461 apqi); 1462 bufpos += n; 1463 nchars += n; 1464 } 1465 } 1466 } else if (apid1 < napm_bits) { 1467 for_each_set_bit_inv(apid, matrix->apm, napm_bits) { 1468 n = sprintf(bufpos, "%02lx.\n", apid); 1469 bufpos += n; 1470 nchars += n; 1471 } 1472 } else if (apqi1 < naqm_bits) { 1473 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) { 1474 n = sprintf(bufpos, ".%04lx\n", apqi); 1475 bufpos += n; 1476 nchars += n; 1477 } 1478 } 1479 1480 return nchars; 1481 } 1482 1483 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr, 1484 char *buf) 1485 { 1486 ssize_t nchars; 1487 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1488 1489 mutex_lock(&matrix_dev->mdevs_lock); 1490 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf); 1491 mutex_unlock(&matrix_dev->mdevs_lock); 1492 1493 return nchars; 1494 } 1495 static DEVICE_ATTR_RO(matrix); 1496 1497 static ssize_t guest_matrix_show(struct device *dev, 1498 struct device_attribute *attr, char *buf) 1499 { 1500 ssize_t nchars; 1501 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); 1502 1503 mutex_lock(&matrix_dev->mdevs_lock); 1504 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf); 1505 mutex_unlock(&matrix_dev->mdevs_lock); 1506 1507 return nchars; 1508 } 1509 static DEVICE_ATTR_RO(guest_matrix); 1510 1511 static struct attribute *vfio_ap_mdev_attrs[] = { 1512 &dev_attr_assign_adapter.attr, 1513 &dev_attr_unassign_adapter.attr, 1514 &dev_attr_assign_domain.attr, 1515 &dev_attr_unassign_domain.attr, 1516 &dev_attr_assign_control_domain.attr, 1517 &dev_attr_unassign_control_domain.attr, 1518 &dev_attr_control_domains.attr, 1519 &dev_attr_matrix.attr, 1520 &dev_attr_guest_matrix.attr, 1521 NULL, 1522 }; 1523 1524 static struct attribute_group vfio_ap_mdev_attr_group = { 1525 .attrs = vfio_ap_mdev_attrs 1526 }; 1527 1528 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = { 1529 &vfio_ap_mdev_attr_group, 1530 NULL 1531 }; 1532 1533 /** 1534 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed 1535 * to manage AP resources for the guest whose state is represented by @kvm 1536 * 1537 * @matrix_mdev: a mediated matrix device 1538 * @kvm: reference to KVM instance 1539 * 1540 * Return: 0 if no other mediated matrix device has a reference to @kvm; 1541 * otherwise, returns an -EPERM. 1542 */ 1543 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev, 1544 struct kvm *kvm) 1545 { 1546 struct ap_matrix_mdev *m; 1547 1548 if (kvm->arch.crypto.crycbd) { 1549 down_write(&kvm->arch.crypto.pqap_hook_rwsem); 1550 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook; 1551 up_write(&kvm->arch.crypto.pqap_hook_rwsem); 1552 1553 get_update_locks_for_kvm(kvm); 1554 1555 list_for_each_entry(m, &matrix_dev->mdev_list, node) { 1556 if (m != matrix_mdev && m->kvm == kvm) { 1557 release_update_locks_for_kvm(kvm); 1558 return -EPERM; 1559 } 1560 } 1561 1562 kvm_get_kvm(kvm); 1563 matrix_mdev->kvm = kvm; 1564 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 1565 1566 release_update_locks_for_kvm(kvm); 1567 } 1568 1569 return 0; 1570 } 1571 1572 static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length) 1573 { 1574 struct ap_queue_table *qtable = &matrix_mdev->qtable; 1575 struct vfio_ap_queue *q; 1576 int loop_cursor; 1577 1578 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) { 1579 if (q->saved_iova >= iova && q->saved_iova < iova + length) 1580 vfio_ap_irq_disable(q); 1581 } 1582 } 1583 1584 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova, 1585 u64 length) 1586 { 1587 struct ap_matrix_mdev *matrix_mdev = 1588 container_of(vdev, struct ap_matrix_mdev, vdev); 1589 1590 mutex_lock(&matrix_dev->mdevs_lock); 1591 1592 unmap_iova(matrix_mdev, iova, length); 1593 1594 mutex_unlock(&matrix_dev->mdevs_lock); 1595 } 1596 1597 /** 1598 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed 1599 * by @matrix_mdev. 1600 * 1601 * @matrix_mdev: a matrix mediated device 1602 */ 1603 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev) 1604 { 1605 struct kvm *kvm = matrix_mdev->kvm; 1606 1607 if (kvm && kvm->arch.crypto.crycbd) { 1608 down_write(&kvm->arch.crypto.pqap_hook_rwsem); 1609 kvm->arch.crypto.pqap_hook = NULL; 1610 up_write(&kvm->arch.crypto.pqap_hook_rwsem); 1611 1612 get_update_locks_for_kvm(kvm); 1613 1614 kvm_arch_crypto_clear_masks(kvm); 1615 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable); 1616 kvm_put_kvm(kvm); 1617 matrix_mdev->kvm = NULL; 1618 1619 release_update_locks_for_kvm(kvm); 1620 } 1621 } 1622 1623 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn) 1624 { 1625 struct ap_queue *queue; 1626 struct vfio_ap_queue *q = NULL; 1627 1628 queue = ap_get_qdev(apqn); 1629 if (!queue) 1630 return NULL; 1631 1632 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver) 1633 q = dev_get_drvdata(&queue->ap_dev.device); 1634 1635 put_device(&queue->ap_dev.device); 1636 1637 return q; 1638 } 1639 1640 static int apq_status_check(int apqn, struct ap_queue_status *status) 1641 { 1642 switch (status->response_code) { 1643 case AP_RESPONSE_NORMAL: 1644 case AP_RESPONSE_DECONFIGURED: 1645 return 0; 1646 case AP_RESPONSE_RESET_IN_PROGRESS: 1647 case AP_RESPONSE_BUSY: 1648 return -EBUSY; 1649 case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE: 1650 case AP_RESPONSE_ASSOC_FAILED: 1651 /* 1652 * These asynchronous response codes indicate a PQAP(AAPQ) 1653 * instruction to associate a secret with the guest failed. All 1654 * subsequent AP instructions will end with the asynchronous 1655 * response code until the AP queue is reset; so, let's return 1656 * a value indicating a reset needs to be performed again. 1657 */ 1658 return -EAGAIN; 1659 default: 1660 WARN(true, 1661 "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n", 1662 AP_QID_CARD(apqn), AP_QID_QUEUE(apqn), 1663 status->response_code); 1664 return -EIO; 1665 } 1666 } 1667 1668 #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)" 1669 1670 static void apq_reset_check(struct work_struct *reset_work) 1671 { 1672 int ret = -EBUSY, elapsed = 0; 1673 struct ap_queue_status status; 1674 struct vfio_ap_queue *q; 1675 1676 q = container_of(reset_work, struct vfio_ap_queue, reset_work); 1677 memcpy(&status, &q->reset_status, sizeof(status)); 1678 while (true) { 1679 msleep(AP_RESET_INTERVAL); 1680 elapsed += AP_RESET_INTERVAL; 1681 status = ap_tapq(q->apqn, NULL); 1682 ret = apq_status_check(q->apqn, &status); 1683 if (ret == -EIO) 1684 return; 1685 if (ret == -EBUSY) { 1686 pr_notice_ratelimited(WAIT_MSG, elapsed, 1687 AP_QID_CARD(q->apqn), 1688 AP_QID_QUEUE(q->apqn), 1689 status.response_code, 1690 status.queue_empty, 1691 status.irq_enabled); 1692 } else { 1693 if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS || 1694 q->reset_status.response_code == AP_RESPONSE_BUSY || 1695 q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS || 1696 ret == -EAGAIN) { 1697 status = ap_zapq(q->apqn, 0); 1698 memcpy(&q->reset_status, &status, sizeof(status)); 1699 continue; 1700 } 1701 /* 1702 * When an AP adapter is deconfigured, the 1703 * associated queues are reset, so let's set the 1704 * status response code to 0 so the queue may be 1705 * passed through (i.e., not filtered) 1706 */ 1707 if (status.response_code == AP_RESPONSE_DECONFIGURED) 1708 q->reset_status.response_code = 0; 1709 if (q->saved_isc != VFIO_AP_ISC_INVALID) 1710 vfio_ap_free_aqic_resources(q); 1711 break; 1712 } 1713 } 1714 } 1715 1716 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q) 1717 { 1718 struct ap_queue_status status; 1719 1720 if (!q) 1721 return; 1722 status = ap_zapq(q->apqn, 0); 1723 memcpy(&q->reset_status, &status, sizeof(status)); 1724 switch (status.response_code) { 1725 case AP_RESPONSE_NORMAL: 1726 case AP_RESPONSE_RESET_IN_PROGRESS: 1727 case AP_RESPONSE_BUSY: 1728 case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS: 1729 /* 1730 * Let's verify whether the ZAPQ completed successfully on a work queue. 1731 */ 1732 queue_work(system_long_wq, &q->reset_work); 1733 break; 1734 case AP_RESPONSE_DECONFIGURED: 1735 /* 1736 * When an AP adapter is deconfigured, the associated 1737 * queues are reset, so let's set the status response code to 0 1738 * so the queue may be passed through (i.e., not filtered). 1739 */ 1740 q->reset_status.response_code = 0; 1741 vfio_ap_free_aqic_resources(q); 1742 break; 1743 default: 1744 WARN(true, 1745 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n", 1746 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn), 1747 status.response_code); 1748 } 1749 } 1750 1751 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable) 1752 { 1753 int ret = 0, loop_cursor; 1754 struct vfio_ap_queue *q; 1755 1756 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) 1757 vfio_ap_mdev_reset_queue(q); 1758 1759 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) { 1760 flush_work(&q->reset_work); 1761 1762 if (q->reset_status.response_code) 1763 ret = -EIO; 1764 } 1765 1766 return ret; 1767 } 1768 1769 static int vfio_ap_mdev_open_device(struct vfio_device *vdev) 1770 { 1771 struct ap_matrix_mdev *matrix_mdev = 1772 container_of(vdev, struct ap_matrix_mdev, vdev); 1773 1774 if (!vdev->kvm) 1775 return -EINVAL; 1776 1777 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm); 1778 } 1779 1780 static void vfio_ap_mdev_close_device(struct vfio_device *vdev) 1781 { 1782 struct ap_matrix_mdev *matrix_mdev = 1783 container_of(vdev, struct ap_matrix_mdev, vdev); 1784 1785 vfio_ap_mdev_unset_kvm(matrix_mdev); 1786 } 1787 1788 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count) 1789 { 1790 struct device *dev = vdev->dev; 1791 struct ap_matrix_mdev *matrix_mdev; 1792 1793 matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev); 1794 1795 if (matrix_mdev->req_trigger) { 1796 if (!(count % 10)) 1797 dev_notice_ratelimited(dev, 1798 "Relaying device request to user (#%u)\n", 1799 count); 1800 1801 eventfd_signal(matrix_mdev->req_trigger); 1802 } else if (count == 0) { 1803 dev_notice(dev, 1804 "No device request registered, blocked until released by user\n"); 1805 } 1806 } 1807 1808 static int vfio_ap_mdev_get_device_info(unsigned long arg) 1809 { 1810 unsigned long minsz; 1811 struct vfio_device_info info; 1812 1813 minsz = offsetofend(struct vfio_device_info, num_irqs); 1814 1815 if (copy_from_user(&info, (void __user *)arg, minsz)) 1816 return -EFAULT; 1817 1818 if (info.argsz < minsz) 1819 return -EINVAL; 1820 1821 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET; 1822 info.num_regions = 0; 1823 info.num_irqs = VFIO_AP_NUM_IRQS; 1824 1825 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0; 1826 } 1827 1828 static ssize_t vfio_ap_get_irq_info(unsigned long arg) 1829 { 1830 unsigned long minsz; 1831 struct vfio_irq_info info; 1832 1833 minsz = offsetofend(struct vfio_irq_info, count); 1834 1835 if (copy_from_user(&info, (void __user *)arg, minsz)) 1836 return -EFAULT; 1837 1838 if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS) 1839 return -EINVAL; 1840 1841 switch (info.index) { 1842 case VFIO_AP_REQ_IRQ_INDEX: 1843 info.count = 1; 1844 info.flags = VFIO_IRQ_INFO_EVENTFD; 1845 break; 1846 default: 1847 return -EINVAL; 1848 } 1849 1850 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0; 1851 } 1852 1853 static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg) 1854 { 1855 int ret; 1856 size_t data_size; 1857 unsigned long minsz; 1858 1859 minsz = offsetofend(struct vfio_irq_set, count); 1860 1861 if (copy_from_user(irq_set, (void __user *)arg, minsz)) 1862 return -EFAULT; 1863 1864 ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS, 1865 &data_size); 1866 if (ret) 1867 return ret; 1868 1869 if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER)) 1870 return -EINVAL; 1871 1872 return 0; 1873 } 1874 1875 static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev, 1876 unsigned long arg) 1877 { 1878 s32 fd; 1879 void __user *data; 1880 unsigned long minsz; 1881 struct eventfd_ctx *req_trigger; 1882 1883 minsz = offsetofend(struct vfio_irq_set, count); 1884 data = (void __user *)(arg + minsz); 1885 1886 if (get_user(fd, (s32 __user *)data)) 1887 return -EFAULT; 1888 1889 if (fd == -1) { 1890 if (matrix_mdev->req_trigger) 1891 eventfd_ctx_put(matrix_mdev->req_trigger); 1892 matrix_mdev->req_trigger = NULL; 1893 } else if (fd >= 0) { 1894 req_trigger = eventfd_ctx_fdget(fd); 1895 if (IS_ERR(req_trigger)) 1896 return PTR_ERR(req_trigger); 1897 1898 if (matrix_mdev->req_trigger) 1899 eventfd_ctx_put(matrix_mdev->req_trigger); 1900 1901 matrix_mdev->req_trigger = req_trigger; 1902 } else { 1903 return -EINVAL; 1904 } 1905 1906 return 0; 1907 } 1908 1909 static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev, 1910 unsigned long arg) 1911 { 1912 int ret; 1913 struct vfio_irq_set irq_set; 1914 1915 ret = vfio_ap_irq_set_init(&irq_set, arg); 1916 if (ret) 1917 return ret; 1918 1919 switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) { 1920 case VFIO_IRQ_SET_DATA_EVENTFD: 1921 switch (irq_set.index) { 1922 case VFIO_AP_REQ_IRQ_INDEX: 1923 return vfio_ap_set_request_irq(matrix_mdev, arg); 1924 default: 1925 return -EINVAL; 1926 } 1927 default: 1928 return -EINVAL; 1929 } 1930 } 1931 1932 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev, 1933 unsigned int cmd, unsigned long arg) 1934 { 1935 struct ap_matrix_mdev *matrix_mdev = 1936 container_of(vdev, struct ap_matrix_mdev, vdev); 1937 int ret; 1938 1939 mutex_lock(&matrix_dev->mdevs_lock); 1940 switch (cmd) { 1941 case VFIO_DEVICE_GET_INFO: 1942 ret = vfio_ap_mdev_get_device_info(arg); 1943 break; 1944 case VFIO_DEVICE_RESET: 1945 ret = vfio_ap_mdev_reset_queues(&matrix_mdev->qtable); 1946 break; 1947 case VFIO_DEVICE_GET_IRQ_INFO: 1948 ret = vfio_ap_get_irq_info(arg); 1949 break; 1950 case VFIO_DEVICE_SET_IRQS: 1951 ret = vfio_ap_set_irqs(matrix_mdev, arg); 1952 break; 1953 default: 1954 ret = -EOPNOTSUPP; 1955 break; 1956 } 1957 mutex_unlock(&matrix_dev->mdevs_lock); 1958 1959 return ret; 1960 } 1961 1962 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q) 1963 { 1964 struct ap_matrix_mdev *matrix_mdev; 1965 unsigned long apid = AP_QID_CARD(q->apqn); 1966 unsigned long apqi = AP_QID_QUEUE(q->apqn); 1967 1968 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) { 1969 if (test_bit_inv(apid, matrix_mdev->matrix.apm) && 1970 test_bit_inv(apqi, matrix_mdev->matrix.aqm)) 1971 return matrix_mdev; 1972 } 1973 1974 return NULL; 1975 } 1976 1977 static ssize_t status_show(struct device *dev, 1978 struct device_attribute *attr, 1979 char *buf) 1980 { 1981 ssize_t nchars = 0; 1982 struct vfio_ap_queue *q; 1983 unsigned long apid, apqi; 1984 struct ap_matrix_mdev *matrix_mdev; 1985 struct ap_device *apdev = to_ap_dev(dev); 1986 1987 mutex_lock(&matrix_dev->mdevs_lock); 1988 q = dev_get_drvdata(&apdev->device); 1989 matrix_mdev = vfio_ap_mdev_for_queue(q); 1990 1991 /* If the queue is assigned to the matrix mediated device, then 1992 * determine whether it is passed through to a guest; otherwise, 1993 * indicate that it is unassigned. 1994 */ 1995 if (matrix_mdev) { 1996 apid = AP_QID_CARD(q->apqn); 1997 apqi = AP_QID_QUEUE(q->apqn); 1998 /* 1999 * If the queue is passed through to the guest, then indicate 2000 * that it is in use; otherwise, indicate that it is 2001 * merely assigned to a matrix mediated device. 2002 */ 2003 if (matrix_mdev->kvm && 2004 test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) && 2005 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) 2006 nchars = scnprintf(buf, PAGE_SIZE, "%s\n", 2007 AP_QUEUE_IN_USE); 2008 else 2009 nchars = scnprintf(buf, PAGE_SIZE, "%s\n", 2010 AP_QUEUE_ASSIGNED); 2011 } else { 2012 nchars = scnprintf(buf, PAGE_SIZE, "%s\n", 2013 AP_QUEUE_UNASSIGNED); 2014 } 2015 2016 mutex_unlock(&matrix_dev->mdevs_lock); 2017 2018 return nchars; 2019 } 2020 2021 static DEVICE_ATTR_RO(status); 2022 2023 static struct attribute *vfio_queue_attrs[] = { 2024 &dev_attr_status.attr, 2025 NULL, 2026 }; 2027 2028 static const struct attribute_group vfio_queue_attr_group = { 2029 .attrs = vfio_queue_attrs, 2030 }; 2031 2032 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = { 2033 .init = vfio_ap_mdev_init_dev, 2034 .open_device = vfio_ap_mdev_open_device, 2035 .close_device = vfio_ap_mdev_close_device, 2036 .ioctl = vfio_ap_mdev_ioctl, 2037 .dma_unmap = vfio_ap_mdev_dma_unmap, 2038 .bind_iommufd = vfio_iommufd_emulated_bind, 2039 .unbind_iommufd = vfio_iommufd_emulated_unbind, 2040 .attach_ioas = vfio_iommufd_emulated_attach_ioas, 2041 .detach_ioas = vfio_iommufd_emulated_detach_ioas, 2042 .request = vfio_ap_mdev_request 2043 }; 2044 2045 static struct mdev_driver vfio_ap_matrix_driver = { 2046 .device_api = VFIO_DEVICE_API_AP_STRING, 2047 .max_instances = MAX_ZDEV_ENTRIES_EXT, 2048 .driver = { 2049 .name = "vfio_ap_mdev", 2050 .owner = THIS_MODULE, 2051 .mod_name = KBUILD_MODNAME, 2052 .dev_groups = vfio_ap_mdev_attr_groups, 2053 }, 2054 .probe = vfio_ap_mdev_probe, 2055 .remove = vfio_ap_mdev_remove, 2056 }; 2057 2058 int vfio_ap_mdev_register(void) 2059 { 2060 int ret; 2061 2062 ret = mdev_register_driver(&vfio_ap_matrix_driver); 2063 if (ret) 2064 return ret; 2065 2066 matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT; 2067 matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT; 2068 matrix_dev->mdev_types[0] = &matrix_dev->mdev_type; 2069 ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device, 2070 &vfio_ap_matrix_driver, 2071 matrix_dev->mdev_types, 1); 2072 if (ret) 2073 goto err_driver; 2074 return 0; 2075 2076 err_driver: 2077 mdev_unregister_driver(&vfio_ap_matrix_driver); 2078 return ret; 2079 } 2080 2081 void vfio_ap_mdev_unregister(void) 2082 { 2083 mdev_unregister_parent(&matrix_dev->parent); 2084 mdev_unregister_driver(&vfio_ap_matrix_driver); 2085 } 2086 2087 int vfio_ap_mdev_probe_queue(struct ap_device *apdev) 2088 { 2089 int ret; 2090 struct vfio_ap_queue *q; 2091 struct ap_matrix_mdev *matrix_mdev; 2092 2093 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group); 2094 if (ret) 2095 return ret; 2096 2097 q = kzalloc(sizeof(*q), GFP_KERNEL); 2098 if (!q) { 2099 ret = -ENOMEM; 2100 goto err_remove_group; 2101 } 2102 2103 q->apqn = to_ap_queue(&apdev->device)->qid; 2104 q->saved_isc = VFIO_AP_ISC_INVALID; 2105 memset(&q->reset_status, 0, sizeof(q->reset_status)); 2106 INIT_WORK(&q->reset_work, apq_reset_check); 2107 matrix_mdev = get_update_locks_by_apqn(q->apqn); 2108 2109 if (matrix_mdev) { 2110 vfio_ap_mdev_link_queue(matrix_mdev, q); 2111 2112 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, 2113 matrix_mdev->matrix.aqm, 2114 matrix_mdev)) 2115 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 2116 } 2117 dev_set_drvdata(&apdev->device, q); 2118 release_update_locks_for_mdev(matrix_mdev); 2119 2120 return 0; 2121 2122 err_remove_group: 2123 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group); 2124 return ret; 2125 } 2126 2127 void vfio_ap_mdev_remove_queue(struct ap_device *apdev) 2128 { 2129 unsigned long apid, apqi; 2130 struct vfio_ap_queue *q; 2131 struct ap_matrix_mdev *matrix_mdev; 2132 2133 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group); 2134 q = dev_get_drvdata(&apdev->device); 2135 get_update_locks_for_queue(q); 2136 matrix_mdev = q->matrix_mdev; 2137 2138 if (matrix_mdev) { 2139 vfio_ap_unlink_queue_fr_mdev(q); 2140 2141 apid = AP_QID_CARD(q->apqn); 2142 apqi = AP_QID_QUEUE(q->apqn); 2143 2144 /* 2145 * If the queue is assigned to the guest's APCB, then remove 2146 * the adapter's APID from the APCB and hot it into the guest. 2147 */ 2148 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) && 2149 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) { 2150 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm); 2151 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 2152 } 2153 } 2154 2155 vfio_ap_mdev_reset_queue(q); 2156 flush_work(&q->reset_work); 2157 dev_set_drvdata(&apdev->device, NULL); 2158 kfree(q); 2159 release_update_locks_for_mdev(matrix_mdev); 2160 } 2161 2162 /** 2163 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is 2164 * assigned to a mediated device under the control 2165 * of the vfio_ap device driver. 2166 * 2167 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check. 2168 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check. 2169 * 2170 * Return: 2171 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are 2172 * assigned to a mediated device under the control of the vfio_ap 2173 * device driver. 2174 * * Otherwise, return 0. 2175 */ 2176 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm) 2177 { 2178 int ret; 2179 2180 mutex_lock(&matrix_dev->guests_lock); 2181 mutex_lock(&matrix_dev->mdevs_lock); 2182 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm); 2183 mutex_unlock(&matrix_dev->mdevs_lock); 2184 mutex_unlock(&matrix_dev->guests_lock); 2185 2186 return ret; 2187 } 2188 2189 /** 2190 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control 2191 * domains that have been removed from the host's 2192 * AP configuration from a guest. 2193 * 2194 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest. 2195 * @aprem: the adapters that have been removed from the host's AP configuration 2196 * @aqrem: the domains that have been removed from the host's AP configuration 2197 * @cdrem: the control domains that have been removed from the host's AP 2198 * configuration. 2199 */ 2200 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev, 2201 unsigned long *aprem, 2202 unsigned long *aqrem, 2203 unsigned long *cdrem) 2204 { 2205 int do_hotplug = 0; 2206 2207 if (!bitmap_empty(aprem, AP_DEVICES)) { 2208 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm, 2209 matrix_mdev->shadow_apcb.apm, 2210 aprem, AP_DEVICES); 2211 } 2212 2213 if (!bitmap_empty(aqrem, AP_DOMAINS)) { 2214 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm, 2215 matrix_mdev->shadow_apcb.aqm, 2216 aqrem, AP_DEVICES); 2217 } 2218 2219 if (!bitmap_empty(cdrem, AP_DOMAINS)) 2220 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm, 2221 matrix_mdev->shadow_apcb.adm, 2222 cdrem, AP_DOMAINS); 2223 2224 if (do_hotplug) 2225 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 2226 } 2227 2228 /** 2229 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters, 2230 * domains and control domains that have been removed 2231 * from the host AP configuration and unplugs them 2232 * from those guests. 2233 * 2234 * @ap_remove: bitmap specifying which adapters have been removed from the host 2235 * config. 2236 * @aq_remove: bitmap specifying which domains have been removed from the host 2237 * config. 2238 * @cd_remove: bitmap specifying which control domains have been removed from 2239 * the host config. 2240 */ 2241 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove, 2242 unsigned long *aq_remove, 2243 unsigned long *cd_remove) 2244 { 2245 struct ap_matrix_mdev *matrix_mdev; 2246 DECLARE_BITMAP(aprem, AP_DEVICES); 2247 DECLARE_BITMAP(aqrem, AP_DOMAINS); 2248 DECLARE_BITMAP(cdrem, AP_DOMAINS); 2249 int do_remove = 0; 2250 2251 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) { 2252 mutex_lock(&matrix_mdev->kvm->lock); 2253 mutex_lock(&matrix_dev->mdevs_lock); 2254 2255 do_remove |= bitmap_and(aprem, ap_remove, 2256 matrix_mdev->matrix.apm, 2257 AP_DEVICES); 2258 do_remove |= bitmap_and(aqrem, aq_remove, 2259 matrix_mdev->matrix.aqm, 2260 AP_DOMAINS); 2261 do_remove |= bitmap_andnot(cdrem, cd_remove, 2262 matrix_mdev->matrix.adm, 2263 AP_DOMAINS); 2264 2265 if (do_remove) 2266 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem, 2267 cdrem); 2268 2269 mutex_unlock(&matrix_dev->mdevs_lock); 2270 mutex_unlock(&matrix_mdev->kvm->lock); 2271 } 2272 } 2273 2274 /** 2275 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and 2276 * control domains from the host AP configuration 2277 * by unplugging them from the guests that are 2278 * using them. 2279 * @cur_config_info: the current host AP configuration information 2280 * @prev_config_info: the previous host AP configuration information 2281 */ 2282 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info, 2283 struct ap_config_info *prev_config_info) 2284 { 2285 int do_remove; 2286 DECLARE_BITMAP(aprem, AP_DEVICES); 2287 DECLARE_BITMAP(aqrem, AP_DOMAINS); 2288 DECLARE_BITMAP(cdrem, AP_DOMAINS); 2289 2290 do_remove = bitmap_andnot(aprem, 2291 (unsigned long *)prev_config_info->apm, 2292 (unsigned long *)cur_config_info->apm, 2293 AP_DEVICES); 2294 do_remove |= bitmap_andnot(aqrem, 2295 (unsigned long *)prev_config_info->aqm, 2296 (unsigned long *)cur_config_info->aqm, 2297 AP_DEVICES); 2298 do_remove |= bitmap_andnot(cdrem, 2299 (unsigned long *)prev_config_info->adm, 2300 (unsigned long *)cur_config_info->adm, 2301 AP_DEVICES); 2302 2303 if (do_remove) 2304 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem); 2305 } 2306 2307 /** 2308 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that 2309 * are older than AP type 10 (CEX4). 2310 * @apm: a bitmap of the APIDs to examine 2311 * @aqm: a bitmap of the APQIs of the queues to query for the AP type. 2312 */ 2313 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm) 2314 { 2315 bool apid_cleared; 2316 struct ap_queue_status status; 2317 unsigned long apid, apqi; 2318 struct ap_tapq_hwinfo info; 2319 2320 for_each_set_bit_inv(apid, apm, AP_DEVICES) { 2321 apid_cleared = false; 2322 2323 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) { 2324 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info); 2325 switch (status.response_code) { 2326 /* 2327 * According to the architecture in each case 2328 * below, the queue's info should be filled. 2329 */ 2330 case AP_RESPONSE_NORMAL: 2331 case AP_RESPONSE_RESET_IN_PROGRESS: 2332 case AP_RESPONSE_DECONFIGURED: 2333 case AP_RESPONSE_CHECKSTOPPED: 2334 case AP_RESPONSE_BUSY: 2335 /* 2336 * The vfio_ap device driver only 2337 * supports CEX4 and newer adapters, so 2338 * remove the APID if the adapter is 2339 * older than a CEX4. 2340 */ 2341 if (info.at < AP_DEVICE_TYPE_CEX4) { 2342 clear_bit_inv(apid, apm); 2343 apid_cleared = true; 2344 } 2345 2346 break; 2347 2348 default: 2349 /* 2350 * If we don't know the adapter type, 2351 * clear its APID since it can't be 2352 * determined whether the vfio_ap 2353 * device driver supports it. 2354 */ 2355 clear_bit_inv(apid, apm); 2356 apid_cleared = true; 2357 break; 2358 } 2359 2360 /* 2361 * If we've already cleared the APID from the apm, there 2362 * is no need to continue examining the remainin AP 2363 * queues to determine the type of the adapter. 2364 */ 2365 if (apid_cleared) 2366 continue; 2367 } 2368 } 2369 } 2370 2371 /** 2372 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and 2373 * control domains that have been added to the host's 2374 * AP configuration for each matrix mdev to which they 2375 * are assigned. 2376 * 2377 * @apm_add: a bitmap specifying the adapters that have been added to the AP 2378 * configuration. 2379 * @aqm_add: a bitmap specifying the domains that have been added to the AP 2380 * configuration. 2381 * @adm_add: a bitmap specifying the control domains that have been added to the 2382 * AP configuration. 2383 */ 2384 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add, 2385 unsigned long *adm_add) 2386 { 2387 struct ap_matrix_mdev *matrix_mdev; 2388 2389 if (list_empty(&matrix_dev->mdev_list)) 2390 return; 2391 2392 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add); 2393 2394 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) { 2395 bitmap_and(matrix_mdev->apm_add, 2396 matrix_mdev->matrix.apm, apm_add, AP_DEVICES); 2397 bitmap_and(matrix_mdev->aqm_add, 2398 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS); 2399 bitmap_and(matrix_mdev->adm_add, 2400 matrix_mdev->matrix.adm, adm_add, AP_DEVICES); 2401 } 2402 } 2403 2404 /** 2405 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and 2406 * control domains to the host AP configuration 2407 * by updating the bitmaps that specify what adapters, 2408 * domains and control domains have been added so they 2409 * can be hot plugged into the guest when the AP bus 2410 * scan completes (see vfio_ap_on_scan_complete 2411 * function). 2412 * @cur_config_info: the current AP configuration information 2413 * @prev_config_info: the previous AP configuration information 2414 */ 2415 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info, 2416 struct ap_config_info *prev_config_info) 2417 { 2418 bool do_add; 2419 DECLARE_BITMAP(apm_add, AP_DEVICES); 2420 DECLARE_BITMAP(aqm_add, AP_DOMAINS); 2421 DECLARE_BITMAP(adm_add, AP_DOMAINS); 2422 2423 do_add = bitmap_andnot(apm_add, 2424 (unsigned long *)cur_config_info->apm, 2425 (unsigned long *)prev_config_info->apm, 2426 AP_DEVICES); 2427 do_add |= bitmap_andnot(aqm_add, 2428 (unsigned long *)cur_config_info->aqm, 2429 (unsigned long *)prev_config_info->aqm, 2430 AP_DOMAINS); 2431 do_add |= bitmap_andnot(adm_add, 2432 (unsigned long *)cur_config_info->adm, 2433 (unsigned long *)prev_config_info->adm, 2434 AP_DOMAINS); 2435 2436 if (do_add) 2437 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add); 2438 } 2439 2440 /** 2441 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP 2442 * configuration. 2443 * 2444 * @cur_cfg_info: the current host AP configuration 2445 * @prev_cfg_info: the previous host AP configuration 2446 */ 2447 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info, 2448 struct ap_config_info *prev_cfg_info) 2449 { 2450 if (!cur_cfg_info || !prev_cfg_info) 2451 return; 2452 2453 mutex_lock(&matrix_dev->guests_lock); 2454 2455 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info); 2456 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info); 2457 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info)); 2458 2459 mutex_unlock(&matrix_dev->guests_lock); 2460 } 2461 2462 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev) 2463 { 2464 bool do_hotplug = false; 2465 int filter_domains = 0; 2466 int filter_adapters = 0; 2467 DECLARE_BITMAP(apm, AP_DEVICES); 2468 DECLARE_BITMAP(aqm, AP_DOMAINS); 2469 2470 mutex_lock(&matrix_mdev->kvm->lock); 2471 mutex_lock(&matrix_dev->mdevs_lock); 2472 2473 filter_adapters = bitmap_and(apm, matrix_mdev->matrix.apm, 2474 matrix_mdev->apm_add, AP_DEVICES); 2475 filter_domains = bitmap_and(aqm, matrix_mdev->matrix.aqm, 2476 matrix_mdev->aqm_add, AP_DOMAINS); 2477 2478 if (filter_adapters && filter_domains) 2479 do_hotplug |= vfio_ap_mdev_filter_matrix(apm, aqm, matrix_mdev); 2480 else if (filter_adapters) 2481 do_hotplug |= 2482 vfio_ap_mdev_filter_matrix(apm, 2483 matrix_mdev->shadow_apcb.aqm, 2484 matrix_mdev); 2485 else 2486 do_hotplug |= 2487 vfio_ap_mdev_filter_matrix(matrix_mdev->shadow_apcb.apm, 2488 aqm, matrix_mdev); 2489 2490 if (bitmap_intersects(matrix_mdev->matrix.adm, matrix_mdev->adm_add, 2491 AP_DOMAINS)) 2492 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev); 2493 2494 if (do_hotplug) 2495 vfio_ap_mdev_update_guest_apcb(matrix_mdev); 2496 2497 mutex_unlock(&matrix_dev->mdevs_lock); 2498 mutex_unlock(&matrix_mdev->kvm->lock); 2499 } 2500 2501 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info, 2502 struct ap_config_info *old_config_info) 2503 { 2504 struct ap_matrix_mdev *matrix_mdev; 2505 2506 mutex_lock(&matrix_dev->guests_lock); 2507 2508 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) { 2509 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) && 2510 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) && 2511 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS)) 2512 continue; 2513 2514 vfio_ap_mdev_hot_plug_cfg(matrix_mdev); 2515 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES); 2516 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS); 2517 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS); 2518 } 2519 2520 mutex_unlock(&matrix_dev->guests_lock); 2521 } 2522