xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_cp.c (revision cfedb3d5e602dbf12e254cf88aceac348342f9b2)
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>
140a19e61eSDong Jia Shi #include <linux/iommu.h>
150a19e61eSDong Jia Shi #include <linux/vfio.h>
160a19e61eSDong Jia Shi #include <asm/idals.h>
170a19e61eSDong Jia Shi 
180a19e61eSDong Jia Shi #include "vfio_ccw_cp.h"
190a587956SJason Gunthorpe #include "vfio_ccw_private.h"
200a19e61eSDong Jia Shi 
210a19e61eSDong Jia Shi struct pfn_array {
2280c57f7aSDong Jia Shi 	/* Starting guest physical I/O address. */
230a19e61eSDong Jia Shi 	unsigned long		pa_iova;
2480c57f7aSDong Jia Shi 	/* Array that stores PFNs of the pages need to pin. */
250a19e61eSDong Jia Shi 	unsigned long		*pa_iova_pfn;
2680c57f7aSDong Jia Shi 	/* Array that receives PFNs of the pages pinned. */
270a19e61eSDong Jia Shi 	unsigned long		*pa_pfn;
285c1cfb1cSDong Jia Shi 	/* Number of pages pinned from @pa_iova. */
290a19e61eSDong Jia Shi 	int			pa_nr;
300a19e61eSDong Jia Shi };
310a19e61eSDong Jia Shi 
320a19e61eSDong Jia Shi struct ccwchain {
330a19e61eSDong Jia Shi 	struct list_head	next;
340a19e61eSDong Jia Shi 	struct ccw1		*ch_ccw;
350a19e61eSDong Jia Shi 	/* Guest physical address of the current chain. */
360a19e61eSDong Jia Shi 	u64			ch_iova;
370a19e61eSDong Jia Shi 	/* Count of the valid ccws in chain. */
380a19e61eSDong Jia Shi 	int			ch_len;
390a19e61eSDong Jia Shi 	/* Pinned PAGEs for the original data. */
40e7eaf91bSEric Farman 	struct pfn_array	*ch_pa;
410a19e61eSDong Jia Shi };
420a19e61eSDong Jia Shi 
430a19e61eSDong Jia Shi /*
44e4f3f18bSEric Farman  * pfn_array_alloc() - alloc memory for PFNs
450a19e61eSDong Jia Shi  * @pa: pfn_array on which to perform the operation
465c1cfb1cSDong Jia Shi  * @iova: target guest physical address
475c1cfb1cSDong Jia Shi  * @len: number of bytes that should be pinned from @iova
480a19e61eSDong Jia Shi  *
49e4f3f18bSEric Farman  * Attempt to allocate memory for PFNs.
500a19e61eSDong Jia Shi  *
510a19e61eSDong Jia Shi  * Usage of pfn_array:
525c1cfb1cSDong Jia Shi  * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in
535c1cfb1cSDong Jia Shi  * this structure will be filled in by this function.
540a19e61eSDong Jia Shi  *
550a19e61eSDong Jia Shi  * Returns:
56e4f3f18bSEric Farman  *         0 if PFNs are allocated
57e4f3f18bSEric Farman  *   -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova_pfn is not NULL
58e4f3f18bSEric Farman  *   -ENOMEM if alloc failed
590a19e61eSDong Jia Shi  */
60e4f3f18bSEric Farman static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len)
610a19e61eSDong Jia Shi {
62e4f3f18bSEric Farman 	int i;
630a19e61eSDong Jia Shi 
645c1cfb1cSDong Jia Shi 	if (pa->pa_nr || pa->pa_iova_pfn)
650a19e61eSDong Jia Shi 		return -EINVAL;
660a19e61eSDong Jia Shi 
670a19e61eSDong Jia Shi 	pa->pa_iova = iova;
680a19e61eSDong Jia Shi 
690a19e61eSDong Jia Shi 	pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
700a19e61eSDong Jia Shi 	if (!pa->pa_nr)
710a19e61eSDong Jia Shi 		return -EINVAL;
720a19e61eSDong Jia Shi 
730a19e61eSDong Jia Shi 	pa->pa_iova_pfn = kcalloc(pa->pa_nr,
740a19e61eSDong Jia Shi 				  sizeof(*pa->pa_iova_pfn) +
750a19e61eSDong Jia Shi 				  sizeof(*pa->pa_pfn),
760a19e61eSDong Jia Shi 				  GFP_KERNEL);
77c1ab6926SFarhan Ali 	if (unlikely(!pa->pa_iova_pfn)) {
78c1ab6926SFarhan Ali 		pa->pa_nr = 0;
790a19e61eSDong Jia Shi 		return -ENOMEM;
80c1ab6926SFarhan Ali 	}
810a19e61eSDong Jia Shi 	pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
820a19e61eSDong Jia Shi 
835c1cfb1cSDong Jia Shi 	pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
84c34a12e6SEric Farman 	pa->pa_pfn[0] = -1ULL;
85c34a12e6SEric Farman 	for (i = 1; i < pa->pa_nr; i++) {
865c1cfb1cSDong Jia Shi 		pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
87c34a12e6SEric Farman 		pa->pa_pfn[i] = -1ULL;
88c34a12e6SEric Farman 	}
890a19e61eSDong Jia Shi 
90e4f3f18bSEric Farman 	return 0;
91e4f3f18bSEric Farman }
92e4f3f18bSEric Farman 
93e4f3f18bSEric Farman /*
94*cfedb3d5SNicolin Chen  * pfn_array_unpin() - Unpin user pages in memory
95*cfedb3d5SNicolin Chen  * @pa: pfn_array on which to perform the operation
96*cfedb3d5SNicolin Chen  * @vdev: the vfio device to perform the operation
97*cfedb3d5SNicolin Chen  * @pa_nr: number of user pages to unpin
98*cfedb3d5SNicolin Chen  *
99*cfedb3d5SNicolin Chen  * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0,
100*cfedb3d5SNicolin Chen  * otherwise only clear pa->pa_nr
101*cfedb3d5SNicolin Chen  */
102*cfedb3d5SNicolin Chen static void pfn_array_unpin(struct pfn_array *pa,
103*cfedb3d5SNicolin Chen 			    struct vfio_device *vdev, int pa_nr)
104*cfedb3d5SNicolin Chen {
105*cfedb3d5SNicolin Chen 	int unpinned = 0, npage = 1;
106*cfedb3d5SNicolin Chen 
107*cfedb3d5SNicolin Chen 	while (unpinned < pa_nr) {
108*cfedb3d5SNicolin Chen 		unsigned long *first = &pa->pa_iova_pfn[unpinned];
109*cfedb3d5SNicolin Chen 		unsigned long *last = &first[npage];
110*cfedb3d5SNicolin Chen 
111*cfedb3d5SNicolin Chen 		if (unpinned + npage < pa_nr &&
112*cfedb3d5SNicolin Chen 		    *first + npage == *last) {
113*cfedb3d5SNicolin Chen 			npage++;
114*cfedb3d5SNicolin Chen 			continue;
115*cfedb3d5SNicolin Chen 		}
116*cfedb3d5SNicolin Chen 
117*cfedb3d5SNicolin Chen 		vfio_unpin_pages(vdev, first, npage);
118*cfedb3d5SNicolin Chen 		unpinned += npage;
119*cfedb3d5SNicolin Chen 		npage = 1;
120*cfedb3d5SNicolin Chen 	}
121*cfedb3d5SNicolin Chen 
122*cfedb3d5SNicolin Chen 	pa->pa_nr = 0;
123*cfedb3d5SNicolin Chen }
124*cfedb3d5SNicolin Chen 
125*cfedb3d5SNicolin Chen /*
126e4f3f18bSEric Farman  * pfn_array_pin() - Pin user pages in memory
127e4f3f18bSEric Farman  * @pa: pfn_array on which to perform the operation
128e4f3f18bSEric Farman  * @mdev: the mediated device to perform pin operations
129e4f3f18bSEric Farman  *
130e4f3f18bSEric Farman  * Returns number of pages pinned upon success.
131e4f3f18bSEric Farman  * If the pin request partially succeeds, or fails completely,
132e4f3f18bSEric Farman  * all pages are left unpinned and a negative error value is returned.
133e4f3f18bSEric Farman  */
1340a587956SJason Gunthorpe static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev)
135e4f3f18bSEric Farman {
136*cfedb3d5SNicolin Chen 	int pinned = 0, npage = 1;
137e4f3f18bSEric Farman 	int ret = 0;
138e4f3f18bSEric Farman 
139*cfedb3d5SNicolin Chen 	while (pinned < pa->pa_nr) {
140*cfedb3d5SNicolin Chen 		unsigned long *first = &pa->pa_iova_pfn[pinned];
141*cfedb3d5SNicolin Chen 		unsigned long *last = &first[npage];
1425c1cfb1cSDong Jia Shi 
143*cfedb3d5SNicolin Chen 		if (pinned + npage < pa->pa_nr &&
144*cfedb3d5SNicolin Chen 		    *first + npage == *last) {
145*cfedb3d5SNicolin Chen 			npage++;
146*cfedb3d5SNicolin Chen 			continue;
147*cfedb3d5SNicolin Chen 		}
148*cfedb3d5SNicolin Chen 
149*cfedb3d5SNicolin Chen 		ret = vfio_pin_pages(vdev, first, npage,
150*cfedb3d5SNicolin Chen 				     IOMMU_READ | IOMMU_WRITE,
151*cfedb3d5SNicolin Chen 				     &pa->pa_pfn[pinned]);
1525c1cfb1cSDong Jia Shi 		if (ret < 0) {
1535c1cfb1cSDong Jia Shi 			goto err_out;
154*cfedb3d5SNicolin Chen 		} else if (ret > 0 && ret != npage) {
155*cfedb3d5SNicolin Chen 			pinned += ret;
1560a19e61eSDong Jia Shi 			ret = -EINVAL;
1575c1cfb1cSDong Jia Shi 			goto err_out;
1585c1cfb1cSDong Jia Shi 		}
159*cfedb3d5SNicolin Chen 		pinned += npage;
160*cfedb3d5SNicolin Chen 		npage = 1;
161*cfedb3d5SNicolin Chen 	}
1620a19e61eSDong Jia Shi 
1630a19e61eSDong Jia Shi 	return ret;
1645c1cfb1cSDong Jia Shi 
1655c1cfb1cSDong Jia Shi err_out:
166*cfedb3d5SNicolin Chen 	pfn_array_unpin(pa, vdev, pinned);
1675c1cfb1cSDong Jia Shi 	return ret;
1685c1cfb1cSDong Jia Shi }
1695c1cfb1cSDong Jia Shi 
1705c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */
1710a587956SJason Gunthorpe static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev)
1725c1cfb1cSDong Jia Shi {
173*cfedb3d5SNicolin Chen 	pfn_array_unpin(pa, vdev, pa->pa_nr);
1745c1cfb1cSDong Jia Shi 	kfree(pa->pa_iova_pfn);
1750a19e61eSDong Jia Shi }
1760a19e61eSDong Jia Shi 
177e7eaf91bSEric Farman static bool pfn_array_iova_pinned(struct pfn_array *pa, unsigned long iova)
1780a19e61eSDong Jia Shi {
179e7eaf91bSEric Farman 	unsigned long iova_pfn = iova >> PAGE_SHIFT;
1800a19e61eSDong Jia Shi 	int i;
1810a19e61eSDong Jia Shi 
182e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
183e7eaf91bSEric Farman 		if (pa->pa_iova_pfn[i] == iova_pfn)
1840a19e61eSDong Jia Shi 			return true;
1850a19e61eSDong Jia Shi 
1860a19e61eSDong Jia Shi 	return false;
1870a19e61eSDong Jia Shi }
188e7eaf91bSEric Farman /* Create the list of IDAL words for a pfn_array. */
189e7eaf91bSEric Farman static inline void pfn_array_idal_create_words(
190e7eaf91bSEric Farman 	struct pfn_array *pa,
1910a19e61eSDong Jia Shi 	unsigned long *idaws)
1920a19e61eSDong Jia Shi {
193e7eaf91bSEric Farman 	int i;
1940a19e61eSDong Jia Shi 
1950a19e61eSDong Jia Shi 	/*
1960a19e61eSDong Jia Shi 	 * Idal words (execept the first one) rely on the memory being 4k
1970a19e61eSDong Jia Shi 	 * aligned. If a user virtual address is 4K aligned, then it's
1980a19e61eSDong Jia Shi 	 * corresponding kernel physical address will also be 4K aligned. Thus
1990a19e61eSDong Jia Shi 	 * there will be no problem here to simply use the phys to create an
2000a19e61eSDong Jia Shi 	 * idaw.
2010a19e61eSDong Jia Shi 	 */
202e7eaf91bSEric Farman 
203e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
204e7eaf91bSEric Farman 		idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT;
2058aabf0edSEric Farman 
2068aabf0edSEric Farman 	/* Adjust the first IDAW, since it may not start on a page boundary */
207e7eaf91bSEric Farman 	idaws[0] += pa->pa_iova & (PAGE_SIZE - 1);
2080a19e61eSDong Jia Shi }
2090a19e61eSDong Jia Shi 
210dbd66558SCornelia Huck static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
2117f8e89a8SEric Farman {
2127f8e89a8SEric Farman 	struct ccw0 ccw0;
2137f8e89a8SEric Farman 	struct ccw1 *pccw1 = source;
2147f8e89a8SEric Farman 	int i;
2157f8e89a8SEric Farman 
2167f8e89a8SEric Farman 	for (i = 0; i < len; i++) {
2177f8e89a8SEric Farman 		ccw0 = *(struct ccw0 *)pccw1;
2187f8e89a8SEric Farman 		if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
2197f8e89a8SEric Farman 			pccw1->cmd_code = CCW_CMD_TIC;
2207f8e89a8SEric Farman 			pccw1->flags = 0;
2217f8e89a8SEric Farman 			pccw1->count = 0;
2227f8e89a8SEric Farman 		} else {
2237f8e89a8SEric Farman 			pccw1->cmd_code = ccw0.cmd_code;
2247f8e89a8SEric Farman 			pccw1->flags = ccw0.flags;
2257f8e89a8SEric Farman 			pccw1->count = ccw0.count;
2267f8e89a8SEric Farman 		}
2277f8e89a8SEric Farman 		pccw1->cda = ccw0.cda;
2287f8e89a8SEric Farman 		pccw1++;
2297f8e89a8SEric Farman 	}
2307f8e89a8SEric Farman }
2310a19e61eSDong Jia Shi 
2320a19e61eSDong Jia Shi /*
2330a19e61eSDong Jia Shi  * Within the domain (@mdev), copy @n bytes from a guest physical
2340a19e61eSDong Jia Shi  * address (@iova) to a host physical address (@to).
2350a19e61eSDong Jia Shi  */
2360a587956SJason Gunthorpe static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova,
2370a19e61eSDong Jia Shi 			   unsigned long n)
2380a19e61eSDong Jia Shi {
2390a19e61eSDong Jia Shi 	struct pfn_array pa = {0};
2400a19e61eSDong Jia Shi 	u64 from;
2410a19e61eSDong Jia Shi 	int i, ret;
2420a19e61eSDong Jia Shi 	unsigned long l, m;
2430a19e61eSDong Jia Shi 
244e4f3f18bSEric Farman 	ret = pfn_array_alloc(&pa, iova, n);
245e4f3f18bSEric Farman 	if (ret < 0)
2460a19e61eSDong Jia Shi 		return ret;
2470a19e61eSDong Jia Shi 
2480a587956SJason Gunthorpe 	ret = pfn_array_pin(&pa, vdev);
249e4f3f18bSEric Farman 	if (ret < 0) {
2500a587956SJason Gunthorpe 		pfn_array_unpin_free(&pa, vdev);
251e4f3f18bSEric Farman 		return ret;
252e4f3f18bSEric Farman 	}
253e4f3f18bSEric Farman 
2540a19e61eSDong Jia Shi 	l = n;
2550a19e61eSDong Jia Shi 	for (i = 0; i < pa.pa_nr; i++) {
2560a19e61eSDong Jia Shi 		from = pa.pa_pfn[i] << PAGE_SHIFT;
2570a19e61eSDong Jia Shi 		m = PAGE_SIZE;
2580a19e61eSDong Jia Shi 		if (i == 0) {
2590a19e61eSDong Jia Shi 			from += iova & (PAGE_SIZE - 1);
2600a19e61eSDong Jia Shi 			m -= iova & (PAGE_SIZE - 1);
2610a19e61eSDong Jia Shi 		}
2620a19e61eSDong Jia Shi 
2630a19e61eSDong Jia Shi 		m = min(l, m);
2640a19e61eSDong Jia Shi 		memcpy(to + (n - l), (void *)from, m);
2650a19e61eSDong Jia Shi 
2660a19e61eSDong Jia Shi 		l -= m;
2670a19e61eSDong Jia Shi 		if (l == 0)
2680a19e61eSDong Jia Shi 			break;
2690a19e61eSDong Jia Shi 	}
2700a19e61eSDong Jia Shi 
2710a587956SJason Gunthorpe 	pfn_array_unpin_free(&pa, vdev);
2720a19e61eSDong Jia Shi 
2730a19e61eSDong Jia Shi 	return l;
2740a19e61eSDong Jia Shi }
2750a19e61eSDong Jia Shi 
2760a19e61eSDong Jia Shi /*
2770a19e61eSDong Jia Shi  * Helpers to operate ccwchain.
2780a19e61eSDong Jia Shi  */
2795d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
2805d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
2815d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
2825d87fbf7SEric Farman 
2830a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
2840a19e61eSDong Jia Shi 
2850a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
2860a19e61eSDong Jia Shi 
2870a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
2885d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
2890a19e61eSDong Jia Shi 
2900a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
2910a19e61eSDong Jia Shi 
29248bd0eeeSEric Farman /*
2935d87fbf7SEric Farman  * ccw_does_data_transfer()
2945d87fbf7SEric Farman  *
2955d87fbf7SEric Farman  * Determine whether a CCW will move any data, such that the guest pages
2965d87fbf7SEric Farman  * would need to be pinned before performing the I/O.
2975d87fbf7SEric Farman  *
2985d87fbf7SEric Farman  * Returns 1 if yes, 0 if no.
2995d87fbf7SEric Farman  */
3005d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw)
3015d87fbf7SEric Farman {
302453eac31SEric Farman 	/* If the count field is zero, then no data will be transferred */
303453eac31SEric Farman 	if (ccw->count == 0)
304453eac31SEric Farman 		return 0;
305453eac31SEric Farman 
3069b6e57e5SEric Farman 	/* If the command is a NOP, then no data will be transferred */
3079b6e57e5SEric Farman 	if (ccw_is_noop(ccw))
3089b6e57e5SEric Farman 		return 0;
3099b6e57e5SEric Farman 
3105d87fbf7SEric Farman 	/* If the skip flag is off, then data will be transferred */
3115d87fbf7SEric Farman 	if (!ccw_is_skip(ccw))
3125d87fbf7SEric Farman 		return 1;
3135d87fbf7SEric Farman 
3145d87fbf7SEric Farman 	/*
3155d87fbf7SEric Farman 	 * If the skip flag is on, it is only meaningful if the command
3165d87fbf7SEric Farman 	 * code is a read, read backward, sense, or sense ID.  In those
3175d87fbf7SEric Farman 	 * cases, no data will be transferred.
3185d87fbf7SEric Farman 	 */
3195d87fbf7SEric Farman 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
3205d87fbf7SEric Farman 		return 0;
3215d87fbf7SEric Farman 
3225d87fbf7SEric Farman 	if (ccw_is_sense(ccw))
3235d87fbf7SEric Farman 		return 0;
3245d87fbf7SEric Farman 
3255d87fbf7SEric Farman 	/* The skip flag is on, but it is ignored for this command code. */
3265d87fbf7SEric Farman 	return 1;
3275d87fbf7SEric Farman }
3285d87fbf7SEric Farman 
3295d87fbf7SEric Farman /*
33048bd0eeeSEric Farman  * is_cpa_within_range()
33148bd0eeeSEric Farman  *
33248bd0eeeSEric Farman  * @cpa: channel program address being questioned
33348bd0eeeSEric Farman  * @head: address of the beginning of a CCW chain
33448bd0eeeSEric Farman  * @len: number of CCWs within the chain
33548bd0eeeSEric Farman  *
33648bd0eeeSEric Farman  * Determine whether the address of a CCW (whether a new chain,
33748bd0eeeSEric Farman  * or the target of a TIC) falls within a range (including the end points).
33848bd0eeeSEric Farman  *
33948bd0eeeSEric Farman  * Returns 1 if yes, 0 if no.
34048bd0eeeSEric Farman  */
34148bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
34248bd0eeeSEric Farman {
34348bd0eeeSEric Farman 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
34448bd0eeeSEric Farman 
34548bd0eeeSEric Farman 	return (head <= cpa && cpa <= tail);
34648bd0eeeSEric Farman }
34748bd0eeeSEric Farman 
34848bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
34948bd0eeeSEric Farman {
35048bd0eeeSEric Farman 	if (!ccw_is_tic(ccw))
35148bd0eeeSEric Farman 		return 0;
35248bd0eeeSEric Farman 
35348bd0eeeSEric Farman 	return is_cpa_within_range(ccw->cda, head, len);
35448bd0eeeSEric Farman }
35548bd0eeeSEric Farman 
3560a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
3570a19e61eSDong Jia Shi {
3580a19e61eSDong Jia Shi 	struct ccwchain *chain;
3590a19e61eSDong Jia Shi 	void *data;
3600a19e61eSDong Jia Shi 	size_t size;
3610a19e61eSDong Jia Shi 
3620a19e61eSDong Jia Shi 	/* Make ccw address aligned to 8. */
3630a19e61eSDong Jia Shi 	size = ((sizeof(*chain) + 7L) & -8L) +
3640a19e61eSDong Jia Shi 		sizeof(*chain->ch_ccw) * len +
365e7eaf91bSEric Farman 		sizeof(*chain->ch_pa) * len;
3660a19e61eSDong Jia Shi 	chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
3670a19e61eSDong Jia Shi 	if (!chain)
3680a19e61eSDong Jia Shi 		return NULL;
3690a19e61eSDong Jia Shi 
3700a19e61eSDong Jia Shi 	data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
3710a19e61eSDong Jia Shi 	chain->ch_ccw = (struct ccw1 *)data;
3720a19e61eSDong Jia Shi 
3730a19e61eSDong Jia Shi 	data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
374e7eaf91bSEric Farman 	chain->ch_pa = (struct pfn_array *)data;
3750a19e61eSDong Jia Shi 
3760a19e61eSDong Jia Shi 	chain->ch_len = len;
3770a19e61eSDong Jia Shi 
3780a19e61eSDong Jia Shi 	list_add_tail(&chain->next, &cp->ccwchain_list);
3790a19e61eSDong Jia Shi 
3800a19e61eSDong Jia Shi 	return chain;
3810a19e61eSDong Jia Shi }
3820a19e61eSDong Jia Shi 
3830a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain)
3840a19e61eSDong Jia Shi {
3850a19e61eSDong Jia Shi 	list_del(&chain->next);
3860a19e61eSDong Jia Shi 	kfree(chain);
3870a19e61eSDong Jia Shi }
3880a19e61eSDong Jia Shi 
3890a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */
3900a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx)
3910a19e61eSDong Jia Shi {
3920a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
3930a19e61eSDong Jia Shi 
3949b6e57e5SEric Farman 	if (ccw_is_tic(ccw))
395408358b5SJason J. Herne 		return;
3960a19e61eSDong Jia Shi 
3970a19e61eSDong Jia Shi 	kfree((void *)(u64)ccw->cda);
3980a19e61eSDong Jia Shi }
3990a19e61eSDong Jia Shi 
4000a19e61eSDong Jia Shi /**
4010a19e61eSDong Jia Shi  * ccwchain_calc_length - calculate the length of the ccw chain.
4020a19e61eSDong Jia Shi  * @iova: guest physical address of the target ccw chain
4030a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
4040a19e61eSDong Jia Shi  *
4050a19e61eSDong Jia Shi  * This is the chain length not considering any TICs.
4060a19e61eSDong Jia Shi  * You need to do a new round for each TIC target.
4070a19e61eSDong Jia Shi  *
408fb9e7880SHalil Pasic  * The program is also validated for absence of not yet supported
409fb9e7880SHalil Pasic  * indirect data addressing scenarios.
410fb9e7880SHalil Pasic  *
4110a19e61eSDong Jia Shi  * Returns: the length of the ccw chain or -errno.
4120a19e61eSDong Jia Shi  */
4130a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
4140a19e61eSDong Jia Shi {
4151d897e47SEric Farman 	struct ccw1 *ccw = cp->guest_cp;
416ded563f3SEric Farman 	int cnt = 0;
4170a19e61eSDong Jia Shi 
4180a19e61eSDong Jia Shi 	do {
4190a19e61eSDong Jia Shi 		cnt++;
4200a19e61eSDong Jia Shi 
421fb9e7880SHalil Pasic 		/*
422fb9e7880SHalil Pasic 		 * As we don't want to fail direct addressing even if the
423fb9e7880SHalil Pasic 		 * orb specified one of the unsupported formats, we defer
424fb9e7880SHalil Pasic 		 * checking for IDAWs in unsupported formats to here.
425fb9e7880SHalil Pasic 		 */
4261d897e47SEric Farman 		if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw))
427fb9e7880SHalil Pasic 			return -EOPNOTSUPP;
428fb9e7880SHalil Pasic 
42948bd0eeeSEric Farman 		/*
43048bd0eeeSEric Farman 		 * We want to keep counting if the current CCW has the
43148bd0eeeSEric Farman 		 * command-chaining flag enabled, or if it is a TIC CCW
43248bd0eeeSEric Farman 		 * that loops back into the current chain.  The latter
43348bd0eeeSEric Farman 		 * is used for device orientation, where the CCW PRIOR to
43448bd0eeeSEric Farman 		 * the TIC can either jump to the TIC or a CCW immediately
43548bd0eeeSEric Farman 		 * after the TIC, depending on the results of its operation.
43648bd0eeeSEric Farman 		 */
43748bd0eeeSEric Farman 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
4380a19e61eSDong Jia Shi 			break;
4390a19e61eSDong Jia Shi 
4400a19e61eSDong Jia Shi 		ccw++;
4410a19e61eSDong Jia Shi 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
4420a19e61eSDong Jia Shi 
4430a19e61eSDong Jia Shi 	if (cnt == CCWCHAIN_LEN_MAX + 1)
4440a19e61eSDong Jia Shi 		cnt = -EINVAL;
4450a19e61eSDong Jia Shi 
4460a19e61eSDong Jia Shi 	return cnt;
4470a19e61eSDong Jia Shi }
4480a19e61eSDong Jia Shi 
4490a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
4500a19e61eSDong Jia Shi {
4510a19e61eSDong Jia Shi 	struct ccwchain *chain;
4522904337fSEric Farman 	u32 ccw_head;
4530a19e61eSDong Jia Shi 
4540a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
4550a19e61eSDong Jia Shi 		ccw_head = chain->ch_iova;
4562904337fSEric Farman 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
4570a19e61eSDong Jia Shi 			return 1;
4580a19e61eSDong Jia Shi 	}
4590a19e61eSDong Jia Shi 
4600a19e61eSDong Jia Shi 	return 0;
4610a19e61eSDong Jia Shi }
4620a19e61eSDong Jia Shi 
4630a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain,
4640a19e61eSDong Jia Shi 			     struct channel_program *cp);
4650a19e61eSDong Jia Shi 
466363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
4670a19e61eSDong Jia Shi {
4680a587956SJason Gunthorpe 	struct vfio_device *vdev =
4690a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
4700a19e61eSDong Jia Shi 	struct ccwchain *chain;
4718b515be5SFarhan Ali 	int len, ret;
4720a19e61eSDong Jia Shi 
473ded563f3SEric Farman 	/* Copy 2K (the most we support today) of possible CCWs */
4740a587956SJason Gunthorpe 	len = copy_from_iova(vdev, cp->guest_cp, cda,
4755223bee8SEric Farman 			     CCWCHAIN_LEN_MAX * sizeof(struct ccw1));
476ded563f3SEric Farman 	if (len)
477ded563f3SEric Farman 		return len;
478ded563f3SEric Farman 
4797f8e89a8SEric Farman 	/* Convert any Format-0 CCWs to Format-1 */
4807f8e89a8SEric Farman 	if (!cp->orb.cmd.fmt)
481c382cbc6SEric Farman 		convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
4827f8e89a8SEric Farman 
483ded563f3SEric Farman 	/* Count the CCWs in the current chain */
484363fe5f7SEric Farman 	len = ccwchain_calc_length(cda, cp);
4850a19e61eSDong Jia Shi 	if (len < 0)
4860a19e61eSDong Jia Shi 		return len;
4870a19e61eSDong Jia Shi 
4880a19e61eSDong Jia Shi 	/* Need alloc a new chain for this one. */
4890a19e61eSDong Jia Shi 	chain = ccwchain_alloc(cp, len);
4900a19e61eSDong Jia Shi 	if (!chain)
4910a19e61eSDong Jia Shi 		return -ENOMEM;
492363fe5f7SEric Farman 	chain->ch_iova = cda;
4930a19e61eSDong Jia Shi 
49462465902SEric Farman 	/* Copy the actual CCWs into the new chain */
49562465902SEric Farman 	memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
4960a19e61eSDong Jia Shi 
4970a19e61eSDong Jia Shi 	/* Loop for tics on this new chain. */
4988b515be5SFarhan Ali 	ret = ccwchain_loop_tic(chain, cp);
4998b515be5SFarhan Ali 
5008b515be5SFarhan Ali 	if (ret)
5018b515be5SFarhan Ali 		ccwchain_free(chain);
5028b515be5SFarhan Ali 
5038b515be5SFarhan Ali 	return ret;
5040a19e61eSDong Jia Shi }
5050a19e61eSDong Jia Shi 
5060a19e61eSDong Jia Shi /* Loop for TICs. */
5070a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
5080a19e61eSDong Jia Shi {
5090a19e61eSDong Jia Shi 	struct ccw1 *tic;
5100a19e61eSDong Jia Shi 	int i, ret;
5110a19e61eSDong Jia Shi 
5120a19e61eSDong Jia Shi 	for (i = 0; i < chain->ch_len; i++) {
5130a19e61eSDong Jia Shi 		tic = chain->ch_ccw + i;
5140a19e61eSDong Jia Shi 
5150a19e61eSDong Jia Shi 		if (!ccw_is_tic(tic))
5160a19e61eSDong Jia Shi 			continue;
5170a19e61eSDong Jia Shi 
518e64bd689SEric Farman 		/* May transfer to an existing chain. */
519e64bd689SEric Farman 		if (tic_target_chain_exists(tic, cp))
520e64bd689SEric Farman 			continue;
521e64bd689SEric Farman 
522363fe5f7SEric Farman 		/* Build a ccwchain for the next segment */
523363fe5f7SEric Farman 		ret = ccwchain_handle_ccw(tic->cda, cp);
5240a19e61eSDong Jia Shi 		if (ret)
5250a19e61eSDong Jia Shi 			return ret;
5260a19e61eSDong Jia Shi 	}
5270a19e61eSDong Jia Shi 
5280a19e61eSDong Jia Shi 	return 0;
5290a19e61eSDong Jia Shi }
5300a19e61eSDong Jia Shi 
5310a19e61eSDong Jia Shi static int ccwchain_fetch_tic(struct ccwchain *chain,
5320a19e61eSDong Jia Shi 			      int idx,
5330a19e61eSDong Jia Shi 			      struct channel_program *cp)
5340a19e61eSDong Jia Shi {
5350a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
5360a19e61eSDong Jia Shi 	struct ccwchain *iter;
5372904337fSEric Farman 	u32 ccw_head;
5380a19e61eSDong Jia Shi 
5390a19e61eSDong Jia Shi 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
5400a19e61eSDong Jia Shi 		ccw_head = iter->ch_iova;
5412904337fSEric Farman 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
542c389377cSJason J. Herne 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
5430a19e61eSDong Jia Shi 						     (ccw->cda - ccw_head));
5440a19e61eSDong Jia Shi 			return 0;
5450a19e61eSDong Jia Shi 		}
5460a19e61eSDong Jia Shi 	}
5470a19e61eSDong Jia Shi 
5480a19e61eSDong Jia Shi 	return -EFAULT;
5490a19e61eSDong Jia Shi }
5500a19e61eSDong Jia Shi 
5510a19e61eSDong Jia Shi static int ccwchain_fetch_direct(struct ccwchain *chain,
5520a19e61eSDong Jia Shi 				 int idx,
5530a19e61eSDong Jia Shi 				 struct channel_program *cp)
5540a19e61eSDong Jia Shi {
5550a587956SJason Gunthorpe 	struct vfio_device *vdev =
5560a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
5570a19e61eSDong Jia Shi 	struct ccw1 *ccw;
558e7eaf91bSEric Farman 	struct pfn_array *pa;
55901aa26c6SEric Farman 	u64 iova;
5600a19e61eSDong Jia Shi 	unsigned long *idaws;
5616238f921SDong Jia Shi 	int ret;
562453eac31SEric Farman 	int bytes = 1;
56301aa26c6SEric Farman 	int idaw_nr, idal_len;
56401aa26c6SEric Farman 	int i;
5650a19e61eSDong Jia Shi 
5660a19e61eSDong Jia Shi 	ccw = chain->ch_ccw + idx;
5670a19e61eSDong Jia Shi 
568e8573b39SEric Farman 	if (ccw->count)
569453eac31SEric Farman 		bytes = ccw->count;
570e8573b39SEric Farman 
571e8573b39SEric Farman 	/* Calculate size of IDAL */
57201aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
57301aa26c6SEric Farman 		/* Read first IDAW to see if it's 4K-aligned or not. */
57401aa26c6SEric Farman 		/* All subsequent IDAws will be 4K-aligned. */
5750a587956SJason Gunthorpe 		ret = copy_from_iova(vdev, &iova, ccw->cda, sizeof(iova));
57601aa26c6SEric Farman 		if (ret)
57701aa26c6SEric Farman 			return ret;
57801aa26c6SEric Farman 	} else {
57901aa26c6SEric Farman 		iova = ccw->cda;
58001aa26c6SEric Farman 	}
58101aa26c6SEric Farman 	idaw_nr = idal_nr_words((void *)iova, bytes);
58201aa26c6SEric Farman 	idal_len = idaw_nr * sizeof(*idaws);
583e8573b39SEric Farman 
584e8573b39SEric Farman 	/* Allocate an IDAL from host storage */
585e8573b39SEric Farman 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
586e8573b39SEric Farman 	if (!idaws) {
587e8573b39SEric Farman 		ret = -ENOMEM;
588e8573b39SEric Farman 		goto out_init;
5894cebc5d6SDong Jia Shi 	}
5904cebc5d6SDong Jia Shi 
5910a19e61eSDong Jia Shi 	/*
59201aa26c6SEric Farman 	 * Allocate an array of pfn's for pages to pin/translate.
59301aa26c6SEric Farman 	 * The number of pages is actually the count of the idaws
59401aa26c6SEric Farman 	 * required for the data transfer, since we only only support
59501aa26c6SEric Farman 	 * 4K IDAWs today.
5960a19e61eSDong Jia Shi 	 */
597e7eaf91bSEric Farman 	pa = chain->ch_pa + idx;
59801aa26c6SEric Farman 	ret = pfn_array_alloc(pa, iova, bytes);
599e4f3f18bSEric Farman 	if (ret < 0)
600e8573b39SEric Farman 		goto out_free_idaws;
601e4f3f18bSEric Farman 
60201aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
60301aa26c6SEric Farman 		/* Copy guest IDAL into host IDAL */
6040a587956SJason Gunthorpe 		ret = copy_from_iova(vdev, idaws, ccw->cda, idal_len);
60501aa26c6SEric Farman 		if (ret)
60601aa26c6SEric Farman 			goto out_unpin;
60701aa26c6SEric Farman 
60801aa26c6SEric Farman 		/*
60901aa26c6SEric Farman 		 * Copy guest IDAWs into pfn_array, in case the memory they
61001aa26c6SEric Farman 		 * occupy is not contiguous.
61101aa26c6SEric Farman 		 */
61201aa26c6SEric Farman 		for (i = 0; i < idaw_nr; i++)
61301aa26c6SEric Farman 			pa->pa_iova_pfn[i] = idaws[i] >> PAGE_SHIFT;
61401aa26c6SEric Farman 	} else {
61501aa26c6SEric Farman 		/*
61601aa26c6SEric Farman 		 * No action is required here; the iova addresses in pfn_array
61701aa26c6SEric Farman 		 * were initialized sequentially in pfn_array_alloc() beginning
61801aa26c6SEric Farman 		 * with the contents of ccw->cda.
61901aa26c6SEric Farman 		 */
62001aa26c6SEric Farman 	}
62101aa26c6SEric Farman 
6225d87fbf7SEric Farman 	if (ccw_does_data_transfer(ccw)) {
6230a587956SJason Gunthorpe 		ret = pfn_array_pin(pa, vdev);
6246238f921SDong Jia Shi 		if (ret < 0)
625806212f9SEric Farman 			goto out_unpin;
6265d87fbf7SEric Farman 	} else {
627e7eaf91bSEric Farman 		pa->pa_nr = 0;
6285d87fbf7SEric Farman 	}
6290a19e61eSDong Jia Shi 
6300a19e61eSDong Jia Shi 	ccw->cda = (__u32) virt_to_phys(idaws);
6310a19e61eSDong Jia Shi 	ccw->flags |= CCW_FLAG_IDA;
6320a19e61eSDong Jia Shi 
633e8573b39SEric Farman 	/* Populate the IDAL with pinned/translated addresses from pfn */
634e7eaf91bSEric Farman 	pfn_array_idal_create_words(pa, idaws);
6350a19e61eSDong Jia Shi 
6360a19e61eSDong Jia Shi 	return 0;
6376238f921SDong Jia Shi 
6386238f921SDong Jia Shi out_unpin:
6390a587956SJason Gunthorpe 	pfn_array_unpin_free(pa, vdev);
640e8573b39SEric Farman out_free_idaws:
641e8573b39SEric Farman 	kfree(idaws);
6426238f921SDong Jia Shi out_init:
6436238f921SDong Jia Shi 	ccw->cda = 0;
6446238f921SDong Jia Shi 	return ret;
6450a19e61eSDong Jia Shi }
6460a19e61eSDong Jia Shi 
6470a19e61eSDong Jia Shi /*
6480a19e61eSDong Jia Shi  * Fetch one ccw.
6490a19e61eSDong Jia Shi  * To reduce memory copy, we'll pin the cda page in memory,
6500a19e61eSDong Jia Shi  * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
6510a19e61eSDong Jia Shi  * direct ccws to idal ccws.
6520a19e61eSDong Jia Shi  */
6530a19e61eSDong Jia Shi static int ccwchain_fetch_one(struct ccwchain *chain,
6540a19e61eSDong Jia Shi 			      int idx,
6550a19e61eSDong Jia Shi 			      struct channel_program *cp)
6560a19e61eSDong Jia Shi {
6570a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
6580a19e61eSDong Jia Shi 
6590a19e61eSDong Jia Shi 	if (ccw_is_tic(ccw))
6600a19e61eSDong Jia Shi 		return ccwchain_fetch_tic(chain, idx, cp);
6610a19e61eSDong Jia Shi 
6620a19e61eSDong Jia Shi 	return ccwchain_fetch_direct(chain, idx, cp);
6630a19e61eSDong Jia Shi }
6640a19e61eSDong Jia Shi 
6650a19e61eSDong Jia Shi /**
6660a19e61eSDong Jia Shi  * cp_init() - allocate ccwchains for a channel program.
6670a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
6680a19e61eSDong Jia Shi  * @mdev: the mediated device to perform pin/unpin operations
6690a19e61eSDong Jia Shi  * @orb: control block for the channel program from the guest
6700a19e61eSDong Jia Shi  *
6710a19e61eSDong Jia Shi  * This creates one or more ccwchain(s), and copies the raw data of
6720a19e61eSDong Jia Shi  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
6730a19e61eSDong Jia Shi  *
6740a19e61eSDong Jia Shi  * Limitations:
675725b94d7SJared Rossi  * 1. Supports idal(c64) ccw chaining.
676725b94d7SJared Rossi  * 2. Supports 4k idaw.
6770a19e61eSDong Jia Shi  *
6780a19e61eSDong Jia Shi  * Returns:
6790a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
6800a19e61eSDong Jia Shi  */
6810a587956SJason Gunthorpe int cp_init(struct channel_program *cp, union orb *orb)
6820a19e61eSDong Jia Shi {
6830a587956SJason Gunthorpe 	struct vfio_device *vdev =
6840a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
685725b94d7SJared Rossi 	/* custom ratelimit used to avoid flood during guest IPL */
686725b94d7SJared Rossi 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
68799afcb05SEric Farman 	int ret;
6880a19e61eSDong Jia Shi 
689c6c82e0cSEric Farman 	/* this is an error in the caller */
690c6c82e0cSEric Farman 	if (cp->initialized)
691c6c82e0cSEric Farman 		return -EBUSY;
692c6c82e0cSEric Farman 
6930a19e61eSDong Jia Shi 	/*
694725b94d7SJared Rossi 	 * We only support prefetching the channel program. We assume all channel
695725b94d7SJared Rossi 	 * programs executed by supported guests likewise support prefetching.
696725b94d7SJared Rossi 	 * Executing a channel program that does not specify prefetching will
697725b94d7SJared Rossi 	 * typically not cause an error, but a warning is issued to help identify
698725b94d7SJared Rossi 	 * the problem if something does break.
6990a19e61eSDong Jia Shi 	 */
700725b94d7SJared Rossi 	if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
7010a587956SJason Gunthorpe 		dev_warn(
7020a587956SJason Gunthorpe 			vdev->dev,
7030a587956SJason Gunthorpe 			"Prefetching channel program even though prefetch not specified in ORB");
7040a19e61eSDong Jia Shi 
7050a19e61eSDong Jia Shi 	INIT_LIST_HEAD(&cp->ccwchain_list);
7060a19e61eSDong Jia Shi 	memcpy(&cp->orb, orb, sizeof(*orb));
7070a19e61eSDong Jia Shi 
70899afcb05SEric Farman 	/* Build a ccwchain for the first CCW segment */
70999afcb05SEric Farman 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
71099afcb05SEric Farman 
711c9f597a4SFarhan Ali 	if (!ret) {
712c9f597a4SFarhan Ali 		cp->initialized = true;
713c9f597a4SFarhan Ali 
714c9f597a4SFarhan Ali 		/* It is safe to force: if it was not set but idals used
715c9f597a4SFarhan Ali 		 * ccwchain_calc_length would have returned an error.
716fb9e7880SHalil Pasic 		 */
717fb9e7880SHalil Pasic 		cp->orb.cmd.c64 = 1;
718c9f597a4SFarhan Ali 	}
71971189f26SCornelia Huck 
7200a19e61eSDong Jia Shi 	return ret;
7210a19e61eSDong Jia Shi }
7220a19e61eSDong Jia Shi 
7230a19e61eSDong Jia Shi 
7240a19e61eSDong Jia Shi /**
7250a19e61eSDong Jia Shi  * cp_free() - free resources for channel program.
7260a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7270a19e61eSDong Jia Shi  *
7280a19e61eSDong Jia Shi  * This unpins the memory pages and frees the memory space occupied by
7290a19e61eSDong Jia Shi  * @cp, which must have been returned by a previous call to cp_init().
7300a19e61eSDong Jia Shi  * Otherwise, undefined behavior occurs.
7310a19e61eSDong Jia Shi  */
7320a19e61eSDong Jia Shi void cp_free(struct channel_program *cp)
7330a19e61eSDong Jia Shi {
7340a587956SJason Gunthorpe 	struct vfio_device *vdev =
7350a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
736812271b9SEric Farman 	struct ccwchain *chain, *temp;
737812271b9SEric Farman 	int i;
738812271b9SEric Farman 
739812271b9SEric Farman 	if (!cp->initialized)
740812271b9SEric Farman 		return;
741812271b9SEric Farman 
742812271b9SEric Farman 	cp->initialized = false;
743812271b9SEric Farman 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
744812271b9SEric Farman 		for (i = 0; i < chain->ch_len; i++) {
7450a587956SJason Gunthorpe 			pfn_array_unpin_free(chain->ch_pa + i, vdev);
746812271b9SEric Farman 			ccwchain_cda_free(chain, i);
747812271b9SEric Farman 		}
748812271b9SEric Farman 		ccwchain_free(chain);
749812271b9SEric Farman 	}
7500a19e61eSDong Jia Shi }
7510a19e61eSDong Jia Shi 
7520a19e61eSDong Jia Shi /**
7530a19e61eSDong Jia Shi  * cp_prefetch() - translate a guest physical address channel program to
7540a19e61eSDong Jia Shi  *                 a real-device runnable channel program.
7550a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7560a19e61eSDong Jia Shi  *
7570a19e61eSDong Jia Shi  * This function translates the guest-physical-address channel program
7580a19e61eSDong Jia Shi  * and stores the result to ccwchain list. @cp must have been
7590a19e61eSDong Jia Shi  * initialized by a previous call with cp_init(). Otherwise, undefined
7600a19e61eSDong Jia Shi  * behavior occurs.
761d66a7355SHalil Pasic  * For each chain composing the channel program:
762d66a7355SHalil Pasic  * - On entry ch_len holds the count of CCWs to be translated.
763d66a7355SHalil Pasic  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
764d66a7355SHalil Pasic  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
7650a19e61eSDong Jia Shi  *
7660a19e61eSDong Jia Shi  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
7670a19e61eSDong Jia Shi  * as helpers to do ccw chain translation inside the kernel. Basically
7680a19e61eSDong Jia Shi  * they accept a channel program issued by a virtual machine, and
7690a19e61eSDong Jia Shi  * translate the channel program to a real-device runnable channel
7700a19e61eSDong Jia Shi  * program.
7710a19e61eSDong Jia Shi  *
7720a19e61eSDong Jia Shi  * These APIs will copy the ccws into kernel-space buffers, and update
7730a19e61eSDong Jia Shi  * the guest phsical addresses with their corresponding host physical
7740a19e61eSDong Jia Shi  * addresses.  Then channel I/O device drivers could issue the
7750a19e61eSDong Jia Shi  * translated channel program to real devices to perform an I/O
7760a19e61eSDong Jia Shi  * operation.
7770a19e61eSDong Jia Shi  *
7780a19e61eSDong Jia Shi  * These interfaces are designed to support translation only for
7790a19e61eSDong Jia Shi  * channel programs, which are generated and formatted by a
7800a19e61eSDong Jia Shi  * guest. Thus this will make it possible for things like VFIO to
7810a19e61eSDong Jia Shi  * leverage the interfaces to passthrough a channel I/O mediated
7820a19e61eSDong Jia Shi  * device in QEMU.
7830a19e61eSDong Jia Shi  *
7840a19e61eSDong Jia Shi  * We support direct ccw chaining by translating them to idal ccws.
7850a19e61eSDong Jia Shi  *
7860a19e61eSDong Jia Shi  * Returns:
7870a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
7880a19e61eSDong Jia Shi  */
7890a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp)
7900a19e61eSDong Jia Shi {
7910a19e61eSDong Jia Shi 	struct ccwchain *chain;
7920a19e61eSDong Jia Shi 	int len, idx, ret;
7930a19e61eSDong Jia Shi 
79471189f26SCornelia Huck 	/* this is an error in the caller */
79571189f26SCornelia Huck 	if (!cp->initialized)
79671189f26SCornelia Huck 		return -EINVAL;
79771189f26SCornelia Huck 
7980a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
7990a19e61eSDong Jia Shi 		len = chain->ch_len;
8000a19e61eSDong Jia Shi 		for (idx = 0; idx < len; idx++) {
8010a19e61eSDong Jia Shi 			ret = ccwchain_fetch_one(chain, idx, cp);
8020a19e61eSDong Jia Shi 			if (ret)
803d66a7355SHalil Pasic 				goto out_err;
8040a19e61eSDong Jia Shi 		}
8050a19e61eSDong Jia Shi 	}
8060a19e61eSDong Jia Shi 
8070a19e61eSDong Jia Shi 	return 0;
808d66a7355SHalil Pasic out_err:
809d66a7355SHalil Pasic 	/* Only cleanup the chain elements that were actually translated. */
810d66a7355SHalil Pasic 	chain->ch_len = idx;
811d66a7355SHalil Pasic 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
812d66a7355SHalil Pasic 		chain->ch_len = 0;
813d66a7355SHalil Pasic 	}
814d66a7355SHalil Pasic 	return ret;
8150a19e61eSDong Jia Shi }
8160a19e61eSDong Jia Shi 
8170a19e61eSDong Jia Shi /**
8180a19e61eSDong Jia Shi  * cp_get_orb() - get the orb of the channel program
8190a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8200a19e61eSDong Jia Shi  * @intparm: new intparm for the returned orb
8210a19e61eSDong Jia Shi  * @lpm: candidate value of the logical-path mask for the returned orb
8220a19e61eSDong Jia Shi  *
8230a19e61eSDong Jia Shi  * This function returns the address of the updated orb of the channel
8240a19e61eSDong Jia Shi  * program. Channel I/O device drivers could use this orb to issue a
8250a19e61eSDong Jia Shi  * ssch.
8260a19e61eSDong Jia Shi  */
8270a19e61eSDong Jia Shi union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
8280a19e61eSDong Jia Shi {
8290a19e61eSDong Jia Shi 	union orb *orb;
8300a19e61eSDong Jia Shi 	struct ccwchain *chain;
8310a19e61eSDong Jia Shi 	struct ccw1 *cpa;
8320a19e61eSDong Jia Shi 
83371189f26SCornelia Huck 	/* this is an error in the caller */
83471189f26SCornelia Huck 	if (!cp->initialized)
83571189f26SCornelia Huck 		return NULL;
83671189f26SCornelia Huck 
8370a19e61eSDong Jia Shi 	orb = &cp->orb;
8380a19e61eSDong Jia Shi 
8390a19e61eSDong Jia Shi 	orb->cmd.intparm = intparm;
8400a19e61eSDong Jia Shi 	orb->cmd.fmt = 1;
8410a19e61eSDong Jia Shi 	orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
8420a19e61eSDong Jia Shi 
8430a19e61eSDong Jia Shi 	if (orb->cmd.lpm == 0)
8440a19e61eSDong Jia Shi 		orb->cmd.lpm = lpm;
8450a19e61eSDong Jia Shi 
8460a19e61eSDong Jia Shi 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
8470a19e61eSDong Jia Shi 	cpa = chain->ch_ccw;
8480a19e61eSDong Jia Shi 	orb->cmd.cpa = (__u32) __pa(cpa);
8490a19e61eSDong Jia Shi 
8500a19e61eSDong Jia Shi 	return orb;
8510a19e61eSDong Jia Shi }
8520a19e61eSDong Jia Shi 
8530a19e61eSDong Jia Shi /**
8540a19e61eSDong Jia Shi  * cp_update_scsw() - update scsw for a channel program.
8550a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8560a19e61eSDong Jia Shi  * @scsw: I/O results of the channel program and also the target to be
8570a19e61eSDong Jia Shi  *        updated
8580a19e61eSDong Jia Shi  *
8590a19e61eSDong Jia Shi  * @scsw contains the I/O results of the channel program that pointed
8600a19e61eSDong Jia Shi  * to by @cp. However what @scsw->cpa stores is a host physical
8610a19e61eSDong Jia Shi  * address, which is meaningless for the guest, which is waiting for
8620a19e61eSDong Jia Shi  * the I/O results.
8630a19e61eSDong Jia Shi  *
8640a19e61eSDong Jia Shi  * This function updates @scsw->cpa to its coressponding guest physical
8650a19e61eSDong Jia Shi  * address.
8660a19e61eSDong Jia Shi  */
8670a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
8680a19e61eSDong Jia Shi {
8690a19e61eSDong Jia Shi 	struct ccwchain *chain;
8700a19e61eSDong Jia Shi 	u32 cpa = scsw->cmd.cpa;
8712904337fSEric Farman 	u32 ccw_head;
8720a19e61eSDong Jia Shi 
87371189f26SCornelia Huck 	if (!cp->initialized)
87471189f26SCornelia Huck 		return;
87571189f26SCornelia Huck 
8760a19e61eSDong Jia Shi 	/*
8770a19e61eSDong Jia Shi 	 * LATER:
8780a19e61eSDong Jia Shi 	 * For now, only update the cmd.cpa part. We may need to deal with
8790a19e61eSDong Jia Shi 	 * other portions of the schib as well, even if we don't return them
8800a19e61eSDong Jia Shi 	 * in the ioctl directly. Path status changes etc.
8810a19e61eSDong Jia Shi 	 */
8820a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8830a19e61eSDong Jia Shi 		ccw_head = (u32)(u64)chain->ch_ccw;
88415f0eb3dSEric Farman 		/*
88515f0eb3dSEric Farman 		 * On successful execution, cpa points just beyond the end
88615f0eb3dSEric Farman 		 * of the chain.
88715f0eb3dSEric Farman 		 */
88815f0eb3dSEric Farman 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
8890a19e61eSDong Jia Shi 			/*
8900a19e61eSDong Jia Shi 			 * (cpa - ccw_head) is the offset value of the host
8910a19e61eSDong Jia Shi 			 * physical ccw to its chain head.
8920a19e61eSDong Jia Shi 			 * Adding this value to the guest physical ccw chain
8930a19e61eSDong Jia Shi 			 * head gets us the guest cpa.
8940a19e61eSDong Jia Shi 			 */
8950a19e61eSDong Jia Shi 			cpa = chain->ch_iova + (cpa - ccw_head);
8960a19e61eSDong Jia Shi 			break;
8970a19e61eSDong Jia Shi 		}
8980a19e61eSDong Jia Shi 	}
8990a19e61eSDong Jia Shi 
9000a19e61eSDong Jia Shi 	scsw->cmd.cpa = cpa;
9010a19e61eSDong Jia Shi }
9020a19e61eSDong Jia Shi 
9030a19e61eSDong Jia Shi /**
9040a19e61eSDong Jia Shi  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
905364e3f90SSebastian Ott  * @cp: channel_program on which to perform the operation
9060a19e61eSDong Jia Shi  * @iova: the iova to check
9070a19e61eSDong Jia Shi  *
9080a19e61eSDong Jia Shi  * If the @iova is currently pinned for the ccw chain, return true;
9090a19e61eSDong Jia Shi  * else return false.
9100a19e61eSDong Jia Shi  */
9110a19e61eSDong Jia Shi bool cp_iova_pinned(struct channel_program *cp, u64 iova)
9120a19e61eSDong Jia Shi {
9130a19e61eSDong Jia Shi 	struct ccwchain *chain;
9140a19e61eSDong Jia Shi 	int i;
9150a19e61eSDong Jia Shi 
91671189f26SCornelia Huck 	if (!cp->initialized)
91771189f26SCornelia Huck 		return false;
91871189f26SCornelia Huck 
9190a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9200a19e61eSDong Jia Shi 		for (i = 0; i < chain->ch_len; i++)
921e7eaf91bSEric Farman 			if (pfn_array_iova_pinned(chain->ch_pa + i, iova))
9220a19e61eSDong Jia Shi 				return true;
9230a19e61eSDong Jia Shi 	}
9240a19e61eSDong Jia Shi 
9250a19e61eSDong Jia Shi 	return false;
9260a19e61eSDong Jia Shi }
927