Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ccw based virtio transport
0004  *
0005  * Copyright IBM Corp. 2012, 2014
0006  *
0007  *    Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
0008  */
0009 
0010 #include <linux/kernel_stat.h>
0011 #include <linux/init.h>
0012 #include <linux/memblock.h>
0013 #include <linux/err.h>
0014 #include <linux/virtio.h>
0015 #include <linux/virtio_config.h>
0016 #include <linux/slab.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/virtio_ring.h>
0019 #include <linux/pfn.h>
0020 #include <linux/async.h>
0021 #include <linux/wait.h>
0022 #include <linux/list.h>
0023 #include <linux/bitops.h>
0024 #include <linux/moduleparam.h>
0025 #include <linux/io.h>
0026 #include <linux/kvm_para.h>
0027 #include <linux/notifier.h>
0028 #include <asm/diag.h>
0029 #include <asm/setup.h>
0030 #include <asm/irq.h>
0031 #include <asm/cio.h>
0032 #include <asm/ccwdev.h>
0033 #include <asm/virtio-ccw.h>
0034 #include <asm/isc.h>
0035 #include <asm/airq.h>
0036 #include <asm/tpi.h>
0037 
0038 /*
0039  * virtio related functions
0040  */
0041 
0042 struct vq_config_block {
0043     __u16 index;
0044     __u16 num;
0045 } __packed;
0046 
0047 #define VIRTIO_CCW_CONFIG_SIZE 0x100
0048 /* same as PCI config space size, should be enough for all drivers */
0049 
0050 struct vcdev_dma_area {
0051     unsigned long indicators;
0052     unsigned long indicators2;
0053     struct vq_config_block config_block;
0054     __u8 status;
0055 };
0056 
0057 struct virtio_ccw_device {
0058     struct virtio_device vdev;
0059     __u8 config[VIRTIO_CCW_CONFIG_SIZE];
0060     struct ccw_device *cdev;
0061     __u32 curr_io;
0062     int err;
0063     unsigned int revision; /* Transport revision */
0064     wait_queue_head_t wait_q;
0065     spinlock_t lock;
0066     rwlock_t irq_lock;
0067     struct mutex io_lock; /* Serializes I/O requests */
0068     struct list_head virtqueues;
0069     bool is_thinint;
0070     bool going_away;
0071     bool device_lost;
0072     unsigned int config_ready;
0073     void *airq_info;
0074     struct vcdev_dma_area *dma_area;
0075 };
0076 
0077 static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
0078 {
0079     return &vcdev->dma_area->indicators;
0080 }
0081 
0082 static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
0083 {
0084     return &vcdev->dma_area->indicators2;
0085 }
0086 
0087 struct vq_info_block_legacy {
0088     __u64 queue;
0089     __u32 align;
0090     __u16 index;
0091     __u16 num;
0092 } __packed;
0093 
0094 struct vq_info_block {
0095     __u64 desc;
0096     __u32 res0;
0097     __u16 index;
0098     __u16 num;
0099     __u64 avail;
0100     __u64 used;
0101 } __packed;
0102 
0103 struct virtio_feature_desc {
0104     __le32 features;
0105     __u8 index;
0106 } __packed;
0107 
0108 struct virtio_thinint_area {
0109     unsigned long summary_indicator;
0110     unsigned long indicator;
0111     u64 bit_nr;
0112     u8 isc;
0113 } __packed;
0114 
0115 struct virtio_rev_info {
0116     __u16 revision;
0117     __u16 length;
0118     __u8 data[];
0119 };
0120 
0121 /* the highest virtio-ccw revision we support */
0122 #define VIRTIO_CCW_REV_MAX 2
0123 
0124 struct virtio_ccw_vq_info {
0125     struct virtqueue *vq;
0126     int num;
0127     union {
0128         struct vq_info_block s;
0129         struct vq_info_block_legacy l;
0130     } *info_block;
0131     int bit_nr;
0132     struct list_head node;
0133     long cookie;
0134 };
0135 
0136 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
0137 
0138 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
0139 #define MAX_AIRQ_AREAS 20
0140 
0141 static int virtio_ccw_use_airq = 1;
0142 
0143 struct airq_info {
0144     rwlock_t lock;
0145     u8 summary_indicator_idx;
0146     struct airq_struct airq;
0147     struct airq_iv *aiv;
0148 };
0149 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
0150 static DEFINE_MUTEX(airq_areas_lock);
0151 
0152 static u8 *summary_indicators;
0153 
0154 static inline u8 *get_summary_indicator(struct airq_info *info)
0155 {
0156     return summary_indicators + info->summary_indicator_idx;
0157 }
0158 
0159 #define CCW_CMD_SET_VQ 0x13
0160 #define CCW_CMD_VDEV_RESET 0x33
0161 #define CCW_CMD_SET_IND 0x43
0162 #define CCW_CMD_SET_CONF_IND 0x53
0163 #define CCW_CMD_READ_FEAT 0x12
0164 #define CCW_CMD_WRITE_FEAT 0x11
0165 #define CCW_CMD_READ_CONF 0x22
0166 #define CCW_CMD_WRITE_CONF 0x21
0167 #define CCW_CMD_WRITE_STATUS 0x31
0168 #define CCW_CMD_READ_VQ_CONF 0x32
0169 #define CCW_CMD_READ_STATUS 0x72
0170 #define CCW_CMD_SET_IND_ADAPTER 0x73
0171 #define CCW_CMD_SET_VIRTIO_REV 0x83
0172 
0173 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
0174 #define VIRTIO_CCW_DOING_RESET 0x00040000
0175 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
0176 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
0177 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
0178 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
0179 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
0180 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
0181 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
0182 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
0183 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
0184 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
0185 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
0186 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
0187 
0188 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
0189 {
0190     return container_of(vdev, struct virtio_ccw_device, vdev);
0191 }
0192 
0193 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
0194 {
0195     unsigned long i, flags;
0196 
0197     write_lock_irqsave(&info->lock, flags);
0198     for (i = 0; i < airq_iv_end(info->aiv); i++) {
0199         if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
0200             airq_iv_free_bit(info->aiv, i);
0201             airq_iv_set_ptr(info->aiv, i, 0);
0202             break;
0203         }
0204     }
0205     write_unlock_irqrestore(&info->lock, flags);
0206 }
0207 
0208 static void virtio_airq_handler(struct airq_struct *airq,
0209                 struct tpi_info *tpi_info)
0210 {
0211     struct airq_info *info = container_of(airq, struct airq_info, airq);
0212     unsigned long ai;
0213 
0214     inc_irq_stat(IRQIO_VAI);
0215     read_lock(&info->lock);
0216     /* Walk through indicators field, summary indicator active. */
0217     for (ai = 0;;) {
0218         ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
0219         if (ai == -1UL)
0220             break;
0221         vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
0222     }
0223     *(get_summary_indicator(info)) = 0;
0224     smp_wmb();
0225     /* Walk through indicators field, summary indicator not active. */
0226     for (ai = 0;;) {
0227         ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
0228         if (ai == -1UL)
0229             break;
0230         vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
0231     }
0232     read_unlock(&info->lock);
0233 }
0234 
0235 static struct airq_info *new_airq_info(int index)
0236 {
0237     struct airq_info *info;
0238     int rc;
0239 
0240     info = kzalloc(sizeof(*info), GFP_KERNEL);
0241     if (!info)
0242         return NULL;
0243     rwlock_init(&info->lock);
0244     info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
0245                    | AIRQ_IV_CACHELINE, NULL);
0246     if (!info->aiv) {
0247         kfree(info);
0248         return NULL;
0249     }
0250     info->airq.handler = virtio_airq_handler;
0251     info->summary_indicator_idx = index;
0252     info->airq.lsi_ptr = get_summary_indicator(info);
0253     info->airq.lsi_mask = 0xff;
0254     info->airq.isc = VIRTIO_AIRQ_ISC;
0255     rc = register_adapter_interrupt(&info->airq);
0256     if (rc) {
0257         airq_iv_release(info->aiv);
0258         kfree(info);
0259         return NULL;
0260     }
0261     return info;
0262 }
0263 
0264 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
0265                     u64 *first, void **airq_info)
0266 {
0267     int i, j;
0268     struct airq_info *info;
0269     unsigned long indicator_addr = 0;
0270     unsigned long bit, flags;
0271 
0272     for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
0273         mutex_lock(&airq_areas_lock);
0274         if (!airq_areas[i])
0275             airq_areas[i] = new_airq_info(i);
0276         info = airq_areas[i];
0277         mutex_unlock(&airq_areas_lock);
0278         if (!info)
0279             return 0;
0280         write_lock_irqsave(&info->lock, flags);
0281         bit = airq_iv_alloc(info->aiv, nvqs);
0282         if (bit == -1UL) {
0283             /* Not enough vacancies. */
0284             write_unlock_irqrestore(&info->lock, flags);
0285             continue;
0286         }
0287         *first = bit;
0288         *airq_info = info;
0289         indicator_addr = (unsigned long)info->aiv->vector;
0290         for (j = 0; j < nvqs; j++) {
0291             airq_iv_set_ptr(info->aiv, bit + j,
0292                     (unsigned long)vqs[j]);
0293         }
0294         write_unlock_irqrestore(&info->lock, flags);
0295     }
0296     return indicator_addr;
0297 }
0298 
0299 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
0300 {
0301     struct virtio_ccw_vq_info *info;
0302 
0303     if (!vcdev->airq_info)
0304         return;
0305     list_for_each_entry(info, &vcdev->virtqueues, node)
0306         drop_airq_indicator(info->vq, vcdev->airq_info);
0307 }
0308 
0309 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
0310 {
0311     unsigned long flags;
0312     __u32 ret;
0313 
0314     spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
0315     if (vcdev->err)
0316         ret = 0;
0317     else
0318         ret = vcdev->curr_io & flag;
0319     spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
0320     return ret;
0321 }
0322 
0323 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
0324              struct ccw1 *ccw, __u32 intparm)
0325 {
0326     int ret;
0327     unsigned long flags;
0328     int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
0329 
0330     mutex_lock(&vcdev->io_lock);
0331     do {
0332         spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
0333         ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
0334         if (!ret) {
0335             if (!vcdev->curr_io)
0336                 vcdev->err = 0;
0337             vcdev->curr_io |= flag;
0338         }
0339         spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
0340         cpu_relax();
0341     } while (ret == -EBUSY);
0342     wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
0343     ret = ret ? ret : vcdev->err;
0344     mutex_unlock(&vcdev->io_lock);
0345     return ret;
0346 }
0347 
0348 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
0349                       struct ccw1 *ccw)
0350 {
0351     int ret;
0352     unsigned long *indicatorp = NULL;
0353     struct virtio_thinint_area *thinint_area = NULL;
0354     struct airq_info *airq_info = vcdev->airq_info;
0355 
0356     if (vcdev->is_thinint) {
0357         thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
0358                              sizeof(*thinint_area));
0359         if (!thinint_area)
0360             return;
0361         thinint_area->summary_indicator =
0362             (unsigned long) get_summary_indicator(airq_info);
0363         thinint_area->isc = VIRTIO_AIRQ_ISC;
0364         ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
0365         ccw->count = sizeof(*thinint_area);
0366         ccw->cda = (__u32)(unsigned long) thinint_area;
0367     } else {
0368         /* payload is the address of the indicators */
0369         indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
0370                            sizeof(indicators(vcdev)));
0371         if (!indicatorp)
0372             return;
0373         *indicatorp = 0;
0374         ccw->cmd_code = CCW_CMD_SET_IND;
0375         ccw->count = sizeof(indicators(vcdev));
0376         ccw->cda = (__u32)(unsigned long) indicatorp;
0377     }
0378     /* Deregister indicators from host. */
0379     *indicators(vcdev) = 0;
0380     ccw->flags = 0;
0381     ret = ccw_io_helper(vcdev, ccw,
0382                 vcdev->is_thinint ?
0383                 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
0384                 VIRTIO_CCW_DOING_SET_IND);
0385     if (ret && (ret != -ENODEV))
0386         dev_info(&vcdev->cdev->dev,
0387              "Failed to deregister indicators (%d)\n", ret);
0388     else if (vcdev->is_thinint)
0389         virtio_ccw_drop_indicators(vcdev);
0390     ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(indicators(vcdev)));
0391     ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
0392 }
0393 
0394 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
0395 {
0396     struct virtio_ccw_vq_info *info = vq->priv;
0397     struct virtio_ccw_device *vcdev;
0398     struct subchannel_id schid;
0399 
0400     vcdev = to_vc_device(info->vq->vdev);
0401     ccw_device_get_schid(vcdev->cdev, &schid);
0402     BUILD_BUG_ON(sizeof(struct subchannel_id) != sizeof(unsigned int));
0403     info->cookie = kvm_hypercall3(KVM_S390_VIRTIO_CCW_NOTIFY,
0404                       *((unsigned int *)&schid),
0405                       vq->index, info->cookie);
0406     if (info->cookie < 0)
0407         return false;
0408     return true;
0409 }
0410 
0411 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
0412                    struct ccw1 *ccw, int index)
0413 {
0414     int ret;
0415 
0416     vcdev->dma_area->config_block.index = index;
0417     ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
0418     ccw->flags = 0;
0419     ccw->count = sizeof(struct vq_config_block);
0420     ccw->cda = (__u32)(unsigned long)(&vcdev->dma_area->config_block);
0421     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
0422     if (ret)
0423         return ret;
0424     return vcdev->dma_area->config_block.num ?: -ENOENT;
0425 }
0426 
0427 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
0428 {
0429     struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
0430     struct virtio_ccw_vq_info *info = vq->priv;
0431     unsigned long flags;
0432     int ret;
0433     unsigned int index = vq->index;
0434 
0435     /* Remove from our list. */
0436     spin_lock_irqsave(&vcdev->lock, flags);
0437     list_del(&info->node);
0438     spin_unlock_irqrestore(&vcdev->lock, flags);
0439 
0440     /* Release from host. */
0441     if (vcdev->revision == 0) {
0442         info->info_block->l.queue = 0;
0443         info->info_block->l.align = 0;
0444         info->info_block->l.index = index;
0445         info->info_block->l.num = 0;
0446         ccw->count = sizeof(info->info_block->l);
0447     } else {
0448         info->info_block->s.desc = 0;
0449         info->info_block->s.index = index;
0450         info->info_block->s.num = 0;
0451         info->info_block->s.avail = 0;
0452         info->info_block->s.used = 0;
0453         ccw->count = sizeof(info->info_block->s);
0454     }
0455     ccw->cmd_code = CCW_CMD_SET_VQ;
0456     ccw->flags = 0;
0457     ccw->cda = (__u32)(unsigned long)(info->info_block);
0458     ret = ccw_io_helper(vcdev, ccw,
0459                 VIRTIO_CCW_DOING_SET_VQ | index);
0460     /*
0461      * -ENODEV isn't considered an error: The device is gone anyway.
0462      * This may happen on device detach.
0463      */
0464     if (ret && (ret != -ENODEV))
0465         dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
0466              ret, index);
0467 
0468     vring_del_virtqueue(vq);
0469     ccw_device_dma_free(vcdev->cdev, info->info_block,
0470                 sizeof(*info->info_block));
0471     kfree(info);
0472 }
0473 
0474 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
0475 {
0476     struct virtqueue *vq, *n;
0477     struct ccw1 *ccw;
0478     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0479 
0480     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0481     if (!ccw)
0482         return;
0483 
0484     virtio_ccw_drop_indicator(vcdev, ccw);
0485 
0486     list_for_each_entry_safe(vq, n, &vdev->vqs, list)
0487         virtio_ccw_del_vq(vq, ccw);
0488 
0489     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0490 }
0491 
0492 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
0493                          int i, vq_callback_t *callback,
0494                          const char *name, bool ctx,
0495                          struct ccw1 *ccw)
0496 {
0497     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0498     int err;
0499     struct virtqueue *vq = NULL;
0500     struct virtio_ccw_vq_info *info;
0501     u64 queue;
0502     unsigned long flags;
0503     bool may_reduce;
0504 
0505     /* Allocate queue. */
0506     info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
0507     if (!info) {
0508         dev_warn(&vcdev->cdev->dev, "no info\n");
0509         err = -ENOMEM;
0510         goto out_err;
0511     }
0512     info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
0513                          sizeof(*info->info_block));
0514     if (!info->info_block) {
0515         dev_warn(&vcdev->cdev->dev, "no info block\n");
0516         err = -ENOMEM;
0517         goto out_err;
0518     }
0519     info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
0520     if (info->num < 0) {
0521         err = info->num;
0522         goto out_err;
0523     }
0524     may_reduce = vcdev->revision > 0;
0525     vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
0526                     vdev, true, may_reduce, ctx,
0527                     virtio_ccw_kvm_notify, callback, name);
0528 
0529     if (!vq) {
0530         /* For now, we fail if we can't get the requested size. */
0531         dev_warn(&vcdev->cdev->dev, "no vq\n");
0532         err = -ENOMEM;
0533         goto out_err;
0534     }
0535 
0536     vq->num_max = info->num;
0537 
0538     /* it may have been reduced */
0539     info->num = virtqueue_get_vring_size(vq);
0540 
0541     /* Register it with the host. */
0542     queue = virtqueue_get_desc_addr(vq);
0543     if (vcdev->revision == 0) {
0544         info->info_block->l.queue = queue;
0545         info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
0546         info->info_block->l.index = i;
0547         info->info_block->l.num = info->num;
0548         ccw->count = sizeof(info->info_block->l);
0549     } else {
0550         info->info_block->s.desc = queue;
0551         info->info_block->s.index = i;
0552         info->info_block->s.num = info->num;
0553         info->info_block->s.avail = (__u64)virtqueue_get_avail_addr(vq);
0554         info->info_block->s.used = (__u64)virtqueue_get_used_addr(vq);
0555         ccw->count = sizeof(info->info_block->s);
0556     }
0557     ccw->cmd_code = CCW_CMD_SET_VQ;
0558     ccw->flags = 0;
0559     ccw->cda = (__u32)(unsigned long)(info->info_block);
0560     err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
0561     if (err) {
0562         dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
0563         goto out_err;
0564     }
0565 
0566     info->vq = vq;
0567     vq->priv = info;
0568 
0569     /* Save it to our list. */
0570     spin_lock_irqsave(&vcdev->lock, flags);
0571     list_add(&info->node, &vcdev->virtqueues);
0572     spin_unlock_irqrestore(&vcdev->lock, flags);
0573 
0574     return vq;
0575 
0576 out_err:
0577     if (vq)
0578         vring_del_virtqueue(vq);
0579     if (info) {
0580         ccw_device_dma_free(vcdev->cdev, info->info_block,
0581                     sizeof(*info->info_block));
0582     }
0583     kfree(info);
0584     return ERR_PTR(err);
0585 }
0586 
0587 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
0588                        struct virtqueue *vqs[], int nvqs,
0589                        struct ccw1 *ccw)
0590 {
0591     int ret;
0592     struct virtio_thinint_area *thinint_area = NULL;
0593     struct airq_info *info;
0594 
0595     thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
0596                          sizeof(*thinint_area));
0597     if (!thinint_area) {
0598         ret = -ENOMEM;
0599         goto out;
0600     }
0601     /* Try to get an indicator. */
0602     thinint_area->indicator = get_airq_indicator(vqs, nvqs,
0603                              &thinint_area->bit_nr,
0604                              &vcdev->airq_info);
0605     if (!thinint_area->indicator) {
0606         ret = -ENOSPC;
0607         goto out;
0608     }
0609     info = vcdev->airq_info;
0610     thinint_area->summary_indicator =
0611         (unsigned long) get_summary_indicator(info);
0612     thinint_area->isc = VIRTIO_AIRQ_ISC;
0613     ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
0614     ccw->flags = CCW_FLAG_SLI;
0615     ccw->count = sizeof(*thinint_area);
0616     ccw->cda = (__u32)(unsigned long)thinint_area;
0617     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
0618     if (ret) {
0619         if (ret == -EOPNOTSUPP) {
0620             /*
0621              * The host does not support adapter interrupts
0622              * for virtio-ccw, stop trying.
0623              */
0624             virtio_ccw_use_airq = 0;
0625             pr_info("Adapter interrupts unsupported on host\n");
0626         } else
0627             dev_warn(&vcdev->cdev->dev,
0628                  "enabling adapter interrupts = %d\n", ret);
0629         virtio_ccw_drop_indicators(vcdev);
0630     }
0631 out:
0632     ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
0633     return ret;
0634 }
0635 
0636 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
0637                    struct virtqueue *vqs[],
0638                    vq_callback_t *callbacks[],
0639                    const char * const names[],
0640                    const bool *ctx,
0641                    struct irq_affinity *desc)
0642 {
0643     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0644     unsigned long *indicatorp = NULL;
0645     int ret, i, queue_idx = 0;
0646     struct ccw1 *ccw;
0647 
0648     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0649     if (!ccw)
0650         return -ENOMEM;
0651 
0652     for (i = 0; i < nvqs; ++i) {
0653         if (!names[i]) {
0654             vqs[i] = NULL;
0655             continue;
0656         }
0657 
0658         vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
0659                          names[i], ctx ? ctx[i] : false,
0660                          ccw);
0661         if (IS_ERR(vqs[i])) {
0662             ret = PTR_ERR(vqs[i]);
0663             vqs[i] = NULL;
0664             goto out;
0665         }
0666     }
0667     ret = -ENOMEM;
0668     /*
0669      * We need a data area under 2G to communicate. Our payload is
0670      * the address of the indicators.
0671     */
0672     indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
0673                        sizeof(indicators(vcdev)));
0674     if (!indicatorp)
0675         goto out;
0676     *indicatorp = (unsigned long) indicators(vcdev);
0677     if (vcdev->is_thinint) {
0678         ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
0679         if (ret)
0680             /* no error, just fall back to legacy interrupts */
0681             vcdev->is_thinint = false;
0682     }
0683     if (!vcdev->is_thinint) {
0684         /* Register queue indicators with host. */
0685         *indicators(vcdev) = 0;
0686         ccw->cmd_code = CCW_CMD_SET_IND;
0687         ccw->flags = 0;
0688         ccw->count = sizeof(indicators(vcdev));
0689         ccw->cda = (__u32)(unsigned long) indicatorp;
0690         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
0691         if (ret)
0692             goto out;
0693     }
0694     /* Register indicators2 with host for config changes */
0695     *indicatorp = (unsigned long) indicators2(vcdev);
0696     *indicators2(vcdev) = 0;
0697     ccw->cmd_code = CCW_CMD_SET_CONF_IND;
0698     ccw->flags = 0;
0699     ccw->count = sizeof(indicators2(vcdev));
0700     ccw->cda = (__u32)(unsigned long) indicatorp;
0701     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
0702     if (ret)
0703         goto out;
0704 
0705     if (indicatorp)
0706         ccw_device_dma_free(vcdev->cdev, indicatorp,
0707                     sizeof(indicators(vcdev)));
0708     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0709     return 0;
0710 out:
0711     if (indicatorp)
0712         ccw_device_dma_free(vcdev->cdev, indicatorp,
0713                     sizeof(indicators(vcdev)));
0714     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0715     virtio_ccw_del_vqs(vdev);
0716     return ret;
0717 }
0718 
0719 static void virtio_ccw_reset(struct virtio_device *vdev)
0720 {
0721     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0722     struct ccw1 *ccw;
0723 
0724     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0725     if (!ccw)
0726         return;
0727 
0728     /* Zero status bits. */
0729     vcdev->dma_area->status = 0;
0730 
0731     /* Send a reset ccw on device. */
0732     ccw->cmd_code = CCW_CMD_VDEV_RESET;
0733     ccw->flags = 0;
0734     ccw->count = 0;
0735     ccw->cda = 0;
0736     ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
0737     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0738 }
0739 
0740 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
0741 {
0742     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0743     struct virtio_feature_desc *features;
0744     int ret;
0745     u64 rc;
0746     struct ccw1 *ccw;
0747 
0748     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0749     if (!ccw)
0750         return 0;
0751 
0752     features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
0753     if (!features) {
0754         rc = 0;
0755         goto out_free;
0756     }
0757     /* Read the feature bits from the host. */
0758     features->index = 0;
0759     ccw->cmd_code = CCW_CMD_READ_FEAT;
0760     ccw->flags = 0;
0761     ccw->count = sizeof(*features);
0762     ccw->cda = (__u32)(unsigned long)features;
0763     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
0764     if (ret) {
0765         rc = 0;
0766         goto out_free;
0767     }
0768 
0769     rc = le32_to_cpu(features->features);
0770 
0771     if (vcdev->revision == 0)
0772         goto out_free;
0773 
0774     /* Read second half of the feature bits from the host. */
0775     features->index = 1;
0776     ccw->cmd_code = CCW_CMD_READ_FEAT;
0777     ccw->flags = 0;
0778     ccw->count = sizeof(*features);
0779     ccw->cda = (__u32)(unsigned long)features;
0780     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
0781     if (ret == 0)
0782         rc |= (u64)le32_to_cpu(features->features) << 32;
0783 
0784 out_free:
0785     ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
0786     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0787     return rc;
0788 }
0789 
0790 static void ccw_transport_features(struct virtio_device *vdev)
0791 {
0792     /*
0793      * Currently nothing to do here.
0794      */
0795 }
0796 
0797 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
0798 {
0799     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0800     struct virtio_feature_desc *features;
0801     struct ccw1 *ccw;
0802     int ret;
0803 
0804     if (vcdev->revision >= 1 &&
0805         !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
0806         dev_err(&vdev->dev, "virtio: device uses revision 1 "
0807             "but does not have VIRTIO_F_VERSION_1\n");
0808         return -EINVAL;
0809     }
0810 
0811     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0812     if (!ccw)
0813         return -ENOMEM;
0814 
0815     features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
0816     if (!features) {
0817         ret = -ENOMEM;
0818         goto out_free;
0819     }
0820     /* Give virtio_ring a chance to accept features. */
0821     vring_transport_features(vdev);
0822 
0823     /* Give virtio_ccw a chance to accept features. */
0824     ccw_transport_features(vdev);
0825 
0826     features->index = 0;
0827     features->features = cpu_to_le32((u32)vdev->features);
0828     /* Write the first half of the feature bits to the host. */
0829     ccw->cmd_code = CCW_CMD_WRITE_FEAT;
0830     ccw->flags = 0;
0831     ccw->count = sizeof(*features);
0832     ccw->cda = (__u32)(unsigned long)features;
0833     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
0834     if (ret)
0835         goto out_free;
0836 
0837     if (vcdev->revision == 0)
0838         goto out_free;
0839 
0840     features->index = 1;
0841     features->features = cpu_to_le32(vdev->features >> 32);
0842     /* Write the second half of the feature bits to the host. */
0843     ccw->cmd_code = CCW_CMD_WRITE_FEAT;
0844     ccw->flags = 0;
0845     ccw->count = sizeof(*features);
0846     ccw->cda = (__u32)(unsigned long)features;
0847     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
0848 
0849 out_free:
0850     ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
0851     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0852 
0853     return ret;
0854 }
0855 
0856 static void virtio_ccw_get_config(struct virtio_device *vdev,
0857                   unsigned int offset, void *buf, unsigned len)
0858 {
0859     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0860     int ret;
0861     struct ccw1 *ccw;
0862     void *config_area;
0863     unsigned long flags;
0864 
0865     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0866     if (!ccw)
0867         return;
0868 
0869     config_area = ccw_device_dma_zalloc(vcdev->cdev,
0870                         VIRTIO_CCW_CONFIG_SIZE);
0871     if (!config_area)
0872         goto out_free;
0873 
0874     /* Read the config area from the host. */
0875     ccw->cmd_code = CCW_CMD_READ_CONF;
0876     ccw->flags = 0;
0877     ccw->count = offset + len;
0878     ccw->cda = (__u32)(unsigned long)config_area;
0879     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
0880     if (ret)
0881         goto out_free;
0882 
0883     spin_lock_irqsave(&vcdev->lock, flags);
0884     memcpy(vcdev->config, config_area, offset + len);
0885     if (vcdev->config_ready < offset + len)
0886         vcdev->config_ready = offset + len;
0887     spin_unlock_irqrestore(&vcdev->lock, flags);
0888     if (buf)
0889         memcpy(buf, config_area + offset, len);
0890 
0891 out_free:
0892     ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
0893     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0894 }
0895 
0896 static void virtio_ccw_set_config(struct virtio_device *vdev,
0897                   unsigned int offset, const void *buf,
0898                   unsigned len)
0899 {
0900     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0901     struct ccw1 *ccw;
0902     void *config_area;
0903     unsigned long flags;
0904 
0905     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0906     if (!ccw)
0907         return;
0908 
0909     config_area = ccw_device_dma_zalloc(vcdev->cdev,
0910                         VIRTIO_CCW_CONFIG_SIZE);
0911     if (!config_area)
0912         goto out_free;
0913 
0914     /* Make sure we don't overwrite fields. */
0915     if (vcdev->config_ready < offset)
0916         virtio_ccw_get_config(vdev, 0, NULL, offset);
0917     spin_lock_irqsave(&vcdev->lock, flags);
0918     memcpy(&vcdev->config[offset], buf, len);
0919     /* Write the config area to the host. */
0920     memcpy(config_area, vcdev->config, sizeof(vcdev->config));
0921     spin_unlock_irqrestore(&vcdev->lock, flags);
0922     ccw->cmd_code = CCW_CMD_WRITE_CONF;
0923     ccw->flags = 0;
0924     ccw->count = offset + len;
0925     ccw->cda = (__u32)(unsigned long)config_area;
0926     ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
0927 
0928 out_free:
0929     ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
0930     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0931 }
0932 
0933 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
0934 {
0935     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0936     u8 old_status = vcdev->dma_area->status;
0937     struct ccw1 *ccw;
0938 
0939     if (vcdev->revision < 2)
0940         return vcdev->dma_area->status;
0941 
0942     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0943     if (!ccw)
0944         return old_status;
0945 
0946     ccw->cmd_code = CCW_CMD_READ_STATUS;
0947     ccw->flags = 0;
0948     ccw->count = sizeof(vcdev->dma_area->status);
0949     ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
0950     ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
0951 /*
0952  * If the channel program failed (should only happen if the device
0953  * was hotunplugged, and then we clean up via the machine check
0954  * handler anyway), vcdev->dma_area->status was not overwritten and we just
0955  * return the old status, which is fine.
0956 */
0957     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0958 
0959     return vcdev->dma_area->status;
0960 }
0961 
0962 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
0963 {
0964     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0965     u8 old_status = vcdev->dma_area->status;
0966     struct ccw1 *ccw;
0967     int ret;
0968 
0969     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
0970     if (!ccw)
0971         return;
0972 
0973     /* Write the status to the host. */
0974     vcdev->dma_area->status = status;
0975     ccw->cmd_code = CCW_CMD_WRITE_STATUS;
0976     ccw->flags = 0;
0977     ccw->count = sizeof(status);
0978     ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
0979     /* We use ssch for setting the status which is a serializing
0980      * instruction that guarantees the memory writes have
0981      * completed before ssch.
0982      */
0983     ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
0984     /* Write failed? We assume status is unchanged. */
0985     if (ret)
0986         vcdev->dma_area->status = old_status;
0987     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
0988 }
0989 
0990 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
0991 {
0992     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
0993 
0994     return dev_name(&vcdev->cdev->dev);
0995 }
0996 
0997 static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev)
0998 {
0999     struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1000     struct airq_info *info = vcdev->airq_info;
1001 
1002     if (info) {
1003         /*
1004          * This device uses adapter interrupts: synchronize with
1005          * vring_interrupt() called by virtio_airq_handler()
1006          * via the indicator area lock.
1007          */
1008         write_lock_irq(&info->lock);
1009         write_unlock_irq(&info->lock);
1010     } else {
1011         /* This device uses classic interrupts: synchronize
1012          * with vring_interrupt() called by
1013          * virtio_ccw_int_handler() via the per-device
1014          * irq_lock
1015          */
1016         write_lock_irq(&vcdev->irq_lock);
1017         write_unlock_irq(&vcdev->irq_lock);
1018     }
1019 }
1020 
1021 static const struct virtio_config_ops virtio_ccw_config_ops = {
1022     .get_features = virtio_ccw_get_features,
1023     .finalize_features = virtio_ccw_finalize_features,
1024     .get = virtio_ccw_get_config,
1025     .set = virtio_ccw_set_config,
1026     .get_status = virtio_ccw_get_status,
1027     .set_status = virtio_ccw_set_status,
1028     .reset = virtio_ccw_reset,
1029     .find_vqs = virtio_ccw_find_vqs,
1030     .del_vqs = virtio_ccw_del_vqs,
1031     .bus_name = virtio_ccw_bus_name,
1032     .synchronize_cbs = virtio_ccw_synchronize_cbs,
1033 };
1034 
1035 
1036 /*
1037  * ccw bus driver related functions
1038  */
1039 
1040 static void virtio_ccw_release_dev(struct device *_d)
1041 {
1042     struct virtio_device *dev = dev_to_virtio(_d);
1043     struct virtio_ccw_device *vcdev = to_vc_device(dev);
1044 
1045     ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1046                 sizeof(*vcdev->dma_area));
1047     kfree(vcdev);
1048 }
1049 
1050 static int irb_is_error(struct irb *irb)
1051 {
1052     if (scsw_cstat(&irb->scsw) != 0)
1053         return 1;
1054     if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1055         return 1;
1056     if (scsw_cc(&irb->scsw) != 0)
1057         return 1;
1058     return 0;
1059 }
1060 
1061 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1062                           int index)
1063 {
1064     struct virtio_ccw_vq_info *info;
1065     unsigned long flags;
1066     struct virtqueue *vq;
1067 
1068     vq = NULL;
1069     spin_lock_irqsave(&vcdev->lock, flags);
1070     list_for_each_entry(info, &vcdev->virtqueues, node) {
1071         if (info->vq->index == index) {
1072             vq = info->vq;
1073             break;
1074         }
1075     }
1076     spin_unlock_irqrestore(&vcdev->lock, flags);
1077     return vq;
1078 }
1079 
1080 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1081                       __u32 activity)
1082 {
1083     if (vcdev->curr_io & activity) {
1084         switch (activity) {
1085         case VIRTIO_CCW_DOING_READ_FEAT:
1086         case VIRTIO_CCW_DOING_WRITE_FEAT:
1087         case VIRTIO_CCW_DOING_READ_CONFIG:
1088         case VIRTIO_CCW_DOING_WRITE_CONFIG:
1089         case VIRTIO_CCW_DOING_WRITE_STATUS:
1090         case VIRTIO_CCW_DOING_READ_STATUS:
1091         case VIRTIO_CCW_DOING_SET_VQ:
1092         case VIRTIO_CCW_DOING_SET_IND:
1093         case VIRTIO_CCW_DOING_SET_CONF_IND:
1094         case VIRTIO_CCW_DOING_RESET:
1095         case VIRTIO_CCW_DOING_READ_VQ_CONF:
1096         case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1097         case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1098             vcdev->curr_io &= ~activity;
1099             wake_up(&vcdev->wait_q);
1100             break;
1101         default:
1102             /* don't know what to do... */
1103             dev_warn(&vcdev->cdev->dev,
1104                  "Suspicious activity '%08x'\n", activity);
1105             WARN_ON(1);
1106             break;
1107         }
1108     }
1109 }
1110 
1111 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1112                    unsigned long intparm,
1113                    struct irb *irb)
1114 {
1115     __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1116     struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1117     int i;
1118     struct virtqueue *vq;
1119 
1120     if (!vcdev)
1121         return;
1122     if (IS_ERR(irb)) {
1123         vcdev->err = PTR_ERR(irb);
1124         virtio_ccw_check_activity(vcdev, activity);
1125         /* Don't poke around indicators, something's wrong. */
1126         return;
1127     }
1128     /* Check if it's a notification from the host. */
1129     if ((intparm == 0) &&
1130         (scsw_stctl(&irb->scsw) ==
1131          (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1132         /* OK */
1133     }
1134     if (irb_is_error(irb)) {
1135         /* Command reject? */
1136         if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1137             (irb->ecw[0] & SNS0_CMD_REJECT))
1138             vcdev->err = -EOPNOTSUPP;
1139         else
1140             /* Map everything else to -EIO. */
1141             vcdev->err = -EIO;
1142     }
1143     virtio_ccw_check_activity(vcdev, activity);
1144 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1145     /*
1146      * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1147      * disabled here.
1148      */
1149     read_lock(&vcdev->irq_lock);
1150 #endif
1151     for_each_set_bit(i, indicators(vcdev),
1152              sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1153         /* The bit clear must happen before the vring kick. */
1154         clear_bit(i, indicators(vcdev));
1155         barrier();
1156         vq = virtio_ccw_vq_by_ind(vcdev, i);
1157         vring_interrupt(0, vq);
1158     }
1159 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1160     read_unlock(&vcdev->irq_lock);
1161 #endif
1162     if (test_bit(0, indicators2(vcdev))) {
1163         virtio_config_changed(&vcdev->vdev);
1164         clear_bit(0, indicators2(vcdev));
1165     }
1166 }
1167 
1168 /*
1169  * We usually want to autoonline all devices, but give the admin
1170  * a way to exempt devices from this.
1171  */
1172 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1173              (8*sizeof(long)))
1174 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1175 
1176 static char *no_auto = "";
1177 
1178 module_param(no_auto, charp, 0444);
1179 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1180 
1181 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1182 {
1183     struct ccw_dev_id id;
1184 
1185     ccw_device_get_id(cdev, &id);
1186     if (test_bit(id.devno, devs_no_auto[id.ssid]))
1187         return 0;
1188     return 1;
1189 }
1190 
1191 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1192 {
1193     struct ccw_device *cdev = data;
1194     int ret;
1195 
1196     ret = ccw_device_set_online(cdev);
1197     if (ret)
1198         dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1199 }
1200 
1201 static int virtio_ccw_probe(struct ccw_device *cdev)
1202 {
1203     cdev->handler = virtio_ccw_int_handler;
1204 
1205     if (virtio_ccw_check_autoonline(cdev))
1206         async_schedule(virtio_ccw_auto_online, cdev);
1207     return 0;
1208 }
1209 
1210 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1211 {
1212     unsigned long flags;
1213     struct virtio_ccw_device *vcdev;
1214 
1215     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1216     vcdev = dev_get_drvdata(&cdev->dev);
1217     if (!vcdev || vcdev->going_away) {
1218         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1219         return NULL;
1220     }
1221     vcdev->going_away = true;
1222     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1223     return vcdev;
1224 }
1225 
1226 static void virtio_ccw_remove(struct ccw_device *cdev)
1227 {
1228     unsigned long flags;
1229     struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1230 
1231     if (vcdev && cdev->online) {
1232         if (vcdev->device_lost)
1233             virtio_break_device(&vcdev->vdev);
1234         unregister_virtio_device(&vcdev->vdev);
1235         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1236         dev_set_drvdata(&cdev->dev, NULL);
1237         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1238     }
1239     cdev->handler = NULL;
1240 }
1241 
1242 static int virtio_ccw_offline(struct ccw_device *cdev)
1243 {
1244     unsigned long flags;
1245     struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1246 
1247     if (!vcdev)
1248         return 0;
1249     if (vcdev->device_lost)
1250         virtio_break_device(&vcdev->vdev);
1251     unregister_virtio_device(&vcdev->vdev);
1252     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1253     dev_set_drvdata(&cdev->dev, NULL);
1254     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1255     return 0;
1256 }
1257 
1258 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1259 {
1260     struct virtio_rev_info *rev;
1261     struct ccw1 *ccw;
1262     int ret;
1263 
1264     ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
1265     if (!ccw)
1266         return -ENOMEM;
1267     rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev));
1268     if (!rev) {
1269         ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1270         return -ENOMEM;
1271     }
1272 
1273     /* Set transport revision */
1274     ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1275     ccw->flags = 0;
1276     ccw->count = sizeof(*rev);
1277     ccw->cda = (__u32)(unsigned long)rev;
1278 
1279     vcdev->revision = VIRTIO_CCW_REV_MAX;
1280     do {
1281         rev->revision = vcdev->revision;
1282         /* none of our supported revisions carry payload */
1283         rev->length = 0;
1284         ret = ccw_io_helper(vcdev, ccw,
1285                     VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1286         if (ret == -EOPNOTSUPP) {
1287             if (vcdev->revision == 0)
1288                 /*
1289                  * The host device does not support setting
1290                  * the revision: let's operate it in legacy
1291                  * mode.
1292                  */
1293                 ret = 0;
1294             else
1295                 vcdev->revision--;
1296         }
1297     } while (ret == -EOPNOTSUPP);
1298 
1299     ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1300     ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1301     return ret;
1302 }
1303 
1304 static int virtio_ccw_online(struct ccw_device *cdev)
1305 {
1306     int ret;
1307     struct virtio_ccw_device *vcdev;
1308     unsigned long flags;
1309 
1310     vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1311     if (!vcdev) {
1312         dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1313         ret = -ENOMEM;
1314         goto out_free;
1315     }
1316     vcdev->vdev.dev.parent = &cdev->dev;
1317     vcdev->cdev = cdev;
1318     vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1319                         sizeof(*vcdev->dma_area));
1320     if (!vcdev->dma_area) {
1321         ret = -ENOMEM;
1322         goto out_free;
1323     }
1324 
1325     vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1326 
1327     vcdev->vdev.dev.release = virtio_ccw_release_dev;
1328     vcdev->vdev.config = &virtio_ccw_config_ops;
1329     init_waitqueue_head(&vcdev->wait_q);
1330     INIT_LIST_HEAD(&vcdev->virtqueues);
1331     spin_lock_init(&vcdev->lock);
1332     rwlock_init(&vcdev->irq_lock);
1333     mutex_init(&vcdev->io_lock);
1334 
1335     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1336     dev_set_drvdata(&cdev->dev, vcdev);
1337     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1338     vcdev->vdev.id.vendor = cdev->id.cu_type;
1339     vcdev->vdev.id.device = cdev->id.cu_model;
1340 
1341     ret = virtio_ccw_set_transport_rev(vcdev);
1342     if (ret)
1343         goto out_free;
1344 
1345     ret = register_virtio_device(&vcdev->vdev);
1346     if (ret) {
1347         dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1348              ret);
1349         goto out_put;
1350     }
1351     return 0;
1352 out_put:
1353     spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1354     dev_set_drvdata(&cdev->dev, NULL);
1355     spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1356     put_device(&vcdev->vdev.dev);
1357     return ret;
1358 out_free:
1359     if (vcdev) {
1360         ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1361                     sizeof(*vcdev->dma_area));
1362     }
1363     kfree(vcdev);
1364     return ret;
1365 }
1366 
1367 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1368 {
1369     int rc;
1370     struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1371 
1372     /*
1373      * Make sure vcdev is set
1374      * i.e. set_offline/remove callback not already running
1375      */
1376     if (!vcdev)
1377         return NOTIFY_DONE;
1378 
1379     switch (event) {
1380     case CIO_GONE:
1381         vcdev->device_lost = true;
1382         rc = NOTIFY_DONE;
1383         break;
1384     case CIO_OPER:
1385         rc = NOTIFY_OK;
1386         break;
1387     default:
1388         rc = NOTIFY_DONE;
1389         break;
1390     }
1391     return rc;
1392 }
1393 
1394 static struct ccw_device_id virtio_ids[] = {
1395     { CCW_DEVICE(0x3832, 0) },
1396     {},
1397 };
1398 
1399 static struct ccw_driver virtio_ccw_driver = {
1400     .driver = {
1401         .owner = THIS_MODULE,
1402         .name = "virtio_ccw",
1403     },
1404     .ids = virtio_ids,
1405     .probe = virtio_ccw_probe,
1406     .remove = virtio_ccw_remove,
1407     .set_offline = virtio_ccw_offline,
1408     .set_online = virtio_ccw_online,
1409     .notify = virtio_ccw_cio_notify,
1410     .int_class = IRQIO_VIR,
1411 };
1412 
1413 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1414                int max_digit, int max_val)
1415 {
1416     int diff;
1417 
1418     diff = 0;
1419     *val = 0;
1420 
1421     while (diff <= max_digit) {
1422         int value = hex_to_bin(**cp);
1423 
1424         if (value < 0)
1425             break;
1426         *val = *val * 16 + value;
1427         (*cp)++;
1428         diff++;
1429     }
1430 
1431     if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1432         return 1;
1433 
1434     return 0;
1435 }
1436 
1437 static int __init parse_busid(char *str, unsigned int *cssid,
1438                   unsigned int *ssid, unsigned int *devno)
1439 {
1440     char *str_work;
1441     int rc, ret;
1442 
1443     rc = 1;
1444 
1445     if (*str == '\0')
1446         goto out;
1447 
1448     str_work = str;
1449     ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1450     if (ret || (str_work[0] != '.'))
1451         goto out;
1452     str_work++;
1453     ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1454     if (ret || (str_work[0] != '.'))
1455         goto out;
1456     str_work++;
1457     ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1458     if (ret || (str_work[0] != '\0'))
1459         goto out;
1460 
1461     rc = 0;
1462 out:
1463     return rc;
1464 }
1465 
1466 static void __init no_auto_parse(void)
1467 {
1468     unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1469     char *parm, *str;
1470     int rc;
1471 
1472     str = no_auto;
1473     while ((parm = strsep(&str, ","))) {
1474         rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1475                  &from_ssid, &from);
1476         if (rc)
1477             continue;
1478         if (parm != NULL) {
1479             rc = parse_busid(parm, &to_cssid,
1480                      &to_ssid, &to);
1481             if ((from_ssid > to_ssid) ||
1482                 ((from_ssid == to_ssid) && (from > to)))
1483                 rc = -EINVAL;
1484         } else {
1485             to_cssid = from_cssid;
1486             to_ssid = from_ssid;
1487             to = from;
1488         }
1489         if (rc)
1490             continue;
1491         while ((from_ssid < to_ssid) ||
1492                ((from_ssid == to_ssid) && (from <= to))) {
1493             set_bit(from, devs_no_auto[from_ssid]);
1494             from++;
1495             if (from > __MAX_SUBCHANNEL) {
1496                 from_ssid++;
1497                 from = 0;
1498             }
1499         }
1500     }
1501 }
1502 
1503 static int __init virtio_ccw_init(void)
1504 {
1505     int rc;
1506 
1507     /* parse no_auto string before we do anything further */
1508     no_auto_parse();
1509 
1510     summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1511     if (!summary_indicators)
1512         return -ENOMEM;
1513     rc = ccw_driver_register(&virtio_ccw_driver);
1514     if (rc)
1515         cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1516     return rc;
1517 }
1518 device_initcall(virtio_ccw_init);