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