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