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