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 unsigned long pa_iova;
27 unsigned long *pa_iova_pfn;
28 unsigned long *pa_pfn;
32 struct pfn_array_table {
33 struct pfn_array *pat_pa;
38 struct list_head next;
40 /* Guest physical address of the current chain. */
42 /* Count of the valid ccws in chain. */
44 /* Pinned PAGEs for the original data. */
45 struct pfn_array_table *ch_pat;
49 * pfn_array_pin() - pin user pages in memory
50 * @pa: pfn_array on which to perform the operation
51 * @mdev: the mediated device to perform pin/unpin operations
53 * Attempt to pin user pages in memory.
56 * @pa->pa_iova starting guest physical I/O address. Assigned by caller.
57 * @pa->pa_iova_pfn array that stores PFNs of the pages need to pin. Allocated
59 * @pa->pa_pfn array that receives PFNs of the pages pinned. Allocated by
61 * @pa->pa_nr number of pages from @pa->pa_iova to pin. Assigned by
63 * number of pages pinned. Assigned by callee.
66 * Number of pages pinned on success.
67 * If @pa->pa_nr is 0 or negative, returns 0.
68 * If no pages were pinned, returns -errno.
70 static int pfn_array_pin(struct pfn_array *pa, struct device *mdev)
79 pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
80 for (i = 1; i < pa->pa_nr; i++)
81 pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
83 ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
84 IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
86 if (ret > 0 && ret != pa->pa_nr) {
87 vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret);
95 /* Unpin the pages before releasing the memory. */
96 static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
98 vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
100 kfree(pa->pa_iova_pfn);
103 /* Alloc memory for PFNs, then pin pages with them. */
104 static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev,
105 u64 iova, unsigned int len)
109 if (!len || pa->pa_nr)
114 pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
118 pa->pa_iova_pfn = kcalloc(pa->pa_nr,
119 sizeof(*pa->pa_iova_pfn) +
122 if (unlikely(!pa->pa_iova_pfn)) {
126 pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
128 ret = pfn_array_pin(pa, mdev);
135 kfree(pa->pa_iova_pfn);
140 static int pfn_array_table_init(struct pfn_array_table *pat, int nr)
142 pat->pat_pa = kcalloc(nr, sizeof(*pat->pat_pa), GFP_KERNEL);
143 if (unlikely(ZERO_OR_NULL_PTR(pat->pat_pa))) {
153 static void pfn_array_table_unpin_free(struct pfn_array_table *pat,
158 for (i = 0; i < pat->pat_nr; i++)
159 pfn_array_unpin_free(pat->pat_pa + i, mdev);
168 static bool pfn_array_table_iova_pinned(struct pfn_array_table *pat,
171 struct pfn_array *pa = pat->pat_pa;
172 unsigned long iova_pfn = iova >> PAGE_SHIFT;
175 for (i = 0; i < pat->pat_nr; i++, pa++)
176 for (j = 0; j < pa->pa_nr; j++)
177 if (pa->pa_iova_pfn[j] == iova_pfn)
182 /* Create the list idal words for a pfn_array_table. */
183 static inline void pfn_array_table_idal_create_words(
184 struct pfn_array_table *pat,
185 unsigned long *idaws)
187 struct pfn_array *pa;
191 * Idal words (execept the first one) rely on the memory being 4k
192 * aligned. If a user virtual address is 4K aligned, then it's
193 * corresponding kernel physical address will also be 4K aligned. Thus
194 * there will be no problem here to simply use the phys to create an
198 for (i = 0; i < pat->pat_nr; i++) {
199 pa = pat->pat_pa + i;
200 for (j = 0; j < pa->pa_nr; j++) {
201 idaws[k] = pa->pa_pfn[j] << PAGE_SHIFT;
203 idaws[k] += pa->pa_iova & (PAGE_SIZE - 1);
211 * Within the domain (@mdev), copy @n bytes from a guest physical
212 * address (@iova) to a host physical address (@to).
214 static long copy_from_iova(struct device *mdev,
218 struct pfn_array pa = {0};
223 ret = pfn_array_alloc_pin(&pa, mdev, iova, n);
228 for (i = 0; i < pa.pa_nr; i++) {
229 from = pa.pa_pfn[i] << PAGE_SHIFT;
232 from += iova & (PAGE_SIZE - 1);
233 m -= iova & (PAGE_SIZE - 1);
237 memcpy(to + (n - l), (void *)from, m);
244 pfn_array_unpin_free(&pa, mdev);
249 static long copy_ccw_from_iova(struct channel_program *cp,
250 struct ccw1 *to, u64 iova,
258 ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1));
262 if (!cp->orb.cmd.fmt) {
264 for (i = 0; i < len; i++) {
265 ccw0 = *(struct ccw0 *)pccw1;
266 if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
267 pccw1->cmd_code = CCW_CMD_TIC;
271 pccw1->cmd_code = ccw0.cmd_code;
272 pccw1->flags = ccw0.flags;
273 pccw1->count = ccw0.count;
275 pccw1->cda = ccw0.cda;
284 * Helpers to operate ccwchain.
286 #define ccw_is_test(_ccw) (((_ccw)->cmd_code & 0x0F) == 0)
288 #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
290 #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
292 #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
295 #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
297 static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
299 struct ccwchain *chain;
303 /* Make ccw address aligned to 8. */
304 size = ((sizeof(*chain) + 7L) & -8L) +
305 sizeof(*chain->ch_ccw) * len +
306 sizeof(*chain->ch_pat) * len;
307 chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
311 data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
312 chain->ch_ccw = (struct ccw1 *)data;
314 data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
315 chain->ch_pat = (struct pfn_array_table *)data;
319 list_add_tail(&chain->next, &cp->ccwchain_list);
324 static void ccwchain_free(struct ccwchain *chain)
326 list_del(&chain->next);
330 /* Free resource for a ccw that allocated memory for its cda. */
331 static void ccwchain_cda_free(struct ccwchain *chain, int idx)
333 struct ccw1 *ccw = chain->ch_ccw + idx;
335 if (ccw_is_test(ccw) || ccw_is_noop(ccw) || ccw_is_tic(ccw))
340 kfree((void *)(u64)ccw->cda);
343 /* Unpin the pages then free the memory resources. */
344 static void cp_unpin_free(struct channel_program *cp)
346 struct ccwchain *chain, *temp;
349 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
350 for (i = 0; i < chain->ch_len; i++) {
351 pfn_array_table_unpin_free(chain->ch_pat + i,
353 ccwchain_cda_free(chain, i);
355 ccwchain_free(chain);
360 * ccwchain_calc_length - calculate the length of the ccw chain.
361 * @iova: guest physical address of the target ccw chain
362 * @cp: channel_program on which to perform the operation
364 * This is the chain length not considering any TICs.
365 * You need to do a new round for each TIC target.
367 * Returns: the length of the ccw chain or -errno.
369 static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
371 struct ccw1 *ccw, *p;
375 * Copy current chain from guest to host kernel.
376 * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256).
377 * So copying 2K is enough (safe).
379 p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL);
383 cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX);
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;
510 * Pin data page(s) in memory.
511 * The number of pages actually is the count of the idaws which will be
512 * needed when translating a direct ccw to a idal ccw.
514 pat = chain->ch_pat + idx;
515 if (pfn_array_table_init(pat, 1))
517 idaw_nr = pfn_array_alloc_pin(pat->pat_pa, cp->mdev,
518 ccw->cda, ccw->count);
522 /* Translate this direct ccw to a idal ccw. */
523 idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
525 pfn_array_table_unpin_free(pat, cp->mdev);
528 ccw->cda = (__u32) virt_to_phys(idaws);
529 ccw->flags |= CCW_FLAG_IDA;
531 pfn_array_table_idal_create_words(pat, idaws);
536 static int ccwchain_fetch_idal(struct ccwchain *chain,
538 struct channel_program *cp)
541 struct pfn_array_table *pat;
542 unsigned long *idaws;
544 unsigned int idaw_nr, idaw_len;
547 ccw = chain->ch_ccw + idx;
549 /* Calculate size of idaws. */
550 ret = copy_from_iova(cp->mdev, &idaw_iova, ccw->cda, sizeof(idaw_iova));
553 idaw_nr = idal_nr_words((void *)(idaw_iova), ccw->count);
554 idaw_len = idaw_nr * sizeof(*idaws);
556 /* Pin data page(s) in memory. */
557 pat = chain->ch_pat + idx;
558 ret = pfn_array_table_init(pat, idaw_nr);
562 /* Translate idal ccw to use new allocated idaws. */
563 idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL);
569 ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idaw_len);
573 ccw->cda = virt_to_phys(idaws);
575 for (i = 0; i < idaw_nr; i++) {
576 idaw_iova = *(idaws + i);
577 if (IS_ERR_VALUE(idaw_iova)) {
582 ret = pfn_array_alloc_pin(pat->pat_pa + i, cp->mdev,
588 pfn_array_table_idal_create_words(pat, idaws);
595 pfn_array_table_unpin_free(pat, cp->mdev);
601 * To reduce memory copy, we'll pin the cda page in memory,
602 * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
603 * direct ccws to idal ccws.
605 static int ccwchain_fetch_one(struct ccwchain *chain,
607 struct channel_program *cp)
609 struct ccw1 *ccw = chain->ch_ccw + idx;
611 if (ccw_is_test(ccw) || ccw_is_noop(ccw))
615 return ccwchain_fetch_tic(chain, idx, cp);
617 if (ccw_is_idal(ccw))
618 return ccwchain_fetch_idal(chain, idx, cp);
620 return ccwchain_fetch_direct(chain, idx, cp);
624 * cp_init() - allocate ccwchains for a channel program.
625 * @cp: channel_program on which to perform the operation
626 * @mdev: the mediated device to perform pin/unpin operations
627 * @orb: control block for the channel program from the guest
629 * This creates one or more ccwchain(s), and copies the raw data of
630 * the target channel program from @orb->cmd.iova to the new ccwchain(s).
633 * 1. Supports only prefetch enabled mode.
634 * 2. Supports idal(c64) ccw chaining.
635 * 3. Supports 4k idaw.
638 * %0 on success and a negative error value on failure.
640 int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
642 u64 iova = orb->cmd.cpa;
643 struct ccwchain *chain;
648 * Only support prefetch enable mode now.
649 * Only support 64bit addressing idal.
650 * Only support 4k IDAW.
652 if (!orb->cmd.pfch || !orb->cmd.c64 || orb->cmd.i2k)
655 INIT_LIST_HEAD(&cp->ccwchain_list);
656 memcpy(&cp->orb, orb, sizeof(*orb));
659 /* Get chain length. */
660 len = ccwchain_calc_length(iova, cp);
664 /* Alloc mem for the head chain. */
665 chain = ccwchain_alloc(cp, len);
668 chain->ch_iova = iova;
670 /* Copy the head chain from guest. */
671 ret = copy_ccw_from_iova(cp, chain->ch_ccw, iova, len);
673 ccwchain_free(chain);
677 /* Now loop for its TICs. */
678 ret = ccwchain_loop_tic(chain, cp);
687 * cp_free() - free resources for channel program.
688 * @cp: channel_program on which to perform the operation
690 * This unpins the memory pages and frees the memory space occupied by
691 * @cp, which must have been returned by a previous call to cp_init().
692 * Otherwise, undefined behavior occurs.
694 void cp_free(struct channel_program *cp)
700 * cp_prefetch() - translate a guest physical address channel program to
701 * a real-device runnable channel program.
702 * @cp: channel_program on which to perform the operation
704 * This function translates the guest-physical-address channel program
705 * and stores the result to ccwchain list. @cp must have been
706 * initialized by a previous call with cp_init(). Otherwise, undefined
708 * For each chain composing the channel program:
709 * - On entry ch_len holds the count of CCWs to be translated.
710 * - On exit ch_len is adjusted to the count of successfully translated CCWs.
711 * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
713 * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
714 * as helpers to do ccw chain translation inside the kernel. Basically
715 * they accept a channel program issued by a virtual machine, and
716 * translate the channel program to a real-device runnable channel
719 * These APIs will copy the ccws into kernel-space buffers, and update
720 * the guest phsical addresses with their corresponding host physical
721 * addresses. Then channel I/O device drivers could issue the
722 * translated channel program to real devices to perform an I/O
725 * These interfaces are designed to support translation only for
726 * channel programs, which are generated and formatted by a
727 * guest. Thus this will make it possible for things like VFIO to
728 * leverage the interfaces to passthrough a channel I/O mediated
731 * We support direct ccw chaining by translating them to idal ccws.
734 * %0 on success and a negative error value on failure.
736 int cp_prefetch(struct channel_program *cp)
738 struct ccwchain *chain;
741 list_for_each_entry(chain, &cp->ccwchain_list, next) {
743 for (idx = 0; idx < len; idx++) {
744 ret = ccwchain_fetch_one(chain, idx, cp);
752 /* Only cleanup the chain elements that were actually translated. */
754 list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
761 * cp_get_orb() - get the orb of the channel program
762 * @cp: channel_program on which to perform the operation
763 * @intparm: new intparm for the returned orb
764 * @lpm: candidate value of the logical-path mask for the returned orb
766 * This function returns the address of the updated orb of the channel
767 * program. Channel I/O device drivers could use this orb to issue a
770 union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
773 struct ccwchain *chain;
778 orb->cmd.intparm = intparm;
780 orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
782 if (orb->cmd.lpm == 0)
785 chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
787 orb->cmd.cpa = (__u32) __pa(cpa);
793 * cp_update_scsw() - update scsw for a channel program.
794 * @cp: channel_program on which to perform the operation
795 * @scsw: I/O results of the channel program and also the target to be
798 * @scsw contains the I/O results of the channel program that pointed
799 * to by @cp. However what @scsw->cpa stores is a host physical
800 * address, which is meaningless for the guest, which is waiting for
803 * This function updates @scsw->cpa to its coressponding guest physical
806 void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
808 struct ccwchain *chain;
809 u32 cpa = scsw->cmd.cpa;
810 u32 ccw_head, ccw_tail;
814 * For now, only update the cmd.cpa part. We may need to deal with
815 * other portions of the schib as well, even if we don't return them
816 * in the ioctl directly. Path status changes etc.
818 list_for_each_entry(chain, &cp->ccwchain_list, next) {
819 ccw_head = (u32)(u64)chain->ch_ccw;
820 ccw_tail = (u32)(u64)(chain->ch_ccw + chain->ch_len - 1);
822 if ((ccw_head <= cpa) && (cpa <= ccw_tail)) {
824 * (cpa - ccw_head) is the offset value of the host
825 * physical ccw to its chain head.
826 * Adding this value to the guest physical ccw chain
827 * head gets us the guest cpa.
829 cpa = chain->ch_iova + (cpa - ccw_head);
838 * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
839 * @cmd: ccwchain command on which to perform the operation
840 * @iova: the iova to check
842 * If the @iova is currently pinned for the ccw chain, return true;
845 bool cp_iova_pinned(struct channel_program *cp, u64 iova)
847 struct ccwchain *chain;
850 list_for_each_entry(chain, &cp->ccwchain_list, next) {
851 for (i = 0; i < chain->ch_len; i++)
852 if (pfn_array_table_iova_pinned(chain->ch_pat + i,