1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 20a19e61eSDong Jia Shi /* 30a19e61eSDong Jia Shi * channel program interfaces 40a19e61eSDong Jia Shi * 50a19e61eSDong Jia Shi * Copyright IBM Corp. 2017 60a19e61eSDong Jia Shi * 70a19e61eSDong Jia Shi * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> 80a19e61eSDong Jia Shi * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> 90a19e61eSDong Jia Shi */ 100a19e61eSDong Jia Shi 110a19e61eSDong Jia Shi #include <linux/mm.h> 120a19e61eSDong Jia Shi #include <linux/slab.h> 130a19e61eSDong Jia Shi #include <linux/iommu.h> 140a19e61eSDong Jia Shi #include <linux/vfio.h> 150a19e61eSDong Jia Shi #include <asm/idals.h> 160a19e61eSDong Jia Shi 170a19e61eSDong Jia Shi #include "vfio_ccw_cp.h" 180a19e61eSDong Jia Shi 190a19e61eSDong Jia Shi /* 200a19e61eSDong Jia Shi * Max length for ccw chain. 210a19e61eSDong Jia Shi * XXX: Limit to 256, need to check more? 220a19e61eSDong Jia Shi */ 230a19e61eSDong Jia Shi #define CCWCHAIN_LEN_MAX 256 240a19e61eSDong Jia Shi 250a19e61eSDong Jia Shi struct pfn_array { 2680c57f7aSDong Jia Shi /* Starting guest physical I/O address. */ 270a19e61eSDong Jia Shi unsigned long pa_iova; 2880c57f7aSDong Jia Shi /* Array that stores PFNs of the pages need to pin. */ 290a19e61eSDong Jia Shi unsigned long *pa_iova_pfn; 3080c57f7aSDong Jia Shi /* Array that receives PFNs of the pages pinned. */ 310a19e61eSDong Jia Shi unsigned long *pa_pfn; 325c1cfb1cSDong Jia Shi /* Number of pages pinned from @pa_iova. */ 330a19e61eSDong Jia Shi int pa_nr; 340a19e61eSDong Jia Shi }; 350a19e61eSDong Jia Shi 360a19e61eSDong Jia Shi struct ccwchain { 370a19e61eSDong Jia Shi struct list_head next; 380a19e61eSDong Jia Shi struct ccw1 *ch_ccw; 390a19e61eSDong Jia Shi /* Guest physical address of the current chain. */ 400a19e61eSDong Jia Shi u64 ch_iova; 410a19e61eSDong Jia Shi /* Count of the valid ccws in chain. */ 420a19e61eSDong Jia Shi int ch_len; 430a19e61eSDong Jia Shi /* Pinned PAGEs for the original data. */ 44e7eaf91bSEric Farman struct pfn_array *ch_pa; 450a19e61eSDong Jia Shi }; 460a19e61eSDong Jia Shi 470a19e61eSDong Jia Shi /* 48e4f3f18bSEric Farman * pfn_array_alloc() - alloc memory for PFNs 490a19e61eSDong Jia Shi * @pa: pfn_array on which to perform the operation 505c1cfb1cSDong Jia Shi * @iova: target guest physical address 515c1cfb1cSDong Jia Shi * @len: number of bytes that should be pinned from @iova 520a19e61eSDong Jia Shi * 53e4f3f18bSEric Farman * Attempt to allocate memory for PFNs. 540a19e61eSDong Jia Shi * 550a19e61eSDong Jia Shi * Usage of pfn_array: 565c1cfb1cSDong Jia Shi * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in 575c1cfb1cSDong Jia Shi * this structure will be filled in by this function. 580a19e61eSDong Jia Shi * 590a19e61eSDong Jia Shi * Returns: 60e4f3f18bSEric Farman * 0 if PFNs are allocated 61e4f3f18bSEric Farman * -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova_pfn is not NULL 62e4f3f18bSEric Farman * -ENOMEM if alloc failed 630a19e61eSDong Jia Shi */ 64e4f3f18bSEric Farman static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) 650a19e61eSDong Jia Shi { 66e4f3f18bSEric Farman int i; 670a19e61eSDong Jia Shi 685c1cfb1cSDong Jia Shi if (pa->pa_nr || pa->pa_iova_pfn) 690a19e61eSDong Jia Shi return -EINVAL; 700a19e61eSDong Jia Shi 710a19e61eSDong Jia Shi pa->pa_iova = iova; 720a19e61eSDong Jia Shi 730a19e61eSDong Jia Shi pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 740a19e61eSDong Jia Shi if (!pa->pa_nr) 750a19e61eSDong Jia Shi return -EINVAL; 760a19e61eSDong Jia Shi 770a19e61eSDong Jia Shi pa->pa_iova_pfn = kcalloc(pa->pa_nr, 780a19e61eSDong Jia Shi sizeof(*pa->pa_iova_pfn) + 790a19e61eSDong Jia Shi sizeof(*pa->pa_pfn), 800a19e61eSDong Jia Shi GFP_KERNEL); 810a19e61eSDong Jia Shi if (unlikely(!pa->pa_iova_pfn)) 820a19e61eSDong Jia Shi return -ENOMEM; 830a19e61eSDong Jia Shi pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr; 840a19e61eSDong Jia Shi 855c1cfb1cSDong Jia Shi pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT; 86c34a12e6SEric Farman pa->pa_pfn[0] = -1ULL; 87c34a12e6SEric Farman for (i = 1; i < pa->pa_nr; i++) { 885c1cfb1cSDong Jia Shi pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1; 89c34a12e6SEric Farman pa->pa_pfn[i] = -1ULL; 90c34a12e6SEric Farman } 910a19e61eSDong Jia Shi 92e4f3f18bSEric Farman return 0; 93e4f3f18bSEric Farman } 94e4f3f18bSEric Farman 95e4f3f18bSEric Farman /* 96e4f3f18bSEric Farman * pfn_array_pin() - Pin user pages in memory 97e4f3f18bSEric Farman * @pa: pfn_array on which to perform the operation 98e4f3f18bSEric Farman * @mdev: the mediated device to perform pin operations 99e4f3f18bSEric Farman * 100e4f3f18bSEric Farman * Returns number of pages pinned upon success. 101e4f3f18bSEric Farman * If the pin request partially succeeds, or fails completely, 102e4f3f18bSEric Farman * all pages are left unpinned and a negative error value is returned. 103e4f3f18bSEric Farman */ 104e4f3f18bSEric Farman static int pfn_array_pin(struct pfn_array *pa, struct device *mdev) 105e4f3f18bSEric Farman { 106e4f3f18bSEric Farman int ret = 0; 107e4f3f18bSEric Farman 1085c1cfb1cSDong Jia Shi ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr, 1095c1cfb1cSDong Jia Shi IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); 1105c1cfb1cSDong Jia Shi 1115c1cfb1cSDong Jia Shi if (ret < 0) { 1125c1cfb1cSDong Jia Shi goto err_out; 1135c1cfb1cSDong Jia Shi } else if (ret > 0 && ret != pa->pa_nr) { 1145c1cfb1cSDong Jia Shi vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret); 1150a19e61eSDong Jia Shi ret = -EINVAL; 1165c1cfb1cSDong Jia Shi goto err_out; 1175c1cfb1cSDong Jia Shi } 1180a19e61eSDong Jia Shi 1190a19e61eSDong Jia Shi return ret; 1205c1cfb1cSDong Jia Shi 1215c1cfb1cSDong Jia Shi err_out: 1225c1cfb1cSDong Jia Shi pa->pa_nr = 0; 1235c1cfb1cSDong Jia Shi 1245c1cfb1cSDong Jia Shi return ret; 1255c1cfb1cSDong Jia Shi } 1265c1cfb1cSDong Jia Shi 1275c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */ 1285c1cfb1cSDong Jia Shi static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev) 1295c1cfb1cSDong Jia Shi { 130e4f3f18bSEric Farman /* Only unpin if any pages were pinned to begin with */ 131e4f3f18bSEric Farman if (pa->pa_nr) 1325c1cfb1cSDong Jia Shi vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr); 1335c1cfb1cSDong Jia Shi pa->pa_nr = 0; 1345c1cfb1cSDong Jia Shi kfree(pa->pa_iova_pfn); 1350a19e61eSDong Jia Shi } 1360a19e61eSDong Jia Shi 137e7eaf91bSEric Farman static bool pfn_array_iova_pinned(struct pfn_array *pa, unsigned long iova) 1380a19e61eSDong Jia Shi { 139e7eaf91bSEric Farman unsigned long iova_pfn = iova >> PAGE_SHIFT; 1400a19e61eSDong Jia Shi int i; 1410a19e61eSDong Jia Shi 142e7eaf91bSEric Farman for (i = 0; i < pa->pa_nr; i++) 143e7eaf91bSEric Farman if (pa->pa_iova_pfn[i] == iova_pfn) 1440a19e61eSDong Jia Shi return true; 1450a19e61eSDong Jia Shi 1460a19e61eSDong Jia Shi return false; 1470a19e61eSDong Jia Shi } 148e7eaf91bSEric Farman /* Create the list of IDAL words for a pfn_array. */ 149e7eaf91bSEric Farman static inline void pfn_array_idal_create_words( 150e7eaf91bSEric Farman struct pfn_array *pa, 1510a19e61eSDong Jia Shi unsigned long *idaws) 1520a19e61eSDong Jia Shi { 153e7eaf91bSEric Farman int i; 1540a19e61eSDong Jia Shi 1550a19e61eSDong Jia Shi /* 1560a19e61eSDong Jia Shi * Idal words (execept the first one) rely on the memory being 4k 1570a19e61eSDong Jia Shi * aligned. If a user virtual address is 4K aligned, then it's 1580a19e61eSDong Jia Shi * corresponding kernel physical address will also be 4K aligned. Thus 1590a19e61eSDong Jia Shi * there will be no problem here to simply use the phys to create an 1600a19e61eSDong Jia Shi * idaw. 1610a19e61eSDong Jia Shi */ 162e7eaf91bSEric Farman 163e7eaf91bSEric Farman for (i = 0; i < pa->pa_nr; i++) 164e7eaf91bSEric Farman idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT; 1658aabf0edSEric Farman 1668aabf0edSEric Farman /* Adjust the first IDAW, since it may not start on a page boundary */ 167e7eaf91bSEric Farman idaws[0] += pa->pa_iova & (PAGE_SIZE - 1); 1680a19e61eSDong Jia Shi } 1690a19e61eSDong Jia Shi 1700a19e61eSDong Jia Shi 1710a19e61eSDong Jia Shi /* 1720a19e61eSDong Jia Shi * Within the domain (@mdev), copy @n bytes from a guest physical 1730a19e61eSDong Jia Shi * address (@iova) to a host physical address (@to). 1740a19e61eSDong Jia Shi */ 1750a19e61eSDong Jia Shi static long copy_from_iova(struct device *mdev, 1760a19e61eSDong Jia Shi void *to, u64 iova, 1770a19e61eSDong Jia Shi unsigned long n) 1780a19e61eSDong Jia Shi { 1790a19e61eSDong Jia Shi struct pfn_array pa = {0}; 1800a19e61eSDong Jia Shi u64 from; 1810a19e61eSDong Jia Shi int i, ret; 1820a19e61eSDong Jia Shi unsigned long l, m; 1830a19e61eSDong Jia Shi 184e4f3f18bSEric Farman ret = pfn_array_alloc(&pa, iova, n); 185e4f3f18bSEric Farman if (ret < 0) 1860a19e61eSDong Jia Shi return ret; 1870a19e61eSDong Jia Shi 188e4f3f18bSEric Farman ret = pfn_array_pin(&pa, mdev); 189e4f3f18bSEric Farman if (ret < 0) { 190e4f3f18bSEric Farman pfn_array_unpin_free(&pa, mdev); 191e4f3f18bSEric Farman return ret; 192e4f3f18bSEric Farman } 193e4f3f18bSEric Farman 1940a19e61eSDong Jia Shi l = n; 1950a19e61eSDong Jia Shi for (i = 0; i < pa.pa_nr; i++) { 1960a19e61eSDong Jia Shi from = pa.pa_pfn[i] << PAGE_SHIFT; 1970a19e61eSDong Jia Shi m = PAGE_SIZE; 1980a19e61eSDong Jia Shi if (i == 0) { 1990a19e61eSDong Jia Shi from += iova & (PAGE_SIZE - 1); 2000a19e61eSDong Jia Shi m -= iova & (PAGE_SIZE - 1); 2010a19e61eSDong Jia Shi } 2020a19e61eSDong Jia Shi 2030a19e61eSDong Jia Shi m = min(l, m); 2040a19e61eSDong Jia Shi memcpy(to + (n - l), (void *)from, m); 2050a19e61eSDong Jia Shi 2060a19e61eSDong Jia Shi l -= m; 2070a19e61eSDong Jia Shi if (l == 0) 2080a19e61eSDong Jia Shi break; 2090a19e61eSDong Jia Shi } 2100a19e61eSDong Jia Shi 2110a19e61eSDong Jia Shi pfn_array_unpin_free(&pa, mdev); 2120a19e61eSDong Jia Shi 2130a19e61eSDong Jia Shi return l; 2140a19e61eSDong Jia Shi } 2150a19e61eSDong Jia Shi 2160a19e61eSDong Jia Shi static long copy_ccw_from_iova(struct channel_program *cp, 2170a19e61eSDong Jia Shi struct ccw1 *to, u64 iova, 2180a19e61eSDong Jia Shi unsigned long len) 2190a19e61eSDong Jia Shi { 220d686f21aSDong Jia Shi struct ccw0 ccw0; 221d686f21aSDong Jia Shi struct ccw1 *pccw1; 222d686f21aSDong Jia Shi int ret; 223d686f21aSDong Jia Shi int i; 224d686f21aSDong Jia Shi 225d686f21aSDong Jia Shi ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1)); 226d686f21aSDong Jia Shi if (ret) 227d686f21aSDong Jia Shi return ret; 228d686f21aSDong Jia Shi 229d686f21aSDong Jia Shi if (!cp->orb.cmd.fmt) { 230d686f21aSDong Jia Shi pccw1 = to; 231d686f21aSDong Jia Shi for (i = 0; i < len; i++) { 232d686f21aSDong Jia Shi ccw0 = *(struct ccw0 *)pccw1; 233d686f21aSDong Jia Shi if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) { 234d686f21aSDong Jia Shi pccw1->cmd_code = CCW_CMD_TIC; 235d686f21aSDong Jia Shi pccw1->flags = 0; 236d686f21aSDong Jia Shi pccw1->count = 0; 237d686f21aSDong Jia Shi } else { 238d686f21aSDong Jia Shi pccw1->cmd_code = ccw0.cmd_code; 239d686f21aSDong Jia Shi pccw1->flags = ccw0.flags; 240d686f21aSDong Jia Shi pccw1->count = ccw0.count; 241d686f21aSDong Jia Shi } 242d686f21aSDong Jia Shi pccw1->cda = ccw0.cda; 243d686f21aSDong Jia Shi pccw1++; 244d686f21aSDong Jia Shi } 245d686f21aSDong Jia Shi } 246d686f21aSDong Jia Shi 247d686f21aSDong Jia Shi return ret; 2480a19e61eSDong Jia Shi } 2490a19e61eSDong Jia Shi 2500a19e61eSDong Jia Shi /* 2510a19e61eSDong Jia Shi * Helpers to operate ccwchain. 2520a19e61eSDong Jia Shi */ 2535d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02) 2545d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C) 2555d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE) 2565d87fbf7SEric Farman 2570a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP) 2580a19e61eSDong Jia Shi 2590a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC) 2600a19e61eSDong Jia Shi 2610a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA) 2625d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP) 2630a19e61eSDong Jia Shi 2640a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC)) 2650a19e61eSDong Jia Shi 26648bd0eeeSEric Farman /* 2675d87fbf7SEric Farman * ccw_does_data_transfer() 2685d87fbf7SEric Farman * 2695d87fbf7SEric Farman * Determine whether a CCW will move any data, such that the guest pages 2705d87fbf7SEric Farman * would need to be pinned before performing the I/O. 2715d87fbf7SEric Farman * 2725d87fbf7SEric Farman * Returns 1 if yes, 0 if no. 2735d87fbf7SEric Farman */ 2745d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw) 2755d87fbf7SEric Farman { 276453eac31SEric Farman /* If the count field is zero, then no data will be transferred */ 277453eac31SEric Farman if (ccw->count == 0) 278453eac31SEric Farman return 0; 279453eac31SEric Farman 2809b6e57e5SEric Farman /* If the command is a NOP, then no data will be transferred */ 2819b6e57e5SEric Farman if (ccw_is_noop(ccw)) 2829b6e57e5SEric Farman return 0; 2839b6e57e5SEric Farman 2845d87fbf7SEric Farman /* If the skip flag is off, then data will be transferred */ 2855d87fbf7SEric Farman if (!ccw_is_skip(ccw)) 2865d87fbf7SEric Farman return 1; 2875d87fbf7SEric Farman 2885d87fbf7SEric Farman /* 2895d87fbf7SEric Farman * If the skip flag is on, it is only meaningful if the command 2905d87fbf7SEric Farman * code is a read, read backward, sense, or sense ID. In those 2915d87fbf7SEric Farman * cases, no data will be transferred. 2925d87fbf7SEric Farman */ 2935d87fbf7SEric Farman if (ccw_is_read(ccw) || ccw_is_read_backward(ccw)) 2945d87fbf7SEric Farman return 0; 2955d87fbf7SEric Farman 2965d87fbf7SEric Farman if (ccw_is_sense(ccw)) 2975d87fbf7SEric Farman return 0; 2985d87fbf7SEric Farman 2995d87fbf7SEric Farman /* The skip flag is on, but it is ignored for this command code. */ 3005d87fbf7SEric Farman return 1; 3015d87fbf7SEric Farman } 3025d87fbf7SEric Farman 3035d87fbf7SEric Farman /* 30448bd0eeeSEric Farman * is_cpa_within_range() 30548bd0eeeSEric Farman * 30648bd0eeeSEric Farman * @cpa: channel program address being questioned 30748bd0eeeSEric Farman * @head: address of the beginning of a CCW chain 30848bd0eeeSEric Farman * @len: number of CCWs within the chain 30948bd0eeeSEric Farman * 31048bd0eeeSEric Farman * Determine whether the address of a CCW (whether a new chain, 31148bd0eeeSEric Farman * or the target of a TIC) falls within a range (including the end points). 31248bd0eeeSEric Farman * 31348bd0eeeSEric Farman * Returns 1 if yes, 0 if no. 31448bd0eeeSEric Farman */ 31548bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len) 31648bd0eeeSEric Farman { 31748bd0eeeSEric Farman u32 tail = head + (len - 1) * sizeof(struct ccw1); 31848bd0eeeSEric Farman 31948bd0eeeSEric Farman return (head <= cpa && cpa <= tail); 32048bd0eeeSEric Farman } 32148bd0eeeSEric Farman 32248bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len) 32348bd0eeeSEric Farman { 32448bd0eeeSEric Farman if (!ccw_is_tic(ccw)) 32548bd0eeeSEric Farman return 0; 32648bd0eeeSEric Farman 32748bd0eeeSEric Farman return is_cpa_within_range(ccw->cda, head, len); 32848bd0eeeSEric Farman } 32948bd0eeeSEric Farman 3300a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len) 3310a19e61eSDong Jia Shi { 3320a19e61eSDong Jia Shi struct ccwchain *chain; 3330a19e61eSDong Jia Shi void *data; 3340a19e61eSDong Jia Shi size_t size; 3350a19e61eSDong Jia Shi 3360a19e61eSDong Jia Shi /* Make ccw address aligned to 8. */ 3370a19e61eSDong Jia Shi size = ((sizeof(*chain) + 7L) & -8L) + 3380a19e61eSDong Jia Shi sizeof(*chain->ch_ccw) * len + 339e7eaf91bSEric Farman sizeof(*chain->ch_pa) * len; 3400a19e61eSDong Jia Shi chain = kzalloc(size, GFP_DMA | GFP_KERNEL); 3410a19e61eSDong Jia Shi if (!chain) 3420a19e61eSDong Jia Shi return NULL; 3430a19e61eSDong Jia Shi 3440a19e61eSDong Jia Shi data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L); 3450a19e61eSDong Jia Shi chain->ch_ccw = (struct ccw1 *)data; 3460a19e61eSDong Jia Shi 3470a19e61eSDong Jia Shi data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len; 348e7eaf91bSEric Farman chain->ch_pa = (struct pfn_array *)data; 3490a19e61eSDong Jia Shi 3500a19e61eSDong Jia Shi chain->ch_len = len; 3510a19e61eSDong Jia Shi 3520a19e61eSDong Jia Shi list_add_tail(&chain->next, &cp->ccwchain_list); 3530a19e61eSDong Jia Shi 3540a19e61eSDong Jia Shi return chain; 3550a19e61eSDong Jia Shi } 3560a19e61eSDong Jia Shi 3570a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain) 3580a19e61eSDong Jia Shi { 3590a19e61eSDong Jia Shi list_del(&chain->next); 3600a19e61eSDong Jia Shi kfree(chain); 3610a19e61eSDong Jia Shi } 3620a19e61eSDong Jia Shi 3630a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */ 3640a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx) 3650a19e61eSDong Jia Shi { 3660a19e61eSDong Jia Shi struct ccw1 *ccw = chain->ch_ccw + idx; 3670a19e61eSDong Jia Shi 3689b6e57e5SEric Farman if (ccw_is_tic(ccw)) 369408358b5SJason J. Herne return; 3700a19e61eSDong Jia Shi 3710a19e61eSDong Jia Shi kfree((void *)(u64)ccw->cda); 3720a19e61eSDong Jia Shi } 3730a19e61eSDong Jia Shi 3740a19e61eSDong Jia Shi /** 3750a19e61eSDong Jia Shi * ccwchain_calc_length - calculate the length of the ccw chain. 3760a19e61eSDong Jia Shi * @iova: guest physical address of the target ccw chain 3770a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 3780a19e61eSDong Jia Shi * 3790a19e61eSDong Jia Shi * This is the chain length not considering any TICs. 3800a19e61eSDong Jia Shi * You need to do a new round for each TIC target. 3810a19e61eSDong Jia Shi * 382fb9e7880SHalil Pasic * The program is also validated for absence of not yet supported 383fb9e7880SHalil Pasic * indirect data addressing scenarios. 384fb9e7880SHalil Pasic * 3850a19e61eSDong Jia Shi * Returns: the length of the ccw chain or -errno. 3860a19e61eSDong Jia Shi */ 3870a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp) 3880a19e61eSDong Jia Shi { 3890a19e61eSDong Jia Shi struct ccw1 *ccw, *p; 3900a19e61eSDong Jia Shi int cnt; 3910a19e61eSDong Jia Shi 3920a19e61eSDong Jia Shi /* 3930a19e61eSDong Jia Shi * Copy current chain from guest to host kernel. 3940a19e61eSDong Jia Shi * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256). 3950a19e61eSDong Jia Shi * So copying 2K is enough (safe). 3960a19e61eSDong Jia Shi */ 3970a19e61eSDong Jia Shi p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL); 3980a19e61eSDong Jia Shi if (!ccw) 3990a19e61eSDong Jia Shi return -ENOMEM; 4000a19e61eSDong Jia Shi 4010a19e61eSDong Jia Shi cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX); 4020a19e61eSDong Jia Shi if (cnt) { 4030a19e61eSDong Jia Shi kfree(ccw); 4040a19e61eSDong Jia Shi return cnt; 4050a19e61eSDong Jia Shi } 4060a19e61eSDong Jia Shi 4070a19e61eSDong Jia Shi cnt = 0; 4080a19e61eSDong Jia Shi do { 4090a19e61eSDong Jia Shi cnt++; 4100a19e61eSDong Jia Shi 411fb9e7880SHalil Pasic /* 412fb9e7880SHalil Pasic * As we don't want to fail direct addressing even if the 413fb9e7880SHalil Pasic * orb specified one of the unsupported formats, we defer 414fb9e7880SHalil Pasic * checking for IDAWs in unsupported formats to here. 415fb9e7880SHalil Pasic */ 416b89e242eSEric Farman if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw)) { 417b89e242eSEric Farman kfree(p); 418fb9e7880SHalil Pasic return -EOPNOTSUPP; 419b89e242eSEric Farman } 420fb9e7880SHalil Pasic 42148bd0eeeSEric Farman /* 42248bd0eeeSEric Farman * We want to keep counting if the current CCW has the 42348bd0eeeSEric Farman * command-chaining flag enabled, or if it is a TIC CCW 42448bd0eeeSEric Farman * that loops back into the current chain. The latter 42548bd0eeeSEric Farman * is used for device orientation, where the CCW PRIOR to 42648bd0eeeSEric Farman * the TIC can either jump to the TIC or a CCW immediately 42748bd0eeeSEric Farman * after the TIC, depending on the results of its operation. 42848bd0eeeSEric Farman */ 42948bd0eeeSEric Farman if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt)) 4300a19e61eSDong Jia Shi break; 4310a19e61eSDong Jia Shi 4320a19e61eSDong Jia Shi ccw++; 4330a19e61eSDong Jia Shi } while (cnt < CCWCHAIN_LEN_MAX + 1); 4340a19e61eSDong Jia Shi 4350a19e61eSDong Jia Shi if (cnt == CCWCHAIN_LEN_MAX + 1) 4360a19e61eSDong Jia Shi cnt = -EINVAL; 4370a19e61eSDong Jia Shi 4380a19e61eSDong Jia Shi kfree(p); 4390a19e61eSDong Jia Shi return cnt; 4400a19e61eSDong Jia Shi } 4410a19e61eSDong Jia Shi 4420a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp) 4430a19e61eSDong Jia Shi { 4440a19e61eSDong Jia Shi struct ccwchain *chain; 4452904337fSEric Farman u32 ccw_head; 4460a19e61eSDong Jia Shi 4470a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 4480a19e61eSDong Jia Shi ccw_head = chain->ch_iova; 4492904337fSEric Farman if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len)) 4500a19e61eSDong Jia Shi return 1; 4510a19e61eSDong Jia Shi } 4520a19e61eSDong Jia Shi 4530a19e61eSDong Jia Shi return 0; 4540a19e61eSDong Jia Shi } 4550a19e61eSDong Jia Shi 4560a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, 4570a19e61eSDong Jia Shi struct channel_program *cp); 4580a19e61eSDong Jia Shi 459363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp) 4600a19e61eSDong Jia Shi { 4610a19e61eSDong Jia Shi struct ccwchain *chain; 4620a19e61eSDong Jia Shi int len, ret; 4630a19e61eSDong Jia Shi 4640a19e61eSDong Jia Shi /* Get chain length. */ 465363fe5f7SEric Farman len = ccwchain_calc_length(cda, cp); 4660a19e61eSDong Jia Shi if (len < 0) 4670a19e61eSDong Jia Shi return len; 4680a19e61eSDong Jia Shi 4690a19e61eSDong Jia Shi /* Need alloc a new chain for this one. */ 4700a19e61eSDong Jia Shi chain = ccwchain_alloc(cp, len); 4710a19e61eSDong Jia Shi if (!chain) 4720a19e61eSDong Jia Shi return -ENOMEM; 473363fe5f7SEric Farman chain->ch_iova = cda; 4740a19e61eSDong Jia Shi 4750a19e61eSDong Jia Shi /* Copy the new chain from user. */ 476363fe5f7SEric Farman ret = copy_ccw_from_iova(cp, chain->ch_ccw, cda, len); 4770a19e61eSDong Jia Shi if (ret) { 4780a19e61eSDong Jia Shi ccwchain_free(chain); 4790a19e61eSDong Jia Shi return ret; 4800a19e61eSDong Jia Shi } 4810a19e61eSDong Jia Shi 4820a19e61eSDong Jia Shi /* Loop for tics on this new chain. */ 4830a19e61eSDong Jia Shi return ccwchain_loop_tic(chain, cp); 4840a19e61eSDong Jia Shi } 4850a19e61eSDong Jia Shi 4860a19e61eSDong Jia Shi /* Loop for TICs. */ 4870a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp) 4880a19e61eSDong Jia Shi { 4890a19e61eSDong Jia Shi struct ccw1 *tic; 4900a19e61eSDong Jia Shi int i, ret; 4910a19e61eSDong Jia Shi 4920a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) { 4930a19e61eSDong Jia Shi tic = chain->ch_ccw + i; 4940a19e61eSDong Jia Shi 4950a19e61eSDong Jia Shi if (!ccw_is_tic(tic)) 4960a19e61eSDong Jia Shi continue; 4970a19e61eSDong Jia Shi 498e64bd689SEric Farman /* May transfer to an existing chain. */ 499e64bd689SEric Farman if (tic_target_chain_exists(tic, cp)) 500e64bd689SEric Farman continue; 501e64bd689SEric Farman 502363fe5f7SEric Farman /* Build a ccwchain for the next segment */ 503363fe5f7SEric Farman ret = ccwchain_handle_ccw(tic->cda, cp); 5040a19e61eSDong Jia Shi if (ret) 5050a19e61eSDong Jia Shi return ret; 5060a19e61eSDong Jia Shi } 5070a19e61eSDong Jia Shi 5080a19e61eSDong Jia Shi return 0; 5090a19e61eSDong Jia Shi } 5100a19e61eSDong Jia Shi 5110a19e61eSDong Jia Shi static int ccwchain_fetch_tic(struct ccwchain *chain, 5120a19e61eSDong Jia Shi int idx, 5130a19e61eSDong Jia Shi struct channel_program *cp) 5140a19e61eSDong Jia Shi { 5150a19e61eSDong Jia Shi struct ccw1 *ccw = chain->ch_ccw + idx; 5160a19e61eSDong Jia Shi struct ccwchain *iter; 5172904337fSEric Farman u32 ccw_head; 5180a19e61eSDong Jia Shi 5190a19e61eSDong Jia Shi list_for_each_entry(iter, &cp->ccwchain_list, next) { 5200a19e61eSDong Jia Shi ccw_head = iter->ch_iova; 5212904337fSEric Farman if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) { 522c389377cSJason J. Herne ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) + 5230a19e61eSDong Jia Shi (ccw->cda - ccw_head)); 5240a19e61eSDong Jia Shi return 0; 5250a19e61eSDong Jia Shi } 5260a19e61eSDong Jia Shi } 5270a19e61eSDong Jia Shi 5280a19e61eSDong Jia Shi return -EFAULT; 5290a19e61eSDong Jia Shi } 5300a19e61eSDong Jia Shi 5310a19e61eSDong Jia Shi static int ccwchain_fetch_direct(struct ccwchain *chain, 5320a19e61eSDong Jia Shi int idx, 5330a19e61eSDong Jia Shi struct channel_program *cp) 5340a19e61eSDong Jia Shi { 5350a19e61eSDong Jia Shi struct ccw1 *ccw; 536e7eaf91bSEric Farman struct pfn_array *pa; 537*01aa26c6SEric Farman u64 iova; 5380a19e61eSDong Jia Shi unsigned long *idaws; 5396238f921SDong Jia Shi int ret; 540453eac31SEric Farman int bytes = 1; 541*01aa26c6SEric Farman int idaw_nr, idal_len; 542*01aa26c6SEric Farman int i; 5430a19e61eSDong Jia Shi 5440a19e61eSDong Jia Shi ccw = chain->ch_ccw + idx; 5450a19e61eSDong Jia Shi 546e8573b39SEric Farman if (ccw->count) 547453eac31SEric Farman bytes = ccw->count; 548e8573b39SEric Farman 549e8573b39SEric Farman /* Calculate size of IDAL */ 550*01aa26c6SEric Farman if (ccw_is_idal(ccw)) { 551*01aa26c6SEric Farman /* Read first IDAW to see if it's 4K-aligned or not. */ 552*01aa26c6SEric Farman /* All subsequent IDAws will be 4K-aligned. */ 553*01aa26c6SEric Farman ret = copy_from_iova(cp->mdev, &iova, ccw->cda, sizeof(iova)); 554*01aa26c6SEric Farman if (ret) 555*01aa26c6SEric Farman return ret; 556*01aa26c6SEric Farman } else { 557*01aa26c6SEric Farman iova = ccw->cda; 558*01aa26c6SEric Farman } 559*01aa26c6SEric Farman idaw_nr = idal_nr_words((void *)iova, bytes); 560*01aa26c6SEric Farman idal_len = idaw_nr * sizeof(*idaws); 561e8573b39SEric Farman 562e8573b39SEric Farman /* Allocate an IDAL from host storage */ 563e8573b39SEric Farman idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL); 564e8573b39SEric Farman if (!idaws) { 565e8573b39SEric Farman ret = -ENOMEM; 566e8573b39SEric Farman goto out_init; 5674cebc5d6SDong Jia Shi } 5684cebc5d6SDong Jia Shi 5690a19e61eSDong Jia Shi /* 570*01aa26c6SEric Farman * Allocate an array of pfn's for pages to pin/translate. 571*01aa26c6SEric Farman * The number of pages is actually the count of the idaws 572*01aa26c6SEric Farman * required for the data transfer, since we only only support 573*01aa26c6SEric Farman * 4K IDAWs today. 5740a19e61eSDong Jia Shi */ 575e7eaf91bSEric Farman pa = chain->ch_pa + idx; 576*01aa26c6SEric Farman ret = pfn_array_alloc(pa, iova, bytes); 577e4f3f18bSEric Farman if (ret < 0) 578e8573b39SEric Farman goto out_free_idaws; 579e4f3f18bSEric Farman 580*01aa26c6SEric Farman if (ccw_is_idal(ccw)) { 581*01aa26c6SEric Farman /* Copy guest IDAL into host IDAL */ 582*01aa26c6SEric Farman ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idal_len); 583*01aa26c6SEric Farman if (ret) 584*01aa26c6SEric Farman goto out_unpin; 585*01aa26c6SEric Farman 586*01aa26c6SEric Farman /* 587*01aa26c6SEric Farman * Copy guest IDAWs into pfn_array, in case the memory they 588*01aa26c6SEric Farman * occupy is not contiguous. 589*01aa26c6SEric Farman */ 590*01aa26c6SEric Farman for (i = 0; i < idaw_nr; i++) 591*01aa26c6SEric Farman pa->pa_iova_pfn[i] = idaws[i] >> PAGE_SHIFT; 592*01aa26c6SEric Farman } else { 593*01aa26c6SEric Farman /* 594*01aa26c6SEric Farman * No action is required here; the iova addresses in pfn_array 595*01aa26c6SEric Farman * were initialized sequentially in pfn_array_alloc() beginning 596*01aa26c6SEric Farman * with the contents of ccw->cda. 597*01aa26c6SEric Farman */ 598*01aa26c6SEric Farman } 599*01aa26c6SEric Farman 6005d87fbf7SEric Farman if (ccw_does_data_transfer(ccw)) { 601e7eaf91bSEric Farman ret = pfn_array_pin(pa, cp->mdev); 6026238f921SDong Jia Shi if (ret < 0) 603806212f9SEric Farman goto out_unpin; 6045d87fbf7SEric Farman } else { 605e7eaf91bSEric Farman pa->pa_nr = 0; 6065d87fbf7SEric Farman } 6070a19e61eSDong Jia Shi 6080a19e61eSDong Jia Shi ccw->cda = (__u32) virt_to_phys(idaws); 6090a19e61eSDong Jia Shi ccw->flags |= CCW_FLAG_IDA; 6100a19e61eSDong Jia Shi 611e8573b39SEric Farman /* Populate the IDAL with pinned/translated addresses from pfn */ 612e7eaf91bSEric Farman pfn_array_idal_create_words(pa, idaws); 6130a19e61eSDong Jia Shi 6140a19e61eSDong Jia Shi return 0; 6156238f921SDong Jia Shi 6166238f921SDong Jia Shi out_unpin: 617e7eaf91bSEric Farman pfn_array_unpin_free(pa, cp->mdev); 618e8573b39SEric Farman out_free_idaws: 619e8573b39SEric Farman kfree(idaws); 6206238f921SDong Jia Shi out_init: 6216238f921SDong Jia Shi ccw->cda = 0; 6226238f921SDong Jia Shi return ret; 6230a19e61eSDong Jia Shi } 6240a19e61eSDong Jia Shi 6250a19e61eSDong Jia Shi /* 6260a19e61eSDong Jia Shi * Fetch one ccw. 6270a19e61eSDong Jia Shi * To reduce memory copy, we'll pin the cda page in memory, 6280a19e61eSDong Jia Shi * and to get rid of the cda 2G limitiaion of ccw1, we'll translate 6290a19e61eSDong Jia Shi * direct ccws to idal ccws. 6300a19e61eSDong Jia Shi */ 6310a19e61eSDong Jia Shi static int ccwchain_fetch_one(struct ccwchain *chain, 6320a19e61eSDong Jia Shi int idx, 6330a19e61eSDong Jia Shi struct channel_program *cp) 6340a19e61eSDong Jia Shi { 6350a19e61eSDong Jia Shi struct ccw1 *ccw = chain->ch_ccw + idx; 6360a19e61eSDong Jia Shi 6370a19e61eSDong Jia Shi if (ccw_is_tic(ccw)) 6380a19e61eSDong Jia Shi return ccwchain_fetch_tic(chain, idx, cp); 6390a19e61eSDong Jia Shi 6400a19e61eSDong Jia Shi return ccwchain_fetch_direct(chain, idx, cp); 6410a19e61eSDong Jia Shi } 6420a19e61eSDong Jia Shi 6430a19e61eSDong Jia Shi /** 6440a19e61eSDong Jia Shi * cp_init() - allocate ccwchains for a channel program. 6450a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6460a19e61eSDong Jia Shi * @mdev: the mediated device to perform pin/unpin operations 6470a19e61eSDong Jia Shi * @orb: control block for the channel program from the guest 6480a19e61eSDong Jia Shi * 6490a19e61eSDong Jia Shi * This creates one or more ccwchain(s), and copies the raw data of 6500a19e61eSDong Jia Shi * the target channel program from @orb->cmd.iova to the new ccwchain(s). 6510a19e61eSDong Jia Shi * 6520a19e61eSDong Jia Shi * Limitations: 6530a19e61eSDong Jia Shi * 1. Supports only prefetch enabled mode. 6540a19e61eSDong Jia Shi * 2. Supports idal(c64) ccw chaining. 6550a19e61eSDong Jia Shi * 3. Supports 4k idaw. 6560a19e61eSDong Jia Shi * 6570a19e61eSDong Jia Shi * Returns: 6580a19e61eSDong Jia Shi * %0 on success and a negative error value on failure. 6590a19e61eSDong Jia Shi */ 6600a19e61eSDong Jia Shi int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb) 6610a19e61eSDong Jia Shi { 66299afcb05SEric Farman int ret; 6630a19e61eSDong Jia Shi 6640a19e61eSDong Jia Shi /* 6650a19e61eSDong Jia Shi * XXX: 6660a19e61eSDong Jia Shi * Only support prefetch enable mode now. 6670a19e61eSDong Jia Shi */ 668fb9e7880SHalil Pasic if (!orb->cmd.pfch) 6690a19e61eSDong Jia Shi return -EOPNOTSUPP; 6700a19e61eSDong Jia Shi 6710a19e61eSDong Jia Shi INIT_LIST_HEAD(&cp->ccwchain_list); 6720a19e61eSDong Jia Shi memcpy(&cp->orb, orb, sizeof(*orb)); 6730a19e61eSDong Jia Shi cp->mdev = mdev; 6740a19e61eSDong Jia Shi 67599afcb05SEric Farman /* Build a ccwchain for the first CCW segment */ 67699afcb05SEric Farman ret = ccwchain_handle_ccw(orb->cmd.cpa, cp); 6770a19e61eSDong Jia Shi if (ret) 678812271b9SEric Farman cp_free(cp); 67999afcb05SEric Farman 680fb9e7880SHalil Pasic /* It is safe to force: if not set but idals used 681fb9e7880SHalil Pasic * ccwchain_calc_length returns an error. 682fb9e7880SHalil Pasic */ 683fb9e7880SHalil Pasic cp->orb.cmd.c64 = 1; 6840a19e61eSDong Jia Shi 68571189f26SCornelia Huck if (!ret) 68671189f26SCornelia Huck cp->initialized = true; 68771189f26SCornelia Huck 6880a19e61eSDong Jia Shi return ret; 6890a19e61eSDong Jia Shi } 6900a19e61eSDong Jia Shi 6910a19e61eSDong Jia Shi 6920a19e61eSDong Jia Shi /** 6930a19e61eSDong Jia Shi * cp_free() - free resources for channel program. 6940a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6950a19e61eSDong Jia Shi * 6960a19e61eSDong Jia Shi * This unpins the memory pages and frees the memory space occupied by 6970a19e61eSDong Jia Shi * @cp, which must have been returned by a previous call to cp_init(). 6980a19e61eSDong Jia Shi * Otherwise, undefined behavior occurs. 6990a19e61eSDong Jia Shi */ 7000a19e61eSDong Jia Shi void cp_free(struct channel_program *cp) 7010a19e61eSDong Jia Shi { 702812271b9SEric Farman struct ccwchain *chain, *temp; 703812271b9SEric Farman int i; 704812271b9SEric Farman 705812271b9SEric Farman if (!cp->initialized) 706812271b9SEric Farman return; 707812271b9SEric Farman 708812271b9SEric Farman cp->initialized = false; 709812271b9SEric Farman list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { 710812271b9SEric Farman for (i = 0; i < chain->ch_len; i++) { 711e7eaf91bSEric Farman pfn_array_unpin_free(chain->ch_pa + i, cp->mdev); 712812271b9SEric Farman ccwchain_cda_free(chain, i); 713812271b9SEric Farman } 714812271b9SEric Farman ccwchain_free(chain); 715812271b9SEric Farman } 7160a19e61eSDong Jia Shi } 7170a19e61eSDong Jia Shi 7180a19e61eSDong Jia Shi /** 7190a19e61eSDong Jia Shi * cp_prefetch() - translate a guest physical address channel program to 7200a19e61eSDong Jia Shi * a real-device runnable channel program. 7210a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 7220a19e61eSDong Jia Shi * 7230a19e61eSDong Jia Shi * This function translates the guest-physical-address channel program 7240a19e61eSDong Jia Shi * and stores the result to ccwchain list. @cp must have been 7250a19e61eSDong Jia Shi * initialized by a previous call with cp_init(). Otherwise, undefined 7260a19e61eSDong Jia Shi * behavior occurs. 727d66a7355SHalil Pasic * For each chain composing the channel program: 728d66a7355SHalil Pasic * - On entry ch_len holds the count of CCWs to be translated. 729d66a7355SHalil Pasic * - On exit ch_len is adjusted to the count of successfully translated CCWs. 730d66a7355SHalil Pasic * This allows cp_free to find in ch_len the count of CCWs to free in a chain. 7310a19e61eSDong Jia Shi * 7320a19e61eSDong Jia Shi * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced 7330a19e61eSDong Jia Shi * as helpers to do ccw chain translation inside the kernel. Basically 7340a19e61eSDong Jia Shi * they accept a channel program issued by a virtual machine, and 7350a19e61eSDong Jia Shi * translate the channel program to a real-device runnable channel 7360a19e61eSDong Jia Shi * program. 7370a19e61eSDong Jia Shi * 7380a19e61eSDong Jia Shi * These APIs will copy the ccws into kernel-space buffers, and update 7390a19e61eSDong Jia Shi * the guest phsical addresses with their corresponding host physical 7400a19e61eSDong Jia Shi * addresses. Then channel I/O device drivers could issue the 7410a19e61eSDong Jia Shi * translated channel program to real devices to perform an I/O 7420a19e61eSDong Jia Shi * operation. 7430a19e61eSDong Jia Shi * 7440a19e61eSDong Jia Shi * These interfaces are designed to support translation only for 7450a19e61eSDong Jia Shi * channel programs, which are generated and formatted by a 7460a19e61eSDong Jia Shi * guest. Thus this will make it possible for things like VFIO to 7470a19e61eSDong Jia Shi * leverage the interfaces to passthrough a channel I/O mediated 7480a19e61eSDong Jia Shi * device in QEMU. 7490a19e61eSDong Jia Shi * 7500a19e61eSDong Jia Shi * We support direct ccw chaining by translating them to idal ccws. 7510a19e61eSDong Jia Shi * 7520a19e61eSDong Jia Shi * Returns: 7530a19e61eSDong Jia Shi * %0 on success and a negative error value on failure. 7540a19e61eSDong Jia Shi */ 7550a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp) 7560a19e61eSDong Jia Shi { 7570a19e61eSDong Jia Shi struct ccwchain *chain; 7580a19e61eSDong Jia Shi int len, idx, ret; 7590a19e61eSDong Jia Shi 76071189f26SCornelia Huck /* this is an error in the caller */ 76171189f26SCornelia Huck if (!cp->initialized) 76271189f26SCornelia Huck return -EINVAL; 76371189f26SCornelia Huck 7640a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 7650a19e61eSDong Jia Shi len = chain->ch_len; 7660a19e61eSDong Jia Shi for (idx = 0; idx < len; idx++) { 7670a19e61eSDong Jia Shi ret = ccwchain_fetch_one(chain, idx, cp); 7680a19e61eSDong Jia Shi if (ret) 769d66a7355SHalil Pasic goto out_err; 7700a19e61eSDong Jia Shi } 7710a19e61eSDong Jia Shi } 7720a19e61eSDong Jia Shi 7730a19e61eSDong Jia Shi return 0; 774d66a7355SHalil Pasic out_err: 775d66a7355SHalil Pasic /* Only cleanup the chain elements that were actually translated. */ 776d66a7355SHalil Pasic chain->ch_len = idx; 777d66a7355SHalil Pasic list_for_each_entry_continue(chain, &cp->ccwchain_list, next) { 778d66a7355SHalil Pasic chain->ch_len = 0; 779d66a7355SHalil Pasic } 780d66a7355SHalil Pasic return ret; 7810a19e61eSDong Jia Shi } 7820a19e61eSDong Jia Shi 7830a19e61eSDong Jia Shi /** 7840a19e61eSDong Jia Shi * cp_get_orb() - get the orb of the channel program 7850a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 7860a19e61eSDong Jia Shi * @intparm: new intparm for the returned orb 7870a19e61eSDong Jia Shi * @lpm: candidate value of the logical-path mask for the returned orb 7880a19e61eSDong Jia Shi * 7890a19e61eSDong Jia Shi * This function returns the address of the updated orb of the channel 7900a19e61eSDong Jia Shi * program. Channel I/O device drivers could use this orb to issue a 7910a19e61eSDong Jia Shi * ssch. 7920a19e61eSDong Jia Shi */ 7930a19e61eSDong Jia Shi union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm) 7940a19e61eSDong Jia Shi { 7950a19e61eSDong Jia Shi union orb *orb; 7960a19e61eSDong Jia Shi struct ccwchain *chain; 7970a19e61eSDong Jia Shi struct ccw1 *cpa; 7980a19e61eSDong Jia Shi 79971189f26SCornelia Huck /* this is an error in the caller */ 80071189f26SCornelia Huck if (!cp->initialized) 80171189f26SCornelia Huck return NULL; 80271189f26SCornelia Huck 8030a19e61eSDong Jia Shi orb = &cp->orb; 8040a19e61eSDong Jia Shi 8050a19e61eSDong Jia Shi orb->cmd.intparm = intparm; 8060a19e61eSDong Jia Shi orb->cmd.fmt = 1; 8070a19e61eSDong Jia Shi orb->cmd.key = PAGE_DEFAULT_KEY >> 4; 8080a19e61eSDong Jia Shi 8090a19e61eSDong Jia Shi if (orb->cmd.lpm == 0) 8100a19e61eSDong Jia Shi orb->cmd.lpm = lpm; 8110a19e61eSDong Jia Shi 8120a19e61eSDong Jia Shi chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next); 8130a19e61eSDong Jia Shi cpa = chain->ch_ccw; 8140a19e61eSDong Jia Shi orb->cmd.cpa = (__u32) __pa(cpa); 8150a19e61eSDong Jia Shi 8160a19e61eSDong Jia Shi return orb; 8170a19e61eSDong Jia Shi } 8180a19e61eSDong Jia Shi 8190a19e61eSDong Jia Shi /** 8200a19e61eSDong Jia Shi * cp_update_scsw() - update scsw for a channel program. 8210a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 8220a19e61eSDong Jia Shi * @scsw: I/O results of the channel program and also the target to be 8230a19e61eSDong Jia Shi * updated 8240a19e61eSDong Jia Shi * 8250a19e61eSDong Jia Shi * @scsw contains the I/O results of the channel program that pointed 8260a19e61eSDong Jia Shi * to by @cp. However what @scsw->cpa stores is a host physical 8270a19e61eSDong Jia Shi * address, which is meaningless for the guest, which is waiting for 8280a19e61eSDong Jia Shi * the I/O results. 8290a19e61eSDong Jia Shi * 8300a19e61eSDong Jia Shi * This function updates @scsw->cpa to its coressponding guest physical 8310a19e61eSDong Jia Shi * address. 8320a19e61eSDong Jia Shi */ 8330a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw) 8340a19e61eSDong Jia Shi { 8350a19e61eSDong Jia Shi struct ccwchain *chain; 8360a19e61eSDong Jia Shi u32 cpa = scsw->cmd.cpa; 8372904337fSEric Farman u32 ccw_head; 8380a19e61eSDong Jia Shi 83971189f26SCornelia Huck if (!cp->initialized) 84071189f26SCornelia Huck return; 84171189f26SCornelia Huck 8420a19e61eSDong Jia Shi /* 8430a19e61eSDong Jia Shi * LATER: 8440a19e61eSDong Jia Shi * For now, only update the cmd.cpa part. We may need to deal with 8450a19e61eSDong Jia Shi * other portions of the schib as well, even if we don't return them 8460a19e61eSDong Jia Shi * in the ioctl directly. Path status changes etc. 8470a19e61eSDong Jia Shi */ 8480a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 8490a19e61eSDong Jia Shi ccw_head = (u32)(u64)chain->ch_ccw; 85015f0eb3dSEric Farman /* 85115f0eb3dSEric Farman * On successful execution, cpa points just beyond the end 85215f0eb3dSEric Farman * of the chain. 85315f0eb3dSEric Farman */ 85415f0eb3dSEric Farman if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) { 8550a19e61eSDong Jia Shi /* 8560a19e61eSDong Jia Shi * (cpa - ccw_head) is the offset value of the host 8570a19e61eSDong Jia Shi * physical ccw to its chain head. 8580a19e61eSDong Jia Shi * Adding this value to the guest physical ccw chain 8590a19e61eSDong Jia Shi * head gets us the guest cpa. 8600a19e61eSDong Jia Shi */ 8610a19e61eSDong Jia Shi cpa = chain->ch_iova + (cpa - ccw_head); 8620a19e61eSDong Jia Shi break; 8630a19e61eSDong Jia Shi } 8640a19e61eSDong Jia Shi } 8650a19e61eSDong Jia Shi 8660a19e61eSDong Jia Shi scsw->cmd.cpa = cpa; 8670a19e61eSDong Jia Shi } 8680a19e61eSDong Jia Shi 8690a19e61eSDong Jia Shi /** 8700a19e61eSDong Jia Shi * cp_iova_pinned() - check if an iova is pinned for a ccw chain. 871364e3f90SSebastian Ott * @cp: channel_program on which to perform the operation 8720a19e61eSDong Jia Shi * @iova: the iova to check 8730a19e61eSDong Jia Shi * 8740a19e61eSDong Jia Shi * If the @iova is currently pinned for the ccw chain, return true; 8750a19e61eSDong Jia Shi * else return false. 8760a19e61eSDong Jia Shi */ 8770a19e61eSDong Jia Shi bool cp_iova_pinned(struct channel_program *cp, u64 iova) 8780a19e61eSDong Jia Shi { 8790a19e61eSDong Jia Shi struct ccwchain *chain; 8800a19e61eSDong Jia Shi int i; 8810a19e61eSDong Jia Shi 88271189f26SCornelia Huck if (!cp->initialized) 88371189f26SCornelia Huck return false; 88471189f26SCornelia Huck 8850a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 8860a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) 887e7eaf91bSEric Farman if (pfn_array_iova_pinned(chain->ch_pa + i, iova)) 8880a19e61eSDong Jia Shi return true; 8890a19e61eSDong Jia Shi } 8900a19e61eSDong Jia Shi 8910a19e61eSDong Jia Shi return false; 8920a19e61eSDong Jia Shi } 893