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