2 * channel program interfaces
4 * Copyright IBM Corp. 2017
6 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
7 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
11 #include <linux/slab.h>
12 #include <linux/iommu.h>
13 #include <linux/vfio.h>
14 #include <asm/idals.h>
16 #include "vfio_ccw_cp.h"
19 * Max length for ccw chain.
20 * XXX: Limit to 256, need to check more?
22 #define CCWCHAIN_LEN_MAX 256
25 unsigned long pa_iova;
26 unsigned long *pa_iova_pfn;
27 unsigned long *pa_pfn;
31 struct pfn_array_table {
32 struct pfn_array *pat_pa;
37 struct list_head next;
39 /* Guest physical address of the current chain. */
41 /* Count of the valid ccws in chain. */
43 /* Pinned PAGEs for the original data. */
44 struct pfn_array_table *ch_pat;
48 * pfn_array_pin() - pin user pages in memory
49 * @pa: pfn_array on which to perform the operation
50 * @mdev: the mediated device to perform pin/unpin operations
52 * Attempt to pin user pages in memory.
55 * @pa->pa_iova starting guest physical I/O address. Assigned by caller.
56 * @pa->pa_iova_pfn array that stores PFNs of the pages need to pin. Allocated
58 * @pa->pa_pfn array that receives PFNs of the pages pinned. Allocated by
60 * @pa->pa_nr number of pages from @pa->pa_iova to pin. Assigned by
62 * number of pages pinned. Assigned by callee.
65 * Number of pages pinned on success.
66 * If @pa->pa_nr is 0 or negative, returns 0.
67 * If no pages were pinned, returns -errno.
69 static int pfn_array_pin(struct pfn_array *pa, struct device *mdev)
78 pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
79 for (i = 1; i < pa->pa_nr; i++)
80 pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
82 ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
83 IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
85 if (ret > 0 && ret != pa->pa_nr) {
86 vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret);
94 /* Unpin the pages before releasing the memory. */
95 static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
97 vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
99 kfree(pa->pa_iova_pfn);
102 /* Alloc memory for PFNs, then pin pages with them. */
103 static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev,
104 u64 iova, unsigned int len)
108 if (!len || pa->pa_nr)
113 pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
117 pa->pa_iova_pfn = kcalloc(pa->pa_nr,
118 sizeof(*pa->pa_iova_pfn) +
121 if (unlikely(!pa->pa_iova_pfn))
123 pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
125 ret = pfn_array_pin(pa, mdev);
132 kfree(pa->pa_iova_pfn);
137 static int pfn_array_table_init(struct pfn_array_table *pat, int nr)
139 pat->pat_pa = kcalloc(nr, sizeof(*pat->pat_pa), GFP_KERNEL);
140 if (unlikely(ZERO_OR_NULL_PTR(pat->pat_pa))) {
150 static void pfn_array_table_unpin_free(struct pfn_array_table *pat,
155 for (i = 0; i < pat->pat_nr; i++)
156 pfn_array_unpin_free(pat->pat_pa + i, mdev);
165 static bool pfn_array_table_iova_pinned(struct pfn_array_table *pat,
168 struct pfn_array *pa = pat->pat_pa;
169 unsigned long iova_pfn = iova >> PAGE_SHIFT;
172 for (i = 0; i < pat->pat_nr; i++, pa++)
173 for (j = 0; j < pa->pa_nr; j++)
174 if (pa->pa_iova_pfn[i] == iova_pfn)
179 /* Create the list idal words for a pfn_array_table. */
180 static inline void pfn_array_table_idal_create_words(
181 struct pfn_array_table *pat,
182 unsigned long *idaws)
184 struct pfn_array *pa;
188 * Idal words (execept the first one) rely on the memory being 4k
189 * aligned. If a user virtual address is 4K aligned, then it's
190 * corresponding kernel physical address will also be 4K aligned. Thus
191 * there will be no problem here to simply use the phys to create an
195 for (i = 0; i < pat->pat_nr; i++) {
196 pa = pat->pat_pa + i;
197 for (j = 0; j < pa->pa_nr; j++) {
198 idaws[k] = pa->pa_pfn[j] << PAGE_SHIFT;
200 idaws[k] += pa->pa_iova & (PAGE_SIZE - 1);
208 * Within the domain (@mdev), copy @n bytes from a guest physical
209 * address (@iova) to a host physical address (@to).
211 static long copy_from_iova(struct device *mdev,
215 struct pfn_array pa = {0};
220 ret = pfn_array_alloc_pin(&pa, mdev, iova, n);
225 for (i = 0; i < pa.pa_nr; i++) {
226 from = pa.pa_pfn[i] << PAGE_SHIFT;
229 from += iova & (PAGE_SIZE - 1);
230 m -= iova & (PAGE_SIZE - 1);
234 memcpy(to + (n - l), (void *)from, m);
241 pfn_array_unpin_free(&pa, mdev);
246 static long copy_ccw_from_iova(struct channel_program *cp,
247 struct ccw1 *to, u64 iova,
255 ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1));
259 if (!cp->orb.cmd.fmt) {
261 for (i = 0; i < len; i++) {
262 ccw0 = *(struct ccw0 *)pccw1;
263 if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
264 pccw1->cmd_code = CCW_CMD_TIC;
268 pccw1->cmd_code = ccw0.cmd_code;
269 pccw1->flags = ccw0.flags;
270 pccw1->count = ccw0.count;
272 pccw1->cda = ccw0.cda;
281 * Helpers to operate ccwchain.
283 #define ccw_is_test(_ccw) (((_ccw)->cmd_code & 0x0F) == 0)
285 #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
287 #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
289 #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
292 #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
294 static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
296 struct ccwchain *chain;
300 /* Make ccw address aligned to 8. */
301 size = ((sizeof(*chain) + 7L) & -8L) +
302 sizeof(*chain->ch_ccw) * len +
303 sizeof(*chain->ch_pat) * len;
304 chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
308 data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
309 chain->ch_ccw = (struct ccw1 *)data;
311 data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
312 chain->ch_pat = (struct pfn_array_table *)data;
316 list_add_tail(&chain->next, &cp->ccwchain_list);
321 static void ccwchain_free(struct ccwchain *chain)
323 list_del(&chain->next);
327 /* Free resource for a ccw that allocated memory for its cda. */
328 static void ccwchain_cda_free(struct ccwchain *chain, int idx)
330 struct ccw1 *ccw = chain->ch_ccw + idx;
335 kfree((void *)(u64)ccw->cda);
338 /* Unpin the pages then free the memory resources. */
339 static void cp_unpin_free(struct channel_program *cp)
341 struct ccwchain *chain, *temp;
344 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
345 for (i = 0; i < chain->ch_len; i++) {
346 pfn_array_table_unpin_free(chain->ch_pat + i,
348 ccwchain_cda_free(chain, i);
350 ccwchain_free(chain);
355 * ccwchain_calc_length - calculate the length of the ccw chain.
356 * @iova: guest physical address of the target ccw chain
357 * @cp: channel_program on which to perform the operation
359 * This is the chain length not considering any TICs.
360 * You need to do a new round for each TIC target.
362 * Returns: the length of the ccw chain or -errno.
364 static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
366 struct ccw1 *ccw, *p;
370 * Copy current chain from guest to host kernel.
371 * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256).
372 * So copying 2K is enough (safe).
374 p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL);
378 cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX);
388 if ((!ccw_is_chain(ccw)) && (!ccw_is_tic(ccw)))
392 } while (cnt < CCWCHAIN_LEN_MAX + 1);
394 if (cnt == CCWCHAIN_LEN_MAX + 1)
401 static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
403 struct ccwchain *chain;
404 u32 ccw_head, ccw_tail;
406 list_for_each_entry(chain, &cp->ccwchain_list, next) {
407 ccw_head = chain->ch_iova;
408 ccw_tail = ccw_head + (chain->ch_len - 1) * sizeof(struct ccw1);
410 if ((ccw_head <= tic->cda) && (tic->cda <= ccw_tail))
417 static int ccwchain_loop_tic(struct ccwchain *chain,
418 struct channel_program *cp);
420 static int ccwchain_handle_tic(struct ccw1 *tic, struct channel_program *cp)
422 struct ccwchain *chain;
425 /* May transfer to an existing chain. */
426 if (tic_target_chain_exists(tic, cp))
429 /* Get chain length. */
430 len = ccwchain_calc_length(tic->cda, cp);
434 /* Need alloc a new chain for this one. */
435 chain = ccwchain_alloc(cp, len);
438 chain->ch_iova = tic->cda;
440 /* Copy the new chain from user. */
441 ret = copy_ccw_from_iova(cp, chain->ch_ccw, tic->cda, len);
443 ccwchain_free(chain);
447 /* Loop for tics on this new chain. */
448 return ccwchain_loop_tic(chain, cp);
452 static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
457 for (i = 0; i < chain->ch_len; i++) {
458 tic = chain->ch_ccw + i;
460 if (!ccw_is_tic(tic))
463 ret = ccwchain_handle_tic(tic, cp);
471 static int ccwchain_fetch_tic(struct ccwchain *chain,
473 struct channel_program *cp)
475 struct ccw1 *ccw = chain->ch_ccw + idx;
476 struct ccwchain *iter;
477 u32 ccw_head, ccw_tail;
479 list_for_each_entry(iter, &cp->ccwchain_list, next) {
480 ccw_head = iter->ch_iova;
481 ccw_tail = ccw_head + (iter->ch_len - 1) * sizeof(struct ccw1);
483 if ((ccw_head <= ccw->cda) && (ccw->cda <= ccw_tail)) {
484 ccw->cda = (__u32) (addr_t) (iter->ch_ccw +
485 (ccw->cda - ccw_head));
493 static int ccwchain_fetch_direct(struct ccwchain *chain,
495 struct channel_program *cp)
498 struct pfn_array_table *pat;
499 unsigned long *idaws;
502 ccw = chain->ch_ccw + idx;
505 * Pin data page(s) in memory.
506 * The number of pages actually is the count of the idaws which will be
507 * needed when translating a direct ccw to a idal ccw.
509 pat = chain->ch_pat + idx;
510 if (pfn_array_table_init(pat, 1))
512 idaw_nr = pfn_array_alloc_pin(pat->pat_pa, cp->mdev,
513 ccw->cda, ccw->count);
517 /* Translate this direct ccw to a idal ccw. */
518 idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
520 pfn_array_table_unpin_free(pat, cp->mdev);
523 ccw->cda = (__u32) virt_to_phys(idaws);
524 ccw->flags |= CCW_FLAG_IDA;
526 pfn_array_table_idal_create_words(pat, idaws);
531 static int ccwchain_fetch_idal(struct ccwchain *chain,
533 struct channel_program *cp)
536 struct pfn_array_table *pat;
537 unsigned long *idaws;
539 unsigned int idaw_nr, idaw_len;
542 ccw = chain->ch_ccw + idx;
544 /* Calculate size of idaws. */
545 ret = copy_from_iova(cp->mdev, &idaw_iova, ccw->cda, sizeof(idaw_iova));
548 idaw_nr = idal_nr_words((void *)(idaw_iova), ccw->count);
549 idaw_len = idaw_nr * sizeof(*idaws);
551 /* Pin data page(s) in memory. */
552 pat = chain->ch_pat + idx;
553 ret = pfn_array_table_init(pat, idaw_nr);
557 /* Translate idal ccw to use new allocated idaws. */
558 idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL);
564 ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idaw_len);
568 ccw->cda = virt_to_phys(idaws);
570 for (i = 0; i < idaw_nr; i++) {
571 idaw_iova = *(idaws + i);
572 if (IS_ERR_VALUE(idaw_iova)) {
577 ret = pfn_array_alloc_pin(pat->pat_pa + i, cp->mdev,
583 pfn_array_table_idal_create_words(pat, idaws);
590 pfn_array_table_unpin_free(pat, cp->mdev);
596 * To reduce memory copy, we'll pin the cda page in memory,
597 * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
598 * direct ccws to idal ccws.
600 static int ccwchain_fetch_one(struct ccwchain *chain,
602 struct channel_program *cp)
604 struct ccw1 *ccw = chain->ch_ccw + idx;
606 if (ccw_is_test(ccw) || ccw_is_noop(ccw))
610 return ccwchain_fetch_tic(chain, idx, cp);
612 if (ccw_is_idal(ccw))
613 return ccwchain_fetch_idal(chain, idx, cp);
615 return ccwchain_fetch_direct(chain, idx, cp);
619 * cp_init() - allocate ccwchains for a channel program.
620 * @cp: channel_program on which to perform the operation
621 * @mdev: the mediated device to perform pin/unpin operations
622 * @orb: control block for the channel program from the guest
624 * This creates one or more ccwchain(s), and copies the raw data of
625 * the target channel program from @orb->cmd.iova to the new ccwchain(s).
628 * 1. Supports only prefetch enabled mode.
629 * 2. Supports idal(c64) ccw chaining.
630 * 3. Supports 4k idaw.
633 * %0 on success and a negative error value on failure.
635 int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
637 u64 iova = orb->cmd.cpa;
638 struct ccwchain *chain;
643 * Only support prefetch enable mode now.
644 * Only support 64bit addressing idal.
645 * Only support 4k IDAW.
647 if (!orb->cmd.pfch || !orb->cmd.c64 || orb->cmd.i2k)
650 INIT_LIST_HEAD(&cp->ccwchain_list);
651 memcpy(&cp->orb, orb, sizeof(*orb));
654 /* Get chain length. */
655 len = ccwchain_calc_length(iova, cp);
659 /* Alloc mem for the head chain. */
660 chain = ccwchain_alloc(cp, len);
663 chain->ch_iova = iova;
665 /* Copy the head chain from guest. */
666 ret = copy_ccw_from_iova(cp, chain->ch_ccw, iova, len);
668 ccwchain_free(chain);
672 /* Now loop for its TICs. */
673 ret = ccwchain_loop_tic(chain, cp);
682 * cp_free() - free resources for channel program.
683 * @cp: channel_program on which to perform the operation
685 * This unpins the memory pages and frees the memory space occupied by
686 * @cp, which must have been returned by a previous call to cp_init().
687 * Otherwise, undefined behavior occurs.
689 void cp_free(struct channel_program *cp)
695 * cp_prefetch() - translate a guest physical address channel program to
696 * a real-device runnable channel program.
697 * @cp: channel_program on which to perform the operation
699 * This function translates the guest-physical-address channel program
700 * and stores the result to ccwchain list. @cp must have been
701 * initialized by a previous call with cp_init(). Otherwise, undefined
704 * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
705 * as helpers to do ccw chain translation inside the kernel. Basically
706 * they accept a channel program issued by a virtual machine, and
707 * translate the channel program to a real-device runnable channel
710 * These APIs will copy the ccws into kernel-space buffers, and update
711 * the guest phsical addresses with their corresponding host physical
712 * addresses. Then channel I/O device drivers could issue the
713 * translated channel program to real devices to perform an I/O
716 * These interfaces are designed to support translation only for
717 * channel programs, which are generated and formatted by a
718 * guest. Thus this will make it possible for things like VFIO to
719 * leverage the interfaces to passthrough a channel I/O mediated
722 * We support direct ccw chaining by translating them to idal ccws.
725 * %0 on success and a negative error value on failure.
727 int cp_prefetch(struct channel_program *cp)
729 struct ccwchain *chain;
732 list_for_each_entry(chain, &cp->ccwchain_list, next) {
734 for (idx = 0; idx < len; idx++) {
735 ret = ccwchain_fetch_one(chain, idx, cp);
745 * cp_get_orb() - get the orb of the channel program
746 * @cp: channel_program on which to perform the operation
747 * @intparm: new intparm for the returned orb
748 * @lpm: candidate value of the logical-path mask for the returned orb
750 * This function returns the address of the updated orb of the channel
751 * program. Channel I/O device drivers could use this orb to issue a
754 union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
757 struct ccwchain *chain;
762 orb->cmd.intparm = intparm;
764 orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
766 if (orb->cmd.lpm == 0)
769 chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
771 orb->cmd.cpa = (__u32) __pa(cpa);
777 * cp_update_scsw() - update scsw for a channel program.
778 * @cp: channel_program on which to perform the operation
779 * @scsw: I/O results of the channel program and also the target to be
782 * @scsw contains the I/O results of the channel program that pointed
783 * to by @cp. However what @scsw->cpa stores is a host physical
784 * address, which is meaningless for the guest, which is waiting for
787 * This function updates @scsw->cpa to its coressponding guest physical
790 void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
792 struct ccwchain *chain;
793 u32 cpa = scsw->cmd.cpa;
794 u32 ccw_head, ccw_tail;
798 * For now, only update the cmd.cpa part. We may need to deal with
799 * other portions of the schib as well, even if we don't return them
800 * in the ioctl directly. Path status changes etc.
802 list_for_each_entry(chain, &cp->ccwchain_list, next) {
803 ccw_head = (u32)(u64)chain->ch_ccw;
804 ccw_tail = (u32)(u64)(chain->ch_ccw + chain->ch_len - 1);
806 if ((ccw_head <= cpa) && (cpa <= ccw_tail)) {
808 * (cpa - ccw_head) is the offset value of the host
809 * physical ccw to its chain head.
810 * Adding this value to the guest physical ccw chain
811 * head gets us the guest cpa.
813 cpa = chain->ch_iova + (cpa - ccw_head);
822 * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
823 * @cmd: ccwchain command on which to perform the operation
824 * @iova: the iova to check
826 * If the @iova is currently pinned for the ccw chain, return true;
829 bool cp_iova_pinned(struct channel_program *cp, u64 iova)
831 struct ccwchain *chain;
834 list_for_each_entry(chain, &cp->ccwchain_list, next) {
835 for (i = 0; i < chain->ch_len; i++)
836 if (pfn_array_table_iova_pinned(chain->ch_pat + i,