perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / drivers / s390 / cio / vfio_ccw_cp.c
blob70a006ba4d050d2d3063f6653e610372e7d2d074
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * channel program interfaces
5 * Copyright IBM Corp. 2017
7 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
8 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
9 */
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>
17 #include "vfio_ccw_cp.h"
20 * Max length for ccw chain.
21 * XXX: Limit to 256, need to check more?
23 #define CCWCHAIN_LEN_MAX 256
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;
36 struct pfn_array_table {
37 struct pfn_array *pat_pa;
38 int pat_nr;
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;
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
59 * Attempt to allocate memory for PFNs, and pin user pages in memory.
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.
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.
71 static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev,
72 u64 iova, unsigned int len)
74 int i, ret = 0;
76 if (!len)
77 return 0;
79 if (pa->pa_nr || pa->pa_iova_pfn)
80 return -EINVAL;
82 pa->pa_iova = iova;
84 pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
85 if (!pa->pa_nr)
86 return -EINVAL;
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;
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;
100 ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
101 IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
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;
111 return ret;
113 err_out:
114 pa->pa_nr = 0;
115 kfree(pa->pa_iova_pfn);
116 pa->pa_iova_pfn = NULL;
118 return ret;
121 /* Unpin the pages before releasing the memory. */
122 static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
124 vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
125 pa->pa_nr = 0;
126 kfree(pa->pa_iova_pfn);
129 static int pfn_array_table_init(struct pfn_array_table *pat, int nr)
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;
137 pat->pat_nr = nr;
139 return 0;
142 static void pfn_array_table_unpin_free(struct pfn_array_table *pat,
143 struct device *mdev)
145 int i;
147 for (i = 0; i < pat->pat_nr; i++)
148 pfn_array_unpin_free(pat->pat_pa + i, mdev);
150 if (pat->pat_nr) {
151 kfree(pat->pat_pa);
152 pat->pat_pa = NULL;
153 pat->pat_nr = 0;
157 static bool pfn_array_table_iova_pinned(struct pfn_array_table *pat,
158 unsigned long iova)
160 struct pfn_array *pa = pat->pat_pa;
161 unsigned long iova_pfn = iova >> PAGE_SHIFT;
162 int i, j;
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;
169 return false;
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)
176 struct pfn_array *pa;
177 int i, j, k;
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.
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++;
200 * Within the domain (@mdev), copy @n bytes from a guest physical
201 * address (@iova) to a host physical address (@to).
203 static long copy_from_iova(struct device *mdev,
204 void *to, u64 iova,
205 unsigned long n)
207 struct pfn_array pa = {0};
208 u64 from;
209 int i, ret;
210 unsigned long l, m;
212 ret = pfn_array_alloc_pin(&pa, mdev, iova, n);
213 if (ret <= 0)
214 return ret;
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);
225 m = min(l, m);
226 memcpy(to + (n - l), (void *)from, m);
228 l -= m;
229 if (l == 0)
230 break;
233 pfn_array_unpin_free(&pa, mdev);
235 return l;
238 static long copy_ccw_from_iova(struct channel_program *cp,
239 struct ccw1 *to, u64 iova,
240 unsigned long len)
242 struct ccw0 ccw0;
243 struct ccw1 *pccw1;
244 int ret;
245 int i;
247 ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1));
248 if (ret)
249 return ret;
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;
264 pccw1->cda = ccw0.cda;
265 pccw1++;
269 return ret;
273 * Helpers to operate ccwchain.
275 #define ccw_is_test(_ccw) (((_ccw)->cmd_code & 0x0F) == 0)
277 #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
279 #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
281 #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
284 #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
286 static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
288 struct ccwchain *chain;
289 void *data;
290 size_t size;
292 /* Make ccw address aligned to 8. */
293 size = ((sizeof(*chain) + 7L) & -8L) +
294 sizeof(*chain->ch_ccw) * len +
295 sizeof(*chain->ch_pat) * len;
296 chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
297 if (!chain)
298 return NULL;
300 data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
301 chain->ch_ccw = (struct ccw1 *)data;
303 data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
304 chain->ch_pat = (struct pfn_array_table *)data;
306 chain->ch_len = len;
308 list_add_tail(&chain->next, &cp->ccwchain_list);
310 return chain;
313 static void ccwchain_free(struct ccwchain *chain)
315 list_del(&chain->next);
316 kfree(chain);
319 /* Free resource for a ccw that allocated memory for its cda. */
320 static void ccwchain_cda_free(struct ccwchain *chain, int idx)
322 struct ccw1 *ccw = chain->ch_ccw + idx;
324 if (ccw_is_test(ccw) || ccw_is_noop(ccw) || ccw_is_tic(ccw))
325 return;
326 if (!ccw->count)
327 return;
329 kfree((void *)(u64)ccw->cda);
332 /* Unpin the pages then free the memory resources. */
333 static void cp_unpin_free(struct channel_program *cp)
335 struct ccwchain *chain, *temp;
336 int i;
338 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
339 for (i = 0; i < chain->ch_len; i++) {
340 pfn_array_table_unpin_free(chain->ch_pat + i,
341 cp->mdev);
342 ccwchain_cda_free(chain, i);
344 ccwchain_free(chain);
349 * ccwchain_calc_length - calculate the length of the ccw chain.
350 * @iova: guest physical address of the target ccw chain
351 * @cp: channel_program on which to perform the operation
353 * This is the chain length not considering any TICs.
354 * You need to do a new round for each TIC target.
356 * The program is also validated for absence of not yet supported
357 * indirect data addressing scenarios.
359 * Returns: the length of the ccw chain or -errno.
361 static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
363 struct ccw1 *ccw, *p;
364 int cnt;
367 * Copy current chain from guest to host kernel.
368 * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256).
369 * So copying 2K is enough (safe).
371 p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL);
372 if (!ccw)
373 return -ENOMEM;
375 cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX);
376 if (cnt) {
377 kfree(ccw);
378 return cnt;
381 cnt = 0;
382 do {
383 cnt++;
386 * As we don't want to fail direct addressing even if the
387 * orb specified one of the unsupported formats, we defer
388 * checking for IDAWs in unsupported formats to here.
390 if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw)) {
391 kfree(p);
392 return -EOPNOTSUPP;
395 if ((!ccw_is_chain(ccw)) && (!ccw_is_tic(ccw)))
396 break;
398 ccw++;
399 } while (cnt < CCWCHAIN_LEN_MAX + 1);
401 if (cnt == CCWCHAIN_LEN_MAX + 1)
402 cnt = -EINVAL;
404 kfree(p);
405 return cnt;
408 static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
410 struct ccwchain *chain;
411 u32 ccw_head, ccw_tail;
413 list_for_each_entry(chain, &cp->ccwchain_list, next) {
414 ccw_head = chain->ch_iova;
415 ccw_tail = ccw_head + (chain->ch_len - 1) * sizeof(struct ccw1);
417 if ((ccw_head <= tic->cda) && (tic->cda <= ccw_tail))
418 return 1;
421 return 0;
424 static int ccwchain_loop_tic(struct ccwchain *chain,
425 struct channel_program *cp);
427 static int ccwchain_handle_tic(struct ccw1 *tic, struct channel_program *cp)
429 struct ccwchain *chain;
430 int len, ret;
432 /* May transfer to an existing chain. */
433 if (tic_target_chain_exists(tic, cp))
434 return 0;
436 /* Get chain length. */
437 len = ccwchain_calc_length(tic->cda, cp);
438 if (len < 0)
439 return len;
441 /* Need alloc a new chain for this one. */
442 chain = ccwchain_alloc(cp, len);
443 if (!chain)
444 return -ENOMEM;
445 chain->ch_iova = tic->cda;
447 /* Copy the new chain from user. */
448 ret = copy_ccw_from_iova(cp, chain->ch_ccw, tic->cda, len);
449 if (ret) {
450 ccwchain_free(chain);
451 return ret;
454 /* Loop for tics on this new chain. */
455 return ccwchain_loop_tic(chain, cp);
458 /* Loop for TICs. */
459 static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
461 struct ccw1 *tic;
462 int i, ret;
464 for (i = 0; i < chain->ch_len; i++) {
465 tic = chain->ch_ccw + i;
467 if (!ccw_is_tic(tic))
468 continue;
470 ret = ccwchain_handle_tic(tic, cp);
471 if (ret)
472 return ret;
475 return 0;
478 static int ccwchain_fetch_tic(struct ccwchain *chain,
479 int idx,
480 struct channel_program *cp)
482 struct ccw1 *ccw = chain->ch_ccw + idx;
483 struct ccwchain *iter;
484 u32 ccw_head, ccw_tail;
486 list_for_each_entry(iter, &cp->ccwchain_list, next) {
487 ccw_head = iter->ch_iova;
488 ccw_tail = ccw_head + (iter->ch_len - 1) * sizeof(struct ccw1);
490 if ((ccw_head <= ccw->cda) && (ccw->cda <= ccw_tail)) {
491 ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
492 (ccw->cda - ccw_head));
493 return 0;
497 return -EFAULT;
500 static int ccwchain_fetch_direct(struct ccwchain *chain,
501 int idx,
502 struct channel_program *cp)
504 struct ccw1 *ccw;
505 struct pfn_array_table *pat;
506 unsigned long *idaws;
507 int ret;
509 ccw = chain->ch_ccw + idx;
511 if (!ccw->count) {
513 * We just want the translation result of any direct ccw
514 * to be an IDA ccw, so let's add the IDA flag for it.
515 * Although the flag will be ignored by firmware.
517 ccw->flags |= CCW_FLAG_IDA;
518 return 0;
522 * Pin data page(s) in memory.
523 * The number of pages actually is the count of the idaws which will be
524 * needed when translating a direct ccw to a idal ccw.
526 pat = chain->ch_pat + idx;
527 ret = pfn_array_table_init(pat, 1);
528 if (ret)
529 goto out_init;
531 ret = pfn_array_alloc_pin(pat->pat_pa, cp->mdev, ccw->cda, ccw->count);
532 if (ret < 0)
533 goto out_unpin;
535 /* Translate this direct ccw to a idal ccw. */
536 idaws = kcalloc(ret, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
537 if (!idaws) {
538 ret = -ENOMEM;
539 goto out_unpin;
541 ccw->cda = (__u32) virt_to_phys(idaws);
542 ccw->flags |= CCW_FLAG_IDA;
544 pfn_array_table_idal_create_words(pat, idaws);
546 return 0;
548 out_unpin:
549 pfn_array_table_unpin_free(pat, cp->mdev);
550 out_init:
551 ccw->cda = 0;
552 return ret;
555 static int ccwchain_fetch_idal(struct ccwchain *chain,
556 int idx,
557 struct channel_program *cp)
559 struct ccw1 *ccw;
560 struct pfn_array_table *pat;
561 unsigned long *idaws;
562 u64 idaw_iova;
563 unsigned int idaw_nr, idaw_len;
564 int i, ret;
566 ccw = chain->ch_ccw + idx;
568 if (!ccw->count)
569 return 0;
571 /* Calculate size of idaws. */
572 ret = copy_from_iova(cp->mdev, &idaw_iova, ccw->cda, sizeof(idaw_iova));
573 if (ret)
574 return ret;
575 idaw_nr = idal_nr_words((void *)(idaw_iova), ccw->count);
576 idaw_len = idaw_nr * sizeof(*idaws);
578 /* Pin data page(s) in memory. */
579 pat = chain->ch_pat + idx;
580 ret = pfn_array_table_init(pat, idaw_nr);
581 if (ret)
582 goto out_init;
584 /* Translate idal ccw to use new allocated idaws. */
585 idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL);
586 if (!idaws) {
587 ret = -ENOMEM;
588 goto out_unpin;
591 ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idaw_len);
592 if (ret)
593 goto out_free_idaws;
595 ccw->cda = virt_to_phys(idaws);
597 for (i = 0; i < idaw_nr; i++) {
598 idaw_iova = *(idaws + i);
600 ret = pfn_array_alloc_pin(pat->pat_pa + i, cp->mdev,
601 idaw_iova, 1);
602 if (ret < 0)
603 goto out_free_idaws;
606 pfn_array_table_idal_create_words(pat, idaws);
608 return 0;
610 out_free_idaws:
611 kfree(idaws);
612 out_unpin:
613 pfn_array_table_unpin_free(pat, cp->mdev);
614 out_init:
615 ccw->cda = 0;
616 return ret;
620 * Fetch one ccw.
621 * To reduce memory copy, we'll pin the cda page in memory,
622 * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
623 * direct ccws to idal ccws.
625 static int ccwchain_fetch_one(struct ccwchain *chain,
626 int idx,
627 struct channel_program *cp)
629 struct ccw1 *ccw = chain->ch_ccw + idx;
631 if (ccw_is_test(ccw) || ccw_is_noop(ccw))
632 return 0;
634 if (ccw_is_tic(ccw))
635 return ccwchain_fetch_tic(chain, idx, cp);
637 if (ccw_is_idal(ccw))
638 return ccwchain_fetch_idal(chain, idx, cp);
640 return ccwchain_fetch_direct(chain, idx, cp);
644 * cp_init() - allocate ccwchains for a channel program.
645 * @cp: channel_program on which to perform the operation
646 * @mdev: the mediated device to perform pin/unpin operations
647 * @orb: control block for the channel program from the guest
649 * This creates one or more ccwchain(s), and copies the raw data of
650 * the target channel program from @orb->cmd.iova to the new ccwchain(s).
652 * Limitations:
653 * 1. Supports only prefetch enabled mode.
654 * 2. Supports idal(c64) ccw chaining.
655 * 3. Supports 4k idaw.
657 * Returns:
658 * %0 on success and a negative error value on failure.
660 int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
662 u64 iova = orb->cmd.cpa;
663 struct ccwchain *chain;
664 int len, ret;
667 * XXX:
668 * Only support prefetch enable mode now.
670 if (!orb->cmd.pfch)
671 return -EOPNOTSUPP;
673 INIT_LIST_HEAD(&cp->ccwchain_list);
674 memcpy(&cp->orb, orb, sizeof(*orb));
675 cp->mdev = mdev;
677 /* Get chain length. */
678 len = ccwchain_calc_length(iova, cp);
679 if (len < 0)
680 return len;
682 /* Alloc mem for the head chain. */
683 chain = ccwchain_alloc(cp, len);
684 if (!chain)
685 return -ENOMEM;
686 chain->ch_iova = iova;
688 /* Copy the head chain from guest. */
689 ret = copy_ccw_from_iova(cp, chain->ch_ccw, iova, len);
690 if (ret) {
691 ccwchain_free(chain);
692 return ret;
695 /* Now loop for its TICs. */
696 ret = ccwchain_loop_tic(chain, cp);
697 if (ret)
698 cp_unpin_free(cp);
699 /* It is safe to force: if not set but idals used
700 * ccwchain_calc_length returns an error.
702 cp->orb.cmd.c64 = 1;
704 return ret;
709 * cp_free() - free resources for channel program.
710 * @cp: channel_program on which to perform the operation
712 * This unpins the memory pages and frees the memory space occupied by
713 * @cp, which must have been returned by a previous call to cp_init().
714 * Otherwise, undefined behavior occurs.
716 void cp_free(struct channel_program *cp)
718 cp_unpin_free(cp);
722 * cp_prefetch() - translate a guest physical address channel program to
723 * a real-device runnable channel program.
724 * @cp: channel_program on which to perform the operation
726 * This function translates the guest-physical-address channel program
727 * and stores the result to ccwchain list. @cp must have been
728 * initialized by a previous call with cp_init(). Otherwise, undefined
729 * behavior occurs.
730 * For each chain composing the channel program:
731 * - On entry ch_len holds the count of CCWs to be translated.
732 * - On exit ch_len is adjusted to the count of successfully translated CCWs.
733 * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
735 * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
736 * as helpers to do ccw chain translation inside the kernel. Basically
737 * they accept a channel program issued by a virtual machine, and
738 * translate the channel program to a real-device runnable channel
739 * program.
741 * These APIs will copy the ccws into kernel-space buffers, and update
742 * the guest phsical addresses with their corresponding host physical
743 * addresses. Then channel I/O device drivers could issue the
744 * translated channel program to real devices to perform an I/O
745 * operation.
747 * These interfaces are designed to support translation only for
748 * channel programs, which are generated and formatted by a
749 * guest. Thus this will make it possible for things like VFIO to
750 * leverage the interfaces to passthrough a channel I/O mediated
751 * device in QEMU.
753 * We support direct ccw chaining by translating them to idal ccws.
755 * Returns:
756 * %0 on success and a negative error value on failure.
758 int cp_prefetch(struct channel_program *cp)
760 struct ccwchain *chain;
761 int len, idx, ret;
763 list_for_each_entry(chain, &cp->ccwchain_list, next) {
764 len = chain->ch_len;
765 for (idx = 0; idx < len; idx++) {
766 ret = ccwchain_fetch_one(chain, idx, cp);
767 if (ret)
768 goto out_err;
772 return 0;
773 out_err:
774 /* Only cleanup the chain elements that were actually translated. */
775 chain->ch_len = idx;
776 list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
777 chain->ch_len = 0;
779 return ret;
783 * cp_get_orb() - get the orb of the channel program
784 * @cp: channel_program on which to perform the operation
785 * @intparm: new intparm for the returned orb
786 * @lpm: candidate value of the logical-path mask for the returned orb
788 * This function returns the address of the updated orb of the channel
789 * program. Channel I/O device drivers could use this orb to issue a
790 * ssch.
792 union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
794 union orb *orb;
795 struct ccwchain *chain;
796 struct ccw1 *cpa;
798 orb = &cp->orb;
800 orb->cmd.intparm = intparm;
801 orb->cmd.fmt = 1;
802 orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
804 if (orb->cmd.lpm == 0)
805 orb->cmd.lpm = lpm;
807 chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
808 cpa = chain->ch_ccw;
809 orb->cmd.cpa = (__u32) __pa(cpa);
811 return orb;
815 * cp_update_scsw() - update scsw for a channel program.
816 * @cp: channel_program on which to perform the operation
817 * @scsw: I/O results of the channel program and also the target to be
818 * updated
820 * @scsw contains the I/O results of the channel program that pointed
821 * to by @cp. However what @scsw->cpa stores is a host physical
822 * address, which is meaningless for the guest, which is waiting for
823 * the I/O results.
825 * This function updates @scsw->cpa to its coressponding guest physical
826 * address.
828 void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
830 struct ccwchain *chain;
831 u32 cpa = scsw->cmd.cpa;
832 u32 ccw_head, ccw_tail;
835 * LATER:
836 * For now, only update the cmd.cpa part. We may need to deal with
837 * other portions of the schib as well, even if we don't return them
838 * in the ioctl directly. Path status changes etc.
840 list_for_each_entry(chain, &cp->ccwchain_list, next) {
841 ccw_head = (u32)(u64)chain->ch_ccw;
842 ccw_tail = (u32)(u64)(chain->ch_ccw + chain->ch_len - 1);
844 if ((ccw_head <= cpa) && (cpa <= ccw_tail)) {
846 * (cpa - ccw_head) is the offset value of the host
847 * physical ccw to its chain head.
848 * Adding this value to the guest physical ccw chain
849 * head gets us the guest cpa.
851 cpa = chain->ch_iova + (cpa - ccw_head);
852 break;
856 scsw->cmd.cpa = cpa;
860 * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
861 * @cp: channel_program on which to perform the operation
862 * @iova: the iova to check
864 * If the @iova is currently pinned for the ccw chain, return true;
865 * else return false.
867 bool cp_iova_pinned(struct channel_program *cp, u64 iova)
869 struct ccwchain *chain;
870 int i;
872 list_for_each_entry(chain, &cp->ccwchain_list, next) {
873 for (i = 0; i < chain->ch_len; i++)
874 if (pfn_array_table_iova_pinned(chain->ch_pat + i,
875 iova))
876 return true;
879 return false;