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