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 11725b94d7SJared Rossi #include <linux/ratelimit.h> 120a19e61eSDong Jia Shi #include <linux/mm.h> 130a19e61eSDong Jia Shi #include <linux/slab.h> 14c2863febSNicolin Chen #include <linux/highmem.h> 150a19e61eSDong Jia Shi #include <linux/iommu.h> 160a19e61eSDong Jia Shi #include <linux/vfio.h> 170a19e61eSDong Jia Shi #include <asm/idals.h> 180a19e61eSDong Jia Shi 190a19e61eSDong Jia Shi #include "vfio_ccw_cp.h" 200a587956SJason Gunthorpe #include "vfio_ccw_private.h" 210a19e61eSDong Jia Shi 2213314605SNicolin Chen struct page_array { 2313314605SNicolin Chen /* Array that stores pages need to pin. */ 2413314605SNicolin Chen dma_addr_t *pa_iova; 2534a255e6SNicolin Chen /* Array that receives the pinned pages. */ 2634a255e6SNicolin Chen struct page **pa_page; 275c1cfb1cSDong Jia Shi /* Number of pages pinned from @pa_iova. */ 280a19e61eSDong Jia Shi int pa_nr; 290a19e61eSDong Jia Shi }; 300a19e61eSDong Jia Shi 310a19e61eSDong Jia Shi struct ccwchain { 320a19e61eSDong Jia Shi struct list_head next; 330a19e61eSDong Jia Shi struct ccw1 *ch_ccw; 340a19e61eSDong Jia Shi /* Guest physical address of the current chain. */ 350a19e61eSDong Jia Shi u64 ch_iova; 360a19e61eSDong Jia Shi /* Count of the valid ccws in chain. */ 370a19e61eSDong Jia Shi int ch_len; 380a19e61eSDong Jia Shi /* Pinned PAGEs for the original data. */ 3913314605SNicolin Chen struct page_array *ch_pa; 400a19e61eSDong Jia Shi }; 410a19e61eSDong Jia Shi 420a19e61eSDong Jia Shi /* 4313314605SNicolin Chen * page_array_alloc() - alloc memory for page array 4413314605SNicolin Chen * @pa: page_array on which to perform the operation 455c1cfb1cSDong Jia Shi * @iova: target guest physical address 465c1cfb1cSDong Jia Shi * @len: number of bytes that should be pinned from @iova 470a19e61eSDong Jia Shi * 4813314605SNicolin Chen * Attempt to allocate memory for page array. 490a19e61eSDong Jia Shi * 5013314605SNicolin Chen * Usage of page_array: 5113314605SNicolin Chen * We expect (pa_nr == 0) and (pa_iova == NULL), any field in 525c1cfb1cSDong Jia Shi * this structure will be filled in by this function. 530a19e61eSDong Jia Shi * 540a19e61eSDong Jia Shi * Returns: 5513314605SNicolin Chen * 0 if page array is allocated 5613314605SNicolin Chen * -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova is not NULL 57e4f3f18bSEric Farman * -ENOMEM if alloc failed 580a19e61eSDong Jia Shi */ 5913314605SNicolin Chen static int page_array_alloc(struct page_array *pa, u64 iova, unsigned int len) 600a19e61eSDong Jia Shi { 61e4f3f18bSEric Farman int i; 620a19e61eSDong Jia Shi 6313314605SNicolin Chen if (pa->pa_nr || pa->pa_iova) 640a19e61eSDong Jia Shi return -EINVAL; 650a19e61eSDong Jia Shi 660a19e61eSDong Jia Shi pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 670a19e61eSDong Jia Shi if (!pa->pa_nr) 680a19e61eSDong Jia Shi return -EINVAL; 690a19e61eSDong Jia Shi 7013314605SNicolin Chen pa->pa_iova = kcalloc(pa->pa_nr, 7134a255e6SNicolin Chen sizeof(*pa->pa_iova) + sizeof(*pa->pa_page), 720a19e61eSDong Jia Shi GFP_KERNEL); 7313314605SNicolin Chen if (unlikely(!pa->pa_iova)) { 74c1ab6926SFarhan Ali pa->pa_nr = 0; 750a19e61eSDong Jia Shi return -ENOMEM; 76c1ab6926SFarhan Ali } 7734a255e6SNicolin Chen pa->pa_page = (struct page **)&pa->pa_iova[pa->pa_nr]; 780a19e61eSDong Jia Shi 7913314605SNicolin Chen pa->pa_iova[0] = iova; 8034a255e6SNicolin Chen pa->pa_page[0] = NULL; 81c34a12e6SEric Farman for (i = 1; i < pa->pa_nr; i++) { 8213314605SNicolin Chen pa->pa_iova[i] = pa->pa_iova[i - 1] + PAGE_SIZE; 8334a255e6SNicolin Chen pa->pa_page[i] = NULL; 84c34a12e6SEric Farman } 850a19e61eSDong Jia Shi 86e4f3f18bSEric Farman return 0; 87e4f3f18bSEric Farman } 88e4f3f18bSEric Farman 89e4f3f18bSEric Farman /* 9013314605SNicolin Chen * page_array_unpin() - Unpin user pages in memory 9113314605SNicolin Chen * @pa: page_array on which to perform the operation 92cfedb3d5SNicolin Chen * @vdev: the vfio device to perform the operation 93cfedb3d5SNicolin Chen * @pa_nr: number of user pages to unpin 94cfedb3d5SNicolin Chen * 95cfedb3d5SNicolin Chen * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0, 96cfedb3d5SNicolin Chen * otherwise only clear pa->pa_nr 97cfedb3d5SNicolin Chen */ 9813314605SNicolin Chen static void page_array_unpin(struct page_array *pa, 99cfedb3d5SNicolin Chen struct vfio_device *vdev, int pa_nr) 100cfedb3d5SNicolin Chen { 101cfedb3d5SNicolin Chen int unpinned = 0, npage = 1; 102cfedb3d5SNicolin Chen 103cfedb3d5SNicolin Chen while (unpinned < pa_nr) { 10413314605SNicolin Chen dma_addr_t *first = &pa->pa_iova[unpinned]; 10513314605SNicolin Chen dma_addr_t *last = &first[npage]; 106cfedb3d5SNicolin Chen 107cfedb3d5SNicolin Chen if (unpinned + npage < pa_nr && 10813314605SNicolin Chen *first + npage * PAGE_SIZE == *last) { 109cfedb3d5SNicolin Chen npage++; 110cfedb3d5SNicolin Chen continue; 111cfedb3d5SNicolin Chen } 112cfedb3d5SNicolin Chen 11313314605SNicolin Chen vfio_unpin_pages(vdev, *first, npage); 114cfedb3d5SNicolin Chen unpinned += npage; 115cfedb3d5SNicolin Chen npage = 1; 116cfedb3d5SNicolin Chen } 117cfedb3d5SNicolin Chen 118cfedb3d5SNicolin Chen pa->pa_nr = 0; 119cfedb3d5SNicolin Chen } 120cfedb3d5SNicolin Chen 121cfedb3d5SNicolin Chen /* 12213314605SNicolin Chen * page_array_pin() - Pin user pages in memory 12313314605SNicolin Chen * @pa: page_array on which to perform the operation 1248a54e238SEric Farman * @vdev: the vfio device to perform pin operations 125e4f3f18bSEric Farman * 126e4f3f18bSEric Farman * Returns number of pages pinned upon success. 127e4f3f18bSEric Farman * If the pin request partially succeeds, or fails completely, 128e4f3f18bSEric Farman * all pages are left unpinned and a negative error value is returned. 129e4f3f18bSEric Farman */ 13013314605SNicolin Chen static int page_array_pin(struct page_array *pa, struct vfio_device *vdev) 131e4f3f18bSEric Farman { 132cfedb3d5SNicolin Chen int pinned = 0, npage = 1; 133e4f3f18bSEric Farman int ret = 0; 134e4f3f18bSEric Farman 135cfedb3d5SNicolin Chen while (pinned < pa->pa_nr) { 13613314605SNicolin Chen dma_addr_t *first = &pa->pa_iova[pinned]; 13713314605SNicolin Chen dma_addr_t *last = &first[npage]; 1385c1cfb1cSDong Jia Shi 139cfedb3d5SNicolin Chen if (pinned + npage < pa->pa_nr && 14013314605SNicolin Chen *first + npage * PAGE_SIZE == *last) { 141cfedb3d5SNicolin Chen npage++; 142cfedb3d5SNicolin Chen continue; 143cfedb3d5SNicolin Chen } 144cfedb3d5SNicolin Chen 14513314605SNicolin Chen ret = vfio_pin_pages(vdev, *first, npage, 146cfedb3d5SNicolin Chen IOMMU_READ | IOMMU_WRITE, 14734a255e6SNicolin Chen &pa->pa_page[pinned]); 1485c1cfb1cSDong Jia Shi if (ret < 0) { 1495c1cfb1cSDong Jia Shi goto err_out; 150cfedb3d5SNicolin Chen } else if (ret > 0 && ret != npage) { 151cfedb3d5SNicolin Chen pinned += ret; 1520a19e61eSDong Jia Shi ret = -EINVAL; 1535c1cfb1cSDong Jia Shi goto err_out; 1545c1cfb1cSDong Jia Shi } 155cfedb3d5SNicolin Chen pinned += npage; 156cfedb3d5SNicolin Chen npage = 1; 157cfedb3d5SNicolin Chen } 1580a19e61eSDong Jia Shi 1590a19e61eSDong Jia Shi return ret; 1605c1cfb1cSDong Jia Shi 1615c1cfb1cSDong Jia Shi err_out: 16213314605SNicolin Chen page_array_unpin(pa, vdev, pinned); 1635c1cfb1cSDong Jia Shi return ret; 1645c1cfb1cSDong Jia Shi } 1655c1cfb1cSDong Jia Shi 1665c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */ 16713314605SNicolin Chen static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev) 1685c1cfb1cSDong Jia Shi { 16913314605SNicolin Chen page_array_unpin(pa, vdev, pa->pa_nr); 17013314605SNicolin Chen kfree(pa->pa_iova); 1710a19e61eSDong Jia Shi } 1720a19e61eSDong Jia Shi 1735a4fe7c4SEric Farman static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length) 1740a19e61eSDong Jia Shi { 1755a4fe7c4SEric Farman u64 iova_pfn_start = iova >> PAGE_SHIFT; 1765a4fe7c4SEric Farman u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT; 1775a4fe7c4SEric Farman u64 pfn; 1780a19e61eSDong Jia Shi int i; 1790a19e61eSDong Jia Shi 1805a4fe7c4SEric Farman for (i = 0; i < pa->pa_nr; i++) { 1815a4fe7c4SEric Farman pfn = pa->pa_iova[i] >> PAGE_SHIFT; 1825a4fe7c4SEric Farman if (pfn >= iova_pfn_start && pfn <= iova_pfn_end) 1830a19e61eSDong Jia Shi return true; 1845a4fe7c4SEric Farman } 1850a19e61eSDong Jia Shi 1860a19e61eSDong Jia Shi return false; 1870a19e61eSDong Jia Shi } 18813314605SNicolin Chen /* Create the list of IDAL words for a page_array. */ 18913314605SNicolin Chen static inline void page_array_idal_create_words(struct page_array *pa, 1900a19e61eSDong Jia Shi unsigned long *idaws) 1910a19e61eSDong Jia Shi { 192e7eaf91bSEric Farman int i; 1930a19e61eSDong Jia Shi 1940a19e61eSDong Jia Shi /* 1950a19e61eSDong Jia Shi * Idal words (execept the first one) rely on the memory being 4k 1960a19e61eSDong Jia Shi * aligned. If a user virtual address is 4K aligned, then it's 1970a19e61eSDong Jia Shi * corresponding kernel physical address will also be 4K aligned. Thus 1980a19e61eSDong Jia Shi * there will be no problem here to simply use the phys to create an 1990a19e61eSDong Jia Shi * idaw. 2000a19e61eSDong Jia Shi */ 201e7eaf91bSEric Farman 202e7eaf91bSEric Farman for (i = 0; i < pa->pa_nr; i++) 20334a255e6SNicolin Chen idaws[i] = page_to_phys(pa->pa_page[i]); 2048aabf0edSEric Farman 2058aabf0edSEric Farman /* Adjust the first IDAW, since it may not start on a page boundary */ 20613314605SNicolin Chen idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1); 2070a19e61eSDong Jia Shi } 2080a19e61eSDong Jia Shi 209dbd66558SCornelia Huck static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len) 2107f8e89a8SEric Farman { 2117f8e89a8SEric Farman struct ccw0 ccw0; 2127f8e89a8SEric Farman struct ccw1 *pccw1 = source; 2137f8e89a8SEric Farman int i; 2147f8e89a8SEric Farman 2157f8e89a8SEric Farman for (i = 0; i < len; i++) { 2167f8e89a8SEric Farman ccw0 = *(struct ccw0 *)pccw1; 2177f8e89a8SEric Farman if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) { 2187f8e89a8SEric Farman pccw1->cmd_code = CCW_CMD_TIC; 2197f8e89a8SEric Farman pccw1->flags = 0; 2207f8e89a8SEric Farman pccw1->count = 0; 2217f8e89a8SEric Farman } else { 2227f8e89a8SEric Farman pccw1->cmd_code = ccw0.cmd_code; 2237f8e89a8SEric Farman pccw1->flags = ccw0.flags; 2247f8e89a8SEric Farman pccw1->count = ccw0.count; 2257f8e89a8SEric Farman } 2267f8e89a8SEric Farman pccw1->cda = ccw0.cda; 2277f8e89a8SEric Farman pccw1++; 2287f8e89a8SEric Farman } 2297f8e89a8SEric Farman } 2300a19e61eSDong Jia Shi 2310a19e61eSDong Jia Shi /* 2320a19e61eSDong Jia Shi * Helpers to operate ccwchain. 2330a19e61eSDong Jia Shi */ 2345d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02) 2355d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C) 2365d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE) 2375d87fbf7SEric Farman 2380a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP) 2390a19e61eSDong Jia Shi 2400a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC) 2410a19e61eSDong Jia Shi 2420a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA) 2435d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP) 2440a19e61eSDong Jia Shi 2450a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC)) 2460a19e61eSDong Jia Shi 24748bd0eeeSEric Farman /* 2485d87fbf7SEric Farman * ccw_does_data_transfer() 2495d87fbf7SEric Farman * 2505d87fbf7SEric Farman * Determine whether a CCW will move any data, such that the guest pages 2515d87fbf7SEric Farman * would need to be pinned before performing the I/O. 2525d87fbf7SEric Farman * 2535d87fbf7SEric Farman * Returns 1 if yes, 0 if no. 2545d87fbf7SEric Farman */ 2555d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw) 2565d87fbf7SEric Farman { 257453eac31SEric Farman /* If the count field is zero, then no data will be transferred */ 258453eac31SEric Farman if (ccw->count == 0) 259453eac31SEric Farman return 0; 260453eac31SEric Farman 2619b6e57e5SEric Farman /* If the command is a NOP, then no data will be transferred */ 2629b6e57e5SEric Farman if (ccw_is_noop(ccw)) 2639b6e57e5SEric Farman return 0; 2649b6e57e5SEric Farman 2655d87fbf7SEric Farman /* If the skip flag is off, then data will be transferred */ 2665d87fbf7SEric Farman if (!ccw_is_skip(ccw)) 2675d87fbf7SEric Farman return 1; 2685d87fbf7SEric Farman 2695d87fbf7SEric Farman /* 2705d87fbf7SEric Farman * If the skip flag is on, it is only meaningful if the command 2715d87fbf7SEric Farman * code is a read, read backward, sense, or sense ID. In those 2725d87fbf7SEric Farman * cases, no data will be transferred. 2735d87fbf7SEric Farman */ 2745d87fbf7SEric Farman if (ccw_is_read(ccw) || ccw_is_read_backward(ccw)) 2755d87fbf7SEric Farman return 0; 2765d87fbf7SEric Farman 2775d87fbf7SEric Farman if (ccw_is_sense(ccw)) 2785d87fbf7SEric Farman return 0; 2795d87fbf7SEric Farman 2805d87fbf7SEric Farman /* The skip flag is on, but it is ignored for this command code. */ 2815d87fbf7SEric Farman return 1; 2825d87fbf7SEric Farman } 2835d87fbf7SEric Farman 2845d87fbf7SEric Farman /* 28548bd0eeeSEric Farman * is_cpa_within_range() 28648bd0eeeSEric Farman * 28748bd0eeeSEric Farman * @cpa: channel program address being questioned 28848bd0eeeSEric Farman * @head: address of the beginning of a CCW chain 28948bd0eeeSEric Farman * @len: number of CCWs within the chain 29048bd0eeeSEric Farman * 29148bd0eeeSEric Farman * Determine whether the address of a CCW (whether a new chain, 29248bd0eeeSEric Farman * or the target of a TIC) falls within a range (including the end points). 29348bd0eeeSEric Farman * 29448bd0eeeSEric Farman * Returns 1 if yes, 0 if no. 29548bd0eeeSEric Farman */ 29648bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len) 29748bd0eeeSEric Farman { 29848bd0eeeSEric Farman u32 tail = head + (len - 1) * sizeof(struct ccw1); 29948bd0eeeSEric Farman 30048bd0eeeSEric Farman return (head <= cpa && cpa <= tail); 30148bd0eeeSEric Farman } 30248bd0eeeSEric Farman 30348bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len) 30448bd0eeeSEric Farman { 30548bd0eeeSEric Farman if (!ccw_is_tic(ccw)) 30648bd0eeeSEric Farman return 0; 30748bd0eeeSEric Farman 30848bd0eeeSEric Farman return is_cpa_within_range(ccw->cda, head, len); 30948bd0eeeSEric Farman } 31048bd0eeeSEric Farman 3110a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len) 3120a19e61eSDong Jia Shi { 3130a19e61eSDong Jia Shi struct ccwchain *chain; 3140a19e61eSDong Jia Shi 315*4b946d65SEric Farman chain = kzalloc(sizeof(*chain), GFP_KERNEL); 3160a19e61eSDong Jia Shi if (!chain) 3170a19e61eSDong Jia Shi return NULL; 3180a19e61eSDong Jia Shi 319*4b946d65SEric Farman chain->ch_ccw = kcalloc(len, sizeof(*chain->ch_ccw), GFP_DMA | GFP_KERNEL); 320*4b946d65SEric Farman if (!chain->ch_ccw) 321*4b946d65SEric Farman goto out_err; 3220a19e61eSDong Jia Shi 323*4b946d65SEric Farman chain->ch_pa = kcalloc(len, sizeof(*chain->ch_pa), GFP_KERNEL); 324*4b946d65SEric Farman if (!chain->ch_pa) 325*4b946d65SEric Farman goto out_err; 3260a19e61eSDong Jia Shi 3270a19e61eSDong Jia Shi list_add_tail(&chain->next, &cp->ccwchain_list); 3280a19e61eSDong Jia Shi 3290a19e61eSDong Jia Shi return chain; 330*4b946d65SEric Farman 331*4b946d65SEric Farman out_err: 332*4b946d65SEric Farman kfree(chain->ch_ccw); 333*4b946d65SEric Farman kfree(chain); 334*4b946d65SEric Farman return NULL; 3350a19e61eSDong Jia Shi } 3360a19e61eSDong Jia Shi 3370a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain) 3380a19e61eSDong Jia Shi { 3390a19e61eSDong Jia Shi list_del(&chain->next); 340*4b946d65SEric Farman kfree(chain->ch_pa); 341*4b946d65SEric Farman kfree(chain->ch_ccw); 3420a19e61eSDong Jia Shi kfree(chain); 3430a19e61eSDong Jia Shi } 3440a19e61eSDong Jia Shi 3450a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */ 3460a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx) 3470a19e61eSDong Jia Shi { 348*4b946d65SEric Farman struct ccw1 *ccw = &chain->ch_ccw[idx]; 3490a19e61eSDong Jia Shi 3509b6e57e5SEric Farman if (ccw_is_tic(ccw)) 351408358b5SJason J. Herne return; 3520a19e61eSDong Jia Shi 3535de2322dSEric Farman kfree(phys_to_virt(ccw->cda)); 3540a19e61eSDong Jia Shi } 3550a19e61eSDong Jia Shi 3560a19e61eSDong Jia Shi /** 3570a19e61eSDong Jia Shi * ccwchain_calc_length - calculate the length of the ccw chain. 3580a19e61eSDong Jia Shi * @iova: guest physical address of the target ccw chain 3590a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 3600a19e61eSDong Jia Shi * 3610a19e61eSDong Jia Shi * This is the chain length not considering any TICs. 3620a19e61eSDong Jia Shi * You need to do a new round for each TIC target. 3630a19e61eSDong Jia Shi * 364fb9e7880SHalil Pasic * The program is also validated for absence of not yet supported 365fb9e7880SHalil Pasic * indirect data addressing scenarios. 366fb9e7880SHalil Pasic * 3670a19e61eSDong Jia Shi * Returns: the length of the ccw chain or -errno. 3680a19e61eSDong Jia Shi */ 3690a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp) 3700a19e61eSDong Jia Shi { 3711d897e47SEric Farman struct ccw1 *ccw = cp->guest_cp; 372ded563f3SEric Farman int cnt = 0; 3730a19e61eSDong Jia Shi 3740a19e61eSDong Jia Shi do { 3750a19e61eSDong Jia Shi cnt++; 3760a19e61eSDong Jia Shi 377fb9e7880SHalil Pasic /* 378fb9e7880SHalil Pasic * As we don't want to fail direct addressing even if the 379fb9e7880SHalil Pasic * orb specified one of the unsupported formats, we defer 380fb9e7880SHalil Pasic * checking for IDAWs in unsupported formats to here. 381fb9e7880SHalil Pasic */ 3821d897e47SEric Farman if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw)) 383fb9e7880SHalil Pasic return -EOPNOTSUPP; 384fb9e7880SHalil Pasic 38548bd0eeeSEric Farman /* 38648bd0eeeSEric Farman * We want to keep counting if the current CCW has the 38748bd0eeeSEric Farman * command-chaining flag enabled, or if it is a TIC CCW 38848bd0eeeSEric Farman * that loops back into the current chain. The latter 38948bd0eeeSEric Farman * is used for device orientation, where the CCW PRIOR to 39048bd0eeeSEric Farman * the TIC can either jump to the TIC or a CCW immediately 39148bd0eeeSEric Farman * after the TIC, depending on the results of its operation. 39248bd0eeeSEric Farman */ 39348bd0eeeSEric Farman if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt)) 3940a19e61eSDong Jia Shi break; 3950a19e61eSDong Jia Shi 3960a19e61eSDong Jia Shi ccw++; 3970a19e61eSDong Jia Shi } while (cnt < CCWCHAIN_LEN_MAX + 1); 3980a19e61eSDong Jia Shi 3990a19e61eSDong Jia Shi if (cnt == CCWCHAIN_LEN_MAX + 1) 4000a19e61eSDong Jia Shi cnt = -EINVAL; 4010a19e61eSDong Jia Shi 4020a19e61eSDong Jia Shi return cnt; 4030a19e61eSDong Jia Shi } 4040a19e61eSDong Jia Shi 4050a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp) 4060a19e61eSDong Jia Shi { 4070a19e61eSDong Jia Shi struct ccwchain *chain; 4082904337fSEric Farman u32 ccw_head; 4090a19e61eSDong Jia Shi 4100a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 4110a19e61eSDong Jia Shi ccw_head = chain->ch_iova; 4122904337fSEric Farman if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len)) 4130a19e61eSDong Jia Shi return 1; 4140a19e61eSDong Jia Shi } 4150a19e61eSDong Jia Shi 4160a19e61eSDong Jia Shi return 0; 4170a19e61eSDong Jia Shi } 4180a19e61eSDong Jia Shi 4190a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, 4200a19e61eSDong Jia Shi struct channel_program *cp); 4210a19e61eSDong Jia Shi 422363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp) 4230a19e61eSDong Jia Shi { 4240a587956SJason Gunthorpe struct vfio_device *vdev = 4250a587956SJason Gunthorpe &container_of(cp, struct vfio_ccw_private, cp)->vdev; 4260a19e61eSDong Jia Shi struct ccwchain *chain; 4278b515be5SFarhan Ali int len, ret; 4280a19e61eSDong Jia Shi 429ded563f3SEric Farman /* Copy 2K (the most we support today) of possible CCWs */ 430c5e8083fSEric Farman ret = vfio_dma_rw(vdev, cda, cp->guest_cp, CCWCHAIN_LEN_MAX * sizeof(struct ccw1), false); 431c5e8083fSEric Farman if (ret) 432c5e8083fSEric Farman return ret; 433ded563f3SEric Farman 4347f8e89a8SEric Farman /* Convert any Format-0 CCWs to Format-1 */ 4357f8e89a8SEric Farman if (!cp->orb.cmd.fmt) 436c382cbc6SEric Farman convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX); 4377f8e89a8SEric Farman 438ded563f3SEric Farman /* Count the CCWs in the current chain */ 439363fe5f7SEric Farman len = ccwchain_calc_length(cda, cp); 4400a19e61eSDong Jia Shi if (len < 0) 4410a19e61eSDong Jia Shi return len; 4420a19e61eSDong Jia Shi 4430a19e61eSDong Jia Shi /* Need alloc a new chain for this one. */ 4440a19e61eSDong Jia Shi chain = ccwchain_alloc(cp, len); 4450a19e61eSDong Jia Shi if (!chain) 4460a19e61eSDong Jia Shi return -ENOMEM; 447*4b946d65SEric Farman 448*4b946d65SEric Farman chain->ch_len = len; 449363fe5f7SEric Farman chain->ch_iova = cda; 4500a19e61eSDong Jia Shi 45162465902SEric Farman /* Copy the actual CCWs into the new chain */ 45262465902SEric Farman memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1)); 4530a19e61eSDong Jia Shi 4540a19e61eSDong Jia Shi /* Loop for tics on this new chain. */ 4558b515be5SFarhan Ali ret = ccwchain_loop_tic(chain, cp); 4568b515be5SFarhan Ali 4578b515be5SFarhan Ali if (ret) 4588b515be5SFarhan Ali ccwchain_free(chain); 4598b515be5SFarhan Ali 4608b515be5SFarhan Ali return ret; 4610a19e61eSDong Jia Shi } 4620a19e61eSDong Jia Shi 4630a19e61eSDong Jia Shi /* Loop for TICs. */ 4640a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp) 4650a19e61eSDong Jia Shi { 4660a19e61eSDong Jia Shi struct ccw1 *tic; 4670a19e61eSDong Jia Shi int i, ret; 4680a19e61eSDong Jia Shi 4690a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) { 470*4b946d65SEric Farman tic = &chain->ch_ccw[i]; 4710a19e61eSDong Jia Shi 4720a19e61eSDong Jia Shi if (!ccw_is_tic(tic)) 4730a19e61eSDong Jia Shi continue; 4740a19e61eSDong Jia Shi 475e64bd689SEric Farman /* May transfer to an existing chain. */ 476e64bd689SEric Farman if (tic_target_chain_exists(tic, cp)) 477e64bd689SEric Farman continue; 478e64bd689SEric Farman 479363fe5f7SEric Farman /* Build a ccwchain for the next segment */ 480363fe5f7SEric Farman ret = ccwchain_handle_ccw(tic->cda, cp); 4810a19e61eSDong Jia Shi if (ret) 4820a19e61eSDong Jia Shi return ret; 4830a19e61eSDong Jia Shi } 4840a19e61eSDong Jia Shi 4850a19e61eSDong Jia Shi return 0; 4860a19e61eSDong Jia Shi } 4870a19e61eSDong Jia Shi 488a4c60404SEric Farman static int ccwchain_fetch_tic(struct ccw1 *ccw, 4890a19e61eSDong Jia Shi struct channel_program *cp) 4900a19e61eSDong Jia Shi { 4910a19e61eSDong Jia Shi struct ccwchain *iter; 4922904337fSEric Farman u32 ccw_head; 4930a19e61eSDong Jia Shi 4940a19e61eSDong Jia Shi list_for_each_entry(iter, &cp->ccwchain_list, next) { 4950a19e61eSDong Jia Shi ccw_head = iter->ch_iova; 4962904337fSEric Farman if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) { 497c389377cSJason J. Herne ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) + 4980a19e61eSDong Jia Shi (ccw->cda - ccw_head)); 4990a19e61eSDong Jia Shi return 0; 5000a19e61eSDong Jia Shi } 5010a19e61eSDong Jia Shi } 5020a19e61eSDong Jia Shi 5030a19e61eSDong Jia Shi return -EFAULT; 5040a19e61eSDong Jia Shi } 5050a19e61eSDong Jia Shi 506a4c60404SEric Farman static int ccwchain_fetch_ccw(struct ccw1 *ccw, 507a4c60404SEric Farman struct page_array *pa, 5080a19e61eSDong Jia Shi struct channel_program *cp) 5090a19e61eSDong Jia Shi { 5100a587956SJason Gunthorpe struct vfio_device *vdev = 5110a587956SJason Gunthorpe &container_of(cp, struct vfio_ccw_private, cp)->vdev; 51201aa26c6SEric Farman u64 iova; 5130a19e61eSDong Jia Shi unsigned long *idaws; 5146238f921SDong Jia Shi int ret; 515453eac31SEric Farman int bytes = 1; 51601aa26c6SEric Farman int idaw_nr, idal_len; 51701aa26c6SEric Farman int i; 5180a19e61eSDong Jia Shi 519e8573b39SEric Farman if (ccw->count) 520453eac31SEric Farman bytes = ccw->count; 521e8573b39SEric Farman 522e8573b39SEric Farman /* Calculate size of IDAL */ 52301aa26c6SEric Farman if (ccw_is_idal(ccw)) { 52401aa26c6SEric Farman /* Read first IDAW to see if it's 4K-aligned or not. */ 52501aa26c6SEric Farman /* All subsequent IDAws will be 4K-aligned. */ 526c5e8083fSEric Farman ret = vfio_dma_rw(vdev, ccw->cda, &iova, sizeof(iova), false); 52701aa26c6SEric Farman if (ret) 52801aa26c6SEric Farman return ret; 52901aa26c6SEric Farman } else { 53001aa26c6SEric Farman iova = ccw->cda; 53101aa26c6SEric Farman } 53201aa26c6SEric Farman idaw_nr = idal_nr_words((void *)iova, bytes); 53301aa26c6SEric Farman idal_len = idaw_nr * sizeof(*idaws); 534e8573b39SEric Farman 535e8573b39SEric Farman /* Allocate an IDAL from host storage */ 536e8573b39SEric Farman idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL); 537e8573b39SEric Farman if (!idaws) { 538e8573b39SEric Farman ret = -ENOMEM; 539e8573b39SEric Farman goto out_init; 5404cebc5d6SDong Jia Shi } 5414cebc5d6SDong Jia Shi 5420a19e61eSDong Jia Shi /* 54313314605SNicolin Chen * Allocate an array of pages to pin/translate. 54401aa26c6SEric Farman * The number of pages is actually the count of the idaws 54501aa26c6SEric Farman * required for the data transfer, since we only only support 54601aa26c6SEric Farman * 4K IDAWs today. 5470a19e61eSDong Jia Shi */ 54813314605SNicolin Chen ret = page_array_alloc(pa, iova, bytes); 549e4f3f18bSEric Farman if (ret < 0) 550e8573b39SEric Farman goto out_free_idaws; 551e4f3f18bSEric Farman 55201aa26c6SEric Farman if (ccw_is_idal(ccw)) { 55301aa26c6SEric Farman /* Copy guest IDAL into host IDAL */ 554c5e8083fSEric Farman ret = vfio_dma_rw(vdev, ccw->cda, idaws, idal_len, false); 55501aa26c6SEric Farman if (ret) 55601aa26c6SEric Farman goto out_unpin; 55701aa26c6SEric Farman 55801aa26c6SEric Farman /* 55913314605SNicolin Chen * Copy guest IDAWs into page_array, in case the memory they 56001aa26c6SEric Farman * occupy is not contiguous. 56101aa26c6SEric Farman */ 56201aa26c6SEric Farman for (i = 0; i < idaw_nr; i++) 56313314605SNicolin Chen pa->pa_iova[i] = idaws[i]; 56401aa26c6SEric Farman } else { 56501aa26c6SEric Farman /* 56613314605SNicolin Chen * No action is required here; the iova addresses in page_array 56713314605SNicolin Chen * were initialized sequentially in page_array_alloc() beginning 56801aa26c6SEric Farman * with the contents of ccw->cda. 56901aa26c6SEric Farman */ 57001aa26c6SEric Farman } 57101aa26c6SEric Farman 5725d87fbf7SEric Farman if (ccw_does_data_transfer(ccw)) { 57313314605SNicolin Chen ret = page_array_pin(pa, vdev); 5746238f921SDong Jia Shi if (ret < 0) 575806212f9SEric Farman goto out_unpin; 5765d87fbf7SEric Farman } else { 577e7eaf91bSEric Farman pa->pa_nr = 0; 5785d87fbf7SEric Farman } 5790a19e61eSDong Jia Shi 5800a19e61eSDong Jia Shi ccw->cda = (__u32) virt_to_phys(idaws); 5810a19e61eSDong Jia Shi ccw->flags |= CCW_FLAG_IDA; 5820a19e61eSDong Jia Shi 58313314605SNicolin Chen /* Populate the IDAL with pinned/translated addresses from page */ 58413314605SNicolin Chen page_array_idal_create_words(pa, idaws); 5850a19e61eSDong Jia Shi 5860a19e61eSDong Jia Shi return 0; 5876238f921SDong Jia Shi 5886238f921SDong Jia Shi out_unpin: 58913314605SNicolin Chen page_array_unpin_free(pa, vdev); 590e8573b39SEric Farman out_free_idaws: 591e8573b39SEric Farman kfree(idaws); 5926238f921SDong Jia Shi out_init: 5936238f921SDong Jia Shi ccw->cda = 0; 5946238f921SDong Jia Shi return ret; 5950a19e61eSDong Jia Shi } 5960a19e61eSDong Jia Shi 5970a19e61eSDong Jia Shi /* 5980a19e61eSDong Jia Shi * Fetch one ccw. 5990a19e61eSDong Jia Shi * To reduce memory copy, we'll pin the cda page in memory, 6000a19e61eSDong Jia Shi * and to get rid of the cda 2G limitiaion of ccw1, we'll translate 6010a19e61eSDong Jia Shi * direct ccws to idal ccws. 6020a19e61eSDong Jia Shi */ 603a4c60404SEric Farman static int ccwchain_fetch_one(struct ccw1 *ccw, 604a4c60404SEric Farman struct page_array *pa, 6050a19e61eSDong Jia Shi struct channel_program *cp) 606a4c60404SEric Farman 6070a19e61eSDong Jia Shi { 6080a19e61eSDong Jia Shi if (ccw_is_tic(ccw)) 609a4c60404SEric Farman return ccwchain_fetch_tic(ccw, cp); 6100a19e61eSDong Jia Shi 611a4c60404SEric Farman return ccwchain_fetch_ccw(ccw, pa, cp); 6120a19e61eSDong Jia Shi } 6130a19e61eSDong Jia Shi 6140a19e61eSDong Jia Shi /** 6150a19e61eSDong Jia Shi * cp_init() - allocate ccwchains for a channel program. 6160a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6170a19e61eSDong Jia Shi * @orb: control block for the channel program from the guest 6180a19e61eSDong Jia Shi * 6190a19e61eSDong Jia Shi * This creates one or more ccwchain(s), and copies the raw data of 6200a19e61eSDong Jia Shi * the target channel program from @orb->cmd.iova to the new ccwchain(s). 6210a19e61eSDong Jia Shi * 6220a19e61eSDong Jia Shi * Limitations: 623725b94d7SJared Rossi * 1. Supports idal(c64) ccw chaining. 624725b94d7SJared Rossi * 2. Supports 4k idaw. 6250a19e61eSDong Jia Shi * 6260a19e61eSDong Jia Shi * Returns: 6270a19e61eSDong Jia Shi * %0 on success and a negative error value on failure. 6280a19e61eSDong Jia Shi */ 6290a587956SJason Gunthorpe int cp_init(struct channel_program *cp, union orb *orb) 6300a19e61eSDong Jia Shi { 6310a587956SJason Gunthorpe struct vfio_device *vdev = 6320a587956SJason Gunthorpe &container_of(cp, struct vfio_ccw_private, cp)->vdev; 633725b94d7SJared Rossi /* custom ratelimit used to avoid flood during guest IPL */ 634725b94d7SJared Rossi static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1); 63599afcb05SEric Farman int ret; 6360a19e61eSDong Jia Shi 637c6c82e0cSEric Farman /* this is an error in the caller */ 638c6c82e0cSEric Farman if (cp->initialized) 639c6c82e0cSEric Farman return -EBUSY; 640c6c82e0cSEric Farman 6410a19e61eSDong Jia Shi /* 642725b94d7SJared Rossi * We only support prefetching the channel program. We assume all channel 643725b94d7SJared Rossi * programs executed by supported guests likewise support prefetching. 644725b94d7SJared Rossi * Executing a channel program that does not specify prefetching will 645725b94d7SJared Rossi * typically not cause an error, but a warning is issued to help identify 646725b94d7SJared Rossi * the problem if something does break. 6470a19e61eSDong Jia Shi */ 648725b94d7SJared Rossi if (!orb->cmd.pfch && __ratelimit(&ratelimit_state)) 6490a587956SJason Gunthorpe dev_warn( 6500a587956SJason Gunthorpe vdev->dev, 6510a587956SJason Gunthorpe "Prefetching channel program even though prefetch not specified in ORB"); 6520a19e61eSDong Jia Shi 6530a19e61eSDong Jia Shi INIT_LIST_HEAD(&cp->ccwchain_list); 6540a19e61eSDong Jia Shi memcpy(&cp->orb, orb, sizeof(*orb)); 6550a19e61eSDong Jia Shi 65699afcb05SEric Farman /* Build a ccwchain for the first CCW segment */ 65799afcb05SEric Farman ret = ccwchain_handle_ccw(orb->cmd.cpa, cp); 65899afcb05SEric Farman 659254cb663SEric Farman if (!ret) 660c9f597a4SFarhan Ali cp->initialized = true; 661c9f597a4SFarhan Ali 6620a19e61eSDong Jia Shi return ret; 6630a19e61eSDong Jia Shi } 6640a19e61eSDong Jia Shi 6650a19e61eSDong Jia Shi 6660a19e61eSDong Jia Shi /** 6670a19e61eSDong Jia Shi * cp_free() - free resources for channel program. 6680a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6690a19e61eSDong Jia Shi * 6700a19e61eSDong Jia Shi * This unpins the memory pages and frees the memory space occupied by 6710a19e61eSDong Jia Shi * @cp, which must have been returned by a previous call to cp_init(). 6720a19e61eSDong Jia Shi * Otherwise, undefined behavior occurs. 6730a19e61eSDong Jia Shi */ 6740a19e61eSDong Jia Shi void cp_free(struct channel_program *cp) 6750a19e61eSDong Jia Shi { 6760a587956SJason Gunthorpe struct vfio_device *vdev = 6770a587956SJason Gunthorpe &container_of(cp, struct vfio_ccw_private, cp)->vdev; 678812271b9SEric Farman struct ccwchain *chain, *temp; 679812271b9SEric Farman int i; 680812271b9SEric Farman 681812271b9SEric Farman if (!cp->initialized) 682812271b9SEric Farman return; 683812271b9SEric Farman 684812271b9SEric Farman cp->initialized = false; 685812271b9SEric Farman list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { 686812271b9SEric Farman for (i = 0; i < chain->ch_len; i++) { 687*4b946d65SEric Farman page_array_unpin_free(&chain->ch_pa[i], vdev); 688812271b9SEric Farman ccwchain_cda_free(chain, i); 689812271b9SEric Farman } 690812271b9SEric Farman ccwchain_free(chain); 691812271b9SEric Farman } 6920a19e61eSDong Jia Shi } 6930a19e61eSDong Jia Shi 6940a19e61eSDong Jia Shi /** 6950a19e61eSDong Jia Shi * cp_prefetch() - translate a guest physical address channel program to 6960a19e61eSDong Jia Shi * a real-device runnable channel program. 6970a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6980a19e61eSDong Jia Shi * 6990a19e61eSDong Jia Shi * This function translates the guest-physical-address channel program 7000a19e61eSDong Jia Shi * and stores the result to ccwchain list. @cp must have been 7010a19e61eSDong Jia Shi * initialized by a previous call with cp_init(). Otherwise, undefined 7020a19e61eSDong Jia Shi * behavior occurs. 703d66a7355SHalil Pasic * For each chain composing the channel program: 704d66a7355SHalil Pasic * - On entry ch_len holds the count of CCWs to be translated. 705d66a7355SHalil Pasic * - On exit ch_len is adjusted to the count of successfully translated CCWs. 706d66a7355SHalil Pasic * This allows cp_free to find in ch_len the count of CCWs to free in a chain. 7070a19e61eSDong Jia Shi * 7080a19e61eSDong Jia Shi * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced 7090a19e61eSDong Jia Shi * as helpers to do ccw chain translation inside the kernel. Basically 7100a19e61eSDong Jia Shi * they accept a channel program issued by a virtual machine, and 7110a19e61eSDong Jia Shi * translate the channel program to a real-device runnable channel 7120a19e61eSDong Jia Shi * program. 7130a19e61eSDong Jia Shi * 7140a19e61eSDong Jia Shi * These APIs will copy the ccws into kernel-space buffers, and update 7150a19e61eSDong Jia Shi * the guest phsical addresses with their corresponding host physical 7160a19e61eSDong Jia Shi * addresses. Then channel I/O device drivers could issue the 7170a19e61eSDong Jia Shi * translated channel program to real devices to perform an I/O 7180a19e61eSDong Jia Shi * operation. 7190a19e61eSDong Jia Shi * 7200a19e61eSDong Jia Shi * These interfaces are designed to support translation only for 7210a19e61eSDong Jia Shi * channel programs, which are generated and formatted by a 7220a19e61eSDong Jia Shi * guest. Thus this will make it possible for things like VFIO to 7230a19e61eSDong Jia Shi * leverage the interfaces to passthrough a channel I/O mediated 7240a19e61eSDong Jia Shi * device in QEMU. 7250a19e61eSDong Jia Shi * 7260a19e61eSDong Jia Shi * We support direct ccw chaining by translating them to idal ccws. 7270a19e61eSDong Jia Shi * 7280a19e61eSDong Jia Shi * Returns: 7290a19e61eSDong Jia Shi * %0 on success and a negative error value on failure. 7300a19e61eSDong Jia Shi */ 7310a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp) 7320a19e61eSDong Jia Shi { 7330a19e61eSDong Jia Shi struct ccwchain *chain; 734a4c60404SEric Farman struct ccw1 *ccw; 735a4c60404SEric Farman struct page_array *pa; 7360a19e61eSDong Jia Shi int len, idx, ret; 7370a19e61eSDong Jia Shi 73871189f26SCornelia Huck /* this is an error in the caller */ 73971189f26SCornelia Huck if (!cp->initialized) 74071189f26SCornelia Huck return -EINVAL; 74171189f26SCornelia Huck 7420a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 7430a19e61eSDong Jia Shi len = chain->ch_len; 7440a19e61eSDong Jia Shi for (idx = 0; idx < len; idx++) { 745*4b946d65SEric Farman ccw = &chain->ch_ccw[idx]; 746*4b946d65SEric Farman pa = &chain->ch_pa[idx]; 747a4c60404SEric Farman 748a4c60404SEric Farman ret = ccwchain_fetch_one(ccw, pa, cp); 7490a19e61eSDong Jia Shi if (ret) 750d66a7355SHalil Pasic goto out_err; 7510a19e61eSDong Jia Shi } 7520a19e61eSDong Jia Shi } 7530a19e61eSDong Jia Shi 7540a19e61eSDong Jia Shi return 0; 755d66a7355SHalil Pasic out_err: 756d66a7355SHalil Pasic /* Only cleanup the chain elements that were actually translated. */ 757d66a7355SHalil Pasic chain->ch_len = idx; 758d66a7355SHalil Pasic list_for_each_entry_continue(chain, &cp->ccwchain_list, next) { 759d66a7355SHalil Pasic chain->ch_len = 0; 760d66a7355SHalil Pasic } 761d66a7355SHalil Pasic return ret; 7620a19e61eSDong Jia Shi } 7630a19e61eSDong Jia Shi 7640a19e61eSDong Jia Shi /** 7650a19e61eSDong Jia Shi * cp_get_orb() - get the orb of the channel program 7660a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 7679fbed59fSEric Farman * @sch: subchannel the operation will be performed against 7680a19e61eSDong Jia Shi * 7690a19e61eSDong Jia Shi * This function returns the address of the updated orb of the channel 7700a19e61eSDong Jia Shi * program. Channel I/O device drivers could use this orb to issue a 7710a19e61eSDong Jia Shi * ssch. 7720a19e61eSDong Jia Shi */ 7739fbed59fSEric Farman union orb *cp_get_orb(struct channel_program *cp, struct subchannel *sch) 7740a19e61eSDong Jia Shi { 7750a19e61eSDong Jia Shi union orb *orb; 7760a19e61eSDong Jia Shi struct ccwchain *chain; 7770a19e61eSDong Jia Shi struct ccw1 *cpa; 7780a19e61eSDong Jia Shi 77971189f26SCornelia Huck /* this is an error in the caller */ 78071189f26SCornelia Huck if (!cp->initialized) 78171189f26SCornelia Huck return NULL; 78271189f26SCornelia Huck 7830a19e61eSDong Jia Shi orb = &cp->orb; 7840a19e61eSDong Jia Shi 7859fbed59fSEric Farman orb->cmd.intparm = (u32)virt_to_phys(sch); 7860a19e61eSDong Jia Shi orb->cmd.fmt = 1; 7870a19e61eSDong Jia Shi 788254cb663SEric Farman /* 789254cb663SEric Farman * Everything built by vfio-ccw is a Format-2 IDAL. 790254cb663SEric Farman */ 791254cb663SEric Farman orb->cmd.c64 = 1; 792254cb663SEric Farman 7930a19e61eSDong Jia Shi if (orb->cmd.lpm == 0) 7949fbed59fSEric Farman orb->cmd.lpm = sch->lpm; 7950a19e61eSDong Jia Shi 7960a19e61eSDong Jia Shi chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next); 7970a19e61eSDong Jia Shi cpa = chain->ch_ccw; 7985de2322dSEric Farman orb->cmd.cpa = (__u32)virt_to_phys(cpa); 7990a19e61eSDong Jia Shi 8000a19e61eSDong Jia Shi return orb; 8010a19e61eSDong Jia Shi } 8020a19e61eSDong Jia Shi 8030a19e61eSDong Jia Shi /** 8040a19e61eSDong Jia Shi * cp_update_scsw() - update scsw for a channel program. 8050a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 8060a19e61eSDong Jia Shi * @scsw: I/O results of the channel program and also the target to be 8070a19e61eSDong Jia Shi * updated 8080a19e61eSDong Jia Shi * 8090a19e61eSDong Jia Shi * @scsw contains the I/O results of the channel program that pointed 8100a19e61eSDong Jia Shi * to by @cp. However what @scsw->cpa stores is a host physical 8110a19e61eSDong Jia Shi * address, which is meaningless for the guest, which is waiting for 8120a19e61eSDong Jia Shi * the I/O results. 8130a19e61eSDong Jia Shi * 8140a19e61eSDong Jia Shi * This function updates @scsw->cpa to its coressponding guest physical 8150a19e61eSDong Jia Shi * address. 8160a19e61eSDong Jia Shi */ 8170a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw) 8180a19e61eSDong Jia Shi { 8190a19e61eSDong Jia Shi struct ccwchain *chain; 8200a19e61eSDong Jia Shi u32 cpa = scsw->cmd.cpa; 8212904337fSEric Farman u32 ccw_head; 8220a19e61eSDong Jia Shi 82371189f26SCornelia Huck if (!cp->initialized) 82471189f26SCornelia Huck return; 82571189f26SCornelia Huck 8260a19e61eSDong Jia Shi /* 8270a19e61eSDong Jia Shi * LATER: 8280a19e61eSDong Jia Shi * For now, only update the cmd.cpa part. We may need to deal with 8290a19e61eSDong Jia Shi * other portions of the schib as well, even if we don't return them 8300a19e61eSDong Jia Shi * in the ioctl directly. Path status changes etc. 8310a19e61eSDong Jia Shi */ 8320a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 8330a19e61eSDong Jia Shi ccw_head = (u32)(u64)chain->ch_ccw; 83415f0eb3dSEric Farman /* 83515f0eb3dSEric Farman * On successful execution, cpa points just beyond the end 83615f0eb3dSEric Farman * of the chain. 83715f0eb3dSEric Farman */ 83815f0eb3dSEric Farman if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) { 8390a19e61eSDong Jia Shi /* 8400a19e61eSDong Jia Shi * (cpa - ccw_head) is the offset value of the host 8410a19e61eSDong Jia Shi * physical ccw to its chain head. 8420a19e61eSDong Jia Shi * Adding this value to the guest physical ccw chain 8430a19e61eSDong Jia Shi * head gets us the guest cpa. 8440a19e61eSDong Jia Shi */ 8450a19e61eSDong Jia Shi cpa = chain->ch_iova + (cpa - ccw_head); 8460a19e61eSDong Jia Shi break; 8470a19e61eSDong Jia Shi } 8480a19e61eSDong Jia Shi } 8490a19e61eSDong Jia Shi 8500a19e61eSDong Jia Shi scsw->cmd.cpa = cpa; 8510a19e61eSDong Jia Shi } 8520a19e61eSDong Jia Shi 8530a19e61eSDong Jia Shi /** 8540a19e61eSDong Jia Shi * cp_iova_pinned() - check if an iova is pinned for a ccw chain. 855364e3f90SSebastian Ott * @cp: channel_program on which to perform the operation 8560a19e61eSDong Jia Shi * @iova: the iova to check 8575a4fe7c4SEric Farman * @length: the length to check from @iova 8580a19e61eSDong Jia Shi * 8590a19e61eSDong Jia Shi * If the @iova is currently pinned for the ccw chain, return true; 8600a19e61eSDong Jia Shi * else return false. 8610a19e61eSDong Jia Shi */ 8625a4fe7c4SEric Farman bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length) 8630a19e61eSDong Jia Shi { 8640a19e61eSDong Jia Shi struct ccwchain *chain; 8650a19e61eSDong Jia Shi int i; 8660a19e61eSDong Jia Shi 86771189f26SCornelia Huck if (!cp->initialized) 86871189f26SCornelia Huck return false; 86971189f26SCornelia Huck 8700a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 8710a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) 872*4b946d65SEric Farman if (page_array_iova_pinned(&chain->ch_pa[i], iova, length)) 8730a19e61eSDong Jia Shi return true; 8740a19e61eSDong Jia Shi } 8750a19e61eSDong Jia Shi 8760a19e61eSDong Jia Shi return false; 8770a19e61eSDong Jia Shi } 878