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