linux/drivers/s390/virtio/virtio_ccw.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * ccw based virtio transport
   4 *
   5 * Copyright IBM Corp. 2012, 2014
   6 *
   7 *    Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
   8 */
   9
  10#include <linux/kernel_stat.h>
  11#include <linux/init.h>
  12#include <linux/bootmem.h>
  13#include <linux/err.h>
  14#include <linux/virtio.h>
  15#include <linux/virtio_config.h>
  16#include <linux/slab.h>
  17#include <linux/interrupt.h>
  18#include <linux/virtio_ring.h>
  19#include <linux/pfn.h>
  20#include <linux/async.h>
  21#include <linux/wait.h>
  22#include <linux/list.h>
  23#include <linux/bitops.h>
  24#include <linux/moduleparam.h>
  25#include <linux/io.h>
  26#include <linux/kvm_para.h>
  27#include <linux/notifier.h>
  28#include <asm/diag.h>
  29#include <asm/setup.h>
  30#include <asm/irq.h>
  31#include <asm/cio.h>
  32#include <asm/ccwdev.h>
  33#include <asm/virtio-ccw.h>
  34#include <asm/isc.h>
  35#include <asm/airq.h>
  36
  37/*
  38 * virtio related functions
  39 */
  40
  41struct vq_config_block {
  42        __u16 index;
  43        __u16 num;
  44} __packed;
  45
  46#define VIRTIO_CCW_CONFIG_SIZE 0x100
  47/* same as PCI config space size, should be enough for all drivers */
  48
  49struct vcdev_dma_area {
  50        unsigned long indicators;
  51        unsigned long indicators2;
  52        struct vq_config_block config_block;
  53        __u8 status;
  54};
  55
  56struct virtio_ccw_device {
  57        struct virtio_device vdev;
  58        __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  59        struct ccw_device *cdev;
  60        __u32 curr_io;
  61        int err;
  62        unsigned int revision; /* Transport revision */
  63        wait_queue_head_t wait_q;
  64        spinlock_t lock;
  65        struct mutex io_lock; /* Serializes I/O requests */
  66        struct list_head virtqueues;
  67        bool is_thinint;
  68        bool going_away;
  69        bool device_lost;
  70        unsigned int config_ready;
  71        void *airq_info;
  72        struct vcdev_dma_area *dma_area;
  73};
  74
  75static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
  76{
  77        return &vcdev->dma_area->indicators;
  78}
  79
  80static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
  81{
  82        return &vcdev->dma_area->indicators2;
  83}
  84
  85struct vq_info_block_legacy {
  86        __u64 queue;
  87        __u32 align;
  88        __u16 index;
  89        __u16 num;
  90} __packed;
  91
  92struct vq_info_block {
  93        __u64 desc;
  94        __u32 res0;
  95        __u16 index;
  96        __u16 num;
  97        __u64 avail;
  98        __u64 used;
  99} __packed;
 100
 101struct virtio_feature_desc {
 102        __le32 features;
 103        __u8 index;
 104} __packed;
 105
 106struct virtio_thinint_area {
 107        unsigned long summary_indicator;
 108        unsigned long indicator;
 109        u64 bit_nr;
 110        u8 isc;
 111} __packed;
 112
 113struct virtio_rev_info {
 114        __u16 revision;
 115        __u16 length;
 116        __u8 data[];
 117};
 118
 119/* the highest virtio-ccw revision we support */
 120#define VIRTIO_CCW_REV_MAX 1
 121
 122struct virtio_ccw_vq_info {
 123        struct virtqueue *vq;
 124        int num;
 125        union {
 126                struct vq_info_block s;
 127                struct vq_info_block_legacy l;
 128        } *info_block;
 129        int bit_nr;
 130        struct list_head node;
 131        long cookie;
 132};
 133
 134#define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
 135
 136#define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
 137#define MAX_AIRQ_AREAS 20
 138
 139static int virtio_ccw_use_airq = 1;
 140
 141struct airq_info {
 142        rwlock_t lock;
 143        u8 summary_indicator_idx;
 144        struct airq_struct airq;
 145        struct airq_iv *aiv;
 146};
 147static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
 148static DEFINE_MUTEX(airq_areas_lock);
 149
 150static u8 *summary_indicators;
 151
 152static inline u8 *get_summary_indicator(struct airq_info *info)
 153{
 154        return summary_indicators + info->summary_indicator_idx;
 155}
 156
 157#define CCW_CMD_SET_VQ 0x13
 158#define CCW_CMD_VDEV_RESET 0x33
 159#define CCW_CMD_SET_IND 0x43
 160#define CCW_CMD_SET_CONF_IND 0x53
 161#define CCW_CMD_READ_FEAT 0x12
 162#define CCW_CMD_WRITE_FEAT 0x11
 163#define CCW_CMD_READ_CONF 0x22
 164#define CCW_CMD_WRITE_CONF 0x21
 165#define CCW_CMD_WRITE_STATUS 0x31
 166#define CCW_CMD_READ_VQ_CONF 0x32
 167#define CCW_CMD_READ_STATUS 0x72
 168#define CCW_CMD_SET_IND_ADAPTER 0x73
 169#define CCW_CMD_SET_VIRTIO_REV 0x83
 170
 171#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
 172#define VIRTIO_CCW_DOING_RESET 0x00040000
 173#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
 174#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
 175#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
 176#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
 177#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
 178#define VIRTIO_CCW_DOING_SET_IND 0x01000000
 179#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
 180#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
 181#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
 182#define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
 183#define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
 184#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
 185
 186static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
 187{
 188        return container_of(vdev, struct virtio_ccw_device, vdev);
 189}
 190
 191static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
 192{
 193        unsigned long i, flags;
 194
 195        write_lock_irqsave(&info->lock, flags);
 196        for (i = 0; i < airq_iv_end(info->aiv); i++) {
 197                if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
 198                        airq_iv_free_bit(info->aiv, i);
 199                        airq_iv_set_ptr(info->aiv, i, 0);
 200                        break;
 201                }
 202        }
 203        write_unlock_irqrestore(&info->lock, flags);
 204}
 205
 206static void virtio_airq_handler(struct airq_struct *airq, bool floating)
 207{
 208        struct airq_info *info = container_of(airq, struct airq_info, airq);
 209        unsigned long ai;
 210
 211        inc_irq_stat(IRQIO_VAI);
 212        read_lock(&info->lock);
 213        /* Walk through indicators field, summary indicator active. */
 214        for (ai = 0;;) {
 215                ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
 216                if (ai == -1UL)
 217                        break;
 218                vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
 219        }
 220        *(get_summary_indicator(info)) = 0;
 221        smp_wmb();
 222        /* Walk through indicators field, summary indicator not active. */
 223        for (ai = 0;;) {
 224                ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
 225                if (ai == -1UL)
 226                        break;
 227                vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
 228        }
 229        read_unlock(&info->lock);
 230}
 231
 232static struct airq_info *new_airq_info(int index)
 233{
 234        struct airq_info *info;
 235        int rc;
 236
 237        info = kzalloc(sizeof(*info), GFP_KERNEL);
 238        if (!info)
 239                return NULL;
 240        rwlock_init(&info->lock);
 241        info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
 242                                   | AIRQ_IV_CACHELINE);
 243        if (!info->aiv) {
 244                kfree(info);
 245                return NULL;
 246        }
 247        info->airq.handler = virtio_airq_handler;
 248        info->summary_indicator_idx = index;
 249        info->airq.lsi_ptr = get_summary_indicator(info);
 250        info->airq.lsi_mask = 0xff;
 251        info->airq.isc = VIRTIO_AIRQ_ISC;
 252        rc = register_adapter_interrupt(&info->airq);
 253        if (rc) {
 254                airq_iv_release(info->aiv);
 255                kfree(info);
 256                return NULL;
 257        }
 258        return info;
 259}
 260
 261static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
 262                                        u64 *first, void **airq_info)
 263{
 264        int i, j;
 265        struct airq_info *info;
 266        unsigned long indicator_addr = 0;
 267        unsigned long bit, flags;
 268
 269        for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
 270                mutex_lock(&airq_areas_lock);
 271                if (!airq_areas[i])
 272                        airq_areas[i] = new_airq_info(i);
 273                info = airq_areas[i];
 274                mutex_unlock(&airq_areas_lock);
 275                if (!info)
 276                        return 0;
 277                write_lock_irqsave(&info->lock, flags);
 278                bit = airq_iv_alloc(info->aiv, nvqs);
 279                if (bit == -1UL) {
 280                        /* Not enough vacancies. */
 281                        write_unlock_irqrestore(&info->lock, flags);
 282                        continue;
 283                }
 284                *first = bit;
 285                *airq_info = info;
 286                indicator_addr = (unsigned long)info->aiv->vector;
 287                for (j = 0; j < nvqs; j++) {
 288                        airq_iv_set_ptr(info->aiv, bit + j,
 289                                        (unsigned long)vqs[j]);
 290                }
 291                write_unlock_irqrestore(&info->lock, flags);
 292        }
 293        return indicator_addr;
 294}
 295
 296static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
 297{
 298        struct virtio_ccw_vq_info *info;
 299
 300        if (!vcdev->airq_info)
 301                return;
 302        list_for_each_entry(info, &vcdev->virtqueues, node)
 303                drop_airq_indicator(info->vq, vcdev->airq_info);
 304}
 305
 306static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
 307{
 308        unsigned long flags;
 309        __u32 ret;
 310
 311        spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
 312        if (vcdev->err)
 313                ret = 0;
 314        else
 315                ret = vcdev->curr_io & flag;
 316        spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
 317        return ret;
 318}
 319
 320static int ccw_io_helper(struct virtio_ccw_device *vcdev,
 321                         struct ccw1 *ccw, __u32 intparm)
 322{
 323        int ret;
 324        unsigned long flags;
 325        int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
 326
 327        mutex_lock(&vcdev->io_lock);
 328        do {
 329                spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
 330                ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
 331                if (!ret) {
 332                        if (!vcdev->curr_io)
 333                                vcdev->err = 0;
 334                        vcdev->curr_io |= flag;
 335                }
 336                spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
 337                cpu_relax();
 338        } while (ret == -EBUSY);
 339        wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
 340        ret = ret ? ret : vcdev->err;
 341        mutex_unlock(&vcdev->io_lock);
 342        return ret;
 343}
 344
 345static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
 346                                      struct ccw1 *ccw)
 347{
 348        int ret;
 349        unsigned long *indicatorp = NULL;
 350        struct virtio_thinint_area *thinint_area = NULL;
 351        struct airq_info *airq_info = vcdev->airq_info;
 352
 353        if (vcdev->is_thinint) {
 354                thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
 355                                                     sizeof(*thinint_area));
 356                if (!thinint_area)
 357                        return;
 358                thinint_area->summary_indicator =
 359                        (unsigned long) get_summary_indicator(airq_info);
 360                thinint_area->isc = VIRTIO_AIRQ_ISC;
 361                ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
 362                ccw->count = sizeof(*thinint_area);
 363                ccw->cda = (__u32)(unsigned long) thinint_area;
 364        } else {
 365                /* payload is the address of the indicators */
 366                indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
 367                                                   sizeof(indicators(vcdev)));
 368                if (!indicatorp)
 369                        return;
 370                *indicatorp = 0;
 371                ccw->cmd_code = CCW_CMD_SET_IND;
 372                ccw->count = sizeof(indicators(vcdev));
 373                ccw->cda = (__u32)(unsigned long) indicatorp;
 374        }
 375        /* Deregister indicators from host. */
 376        *indicators(vcdev) = 0;
 377        ccw->flags = 0;
 378        ret = ccw_io_helper(vcdev, ccw,
 379                            vcdev->is_thinint ?
 380                            VIRTIO_CCW_DOING_SET_IND_ADAPTER :
 381                            VIRTIO_CCW_DOING_SET_IND);
 382        if (ret && (ret != -ENODEV))
 383                dev_info(&vcdev->cdev->dev,
 384                         "Failed to deregister indicators (%d)\n", ret);
 385        else if (vcdev->is_thinint)
 386                virtio_ccw_drop_indicators(vcdev);
 387        ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(indicators(vcdev)));
 388        ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
 389}
 390
 391static inline long __do_kvm_notify(struct subchannel_id schid,
 392                                   unsigned long queue_index,
 393                                   long cookie)
 394{
 395        register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
 396        register struct subchannel_id __schid asm("2") = schid;
 397        register unsigned long __index asm("3") = queue_index;
 398        register long __rc asm("2");
 399        register long __cookie asm("4") = cookie;
 400
 401        asm volatile ("diag 2,4,0x500\n"
 402                      : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
 403                      "d"(__cookie)
 404                      : "memory", "cc");
 405        return __rc;
 406}
 407
 408static inline long do_kvm_notify(struct subchannel_id schid,
 409                                 unsigned long queue_index,
 410                                 long cookie)
 411{
 412        diag_stat_inc(DIAG_STAT_X500);
 413        return __do_kvm_notify(schid, queue_index, cookie);
 414}
 415
 416static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
 417{
 418        struct virtio_ccw_vq_info *info = vq->priv;
 419        struct virtio_ccw_device *vcdev;
 420        struct subchannel_id schid;
 421
 422        vcdev = to_vc_device(info->vq->vdev);
 423        ccw_device_get_schid(vcdev->cdev, &schid);
 424        info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
 425        if (info->cookie < 0)
 426                return false;
 427        return true;
 428}
 429
 430static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
 431                                   struct ccw1 *ccw, int index)
 432{
 433        int ret;
 434
 435        vcdev->dma_area->config_block.index = index;
 436        ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
 437        ccw->flags = 0;
 438        ccw->count = sizeof(struct vq_config_block);
 439        ccw->cda = (__u32)(unsigned long)(&vcdev->dma_area->config_block);
 440        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
 441        if (ret)
 442                return ret;
 443        return vcdev->dma_area->config_block.num ?: -ENOENT;
 444}
 445
 446static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
 447{
 448        struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
 449        struct virtio_ccw_vq_info *info = vq->priv;
 450        unsigned long flags;
 451        int ret;
 452        unsigned int index = vq->index;
 453
 454        /* Remove from our list. */
 455        spin_lock_irqsave(&vcdev->lock, flags);
 456        list_del(&info->node);
 457        spin_unlock_irqrestore(&vcdev->lock, flags);
 458
 459        /* Release from host. */
 460        if (vcdev->revision == 0) {
 461                info->info_block->l.queue = 0;
 462                info->info_block->l.align = 0;
 463                info->info_block->l.index = index;
 464                info->info_block->l.num = 0;
 465                ccw->count = sizeof(info->info_block->l);
 466        } else {
 467                info->info_block->s.desc = 0;
 468                info->info_block->s.index = index;
 469                info->info_block->s.num = 0;
 470                info->info_block->s.avail = 0;
 471                info->info_block->s.used = 0;
 472                ccw->count = sizeof(info->info_block->s);
 473        }
 474        ccw->cmd_code = CCW_CMD_SET_VQ;
 475        ccw->flags = 0;
 476        ccw->cda = (__u32)(unsigned long)(info->info_block);
 477        ret = ccw_io_helper(vcdev, ccw,
 478                            VIRTIO_CCW_DOING_SET_VQ | index);
 479        /*
 480         * -ENODEV isn't considered an error: The device is gone anyway.
 481         * This may happen on device detach.
 482         */
 483        if (ret && (ret != -ENODEV))
 484                dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
 485                         ret, index);
 486
 487        vring_del_virtqueue(vq);
 488        ccw_device_dma_free(vcdev->cdev, info->info_block,
 489                            sizeof(*info->info_block));
 490        kfree(info);
 491}
 492
 493static void virtio_ccw_del_vqs(struct virtio_device *vdev)
 494{
 495        struct virtqueue *vq, *n;
 496        struct ccw1 *ccw;
 497        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 498
 499        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 500        if (!ccw)
 501                return;
 502
 503        virtio_ccw_drop_indicator(vcdev, ccw);
 504
 505        list_for_each_entry_safe(vq, n, &vdev->vqs, list)
 506                virtio_ccw_del_vq(vq, ccw);
 507
 508        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 509}
 510
 511static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
 512                                             int i, vq_callback_t *callback,
 513                                             const char *name, bool ctx,
 514                                             struct ccw1 *ccw)
 515{
 516        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 517        int err;
 518        struct virtqueue *vq = NULL;
 519        struct virtio_ccw_vq_info *info;
 520        u64 queue;
 521        unsigned long flags;
 522        bool may_reduce;
 523
 524        /* Allocate queue. */
 525        info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
 526        if (!info) {
 527                dev_warn(&vcdev->cdev->dev, "no info\n");
 528                err = -ENOMEM;
 529                goto out_err;
 530        }
 531        info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
 532                                                 sizeof(*info->info_block));
 533        if (!info->info_block) {
 534                dev_warn(&vcdev->cdev->dev, "no info block\n");
 535                err = -ENOMEM;
 536                goto out_err;
 537        }
 538        info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
 539        if (info->num < 0) {
 540                err = info->num;
 541                goto out_err;
 542        }
 543        may_reduce = vcdev->revision > 0;
 544        vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
 545                                    vdev, true, may_reduce, ctx,
 546                                    virtio_ccw_kvm_notify, callback, name);
 547
 548        if (!vq) {
 549                /* For now, we fail if we can't get the requested size. */
 550                dev_warn(&vcdev->cdev->dev, "no vq\n");
 551                err = -ENOMEM;
 552                goto out_err;
 553        }
 554        /* it may have been reduced */
 555        info->num = virtqueue_get_vring_size(vq);
 556
 557        /* Register it with the host. */
 558        queue = virtqueue_get_desc_addr(vq);
 559        if (vcdev->revision == 0) {
 560                info->info_block->l.queue = queue;
 561                info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
 562                info->info_block->l.index = i;
 563                info->info_block->l.num = info->num;
 564                ccw->count = sizeof(info->info_block->l);
 565        } else {
 566                info->info_block->s.desc = queue;
 567                info->info_block->s.index = i;
 568                info->info_block->s.num = info->num;
 569                info->info_block->s.avail = (__u64)virtqueue_get_avail_addr(vq);
 570                info->info_block->s.used = (__u64)virtqueue_get_used_addr(vq);
 571                ccw->count = sizeof(info->info_block->s);
 572        }
 573        ccw->cmd_code = CCW_CMD_SET_VQ;
 574        ccw->flags = 0;
 575        ccw->cda = (__u32)(unsigned long)(info->info_block);
 576        err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
 577        if (err) {
 578                dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
 579                goto out_err;
 580        }
 581
 582        info->vq = vq;
 583        vq->priv = info;
 584
 585        /* Save it to our list. */
 586        spin_lock_irqsave(&vcdev->lock, flags);
 587        list_add(&info->node, &vcdev->virtqueues);
 588        spin_unlock_irqrestore(&vcdev->lock, flags);
 589
 590        return vq;
 591
 592out_err:
 593        if (vq)
 594                vring_del_virtqueue(vq);
 595        if (info) {
 596                ccw_device_dma_free(vcdev->cdev, info->info_block,
 597                                    sizeof(*info->info_block));
 598        }
 599        kfree(info);
 600        return ERR_PTR(err);
 601}
 602
 603static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
 604                                           struct virtqueue *vqs[], int nvqs,
 605                                           struct ccw1 *ccw)
 606{
 607        int ret;
 608        struct virtio_thinint_area *thinint_area = NULL;
 609        struct airq_info *info;
 610
 611        thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
 612                                             sizeof(*thinint_area));
 613        if (!thinint_area) {
 614                ret = -ENOMEM;
 615                goto out;
 616        }
 617        /* Try to get an indicator. */
 618        thinint_area->indicator = get_airq_indicator(vqs, nvqs,
 619                                                     &thinint_area->bit_nr,
 620                                                     &vcdev->airq_info);
 621        if (!thinint_area->indicator) {
 622                ret = -ENOSPC;
 623                goto out;
 624        }
 625        info = vcdev->airq_info;
 626        thinint_area->summary_indicator =
 627                (unsigned long) get_summary_indicator(info);
 628        thinint_area->isc = VIRTIO_AIRQ_ISC;
 629        ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
 630        ccw->flags = CCW_FLAG_SLI;
 631        ccw->count = sizeof(*thinint_area);
 632        ccw->cda = (__u32)(unsigned long)thinint_area;
 633        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
 634        if (ret) {
 635                if (ret == -EOPNOTSUPP) {
 636                        /*
 637                         * The host does not support adapter interrupts
 638                         * for virtio-ccw, stop trying.
 639                         */
 640                        virtio_ccw_use_airq = 0;
 641                        pr_info("Adapter interrupts unsupported on host\n");
 642                } else
 643                        dev_warn(&vcdev->cdev->dev,
 644                                 "enabling adapter interrupts = %d\n", ret);
 645                virtio_ccw_drop_indicators(vcdev);
 646        }
 647out:
 648        ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
 649        return ret;
 650}
 651
 652static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
 653                               struct virtqueue *vqs[],
 654                               vq_callback_t *callbacks[],
 655                               const char * const names[],
 656                               const bool *ctx,
 657                               struct irq_affinity *desc)
 658{
 659        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 660        unsigned long *indicatorp = NULL;
 661        int ret, i;
 662        struct ccw1 *ccw;
 663
 664        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 665        if (!ccw)
 666                return -ENOMEM;
 667
 668        for (i = 0; i < nvqs; ++i) {
 669                vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
 670                                             ctx ? ctx[i] : false, ccw);
 671                if (IS_ERR(vqs[i])) {
 672                        ret = PTR_ERR(vqs[i]);
 673                        vqs[i] = NULL;
 674                        goto out;
 675                }
 676        }
 677        ret = -ENOMEM;
 678        /*
 679         * We need a data area under 2G to communicate. Our payload is
 680         * the address of the indicators.
 681        */
 682        indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
 683                                           sizeof(indicators(vcdev)));
 684        if (!indicatorp)
 685                goto out;
 686        *indicatorp = (unsigned long) indicators(vcdev);
 687        if (vcdev->is_thinint) {
 688                ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
 689                if (ret)
 690                        /* no error, just fall back to legacy interrupts */
 691                        vcdev->is_thinint = false;
 692        }
 693        if (!vcdev->is_thinint) {
 694                /* Register queue indicators with host. */
 695                *indicators(vcdev) = 0;
 696                ccw->cmd_code = CCW_CMD_SET_IND;
 697                ccw->flags = 0;
 698                ccw->count = sizeof(indicators(vcdev));
 699                ccw->cda = (__u32)(unsigned long) indicatorp;
 700                ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
 701                if (ret)
 702                        goto out;
 703        }
 704        /* Register indicators2 with host for config changes */
 705        *indicatorp = (unsigned long) indicators2(vcdev);
 706        *indicators2(vcdev) = 0;
 707        ccw->cmd_code = CCW_CMD_SET_CONF_IND;
 708        ccw->flags = 0;
 709        ccw->count = sizeof(indicators2(vcdev));
 710        ccw->cda = (__u32)(unsigned long) indicatorp;
 711        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
 712        if (ret)
 713                goto out;
 714
 715        if (indicatorp)
 716                ccw_device_dma_free(vcdev->cdev, indicatorp,
 717                                    sizeof(indicators(vcdev)));
 718        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 719        return 0;
 720out:
 721        if (indicatorp)
 722                ccw_device_dma_free(vcdev->cdev, indicatorp,
 723                                    sizeof(indicators(vcdev)));
 724        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 725        virtio_ccw_del_vqs(vdev);
 726        return ret;
 727}
 728
 729static void virtio_ccw_reset(struct virtio_device *vdev)
 730{
 731        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 732        struct ccw1 *ccw;
 733
 734        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 735        if (!ccw)
 736                return;
 737
 738        /* Zero status bits. */
 739        vcdev->dma_area->status = 0;
 740
 741        /* Send a reset ccw on device. */
 742        ccw->cmd_code = CCW_CMD_VDEV_RESET;
 743        ccw->flags = 0;
 744        ccw->count = 0;
 745        ccw->cda = 0;
 746        ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
 747        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 748}
 749
 750static u64 virtio_ccw_get_features(struct virtio_device *vdev)
 751{
 752        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 753        struct virtio_feature_desc *features;
 754        int ret;
 755        u64 rc;
 756        struct ccw1 *ccw;
 757
 758        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 759        if (!ccw)
 760                return 0;
 761
 762        features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
 763        if (!features) {
 764                rc = 0;
 765                goto out_free;
 766        }
 767        /* Read the feature bits from the host. */
 768        features->index = 0;
 769        ccw->cmd_code = CCW_CMD_READ_FEAT;
 770        ccw->flags = 0;
 771        ccw->count = sizeof(*features);
 772        ccw->cda = (__u32)(unsigned long)features;
 773        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
 774        if (ret) {
 775                rc = 0;
 776                goto out_free;
 777        }
 778
 779        rc = le32_to_cpu(features->features);
 780
 781        if (vcdev->revision == 0)
 782                goto out_free;
 783
 784        /* Read second half of the feature bits from the host. */
 785        features->index = 1;
 786        ccw->cmd_code = CCW_CMD_READ_FEAT;
 787        ccw->flags = 0;
 788        ccw->count = sizeof(*features);
 789        ccw->cda = (__u32)(unsigned long)features;
 790        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
 791        if (ret == 0)
 792                rc |= (u64)le32_to_cpu(features->features) << 32;
 793
 794out_free:
 795        ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
 796        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 797        return rc;
 798}
 799
 800static int virtio_ccw_finalize_features(struct virtio_device *vdev)
 801{
 802        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 803        struct virtio_feature_desc *features;
 804        struct ccw1 *ccw;
 805        int ret;
 806
 807        if (vcdev->revision >= 1 &&
 808            !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
 809                dev_err(&vdev->dev, "virtio: device uses revision 1 "
 810                        "but does not have VIRTIO_F_VERSION_1\n");
 811                return -EINVAL;
 812        }
 813
 814        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 815        if (!ccw)
 816                return -ENOMEM;
 817
 818        features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
 819        if (!features) {
 820                ret = -ENOMEM;
 821                goto out_free;
 822        }
 823        /* Give virtio_ring a chance to accept features. */
 824        vring_transport_features(vdev);
 825
 826        features->index = 0;
 827        features->features = cpu_to_le32((u32)vdev->features);
 828        /* Write the first half of the feature bits to the host. */
 829        ccw->cmd_code = CCW_CMD_WRITE_FEAT;
 830        ccw->flags = 0;
 831        ccw->count = sizeof(*features);
 832        ccw->cda = (__u32)(unsigned long)features;
 833        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
 834        if (ret)
 835                goto out_free;
 836
 837        if (vcdev->revision == 0)
 838                goto out_free;
 839
 840        features->index = 1;
 841        features->features = cpu_to_le32(vdev->features >> 32);
 842        /* Write the second half of the feature bits to the host. */
 843        ccw->cmd_code = CCW_CMD_WRITE_FEAT;
 844        ccw->flags = 0;
 845        ccw->count = sizeof(*features);
 846        ccw->cda = (__u32)(unsigned long)features;
 847        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
 848
 849out_free:
 850        ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
 851        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 852
 853        return ret;
 854}
 855
 856static void virtio_ccw_get_config(struct virtio_device *vdev,
 857                                  unsigned int offset, void *buf, unsigned len)
 858{
 859        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 860        int ret;
 861        struct ccw1 *ccw;
 862        void *config_area;
 863        unsigned long flags;
 864
 865        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 866        if (!ccw)
 867                return;
 868
 869        config_area = ccw_device_dma_zalloc(vcdev->cdev,
 870                                            VIRTIO_CCW_CONFIG_SIZE);
 871        if (!config_area)
 872                goto out_free;
 873
 874        /* Read the config area from the host. */
 875        ccw->cmd_code = CCW_CMD_READ_CONF;
 876        ccw->flags = 0;
 877        ccw->count = offset + len;
 878        ccw->cda = (__u32)(unsigned long)config_area;
 879        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
 880        if (ret)
 881                goto out_free;
 882
 883        spin_lock_irqsave(&vcdev->lock, flags);
 884        memcpy(vcdev->config, config_area, offset + len);
 885        if (vcdev->config_ready < offset + len)
 886                vcdev->config_ready = offset + len;
 887        spin_unlock_irqrestore(&vcdev->lock, flags);
 888        if (buf)
 889                memcpy(buf, config_area + offset, len);
 890
 891out_free:
 892        ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
 893        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 894}
 895
 896static void virtio_ccw_set_config(struct virtio_device *vdev,
 897                                  unsigned int offset, const void *buf,
 898                                  unsigned len)
 899{
 900        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 901        struct ccw1 *ccw;
 902        void *config_area;
 903        unsigned long flags;
 904
 905        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 906        if (!ccw)
 907                return;
 908
 909        config_area = ccw_device_dma_zalloc(vcdev->cdev,
 910                                            VIRTIO_CCW_CONFIG_SIZE);
 911        if (!config_area)
 912                goto out_free;
 913
 914        /* Make sure we don't overwrite fields. */
 915        if (vcdev->config_ready < offset)
 916                virtio_ccw_get_config(vdev, 0, NULL, offset);
 917        spin_lock_irqsave(&vcdev->lock, flags);
 918        memcpy(&vcdev->config[offset], buf, len);
 919        /* Write the config area to the host. */
 920        memcpy(config_area, vcdev->config, sizeof(vcdev->config));
 921        spin_unlock_irqrestore(&vcdev->lock, flags);
 922        ccw->cmd_code = CCW_CMD_WRITE_CONF;
 923        ccw->flags = 0;
 924        ccw->count = offset + len;
 925        ccw->cda = (__u32)(unsigned long)config_area;
 926        ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
 927
 928out_free:
 929        ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
 930        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 931}
 932
 933static u8 virtio_ccw_get_status(struct virtio_device *vdev)
 934{
 935        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 936        u8 old_status = vcdev->dma_area->status;
 937        struct ccw1 *ccw;
 938
 939        if (vcdev->revision < 1)
 940                return vcdev->dma_area->status;
 941
 942        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 943        if (!ccw)
 944                return old_status;
 945
 946        ccw->cmd_code = CCW_CMD_READ_STATUS;
 947        ccw->flags = 0;
 948        ccw->count = sizeof(vcdev->dma_area->status);
 949        ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
 950        ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
 951/*
 952 * If the channel program failed (should only happen if the device
 953 * was hotunplugged, and then we clean up via the machine check
 954 * handler anyway), vcdev->dma_area->status was not overwritten and we just
 955 * return the old status, which is fine.
 956*/
 957        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 958
 959        return vcdev->dma_area->status;
 960}
 961
 962static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
 963{
 964        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 965        u8 old_status = vcdev->dma_area->status;
 966        struct ccw1 *ccw;
 967        int ret;
 968
 969        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
 970        if (!ccw)
 971                return;
 972
 973        /* Write the status to the host. */
 974        vcdev->dma_area->status = status;
 975        ccw->cmd_code = CCW_CMD_WRITE_STATUS;
 976        ccw->flags = 0;
 977        ccw->count = sizeof(status);
 978        ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
 979        ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
 980        /* Write failed? We assume status is unchanged. */
 981        if (ret)
 982                vcdev->dma_area->status = old_status;
 983        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
 984}
 985
 986static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
 987{
 988        struct virtio_ccw_device *vcdev = to_vc_device(vdev);
 989
 990        return dev_name(&vcdev->cdev->dev);
 991}
 992
 993static const struct virtio_config_ops virtio_ccw_config_ops = {
 994        .get_features = virtio_ccw_get_features,
 995        .finalize_features = virtio_ccw_finalize_features,
 996        .get = virtio_ccw_get_config,
 997        .set = virtio_ccw_set_config,
 998        .get_status = virtio_ccw_get_status,
 999        .set_status = virtio_ccw_set_status,
1000        .reset = virtio_ccw_reset,
1001        .find_vqs = virtio_ccw_find_vqs,
1002        .del_vqs = virtio_ccw_del_vqs,
1003        .bus_name = virtio_ccw_bus_name,
1004};
1005
1006
1007/*
1008 * ccw bus driver related functions
1009 */
1010
1011static void virtio_ccw_release_dev(struct device *_d)
1012{
1013        struct virtio_device *dev = dev_to_virtio(_d);
1014        struct virtio_ccw_device *vcdev = to_vc_device(dev);
1015
1016        ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1017                            sizeof(*vcdev->dma_area));
1018        kfree(vcdev);
1019}
1020
1021static int irb_is_error(struct irb *irb)
1022{
1023        if (scsw_cstat(&irb->scsw) != 0)
1024                return 1;
1025        if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1026                return 1;
1027        if (scsw_cc(&irb->scsw) != 0)
1028                return 1;
1029        return 0;
1030}
1031
1032static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1033                                              int index)
1034{
1035        struct virtio_ccw_vq_info *info;
1036        unsigned long flags;
1037        struct virtqueue *vq;
1038
1039        vq = NULL;
1040        spin_lock_irqsave(&vcdev->lock, flags);
1041        list_for_each_entry(info, &vcdev->virtqueues, node) {
1042                if (info->vq->index == index) {
1043                        vq = info->vq;
1044                        break;
1045                }
1046        }
1047        spin_unlock_irqrestore(&vcdev->lock, flags);
1048        return vq;
1049}
1050
1051static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1052                                      __u32 activity)
1053{
1054        if (vcdev->curr_io & activity) {
1055                switch (activity) {
1056                case VIRTIO_CCW_DOING_READ_FEAT:
1057                case VIRTIO_CCW_DOING_WRITE_FEAT:
1058                case VIRTIO_CCW_DOING_READ_CONFIG:
1059                case VIRTIO_CCW_DOING_WRITE_CONFIG:
1060                case VIRTIO_CCW_DOING_WRITE_STATUS:
1061                case VIRTIO_CCW_DOING_READ_STATUS:
1062                case VIRTIO_CCW_DOING_SET_VQ:
1063                case VIRTIO_CCW_DOING_SET_IND:
1064                case VIRTIO_CCW_DOING_SET_CONF_IND:
1065                case VIRTIO_CCW_DOING_RESET:
1066                case VIRTIO_CCW_DOING_READ_VQ_CONF:
1067                case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1068                case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1069                        vcdev->curr_io &= ~activity;
1070                        wake_up(&vcdev->wait_q);
1071                        break;
1072                default:
1073                        /* don't know what to do... */
1074                        dev_warn(&vcdev->cdev->dev,
1075                                 "Suspicious activity '%08x'\n", activity);
1076                        WARN_ON(1);
1077                        break;
1078                }
1079        }
1080}
1081
1082static void virtio_ccw_int_handler(struct ccw_device *cdev,
1083                                   unsigned long intparm,
1084                                   struct irb *irb)
1085{
1086        __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1087        struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1088        int i;
1089        struct virtqueue *vq;
1090
1091        if (!vcdev)
1092                return;
1093        if (IS_ERR(irb)) {
1094                vcdev->err = PTR_ERR(irb);
1095                virtio_ccw_check_activity(vcdev, activity);
1096                /* Don't poke around indicators, something's wrong. */
1097                return;
1098        }
1099        /* Check if it's a notification from the host. */
1100        if ((intparm == 0) &&
1101            (scsw_stctl(&irb->scsw) ==
1102             (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1103                /* OK */
1104        }
1105        if (irb_is_error(irb)) {
1106                /* Command reject? */
1107                if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1108                    (irb->ecw[0] & SNS0_CMD_REJECT))
1109                        vcdev->err = -EOPNOTSUPP;
1110                else
1111                        /* Map everything else to -EIO. */
1112                        vcdev->err = -EIO;
1113        }
1114        virtio_ccw_check_activity(vcdev, activity);
1115        for_each_set_bit(i, indicators(vcdev),
1116                         sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1117                /* The bit clear must happen before the vring kick. */
1118                clear_bit(i, indicators(vcdev));
1119                barrier();
1120                vq = virtio_ccw_vq_by_ind(vcdev, i);
1121                vring_interrupt(0, vq);
1122        }
1123        if (test_bit(0, indicators2(vcdev))) {
1124                virtio_config_changed(&vcdev->vdev);
1125                clear_bit(0, indicators2(vcdev));
1126        }
1127}
1128
1129/*
1130 * We usually want to autoonline all devices, but give the admin
1131 * a way to exempt devices from this.
1132 */
1133#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1134                     (8*sizeof(long)))
1135static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1136
1137static char *no_auto = "";
1138
1139module_param(no_auto, charp, 0444);
1140MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1141
1142static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1143{
1144        struct ccw_dev_id id;
1145
1146        ccw_device_get_id(cdev, &id);
1147        if (test_bit(id.devno, devs_no_auto[id.ssid]))
1148                return 0;
1149        return 1;
1150}
1151
1152static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1153{
1154        struct ccw_device *cdev = data;
1155        int ret;
1156
1157        ret = ccw_device_set_online(cdev);
1158        if (ret)
1159                dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1160}
1161
1162static int virtio_ccw_probe(struct ccw_device *cdev)
1163{
1164        cdev->handler = virtio_ccw_int_handler;
1165
1166        if (virtio_ccw_check_autoonline(cdev))
1167                async_schedule(virtio_ccw_auto_online, cdev);
1168        return 0;
1169}
1170
1171static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1172{
1173        unsigned long flags;
1174        struct virtio_ccw_device *vcdev;
1175
1176        spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1177        vcdev = dev_get_drvdata(&cdev->dev);
1178        if (!vcdev || vcdev->going_away) {
1179                spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1180                return NULL;
1181        }
1182        vcdev->going_away = true;
1183        spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1184        return vcdev;
1185}
1186
1187static void virtio_ccw_remove(struct ccw_device *cdev)
1188{
1189        unsigned long flags;
1190        struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1191
1192        if (vcdev && cdev->online) {
1193                if (vcdev->device_lost)
1194                        virtio_break_device(&vcdev->vdev);
1195                unregister_virtio_device(&vcdev->vdev);
1196                spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1197                dev_set_drvdata(&cdev->dev, NULL);
1198                spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1199        }
1200        cdev->handler = NULL;
1201}
1202
1203static int virtio_ccw_offline(struct ccw_device *cdev)
1204{
1205        unsigned long flags;
1206        struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1207
1208        if (!vcdev)
1209                return 0;
1210        if (vcdev->device_lost)
1211                virtio_break_device(&vcdev->vdev);
1212        unregister_virtio_device(&vcdev->vdev);
1213        spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1214        dev_set_drvdata(&cdev->dev, NULL);
1215        spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1216        return 0;
1217}
1218
1219static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1220{
1221        struct virtio_rev_info *rev;
1222        struct ccw1 *ccw;
1223        int ret;
1224
1225        ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
1226        if (!ccw)
1227                return -ENOMEM;
1228        rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev));
1229        if (!rev) {
1230                ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1231                return -ENOMEM;
1232        }
1233
1234        /* Set transport revision */
1235        ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1236        ccw->flags = 0;
1237        ccw->count = sizeof(*rev);
1238        ccw->cda = (__u32)(unsigned long)rev;
1239
1240        vcdev->revision = VIRTIO_CCW_REV_MAX;
1241        do {
1242                rev->revision = vcdev->revision;
1243                /* none of our supported revisions carry payload */
1244                rev->length = 0;
1245                ret = ccw_io_helper(vcdev, ccw,
1246                                    VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1247                if (ret == -EOPNOTSUPP) {
1248                        if (vcdev->revision == 0)
1249                                /*
1250                                 * The host device does not support setting
1251                                 * the revision: let's operate it in legacy
1252                                 * mode.
1253                                 */
1254                                ret = 0;
1255                        else
1256                                vcdev->revision--;
1257                }
1258        } while (ret == -EOPNOTSUPP);
1259
1260        ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1261        ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1262        return ret;
1263}
1264
1265static int virtio_ccw_online(struct ccw_device *cdev)
1266{
1267        int ret;
1268        struct virtio_ccw_device *vcdev;
1269        unsigned long flags;
1270
1271        vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1272        if (!vcdev) {
1273                dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1274                ret = -ENOMEM;
1275                goto out_free;
1276        }
1277        vcdev->vdev.dev.parent = &cdev->dev;
1278        vcdev->cdev = cdev;
1279        vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1280                                                sizeof(*vcdev->dma_area));
1281        if (!vcdev->dma_area) {
1282                ret = -ENOMEM;
1283                goto out_free;
1284        }
1285
1286        vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1287
1288        vcdev->vdev.dev.release = virtio_ccw_release_dev;
1289        vcdev->vdev.config = &virtio_ccw_config_ops;
1290        init_waitqueue_head(&vcdev->wait_q);
1291        INIT_LIST_HEAD(&vcdev->virtqueues);
1292        spin_lock_init(&vcdev->lock);
1293        mutex_init(&vcdev->io_lock);
1294
1295        spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1296        dev_set_drvdata(&cdev->dev, vcdev);
1297        spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1298        vcdev->vdev.id.vendor = cdev->id.cu_type;
1299        vcdev->vdev.id.device = cdev->id.cu_model;
1300
1301        ret = virtio_ccw_set_transport_rev(vcdev);
1302        if (ret)
1303                goto out_free;
1304
1305        ret = register_virtio_device(&vcdev->vdev);
1306        if (ret) {
1307                dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1308                         ret);
1309                goto out_put;
1310        }
1311        return 0;
1312out_put:
1313        spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1314        dev_set_drvdata(&cdev->dev, NULL);
1315        spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1316        put_device(&vcdev->vdev.dev);
1317        return ret;
1318out_free:
1319        if (vcdev) {
1320                ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1321                                    sizeof(*vcdev->dma_area));
1322        }
1323        kfree(vcdev);
1324        return ret;
1325}
1326
1327static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1328{
1329        int rc;
1330        struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1331
1332        /*
1333         * Make sure vcdev is set
1334         * i.e. set_offline/remove callback not already running
1335         */
1336        if (!vcdev)
1337                return NOTIFY_DONE;
1338
1339        switch (event) {
1340        case CIO_GONE:
1341                vcdev->device_lost = true;
1342                rc = NOTIFY_DONE;
1343                break;
1344        case CIO_OPER:
1345                rc = NOTIFY_OK;
1346                break;
1347        default:
1348                rc = NOTIFY_DONE;
1349                break;
1350        }
1351        return rc;
1352}
1353
1354static struct ccw_device_id virtio_ids[] = {
1355        { CCW_DEVICE(0x3832, 0) },
1356        {},
1357};
1358
1359#ifdef CONFIG_PM_SLEEP
1360static int virtio_ccw_freeze(struct ccw_device *cdev)
1361{
1362        struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1363
1364        return virtio_device_freeze(&vcdev->vdev);
1365}
1366
1367static int virtio_ccw_restore(struct ccw_device *cdev)
1368{
1369        struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1370        int ret;
1371
1372        ret = virtio_ccw_set_transport_rev(vcdev);
1373        if (ret)
1374                return ret;
1375
1376        return virtio_device_restore(&vcdev->vdev);
1377}
1378#endif
1379
1380static struct ccw_driver virtio_ccw_driver = {
1381        .driver = {
1382                .owner = THIS_MODULE,
1383                .name = "virtio_ccw",
1384        },
1385        .ids = virtio_ids,
1386        .probe = virtio_ccw_probe,
1387        .remove = virtio_ccw_remove,
1388        .set_offline = virtio_ccw_offline,
1389        .set_online = virtio_ccw_online,
1390        .notify = virtio_ccw_cio_notify,
1391        .int_class = IRQIO_VIR,
1392#ifdef CONFIG_PM_SLEEP
1393        .freeze = virtio_ccw_freeze,
1394        .thaw = virtio_ccw_restore,
1395        .restore = virtio_ccw_restore,
1396#endif
1397};
1398
1399static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1400                           int max_digit, int max_val)
1401{
1402        int diff;
1403
1404        diff = 0;
1405        *val = 0;
1406
1407        while (diff <= max_digit) {
1408                int value = hex_to_bin(**cp);
1409
1410                if (value < 0)
1411                        break;
1412                *val = *val * 16 + value;
1413                (*cp)++;
1414                diff++;
1415        }
1416
1417        if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1418                return 1;
1419
1420        return 0;
1421}
1422
1423static int __init parse_busid(char *str, unsigned int *cssid,
1424                              unsigned int *ssid, unsigned int *devno)
1425{
1426        char *str_work;
1427        int rc, ret;
1428
1429        rc = 1;
1430
1431        if (*str == '\0')
1432                goto out;
1433
1434        str_work = str;
1435        ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1436        if (ret || (str_work[0] != '.'))
1437                goto out;
1438        str_work++;
1439        ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1440        if (ret || (str_work[0] != '.'))
1441                goto out;
1442        str_work++;
1443        ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1444        if (ret || (str_work[0] != '\0'))
1445                goto out;
1446
1447        rc = 0;
1448out:
1449        return rc;
1450}
1451
1452static void __init no_auto_parse(void)
1453{
1454        unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1455        char *parm, *str;
1456        int rc;
1457
1458        str = no_auto;
1459        while ((parm = strsep(&str, ","))) {
1460                rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1461                                 &from_ssid, &from);
1462                if (rc)
1463                        continue;
1464                if (parm != NULL) {
1465                        rc = parse_busid(parm, &to_cssid,
1466                                         &to_ssid, &to);
1467                        if ((from_ssid > to_ssid) ||
1468                            ((from_ssid == to_ssid) && (from > to)))
1469                                rc = -EINVAL;
1470                } else {
1471                        to_cssid = from_cssid;
1472                        to_ssid = from_ssid;
1473                        to = from;
1474                }
1475                if (rc)
1476                        continue;
1477                while ((from_ssid < to_ssid) ||
1478                       ((from_ssid == to_ssid) && (from <= to))) {
1479                        set_bit(from, devs_no_auto[from_ssid]);
1480                        from++;
1481                        if (from > __MAX_SUBCHANNEL) {
1482                                from_ssid++;
1483                                from = 0;
1484                        }
1485                }
1486        }
1487}
1488
1489static int __init virtio_ccw_init(void)
1490{
1491        int rc;
1492
1493        /* parse no_auto string before we do anything further */
1494        no_auto_parse();
1495
1496        summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1497        if (!summary_indicators)
1498                return -ENOMEM;
1499        rc = ccw_driver_register(&virtio_ccw_driver);
1500        if (rc)
1501                cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1502        return rc;
1503}
1504device_initcall(virtio_ccw_init);
1505