xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_cp.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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  */
page_array_alloc(struct page_array * pa,unsigned int len)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
86b5a73e8eSEric Farman  * @unaligned: were pages unaligned on the pin request
87cfedb3d5SNicolin Chen  *
88cfedb3d5SNicolin Chen  * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0,
89cfedb3d5SNicolin Chen  * otherwise only clear pa->pa_nr
90cfedb3d5SNicolin Chen  */
page_array_unpin(struct page_array * pa,struct vfio_device * vdev,int pa_nr,bool unaligned)9113314605SNicolin Chen static void page_array_unpin(struct page_array *pa,
92b5a73e8eSEric Farman 			     struct vfio_device *vdev, int pa_nr, bool unaligned)
93cfedb3d5SNicolin Chen {
94cfedb3d5SNicolin Chen 	int unpinned = 0, npage = 1;
95cfedb3d5SNicolin Chen 
96cfedb3d5SNicolin Chen 	while (unpinned < pa_nr) {
9713314605SNicolin Chen 		dma_addr_t *first = &pa->pa_iova[unpinned];
9813314605SNicolin Chen 		dma_addr_t *last = &first[npage];
99cfedb3d5SNicolin Chen 
100cfedb3d5SNicolin Chen 		if (unpinned + npage < pa_nr &&
101b5a73e8eSEric Farman 		    *first + npage * PAGE_SIZE == *last &&
102b5a73e8eSEric Farman 		    !unaligned) {
103cfedb3d5SNicolin Chen 			npage++;
104cfedb3d5SNicolin Chen 			continue;
105cfedb3d5SNicolin Chen 		}
106cfedb3d5SNicolin Chen 
10713314605SNicolin Chen 		vfio_unpin_pages(vdev, *first, npage);
108cfedb3d5SNicolin Chen 		unpinned += npage;
109cfedb3d5SNicolin Chen 		npage = 1;
110cfedb3d5SNicolin Chen 	}
111cfedb3d5SNicolin Chen 
112cfedb3d5SNicolin Chen 	pa->pa_nr = 0;
113cfedb3d5SNicolin Chen }
114cfedb3d5SNicolin Chen 
115cfedb3d5SNicolin Chen /*
11613314605SNicolin Chen  * page_array_pin() - Pin user pages in memory
11713314605SNicolin Chen  * @pa: page_array on which to perform the operation
1188a54e238SEric Farman  * @vdev: the vfio device to perform pin operations
119b5a73e8eSEric Farman  * @unaligned: are pages aligned to 4K boundary?
120e4f3f18bSEric Farman  *
121e4f3f18bSEric Farman  * Returns number of pages pinned upon success.
122e4f3f18bSEric Farman  * If the pin request partially succeeds, or fails completely,
123e4f3f18bSEric Farman  * all pages are left unpinned and a negative error value is returned.
124b5a73e8eSEric Farman  *
125b5a73e8eSEric Farman  * Requests to pin "aligned" pages can be coalesced into a single
126b5a73e8eSEric Farman  * vfio_pin_pages request for the sake of efficiency, based on the
127b5a73e8eSEric Farman  * expectation of 4K page requests. Unaligned requests are probably
128b5a73e8eSEric Farman  * dealing with 2K "pages", and cannot be coalesced without
129b5a73e8eSEric Farman  * reworking this logic to incorporate that math.
130e4f3f18bSEric Farman  */
page_array_pin(struct page_array * pa,struct vfio_device * vdev,bool unaligned)131b5a73e8eSEric Farman static int page_array_pin(struct page_array *pa, struct vfio_device *vdev, bool unaligned)
132e4f3f18bSEric Farman {
133cfedb3d5SNicolin Chen 	int pinned = 0, npage = 1;
134e4f3f18bSEric Farman 	int ret = 0;
135e4f3f18bSEric Farman 
136cfedb3d5SNicolin Chen 	while (pinned < pa->pa_nr) {
13713314605SNicolin Chen 		dma_addr_t *first = &pa->pa_iova[pinned];
13813314605SNicolin Chen 		dma_addr_t *last = &first[npage];
1395c1cfb1cSDong Jia Shi 
140cfedb3d5SNicolin Chen 		if (pinned + npage < pa->pa_nr &&
141b5a73e8eSEric Farman 		    *first + npage * PAGE_SIZE == *last &&
142b5a73e8eSEric Farman 		    !unaligned) {
143cfedb3d5SNicolin Chen 			npage++;
144cfedb3d5SNicolin Chen 			continue;
145cfedb3d5SNicolin Chen 		}
146cfedb3d5SNicolin Chen 
14713314605SNicolin Chen 		ret = vfio_pin_pages(vdev, *first, npage,
148cfedb3d5SNicolin Chen 				     IOMMU_READ | IOMMU_WRITE,
14934a255e6SNicolin Chen 				     &pa->pa_page[pinned]);
1505c1cfb1cSDong Jia Shi 		if (ret < 0) {
1515c1cfb1cSDong Jia Shi 			goto err_out;
152cfedb3d5SNicolin Chen 		} else if (ret > 0 && ret != npage) {
153cfedb3d5SNicolin Chen 			pinned += ret;
1540a19e61eSDong Jia Shi 			ret = -EINVAL;
1555c1cfb1cSDong Jia Shi 			goto err_out;
1565c1cfb1cSDong Jia Shi 		}
157cfedb3d5SNicolin Chen 		pinned += npage;
158cfedb3d5SNicolin Chen 		npage = 1;
159cfedb3d5SNicolin Chen 	}
1600a19e61eSDong Jia Shi 
1610a19e61eSDong Jia Shi 	return ret;
1625c1cfb1cSDong Jia Shi 
1635c1cfb1cSDong Jia Shi err_out:
164b5a73e8eSEric Farman 	page_array_unpin(pa, vdev, pinned, unaligned);
1655c1cfb1cSDong Jia Shi 	return ret;
1665c1cfb1cSDong Jia Shi }
1675c1cfb1cSDong Jia Shi 
1685c1cfb1cSDong Jia Shi /* Unpin the pages before releasing the memory. */
page_array_unpin_free(struct page_array * pa,struct vfio_device * vdev,bool unaligned)169b5a73e8eSEric Farman static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev, bool unaligned)
1705c1cfb1cSDong Jia Shi {
171b5a73e8eSEric Farman 	page_array_unpin(pa, vdev, pa->pa_nr, unaligned);
17262a97a56SEric Farman 	kfree(pa->pa_page);
17313314605SNicolin Chen 	kfree(pa->pa_iova);
1740a19e61eSDong Jia Shi }
1750a19e61eSDong Jia Shi 
page_array_iova_pinned(struct page_array * pa,u64 iova,u64 length)1765a4fe7c4SEric Farman static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length)
1770a19e61eSDong Jia Shi {
1785a4fe7c4SEric Farman 	u64 iova_pfn_start = iova >> PAGE_SHIFT;
1795a4fe7c4SEric Farman 	u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT;
1805a4fe7c4SEric Farman 	u64 pfn;
1810a19e61eSDong Jia Shi 	int i;
1820a19e61eSDong Jia Shi 
1835a4fe7c4SEric Farman 	for (i = 0; i < pa->pa_nr; i++) {
1845a4fe7c4SEric Farman 		pfn = pa->pa_iova[i] >> PAGE_SHIFT;
1855a4fe7c4SEric Farman 		if (pfn >= iova_pfn_start && pfn <= iova_pfn_end)
1860a19e61eSDong Jia Shi 			return true;
1875a4fe7c4SEric Farman 	}
1880a19e61eSDong Jia Shi 
1890a19e61eSDong Jia Shi 	return false;
1900a19e61eSDong Jia Shi }
19113314605SNicolin Chen /* Create the list of IDAL words for a page_array. */
page_array_idal_create_words(struct page_array * pa,unsigned long * idaws)19213314605SNicolin Chen static inline void page_array_idal_create_words(struct page_array *pa,
1930a19e61eSDong Jia Shi 						unsigned long *idaws)
1940a19e61eSDong Jia Shi {
195e7eaf91bSEric Farman 	int i;
1960a19e61eSDong Jia Shi 
1970a19e61eSDong Jia Shi 	/*
1980a19e61eSDong Jia Shi 	 * Idal words (execept the first one) rely on the memory being 4k
1990a19e61eSDong Jia Shi 	 * aligned. If a user virtual address is 4K aligned, then it's
2000a19e61eSDong Jia Shi 	 * corresponding kernel physical address will also be 4K aligned. Thus
2010a19e61eSDong Jia Shi 	 * there will be no problem here to simply use the phys to create an
2020a19e61eSDong Jia Shi 	 * idaw.
2030a19e61eSDong Jia Shi 	 */
204e7eaf91bSEric Farman 
20561f3a16bSEric Farman 	for (i = 0; i < pa->pa_nr; i++) {
20634a255e6SNicolin Chen 		idaws[i] = page_to_phys(pa->pa_page[i]);
2078aabf0edSEric Farman 
20861f3a16bSEric Farman 		/* Incorporate any offset from each starting address */
20961f3a16bSEric Farman 		idaws[i] += pa->pa_iova[i] & (PAGE_SIZE - 1);
21061f3a16bSEric Farman 	}
2110a19e61eSDong Jia Shi }
2120a19e61eSDong Jia Shi 
convert_ccw0_to_ccw1(struct ccw1 * source,unsigned long len)213dbd66558SCornelia Huck static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
2147f8e89a8SEric Farman {
2157f8e89a8SEric Farman 	struct ccw0 ccw0;
2167f8e89a8SEric Farman 	struct ccw1 *pccw1 = source;
2177f8e89a8SEric Farman 	int i;
2187f8e89a8SEric Farman 
2197f8e89a8SEric Farman 	for (i = 0; i < len; i++) {
2207f8e89a8SEric Farman 		ccw0 = *(struct ccw0 *)pccw1;
2217f8e89a8SEric Farman 		if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
2227f8e89a8SEric Farman 			pccw1->cmd_code = CCW_CMD_TIC;
2237f8e89a8SEric Farman 			pccw1->flags = 0;
2247f8e89a8SEric Farman 			pccw1->count = 0;
2257f8e89a8SEric Farman 		} else {
2267f8e89a8SEric Farman 			pccw1->cmd_code = ccw0.cmd_code;
2277f8e89a8SEric Farman 			pccw1->flags = ccw0.flags;
2287f8e89a8SEric Farman 			pccw1->count = ccw0.count;
2297f8e89a8SEric Farman 		}
2307f8e89a8SEric Farman 		pccw1->cda = ccw0.cda;
2317f8e89a8SEric Farman 		pccw1++;
2327f8e89a8SEric Farman 	}
2337f8e89a8SEric Farman }
2340a19e61eSDong Jia Shi 
2351b676fe3SEric Farman #define idal_is_2k(_cp) (!(_cp)->orb.cmd.c64 || (_cp)->orb.cmd.i2k)
2361b676fe3SEric Farman 
2370a19e61eSDong Jia Shi /*
2380a19e61eSDong Jia Shi  * Helpers to operate ccwchain.
2390a19e61eSDong Jia Shi  */
2405d87fbf7SEric Farman #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
2415d87fbf7SEric Farman #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
2425d87fbf7SEric Farman #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
2435d87fbf7SEric Farman 
2440a19e61eSDong Jia Shi #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
2450a19e61eSDong Jia Shi 
2460a19e61eSDong Jia Shi #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
2470a19e61eSDong Jia Shi 
2480a19e61eSDong Jia Shi #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
2495d87fbf7SEric Farman #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
2500a19e61eSDong Jia Shi 
2510a19e61eSDong Jia Shi #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
2520a19e61eSDong Jia Shi 
25348bd0eeeSEric Farman /*
2545d87fbf7SEric Farman  * ccw_does_data_transfer()
2555d87fbf7SEric Farman  *
2565d87fbf7SEric Farman  * Determine whether a CCW will move any data, such that the guest pages
2575d87fbf7SEric Farman  * would need to be pinned before performing the I/O.
2585d87fbf7SEric Farman  *
2595d87fbf7SEric Farman  * Returns 1 if yes, 0 if no.
2605d87fbf7SEric Farman  */
ccw_does_data_transfer(struct ccw1 * ccw)2615d87fbf7SEric Farman static inline int ccw_does_data_transfer(struct ccw1 *ccw)
2625d87fbf7SEric Farman {
263453eac31SEric Farman 	/* If the count field is zero, then no data will be transferred */
264453eac31SEric Farman 	if (ccw->count == 0)
265453eac31SEric Farman 		return 0;
266453eac31SEric Farman 
2679b6e57e5SEric Farman 	/* If the command is a NOP, then no data will be transferred */
2689b6e57e5SEric Farman 	if (ccw_is_noop(ccw))
2699b6e57e5SEric Farman 		return 0;
2709b6e57e5SEric Farman 
2715d87fbf7SEric Farman 	/* If the skip flag is off, then data will be transferred */
2725d87fbf7SEric Farman 	if (!ccw_is_skip(ccw))
2735d87fbf7SEric Farman 		return 1;
2745d87fbf7SEric Farman 
2755d87fbf7SEric Farman 	/*
2765d87fbf7SEric Farman 	 * If the skip flag is on, it is only meaningful if the command
2775d87fbf7SEric Farman 	 * code is a read, read backward, sense, or sense ID.  In those
2785d87fbf7SEric Farman 	 * cases, no data will be transferred.
2795d87fbf7SEric Farman 	 */
2805d87fbf7SEric Farman 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
2815d87fbf7SEric Farman 		return 0;
2825d87fbf7SEric Farman 
2835d87fbf7SEric Farman 	if (ccw_is_sense(ccw))
2845d87fbf7SEric Farman 		return 0;
2855d87fbf7SEric Farman 
2865d87fbf7SEric Farman 	/* The skip flag is on, but it is ignored for this command code. */
2875d87fbf7SEric Farman 	return 1;
2885d87fbf7SEric Farman }
2895d87fbf7SEric Farman 
2905d87fbf7SEric Farman /*
29148bd0eeeSEric Farman  * is_cpa_within_range()
29248bd0eeeSEric Farman  *
29348bd0eeeSEric Farman  * @cpa: channel program address being questioned
29448bd0eeeSEric Farman  * @head: address of the beginning of a CCW chain
29548bd0eeeSEric Farman  * @len: number of CCWs within the chain
29648bd0eeeSEric Farman  *
29748bd0eeeSEric Farman  * Determine whether the address of a CCW (whether a new chain,
29848bd0eeeSEric Farman  * or the target of a TIC) falls within a range (including the end points).
29948bd0eeeSEric Farman  *
30048bd0eeeSEric Farman  * Returns 1 if yes, 0 if no.
30148bd0eeeSEric Farman  */
is_cpa_within_range(u32 cpa,u32 head,int len)30248bd0eeeSEric Farman static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
30348bd0eeeSEric Farman {
30448bd0eeeSEric Farman 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
30548bd0eeeSEric Farman 
30648bd0eeeSEric Farman 	return (head <= cpa && cpa <= tail);
30748bd0eeeSEric Farman }
30848bd0eeeSEric Farman 
is_tic_within_range(struct ccw1 * ccw,u32 head,int len)30948bd0eeeSEric Farman static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
31048bd0eeeSEric Farman {
31148bd0eeeSEric Farman 	if (!ccw_is_tic(ccw))
31248bd0eeeSEric Farman 		return 0;
31348bd0eeeSEric Farman 
31448bd0eeeSEric Farman 	return is_cpa_within_range(ccw->cda, head, len);
31548bd0eeeSEric Farman }
31648bd0eeeSEric Farman 
ccwchain_alloc(struct channel_program * cp,int len)3170a19e61eSDong Jia Shi static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
3180a19e61eSDong Jia Shi {
3190a19e61eSDong Jia Shi 	struct ccwchain *chain;
3200a19e61eSDong Jia Shi 
3214b946d65SEric Farman 	chain = kzalloc(sizeof(*chain), GFP_KERNEL);
3220a19e61eSDong Jia Shi 	if (!chain)
3230a19e61eSDong Jia Shi 		return NULL;
3240a19e61eSDong Jia Shi 
3254b946d65SEric Farman 	chain->ch_ccw = kcalloc(len, sizeof(*chain->ch_ccw), GFP_DMA | GFP_KERNEL);
3264b946d65SEric Farman 	if (!chain->ch_ccw)
3274b946d65SEric Farman 		goto out_err;
3280a19e61eSDong Jia Shi 
3294b946d65SEric Farman 	chain->ch_pa = kcalloc(len, sizeof(*chain->ch_pa), GFP_KERNEL);
3304b946d65SEric Farman 	if (!chain->ch_pa)
3314b946d65SEric Farman 		goto out_err;
3320a19e61eSDong Jia Shi 
3330a19e61eSDong Jia Shi 	list_add_tail(&chain->next, &cp->ccwchain_list);
3340a19e61eSDong Jia Shi 
3350a19e61eSDong Jia Shi 	return chain;
3364b946d65SEric Farman 
3374b946d65SEric Farman out_err:
3384b946d65SEric Farman 	kfree(chain->ch_ccw);
3394b946d65SEric Farman 	kfree(chain);
3404b946d65SEric Farman 	return NULL;
3410a19e61eSDong Jia Shi }
3420a19e61eSDong Jia Shi 
ccwchain_free(struct ccwchain * chain)3430a19e61eSDong Jia Shi static void ccwchain_free(struct ccwchain *chain)
3440a19e61eSDong Jia Shi {
3450a19e61eSDong Jia Shi 	list_del(&chain->next);
3464b946d65SEric Farman 	kfree(chain->ch_pa);
3474b946d65SEric Farman 	kfree(chain->ch_ccw);
3480a19e61eSDong Jia Shi 	kfree(chain);
3490a19e61eSDong Jia Shi }
3500a19e61eSDong Jia Shi 
3510a19e61eSDong Jia Shi /* Free resource for a ccw that allocated memory for its cda. */
ccwchain_cda_free(struct ccwchain * chain,int idx)3520a19e61eSDong Jia Shi static void ccwchain_cda_free(struct ccwchain *chain, int idx)
3530a19e61eSDong Jia Shi {
3544b946d65SEric Farman 	struct ccw1 *ccw = &chain->ch_ccw[idx];
3550a19e61eSDong Jia Shi 
3569b6e57e5SEric Farman 	if (ccw_is_tic(ccw))
357408358b5SJason J. Herne 		return;
3580a19e61eSDong Jia Shi 
3595de2322dSEric Farman 	kfree(phys_to_virt(ccw->cda));
3600a19e61eSDong Jia Shi }
3610a19e61eSDong Jia Shi 
3620a19e61eSDong Jia Shi /**
3630a19e61eSDong Jia Shi  * ccwchain_calc_length - calculate the length of the ccw chain.
3640a19e61eSDong Jia Shi  * @iova: guest physical address of the target ccw chain
3650a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
3660a19e61eSDong Jia Shi  *
3670a19e61eSDong Jia Shi  * This is the chain length not considering any TICs.
3680a19e61eSDong Jia Shi  * You need to do a new round for each TIC target.
3690a19e61eSDong Jia Shi  *
370fb9e7880SHalil Pasic  * The program is also validated for absence of not yet supported
371fb9e7880SHalil Pasic  * indirect data addressing scenarios.
372fb9e7880SHalil Pasic  *
3730a19e61eSDong Jia Shi  * Returns: the length of the ccw chain or -errno.
3740a19e61eSDong Jia Shi  */
ccwchain_calc_length(u64 iova,struct channel_program * cp)3750a19e61eSDong Jia Shi static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
3760a19e61eSDong Jia Shi {
3771d897e47SEric Farman 	struct ccw1 *ccw = cp->guest_cp;
378ded563f3SEric Farman 	int cnt = 0;
3790a19e61eSDong Jia Shi 
3800a19e61eSDong Jia Shi 	do {
3810a19e61eSDong Jia Shi 		cnt++;
3820a19e61eSDong Jia Shi 
383fb9e7880SHalil Pasic 		/*
38448bd0eeeSEric Farman 		 * We want to keep counting if the current CCW has the
38548bd0eeeSEric Farman 		 * command-chaining flag enabled, or if it is a TIC CCW
38648bd0eeeSEric Farman 		 * that loops back into the current chain.  The latter
38748bd0eeeSEric Farman 		 * is used for device orientation, where the CCW PRIOR to
38848bd0eeeSEric Farman 		 * the TIC can either jump to the TIC or a CCW immediately
38948bd0eeeSEric Farman 		 * after the TIC, depending on the results of its operation.
39048bd0eeeSEric Farman 		 */
39148bd0eeeSEric Farman 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
3920a19e61eSDong Jia Shi 			break;
3930a19e61eSDong Jia Shi 
3940a19e61eSDong Jia Shi 		ccw++;
3950a19e61eSDong Jia Shi 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
3960a19e61eSDong Jia Shi 
3970a19e61eSDong Jia Shi 	if (cnt == CCWCHAIN_LEN_MAX + 1)
3980a19e61eSDong Jia Shi 		cnt = -EINVAL;
3990a19e61eSDong Jia Shi 
4000a19e61eSDong Jia Shi 	return cnt;
4010a19e61eSDong Jia Shi }
4020a19e61eSDong Jia Shi 
tic_target_chain_exists(struct ccw1 * tic,struct channel_program * cp)4030a19e61eSDong Jia Shi static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
4040a19e61eSDong Jia Shi {
4050a19e61eSDong Jia Shi 	struct ccwchain *chain;
4062904337fSEric Farman 	u32 ccw_head;
4070a19e61eSDong Jia Shi 
4080a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
4090a19e61eSDong Jia Shi 		ccw_head = chain->ch_iova;
4102904337fSEric Farman 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
4110a19e61eSDong Jia Shi 			return 1;
4120a19e61eSDong Jia Shi 	}
4130a19e61eSDong Jia Shi 
4140a19e61eSDong Jia Shi 	return 0;
4150a19e61eSDong Jia Shi }
4160a19e61eSDong Jia Shi 
4170a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain,
4180a19e61eSDong Jia Shi 			     struct channel_program *cp);
4190a19e61eSDong Jia Shi 
ccwchain_handle_ccw(u32 cda,struct channel_program * cp)420363fe5f7SEric Farman static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
4210a19e61eSDong Jia Shi {
4220a587956SJason Gunthorpe 	struct vfio_device *vdev =
4230a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
4240a19e61eSDong Jia Shi 	struct ccwchain *chain;
4258b515be5SFarhan Ali 	int len, ret;
4260a19e61eSDong Jia Shi 
427ded563f3SEric Farman 	/* Copy 2K (the most we support today) of possible CCWs */
428c5e8083fSEric Farman 	ret = vfio_dma_rw(vdev, cda, cp->guest_cp, CCWCHAIN_LEN_MAX * sizeof(struct ccw1), false);
429c5e8083fSEric Farman 	if (ret)
430c5e8083fSEric Farman 		return ret;
431ded563f3SEric Farman 
4327f8e89a8SEric Farman 	/* Convert any Format-0 CCWs to Format-1 */
4337f8e89a8SEric Farman 	if (!cp->orb.cmd.fmt)
434c382cbc6SEric Farman 		convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
4357f8e89a8SEric Farman 
436ded563f3SEric Farman 	/* Count the CCWs in the current chain */
437363fe5f7SEric Farman 	len = ccwchain_calc_length(cda, cp);
4380a19e61eSDong Jia Shi 	if (len < 0)
4390a19e61eSDong Jia Shi 		return len;
4400a19e61eSDong Jia Shi 
4410a19e61eSDong Jia Shi 	/* Need alloc a new chain for this one. */
4420a19e61eSDong Jia Shi 	chain = ccwchain_alloc(cp, len);
4430a19e61eSDong Jia Shi 	if (!chain)
4440a19e61eSDong Jia Shi 		return -ENOMEM;
4454b946d65SEric Farman 
4464b946d65SEric Farman 	chain->ch_len = len;
447363fe5f7SEric Farman 	chain->ch_iova = cda;
4480a19e61eSDong Jia Shi 
44962465902SEric Farman 	/* Copy the actual CCWs into the new chain */
45062465902SEric Farman 	memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
4510a19e61eSDong Jia Shi 
4520a19e61eSDong Jia Shi 	/* Loop for tics on this new chain. */
4538b515be5SFarhan Ali 	ret = ccwchain_loop_tic(chain, cp);
4548b515be5SFarhan Ali 
4558b515be5SFarhan Ali 	if (ret)
4568b515be5SFarhan Ali 		ccwchain_free(chain);
4578b515be5SFarhan Ali 
4588b515be5SFarhan Ali 	return ret;
4590a19e61eSDong Jia Shi }
4600a19e61eSDong Jia Shi 
4610a19e61eSDong Jia Shi /* Loop for TICs. */
ccwchain_loop_tic(struct ccwchain * chain,struct channel_program * cp)4620a19e61eSDong Jia Shi static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
4630a19e61eSDong Jia Shi {
4640a19e61eSDong Jia Shi 	struct ccw1 *tic;
4650a19e61eSDong Jia Shi 	int i, ret;
4660a19e61eSDong Jia Shi 
4670a19e61eSDong Jia Shi 	for (i = 0; i < chain->ch_len; i++) {
4684b946d65SEric Farman 		tic = &chain->ch_ccw[i];
4690a19e61eSDong Jia Shi 
4700a19e61eSDong Jia Shi 		if (!ccw_is_tic(tic))
4710a19e61eSDong Jia Shi 			continue;
4720a19e61eSDong Jia Shi 
473e64bd689SEric Farman 		/* May transfer to an existing chain. */
474e64bd689SEric Farman 		if (tic_target_chain_exists(tic, cp))
475e64bd689SEric Farman 			continue;
476e64bd689SEric Farman 
477363fe5f7SEric Farman 		/* Build a ccwchain for the next segment */
478363fe5f7SEric Farman 		ret = ccwchain_handle_ccw(tic->cda, cp);
4790a19e61eSDong Jia Shi 		if (ret)
4800a19e61eSDong Jia Shi 			return ret;
4810a19e61eSDong Jia Shi 	}
4820a19e61eSDong Jia Shi 
4830a19e61eSDong Jia Shi 	return 0;
4840a19e61eSDong Jia Shi }
4850a19e61eSDong Jia Shi 
ccwchain_fetch_tic(struct ccw1 * ccw,struct channel_program * cp)486a4c60404SEric Farman static int ccwchain_fetch_tic(struct ccw1 *ccw,
4870a19e61eSDong Jia Shi 			      struct channel_program *cp)
4880a19e61eSDong Jia Shi {
4890a19e61eSDong Jia Shi 	struct ccwchain *iter;
4902904337fSEric Farman 	u32 ccw_head;
4910a19e61eSDong Jia Shi 
4920a19e61eSDong Jia Shi 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
4930a19e61eSDong Jia Shi 		ccw_head = iter->ch_iova;
4942904337fSEric Farman 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
495c389377cSJason J. Herne 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
4960a19e61eSDong Jia Shi 						     (ccw->cda - ccw_head));
4970a19e61eSDong Jia Shi 			return 0;
4980a19e61eSDong Jia Shi 		}
4990a19e61eSDong Jia Shi 	}
5000a19e61eSDong Jia Shi 
5010a19e61eSDong Jia Shi 	return -EFAULT;
5020a19e61eSDong Jia Shi }
5030a19e61eSDong Jia Shi 
get_guest_idal(struct ccw1 * ccw,struct channel_program * cp,int idaw_nr)50461f3a16bSEric Farman static unsigned long *get_guest_idal(struct ccw1 *ccw,
50561f3a16bSEric Farman 				     struct channel_program *cp,
50661f3a16bSEric Farman 				     int idaw_nr)
50761f3a16bSEric Farman {
50861f3a16bSEric Farman 	struct vfio_device *vdev =
50961f3a16bSEric Farman 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
51061f3a16bSEric Farman 	unsigned long *idaws;
5111b676fe3SEric Farman 	unsigned int *idaws_f1;
51261f3a16bSEric Farman 	int idal_len = idaw_nr * sizeof(*idaws);
5131b676fe3SEric Farman 	int idaw_size = idal_is_2k(cp) ? PAGE_SIZE / 2 : PAGE_SIZE;
51461f3a16bSEric Farman 	int idaw_mask = ~(idaw_size - 1);
51561f3a16bSEric Farman 	int i, ret;
51661f3a16bSEric Farman 
51761f3a16bSEric Farman 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
51861f3a16bSEric Farman 	if (!idaws)
51961f3a16bSEric Farman 		return ERR_PTR(-ENOMEM);
52061f3a16bSEric Farman 
52161f3a16bSEric Farman 	if (ccw_is_idal(ccw)) {
52261f3a16bSEric Farman 		/* Copy IDAL from guest */
52361f3a16bSEric Farman 		ret = vfio_dma_rw(vdev, ccw->cda, idaws, idal_len, false);
52461f3a16bSEric Farman 		if (ret) {
52561f3a16bSEric Farman 			kfree(idaws);
52661f3a16bSEric Farman 			return ERR_PTR(ret);
52761f3a16bSEric Farman 		}
52861f3a16bSEric Farman 	} else {
52961f3a16bSEric Farman 		/* Fabricate an IDAL based off CCW data address */
53061f3a16bSEric Farman 		if (cp->orb.cmd.c64) {
53161f3a16bSEric Farman 			idaws[0] = ccw->cda;
53261f3a16bSEric Farman 			for (i = 1; i < idaw_nr; i++)
53361f3a16bSEric Farman 				idaws[i] = (idaws[i - 1] + idaw_size) & idaw_mask;
53461f3a16bSEric Farman 		} else {
5351b676fe3SEric Farman 			idaws_f1 = (unsigned int *)idaws;
5361b676fe3SEric Farman 			idaws_f1[0] = ccw->cda;
5371b676fe3SEric Farman 			for (i = 1; i < idaw_nr; i++)
5381b676fe3SEric Farman 				idaws_f1[i] = (idaws_f1[i - 1] + idaw_size) & idaw_mask;
53961f3a16bSEric Farman 		}
54061f3a16bSEric Farman 	}
54161f3a16bSEric Farman 
54261f3a16bSEric Farman 	return idaws;
54361f3a16bSEric Farman }
54461f3a16bSEric Farman 
545b21f9cb1SEric Farman /*
546b21f9cb1SEric Farman  * ccw_count_idaws() - Calculate the number of IDAWs needed to transfer
547b21f9cb1SEric Farman  * a specified amount of data
548b21f9cb1SEric Farman  *
549b21f9cb1SEric Farman  * @ccw: The Channel Command Word being translated
550b21f9cb1SEric Farman  * @cp: Channel Program being processed
5516a6dc14aSEric Farman  *
5526a6dc14aSEric Farman  * The ORB is examined, since it specifies what IDAWs could actually be
5536a6dc14aSEric Farman  * used by any CCW in the channel program, regardless of whether or not
5546a6dc14aSEric Farman  * the CCW actually does. An ORB that does not specify Format-2-IDAW
5556a6dc14aSEric Farman  * Control could still contain a CCW with an IDAL, which would be
5566a6dc14aSEric Farman  * Format-1 and thus only move 2K with each IDAW. Thus all CCWs within
5576a6dc14aSEric Farman  * the channel program must follow the same size requirements.
558b21f9cb1SEric Farman  */
ccw_count_idaws(struct ccw1 * ccw,struct channel_program * cp)559b21f9cb1SEric Farman static int ccw_count_idaws(struct ccw1 *ccw,
5600a19e61eSDong Jia Shi 			   struct channel_program *cp)
5610a19e61eSDong Jia Shi {
5620a587956SJason Gunthorpe 	struct vfio_device *vdev =
5630a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
56401aa26c6SEric Farman 	u64 iova;
565667e5dbaSEric Farman 	int size = cp->orb.cmd.c64 ? sizeof(u64) : sizeof(u32);
5666238f921SDong Jia Shi 	int ret;
567453eac31SEric Farman 	int bytes = 1;
5680a19e61eSDong Jia Shi 
569e8573b39SEric Farman 	if (ccw->count)
570453eac31SEric Farman 		bytes = ccw->count;
571e8573b39SEric Farman 
57201aa26c6SEric Farman 	if (ccw_is_idal(ccw)) {
573667e5dbaSEric Farman 		/* Read first IDAW to check its starting address. */
574667e5dbaSEric Farman 		/* All subsequent IDAWs will be 2K- or 4K-aligned. */
575667e5dbaSEric Farman 		ret = vfio_dma_rw(vdev, ccw->cda, &iova, size, false);
57601aa26c6SEric Farman 		if (ret)
57701aa26c6SEric Farman 			return ret;
578667e5dbaSEric Farman 
579667e5dbaSEric Farman 		/*
580667e5dbaSEric Farman 		 * Format-1 IDAWs only occupy the first 32 bits,
581667e5dbaSEric Farman 		 * and bit 0 is always off.
582667e5dbaSEric Farman 		 */
583667e5dbaSEric Farman 		if (!cp->orb.cmd.c64)
584667e5dbaSEric Farman 			iova = iova >> 32;
58501aa26c6SEric Farman 	} else {
58601aa26c6SEric Farman 		iova = ccw->cda;
58701aa26c6SEric Farman 	}
588b21f9cb1SEric Farman 
5896a6dc14aSEric Farman 	/* Format-1 IDAWs operate on 2K each */
5906a6dc14aSEric Farman 	if (!cp->orb.cmd.c64)
5916a6dc14aSEric Farman 		return idal_2k_nr_words((void *)iova, bytes);
5926a6dc14aSEric Farman 
5936a6dc14aSEric Farman 	/* Using the 2K variant of Format-2 IDAWs? */
5946a6dc14aSEric Farman 	if (cp->orb.cmd.i2k)
5956a6dc14aSEric Farman 		return idal_2k_nr_words((void *)iova, bytes);
5966a6dc14aSEric Farman 
5976a6dc14aSEric Farman 	/* The 'usual' case is 4K Format-2 IDAWs */
598b21f9cb1SEric Farman 	return idal_nr_words((void *)iova, bytes);
599b21f9cb1SEric Farman }
600b21f9cb1SEric Farman 
ccwchain_fetch_ccw(struct ccw1 * ccw,struct page_array * pa,struct channel_program * cp)601b21f9cb1SEric Farman static int ccwchain_fetch_ccw(struct ccw1 *ccw,
602b21f9cb1SEric Farman 			      struct page_array *pa,
603b21f9cb1SEric Farman 			      struct channel_program *cp)
604b21f9cb1SEric Farman {
605b21f9cb1SEric Farman 	struct vfio_device *vdev =
606b21f9cb1SEric Farman 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
607b21f9cb1SEric Farman 	unsigned long *idaws;
6081b676fe3SEric Farman 	unsigned int *idaws_f1;
609b21f9cb1SEric Farman 	int ret;
61061f3a16bSEric Farman 	int idaw_nr;
611b21f9cb1SEric Farman 	int i;
612b21f9cb1SEric Farman 
613b21f9cb1SEric Farman 	/* Calculate size of IDAL */
614b21f9cb1SEric Farman 	idaw_nr = ccw_count_idaws(ccw, cp);
615b21f9cb1SEric Farman 	if (idaw_nr < 0)
616b21f9cb1SEric Farman 		return idaw_nr;
617b21f9cb1SEric Farman 
618e8573b39SEric Farman 	/* Allocate an IDAL from host storage */
61961f3a16bSEric Farman 	idaws = get_guest_idal(ccw, cp, idaw_nr);
62061f3a16bSEric Farman 	if (IS_ERR(idaws)) {
62161f3a16bSEric Farman 		ret = PTR_ERR(idaws);
622e8573b39SEric Farman 		goto out_init;
6234cebc5d6SDong Jia Shi 	}
6244cebc5d6SDong Jia Shi 
6250a19e61eSDong Jia Shi 	/*
62613314605SNicolin Chen 	 * Allocate an array of pages to pin/translate.
62701aa26c6SEric Farman 	 * The number of pages is actually the count of the idaws
62801aa26c6SEric Farman 	 * required for the data transfer, since we only only support
62901aa26c6SEric Farman 	 * 4K IDAWs today.
6300a19e61eSDong Jia Shi 	 */
63161783394SEric Farman 	ret = page_array_alloc(pa, idaw_nr);
632e4f3f18bSEric Farman 	if (ret < 0)
633e8573b39SEric Farman 		goto out_free_idaws;
634e4f3f18bSEric Farman 
63501aa26c6SEric Farman 	/*
63613314605SNicolin Chen 	 * Copy guest IDAWs into page_array, in case the memory they
63701aa26c6SEric Farman 	 * occupy is not contiguous.
63801aa26c6SEric Farman 	 */
6391b676fe3SEric Farman 	idaws_f1 = (unsigned int *)idaws;
64061f3a16bSEric Farman 	for (i = 0; i < idaw_nr; i++) {
64161f3a16bSEric Farman 		if (cp->orb.cmd.c64)
64213314605SNicolin Chen 			pa->pa_iova[i] = idaws[i];
6431b676fe3SEric Farman 		else
6441b676fe3SEric Farman 			pa->pa_iova[i] = idaws_f1[i];
64501aa26c6SEric Farman 	}
64601aa26c6SEric Farman 
6475d87fbf7SEric Farman 	if (ccw_does_data_transfer(ccw)) {
648b5a73e8eSEric Farman 		ret = page_array_pin(pa, vdev, idal_is_2k(cp));
6496238f921SDong Jia Shi 		if (ret < 0)
650806212f9SEric Farman 			goto out_unpin;
6515d87fbf7SEric Farman 	} else {
652e7eaf91bSEric Farman 		pa->pa_nr = 0;
6535d87fbf7SEric Farman 	}
6540a19e61eSDong Jia Shi 
6550a19e61eSDong Jia Shi 	ccw->cda = (__u32) virt_to_phys(idaws);
6560a19e61eSDong Jia Shi 	ccw->flags |= CCW_FLAG_IDA;
6570a19e61eSDong Jia Shi 
65813314605SNicolin Chen 	/* Populate the IDAL with pinned/translated addresses from page */
65913314605SNicolin Chen 	page_array_idal_create_words(pa, idaws);
6600a19e61eSDong Jia Shi 
6610a19e61eSDong Jia Shi 	return 0;
6626238f921SDong Jia Shi 
6636238f921SDong Jia Shi out_unpin:
664b5a73e8eSEric Farman 	page_array_unpin_free(pa, vdev, idal_is_2k(cp));
665e8573b39SEric Farman out_free_idaws:
666e8573b39SEric Farman 	kfree(idaws);
6676238f921SDong Jia Shi out_init:
6686238f921SDong Jia Shi 	ccw->cda = 0;
6696238f921SDong Jia Shi 	return ret;
6700a19e61eSDong Jia Shi }
6710a19e61eSDong Jia Shi 
6720a19e61eSDong Jia Shi /*
6730a19e61eSDong Jia Shi  * Fetch one ccw.
6740a19e61eSDong Jia Shi  * To reduce memory copy, we'll pin the cda page in memory,
675*cada938aSHeiko Carstens  * and to get rid of the cda 2G limitation of ccw1, we'll translate
6760a19e61eSDong Jia Shi  * direct ccws to idal ccws.
6770a19e61eSDong Jia Shi  */
ccwchain_fetch_one(struct ccw1 * ccw,struct page_array * pa,struct channel_program * cp)678a4c60404SEric Farman static int ccwchain_fetch_one(struct ccw1 *ccw,
679a4c60404SEric Farman 			      struct page_array *pa,
6800a19e61eSDong Jia Shi 			      struct channel_program *cp)
681a4c60404SEric Farman 
6820a19e61eSDong Jia Shi {
6830a19e61eSDong Jia Shi 	if (ccw_is_tic(ccw))
684a4c60404SEric Farman 		return ccwchain_fetch_tic(ccw, cp);
6850a19e61eSDong Jia Shi 
686a4c60404SEric Farman 	return ccwchain_fetch_ccw(ccw, pa, cp);
6870a19e61eSDong Jia Shi }
6880a19e61eSDong Jia Shi 
6890a19e61eSDong Jia Shi /**
6900a19e61eSDong Jia Shi  * cp_init() - allocate ccwchains for a channel program.
6910a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
6920a19e61eSDong Jia Shi  * @orb: control block for the channel program from the guest
6930a19e61eSDong Jia Shi  *
6940a19e61eSDong Jia Shi  * This creates one or more ccwchain(s), and copies the raw data of
6950a19e61eSDong Jia Shi  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
6960a19e61eSDong Jia Shi  *
6970a19e61eSDong Jia Shi  * Limitations:
698725b94d7SJared Rossi  * 1. Supports idal(c64) ccw chaining.
699725b94d7SJared Rossi  * 2. Supports 4k idaw.
7000a19e61eSDong Jia Shi  *
7010a19e61eSDong Jia Shi  * Returns:
7020a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
7030a19e61eSDong Jia Shi  */
cp_init(struct channel_program * cp,union orb * orb)7040a587956SJason Gunthorpe int cp_init(struct channel_program *cp, union orb *orb)
7050a19e61eSDong Jia Shi {
7060a587956SJason Gunthorpe 	struct vfio_device *vdev =
7070a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
708725b94d7SJared Rossi 	/* custom ratelimit used to avoid flood during guest IPL */
709725b94d7SJared Rossi 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
71099afcb05SEric Farman 	int ret;
7110a19e61eSDong Jia Shi 
712c6c82e0cSEric Farman 	/* this is an error in the caller */
713c6c82e0cSEric Farman 	if (cp->initialized)
714c6c82e0cSEric Farman 		return -EBUSY;
715c6c82e0cSEric Farman 
7160a19e61eSDong Jia Shi 	/*
717725b94d7SJared Rossi 	 * We only support prefetching the channel program. We assume all channel
718725b94d7SJared Rossi 	 * programs executed by supported guests likewise support prefetching.
719725b94d7SJared Rossi 	 * Executing a channel program that does not specify prefetching will
720725b94d7SJared Rossi 	 * typically not cause an error, but a warning is issued to help identify
721725b94d7SJared Rossi 	 * the problem if something does break.
7220a19e61eSDong Jia Shi 	 */
723725b94d7SJared Rossi 	if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
7240a587956SJason Gunthorpe 		dev_warn(
7250a587956SJason Gunthorpe 			vdev->dev,
7260a587956SJason Gunthorpe 			"Prefetching channel program even though prefetch not specified in ORB");
7270a19e61eSDong Jia Shi 
7280a19e61eSDong Jia Shi 	INIT_LIST_HEAD(&cp->ccwchain_list);
7290a19e61eSDong Jia Shi 	memcpy(&cp->orb, orb, sizeof(*orb));
7300a19e61eSDong Jia Shi 
73199afcb05SEric Farman 	/* Build a ccwchain for the first CCW segment */
73299afcb05SEric Farman 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
73399afcb05SEric Farman 
734254cb663SEric Farman 	if (!ret)
735c9f597a4SFarhan Ali 		cp->initialized = true;
736c9f597a4SFarhan Ali 
7370a19e61eSDong Jia Shi 	return ret;
7380a19e61eSDong Jia Shi }
7390a19e61eSDong Jia Shi 
7400a19e61eSDong Jia Shi 
7410a19e61eSDong Jia Shi /**
7420a19e61eSDong Jia Shi  * cp_free() - free resources for channel program.
7430a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7440a19e61eSDong Jia Shi  *
7450a19e61eSDong Jia Shi  * This unpins the memory pages and frees the memory space occupied by
7460a19e61eSDong Jia Shi  * @cp, which must have been returned by a previous call to cp_init().
7470a19e61eSDong Jia Shi  * Otherwise, undefined behavior occurs.
7480a19e61eSDong Jia Shi  */
cp_free(struct channel_program * cp)7490a19e61eSDong Jia Shi void cp_free(struct channel_program *cp)
7500a19e61eSDong Jia Shi {
7510a587956SJason Gunthorpe 	struct vfio_device *vdev =
7520a587956SJason Gunthorpe 		&container_of(cp, struct vfio_ccw_private, cp)->vdev;
753812271b9SEric Farman 	struct ccwchain *chain, *temp;
754812271b9SEric Farman 	int i;
755812271b9SEric Farman 
756812271b9SEric Farman 	if (!cp->initialized)
757812271b9SEric Farman 		return;
758812271b9SEric Farman 
759812271b9SEric Farman 	cp->initialized = false;
760812271b9SEric Farman 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
761812271b9SEric Farman 		for (i = 0; i < chain->ch_len; i++) {
762b5a73e8eSEric Farman 			page_array_unpin_free(&chain->ch_pa[i], vdev, idal_is_2k(cp));
763812271b9SEric Farman 			ccwchain_cda_free(chain, i);
764812271b9SEric Farman 		}
765812271b9SEric Farman 		ccwchain_free(chain);
766812271b9SEric Farman 	}
7670a19e61eSDong Jia Shi }
7680a19e61eSDong Jia Shi 
7690a19e61eSDong Jia Shi /**
7700a19e61eSDong Jia Shi  * cp_prefetch() - translate a guest physical address channel program to
7710a19e61eSDong Jia Shi  *                 a real-device runnable channel program.
7720a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
7730a19e61eSDong Jia Shi  *
7740a19e61eSDong Jia Shi  * This function translates the guest-physical-address channel program
7750a19e61eSDong Jia Shi  * and stores the result to ccwchain list. @cp must have been
7760a19e61eSDong Jia Shi  * initialized by a previous call with cp_init(). Otherwise, undefined
7770a19e61eSDong Jia Shi  * behavior occurs.
778d66a7355SHalil Pasic  * For each chain composing the channel program:
779d66a7355SHalil Pasic  * - On entry ch_len holds the count of CCWs to be translated.
780d66a7355SHalil Pasic  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
781d66a7355SHalil Pasic  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
7820a19e61eSDong Jia Shi  *
7830a19e61eSDong Jia Shi  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
7840a19e61eSDong Jia Shi  * as helpers to do ccw chain translation inside the kernel. Basically
7850a19e61eSDong Jia Shi  * they accept a channel program issued by a virtual machine, and
7860a19e61eSDong Jia Shi  * translate the channel program to a real-device runnable channel
7870a19e61eSDong Jia Shi  * program.
7880a19e61eSDong Jia Shi  *
7890a19e61eSDong Jia Shi  * These APIs will copy the ccws into kernel-space buffers, and update
790*cada938aSHeiko Carstens  * the guest physical addresses with their corresponding host physical
7910a19e61eSDong Jia Shi  * addresses.  Then channel I/O device drivers could issue the
7920a19e61eSDong Jia Shi  * translated channel program to real devices to perform an I/O
7930a19e61eSDong Jia Shi  * operation.
7940a19e61eSDong Jia Shi  *
7950a19e61eSDong Jia Shi  * These interfaces are designed to support translation only for
7960a19e61eSDong Jia Shi  * channel programs, which are generated and formatted by a
7970a19e61eSDong Jia Shi  * guest. Thus this will make it possible for things like VFIO to
7980a19e61eSDong Jia Shi  * leverage the interfaces to passthrough a channel I/O mediated
7990a19e61eSDong Jia Shi  * device in QEMU.
8000a19e61eSDong Jia Shi  *
8010a19e61eSDong Jia Shi  * We support direct ccw chaining by translating them to idal ccws.
8020a19e61eSDong Jia Shi  *
8030a19e61eSDong Jia Shi  * Returns:
8040a19e61eSDong Jia Shi  *   %0 on success and a negative error value on failure.
8050a19e61eSDong Jia Shi  */
cp_prefetch(struct channel_program * cp)8060a19e61eSDong Jia Shi int cp_prefetch(struct channel_program *cp)
8070a19e61eSDong Jia Shi {
8080a19e61eSDong Jia Shi 	struct ccwchain *chain;
809a4c60404SEric Farman 	struct ccw1 *ccw;
810a4c60404SEric Farman 	struct page_array *pa;
8110a19e61eSDong Jia Shi 	int len, idx, ret;
8120a19e61eSDong Jia Shi 
81371189f26SCornelia Huck 	/* this is an error in the caller */
81471189f26SCornelia Huck 	if (!cp->initialized)
81571189f26SCornelia Huck 		return -EINVAL;
81671189f26SCornelia Huck 
8170a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
8180a19e61eSDong Jia Shi 		len = chain->ch_len;
8190a19e61eSDong Jia Shi 		for (idx = 0; idx < len; idx++) {
8204b946d65SEric Farman 			ccw = &chain->ch_ccw[idx];
8214b946d65SEric Farman 			pa = &chain->ch_pa[idx];
822a4c60404SEric Farman 
823a4c60404SEric Farman 			ret = ccwchain_fetch_one(ccw, pa, cp);
8240a19e61eSDong Jia Shi 			if (ret)
825d66a7355SHalil Pasic 				goto out_err;
8260a19e61eSDong Jia Shi 		}
8270a19e61eSDong Jia Shi 	}
8280a19e61eSDong Jia Shi 
8290a19e61eSDong Jia Shi 	return 0;
830d66a7355SHalil Pasic out_err:
831d66a7355SHalil Pasic 	/* Only cleanup the chain elements that were actually translated. */
832d66a7355SHalil Pasic 	chain->ch_len = idx;
833d66a7355SHalil Pasic 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
834d66a7355SHalil Pasic 		chain->ch_len = 0;
835d66a7355SHalil Pasic 	}
836d66a7355SHalil Pasic 	return ret;
8370a19e61eSDong Jia Shi }
8380a19e61eSDong Jia Shi 
8390a19e61eSDong Jia Shi /**
8400a19e61eSDong Jia Shi  * cp_get_orb() - get the orb of the channel program
8410a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8429fbed59fSEric Farman  * @sch: subchannel the operation will be performed against
8430a19e61eSDong Jia Shi  *
8440a19e61eSDong Jia Shi  * This function returns the address of the updated orb of the channel
8450a19e61eSDong Jia Shi  * program. Channel I/O device drivers could use this orb to issue a
8460a19e61eSDong Jia Shi  * ssch.
8470a19e61eSDong Jia Shi  */
cp_get_orb(struct channel_program * cp,struct subchannel * sch)8489fbed59fSEric Farman union orb *cp_get_orb(struct channel_program *cp, struct subchannel *sch)
8490a19e61eSDong Jia Shi {
8500a19e61eSDong Jia Shi 	union orb *orb;
8510a19e61eSDong Jia Shi 	struct ccwchain *chain;
8520a19e61eSDong Jia Shi 	struct ccw1 *cpa;
8530a19e61eSDong Jia Shi 
85471189f26SCornelia Huck 	/* this is an error in the caller */
85571189f26SCornelia Huck 	if (!cp->initialized)
85671189f26SCornelia Huck 		return NULL;
85771189f26SCornelia Huck 
8580a19e61eSDong Jia Shi 	orb = &cp->orb;
8590a19e61eSDong Jia Shi 
8609fbed59fSEric Farman 	orb->cmd.intparm = (u32)virt_to_phys(sch);
8610a19e61eSDong Jia Shi 	orb->cmd.fmt = 1;
8620a19e61eSDong Jia Shi 
863254cb663SEric Farman 	/*
864254cb663SEric Farman 	 * Everything built by vfio-ccw is a Format-2 IDAL.
8651b676fe3SEric Farman 	 * If the input was a Format-1 IDAL, indicate that
8661b676fe3SEric Farman 	 * 2K Format-2 IDAWs were created here.
867254cb663SEric Farman 	 */
8681b676fe3SEric Farman 	if (!orb->cmd.c64)
8691b676fe3SEric Farman 		orb->cmd.i2k = 1;
870254cb663SEric Farman 	orb->cmd.c64 = 1;
871254cb663SEric Farman 
8720a19e61eSDong Jia Shi 	if (orb->cmd.lpm == 0)
8739fbed59fSEric Farman 		orb->cmd.lpm = sch->lpm;
8740a19e61eSDong Jia Shi 
8750a19e61eSDong Jia Shi 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
8760a19e61eSDong Jia Shi 	cpa = chain->ch_ccw;
8775de2322dSEric Farman 	orb->cmd.cpa = (__u32)virt_to_phys(cpa);
8780a19e61eSDong Jia Shi 
8790a19e61eSDong Jia Shi 	return orb;
8800a19e61eSDong Jia Shi }
8810a19e61eSDong Jia Shi 
8820a19e61eSDong Jia Shi /**
8830a19e61eSDong Jia Shi  * cp_update_scsw() - update scsw for a channel program.
8840a19e61eSDong Jia Shi  * @cp: channel_program on which to perform the operation
8850a19e61eSDong Jia Shi  * @scsw: I/O results of the channel program and also the target to be
8860a19e61eSDong Jia Shi  *        updated
8870a19e61eSDong Jia Shi  *
8880a19e61eSDong Jia Shi  * @scsw contains the I/O results of the channel program that pointed
8890a19e61eSDong Jia Shi  * to by @cp. However what @scsw->cpa stores is a host physical
8900a19e61eSDong Jia Shi  * address, which is meaningless for the guest, which is waiting for
8910a19e61eSDong Jia Shi  * the I/O results.
8920a19e61eSDong Jia Shi  *
8930a19e61eSDong Jia Shi  * This function updates @scsw->cpa to its coressponding guest physical
8940a19e61eSDong Jia Shi  * address.
8950a19e61eSDong Jia Shi  */
cp_update_scsw(struct channel_program * cp,union scsw * scsw)8960a19e61eSDong Jia Shi void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
8970a19e61eSDong Jia Shi {
8980a19e61eSDong Jia Shi 	struct ccwchain *chain;
8990a19e61eSDong Jia Shi 	u32 cpa = scsw->cmd.cpa;
9002904337fSEric Farman 	u32 ccw_head;
9010a19e61eSDong Jia Shi 
90271189f26SCornelia Huck 	if (!cp->initialized)
90371189f26SCornelia Huck 		return;
90471189f26SCornelia Huck 
9050a19e61eSDong Jia Shi 	/*
9060a19e61eSDong Jia Shi 	 * LATER:
9070a19e61eSDong Jia Shi 	 * For now, only update the cmd.cpa part. We may need to deal with
9080a19e61eSDong Jia Shi 	 * other portions of the schib as well, even if we don't return them
9090a19e61eSDong Jia Shi 	 * in the ioctl directly. Path status changes etc.
9100a19e61eSDong Jia Shi 	 */
9110a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9120a19e61eSDong Jia Shi 		ccw_head = (u32)(u64)chain->ch_ccw;
91315f0eb3dSEric Farman 		/*
91415f0eb3dSEric Farman 		 * On successful execution, cpa points just beyond the end
91515f0eb3dSEric Farman 		 * of the chain.
91615f0eb3dSEric Farman 		 */
91715f0eb3dSEric Farman 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
9180a19e61eSDong Jia Shi 			/*
9190a19e61eSDong Jia Shi 			 * (cpa - ccw_head) is the offset value of the host
9200a19e61eSDong Jia Shi 			 * physical ccw to its chain head.
9210a19e61eSDong Jia Shi 			 * Adding this value to the guest physical ccw chain
9220a19e61eSDong Jia Shi 			 * head gets us the guest cpa.
9230a19e61eSDong Jia Shi 			 */
9240a19e61eSDong Jia Shi 			cpa = chain->ch_iova + (cpa - ccw_head);
9250a19e61eSDong Jia Shi 			break;
9260a19e61eSDong Jia Shi 		}
9270a19e61eSDong Jia Shi 	}
9280a19e61eSDong Jia Shi 
9290a19e61eSDong Jia Shi 	scsw->cmd.cpa = cpa;
9300a19e61eSDong Jia Shi }
9310a19e61eSDong Jia Shi 
9320a19e61eSDong Jia Shi /**
9330a19e61eSDong Jia Shi  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
934364e3f90SSebastian Ott  * @cp: channel_program on which to perform the operation
9350a19e61eSDong Jia Shi  * @iova: the iova to check
9365a4fe7c4SEric Farman  * @length: the length to check from @iova
9370a19e61eSDong Jia Shi  *
9380a19e61eSDong Jia Shi  * If the @iova is currently pinned for the ccw chain, return true;
9390a19e61eSDong Jia Shi  * else return false.
9400a19e61eSDong Jia Shi  */
cp_iova_pinned(struct channel_program * cp,u64 iova,u64 length)9415a4fe7c4SEric Farman bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length)
9420a19e61eSDong Jia Shi {
9430a19e61eSDong Jia Shi 	struct ccwchain *chain;
9440a19e61eSDong Jia Shi 	int i;
9450a19e61eSDong Jia Shi 
94671189f26SCornelia Huck 	if (!cp->initialized)
94771189f26SCornelia Huck 		return false;
94871189f26SCornelia Huck 
9490a19e61eSDong Jia Shi 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
9500a19e61eSDong Jia Shi 		for (i = 0; i < chain->ch_len; i++)
9514b946d65SEric Farman 			if (page_array_iova_pinned(&chain->ch_pa[i], iova, length))
9520a19e61eSDong Jia Shi 				return true;
9530a19e61eSDong Jia Shi 	}
9540a19e61eSDong Jia Shi 
9550a19e61eSDong Jia Shi 	return false;
9560a19e61eSDong Jia Shi }
957