xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_cp.c (revision 99afcb05d973f7f74c0c4b8a8c5f6f87c8427aa3)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20a19e61eSDong Jia Shi /*
30a19e61eSDong Jia Shi  * channel program interfaces
40a19e61eSDong Jia Shi  *
50a19e61eSDong Jia Shi  * Copyright IBM Corp. 2017
60a19e61eSDong Jia Shi  *
70a19e61eSDong Jia Shi  * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
80a19e61eSDong Jia Shi  *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
90a19e61eSDong Jia Shi  */
100a19e61eSDong Jia Shi 
110a19e61eSDong Jia Shi #include <linux/mm.h>
120a19e61eSDong Jia Shi #include <linux/slab.h>
130a19e61eSDong Jia Shi #include <linux/iommu.h>
140a19e61eSDong Jia Shi #include <linux/vfio.h>
150a19e61eSDong Jia Shi #include <asm/idals.h>
160a19e61eSDong Jia Shi 
170a19e61eSDong Jia Shi #include "vfio_ccw_cp.h"
180a19e61eSDong Jia Shi 
190a19e61eSDong Jia Shi /*
200a19e61eSDong Jia Shi  * Max length for ccw chain.
210a19e61eSDong Jia Shi  * XXX: Limit to 256, need to check more?
220a19e61eSDong Jia Shi  */
230a19e61eSDong Jia Shi #define CCWCHAIN_LEN_MAX	256
240a19e61eSDong Jia Shi 
250a19e61eSDong Jia Shi struct pfn_array {
2680c57f7aSDong Jia Shi 	/* Starting guest physical I/O address. */
270a19e61eSDong Jia Shi 	unsigned long		pa_iova;
2880c57f7aSDong Jia Shi 	/* Array that stores PFNs of the pages need to pin. */
290a19e61eSDong Jia Shi 	unsigned long		*pa_iova_pfn;
3080c57f7aSDong Jia Shi 	/* Array that receives PFNs of the pages pinned. */
310a19e61eSDong Jia Shi 	unsigned long		*pa_pfn;
325c1cfb1cSDong Jia Shi 	/* Number of pages pinned from @pa_iova. */
330a19e61eSDong Jia Shi 	int			pa_nr;
340a19e61eSDong Jia Shi };
350a19e61eSDong Jia Shi 
360a19e61eSDong Jia Shi struct pfn_array_table {
370a19e61eSDong Jia Shi 	struct pfn_array	*pat_pa;
380a19e61eSDong Jia Shi 	int			pat_nr;
390a19e61eSDong Jia Shi };
400a19e61eSDong Jia Shi 
410a19e61eSDong Jia Shi struct ccwchain {
420a19e61eSDong Jia Shi 	struct list_head	next;
430a19e61eSDong Jia Shi 	struct ccw1		*ch_ccw;
440a19e61eSDong Jia Shi 	/* Guest physical address of the current chain. */
450a19e61eSDong Jia Shi 	u64			ch_iova;
460a19e61eSDong Jia Shi 	/* Count of the valid ccws in chain. */
470a19e61eSDong Jia Shi 	int			ch_len;
480a19e61eSDong Jia Shi 	/* Pinned PAGEs for the original data. */
490a19e61eSDong Jia Shi 	struct pfn_array_table	*ch_pat;
500a19e61eSDong Jia Shi };
510a19e61eSDong Jia Shi 
520a19e61eSDong Jia Shi /*
53e4f3f18bSEric Farman  * pfn_array_alloc() - alloc memory for PFNs
540a19e61eSDong Jia Shi  * @pa: pfn_array on which to perform the operation
555c1cfb1cSDong Jia Shi  * @iova: target guest physical address
565c1cfb1cSDong Jia Shi  * @len: number of bytes that should be pinned from @iova
570a19e61eSDong Jia Shi  *
58e4f3f18bSEric Farman  * Attempt to allocate memory for PFNs.
590a19e61eSDong Jia Shi  *
600a19e61eSDong Jia Shi  * Usage of pfn_array:
615c1cfb1cSDong Jia Shi  * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in
625c1cfb1cSDong Jia Shi  * this structure will be filled in by this function.
630a19e61eSDong Jia Shi  *
640a19e61eSDong Jia Shi  * Returns:
65e4f3f18bSEric Farman  *         0 if PFNs are allocated
66e4f3f18bSEric Farman  *   -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova_pfn is not NULL
67e4f3f18bSEric Farman  *   -ENOMEM if alloc failed
680a19e61eSDong Jia Shi  */
69e4f3f18bSEric Farman static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len)
700a19e61eSDong Jia Shi {
71e4f3f18bSEric Farman 	int i;
720a19e61eSDong Jia Shi 
735c1cfb1cSDong Jia Shi 	if (pa->pa_nr || pa->pa_iova_pfn)
740a19e61eSDong Jia Shi 		return -EINVAL;
750a19e61eSDong Jia Shi 
760a19e61eSDong Jia Shi 	pa->pa_iova = iova;
770a19e61eSDong Jia Shi 
780a19e61eSDong Jia Shi 	pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
790a19e61eSDong Jia Shi 	if (!pa->pa_nr)
800a19e61eSDong Jia Shi 		return -EINVAL;
810a19e61eSDong Jia Shi 
820a19e61eSDong Jia Shi 	pa->pa_iova_pfn = kcalloc(pa->pa_nr,
830a19e61eSDong Jia Shi 				  sizeof(*pa->pa_iova_pfn) +
840a19e61eSDong Jia Shi 				  sizeof(*pa->pa_pfn),
850a19e61eSDong Jia Shi 				  GFP_KERNEL);
860a19e61eSDong Jia Shi 	if (unlikely(!pa->pa_iova_pfn))
870a19e61eSDong Jia Shi 		return -ENOMEM;
880a19e61eSDong Jia Shi 	pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
890a19e61eSDong Jia Shi 
905c1cfb1cSDong Jia Shi 	pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
91c34a12e6SEric Farman 	pa->pa_pfn[0] = -1ULL;
92c34a12e6SEric Farman 	for (i = 1; i < pa->pa_nr; i++) {
935c1cfb1cSDong Jia Shi 		pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
94c34a12e6SEric Farman 		pa->pa_pfn[i] = -1ULL;
95c34a12e6SEric Farman 	}
960a19e61eSDong Jia Shi 
97e4f3f18bSEric Farman 	return 0;
98e4f3f18bSEric Farman }
99e4f3f18bSEric Farman 
100e4f3f18bSEric Farman /*
101e4f3f18bSEric Farman  * pfn_array_pin() - Pin user pages in memory
102e4f3f18bSEric Farman  * @pa: pfn_array on which to perform the operation
103e4f3f18bSEric Farman  * @mdev: the mediated device to perform pin operations
104e4f3f18bSEric Farman  *
105e4f3f18bSEric Farman  * Returns number of pages pinned upon success.
106e4f3f18bSEric Farman  * If the pin request partially succeeds, or fails completely,
107e4f3f18bSEric Farman  * all pages are left unpinned and a negative error value is returned.
108e4f3f18bSEric Farman  */
109e4f3f18bSEric Farman static int pfn_array_pin(struct pfn_array *pa, struct device *mdev)
110e4f3f18bSEric Farman {
111e4f3f18bSEric Farman 	int ret = 0;
112e4f3f18bSEric Farman 
1135c1cfb1cSDong Jia Shi 	ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
1145c1cfb1cSDong Jia Shi 			     IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
1155c1cfb1cSDong Jia Shi 
1165c1cfb1cSDong Jia Shi 	if (ret < 0) {
1175c1cfb1cSDong Jia Shi 		goto err_out;
1185c1cfb1cSDong Jia Shi 	} else if (ret > 0 && ret != pa->pa_nr) {
1195c1cfb1cSDong Jia Shi 		vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret);
1200a19e61eSDong Jia Shi 		ret = -EINVAL;
1215c1cfb1cSDong Jia Shi 		goto err_out;
1225c1cfb1cSDong Jia Shi 	}
1230a19e61eSDong Jia Shi 
1240a19e61eSDong Jia Shi 	return ret;
1255c1cfb1cSDong Jia Shi 
1265c1cfb1cSDong Jia Shi err_out:
1275c1cfb1cSDong Jia Shi 	pa->pa_nr = 0;
1285c1cfb1cSDong Jia Shi 
1295c1cfb1cSDong Jia Shi 	return ret;
1305c1cfb1cSDong Jia Shi }
1315c1cfb1cSDong Jia Shi 
1325c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */
1335c1cfb1cSDong Jia Shi static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
1345c1cfb1cSDong Jia Shi {
135e4f3f18bSEric Farman 	/* Only unpin if any pages were pinned to begin with */
136e4f3f18bSEric Farman 	if (pa->pa_nr)
1375c1cfb1cSDong Jia Shi 		vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
1385c1cfb1cSDong Jia Shi 	pa->pa_nr = 0;
1395c1cfb1cSDong Jia Shi 	kfree(pa->pa_iova_pfn);
1400a19e61eSDong Jia Shi }
1410a19e61eSDong Jia Shi 
1420a19e61eSDong Jia Shi static int pfn_array_table_init(struct pfn_array_table *pat, int nr)
1430a19e61eSDong Jia Shi {
1440a19e61eSDong Jia Shi 	pat->pat_pa = kcalloc(nr, sizeof(*pat->pat_pa), GFP_KERNEL);
1450a19e61eSDong Jia Shi 	if (unlikely(ZERO_OR_NULL_PTR(pat->pat_pa))) {
1460a19e61eSDong Jia Shi 		pat->pat_nr = 0;
1470a19e61eSDong Jia Shi 		return -ENOMEM;
1480a19e61eSDong Jia Shi 	}
1490a19e61eSDong Jia Shi 
1500a19e61eSDong Jia Shi 	pat->pat_nr = nr;
1510a19e61eSDong Jia Shi 
1520a19e61eSDong Jia Shi 	return 0;
1530a19e61eSDong Jia Shi }
1540a19e61eSDong Jia Shi 
1550a19e61eSDong Jia Shi static void pfn_array_table_unpin_free(struct pfn_array_table *pat,
1560a19e61eSDong Jia Shi 				       struct device *mdev)
1570a19e61eSDong Jia Shi {
1580a19e61eSDong Jia Shi 	int i;
1590a19e61eSDong Jia Shi 
1600a19e61eSDong Jia Shi 	for (i = 0; i < pat->pat_nr; i++)
1610a19e61eSDong Jia Shi 		pfn_array_unpin_free(pat->pat_pa + i, mdev);
1620a19e61eSDong Jia Shi 
1630a19e61eSDong Jia Shi 	if (pat->pat_nr) {
1640a19e61eSDong Jia Shi 		kfree(pat->pat_pa);
1650a19e61eSDong Jia Shi 		pat->pat_pa = NULL;
1660a19e61eSDong Jia Shi 		pat->pat_nr = 0;
1670a19e61eSDong Jia Shi 	}
1680a19e61eSDong Jia Shi }
1690a19e61eSDong Jia Shi 
1700a19e61eSDong Jia Shi static bool pfn_array_table_iova_pinned(struct pfn_array_table *pat,
1710a19e61eSDong Jia Shi 					unsigned long iova)
1720a19e61eSDong Jia Shi {
1730a19e61eSDong Jia Shi 	struct pfn_array *pa = pat->pat_pa;
1740a19e61eSDong Jia Shi 	unsigned long iova_pfn = iova >> PAGE_SHIFT;
1750a19e61eSDong Jia Shi 	int i, j;
1760a19e61eSDong Jia Shi 
1770a19e61eSDong Jia Shi 	for (i = 0; i < pat->pat_nr; i++, pa++)
1780a19e61eSDong Jia Shi 		for (j = 0; j < pa->pa_nr; j++)
17924abf290SEric Farman 			if (pa->pa_iova_pfn[j] == iova_pfn)
1800a19e61eSDong Jia Shi 				return true;
1810a19e61eSDong Jia Shi 
1820a19e61eSDong Jia Shi 	return false;
1830a19e61eSDong Jia Shi }
1840a19e61eSDong Jia Shi /* Create the list idal words for a pfn_array_table. */
1850a19e61eSDong Jia Shi static inline void pfn_array_table_idal_create_words(
1860a19e61eSDong Jia Shi 	struct pfn_array_table *pat,
1870a19e61eSDong Jia Shi 	unsigned long *idaws)
1880a19e61eSDong Jia Shi {
1890a19e61eSDong Jia Shi 	struct pfn_array *pa;
1900a19e61eSDong Jia Shi 	int i, j, k;
1910a19e61eSDong Jia Shi 
1920a19e61eSDong Jia Shi 	/*
1930a19e61eSDong Jia Shi 	 * Idal words (execept the first one) rely on the memory being 4k
1940a19e61eSDong Jia Shi 	 * aligned. If a user virtual address is 4K aligned, then it's
1950a19e61eSDong Jia Shi 	 * corresponding kernel physical address will also be 4K aligned. Thus
1960a19e61eSDong Jia Shi 	 * there will be no problem here to simply use the phys to create an
1970a19e61eSDong Jia Shi 	 * idaw.
1980a19e61eSDong Jia Shi 	 */
1990a19e61eSDong Jia Shi 	k = 0;
2000a19e61eSDong Jia Shi 	for (i = 0; i < pat->pat_nr; i++) {
2010a19e61eSDong Jia Shi 		pa = pat->pat_pa + i;
2020a19e61eSDong Jia Shi 		for (j = 0; j < pa->pa_nr; j++) {
2030a19e61eSDong Jia Shi 			idaws[k] = pa->pa_pfn[j] << PAGE_SHIFT;
2040a19e61eSDong Jia Shi 			if (k == 0)
2050a19e61eSDong Jia Shi 				idaws[k] += pa->pa_iova & (PAGE_SIZE - 1);
2060a19e61eSDong Jia Shi 			k++;
2070a19e61eSDong Jia Shi 		}
2080a19e61eSDong Jia Shi 	}
2090a19e61eSDong Jia Shi }
2100a19e61eSDong Jia Shi 
2110a19e61eSDong Jia Shi 
2120a19e61eSDong Jia Shi /*
2130a19e61eSDong Jia Shi  * Within the domain (@mdev), copy @n bytes from a guest physical
2140a19e61eSDong Jia Shi  * address (@iova) to a host physical address (@to).
2150a19e61eSDong Jia Shi  */
2160a19e61eSDong Jia Shi static long copy_from_iova(struct device *mdev,
2170a19e61eSDong Jia Shi 			   void *to, u64 iova,
2180a19e61eSDong Jia Shi 			   unsigned long n)
2190a19e61eSDong Jia Shi {
2200a19e61eSDong Jia Shi 	struct pfn_array pa = {0};
2210a19e61eSDong Jia Shi 	u64 from;
2220a19e61eSDong Jia Shi 	int i, ret;
2230a19e61eSDong Jia Shi 	unsigned long l, m;
2240a19e61eSDong Jia Shi 
225e4f3f18bSEric Farman 	ret = pfn_array_alloc(&pa, iova, n);
226e4f3f18bSEric Farman 	if (ret < 0)
2270a19e61eSDong Jia Shi 		return ret;
2280a19e61eSDong Jia Shi 
229e4f3f18bSEric Farman 	ret = pfn_array_pin(&pa, mdev);
230e4f3f18bSEric Farman 	if (ret < 0) {
231e4f3f18bSEric Farman 		pfn_array_unpin_free(&pa, mdev);
232e4f3f18bSEric Farman 		return ret;
233e4f3f18bSEric Farman 	}
234e4f3f18bSEric Farman 
2350a19e61eSDong Jia Shi 	l = n;
2360a19e61eSDong Jia Shi 	for (i = 0; i < pa.pa_nr; i++) {
2370a19e61eSDong Jia Shi 		from = pa.pa_pfn[i] << PAGE_SHIFT;
2380a19e61eSDong Jia Shi 		m = PAGE_SIZE;
2390a19e61eSDong Jia Shi 		if (i == 0) {
2400a19e61eSDong Jia Shi 			from += iova & (PAGE_SIZE - 1);
2410a19e61eSDong Jia Shi 			m -= iova & (PAGE_SIZE - 1);
2420a19e61eSDong Jia Shi 		}
2430a19e61eSDong Jia Shi 
2440a19e61eSDong Jia Shi 		m = min(l, m);
2450a19e61eSDong Jia Shi 		memcpy(to + (n - l), (void *)from, m);
2460a19e61eSDong Jia Shi 
2470a19e61eSDong Jia Shi 		l -= m;
2480a19e61eSDong Jia Shi 		if (l == 0)
2490a19e61eSDong Jia Shi 			break;
2500a19e61eSDong Jia Shi 	}
2510a19e61eSDong Jia Shi 
2520a19e61eSDong Jia Shi 	pfn_array_unpin_free(&pa, mdev);
2530a19e61eSDong Jia Shi 
2540a19e61eSDong Jia Shi 	return l;
2550a19e61eSDong Jia Shi }
2560a19e61eSDong Jia Shi 
2570a19e61eSDong Jia Shi static long copy_ccw_from_iova(struct channel_program *cp,
2580a19e61eSDong Jia Shi 			       struct ccw1 *to, u64 iova,
2590a19e61eSDong Jia Shi 			       unsigned long len)
2600a19e61eSDong Jia Shi {
261d686f21aSDong Jia Shi 	struct ccw0 ccw0;
262d686f21aSDong Jia Shi 	struct ccw1 *pccw1;
263d686f21aSDong Jia Shi 	int ret;
264d686f21aSDong Jia Shi 	int i;
265d686f21aSDong Jia Shi 
266d686f21aSDong Jia Shi 	ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1));
267d686f21aSDong Jia Shi 	if (ret)
268d686f21aSDong Jia Shi 		return ret;
269d686f21aSDong Jia Shi 
270d686f21aSDong Jia Shi 	if (!cp->orb.cmd.fmt) {
271d686f21aSDong Jia Shi 		pccw1 = to;
272d686f21aSDong Jia Shi 		for (i = 0; i < len; i++) {
273d686f21aSDong Jia Shi 			ccw0 = *(struct ccw0 *)pccw1;
274d686f21aSDong Jia Shi 			if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
275d686f21aSDong Jia Shi 				pccw1->cmd_code = CCW_CMD_TIC;
276d686f21aSDong Jia Shi 				pccw1->flags = 0;
277d686f21aSDong Jia Shi 				pccw1->count = 0;
278d686f21aSDong Jia Shi 			} else {
279d686f21aSDong Jia Shi 				pccw1->cmd_code = ccw0.cmd_code;
280d686f21aSDong Jia Shi 				pccw1->flags = ccw0.flags;
281d686f21aSDong Jia Shi 				pccw1->count = ccw0.count;
282d686f21aSDong Jia Shi 			}
283d686f21aSDong Jia Shi 			pccw1->cda = ccw0.cda;
284d686f21aSDong Jia Shi 			pccw1++;
285d686f21aSDong Jia Shi 		}
286d686f21aSDong Jia Shi 	}
287d686f21aSDong Jia Shi 
288d686f21aSDong Jia Shi 	return ret;
2890a19e61eSDong Jia Shi }
2900a19e61eSDong Jia Shi 
2910a19e61eSDong Jia Shi /*
2920a19e61eSDong Jia Shi  * Helpers to operate ccwchain.
2930a19e61eSDong Jia Shi  */
2945d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
2955d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
2965d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
2975d87fbf7SEric Farman 
2980a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
2990a19e61eSDong Jia Shi 
3000a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
3010a19e61eSDong Jia Shi 
3020a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
3035d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
3040a19e61eSDong Jia Shi 
3050a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
3060a19e61eSDong Jia Shi 
30748bd0eeeSEric Farman /*
3085d87fbf7SEric Farman  * ccw_does_data_transfer()
3095d87fbf7SEric Farman  *
3105d87fbf7SEric Farman  * Determine whether a CCW will move any data, such that the guest pages
3115d87fbf7SEric Farman  * would need to be pinned before performing the I/O.
3125d87fbf7SEric Farman  *
3135d87fbf7SEric Farman  * Returns 1 if yes, 0 if no.
3145d87fbf7SEric Farman  */
3155d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw)
3165d87fbf7SEric Farman {
317453eac31SEric Farman 	/* If the count field is zero, then no data will be transferred */
318453eac31SEric Farman 	if (ccw->count == 0)
319453eac31SEric Farman 		return 0;
320453eac31SEric Farman 
3219b6e57e5SEric Farman 	/* If the command is a NOP, then no data will be transferred */
3229b6e57e5SEric Farman 	if (ccw_is_noop(ccw))
3239b6e57e5SEric Farman 		return 0;
3249b6e57e5SEric Farman 
3255d87fbf7SEric Farman 	/* If the skip flag is off, then data will be transferred */
3265d87fbf7SEric Farman 	if (!ccw_is_skip(ccw))
3275d87fbf7SEric Farman 		return 1;
3285d87fbf7SEric Farman 
3295d87fbf7SEric Farman 	/*
3305d87fbf7SEric Farman 	 * If the skip flag is on, it is only meaningful if the command
3315d87fbf7SEric Farman 	 * code is a read, read backward, sense, or sense ID.  In those
3325d87fbf7SEric Farman 	 * cases, no data will be transferred.
3335d87fbf7SEric Farman 	 */
3345d87fbf7SEric Farman 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
3355d87fbf7SEric Farman 		return 0;
3365d87fbf7SEric Farman 
3375d87fbf7SEric Farman 	if (ccw_is_sense(ccw))
3385d87fbf7SEric Farman 		return 0;
3395d87fbf7SEric Farman 
3405d87fbf7SEric Farman 	/* The skip flag is on, but it is ignored for this command code. */
3415d87fbf7SEric Farman 	return 1;
3425d87fbf7SEric Farman }
3435d87fbf7SEric Farman 
3445d87fbf7SEric Farman /*
34548bd0eeeSEric Farman  * is_cpa_within_range()
34648bd0eeeSEric Farman  *
34748bd0eeeSEric Farman  * @cpa: channel program address being questioned
34848bd0eeeSEric Farman  * @head: address of the beginning of a CCW chain
34948bd0eeeSEric Farman  * @len: number of CCWs within the chain
35048bd0eeeSEric Farman  *
35148bd0eeeSEric Farman  * Determine whether the address of a CCW (whether a new chain,
35248bd0eeeSEric Farman  * or the target of a TIC) falls within a range (including the end points).
35348bd0eeeSEric Farman  *
35448bd0eeeSEric Farman  * Returns 1 if yes, 0 if no.
35548bd0eeeSEric Farman  */
35648bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
35748bd0eeeSEric Farman {
35848bd0eeeSEric Farman 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
35948bd0eeeSEric Farman 
36048bd0eeeSEric Farman 	return (head <= cpa && cpa <= tail);
36148bd0eeeSEric Farman }
36248bd0eeeSEric Farman 
36348bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
36448bd0eeeSEric Farman {
36548bd0eeeSEric Farman 	if (!ccw_is_tic(ccw))
36648bd0eeeSEric Farman 		return 0;
36748bd0eeeSEric Farman 
36848bd0eeeSEric Farman 	return is_cpa_within_range(ccw->cda, head, len);
36948bd0eeeSEric Farman }
37048bd0eeeSEric Farman 
3710a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
3720a19e61eSDong Jia Shi {
3730a19e61eSDong Jia Shi 	struct ccwchain *chain;
3740a19e61eSDong Jia Shi 	void *data;
3750a19e61eSDong Jia Shi 	size_t size;
3760a19e61eSDong Jia Shi 
3770a19e61eSDong Jia Shi 	/* Make ccw address aligned to 8. */
3780a19e61eSDong Jia Shi 	size = ((sizeof(*chain) + 7L) & -8L) +
3790a19e61eSDong Jia Shi 		sizeof(*chain->ch_ccw) * len +
3800a19e61eSDong Jia Shi 		sizeof(*chain->ch_pat) * len;
3810a19e61eSDong Jia Shi 	chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
3820a19e61eSDong Jia Shi 	if (!chain)
3830a19e61eSDong Jia Shi 		return NULL;
3840a19e61eSDong Jia Shi 
3850a19e61eSDong Jia Shi 	data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
3860a19e61eSDong Jia Shi 	chain->ch_ccw = (struct ccw1 *)data;
3870a19e61eSDong Jia Shi 
3880a19e61eSDong Jia Shi 	data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
3890a19e61eSDong Jia Shi 	chain->ch_pat = (struct pfn_array_table *)data;
3900a19e61eSDong Jia Shi 
3910a19e61eSDong Jia Shi 	chain->ch_len = len;
3920a19e61eSDong Jia Shi 
3930a19e61eSDong Jia Shi 	list_add_tail(&chain->next, &cp->ccwchain_list);
3940a19e61eSDong Jia Shi 
3950a19e61eSDong Jia Shi 	return chain;
3960a19e61eSDong Jia Shi }
3970a19e61eSDong Jia Shi 
3980a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain)
3990a19e61eSDong Jia Shi {
4000a19e61eSDong Jia Shi 	list_del(&chain->next);
4010a19e61eSDong Jia Shi 	kfree(chain);
4020a19e61eSDong Jia Shi }
4030a19e61eSDong Jia Shi 
4040a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */
4050a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx)
4060a19e61eSDong Jia Shi {
4070a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
4080a19e61eSDong Jia Shi 
4099b6e57e5SEric Farman 	if (ccw_is_tic(ccw))
410408358b5SJason J. Herne 		return;
4110a19e61eSDong Jia Shi 
4120a19e61eSDong Jia Shi 	kfree((void *)(u64)ccw->cda);
4130a19e61eSDong Jia Shi }
4140a19e61eSDong Jia Shi 
4150a19e61eSDong Jia Shi /**
4160a19e61eSDong Jia Shi  * ccwchain_calc_length - calculate the length of the ccw chain.
4170a19e61eSDong Jia Shi  * @iova: guest physical address of the target ccw chain
4180a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
4190a19e61eSDong Jia Shi  *
4200a19e61eSDong Jia Shi  * This is the chain length not considering any TICs.
4210a19e61eSDong Jia Shi  * You need to do a new round for each TIC target.
4220a19e61eSDong Jia Shi  *
423fb9e7880SHalil Pasic  * The program is also validated for absence of not yet supported
424fb9e7880SHalil Pasic  * indirect data addressing scenarios.
425fb9e7880SHalil Pasic  *
4260a19e61eSDong Jia Shi  * Returns: the length of the ccw chain or -errno.
4270a19e61eSDong Jia Shi  */
4280a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
4290a19e61eSDong Jia Shi {
4300a19e61eSDong Jia Shi 	struct ccw1 *ccw, *p;
4310a19e61eSDong Jia Shi 	int cnt;
4320a19e61eSDong Jia Shi 
4330a19e61eSDong Jia Shi 	/*
4340a19e61eSDong Jia Shi 	 * Copy current chain from guest to host kernel.
4350a19e61eSDong Jia Shi 	 * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256).
4360a19e61eSDong Jia Shi 	 * So copying 2K is enough (safe).
4370a19e61eSDong Jia Shi 	 */
4380a19e61eSDong Jia Shi 	p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL);
4390a19e61eSDong Jia Shi 	if (!ccw)
4400a19e61eSDong Jia Shi 		return -ENOMEM;
4410a19e61eSDong Jia Shi 
4420a19e61eSDong Jia Shi 	cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX);
4430a19e61eSDong Jia Shi 	if (cnt) {
4440a19e61eSDong Jia Shi 		kfree(ccw);
4450a19e61eSDong Jia Shi 		return cnt;
4460a19e61eSDong Jia Shi 	}
4470a19e61eSDong Jia Shi 
4480a19e61eSDong Jia Shi 	cnt = 0;
4490a19e61eSDong Jia Shi 	do {
4500a19e61eSDong Jia Shi 		cnt++;
4510a19e61eSDong Jia Shi 
452fb9e7880SHalil Pasic 		/*
453fb9e7880SHalil Pasic 		 * As we don't want to fail direct addressing even if the
454fb9e7880SHalil Pasic 		 * orb specified one of the unsupported formats, we defer
455fb9e7880SHalil Pasic 		 * checking for IDAWs in unsupported formats to here.
456fb9e7880SHalil Pasic 		 */
457b89e242eSEric Farman 		if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw)) {
458b89e242eSEric Farman 			kfree(p);
459fb9e7880SHalil Pasic 			return -EOPNOTSUPP;
460b89e242eSEric Farman 		}
461fb9e7880SHalil Pasic 
46248bd0eeeSEric Farman 		/*
46348bd0eeeSEric Farman 		 * We want to keep counting if the current CCW has the
46448bd0eeeSEric Farman 		 * command-chaining flag enabled, or if it is a TIC CCW
46548bd0eeeSEric Farman 		 * that loops back into the current chain.  The latter
46648bd0eeeSEric Farman 		 * is used for device orientation, where the CCW PRIOR to
46748bd0eeeSEric Farman 		 * the TIC can either jump to the TIC or a CCW immediately
46848bd0eeeSEric Farman 		 * after the TIC, depending on the results of its operation.
46948bd0eeeSEric Farman 		 */
47048bd0eeeSEric Farman 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
4710a19e61eSDong Jia Shi 			break;
4720a19e61eSDong Jia Shi 
4730a19e61eSDong Jia Shi 		ccw++;
4740a19e61eSDong Jia Shi 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
4750a19e61eSDong Jia Shi 
4760a19e61eSDong Jia Shi 	if (cnt == CCWCHAIN_LEN_MAX + 1)
4770a19e61eSDong Jia Shi 		cnt = -EINVAL;
4780a19e61eSDong Jia Shi 
4790a19e61eSDong Jia Shi 	kfree(p);
4800a19e61eSDong Jia Shi 	return cnt;
4810a19e61eSDong Jia Shi }
4820a19e61eSDong Jia Shi 
4830a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
4840a19e61eSDong Jia Shi {
4850a19e61eSDong Jia Shi 	struct ccwchain *chain;
4862904337fSEric Farman 	u32 ccw_head;
4870a19e61eSDong Jia Shi 
4880a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
4890a19e61eSDong Jia Shi 		ccw_head = chain->ch_iova;
4902904337fSEric Farman 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
4910a19e61eSDong Jia Shi 			return 1;
4920a19e61eSDong Jia Shi 	}
4930a19e61eSDong Jia Shi 
4940a19e61eSDong Jia Shi 	return 0;
4950a19e61eSDong Jia Shi }
4960a19e61eSDong Jia Shi 
4970a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain,
4980a19e61eSDong Jia Shi 			     struct channel_program *cp);
4990a19e61eSDong Jia Shi 
500363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
5010a19e61eSDong Jia Shi {
5020a19e61eSDong Jia Shi 	struct ccwchain *chain;
5030a19e61eSDong Jia Shi 	int len, ret;
5040a19e61eSDong Jia Shi 
5050a19e61eSDong Jia Shi 	/* Get chain length. */
506363fe5f7SEric Farman 	len = ccwchain_calc_length(cda, cp);
5070a19e61eSDong Jia Shi 	if (len < 0)
5080a19e61eSDong Jia Shi 		return len;
5090a19e61eSDong Jia Shi 
5100a19e61eSDong Jia Shi 	/* Need alloc a new chain for this one. */
5110a19e61eSDong Jia Shi 	chain = ccwchain_alloc(cp, len);
5120a19e61eSDong Jia Shi 	if (!chain)
5130a19e61eSDong Jia Shi 		return -ENOMEM;
514363fe5f7SEric Farman 	chain->ch_iova = cda;
5150a19e61eSDong Jia Shi 
5160a19e61eSDong Jia Shi 	/* Copy the new chain from user. */
517363fe5f7SEric Farman 	ret = copy_ccw_from_iova(cp, chain->ch_ccw, cda, len);
5180a19e61eSDong Jia Shi 	if (ret) {
5190a19e61eSDong Jia Shi 		ccwchain_free(chain);
5200a19e61eSDong Jia Shi 		return ret;
5210a19e61eSDong Jia Shi 	}
5220a19e61eSDong Jia Shi 
5230a19e61eSDong Jia Shi 	/* Loop for tics on this new chain. */
5240a19e61eSDong Jia Shi 	return ccwchain_loop_tic(chain, cp);
5250a19e61eSDong Jia Shi }
5260a19e61eSDong Jia Shi 
5270a19e61eSDong Jia Shi /* Loop for TICs. */
5280a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
5290a19e61eSDong Jia Shi {
5300a19e61eSDong Jia Shi 	struct ccw1 *tic;
5310a19e61eSDong Jia Shi 	int i, ret;
5320a19e61eSDong Jia Shi 
5330a19e61eSDong Jia Shi 	for (i = 0; i < chain->ch_len; i++) {
5340a19e61eSDong Jia Shi 		tic = chain->ch_ccw + i;
5350a19e61eSDong Jia Shi 
5360a19e61eSDong Jia Shi 		if (!ccw_is_tic(tic))
5370a19e61eSDong Jia Shi 			continue;
5380a19e61eSDong Jia Shi 
539e64bd689SEric Farman 		/* May transfer to an existing chain. */
540e64bd689SEric Farman 		if (tic_target_chain_exists(tic, cp))
541e64bd689SEric Farman 			continue;
542e64bd689SEric Farman 
543363fe5f7SEric Farman 		/* Build a ccwchain for the next segment */
544363fe5f7SEric Farman 		ret = ccwchain_handle_ccw(tic->cda, cp);
5450a19e61eSDong Jia Shi 		if (ret)
5460a19e61eSDong Jia Shi 			return ret;
5470a19e61eSDong Jia Shi 	}
5480a19e61eSDong Jia Shi 
5490a19e61eSDong Jia Shi 	return 0;
5500a19e61eSDong Jia Shi }
5510a19e61eSDong Jia Shi 
5520a19e61eSDong Jia Shi static int ccwchain_fetch_tic(struct ccwchain *chain,
5530a19e61eSDong Jia Shi 			      int idx,
5540a19e61eSDong Jia Shi 			      struct channel_program *cp)
5550a19e61eSDong Jia Shi {
5560a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
5570a19e61eSDong Jia Shi 	struct ccwchain *iter;
5582904337fSEric Farman 	u32 ccw_head;
5590a19e61eSDong Jia Shi 
5600a19e61eSDong Jia Shi 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
5610a19e61eSDong Jia Shi 		ccw_head = iter->ch_iova;
5622904337fSEric Farman 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
563c389377cSJason J. Herne 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
5640a19e61eSDong Jia Shi 						     (ccw->cda - ccw_head));
5650a19e61eSDong Jia Shi 			return 0;
5660a19e61eSDong Jia Shi 		}
5670a19e61eSDong Jia Shi 	}
5680a19e61eSDong Jia Shi 
5690a19e61eSDong Jia Shi 	return -EFAULT;
5700a19e61eSDong Jia Shi }
5710a19e61eSDong Jia Shi 
5720a19e61eSDong Jia Shi static int ccwchain_fetch_direct(struct ccwchain *chain,
5730a19e61eSDong Jia Shi 				 int idx,
5740a19e61eSDong Jia Shi 				 struct channel_program *cp)
5750a19e61eSDong Jia Shi {
5760a19e61eSDong Jia Shi 	struct ccw1 *ccw;
5770a19e61eSDong Jia Shi 	struct pfn_array_table *pat;
5780a19e61eSDong Jia Shi 	unsigned long *idaws;
5796238f921SDong Jia Shi 	int ret;
580453eac31SEric Farman 	int bytes = 1;
5815d87fbf7SEric Farman 	int idaw_nr = 1;
5820a19e61eSDong Jia Shi 
5830a19e61eSDong Jia Shi 	ccw = chain->ch_ccw + idx;
5840a19e61eSDong Jia Shi 
585453eac31SEric Farman 	if (ccw->count) {
586453eac31SEric Farman 		bytes = ccw->count;
5875d87fbf7SEric Farman 		idaw_nr = idal_nr_words((void *)(u64)ccw->cda, ccw->count);
5884cebc5d6SDong Jia Shi 	}
5894cebc5d6SDong Jia Shi 
5900a19e61eSDong Jia Shi 	/*
5910a19e61eSDong Jia Shi 	 * Pin data page(s) in memory.
5920a19e61eSDong Jia Shi 	 * The number of pages actually is the count of the idaws which will be
5930a19e61eSDong Jia Shi 	 * needed when translating a direct ccw to a idal ccw.
5940a19e61eSDong Jia Shi 	 */
5950a19e61eSDong Jia Shi 	pat = chain->ch_pat + idx;
5966238f921SDong Jia Shi 	ret = pfn_array_table_init(pat, 1);
5976238f921SDong Jia Shi 	if (ret)
5986238f921SDong Jia Shi 		goto out_init;
5996238f921SDong Jia Shi 
600453eac31SEric Farman 	ret = pfn_array_alloc(pat->pat_pa, ccw->cda, bytes);
601e4f3f18bSEric Farman 	if (ret < 0)
602e4f3f18bSEric Farman 		goto out_unpin;
603e4f3f18bSEric Farman 
6045d87fbf7SEric Farman 	if (ccw_does_data_transfer(ccw)) {
605e4f3f18bSEric Farman 		ret = pfn_array_pin(pat->pat_pa, cp->mdev);
6066238f921SDong Jia Shi 		if (ret < 0)
607806212f9SEric Farman 			goto out_unpin;
6085d87fbf7SEric Farman 	} else {
6095d87fbf7SEric Farman 		pat->pat_pa->pa_nr = 0;
6105d87fbf7SEric Farman 	}
6110a19e61eSDong Jia Shi 
6120a19e61eSDong Jia Shi 	/* Translate this direct ccw to a idal ccw. */
6135d87fbf7SEric Farman 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
6140a19e61eSDong Jia Shi 	if (!idaws) {
6156238f921SDong Jia Shi 		ret = -ENOMEM;
6166238f921SDong Jia Shi 		goto out_unpin;
6170a19e61eSDong Jia Shi 	}
6180a19e61eSDong Jia Shi 	ccw->cda = (__u32) virt_to_phys(idaws);
6190a19e61eSDong Jia Shi 	ccw->flags |= CCW_FLAG_IDA;
6200a19e61eSDong Jia Shi 
6210a19e61eSDong Jia Shi 	pfn_array_table_idal_create_words(pat, idaws);
6220a19e61eSDong Jia Shi 
6230a19e61eSDong Jia Shi 	return 0;
6246238f921SDong Jia Shi 
6256238f921SDong Jia Shi out_unpin:
6266238f921SDong Jia Shi 	pfn_array_table_unpin_free(pat, cp->mdev);
6276238f921SDong Jia Shi out_init:
6286238f921SDong Jia Shi 	ccw->cda = 0;
6296238f921SDong Jia Shi 	return ret;
6300a19e61eSDong Jia Shi }
6310a19e61eSDong Jia Shi 
6320a19e61eSDong Jia Shi static int ccwchain_fetch_idal(struct ccwchain *chain,
6330a19e61eSDong Jia Shi 			       int idx,
6340a19e61eSDong Jia Shi 			       struct channel_program *cp)
6350a19e61eSDong Jia Shi {
6360a19e61eSDong Jia Shi 	struct ccw1 *ccw;
6370a19e61eSDong Jia Shi 	struct pfn_array_table *pat;
638e4f3f18bSEric Farman 	struct pfn_array *pa;
6390a19e61eSDong Jia Shi 	unsigned long *idaws;
6400a19e61eSDong Jia Shi 	u64 idaw_iova;
6410a19e61eSDong Jia Shi 	unsigned int idaw_nr, idaw_len;
6420a19e61eSDong Jia Shi 	int i, ret;
643453eac31SEric Farman 	int bytes = 1;
6440a19e61eSDong Jia Shi 
6450a19e61eSDong Jia Shi 	ccw = chain->ch_ccw + idx;
6460a19e61eSDong Jia Shi 
647453eac31SEric Farman 	if (ccw->count)
648453eac31SEric Farman 		bytes = ccw->count;
6494cebc5d6SDong Jia Shi 
6500a19e61eSDong Jia Shi 	/* Calculate size of idaws. */
6510a19e61eSDong Jia Shi 	ret = copy_from_iova(cp->mdev, &idaw_iova, ccw->cda, sizeof(idaw_iova));
6520a19e61eSDong Jia Shi 	if (ret)
6530a19e61eSDong Jia Shi 		return ret;
654453eac31SEric Farman 	idaw_nr = idal_nr_words((void *)(idaw_iova), bytes);
6550a19e61eSDong Jia Shi 	idaw_len = idaw_nr * sizeof(*idaws);
6560a19e61eSDong Jia Shi 
6570a19e61eSDong Jia Shi 	/* Pin data page(s) in memory. */
6580a19e61eSDong Jia Shi 	pat = chain->ch_pat + idx;
6590a19e61eSDong Jia Shi 	ret = pfn_array_table_init(pat, idaw_nr);
6600a19e61eSDong Jia Shi 	if (ret)
6616238f921SDong Jia Shi 		goto out_init;
6620a19e61eSDong Jia Shi 
6630a19e61eSDong Jia Shi 	/* Translate idal ccw to use new allocated idaws. */
6640a19e61eSDong Jia Shi 	idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL);
6650a19e61eSDong Jia Shi 	if (!idaws) {
6660a19e61eSDong Jia Shi 		ret = -ENOMEM;
6670a19e61eSDong Jia Shi 		goto out_unpin;
6680a19e61eSDong Jia Shi 	}
6690a19e61eSDong Jia Shi 
6700a19e61eSDong Jia Shi 	ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idaw_len);
6710a19e61eSDong Jia Shi 	if (ret)
6720a19e61eSDong Jia Shi 		goto out_free_idaws;
6730a19e61eSDong Jia Shi 
6740a19e61eSDong Jia Shi 	ccw->cda = virt_to_phys(idaws);
6750a19e61eSDong Jia Shi 
6760a19e61eSDong Jia Shi 	for (i = 0; i < idaw_nr; i++) {
6770a19e61eSDong Jia Shi 		idaw_iova = *(idaws + i);
678e4f3f18bSEric Farman 		pa = pat->pat_pa + i;
6790a19e61eSDong Jia Shi 
680e4f3f18bSEric Farman 		ret = pfn_array_alloc(pa, idaw_iova, 1);
681e4f3f18bSEric Farman 		if (ret < 0)
682e4f3f18bSEric Farman 			goto out_free_idaws;
683e4f3f18bSEric Farman 
6845d87fbf7SEric Farman 		if (!ccw_does_data_transfer(ccw)) {
6855d87fbf7SEric Farman 			pa->pa_nr = 0;
6865d87fbf7SEric Farman 			continue;
6875d87fbf7SEric Farman 		}
6885d87fbf7SEric Farman 
689e4f3f18bSEric Farman 		ret = pfn_array_pin(pa, cp->mdev);
6900a19e61eSDong Jia Shi 		if (ret < 0)
6910a19e61eSDong Jia Shi 			goto out_free_idaws;
6920a19e61eSDong Jia Shi 	}
6930a19e61eSDong Jia Shi 
6940a19e61eSDong Jia Shi 	pfn_array_table_idal_create_words(pat, idaws);
6950a19e61eSDong Jia Shi 
6960a19e61eSDong Jia Shi 	return 0;
6970a19e61eSDong Jia Shi 
6980a19e61eSDong Jia Shi out_free_idaws:
6990a19e61eSDong Jia Shi 	kfree(idaws);
7000a19e61eSDong Jia Shi out_unpin:
7010a19e61eSDong Jia Shi 	pfn_array_table_unpin_free(pat, cp->mdev);
7026238f921SDong Jia Shi out_init:
7036238f921SDong Jia Shi 	ccw->cda = 0;
7040a19e61eSDong Jia Shi 	return ret;
7050a19e61eSDong Jia Shi }
7060a19e61eSDong Jia Shi 
7070a19e61eSDong Jia Shi /*
7080a19e61eSDong Jia Shi  * Fetch one ccw.
7090a19e61eSDong Jia Shi  * To reduce memory copy, we'll pin the cda page in memory,
7100a19e61eSDong Jia Shi  * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
7110a19e61eSDong Jia Shi  * direct ccws to idal ccws.
7120a19e61eSDong Jia Shi  */
7130a19e61eSDong Jia Shi static int ccwchain_fetch_one(struct ccwchain *chain,
7140a19e61eSDong Jia Shi 			      int idx,
7150a19e61eSDong Jia Shi 			      struct channel_program *cp)
7160a19e61eSDong Jia Shi {
7170a19e61eSDong Jia Shi 	struct ccw1 *ccw = chain->ch_ccw + idx;
7180a19e61eSDong Jia Shi 
7190a19e61eSDong Jia Shi 	if (ccw_is_tic(ccw))
7200a19e61eSDong Jia Shi 		return ccwchain_fetch_tic(chain, idx, cp);
7210a19e61eSDong Jia Shi 
7220a19e61eSDong Jia Shi 	if (ccw_is_idal(ccw))
7230a19e61eSDong Jia Shi 		return ccwchain_fetch_idal(chain, idx, cp);
7240a19e61eSDong Jia Shi 
7250a19e61eSDong Jia Shi 	return ccwchain_fetch_direct(chain, idx, cp);
7260a19e61eSDong Jia Shi }
7270a19e61eSDong Jia Shi 
7280a19e61eSDong Jia Shi /**
7290a19e61eSDong Jia Shi  * cp_init() - allocate ccwchains for a channel program.
7300a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7310a19e61eSDong Jia Shi  * @mdev: the mediated device to perform pin/unpin operations
7320a19e61eSDong Jia Shi  * @orb: control block for the channel program from the guest
7330a19e61eSDong Jia Shi  *
7340a19e61eSDong Jia Shi  * This creates one or more ccwchain(s), and copies the raw data of
7350a19e61eSDong Jia Shi  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
7360a19e61eSDong Jia Shi  *
7370a19e61eSDong Jia Shi  * Limitations:
7380a19e61eSDong Jia Shi  * 1. Supports only prefetch enabled mode.
7390a19e61eSDong Jia Shi  * 2. Supports idal(c64) ccw chaining.
7400a19e61eSDong Jia Shi  * 3. Supports 4k idaw.
7410a19e61eSDong Jia Shi  *
7420a19e61eSDong Jia Shi  * Returns:
7430a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
7440a19e61eSDong Jia Shi  */
7450a19e61eSDong Jia Shi int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
7460a19e61eSDong Jia Shi {
747*99afcb05SEric Farman 	int ret;
7480a19e61eSDong Jia Shi 
7490a19e61eSDong Jia Shi 	/*
7500a19e61eSDong Jia Shi 	 * XXX:
7510a19e61eSDong Jia Shi 	 * Only support prefetch enable mode now.
7520a19e61eSDong Jia Shi 	 */
753fb9e7880SHalil Pasic 	if (!orb->cmd.pfch)
7540a19e61eSDong Jia Shi 		return -EOPNOTSUPP;
7550a19e61eSDong Jia Shi 
7560a19e61eSDong Jia Shi 	INIT_LIST_HEAD(&cp->ccwchain_list);
7570a19e61eSDong Jia Shi 	memcpy(&cp->orb, orb, sizeof(*orb));
7580a19e61eSDong Jia Shi 	cp->mdev = mdev;
7590a19e61eSDong Jia Shi 
760*99afcb05SEric Farman 	/* Build a ccwchain for the first CCW segment */
761*99afcb05SEric Farman 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
7620a19e61eSDong Jia Shi 	if (ret)
763812271b9SEric Farman 		cp_free(cp);
764*99afcb05SEric Farman 
765fb9e7880SHalil Pasic 	/* It is safe to force: if not set but idals used
766fb9e7880SHalil Pasic 	 * ccwchain_calc_length returns an error.
767fb9e7880SHalil Pasic 	 */
768fb9e7880SHalil Pasic 	cp->orb.cmd.c64 = 1;
7690a19e61eSDong Jia Shi 
77071189f26SCornelia Huck 	if (!ret)
77171189f26SCornelia Huck 		cp->initialized = true;
77271189f26SCornelia Huck 
7730a19e61eSDong Jia Shi 	return ret;
7740a19e61eSDong Jia Shi }
7750a19e61eSDong Jia Shi 
7760a19e61eSDong Jia Shi 
7770a19e61eSDong Jia Shi /**
7780a19e61eSDong Jia Shi  * cp_free() - free resources for channel program.
7790a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7800a19e61eSDong Jia Shi  *
7810a19e61eSDong Jia Shi  * This unpins the memory pages and frees the memory space occupied by
7820a19e61eSDong Jia Shi  * @cp, which must have been returned by a previous call to cp_init().
7830a19e61eSDong Jia Shi  * Otherwise, undefined behavior occurs.
7840a19e61eSDong Jia Shi  */
7850a19e61eSDong Jia Shi void cp_free(struct channel_program *cp)
7860a19e61eSDong Jia Shi {
787812271b9SEric Farman 	struct ccwchain *chain, *temp;
788812271b9SEric Farman 	int i;
789812271b9SEric Farman 
790812271b9SEric Farman 	if (!cp->initialized)
791812271b9SEric Farman 		return;
792812271b9SEric Farman 
793812271b9SEric Farman 	cp->initialized = false;
794812271b9SEric Farman 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
795812271b9SEric Farman 		for (i = 0; i < chain->ch_len; i++) {
796812271b9SEric Farman 			pfn_array_table_unpin_free(chain->ch_pat + i,
797812271b9SEric Farman 						   cp->mdev);
798812271b9SEric Farman 			ccwchain_cda_free(chain, i);
799812271b9SEric Farman 		}
800812271b9SEric Farman 		ccwchain_free(chain);
801812271b9SEric Farman 	}
8020a19e61eSDong Jia Shi }
8030a19e61eSDong Jia Shi 
8040a19e61eSDong Jia Shi /**
8050a19e61eSDong Jia Shi  * cp_prefetch() - translate a guest physical address channel program to
8060a19e61eSDong Jia Shi  *                 a real-device runnable channel program.
8070a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8080a19e61eSDong Jia Shi  *
8090a19e61eSDong Jia Shi  * This function translates the guest-physical-address channel program
8100a19e61eSDong Jia Shi  * and stores the result to ccwchain list. @cp must have been
8110a19e61eSDong Jia Shi  * initialized by a previous call with cp_init(). Otherwise, undefined
8120a19e61eSDong Jia Shi  * behavior occurs.
813d66a7355SHalil Pasic  * For each chain composing the channel program:
814d66a7355SHalil Pasic  * - On entry ch_len holds the count of CCWs to be translated.
815d66a7355SHalil Pasic  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
816d66a7355SHalil Pasic  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
8170a19e61eSDong Jia Shi  *
8180a19e61eSDong Jia Shi  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
8190a19e61eSDong Jia Shi  * as helpers to do ccw chain translation inside the kernel. Basically
8200a19e61eSDong Jia Shi  * they accept a channel program issued by a virtual machine, and
8210a19e61eSDong Jia Shi  * translate the channel program to a real-device runnable channel
8220a19e61eSDong Jia Shi  * program.
8230a19e61eSDong Jia Shi  *
8240a19e61eSDong Jia Shi  * These APIs will copy the ccws into kernel-space buffers, and update
8250a19e61eSDong Jia Shi  * the guest phsical addresses with their corresponding host physical
8260a19e61eSDong Jia Shi  * addresses.  Then channel I/O device drivers could issue the
8270a19e61eSDong Jia Shi  * translated channel program to real devices to perform an I/O
8280a19e61eSDong Jia Shi  * operation.
8290a19e61eSDong Jia Shi  *
8300a19e61eSDong Jia Shi  * These interfaces are designed to support translation only for
8310a19e61eSDong Jia Shi  * channel programs, which are generated and formatted by a
8320a19e61eSDong Jia Shi  * guest. Thus this will make it possible for things like VFIO to
8330a19e61eSDong Jia Shi  * leverage the interfaces to passthrough a channel I/O mediated
8340a19e61eSDong Jia Shi  * device in QEMU.
8350a19e61eSDong Jia Shi  *
8360a19e61eSDong Jia Shi  * We support direct ccw chaining by translating them to idal ccws.
8370a19e61eSDong Jia Shi  *
8380a19e61eSDong Jia Shi  * Returns:
8390a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
8400a19e61eSDong Jia Shi  */
8410a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp)
8420a19e61eSDong Jia Shi {
8430a19e61eSDong Jia Shi 	struct ccwchain *chain;
8440a19e61eSDong Jia Shi 	int len, idx, ret;
8450a19e61eSDong Jia Shi 
84671189f26SCornelia Huck 	/* this is an error in the caller */
84771189f26SCornelia Huck 	if (!cp->initialized)
84871189f26SCornelia Huck 		return -EINVAL;
84971189f26SCornelia Huck 
8500a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8510a19e61eSDong Jia Shi 		len = chain->ch_len;
8520a19e61eSDong Jia Shi 		for (idx = 0; idx < len; idx++) {
8530a19e61eSDong Jia Shi 			ret = ccwchain_fetch_one(chain, idx, cp);
8540a19e61eSDong Jia Shi 			if (ret)
855d66a7355SHalil Pasic 				goto out_err;
8560a19e61eSDong Jia Shi 		}
8570a19e61eSDong Jia Shi 	}
8580a19e61eSDong Jia Shi 
8590a19e61eSDong Jia Shi 	return 0;
860d66a7355SHalil Pasic out_err:
861d66a7355SHalil Pasic 	/* Only cleanup the chain elements that were actually translated. */
862d66a7355SHalil Pasic 	chain->ch_len = idx;
863d66a7355SHalil Pasic 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
864d66a7355SHalil Pasic 		chain->ch_len = 0;
865d66a7355SHalil Pasic 	}
866d66a7355SHalil Pasic 	return ret;
8670a19e61eSDong Jia Shi }
8680a19e61eSDong Jia Shi 
8690a19e61eSDong Jia Shi /**
8700a19e61eSDong Jia Shi  * cp_get_orb() - get the orb of the channel program
8710a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8720a19e61eSDong Jia Shi  * @intparm: new intparm for the returned orb
8730a19e61eSDong Jia Shi  * @lpm: candidate value of the logical-path mask for the returned orb
8740a19e61eSDong Jia Shi  *
8750a19e61eSDong Jia Shi  * This function returns the address of the updated orb of the channel
8760a19e61eSDong Jia Shi  * program. Channel I/O device drivers could use this orb to issue a
8770a19e61eSDong Jia Shi  * ssch.
8780a19e61eSDong Jia Shi  */
8790a19e61eSDong Jia Shi union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
8800a19e61eSDong Jia Shi {
8810a19e61eSDong Jia Shi 	union orb *orb;
8820a19e61eSDong Jia Shi 	struct ccwchain *chain;
8830a19e61eSDong Jia Shi 	struct ccw1 *cpa;
8840a19e61eSDong Jia Shi 
88571189f26SCornelia Huck 	/* this is an error in the caller */
88671189f26SCornelia Huck 	if (!cp->initialized)
88771189f26SCornelia Huck 		return NULL;
88871189f26SCornelia Huck 
8890a19e61eSDong Jia Shi 	orb = &cp->orb;
8900a19e61eSDong Jia Shi 
8910a19e61eSDong Jia Shi 	orb->cmd.intparm = intparm;
8920a19e61eSDong Jia Shi 	orb->cmd.fmt = 1;
8930a19e61eSDong Jia Shi 	orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
8940a19e61eSDong Jia Shi 
8950a19e61eSDong Jia Shi 	if (orb->cmd.lpm == 0)
8960a19e61eSDong Jia Shi 		orb->cmd.lpm = lpm;
8970a19e61eSDong Jia Shi 
8980a19e61eSDong Jia Shi 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
8990a19e61eSDong Jia Shi 	cpa = chain->ch_ccw;
9000a19e61eSDong Jia Shi 	orb->cmd.cpa = (__u32) __pa(cpa);
9010a19e61eSDong Jia Shi 
9020a19e61eSDong Jia Shi 	return orb;
9030a19e61eSDong Jia Shi }
9040a19e61eSDong Jia Shi 
9050a19e61eSDong Jia Shi /**
9060a19e61eSDong Jia Shi  * cp_update_scsw() - update scsw for a channel program.
9070a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
9080a19e61eSDong Jia Shi  * @scsw: I/O results of the channel program and also the target to be
9090a19e61eSDong Jia Shi  *        updated
9100a19e61eSDong Jia Shi  *
9110a19e61eSDong Jia Shi  * @scsw contains the I/O results of the channel program that pointed
9120a19e61eSDong Jia Shi  * to by @cp. However what @scsw->cpa stores is a host physical
9130a19e61eSDong Jia Shi  * address, which is meaningless for the guest, which is waiting for
9140a19e61eSDong Jia Shi  * the I/O results.
9150a19e61eSDong Jia Shi  *
9160a19e61eSDong Jia Shi  * This function updates @scsw->cpa to its coressponding guest physical
9170a19e61eSDong Jia Shi  * address.
9180a19e61eSDong Jia Shi  */
9190a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
9200a19e61eSDong Jia Shi {
9210a19e61eSDong Jia Shi 	struct ccwchain *chain;
9220a19e61eSDong Jia Shi 	u32 cpa = scsw->cmd.cpa;
9232904337fSEric Farman 	u32 ccw_head;
9240a19e61eSDong Jia Shi 
92571189f26SCornelia Huck 	if (!cp->initialized)
92671189f26SCornelia Huck 		return;
92771189f26SCornelia Huck 
9280a19e61eSDong Jia Shi 	/*
9290a19e61eSDong Jia Shi 	 * LATER:
9300a19e61eSDong Jia Shi 	 * For now, only update the cmd.cpa part. We may need to deal with
9310a19e61eSDong Jia Shi 	 * other portions of the schib as well, even if we don't return them
9320a19e61eSDong Jia Shi 	 * in the ioctl directly. Path status changes etc.
9330a19e61eSDong Jia Shi 	 */
9340a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9350a19e61eSDong Jia Shi 		ccw_head = (u32)(u64)chain->ch_ccw;
93615f0eb3dSEric Farman 		/*
93715f0eb3dSEric Farman 		 * On successful execution, cpa points just beyond the end
93815f0eb3dSEric Farman 		 * of the chain.
93915f0eb3dSEric Farman 		 */
94015f0eb3dSEric Farman 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
9410a19e61eSDong Jia Shi 			/*
9420a19e61eSDong Jia Shi 			 * (cpa - ccw_head) is the offset value of the host
9430a19e61eSDong Jia Shi 			 * physical ccw to its chain head.
9440a19e61eSDong Jia Shi 			 * Adding this value to the guest physical ccw chain
9450a19e61eSDong Jia Shi 			 * head gets us the guest cpa.
9460a19e61eSDong Jia Shi 			 */
9470a19e61eSDong Jia Shi 			cpa = chain->ch_iova + (cpa - ccw_head);
9480a19e61eSDong Jia Shi 			break;
9490a19e61eSDong Jia Shi 		}
9500a19e61eSDong Jia Shi 	}
9510a19e61eSDong Jia Shi 
9520a19e61eSDong Jia Shi 	scsw->cmd.cpa = cpa;
9530a19e61eSDong Jia Shi }
9540a19e61eSDong Jia Shi 
9550a19e61eSDong Jia Shi /**
9560a19e61eSDong Jia Shi  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
957364e3f90SSebastian Ott  * @cp: channel_program on which to perform the operation
9580a19e61eSDong Jia Shi  * @iova: the iova to check
9590a19e61eSDong Jia Shi  *
9600a19e61eSDong Jia Shi  * If the @iova is currently pinned for the ccw chain, return true;
9610a19e61eSDong Jia Shi  * else return false.
9620a19e61eSDong Jia Shi  */
9630a19e61eSDong Jia Shi bool cp_iova_pinned(struct channel_program *cp, u64 iova)
9640a19e61eSDong Jia Shi {
9650a19e61eSDong Jia Shi 	struct ccwchain *chain;
9660a19e61eSDong Jia Shi 	int i;
9670a19e61eSDong Jia Shi 
96871189f26SCornelia Huck 	if (!cp->initialized)
96971189f26SCornelia Huck 		return false;
97071189f26SCornelia Huck 
9710a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9720a19e61eSDong Jia Shi 		for (i = 0; i < chain->ch_len; i++)
9730a19e61eSDong Jia Shi 			if (pfn_array_table_iova_pinned(chain->ch_pat + i,
9740a19e61eSDong Jia Shi 							iova))
9750a19e61eSDong Jia Shi 				return true;
9760a19e61eSDong Jia Shi 	}
9770a19e61eSDong Jia Shi 
9780a19e61eSDong Jia Shi 	return false;
9790a19e61eSDong Jia Shi }
980