xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_cp.c (revision c6c82e0cd8125d30f2f1b29205c7e1a2f1a6785b)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20a19e61eSDong Jia Shi /*
30a19e61eSDong Jia Shi  * channel program interfaces
40a19e61eSDong Jia Shi  *
50a19e61eSDong Jia Shi  * Copyright IBM Corp. 2017
60a19e61eSDong Jia Shi  *
70a19e61eSDong Jia Shi  * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
80a19e61eSDong Jia Shi  *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
90a19e61eSDong Jia Shi  */
100a19e61eSDong Jia Shi 
11725b94d7SJared Rossi #include <linux/ratelimit.h>
120a19e61eSDong Jia Shi #include <linux/mm.h>
130a19e61eSDong Jia Shi #include <linux/slab.h>
140a19e61eSDong Jia Shi #include <linux/iommu.h>
150a19e61eSDong Jia Shi #include <linux/vfio.h>
160a19e61eSDong Jia Shi #include <asm/idals.h>
170a19e61eSDong Jia Shi 
180a19e61eSDong Jia Shi #include "vfio_ccw_cp.h"
190a19e61eSDong Jia Shi 
200a19e61eSDong Jia Shi struct pfn_array {
2180c57f7aSDong Jia Shi 	/* Starting guest physical I/O address. */
220a19e61eSDong Jia Shi 	unsigned long		pa_iova;
2380c57f7aSDong Jia Shi 	/* Array that stores PFNs of the pages need to pin. */
240a19e61eSDong Jia Shi 	unsigned long		*pa_iova_pfn;
2580c57f7aSDong Jia Shi 	/* Array that receives PFNs of the pages pinned. */
260a19e61eSDong Jia Shi 	unsigned long		*pa_pfn;
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. */
39e7eaf91bSEric Farman 	struct pfn_array	*ch_pa;
400a19e61eSDong Jia Shi };
410a19e61eSDong Jia Shi 
420a19e61eSDong Jia Shi /*
43e4f3f18bSEric Farman  * pfn_array_alloc() - alloc memory for PFNs
440a19e61eSDong Jia Shi  * @pa: pfn_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  *
48e4f3f18bSEric Farman  * Attempt to allocate memory for PFNs.
490a19e61eSDong Jia Shi  *
500a19e61eSDong Jia Shi  * Usage of pfn_array:
515c1cfb1cSDong Jia Shi  * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in
525c1cfb1cSDong Jia Shi  * this structure will be filled in by this function.
530a19e61eSDong Jia Shi  *
540a19e61eSDong Jia Shi  * Returns:
55e4f3f18bSEric Farman  *         0 if PFNs are allocated
56e4f3f18bSEric Farman  *   -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova_pfn is not NULL
57e4f3f18bSEric Farman  *   -ENOMEM if alloc failed
580a19e61eSDong Jia Shi  */
59e4f3f18bSEric Farman static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len)
600a19e61eSDong Jia Shi {
61e4f3f18bSEric Farman 	int i;
620a19e61eSDong Jia Shi 
635c1cfb1cSDong Jia Shi 	if (pa->pa_nr || pa->pa_iova_pfn)
640a19e61eSDong Jia Shi 		return -EINVAL;
650a19e61eSDong Jia Shi 
660a19e61eSDong Jia Shi 	pa->pa_iova = iova;
670a19e61eSDong Jia Shi 
680a19e61eSDong Jia Shi 	pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
690a19e61eSDong Jia Shi 	if (!pa->pa_nr)
700a19e61eSDong Jia Shi 		return -EINVAL;
710a19e61eSDong Jia Shi 
720a19e61eSDong Jia Shi 	pa->pa_iova_pfn = kcalloc(pa->pa_nr,
730a19e61eSDong Jia Shi 				  sizeof(*pa->pa_iova_pfn) +
740a19e61eSDong Jia Shi 				  sizeof(*pa->pa_pfn),
750a19e61eSDong Jia Shi 				  GFP_KERNEL);
76c1ab6926SFarhan Ali 	if (unlikely(!pa->pa_iova_pfn)) {
77c1ab6926SFarhan Ali 		pa->pa_nr = 0;
780a19e61eSDong Jia Shi 		return -ENOMEM;
79c1ab6926SFarhan Ali 	}
800a19e61eSDong Jia Shi 	pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
810a19e61eSDong Jia Shi 
825c1cfb1cSDong Jia Shi 	pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
83c34a12e6SEric Farman 	pa->pa_pfn[0] = -1ULL;
84c34a12e6SEric Farman 	for (i = 1; i < pa->pa_nr; i++) {
855c1cfb1cSDong Jia Shi 		pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
86c34a12e6SEric Farman 		pa->pa_pfn[i] = -1ULL;
87c34a12e6SEric Farman 	}
880a19e61eSDong Jia Shi 
89e4f3f18bSEric Farman 	return 0;
90e4f3f18bSEric Farman }
91e4f3f18bSEric Farman 
92e4f3f18bSEric Farman /*
93e4f3f18bSEric Farman  * pfn_array_pin() - Pin user pages in memory
94e4f3f18bSEric Farman  * @pa: pfn_array on which to perform the operation
95e4f3f18bSEric Farman  * @mdev: the mediated device to perform pin operations
96e4f3f18bSEric Farman  *
97e4f3f18bSEric Farman  * Returns number of pages pinned upon success.
98e4f3f18bSEric Farman  * If the pin request partially succeeds, or fails completely,
99e4f3f18bSEric Farman  * all pages are left unpinned and a negative error value is returned.
100e4f3f18bSEric Farman  */
101e4f3f18bSEric Farman static int pfn_array_pin(struct pfn_array *pa, struct device *mdev)
102e4f3f18bSEric Farman {
103e4f3f18bSEric Farman 	int ret = 0;
104e4f3f18bSEric Farman 
1055c1cfb1cSDong Jia Shi 	ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
1065c1cfb1cSDong Jia Shi 			     IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
1075c1cfb1cSDong Jia Shi 
1085c1cfb1cSDong Jia Shi 	if (ret < 0) {
1095c1cfb1cSDong Jia Shi 		goto err_out;
1105c1cfb1cSDong Jia Shi 	} else if (ret > 0 && ret != pa->pa_nr) {
1115c1cfb1cSDong Jia Shi 		vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret);
1120a19e61eSDong Jia Shi 		ret = -EINVAL;
1135c1cfb1cSDong Jia Shi 		goto err_out;
1145c1cfb1cSDong Jia Shi 	}
1150a19e61eSDong Jia Shi 
1160a19e61eSDong Jia Shi 	return ret;
1175c1cfb1cSDong Jia Shi 
1185c1cfb1cSDong Jia Shi err_out:
1195c1cfb1cSDong Jia Shi 	pa->pa_nr = 0;
1205c1cfb1cSDong Jia Shi 
1215c1cfb1cSDong Jia Shi 	return ret;
1225c1cfb1cSDong Jia Shi }
1235c1cfb1cSDong Jia Shi 
1245c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */
1255c1cfb1cSDong Jia Shi static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
1265c1cfb1cSDong Jia Shi {
127e4f3f18bSEric Farman 	/* Only unpin if any pages were pinned to begin with */
128e4f3f18bSEric Farman 	if (pa->pa_nr)
1295c1cfb1cSDong Jia Shi 		vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
1305c1cfb1cSDong Jia Shi 	pa->pa_nr = 0;
1315c1cfb1cSDong Jia Shi 	kfree(pa->pa_iova_pfn);
1320a19e61eSDong Jia Shi }
1330a19e61eSDong Jia Shi 
134e7eaf91bSEric Farman static bool pfn_array_iova_pinned(struct pfn_array *pa, unsigned long iova)
1350a19e61eSDong Jia Shi {
136e7eaf91bSEric Farman 	unsigned long iova_pfn = iova >> PAGE_SHIFT;
1370a19e61eSDong Jia Shi 	int i;
1380a19e61eSDong Jia Shi 
139e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
140e7eaf91bSEric Farman 		if (pa->pa_iova_pfn[i] == iova_pfn)
1410a19e61eSDong Jia Shi 			return true;
1420a19e61eSDong Jia Shi 
1430a19e61eSDong Jia Shi 	return false;
1440a19e61eSDong Jia Shi }
145e7eaf91bSEric Farman /* Create the list of IDAL words for a pfn_array. */
146e7eaf91bSEric Farman static inline void pfn_array_idal_create_words(
147e7eaf91bSEric Farman 	struct pfn_array *pa,
1480a19e61eSDong Jia Shi 	unsigned long *idaws)
1490a19e61eSDong Jia Shi {
150e7eaf91bSEric Farman 	int i;
1510a19e61eSDong Jia Shi 
1520a19e61eSDong Jia Shi 	/*
1530a19e61eSDong Jia Shi 	 * Idal words (execept the first one) rely on the memory being 4k
1540a19e61eSDong Jia Shi 	 * aligned. If a user virtual address is 4K aligned, then it's
1550a19e61eSDong Jia Shi 	 * corresponding kernel physical address will also be 4K aligned. Thus
1560a19e61eSDong Jia Shi 	 * there will be no problem here to simply use the phys to create an
1570a19e61eSDong Jia Shi 	 * idaw.
1580a19e61eSDong Jia Shi 	 */
159e7eaf91bSEric Farman 
160e7eaf91bSEric Farman 	for (i = 0; i < pa->pa_nr; i++)
161e7eaf91bSEric Farman 		idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT;
1628aabf0edSEric Farman 
1638aabf0edSEric Farman 	/* Adjust the first IDAW, since it may not start on a page boundary */
164e7eaf91bSEric Farman 	idaws[0] += pa->pa_iova & (PAGE_SIZE - 1);
1650a19e61eSDong Jia Shi }
1660a19e61eSDong Jia Shi 
167dbd66558SCornelia Huck static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
1687f8e89a8SEric Farman {
1697f8e89a8SEric Farman 	struct ccw0 ccw0;
1707f8e89a8SEric Farman 	struct ccw1 *pccw1 = source;
1717f8e89a8SEric Farman 	int i;
1727f8e89a8SEric Farman 
1737f8e89a8SEric Farman 	for (i = 0; i < len; i++) {
1747f8e89a8SEric Farman 		ccw0 = *(struct ccw0 *)pccw1;
1757f8e89a8SEric Farman 		if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
1767f8e89a8SEric Farman 			pccw1->cmd_code = CCW_CMD_TIC;
1777f8e89a8SEric Farman 			pccw1->flags = 0;
1787f8e89a8SEric Farman 			pccw1->count = 0;
1797f8e89a8SEric Farman 		} else {
1807f8e89a8SEric Farman 			pccw1->cmd_code = ccw0.cmd_code;
1817f8e89a8SEric Farman 			pccw1->flags = ccw0.flags;
1827f8e89a8SEric Farman 			pccw1->count = ccw0.count;
1837f8e89a8SEric Farman 		}
1847f8e89a8SEric Farman 		pccw1->cda = ccw0.cda;
1857f8e89a8SEric Farman 		pccw1++;
1867f8e89a8SEric Farman 	}
1877f8e89a8SEric Farman }
1880a19e61eSDong Jia Shi 
1890a19e61eSDong Jia Shi /*
1900a19e61eSDong Jia Shi  * Within the domain (@mdev), copy @n bytes from a guest physical
1910a19e61eSDong Jia Shi  * address (@iova) to a host physical address (@to).
1920a19e61eSDong Jia Shi  */
1930a19e61eSDong Jia Shi static long copy_from_iova(struct device *mdev,
1940a19e61eSDong Jia Shi 			   void *to, u64 iova,
1950a19e61eSDong Jia Shi 			   unsigned long n)
1960a19e61eSDong Jia Shi {
1970a19e61eSDong Jia Shi 	struct pfn_array pa = {0};
1980a19e61eSDong Jia Shi 	u64 from;
1990a19e61eSDong Jia Shi 	int i, ret;
2000a19e61eSDong Jia Shi 	unsigned long l, m;
2010a19e61eSDong Jia Shi 
202e4f3f18bSEric Farman 	ret = pfn_array_alloc(&pa, iova, n);
203e4f3f18bSEric Farman 	if (ret < 0)
2040a19e61eSDong Jia Shi 		return ret;
2050a19e61eSDong Jia Shi 
206e4f3f18bSEric Farman 	ret = pfn_array_pin(&pa, mdev);
207e4f3f18bSEric Farman 	if (ret < 0) {
208e4f3f18bSEric Farman 		pfn_array_unpin_free(&pa, mdev);
209e4f3f18bSEric Farman 		return ret;
210e4f3f18bSEric Farman 	}
211e4f3f18bSEric Farman 
2120a19e61eSDong Jia Shi 	l = n;
2130a19e61eSDong Jia Shi 	for (i = 0; i < pa.pa_nr; i++) {
2140a19e61eSDong Jia Shi 		from = pa.pa_pfn[i] << PAGE_SHIFT;
2150a19e61eSDong Jia Shi 		m = PAGE_SIZE;
2160a19e61eSDong Jia Shi 		if (i == 0) {
2170a19e61eSDong Jia Shi 			from += iova & (PAGE_SIZE - 1);
2180a19e61eSDong Jia Shi 			m -= iova & (PAGE_SIZE - 1);
2190a19e61eSDong Jia Shi 		}
2200a19e61eSDong Jia Shi 
2210a19e61eSDong Jia Shi 		m = min(l, m);
2220a19e61eSDong Jia Shi 		memcpy(to + (n - l), (void *)from, m);
2230a19e61eSDong Jia Shi 
2240a19e61eSDong Jia Shi 		l -= m;
2250a19e61eSDong Jia Shi 		if (l == 0)
2260a19e61eSDong Jia Shi 			break;
2270a19e61eSDong Jia Shi 	}
2280a19e61eSDong Jia Shi 
2290a19e61eSDong Jia Shi 	pfn_array_unpin_free(&pa, mdev);
2300a19e61eSDong Jia Shi 
2310a19e61eSDong Jia Shi 	return l;
2320a19e61eSDong Jia Shi }
2330a19e61eSDong Jia Shi 
2340a19e61eSDong Jia Shi /*
2350a19e61eSDong Jia Shi  * Helpers to operate ccwchain.
2360a19e61eSDong Jia Shi  */
2375d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
2385d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
2395d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
2405d87fbf7SEric Farman 
2410a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
2420a19e61eSDong Jia Shi 
2430a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
2440a19e61eSDong Jia Shi 
2450a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
2465d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
2470a19e61eSDong Jia Shi 
2480a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
2490a19e61eSDong Jia Shi 
25048bd0eeeSEric Farman /*
2515d87fbf7SEric Farman  * ccw_does_data_transfer()
2525d87fbf7SEric Farman  *
2535d87fbf7SEric Farman  * Determine whether a CCW will move any data, such that the guest pages
2545d87fbf7SEric Farman  * would need to be pinned before performing the I/O.
2555d87fbf7SEric Farman  *
2565d87fbf7SEric Farman  * Returns 1 if yes, 0 if no.
2575d87fbf7SEric Farman  */
2585d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw)
2595d87fbf7SEric Farman {
260453eac31SEric Farman 	/* If the count field is zero, then no data will be transferred */
261453eac31SEric Farman 	if (ccw->count == 0)
262453eac31SEric Farman 		return 0;
263453eac31SEric Farman 
2649b6e57e5SEric Farman 	/* If the command is a NOP, then no data will be transferred */
2659b6e57e5SEric Farman 	if (ccw_is_noop(ccw))
2669b6e57e5SEric Farman 		return 0;
2679b6e57e5SEric Farman 
2685d87fbf7SEric Farman 	/* If the skip flag is off, then data will be transferred */
2695d87fbf7SEric Farman 	if (!ccw_is_skip(ccw))
2705d87fbf7SEric Farman 		return 1;
2715d87fbf7SEric Farman 
2725d87fbf7SEric Farman 	/*
2735d87fbf7SEric Farman 	 * If the skip flag is on, it is only meaningful if the command
2745d87fbf7SEric Farman 	 * code is a read, read backward, sense, or sense ID.  In those
2755d87fbf7SEric Farman 	 * cases, no data will be transferred.
2765d87fbf7SEric Farman 	 */
2775d87fbf7SEric Farman 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
2785d87fbf7SEric Farman 		return 0;
2795d87fbf7SEric Farman 
2805d87fbf7SEric Farman 	if (ccw_is_sense(ccw))
2815d87fbf7SEric Farman 		return 0;
2825d87fbf7SEric Farman 
2835d87fbf7SEric Farman 	/* The skip flag is on, but it is ignored for this command code. */
2845d87fbf7SEric Farman 	return 1;
2855d87fbf7SEric Farman }
2865d87fbf7SEric Farman 
2875d87fbf7SEric Farman /*
28848bd0eeeSEric Farman  * is_cpa_within_range()
28948bd0eeeSEric Farman  *
29048bd0eeeSEric Farman  * @cpa: channel program address being questioned
29148bd0eeeSEric Farman  * @head: address of the beginning of a CCW chain
29248bd0eeeSEric Farman  * @len: number of CCWs within the chain
29348bd0eeeSEric Farman  *
29448bd0eeeSEric Farman  * Determine whether the address of a CCW (whether a new chain,
29548bd0eeeSEric Farman  * or the target of a TIC) falls within a range (including the end points).
29648bd0eeeSEric Farman  *
29748bd0eeeSEric Farman  * Returns 1 if yes, 0 if no.
29848bd0eeeSEric Farman  */
29948bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
30048bd0eeeSEric Farman {
30148bd0eeeSEric Farman 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
30248bd0eeeSEric Farman 
30348bd0eeeSEric Farman 	return (head <= cpa && cpa <= tail);
30448bd0eeeSEric Farman }
30548bd0eeeSEric Farman 
30648bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
30748bd0eeeSEric Farman {
30848bd0eeeSEric Farman 	if (!ccw_is_tic(ccw))
30948bd0eeeSEric Farman 		return 0;
31048bd0eeeSEric Farman 
31148bd0eeeSEric Farman 	return is_cpa_within_range(ccw->cda, head, len);
31248bd0eeeSEric Farman }
31348bd0eeeSEric Farman 
3140a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
3150a19e61eSDong Jia Shi {
3160a19e61eSDong Jia Shi 	struct ccwchain *chain;
3170a19e61eSDong Jia Shi 	void *data;
3180a19e61eSDong Jia Shi 	size_t size;
3190a19e61eSDong Jia Shi 
3200a19e61eSDong Jia Shi 	/* Make ccw address aligned to 8. */
3210a19e61eSDong Jia Shi 	size = ((sizeof(*chain) + 7L) & -8L) +
3220a19e61eSDong Jia Shi 		sizeof(*chain->ch_ccw) * len +
323e7eaf91bSEric Farman 		sizeof(*chain->ch_pa) * len;
3240a19e61eSDong Jia Shi 	chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
3250a19e61eSDong Jia Shi 	if (!chain)
3260a19e61eSDong Jia Shi 		return NULL;
3270a19e61eSDong Jia Shi 
3280a19e61eSDong Jia Shi 	data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
3290a19e61eSDong Jia Shi 	chain->ch_ccw = (struct ccw1 *)data;
3300a19e61eSDong Jia Shi 
3310a19e61eSDong Jia Shi 	data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
332e7eaf91bSEric Farman 	chain->ch_pa = (struct pfn_array *)data;
3330a19e61eSDong Jia Shi 
3340a19e61eSDong Jia Shi 	chain->ch_len = len;
3350a19e61eSDong Jia Shi 
3360a19e61eSDong Jia Shi 	list_add_tail(&chain->next, &cp->ccwchain_list);
3370a19e61eSDong Jia Shi 
3380a19e61eSDong Jia Shi 	return chain;
3390a19e61eSDong Jia Shi }
3400a19e61eSDong Jia Shi 
3410a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain)
3420a19e61eSDong Jia Shi {
3430a19e61eSDong Jia Shi 	list_del(&chain->next);
3440a19e61eSDong Jia Shi 	kfree(chain);
3450a19e61eSDong Jia Shi }
3460a19e61eSDong Jia Shi 
3470a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */
3480a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx)
3490a19e61eSDong Jia Shi {
3500a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
3510a19e61eSDong Jia Shi 
3529b6e57e5SEric Farman 	if (ccw_is_tic(ccw))
353408358b5SJason J. Herne 		return;
3540a19e61eSDong Jia Shi 
3550a19e61eSDong Jia Shi 	kfree((void *)(u64)ccw->cda);
3560a19e61eSDong Jia Shi }
3570a19e61eSDong Jia Shi 
3580a19e61eSDong Jia Shi /**
3590a19e61eSDong Jia Shi  * ccwchain_calc_length - calculate the length of the ccw chain.
3600a19e61eSDong Jia Shi  * @iova: guest physical address of the target ccw chain
3610a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
3620a19e61eSDong Jia Shi  *
3630a19e61eSDong Jia Shi  * This is the chain length not considering any TICs.
3640a19e61eSDong Jia Shi  * You need to do a new round for each TIC target.
3650a19e61eSDong Jia Shi  *
366fb9e7880SHalil Pasic  * The program is also validated for absence of not yet supported
367fb9e7880SHalil Pasic  * indirect data addressing scenarios.
368fb9e7880SHalil Pasic  *
3690a19e61eSDong Jia Shi  * Returns: the length of the ccw chain or -errno.
3700a19e61eSDong Jia Shi  */
3710a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
3720a19e61eSDong Jia Shi {
3731d897e47SEric Farman 	struct ccw1 *ccw = cp->guest_cp;
374ded563f3SEric Farman 	int cnt = 0;
3750a19e61eSDong Jia Shi 
3760a19e61eSDong Jia Shi 	do {
3770a19e61eSDong Jia Shi 		cnt++;
3780a19e61eSDong Jia Shi 
379fb9e7880SHalil Pasic 		/*
380fb9e7880SHalil Pasic 		 * As we don't want to fail direct addressing even if the
381fb9e7880SHalil Pasic 		 * orb specified one of the unsupported formats, we defer
382fb9e7880SHalil Pasic 		 * checking for IDAWs in unsupported formats to here.
383fb9e7880SHalil Pasic 		 */
3841d897e47SEric Farman 		if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw))
385fb9e7880SHalil Pasic 			return -EOPNOTSUPP;
386fb9e7880SHalil Pasic 
38748bd0eeeSEric Farman 		/*
38848bd0eeeSEric Farman 		 * We want to keep counting if the current CCW has the
38948bd0eeeSEric Farman 		 * command-chaining flag enabled, or if it is a TIC CCW
39048bd0eeeSEric Farman 		 * that loops back into the current chain.  The latter
39148bd0eeeSEric Farman 		 * is used for device orientation, where the CCW PRIOR to
39248bd0eeeSEric Farman 		 * the TIC can either jump to the TIC or a CCW immediately
39348bd0eeeSEric Farman 		 * after the TIC, depending on the results of its operation.
39448bd0eeeSEric Farman 		 */
39548bd0eeeSEric Farman 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
3960a19e61eSDong Jia Shi 			break;
3970a19e61eSDong Jia Shi 
3980a19e61eSDong Jia Shi 		ccw++;
3990a19e61eSDong Jia Shi 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
4000a19e61eSDong Jia Shi 
4010a19e61eSDong Jia Shi 	if (cnt == CCWCHAIN_LEN_MAX + 1)
4020a19e61eSDong Jia Shi 		cnt = -EINVAL;
4030a19e61eSDong Jia Shi 
4040a19e61eSDong Jia Shi 	return cnt;
4050a19e61eSDong Jia Shi }
4060a19e61eSDong Jia Shi 
4070a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
4080a19e61eSDong Jia Shi {
4090a19e61eSDong Jia Shi 	struct ccwchain *chain;
4102904337fSEric Farman 	u32 ccw_head;
4110a19e61eSDong Jia Shi 
4120a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
4130a19e61eSDong Jia Shi 		ccw_head = chain->ch_iova;
4142904337fSEric Farman 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
4150a19e61eSDong Jia Shi 			return 1;
4160a19e61eSDong Jia Shi 	}
4170a19e61eSDong Jia Shi 
4180a19e61eSDong Jia Shi 	return 0;
4190a19e61eSDong Jia Shi }
4200a19e61eSDong Jia Shi 
4210a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain,
4220a19e61eSDong Jia Shi 			     struct channel_program *cp);
4230a19e61eSDong Jia Shi 
424363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
4250a19e61eSDong Jia Shi {
4260a19e61eSDong Jia Shi 	struct ccwchain *chain;
4278b515be5SFarhan Ali 	int len, ret;
4280a19e61eSDong Jia Shi 
429ded563f3SEric Farman 	/* Copy 2K (the most we support today) of possible CCWs */
4305223bee8SEric Farman 	len = copy_from_iova(cp->mdev, cp->guest_cp, cda,
4315223bee8SEric Farman 			     CCWCHAIN_LEN_MAX * sizeof(struct ccw1));
432ded563f3SEric Farman 	if (len)
433ded563f3SEric Farman 		return len;
434ded563f3SEric Farman 
4357f8e89a8SEric Farman 	/* Convert any Format-0 CCWs to Format-1 */
4367f8e89a8SEric Farman 	if (!cp->orb.cmd.fmt)
437c382cbc6SEric Farman 		convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
4387f8e89a8SEric Farman 
439ded563f3SEric Farman 	/* Count the CCWs in the current chain */
440363fe5f7SEric Farman 	len = ccwchain_calc_length(cda, cp);
4410a19e61eSDong Jia Shi 	if (len < 0)
4420a19e61eSDong Jia Shi 		return len;
4430a19e61eSDong Jia Shi 
4440a19e61eSDong Jia Shi 	/* Need alloc a new chain for this one. */
4450a19e61eSDong Jia Shi 	chain = ccwchain_alloc(cp, len);
4460a19e61eSDong Jia Shi 	if (!chain)
4470a19e61eSDong Jia Shi 		return -ENOMEM;
448363fe5f7SEric Farman 	chain->ch_iova = cda;
4490a19e61eSDong Jia Shi 
45062465902SEric Farman 	/* Copy the actual CCWs into the new chain */
45162465902SEric Farman 	memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
4520a19e61eSDong Jia Shi 
4530a19e61eSDong Jia Shi 	/* Loop for tics on this new chain. */
4548b515be5SFarhan Ali 	ret = ccwchain_loop_tic(chain, cp);
4558b515be5SFarhan Ali 
4568b515be5SFarhan Ali 	if (ret)
4578b515be5SFarhan Ali 		ccwchain_free(chain);
4588b515be5SFarhan Ali 
4598b515be5SFarhan Ali 	return ret;
4600a19e61eSDong Jia Shi }
4610a19e61eSDong Jia Shi 
4620a19e61eSDong Jia Shi /* Loop for TICs. */
4630a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
4640a19e61eSDong Jia Shi {
4650a19e61eSDong Jia Shi 	struct ccw1 *tic;
4660a19e61eSDong Jia Shi 	int i, ret;
4670a19e61eSDong Jia Shi 
4680a19e61eSDong Jia Shi 	for (i = 0; i < chain->ch_len; i++) {
4690a19e61eSDong Jia Shi 		tic = chain->ch_ccw + i;
4700a19e61eSDong Jia Shi 
4710a19e61eSDong Jia Shi 		if (!ccw_is_tic(tic))
4720a19e61eSDong Jia Shi 			continue;
4730a19e61eSDong Jia Shi 
474e64bd689SEric Farman 		/* May transfer to an existing chain. */
475e64bd689SEric Farman 		if (tic_target_chain_exists(tic, cp))
476e64bd689SEric Farman 			continue;
477e64bd689SEric Farman 
478363fe5f7SEric Farman 		/* Build a ccwchain for the next segment */
479363fe5f7SEric Farman 		ret = ccwchain_handle_ccw(tic->cda, cp);
4800a19e61eSDong Jia Shi 		if (ret)
4810a19e61eSDong Jia Shi 			return ret;
4820a19e61eSDong Jia Shi 	}
4830a19e61eSDong Jia Shi 
4840a19e61eSDong Jia Shi 	return 0;
4850a19e61eSDong Jia Shi }
4860a19e61eSDong Jia Shi 
4870a19e61eSDong Jia Shi static int ccwchain_fetch_tic(struct ccwchain *chain,
4880a19e61eSDong Jia Shi 			      int idx,
4890a19e61eSDong Jia Shi 			      struct channel_program *cp)
4900a19e61eSDong Jia Shi {
4910a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
4920a19e61eSDong Jia Shi 	struct ccwchain *iter;
4932904337fSEric Farman 	u32 ccw_head;
4940a19e61eSDong Jia Shi 
4950a19e61eSDong Jia Shi 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
4960a19e61eSDong Jia Shi 		ccw_head = iter->ch_iova;
4972904337fSEric Farman 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
498c389377cSJason J. Herne 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
4990a19e61eSDong Jia Shi 						     (ccw->cda - ccw_head));
5000a19e61eSDong Jia Shi 			return 0;
5010a19e61eSDong Jia Shi 		}
5020a19e61eSDong Jia Shi 	}
5030a19e61eSDong Jia Shi 
5040a19e61eSDong Jia Shi 	return -EFAULT;
5050a19e61eSDong Jia Shi }
5060a19e61eSDong Jia Shi 
5070a19e61eSDong Jia Shi static int ccwchain_fetch_direct(struct ccwchain *chain,
5080a19e61eSDong Jia Shi 				 int idx,
5090a19e61eSDong Jia Shi 				 struct channel_program *cp)
5100a19e61eSDong Jia Shi {
5110a19e61eSDong Jia Shi 	struct ccw1 *ccw;
512e7eaf91bSEric Farman 	struct pfn_array *pa;
51301aa26c6SEric Farman 	u64 iova;
5140a19e61eSDong Jia Shi 	unsigned long *idaws;
5156238f921SDong Jia Shi 	int ret;
516453eac31SEric Farman 	int bytes = 1;
51701aa26c6SEric Farman 	int idaw_nr, idal_len;
51801aa26c6SEric Farman 	int i;
5190a19e61eSDong Jia Shi 
5200a19e61eSDong Jia Shi 	ccw = chain->ch_ccw + idx;
5210a19e61eSDong Jia Shi 
522e8573b39SEric Farman 	if (ccw->count)
523453eac31SEric Farman 		bytes = ccw->count;
524e8573b39SEric Farman 
525e8573b39SEric Farman 	/* Calculate size of IDAL */
52601aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
52701aa26c6SEric Farman 		/* Read first IDAW to see if it's 4K-aligned or not. */
52801aa26c6SEric Farman 		/* All subsequent IDAws will be 4K-aligned. */
52901aa26c6SEric Farman 		ret = copy_from_iova(cp->mdev, &iova, ccw->cda, sizeof(iova));
53001aa26c6SEric Farman 		if (ret)
53101aa26c6SEric Farman 			return ret;
53201aa26c6SEric Farman 	} else {
53301aa26c6SEric Farman 		iova = ccw->cda;
53401aa26c6SEric Farman 	}
53501aa26c6SEric Farman 	idaw_nr = idal_nr_words((void *)iova, bytes);
53601aa26c6SEric Farman 	idal_len = idaw_nr * sizeof(*idaws);
537e8573b39SEric Farman 
538e8573b39SEric Farman 	/* Allocate an IDAL from host storage */
539e8573b39SEric Farman 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
540e8573b39SEric Farman 	if (!idaws) {
541e8573b39SEric Farman 		ret = -ENOMEM;
542e8573b39SEric Farman 		goto out_init;
5434cebc5d6SDong Jia Shi 	}
5444cebc5d6SDong Jia Shi 
5450a19e61eSDong Jia Shi 	/*
54601aa26c6SEric Farman 	 * Allocate an array of pfn's for pages to pin/translate.
54701aa26c6SEric Farman 	 * The number of pages is actually the count of the idaws
54801aa26c6SEric Farman 	 * required for the data transfer, since we only only support
54901aa26c6SEric Farman 	 * 4K IDAWs today.
5500a19e61eSDong Jia Shi 	 */
551e7eaf91bSEric Farman 	pa = chain->ch_pa + idx;
55201aa26c6SEric Farman 	ret = pfn_array_alloc(pa, iova, bytes);
553e4f3f18bSEric Farman 	if (ret < 0)
554e8573b39SEric Farman 		goto out_free_idaws;
555e4f3f18bSEric Farman 
55601aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
55701aa26c6SEric Farman 		/* Copy guest IDAL into host IDAL */
55801aa26c6SEric Farman 		ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idal_len);
55901aa26c6SEric Farman 		if (ret)
56001aa26c6SEric Farman 			goto out_unpin;
56101aa26c6SEric Farman 
56201aa26c6SEric Farman 		/*
56301aa26c6SEric Farman 		 * Copy guest IDAWs into pfn_array, in case the memory they
56401aa26c6SEric Farman 		 * occupy is not contiguous.
56501aa26c6SEric Farman 		 */
56601aa26c6SEric Farman 		for (i = 0; i < idaw_nr; i++)
56701aa26c6SEric Farman 			pa->pa_iova_pfn[i] = idaws[i] >> PAGE_SHIFT;
56801aa26c6SEric Farman 	} else {
56901aa26c6SEric Farman 		/*
57001aa26c6SEric Farman 		 * No action is required here; the iova addresses in pfn_array
57101aa26c6SEric Farman 		 * were initialized sequentially in pfn_array_alloc() beginning
57201aa26c6SEric Farman 		 * with the contents of ccw->cda.
57301aa26c6SEric Farman 		 */
57401aa26c6SEric Farman 	}
57501aa26c6SEric Farman 
5765d87fbf7SEric Farman 	if (ccw_does_data_transfer(ccw)) {
577e7eaf91bSEric Farman 		ret = pfn_array_pin(pa, cp->mdev);
5786238f921SDong Jia Shi 		if (ret < 0)
579806212f9SEric Farman 			goto out_unpin;
5805d87fbf7SEric Farman 	} else {
581e7eaf91bSEric Farman 		pa->pa_nr = 0;
5825d87fbf7SEric Farman 	}
5830a19e61eSDong Jia Shi 
5840a19e61eSDong Jia Shi 	ccw->cda = (__u32) virt_to_phys(idaws);
5850a19e61eSDong Jia Shi 	ccw->flags |= CCW_FLAG_IDA;
5860a19e61eSDong Jia Shi 
587e8573b39SEric Farman 	/* Populate the IDAL with pinned/translated addresses from pfn */
588e7eaf91bSEric Farman 	pfn_array_idal_create_words(pa, idaws);
5890a19e61eSDong Jia Shi 
5900a19e61eSDong Jia Shi 	return 0;
5916238f921SDong Jia Shi 
5926238f921SDong Jia Shi out_unpin:
593e7eaf91bSEric Farman 	pfn_array_unpin_free(pa, cp->mdev);
594e8573b39SEric Farman out_free_idaws:
595e8573b39SEric Farman 	kfree(idaws);
5966238f921SDong Jia Shi out_init:
5976238f921SDong Jia Shi 	ccw->cda = 0;
5986238f921SDong Jia Shi 	return ret;
5990a19e61eSDong Jia Shi }
6000a19e61eSDong Jia Shi 
6010a19e61eSDong Jia Shi /*
6020a19e61eSDong Jia Shi  * Fetch one ccw.
6030a19e61eSDong Jia Shi  * To reduce memory copy, we'll pin the cda page in memory,
6040a19e61eSDong Jia Shi  * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
6050a19e61eSDong Jia Shi  * direct ccws to idal ccws.
6060a19e61eSDong Jia Shi  */
6070a19e61eSDong Jia Shi static int ccwchain_fetch_one(struct ccwchain *chain,
6080a19e61eSDong Jia Shi 			      int idx,
6090a19e61eSDong Jia Shi 			      struct channel_program *cp)
6100a19e61eSDong Jia Shi {
6110a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
6120a19e61eSDong Jia Shi 
6130a19e61eSDong Jia Shi 	if (ccw_is_tic(ccw))
6140a19e61eSDong Jia Shi 		return ccwchain_fetch_tic(chain, idx, cp);
6150a19e61eSDong Jia Shi 
6160a19e61eSDong Jia Shi 	return ccwchain_fetch_direct(chain, idx, cp);
6170a19e61eSDong Jia Shi }
6180a19e61eSDong Jia Shi 
6190a19e61eSDong Jia Shi /**
6200a19e61eSDong Jia Shi  * cp_init() - allocate ccwchains for a channel program.
6210a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
6220a19e61eSDong Jia Shi  * @mdev: the mediated device to perform pin/unpin operations
6230a19e61eSDong Jia Shi  * @orb: control block for the channel program from the guest
6240a19e61eSDong Jia Shi  *
6250a19e61eSDong Jia Shi  * This creates one or more ccwchain(s), and copies the raw data of
6260a19e61eSDong Jia Shi  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
6270a19e61eSDong Jia Shi  *
6280a19e61eSDong Jia Shi  * Limitations:
629725b94d7SJared Rossi  * 1. Supports idal(c64) ccw chaining.
630725b94d7SJared Rossi  * 2. Supports 4k idaw.
6310a19e61eSDong Jia Shi  *
6320a19e61eSDong Jia Shi  * Returns:
6330a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
6340a19e61eSDong Jia Shi  */
6350a19e61eSDong Jia Shi int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
6360a19e61eSDong Jia Shi {
637725b94d7SJared Rossi 	/* custom ratelimit used to avoid flood during guest IPL */
638725b94d7SJared Rossi 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
63999afcb05SEric Farman 	int ret;
6400a19e61eSDong Jia Shi 
641*c6c82e0cSEric Farman 	/* this is an error in the caller */
642*c6c82e0cSEric Farman 	if (cp->initialized)
643*c6c82e0cSEric Farman 		return -EBUSY;
644*c6c82e0cSEric Farman 
6450a19e61eSDong Jia Shi 	/*
646725b94d7SJared Rossi 	 * We only support prefetching the channel program. We assume all channel
647725b94d7SJared Rossi 	 * programs executed by supported guests likewise support prefetching.
648725b94d7SJared Rossi 	 * Executing a channel program that does not specify prefetching will
649725b94d7SJared Rossi 	 * typically not cause an error, but a warning is issued to help identify
650725b94d7SJared Rossi 	 * the problem if something does break.
6510a19e61eSDong Jia Shi 	 */
652725b94d7SJared Rossi 	if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
653725b94d7SJared Rossi 		dev_warn(mdev, "Prefetching channel program even though prefetch not specified in ORB");
6540a19e61eSDong Jia Shi 
6550a19e61eSDong Jia Shi 	INIT_LIST_HEAD(&cp->ccwchain_list);
6560a19e61eSDong Jia Shi 	memcpy(&cp->orb, orb, sizeof(*orb));
6570a19e61eSDong Jia Shi 	cp->mdev = mdev;
6580a19e61eSDong Jia Shi 
65999afcb05SEric Farman 	/* Build a ccwchain for the first CCW segment */
66099afcb05SEric Farman 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
66199afcb05SEric Farman 
662c9f597a4SFarhan Ali 	if (!ret) {
663c9f597a4SFarhan Ali 		cp->initialized = true;
664c9f597a4SFarhan Ali 
665c9f597a4SFarhan Ali 		/* It is safe to force: if it was not set but idals used
666c9f597a4SFarhan Ali 		 * ccwchain_calc_length would have returned an error.
667fb9e7880SHalil Pasic 		 */
668fb9e7880SHalil Pasic 		cp->orb.cmd.c64 = 1;
669c9f597a4SFarhan Ali 	}
67071189f26SCornelia Huck 
6710a19e61eSDong Jia Shi 	return ret;
6720a19e61eSDong Jia Shi }
6730a19e61eSDong Jia Shi 
6740a19e61eSDong Jia Shi 
6750a19e61eSDong Jia Shi /**
6760a19e61eSDong Jia Shi  * cp_free() - free resources for channel program.
6770a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
6780a19e61eSDong Jia Shi  *
6790a19e61eSDong Jia Shi  * This unpins the memory pages and frees the memory space occupied by
6800a19e61eSDong Jia Shi  * @cp, which must have been returned by a previous call to cp_init().
6810a19e61eSDong Jia Shi  * Otherwise, undefined behavior occurs.
6820a19e61eSDong Jia Shi  */
6830a19e61eSDong Jia Shi void cp_free(struct channel_program *cp)
6840a19e61eSDong Jia Shi {
685812271b9SEric Farman 	struct ccwchain *chain, *temp;
686812271b9SEric Farman 	int i;
687812271b9SEric Farman 
688812271b9SEric Farman 	if (!cp->initialized)
689812271b9SEric Farman 		return;
690812271b9SEric Farman 
691812271b9SEric Farman 	cp->initialized = false;
692812271b9SEric Farman 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
693812271b9SEric Farman 		for (i = 0; i < chain->ch_len; i++) {
694e7eaf91bSEric Farman 			pfn_array_unpin_free(chain->ch_pa + i, cp->mdev);
695812271b9SEric Farman 			ccwchain_cda_free(chain, i);
696812271b9SEric Farman 		}
697812271b9SEric Farman 		ccwchain_free(chain);
698812271b9SEric Farman 	}
6990a19e61eSDong Jia Shi }
7000a19e61eSDong Jia Shi 
7010a19e61eSDong Jia Shi /**
7020a19e61eSDong Jia Shi  * cp_prefetch() - translate a guest physical address channel program to
7030a19e61eSDong Jia Shi  *                 a real-device runnable channel program.
7040a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7050a19e61eSDong Jia Shi  *
7060a19e61eSDong Jia Shi  * This function translates the guest-physical-address channel program
7070a19e61eSDong Jia Shi  * and stores the result to ccwchain list. @cp must have been
7080a19e61eSDong Jia Shi  * initialized by a previous call with cp_init(). Otherwise, undefined
7090a19e61eSDong Jia Shi  * behavior occurs.
710d66a7355SHalil Pasic  * For each chain composing the channel program:
711d66a7355SHalil Pasic  * - On entry ch_len holds the count of CCWs to be translated.
712d66a7355SHalil Pasic  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
713d66a7355SHalil Pasic  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
7140a19e61eSDong Jia Shi  *
7150a19e61eSDong Jia Shi  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
7160a19e61eSDong Jia Shi  * as helpers to do ccw chain translation inside the kernel. Basically
7170a19e61eSDong Jia Shi  * they accept a channel program issued by a virtual machine, and
7180a19e61eSDong Jia Shi  * translate the channel program to a real-device runnable channel
7190a19e61eSDong Jia Shi  * program.
7200a19e61eSDong Jia Shi  *
7210a19e61eSDong Jia Shi  * These APIs will copy the ccws into kernel-space buffers, and update
7220a19e61eSDong Jia Shi  * the guest phsical addresses with their corresponding host physical
7230a19e61eSDong Jia Shi  * addresses.  Then channel I/O device drivers could issue the
7240a19e61eSDong Jia Shi  * translated channel program to real devices to perform an I/O
7250a19e61eSDong Jia Shi  * operation.
7260a19e61eSDong Jia Shi  *
7270a19e61eSDong Jia Shi  * These interfaces are designed to support translation only for
7280a19e61eSDong Jia Shi  * channel programs, which are generated and formatted by a
7290a19e61eSDong Jia Shi  * guest. Thus this will make it possible for things like VFIO to
7300a19e61eSDong Jia Shi  * leverage the interfaces to passthrough a channel I/O mediated
7310a19e61eSDong Jia Shi  * device in QEMU.
7320a19e61eSDong Jia Shi  *
7330a19e61eSDong Jia Shi  * We support direct ccw chaining by translating them to idal ccws.
7340a19e61eSDong Jia Shi  *
7350a19e61eSDong Jia Shi  * Returns:
7360a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
7370a19e61eSDong Jia Shi  */
7380a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp)
7390a19e61eSDong Jia Shi {
7400a19e61eSDong Jia Shi 	struct ccwchain *chain;
7410a19e61eSDong Jia Shi 	int len, idx, ret;
7420a19e61eSDong Jia Shi 
74371189f26SCornelia Huck 	/* this is an error in the caller */
74471189f26SCornelia Huck 	if (!cp->initialized)
74571189f26SCornelia Huck 		return -EINVAL;
74671189f26SCornelia Huck 
7470a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
7480a19e61eSDong Jia Shi 		len = chain->ch_len;
7490a19e61eSDong Jia Shi 		for (idx = 0; idx < len; idx++) {
7500a19e61eSDong Jia Shi 			ret = ccwchain_fetch_one(chain, idx, cp);
7510a19e61eSDong Jia Shi 			if (ret)
752d66a7355SHalil Pasic 				goto out_err;
7530a19e61eSDong Jia Shi 		}
7540a19e61eSDong Jia Shi 	}
7550a19e61eSDong Jia Shi 
7560a19e61eSDong Jia Shi 	return 0;
757d66a7355SHalil Pasic out_err:
758d66a7355SHalil Pasic 	/* Only cleanup the chain elements that were actually translated. */
759d66a7355SHalil Pasic 	chain->ch_len = idx;
760d66a7355SHalil Pasic 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
761d66a7355SHalil Pasic 		chain->ch_len = 0;
762d66a7355SHalil Pasic 	}
763d66a7355SHalil Pasic 	return ret;
7640a19e61eSDong Jia Shi }
7650a19e61eSDong Jia Shi 
7660a19e61eSDong Jia Shi /**
7670a19e61eSDong Jia Shi  * cp_get_orb() - get the orb of the channel program
7680a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7690a19e61eSDong Jia Shi  * @intparm: new intparm for the returned orb
7700a19e61eSDong Jia Shi  * @lpm: candidate value of the logical-path mask for the returned orb
7710a19e61eSDong Jia Shi  *
7720a19e61eSDong Jia Shi  * This function returns the address of the updated orb of the channel
7730a19e61eSDong Jia Shi  * program. Channel I/O device drivers could use this orb to issue a
7740a19e61eSDong Jia Shi  * ssch.
7750a19e61eSDong Jia Shi  */
7760a19e61eSDong Jia Shi union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
7770a19e61eSDong Jia Shi {
7780a19e61eSDong Jia Shi 	union orb *orb;
7790a19e61eSDong Jia Shi 	struct ccwchain *chain;
7800a19e61eSDong Jia Shi 	struct ccw1 *cpa;
7810a19e61eSDong Jia Shi 
78271189f26SCornelia Huck 	/* this is an error in the caller */
78371189f26SCornelia Huck 	if (!cp->initialized)
78471189f26SCornelia Huck 		return NULL;
78571189f26SCornelia Huck 
7860a19e61eSDong Jia Shi 	orb = &cp->orb;
7870a19e61eSDong Jia Shi 
7880a19e61eSDong Jia Shi 	orb->cmd.intparm = intparm;
7890a19e61eSDong Jia Shi 	orb->cmd.fmt = 1;
7900a19e61eSDong Jia Shi 	orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
7910a19e61eSDong Jia Shi 
7920a19e61eSDong Jia Shi 	if (orb->cmd.lpm == 0)
7930a19e61eSDong Jia Shi 		orb->cmd.lpm = lpm;
7940a19e61eSDong Jia Shi 
7950a19e61eSDong Jia Shi 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
7960a19e61eSDong Jia Shi 	cpa = chain->ch_ccw;
7970a19e61eSDong Jia Shi 	orb->cmd.cpa = (__u32) __pa(cpa);
7980a19e61eSDong Jia Shi 
7990a19e61eSDong Jia Shi 	return orb;
8000a19e61eSDong Jia Shi }
8010a19e61eSDong Jia Shi 
8020a19e61eSDong Jia Shi /**
8030a19e61eSDong Jia Shi  * cp_update_scsw() - update scsw for a channel program.
8040a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8050a19e61eSDong Jia Shi  * @scsw: I/O results of the channel program and also the target to be
8060a19e61eSDong Jia Shi  *        updated
8070a19e61eSDong Jia Shi  *
8080a19e61eSDong Jia Shi  * @scsw contains the I/O results of the channel program that pointed
8090a19e61eSDong Jia Shi  * to by @cp. However what @scsw->cpa stores is a host physical
8100a19e61eSDong Jia Shi  * address, which is meaningless for the guest, which is waiting for
8110a19e61eSDong Jia Shi  * the I/O results.
8120a19e61eSDong Jia Shi  *
8130a19e61eSDong Jia Shi  * This function updates @scsw->cpa to its coressponding guest physical
8140a19e61eSDong Jia Shi  * address.
8150a19e61eSDong Jia Shi  */
8160a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
8170a19e61eSDong Jia Shi {
8180a19e61eSDong Jia Shi 	struct ccwchain *chain;
8190a19e61eSDong Jia Shi 	u32 cpa = scsw->cmd.cpa;
8202904337fSEric Farman 	u32 ccw_head;
8210a19e61eSDong Jia Shi 
82271189f26SCornelia Huck 	if (!cp->initialized)
82371189f26SCornelia Huck 		return;
82471189f26SCornelia Huck 
8250a19e61eSDong Jia Shi 	/*
8260a19e61eSDong Jia Shi 	 * LATER:
8270a19e61eSDong Jia Shi 	 * For now, only update the cmd.cpa part. We may need to deal with
8280a19e61eSDong Jia Shi 	 * other portions of the schib as well, even if we don't return them
8290a19e61eSDong Jia Shi 	 * in the ioctl directly. Path status changes etc.
8300a19e61eSDong Jia Shi 	 */
8310a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8320a19e61eSDong Jia Shi 		ccw_head = (u32)(u64)chain->ch_ccw;
83315f0eb3dSEric Farman 		/*
83415f0eb3dSEric Farman 		 * On successful execution, cpa points just beyond the end
83515f0eb3dSEric Farman 		 * of the chain.
83615f0eb3dSEric Farman 		 */
83715f0eb3dSEric Farman 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
8380a19e61eSDong Jia Shi 			/*
8390a19e61eSDong Jia Shi 			 * (cpa - ccw_head) is the offset value of the host
8400a19e61eSDong Jia Shi 			 * physical ccw to its chain head.
8410a19e61eSDong Jia Shi 			 * Adding this value to the guest physical ccw chain
8420a19e61eSDong Jia Shi 			 * head gets us the guest cpa.
8430a19e61eSDong Jia Shi 			 */
8440a19e61eSDong Jia Shi 			cpa = chain->ch_iova + (cpa - ccw_head);
8450a19e61eSDong Jia Shi 			break;
8460a19e61eSDong Jia Shi 		}
8470a19e61eSDong Jia Shi 	}
8480a19e61eSDong Jia Shi 
8490a19e61eSDong Jia Shi 	scsw->cmd.cpa = cpa;
8500a19e61eSDong Jia Shi }
8510a19e61eSDong Jia Shi 
8520a19e61eSDong Jia Shi /**
8530a19e61eSDong Jia Shi  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
854364e3f90SSebastian Ott  * @cp: channel_program on which to perform the operation
8550a19e61eSDong Jia Shi  * @iova: the iova to check
8560a19e61eSDong Jia Shi  *
8570a19e61eSDong Jia Shi  * If the @iova is currently pinned for the ccw chain, return true;
8580a19e61eSDong Jia Shi  * else return false.
8590a19e61eSDong Jia Shi  */
8600a19e61eSDong Jia Shi bool cp_iova_pinned(struct channel_program *cp, u64 iova)
8610a19e61eSDong Jia Shi {
8620a19e61eSDong Jia Shi 	struct ccwchain *chain;
8630a19e61eSDong Jia Shi 	int i;
8640a19e61eSDong Jia Shi 
86571189f26SCornelia Huck 	if (!cp->initialized)
86671189f26SCornelia Huck 		return false;
86771189f26SCornelia Huck 
8680a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8690a19e61eSDong Jia Shi 		for (i = 0; i < chain->ch_len; i++)
870e7eaf91bSEric Farman 			if (pfn_array_iova_pinned(chain->ch_pa + i, iova))
8710a19e61eSDong Jia Shi 				return true;
8720a19e61eSDong Jia Shi 	}
8730a19e61eSDong Jia Shi 
8740a19e61eSDong Jia Shi 	return false;
8750a19e61eSDong Jia Shi }
876