1 // SPDX-License-Identifier: GPL-2.0
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>
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
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. */
36 struct pfn_array_table
{
37 struct pfn_array
*pat_pa
;
42 struct list_head next
;
44 /* Guest physical address of the current chain. */
46 /* Count of the valid ccws in chain. */
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.
62 * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in
63 * this structure will be filled in by this function.
66 * Number of pages pinned on success.
67 * If @pa->pa_nr is not 0, or @pa->pa_iova_pfn is not NULL initially,
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
)
79 if (pa
->pa_nr
|| pa
->pa_iova_pfn
)
84 pa
->pa_nr
= ((iova
& ~PAGE_MASK
) + len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
88 pa
->pa_iova_pfn
= kcalloc(pa
->pa_nr
,
89 sizeof(*pa
->pa_iova_pfn
) +
92 if (unlikely(!pa
->pa_iova_pfn
))
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
);
105 } else if (ret
> 0 && ret
!= pa
->pa_nr
) {
106 vfio_unpin_pages(mdev
, pa
->pa_iova_pfn
, ret
);
115 kfree(pa
->pa_iova_pfn
);
116 pa
->pa_iova_pfn
= NULL
;
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
);
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
))) {
142 static void pfn_array_table_unpin_free(struct pfn_array_table
*pat
,
147 for (i
= 0; i
< pat
->pat_nr
; i
++)
148 pfn_array_unpin_free(pat
->pat_pa
+ i
, mdev
);
157 static bool pfn_array_table_iova_pinned(struct pfn_array_table
*pat
,
160 struct pfn_array
*pa
= pat
->pat_pa
;
161 unsigned long iova_pfn
= iova
>> PAGE_SHIFT
;
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
[i
] == iova_pfn
)
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
;
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
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
;
192 idaws
[k
] += pa
->pa_iova
& (PAGE_SIZE
- 1);
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
,
207 struct pfn_array pa
= {0};
212 ret
= pfn_array_alloc_pin(&pa
, mdev
, iova
, n
);
217 for (i
= 0; i
< pa
.pa_nr
; i
++) {
218 from
= pa
.pa_pfn
[i
] << PAGE_SHIFT
;
221 from
+= iova
& (PAGE_SIZE
- 1);
222 m
-= iova
& (PAGE_SIZE
- 1);
226 memcpy(to
+ (n
- l
), (void *)from
, m
);
233 pfn_array_unpin_free(&pa
, mdev
);
238 static long copy_ccw_from_iova(struct channel_program
*cp
,
239 struct ccw1
*to
, u64 iova
,
247 ret
= copy_from_iova(cp
->mdev
, to
, iova
, len
* sizeof(struct ccw1
));
251 if (!cp
->orb
.cmd
.fmt
) {
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
;
260 pccw1
->cmd_code
= ccw0
.cmd_code
;
261 pccw1
->flags
= ccw0
.flags
;
262 pccw1
->count
= ccw0
.count
;
264 pccw1
->cda
= ccw0
.cda
;
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
;
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
);
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
;
308 list_add_tail(&chain
->next
, &cp
->ccwchain_list
);
313 static void ccwchain_free(struct ccwchain
*chain
)
315 list_del(&chain
->next
);
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
))
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
;
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
,
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
;
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
);
375 cnt
= copy_ccw_from_iova(cp
, ccw
, iova
, CCWCHAIN_LEN_MAX
);
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
))
393 if ((!ccw_is_chain(ccw
)) && (!ccw_is_tic(ccw
)))
397 } while (cnt
< CCWCHAIN_LEN_MAX
+ 1);
399 if (cnt
== CCWCHAIN_LEN_MAX
+ 1)
406 static int tic_target_chain_exists(struct ccw1
*tic
, struct channel_program
*cp
)
408 struct ccwchain
*chain
;
409 u32 ccw_head
, ccw_tail
;
411 list_for_each_entry(chain
, &cp
->ccwchain_list
, next
) {
412 ccw_head
= chain
->ch_iova
;
413 ccw_tail
= ccw_head
+ (chain
->ch_len
- 1) * sizeof(struct ccw1
);
415 if ((ccw_head
<= tic
->cda
) && (tic
->cda
<= ccw_tail
))
422 static int ccwchain_loop_tic(struct ccwchain
*chain
,
423 struct channel_program
*cp
);
425 static int ccwchain_handle_tic(struct ccw1
*tic
, struct channel_program
*cp
)
427 struct ccwchain
*chain
;
430 /* May transfer to an existing chain. */
431 if (tic_target_chain_exists(tic
, cp
))
434 /* Get chain length. */
435 len
= ccwchain_calc_length(tic
->cda
, cp
);
439 /* Need alloc a new chain for this one. */
440 chain
= ccwchain_alloc(cp
, len
);
443 chain
->ch_iova
= tic
->cda
;
445 /* Copy the new chain from user. */
446 ret
= copy_ccw_from_iova(cp
, chain
->ch_ccw
, tic
->cda
, len
);
448 ccwchain_free(chain
);
452 /* Loop for tics on this new chain. */
453 return ccwchain_loop_tic(chain
, cp
);
457 static int ccwchain_loop_tic(struct ccwchain
*chain
, struct channel_program
*cp
)
462 for (i
= 0; i
< chain
->ch_len
; i
++) {
463 tic
= chain
->ch_ccw
+ i
;
465 if (!ccw_is_tic(tic
))
468 ret
= ccwchain_handle_tic(tic
, cp
);
476 static int ccwchain_fetch_tic(struct ccwchain
*chain
,
478 struct channel_program
*cp
)
480 struct ccw1
*ccw
= chain
->ch_ccw
+ idx
;
481 struct ccwchain
*iter
;
482 u32 ccw_head
, ccw_tail
;
484 list_for_each_entry(iter
, &cp
->ccwchain_list
, next
) {
485 ccw_head
= iter
->ch_iova
;
486 ccw_tail
= ccw_head
+ (iter
->ch_len
- 1) * sizeof(struct ccw1
);
488 if ((ccw_head
<= ccw
->cda
) && (ccw
->cda
<= ccw_tail
)) {
489 ccw
->cda
= (__u32
) (addr_t
) (((char *)iter
->ch_ccw
) +
490 (ccw
->cda
- ccw_head
));
498 static int ccwchain_fetch_direct(struct ccwchain
*chain
,
500 struct channel_program
*cp
)
503 struct pfn_array_table
*pat
;
504 unsigned long *idaws
;
507 ccw
= chain
->ch_ccw
+ idx
;
511 * We just want the translation result of any direct ccw
512 * to be an IDA ccw, so let's add the IDA flag for it.
513 * Although the flag will be ignored by firmware.
515 ccw
->flags
|= CCW_FLAG_IDA
;
520 * Pin data page(s) in memory.
521 * The number of pages actually is the count of the idaws which will be
522 * needed when translating a direct ccw to a idal ccw.
524 pat
= chain
->ch_pat
+ idx
;
525 ret
= pfn_array_table_init(pat
, 1);
529 ret
= pfn_array_alloc_pin(pat
->pat_pa
, cp
->mdev
, ccw
->cda
, ccw
->count
);
533 /* Translate this direct ccw to a idal ccw. */
534 idaws
= kcalloc(ret
, sizeof(*idaws
), GFP_DMA
| GFP_KERNEL
);
539 ccw
->cda
= (__u32
) virt_to_phys(idaws
);
540 ccw
->flags
|= CCW_FLAG_IDA
;
542 pfn_array_table_idal_create_words(pat
, idaws
);
547 pfn_array_table_unpin_free(pat
, cp
->mdev
);
553 static int ccwchain_fetch_idal(struct ccwchain
*chain
,
555 struct channel_program
*cp
)
558 struct pfn_array_table
*pat
;
559 unsigned long *idaws
;
561 unsigned int idaw_nr
, idaw_len
;
564 ccw
= chain
->ch_ccw
+ idx
;
569 /* Calculate size of idaws. */
570 ret
= copy_from_iova(cp
->mdev
, &idaw_iova
, ccw
->cda
, sizeof(idaw_iova
));
573 idaw_nr
= idal_nr_words((void *)(idaw_iova
), ccw
->count
);
574 idaw_len
= idaw_nr
* sizeof(*idaws
);
576 /* Pin data page(s) in memory. */
577 pat
= chain
->ch_pat
+ idx
;
578 ret
= pfn_array_table_init(pat
, idaw_nr
);
582 /* Translate idal ccw to use new allocated idaws. */
583 idaws
= kzalloc(idaw_len
, GFP_DMA
| GFP_KERNEL
);
589 ret
= copy_from_iova(cp
->mdev
, idaws
, ccw
->cda
, idaw_len
);
593 ccw
->cda
= virt_to_phys(idaws
);
595 for (i
= 0; i
< idaw_nr
; i
++) {
596 idaw_iova
= *(idaws
+ i
);
598 ret
= pfn_array_alloc_pin(pat
->pat_pa
+ i
, cp
->mdev
,
604 pfn_array_table_idal_create_words(pat
, idaws
);
611 pfn_array_table_unpin_free(pat
, cp
->mdev
);
619 * To reduce memory copy, we'll pin the cda page in memory,
620 * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
621 * direct ccws to idal ccws.
623 static int ccwchain_fetch_one(struct ccwchain
*chain
,
625 struct channel_program
*cp
)
627 struct ccw1
*ccw
= chain
->ch_ccw
+ idx
;
629 if (ccw_is_test(ccw
) || ccw_is_noop(ccw
))
633 return ccwchain_fetch_tic(chain
, idx
, cp
);
635 if (ccw_is_idal(ccw
))
636 return ccwchain_fetch_idal(chain
, idx
, cp
);
638 return ccwchain_fetch_direct(chain
, idx
, cp
);
642 * cp_init() - allocate ccwchains for a channel program.
643 * @cp: channel_program on which to perform the operation
644 * @mdev: the mediated device to perform pin/unpin operations
645 * @orb: control block for the channel program from the guest
647 * This creates one or more ccwchain(s), and copies the raw data of
648 * the target channel program from @orb->cmd.iova to the new ccwchain(s).
651 * 1. Supports only prefetch enabled mode.
652 * 2. Supports idal(c64) ccw chaining.
653 * 3. Supports 4k idaw.
656 * %0 on success and a negative error value on failure.
658 int cp_init(struct channel_program
*cp
, struct device
*mdev
, union orb
*orb
)
660 u64 iova
= orb
->cmd
.cpa
;
661 struct ccwchain
*chain
;
666 * Only support prefetch enable mode now.
671 INIT_LIST_HEAD(&cp
->ccwchain_list
);
672 memcpy(&cp
->orb
, orb
, sizeof(*orb
));
675 /* Get chain length. */
676 len
= ccwchain_calc_length(iova
, cp
);
680 /* Alloc mem for the head chain. */
681 chain
= ccwchain_alloc(cp
, len
);
684 chain
->ch_iova
= iova
;
686 /* Copy the head chain from guest. */
687 ret
= copy_ccw_from_iova(cp
, chain
->ch_ccw
, iova
, len
);
689 ccwchain_free(chain
);
693 /* Now loop for its TICs. */
694 ret
= ccwchain_loop_tic(chain
, cp
);
697 /* It is safe to force: if not set but idals used
698 * ccwchain_calc_length returns an error.
707 * cp_free() - free resources for channel program.
708 * @cp: channel_program on which to perform the operation
710 * This unpins the memory pages and frees the memory space occupied by
711 * @cp, which must have been returned by a previous call to cp_init().
712 * Otherwise, undefined behavior occurs.
714 void cp_free(struct channel_program
*cp
)
720 * cp_prefetch() - translate a guest physical address channel program to
721 * a real-device runnable channel program.
722 * @cp: channel_program on which to perform the operation
724 * This function translates the guest-physical-address channel program
725 * and stores the result to ccwchain list. @cp must have been
726 * initialized by a previous call with cp_init(). Otherwise, undefined
728 * For each chain composing the channel program:
729 * - On entry ch_len holds the count of CCWs to be translated.
730 * - On exit ch_len is adjusted to the count of successfully translated CCWs.
731 * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
733 * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
734 * as helpers to do ccw chain translation inside the kernel. Basically
735 * they accept a channel program issued by a virtual machine, and
736 * translate the channel program to a real-device runnable channel
739 * These APIs will copy the ccws into kernel-space buffers, and update
740 * the guest phsical addresses with their corresponding host physical
741 * addresses. Then channel I/O device drivers could issue the
742 * translated channel program to real devices to perform an I/O
745 * These interfaces are designed to support translation only for
746 * channel programs, which are generated and formatted by a
747 * guest. Thus this will make it possible for things like VFIO to
748 * leverage the interfaces to passthrough a channel I/O mediated
751 * We support direct ccw chaining by translating them to idal ccws.
754 * %0 on success and a negative error value on failure.
756 int cp_prefetch(struct channel_program
*cp
)
758 struct ccwchain
*chain
;
761 list_for_each_entry(chain
, &cp
->ccwchain_list
, next
) {
763 for (idx
= 0; idx
< len
; idx
++) {
764 ret
= ccwchain_fetch_one(chain
, idx
, cp
);
772 /* Only cleanup the chain elements that were actually translated. */
774 list_for_each_entry_continue(chain
, &cp
->ccwchain_list
, next
) {
781 * cp_get_orb() - get the orb of the channel program
782 * @cp: channel_program on which to perform the operation
783 * @intparm: new intparm for the returned orb
784 * @lpm: candidate value of the logical-path mask for the returned orb
786 * This function returns the address of the updated orb of the channel
787 * program. Channel I/O device drivers could use this orb to issue a
790 union orb
*cp_get_orb(struct channel_program
*cp
, u32 intparm
, u8 lpm
)
793 struct ccwchain
*chain
;
798 orb
->cmd
.intparm
= intparm
;
800 orb
->cmd
.key
= PAGE_DEFAULT_KEY
>> 4;
802 if (orb
->cmd
.lpm
== 0)
805 chain
= list_first_entry(&cp
->ccwchain_list
, struct ccwchain
, next
);
807 orb
->cmd
.cpa
= (__u32
) __pa(cpa
);
813 * cp_update_scsw() - update scsw for a channel program.
814 * @cp: channel_program on which to perform the operation
815 * @scsw: I/O results of the channel program and also the target to be
818 * @scsw contains the I/O results of the channel program that pointed
819 * to by @cp. However what @scsw->cpa stores is a host physical
820 * address, which is meaningless for the guest, which is waiting for
823 * This function updates @scsw->cpa to its coressponding guest physical
826 void cp_update_scsw(struct channel_program
*cp
, union scsw
*scsw
)
828 struct ccwchain
*chain
;
829 u32 cpa
= scsw
->cmd
.cpa
;
830 u32 ccw_head
, ccw_tail
;
834 * For now, only update the cmd.cpa part. We may need to deal with
835 * other portions of the schib as well, even if we don't return them
836 * in the ioctl directly. Path status changes etc.
838 list_for_each_entry(chain
, &cp
->ccwchain_list
, next
) {
839 ccw_head
= (u32
)(u64
)chain
->ch_ccw
;
840 ccw_tail
= (u32
)(u64
)(chain
->ch_ccw
+ chain
->ch_len
- 1);
842 if ((ccw_head
<= cpa
) && (cpa
<= ccw_tail
)) {
844 * (cpa - ccw_head) is the offset value of the host
845 * physical ccw to its chain head.
846 * Adding this value to the guest physical ccw chain
847 * head gets us the guest cpa.
849 cpa
= chain
->ch_iova
+ (cpa
- ccw_head
);
858 * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
859 * @cp: channel_program on which to perform the operation
860 * @iova: the iova to check
862 * If the @iova is currently pinned for the ccw chain, return true;
865 bool cp_iova_pinned(struct channel_program
*cp
, u64 iova
)
867 struct ccwchain
*chain
;
870 list_for_each_entry(chain
, &cp
->ccwchain_list
, next
) {
871 for (i
= 0; i
< chain
->ch_len
; i
++)
872 if (pfn_array_table_iova_pinned(chain
->ch_pat
+ i
,