xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_cp.c (revision 34a255e67615995f729254307a0581c143e03752)
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;
25*34a255e6SNicolin Chen 	/* Array that receives the pinned pages. */
26*34a255e6SNicolin 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,
71*34a255e6SNicolin 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 	}
77*34a255e6SNicolin Chen 	pa->pa_page = (struct page **)&pa->pa_iova[pa->pa_nr];
780a19e61eSDong Jia Shi 
7913314605SNicolin Chen 	pa->pa_iova[0] = iova;
80*34a255e6SNicolin 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;
83*34a255e6SNicolin 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
124e4f3f18bSEric Farman  * @mdev: the mediated 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,
147*34a255e6SNicolin 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 
17313314605SNicolin Chen static bool page_array_iova_pinned(struct page_array *pa, unsigned long iova)
1740a19e61eSDong Jia Shi {
1750a19e61eSDong Jia Shi 	int i;
1760a19e61eSDong Jia Shi 
177e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
17813314605SNicolin Chen 		if (pa->pa_iova[i] == iova)
1790a19e61eSDong Jia Shi 			return true;
1800a19e61eSDong Jia Shi 
1810a19e61eSDong Jia Shi 	return false;
1820a19e61eSDong Jia Shi }
18313314605SNicolin Chen /* Create the list of IDAL words for a page_array. */
18413314605SNicolin Chen static inline void page_array_idal_create_words(struct page_array *pa,
1850a19e61eSDong Jia Shi 						unsigned long *idaws)
1860a19e61eSDong Jia Shi {
187e7eaf91bSEric Farman 	int i;
1880a19e61eSDong Jia Shi 
1890a19e61eSDong Jia Shi 	/*
1900a19e61eSDong Jia Shi 	 * Idal words (execept the first one) rely on the memory being 4k
1910a19e61eSDong Jia Shi 	 * aligned. If a user virtual address is 4K aligned, then it's
1920a19e61eSDong Jia Shi 	 * corresponding kernel physical address will also be 4K aligned. Thus
1930a19e61eSDong Jia Shi 	 * there will be no problem here to simply use the phys to create an
1940a19e61eSDong Jia Shi 	 * idaw.
1950a19e61eSDong Jia Shi 	 */
196e7eaf91bSEric Farman 
197e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
198*34a255e6SNicolin Chen 		idaws[i] = page_to_phys(pa->pa_page[i]);
1998aabf0edSEric Farman 
2008aabf0edSEric Farman 	/* Adjust the first IDAW, since it may not start on a page boundary */
20113314605SNicolin Chen 	idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1);
2020a19e61eSDong Jia Shi }
2030a19e61eSDong Jia Shi 
204dbd66558SCornelia Huck static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
2057f8e89a8SEric Farman {
2067f8e89a8SEric Farman 	struct ccw0 ccw0;
2077f8e89a8SEric Farman 	struct ccw1 *pccw1 = source;
2087f8e89a8SEric Farman 	int i;
2097f8e89a8SEric Farman 
2107f8e89a8SEric Farman 	for (i = 0; i < len; i++) {
2117f8e89a8SEric Farman 		ccw0 = *(struct ccw0 *)pccw1;
2127f8e89a8SEric Farman 		if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
2137f8e89a8SEric Farman 			pccw1->cmd_code = CCW_CMD_TIC;
2147f8e89a8SEric Farman 			pccw1->flags = 0;
2157f8e89a8SEric Farman 			pccw1->count = 0;
2167f8e89a8SEric Farman 		} else {
2177f8e89a8SEric Farman 			pccw1->cmd_code = ccw0.cmd_code;
2187f8e89a8SEric Farman 			pccw1->flags = ccw0.flags;
2197f8e89a8SEric Farman 			pccw1->count = ccw0.count;
2207f8e89a8SEric Farman 		}
2217f8e89a8SEric Farman 		pccw1->cda = ccw0.cda;
2227f8e89a8SEric Farman 		pccw1++;
2237f8e89a8SEric Farman 	}
2247f8e89a8SEric Farman }
2250a19e61eSDong Jia Shi 
2260a19e61eSDong Jia Shi /*
2270a19e61eSDong Jia Shi  * Within the domain (@mdev), copy @n bytes from a guest physical
2280a19e61eSDong Jia Shi  * address (@iova) to a host physical address (@to).
2290a19e61eSDong Jia Shi  */
2300a587956SJason Gunthorpe static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova,
2310a19e61eSDong Jia Shi 			   unsigned long n)
2320a19e61eSDong Jia Shi {
23313314605SNicolin Chen 	struct page_array pa = {0};
2340a19e61eSDong Jia Shi 	int i, ret;
2350a19e61eSDong Jia Shi 	unsigned long l, m;
2360a19e61eSDong Jia Shi 
23713314605SNicolin Chen 	ret = page_array_alloc(&pa, iova, n);
238e4f3f18bSEric Farman 	if (ret < 0)
2390a19e61eSDong Jia Shi 		return ret;
2400a19e61eSDong Jia Shi 
24113314605SNicolin Chen 	ret = page_array_pin(&pa, vdev);
242e4f3f18bSEric Farman 	if (ret < 0) {
24313314605SNicolin Chen 		page_array_unpin_free(&pa, vdev);
244e4f3f18bSEric Farman 		return ret;
245e4f3f18bSEric Farman 	}
246e4f3f18bSEric Farman 
2470a19e61eSDong Jia Shi 	l = n;
2480a19e61eSDong Jia Shi 	for (i = 0; i < pa.pa_nr; i++) {
249*34a255e6SNicolin Chen 		void *from = kmap_local_page(pa.pa_page[i]);
250c2863febSNicolin Chen 
2510a19e61eSDong Jia Shi 		m = PAGE_SIZE;
2520a19e61eSDong Jia Shi 		if (i == 0) {
2530a19e61eSDong Jia Shi 			from += iova & (PAGE_SIZE - 1);
2540a19e61eSDong Jia Shi 			m -= iova & (PAGE_SIZE - 1);
2550a19e61eSDong Jia Shi 		}
2560a19e61eSDong Jia Shi 
2570a19e61eSDong Jia Shi 		m = min(l, m);
258c2863febSNicolin Chen 		memcpy(to + (n - l), from, m);
259c2863febSNicolin Chen 		kunmap_local(from);
2600a19e61eSDong Jia Shi 
2610a19e61eSDong Jia Shi 		l -= m;
2620a19e61eSDong Jia Shi 		if (l == 0)
2630a19e61eSDong Jia Shi 			break;
2640a19e61eSDong Jia Shi 	}
2650a19e61eSDong Jia Shi 
26613314605SNicolin Chen 	page_array_unpin_free(&pa, vdev);
2670a19e61eSDong Jia Shi 
2680a19e61eSDong Jia Shi 	return l;
2690a19e61eSDong Jia Shi }
2700a19e61eSDong Jia Shi 
2710a19e61eSDong Jia Shi /*
2720a19e61eSDong Jia Shi  * Helpers to operate ccwchain.
2730a19e61eSDong Jia Shi  */
2745d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
2755d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
2765d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
2775d87fbf7SEric Farman 
2780a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
2790a19e61eSDong Jia Shi 
2800a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
2810a19e61eSDong Jia Shi 
2820a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
2835d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
2840a19e61eSDong Jia Shi 
2850a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
2860a19e61eSDong Jia Shi 
28748bd0eeeSEric Farman /*
2885d87fbf7SEric Farman  * ccw_does_data_transfer()
2895d87fbf7SEric Farman  *
2905d87fbf7SEric Farman  * Determine whether a CCW will move any data, such that the guest pages
2915d87fbf7SEric Farman  * would need to be pinned before performing the I/O.
2925d87fbf7SEric Farman  *
2935d87fbf7SEric Farman  * Returns 1 if yes, 0 if no.
2945d87fbf7SEric Farman  */
2955d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw)
2965d87fbf7SEric Farman {
297453eac31SEric Farman 	/* If the count field is zero, then no data will be transferred */
298453eac31SEric Farman 	if (ccw->count == 0)
299453eac31SEric Farman 		return 0;
300453eac31SEric Farman 
3019b6e57e5SEric Farman 	/* If the command is a NOP, then no data will be transferred */
3029b6e57e5SEric Farman 	if (ccw_is_noop(ccw))
3039b6e57e5SEric Farman 		return 0;
3049b6e57e5SEric Farman 
3055d87fbf7SEric Farman 	/* If the skip flag is off, then data will be transferred */
3065d87fbf7SEric Farman 	if (!ccw_is_skip(ccw))
3075d87fbf7SEric Farman 		return 1;
3085d87fbf7SEric Farman 
3095d87fbf7SEric Farman 	/*
3105d87fbf7SEric Farman 	 * If the skip flag is on, it is only meaningful if the command
3115d87fbf7SEric Farman 	 * code is a read, read backward, sense, or sense ID.  In those
3125d87fbf7SEric Farman 	 * cases, no data will be transferred.
3135d87fbf7SEric Farman 	 */
3145d87fbf7SEric Farman 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
3155d87fbf7SEric Farman 		return 0;
3165d87fbf7SEric Farman 
3175d87fbf7SEric Farman 	if (ccw_is_sense(ccw))
3185d87fbf7SEric Farman 		return 0;
3195d87fbf7SEric Farman 
3205d87fbf7SEric Farman 	/* The skip flag is on, but it is ignored for this command code. */
3215d87fbf7SEric Farman 	return 1;
3225d87fbf7SEric Farman }
3235d87fbf7SEric Farman 
3245d87fbf7SEric Farman /*
32548bd0eeeSEric Farman  * is_cpa_within_range()
32648bd0eeeSEric Farman  *
32748bd0eeeSEric Farman  * @cpa: channel program address being questioned
32848bd0eeeSEric Farman  * @head: address of the beginning of a CCW chain
32948bd0eeeSEric Farman  * @len: number of CCWs within the chain
33048bd0eeeSEric Farman  *
33148bd0eeeSEric Farman  * Determine whether the address of a CCW (whether a new chain,
33248bd0eeeSEric Farman  * or the target of a TIC) falls within a range (including the end points).
33348bd0eeeSEric Farman  *
33448bd0eeeSEric Farman  * Returns 1 if yes, 0 if no.
33548bd0eeeSEric Farman  */
33648bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
33748bd0eeeSEric Farman {
33848bd0eeeSEric Farman 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
33948bd0eeeSEric Farman 
34048bd0eeeSEric Farman 	return (head <= cpa && cpa <= tail);
34148bd0eeeSEric Farman }
34248bd0eeeSEric Farman 
34348bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
34448bd0eeeSEric Farman {
34548bd0eeeSEric Farman 	if (!ccw_is_tic(ccw))
34648bd0eeeSEric Farman 		return 0;
34748bd0eeeSEric Farman 
34848bd0eeeSEric Farman 	return is_cpa_within_range(ccw->cda, head, len);
34948bd0eeeSEric Farman }
35048bd0eeeSEric Farman 
3510a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
3520a19e61eSDong Jia Shi {
3530a19e61eSDong Jia Shi 	struct ccwchain *chain;
3540a19e61eSDong Jia Shi 	void *data;
3550a19e61eSDong Jia Shi 	size_t size;
3560a19e61eSDong Jia Shi 
3570a19e61eSDong Jia Shi 	/* Make ccw address aligned to 8. */
3580a19e61eSDong Jia Shi 	size = ((sizeof(*chain) + 7L) & -8L) +
3590a19e61eSDong Jia Shi 		sizeof(*chain->ch_ccw) * len +
360e7eaf91bSEric Farman 		sizeof(*chain->ch_pa) * len;
3610a19e61eSDong Jia Shi 	chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
3620a19e61eSDong Jia Shi 	if (!chain)
3630a19e61eSDong Jia Shi 		return NULL;
3640a19e61eSDong Jia Shi 
3650a19e61eSDong Jia Shi 	data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
3660a19e61eSDong Jia Shi 	chain->ch_ccw = (struct ccw1 *)data;
3670a19e61eSDong Jia Shi 
3680a19e61eSDong Jia Shi 	data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
36913314605SNicolin Chen 	chain->ch_pa = (struct page_array *)data;
3700a19e61eSDong Jia Shi 
3710a19e61eSDong Jia Shi 	chain->ch_len = len;
3720a19e61eSDong Jia Shi 
3730a19e61eSDong Jia Shi 	list_add_tail(&chain->next, &cp->ccwchain_list);
3740a19e61eSDong Jia Shi 
3750a19e61eSDong Jia Shi 	return chain;
3760a19e61eSDong Jia Shi }
3770a19e61eSDong Jia Shi 
3780a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain)
3790a19e61eSDong Jia Shi {
3800a19e61eSDong Jia Shi 	list_del(&chain->next);
3810a19e61eSDong Jia Shi 	kfree(chain);
3820a19e61eSDong Jia Shi }
3830a19e61eSDong Jia Shi 
3840a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */
3850a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx)
3860a19e61eSDong Jia Shi {
3870a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
3880a19e61eSDong Jia Shi 
3899b6e57e5SEric Farman 	if (ccw_is_tic(ccw))
390408358b5SJason J. Herne 		return;
3910a19e61eSDong Jia Shi 
3920a19e61eSDong Jia Shi 	kfree((void *)(u64)ccw->cda);
3930a19e61eSDong Jia Shi }
3940a19e61eSDong Jia Shi 
3950a19e61eSDong Jia Shi /**
3960a19e61eSDong Jia Shi  * ccwchain_calc_length - calculate the length of the ccw chain.
3970a19e61eSDong Jia Shi  * @iova: guest physical address of the target ccw chain
3980a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
3990a19e61eSDong Jia Shi  *
4000a19e61eSDong Jia Shi  * This is the chain length not considering any TICs.
4010a19e61eSDong Jia Shi  * You need to do a new round for each TIC target.
4020a19e61eSDong Jia Shi  *
403fb9e7880SHalil Pasic  * The program is also validated for absence of not yet supported
404fb9e7880SHalil Pasic  * indirect data addressing scenarios.
405fb9e7880SHalil Pasic  *
4060a19e61eSDong Jia Shi  * Returns: the length of the ccw chain or -errno.
4070a19e61eSDong Jia Shi  */
4080a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
4090a19e61eSDong Jia Shi {
4101d897e47SEric Farman 	struct ccw1 *ccw = cp->guest_cp;
411ded563f3SEric Farman 	int cnt = 0;
4120a19e61eSDong Jia Shi 
4130a19e61eSDong Jia Shi 	do {
4140a19e61eSDong Jia Shi 		cnt++;
4150a19e61eSDong Jia Shi 
416fb9e7880SHalil Pasic 		/*
417fb9e7880SHalil Pasic 		 * As we don't want to fail direct addressing even if the
418fb9e7880SHalil Pasic 		 * orb specified one of the unsupported formats, we defer
419fb9e7880SHalil Pasic 		 * checking for IDAWs in unsupported formats to here.
420fb9e7880SHalil Pasic 		 */
4211d897e47SEric Farman 		if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw))
422fb9e7880SHalil Pasic 			return -EOPNOTSUPP;
423fb9e7880SHalil Pasic 
42448bd0eeeSEric Farman 		/*
42548bd0eeeSEric Farman 		 * We want to keep counting if the current CCW has the
42648bd0eeeSEric Farman 		 * command-chaining flag enabled, or if it is a TIC CCW
42748bd0eeeSEric Farman 		 * that loops back into the current chain.  The latter
42848bd0eeeSEric Farman 		 * is used for device orientation, where the CCW PRIOR to
42948bd0eeeSEric Farman 		 * the TIC can either jump to the TIC or a CCW immediately
43048bd0eeeSEric Farman 		 * after the TIC, depending on the results of its operation.
43148bd0eeeSEric Farman 		 */
43248bd0eeeSEric Farman 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
4330a19e61eSDong Jia Shi 			break;
4340a19e61eSDong Jia Shi 
4350a19e61eSDong Jia Shi 		ccw++;
4360a19e61eSDong Jia Shi 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
4370a19e61eSDong Jia Shi 
4380a19e61eSDong Jia Shi 	if (cnt == CCWCHAIN_LEN_MAX + 1)
4390a19e61eSDong Jia Shi 		cnt = -EINVAL;
4400a19e61eSDong Jia Shi 
4410a19e61eSDong Jia Shi 	return cnt;
4420a19e61eSDong Jia Shi }
4430a19e61eSDong Jia Shi 
4440a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
4450a19e61eSDong Jia Shi {
4460a19e61eSDong Jia Shi 	struct ccwchain *chain;
4472904337fSEric Farman 	u32 ccw_head;
4480a19e61eSDong Jia Shi 
4490a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
4500a19e61eSDong Jia Shi 		ccw_head = chain->ch_iova;
4512904337fSEric Farman 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
4520a19e61eSDong Jia Shi 			return 1;
4530a19e61eSDong Jia Shi 	}
4540a19e61eSDong Jia Shi 
4550a19e61eSDong Jia Shi 	return 0;
4560a19e61eSDong Jia Shi }
4570a19e61eSDong Jia Shi 
4580a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain,
4590a19e61eSDong Jia Shi 			     struct channel_program *cp);
4600a19e61eSDong Jia Shi 
461363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
4620a19e61eSDong Jia Shi {
4630a587956SJason Gunthorpe 	struct vfio_device *vdev =
4640a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
4650a19e61eSDong Jia Shi 	struct ccwchain *chain;
4668b515be5SFarhan Ali 	int len, ret;
4670a19e61eSDong Jia Shi 
468ded563f3SEric Farman 	/* Copy 2K (the most we support today) of possible CCWs */
4690a587956SJason Gunthorpe 	len = copy_from_iova(vdev, cp->guest_cp, cda,
4705223bee8SEric Farman 			     CCWCHAIN_LEN_MAX * sizeof(struct ccw1));
471ded563f3SEric Farman 	if (len)
472ded563f3SEric Farman 		return len;
473ded563f3SEric Farman 
4747f8e89a8SEric Farman 	/* Convert any Format-0 CCWs to Format-1 */
4757f8e89a8SEric Farman 	if (!cp->orb.cmd.fmt)
476c382cbc6SEric Farman 		convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
4777f8e89a8SEric Farman 
478ded563f3SEric Farman 	/* Count the CCWs in the current chain */
479363fe5f7SEric Farman 	len = ccwchain_calc_length(cda, cp);
4800a19e61eSDong Jia Shi 	if (len < 0)
4810a19e61eSDong Jia Shi 		return len;
4820a19e61eSDong Jia Shi 
4830a19e61eSDong Jia Shi 	/* Need alloc a new chain for this one. */
4840a19e61eSDong Jia Shi 	chain = ccwchain_alloc(cp, len);
4850a19e61eSDong Jia Shi 	if (!chain)
4860a19e61eSDong Jia Shi 		return -ENOMEM;
487363fe5f7SEric Farman 	chain->ch_iova = cda;
4880a19e61eSDong Jia Shi 
48962465902SEric Farman 	/* Copy the actual CCWs into the new chain */
49062465902SEric Farman 	memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
4910a19e61eSDong Jia Shi 
4920a19e61eSDong Jia Shi 	/* Loop for tics on this new chain. */
4938b515be5SFarhan Ali 	ret = ccwchain_loop_tic(chain, cp);
4948b515be5SFarhan Ali 
4958b515be5SFarhan Ali 	if (ret)
4968b515be5SFarhan Ali 		ccwchain_free(chain);
4978b515be5SFarhan Ali 
4988b515be5SFarhan Ali 	return ret;
4990a19e61eSDong Jia Shi }
5000a19e61eSDong Jia Shi 
5010a19e61eSDong Jia Shi /* Loop for TICs. */
5020a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
5030a19e61eSDong Jia Shi {
5040a19e61eSDong Jia Shi 	struct ccw1 *tic;
5050a19e61eSDong Jia Shi 	int i, ret;
5060a19e61eSDong Jia Shi 
5070a19e61eSDong Jia Shi 	for (i = 0; i < chain->ch_len; i++) {
5080a19e61eSDong Jia Shi 		tic = chain->ch_ccw + i;
5090a19e61eSDong Jia Shi 
5100a19e61eSDong Jia Shi 		if (!ccw_is_tic(tic))
5110a19e61eSDong Jia Shi 			continue;
5120a19e61eSDong Jia Shi 
513e64bd689SEric Farman 		/* May transfer to an existing chain. */
514e64bd689SEric Farman 		if (tic_target_chain_exists(tic, cp))
515e64bd689SEric Farman 			continue;
516e64bd689SEric Farman 
517363fe5f7SEric Farman 		/* Build a ccwchain for the next segment */
518363fe5f7SEric Farman 		ret = ccwchain_handle_ccw(tic->cda, cp);
5190a19e61eSDong Jia Shi 		if (ret)
5200a19e61eSDong Jia Shi 			return ret;
5210a19e61eSDong Jia Shi 	}
5220a19e61eSDong Jia Shi 
5230a19e61eSDong Jia Shi 	return 0;
5240a19e61eSDong Jia Shi }
5250a19e61eSDong Jia Shi 
5260a19e61eSDong Jia Shi static int ccwchain_fetch_tic(struct ccwchain *chain,
5270a19e61eSDong Jia Shi 			      int idx,
5280a19e61eSDong Jia Shi 			      struct channel_program *cp)
5290a19e61eSDong Jia Shi {
5300a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
5310a19e61eSDong Jia Shi 	struct ccwchain *iter;
5322904337fSEric Farman 	u32 ccw_head;
5330a19e61eSDong Jia Shi 
5340a19e61eSDong Jia Shi 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
5350a19e61eSDong Jia Shi 		ccw_head = iter->ch_iova;
5362904337fSEric Farman 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
537c389377cSJason J. Herne 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
5380a19e61eSDong Jia Shi 						     (ccw->cda - ccw_head));
5390a19e61eSDong Jia Shi 			return 0;
5400a19e61eSDong Jia Shi 		}
5410a19e61eSDong Jia Shi 	}
5420a19e61eSDong Jia Shi 
5430a19e61eSDong Jia Shi 	return -EFAULT;
5440a19e61eSDong Jia Shi }
5450a19e61eSDong Jia Shi 
5460a19e61eSDong Jia Shi static int ccwchain_fetch_direct(struct ccwchain *chain,
5470a19e61eSDong Jia Shi 				 int idx,
5480a19e61eSDong Jia Shi 				 struct channel_program *cp)
5490a19e61eSDong Jia Shi {
5500a587956SJason Gunthorpe 	struct vfio_device *vdev =
5510a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
5520a19e61eSDong Jia Shi 	struct ccw1 *ccw;
55313314605SNicolin Chen 	struct page_array *pa;
55401aa26c6SEric Farman 	u64 iova;
5550a19e61eSDong Jia Shi 	unsigned long *idaws;
5566238f921SDong Jia Shi 	int ret;
557453eac31SEric Farman 	int bytes = 1;
55801aa26c6SEric Farman 	int idaw_nr, idal_len;
55901aa26c6SEric Farman 	int i;
5600a19e61eSDong Jia Shi 
5610a19e61eSDong Jia Shi 	ccw = chain->ch_ccw + idx;
5620a19e61eSDong Jia Shi 
563e8573b39SEric Farman 	if (ccw->count)
564453eac31SEric Farman 		bytes = ccw->count;
565e8573b39SEric Farman 
566e8573b39SEric Farman 	/* Calculate size of IDAL */
56701aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
56801aa26c6SEric Farman 		/* Read first IDAW to see if it's 4K-aligned or not. */
56901aa26c6SEric Farman 		/* All subsequent IDAws will be 4K-aligned. */
5700a587956SJason Gunthorpe 		ret = copy_from_iova(vdev, &iova, ccw->cda, sizeof(iova));
57101aa26c6SEric Farman 		if (ret)
57201aa26c6SEric Farman 			return ret;
57301aa26c6SEric Farman 	} else {
57401aa26c6SEric Farman 		iova = ccw->cda;
57501aa26c6SEric Farman 	}
57601aa26c6SEric Farman 	idaw_nr = idal_nr_words((void *)iova, bytes);
57701aa26c6SEric Farman 	idal_len = idaw_nr * sizeof(*idaws);
578e8573b39SEric Farman 
579e8573b39SEric Farman 	/* Allocate an IDAL from host storage */
580e8573b39SEric Farman 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
581e8573b39SEric Farman 	if (!idaws) {
582e8573b39SEric Farman 		ret = -ENOMEM;
583e8573b39SEric Farman 		goto out_init;
5844cebc5d6SDong Jia Shi 	}
5854cebc5d6SDong Jia Shi 
5860a19e61eSDong Jia Shi 	/*
58713314605SNicolin Chen 	 * Allocate an array of pages to pin/translate.
58801aa26c6SEric Farman 	 * The number of pages is actually the count of the idaws
58901aa26c6SEric Farman 	 * required for the data transfer, since we only only support
59001aa26c6SEric Farman 	 * 4K IDAWs today.
5910a19e61eSDong Jia Shi 	 */
592e7eaf91bSEric Farman 	pa = chain->ch_pa + idx;
59313314605SNicolin Chen 	ret = page_array_alloc(pa, iova, bytes);
594e4f3f18bSEric Farman 	if (ret < 0)
595e8573b39SEric Farman 		goto out_free_idaws;
596e4f3f18bSEric Farman 
59701aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
59801aa26c6SEric Farman 		/* Copy guest IDAL into host IDAL */
5990a587956SJason Gunthorpe 		ret = copy_from_iova(vdev, idaws, ccw->cda, idal_len);
60001aa26c6SEric Farman 		if (ret)
60101aa26c6SEric Farman 			goto out_unpin;
60201aa26c6SEric Farman 
60301aa26c6SEric Farman 		/*
60413314605SNicolin Chen 		 * Copy guest IDAWs into page_array, in case the memory they
60501aa26c6SEric Farman 		 * occupy is not contiguous.
60601aa26c6SEric Farman 		 */
60701aa26c6SEric Farman 		for (i = 0; i < idaw_nr; i++)
60813314605SNicolin Chen 			pa->pa_iova[i] = idaws[i];
60901aa26c6SEric Farman 	} else {
61001aa26c6SEric Farman 		/*
61113314605SNicolin Chen 		 * No action is required here; the iova addresses in page_array
61213314605SNicolin Chen 		 * were initialized sequentially in page_array_alloc() beginning
61301aa26c6SEric Farman 		 * with the contents of ccw->cda.
61401aa26c6SEric Farman 		 */
61501aa26c6SEric Farman 	}
61601aa26c6SEric Farman 
6175d87fbf7SEric Farman 	if (ccw_does_data_transfer(ccw)) {
61813314605SNicolin Chen 		ret = page_array_pin(pa, vdev);
6196238f921SDong Jia Shi 		if (ret < 0)
620806212f9SEric Farman 			goto out_unpin;
6215d87fbf7SEric Farman 	} else {
622e7eaf91bSEric Farman 		pa->pa_nr = 0;
6235d87fbf7SEric Farman 	}
6240a19e61eSDong Jia Shi 
6250a19e61eSDong Jia Shi 	ccw->cda = (__u32) virt_to_phys(idaws);
6260a19e61eSDong Jia Shi 	ccw->flags |= CCW_FLAG_IDA;
6270a19e61eSDong Jia Shi 
62813314605SNicolin Chen 	/* Populate the IDAL with pinned/translated addresses from page */
62913314605SNicolin Chen 	page_array_idal_create_words(pa, idaws);
6300a19e61eSDong Jia Shi 
6310a19e61eSDong Jia Shi 	return 0;
6326238f921SDong Jia Shi 
6336238f921SDong Jia Shi out_unpin:
63413314605SNicolin Chen 	page_array_unpin_free(pa, vdev);
635e8573b39SEric Farman out_free_idaws:
636e8573b39SEric Farman 	kfree(idaws);
6376238f921SDong Jia Shi out_init:
6386238f921SDong Jia Shi 	ccw->cda = 0;
6396238f921SDong Jia Shi 	return ret;
6400a19e61eSDong Jia Shi }
6410a19e61eSDong Jia Shi 
6420a19e61eSDong Jia Shi /*
6430a19e61eSDong Jia Shi  * Fetch one ccw.
6440a19e61eSDong Jia Shi  * To reduce memory copy, we'll pin the cda page in memory,
6450a19e61eSDong Jia Shi  * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
6460a19e61eSDong Jia Shi  * direct ccws to idal ccws.
6470a19e61eSDong Jia Shi  */
6480a19e61eSDong Jia Shi static int ccwchain_fetch_one(struct ccwchain *chain,
6490a19e61eSDong Jia Shi 			      int idx,
6500a19e61eSDong Jia Shi 			      struct channel_program *cp)
6510a19e61eSDong Jia Shi {
6520a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
6530a19e61eSDong Jia Shi 
6540a19e61eSDong Jia Shi 	if (ccw_is_tic(ccw))
6550a19e61eSDong Jia Shi 		return ccwchain_fetch_tic(chain, idx, cp);
6560a19e61eSDong Jia Shi 
6570a19e61eSDong Jia Shi 	return ccwchain_fetch_direct(chain, idx, cp);
6580a19e61eSDong Jia Shi }
6590a19e61eSDong Jia Shi 
6600a19e61eSDong Jia Shi /**
6610a19e61eSDong Jia Shi  * cp_init() - allocate ccwchains for a channel program.
6620a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
6630a19e61eSDong Jia Shi  * @mdev: the mediated device to perform pin/unpin operations
6640a19e61eSDong Jia Shi  * @orb: control block for the channel program from the guest
6650a19e61eSDong Jia Shi  *
6660a19e61eSDong Jia Shi  * This creates one or more ccwchain(s), and copies the raw data of
6670a19e61eSDong Jia Shi  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
6680a19e61eSDong Jia Shi  *
6690a19e61eSDong Jia Shi  * Limitations:
670725b94d7SJared Rossi  * 1. Supports idal(c64) ccw chaining.
671725b94d7SJared Rossi  * 2. Supports 4k idaw.
6720a19e61eSDong Jia Shi  *
6730a19e61eSDong Jia Shi  * Returns:
6740a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
6750a19e61eSDong Jia Shi  */
6760a587956SJason Gunthorpe int cp_init(struct channel_program *cp, union orb *orb)
6770a19e61eSDong Jia Shi {
6780a587956SJason Gunthorpe 	struct vfio_device *vdev =
6790a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
680725b94d7SJared Rossi 	/* custom ratelimit used to avoid flood during guest IPL */
681725b94d7SJared Rossi 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
68299afcb05SEric Farman 	int ret;
6830a19e61eSDong Jia Shi 
684c6c82e0cSEric Farman 	/* this is an error in the caller */
685c6c82e0cSEric Farman 	if (cp->initialized)
686c6c82e0cSEric Farman 		return -EBUSY;
687c6c82e0cSEric Farman 
6880a19e61eSDong Jia Shi 	/*
689725b94d7SJared Rossi 	 * We only support prefetching the channel program. We assume all channel
690725b94d7SJared Rossi 	 * programs executed by supported guests likewise support prefetching.
691725b94d7SJared Rossi 	 * Executing a channel program that does not specify prefetching will
692725b94d7SJared Rossi 	 * typically not cause an error, but a warning is issued to help identify
693725b94d7SJared Rossi 	 * the problem if something does break.
6940a19e61eSDong Jia Shi 	 */
695725b94d7SJared Rossi 	if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
6960a587956SJason Gunthorpe 		dev_warn(
6970a587956SJason Gunthorpe 			vdev->dev,
6980a587956SJason Gunthorpe 			"Prefetching channel program even though prefetch not specified in ORB");
6990a19e61eSDong Jia Shi 
7000a19e61eSDong Jia Shi 	INIT_LIST_HEAD(&cp->ccwchain_list);
7010a19e61eSDong Jia Shi 	memcpy(&cp->orb, orb, sizeof(*orb));
7020a19e61eSDong Jia Shi 
70399afcb05SEric Farman 	/* Build a ccwchain for the first CCW segment */
70499afcb05SEric Farman 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
70599afcb05SEric Farman 
706c9f597a4SFarhan Ali 	if (!ret) {
707c9f597a4SFarhan Ali 		cp->initialized = true;
708c9f597a4SFarhan Ali 
709c9f597a4SFarhan Ali 		/* It is safe to force: if it was not set but idals used
710c9f597a4SFarhan Ali 		 * ccwchain_calc_length would have returned an error.
711fb9e7880SHalil Pasic 		 */
712fb9e7880SHalil Pasic 		cp->orb.cmd.c64 = 1;
713c9f597a4SFarhan Ali 	}
71471189f26SCornelia Huck 
7150a19e61eSDong Jia Shi 	return ret;
7160a19e61eSDong Jia Shi }
7170a19e61eSDong Jia Shi 
7180a19e61eSDong Jia Shi 
7190a19e61eSDong Jia Shi /**
7200a19e61eSDong Jia Shi  * cp_free() - free resources for channel program.
7210a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7220a19e61eSDong Jia Shi  *
7230a19e61eSDong Jia Shi  * This unpins the memory pages and frees the memory space occupied by
7240a19e61eSDong Jia Shi  * @cp, which must have been returned by a previous call to cp_init().
7250a19e61eSDong Jia Shi  * Otherwise, undefined behavior occurs.
7260a19e61eSDong Jia Shi  */
7270a19e61eSDong Jia Shi void cp_free(struct channel_program *cp)
7280a19e61eSDong Jia Shi {
7290a587956SJason Gunthorpe 	struct vfio_device *vdev =
7300a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
731812271b9SEric Farman 	struct ccwchain *chain, *temp;
732812271b9SEric Farman 	int i;
733812271b9SEric Farman 
734812271b9SEric Farman 	if (!cp->initialized)
735812271b9SEric Farman 		return;
736812271b9SEric Farman 
737812271b9SEric Farman 	cp->initialized = false;
738812271b9SEric Farman 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
739812271b9SEric Farman 		for (i = 0; i < chain->ch_len; i++) {
74013314605SNicolin Chen 			page_array_unpin_free(chain->ch_pa + i, vdev);
741812271b9SEric Farman 			ccwchain_cda_free(chain, i);
742812271b9SEric Farman 		}
743812271b9SEric Farman 		ccwchain_free(chain);
744812271b9SEric Farman 	}
7450a19e61eSDong Jia Shi }
7460a19e61eSDong Jia Shi 
7470a19e61eSDong Jia Shi /**
7480a19e61eSDong Jia Shi  * cp_prefetch() - translate a guest physical address channel program to
7490a19e61eSDong Jia Shi  *                 a real-device runnable channel program.
7500a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7510a19e61eSDong Jia Shi  *
7520a19e61eSDong Jia Shi  * This function translates the guest-physical-address channel program
7530a19e61eSDong Jia Shi  * and stores the result to ccwchain list. @cp must have been
7540a19e61eSDong Jia Shi  * initialized by a previous call with cp_init(). Otherwise, undefined
7550a19e61eSDong Jia Shi  * behavior occurs.
756d66a7355SHalil Pasic  * For each chain composing the channel program:
757d66a7355SHalil Pasic  * - On entry ch_len holds the count of CCWs to be translated.
758d66a7355SHalil Pasic  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
759d66a7355SHalil Pasic  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
7600a19e61eSDong Jia Shi  *
7610a19e61eSDong Jia Shi  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
7620a19e61eSDong Jia Shi  * as helpers to do ccw chain translation inside the kernel. Basically
7630a19e61eSDong Jia Shi  * they accept a channel program issued by a virtual machine, and
7640a19e61eSDong Jia Shi  * translate the channel program to a real-device runnable channel
7650a19e61eSDong Jia Shi  * program.
7660a19e61eSDong Jia Shi  *
7670a19e61eSDong Jia Shi  * These APIs will copy the ccws into kernel-space buffers, and update
7680a19e61eSDong Jia Shi  * the guest phsical addresses with their corresponding host physical
7690a19e61eSDong Jia Shi  * addresses.  Then channel I/O device drivers could issue the
7700a19e61eSDong Jia Shi  * translated channel program to real devices to perform an I/O
7710a19e61eSDong Jia Shi  * operation.
7720a19e61eSDong Jia Shi  *
7730a19e61eSDong Jia Shi  * These interfaces are designed to support translation only for
7740a19e61eSDong Jia Shi  * channel programs, which are generated and formatted by a
7750a19e61eSDong Jia Shi  * guest. Thus this will make it possible for things like VFIO to
7760a19e61eSDong Jia Shi  * leverage the interfaces to passthrough a channel I/O mediated
7770a19e61eSDong Jia Shi  * device in QEMU.
7780a19e61eSDong Jia Shi  *
7790a19e61eSDong Jia Shi  * We support direct ccw chaining by translating them to idal ccws.
7800a19e61eSDong Jia Shi  *
7810a19e61eSDong Jia Shi  * Returns:
7820a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
7830a19e61eSDong Jia Shi  */
7840a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp)
7850a19e61eSDong Jia Shi {
7860a19e61eSDong Jia Shi 	struct ccwchain *chain;
7870a19e61eSDong Jia Shi 	int len, idx, ret;
7880a19e61eSDong Jia Shi 
78971189f26SCornelia Huck 	/* this is an error in the caller */
79071189f26SCornelia Huck 	if (!cp->initialized)
79171189f26SCornelia Huck 		return -EINVAL;
79271189f26SCornelia Huck 
7930a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
7940a19e61eSDong Jia Shi 		len = chain->ch_len;
7950a19e61eSDong Jia Shi 		for (idx = 0; idx < len; idx++) {
7960a19e61eSDong Jia Shi 			ret = ccwchain_fetch_one(chain, idx, cp);
7970a19e61eSDong Jia Shi 			if (ret)
798d66a7355SHalil Pasic 				goto out_err;
7990a19e61eSDong Jia Shi 		}
8000a19e61eSDong Jia Shi 	}
8010a19e61eSDong Jia Shi 
8020a19e61eSDong Jia Shi 	return 0;
803d66a7355SHalil Pasic out_err:
804d66a7355SHalil Pasic 	/* Only cleanup the chain elements that were actually translated. */
805d66a7355SHalil Pasic 	chain->ch_len = idx;
806d66a7355SHalil Pasic 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
807d66a7355SHalil Pasic 		chain->ch_len = 0;
808d66a7355SHalil Pasic 	}
809d66a7355SHalil Pasic 	return ret;
8100a19e61eSDong Jia Shi }
8110a19e61eSDong Jia Shi 
8120a19e61eSDong Jia Shi /**
8130a19e61eSDong Jia Shi  * cp_get_orb() - get the orb of the channel program
8140a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8150a19e61eSDong Jia Shi  * @intparm: new intparm for the returned orb
8160a19e61eSDong Jia Shi  * @lpm: candidate value of the logical-path mask for the returned orb
8170a19e61eSDong Jia Shi  *
8180a19e61eSDong Jia Shi  * This function returns the address of the updated orb of the channel
8190a19e61eSDong Jia Shi  * program. Channel I/O device drivers could use this orb to issue a
8200a19e61eSDong Jia Shi  * ssch.
8210a19e61eSDong Jia Shi  */
8220a19e61eSDong Jia Shi union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
8230a19e61eSDong Jia Shi {
8240a19e61eSDong Jia Shi 	union orb *orb;
8250a19e61eSDong Jia Shi 	struct ccwchain *chain;
8260a19e61eSDong Jia Shi 	struct ccw1 *cpa;
8270a19e61eSDong Jia Shi 
82871189f26SCornelia Huck 	/* this is an error in the caller */
82971189f26SCornelia Huck 	if (!cp->initialized)
83071189f26SCornelia Huck 		return NULL;
83171189f26SCornelia Huck 
8320a19e61eSDong Jia Shi 	orb = &cp->orb;
8330a19e61eSDong Jia Shi 
8340a19e61eSDong Jia Shi 	orb->cmd.intparm = intparm;
8350a19e61eSDong Jia Shi 	orb->cmd.fmt = 1;
8360a19e61eSDong Jia Shi 	orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
8370a19e61eSDong Jia Shi 
8380a19e61eSDong Jia Shi 	if (orb->cmd.lpm == 0)
8390a19e61eSDong Jia Shi 		orb->cmd.lpm = lpm;
8400a19e61eSDong Jia Shi 
8410a19e61eSDong Jia Shi 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
8420a19e61eSDong Jia Shi 	cpa = chain->ch_ccw;
8430a19e61eSDong Jia Shi 	orb->cmd.cpa = (__u32) __pa(cpa);
8440a19e61eSDong Jia Shi 
8450a19e61eSDong Jia Shi 	return orb;
8460a19e61eSDong Jia Shi }
8470a19e61eSDong Jia Shi 
8480a19e61eSDong Jia Shi /**
8490a19e61eSDong Jia Shi  * cp_update_scsw() - update scsw for a channel program.
8500a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8510a19e61eSDong Jia Shi  * @scsw: I/O results of the channel program and also the target to be
8520a19e61eSDong Jia Shi  *        updated
8530a19e61eSDong Jia Shi  *
8540a19e61eSDong Jia Shi  * @scsw contains the I/O results of the channel program that pointed
8550a19e61eSDong Jia Shi  * to by @cp. However what @scsw->cpa stores is a host physical
8560a19e61eSDong Jia Shi  * address, which is meaningless for the guest, which is waiting for
8570a19e61eSDong Jia Shi  * the I/O results.
8580a19e61eSDong Jia Shi  *
8590a19e61eSDong Jia Shi  * This function updates @scsw->cpa to its coressponding guest physical
8600a19e61eSDong Jia Shi  * address.
8610a19e61eSDong Jia Shi  */
8620a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
8630a19e61eSDong Jia Shi {
8640a19e61eSDong Jia Shi 	struct ccwchain *chain;
8650a19e61eSDong Jia Shi 	u32 cpa = scsw->cmd.cpa;
8662904337fSEric Farman 	u32 ccw_head;
8670a19e61eSDong Jia Shi 
86871189f26SCornelia Huck 	if (!cp->initialized)
86971189f26SCornelia Huck 		return;
87071189f26SCornelia Huck 
8710a19e61eSDong Jia Shi 	/*
8720a19e61eSDong Jia Shi 	 * LATER:
8730a19e61eSDong Jia Shi 	 * For now, only update the cmd.cpa part. We may need to deal with
8740a19e61eSDong Jia Shi 	 * other portions of the schib as well, even if we don't return them
8750a19e61eSDong Jia Shi 	 * in the ioctl directly. Path status changes etc.
8760a19e61eSDong Jia Shi 	 */
8770a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8780a19e61eSDong Jia Shi 		ccw_head = (u32)(u64)chain->ch_ccw;
87915f0eb3dSEric Farman 		/*
88015f0eb3dSEric Farman 		 * On successful execution, cpa points just beyond the end
88115f0eb3dSEric Farman 		 * of the chain.
88215f0eb3dSEric Farman 		 */
88315f0eb3dSEric Farman 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
8840a19e61eSDong Jia Shi 			/*
8850a19e61eSDong Jia Shi 			 * (cpa - ccw_head) is the offset value of the host
8860a19e61eSDong Jia Shi 			 * physical ccw to its chain head.
8870a19e61eSDong Jia Shi 			 * Adding this value to the guest physical ccw chain
8880a19e61eSDong Jia Shi 			 * head gets us the guest cpa.
8890a19e61eSDong Jia Shi 			 */
8900a19e61eSDong Jia Shi 			cpa = chain->ch_iova + (cpa - ccw_head);
8910a19e61eSDong Jia Shi 			break;
8920a19e61eSDong Jia Shi 		}
8930a19e61eSDong Jia Shi 	}
8940a19e61eSDong Jia Shi 
8950a19e61eSDong Jia Shi 	scsw->cmd.cpa = cpa;
8960a19e61eSDong Jia Shi }
8970a19e61eSDong Jia Shi 
8980a19e61eSDong Jia Shi /**
8990a19e61eSDong Jia Shi  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
900364e3f90SSebastian Ott  * @cp: channel_program on which to perform the operation
9010a19e61eSDong Jia Shi  * @iova: the iova to check
9020a19e61eSDong Jia Shi  *
9030a19e61eSDong Jia Shi  * If the @iova is currently pinned for the ccw chain, return true;
9040a19e61eSDong Jia Shi  * else return false.
9050a19e61eSDong Jia Shi  */
9060a19e61eSDong Jia Shi bool cp_iova_pinned(struct channel_program *cp, u64 iova)
9070a19e61eSDong Jia Shi {
9080a19e61eSDong Jia Shi 	struct ccwchain *chain;
9090a19e61eSDong Jia Shi 	int i;
9100a19e61eSDong Jia Shi 
91171189f26SCornelia Huck 	if (!cp->initialized)
91271189f26SCornelia Huck 		return false;
91371189f26SCornelia Huck 
9140a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9150a19e61eSDong Jia Shi 		for (i = 0; i < chain->ch_len; i++)
91613314605SNicolin Chen 			if (page_array_iova_pinned(chain->ch_pa + i, iova))
9170a19e61eSDong Jia Shi 				return true;
9180a19e61eSDong Jia Shi 	}
9190a19e61eSDong Jia Shi 
9200a19e61eSDong Jia Shi 	return false;
9210a19e61eSDong Jia Shi }
922