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