xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_cp.c (revision 6a6dc14ac84733cf5864a7cf9f5b3e43f6a79be8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20a19e61eSDong Jia Shi /*
30a19e61eSDong Jia Shi  * channel program interfaces
40a19e61eSDong Jia Shi  *
50a19e61eSDong Jia Shi  * Copyright IBM Corp. 2017
60a19e61eSDong Jia Shi  *
70a19e61eSDong Jia Shi  * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
80a19e61eSDong Jia Shi  *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
90a19e61eSDong Jia Shi  */
100a19e61eSDong Jia Shi 
11725b94d7SJared Rossi #include <linux/ratelimit.h>
120a19e61eSDong Jia Shi #include <linux/mm.h>
130a19e61eSDong Jia Shi #include <linux/slab.h>
14c2863febSNicolin Chen #include <linux/highmem.h>
150a19e61eSDong Jia Shi #include <linux/iommu.h>
160a19e61eSDong Jia Shi #include <linux/vfio.h>
170a19e61eSDong Jia Shi #include <asm/idals.h>
180a19e61eSDong Jia Shi 
190a19e61eSDong Jia Shi #include "vfio_ccw_cp.h"
200a587956SJason Gunthorpe #include "vfio_ccw_private.h"
210a19e61eSDong Jia Shi 
2213314605SNicolin Chen struct page_array {
2313314605SNicolin Chen 	/* Array that stores pages need to pin. */
2413314605SNicolin Chen 	dma_addr_t		*pa_iova;
2534a255e6SNicolin Chen 	/* Array that receives the pinned pages. */
2634a255e6SNicolin Chen 	struct page		**pa_page;
275c1cfb1cSDong Jia Shi 	/* Number of pages pinned from @pa_iova. */
280a19e61eSDong Jia Shi 	int			pa_nr;
290a19e61eSDong Jia Shi };
300a19e61eSDong Jia Shi 
310a19e61eSDong Jia Shi struct ccwchain {
320a19e61eSDong Jia Shi 	struct list_head	next;
330a19e61eSDong Jia Shi 	struct ccw1		*ch_ccw;
340a19e61eSDong Jia Shi 	/* Guest physical address of the current chain. */
350a19e61eSDong Jia Shi 	u64			ch_iova;
360a19e61eSDong Jia Shi 	/* Count of the valid ccws in chain. */
370a19e61eSDong Jia Shi 	int			ch_len;
380a19e61eSDong Jia Shi 	/* Pinned PAGEs for the original data. */
3913314605SNicolin Chen 	struct page_array	*ch_pa;
400a19e61eSDong Jia Shi };
410a19e61eSDong Jia Shi 
420a19e61eSDong Jia Shi /*
4313314605SNicolin Chen  * page_array_alloc() - alloc memory for page array
4413314605SNicolin Chen  * @pa: page_array on which to perform the operation
4562a97a56SEric Farman  * @len: number of pages that should be pinned from @iova
460a19e61eSDong Jia Shi  *
4713314605SNicolin Chen  * Attempt to allocate memory for page array.
480a19e61eSDong Jia Shi  *
4913314605SNicolin Chen  * Usage of page_array:
5013314605SNicolin Chen  * We expect (pa_nr == 0) and (pa_iova == NULL), any field in
515c1cfb1cSDong Jia Shi  * this structure will be filled in by this function.
520a19e61eSDong Jia Shi  *
530a19e61eSDong Jia Shi  * Returns:
5413314605SNicolin Chen  *         0 if page array is allocated
5513314605SNicolin Chen  *   -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova is not NULL
56e4f3f18bSEric Farman  *   -ENOMEM if alloc failed
570a19e61eSDong Jia Shi  */
5861783394SEric Farman static int page_array_alloc(struct page_array *pa, unsigned int len)
590a19e61eSDong Jia Shi {
6013314605SNicolin Chen 	if (pa->pa_nr || pa->pa_iova)
610a19e61eSDong Jia Shi 		return -EINVAL;
620a19e61eSDong Jia Shi 
6362a97a56SEric Farman 	if (len == 0)
640a19e61eSDong Jia Shi 		return -EINVAL;
650a19e61eSDong Jia Shi 
6662a97a56SEric Farman 	pa->pa_nr = len;
6762a97a56SEric Farman 
6862a97a56SEric Farman 	pa->pa_iova = kcalloc(len, sizeof(*pa->pa_iova), GFP_KERNEL);
6962a97a56SEric Farman 	if (!pa->pa_iova)
7062a97a56SEric Farman 		return -ENOMEM;
7162a97a56SEric Farman 
7262a97a56SEric Farman 	pa->pa_page = kcalloc(len, sizeof(*pa->pa_page), GFP_KERNEL);
7362a97a56SEric Farman 	if (!pa->pa_page) {
7462a97a56SEric Farman 		kfree(pa->pa_iova);
750a19e61eSDong Jia Shi 		return -ENOMEM;
76c1ab6926SFarhan Ali 	}
770a19e61eSDong Jia Shi 
78e4f3f18bSEric Farman 	return 0;
79e4f3f18bSEric Farman }
80e4f3f18bSEric Farman 
81e4f3f18bSEric Farman /*
8213314605SNicolin Chen  * page_array_unpin() - Unpin user pages in memory
8313314605SNicolin Chen  * @pa: page_array on which to perform the operation
84cfedb3d5SNicolin Chen  * @vdev: the vfio device to perform the operation
85cfedb3d5SNicolin Chen  * @pa_nr: number of user pages to unpin
86cfedb3d5SNicolin Chen  *
87cfedb3d5SNicolin Chen  * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0,
88cfedb3d5SNicolin Chen  * otherwise only clear pa->pa_nr
89cfedb3d5SNicolin Chen  */
9013314605SNicolin Chen static void page_array_unpin(struct page_array *pa,
91cfedb3d5SNicolin Chen 			     struct vfio_device *vdev, int pa_nr)
92cfedb3d5SNicolin Chen {
93cfedb3d5SNicolin Chen 	int unpinned = 0, npage = 1;
94cfedb3d5SNicolin Chen 
95cfedb3d5SNicolin Chen 	while (unpinned < pa_nr) {
9613314605SNicolin Chen 		dma_addr_t *first = &pa->pa_iova[unpinned];
9713314605SNicolin Chen 		dma_addr_t *last = &first[npage];
98cfedb3d5SNicolin Chen 
99cfedb3d5SNicolin Chen 		if (unpinned + npage < pa_nr &&
10013314605SNicolin Chen 		    *first + npage * PAGE_SIZE == *last) {
101cfedb3d5SNicolin Chen 			npage++;
102cfedb3d5SNicolin Chen 			continue;
103cfedb3d5SNicolin Chen 		}
104cfedb3d5SNicolin Chen 
10513314605SNicolin Chen 		vfio_unpin_pages(vdev, *first, npage);
106cfedb3d5SNicolin Chen 		unpinned += npage;
107cfedb3d5SNicolin Chen 		npage = 1;
108cfedb3d5SNicolin Chen 	}
109cfedb3d5SNicolin Chen 
110cfedb3d5SNicolin Chen 	pa->pa_nr = 0;
111cfedb3d5SNicolin Chen }
112cfedb3d5SNicolin Chen 
113cfedb3d5SNicolin Chen /*
11413314605SNicolin Chen  * page_array_pin() - Pin user pages in memory
11513314605SNicolin Chen  * @pa: page_array on which to perform the operation
1168a54e238SEric Farman  * @vdev: the vfio device to perform pin operations
117e4f3f18bSEric Farman  *
118e4f3f18bSEric Farman  * Returns number of pages pinned upon success.
119e4f3f18bSEric Farman  * If the pin request partially succeeds, or fails completely,
120e4f3f18bSEric Farman  * all pages are left unpinned and a negative error value is returned.
121e4f3f18bSEric Farman  */
12213314605SNicolin Chen static int page_array_pin(struct page_array *pa, struct vfio_device *vdev)
123e4f3f18bSEric Farman {
124cfedb3d5SNicolin Chen 	int pinned = 0, npage = 1;
125e4f3f18bSEric Farman 	int ret = 0;
126e4f3f18bSEric Farman 
127cfedb3d5SNicolin Chen 	while (pinned < pa->pa_nr) {
12813314605SNicolin Chen 		dma_addr_t *first = &pa->pa_iova[pinned];
12913314605SNicolin Chen 		dma_addr_t *last = &first[npage];
1305c1cfb1cSDong Jia Shi 
131cfedb3d5SNicolin Chen 		if (pinned + npage < pa->pa_nr &&
13213314605SNicolin Chen 		    *first + npage * PAGE_SIZE == *last) {
133cfedb3d5SNicolin Chen 			npage++;
134cfedb3d5SNicolin Chen 			continue;
135cfedb3d5SNicolin Chen 		}
136cfedb3d5SNicolin Chen 
13713314605SNicolin Chen 		ret = vfio_pin_pages(vdev, *first, npage,
138cfedb3d5SNicolin Chen 				     IOMMU_READ | IOMMU_WRITE,
13934a255e6SNicolin Chen 				     &pa->pa_page[pinned]);
1405c1cfb1cSDong Jia Shi 		if (ret < 0) {
1415c1cfb1cSDong Jia Shi 			goto err_out;
142cfedb3d5SNicolin Chen 		} else if (ret > 0 && ret != npage) {
143cfedb3d5SNicolin Chen 			pinned += ret;
1440a19e61eSDong Jia Shi 			ret = -EINVAL;
1455c1cfb1cSDong Jia Shi 			goto err_out;
1465c1cfb1cSDong Jia Shi 		}
147cfedb3d5SNicolin Chen 		pinned += npage;
148cfedb3d5SNicolin Chen 		npage = 1;
149cfedb3d5SNicolin Chen 	}
1500a19e61eSDong Jia Shi 
1510a19e61eSDong Jia Shi 	return ret;
1525c1cfb1cSDong Jia Shi 
1535c1cfb1cSDong Jia Shi err_out:
15413314605SNicolin Chen 	page_array_unpin(pa, vdev, pinned);
1555c1cfb1cSDong Jia Shi 	return ret;
1565c1cfb1cSDong Jia Shi }
1575c1cfb1cSDong Jia Shi 
1585c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */
15913314605SNicolin Chen static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev)
1605c1cfb1cSDong Jia Shi {
16113314605SNicolin Chen 	page_array_unpin(pa, vdev, pa->pa_nr);
16262a97a56SEric Farman 	kfree(pa->pa_page);
16313314605SNicolin Chen 	kfree(pa->pa_iova);
1640a19e61eSDong Jia Shi }
1650a19e61eSDong Jia Shi 
1665a4fe7c4SEric Farman static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length)
1670a19e61eSDong Jia Shi {
1685a4fe7c4SEric Farman 	u64 iova_pfn_start = iova >> PAGE_SHIFT;
1695a4fe7c4SEric Farman 	u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT;
1705a4fe7c4SEric Farman 	u64 pfn;
1710a19e61eSDong Jia Shi 	int i;
1720a19e61eSDong Jia Shi 
1735a4fe7c4SEric Farman 	for (i = 0; i < pa->pa_nr; i++) {
1745a4fe7c4SEric Farman 		pfn = pa->pa_iova[i] >> PAGE_SHIFT;
1755a4fe7c4SEric Farman 		if (pfn >= iova_pfn_start && pfn <= iova_pfn_end)
1760a19e61eSDong Jia Shi 			return true;
1775a4fe7c4SEric Farman 	}
1780a19e61eSDong Jia Shi 
1790a19e61eSDong Jia Shi 	return false;
1800a19e61eSDong Jia Shi }
18113314605SNicolin Chen /* Create the list of IDAL words for a page_array. */
18213314605SNicolin Chen static inline void page_array_idal_create_words(struct page_array *pa,
1830a19e61eSDong Jia Shi 						unsigned long *idaws)
1840a19e61eSDong Jia Shi {
185e7eaf91bSEric Farman 	int i;
1860a19e61eSDong Jia Shi 
1870a19e61eSDong Jia Shi 	/*
1880a19e61eSDong Jia Shi 	 * Idal words (execept the first one) rely on the memory being 4k
1890a19e61eSDong Jia Shi 	 * aligned. If a user virtual address is 4K aligned, then it's
1900a19e61eSDong Jia Shi 	 * corresponding kernel physical address will also be 4K aligned. Thus
1910a19e61eSDong Jia Shi 	 * there will be no problem here to simply use the phys to create an
1920a19e61eSDong Jia Shi 	 * idaw.
1930a19e61eSDong Jia Shi 	 */
194e7eaf91bSEric Farman 
195e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
19634a255e6SNicolin Chen 		idaws[i] = page_to_phys(pa->pa_page[i]);
1978aabf0edSEric Farman 
1988aabf0edSEric Farman 	/* Adjust the first IDAW, since it may not start on a page boundary */
19913314605SNicolin Chen 	idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1);
2000a19e61eSDong Jia Shi }
2010a19e61eSDong Jia Shi 
202dbd66558SCornelia Huck static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
2037f8e89a8SEric Farman {
2047f8e89a8SEric Farman 	struct ccw0 ccw0;
2057f8e89a8SEric Farman 	struct ccw1 *pccw1 = source;
2067f8e89a8SEric Farman 	int i;
2077f8e89a8SEric Farman 
2087f8e89a8SEric Farman 	for (i = 0; i < len; i++) {
2097f8e89a8SEric Farman 		ccw0 = *(struct ccw0 *)pccw1;
2107f8e89a8SEric Farman 		if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
2117f8e89a8SEric Farman 			pccw1->cmd_code = CCW_CMD_TIC;
2127f8e89a8SEric Farman 			pccw1->flags = 0;
2137f8e89a8SEric Farman 			pccw1->count = 0;
2147f8e89a8SEric Farman 		} else {
2157f8e89a8SEric Farman 			pccw1->cmd_code = ccw0.cmd_code;
2167f8e89a8SEric Farman 			pccw1->flags = ccw0.flags;
2177f8e89a8SEric Farman 			pccw1->count = ccw0.count;
2187f8e89a8SEric Farman 		}
2197f8e89a8SEric Farman 		pccw1->cda = ccw0.cda;
2207f8e89a8SEric Farman 		pccw1++;
2217f8e89a8SEric Farman 	}
2227f8e89a8SEric Farman }
2230a19e61eSDong Jia Shi 
2240a19e61eSDong Jia Shi /*
2250a19e61eSDong Jia Shi  * Helpers to operate ccwchain.
2260a19e61eSDong Jia Shi  */
2275d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
2285d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
2295d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
2305d87fbf7SEric Farman 
2310a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
2320a19e61eSDong Jia Shi 
2330a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
2340a19e61eSDong Jia Shi 
2350a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
2365d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
2370a19e61eSDong Jia Shi 
2380a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
2390a19e61eSDong Jia Shi 
24048bd0eeeSEric Farman /*
2415d87fbf7SEric Farman  * ccw_does_data_transfer()
2425d87fbf7SEric Farman  *
2435d87fbf7SEric Farman  * Determine whether a CCW will move any data, such that the guest pages
2445d87fbf7SEric Farman  * would need to be pinned before performing the I/O.
2455d87fbf7SEric Farman  *
2465d87fbf7SEric Farman  * Returns 1 if yes, 0 if no.
2475d87fbf7SEric Farman  */
2485d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw)
2495d87fbf7SEric Farman {
250453eac31SEric Farman 	/* If the count field is zero, then no data will be transferred */
251453eac31SEric Farman 	if (ccw->count == 0)
252453eac31SEric Farman 		return 0;
253453eac31SEric Farman 
2549b6e57e5SEric Farman 	/* If the command is a NOP, then no data will be transferred */
2559b6e57e5SEric Farman 	if (ccw_is_noop(ccw))
2569b6e57e5SEric Farman 		return 0;
2579b6e57e5SEric Farman 
2585d87fbf7SEric Farman 	/* If the skip flag is off, then data will be transferred */
2595d87fbf7SEric Farman 	if (!ccw_is_skip(ccw))
2605d87fbf7SEric Farman 		return 1;
2615d87fbf7SEric Farman 
2625d87fbf7SEric Farman 	/*
2635d87fbf7SEric Farman 	 * If the skip flag is on, it is only meaningful if the command
2645d87fbf7SEric Farman 	 * code is a read, read backward, sense, or sense ID.  In those
2655d87fbf7SEric Farman 	 * cases, no data will be transferred.
2665d87fbf7SEric Farman 	 */
2675d87fbf7SEric Farman 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
2685d87fbf7SEric Farman 		return 0;
2695d87fbf7SEric Farman 
2705d87fbf7SEric Farman 	if (ccw_is_sense(ccw))
2715d87fbf7SEric Farman 		return 0;
2725d87fbf7SEric Farman 
2735d87fbf7SEric Farman 	/* The skip flag is on, but it is ignored for this command code. */
2745d87fbf7SEric Farman 	return 1;
2755d87fbf7SEric Farman }
2765d87fbf7SEric Farman 
2775d87fbf7SEric Farman /*
27848bd0eeeSEric Farman  * is_cpa_within_range()
27948bd0eeeSEric Farman  *
28048bd0eeeSEric Farman  * @cpa: channel program address being questioned
28148bd0eeeSEric Farman  * @head: address of the beginning of a CCW chain
28248bd0eeeSEric Farman  * @len: number of CCWs within the chain
28348bd0eeeSEric Farman  *
28448bd0eeeSEric Farman  * Determine whether the address of a CCW (whether a new chain,
28548bd0eeeSEric Farman  * or the target of a TIC) falls within a range (including the end points).
28648bd0eeeSEric Farman  *
28748bd0eeeSEric Farman  * Returns 1 if yes, 0 if no.
28848bd0eeeSEric Farman  */
28948bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
29048bd0eeeSEric Farman {
29148bd0eeeSEric Farman 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
29248bd0eeeSEric Farman 
29348bd0eeeSEric Farman 	return (head <= cpa && cpa <= tail);
29448bd0eeeSEric Farman }
29548bd0eeeSEric Farman 
29648bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
29748bd0eeeSEric Farman {
29848bd0eeeSEric Farman 	if (!ccw_is_tic(ccw))
29948bd0eeeSEric Farman 		return 0;
30048bd0eeeSEric Farman 
30148bd0eeeSEric Farman 	return is_cpa_within_range(ccw->cda, head, len);
30248bd0eeeSEric Farman }
30348bd0eeeSEric Farman 
3040a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
3050a19e61eSDong Jia Shi {
3060a19e61eSDong Jia Shi 	struct ccwchain *chain;
3070a19e61eSDong Jia Shi 
3084b946d65SEric Farman 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
3090a19e61eSDong Jia Shi 	if (!chain)
3100a19e61eSDong Jia Shi 		return NULL;
3110a19e61eSDong Jia Shi 
3124b946d65SEric Farman 	chain->ch_ccw = kcalloc(len, sizeof(*chain->ch_ccw), GFP_DMA | GFP_KERNEL);
3134b946d65SEric Farman 	if (!chain->ch_ccw)
3144b946d65SEric Farman 		goto out_err;
3150a19e61eSDong Jia Shi 
3164b946d65SEric Farman 	chain->ch_pa = kcalloc(len, sizeof(*chain->ch_pa), GFP_KERNEL);
3174b946d65SEric Farman 	if (!chain->ch_pa)
3184b946d65SEric Farman 		goto out_err;
3190a19e61eSDong Jia Shi 
3200a19e61eSDong Jia Shi 	list_add_tail(&chain->next, &cp->ccwchain_list);
3210a19e61eSDong Jia Shi 
3220a19e61eSDong Jia Shi 	return chain;
3234b946d65SEric Farman 
3244b946d65SEric Farman out_err:
3254b946d65SEric Farman 	kfree(chain->ch_ccw);
3264b946d65SEric Farman 	kfree(chain);
3274b946d65SEric Farman 	return NULL;
3280a19e61eSDong Jia Shi }
3290a19e61eSDong Jia Shi 
3300a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain)
3310a19e61eSDong Jia Shi {
3320a19e61eSDong Jia Shi 	list_del(&chain->next);
3334b946d65SEric Farman 	kfree(chain->ch_pa);
3344b946d65SEric Farman 	kfree(chain->ch_ccw);
3350a19e61eSDong Jia Shi 	kfree(chain);
3360a19e61eSDong Jia Shi }
3370a19e61eSDong Jia Shi 
3380a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */
3390a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx)
3400a19e61eSDong Jia Shi {
3414b946d65SEric Farman 	struct ccw1 *ccw = &chain->ch_ccw[idx];
3420a19e61eSDong Jia Shi 
3439b6e57e5SEric Farman 	if (ccw_is_tic(ccw))
344408358b5SJason J. Herne 		return;
3450a19e61eSDong Jia Shi 
3465de2322dSEric Farman 	kfree(phys_to_virt(ccw->cda));
3470a19e61eSDong Jia Shi }
3480a19e61eSDong Jia Shi 
3490a19e61eSDong Jia Shi /**
3500a19e61eSDong Jia Shi  * ccwchain_calc_length - calculate the length of the ccw chain.
3510a19e61eSDong Jia Shi  * @iova: guest physical address of the target ccw chain
3520a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
3530a19e61eSDong Jia Shi  *
3540a19e61eSDong Jia Shi  * This is the chain length not considering any TICs.
3550a19e61eSDong Jia Shi  * You need to do a new round for each TIC target.
3560a19e61eSDong Jia Shi  *
357fb9e7880SHalil Pasic  * The program is also validated for absence of not yet supported
358fb9e7880SHalil Pasic  * indirect data addressing scenarios.
359fb9e7880SHalil Pasic  *
3600a19e61eSDong Jia Shi  * Returns: the length of the ccw chain or -errno.
3610a19e61eSDong Jia Shi  */
3620a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
3630a19e61eSDong Jia Shi {
3641d897e47SEric Farman 	struct ccw1 *ccw = cp->guest_cp;
365ded563f3SEric Farman 	int cnt = 0;
3660a19e61eSDong Jia Shi 
3670a19e61eSDong Jia Shi 	do {
3680a19e61eSDong Jia Shi 		cnt++;
3690a19e61eSDong Jia Shi 
370fb9e7880SHalil Pasic 		/*
371fb9e7880SHalil Pasic 		 * As we don't want to fail direct addressing even if the
372fb9e7880SHalil Pasic 		 * orb specified one of the unsupported formats, we defer
373fb9e7880SHalil Pasic 		 * checking for IDAWs in unsupported formats to here.
374fb9e7880SHalil Pasic 		 */
3751d897e47SEric Farman 		if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw))
376fb9e7880SHalil Pasic 			return -EOPNOTSUPP;
377fb9e7880SHalil Pasic 
37848bd0eeeSEric Farman 		/*
37948bd0eeeSEric Farman 		 * We want to keep counting if the current CCW has the
38048bd0eeeSEric Farman 		 * command-chaining flag enabled, or if it is a TIC CCW
38148bd0eeeSEric Farman 		 * that loops back into the current chain.  The latter
38248bd0eeeSEric Farman 		 * is used for device orientation, where the CCW PRIOR to
38348bd0eeeSEric Farman 		 * the TIC can either jump to the TIC or a CCW immediately
38448bd0eeeSEric Farman 		 * after the TIC, depending on the results of its operation.
38548bd0eeeSEric Farman 		 */
38648bd0eeeSEric Farman 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
3870a19e61eSDong Jia Shi 			break;
3880a19e61eSDong Jia Shi 
3890a19e61eSDong Jia Shi 		ccw++;
3900a19e61eSDong Jia Shi 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
3910a19e61eSDong Jia Shi 
3920a19e61eSDong Jia Shi 	if (cnt == CCWCHAIN_LEN_MAX + 1)
3930a19e61eSDong Jia Shi 		cnt = -EINVAL;
3940a19e61eSDong Jia Shi 
3950a19e61eSDong Jia Shi 	return cnt;
3960a19e61eSDong Jia Shi }
3970a19e61eSDong Jia Shi 
3980a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
3990a19e61eSDong Jia Shi {
4000a19e61eSDong Jia Shi 	struct ccwchain *chain;
4012904337fSEric Farman 	u32 ccw_head;
4020a19e61eSDong Jia Shi 
4030a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
4040a19e61eSDong Jia Shi 		ccw_head = chain->ch_iova;
4052904337fSEric Farman 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
4060a19e61eSDong Jia Shi 			return 1;
4070a19e61eSDong Jia Shi 	}
4080a19e61eSDong Jia Shi 
4090a19e61eSDong Jia Shi 	return 0;
4100a19e61eSDong Jia Shi }
4110a19e61eSDong Jia Shi 
4120a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain,
4130a19e61eSDong Jia Shi 			     struct channel_program *cp);
4140a19e61eSDong Jia Shi 
415363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
4160a19e61eSDong Jia Shi {
4170a587956SJason Gunthorpe 	struct vfio_device *vdev =
4180a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
4190a19e61eSDong Jia Shi 	struct ccwchain *chain;
4208b515be5SFarhan Ali 	int len, ret;
4210a19e61eSDong Jia Shi 
422ded563f3SEric Farman 	/* Copy 2K (the most we support today) of possible CCWs */
423c5e8083fSEric Farman 	ret = vfio_dma_rw(vdev, cda, cp->guest_cp, CCWCHAIN_LEN_MAX * sizeof(struct ccw1), false);
424c5e8083fSEric Farman 	if (ret)
425c5e8083fSEric Farman 		return ret;
426ded563f3SEric Farman 
4277f8e89a8SEric Farman 	/* Convert any Format-0 CCWs to Format-1 */
4287f8e89a8SEric Farman 	if (!cp->orb.cmd.fmt)
429c382cbc6SEric Farman 		convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
4307f8e89a8SEric Farman 
431ded563f3SEric Farman 	/* Count the CCWs in the current chain */
432363fe5f7SEric Farman 	len = ccwchain_calc_length(cda, cp);
4330a19e61eSDong Jia Shi 	if (len < 0)
4340a19e61eSDong Jia Shi 		return len;
4350a19e61eSDong Jia Shi 
4360a19e61eSDong Jia Shi 	/* Need alloc a new chain for this one. */
4370a19e61eSDong Jia Shi 	chain = ccwchain_alloc(cp, len);
4380a19e61eSDong Jia Shi 	if (!chain)
4390a19e61eSDong Jia Shi 		return -ENOMEM;
4404b946d65SEric Farman 
4414b946d65SEric Farman 	chain->ch_len = len;
442363fe5f7SEric Farman 	chain->ch_iova = cda;
4430a19e61eSDong Jia Shi 
44462465902SEric Farman 	/* Copy the actual CCWs into the new chain */
44562465902SEric Farman 	memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
4460a19e61eSDong Jia Shi 
4470a19e61eSDong Jia Shi 	/* Loop for tics on this new chain. */
4488b515be5SFarhan Ali 	ret = ccwchain_loop_tic(chain, cp);
4498b515be5SFarhan Ali 
4508b515be5SFarhan Ali 	if (ret)
4518b515be5SFarhan Ali 		ccwchain_free(chain);
4528b515be5SFarhan Ali 
4538b515be5SFarhan Ali 	return ret;
4540a19e61eSDong Jia Shi }
4550a19e61eSDong Jia Shi 
4560a19e61eSDong Jia Shi /* Loop for TICs. */
4570a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
4580a19e61eSDong Jia Shi {
4590a19e61eSDong Jia Shi 	struct ccw1 *tic;
4600a19e61eSDong Jia Shi 	int i, ret;
4610a19e61eSDong Jia Shi 
4620a19e61eSDong Jia Shi 	for (i = 0; i < chain->ch_len; i++) {
4634b946d65SEric Farman 		tic = &chain->ch_ccw[i];
4640a19e61eSDong Jia Shi 
4650a19e61eSDong Jia Shi 		if (!ccw_is_tic(tic))
4660a19e61eSDong Jia Shi 			continue;
4670a19e61eSDong Jia Shi 
468e64bd689SEric Farman 		/* May transfer to an existing chain. */
469e64bd689SEric Farman 		if (tic_target_chain_exists(tic, cp))
470e64bd689SEric Farman 			continue;
471e64bd689SEric Farman 
472363fe5f7SEric Farman 		/* Build a ccwchain for the next segment */
473363fe5f7SEric Farman 		ret = ccwchain_handle_ccw(tic->cda, cp);
4740a19e61eSDong Jia Shi 		if (ret)
4750a19e61eSDong Jia Shi 			return ret;
4760a19e61eSDong Jia Shi 	}
4770a19e61eSDong Jia Shi 
4780a19e61eSDong Jia Shi 	return 0;
4790a19e61eSDong Jia Shi }
4800a19e61eSDong Jia Shi 
481a4c60404SEric Farman static int ccwchain_fetch_tic(struct ccw1 *ccw,
4820a19e61eSDong Jia Shi 			      struct channel_program *cp)
4830a19e61eSDong Jia Shi {
4840a19e61eSDong Jia Shi 	struct ccwchain *iter;
4852904337fSEric Farman 	u32 ccw_head;
4860a19e61eSDong Jia Shi 
4870a19e61eSDong Jia Shi 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
4880a19e61eSDong Jia Shi 		ccw_head = iter->ch_iova;
4892904337fSEric Farman 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
490c389377cSJason J. Herne 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
4910a19e61eSDong Jia Shi 						     (ccw->cda - ccw_head));
4920a19e61eSDong Jia Shi 			return 0;
4930a19e61eSDong Jia Shi 		}
4940a19e61eSDong Jia Shi 	}
4950a19e61eSDong Jia Shi 
4960a19e61eSDong Jia Shi 	return -EFAULT;
4970a19e61eSDong Jia Shi }
4980a19e61eSDong Jia Shi 
499b21f9cb1SEric Farman /*
500b21f9cb1SEric Farman  * ccw_count_idaws() - Calculate the number of IDAWs needed to transfer
501b21f9cb1SEric Farman  * a specified amount of data
502b21f9cb1SEric Farman  *
503b21f9cb1SEric Farman  * @ccw: The Channel Command Word being translated
504b21f9cb1SEric Farman  * @cp: Channel Program being processed
505*6a6dc14aSEric Farman  *
506*6a6dc14aSEric Farman  * The ORB is examined, since it specifies what IDAWs could actually be
507*6a6dc14aSEric Farman  * used by any CCW in the channel program, regardless of whether or not
508*6a6dc14aSEric Farman  * the CCW actually does. An ORB that does not specify Format-2-IDAW
509*6a6dc14aSEric Farman  * Control could still contain a CCW with an IDAL, which would be
510*6a6dc14aSEric Farman  * Format-1 and thus only move 2K with each IDAW. Thus all CCWs within
511*6a6dc14aSEric Farman  * the channel program must follow the same size requirements.
512b21f9cb1SEric Farman  */
513b21f9cb1SEric Farman static int ccw_count_idaws(struct ccw1 *ccw,
5140a19e61eSDong Jia Shi 			   struct channel_program *cp)
5150a19e61eSDong Jia Shi {
5160a587956SJason Gunthorpe 	struct vfio_device *vdev =
5170a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
51801aa26c6SEric Farman 	u64 iova;
519667e5dbaSEric Farman 	int size = cp->orb.cmd.c64 ? sizeof(u64) : sizeof(u32);
5206238f921SDong Jia Shi 	int ret;
521453eac31SEric Farman 	int bytes = 1;
5220a19e61eSDong Jia Shi 
523e8573b39SEric Farman 	if (ccw->count)
524453eac31SEric Farman 		bytes = ccw->count;
525e8573b39SEric Farman 
52601aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
527667e5dbaSEric Farman 		/* Read first IDAW to check its starting address. */
528667e5dbaSEric Farman 		/* All subsequent IDAWs will be 2K- or 4K-aligned. */
529667e5dbaSEric Farman 		ret = vfio_dma_rw(vdev, ccw->cda, &iova, size, false);
53001aa26c6SEric Farman 		if (ret)
53101aa26c6SEric Farman 			return ret;
532667e5dbaSEric Farman 
533667e5dbaSEric Farman 		/*
534667e5dbaSEric Farman 		 * Format-1 IDAWs only occupy the first 32 bits,
535667e5dbaSEric Farman 		 * and bit 0 is always off.
536667e5dbaSEric Farman 		 */
537667e5dbaSEric Farman 		if (!cp->orb.cmd.c64)
538667e5dbaSEric Farman 			iova = iova >> 32;
53901aa26c6SEric Farman 	} else {
54001aa26c6SEric Farman 		iova = ccw->cda;
54101aa26c6SEric Farman 	}
542b21f9cb1SEric Farman 
543*6a6dc14aSEric Farman 	/* Format-1 IDAWs operate on 2K each */
544*6a6dc14aSEric Farman 	if (!cp->orb.cmd.c64)
545*6a6dc14aSEric Farman 		return idal_2k_nr_words((void *)iova, bytes);
546*6a6dc14aSEric Farman 
547*6a6dc14aSEric Farman 	/* Using the 2K variant of Format-2 IDAWs? */
548*6a6dc14aSEric Farman 	if (cp->orb.cmd.i2k)
549*6a6dc14aSEric Farman 		return idal_2k_nr_words((void *)iova, bytes);
550*6a6dc14aSEric Farman 
551*6a6dc14aSEric Farman 	/* The 'usual' case is 4K Format-2 IDAWs */
552b21f9cb1SEric Farman 	return idal_nr_words((void *)iova, bytes);
553b21f9cb1SEric Farman }
554b21f9cb1SEric Farman 
555b21f9cb1SEric Farman static int ccwchain_fetch_ccw(struct ccw1 *ccw,
556b21f9cb1SEric Farman 			      struct page_array *pa,
557b21f9cb1SEric Farman 			      struct channel_program *cp)
558b21f9cb1SEric Farman {
559b21f9cb1SEric Farman 	struct vfio_device *vdev =
560b21f9cb1SEric Farman 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
561b21f9cb1SEric Farman 	unsigned long *idaws;
562b21f9cb1SEric Farman 	int ret;
563b21f9cb1SEric Farman 	int idaw_nr, idal_len;
564b21f9cb1SEric Farman 	int i;
565b21f9cb1SEric Farman 
566b21f9cb1SEric Farman 	/* Calculate size of IDAL */
567b21f9cb1SEric Farman 	idaw_nr = ccw_count_idaws(ccw, cp);
568b21f9cb1SEric Farman 	if (idaw_nr < 0)
569b21f9cb1SEric Farman 		return idaw_nr;
570b21f9cb1SEric Farman 
57101aa26c6SEric Farman 	idal_len = idaw_nr * sizeof(*idaws);
572e8573b39SEric Farman 
573e8573b39SEric Farman 	/* Allocate an IDAL from host storage */
574e8573b39SEric Farman 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
575e8573b39SEric Farman 	if (!idaws) {
576e8573b39SEric Farman 		ret = -ENOMEM;
577e8573b39SEric Farman 		goto out_init;
5784cebc5d6SDong Jia Shi 	}
5794cebc5d6SDong Jia Shi 
5800a19e61eSDong Jia Shi 	/*
58113314605SNicolin Chen 	 * Allocate an array of pages to pin/translate.
58201aa26c6SEric Farman 	 * The number of pages is actually the count of the idaws
58301aa26c6SEric Farman 	 * required for the data transfer, since we only only support
58401aa26c6SEric Farman 	 * 4K IDAWs today.
5850a19e61eSDong Jia Shi 	 */
58661783394SEric Farman 	ret = page_array_alloc(pa, idaw_nr);
587e4f3f18bSEric Farman 	if (ret < 0)
588e8573b39SEric Farman 		goto out_free_idaws;
589e4f3f18bSEric Farman 
59001aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
59101aa26c6SEric Farman 		/* Copy guest IDAL into host IDAL */
592c5e8083fSEric Farman 		ret = vfio_dma_rw(vdev, ccw->cda, idaws, idal_len, false);
59301aa26c6SEric Farman 		if (ret)
59401aa26c6SEric Farman 			goto out_unpin;
59501aa26c6SEric Farman 
59601aa26c6SEric Farman 		/*
59713314605SNicolin Chen 		 * Copy guest IDAWs into page_array, in case the memory they
59801aa26c6SEric Farman 		 * occupy is not contiguous.
59901aa26c6SEric Farman 		 */
60001aa26c6SEric Farman 		for (i = 0; i < idaw_nr; i++)
60113314605SNicolin Chen 			pa->pa_iova[i] = idaws[i];
60201aa26c6SEric Farman 	} else {
603b21f9cb1SEric Farman 		pa->pa_iova[0] = ccw->cda;
60461783394SEric Farman 		for (i = 1; i < pa->pa_nr; i++)
60561783394SEric Farman 			pa->pa_iova[i] = pa->pa_iova[i - 1] + PAGE_SIZE;
60601aa26c6SEric Farman 	}
60701aa26c6SEric Farman 
6085d87fbf7SEric Farman 	if (ccw_does_data_transfer(ccw)) {
60913314605SNicolin Chen 		ret = page_array_pin(pa, vdev);
6106238f921SDong Jia Shi 		if (ret < 0)
611806212f9SEric Farman 			goto out_unpin;
6125d87fbf7SEric Farman 	} else {
613e7eaf91bSEric Farman 		pa->pa_nr = 0;
6145d87fbf7SEric Farman 	}
6150a19e61eSDong Jia Shi 
6160a19e61eSDong Jia Shi 	ccw->cda = (__u32) virt_to_phys(idaws);
6170a19e61eSDong Jia Shi 	ccw->flags |= CCW_FLAG_IDA;
6180a19e61eSDong Jia Shi 
61913314605SNicolin Chen 	/* Populate the IDAL with pinned/translated addresses from page */
62013314605SNicolin Chen 	page_array_idal_create_words(pa, idaws);
6210a19e61eSDong Jia Shi 
6220a19e61eSDong Jia Shi 	return 0;
6236238f921SDong Jia Shi 
6246238f921SDong Jia Shi out_unpin:
62513314605SNicolin Chen 	page_array_unpin_free(pa, vdev);
626e8573b39SEric Farman out_free_idaws:
627e8573b39SEric Farman 	kfree(idaws);
6286238f921SDong Jia Shi out_init:
6296238f921SDong Jia Shi 	ccw->cda = 0;
6306238f921SDong Jia Shi 	return ret;
6310a19e61eSDong Jia Shi }
6320a19e61eSDong Jia Shi 
6330a19e61eSDong Jia Shi /*
6340a19e61eSDong Jia Shi  * Fetch one ccw.
6350a19e61eSDong Jia Shi  * To reduce memory copy, we'll pin the cda page in memory,
6360a19e61eSDong Jia Shi  * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
6370a19e61eSDong Jia Shi  * direct ccws to idal ccws.
6380a19e61eSDong Jia Shi  */
639a4c60404SEric Farman static int ccwchain_fetch_one(struct ccw1 *ccw,
640a4c60404SEric Farman 			      struct page_array *pa,
6410a19e61eSDong Jia Shi 			      struct channel_program *cp)
642a4c60404SEric Farman 
6430a19e61eSDong Jia Shi {
6440a19e61eSDong Jia Shi 	if (ccw_is_tic(ccw))
645a4c60404SEric Farman 		return ccwchain_fetch_tic(ccw, cp);
6460a19e61eSDong Jia Shi 
647a4c60404SEric Farman 	return ccwchain_fetch_ccw(ccw, pa, cp);
6480a19e61eSDong Jia Shi }
6490a19e61eSDong Jia Shi 
6500a19e61eSDong Jia Shi /**
6510a19e61eSDong Jia Shi  * cp_init() - allocate ccwchains for a channel program.
6520a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
6530a19e61eSDong Jia Shi  * @orb: control block for the channel program from the guest
6540a19e61eSDong Jia Shi  *
6550a19e61eSDong Jia Shi  * This creates one or more ccwchain(s), and copies the raw data of
6560a19e61eSDong Jia Shi  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
6570a19e61eSDong Jia Shi  *
6580a19e61eSDong Jia Shi  * Limitations:
659725b94d7SJared Rossi  * 1. Supports idal(c64) ccw chaining.
660725b94d7SJared Rossi  * 2. Supports 4k idaw.
6610a19e61eSDong Jia Shi  *
6620a19e61eSDong Jia Shi  * Returns:
6630a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
6640a19e61eSDong Jia Shi  */
6650a587956SJason Gunthorpe int cp_init(struct channel_program *cp, union orb *orb)
6660a19e61eSDong Jia Shi {
6670a587956SJason Gunthorpe 	struct vfio_device *vdev =
6680a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
669725b94d7SJared Rossi 	/* custom ratelimit used to avoid flood during guest IPL */
670725b94d7SJared Rossi 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
67199afcb05SEric Farman 	int ret;
6720a19e61eSDong Jia Shi 
673c6c82e0cSEric Farman 	/* this is an error in the caller */
674c6c82e0cSEric Farman 	if (cp->initialized)
675c6c82e0cSEric Farman 		return -EBUSY;
676c6c82e0cSEric Farman 
6770a19e61eSDong Jia Shi 	/*
678725b94d7SJared Rossi 	 * We only support prefetching the channel program. We assume all channel
679725b94d7SJared Rossi 	 * programs executed by supported guests likewise support prefetching.
680725b94d7SJared Rossi 	 * Executing a channel program that does not specify prefetching will
681725b94d7SJared Rossi 	 * typically not cause an error, but a warning is issued to help identify
682725b94d7SJared Rossi 	 * the problem if something does break.
6830a19e61eSDong Jia Shi 	 */
684725b94d7SJared Rossi 	if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
6850a587956SJason Gunthorpe 		dev_warn(
6860a587956SJason Gunthorpe 			vdev->dev,
6870a587956SJason Gunthorpe 			"Prefetching channel program even though prefetch not specified in ORB");
6880a19e61eSDong Jia Shi 
6890a19e61eSDong Jia Shi 	INIT_LIST_HEAD(&cp->ccwchain_list);
6900a19e61eSDong Jia Shi 	memcpy(&cp->orb, orb, sizeof(*orb));
6910a19e61eSDong Jia Shi 
69299afcb05SEric Farman 	/* Build a ccwchain for the first CCW segment */
69399afcb05SEric Farman 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
69499afcb05SEric Farman 
695254cb663SEric Farman 	if (!ret)
696c9f597a4SFarhan Ali 		cp->initialized = true;
697c9f597a4SFarhan Ali 
6980a19e61eSDong Jia Shi 	return ret;
6990a19e61eSDong Jia Shi }
7000a19e61eSDong Jia Shi 
7010a19e61eSDong Jia Shi 
7020a19e61eSDong Jia Shi /**
7030a19e61eSDong Jia Shi  * cp_free() - free resources for channel program.
7040a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7050a19e61eSDong Jia Shi  *
7060a19e61eSDong Jia Shi  * This unpins the memory pages and frees the memory space occupied by
7070a19e61eSDong Jia Shi  * @cp, which must have been returned by a previous call to cp_init().
7080a19e61eSDong Jia Shi  * Otherwise, undefined behavior occurs.
7090a19e61eSDong Jia Shi  */
7100a19e61eSDong Jia Shi void cp_free(struct channel_program *cp)
7110a19e61eSDong Jia Shi {
7120a587956SJason Gunthorpe 	struct vfio_device *vdev =
7130a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
714812271b9SEric Farman 	struct ccwchain *chain, *temp;
715812271b9SEric Farman 	int i;
716812271b9SEric Farman 
717812271b9SEric Farman 	if (!cp->initialized)
718812271b9SEric Farman 		return;
719812271b9SEric Farman 
720812271b9SEric Farman 	cp->initialized = false;
721812271b9SEric Farman 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
722812271b9SEric Farman 		for (i = 0; i < chain->ch_len; i++) {
7234b946d65SEric Farman 			page_array_unpin_free(&chain->ch_pa[i], vdev);
724812271b9SEric Farman 			ccwchain_cda_free(chain, i);
725812271b9SEric Farman 		}
726812271b9SEric Farman 		ccwchain_free(chain);
727812271b9SEric Farman 	}
7280a19e61eSDong Jia Shi }
7290a19e61eSDong Jia Shi 
7300a19e61eSDong Jia Shi /**
7310a19e61eSDong Jia Shi  * cp_prefetch() - translate a guest physical address channel program to
7320a19e61eSDong Jia Shi  *                 a real-device runnable channel program.
7330a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7340a19e61eSDong Jia Shi  *
7350a19e61eSDong Jia Shi  * This function translates the guest-physical-address channel program
7360a19e61eSDong Jia Shi  * and stores the result to ccwchain list. @cp must have been
7370a19e61eSDong Jia Shi  * initialized by a previous call with cp_init(). Otherwise, undefined
7380a19e61eSDong Jia Shi  * behavior occurs.
739d66a7355SHalil Pasic  * For each chain composing the channel program:
740d66a7355SHalil Pasic  * - On entry ch_len holds the count of CCWs to be translated.
741d66a7355SHalil Pasic  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
742d66a7355SHalil Pasic  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
7430a19e61eSDong Jia Shi  *
7440a19e61eSDong Jia Shi  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
7450a19e61eSDong Jia Shi  * as helpers to do ccw chain translation inside the kernel. Basically
7460a19e61eSDong Jia Shi  * they accept a channel program issued by a virtual machine, and
7470a19e61eSDong Jia Shi  * translate the channel program to a real-device runnable channel
7480a19e61eSDong Jia Shi  * program.
7490a19e61eSDong Jia Shi  *
7500a19e61eSDong Jia Shi  * These APIs will copy the ccws into kernel-space buffers, and update
7510a19e61eSDong Jia Shi  * the guest phsical addresses with their corresponding host physical
7520a19e61eSDong Jia Shi  * addresses.  Then channel I/O device drivers could issue the
7530a19e61eSDong Jia Shi  * translated channel program to real devices to perform an I/O
7540a19e61eSDong Jia Shi  * operation.
7550a19e61eSDong Jia Shi  *
7560a19e61eSDong Jia Shi  * These interfaces are designed to support translation only for
7570a19e61eSDong Jia Shi  * channel programs, which are generated and formatted by a
7580a19e61eSDong Jia Shi  * guest. Thus this will make it possible for things like VFIO to
7590a19e61eSDong Jia Shi  * leverage the interfaces to passthrough a channel I/O mediated
7600a19e61eSDong Jia Shi  * device in QEMU.
7610a19e61eSDong Jia Shi  *
7620a19e61eSDong Jia Shi  * We support direct ccw chaining by translating them to idal ccws.
7630a19e61eSDong Jia Shi  *
7640a19e61eSDong Jia Shi  * Returns:
7650a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
7660a19e61eSDong Jia Shi  */
7670a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp)
7680a19e61eSDong Jia Shi {
7690a19e61eSDong Jia Shi 	struct ccwchain *chain;
770a4c60404SEric Farman 	struct ccw1 *ccw;
771a4c60404SEric Farman 	struct page_array *pa;
7720a19e61eSDong Jia Shi 	int len, idx, ret;
7730a19e61eSDong Jia Shi 
77471189f26SCornelia Huck 	/* this is an error in the caller */
77571189f26SCornelia Huck 	if (!cp->initialized)
77671189f26SCornelia Huck 		return -EINVAL;
77771189f26SCornelia Huck 
7780a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
7790a19e61eSDong Jia Shi 		len = chain->ch_len;
7800a19e61eSDong Jia Shi 		for (idx = 0; idx < len; idx++) {
7814b946d65SEric Farman 			ccw = &chain->ch_ccw[idx];
7824b946d65SEric Farman 			pa = &chain->ch_pa[idx];
783a4c60404SEric Farman 
784a4c60404SEric Farman 			ret = ccwchain_fetch_one(ccw, pa, cp);
7850a19e61eSDong Jia Shi 			if (ret)
786d66a7355SHalil Pasic 				goto out_err;
7870a19e61eSDong Jia Shi 		}
7880a19e61eSDong Jia Shi 	}
7890a19e61eSDong Jia Shi 
7900a19e61eSDong Jia Shi 	return 0;
791d66a7355SHalil Pasic out_err:
792d66a7355SHalil Pasic 	/* Only cleanup the chain elements that were actually translated. */
793d66a7355SHalil Pasic 	chain->ch_len = idx;
794d66a7355SHalil Pasic 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
795d66a7355SHalil Pasic 		chain->ch_len = 0;
796d66a7355SHalil Pasic 	}
797d66a7355SHalil Pasic 	return ret;
7980a19e61eSDong Jia Shi }
7990a19e61eSDong Jia Shi 
8000a19e61eSDong Jia Shi /**
8010a19e61eSDong Jia Shi  * cp_get_orb() - get the orb of the channel program
8020a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8039fbed59fSEric Farman  * @sch: subchannel the operation will be performed against
8040a19e61eSDong Jia Shi  *
8050a19e61eSDong Jia Shi  * This function returns the address of the updated orb of the channel
8060a19e61eSDong Jia Shi  * program. Channel I/O device drivers could use this orb to issue a
8070a19e61eSDong Jia Shi  * ssch.
8080a19e61eSDong Jia Shi  */
8099fbed59fSEric Farman union orb *cp_get_orb(struct channel_program *cp, struct subchannel *sch)
8100a19e61eSDong Jia Shi {
8110a19e61eSDong Jia Shi 	union orb *orb;
8120a19e61eSDong Jia Shi 	struct ccwchain *chain;
8130a19e61eSDong Jia Shi 	struct ccw1 *cpa;
8140a19e61eSDong Jia Shi 
81571189f26SCornelia Huck 	/* this is an error in the caller */
81671189f26SCornelia Huck 	if (!cp->initialized)
81771189f26SCornelia Huck 		return NULL;
81871189f26SCornelia Huck 
8190a19e61eSDong Jia Shi 	orb = &cp->orb;
8200a19e61eSDong Jia Shi 
8219fbed59fSEric Farman 	orb->cmd.intparm = (u32)virt_to_phys(sch);
8220a19e61eSDong Jia Shi 	orb->cmd.fmt = 1;
8230a19e61eSDong Jia Shi 
824254cb663SEric Farman 	/*
825254cb663SEric Farman 	 * Everything built by vfio-ccw is a Format-2 IDAL.
826254cb663SEric Farman 	 */
827254cb663SEric Farman 	orb->cmd.c64 = 1;
828254cb663SEric Farman 
8290a19e61eSDong Jia Shi 	if (orb->cmd.lpm == 0)
8309fbed59fSEric Farman 		orb->cmd.lpm = sch->lpm;
8310a19e61eSDong Jia Shi 
8320a19e61eSDong Jia Shi 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
8330a19e61eSDong Jia Shi 	cpa = chain->ch_ccw;
8345de2322dSEric Farman 	orb->cmd.cpa = (__u32)virt_to_phys(cpa);
8350a19e61eSDong Jia Shi 
8360a19e61eSDong Jia Shi 	return orb;
8370a19e61eSDong Jia Shi }
8380a19e61eSDong Jia Shi 
8390a19e61eSDong Jia Shi /**
8400a19e61eSDong Jia Shi  * cp_update_scsw() - update scsw for a channel program.
8410a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8420a19e61eSDong Jia Shi  * @scsw: I/O results of the channel program and also the target to be
8430a19e61eSDong Jia Shi  *        updated
8440a19e61eSDong Jia Shi  *
8450a19e61eSDong Jia Shi  * @scsw contains the I/O results of the channel program that pointed
8460a19e61eSDong Jia Shi  * to by @cp. However what @scsw->cpa stores is a host physical
8470a19e61eSDong Jia Shi  * address, which is meaningless for the guest, which is waiting for
8480a19e61eSDong Jia Shi  * the I/O results.
8490a19e61eSDong Jia Shi  *
8500a19e61eSDong Jia Shi  * This function updates @scsw->cpa to its coressponding guest physical
8510a19e61eSDong Jia Shi  * address.
8520a19e61eSDong Jia Shi  */
8530a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
8540a19e61eSDong Jia Shi {
8550a19e61eSDong Jia Shi 	struct ccwchain *chain;
8560a19e61eSDong Jia Shi 	u32 cpa = scsw->cmd.cpa;
8572904337fSEric Farman 	u32 ccw_head;
8580a19e61eSDong Jia Shi 
85971189f26SCornelia Huck 	if (!cp->initialized)
86071189f26SCornelia Huck 		return;
86171189f26SCornelia Huck 
8620a19e61eSDong Jia Shi 	/*
8630a19e61eSDong Jia Shi 	 * LATER:
8640a19e61eSDong Jia Shi 	 * For now, only update the cmd.cpa part. We may need to deal with
8650a19e61eSDong Jia Shi 	 * other portions of the schib as well, even if we don't return them
8660a19e61eSDong Jia Shi 	 * in the ioctl directly. Path status changes etc.
8670a19e61eSDong Jia Shi 	 */
8680a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8690a19e61eSDong Jia Shi 		ccw_head = (u32)(u64)chain->ch_ccw;
87015f0eb3dSEric Farman 		/*
87115f0eb3dSEric Farman 		 * On successful execution, cpa points just beyond the end
87215f0eb3dSEric Farman 		 * of the chain.
87315f0eb3dSEric Farman 		 */
87415f0eb3dSEric Farman 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
8750a19e61eSDong Jia Shi 			/*
8760a19e61eSDong Jia Shi 			 * (cpa - ccw_head) is the offset value of the host
8770a19e61eSDong Jia Shi 			 * physical ccw to its chain head.
8780a19e61eSDong Jia Shi 			 * Adding this value to the guest physical ccw chain
8790a19e61eSDong Jia Shi 			 * head gets us the guest cpa.
8800a19e61eSDong Jia Shi 			 */
8810a19e61eSDong Jia Shi 			cpa = chain->ch_iova + (cpa - ccw_head);
8820a19e61eSDong Jia Shi 			break;
8830a19e61eSDong Jia Shi 		}
8840a19e61eSDong Jia Shi 	}
8850a19e61eSDong Jia Shi 
8860a19e61eSDong Jia Shi 	scsw->cmd.cpa = cpa;
8870a19e61eSDong Jia Shi }
8880a19e61eSDong Jia Shi 
8890a19e61eSDong Jia Shi /**
8900a19e61eSDong Jia Shi  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
891364e3f90SSebastian Ott  * @cp: channel_program on which to perform the operation
8920a19e61eSDong Jia Shi  * @iova: the iova to check
8935a4fe7c4SEric Farman  * @length: the length to check from @iova
8940a19e61eSDong Jia Shi  *
8950a19e61eSDong Jia Shi  * If the @iova is currently pinned for the ccw chain, return true;
8960a19e61eSDong Jia Shi  * else return false.
8970a19e61eSDong Jia Shi  */
8985a4fe7c4SEric Farman bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length)
8990a19e61eSDong Jia Shi {
9000a19e61eSDong Jia Shi 	struct ccwchain *chain;
9010a19e61eSDong Jia Shi 	int i;
9020a19e61eSDong Jia Shi 
90371189f26SCornelia Huck 	if (!cp->initialized)
90471189f26SCornelia Huck 		return false;
90571189f26SCornelia Huck 
9060a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9070a19e61eSDong Jia Shi 		for (i = 0; i < chain->ch_len; i++)
9084b946d65SEric Farman 			if (page_array_iova_pinned(&chain->ch_pa[i], iova, length))
9090a19e61eSDong Jia Shi 				return true;
9100a19e61eSDong Jia Shi 	}
9110a19e61eSDong Jia Shi 
9120a19e61eSDong Jia Shi 	return false;
9130a19e61eSDong Jia Shi }
914