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/ratelimit.h>
12 #include <linux/mm.h>
13 #include <linux/slab.h>
14 #include <linux/highmem.h>
15 #include <linux/iommu.h>
16 #include <linux/vfio.h>
17 #include <asm/idals.h>
18
19 #include "vfio_ccw_cp.h"
20 #include "vfio_ccw_private.h"
21
22 struct page_array {
23 /* Array that stores pages need to pin. */
24 dma_addr_t *pa_iova;
25 /* Array that receives the pinned pages. */
26 struct page **pa_page;
27 /* Number of pages pinned from @pa_iova. */
28 int pa_nr;
29 };
30
31 struct ccwchain {
32 struct list_head next;
33 struct ccw1 *ch_ccw;
34 /* Guest physical address of the current chain. */
35 u64 ch_iova;
36 /* Count of the valid ccws in chain. */
37 int ch_len;
38 /* Pinned PAGEs for the original data. */
39 struct page_array *ch_pa;
40 };
41
42 /*
43 * page_array_alloc() - alloc memory for page array
44 * @pa: page_array on which to perform the operation
45 * @len: number of pages that should be pinned from @iova
46 *
47 * Attempt to allocate memory for page array.
48 *
49 * Usage of page_array:
50 * We expect (pa_nr == 0) and (pa_iova == NULL), any field in
51 * this structure will be filled in by this function.
52 *
53 * Returns:
54 * 0 if page array is allocated
55 * -EINVAL if pa->pa_nr is not initially zero, or pa->pa_iova is not NULL
56 * -ENOMEM if alloc failed
57 */
page_array_alloc(struct page_array * pa,unsigned int len)58 static int page_array_alloc(struct page_array *pa, unsigned int len)
59 {
60 if (pa->pa_nr || pa->pa_iova)
61 return -EINVAL;
62
63 if (len == 0)
64 return -EINVAL;
65
66 pa->pa_nr = len;
67
68 pa->pa_iova = kzalloc_objs(*pa->pa_iova, len);
69 if (!pa->pa_iova)
70 return -ENOMEM;
71
72 pa->pa_page = kzalloc_objs(*pa->pa_page, len);
73 if (!pa->pa_page) {
74 kfree(pa->pa_iova);
75 return -ENOMEM;
76 }
77
78 return 0;
79 }
80
81 /*
82 * page_array_unpin() - Unpin user pages in memory
83 * @pa: page_array on which to perform the operation
84 * @vdev: the vfio device to perform the operation
85 * @pa_nr: number of user pages to unpin
86 * @unaligned: were pages unaligned on the pin request
87 *
88 * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0,
89 * otherwise only clear pa->pa_nr
90 */
page_array_unpin(struct page_array * pa,struct vfio_device * vdev,int pa_nr,bool unaligned)91 static void page_array_unpin(struct page_array *pa,
92 struct vfio_device *vdev, int pa_nr, bool unaligned)
93 {
94 int unpinned = 0, npage = 1;
95
96 while (unpinned < pa_nr) {
97 dma_addr_t *first = &pa->pa_iova[unpinned];
98 dma_addr_t *last = &first[npage];
99
100 if (unpinned + npage < pa_nr &&
101 *first + npage * PAGE_SIZE == *last &&
102 !unaligned) {
103 npage++;
104 continue;
105 }
106
107 vfio_unpin_pages(vdev, *first, npage);
108 unpinned += npage;
109 npage = 1;
110 }
111
112 pa->pa_nr = 0;
113 }
114
115 /*
116 * page_array_pin() - Pin user pages in memory
117 * @pa: page_array on which to perform the operation
118 * @vdev: the vfio device to perform pin operations
119 * @unaligned: are pages aligned to 4K boundary?
120 *
121 * Returns number of pages pinned upon success.
122 * If the pin request partially succeeds, or fails completely,
123 * all pages are left unpinned and a negative error value is returned.
124 *
125 * Requests to pin "aligned" pages can be coalesced into a single
126 * vfio_pin_pages request for the sake of efficiency, based on the
127 * expectation of 4K page requests. Unaligned requests are probably
128 * dealing with 2K "pages", and cannot be coalesced without
129 * reworking this logic to incorporate that math.
130 */
page_array_pin(struct page_array * pa,struct vfio_device * vdev,bool unaligned)131 static int page_array_pin(struct page_array *pa, struct vfio_device *vdev, bool unaligned)
132 {
133 int pinned = 0, npage = 1;
134 int ret = 0;
135
136 while (pinned < pa->pa_nr) {
137 dma_addr_t *first = &pa->pa_iova[pinned];
138 dma_addr_t *last = &first[npage];
139
140 if (pinned + npage < pa->pa_nr &&
141 *first + npage * PAGE_SIZE == *last &&
142 !unaligned) {
143 npage++;
144 continue;
145 }
146
147 ret = vfio_pin_pages(vdev, *first, npage,
148 IOMMU_READ | IOMMU_WRITE,
149 &pa->pa_page[pinned]);
150 if (ret < 0) {
151 goto err_out;
152 } else if (ret > 0 && ret != npage) {
153 pinned += ret;
154 ret = -EINVAL;
155 goto err_out;
156 }
157 pinned += npage;
158 npage = 1;
159 }
160
161 return ret;
162
163 err_out:
164 page_array_unpin(pa, vdev, pinned, unaligned);
165 return ret;
166 }
167
168 /* Unpin the pages before releasing the memory. */
page_array_unpin_free(struct page_array * pa,struct vfio_device * vdev,bool unaligned)169 static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev, bool unaligned)
170 {
171 page_array_unpin(pa, vdev, pa->pa_nr, unaligned);
172 kfree(pa->pa_page);
173 kfree(pa->pa_iova);
174 }
175
page_array_iova_pinned(struct page_array * pa,u64 iova,u64 length)176 static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length)
177 {
178 u64 iova_pfn_start = iova >> PAGE_SHIFT;
179 u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT;
180 u64 pfn;
181 int i;
182
183 for (i = 0; i < pa->pa_nr; i++) {
184 pfn = pa->pa_iova[i] >> PAGE_SHIFT;
185 if (pfn >= iova_pfn_start && pfn <= iova_pfn_end)
186 return true;
187 }
188
189 return false;
190 }
191 /* Create the list of IDAL words for a page_array. */
page_array_idal_create_words(struct page_array * pa,dma64_t * idaws)192 static inline void page_array_idal_create_words(struct page_array *pa,
193 dma64_t *idaws)
194 {
195 int i;
196
197 /*
198 * Idal words (execept the first one) rely on the memory being 4k
199 * aligned. If a user virtual address is 4K aligned, then it's
200 * corresponding kernel physical address will also be 4K aligned. Thus
201 * there will be no problem here to simply use the phys to create an
202 * idaw.
203 */
204
205 for (i = 0; i < pa->pa_nr; i++) {
206 idaws[i] = virt_to_dma64(page_to_virt(pa->pa_page[i]));
207
208 /* Incorporate any offset from each starting address */
209 idaws[i] = dma64_add(idaws[i], pa->pa_iova[i] & ~PAGE_MASK);
210 }
211 }
212
convert_ccw0_to_ccw1(struct ccw1 * source,unsigned long len)213 static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
214 {
215 struct ccw0 ccw0;
216 struct ccw1 *pccw1 = source;
217 int i;
218
219 for (i = 0; i < len; i++) {
220 ccw0 = *(struct ccw0 *)pccw1;
221 if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
222 pccw1->cmd_code = CCW_CMD_TIC;
223 pccw1->flags = 0;
224 pccw1->count = 0;
225 } else {
226 pccw1->cmd_code = ccw0.cmd_code;
227 pccw1->flags = ccw0.flags;
228 pccw1->count = ccw0.count;
229 }
230 pccw1->cda = u32_to_dma32(ccw0.cda);
231 pccw1++;
232 }
233 }
234
235 #define idal_is_2k(_cp) (!(_cp)->orb.cmd.c64 || (_cp)->orb.cmd.i2k)
236 #define get_idaw_size(_cp) ((_cp)->orb.cmd.c64 ? sizeof(u64) : sizeof(u32))
237
238 /*
239 * Helpers to operate ccwchain.
240 */
241 #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
242 #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
243 #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
244
245 #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
246
247 #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
248
249 #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
250 #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
251
252 #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
253
254 /*
255 * ccw_does_data_transfer()
256 *
257 * Determine whether a CCW will move any data, such that the guest pages
258 * would need to be pinned before performing the I/O.
259 *
260 * Returns 1 if yes, 0 if no.
261 */
ccw_does_data_transfer(struct ccw1 * ccw)262 static inline int ccw_does_data_transfer(struct ccw1 *ccw)
263 {
264 /* If the count field is zero, then no data will be transferred */
265 if (ccw->count == 0)
266 return 0;
267
268 /* If the command is a NOP, then no data will be transferred */
269 if (ccw_is_noop(ccw))
270 return 0;
271
272 /* If the skip flag is off, then data will be transferred */
273 if (!ccw_is_skip(ccw))
274 return 1;
275
276 /*
277 * If the skip flag is on, it is only meaningful if the command
278 * code is a read, read backward, sense, or sense ID. In those
279 * cases, no data will be transferred.
280 */
281 if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
282 return 0;
283
284 if (ccw_is_sense(ccw))
285 return 0;
286
287 /* The skip flag is on, but it is ignored for this command code. */
288 return 1;
289 }
290
291 /*
292 * is_cpa_within_range()
293 *
294 * @cpa: channel program address being questioned
295 * @head: address of the beginning of a CCW chain
296 * @len: number of CCWs within the chain
297 *
298 * Determine whether the address of a CCW (whether a new chain,
299 * or the target of a TIC) falls within a range (including the end points).
300 *
301 * Returns 1 if yes, 0 if no.
302 */
is_cpa_within_range(dma32_t cpa,u32 head,int len)303 static inline int is_cpa_within_range(dma32_t cpa, u32 head, int len)
304 {
305 u32 tail = head + (len - 1) * sizeof(struct ccw1);
306 u32 gcpa = dma32_to_u32(cpa);
307
308 return head <= gcpa && gcpa <= tail;
309 }
310
is_tic_within_range(struct ccw1 * ccw,u32 head,int len)311 static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
312 {
313 if (!ccw_is_tic(ccw))
314 return 0;
315
316 return is_cpa_within_range(ccw->cda, head, len);
317 }
318
ccwchain_alloc(struct channel_program * cp,int len)319 static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
320 {
321 struct ccwchain *chain;
322
323 chain = kzalloc_obj(*chain);
324 if (!chain)
325 return NULL;
326
327 chain->ch_ccw = kzalloc_objs(*chain->ch_ccw, len, GFP_DMA | GFP_KERNEL);
328 if (!chain->ch_ccw)
329 goto out_err;
330
331 chain->ch_pa = kzalloc_objs(*chain->ch_pa, len);
332 if (!chain->ch_pa)
333 goto out_err;
334
335 list_add_tail(&chain->next, &cp->ccwchain_list);
336 cp->ccwchain_count++;
337
338 return chain;
339
340 out_err:
341 kfree(chain->ch_ccw);
342 kfree(chain);
343 return NULL;
344 }
345
ccwchain_free(struct ccwchain * chain)346 static void ccwchain_free(struct ccwchain *chain)
347 {
348 list_del(&chain->next);
349 kfree(chain->ch_pa);
350 kfree(chain->ch_ccw);
351 kfree(chain);
352 }
353
354 /* Free resource for a ccw that allocated memory for its cda. */
ccwchain_cda_free(struct ccwchain * chain,int idx)355 static void ccwchain_cda_free(struct ccwchain *chain, int idx)
356 {
357 struct ccw1 *ccw = &chain->ch_ccw[idx];
358
359 if (ccw_is_tic(ccw))
360 return;
361
362 kfree(dma32_to_virt(ccw->cda));
363 }
364
365 /**
366 * ccwchain_calc_length - calculate the length of the ccw chain.
367 * @iova: guest physical address of the target ccw chain
368 * @cp: channel_program on which to perform the operation
369 *
370 * This is the chain length not considering any TICs.
371 * You need to do a new round for each TIC target.
372 *
373 * The program is also validated for absence of not yet supported
374 * indirect data addressing scenarios.
375 *
376 * Returns: the length of the ccw chain or -errno.
377 */
ccwchain_calc_length(u64 iova,struct channel_program * cp)378 static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
379 {
380 struct ccw1 *ccw = cp->guest_cp;
381 int cnt;
382
383 for (cnt = 1; cnt <= CCWCHAIN_LEN_MAX; cnt++, ccw++) {
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 return cnt;
394 }
395
396 return -EINVAL;
397 }
398
tic_target_chain_exists(struct ccw1 * tic,struct channel_program * cp)399 static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
400 {
401 struct ccwchain *chain;
402 u32 ccw_head;
403
404 list_for_each_entry(chain, &cp->ccwchain_list, next) {
405 ccw_head = chain->ch_iova;
406 if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
407 return 1;
408 }
409
410 return 0;
411 }
412
413 static int ccwchain_loop_tic(struct ccwchain *chain,
414 struct channel_program *cp);
415
ccwchain_handle_ccw(dma32_t cda,struct channel_program * cp)416 static int ccwchain_handle_ccw(dma32_t cda, struct channel_program *cp)
417 {
418 struct vfio_device *vdev =
419 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
420 struct ccwchain *chain;
421 int len, ret;
422 u32 gcda;
423
424 gcda = dma32_to_u32(cda);
425 /* Copy 2K (the most we support today) of possible CCWs */
426 ret = vfio_dma_rw(vdev, gcda, cp->guest_cp, CCWCHAIN_LEN_MAX * sizeof(struct ccw1), false);
427 if (ret)
428 return ret;
429
430 /* Convert any Format-0 CCWs to Format-1 */
431 if (!cp->orb.cmd.fmt)
432 convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
433
434 /* Count the CCWs in the current chain */
435 len = ccwchain_calc_length(gcda, cp);
436 if (len < 0)
437 return len;
438
439 /* Limit number of chains in a single channel program */
440 if (cp->ccwchain_count >= CCWCHAIN_COUNT_MAX)
441 return -EINVAL;
442
443 /* Need alloc a new chain for this one. */
444 chain = ccwchain_alloc(cp, len);
445 if (!chain)
446 return -ENOMEM;
447
448 chain->ch_len = len;
449 chain->ch_iova = gcda;
450
451 /* Copy the actual CCWs into the new chain */
452 memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
453
454 /* Loop for tics on this new chain. */
455 ret = ccwchain_loop_tic(chain, cp);
456
457 return ret;
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 /* May transfer to an existing chain. */
473 if (tic_target_chain_exists(tic, cp))
474 continue;
475
476 /* Build a ccwchain for the next segment */
477 ret = ccwchain_handle_ccw(tic->cda, cp);
478 if (ret)
479 return ret;
480 }
481
482 return 0;
483 }
484
ccwchain_build_ccws(dma32_t cda,struct channel_program * cp)485 static int ccwchain_build_ccws(dma32_t cda, struct channel_program *cp)
486 {
487 struct ccwchain *chain, *temp;
488 int ret;
489
490 ret = ccwchain_handle_ccw(cda, cp);
491
492 if (ret) {
493 /* Cleanup if an error occurred */
494 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
495 ccwchain_free(chain);
496 }
497 }
498
499 return ret;
500 }
501
ccwchain_fetch_tic(struct ccw1 * ccw,struct channel_program * cp)502 static int ccwchain_fetch_tic(struct ccw1 *ccw,
503 struct channel_program *cp)
504 {
505 struct ccwchain *iter;
506 u32 offset, ccw_head;
507
508 list_for_each_entry(iter, &cp->ccwchain_list, next) {
509 ccw_head = iter->ch_iova;
510 if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
511 /* Calculate offset of TIC target */
512 offset = dma32_to_u32(ccw->cda) - ccw_head;
513 ccw->cda = virt_to_dma32((void *)iter->ch_ccw + offset);
514 return 0;
515 }
516 }
517
518 return -EFAULT;
519 }
520
get_guest_idal(struct ccw1 * ccw,struct channel_program * cp,int idaw_nr)521 static dma64_t *get_guest_idal(struct ccw1 *ccw, struct channel_program *cp, int idaw_nr)
522 {
523 struct vfio_device *vdev =
524 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
525 dma64_t *idaws;
526 dma32_t *idaws_f1;
527 u64 first_idaw;
528 int idal_len = idaw_nr * get_idaw_size(cp);
529 int idaw_size = idal_is_2k(cp) ? PAGE_SIZE / 2 : PAGE_SIZE;
530 int idaw_mask = ~(idaw_size - 1);
531 int i, ret;
532
533 idaws = kzalloc_objs(*idaws, idaw_nr, GFP_DMA | GFP_KERNEL);
534 if (!idaws)
535 return ERR_PTR(-ENOMEM);
536
537 if (ccw_is_idal(ccw)) {
538 /* Copy IDAL from guest */
539 ret = vfio_dma_rw(vdev, dma32_to_u32(ccw->cda), idaws, idal_len, false);
540 if (ret) {
541 kfree(idaws);
542 return ERR_PTR(ret);
543 }
544
545 idaws_f1 = (dma32_t *)idaws;
546 if (cp->orb.cmd.c64)
547 first_idaw = dma64_to_u64(idaws[0]);
548 else
549 first_idaw = dma32_to_u32(idaws_f1[0]);
550
551 /* Unexpected mismatch from earlier read */
552 if (first_idaw != cp->guest_iova) {
553 kfree(idaws);
554 return ERR_PTR(-EINVAL);
555 }
556 } else {
557 /* Fabricate an IDAL based off CCW data address */
558 if (cp->orb.cmd.c64) {
559 idaws[0] = u64_to_dma64(dma32_to_u32(ccw->cda));
560 for (i = 1; i < idaw_nr; i++) {
561 idaws[i] = dma64_add(idaws[i - 1], idaw_size);
562 idaws[i] = dma64_and(idaws[i], idaw_mask);
563 }
564 } else {
565 idaws_f1 = (dma32_t *)idaws;
566 idaws_f1[0] = ccw->cda;
567 for (i = 1; i < idaw_nr; i++) {
568 idaws_f1[i] = dma32_add(idaws_f1[i - 1], idaw_size);
569 idaws_f1[i] = dma32_and(idaws_f1[i], idaw_mask);
570 }
571 }
572 }
573
574 return idaws;
575 }
576
577 /*
578 * ccw_count_idaws() - Calculate the number of IDAWs needed to transfer
579 * a specified amount of data
580 *
581 * @ccw: The Channel Command Word being translated
582 * @cp: Channel Program being processed
583 *
584 * The ORB is examined, since it specifies what IDAWs could actually be
585 * used by any CCW in the channel program, regardless of whether or not
586 * the CCW actually does. An ORB that does not specify Format-2-IDAW
587 * Control could still contain a CCW with an IDAL, which would be
588 * Format-1 and thus only move 2K with each IDAW. Thus all CCWs within
589 * the channel program must follow the same size requirements.
590 */
ccw_count_idaws(struct ccw1 * ccw,struct channel_program * cp)591 static int ccw_count_idaws(struct ccw1 *ccw,
592 struct channel_program *cp)
593 {
594 struct vfio_device *vdev =
595 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
596 u64 iova;
597 int size = get_idaw_size(cp);
598 int ret;
599 int bytes = 1;
600
601 if (ccw->count)
602 bytes = ccw->count;
603
604 if (ccw_is_idal(ccw)) {
605 /* Read first IDAW to check its starting address. */
606 /* All subsequent IDAWs will be 2K- or 4K-aligned. */
607 ret = vfio_dma_rw(vdev, dma32_to_u32(ccw->cda), &iova, size, false);
608 if (ret)
609 return ret;
610
611 /*
612 * Format-1 IDAWs only occupy the first 32 bits,
613 * and bit 0 is always off.
614 */
615 if (!cp->orb.cmd.c64)
616 iova = iova >> 32;
617 } else {
618 iova = dma32_to_u32(ccw->cda);
619 }
620
621 /* Save the read address for later */
622 cp->guest_iova = iova;
623
624 /* Format-1 IDAWs operate on 2K each */
625 if (!cp->orb.cmd.c64)
626 return idal_2k_nr_words((void *)iova, bytes);
627
628 /* Using the 2K variant of Format-2 IDAWs? */
629 if (cp->orb.cmd.i2k)
630 return idal_2k_nr_words((void *)iova, bytes);
631
632 /* The 'usual' case is 4K Format-2 IDAWs */
633 return idal_nr_words((void *)iova, bytes);
634 }
635
ccwchain_fetch_ccw(struct ccw1 * ccw,struct page_array * pa,struct channel_program * cp)636 static int ccwchain_fetch_ccw(struct ccw1 *ccw,
637 struct page_array *pa,
638 struct channel_program *cp)
639 {
640 struct vfio_device *vdev =
641 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
642 dma64_t *idaws;
643 dma32_t *idaws_f1;
644 int ret;
645 int idaw_nr;
646 int i;
647
648 /* Calculate size of IDAL */
649 idaw_nr = ccw_count_idaws(ccw, cp);
650 if (idaw_nr < 0)
651 return idaw_nr;
652
653 /* Allocate an IDAL from host storage */
654 idaws = get_guest_idal(ccw, cp, idaw_nr);
655 if (IS_ERR(idaws)) {
656 ret = PTR_ERR(idaws);
657 goto out_init;
658 }
659
660 /*
661 * Allocate an array of pages to pin/translate.
662 * The number of pages is actually the count of the idaws
663 * required for the data transfer, since we only only support
664 * 4K IDAWs today.
665 */
666 ret = page_array_alloc(pa, idaw_nr);
667 if (ret < 0)
668 goto out_free_idaws;
669
670 /*
671 * Copy guest IDAWs into page_array, in case the memory they
672 * occupy is not contiguous.
673 */
674 idaws_f1 = (dma32_t *)idaws;
675 for (i = 0; i < idaw_nr; i++) {
676 if (cp->orb.cmd.c64)
677 pa->pa_iova[i] = dma64_to_u64(idaws[i]);
678 else
679 pa->pa_iova[i] = dma32_to_u32(idaws_f1[i]);
680 }
681
682 if (ccw_does_data_transfer(ccw)) {
683 ret = page_array_pin(pa, vdev, idal_is_2k(cp));
684 if (ret < 0)
685 goto out_unpin;
686 } else {
687 pa->pa_nr = 0;
688 }
689
690 ccw->cda = virt_to_dma32(idaws);
691 ccw->flags |= CCW_FLAG_IDA;
692
693 /* Populate the IDAL with pinned/translated addresses from page */
694 page_array_idal_create_words(pa, idaws);
695
696 return 0;
697
698 out_unpin:
699 page_array_unpin_free(pa, vdev, idal_is_2k(cp));
700 out_free_idaws:
701 kfree(idaws);
702 out_init:
703 ccw->cda = 0;
704 return ret;
705 }
706
707 /*
708 * Fetch one ccw.
709 * To reduce memory copy, we'll pin the cda page in memory,
710 * and to get rid of the cda 2G limitation of ccw1, we'll translate
711 * direct ccws to idal ccws.
712 */
ccwchain_fetch_one(struct ccw1 * ccw,struct page_array * pa,struct channel_program * cp)713 static int ccwchain_fetch_one(struct ccw1 *ccw,
714 struct page_array *pa,
715 struct channel_program *cp)
716
717 {
718 if (ccw_is_tic(ccw))
719 return ccwchain_fetch_tic(ccw, cp);
720
721 return ccwchain_fetch_ccw(ccw, pa, cp);
722 }
723
724 /**
725 * cp_init() - allocate ccwchains for a channel program.
726 * @cp: channel_program on which to perform the operation
727 * @orb: control block for the channel program from the guest
728 *
729 * This creates one or more ccwchain(s), and copies the raw data of
730 * the target channel program from @orb->cmd.iova to the new ccwchain(s).
731 *
732 * Limitations:
733 * 1. Supports idal(c64) ccw chaining.
734 * 2. Supports 4k idaw.
735 *
736 * Returns:
737 * %0 on success and a negative error value on failure.
738 */
cp_init(struct channel_program * cp,union orb * orb)739 int cp_init(struct channel_program *cp, union orb *orb)
740 {
741 struct vfio_device *vdev =
742 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
743 /* custom ratelimit used to avoid flood during guest IPL */
744 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
745 int ret;
746
747 /* this is an error in the caller */
748 if (cp->initialized)
749 return -EBUSY;
750
751 /*
752 * We only support prefetching the channel program. We assume all channel
753 * programs executed by supported guests likewise support prefetching.
754 * Executing a channel program that does not specify prefetching will
755 * typically not cause an error, but a warning is issued to help identify
756 * the problem if something does break.
757 */
758 if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
759 dev_warn(
760 vdev->dev,
761 "Prefetching channel program even though prefetch not specified in ORB");
762
763 cp->ccwchain_count = 0;
764 INIT_LIST_HEAD(&cp->ccwchain_list);
765 memcpy(&cp->orb, orb, sizeof(*orb));
766
767 /* Build a ccwchain for the first CCW segment */
768 ret = ccwchain_build_ccws(orb->cmd.cpa, cp);
769
770 if (!ret)
771 cp->initialized = true;
772
773 return ret;
774 }
775
776
777 /**
778 * cp_free() - free resources for channel program.
779 * @cp: channel_program on which to perform the operation
780 *
781 * This unpins the memory pages and frees the memory space occupied by
782 * @cp, which must have been returned by a previous call to cp_init().
783 * Otherwise, undefined behavior occurs.
784 */
cp_free(struct channel_program * cp)785 void cp_free(struct channel_program *cp)
786 {
787 struct vfio_device *vdev =
788 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
789 struct ccwchain *chain, *temp;
790 int i;
791
792 if (!cp->initialized)
793 return;
794
795 cp->initialized = false;
796 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
797 for (i = 0; i < chain->ch_len; i++) {
798 page_array_unpin_free(&chain->ch_pa[i], vdev, idal_is_2k(cp));
799 ccwchain_cda_free(chain, i);
800 }
801 ccwchain_free(chain);
802 }
803 }
804
805 /**
806 * cp_prefetch() - translate a guest physical address channel program to
807 * a real-device runnable channel program.
808 * @cp: channel_program on which to perform the operation
809 *
810 * This function translates the guest-physical-address channel program
811 * and stores the result to ccwchain list. @cp must have been
812 * initialized by a previous call with cp_init(). Otherwise, undefined
813 * behavior occurs.
814 * For each chain composing the channel program:
815 * - On entry ch_len holds the count of CCWs to be translated.
816 * - On exit ch_len is adjusted to the count of successfully translated CCWs.
817 * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
818 *
819 * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
820 * as helpers to do ccw chain translation inside the kernel. Basically
821 * they accept a channel program issued by a virtual machine, and
822 * translate the channel program to a real-device runnable channel
823 * program.
824 *
825 * These APIs will copy the ccws into kernel-space buffers, and update
826 * the guest physical addresses with their corresponding host physical
827 * addresses. Then channel I/O device drivers could issue the
828 * translated channel program to real devices to perform an I/O
829 * operation.
830 *
831 * These interfaces are designed to support translation only for
832 * channel programs, which are generated and formatted by a
833 * guest. Thus this will make it possible for things like VFIO to
834 * leverage the interfaces to passthrough a channel I/O mediated
835 * device in QEMU.
836 *
837 * We support direct ccw chaining by translating them to idal ccws.
838 *
839 * Returns:
840 * %0 on success and a negative error value on failure.
841 */
cp_prefetch(struct channel_program * cp)842 int cp_prefetch(struct channel_program *cp)
843 {
844 struct ccwchain *chain;
845 struct ccw1 *ccw;
846 struct page_array *pa;
847 int len, idx, ret;
848
849 /* this is an error in the caller */
850 if (!cp->initialized)
851 return -EINVAL;
852
853 list_for_each_entry(chain, &cp->ccwchain_list, next) {
854 len = chain->ch_len;
855 for (idx = 0; idx < len; idx++) {
856 ccw = &chain->ch_ccw[idx];
857 pa = &chain->ch_pa[idx];
858
859 ret = ccwchain_fetch_one(ccw, pa, cp);
860 if (ret)
861 goto out_err;
862 }
863 }
864
865 return 0;
866 out_err:
867 /* Only cleanup the chain elements that were actually translated. */
868 chain->ch_len = idx;
869 list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
870 chain->ch_len = 0;
871 }
872 return ret;
873 }
874
875 /**
876 * cp_get_orb() - get the orb of the channel program
877 * @cp: channel_program on which to perform the operation
878 * @sch: subchannel the operation will be performed against
879 *
880 * This function returns the address of the updated orb of the channel
881 * program. Channel I/O device drivers could use this orb to issue a
882 * ssch.
883 */
cp_get_orb(struct channel_program * cp,struct subchannel * sch)884 union orb *cp_get_orb(struct channel_program *cp, struct subchannel *sch)
885 {
886 union orb *orb;
887 struct ccwchain *chain;
888 struct ccw1 *cpa;
889
890 /* this is an error in the caller */
891 if (!cp->initialized)
892 return NULL;
893
894 orb = &cp->orb;
895
896 orb->cmd.intparm = (u32)virt_to_phys(sch);
897 orb->cmd.fmt = 1;
898
899 /*
900 * Everything built by vfio-ccw is a Format-2 IDAL.
901 * If the input was a Format-1 IDAL, indicate that
902 * 2K Format-2 IDAWs were created here.
903 */
904 if (!orb->cmd.c64)
905 orb->cmd.i2k = 1;
906 orb->cmd.c64 = 1;
907
908 if (orb->cmd.lpm == 0)
909 orb->cmd.lpm = sch->lpm;
910
911 chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
912 cpa = chain->ch_ccw;
913 orb->cmd.cpa = virt_to_dma32(cpa);
914
915 return orb;
916 }
917
918 /**
919 * cp_update_scsw() - update scsw for a channel program.
920 * @cp: channel_program on which to perform the operation
921 * @scsw: I/O results of the channel program and also the target to be
922 * updated
923 *
924 * @scsw contains the I/O results of the channel program that pointed
925 * to by @cp. However what @scsw->cpa stores is a host physical
926 * address, which is meaningless for the guest, which is waiting for
927 * the I/O results.
928 *
929 * This function updates @scsw->cpa to its coressponding guest physical
930 * address.
931 */
cp_update_scsw(struct channel_program * cp,union scsw * scsw)932 void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
933 {
934 struct ccwchain *chain;
935 dma32_t cpa = scsw->cmd.cpa;
936 u32 ccw_head;
937
938 if (!cp->initialized)
939 return;
940
941 /*
942 * LATER:
943 * For now, only update the cmd.cpa part. We may need to deal with
944 * other portions of the schib as well, even if we don't return them
945 * in the ioctl directly. Path status changes etc.
946 */
947 list_for_each_entry(chain, &cp->ccwchain_list, next) {
948 ccw_head = dma32_to_u32(virt_to_dma32(chain->ch_ccw));
949 /*
950 * On successful execution, cpa points just beyond the end
951 * of the chain.
952 */
953 if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
954 /*
955 * (cpa - ccw_head) is the offset value of the host
956 * physical ccw to its chain head.
957 * Adding this value to the guest physical ccw chain
958 * head gets us the guest cpa:
959 * cpa = chain->ch_iova + (cpa - ccw_head)
960 */
961 cpa = dma32_add(cpa, chain->ch_iova - ccw_head);
962 break;
963 }
964 }
965
966 scsw->cmd.cpa = cpa;
967 }
968
969 /**
970 * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
971 * @cp: channel_program on which to perform the operation
972 * @iova: the iova to check
973 * @length: the length to check from @iova
974 *
975 * If the @iova is currently pinned for the ccw chain, return true;
976 * else return false.
977 */
cp_iova_pinned(struct channel_program * cp,u64 iova,u64 length)978 bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length)
979 {
980 struct vfio_ccw_private *private =
981 container_of(cp, struct vfio_ccw_private, cp);
982 struct ccwchain *chain;
983 int i;
984
985 if (!cp->initialized)
986 return false;
987
988 mutex_lock(&private->io_mutex);
989 list_for_each_entry(chain, &cp->ccwchain_list, next) {
990 for (i = 0; i < chain->ch_len; i++)
991 if (page_array_iova_pinned(&chain->ch_pa[i], iova, length)) {
992 mutex_unlock(&private->io_mutex);
993 return true;
994 }
995 }
996 mutex_unlock(&private->io_mutex);
997
998 return false;
999 }
1000