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