xref: /OK3568_Linux_fs/kernel/drivers/s390/cio/vfio_ccw_cp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * channel program interfaces
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright IBM Corp. 2017
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
8*4882a593Smuzhiyun  *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/ratelimit.h>
12*4882a593Smuzhiyun #include <linux/mm.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/iommu.h>
15*4882a593Smuzhiyun #include <linux/vfio.h>
16*4882a593Smuzhiyun #include <asm/idals.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "vfio_ccw_cp.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct pfn_array {
21*4882a593Smuzhiyun 	/* Starting guest physical I/O address. */
22*4882a593Smuzhiyun 	unsigned long		pa_iova;
23*4882a593Smuzhiyun 	/* Array that stores PFNs of the pages need to pin. */
24*4882a593Smuzhiyun 	unsigned long		*pa_iova_pfn;
25*4882a593Smuzhiyun 	/* Array that receives PFNs of the pages pinned. */
26*4882a593Smuzhiyun 	unsigned long		*pa_pfn;
27*4882a593Smuzhiyun 	/* Number of pages pinned from @pa_iova. */
28*4882a593Smuzhiyun 	int			pa_nr;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun struct ccwchain {
32*4882a593Smuzhiyun 	struct list_head	next;
33*4882a593Smuzhiyun 	struct ccw1		*ch_ccw;
34*4882a593Smuzhiyun 	/* Guest physical address of the current chain. */
35*4882a593Smuzhiyun 	u64			ch_iova;
36*4882a593Smuzhiyun 	/* Count of the valid ccws in chain. */
37*4882a593Smuzhiyun 	int			ch_len;
38*4882a593Smuzhiyun 	/* Pinned PAGEs for the original data. */
39*4882a593Smuzhiyun 	struct pfn_array	*ch_pa;
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * pfn_array_alloc() - alloc memory for PFNs
44*4882a593Smuzhiyun  * @pa: pfn_array on which to perform the operation
45*4882a593Smuzhiyun  * @iova: target guest physical address
46*4882a593Smuzhiyun  * @len: number of bytes that should be pinned from @iova
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * Attempt to allocate memory for PFNs.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Usage of pfn_array:
51*4882a593Smuzhiyun  * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in
52*4882a593Smuzhiyun  * this structure will be filled in by this function.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * Returns:
55*4882a593Smuzhiyun  *         0 if PFNs are allocated
56*4882a593Smuzhiyun  *   -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova_pfn is not NULL
57*4882a593Smuzhiyun  *   -ENOMEM if alloc failed
58*4882a593Smuzhiyun  */
pfn_array_alloc(struct pfn_array * pa,u64 iova,unsigned int len)59*4882a593Smuzhiyun static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	int i;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	if (pa->pa_nr || pa->pa_iova_pfn)
64*4882a593Smuzhiyun 		return -EINVAL;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	pa->pa_iova = iova;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
69*4882a593Smuzhiyun 	if (!pa->pa_nr)
70*4882a593Smuzhiyun 		return -EINVAL;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	pa->pa_iova_pfn = kcalloc(pa->pa_nr,
73*4882a593Smuzhiyun 				  sizeof(*pa->pa_iova_pfn) +
74*4882a593Smuzhiyun 				  sizeof(*pa->pa_pfn),
75*4882a593Smuzhiyun 				  GFP_KERNEL);
76*4882a593Smuzhiyun 	if (unlikely(!pa->pa_iova_pfn)) {
77*4882a593Smuzhiyun 		pa->pa_nr = 0;
78*4882a593Smuzhiyun 		return -ENOMEM;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 	pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
83*4882a593Smuzhiyun 	pa->pa_pfn[0] = -1ULL;
84*4882a593Smuzhiyun 	for (i = 1; i < pa->pa_nr; i++) {
85*4882a593Smuzhiyun 		pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
86*4882a593Smuzhiyun 		pa->pa_pfn[i] = -1ULL;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun  * pfn_array_pin() - Pin user pages in memory
94*4882a593Smuzhiyun  * @pa: pfn_array on which to perform the operation
95*4882a593Smuzhiyun  * @mdev: the mediated device to perform pin operations
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * Returns number of pages pinned upon success.
98*4882a593Smuzhiyun  * If the pin request partially succeeds, or fails completely,
99*4882a593Smuzhiyun  * all pages are left unpinned and a negative error value is returned.
100*4882a593Smuzhiyun  */
pfn_array_pin(struct pfn_array * pa,struct device * mdev)101*4882a593Smuzhiyun static int pfn_array_pin(struct pfn_array *pa, struct device *mdev)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	int ret = 0;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
106*4882a593Smuzhiyun 			     IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (ret < 0) {
109*4882a593Smuzhiyun 		goto err_out;
110*4882a593Smuzhiyun 	} else if (ret > 0 && ret != pa->pa_nr) {
111*4882a593Smuzhiyun 		vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret);
112*4882a593Smuzhiyun 		ret = -EINVAL;
113*4882a593Smuzhiyun 		goto err_out;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return ret;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun err_out:
119*4882a593Smuzhiyun 	pa->pa_nr = 0;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return ret;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* Unpin the pages before releasing the memory. */
pfn_array_unpin_free(struct pfn_array * pa,struct device * mdev)125*4882a593Smuzhiyun static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	/* Only unpin if any pages were pinned to begin with */
128*4882a593Smuzhiyun 	if (pa->pa_nr)
129*4882a593Smuzhiyun 		vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
130*4882a593Smuzhiyun 	pa->pa_nr = 0;
131*4882a593Smuzhiyun 	kfree(pa->pa_iova_pfn);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
pfn_array_iova_pinned(struct pfn_array * pa,unsigned long iova)134*4882a593Smuzhiyun static bool pfn_array_iova_pinned(struct pfn_array *pa, unsigned long iova)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	unsigned long iova_pfn = iova >> PAGE_SHIFT;
137*4882a593Smuzhiyun 	int i;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	for (i = 0; i < pa->pa_nr; i++)
140*4882a593Smuzhiyun 		if (pa->pa_iova_pfn[i] == iova_pfn)
141*4882a593Smuzhiyun 			return true;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return false;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun /* Create the list of IDAL words for a pfn_array. */
pfn_array_idal_create_words(struct pfn_array * pa,unsigned long * idaws)146*4882a593Smuzhiyun static inline void pfn_array_idal_create_words(
147*4882a593Smuzhiyun 	struct pfn_array *pa,
148*4882a593Smuzhiyun 	unsigned long *idaws)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	int i;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	/*
153*4882a593Smuzhiyun 	 * Idal words (execept the first one) rely on the memory being 4k
154*4882a593Smuzhiyun 	 * aligned. If a user virtual address is 4K aligned, then it's
155*4882a593Smuzhiyun 	 * corresponding kernel physical address will also be 4K aligned. Thus
156*4882a593Smuzhiyun 	 * there will be no problem here to simply use the phys to create an
157*4882a593Smuzhiyun 	 * idaw.
158*4882a593Smuzhiyun 	 */
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	for (i = 0; i < pa->pa_nr; i++)
161*4882a593Smuzhiyun 		idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* Adjust the first IDAW, since it may not start on a page boundary */
164*4882a593Smuzhiyun 	idaws[0] += pa->pa_iova & (PAGE_SIZE - 1);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
convert_ccw0_to_ccw1(struct ccw1 * source,unsigned long len)167*4882a593Smuzhiyun static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct ccw0 ccw0;
170*4882a593Smuzhiyun 	struct ccw1 *pccw1 = source;
171*4882a593Smuzhiyun 	int i;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	for (i = 0; i < len; i++) {
174*4882a593Smuzhiyun 		ccw0 = *(struct ccw0 *)pccw1;
175*4882a593Smuzhiyun 		if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
176*4882a593Smuzhiyun 			pccw1->cmd_code = CCW_CMD_TIC;
177*4882a593Smuzhiyun 			pccw1->flags = 0;
178*4882a593Smuzhiyun 			pccw1->count = 0;
179*4882a593Smuzhiyun 		} else {
180*4882a593Smuzhiyun 			pccw1->cmd_code = ccw0.cmd_code;
181*4882a593Smuzhiyun 			pccw1->flags = ccw0.flags;
182*4882a593Smuzhiyun 			pccw1->count = ccw0.count;
183*4882a593Smuzhiyun 		}
184*4882a593Smuzhiyun 		pccw1->cda = ccw0.cda;
185*4882a593Smuzhiyun 		pccw1++;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun  * Within the domain (@mdev), copy @n bytes from a guest physical
191*4882a593Smuzhiyun  * address (@iova) to a host physical address (@to).
192*4882a593Smuzhiyun  */
copy_from_iova(struct device * mdev,void * to,u64 iova,unsigned long n)193*4882a593Smuzhiyun static long copy_from_iova(struct device *mdev,
194*4882a593Smuzhiyun 			   void *to, u64 iova,
195*4882a593Smuzhiyun 			   unsigned long n)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct pfn_array pa = {0};
198*4882a593Smuzhiyun 	u64 from;
199*4882a593Smuzhiyun 	int i, ret;
200*4882a593Smuzhiyun 	unsigned long l, m;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	ret = pfn_array_alloc(&pa, iova, n);
203*4882a593Smuzhiyun 	if (ret < 0)
204*4882a593Smuzhiyun 		return ret;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	ret = pfn_array_pin(&pa, mdev);
207*4882a593Smuzhiyun 	if (ret < 0) {
208*4882a593Smuzhiyun 		pfn_array_unpin_free(&pa, mdev);
209*4882a593Smuzhiyun 		return ret;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	l = n;
213*4882a593Smuzhiyun 	for (i = 0; i < pa.pa_nr; i++) {
214*4882a593Smuzhiyun 		from = pa.pa_pfn[i] << PAGE_SHIFT;
215*4882a593Smuzhiyun 		m = PAGE_SIZE;
216*4882a593Smuzhiyun 		if (i == 0) {
217*4882a593Smuzhiyun 			from += iova & (PAGE_SIZE - 1);
218*4882a593Smuzhiyun 			m -= iova & (PAGE_SIZE - 1);
219*4882a593Smuzhiyun 		}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		m = min(l, m);
222*4882a593Smuzhiyun 		memcpy(to + (n - l), (void *)from, m);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		l -= m;
225*4882a593Smuzhiyun 		if (l == 0)
226*4882a593Smuzhiyun 			break;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	pfn_array_unpin_free(&pa, mdev);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	return l;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  * Helpers to operate ccwchain.
236*4882a593Smuzhiyun  */
237*4882a593Smuzhiyun #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
238*4882a593Smuzhiyun #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
239*4882a593Smuzhiyun #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
246*4882a593Smuzhiyun #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun  * ccw_does_data_transfer()
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * Determine whether a CCW will move any data, such that the guest pages
254*4882a593Smuzhiyun  * would need to be pinned before performing the I/O.
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * Returns 1 if yes, 0 if no.
257*4882a593Smuzhiyun  */
ccw_does_data_transfer(struct ccw1 * ccw)258*4882a593Smuzhiyun static inline int ccw_does_data_transfer(struct ccw1 *ccw)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	/* If the count field is zero, then no data will be transferred */
261*4882a593Smuzhiyun 	if (ccw->count == 0)
262*4882a593Smuzhiyun 		return 0;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/* If the command is a NOP, then no data will be transferred */
265*4882a593Smuzhiyun 	if (ccw_is_noop(ccw))
266*4882a593Smuzhiyun 		return 0;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/* If the skip flag is off, then data will be transferred */
269*4882a593Smuzhiyun 	if (!ccw_is_skip(ccw))
270*4882a593Smuzhiyun 		return 1;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	/*
273*4882a593Smuzhiyun 	 * If the skip flag is on, it is only meaningful if the command
274*4882a593Smuzhiyun 	 * code is a read, read backward, sense, or sense ID.  In those
275*4882a593Smuzhiyun 	 * cases, no data will be transferred.
276*4882a593Smuzhiyun 	 */
277*4882a593Smuzhiyun 	if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
278*4882a593Smuzhiyun 		return 0;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	if (ccw_is_sense(ccw))
281*4882a593Smuzhiyun 		return 0;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	/* The skip flag is on, but it is ignored for this command code. */
284*4882a593Smuzhiyun 	return 1;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun  * is_cpa_within_range()
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * @cpa: channel program address being questioned
291*4882a593Smuzhiyun  * @head: address of the beginning of a CCW chain
292*4882a593Smuzhiyun  * @len: number of CCWs within the chain
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * Determine whether the address of a CCW (whether a new chain,
295*4882a593Smuzhiyun  * or the target of a TIC) falls within a range (including the end points).
296*4882a593Smuzhiyun  *
297*4882a593Smuzhiyun  * Returns 1 if yes, 0 if no.
298*4882a593Smuzhiyun  */
is_cpa_within_range(u32 cpa,u32 head,int len)299*4882a593Smuzhiyun static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	u32 tail = head + (len - 1) * sizeof(struct ccw1);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return (head <= cpa && cpa <= tail);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
is_tic_within_range(struct ccw1 * ccw,u32 head,int len)306*4882a593Smuzhiyun static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	if (!ccw_is_tic(ccw))
309*4882a593Smuzhiyun 		return 0;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	return is_cpa_within_range(ccw->cda, head, len);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun 
ccwchain_alloc(struct channel_program * cp,int len)314*4882a593Smuzhiyun static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct ccwchain *chain;
317*4882a593Smuzhiyun 	void *data;
318*4882a593Smuzhiyun 	size_t size;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* Make ccw address aligned to 8. */
321*4882a593Smuzhiyun 	size = ((sizeof(*chain) + 7L) & -8L) +
322*4882a593Smuzhiyun 		sizeof(*chain->ch_ccw) * len +
323*4882a593Smuzhiyun 		sizeof(*chain->ch_pa) * len;
324*4882a593Smuzhiyun 	chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
325*4882a593Smuzhiyun 	if (!chain)
326*4882a593Smuzhiyun 		return NULL;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
329*4882a593Smuzhiyun 	chain->ch_ccw = (struct ccw1 *)data;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
332*4882a593Smuzhiyun 	chain->ch_pa = (struct pfn_array *)data;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	chain->ch_len = len;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	list_add_tail(&chain->next, &cp->ccwchain_list);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	return chain;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
ccwchain_free(struct ccwchain * chain)341*4882a593Smuzhiyun static void ccwchain_free(struct ccwchain *chain)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	list_del(&chain->next);
344*4882a593Smuzhiyun 	kfree(chain);
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /* Free resource for a ccw that allocated memory for its cda. */
ccwchain_cda_free(struct ccwchain * chain,int idx)348*4882a593Smuzhiyun static void ccwchain_cda_free(struct ccwchain *chain, int idx)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	struct ccw1 *ccw = chain->ch_ccw + idx;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (ccw_is_tic(ccw))
353*4882a593Smuzhiyun 		return;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	kfree((void *)(u64)ccw->cda);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /**
359*4882a593Smuzhiyun  * ccwchain_calc_length - calculate the length of the ccw chain.
360*4882a593Smuzhiyun  * @iova: guest physical address of the target ccw chain
361*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
362*4882a593Smuzhiyun  *
363*4882a593Smuzhiyun  * This is the chain length not considering any TICs.
364*4882a593Smuzhiyun  * You need to do a new round for each TIC target.
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  * The program is also validated for absence of not yet supported
367*4882a593Smuzhiyun  * indirect data addressing scenarios.
368*4882a593Smuzhiyun  *
369*4882a593Smuzhiyun  * Returns: the length of the ccw chain or -errno.
370*4882a593Smuzhiyun  */
ccwchain_calc_length(u64 iova,struct channel_program * cp)371*4882a593Smuzhiyun static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	struct ccw1 *ccw = cp->guest_cp;
374*4882a593Smuzhiyun 	int cnt = 0;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	do {
377*4882a593Smuzhiyun 		cnt++;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		/*
380*4882a593Smuzhiyun 		 * As we don't want to fail direct addressing even if the
381*4882a593Smuzhiyun 		 * orb specified one of the unsupported formats, we defer
382*4882a593Smuzhiyun 		 * checking for IDAWs in unsupported formats to here.
383*4882a593Smuzhiyun 		 */
384*4882a593Smuzhiyun 		if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw))
385*4882a593Smuzhiyun 			return -EOPNOTSUPP;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		/*
388*4882a593Smuzhiyun 		 * We want to keep counting if the current CCW has the
389*4882a593Smuzhiyun 		 * command-chaining flag enabled, or if it is a TIC CCW
390*4882a593Smuzhiyun 		 * that loops back into the current chain.  The latter
391*4882a593Smuzhiyun 		 * is used for device orientation, where the CCW PRIOR to
392*4882a593Smuzhiyun 		 * the TIC can either jump to the TIC or a CCW immediately
393*4882a593Smuzhiyun 		 * after the TIC, depending on the results of its operation.
394*4882a593Smuzhiyun 		 */
395*4882a593Smuzhiyun 		if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
396*4882a593Smuzhiyun 			break;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 		ccw++;
399*4882a593Smuzhiyun 	} while (cnt < CCWCHAIN_LEN_MAX + 1);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	if (cnt == CCWCHAIN_LEN_MAX + 1)
402*4882a593Smuzhiyun 		cnt = -EINVAL;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	return cnt;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
tic_target_chain_exists(struct ccw1 * tic,struct channel_program * cp)407*4882a593Smuzhiyun static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	struct ccwchain *chain;
410*4882a593Smuzhiyun 	u32 ccw_head;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
413*4882a593Smuzhiyun 		ccw_head = chain->ch_iova;
414*4882a593Smuzhiyun 		if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
415*4882a593Smuzhiyun 			return 1;
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	return 0;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun static int ccwchain_loop_tic(struct ccwchain *chain,
422*4882a593Smuzhiyun 			     struct channel_program *cp);
423*4882a593Smuzhiyun 
ccwchain_handle_ccw(u32 cda,struct channel_program * cp)424*4882a593Smuzhiyun static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	struct ccwchain *chain;
427*4882a593Smuzhiyun 	int len, ret;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	/* Copy 2K (the most we support today) of possible CCWs */
430*4882a593Smuzhiyun 	len = copy_from_iova(cp->mdev, cp->guest_cp, cda,
431*4882a593Smuzhiyun 			     CCWCHAIN_LEN_MAX * sizeof(struct ccw1));
432*4882a593Smuzhiyun 	if (len)
433*4882a593Smuzhiyun 		return len;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/* Convert any Format-0 CCWs to Format-1 */
436*4882a593Smuzhiyun 	if (!cp->orb.cmd.fmt)
437*4882a593Smuzhiyun 		convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	/* Count the CCWs in the current chain */
440*4882a593Smuzhiyun 	len = ccwchain_calc_length(cda, cp);
441*4882a593Smuzhiyun 	if (len < 0)
442*4882a593Smuzhiyun 		return len;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* Need alloc a new chain for this one. */
445*4882a593Smuzhiyun 	chain = ccwchain_alloc(cp, len);
446*4882a593Smuzhiyun 	if (!chain)
447*4882a593Smuzhiyun 		return -ENOMEM;
448*4882a593Smuzhiyun 	chain->ch_iova = cda;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	/* Copy the actual CCWs into the new chain */
451*4882a593Smuzhiyun 	memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	/* Loop for tics on this new chain. */
454*4882a593Smuzhiyun 	ret = ccwchain_loop_tic(chain, cp);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	if (ret)
457*4882a593Smuzhiyun 		ccwchain_free(chain);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	return ret;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun /* Loop for TICs. */
ccwchain_loop_tic(struct ccwchain * chain,struct channel_program * cp)463*4882a593Smuzhiyun static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	struct ccw1 *tic;
466*4882a593Smuzhiyun 	int i, ret;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	for (i = 0; i < chain->ch_len; i++) {
469*4882a593Smuzhiyun 		tic = chain->ch_ccw + i;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 		if (!ccw_is_tic(tic))
472*4882a593Smuzhiyun 			continue;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 		/* May transfer to an existing chain. */
475*4882a593Smuzhiyun 		if (tic_target_chain_exists(tic, cp))
476*4882a593Smuzhiyun 			continue;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 		/* Build a ccwchain for the next segment */
479*4882a593Smuzhiyun 		ret = ccwchain_handle_ccw(tic->cda, cp);
480*4882a593Smuzhiyun 		if (ret)
481*4882a593Smuzhiyun 			return ret;
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
ccwchain_fetch_tic(struct ccwchain * chain,int idx,struct channel_program * cp)487*4882a593Smuzhiyun static int ccwchain_fetch_tic(struct ccwchain *chain,
488*4882a593Smuzhiyun 			      int idx,
489*4882a593Smuzhiyun 			      struct channel_program *cp)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun 	struct ccw1 *ccw = chain->ch_ccw + idx;
492*4882a593Smuzhiyun 	struct ccwchain *iter;
493*4882a593Smuzhiyun 	u32 ccw_head;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	list_for_each_entry(iter, &cp->ccwchain_list, next) {
496*4882a593Smuzhiyun 		ccw_head = iter->ch_iova;
497*4882a593Smuzhiyun 		if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
498*4882a593Smuzhiyun 			ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
499*4882a593Smuzhiyun 						     (ccw->cda - ccw_head));
500*4882a593Smuzhiyun 			return 0;
501*4882a593Smuzhiyun 		}
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	return -EFAULT;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
ccwchain_fetch_direct(struct ccwchain * chain,int idx,struct channel_program * cp)507*4882a593Smuzhiyun static int ccwchain_fetch_direct(struct ccwchain *chain,
508*4882a593Smuzhiyun 				 int idx,
509*4882a593Smuzhiyun 				 struct channel_program *cp)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	struct ccw1 *ccw;
512*4882a593Smuzhiyun 	struct pfn_array *pa;
513*4882a593Smuzhiyun 	u64 iova;
514*4882a593Smuzhiyun 	unsigned long *idaws;
515*4882a593Smuzhiyun 	int ret;
516*4882a593Smuzhiyun 	int bytes = 1;
517*4882a593Smuzhiyun 	int idaw_nr, idal_len;
518*4882a593Smuzhiyun 	int i;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	ccw = chain->ch_ccw + idx;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	if (ccw->count)
523*4882a593Smuzhiyun 		bytes = ccw->count;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	/* Calculate size of IDAL */
526*4882a593Smuzhiyun 	if (ccw_is_idal(ccw)) {
527*4882a593Smuzhiyun 		/* Read first IDAW to see if it's 4K-aligned or not. */
528*4882a593Smuzhiyun 		/* All subsequent IDAws will be 4K-aligned. */
529*4882a593Smuzhiyun 		ret = copy_from_iova(cp->mdev, &iova, ccw->cda, sizeof(iova));
530*4882a593Smuzhiyun 		if (ret)
531*4882a593Smuzhiyun 			return ret;
532*4882a593Smuzhiyun 	} else {
533*4882a593Smuzhiyun 		iova = ccw->cda;
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 	idaw_nr = idal_nr_words((void *)iova, bytes);
536*4882a593Smuzhiyun 	idal_len = idaw_nr * sizeof(*idaws);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/* Allocate an IDAL from host storage */
539*4882a593Smuzhiyun 	idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
540*4882a593Smuzhiyun 	if (!idaws) {
541*4882a593Smuzhiyun 		ret = -ENOMEM;
542*4882a593Smuzhiyun 		goto out_init;
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/*
546*4882a593Smuzhiyun 	 * Allocate an array of pfn's for pages to pin/translate.
547*4882a593Smuzhiyun 	 * The number of pages is actually the count of the idaws
548*4882a593Smuzhiyun 	 * required for the data transfer, since we only only support
549*4882a593Smuzhiyun 	 * 4K IDAWs today.
550*4882a593Smuzhiyun 	 */
551*4882a593Smuzhiyun 	pa = chain->ch_pa + idx;
552*4882a593Smuzhiyun 	ret = pfn_array_alloc(pa, iova, bytes);
553*4882a593Smuzhiyun 	if (ret < 0)
554*4882a593Smuzhiyun 		goto out_free_idaws;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	if (ccw_is_idal(ccw)) {
557*4882a593Smuzhiyun 		/* Copy guest IDAL into host IDAL */
558*4882a593Smuzhiyun 		ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idal_len);
559*4882a593Smuzhiyun 		if (ret)
560*4882a593Smuzhiyun 			goto out_unpin;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 		/*
563*4882a593Smuzhiyun 		 * Copy guest IDAWs into pfn_array, in case the memory they
564*4882a593Smuzhiyun 		 * occupy is not contiguous.
565*4882a593Smuzhiyun 		 */
566*4882a593Smuzhiyun 		for (i = 0; i < idaw_nr; i++)
567*4882a593Smuzhiyun 			pa->pa_iova_pfn[i] = idaws[i] >> PAGE_SHIFT;
568*4882a593Smuzhiyun 	} else {
569*4882a593Smuzhiyun 		/*
570*4882a593Smuzhiyun 		 * No action is required here; the iova addresses in pfn_array
571*4882a593Smuzhiyun 		 * were initialized sequentially in pfn_array_alloc() beginning
572*4882a593Smuzhiyun 		 * with the contents of ccw->cda.
573*4882a593Smuzhiyun 		 */
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	if (ccw_does_data_transfer(ccw)) {
577*4882a593Smuzhiyun 		ret = pfn_array_pin(pa, cp->mdev);
578*4882a593Smuzhiyun 		if (ret < 0)
579*4882a593Smuzhiyun 			goto out_unpin;
580*4882a593Smuzhiyun 	} else {
581*4882a593Smuzhiyun 		pa->pa_nr = 0;
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	ccw->cda = (__u32) virt_to_phys(idaws);
585*4882a593Smuzhiyun 	ccw->flags |= CCW_FLAG_IDA;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* Populate the IDAL with pinned/translated addresses from pfn */
588*4882a593Smuzhiyun 	pfn_array_idal_create_words(pa, idaws);
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	return 0;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun out_unpin:
593*4882a593Smuzhiyun 	pfn_array_unpin_free(pa, cp->mdev);
594*4882a593Smuzhiyun out_free_idaws:
595*4882a593Smuzhiyun 	kfree(idaws);
596*4882a593Smuzhiyun out_init:
597*4882a593Smuzhiyun 	ccw->cda = 0;
598*4882a593Smuzhiyun 	return ret;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun  * Fetch one ccw.
603*4882a593Smuzhiyun  * To reduce memory copy, we'll pin the cda page in memory,
604*4882a593Smuzhiyun  * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
605*4882a593Smuzhiyun  * direct ccws to idal ccws.
606*4882a593Smuzhiyun  */
ccwchain_fetch_one(struct ccwchain * chain,int idx,struct channel_program * cp)607*4882a593Smuzhiyun static int ccwchain_fetch_one(struct ccwchain *chain,
608*4882a593Smuzhiyun 			      int idx,
609*4882a593Smuzhiyun 			      struct channel_program *cp)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	struct ccw1 *ccw = chain->ch_ccw + idx;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	if (ccw_is_tic(ccw))
614*4882a593Smuzhiyun 		return ccwchain_fetch_tic(chain, idx, cp);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	return ccwchain_fetch_direct(chain, idx, cp);
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun /**
620*4882a593Smuzhiyun  * cp_init() - allocate ccwchains for a channel program.
621*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
622*4882a593Smuzhiyun  * @mdev: the mediated device to perform pin/unpin operations
623*4882a593Smuzhiyun  * @orb: control block for the channel program from the guest
624*4882a593Smuzhiyun  *
625*4882a593Smuzhiyun  * This creates one or more ccwchain(s), and copies the raw data of
626*4882a593Smuzhiyun  * the target channel program from @orb->cmd.iova to the new ccwchain(s).
627*4882a593Smuzhiyun  *
628*4882a593Smuzhiyun  * Limitations:
629*4882a593Smuzhiyun  * 1. Supports idal(c64) ccw chaining.
630*4882a593Smuzhiyun  * 2. Supports 4k idaw.
631*4882a593Smuzhiyun  *
632*4882a593Smuzhiyun  * Returns:
633*4882a593Smuzhiyun  *   %0 on success and a negative error value on failure.
634*4882a593Smuzhiyun  */
cp_init(struct channel_program * cp,struct device * mdev,union orb * orb)635*4882a593Smuzhiyun int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	/* custom ratelimit used to avoid flood during guest IPL */
638*4882a593Smuzhiyun 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
639*4882a593Smuzhiyun 	int ret;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	/* this is an error in the caller */
642*4882a593Smuzhiyun 	if (cp->initialized)
643*4882a593Smuzhiyun 		return -EBUSY;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	/*
646*4882a593Smuzhiyun 	 * We only support prefetching the channel program. We assume all channel
647*4882a593Smuzhiyun 	 * programs executed by supported guests likewise support prefetching.
648*4882a593Smuzhiyun 	 * Executing a channel program that does not specify prefetching will
649*4882a593Smuzhiyun 	 * typically not cause an error, but a warning is issued to help identify
650*4882a593Smuzhiyun 	 * the problem if something does break.
651*4882a593Smuzhiyun 	 */
652*4882a593Smuzhiyun 	if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
653*4882a593Smuzhiyun 		dev_warn(mdev, "Prefetching channel program even though prefetch not specified in ORB");
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cp->ccwchain_list);
656*4882a593Smuzhiyun 	memcpy(&cp->orb, orb, sizeof(*orb));
657*4882a593Smuzhiyun 	cp->mdev = mdev;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	/* Build a ccwchain for the first CCW segment */
660*4882a593Smuzhiyun 	ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	if (!ret) {
663*4882a593Smuzhiyun 		cp->initialized = true;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 		/* It is safe to force: if it was not set but idals used
666*4882a593Smuzhiyun 		 * ccwchain_calc_length would have returned an error.
667*4882a593Smuzhiyun 		 */
668*4882a593Smuzhiyun 		cp->orb.cmd.c64 = 1;
669*4882a593Smuzhiyun 	}
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	return ret;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun /**
676*4882a593Smuzhiyun  * cp_free() - free resources for channel program.
677*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
678*4882a593Smuzhiyun  *
679*4882a593Smuzhiyun  * This unpins the memory pages and frees the memory space occupied by
680*4882a593Smuzhiyun  * @cp, which must have been returned by a previous call to cp_init().
681*4882a593Smuzhiyun  * Otherwise, undefined behavior occurs.
682*4882a593Smuzhiyun  */
cp_free(struct channel_program * cp)683*4882a593Smuzhiyun void cp_free(struct channel_program *cp)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	struct ccwchain *chain, *temp;
686*4882a593Smuzhiyun 	int i;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	if (!cp->initialized)
689*4882a593Smuzhiyun 		return;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	cp->initialized = false;
692*4882a593Smuzhiyun 	list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
693*4882a593Smuzhiyun 		for (i = 0; i < chain->ch_len; i++) {
694*4882a593Smuzhiyun 			pfn_array_unpin_free(chain->ch_pa + i, cp->mdev);
695*4882a593Smuzhiyun 			ccwchain_cda_free(chain, i);
696*4882a593Smuzhiyun 		}
697*4882a593Smuzhiyun 		ccwchain_free(chain);
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun /**
702*4882a593Smuzhiyun  * cp_prefetch() - translate a guest physical address channel program to
703*4882a593Smuzhiyun  *                 a real-device runnable channel program.
704*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
705*4882a593Smuzhiyun  *
706*4882a593Smuzhiyun  * This function translates the guest-physical-address channel program
707*4882a593Smuzhiyun  * and stores the result to ccwchain list. @cp must have been
708*4882a593Smuzhiyun  * initialized by a previous call with cp_init(). Otherwise, undefined
709*4882a593Smuzhiyun  * behavior occurs.
710*4882a593Smuzhiyun  * For each chain composing the channel program:
711*4882a593Smuzhiyun  * - On entry ch_len holds the count of CCWs to be translated.
712*4882a593Smuzhiyun  * - On exit ch_len is adjusted to the count of successfully translated CCWs.
713*4882a593Smuzhiyun  * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
716*4882a593Smuzhiyun  * as helpers to do ccw chain translation inside the kernel. Basically
717*4882a593Smuzhiyun  * they accept a channel program issued by a virtual machine, and
718*4882a593Smuzhiyun  * translate the channel program to a real-device runnable channel
719*4882a593Smuzhiyun  * program.
720*4882a593Smuzhiyun  *
721*4882a593Smuzhiyun  * These APIs will copy the ccws into kernel-space buffers, and update
722*4882a593Smuzhiyun  * the guest phsical addresses with their corresponding host physical
723*4882a593Smuzhiyun  * addresses.  Then channel I/O device drivers could issue the
724*4882a593Smuzhiyun  * translated channel program to real devices to perform an I/O
725*4882a593Smuzhiyun  * operation.
726*4882a593Smuzhiyun  *
727*4882a593Smuzhiyun  * These interfaces are designed to support translation only for
728*4882a593Smuzhiyun  * channel programs, which are generated and formatted by a
729*4882a593Smuzhiyun  * guest. Thus this will make it possible for things like VFIO to
730*4882a593Smuzhiyun  * leverage the interfaces to passthrough a channel I/O mediated
731*4882a593Smuzhiyun  * device in QEMU.
732*4882a593Smuzhiyun  *
733*4882a593Smuzhiyun  * We support direct ccw chaining by translating them to idal ccws.
734*4882a593Smuzhiyun  *
735*4882a593Smuzhiyun  * Returns:
736*4882a593Smuzhiyun  *   %0 on success and a negative error value on failure.
737*4882a593Smuzhiyun  */
cp_prefetch(struct channel_program * cp)738*4882a593Smuzhiyun int cp_prefetch(struct channel_program *cp)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	struct ccwchain *chain;
741*4882a593Smuzhiyun 	int len, idx, ret;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* this is an error in the caller */
744*4882a593Smuzhiyun 	if (!cp->initialized)
745*4882a593Smuzhiyun 		return -EINVAL;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
748*4882a593Smuzhiyun 		len = chain->ch_len;
749*4882a593Smuzhiyun 		for (idx = 0; idx < len; idx++) {
750*4882a593Smuzhiyun 			ret = ccwchain_fetch_one(chain, idx, cp);
751*4882a593Smuzhiyun 			if (ret)
752*4882a593Smuzhiyun 				goto out_err;
753*4882a593Smuzhiyun 		}
754*4882a593Smuzhiyun 	}
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	return 0;
757*4882a593Smuzhiyun out_err:
758*4882a593Smuzhiyun 	/* Only cleanup the chain elements that were actually translated. */
759*4882a593Smuzhiyun 	chain->ch_len = idx;
760*4882a593Smuzhiyun 	list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
761*4882a593Smuzhiyun 		chain->ch_len = 0;
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 	return ret;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun /**
767*4882a593Smuzhiyun  * cp_get_orb() - get the orb of the channel program
768*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
769*4882a593Smuzhiyun  * @intparm: new intparm for the returned orb
770*4882a593Smuzhiyun  * @lpm: candidate value of the logical-path mask for the returned orb
771*4882a593Smuzhiyun  *
772*4882a593Smuzhiyun  * This function returns the address of the updated orb of the channel
773*4882a593Smuzhiyun  * program. Channel I/O device drivers could use this orb to issue a
774*4882a593Smuzhiyun  * ssch.
775*4882a593Smuzhiyun  */
cp_get_orb(struct channel_program * cp,u32 intparm,u8 lpm)776*4882a593Smuzhiyun union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun 	union orb *orb;
779*4882a593Smuzhiyun 	struct ccwchain *chain;
780*4882a593Smuzhiyun 	struct ccw1 *cpa;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	/* this is an error in the caller */
783*4882a593Smuzhiyun 	if (!cp->initialized)
784*4882a593Smuzhiyun 		return NULL;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	orb = &cp->orb;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	orb->cmd.intparm = intparm;
789*4882a593Smuzhiyun 	orb->cmd.fmt = 1;
790*4882a593Smuzhiyun 	orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	if (orb->cmd.lpm == 0)
793*4882a593Smuzhiyun 		orb->cmd.lpm = lpm;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
796*4882a593Smuzhiyun 	cpa = chain->ch_ccw;
797*4882a593Smuzhiyun 	orb->cmd.cpa = (__u32) __pa(cpa);
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	return orb;
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun /**
803*4882a593Smuzhiyun  * cp_update_scsw() - update scsw for a channel program.
804*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
805*4882a593Smuzhiyun  * @scsw: I/O results of the channel program and also the target to be
806*4882a593Smuzhiyun  *        updated
807*4882a593Smuzhiyun  *
808*4882a593Smuzhiyun  * @scsw contains the I/O results of the channel program that pointed
809*4882a593Smuzhiyun  * to by @cp. However what @scsw->cpa stores is a host physical
810*4882a593Smuzhiyun  * address, which is meaningless for the guest, which is waiting for
811*4882a593Smuzhiyun  * the I/O results.
812*4882a593Smuzhiyun  *
813*4882a593Smuzhiyun  * This function updates @scsw->cpa to its coressponding guest physical
814*4882a593Smuzhiyun  * address.
815*4882a593Smuzhiyun  */
cp_update_scsw(struct channel_program * cp,union scsw * scsw)816*4882a593Smuzhiyun void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun 	struct ccwchain *chain;
819*4882a593Smuzhiyun 	u32 cpa = scsw->cmd.cpa;
820*4882a593Smuzhiyun 	u32 ccw_head;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (!cp->initialized)
823*4882a593Smuzhiyun 		return;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	/*
826*4882a593Smuzhiyun 	 * LATER:
827*4882a593Smuzhiyun 	 * For now, only update the cmd.cpa part. We may need to deal with
828*4882a593Smuzhiyun 	 * other portions of the schib as well, even if we don't return them
829*4882a593Smuzhiyun 	 * in the ioctl directly. Path status changes etc.
830*4882a593Smuzhiyun 	 */
831*4882a593Smuzhiyun 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
832*4882a593Smuzhiyun 		ccw_head = (u32)(u64)chain->ch_ccw;
833*4882a593Smuzhiyun 		/*
834*4882a593Smuzhiyun 		 * On successful execution, cpa points just beyond the end
835*4882a593Smuzhiyun 		 * of the chain.
836*4882a593Smuzhiyun 		 */
837*4882a593Smuzhiyun 		if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
838*4882a593Smuzhiyun 			/*
839*4882a593Smuzhiyun 			 * (cpa - ccw_head) is the offset value of the host
840*4882a593Smuzhiyun 			 * physical ccw to its chain head.
841*4882a593Smuzhiyun 			 * Adding this value to the guest physical ccw chain
842*4882a593Smuzhiyun 			 * head gets us the guest cpa.
843*4882a593Smuzhiyun 			 */
844*4882a593Smuzhiyun 			cpa = chain->ch_iova + (cpa - ccw_head);
845*4882a593Smuzhiyun 			break;
846*4882a593Smuzhiyun 		}
847*4882a593Smuzhiyun 	}
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	scsw->cmd.cpa = cpa;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun /**
853*4882a593Smuzhiyun  * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
854*4882a593Smuzhiyun  * @cp: channel_program on which to perform the operation
855*4882a593Smuzhiyun  * @iova: the iova to check
856*4882a593Smuzhiyun  *
857*4882a593Smuzhiyun  * If the @iova is currently pinned for the ccw chain, return true;
858*4882a593Smuzhiyun  * else return false.
859*4882a593Smuzhiyun  */
cp_iova_pinned(struct channel_program * cp,u64 iova)860*4882a593Smuzhiyun bool cp_iova_pinned(struct channel_program *cp, u64 iova)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun 	struct ccwchain *chain;
863*4882a593Smuzhiyun 	int i;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	if (!cp->initialized)
866*4882a593Smuzhiyun 		return false;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	list_for_each_entry(chain, &cp->ccwchain_list, next) {
869*4882a593Smuzhiyun 		for (i = 0; i < chain->ch_len; i++)
870*4882a593Smuzhiyun 			if (pfn_array_iova_pinned(chain->ch_pa + i, iova))
871*4882a593Smuzhiyun 				return true;
872*4882a593Smuzhiyun 	}
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	return false;
875*4882a593Smuzhiyun }
876