1 /* 2 * drivers/s390/cio/device.c 3 * bus driver for ccw devices 4 * 5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, 6 * IBM Corporation 7 * Author(s): Arnd Bergmann (arndb@de.ibm.com) 8 * Cornelia Huck (cornelia.huck@de.ibm.com) 9 * Martin Schwidefsky (schwidefsky@de.ibm.com) 10 */ 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/spinlock.h> 14 #include <linux/errno.h> 15 #include <linux/err.h> 16 #include <linux/slab.h> 17 #include <linux/list.h> 18 #include <linux/device.h> 19 #include <linux/workqueue.h> 20 21 #include <asm/ccwdev.h> 22 #include <asm/cio.h> 23 #include <asm/param.h> /* HZ */ 24 25 #include "cio.h" 26 #include "css.h" 27 #include "device.h" 28 #include "ioasm.h" 29 30 /******************* bus type handling ***********************/ 31 32 /* The Linux driver model distinguishes between a bus type and 33 * the bus itself. Of course we only have one channel 34 * subsystem driver and one channel system per machine, but 35 * we still use the abstraction. T.R. says it's a good idea. */ 36 static int 37 ccw_bus_match (struct device * dev, struct device_driver * drv) 38 { 39 struct ccw_device *cdev = to_ccwdev(dev); 40 struct ccw_driver *cdrv = to_ccwdrv(drv); 41 const struct ccw_device_id *ids = cdrv->ids, *found; 42 43 if (!ids) 44 return 0; 45 46 found = ccw_device_id_match(ids, &cdev->id); 47 if (!found) 48 return 0; 49 50 cdev->id.driver_info = found->driver_info; 51 52 return 1; 53 } 54 55 /* 56 * Hotplugging interface for ccw devices. 57 * Heavily modeled on pci and usb hotplug. 58 */ 59 static int 60 ccw_uevent (struct device *dev, char **envp, int num_envp, 61 char *buffer, int buffer_size) 62 { 63 struct ccw_device *cdev = to_ccwdev(dev); 64 int i = 0; 65 int length = 0; 66 67 if (!cdev) 68 return -ENODEV; 69 70 /* what we want to pass to /sbin/hotplug */ 71 72 envp[i++] = buffer; 73 length += scnprintf(buffer, buffer_size - length, "CU_TYPE=%04X", 74 cdev->id.cu_type); 75 if ((buffer_size - length <= 0) || (i >= num_envp)) 76 return -ENOMEM; 77 ++length; 78 buffer += length; 79 80 envp[i++] = buffer; 81 length += scnprintf(buffer, buffer_size - length, "CU_MODEL=%02X", 82 cdev->id.cu_model); 83 if ((buffer_size - length <= 0) || (i >= num_envp)) 84 return -ENOMEM; 85 ++length; 86 buffer += length; 87 88 /* The next two can be zero, that's ok for us */ 89 envp[i++] = buffer; 90 length += scnprintf(buffer, buffer_size - length, "DEV_TYPE=%04X", 91 cdev->id.dev_type); 92 if ((buffer_size - length <= 0) || (i >= num_envp)) 93 return -ENOMEM; 94 ++length; 95 buffer += length; 96 97 envp[i++] = buffer; 98 length += scnprintf(buffer, buffer_size - length, "DEV_MODEL=%02X", 99 cdev->id.dev_model); 100 if ((buffer_size - length <= 0) || (i >= num_envp)) 101 return -ENOMEM; 102 103 envp[i] = NULL; 104 105 return 0; 106 } 107 108 struct bus_type ccw_bus_type; 109 110 static int io_subchannel_probe (struct subchannel *); 111 static int io_subchannel_remove (struct subchannel *); 112 void io_subchannel_irq (struct device *); 113 static int io_subchannel_notify(struct device *, int); 114 static void io_subchannel_verify(struct device *); 115 static void io_subchannel_ioterm(struct device *); 116 static void io_subchannel_shutdown(struct subchannel *); 117 118 struct css_driver io_subchannel_driver = { 119 .subchannel_type = SUBCHANNEL_TYPE_IO, 120 .drv = { 121 .name = "io_subchannel", 122 .bus = &css_bus_type, 123 }, 124 .irq = io_subchannel_irq, 125 .notify = io_subchannel_notify, 126 .verify = io_subchannel_verify, 127 .termination = io_subchannel_ioterm, 128 .probe = io_subchannel_probe, 129 .remove = io_subchannel_remove, 130 .shutdown = io_subchannel_shutdown, 131 }; 132 133 struct workqueue_struct *ccw_device_work; 134 struct workqueue_struct *ccw_device_notify_work; 135 wait_queue_head_t ccw_device_init_wq; 136 atomic_t ccw_device_init_count; 137 138 static int __init 139 init_ccw_bus_type (void) 140 { 141 int ret; 142 143 init_waitqueue_head(&ccw_device_init_wq); 144 atomic_set(&ccw_device_init_count, 0); 145 146 ccw_device_work = create_singlethread_workqueue("cio"); 147 if (!ccw_device_work) 148 return -ENOMEM; /* FIXME: better errno ? */ 149 ccw_device_notify_work = create_singlethread_workqueue("cio_notify"); 150 if (!ccw_device_notify_work) { 151 ret = -ENOMEM; /* FIXME: better errno ? */ 152 goto out_err; 153 } 154 slow_path_wq = create_singlethread_workqueue("kslowcrw"); 155 if (!slow_path_wq) { 156 ret = -ENOMEM; /* FIXME: better errno ? */ 157 goto out_err; 158 } 159 if ((ret = bus_register (&ccw_bus_type))) 160 goto out_err; 161 162 if ((ret = driver_register(&io_subchannel_driver.drv))) 163 goto out_err; 164 165 wait_event(ccw_device_init_wq, 166 atomic_read(&ccw_device_init_count) == 0); 167 flush_workqueue(ccw_device_work); 168 return 0; 169 out_err: 170 if (ccw_device_work) 171 destroy_workqueue(ccw_device_work); 172 if (ccw_device_notify_work) 173 destroy_workqueue(ccw_device_notify_work); 174 if (slow_path_wq) 175 destroy_workqueue(slow_path_wq); 176 return ret; 177 } 178 179 static void __exit 180 cleanup_ccw_bus_type (void) 181 { 182 driver_unregister(&io_subchannel_driver.drv); 183 bus_unregister(&ccw_bus_type); 184 destroy_workqueue(ccw_device_notify_work); 185 destroy_workqueue(ccw_device_work); 186 } 187 188 subsys_initcall(init_ccw_bus_type); 189 module_exit(cleanup_ccw_bus_type); 190 191 /************************ device handling **************************/ 192 193 /* 194 * A ccw_device has some interfaces in sysfs in addition to the 195 * standard ones. 196 * The following entries are designed to export the information which 197 * resided in 2.4 in /proc/subchannels. Subchannel and device number 198 * are obvious, so they don't have an entry :) 199 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree? 200 */ 201 static ssize_t 202 chpids_show (struct device * dev, struct device_attribute *attr, char * buf) 203 { 204 struct subchannel *sch = to_subchannel(dev); 205 struct ssd_info *ssd = &sch->ssd_info; 206 ssize_t ret = 0; 207 int chp; 208 209 for (chp = 0; chp < 8; chp++) 210 ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]); 211 212 ret += sprintf (buf+ret, "\n"); 213 return min((ssize_t)PAGE_SIZE, ret); 214 } 215 216 static ssize_t 217 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf) 218 { 219 struct subchannel *sch = to_subchannel(dev); 220 struct pmcw *pmcw = &sch->schib.pmcw; 221 222 return sprintf (buf, "%02x %02x %02x\n", 223 pmcw->pim, pmcw->pam, pmcw->pom); 224 } 225 226 static ssize_t 227 devtype_show (struct device *dev, struct device_attribute *attr, char *buf) 228 { 229 struct ccw_device *cdev = to_ccwdev(dev); 230 struct ccw_device_id *id = &(cdev->id); 231 232 if (id->dev_type != 0) 233 return sprintf(buf, "%04x/%02x\n", 234 id->dev_type, id->dev_model); 235 else 236 return sprintf(buf, "n/a\n"); 237 } 238 239 static ssize_t 240 cutype_show (struct device *dev, struct device_attribute *attr, char *buf) 241 { 242 struct ccw_device *cdev = to_ccwdev(dev); 243 struct ccw_device_id *id = &(cdev->id); 244 245 return sprintf(buf, "%04x/%02x\n", 246 id->cu_type, id->cu_model); 247 } 248 249 static ssize_t 250 modalias_show (struct device *dev, struct device_attribute *attr, char *buf) 251 { 252 struct ccw_device *cdev = to_ccwdev(dev); 253 struct ccw_device_id *id = &(cdev->id); 254 int ret; 255 256 ret = sprintf(buf, "ccw:t%04Xm%02X", 257 id->cu_type, id->cu_model); 258 if (id->dev_type != 0) 259 ret += sprintf(buf + ret, "dt%04Xdm%02X\n", 260 id->dev_type, id->dev_model); 261 else 262 ret += sprintf(buf + ret, "dtdm\n"); 263 return ret; 264 } 265 266 static ssize_t 267 online_show (struct device *dev, struct device_attribute *attr, char *buf) 268 { 269 struct ccw_device *cdev = to_ccwdev(dev); 270 271 return sprintf(buf, cdev->online ? "1\n" : "0\n"); 272 } 273 274 static void 275 ccw_device_remove_disconnected(struct ccw_device *cdev) 276 { 277 struct subchannel *sch; 278 /* 279 * Forced offline in disconnected state means 280 * 'throw away device'. 281 */ 282 sch = to_subchannel(cdev->dev.parent); 283 css_sch_device_unregister(sch); 284 /* Reset intparm to zeroes. */ 285 sch->schib.pmcw.intparm = 0; 286 cio_modify(sch); 287 put_device(&sch->dev); 288 } 289 290 int 291 ccw_device_set_offline(struct ccw_device *cdev) 292 { 293 int ret; 294 295 if (!cdev) 296 return -ENODEV; 297 if (!cdev->online || !cdev->drv) 298 return -EINVAL; 299 300 if (cdev->drv->set_offline) { 301 ret = cdev->drv->set_offline(cdev); 302 if (ret != 0) 303 return ret; 304 } 305 cdev->online = 0; 306 spin_lock_irq(cdev->ccwlock); 307 ret = ccw_device_offline(cdev); 308 if (ret == -ENODEV) { 309 if (cdev->private->state != DEV_STATE_NOT_OPER) { 310 cdev->private->state = DEV_STATE_OFFLINE; 311 dev_fsm_event(cdev, DEV_EVENT_NOTOPER); 312 } 313 spin_unlock_irq(cdev->ccwlock); 314 return ret; 315 } 316 spin_unlock_irq(cdev->ccwlock); 317 if (ret == 0) 318 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 319 else { 320 pr_debug("ccw_device_offline returned %d, device %s\n", 321 ret, cdev->dev.bus_id); 322 cdev->online = 1; 323 } 324 return ret; 325 } 326 327 int 328 ccw_device_set_online(struct ccw_device *cdev) 329 { 330 int ret; 331 332 if (!cdev) 333 return -ENODEV; 334 if (cdev->online || !cdev->drv) 335 return -EINVAL; 336 337 spin_lock_irq(cdev->ccwlock); 338 ret = ccw_device_online(cdev); 339 spin_unlock_irq(cdev->ccwlock); 340 if (ret == 0) 341 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 342 else { 343 pr_debug("ccw_device_online returned %d, device %s\n", 344 ret, cdev->dev.bus_id); 345 return ret; 346 } 347 if (cdev->private->state != DEV_STATE_ONLINE) 348 return -ENODEV; 349 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) { 350 cdev->online = 1; 351 return 0; 352 } 353 spin_lock_irq(cdev->ccwlock); 354 ret = ccw_device_offline(cdev); 355 spin_unlock_irq(cdev->ccwlock); 356 if (ret == 0) 357 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 358 else 359 pr_debug("ccw_device_offline returned %d, device %s\n", 360 ret, cdev->dev.bus_id); 361 return (ret == 0) ? -ENODEV : ret; 362 } 363 364 static ssize_t 365 online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 366 { 367 struct ccw_device *cdev = to_ccwdev(dev); 368 int i, force, ret; 369 char *tmp; 370 371 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0) 372 return -EAGAIN; 373 374 if (cdev->drv && !try_module_get(cdev->drv->owner)) { 375 atomic_set(&cdev->private->onoff, 0); 376 return -EINVAL; 377 } 378 if (!strncmp(buf, "force\n", count)) { 379 force = 1; 380 i = 1; 381 } else { 382 force = 0; 383 i = simple_strtoul(buf, &tmp, 16); 384 } 385 if (i == 1) { 386 /* Do device recognition, if needed. */ 387 if (cdev->id.cu_type == 0) { 388 ret = ccw_device_recognition(cdev); 389 if (ret) { 390 printk(KERN_WARNING"Couldn't start recognition " 391 "for device %s (ret=%d)\n", 392 cdev->dev.bus_id, ret); 393 goto out; 394 } 395 wait_event(cdev->private->wait_q, 396 cdev->private->flags.recog_done); 397 } 398 if (cdev->drv && cdev->drv->set_online) 399 ccw_device_set_online(cdev); 400 } else if (i == 0) { 401 if (cdev->private->state == DEV_STATE_DISCONNECTED) 402 ccw_device_remove_disconnected(cdev); 403 else if (cdev->drv && cdev->drv->set_offline) 404 ccw_device_set_offline(cdev); 405 } 406 if (force && cdev->private->state == DEV_STATE_BOXED) { 407 ret = ccw_device_stlck(cdev); 408 if (ret) { 409 printk(KERN_WARNING"ccw_device_stlck for device %s " 410 "returned %d!\n", cdev->dev.bus_id, ret); 411 goto out; 412 } 413 /* Do device recognition, if needed. */ 414 if (cdev->id.cu_type == 0) { 415 cdev->private->state = DEV_STATE_NOT_OPER; 416 ret = ccw_device_recognition(cdev); 417 if (ret) { 418 printk(KERN_WARNING"Couldn't start recognition " 419 "for device %s (ret=%d)\n", 420 cdev->dev.bus_id, ret); 421 goto out; 422 } 423 wait_event(cdev->private->wait_q, 424 cdev->private->flags.recog_done); 425 } 426 if (cdev->drv && cdev->drv->set_online) 427 ccw_device_set_online(cdev); 428 } 429 out: 430 if (cdev->drv) 431 module_put(cdev->drv->owner); 432 atomic_set(&cdev->private->onoff, 0); 433 return count; 434 } 435 436 static ssize_t 437 available_show (struct device *dev, struct device_attribute *attr, char *buf) 438 { 439 struct ccw_device *cdev = to_ccwdev(dev); 440 struct subchannel *sch; 441 442 switch (cdev->private->state) { 443 case DEV_STATE_BOXED: 444 return sprintf(buf, "boxed\n"); 445 case DEV_STATE_DISCONNECTED: 446 case DEV_STATE_DISCONNECTED_SENSE_ID: 447 case DEV_STATE_NOT_OPER: 448 sch = to_subchannel(dev->parent); 449 if (!sch->lpm) 450 return sprintf(buf, "no path\n"); 451 else 452 return sprintf(buf, "no device\n"); 453 default: 454 /* All other states considered fine. */ 455 return sprintf(buf, "good\n"); 456 } 457 } 458 459 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); 460 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); 461 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); 462 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL); 463 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); 464 static DEVICE_ATTR(online, 0644, online_show, online_store); 465 extern struct device_attribute dev_attr_cmb_enable; 466 static DEVICE_ATTR(availability, 0444, available_show, NULL); 467 468 static struct attribute * subch_attrs[] = { 469 &dev_attr_chpids.attr, 470 &dev_attr_pimpampom.attr, 471 NULL, 472 }; 473 474 static struct attribute_group subch_attr_group = { 475 .attrs = subch_attrs, 476 }; 477 478 static inline int 479 subchannel_add_files (struct device *dev) 480 { 481 return sysfs_create_group(&dev->kobj, &subch_attr_group); 482 } 483 484 static struct attribute * ccwdev_attrs[] = { 485 &dev_attr_devtype.attr, 486 &dev_attr_cutype.attr, 487 &dev_attr_modalias.attr, 488 &dev_attr_online.attr, 489 &dev_attr_cmb_enable.attr, 490 &dev_attr_availability.attr, 491 NULL, 492 }; 493 494 static struct attribute_group ccwdev_attr_group = { 495 .attrs = ccwdev_attrs, 496 }; 497 498 static inline int 499 device_add_files (struct device *dev) 500 { 501 return sysfs_create_group(&dev->kobj, &ccwdev_attr_group); 502 } 503 504 static inline void 505 device_remove_files(struct device *dev) 506 { 507 sysfs_remove_group(&dev->kobj, &ccwdev_attr_group); 508 } 509 510 /* this is a simple abstraction for device_register that sets the 511 * correct bus type and adds the bus specific files */ 512 int 513 ccw_device_register(struct ccw_device *cdev) 514 { 515 struct device *dev = &cdev->dev; 516 int ret; 517 518 dev->bus = &ccw_bus_type; 519 520 if ((ret = device_add(dev))) 521 return ret; 522 523 set_bit(1, &cdev->private->registered); 524 if ((ret = device_add_files(dev))) { 525 if (test_and_clear_bit(1, &cdev->private->registered)) 526 device_del(dev); 527 } 528 return ret; 529 } 530 531 struct match_data { 532 unsigned int devno; 533 unsigned int ssid; 534 struct ccw_device * sibling; 535 }; 536 537 static int 538 match_devno(struct device * dev, void * data) 539 { 540 struct match_data * d = (struct match_data *)data; 541 struct ccw_device * cdev; 542 543 cdev = to_ccwdev(dev); 544 if ((cdev->private->state == DEV_STATE_DISCONNECTED) && 545 (cdev->private->devno == d->devno) && 546 (cdev->private->ssid == d->ssid) && 547 (cdev != d->sibling)) { 548 cdev->private->state = DEV_STATE_NOT_OPER; 549 return 1; 550 } 551 return 0; 552 } 553 554 static struct ccw_device * 555 get_disc_ccwdev_by_devno(unsigned int devno, unsigned int ssid, 556 struct ccw_device *sibling) 557 { 558 struct device *dev; 559 struct match_data data = { 560 .devno = devno, 561 .ssid = ssid, 562 .sibling = sibling, 563 }; 564 565 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno); 566 567 return dev ? to_ccwdev(dev) : NULL; 568 } 569 570 static void 571 ccw_device_add_changed(void *data) 572 { 573 574 struct ccw_device *cdev; 575 576 cdev = (struct ccw_device *)data; 577 if (device_add(&cdev->dev)) { 578 put_device(&cdev->dev); 579 return; 580 } 581 set_bit(1, &cdev->private->registered); 582 if (device_add_files(&cdev->dev)) { 583 if (test_and_clear_bit(1, &cdev->private->registered)) 584 device_unregister(&cdev->dev); 585 } 586 } 587 588 extern int css_get_ssd_info(struct subchannel *sch); 589 590 void 591 ccw_device_do_unreg_rereg(void *data) 592 { 593 struct ccw_device *cdev; 594 struct subchannel *sch; 595 int need_rename; 596 597 cdev = (struct ccw_device *)data; 598 sch = to_subchannel(cdev->dev.parent); 599 if (cdev->private->devno != sch->schib.pmcw.dev) { 600 /* 601 * The device number has changed. This is usually only when 602 * a device has been detached under VM and then re-appeared 603 * on another subchannel because of a different attachment 604 * order than before. Ideally, we should should just switch 605 * subchannels, but unfortunately, this is not possible with 606 * the current implementation. 607 * Instead, we search for the old subchannel for this device 608 * number and deregister so there are no collisions with the 609 * newly registered ccw_device. 610 * FIXME: Find another solution so the block layer doesn't 611 * get possibly sick... 612 */ 613 struct ccw_device *other_cdev; 614 615 need_rename = 1; 616 other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev, 617 sch->schid.ssid, cdev); 618 if (other_cdev) { 619 struct subchannel *other_sch; 620 621 other_sch = to_subchannel(other_cdev->dev.parent); 622 if (get_device(&other_sch->dev)) { 623 stsch(other_sch->schid, &other_sch->schib); 624 if (other_sch->schib.pmcw.dnv) { 625 other_sch->schib.pmcw.intparm = 0; 626 cio_modify(other_sch); 627 } 628 css_sch_device_unregister(other_sch); 629 } 630 } 631 /* Update ssd info here. */ 632 css_get_ssd_info(sch); 633 cdev->private->devno = sch->schib.pmcw.dev; 634 } else 635 need_rename = 0; 636 device_remove_files(&cdev->dev); 637 if (test_and_clear_bit(1, &cdev->private->registered)) 638 device_del(&cdev->dev); 639 if (need_rename) 640 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", 641 sch->schid.ssid, sch->schib.pmcw.dev); 642 PREPARE_WORK(&cdev->private->kick_work, 643 ccw_device_add_changed, (void *)cdev); 644 queue_work(ccw_device_work, &cdev->private->kick_work); 645 } 646 647 static void 648 ccw_device_release(struct device *dev) 649 { 650 struct ccw_device *cdev; 651 652 cdev = to_ccwdev(dev); 653 kfree(cdev->private); 654 kfree(cdev); 655 } 656 657 /* 658 * Register recognized device. 659 */ 660 static void 661 io_subchannel_register(void *data) 662 { 663 struct ccw_device *cdev; 664 struct subchannel *sch; 665 int ret; 666 unsigned long flags; 667 668 cdev = (struct ccw_device *) data; 669 sch = to_subchannel(cdev->dev.parent); 670 671 if (klist_node_attached(&cdev->dev.knode_parent)) { 672 bus_rescan_devices(&ccw_bus_type); 673 goto out; 674 } 675 /* make it known to the system */ 676 ret = ccw_device_register(cdev); 677 if (ret) { 678 printk (KERN_WARNING "%s: could not register %s\n", 679 __func__, cdev->dev.bus_id); 680 put_device(&cdev->dev); 681 spin_lock_irqsave(&sch->lock, flags); 682 sch->dev.driver_data = NULL; 683 spin_unlock_irqrestore(&sch->lock, flags); 684 kfree (cdev->private); 685 kfree (cdev); 686 put_device(&sch->dev); 687 if (atomic_dec_and_test(&ccw_device_init_count)) 688 wake_up(&ccw_device_init_wq); 689 return; 690 } 691 692 ret = subchannel_add_files(cdev->dev.parent); 693 if (ret) 694 printk(KERN_WARNING "%s: could not add attributes to %s\n", 695 __func__, sch->dev.bus_id); 696 put_device(&cdev->dev); 697 out: 698 cdev->private->flags.recog_done = 1; 699 put_device(&sch->dev); 700 wake_up(&cdev->private->wait_q); 701 if (atomic_dec_and_test(&ccw_device_init_count)) 702 wake_up(&ccw_device_init_wq); 703 } 704 705 void 706 ccw_device_call_sch_unregister(void *data) 707 { 708 struct ccw_device *cdev = data; 709 struct subchannel *sch; 710 711 sch = to_subchannel(cdev->dev.parent); 712 css_sch_device_unregister(sch); 713 /* Reset intparm to zeroes. */ 714 sch->schib.pmcw.intparm = 0; 715 cio_modify(sch); 716 put_device(&cdev->dev); 717 put_device(&sch->dev); 718 } 719 720 /* 721 * subchannel recognition done. Called from the state machine. 722 */ 723 void 724 io_subchannel_recog_done(struct ccw_device *cdev) 725 { 726 struct subchannel *sch; 727 728 if (css_init_done == 0) { 729 cdev->private->flags.recog_done = 1; 730 return; 731 } 732 switch (cdev->private->state) { 733 case DEV_STATE_NOT_OPER: 734 cdev->private->flags.recog_done = 1; 735 /* Remove device found not operational. */ 736 if (!get_device(&cdev->dev)) 737 break; 738 sch = to_subchannel(cdev->dev.parent); 739 PREPARE_WORK(&cdev->private->kick_work, 740 ccw_device_call_sch_unregister, (void *) cdev); 741 queue_work(slow_path_wq, &cdev->private->kick_work); 742 if (atomic_dec_and_test(&ccw_device_init_count)) 743 wake_up(&ccw_device_init_wq); 744 break; 745 case DEV_STATE_BOXED: 746 /* Device did not respond in time. */ 747 case DEV_STATE_OFFLINE: 748 /* 749 * We can't register the device in interrupt context so 750 * we schedule a work item. 751 */ 752 if (!get_device(&cdev->dev)) 753 break; 754 PREPARE_WORK(&cdev->private->kick_work, 755 io_subchannel_register, (void *) cdev); 756 queue_work(slow_path_wq, &cdev->private->kick_work); 757 break; 758 } 759 } 760 761 static int 762 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch) 763 { 764 int rc; 765 struct ccw_device_private *priv; 766 767 sch->dev.driver_data = cdev; 768 sch->driver = &io_subchannel_driver; 769 cdev->ccwlock = &sch->lock; 770 771 /* Init private data. */ 772 priv = cdev->private; 773 priv->devno = sch->schib.pmcw.dev; 774 priv->ssid = sch->schid.ssid; 775 priv->sch_no = sch->schid.sch_no; 776 priv->state = DEV_STATE_NOT_OPER; 777 INIT_LIST_HEAD(&priv->cmb_list); 778 init_waitqueue_head(&priv->wait_q); 779 init_timer(&priv->timer); 780 781 /* Set an initial name for the device. */ 782 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", 783 sch->schid.ssid, sch->schib.pmcw.dev); 784 785 /* Increase counter of devices currently in recognition. */ 786 atomic_inc(&ccw_device_init_count); 787 788 /* Start async. device sensing. */ 789 spin_lock_irq(&sch->lock); 790 rc = ccw_device_recognition(cdev); 791 spin_unlock_irq(&sch->lock); 792 if (rc) { 793 if (atomic_dec_and_test(&ccw_device_init_count)) 794 wake_up(&ccw_device_init_wq); 795 } 796 return rc; 797 } 798 799 static int 800 io_subchannel_probe (struct subchannel *sch) 801 { 802 struct ccw_device *cdev; 803 int rc; 804 unsigned long flags; 805 806 if (sch->dev.driver_data) { 807 /* 808 * This subchannel already has an associated ccw_device. 809 * Register it and exit. This happens for all early 810 * device, e.g. the console. 811 */ 812 cdev = sch->dev.driver_data; 813 device_initialize(&cdev->dev); 814 ccw_device_register(cdev); 815 subchannel_add_files(&sch->dev); 816 /* 817 * Check if the device is already online. If it is 818 * the reference count needs to be corrected 819 * (see ccw_device_online and css_init_done for the 820 * ugly details). 821 */ 822 if (cdev->private->state != DEV_STATE_NOT_OPER && 823 cdev->private->state != DEV_STATE_OFFLINE && 824 cdev->private->state != DEV_STATE_BOXED) 825 get_device(&cdev->dev); 826 return 0; 827 } 828 cdev = kzalloc (sizeof(*cdev), GFP_KERNEL); 829 if (!cdev) 830 return -ENOMEM; 831 cdev->private = kzalloc(sizeof(struct ccw_device_private), 832 GFP_KERNEL | GFP_DMA); 833 if (!cdev->private) { 834 kfree(cdev); 835 return -ENOMEM; 836 } 837 atomic_set(&cdev->private->onoff, 0); 838 cdev->dev = (struct device) { 839 .parent = &sch->dev, 840 .release = ccw_device_release, 841 }; 842 INIT_LIST_HEAD(&cdev->private->kick_work.entry); 843 /* Do first half of device_register. */ 844 device_initialize(&cdev->dev); 845 846 if (!get_device(&sch->dev)) { 847 if (cdev->dev.release) 848 cdev->dev.release(&cdev->dev); 849 return -ENODEV; 850 } 851 852 rc = io_subchannel_recog(cdev, sch); 853 if (rc) { 854 spin_lock_irqsave(&sch->lock, flags); 855 sch->dev.driver_data = NULL; 856 spin_unlock_irqrestore(&sch->lock, flags); 857 if (cdev->dev.release) 858 cdev->dev.release(&cdev->dev); 859 } 860 861 return rc; 862 } 863 864 static void 865 ccw_device_unregister(void *data) 866 { 867 struct ccw_device *cdev; 868 869 cdev = (struct ccw_device *)data; 870 if (test_and_clear_bit(1, &cdev->private->registered)) 871 device_unregister(&cdev->dev); 872 put_device(&cdev->dev); 873 } 874 875 static int 876 io_subchannel_remove (struct subchannel *sch) 877 { 878 struct ccw_device *cdev; 879 unsigned long flags; 880 881 if (!sch->dev.driver_data) 882 return 0; 883 cdev = sch->dev.driver_data; 884 /* Set ccw device to not operational and drop reference. */ 885 spin_lock_irqsave(cdev->ccwlock, flags); 886 sch->dev.driver_data = NULL; 887 cdev->private->state = DEV_STATE_NOT_OPER; 888 spin_unlock_irqrestore(cdev->ccwlock, flags); 889 /* 890 * Put unregistration on workqueue to avoid livelocks on the css bus 891 * semaphore. 892 */ 893 if (get_device(&cdev->dev)) { 894 PREPARE_WORK(&cdev->private->kick_work, 895 ccw_device_unregister, (void *) cdev); 896 queue_work(ccw_device_work, &cdev->private->kick_work); 897 } 898 return 0; 899 } 900 901 static int 902 io_subchannel_notify(struct device *dev, int event) 903 { 904 struct ccw_device *cdev; 905 906 cdev = dev->driver_data; 907 if (!cdev) 908 return 0; 909 if (!cdev->drv) 910 return 0; 911 if (!cdev->online) 912 return 0; 913 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0; 914 } 915 916 static void 917 io_subchannel_verify(struct device *dev) 918 { 919 struct ccw_device *cdev; 920 921 cdev = dev->driver_data; 922 if (cdev) 923 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 924 } 925 926 static void 927 io_subchannel_ioterm(struct device *dev) 928 { 929 struct ccw_device *cdev; 930 931 cdev = dev->driver_data; 932 if (!cdev) 933 return; 934 cdev->private->state = DEV_STATE_CLEAR_VERIFY; 935 if (cdev->handler) 936 cdev->handler(cdev, cdev->private->intparm, 937 ERR_PTR(-EIO)); 938 } 939 940 static void 941 io_subchannel_shutdown(struct subchannel *sch) 942 { 943 struct ccw_device *cdev; 944 int ret; 945 946 cdev = sch->dev.driver_data; 947 948 if (cio_is_console(sch->schid)) 949 return; 950 if (!sch->schib.pmcw.ena) 951 /* Nothing to do. */ 952 return; 953 ret = cio_disable_subchannel(sch); 954 if (ret != -EBUSY) 955 /* Subchannel is disabled, we're done. */ 956 return; 957 cdev->private->state = DEV_STATE_QUIESCE; 958 if (cdev->handler) 959 cdev->handler(cdev, cdev->private->intparm, 960 ERR_PTR(-EIO)); 961 ret = ccw_device_cancel_halt_clear(cdev); 962 if (ret == -EBUSY) { 963 ccw_device_set_timeout(cdev, HZ/10); 964 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 965 } 966 cio_disable_subchannel(sch); 967 } 968 969 #ifdef CONFIG_CCW_CONSOLE 970 static struct ccw_device console_cdev; 971 static struct ccw_device_private console_private; 972 static int console_cdev_in_use; 973 974 static int 975 ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch) 976 { 977 int rc; 978 979 /* Initialize the ccw_device structure. */ 980 cdev->dev = (struct device) { 981 .parent = &sch->dev, 982 }; 983 rc = io_subchannel_recog(cdev, sch); 984 if (rc) 985 return rc; 986 987 /* Now wait for the async. recognition to come to an end. */ 988 spin_lock_irq(cdev->ccwlock); 989 while (!dev_fsm_final_state(cdev)) 990 wait_cons_dev(); 991 rc = -EIO; 992 if (cdev->private->state != DEV_STATE_OFFLINE) 993 goto out_unlock; 994 ccw_device_online(cdev); 995 while (!dev_fsm_final_state(cdev)) 996 wait_cons_dev(); 997 if (cdev->private->state != DEV_STATE_ONLINE) 998 goto out_unlock; 999 rc = 0; 1000 out_unlock: 1001 spin_unlock_irq(cdev->ccwlock); 1002 return 0; 1003 } 1004 1005 struct ccw_device * 1006 ccw_device_probe_console(void) 1007 { 1008 struct subchannel *sch; 1009 int ret; 1010 1011 if (xchg(&console_cdev_in_use, 1) != 0) 1012 return ERR_PTR(-EBUSY); 1013 sch = cio_probe_console(); 1014 if (IS_ERR(sch)) { 1015 console_cdev_in_use = 0; 1016 return (void *) sch; 1017 } 1018 memset(&console_cdev, 0, sizeof(struct ccw_device)); 1019 memset(&console_private, 0, sizeof(struct ccw_device_private)); 1020 console_cdev.private = &console_private; 1021 ret = ccw_device_console_enable(&console_cdev, sch); 1022 if (ret) { 1023 cio_release_console(); 1024 console_cdev_in_use = 0; 1025 return ERR_PTR(ret); 1026 } 1027 console_cdev.online = 1; 1028 return &console_cdev; 1029 } 1030 #endif 1031 1032 /* 1033 * get ccw_device matching the busid, but only if owned by cdrv 1034 */ 1035 static int 1036 __ccwdev_check_busid(struct device *dev, void *id) 1037 { 1038 char *bus_id; 1039 1040 bus_id = (char *)id; 1041 1042 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0); 1043 } 1044 1045 1046 struct ccw_device * 1047 get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id) 1048 { 1049 struct device *dev; 1050 struct device_driver *drv; 1051 1052 drv = get_driver(&cdrv->driver); 1053 if (!drv) 1054 return NULL; 1055 1056 dev = driver_find_device(drv, NULL, (void *)bus_id, 1057 __ccwdev_check_busid); 1058 put_driver(drv); 1059 1060 return dev ? to_ccwdev(dev) : NULL; 1061 } 1062 1063 /************************** device driver handling ************************/ 1064 1065 /* This is the implementation of the ccw_driver class. The probe, remove 1066 * and release methods are initially very similar to the device_driver 1067 * implementations, with the difference that they have ccw_device 1068 * arguments. 1069 * 1070 * A ccw driver also contains the information that is needed for 1071 * device matching. 1072 */ 1073 static int 1074 ccw_device_probe (struct device *dev) 1075 { 1076 struct ccw_device *cdev = to_ccwdev(dev); 1077 struct ccw_driver *cdrv = to_ccwdrv(dev->driver); 1078 int ret; 1079 1080 cdev->drv = cdrv; /* to let the driver call _set_online */ 1081 1082 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV; 1083 1084 if (ret) { 1085 cdev->drv = NULL; 1086 return ret; 1087 } 1088 1089 return 0; 1090 } 1091 1092 static int 1093 ccw_device_remove (struct device *dev) 1094 { 1095 struct ccw_device *cdev = to_ccwdev(dev); 1096 struct ccw_driver *cdrv = cdev->drv; 1097 int ret; 1098 1099 pr_debug("removing device %s\n", cdev->dev.bus_id); 1100 if (cdrv->remove) 1101 cdrv->remove(cdev); 1102 if (cdev->online) { 1103 cdev->online = 0; 1104 spin_lock_irq(cdev->ccwlock); 1105 ret = ccw_device_offline(cdev); 1106 spin_unlock_irq(cdev->ccwlock); 1107 if (ret == 0) 1108 wait_event(cdev->private->wait_q, 1109 dev_fsm_final_state(cdev)); 1110 else 1111 //FIXME: we can't fail! 1112 pr_debug("ccw_device_offline returned %d, device %s\n", 1113 ret, cdev->dev.bus_id); 1114 } 1115 ccw_device_set_timeout(cdev, 0); 1116 cdev->drv = NULL; 1117 return 0; 1118 } 1119 1120 struct bus_type ccw_bus_type = { 1121 .name = "ccw", 1122 .match = ccw_bus_match, 1123 .uevent = ccw_uevent, 1124 .probe = ccw_device_probe, 1125 .remove = ccw_device_remove, 1126 }; 1127 1128 int 1129 ccw_driver_register (struct ccw_driver *cdriver) 1130 { 1131 struct device_driver *drv = &cdriver->driver; 1132 1133 drv->bus = &ccw_bus_type; 1134 drv->name = cdriver->name; 1135 1136 return driver_register(drv); 1137 } 1138 1139 void 1140 ccw_driver_unregister (struct ccw_driver *cdriver) 1141 { 1142 driver_unregister(&cdriver->driver); 1143 } 1144 1145 /* Helper func for qdio. */ 1146 struct subchannel_id 1147 ccw_device_get_subchannel_id(struct ccw_device *cdev) 1148 { 1149 struct subchannel *sch; 1150 1151 sch = to_subchannel(cdev->dev.parent); 1152 return sch->schid; 1153 } 1154 1155 MODULE_LICENSE("GPL"); 1156 EXPORT_SYMBOL(ccw_device_set_online); 1157 EXPORT_SYMBOL(ccw_device_set_offline); 1158 EXPORT_SYMBOL(ccw_driver_register); 1159 EXPORT_SYMBOL(ccw_driver_unregister); 1160 EXPORT_SYMBOL(get_ccwdev_by_busid); 1161 EXPORT_SYMBOL(ccw_bus_type); 1162 EXPORT_SYMBOL(ccw_device_work); 1163 EXPORT_SYMBOL(ccw_device_notify_work); 1164 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id); 1165