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