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