1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * channel program interfaces 4 * 5 * Copyright IBM Corp. 2017 6 * 7 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> 8 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> 9 */ 10 11 #include <linux/ratelimit.h> 12 #include <linux/mm.h> 13 #include <linux/slab.h> 14 #include <linux/highmem.h> 15 #include <linux/iommu.h> 16 #include <linux/vfio.h> 17 #include <asm/idals.h> 18 19 #include "vfio_ccw_cp.h" 20 #include "vfio_ccw_private.h" 21 22 struct page_array { 23 /* Array that stores pages need to pin. */ 24 dma_addr_t *pa_iova; 25 /* Array that receives the pinned pages. */ 26 struct page **pa_page; 27 /* Number of pages pinned from @pa_iova. */ 28 int pa_nr; 29 }; 30 31 struct ccwchain { 32 struct list_head next; 33 struct ccw1 *ch_ccw; 34 /* Guest physical address of the current chain. */ 35 u64 ch_iova; 36 /* Count of the valid ccws in chain. */ 37 int ch_len; 38 /* Pinned PAGEs for the original data. */ 39 struct page_array *ch_pa; 40 }; 41 42 /* 43 * page_array_alloc() - alloc memory for page array 44 * @pa: page_array on which to perform the operation 45 * @len: number of pages that should be pinned from @iova 46 * 47 * Attempt to allocate memory for page array. 48 * 49 * Usage of page_array: 50 * We expect (pa_nr == 0) and (pa_iova == NULL), any field in 51 * this structure will be filled in by this function. 52 * 53 * Returns: 54 * 0 if page array is allocated 55 * -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova is not NULL 56 * -ENOMEM if alloc failed 57 */ 58 static int page_array_alloc(struct page_array *pa, unsigned int len) 59 { 60 if (pa->pa_nr || pa->pa_iova) 61 return -EINVAL; 62 63 if (len == 0) 64 return -EINVAL; 65 66 pa->pa_nr = len; 67 68 pa->pa_iova = kcalloc(len, sizeof(*pa->pa_iova), GFP_KERNEL); 69 if (!pa->pa_iova) 70 return -ENOMEM; 71 72 pa->pa_page = kcalloc(len, sizeof(*pa->pa_page), GFP_KERNEL); 73 if (!pa->pa_page) { 74 kfree(pa->pa_iova); 75 return -ENOMEM; 76 } 77 78 return 0; 79 } 80 81 /* 82 * page_array_unpin() - Unpin user pages in memory 83 * @pa: page_array on which to perform the operation 84 * @vdev: the vfio device to perform the operation 85 * @pa_nr: number of user pages to unpin 86 * @unaligned: were pages unaligned on the pin request 87 * 88 * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0, 89 * otherwise only clear pa->pa_nr 90 */ 91 static void page_array_unpin(struct page_array *pa, 92 struct vfio_device *vdev, int pa_nr, bool unaligned) 93 { 94 int unpinned = 0, npage = 1; 95 96 while (unpinned < pa_nr) { 97 dma_addr_t *first = &pa->pa_iova[unpinned]; 98 dma_addr_t *last = &first[npage]; 99 100 if (unpinned + npage < pa_nr && 101 *first + npage * PAGE_SIZE == *last && 102 !unaligned) { 103 npage++; 104 continue; 105 } 106 107 vfio_unpin_pages(vdev, *first, npage); 108 unpinned += npage; 109 npage = 1; 110 } 111 112 pa->pa_nr = 0; 113 } 114 115 /* 116 * page_array_pin() - Pin user pages in memory 117 * @pa: page_array on which to perform the operation 118 * @vdev: the vfio device to perform pin operations 119 * @unaligned: are pages aligned to 4K boundary? 120 * 121 * Returns number of pages pinned upon success. 122 * If the pin request partially succeeds, or fails completely, 123 * all pages are left unpinned and a negative error value is returned. 124 * 125 * Requests to pin "aligned" pages can be coalesced into a single 126 * vfio_pin_pages request for the sake of efficiency, based on the 127 * expectation of 4K page requests. Unaligned requests are probably 128 * dealing with 2K "pages", and cannot be coalesced without 129 * reworking this logic to incorporate that math. 130 */ 131 static int page_array_pin(struct page_array *pa, struct vfio_device *vdev, bool unaligned) 132 { 133 int pinned = 0, npage = 1; 134 int ret = 0; 135 136 while (pinned < pa->pa_nr) { 137 dma_addr_t *first = &pa->pa_iova[pinned]; 138 dma_addr_t *last = &first[npage]; 139 140 if (pinned + npage < pa->pa_nr && 141 *first + npage * PAGE_SIZE == *last && 142 !unaligned) { 143 npage++; 144 continue; 145 } 146 147 ret = vfio_pin_pages(vdev, *first, npage, 148 IOMMU_READ | IOMMU_WRITE, 149 &pa->pa_page[pinned]); 150 if (ret < 0) { 151 goto err_out; 152 } else if (ret > 0 && ret != npage) { 153 pinned += ret; 154 ret = -EINVAL; 155 goto err_out; 156 } 157 pinned += npage; 158 npage = 1; 159 } 160 161 return ret; 162 163 err_out: 164 page_array_unpin(pa, vdev, pinned, unaligned); 165 return ret; 166 } 167 168 /* Unpin the pages before releasing the memory. */ 169 static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev, bool unaligned) 170 { 171 page_array_unpin(pa, vdev, pa->pa_nr, unaligned); 172 kfree(pa->pa_page); 173 kfree(pa->pa_iova); 174 } 175 176 static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length) 177 { 178 u64 iova_pfn_start = iova >> PAGE_SHIFT; 179 u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT; 180 u64 pfn; 181 int i; 182 183 for (i = 0; i < pa->pa_nr; i++) { 184 pfn = pa->pa_iova[i] >> PAGE_SHIFT; 185 if (pfn >= iova_pfn_start && pfn <= iova_pfn_end) 186 return true; 187 } 188 189 return false; 190 } 191 /* Create the list of IDAL words for a page_array. */ 192 static inline void page_array_idal_create_words(struct page_array *pa, 193 unsigned long *idaws) 194 { 195 int i; 196 197 /* 198 * Idal words (execept the first one) rely on the memory being 4k 199 * aligned. If a user virtual address is 4K aligned, then it's 200 * corresponding kernel physical address will also be 4K aligned. Thus 201 * there will be no problem here to simply use the phys to create an 202 * idaw. 203 */ 204 205 for (i = 0; i < pa->pa_nr; i++) { 206 idaws[i] = page_to_phys(pa->pa_page[i]); 207 208 /* Incorporate any offset from each starting address */ 209 idaws[i] += pa->pa_iova[i] & (PAGE_SIZE - 1); 210 } 211 } 212 213 static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len) 214 { 215 struct ccw0 ccw0; 216 struct ccw1 *pccw1 = source; 217 int i; 218 219 for (i = 0; i < len; i++) { 220 ccw0 = *(struct ccw0 *)pccw1; 221 if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) { 222 pccw1->cmd_code = CCW_CMD_TIC; 223 pccw1->flags = 0; 224 pccw1->count = 0; 225 } else { 226 pccw1->cmd_code = ccw0.cmd_code; 227 pccw1->flags = ccw0.flags; 228 pccw1->count = ccw0.count; 229 } 230 pccw1->cda = ccw0.cda; 231 pccw1++; 232 } 233 } 234 235 #define idal_is_2k(_cp) (!(_cp)->orb.cmd.c64 || (_cp)->orb.cmd.i2k) 236 237 /* 238 * Helpers to operate ccwchain. 239 */ 240 #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02) 241 #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C) 242 #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE) 243 244 #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP) 245 246 #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC) 247 248 #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA) 249 #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP) 250 251 #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC)) 252 253 /* 254 * ccw_does_data_transfer() 255 * 256 * Determine whether a CCW will move any data, such that the guest pages 257 * would need to be pinned before performing the I/O. 258 * 259 * Returns 1 if yes, 0 if no. 260 */ 261 static inline int ccw_does_data_transfer(struct ccw1 *ccw) 262 { 263 /* If the count field is zero, then no data will be transferred */ 264 if (ccw->count == 0) 265 return 0; 266 267 /* If the command is a NOP, then no data will be transferred */ 268 if (ccw_is_noop(ccw)) 269 return 0; 270 271 /* If the skip flag is off, then data will be transferred */ 272 if (!ccw_is_skip(ccw)) 273 return 1; 274 275 /* 276 * If the skip flag is on, it is only meaningful if the command 277 * code is a read, read backward, sense, or sense ID. In those 278 * cases, no data will be transferred. 279 */ 280 if (ccw_is_read(ccw) || ccw_is_read_backward(ccw)) 281 return 0; 282 283 if (ccw_is_sense(ccw)) 284 return 0; 285 286 /* The skip flag is on, but it is ignored for this command code. */ 287 return 1; 288 } 289 290 /* 291 * is_cpa_within_range() 292 * 293 * @cpa: channel program address being questioned 294 * @head: address of the beginning of a CCW chain 295 * @len: number of CCWs within the chain 296 * 297 * Determine whether the address of a CCW (whether a new chain, 298 * or the target of a TIC) falls within a range (including the end points). 299 * 300 * Returns 1 if yes, 0 if no. 301 */ 302 static inline int is_cpa_within_range(u32 cpa, u32 head, int len) 303 { 304 u32 tail = head + (len - 1) * sizeof(struct ccw1); 305 306 return (head <= cpa && cpa <= tail); 307 } 308 309 static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len) 310 { 311 if (!ccw_is_tic(ccw)) 312 return 0; 313 314 return is_cpa_within_range(ccw->cda, head, len); 315 } 316 317 static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len) 318 { 319 struct ccwchain *chain; 320 321 chain = kzalloc(sizeof(*chain), GFP_KERNEL); 322 if (!chain) 323 return NULL; 324 325 chain->ch_ccw = kcalloc(len, sizeof(*chain->ch_ccw), GFP_DMA | GFP_KERNEL); 326 if (!chain->ch_ccw) 327 goto out_err; 328 329 chain->ch_pa = kcalloc(len, sizeof(*chain->ch_pa), GFP_KERNEL); 330 if (!chain->ch_pa) 331 goto out_err; 332 333 list_add_tail(&chain->next, &cp->ccwchain_list); 334 335 return chain; 336 337 out_err: 338 kfree(chain->ch_ccw); 339 kfree(chain); 340 return NULL; 341 } 342 343 static void ccwchain_free(struct ccwchain *chain) 344 { 345 list_del(&chain->next); 346 kfree(chain->ch_pa); 347 kfree(chain->ch_ccw); 348 kfree(chain); 349 } 350 351 /* Free resource for a ccw that allocated memory for its cda. */ 352 static void ccwchain_cda_free(struct ccwchain *chain, int idx) 353 { 354 struct ccw1 *ccw = &chain->ch_ccw[idx]; 355 356 if (ccw_is_tic(ccw)) 357 return; 358 359 kfree(phys_to_virt(ccw->cda)); 360 } 361 362 /** 363 * ccwchain_calc_length - calculate the length of the ccw chain. 364 * @iova: guest physical address of the target ccw chain 365 * @cp: channel_program on which to perform the operation 366 * 367 * This is the chain length not considering any TICs. 368 * You need to do a new round for each TIC target. 369 * 370 * The program is also validated for absence of not yet supported 371 * indirect data addressing scenarios. 372 * 373 * Returns: the length of the ccw chain or -errno. 374 */ 375 static int ccwchain_calc_length(u64 iova, struct channel_program *cp) 376 { 377 struct ccw1 *ccw = cp->guest_cp; 378 int cnt = 0; 379 380 do { 381 cnt++; 382 383 /* 384 * As we don't want to fail direct addressing even if the 385 * orb specified one of the unsupported formats, we defer 386 * checking for IDAWs in unsupported formats to here. 387 */ 388 if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw)) 389 return -EOPNOTSUPP; 390 391 /* 392 * We want to keep counting if the current CCW has the 393 * command-chaining flag enabled, or if it is a TIC CCW 394 * that loops back into the current chain. The latter 395 * is used for device orientation, where the CCW PRIOR to 396 * the TIC can either jump to the TIC or a CCW immediately 397 * after the TIC, depending on the results of its operation. 398 */ 399 if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt)) 400 break; 401 402 ccw++; 403 } while (cnt < CCWCHAIN_LEN_MAX + 1); 404 405 if (cnt == CCWCHAIN_LEN_MAX + 1) 406 cnt = -EINVAL; 407 408 return cnt; 409 } 410 411 static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp) 412 { 413 struct ccwchain *chain; 414 u32 ccw_head; 415 416 list_for_each_entry(chain, &cp->ccwchain_list, next) { 417 ccw_head = chain->ch_iova; 418 if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len)) 419 return 1; 420 } 421 422 return 0; 423 } 424 425 static int ccwchain_loop_tic(struct ccwchain *chain, 426 struct channel_program *cp); 427 428 static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp) 429 { 430 struct vfio_device *vdev = 431 &container_of(cp, struct vfio_ccw_private, cp)->vdev; 432 struct ccwchain *chain; 433 int len, ret; 434 435 /* Copy 2K (the most we support today) of possible CCWs */ 436 ret = vfio_dma_rw(vdev, cda, cp->guest_cp, CCWCHAIN_LEN_MAX * sizeof(struct ccw1), false); 437 if (ret) 438 return ret; 439 440 /* Convert any Format-0 CCWs to Format-1 */ 441 if (!cp->orb.cmd.fmt) 442 convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX); 443 444 /* Count the CCWs in the current chain */ 445 len = ccwchain_calc_length(cda, cp); 446 if (len < 0) 447 return len; 448 449 /* Need alloc a new chain for this one. */ 450 chain = ccwchain_alloc(cp, len); 451 if (!chain) 452 return -ENOMEM; 453 454 chain->ch_len = len; 455 chain->ch_iova = cda; 456 457 /* Copy the actual CCWs into the new chain */ 458 memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1)); 459 460 /* Loop for tics on this new chain. */ 461 ret = ccwchain_loop_tic(chain, cp); 462 463 if (ret) 464 ccwchain_free(chain); 465 466 return ret; 467 } 468 469 /* Loop for TICs. */ 470 static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp) 471 { 472 struct ccw1 *tic; 473 int i, ret; 474 475 for (i = 0; i < chain->ch_len; i++) { 476 tic = &chain->ch_ccw[i]; 477 478 if (!ccw_is_tic(tic)) 479 continue; 480 481 /* May transfer to an existing chain. */ 482 if (tic_target_chain_exists(tic, cp)) 483 continue; 484 485 /* Build a ccwchain for the next segment */ 486 ret = ccwchain_handle_ccw(tic->cda, cp); 487 if (ret) 488 return ret; 489 } 490 491 return 0; 492 } 493 494 static int ccwchain_fetch_tic(struct ccw1 *ccw, 495 struct channel_program *cp) 496 { 497 struct ccwchain *iter; 498 u32 ccw_head; 499 500 list_for_each_entry(iter, &cp->ccwchain_list, next) { 501 ccw_head = iter->ch_iova; 502 if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) { 503 ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) + 504 (ccw->cda - ccw_head)); 505 return 0; 506 } 507 } 508 509 return -EFAULT; 510 } 511 512 static unsigned long *get_guest_idal(struct ccw1 *ccw, 513 struct channel_program *cp, 514 int idaw_nr) 515 { 516 struct vfio_device *vdev = 517 &container_of(cp, struct vfio_ccw_private, cp)->vdev; 518 unsigned long *idaws; 519 unsigned int *idaws_f1; 520 int idal_len = idaw_nr * sizeof(*idaws); 521 int idaw_size = idal_is_2k(cp) ? PAGE_SIZE / 2 : PAGE_SIZE; 522 int idaw_mask = ~(idaw_size - 1); 523 int i, ret; 524 525 idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL); 526 if (!idaws) 527 return ERR_PTR(-ENOMEM); 528 529 if (ccw_is_idal(ccw)) { 530 /* Copy IDAL from guest */ 531 ret = vfio_dma_rw(vdev, ccw->cda, idaws, idal_len, false); 532 if (ret) { 533 kfree(idaws); 534 return ERR_PTR(ret); 535 } 536 } else { 537 /* Fabricate an IDAL based off CCW data address */ 538 if (cp->orb.cmd.c64) { 539 idaws[0] = ccw->cda; 540 for (i = 1; i < idaw_nr; i++) 541 idaws[i] = (idaws[i - 1] + idaw_size) & idaw_mask; 542 } else { 543 idaws_f1 = (unsigned int *)idaws; 544 idaws_f1[0] = ccw->cda; 545 for (i = 1; i < idaw_nr; i++) 546 idaws_f1[i] = (idaws_f1[i - 1] + idaw_size) & idaw_mask; 547 } 548 } 549 550 return idaws; 551 } 552 553 /* 554 * ccw_count_idaws() - Calculate the number of IDAWs needed to transfer 555 * a specified amount of data 556 * 557 * @ccw: The Channel Command Word being translated 558 * @cp: Channel Program being processed 559 * 560 * The ORB is examined, since it specifies what IDAWs could actually be 561 * used by any CCW in the channel program, regardless of whether or not 562 * the CCW actually does. An ORB that does not specify Format-2-IDAW 563 * Control could still contain a CCW with an IDAL, which would be 564 * Format-1 and thus only move 2K with each IDAW. Thus all CCWs within 565 * the channel program must follow the same size requirements. 566 */ 567 static int ccw_count_idaws(struct ccw1 *ccw, 568 struct channel_program *cp) 569 { 570 struct vfio_device *vdev = 571 &container_of(cp, struct vfio_ccw_private, cp)->vdev; 572 u64 iova; 573 int size = cp->orb.cmd.c64 ? sizeof(u64) : sizeof(u32); 574 int ret; 575 int bytes = 1; 576 577 if (ccw->count) 578 bytes = ccw->count; 579 580 if (ccw_is_idal(ccw)) { 581 /* Read first IDAW to check its starting address. */ 582 /* All subsequent IDAWs will be 2K- or 4K-aligned. */ 583 ret = vfio_dma_rw(vdev, ccw->cda, &iova, size, false); 584 if (ret) 585 return ret; 586 587 /* 588 * Format-1 IDAWs only occupy the first 32 bits, 589 * and bit 0 is always off. 590 */ 591 if (!cp->orb.cmd.c64) 592 iova = iova >> 32; 593 } else { 594 iova = ccw->cda; 595 } 596 597 /* Format-1 IDAWs operate on 2K each */ 598 if (!cp->orb.cmd.c64) 599 return idal_2k_nr_words((void *)iova, bytes); 600 601 /* Using the 2K variant of Format-2 IDAWs? */ 602 if (cp->orb.cmd.i2k) 603 return idal_2k_nr_words((void *)iova, bytes); 604 605 /* The 'usual' case is 4K Format-2 IDAWs */ 606 return idal_nr_words((void *)iova, bytes); 607 } 608 609 static int ccwchain_fetch_ccw(struct ccw1 *ccw, 610 struct page_array *pa, 611 struct channel_program *cp) 612 { 613 struct vfio_device *vdev = 614 &container_of(cp, struct vfio_ccw_private, cp)->vdev; 615 unsigned long *idaws; 616 unsigned int *idaws_f1; 617 int ret; 618 int idaw_nr; 619 int i; 620 621 /* Calculate size of IDAL */ 622 idaw_nr = ccw_count_idaws(ccw, cp); 623 if (idaw_nr < 0) 624 return idaw_nr; 625 626 /* Allocate an IDAL from host storage */ 627 idaws = get_guest_idal(ccw, cp, idaw_nr); 628 if (IS_ERR(idaws)) { 629 ret = PTR_ERR(idaws); 630 goto out_init; 631 } 632 633 /* 634 * Allocate an array of pages to pin/translate. 635 * The number of pages is actually the count of the idaws 636 * required for the data transfer, since we only only support 637 * 4K IDAWs today. 638 */ 639 ret = page_array_alloc(pa, idaw_nr); 640 if (ret < 0) 641 goto out_free_idaws; 642 643 /* 644 * Copy guest IDAWs into page_array, in case the memory they 645 * occupy is not contiguous. 646 */ 647 idaws_f1 = (unsigned int *)idaws; 648 for (i = 0; i < idaw_nr; i++) { 649 if (cp->orb.cmd.c64) 650 pa->pa_iova[i] = idaws[i]; 651 else 652 pa->pa_iova[i] = idaws_f1[i]; 653 } 654 655 if (ccw_does_data_transfer(ccw)) { 656 ret = page_array_pin(pa, vdev, idal_is_2k(cp)); 657 if (ret < 0) 658 goto out_unpin; 659 } else { 660 pa->pa_nr = 0; 661 } 662 663 ccw->cda = (__u32) virt_to_phys(idaws); 664 ccw->flags |= CCW_FLAG_IDA; 665 666 /* Populate the IDAL with pinned/translated addresses from page */ 667 page_array_idal_create_words(pa, idaws); 668 669 return 0; 670 671 out_unpin: 672 page_array_unpin_free(pa, vdev, idal_is_2k(cp)); 673 out_free_idaws: 674 kfree(idaws); 675 out_init: 676 ccw->cda = 0; 677 return ret; 678 } 679 680 /* 681 * Fetch one ccw. 682 * To reduce memory copy, we'll pin the cda page in memory, 683 * and to get rid of the cda 2G limitiaion of ccw1, we'll translate 684 * direct ccws to idal ccws. 685 */ 686 static int ccwchain_fetch_one(struct ccw1 *ccw, 687 struct page_array *pa, 688 struct channel_program *cp) 689 690 { 691 if (ccw_is_tic(ccw)) 692 return ccwchain_fetch_tic(ccw, cp); 693 694 return ccwchain_fetch_ccw(ccw, pa, cp); 695 } 696 697 /** 698 * cp_init() - allocate ccwchains for a channel program. 699 * @cp: channel_program on which to perform the operation 700 * @orb: control block for the channel program from the guest 701 * 702 * This creates one or more ccwchain(s), and copies the raw data of 703 * the target channel program from @orb->cmd.iova to the new ccwchain(s). 704 * 705 * Limitations: 706 * 1. Supports idal(c64) ccw chaining. 707 * 2. Supports 4k idaw. 708 * 709 * Returns: 710 * %0 on success and a negative error value on failure. 711 */ 712 int cp_init(struct channel_program *cp, union orb *orb) 713 { 714 struct vfio_device *vdev = 715 &container_of(cp, struct vfio_ccw_private, cp)->vdev; 716 /* custom ratelimit used to avoid flood during guest IPL */ 717 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1); 718 int ret; 719 720 /* this is an error in the caller */ 721 if (cp->initialized) 722 return -EBUSY; 723 724 /* 725 * We only support prefetching the channel program. We assume all channel 726 * programs executed by supported guests likewise support prefetching. 727 * Executing a channel program that does not specify prefetching will 728 * typically not cause an error, but a warning is issued to help identify 729 * the problem if something does break. 730 */ 731 if (!orb->cmd.pfch && __ratelimit(&ratelimit_state)) 732 dev_warn( 733 vdev->dev, 734 "Prefetching channel program even though prefetch not specified in ORB"); 735 736 INIT_LIST_HEAD(&cp->ccwchain_list); 737 memcpy(&cp->orb, orb, sizeof(*orb)); 738 739 /* Build a ccwchain for the first CCW segment */ 740 ret = ccwchain_handle_ccw(orb->cmd.cpa, cp); 741 742 if (!ret) 743 cp->initialized = true; 744 745 return ret; 746 } 747 748 749 /** 750 * cp_free() - free resources for channel program. 751 * @cp: channel_program on which to perform the operation 752 * 753 * This unpins the memory pages and frees the memory space occupied by 754 * @cp, which must have been returned by a previous call to cp_init(). 755 * Otherwise, undefined behavior occurs. 756 */ 757 void cp_free(struct channel_program *cp) 758 { 759 struct vfio_device *vdev = 760 &container_of(cp, struct vfio_ccw_private, cp)->vdev; 761 struct ccwchain *chain, *temp; 762 int i; 763 764 if (!cp->initialized) 765 return; 766 767 cp->initialized = false; 768 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { 769 for (i = 0; i < chain->ch_len; i++) { 770 page_array_unpin_free(&chain->ch_pa[i], vdev, idal_is_2k(cp)); 771 ccwchain_cda_free(chain, i); 772 } 773 ccwchain_free(chain); 774 } 775 } 776 777 /** 778 * cp_prefetch() - translate a guest physical address channel program to 779 * a real-device runnable channel program. 780 * @cp: channel_program on which to perform the operation 781 * 782 * This function translates the guest-physical-address channel program 783 * and stores the result to ccwchain list. @cp must have been 784 * initialized by a previous call with cp_init(). Otherwise, undefined 785 * behavior occurs. 786 * For each chain composing the channel program: 787 * - On entry ch_len holds the count of CCWs to be translated. 788 * - On exit ch_len is adjusted to the count of successfully translated CCWs. 789 * This allows cp_free to find in ch_len the count of CCWs to free in a chain. 790 * 791 * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced 792 * as helpers to do ccw chain translation inside the kernel. Basically 793 * they accept a channel program issued by a virtual machine, and 794 * translate the channel program to a real-device runnable channel 795 * program. 796 * 797 * These APIs will copy the ccws into kernel-space buffers, and update 798 * the guest phsical addresses with their corresponding host physical 799 * addresses. Then channel I/O device drivers could issue the 800 * translated channel program to real devices to perform an I/O 801 * operation. 802 * 803 * These interfaces are designed to support translation only for 804 * channel programs, which are generated and formatted by a 805 * guest. Thus this will make it possible for things like VFIO to 806 * leverage the interfaces to passthrough a channel I/O mediated 807 * device in QEMU. 808 * 809 * We support direct ccw chaining by translating them to idal ccws. 810 * 811 * Returns: 812 * %0 on success and a negative error value on failure. 813 */ 814 int cp_prefetch(struct channel_program *cp) 815 { 816 struct ccwchain *chain; 817 struct ccw1 *ccw; 818 struct page_array *pa; 819 int len, idx, ret; 820 821 /* this is an error in the caller */ 822 if (!cp->initialized) 823 return -EINVAL; 824 825 list_for_each_entry(chain, &cp->ccwchain_list, next) { 826 len = chain->ch_len; 827 for (idx = 0; idx < len; idx++) { 828 ccw = &chain->ch_ccw[idx]; 829 pa = &chain->ch_pa[idx]; 830 831 ret = ccwchain_fetch_one(ccw, pa, cp); 832 if (ret) 833 goto out_err; 834 } 835 } 836 837 return 0; 838 out_err: 839 /* Only cleanup the chain elements that were actually translated. */ 840 chain->ch_len = idx; 841 list_for_each_entry_continue(chain, &cp->ccwchain_list, next) { 842 chain->ch_len = 0; 843 } 844 return ret; 845 } 846 847 /** 848 * cp_get_orb() - get the orb of the channel program 849 * @cp: channel_program on which to perform the operation 850 * @sch: subchannel the operation will be performed against 851 * 852 * This function returns the address of the updated orb of the channel 853 * program. Channel I/O device drivers could use this orb to issue a 854 * ssch. 855 */ 856 union orb *cp_get_orb(struct channel_program *cp, struct subchannel *sch) 857 { 858 union orb *orb; 859 struct ccwchain *chain; 860 struct ccw1 *cpa; 861 862 /* this is an error in the caller */ 863 if (!cp->initialized) 864 return NULL; 865 866 orb = &cp->orb; 867 868 orb->cmd.intparm = (u32)virt_to_phys(sch); 869 orb->cmd.fmt = 1; 870 871 /* 872 * Everything built by vfio-ccw is a Format-2 IDAL. 873 * If the input was a Format-1 IDAL, indicate that 874 * 2K Format-2 IDAWs were created here. 875 */ 876 if (!orb->cmd.c64) 877 orb->cmd.i2k = 1; 878 orb->cmd.c64 = 1; 879 880 if (orb->cmd.lpm == 0) 881 orb->cmd.lpm = sch->lpm; 882 883 chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next); 884 cpa = chain->ch_ccw; 885 orb->cmd.cpa = (__u32)virt_to_phys(cpa); 886 887 return orb; 888 } 889 890 /** 891 * cp_update_scsw() - update scsw for a channel program. 892 * @cp: channel_program on which to perform the operation 893 * @scsw: I/O results of the channel program and also the target to be 894 * updated 895 * 896 * @scsw contains the I/O results of the channel program that pointed 897 * to by @cp. However what @scsw->cpa stores is a host physical 898 * address, which is meaningless for the guest, which is waiting for 899 * the I/O results. 900 * 901 * This function updates @scsw->cpa to its coressponding guest physical 902 * address. 903 */ 904 void cp_update_scsw(struct channel_program *cp, union scsw *scsw) 905 { 906 struct ccwchain *chain; 907 u32 cpa = scsw->cmd.cpa; 908 u32 ccw_head; 909 910 if (!cp->initialized) 911 return; 912 913 /* 914 * LATER: 915 * For now, only update the cmd.cpa part. We may need to deal with 916 * other portions of the schib as well, even if we don't return them 917 * in the ioctl directly. Path status changes etc. 918 */ 919 list_for_each_entry(chain, &cp->ccwchain_list, next) { 920 ccw_head = (u32)(u64)chain->ch_ccw; 921 /* 922 * On successful execution, cpa points just beyond the end 923 * of the chain. 924 */ 925 if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) { 926 /* 927 * (cpa - ccw_head) is the offset value of the host 928 * physical ccw to its chain head. 929 * Adding this value to the guest physical ccw chain 930 * head gets us the guest cpa. 931 */ 932 cpa = chain->ch_iova + (cpa - ccw_head); 933 break; 934 } 935 } 936 937 scsw->cmd.cpa = cpa; 938 } 939 940 /** 941 * cp_iova_pinned() - check if an iova is pinned for a ccw chain. 942 * @cp: channel_program on which to perform the operation 943 * @iova: the iova to check 944 * @length: the length to check from @iova 945 * 946 * If the @iova is currently pinned for the ccw chain, return true; 947 * else return false. 948 */ 949 bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length) 950 { 951 struct ccwchain *chain; 952 int i; 953 954 if (!cp->initialized) 955 return false; 956 957 list_for_each_entry(chain, &cp->ccwchain_list, next) { 958 for (i = 0; i < chain->ch_len; i++) 959 if (page_array_iova_pinned(&chain->ch_pa[i], iova, length)) 960 return true; 961 } 962 963 return false; 964 } 965