0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/ratelimit.h>
0012 #include <linux/mm.h>
0013 #include <linux/slab.h>
0014 #include <linux/highmem.h>
0015 #include <linux/iommu.h>
0016 #include <linux/vfio.h>
0017 #include <asm/idals.h>
0018
0019 #include "vfio_ccw_cp.h"
0020 #include "vfio_ccw_private.h"
0021
0022 struct page_array {
0023
0024 dma_addr_t *pa_iova;
0025
0026 struct page **pa_page;
0027
0028 int pa_nr;
0029 };
0030
0031 struct ccwchain {
0032 struct list_head next;
0033 struct ccw1 *ch_ccw;
0034
0035 u64 ch_iova;
0036
0037 int ch_len;
0038
0039 struct page_array *ch_pa;
0040 };
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 static int page_array_alloc(struct page_array *pa, u64 iova, unsigned int len)
0060 {
0061 int i;
0062
0063 if (pa->pa_nr || pa->pa_iova)
0064 return -EINVAL;
0065
0066 pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
0067 if (!pa->pa_nr)
0068 return -EINVAL;
0069
0070 pa->pa_iova = kcalloc(pa->pa_nr,
0071 sizeof(*pa->pa_iova) + sizeof(*pa->pa_page),
0072 GFP_KERNEL);
0073 if (unlikely(!pa->pa_iova)) {
0074 pa->pa_nr = 0;
0075 return -ENOMEM;
0076 }
0077 pa->pa_page = (struct page **)&pa->pa_iova[pa->pa_nr];
0078
0079 pa->pa_iova[0] = iova;
0080 pa->pa_page[0] = NULL;
0081 for (i = 1; i < pa->pa_nr; i++) {
0082 pa->pa_iova[i] = pa->pa_iova[i - 1] + PAGE_SIZE;
0083 pa->pa_page[i] = NULL;
0084 }
0085
0086 return 0;
0087 }
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 static void page_array_unpin(struct page_array *pa,
0099 struct vfio_device *vdev, int pa_nr)
0100 {
0101 int unpinned = 0, npage = 1;
0102
0103 while (unpinned < pa_nr) {
0104 dma_addr_t *first = &pa->pa_iova[unpinned];
0105 dma_addr_t *last = &first[npage];
0106
0107 if (unpinned + npage < pa_nr &&
0108 *first + npage * PAGE_SIZE == *last) {
0109 npage++;
0110 continue;
0111 }
0112
0113 vfio_unpin_pages(vdev, *first, npage);
0114 unpinned += npage;
0115 npage = 1;
0116 }
0117
0118 pa->pa_nr = 0;
0119 }
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 static int page_array_pin(struct page_array *pa, struct vfio_device *vdev)
0131 {
0132 int pinned = 0, npage = 1;
0133 int ret = 0;
0134
0135 while (pinned < pa->pa_nr) {
0136 dma_addr_t *first = &pa->pa_iova[pinned];
0137 dma_addr_t *last = &first[npage];
0138
0139 if (pinned + npage < pa->pa_nr &&
0140 *first + npage * PAGE_SIZE == *last) {
0141 npage++;
0142 continue;
0143 }
0144
0145 ret = vfio_pin_pages(vdev, *first, npage,
0146 IOMMU_READ | IOMMU_WRITE,
0147 &pa->pa_page[pinned]);
0148 if (ret < 0) {
0149 goto err_out;
0150 } else if (ret > 0 && ret != npage) {
0151 pinned += ret;
0152 ret = -EINVAL;
0153 goto err_out;
0154 }
0155 pinned += npage;
0156 npage = 1;
0157 }
0158
0159 return ret;
0160
0161 err_out:
0162 page_array_unpin(pa, vdev, pinned);
0163 return ret;
0164 }
0165
0166
0167 static void page_array_unpin_free(struct page_array *pa, struct vfio_device *vdev)
0168 {
0169 page_array_unpin(pa, vdev, pa->pa_nr);
0170 kfree(pa->pa_iova);
0171 }
0172
0173 static bool page_array_iova_pinned(struct page_array *pa, u64 iova, u64 length)
0174 {
0175 u64 iova_pfn_start = iova >> PAGE_SHIFT;
0176 u64 iova_pfn_end = (iova + length - 1) >> PAGE_SHIFT;
0177 u64 pfn;
0178 int i;
0179
0180 for (i = 0; i < pa->pa_nr; i++) {
0181 pfn = pa->pa_iova[i] >> PAGE_SHIFT;
0182 if (pfn >= iova_pfn_start && pfn <= iova_pfn_end)
0183 return true;
0184 }
0185
0186 return false;
0187 }
0188
0189 static inline void page_array_idal_create_words(struct page_array *pa,
0190 unsigned long *idaws)
0191 {
0192 int i;
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 for (i = 0; i < pa->pa_nr; i++)
0203 idaws[i] = page_to_phys(pa->pa_page[i]);
0204
0205
0206 idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1);
0207 }
0208
0209 static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len)
0210 {
0211 struct ccw0 ccw0;
0212 struct ccw1 *pccw1 = source;
0213 int i;
0214
0215 for (i = 0; i < len; i++) {
0216 ccw0 = *(struct ccw0 *)pccw1;
0217 if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
0218 pccw1->cmd_code = CCW_CMD_TIC;
0219 pccw1->flags = 0;
0220 pccw1->count = 0;
0221 } else {
0222 pccw1->cmd_code = ccw0.cmd_code;
0223 pccw1->flags = ccw0.flags;
0224 pccw1->count = ccw0.count;
0225 }
0226 pccw1->cda = ccw0.cda;
0227 pccw1++;
0228 }
0229 }
0230
0231
0232
0233
0234
0235 static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova,
0236 unsigned long n)
0237 {
0238 struct page_array pa = {0};
0239 int i, ret;
0240 unsigned long l, m;
0241
0242 ret = page_array_alloc(&pa, iova, n);
0243 if (ret < 0)
0244 return ret;
0245
0246 ret = page_array_pin(&pa, vdev);
0247 if (ret < 0) {
0248 page_array_unpin_free(&pa, vdev);
0249 return ret;
0250 }
0251
0252 l = n;
0253 for (i = 0; i < pa.pa_nr; i++) {
0254 void *from = kmap_local_page(pa.pa_page[i]);
0255
0256 m = PAGE_SIZE;
0257 if (i == 0) {
0258 from += iova & (PAGE_SIZE - 1);
0259 m -= iova & (PAGE_SIZE - 1);
0260 }
0261
0262 m = min(l, m);
0263 memcpy(to + (n - l), from, m);
0264 kunmap_local(from);
0265
0266 l -= m;
0267 if (l == 0)
0268 break;
0269 }
0270
0271 page_array_unpin_free(&pa, vdev);
0272
0273 return l;
0274 }
0275
0276
0277
0278
0279 #define ccw_is_read(_ccw) (((_ccw)->cmd_code & 0x03) == 0x02)
0280 #define ccw_is_read_backward(_ccw) (((_ccw)->cmd_code & 0x0F) == 0x0C)
0281 #define ccw_is_sense(_ccw) (((_ccw)->cmd_code & 0x0F) == CCW_CMD_BASIC_SENSE)
0282
0283 #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
0284
0285 #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
0286
0287 #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
0288 #define ccw_is_skip(_ccw) ((_ccw)->flags & CCW_FLAG_SKIP)
0289
0290 #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300 static inline int ccw_does_data_transfer(struct ccw1 *ccw)
0301 {
0302
0303 if (ccw->count == 0)
0304 return 0;
0305
0306
0307 if (ccw_is_noop(ccw))
0308 return 0;
0309
0310
0311 if (!ccw_is_skip(ccw))
0312 return 1;
0313
0314
0315
0316
0317
0318
0319 if (ccw_is_read(ccw) || ccw_is_read_backward(ccw))
0320 return 0;
0321
0322 if (ccw_is_sense(ccw))
0323 return 0;
0324
0325
0326 return 1;
0327 }
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341 static inline int is_cpa_within_range(u32 cpa, u32 head, int len)
0342 {
0343 u32 tail = head + (len - 1) * sizeof(struct ccw1);
0344
0345 return (head <= cpa && cpa <= tail);
0346 }
0347
0348 static inline int is_tic_within_range(struct ccw1 *ccw, u32 head, int len)
0349 {
0350 if (!ccw_is_tic(ccw))
0351 return 0;
0352
0353 return is_cpa_within_range(ccw->cda, head, len);
0354 }
0355
0356 static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
0357 {
0358 struct ccwchain *chain;
0359 void *data;
0360 size_t size;
0361
0362
0363 size = ((sizeof(*chain) + 7L) & -8L) +
0364 sizeof(*chain->ch_ccw) * len +
0365 sizeof(*chain->ch_pa) * len;
0366 chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
0367 if (!chain)
0368 return NULL;
0369
0370 data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
0371 chain->ch_ccw = (struct ccw1 *)data;
0372
0373 data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
0374 chain->ch_pa = (struct page_array *)data;
0375
0376 chain->ch_len = len;
0377
0378 list_add_tail(&chain->next, &cp->ccwchain_list);
0379
0380 return chain;
0381 }
0382
0383 static void ccwchain_free(struct ccwchain *chain)
0384 {
0385 list_del(&chain->next);
0386 kfree(chain);
0387 }
0388
0389
0390 static void ccwchain_cda_free(struct ccwchain *chain, int idx)
0391 {
0392 struct ccw1 *ccw = chain->ch_ccw + idx;
0393
0394 if (ccw_is_tic(ccw))
0395 return;
0396
0397 kfree((void *)(u64)ccw->cda);
0398 }
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413 static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
0414 {
0415 struct ccw1 *ccw = cp->guest_cp;
0416 int cnt = 0;
0417
0418 do {
0419 cnt++;
0420
0421
0422
0423
0424
0425
0426 if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw))
0427 return -EOPNOTSUPP;
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437 if (!ccw_is_chain(ccw) && !is_tic_within_range(ccw, iova, cnt))
0438 break;
0439
0440 ccw++;
0441 } while (cnt < CCWCHAIN_LEN_MAX + 1);
0442
0443 if (cnt == CCWCHAIN_LEN_MAX + 1)
0444 cnt = -EINVAL;
0445
0446 return cnt;
0447 }
0448
0449 static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
0450 {
0451 struct ccwchain *chain;
0452 u32 ccw_head;
0453
0454 list_for_each_entry(chain, &cp->ccwchain_list, next) {
0455 ccw_head = chain->ch_iova;
0456 if (is_cpa_within_range(tic->cda, ccw_head, chain->ch_len))
0457 return 1;
0458 }
0459
0460 return 0;
0461 }
0462
0463 static int ccwchain_loop_tic(struct ccwchain *chain,
0464 struct channel_program *cp);
0465
0466 static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
0467 {
0468 struct vfio_device *vdev =
0469 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
0470 struct ccwchain *chain;
0471 int len, ret;
0472
0473
0474 len = copy_from_iova(vdev, cp->guest_cp, cda,
0475 CCWCHAIN_LEN_MAX * sizeof(struct ccw1));
0476 if (len)
0477 return len;
0478
0479
0480 if (!cp->orb.cmd.fmt)
0481 convert_ccw0_to_ccw1(cp->guest_cp, CCWCHAIN_LEN_MAX);
0482
0483
0484 len = ccwchain_calc_length(cda, cp);
0485 if (len < 0)
0486 return len;
0487
0488
0489 chain = ccwchain_alloc(cp, len);
0490 if (!chain)
0491 return -ENOMEM;
0492 chain->ch_iova = cda;
0493
0494
0495 memcpy(chain->ch_ccw, cp->guest_cp, len * sizeof(struct ccw1));
0496
0497
0498 ret = ccwchain_loop_tic(chain, cp);
0499
0500 if (ret)
0501 ccwchain_free(chain);
0502
0503 return ret;
0504 }
0505
0506
0507 static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
0508 {
0509 struct ccw1 *tic;
0510 int i, ret;
0511
0512 for (i = 0; i < chain->ch_len; i++) {
0513 tic = chain->ch_ccw + i;
0514
0515 if (!ccw_is_tic(tic))
0516 continue;
0517
0518
0519 if (tic_target_chain_exists(tic, cp))
0520 continue;
0521
0522
0523 ret = ccwchain_handle_ccw(tic->cda, cp);
0524 if (ret)
0525 return ret;
0526 }
0527
0528 return 0;
0529 }
0530
0531 static int ccwchain_fetch_tic(struct ccwchain *chain,
0532 int idx,
0533 struct channel_program *cp)
0534 {
0535 struct ccw1 *ccw = chain->ch_ccw + idx;
0536 struct ccwchain *iter;
0537 u32 ccw_head;
0538
0539 list_for_each_entry(iter, &cp->ccwchain_list, next) {
0540 ccw_head = iter->ch_iova;
0541 if (is_cpa_within_range(ccw->cda, ccw_head, iter->ch_len)) {
0542 ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
0543 (ccw->cda - ccw_head));
0544 return 0;
0545 }
0546 }
0547
0548 return -EFAULT;
0549 }
0550
0551 static int ccwchain_fetch_direct(struct ccwchain *chain,
0552 int idx,
0553 struct channel_program *cp)
0554 {
0555 struct vfio_device *vdev =
0556 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
0557 struct ccw1 *ccw;
0558 struct page_array *pa;
0559 u64 iova;
0560 unsigned long *idaws;
0561 int ret;
0562 int bytes = 1;
0563 int idaw_nr, idal_len;
0564 int i;
0565
0566 ccw = chain->ch_ccw + idx;
0567
0568 if (ccw->count)
0569 bytes = ccw->count;
0570
0571
0572 if (ccw_is_idal(ccw)) {
0573
0574
0575 ret = copy_from_iova(vdev, &iova, ccw->cda, sizeof(iova));
0576 if (ret)
0577 return ret;
0578 } else {
0579 iova = ccw->cda;
0580 }
0581 idaw_nr = idal_nr_words((void *)iova, bytes);
0582 idal_len = idaw_nr * sizeof(*idaws);
0583
0584
0585 idaws = kcalloc(idaw_nr, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
0586 if (!idaws) {
0587 ret = -ENOMEM;
0588 goto out_init;
0589 }
0590
0591
0592
0593
0594
0595
0596
0597 pa = chain->ch_pa + idx;
0598 ret = page_array_alloc(pa, iova, bytes);
0599 if (ret < 0)
0600 goto out_free_idaws;
0601
0602 if (ccw_is_idal(ccw)) {
0603
0604 ret = copy_from_iova(vdev, idaws, ccw->cda, idal_len);
0605 if (ret)
0606 goto out_unpin;
0607
0608
0609
0610
0611
0612 for (i = 0; i < idaw_nr; i++)
0613 pa->pa_iova[i] = idaws[i];
0614 } else {
0615
0616
0617
0618
0619
0620 }
0621
0622 if (ccw_does_data_transfer(ccw)) {
0623 ret = page_array_pin(pa, vdev);
0624 if (ret < 0)
0625 goto out_unpin;
0626 } else {
0627 pa->pa_nr = 0;
0628 }
0629
0630 ccw->cda = (__u32) virt_to_phys(idaws);
0631 ccw->flags |= CCW_FLAG_IDA;
0632
0633
0634 page_array_idal_create_words(pa, idaws);
0635
0636 return 0;
0637
0638 out_unpin:
0639 page_array_unpin_free(pa, vdev);
0640 out_free_idaws:
0641 kfree(idaws);
0642 out_init:
0643 ccw->cda = 0;
0644 return ret;
0645 }
0646
0647
0648
0649
0650
0651
0652
0653 static int ccwchain_fetch_one(struct ccwchain *chain,
0654 int idx,
0655 struct channel_program *cp)
0656 {
0657 struct ccw1 *ccw = chain->ch_ccw + idx;
0658
0659 if (ccw_is_tic(ccw))
0660 return ccwchain_fetch_tic(chain, idx, cp);
0661
0662 return ccwchain_fetch_direct(chain, idx, cp);
0663 }
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681 int cp_init(struct channel_program *cp, union orb *orb)
0682 {
0683 struct vfio_device *vdev =
0684 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
0685
0686 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 1);
0687 int ret;
0688
0689
0690 if (cp->initialized)
0691 return -EBUSY;
0692
0693
0694
0695
0696
0697
0698
0699
0700 if (!orb->cmd.pfch && __ratelimit(&ratelimit_state))
0701 dev_warn(
0702 vdev->dev,
0703 "Prefetching channel program even though prefetch not specified in ORB");
0704
0705 INIT_LIST_HEAD(&cp->ccwchain_list);
0706 memcpy(&cp->orb, orb, sizeof(*orb));
0707
0708
0709 ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
0710
0711 if (!ret) {
0712 cp->initialized = true;
0713
0714
0715
0716
0717 cp->orb.cmd.c64 = 1;
0718 }
0719
0720 return ret;
0721 }
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732 void cp_free(struct channel_program *cp)
0733 {
0734 struct vfio_device *vdev =
0735 &container_of(cp, struct vfio_ccw_private, cp)->vdev;
0736 struct ccwchain *chain, *temp;
0737 int i;
0738
0739 if (!cp->initialized)
0740 return;
0741
0742 cp->initialized = false;
0743 list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
0744 for (i = 0; i < chain->ch_len; i++) {
0745 page_array_unpin_free(chain->ch_pa + i, vdev);
0746 ccwchain_cda_free(chain, i);
0747 }
0748 ccwchain_free(chain);
0749 }
0750 }
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789 int cp_prefetch(struct channel_program *cp)
0790 {
0791 struct ccwchain *chain;
0792 int len, idx, ret;
0793
0794
0795 if (!cp->initialized)
0796 return -EINVAL;
0797
0798 list_for_each_entry(chain, &cp->ccwchain_list, next) {
0799 len = chain->ch_len;
0800 for (idx = 0; idx < len; idx++) {
0801 ret = ccwchain_fetch_one(chain, idx, cp);
0802 if (ret)
0803 goto out_err;
0804 }
0805 }
0806
0807 return 0;
0808 out_err:
0809
0810 chain->ch_len = idx;
0811 list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
0812 chain->ch_len = 0;
0813 }
0814 return ret;
0815 }
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827 union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
0828 {
0829 union orb *orb;
0830 struct ccwchain *chain;
0831 struct ccw1 *cpa;
0832
0833
0834 if (!cp->initialized)
0835 return NULL;
0836
0837 orb = &cp->orb;
0838
0839 orb->cmd.intparm = intparm;
0840 orb->cmd.fmt = 1;
0841 orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
0842
0843 if (orb->cmd.lpm == 0)
0844 orb->cmd.lpm = lpm;
0845
0846 chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
0847 cpa = chain->ch_ccw;
0848 orb->cmd.cpa = (__u32) __pa(cpa);
0849
0850 return orb;
0851 }
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867 void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
0868 {
0869 struct ccwchain *chain;
0870 u32 cpa = scsw->cmd.cpa;
0871 u32 ccw_head;
0872
0873 if (!cp->initialized)
0874 return;
0875
0876
0877
0878
0879
0880
0881
0882 list_for_each_entry(chain, &cp->ccwchain_list, next) {
0883 ccw_head = (u32)(u64)chain->ch_ccw;
0884
0885
0886
0887
0888 if (is_cpa_within_range(cpa, ccw_head, chain->ch_len + 1)) {
0889
0890
0891
0892
0893
0894
0895 cpa = chain->ch_iova + (cpa - ccw_head);
0896 break;
0897 }
0898 }
0899
0900 scsw->cmd.cpa = cpa;
0901 }
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912 bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length)
0913 {
0914 struct ccwchain *chain;
0915 int i;
0916
0917 if (!cp->initialized)
0918 return false;
0919
0920 list_for_each_entry(chain, &cp->ccwchain_list, next) {
0921 for (i = 0; i < chain->ch_len; i++)
0922 if (page_array_iova_pinned(chain->ch_pa + i, iova, length))
0923 return true;
0924 }
0925
0926 return false;
0927 }