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 { 260a19e61eSDong Jia Shi unsigned long pa_iova; 270a19e61eSDong Jia Shi unsigned long *pa_iova_pfn; 280a19e61eSDong Jia Shi unsigned long *pa_pfn; 290a19e61eSDong Jia Shi int pa_nr; 300a19e61eSDong Jia Shi }; 310a19e61eSDong Jia Shi 320a19e61eSDong Jia Shi struct pfn_array_table { 330a19e61eSDong Jia Shi struct pfn_array *pat_pa; 340a19e61eSDong Jia Shi int pat_nr; 350a19e61eSDong Jia Shi }; 360a19e61eSDong Jia Shi 370a19e61eSDong Jia Shi struct ccwchain { 380a19e61eSDong Jia Shi struct list_head next; 390a19e61eSDong Jia Shi struct ccw1 *ch_ccw; 400a19e61eSDong Jia Shi /* Guest physical address of the current chain. */ 410a19e61eSDong Jia Shi u64 ch_iova; 420a19e61eSDong Jia Shi /* Count of the valid ccws in chain. */ 430a19e61eSDong Jia Shi int ch_len; 440a19e61eSDong Jia Shi /* Pinned PAGEs for the original data. */ 450a19e61eSDong Jia Shi struct pfn_array_table *ch_pat; 460a19e61eSDong Jia Shi }; 470a19e61eSDong Jia Shi 480a19e61eSDong Jia Shi /* 490a19e61eSDong Jia Shi * pfn_array_pin() - pin user pages in memory 500a19e61eSDong Jia Shi * @pa: pfn_array on which to perform the operation 510a19e61eSDong Jia Shi * @mdev: the mediated device to perform pin/unpin operations 520a19e61eSDong Jia Shi * 530a19e61eSDong Jia Shi * Attempt to pin user pages in memory. 540a19e61eSDong Jia Shi * 550a19e61eSDong Jia Shi * Usage of pfn_array: 560a19e61eSDong Jia Shi * @pa->pa_iova starting guest physical I/O address. Assigned by caller. 570a19e61eSDong Jia Shi * @pa->pa_iova_pfn array that stores PFNs of the pages need to pin. Allocated 580a19e61eSDong Jia Shi * by caller. 590a19e61eSDong Jia Shi * @pa->pa_pfn array that receives PFNs of the pages pinned. Allocated by 600a19e61eSDong Jia Shi * caller. 610a19e61eSDong Jia Shi * @pa->pa_nr number of pages from @pa->pa_iova to pin. Assigned by 620a19e61eSDong Jia Shi * caller. 630a19e61eSDong Jia Shi * number of pages pinned. Assigned by callee. 640a19e61eSDong Jia Shi * 650a19e61eSDong Jia Shi * Returns: 660a19e61eSDong Jia Shi * Number of pages pinned on success. 670a19e61eSDong Jia Shi * If @pa->pa_nr is 0 or negative, returns 0. 680a19e61eSDong Jia Shi * If no pages were pinned, returns -errno. 690a19e61eSDong Jia Shi */ 700a19e61eSDong Jia Shi static int pfn_array_pin(struct pfn_array *pa, struct device *mdev) 710a19e61eSDong Jia Shi { 720a19e61eSDong Jia Shi int i, ret; 730a19e61eSDong Jia Shi 740a19e61eSDong Jia Shi if (pa->pa_nr <= 0) { 750a19e61eSDong Jia Shi pa->pa_nr = 0; 760a19e61eSDong Jia Shi return 0; 770a19e61eSDong Jia Shi } 780a19e61eSDong Jia Shi 790a19e61eSDong Jia Shi pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT; 800a19e61eSDong Jia Shi for (i = 1; i < pa->pa_nr; i++) 810a19e61eSDong Jia Shi pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1; 820a19e61eSDong Jia Shi 830a19e61eSDong Jia Shi ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr, 840a19e61eSDong Jia Shi IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); 850a19e61eSDong Jia Shi 860a19e61eSDong Jia Shi if (ret > 0 && ret != pa->pa_nr) { 870a19e61eSDong Jia Shi vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret); 880a19e61eSDong Jia Shi pa->pa_nr = 0; 890a19e61eSDong Jia Shi return 0; 900a19e61eSDong Jia Shi } 910a19e61eSDong Jia Shi 920a19e61eSDong Jia Shi return ret; 930a19e61eSDong Jia Shi } 940a19e61eSDong Jia Shi 950a19e61eSDong Jia Shi /* Unpin the pages before releasing the memory. */ 960a19e61eSDong Jia Shi static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev) 970a19e61eSDong Jia Shi { 980a19e61eSDong Jia Shi vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr); 990a19e61eSDong Jia Shi pa->pa_nr = 0; 1000a19e61eSDong Jia Shi kfree(pa->pa_iova_pfn); 1010a19e61eSDong Jia Shi } 1020a19e61eSDong Jia Shi 1030a19e61eSDong Jia Shi /* Alloc memory for PFNs, then pin pages with them. */ 1040a19e61eSDong Jia Shi static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev, 1050a19e61eSDong Jia Shi u64 iova, unsigned int len) 1060a19e61eSDong Jia Shi { 1070a19e61eSDong Jia Shi int ret = 0; 1080a19e61eSDong Jia Shi 1094cebc5d6SDong Jia Shi if (!len) 1104cebc5d6SDong Jia Shi return 0; 1114cebc5d6SDong Jia Shi 1124cebc5d6SDong Jia Shi if (pa->pa_nr) 1130a19e61eSDong Jia Shi return -EINVAL; 1140a19e61eSDong Jia Shi 1150a19e61eSDong Jia Shi pa->pa_iova = iova; 1160a19e61eSDong Jia Shi 1170a19e61eSDong Jia Shi pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 1180a19e61eSDong Jia Shi if (!pa->pa_nr) 1190a19e61eSDong Jia Shi return -EINVAL; 1200a19e61eSDong Jia Shi 1210a19e61eSDong Jia Shi pa->pa_iova_pfn = kcalloc(pa->pa_nr, 1220a19e61eSDong Jia Shi sizeof(*pa->pa_iova_pfn) + 1230a19e61eSDong Jia Shi sizeof(*pa->pa_pfn), 1240a19e61eSDong Jia Shi GFP_KERNEL); 1250a19e61eSDong Jia Shi if (unlikely(!pa->pa_iova_pfn)) 1260a19e61eSDong Jia Shi return -ENOMEM; 1270a19e61eSDong Jia Shi pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr; 1280a19e61eSDong Jia Shi 1290a19e61eSDong Jia Shi ret = pfn_array_pin(pa, mdev); 1300a19e61eSDong Jia Shi 1310a19e61eSDong Jia Shi if (ret > 0) 1320a19e61eSDong Jia Shi return ret; 1330a19e61eSDong Jia Shi else if (!ret) 1340a19e61eSDong Jia Shi ret = -EINVAL; 1350a19e61eSDong Jia Shi 1360a19e61eSDong Jia Shi kfree(pa->pa_iova_pfn); 1370a19e61eSDong Jia Shi 1380a19e61eSDong Jia Shi return ret; 1390a19e61eSDong Jia Shi } 1400a19e61eSDong Jia Shi 1410a19e61eSDong Jia Shi static int pfn_array_table_init(struct pfn_array_table *pat, int nr) 1420a19e61eSDong Jia Shi { 1430a19e61eSDong Jia Shi pat->pat_pa = kcalloc(nr, sizeof(*pat->pat_pa), GFP_KERNEL); 1440a19e61eSDong Jia Shi if (unlikely(ZERO_OR_NULL_PTR(pat->pat_pa))) { 1450a19e61eSDong Jia Shi pat->pat_nr = 0; 1460a19e61eSDong Jia Shi return -ENOMEM; 1470a19e61eSDong Jia Shi } 1480a19e61eSDong Jia Shi 1490a19e61eSDong Jia Shi pat->pat_nr = nr; 1500a19e61eSDong Jia Shi 1510a19e61eSDong Jia Shi return 0; 1520a19e61eSDong Jia Shi } 1530a19e61eSDong Jia Shi 1540a19e61eSDong Jia Shi static void pfn_array_table_unpin_free(struct pfn_array_table *pat, 1550a19e61eSDong Jia Shi struct device *mdev) 1560a19e61eSDong Jia Shi { 1570a19e61eSDong Jia Shi int i; 1580a19e61eSDong Jia Shi 1590a19e61eSDong Jia Shi for (i = 0; i < pat->pat_nr; i++) 1600a19e61eSDong Jia Shi pfn_array_unpin_free(pat->pat_pa + i, mdev); 1610a19e61eSDong Jia Shi 1620a19e61eSDong Jia Shi if (pat->pat_nr) { 1630a19e61eSDong Jia Shi kfree(pat->pat_pa); 1640a19e61eSDong Jia Shi pat->pat_pa = NULL; 1650a19e61eSDong Jia Shi pat->pat_nr = 0; 1660a19e61eSDong Jia Shi } 1670a19e61eSDong Jia Shi } 1680a19e61eSDong Jia Shi 1690a19e61eSDong Jia Shi static bool pfn_array_table_iova_pinned(struct pfn_array_table *pat, 1700a19e61eSDong Jia Shi unsigned long iova) 1710a19e61eSDong Jia Shi { 1720a19e61eSDong Jia Shi struct pfn_array *pa = pat->pat_pa; 1730a19e61eSDong Jia Shi unsigned long iova_pfn = iova >> PAGE_SHIFT; 1740a19e61eSDong Jia Shi int i, j; 1750a19e61eSDong Jia Shi 1760a19e61eSDong Jia Shi for (i = 0; i < pat->pat_nr; i++, pa++) 1770a19e61eSDong Jia Shi for (j = 0; j < pa->pa_nr; j++) 1780a19e61eSDong Jia Shi if (pa->pa_iova_pfn[i] == iova_pfn) 1790a19e61eSDong Jia Shi return true; 1800a19e61eSDong Jia Shi 1810a19e61eSDong Jia Shi return false; 1820a19e61eSDong Jia Shi } 1830a19e61eSDong Jia Shi /* Create the list idal words for a pfn_array_table. */ 1840a19e61eSDong Jia Shi static inline void pfn_array_table_idal_create_words( 1850a19e61eSDong Jia Shi struct pfn_array_table *pat, 1860a19e61eSDong Jia Shi unsigned long *idaws) 1870a19e61eSDong Jia Shi { 1880a19e61eSDong Jia Shi struct pfn_array *pa; 1890a19e61eSDong Jia Shi int i, j, k; 1900a19e61eSDong Jia Shi 1910a19e61eSDong Jia Shi /* 1920a19e61eSDong Jia Shi * Idal words (execept the first one) rely on the memory being 4k 1930a19e61eSDong Jia Shi * aligned. If a user virtual address is 4K aligned, then it's 1940a19e61eSDong Jia Shi * corresponding kernel physical address will also be 4K aligned. Thus 1950a19e61eSDong Jia Shi * there will be no problem here to simply use the phys to create an 1960a19e61eSDong Jia Shi * idaw. 1970a19e61eSDong Jia Shi */ 1980a19e61eSDong Jia Shi k = 0; 1990a19e61eSDong Jia Shi for (i = 0; i < pat->pat_nr; i++) { 2000a19e61eSDong Jia Shi pa = pat->pat_pa + i; 2010a19e61eSDong Jia Shi for (j = 0; j < pa->pa_nr; j++) { 2020a19e61eSDong Jia Shi idaws[k] = pa->pa_pfn[j] << PAGE_SHIFT; 2030a19e61eSDong Jia Shi if (k == 0) 2040a19e61eSDong Jia Shi idaws[k] += pa->pa_iova & (PAGE_SIZE - 1); 2050a19e61eSDong Jia Shi k++; 2060a19e61eSDong Jia Shi } 2070a19e61eSDong Jia Shi } 2080a19e61eSDong Jia Shi } 2090a19e61eSDong Jia Shi 2100a19e61eSDong Jia Shi 2110a19e61eSDong Jia Shi /* 2120a19e61eSDong Jia Shi * Within the domain (@mdev), copy @n bytes from a guest physical 2130a19e61eSDong Jia Shi * address (@iova) to a host physical address (@to). 2140a19e61eSDong Jia Shi */ 2150a19e61eSDong Jia Shi static long copy_from_iova(struct device *mdev, 2160a19e61eSDong Jia Shi void *to, u64 iova, 2170a19e61eSDong Jia Shi unsigned long n) 2180a19e61eSDong Jia Shi { 2190a19e61eSDong Jia Shi struct pfn_array pa = {0}; 2200a19e61eSDong Jia Shi u64 from; 2210a19e61eSDong Jia Shi int i, ret; 2220a19e61eSDong Jia Shi unsigned long l, m; 2230a19e61eSDong Jia Shi 2240a19e61eSDong Jia Shi ret = pfn_array_alloc_pin(&pa, mdev, iova, n); 2250a19e61eSDong Jia Shi if (ret <= 0) 2260a19e61eSDong Jia Shi return ret; 2270a19e61eSDong Jia Shi 2280a19e61eSDong Jia Shi l = n; 2290a19e61eSDong Jia Shi for (i = 0; i < pa.pa_nr; i++) { 2300a19e61eSDong Jia Shi from = pa.pa_pfn[i] << PAGE_SHIFT; 2310a19e61eSDong Jia Shi m = PAGE_SIZE; 2320a19e61eSDong Jia Shi if (i == 0) { 2330a19e61eSDong Jia Shi from += iova & (PAGE_SIZE - 1); 2340a19e61eSDong Jia Shi m -= iova & (PAGE_SIZE - 1); 2350a19e61eSDong Jia Shi } 2360a19e61eSDong Jia Shi 2370a19e61eSDong Jia Shi m = min(l, m); 2380a19e61eSDong Jia Shi memcpy(to + (n - l), (void *)from, m); 2390a19e61eSDong Jia Shi 2400a19e61eSDong Jia Shi l -= m; 2410a19e61eSDong Jia Shi if (l == 0) 2420a19e61eSDong Jia Shi break; 2430a19e61eSDong Jia Shi } 2440a19e61eSDong Jia Shi 2450a19e61eSDong Jia Shi pfn_array_unpin_free(&pa, mdev); 2460a19e61eSDong Jia Shi 2470a19e61eSDong Jia Shi return l; 2480a19e61eSDong Jia Shi } 2490a19e61eSDong Jia Shi 2500a19e61eSDong Jia Shi static long copy_ccw_from_iova(struct channel_program *cp, 2510a19e61eSDong Jia Shi struct ccw1 *to, u64 iova, 2520a19e61eSDong Jia Shi unsigned long len) 2530a19e61eSDong Jia Shi { 254d686f21aSDong Jia Shi struct ccw0 ccw0; 255d686f21aSDong Jia Shi struct ccw1 *pccw1; 256d686f21aSDong Jia Shi int ret; 257d686f21aSDong Jia Shi int i; 258d686f21aSDong Jia Shi 259d686f21aSDong Jia Shi ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1)); 260d686f21aSDong Jia Shi if (ret) 261d686f21aSDong Jia Shi return ret; 262d686f21aSDong Jia Shi 263d686f21aSDong Jia Shi if (!cp->orb.cmd.fmt) { 264d686f21aSDong Jia Shi pccw1 = to; 265d686f21aSDong Jia Shi for (i = 0; i < len; i++) { 266d686f21aSDong Jia Shi ccw0 = *(struct ccw0 *)pccw1; 267d686f21aSDong Jia Shi if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) { 268d686f21aSDong Jia Shi pccw1->cmd_code = CCW_CMD_TIC; 269d686f21aSDong Jia Shi pccw1->flags = 0; 270d686f21aSDong Jia Shi pccw1->count = 0; 271d686f21aSDong Jia Shi } else { 272d686f21aSDong Jia Shi pccw1->cmd_code = ccw0.cmd_code; 273d686f21aSDong Jia Shi pccw1->flags = ccw0.flags; 274d686f21aSDong Jia Shi pccw1->count = ccw0.count; 275d686f21aSDong Jia Shi } 276d686f21aSDong Jia Shi pccw1->cda = ccw0.cda; 277d686f21aSDong Jia Shi pccw1++; 278d686f21aSDong Jia Shi } 279d686f21aSDong Jia Shi } 280d686f21aSDong Jia Shi 281d686f21aSDong Jia Shi return ret; 2820a19e61eSDong Jia Shi } 2830a19e61eSDong Jia Shi 2840a19e61eSDong Jia Shi /* 2850a19e61eSDong Jia Shi * Helpers to operate ccwchain. 2860a19e61eSDong Jia Shi */ 2870a19e61eSDong Jia Shi #define ccw_is_test(_ccw) (((_ccw)->cmd_code & 0x0F) == 0) 2880a19e61eSDong Jia Shi 2890a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP) 2900a19e61eSDong Jia Shi 2910a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC) 2920a19e61eSDong Jia Shi 2930a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA) 2940a19e61eSDong Jia Shi 2950a19e61eSDong Jia Shi 2960a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC)) 2970a19e61eSDong Jia Shi 2980a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len) 2990a19e61eSDong Jia Shi { 3000a19e61eSDong Jia Shi struct ccwchain *chain; 3010a19e61eSDong Jia Shi void *data; 3020a19e61eSDong Jia Shi size_t size; 3030a19e61eSDong Jia Shi 3040a19e61eSDong Jia Shi /* Make ccw address aligned to 8. */ 3050a19e61eSDong Jia Shi size = ((sizeof(*chain) + 7L) & -8L) + 3060a19e61eSDong Jia Shi sizeof(*chain->ch_ccw) * len + 3070a19e61eSDong Jia Shi sizeof(*chain->ch_pat) * len; 3080a19e61eSDong Jia Shi chain = kzalloc(size, GFP_DMA | GFP_KERNEL); 3090a19e61eSDong Jia Shi if (!chain) 3100a19e61eSDong Jia Shi return NULL; 3110a19e61eSDong Jia Shi 3120a19e61eSDong Jia Shi data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L); 3130a19e61eSDong Jia Shi chain->ch_ccw = (struct ccw1 *)data; 3140a19e61eSDong Jia Shi 3150a19e61eSDong Jia Shi data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len; 3160a19e61eSDong Jia Shi chain->ch_pat = (struct pfn_array_table *)data; 3170a19e61eSDong Jia Shi 3180a19e61eSDong Jia Shi chain->ch_len = len; 3190a19e61eSDong Jia Shi 3200a19e61eSDong Jia Shi list_add_tail(&chain->next, &cp->ccwchain_list); 3210a19e61eSDong Jia Shi 3220a19e61eSDong Jia Shi return chain; 3230a19e61eSDong Jia Shi } 3240a19e61eSDong Jia Shi 3250a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain) 3260a19e61eSDong Jia Shi { 3270a19e61eSDong Jia Shi list_del(&chain->next); 3280a19e61eSDong Jia Shi kfree(chain); 3290a19e61eSDong Jia Shi } 3300a19e61eSDong Jia Shi 3310a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */ 3320a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx) 3330a19e61eSDong Jia Shi { 3340a19e61eSDong Jia Shi struct ccw1 *ccw = chain->ch_ccw + idx; 3350a19e61eSDong Jia Shi 336408358b5SJason J. Herne if (ccw_is_test(ccw) || ccw_is_noop(ccw) || ccw_is_tic(ccw)) 337408358b5SJason J. Herne return; 3380a19e61eSDong Jia Shi if (!ccw->count) 3390a19e61eSDong Jia Shi return; 3400a19e61eSDong Jia Shi 3410a19e61eSDong Jia Shi kfree((void *)(u64)ccw->cda); 3420a19e61eSDong Jia Shi } 3430a19e61eSDong Jia Shi 3440a19e61eSDong Jia Shi /* Unpin the pages then free the memory resources. */ 3450a19e61eSDong Jia Shi static void cp_unpin_free(struct channel_program *cp) 3460a19e61eSDong Jia Shi { 3470a19e61eSDong Jia Shi struct ccwchain *chain, *temp; 3480a19e61eSDong Jia Shi int i; 3490a19e61eSDong Jia Shi 3500a19e61eSDong Jia Shi list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { 3510a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) { 3520a19e61eSDong Jia Shi pfn_array_table_unpin_free(chain->ch_pat + i, 3530a19e61eSDong Jia Shi cp->mdev); 3540a19e61eSDong Jia Shi ccwchain_cda_free(chain, i); 3550a19e61eSDong Jia Shi } 3560a19e61eSDong Jia Shi ccwchain_free(chain); 3570a19e61eSDong Jia Shi } 3580a19e61eSDong Jia Shi } 3590a19e61eSDong Jia Shi 3600a19e61eSDong Jia Shi /** 3610a19e61eSDong Jia Shi * ccwchain_calc_length - calculate the length of the ccw chain. 3620a19e61eSDong Jia Shi * @iova: guest physical address of the target ccw chain 3630a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 3640a19e61eSDong Jia Shi * 3650a19e61eSDong Jia Shi * This is the chain length not considering any TICs. 3660a19e61eSDong Jia Shi * You need to do a new round for each TIC target. 3670a19e61eSDong Jia Shi * 3680a19e61eSDong Jia Shi * Returns: the length of the ccw chain or -errno. 3690a19e61eSDong Jia Shi */ 3700a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp) 3710a19e61eSDong Jia Shi { 3720a19e61eSDong Jia Shi struct ccw1 *ccw, *p; 3730a19e61eSDong Jia Shi int cnt; 3740a19e61eSDong Jia Shi 3750a19e61eSDong Jia Shi /* 3760a19e61eSDong Jia Shi * Copy current chain from guest to host kernel. 3770a19e61eSDong Jia Shi * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256). 3780a19e61eSDong Jia Shi * So copying 2K is enough (safe). 3790a19e61eSDong Jia Shi */ 3800a19e61eSDong Jia Shi p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL); 3810a19e61eSDong Jia Shi if (!ccw) 3820a19e61eSDong Jia Shi return -ENOMEM; 3830a19e61eSDong Jia Shi 3840a19e61eSDong Jia Shi cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX); 3850a19e61eSDong Jia Shi if (cnt) { 3860a19e61eSDong Jia Shi kfree(ccw); 3870a19e61eSDong Jia Shi return cnt; 3880a19e61eSDong Jia Shi } 3890a19e61eSDong Jia Shi 3900a19e61eSDong Jia Shi cnt = 0; 3910a19e61eSDong Jia Shi do { 3920a19e61eSDong Jia Shi cnt++; 3930a19e61eSDong Jia Shi 3940a19e61eSDong Jia Shi if ((!ccw_is_chain(ccw)) && (!ccw_is_tic(ccw))) 3950a19e61eSDong Jia Shi break; 3960a19e61eSDong Jia Shi 3970a19e61eSDong Jia Shi ccw++; 3980a19e61eSDong Jia Shi } while (cnt < CCWCHAIN_LEN_MAX + 1); 3990a19e61eSDong Jia Shi 4000a19e61eSDong Jia Shi if (cnt == CCWCHAIN_LEN_MAX + 1) 4010a19e61eSDong Jia Shi cnt = -EINVAL; 4020a19e61eSDong Jia Shi 4030a19e61eSDong Jia Shi kfree(p); 4040a19e61eSDong Jia Shi return cnt; 4050a19e61eSDong Jia Shi } 4060a19e61eSDong Jia Shi 4070a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp) 4080a19e61eSDong Jia Shi { 4090a19e61eSDong Jia Shi struct ccwchain *chain; 4100a19e61eSDong Jia Shi u32 ccw_head, ccw_tail; 4110a19e61eSDong Jia Shi 4120a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 4130a19e61eSDong Jia Shi ccw_head = chain->ch_iova; 4140a19e61eSDong Jia Shi ccw_tail = ccw_head + (chain->ch_len - 1) * sizeof(struct ccw1); 4150a19e61eSDong Jia Shi 4160a19e61eSDong Jia Shi if ((ccw_head <= tic->cda) && (tic->cda <= ccw_tail)) 4170a19e61eSDong Jia Shi return 1; 4180a19e61eSDong Jia Shi } 4190a19e61eSDong Jia Shi 4200a19e61eSDong Jia Shi return 0; 4210a19e61eSDong Jia Shi } 4220a19e61eSDong Jia Shi 4230a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, 4240a19e61eSDong Jia Shi struct channel_program *cp); 4250a19e61eSDong Jia Shi 4260a19e61eSDong Jia Shi static int ccwchain_handle_tic(struct ccw1 *tic, struct channel_program *cp) 4270a19e61eSDong Jia Shi { 4280a19e61eSDong Jia Shi struct ccwchain *chain; 4290a19e61eSDong Jia Shi int len, ret; 4300a19e61eSDong Jia Shi 4310a19e61eSDong Jia Shi /* May transfer to an existing chain. */ 4320a19e61eSDong Jia Shi if (tic_target_chain_exists(tic, cp)) 4330a19e61eSDong Jia Shi return 0; 4340a19e61eSDong Jia Shi 4350a19e61eSDong Jia Shi /* Get chain length. */ 4360a19e61eSDong Jia Shi len = ccwchain_calc_length(tic->cda, cp); 4370a19e61eSDong Jia Shi if (len < 0) 4380a19e61eSDong Jia Shi return len; 4390a19e61eSDong Jia Shi 4400a19e61eSDong Jia Shi /* Need alloc a new chain for this one. */ 4410a19e61eSDong Jia Shi chain = ccwchain_alloc(cp, len); 4420a19e61eSDong Jia Shi if (!chain) 4430a19e61eSDong Jia Shi return -ENOMEM; 4440a19e61eSDong Jia Shi chain->ch_iova = tic->cda; 4450a19e61eSDong Jia Shi 4460a19e61eSDong Jia Shi /* Copy the new chain from user. */ 4470a19e61eSDong Jia Shi ret = copy_ccw_from_iova(cp, chain->ch_ccw, tic->cda, len); 4480a19e61eSDong Jia Shi if (ret) { 4490a19e61eSDong Jia Shi ccwchain_free(chain); 4500a19e61eSDong Jia Shi return ret; 4510a19e61eSDong Jia Shi } 4520a19e61eSDong Jia Shi 4530a19e61eSDong Jia Shi /* Loop for tics on this new chain. */ 4540a19e61eSDong Jia Shi return ccwchain_loop_tic(chain, cp); 4550a19e61eSDong Jia Shi } 4560a19e61eSDong Jia Shi 4570a19e61eSDong Jia Shi /* Loop for TICs. */ 4580a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp) 4590a19e61eSDong Jia Shi { 4600a19e61eSDong Jia Shi struct ccw1 *tic; 4610a19e61eSDong Jia Shi int i, ret; 4620a19e61eSDong Jia Shi 4630a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) { 4640a19e61eSDong Jia Shi tic = chain->ch_ccw + i; 4650a19e61eSDong Jia Shi 4660a19e61eSDong Jia Shi if (!ccw_is_tic(tic)) 4670a19e61eSDong Jia Shi continue; 4680a19e61eSDong Jia Shi 4690a19e61eSDong Jia Shi ret = ccwchain_handle_tic(tic, cp); 4700a19e61eSDong Jia Shi if (ret) 4710a19e61eSDong Jia Shi return ret; 4720a19e61eSDong Jia Shi } 4730a19e61eSDong Jia Shi 4740a19e61eSDong Jia Shi return 0; 4750a19e61eSDong Jia Shi } 4760a19e61eSDong Jia Shi 4770a19e61eSDong Jia Shi static int ccwchain_fetch_tic(struct ccwchain *chain, 4780a19e61eSDong Jia Shi int idx, 4790a19e61eSDong Jia Shi struct channel_program *cp) 4800a19e61eSDong Jia Shi { 4810a19e61eSDong Jia Shi struct ccw1 *ccw = chain->ch_ccw + idx; 4820a19e61eSDong Jia Shi struct ccwchain *iter; 4830a19e61eSDong Jia Shi u32 ccw_head, ccw_tail; 4840a19e61eSDong Jia Shi 4850a19e61eSDong Jia Shi list_for_each_entry(iter, &cp->ccwchain_list, next) { 4860a19e61eSDong Jia Shi ccw_head = iter->ch_iova; 4870a19e61eSDong Jia Shi ccw_tail = ccw_head + (iter->ch_len - 1) * sizeof(struct ccw1); 4880a19e61eSDong Jia Shi 4890a19e61eSDong Jia Shi if ((ccw_head <= ccw->cda) && (ccw->cda <= ccw_tail)) { 490c389377cSJason J. Herne ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) + 4910a19e61eSDong Jia Shi (ccw->cda - ccw_head)); 4920a19e61eSDong Jia Shi return 0; 4930a19e61eSDong Jia Shi } 4940a19e61eSDong Jia Shi } 4950a19e61eSDong Jia Shi 4960a19e61eSDong Jia Shi return -EFAULT; 4970a19e61eSDong Jia Shi } 4980a19e61eSDong Jia Shi 4990a19e61eSDong Jia Shi static int ccwchain_fetch_direct(struct ccwchain *chain, 5000a19e61eSDong Jia Shi int idx, 5010a19e61eSDong Jia Shi struct channel_program *cp) 5020a19e61eSDong Jia Shi { 5030a19e61eSDong Jia Shi struct ccw1 *ccw; 5040a19e61eSDong Jia Shi struct pfn_array_table *pat; 5050a19e61eSDong Jia Shi unsigned long *idaws; 5060a19e61eSDong Jia Shi int idaw_nr; 5070a19e61eSDong Jia Shi 5080a19e61eSDong Jia Shi ccw = chain->ch_ccw + idx; 5090a19e61eSDong Jia Shi 5104cebc5d6SDong Jia Shi if (!ccw->count) { 5114cebc5d6SDong Jia Shi /* 5124cebc5d6SDong Jia Shi * We just want the translation result of any direct ccw 5134cebc5d6SDong Jia Shi * to be an IDA ccw, so let's add the IDA flag for it. 5144cebc5d6SDong Jia Shi * Although the flag will be ignored by firmware. 5154cebc5d6SDong Jia Shi */ 5164cebc5d6SDong Jia Shi ccw->flags |= CCW_FLAG_IDA; 5174cebc5d6SDong Jia Shi return 0; 5184cebc5d6SDong Jia Shi } 5194cebc5d6SDong Jia Shi 5200a19e61eSDong Jia Shi /* 5210a19e61eSDong Jia Shi * Pin data page(s) in memory. 5220a19e61eSDong Jia Shi * The number of pages actually is the count of the idaws which will be 5230a19e61eSDong Jia Shi * needed when translating a direct ccw to a idal ccw. 5240a19e61eSDong Jia Shi */ 5250a19e61eSDong Jia Shi pat = chain->ch_pat + idx; 5260a19e61eSDong Jia Shi if (pfn_array_table_init(pat, 1)) 5270a19e61eSDong Jia Shi return -ENOMEM; 5280a19e61eSDong Jia Shi idaw_nr = pfn_array_alloc_pin(pat->pat_pa, cp->mdev, 5290a19e61eSDong Jia Shi ccw->cda, ccw->count); 5300a19e61eSDong Jia Shi if (idaw_nr < 0) 5310a19e61eSDong Jia Shi return idaw_nr; 5320a19e61eSDong Jia Shi 5330a19e61eSDong Jia Shi /* Translate this direct ccw to a idal ccw. */ 5340a19e61eSDong Jia Shi idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL); 5350a19e61eSDong Jia Shi if (!idaws) { 5360a19e61eSDong Jia Shi pfn_array_table_unpin_free(pat, cp->mdev); 5370a19e61eSDong Jia Shi return -ENOMEM; 5380a19e61eSDong Jia Shi } 5390a19e61eSDong Jia Shi ccw->cda = (__u32) virt_to_phys(idaws); 5400a19e61eSDong Jia Shi ccw->flags |= CCW_FLAG_IDA; 5410a19e61eSDong Jia Shi 5420a19e61eSDong Jia Shi pfn_array_table_idal_create_words(pat, idaws); 5430a19e61eSDong Jia Shi 5440a19e61eSDong Jia Shi return 0; 5450a19e61eSDong Jia Shi } 5460a19e61eSDong Jia Shi 5470a19e61eSDong Jia Shi static int ccwchain_fetch_idal(struct ccwchain *chain, 5480a19e61eSDong Jia Shi int idx, 5490a19e61eSDong Jia Shi struct channel_program *cp) 5500a19e61eSDong Jia Shi { 5510a19e61eSDong Jia Shi struct ccw1 *ccw; 5520a19e61eSDong Jia Shi struct pfn_array_table *pat; 5530a19e61eSDong Jia Shi unsigned long *idaws; 5540a19e61eSDong Jia Shi u64 idaw_iova; 5550a19e61eSDong Jia Shi unsigned int idaw_nr, idaw_len; 5560a19e61eSDong Jia Shi int i, ret; 5570a19e61eSDong Jia Shi 5580a19e61eSDong Jia Shi ccw = chain->ch_ccw + idx; 5590a19e61eSDong Jia Shi 5604cebc5d6SDong Jia Shi if (!ccw->count) 5614cebc5d6SDong Jia Shi return 0; 5624cebc5d6SDong Jia Shi 5630a19e61eSDong Jia Shi /* Calculate size of idaws. */ 5640a19e61eSDong Jia Shi ret = copy_from_iova(cp->mdev, &idaw_iova, ccw->cda, sizeof(idaw_iova)); 5650a19e61eSDong Jia Shi if (ret) 5660a19e61eSDong Jia Shi return ret; 5670a19e61eSDong Jia Shi idaw_nr = idal_nr_words((void *)(idaw_iova), ccw->count); 5680a19e61eSDong Jia Shi idaw_len = idaw_nr * sizeof(*idaws); 5690a19e61eSDong Jia Shi 5700a19e61eSDong Jia Shi /* Pin data page(s) in memory. */ 5710a19e61eSDong Jia Shi pat = chain->ch_pat + idx; 5720a19e61eSDong Jia Shi ret = pfn_array_table_init(pat, idaw_nr); 5730a19e61eSDong Jia Shi if (ret) 5740a19e61eSDong Jia Shi return ret; 5750a19e61eSDong Jia Shi 5760a19e61eSDong Jia Shi /* Translate idal ccw to use new allocated idaws. */ 5770a19e61eSDong Jia Shi idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL); 5780a19e61eSDong Jia Shi if (!idaws) { 5790a19e61eSDong Jia Shi ret = -ENOMEM; 5800a19e61eSDong Jia Shi goto out_unpin; 5810a19e61eSDong Jia Shi } 5820a19e61eSDong Jia Shi 5830a19e61eSDong Jia Shi ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idaw_len); 5840a19e61eSDong Jia Shi if (ret) 5850a19e61eSDong Jia Shi goto out_free_idaws; 5860a19e61eSDong Jia Shi 5870a19e61eSDong Jia Shi ccw->cda = virt_to_phys(idaws); 5880a19e61eSDong Jia Shi 5890a19e61eSDong Jia Shi for (i = 0; i < idaw_nr; i++) { 5900a19e61eSDong Jia Shi idaw_iova = *(idaws + i); 5910a19e61eSDong Jia Shi 5920a19e61eSDong Jia Shi ret = pfn_array_alloc_pin(pat->pat_pa + i, cp->mdev, 5930a19e61eSDong Jia Shi idaw_iova, 1); 5940a19e61eSDong Jia Shi if (ret < 0) 5950a19e61eSDong Jia Shi goto out_free_idaws; 5960a19e61eSDong Jia Shi } 5970a19e61eSDong Jia Shi 5980a19e61eSDong Jia Shi pfn_array_table_idal_create_words(pat, idaws); 5990a19e61eSDong Jia Shi 6000a19e61eSDong Jia Shi return 0; 6010a19e61eSDong Jia Shi 6020a19e61eSDong Jia Shi out_free_idaws: 6030a19e61eSDong Jia Shi kfree(idaws); 6040a19e61eSDong Jia Shi out_unpin: 6050a19e61eSDong Jia Shi pfn_array_table_unpin_free(pat, cp->mdev); 6060a19e61eSDong Jia Shi return ret; 6070a19e61eSDong Jia Shi } 6080a19e61eSDong Jia Shi 6090a19e61eSDong Jia Shi /* 6100a19e61eSDong Jia Shi * Fetch one ccw. 6110a19e61eSDong Jia Shi * To reduce memory copy, we'll pin the cda page in memory, 6120a19e61eSDong Jia Shi * and to get rid of the cda 2G limitiaion of ccw1, we'll translate 6130a19e61eSDong Jia Shi * direct ccws to idal ccws. 6140a19e61eSDong Jia Shi */ 6150a19e61eSDong Jia Shi static int ccwchain_fetch_one(struct ccwchain *chain, 6160a19e61eSDong Jia Shi int idx, 6170a19e61eSDong Jia Shi struct channel_program *cp) 6180a19e61eSDong Jia Shi { 6190a19e61eSDong Jia Shi struct ccw1 *ccw = chain->ch_ccw + idx; 6200a19e61eSDong Jia Shi 6210a19e61eSDong Jia Shi if (ccw_is_test(ccw) || ccw_is_noop(ccw)) 6220a19e61eSDong Jia Shi return 0; 6230a19e61eSDong Jia Shi 6240a19e61eSDong Jia Shi if (ccw_is_tic(ccw)) 6250a19e61eSDong Jia Shi return ccwchain_fetch_tic(chain, idx, cp); 6260a19e61eSDong Jia Shi 6270a19e61eSDong Jia Shi if (ccw_is_idal(ccw)) 6280a19e61eSDong Jia Shi return ccwchain_fetch_idal(chain, idx, cp); 6290a19e61eSDong Jia Shi 6300a19e61eSDong Jia Shi return ccwchain_fetch_direct(chain, idx, cp); 6310a19e61eSDong Jia Shi } 6320a19e61eSDong Jia Shi 6330a19e61eSDong Jia Shi /** 6340a19e61eSDong Jia Shi * cp_init() - allocate ccwchains for a channel program. 6350a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6360a19e61eSDong Jia Shi * @mdev: the mediated device to perform pin/unpin operations 6370a19e61eSDong Jia Shi * @orb: control block for the channel program from the guest 6380a19e61eSDong Jia Shi * 6390a19e61eSDong Jia Shi * This creates one or more ccwchain(s), and copies the raw data of 6400a19e61eSDong Jia Shi * the target channel program from @orb->cmd.iova to the new ccwchain(s). 6410a19e61eSDong Jia Shi * 6420a19e61eSDong Jia Shi * Limitations: 6430a19e61eSDong Jia Shi * 1. Supports only prefetch enabled mode. 6440a19e61eSDong Jia Shi * 2. Supports idal(c64) ccw chaining. 6450a19e61eSDong Jia Shi * 3. Supports 4k idaw. 6460a19e61eSDong Jia Shi * 6470a19e61eSDong Jia Shi * Returns: 6480a19e61eSDong Jia Shi * %0 on success and a negative error value on failure. 6490a19e61eSDong Jia Shi */ 6500a19e61eSDong Jia Shi int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb) 6510a19e61eSDong Jia Shi { 6520a19e61eSDong Jia Shi u64 iova = orb->cmd.cpa; 6530a19e61eSDong Jia Shi struct ccwchain *chain; 6540a19e61eSDong Jia Shi int len, ret; 6550a19e61eSDong Jia Shi 6560a19e61eSDong Jia Shi /* 6570a19e61eSDong Jia Shi * XXX: 6580a19e61eSDong Jia Shi * Only support prefetch enable mode now. 6590a19e61eSDong Jia Shi * Only support 64bit addressing idal. 6600a19e61eSDong Jia Shi * Only support 4k IDAW. 6610a19e61eSDong Jia Shi */ 662d686f21aSDong Jia Shi if (!orb->cmd.pfch || !orb->cmd.c64 || orb->cmd.i2k) 6630a19e61eSDong Jia Shi return -EOPNOTSUPP; 6640a19e61eSDong Jia Shi 6650a19e61eSDong Jia Shi INIT_LIST_HEAD(&cp->ccwchain_list); 6660a19e61eSDong Jia Shi memcpy(&cp->orb, orb, sizeof(*orb)); 6670a19e61eSDong Jia Shi cp->mdev = mdev; 6680a19e61eSDong Jia Shi 6690a19e61eSDong Jia Shi /* Get chain length. */ 6700a19e61eSDong Jia Shi len = ccwchain_calc_length(iova, cp); 6710a19e61eSDong Jia Shi if (len < 0) 6720a19e61eSDong Jia Shi return len; 6730a19e61eSDong Jia Shi 6740a19e61eSDong Jia Shi /* Alloc mem for the head chain. */ 6750a19e61eSDong Jia Shi chain = ccwchain_alloc(cp, len); 6760a19e61eSDong Jia Shi if (!chain) 6770a19e61eSDong Jia Shi return -ENOMEM; 6780a19e61eSDong Jia Shi chain->ch_iova = iova; 6790a19e61eSDong Jia Shi 6800a19e61eSDong Jia Shi /* Copy the head chain from guest. */ 6810a19e61eSDong Jia Shi ret = copy_ccw_from_iova(cp, chain->ch_ccw, iova, len); 6820a19e61eSDong Jia Shi if (ret) { 6830a19e61eSDong Jia Shi ccwchain_free(chain); 6840a19e61eSDong Jia Shi return ret; 6850a19e61eSDong Jia Shi } 6860a19e61eSDong Jia Shi 6870a19e61eSDong Jia Shi /* Now loop for its TICs. */ 6880a19e61eSDong Jia Shi ret = ccwchain_loop_tic(chain, cp); 6890a19e61eSDong Jia Shi if (ret) 6900a19e61eSDong Jia Shi cp_unpin_free(cp); 6910a19e61eSDong Jia Shi 6920a19e61eSDong Jia Shi return ret; 6930a19e61eSDong Jia Shi } 6940a19e61eSDong Jia Shi 6950a19e61eSDong Jia Shi 6960a19e61eSDong Jia Shi /** 6970a19e61eSDong Jia Shi * cp_free() - free resources for channel program. 6980a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 6990a19e61eSDong Jia Shi * 7000a19e61eSDong Jia Shi * This unpins the memory pages and frees the memory space occupied by 7010a19e61eSDong Jia Shi * @cp, which must have been returned by a previous call to cp_init(). 7020a19e61eSDong Jia Shi * Otherwise, undefined behavior occurs. 7030a19e61eSDong Jia Shi */ 7040a19e61eSDong Jia Shi void cp_free(struct channel_program *cp) 7050a19e61eSDong Jia Shi { 7060a19e61eSDong Jia Shi cp_unpin_free(cp); 7070a19e61eSDong Jia Shi } 7080a19e61eSDong Jia Shi 7090a19e61eSDong Jia Shi /** 7100a19e61eSDong Jia Shi * cp_prefetch() - translate a guest physical address channel program to 7110a19e61eSDong Jia Shi * a real-device runnable channel program. 7120a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 7130a19e61eSDong Jia Shi * 7140a19e61eSDong Jia Shi * This function translates the guest-physical-address channel program 7150a19e61eSDong Jia Shi * and stores the result to ccwchain list. @cp must have been 7160a19e61eSDong Jia Shi * initialized by a previous call with cp_init(). Otherwise, undefined 7170a19e61eSDong Jia Shi * behavior occurs. 718*d66a7355SHalil Pasic * For each chain composing the channel program: 719*d66a7355SHalil Pasic * - On entry ch_len holds the count of CCWs to be translated. 720*d66a7355SHalil Pasic * - On exit ch_len is adjusted to the count of successfully translated CCWs. 721*d66a7355SHalil Pasic * This allows cp_free to find in ch_len the count of CCWs to free in a chain. 7220a19e61eSDong Jia Shi * 7230a19e61eSDong Jia Shi * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced 7240a19e61eSDong Jia Shi * as helpers to do ccw chain translation inside the kernel. Basically 7250a19e61eSDong Jia Shi * they accept a channel program issued by a virtual machine, and 7260a19e61eSDong Jia Shi * translate the channel program to a real-device runnable channel 7270a19e61eSDong Jia Shi * program. 7280a19e61eSDong Jia Shi * 7290a19e61eSDong Jia Shi * These APIs will copy the ccws into kernel-space buffers, and update 7300a19e61eSDong Jia Shi * the guest phsical addresses with their corresponding host physical 7310a19e61eSDong Jia Shi * addresses. Then channel I/O device drivers could issue the 7320a19e61eSDong Jia Shi * translated channel program to real devices to perform an I/O 7330a19e61eSDong Jia Shi * operation. 7340a19e61eSDong Jia Shi * 7350a19e61eSDong Jia Shi * These interfaces are designed to support translation only for 7360a19e61eSDong Jia Shi * channel programs, which are generated and formatted by a 7370a19e61eSDong Jia Shi * guest. Thus this will make it possible for things like VFIO to 7380a19e61eSDong Jia Shi * leverage the interfaces to passthrough a channel I/O mediated 7390a19e61eSDong Jia Shi * device in QEMU. 7400a19e61eSDong Jia Shi * 7410a19e61eSDong Jia Shi * We support direct ccw chaining by translating them to idal ccws. 7420a19e61eSDong Jia Shi * 7430a19e61eSDong Jia Shi * Returns: 7440a19e61eSDong Jia Shi * %0 on success and a negative error value on failure. 7450a19e61eSDong Jia Shi */ 7460a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp) 7470a19e61eSDong Jia Shi { 7480a19e61eSDong Jia Shi struct ccwchain *chain; 7490a19e61eSDong Jia Shi int len, idx, ret; 7500a19e61eSDong Jia Shi 7510a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 7520a19e61eSDong Jia Shi len = chain->ch_len; 7530a19e61eSDong Jia Shi for (idx = 0; idx < len; idx++) { 7540a19e61eSDong Jia Shi ret = ccwchain_fetch_one(chain, idx, cp); 7550a19e61eSDong Jia Shi if (ret) 756*d66a7355SHalil Pasic goto out_err; 7570a19e61eSDong Jia Shi } 7580a19e61eSDong Jia Shi } 7590a19e61eSDong Jia Shi 7600a19e61eSDong Jia Shi return 0; 761*d66a7355SHalil Pasic out_err: 762*d66a7355SHalil Pasic /* Only cleanup the chain elements that were actually translated. */ 763*d66a7355SHalil Pasic chain->ch_len = idx; 764*d66a7355SHalil Pasic list_for_each_entry_continue(chain, &cp->ccwchain_list, next) { 765*d66a7355SHalil Pasic chain->ch_len = 0; 766*d66a7355SHalil Pasic } 767*d66a7355SHalil Pasic return ret; 7680a19e61eSDong Jia Shi } 7690a19e61eSDong Jia Shi 7700a19e61eSDong Jia Shi /** 7710a19e61eSDong Jia Shi * cp_get_orb() - get the orb of the channel program 7720a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 7730a19e61eSDong Jia Shi * @intparm: new intparm for the returned orb 7740a19e61eSDong Jia Shi * @lpm: candidate value of the logical-path mask for the returned orb 7750a19e61eSDong Jia Shi * 7760a19e61eSDong Jia Shi * This function returns the address of the updated orb of the channel 7770a19e61eSDong Jia Shi * program. Channel I/O device drivers could use this orb to issue a 7780a19e61eSDong Jia Shi * ssch. 7790a19e61eSDong Jia Shi */ 7800a19e61eSDong Jia Shi union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm) 7810a19e61eSDong Jia Shi { 7820a19e61eSDong Jia Shi union orb *orb; 7830a19e61eSDong Jia Shi struct ccwchain *chain; 7840a19e61eSDong Jia Shi struct ccw1 *cpa; 7850a19e61eSDong Jia Shi 7860a19e61eSDong Jia Shi orb = &cp->orb; 7870a19e61eSDong Jia Shi 7880a19e61eSDong Jia Shi orb->cmd.intparm = intparm; 7890a19e61eSDong Jia Shi orb->cmd.fmt = 1; 7900a19e61eSDong Jia Shi orb->cmd.key = PAGE_DEFAULT_KEY >> 4; 7910a19e61eSDong Jia Shi 7920a19e61eSDong Jia Shi if (orb->cmd.lpm == 0) 7930a19e61eSDong Jia Shi orb->cmd.lpm = lpm; 7940a19e61eSDong Jia Shi 7950a19e61eSDong Jia Shi chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next); 7960a19e61eSDong Jia Shi cpa = chain->ch_ccw; 7970a19e61eSDong Jia Shi orb->cmd.cpa = (__u32) __pa(cpa); 7980a19e61eSDong Jia Shi 7990a19e61eSDong Jia Shi return orb; 8000a19e61eSDong Jia Shi } 8010a19e61eSDong Jia Shi 8020a19e61eSDong Jia Shi /** 8030a19e61eSDong Jia Shi * cp_update_scsw() - update scsw for a channel program. 8040a19e61eSDong Jia Shi * @cp: channel_program on which to perform the operation 8050a19e61eSDong Jia Shi * @scsw: I/O results of the channel program and also the target to be 8060a19e61eSDong Jia Shi * updated 8070a19e61eSDong Jia Shi * 8080a19e61eSDong Jia Shi * @scsw contains the I/O results of the channel program that pointed 8090a19e61eSDong Jia Shi * to by @cp. However what @scsw->cpa stores is a host physical 8100a19e61eSDong Jia Shi * address, which is meaningless for the guest, which is waiting for 8110a19e61eSDong Jia Shi * the I/O results. 8120a19e61eSDong Jia Shi * 8130a19e61eSDong Jia Shi * This function updates @scsw->cpa to its coressponding guest physical 8140a19e61eSDong Jia Shi * address. 8150a19e61eSDong Jia Shi */ 8160a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw) 8170a19e61eSDong Jia Shi { 8180a19e61eSDong Jia Shi struct ccwchain *chain; 8190a19e61eSDong Jia Shi u32 cpa = scsw->cmd.cpa; 8200a19e61eSDong Jia Shi u32 ccw_head, ccw_tail; 8210a19e61eSDong Jia Shi 8220a19e61eSDong Jia Shi /* 8230a19e61eSDong Jia Shi * LATER: 8240a19e61eSDong Jia Shi * For now, only update the cmd.cpa part. We may need to deal with 8250a19e61eSDong Jia Shi * other portions of the schib as well, even if we don't return them 8260a19e61eSDong Jia Shi * in the ioctl directly. Path status changes etc. 8270a19e61eSDong Jia Shi */ 8280a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 8290a19e61eSDong Jia Shi ccw_head = (u32)(u64)chain->ch_ccw; 8300a19e61eSDong Jia Shi ccw_tail = (u32)(u64)(chain->ch_ccw + chain->ch_len - 1); 8310a19e61eSDong Jia Shi 8320a19e61eSDong Jia Shi if ((ccw_head <= cpa) && (cpa <= ccw_tail)) { 8330a19e61eSDong Jia Shi /* 8340a19e61eSDong Jia Shi * (cpa - ccw_head) is the offset value of the host 8350a19e61eSDong Jia Shi * physical ccw to its chain head. 8360a19e61eSDong Jia Shi * Adding this value to the guest physical ccw chain 8370a19e61eSDong Jia Shi * head gets us the guest cpa. 8380a19e61eSDong Jia Shi */ 8390a19e61eSDong Jia Shi cpa = chain->ch_iova + (cpa - ccw_head); 8400a19e61eSDong Jia Shi break; 8410a19e61eSDong Jia Shi } 8420a19e61eSDong Jia Shi } 8430a19e61eSDong Jia Shi 8440a19e61eSDong Jia Shi scsw->cmd.cpa = cpa; 8450a19e61eSDong Jia Shi } 8460a19e61eSDong Jia Shi 8470a19e61eSDong Jia Shi /** 8480a19e61eSDong Jia Shi * cp_iova_pinned() - check if an iova is pinned for a ccw chain. 849364e3f90SSebastian Ott * @cp: channel_program on which to perform the operation 8500a19e61eSDong Jia Shi * @iova: the iova to check 8510a19e61eSDong Jia Shi * 8520a19e61eSDong Jia Shi * If the @iova is currently pinned for the ccw chain, return true; 8530a19e61eSDong Jia Shi * else return false. 8540a19e61eSDong Jia Shi */ 8550a19e61eSDong Jia Shi bool cp_iova_pinned(struct channel_program *cp, u64 iova) 8560a19e61eSDong Jia Shi { 8570a19e61eSDong Jia Shi struct ccwchain *chain; 8580a19e61eSDong Jia Shi int i; 8590a19e61eSDong Jia Shi 8600a19e61eSDong Jia Shi list_for_each_entry(chain, &cp->ccwchain_list, next) { 8610a19e61eSDong Jia Shi for (i = 0; i < chain->ch_len; i++) 8620a19e61eSDong Jia Shi if (pfn_array_table_iova_pinned(chain->ch_pat + i, 8630a19e61eSDong Jia Shi iova)) 8640a19e61eSDong Jia Shi return true; 8650a19e61eSDong Jia Shi } 8660a19e61eSDong Jia Shi 8670a19e61eSDong Jia Shi return false; 8680a19e61eSDong Jia Shi } 869