1 /* 2 * drivers/s390/cio/device.c 3 * bus driver for ccw devices 4 * 5 * Copyright IBM Corp. 2002,2008 6 * Author(s): Arnd Bergmann (arndb@de.ibm.com) 7 * Cornelia Huck (cornelia.huck@de.ibm.com) 8 * Martin Schwidefsky (schwidefsky@de.ibm.com) 9 */ 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/spinlock.h> 13 #include <linux/errno.h> 14 #include <linux/err.h> 15 #include <linux/slab.h> 16 #include <linux/list.h> 17 #include <linux/device.h> 18 #include <linux/workqueue.h> 19 #include <linux/timer.h> 20 21 #include <asm/ccwdev.h> 22 #include <asm/cio.h> 23 #include <asm/param.h> /* HZ */ 24 #include <asm/cmb.h> 25 #include <asm/isc.h> 26 27 #include "chp.h" 28 #include "cio.h" 29 #include "cio_debug.h" 30 #include "css.h" 31 #include "device.h" 32 #include "ioasm.h" 33 #include "io_sch.h" 34 #include "blacklist.h" 35 36 static struct timer_list recovery_timer; 37 static DEFINE_SPINLOCK(recovery_lock); 38 static int recovery_phase; 39 static const unsigned long recovery_delay[] = { 3, 30, 300 }; 40 41 /******************* bus type handling ***********************/ 42 43 /* The Linux driver model distinguishes between a bus type and 44 * the bus itself. Of course we only have one channel 45 * subsystem driver and one channel system per machine, but 46 * we still use the abstraction. T.R. says it's a good idea. */ 47 static int 48 ccw_bus_match (struct device * dev, struct device_driver * drv) 49 { 50 struct ccw_device *cdev = to_ccwdev(dev); 51 struct ccw_driver *cdrv = to_ccwdrv(drv); 52 const struct ccw_device_id *ids = cdrv->ids, *found; 53 54 if (!ids) 55 return 0; 56 57 found = ccw_device_id_match(ids, &cdev->id); 58 if (!found) 59 return 0; 60 61 cdev->id.driver_info = found->driver_info; 62 63 return 1; 64 } 65 66 /* Store modalias string delimited by prefix/suffix string into buffer with 67 * specified size. Return length of resulting string (excluding trailing '\0') 68 * even if string doesn't fit buffer (snprintf semantics). */ 69 static int snprint_alias(char *buf, size_t size, 70 struct ccw_device_id *id, const char *suffix) 71 { 72 int len; 73 74 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model); 75 if (len > size) 76 return len; 77 buf += len; 78 size -= len; 79 80 if (id->dev_type != 0) 81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type, 82 id->dev_model, suffix); 83 else 84 len += snprintf(buf, size, "dtdm%s", suffix); 85 86 return len; 87 } 88 89 /* Set up environment variables for ccw device uevent. Return 0 on success, 90 * non-zero otherwise. */ 91 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env) 92 { 93 struct ccw_device *cdev = to_ccwdev(dev); 94 struct ccw_device_id *id = &(cdev->id); 95 int ret; 96 char modalias_buf[30]; 97 98 /* CU_TYPE= */ 99 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type); 100 if (ret) 101 return ret; 102 103 /* CU_MODEL= */ 104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model); 105 if (ret) 106 return ret; 107 108 /* The next two can be zero, that's ok for us */ 109 /* DEV_TYPE= */ 110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type); 111 if (ret) 112 return ret; 113 114 /* DEV_MODEL= */ 115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model); 116 if (ret) 117 return ret; 118 119 /* MODALIAS= */ 120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, ""); 121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf); 122 return ret; 123 } 124 125 struct bus_type ccw_bus_type; 126 127 static void io_subchannel_irq(struct subchannel *); 128 static int io_subchannel_probe(struct subchannel *); 129 static int io_subchannel_remove(struct subchannel *); 130 static void io_subchannel_shutdown(struct subchannel *); 131 static int io_subchannel_sch_event(struct subchannel *, int); 132 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *, 133 int); 134 135 static struct css_device_id io_subchannel_ids[] = { 136 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, }, 137 { /* end of list */ }, 138 }; 139 MODULE_DEVICE_TABLE(css, io_subchannel_ids); 140 141 static struct css_driver io_subchannel_driver = { 142 .owner = THIS_MODULE, 143 .subchannel_type = io_subchannel_ids, 144 .name = "io_subchannel", 145 .irq = io_subchannel_irq, 146 .sch_event = io_subchannel_sch_event, 147 .chp_event = io_subchannel_chp_event, 148 .probe = io_subchannel_probe, 149 .remove = io_subchannel_remove, 150 .shutdown = io_subchannel_shutdown, 151 }; 152 153 struct workqueue_struct *ccw_device_work; 154 wait_queue_head_t ccw_device_init_wq; 155 atomic_t ccw_device_init_count; 156 157 static void recovery_func(unsigned long data); 158 159 static int __init 160 init_ccw_bus_type (void) 161 { 162 int ret; 163 164 init_waitqueue_head(&ccw_device_init_wq); 165 atomic_set(&ccw_device_init_count, 0); 166 setup_timer(&recovery_timer, recovery_func, 0); 167 168 ccw_device_work = create_singlethread_workqueue("cio"); 169 if (!ccw_device_work) 170 return -ENOMEM; /* FIXME: better errno ? */ 171 slow_path_wq = create_singlethread_workqueue("kslowcrw"); 172 if (!slow_path_wq) { 173 ret = -ENOMEM; /* FIXME: better errno ? */ 174 goto out_err; 175 } 176 if ((ret = bus_register (&ccw_bus_type))) 177 goto out_err; 178 179 ret = css_driver_register(&io_subchannel_driver); 180 if (ret) 181 goto out_err; 182 183 wait_event(ccw_device_init_wq, 184 atomic_read(&ccw_device_init_count) == 0); 185 flush_workqueue(ccw_device_work); 186 return 0; 187 out_err: 188 if (ccw_device_work) 189 destroy_workqueue(ccw_device_work); 190 if (slow_path_wq) 191 destroy_workqueue(slow_path_wq); 192 return ret; 193 } 194 195 static void __exit 196 cleanup_ccw_bus_type (void) 197 { 198 css_driver_unregister(&io_subchannel_driver); 199 bus_unregister(&ccw_bus_type); 200 destroy_workqueue(ccw_device_work); 201 } 202 203 subsys_initcall(init_ccw_bus_type); 204 module_exit(cleanup_ccw_bus_type); 205 206 /************************ device handling **************************/ 207 208 /* 209 * A ccw_device has some interfaces in sysfs in addition to the 210 * standard ones. 211 * The following entries are designed to export the information which 212 * resided in 2.4 in /proc/subchannels. Subchannel and device number 213 * are obvious, so they don't have an entry :) 214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree? 215 */ 216 static ssize_t 217 chpids_show (struct device * dev, struct device_attribute *attr, char * buf) 218 { 219 struct subchannel *sch = to_subchannel(dev); 220 struct chsc_ssd_info *ssd = &sch->ssd_info; 221 ssize_t ret = 0; 222 int chp; 223 int mask; 224 225 for (chp = 0; chp < 8; chp++) { 226 mask = 0x80 >> chp; 227 if (ssd->path_mask & mask) 228 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id); 229 else 230 ret += sprintf(buf + ret, "00 "); 231 } 232 ret += sprintf (buf+ret, "\n"); 233 return min((ssize_t)PAGE_SIZE, ret); 234 } 235 236 static ssize_t 237 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf) 238 { 239 struct subchannel *sch = to_subchannel(dev); 240 struct pmcw *pmcw = &sch->schib.pmcw; 241 242 return sprintf (buf, "%02x %02x %02x\n", 243 pmcw->pim, pmcw->pam, pmcw->pom); 244 } 245 246 static ssize_t 247 devtype_show (struct device *dev, struct device_attribute *attr, char *buf) 248 { 249 struct ccw_device *cdev = to_ccwdev(dev); 250 struct ccw_device_id *id = &(cdev->id); 251 252 if (id->dev_type != 0) 253 return sprintf(buf, "%04x/%02x\n", 254 id->dev_type, id->dev_model); 255 else 256 return sprintf(buf, "n/a\n"); 257 } 258 259 static ssize_t 260 cutype_show (struct device *dev, struct device_attribute *attr, char *buf) 261 { 262 struct ccw_device *cdev = to_ccwdev(dev); 263 struct ccw_device_id *id = &(cdev->id); 264 265 return sprintf(buf, "%04x/%02x\n", 266 id->cu_type, id->cu_model); 267 } 268 269 static ssize_t 270 modalias_show (struct device *dev, struct device_attribute *attr, char *buf) 271 { 272 struct ccw_device *cdev = to_ccwdev(dev); 273 struct ccw_device_id *id = &(cdev->id); 274 int len; 275 276 len = snprint_alias(buf, PAGE_SIZE, id, "\n"); 277 278 return len > PAGE_SIZE ? PAGE_SIZE : len; 279 } 280 281 static ssize_t 282 online_show (struct device *dev, struct device_attribute *attr, char *buf) 283 { 284 struct ccw_device *cdev = to_ccwdev(dev); 285 286 return sprintf(buf, cdev->online ? "1\n" : "0\n"); 287 } 288 289 int ccw_device_is_orphan(struct ccw_device *cdev) 290 { 291 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent)); 292 } 293 294 static void ccw_device_unregister(struct ccw_device *cdev) 295 { 296 if (test_and_clear_bit(1, &cdev->private->registered)) 297 device_del(&cdev->dev); 298 } 299 300 static void ccw_device_remove_orphan_cb(struct work_struct *work) 301 { 302 struct ccw_device_private *priv; 303 struct ccw_device *cdev; 304 305 priv = container_of(work, struct ccw_device_private, kick_work); 306 cdev = priv->cdev; 307 ccw_device_unregister(cdev); 308 put_device(&cdev->dev); 309 /* Release cdev reference for workqueue processing. */ 310 put_device(&cdev->dev); 311 } 312 313 static void 314 ccw_device_remove_disconnected(struct ccw_device *cdev) 315 { 316 unsigned long flags; 317 318 /* 319 * Forced offline in disconnected state means 320 * 'throw away device'. 321 */ 322 /* Get cdev reference for workqueue processing. */ 323 if (!get_device(&cdev->dev)) 324 return; 325 if (ccw_device_is_orphan(cdev)) { 326 /* 327 * Deregister ccw device. 328 * Unfortunately, we cannot do this directly from the 329 * attribute method. 330 */ 331 spin_lock_irqsave(cdev->ccwlock, flags); 332 cdev->private->state = DEV_STATE_NOT_OPER; 333 spin_unlock_irqrestore(cdev->ccwlock, flags); 334 PREPARE_WORK(&cdev->private->kick_work, 335 ccw_device_remove_orphan_cb); 336 queue_work(slow_path_wq, &cdev->private->kick_work); 337 } else 338 /* Deregister subchannel, which will kill the ccw device. */ 339 ccw_device_schedule_sch_unregister(cdev); 340 } 341 342 /** 343 * ccw_device_set_offline() - disable a ccw device for I/O 344 * @cdev: target ccw device 345 * 346 * This function calls the driver's set_offline() function for @cdev, if 347 * given, and then disables @cdev. 348 * Returns: 349 * %0 on success and a negative error value on failure. 350 * Context: 351 * enabled, ccw device lock not held 352 */ 353 int ccw_device_set_offline(struct ccw_device *cdev) 354 { 355 int ret; 356 357 if (!cdev) 358 return -ENODEV; 359 if (!cdev->online || !cdev->drv) 360 return -EINVAL; 361 362 if (cdev->drv->set_offline) { 363 ret = cdev->drv->set_offline(cdev); 364 if (ret != 0) 365 return ret; 366 } 367 cdev->online = 0; 368 spin_lock_irq(cdev->ccwlock); 369 ret = ccw_device_offline(cdev); 370 if (ret == -ENODEV) { 371 if (cdev->private->state != DEV_STATE_NOT_OPER) { 372 cdev->private->state = DEV_STATE_OFFLINE; 373 dev_fsm_event(cdev, DEV_EVENT_NOTOPER); 374 } 375 spin_unlock_irq(cdev->ccwlock); 376 /* Give up reference from ccw_device_set_online(). */ 377 put_device(&cdev->dev); 378 return ret; 379 } 380 spin_unlock_irq(cdev->ccwlock); 381 if (ret == 0) { 382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 383 /* Give up reference from ccw_device_set_online(). */ 384 put_device(&cdev->dev); 385 } else { 386 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " 387 "device 0.%x.%04x\n", 388 ret, cdev->private->dev_id.ssid, 389 cdev->private->dev_id.devno); 390 cdev->online = 1; 391 } 392 return ret; 393 } 394 395 /** 396 * ccw_device_set_online() - enable a ccw device for I/O 397 * @cdev: target ccw device 398 * 399 * This function first enables @cdev and then calls the driver's set_online() 400 * function for @cdev, if given. If set_online() returns an error, @cdev is 401 * disabled again. 402 * Returns: 403 * %0 on success and a negative error value on failure. 404 * Context: 405 * enabled, ccw device lock not held 406 */ 407 int ccw_device_set_online(struct ccw_device *cdev) 408 { 409 int ret; 410 411 if (!cdev) 412 return -ENODEV; 413 if (cdev->online || !cdev->drv) 414 return -EINVAL; 415 /* Hold on to an extra reference while device is online. */ 416 if (!get_device(&cdev->dev)) 417 return -ENODEV; 418 419 spin_lock_irq(cdev->ccwlock); 420 ret = ccw_device_online(cdev); 421 spin_unlock_irq(cdev->ccwlock); 422 if (ret == 0) 423 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 424 else { 425 CIO_MSG_EVENT(0, "ccw_device_online returned %d, " 426 "device 0.%x.%04x\n", 427 ret, cdev->private->dev_id.ssid, 428 cdev->private->dev_id.devno); 429 /* Give up online reference since onlining failed. */ 430 put_device(&cdev->dev); 431 return ret; 432 } 433 if (cdev->private->state != DEV_STATE_ONLINE) { 434 /* Give up online reference since onlining failed. */ 435 put_device(&cdev->dev); 436 return -ENODEV; 437 } 438 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) { 439 cdev->online = 1; 440 return 0; 441 } 442 spin_lock_irq(cdev->ccwlock); 443 ret = ccw_device_offline(cdev); 444 spin_unlock_irq(cdev->ccwlock); 445 if (ret == 0) 446 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 447 else 448 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " 449 "device 0.%x.%04x\n", 450 ret, cdev->private->dev_id.ssid, 451 cdev->private->dev_id.devno); 452 /* Give up online reference since onlining failed. */ 453 put_device(&cdev->dev); 454 return (ret == 0) ? -ENODEV : ret; 455 } 456 457 static int online_store_handle_offline(struct ccw_device *cdev) 458 { 459 if (cdev->private->state == DEV_STATE_DISCONNECTED) 460 ccw_device_remove_disconnected(cdev); 461 else if (cdev->online && cdev->drv && cdev->drv->set_offline) 462 return ccw_device_set_offline(cdev); 463 return 0; 464 } 465 466 static int online_store_recog_and_online(struct ccw_device *cdev) 467 { 468 int ret; 469 470 /* Do device recognition, if needed. */ 471 if (cdev->private->state == DEV_STATE_BOXED) { 472 ret = ccw_device_recognition(cdev); 473 if (ret) { 474 CIO_MSG_EVENT(0, "Couldn't start recognition " 475 "for device 0.%x.%04x (ret=%d)\n", 476 cdev->private->dev_id.ssid, 477 cdev->private->dev_id.devno, ret); 478 return ret; 479 } 480 wait_event(cdev->private->wait_q, 481 cdev->private->flags.recog_done); 482 if (cdev->private->state != DEV_STATE_OFFLINE) 483 /* recognition failed */ 484 return -EAGAIN; 485 } 486 if (cdev->drv && cdev->drv->set_online) 487 ccw_device_set_online(cdev); 488 return 0; 489 } 490 491 static int online_store_handle_online(struct ccw_device *cdev, int force) 492 { 493 int ret; 494 495 ret = online_store_recog_and_online(cdev); 496 if (ret && !force) 497 return ret; 498 if (force && cdev->private->state == DEV_STATE_BOXED) { 499 ret = ccw_device_stlck(cdev); 500 if (ret) 501 return ret; 502 if (cdev->id.cu_type == 0) 503 cdev->private->state = DEV_STATE_NOT_OPER; 504 ret = online_store_recog_and_online(cdev); 505 if (ret) 506 return ret; 507 } 508 return 0; 509 } 510 511 static ssize_t online_store (struct device *dev, struct device_attribute *attr, 512 const char *buf, size_t count) 513 { 514 struct ccw_device *cdev = to_ccwdev(dev); 515 int force, ret; 516 unsigned long i; 517 518 if ((cdev->private->state != DEV_STATE_OFFLINE && 519 cdev->private->state != DEV_STATE_ONLINE && 520 cdev->private->state != DEV_STATE_BOXED && 521 cdev->private->state != DEV_STATE_DISCONNECTED) || 522 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0) 523 return -EAGAIN; 524 525 if (cdev->drv && !try_module_get(cdev->drv->owner)) { 526 atomic_set(&cdev->private->onoff, 0); 527 return -EINVAL; 528 } 529 if (!strncmp(buf, "force\n", count)) { 530 force = 1; 531 i = 1; 532 ret = 0; 533 } else { 534 force = 0; 535 ret = strict_strtoul(buf, 16, &i); 536 } 537 if (ret) 538 goto out; 539 switch (i) { 540 case 0: 541 ret = online_store_handle_offline(cdev); 542 break; 543 case 1: 544 ret = online_store_handle_online(cdev, force); 545 break; 546 default: 547 ret = -EINVAL; 548 } 549 out: 550 if (cdev->drv) 551 module_put(cdev->drv->owner); 552 atomic_set(&cdev->private->onoff, 0); 553 return (ret < 0) ? ret : count; 554 } 555 556 static ssize_t 557 available_show (struct device *dev, struct device_attribute *attr, char *buf) 558 { 559 struct ccw_device *cdev = to_ccwdev(dev); 560 struct subchannel *sch; 561 562 if (ccw_device_is_orphan(cdev)) 563 return sprintf(buf, "no device\n"); 564 switch (cdev->private->state) { 565 case DEV_STATE_BOXED: 566 return sprintf(buf, "boxed\n"); 567 case DEV_STATE_DISCONNECTED: 568 case DEV_STATE_DISCONNECTED_SENSE_ID: 569 case DEV_STATE_NOT_OPER: 570 sch = to_subchannel(dev->parent); 571 if (!sch->lpm) 572 return sprintf(buf, "no path\n"); 573 else 574 return sprintf(buf, "no device\n"); 575 default: 576 /* All other states considered fine. */ 577 return sprintf(buf, "good\n"); 578 } 579 } 580 581 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); 582 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); 583 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); 584 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL); 585 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); 586 static DEVICE_ATTR(online, 0644, online_show, online_store); 587 static DEVICE_ATTR(availability, 0444, available_show, NULL); 588 589 static struct attribute *io_subchannel_attrs[] = { 590 &dev_attr_chpids.attr, 591 &dev_attr_pimpampom.attr, 592 NULL, 593 }; 594 595 static struct attribute_group io_subchannel_attr_group = { 596 .attrs = io_subchannel_attrs, 597 }; 598 599 static struct attribute * ccwdev_attrs[] = { 600 &dev_attr_devtype.attr, 601 &dev_attr_cutype.attr, 602 &dev_attr_modalias.attr, 603 &dev_attr_online.attr, 604 &dev_attr_cmb_enable.attr, 605 &dev_attr_availability.attr, 606 NULL, 607 }; 608 609 static struct attribute_group ccwdev_attr_group = { 610 .attrs = ccwdev_attrs, 611 }; 612 613 static struct attribute_group *ccwdev_attr_groups[] = { 614 &ccwdev_attr_group, 615 NULL, 616 }; 617 618 /* this is a simple abstraction for device_register that sets the 619 * correct bus type and adds the bus specific files */ 620 static int ccw_device_register(struct ccw_device *cdev) 621 { 622 struct device *dev = &cdev->dev; 623 int ret; 624 625 dev->bus = &ccw_bus_type; 626 627 if ((ret = device_add(dev))) 628 return ret; 629 630 set_bit(1, &cdev->private->registered); 631 return ret; 632 } 633 634 struct match_data { 635 struct ccw_dev_id dev_id; 636 struct ccw_device * sibling; 637 }; 638 639 static int 640 match_devno(struct device * dev, void * data) 641 { 642 struct match_data * d = data; 643 struct ccw_device * cdev; 644 645 cdev = to_ccwdev(dev); 646 if ((cdev->private->state == DEV_STATE_DISCONNECTED) && 647 !ccw_device_is_orphan(cdev) && 648 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) && 649 (cdev != d->sibling)) 650 return 1; 651 return 0; 652 } 653 654 static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id, 655 struct ccw_device *sibling) 656 { 657 struct device *dev; 658 struct match_data data; 659 660 data.dev_id = *dev_id; 661 data.sibling = sibling; 662 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno); 663 664 return dev ? to_ccwdev(dev) : NULL; 665 } 666 667 static int match_orphan(struct device *dev, void *data) 668 { 669 struct ccw_dev_id *dev_id; 670 struct ccw_device *cdev; 671 672 dev_id = data; 673 cdev = to_ccwdev(dev); 674 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id); 675 } 676 677 static struct ccw_device * 678 get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css, 679 struct ccw_dev_id *dev_id) 680 { 681 struct device *dev; 682 683 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id, 684 match_orphan); 685 686 return dev ? to_ccwdev(dev) : NULL; 687 } 688 689 void ccw_device_do_unbind_bind(struct work_struct *work) 690 { 691 struct ccw_device_private *priv; 692 struct ccw_device *cdev; 693 struct subchannel *sch; 694 int ret; 695 696 priv = container_of(work, struct ccw_device_private, kick_work); 697 cdev = priv->cdev; 698 sch = to_subchannel(cdev->dev.parent); 699 700 if (test_bit(1, &cdev->private->registered)) { 701 device_release_driver(&cdev->dev); 702 ret = device_attach(&cdev->dev); 703 WARN_ON(ret == -ENODEV); 704 } 705 } 706 707 static void 708 ccw_device_release(struct device *dev) 709 { 710 struct ccw_device *cdev; 711 712 cdev = to_ccwdev(dev); 713 /* Release reference of parent subchannel. */ 714 put_device(cdev->dev.parent); 715 kfree(cdev->private); 716 kfree(cdev); 717 } 718 719 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch) 720 { 721 struct ccw_device *cdev; 722 723 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 724 if (cdev) { 725 cdev->private = kzalloc(sizeof(struct ccw_device_private), 726 GFP_KERNEL | GFP_DMA); 727 if (cdev->private) 728 return cdev; 729 } 730 kfree(cdev); 731 return ERR_PTR(-ENOMEM); 732 } 733 734 static int io_subchannel_initialize_dev(struct subchannel *sch, 735 struct ccw_device *cdev) 736 { 737 cdev->private->cdev = cdev; 738 atomic_set(&cdev->private->onoff, 0); 739 cdev->dev.parent = &sch->dev; 740 cdev->dev.release = ccw_device_release; 741 INIT_WORK(&cdev->private->kick_work, NULL); 742 cdev->dev.groups = ccwdev_attr_groups; 743 /* Do first half of device_register. */ 744 device_initialize(&cdev->dev); 745 if (!get_device(&sch->dev)) { 746 /* Release reference from device_initialize(). */ 747 put_device(&cdev->dev); 748 return -ENODEV; 749 } 750 return 0; 751 } 752 753 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch) 754 { 755 struct ccw_device *cdev; 756 int ret; 757 758 cdev = io_subchannel_allocate_dev(sch); 759 if (!IS_ERR(cdev)) { 760 ret = io_subchannel_initialize_dev(sch, cdev); 761 if (ret) { 762 kfree(cdev); 763 cdev = ERR_PTR(ret); 764 } 765 } 766 return cdev; 767 } 768 769 static int io_subchannel_recog(struct ccw_device *, struct subchannel *); 770 771 static void sch_attach_device(struct subchannel *sch, 772 struct ccw_device *cdev) 773 { 774 css_update_ssd_info(sch); 775 spin_lock_irq(sch->lock); 776 sch_set_cdev(sch, cdev); 777 cdev->private->schid = sch->schid; 778 cdev->ccwlock = sch->lock; 779 ccw_device_trigger_reprobe(cdev); 780 spin_unlock_irq(sch->lock); 781 } 782 783 static void sch_attach_disconnected_device(struct subchannel *sch, 784 struct ccw_device *cdev) 785 { 786 struct subchannel *other_sch; 787 int ret; 788 789 /* Get reference for new parent. */ 790 if (!get_device(&sch->dev)) 791 return; 792 other_sch = to_subchannel(cdev->dev.parent); 793 /* Note: device_move() changes cdev->dev.parent */ 794 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV); 795 if (ret) { 796 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed " 797 "(ret=%d)!\n", cdev->private->dev_id.ssid, 798 cdev->private->dev_id.devno, ret); 799 /* Put reference for new parent. */ 800 put_device(&sch->dev); 801 return; 802 } 803 sch_set_cdev(other_sch, NULL); 804 /* No need to keep a subchannel without ccw device around. */ 805 css_sch_device_unregister(other_sch); 806 sch_attach_device(sch, cdev); 807 /* Put reference for old parent. */ 808 put_device(&other_sch->dev); 809 } 810 811 static void sch_attach_orphaned_device(struct subchannel *sch, 812 struct ccw_device *cdev) 813 { 814 int ret; 815 struct subchannel *pseudo_sch; 816 817 /* Get reference for new parent. */ 818 if (!get_device(&sch->dev)) 819 return; 820 pseudo_sch = to_subchannel(cdev->dev.parent); 821 /* 822 * Try to move the ccw device to its new subchannel. 823 * Note: device_move() changes cdev->dev.parent 824 */ 825 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV); 826 if (ret) { 827 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage " 828 "failed (ret=%d)!\n", 829 cdev->private->dev_id.ssid, 830 cdev->private->dev_id.devno, ret); 831 /* Put reference for new parent. */ 832 put_device(&sch->dev); 833 return; 834 } 835 sch_attach_device(sch, cdev); 836 /* Put reference on pseudo subchannel. */ 837 put_device(&pseudo_sch->dev); 838 } 839 840 static void sch_create_and_recog_new_device(struct subchannel *sch) 841 { 842 struct ccw_device *cdev; 843 844 /* Need to allocate a new ccw device. */ 845 cdev = io_subchannel_create_ccwdev(sch); 846 if (IS_ERR(cdev)) { 847 /* OK, we did everything we could... */ 848 css_sch_device_unregister(sch); 849 return; 850 } 851 spin_lock_irq(sch->lock); 852 sch_set_cdev(sch, cdev); 853 spin_unlock_irq(sch->lock); 854 /* Start recognition for the new ccw device. */ 855 if (io_subchannel_recog(cdev, sch)) { 856 spin_lock_irq(sch->lock); 857 sch_set_cdev(sch, NULL); 858 spin_unlock_irq(sch->lock); 859 css_sch_device_unregister(sch); 860 /* Put reference from io_subchannel_create_ccwdev(). */ 861 put_device(&sch->dev); 862 /* Give up initial reference. */ 863 put_device(&cdev->dev); 864 } 865 } 866 867 868 void ccw_device_move_to_orphanage(struct work_struct *work) 869 { 870 struct ccw_device_private *priv; 871 struct ccw_device *cdev; 872 struct ccw_device *replacing_cdev; 873 struct subchannel *sch; 874 int ret; 875 struct channel_subsystem *css; 876 struct ccw_dev_id dev_id; 877 878 priv = container_of(work, struct ccw_device_private, kick_work); 879 cdev = priv->cdev; 880 sch = to_subchannel(cdev->dev.parent); 881 css = to_css(sch->dev.parent); 882 dev_id.devno = sch->schib.pmcw.dev; 883 dev_id.ssid = sch->schid.ssid; 884 885 /* Increase refcount for pseudo subchannel. */ 886 get_device(&css->pseudo_subchannel->dev); 887 /* 888 * Move the orphaned ccw device to the orphanage so the replacing 889 * ccw device can take its place on the subchannel. 890 * Note: device_move() changes cdev->dev.parent 891 */ 892 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev, 893 DPM_ORDER_NONE); 894 if (ret) { 895 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed " 896 "(ret=%d)!\n", cdev->private->dev_id.ssid, 897 cdev->private->dev_id.devno, ret); 898 /* Decrease refcount for pseudo subchannel again. */ 899 put_device(&css->pseudo_subchannel->dev); 900 return; 901 } 902 cdev->ccwlock = css->pseudo_subchannel->lock; 903 /* 904 * Search for the replacing ccw device 905 * - among the disconnected devices 906 * - in the orphanage 907 */ 908 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev); 909 if (replacing_cdev) { 910 sch_attach_disconnected_device(sch, replacing_cdev); 911 /* Release reference from get_disc_ccwdev_by_dev_id() */ 912 put_device(&replacing_cdev->dev); 913 /* Release reference of subchannel from old cdev. */ 914 put_device(&sch->dev); 915 return; 916 } 917 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id); 918 if (replacing_cdev) { 919 sch_attach_orphaned_device(sch, replacing_cdev); 920 /* Release reference from get_orphaned_ccwdev_by_dev_id() */ 921 put_device(&replacing_cdev->dev); 922 /* Release reference of subchannel from old cdev. */ 923 put_device(&sch->dev); 924 return; 925 } 926 sch_create_and_recog_new_device(sch); 927 /* Release reference of subchannel from old cdev. */ 928 put_device(&sch->dev); 929 } 930 931 /* 932 * Register recognized device. 933 */ 934 static void 935 io_subchannel_register(struct work_struct *work) 936 { 937 struct ccw_device_private *priv; 938 struct ccw_device *cdev; 939 struct subchannel *sch; 940 int ret; 941 unsigned long flags; 942 943 priv = container_of(work, struct ccw_device_private, kick_work); 944 cdev = priv->cdev; 945 sch = to_subchannel(cdev->dev.parent); 946 /* 947 * Check if subchannel is still registered. It may have become 948 * unregistered if a machine check hit us after finishing 949 * device recognition but before the register work could be 950 * queued. 951 */ 952 if (!device_is_registered(&sch->dev)) 953 goto out_err; 954 css_update_ssd_info(sch); 955 /* 956 * io_subchannel_register() will also be called after device 957 * recognition has been done for a boxed device (which will already 958 * be registered). We need to reprobe since we may now have sense id 959 * information. 960 */ 961 if (device_is_registered(&cdev->dev)) { 962 if (!cdev->drv) { 963 ret = device_reprobe(&cdev->dev); 964 if (ret) 965 /* We can't do much here. */ 966 CIO_MSG_EVENT(0, "device_reprobe() returned" 967 " %d for 0.%x.%04x\n", ret, 968 cdev->private->dev_id.ssid, 969 cdev->private->dev_id.devno); 970 } 971 goto out; 972 } 973 /* 974 * Now we know this subchannel will stay, we can throw 975 * our delayed uevent. 976 */ 977 dev_set_uevent_suppress(&sch->dev, 0); 978 kobject_uevent(&sch->dev.kobj, KOBJ_ADD); 979 /* make it known to the system */ 980 ret = ccw_device_register(cdev); 981 if (ret) { 982 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n", 983 cdev->private->dev_id.ssid, 984 cdev->private->dev_id.devno, ret); 985 spin_lock_irqsave(sch->lock, flags); 986 sch_set_cdev(sch, NULL); 987 spin_unlock_irqrestore(sch->lock, flags); 988 /* Release initial device reference. */ 989 put_device(&cdev->dev); 990 goto out_err; 991 } 992 out: 993 cdev->private->flags.recog_done = 1; 994 wake_up(&cdev->private->wait_q); 995 out_err: 996 /* Release reference for workqueue processing. */ 997 put_device(&cdev->dev); 998 if (atomic_dec_and_test(&ccw_device_init_count)) 999 wake_up(&ccw_device_init_wq); 1000 } 1001 1002 static void ccw_device_call_sch_unregister(struct work_struct *work) 1003 { 1004 struct ccw_device_private *priv; 1005 struct ccw_device *cdev; 1006 struct subchannel *sch; 1007 1008 priv = container_of(work, struct ccw_device_private, kick_work); 1009 cdev = priv->cdev; 1010 /* Get subchannel reference for local processing. */ 1011 if (!get_device(cdev->dev.parent)) 1012 return; 1013 sch = to_subchannel(cdev->dev.parent); 1014 css_sch_device_unregister(sch); 1015 /* Reset intparm to zeroes. */ 1016 sch->config.intparm = 0; 1017 cio_commit_config(sch); 1018 /* Release cdev reference for workqueue processing.*/ 1019 put_device(&cdev->dev); 1020 /* Release subchannel reference for local processing. */ 1021 put_device(&sch->dev); 1022 } 1023 1024 void ccw_device_schedule_sch_unregister(struct ccw_device *cdev) 1025 { 1026 PREPARE_WORK(&cdev->private->kick_work, 1027 ccw_device_call_sch_unregister); 1028 queue_work(slow_path_wq, &cdev->private->kick_work); 1029 } 1030 1031 /* 1032 * subchannel recognition done. Called from the state machine. 1033 */ 1034 void 1035 io_subchannel_recog_done(struct ccw_device *cdev) 1036 { 1037 if (css_init_done == 0) { 1038 cdev->private->flags.recog_done = 1; 1039 return; 1040 } 1041 switch (cdev->private->state) { 1042 case DEV_STATE_BOXED: 1043 /* Device did not respond in time. */ 1044 case DEV_STATE_NOT_OPER: 1045 cdev->private->flags.recog_done = 1; 1046 /* Remove device found not operational. */ 1047 if (!get_device(&cdev->dev)) 1048 break; 1049 ccw_device_schedule_sch_unregister(cdev); 1050 if (atomic_dec_and_test(&ccw_device_init_count)) 1051 wake_up(&ccw_device_init_wq); 1052 break; 1053 case DEV_STATE_OFFLINE: 1054 /* 1055 * We can't register the device in interrupt context so 1056 * we schedule a work item. 1057 */ 1058 if (!get_device(&cdev->dev)) 1059 break; 1060 PREPARE_WORK(&cdev->private->kick_work, 1061 io_subchannel_register); 1062 queue_work(slow_path_wq, &cdev->private->kick_work); 1063 break; 1064 } 1065 } 1066 1067 static int 1068 io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch) 1069 { 1070 int rc; 1071 struct ccw_device_private *priv; 1072 1073 sch_set_cdev(sch, cdev); 1074 cdev->ccwlock = sch->lock; 1075 1076 /* Init private data. */ 1077 priv = cdev->private; 1078 priv->dev_id.devno = sch->schib.pmcw.dev; 1079 priv->dev_id.ssid = sch->schid.ssid; 1080 priv->schid = sch->schid; 1081 priv->state = DEV_STATE_NOT_OPER; 1082 INIT_LIST_HEAD(&priv->cmb_list); 1083 init_waitqueue_head(&priv->wait_q); 1084 init_timer(&priv->timer); 1085 1086 /* Set an initial name for the device. */ 1087 if (cio_is_console(sch->schid)) 1088 cdev->dev.init_name = cio_get_console_cdev_name(sch); 1089 else 1090 dev_set_name(&cdev->dev, "0.%x.%04x", 1091 sch->schid.ssid, sch->schib.pmcw.dev); 1092 1093 /* Increase counter of devices currently in recognition. */ 1094 atomic_inc(&ccw_device_init_count); 1095 1096 /* Start async. device sensing. */ 1097 spin_lock_irq(sch->lock); 1098 rc = ccw_device_recognition(cdev); 1099 spin_unlock_irq(sch->lock); 1100 if (rc) { 1101 if (atomic_dec_and_test(&ccw_device_init_count)) 1102 wake_up(&ccw_device_init_wq); 1103 } 1104 return rc; 1105 } 1106 1107 static void ccw_device_move_to_sch(struct work_struct *work) 1108 { 1109 struct ccw_device_private *priv; 1110 int rc; 1111 struct subchannel *sch; 1112 struct ccw_device *cdev; 1113 struct subchannel *former_parent; 1114 1115 priv = container_of(work, struct ccw_device_private, kick_work); 1116 sch = priv->sch; 1117 cdev = priv->cdev; 1118 former_parent = to_subchannel(cdev->dev.parent); 1119 /* Get reference for new parent. */ 1120 if (!get_device(&sch->dev)) 1121 return; 1122 mutex_lock(&sch->reg_mutex); 1123 /* 1124 * Try to move the ccw device to its new subchannel. 1125 * Note: device_move() changes cdev->dev.parent 1126 */ 1127 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV); 1128 mutex_unlock(&sch->reg_mutex); 1129 if (rc) { 1130 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel " 1131 "0.%x.%04x failed (ret=%d)!\n", 1132 cdev->private->dev_id.ssid, 1133 cdev->private->dev_id.devno, sch->schid.ssid, 1134 sch->schid.sch_no, rc); 1135 css_sch_device_unregister(sch); 1136 /* Put reference for new parent again. */ 1137 put_device(&sch->dev); 1138 goto out; 1139 } 1140 if (!sch_is_pseudo_sch(former_parent)) { 1141 spin_lock_irq(former_parent->lock); 1142 sch_set_cdev(former_parent, NULL); 1143 spin_unlock_irq(former_parent->lock); 1144 css_sch_device_unregister(former_parent); 1145 /* Reset intparm to zeroes. */ 1146 former_parent->config.intparm = 0; 1147 cio_commit_config(former_parent); 1148 } 1149 sch_attach_device(sch, cdev); 1150 out: 1151 /* Put reference for old parent. */ 1152 put_device(&former_parent->dev); 1153 put_device(&cdev->dev); 1154 } 1155 1156 static void io_subchannel_irq(struct subchannel *sch) 1157 { 1158 struct ccw_device *cdev; 1159 1160 cdev = sch_get_cdev(sch); 1161 1162 CIO_TRACE_EVENT(3, "IRQ"); 1163 CIO_TRACE_EVENT(3, dev_name(&sch->dev)); 1164 if (cdev) 1165 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT); 1166 } 1167 1168 void io_subchannel_init_config(struct subchannel *sch) 1169 { 1170 memset(&sch->config, 0, sizeof(sch->config)); 1171 sch->config.csense = 1; 1172 /* Use subchannel mp mode when there is more than 1 installed CHPID. */ 1173 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0) 1174 sch->config.mp = 1; 1175 } 1176 1177 static void io_subchannel_init_fields(struct subchannel *sch) 1178 { 1179 if (cio_is_console(sch->schid)) 1180 sch->opm = 0xff; 1181 else 1182 sch->opm = chp_get_sch_opm(sch); 1183 sch->lpm = sch->schib.pmcw.pam & sch->opm; 1184 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC; 1185 1186 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X" 1187 " - PIM = %02X, PAM = %02X, POM = %02X\n", 1188 sch->schib.pmcw.dev, sch->schid.ssid, 1189 sch->schid.sch_no, sch->schib.pmcw.pim, 1190 sch->schib.pmcw.pam, sch->schib.pmcw.pom); 1191 1192 io_subchannel_init_config(sch); 1193 } 1194 1195 static void io_subchannel_do_unreg(struct work_struct *work) 1196 { 1197 struct subchannel *sch; 1198 1199 sch = container_of(work, struct subchannel, work); 1200 css_sch_device_unregister(sch); 1201 /* Reset intparm to zeroes. */ 1202 sch->config.intparm = 0; 1203 cio_commit_config(sch); 1204 put_device(&sch->dev); 1205 } 1206 1207 /* Schedule unregister if we have no cdev. */ 1208 static void io_subchannel_schedule_removal(struct subchannel *sch) 1209 { 1210 get_device(&sch->dev); 1211 INIT_WORK(&sch->work, io_subchannel_do_unreg); 1212 queue_work(slow_path_wq, &sch->work); 1213 } 1214 1215 /* 1216 * Note: We always return 0 so that we bind to the device even on error. 1217 * This is needed so that our remove function is called on unregister. 1218 */ 1219 static int io_subchannel_probe(struct subchannel *sch) 1220 { 1221 struct ccw_device *cdev; 1222 int rc; 1223 unsigned long flags; 1224 struct ccw_dev_id dev_id; 1225 1226 cdev = sch_get_cdev(sch); 1227 if (cdev) { 1228 rc = sysfs_create_group(&sch->dev.kobj, 1229 &io_subchannel_attr_group); 1230 if (rc) 1231 CIO_MSG_EVENT(0, "Failed to create io subchannel " 1232 "attributes for subchannel " 1233 "0.%x.%04x (rc=%d)\n", 1234 sch->schid.ssid, sch->schid.sch_no, rc); 1235 /* 1236 * This subchannel already has an associated ccw_device. 1237 * Throw the delayed uevent for the subchannel, register 1238 * the ccw_device and exit. This happens for all early 1239 * devices, e.g. the console. 1240 */ 1241 dev_set_uevent_suppress(&sch->dev, 0); 1242 kobject_uevent(&sch->dev.kobj, KOBJ_ADD); 1243 cdev->dev.groups = ccwdev_attr_groups; 1244 device_initialize(&cdev->dev); 1245 ccw_device_register(cdev); 1246 /* 1247 * Check if the device is already online. If it is 1248 * the reference count needs to be corrected since we 1249 * didn't obtain a reference in ccw_device_set_online. 1250 */ 1251 if (cdev->private->state != DEV_STATE_NOT_OPER && 1252 cdev->private->state != DEV_STATE_OFFLINE && 1253 cdev->private->state != DEV_STATE_BOXED) 1254 get_device(&cdev->dev); 1255 return 0; 1256 } 1257 io_subchannel_init_fields(sch); 1258 rc = cio_commit_config(sch); 1259 if (rc) 1260 goto out_schedule; 1261 rc = sysfs_create_group(&sch->dev.kobj, 1262 &io_subchannel_attr_group); 1263 if (rc) 1264 goto out_schedule; 1265 /* Allocate I/O subchannel private data. */ 1266 sch->private = kzalloc(sizeof(struct io_subchannel_private), 1267 GFP_KERNEL | GFP_DMA); 1268 if (!sch->private) 1269 goto out_err; 1270 /* 1271 * First check if a fitting device may be found amongst the 1272 * disconnected devices or in the orphanage. 1273 */ 1274 dev_id.devno = sch->schib.pmcw.dev; 1275 dev_id.ssid = sch->schid.ssid; 1276 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL); 1277 if (!cdev) 1278 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent), 1279 &dev_id); 1280 if (cdev) { 1281 /* 1282 * Schedule moving the device until when we have a registered 1283 * subchannel to move to and succeed the probe. We can 1284 * unregister later again, when the probe is through. 1285 */ 1286 cdev->private->sch = sch; 1287 PREPARE_WORK(&cdev->private->kick_work, 1288 ccw_device_move_to_sch); 1289 queue_work(slow_path_wq, &cdev->private->kick_work); 1290 return 0; 1291 } 1292 cdev = io_subchannel_create_ccwdev(sch); 1293 if (IS_ERR(cdev)) 1294 goto out_err; 1295 rc = io_subchannel_recog(cdev, sch); 1296 if (rc) { 1297 spin_lock_irqsave(sch->lock, flags); 1298 io_subchannel_recog_done(cdev); 1299 spin_unlock_irqrestore(sch->lock, flags); 1300 } 1301 return 0; 1302 out_err: 1303 kfree(sch->private); 1304 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); 1305 out_schedule: 1306 io_subchannel_schedule_removal(sch); 1307 return 0; 1308 } 1309 1310 static int 1311 io_subchannel_remove (struct subchannel *sch) 1312 { 1313 struct ccw_device *cdev; 1314 unsigned long flags; 1315 1316 cdev = sch_get_cdev(sch); 1317 if (!cdev) 1318 return 0; 1319 /* Set ccw device to not operational and drop reference. */ 1320 spin_lock_irqsave(cdev->ccwlock, flags); 1321 sch_set_cdev(sch, NULL); 1322 cdev->private->state = DEV_STATE_NOT_OPER; 1323 spin_unlock_irqrestore(cdev->ccwlock, flags); 1324 ccw_device_unregister(cdev); 1325 put_device(&cdev->dev); 1326 kfree(sch->private); 1327 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); 1328 return 0; 1329 } 1330 1331 static int io_subchannel_notify(struct subchannel *sch, int event) 1332 { 1333 struct ccw_device *cdev; 1334 1335 cdev = sch_get_cdev(sch); 1336 if (!cdev) 1337 return 0; 1338 return ccw_device_notify(cdev, event); 1339 } 1340 1341 static void io_subchannel_verify(struct subchannel *sch) 1342 { 1343 struct ccw_device *cdev; 1344 1345 cdev = sch_get_cdev(sch); 1346 if (cdev) 1347 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1348 } 1349 1350 static int check_for_io_on_path(struct subchannel *sch, int mask) 1351 { 1352 if (cio_update_schib(sch)) 1353 return 0; 1354 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask) 1355 return 1; 1356 return 0; 1357 } 1358 1359 static void terminate_internal_io(struct subchannel *sch, 1360 struct ccw_device *cdev) 1361 { 1362 if (cio_clear(sch)) { 1363 /* Recheck device in case clear failed. */ 1364 sch->lpm = 0; 1365 if (cdev->online) 1366 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1367 else 1368 css_schedule_eval(sch->schid); 1369 return; 1370 } 1371 cdev->private->state = DEV_STATE_CLEAR_VERIFY; 1372 /* Request retry of internal operation. */ 1373 cdev->private->flags.intretry = 1; 1374 /* Call handler. */ 1375 if (cdev->handler) 1376 cdev->handler(cdev, cdev->private->intparm, 1377 ERR_PTR(-EIO)); 1378 } 1379 1380 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask) 1381 { 1382 struct ccw_device *cdev; 1383 1384 cdev = sch_get_cdev(sch); 1385 if (!cdev) 1386 return; 1387 if (check_for_io_on_path(sch, mask)) { 1388 if (cdev->private->state == DEV_STATE_ONLINE) 1389 ccw_device_kill_io(cdev); 1390 else { 1391 terminate_internal_io(sch, cdev); 1392 /* Re-start path verification. */ 1393 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1394 } 1395 } else 1396 /* trigger path verification. */ 1397 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1398 1399 } 1400 1401 static int io_subchannel_chp_event(struct subchannel *sch, 1402 struct chp_link *link, int event) 1403 { 1404 int mask; 1405 1406 mask = chp_ssd_get_mask(&sch->ssd_info, link); 1407 if (!mask) 1408 return 0; 1409 switch (event) { 1410 case CHP_VARY_OFF: 1411 sch->opm &= ~mask; 1412 sch->lpm &= ~mask; 1413 io_subchannel_terminate_path(sch, mask); 1414 break; 1415 case CHP_VARY_ON: 1416 sch->opm |= mask; 1417 sch->lpm |= mask; 1418 io_subchannel_verify(sch); 1419 break; 1420 case CHP_OFFLINE: 1421 if (cio_update_schib(sch)) 1422 return -ENODEV; 1423 io_subchannel_terminate_path(sch, mask); 1424 break; 1425 case CHP_ONLINE: 1426 if (cio_update_schib(sch)) 1427 return -ENODEV; 1428 sch->lpm |= mask & sch->opm; 1429 io_subchannel_verify(sch); 1430 break; 1431 } 1432 return 0; 1433 } 1434 1435 static void 1436 io_subchannel_shutdown(struct subchannel *sch) 1437 { 1438 struct ccw_device *cdev; 1439 int ret; 1440 1441 cdev = sch_get_cdev(sch); 1442 1443 if (cio_is_console(sch->schid)) 1444 return; 1445 if (!sch->schib.pmcw.ena) 1446 /* Nothing to do. */ 1447 return; 1448 ret = cio_disable_subchannel(sch); 1449 if (ret != -EBUSY) 1450 /* Subchannel is disabled, we're done. */ 1451 return; 1452 cdev->private->state = DEV_STATE_QUIESCE; 1453 if (cdev->handler) 1454 cdev->handler(cdev, cdev->private->intparm, 1455 ERR_PTR(-EIO)); 1456 ret = ccw_device_cancel_halt_clear(cdev); 1457 if (ret == -EBUSY) { 1458 ccw_device_set_timeout(cdev, HZ/10); 1459 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); 1460 } 1461 cio_disable_subchannel(sch); 1462 } 1463 1464 static int io_subchannel_get_status(struct subchannel *sch) 1465 { 1466 struct schib schib; 1467 1468 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv) 1469 return CIO_GONE; 1470 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev)) 1471 return CIO_REVALIDATE; 1472 if (!sch->lpm) 1473 return CIO_NO_PATH; 1474 return CIO_OPER; 1475 } 1476 1477 static int device_is_disconnected(struct ccw_device *cdev) 1478 { 1479 if (!cdev) 1480 return 0; 1481 return (cdev->private->state == DEV_STATE_DISCONNECTED || 1482 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID); 1483 } 1484 1485 static int recovery_check(struct device *dev, void *data) 1486 { 1487 struct ccw_device *cdev = to_ccwdev(dev); 1488 int *redo = data; 1489 1490 spin_lock_irq(cdev->ccwlock); 1491 switch (cdev->private->state) { 1492 case DEV_STATE_DISCONNECTED: 1493 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n", 1494 cdev->private->dev_id.ssid, 1495 cdev->private->dev_id.devno); 1496 dev_fsm_event(cdev, DEV_EVENT_VERIFY); 1497 *redo = 1; 1498 break; 1499 case DEV_STATE_DISCONNECTED_SENSE_ID: 1500 *redo = 1; 1501 break; 1502 } 1503 spin_unlock_irq(cdev->ccwlock); 1504 1505 return 0; 1506 } 1507 1508 static void recovery_work_func(struct work_struct *unused) 1509 { 1510 int redo = 0; 1511 1512 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check); 1513 if (redo) { 1514 spin_lock_irq(&recovery_lock); 1515 if (!timer_pending(&recovery_timer)) { 1516 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1) 1517 recovery_phase++; 1518 mod_timer(&recovery_timer, jiffies + 1519 recovery_delay[recovery_phase] * HZ); 1520 } 1521 spin_unlock_irq(&recovery_lock); 1522 } else 1523 CIO_MSG_EVENT(4, "recovery: end\n"); 1524 } 1525 1526 static DECLARE_WORK(recovery_work, recovery_work_func); 1527 1528 static void recovery_func(unsigned long data) 1529 { 1530 /* 1531 * We can't do our recovery in softirq context and it's not 1532 * performance critical, so we schedule it. 1533 */ 1534 schedule_work(&recovery_work); 1535 } 1536 1537 static void ccw_device_schedule_recovery(void) 1538 { 1539 unsigned long flags; 1540 1541 CIO_MSG_EVENT(4, "recovery: schedule\n"); 1542 spin_lock_irqsave(&recovery_lock, flags); 1543 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) { 1544 recovery_phase = 0; 1545 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ); 1546 } 1547 spin_unlock_irqrestore(&recovery_lock, flags); 1548 } 1549 1550 static int purge_fn(struct device *dev, void *data) 1551 { 1552 struct ccw_device *cdev = to_ccwdev(dev); 1553 struct ccw_device_private *priv = cdev->private; 1554 int unreg; 1555 1556 spin_lock_irq(cdev->ccwlock); 1557 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) && 1558 (priv->state == DEV_STATE_OFFLINE); 1559 spin_unlock_irq(cdev->ccwlock); 1560 if (!unreg) 1561 goto out; 1562 if (!get_device(&cdev->dev)) 1563 goto out; 1564 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid, 1565 priv->dev_id.devno); 1566 ccw_device_schedule_sch_unregister(cdev); 1567 1568 out: 1569 /* Abort loop in case of pending signal. */ 1570 if (signal_pending(current)) 1571 return -EINTR; 1572 1573 return 0; 1574 } 1575 1576 /** 1577 * ccw_purge_blacklisted - purge unused, blacklisted devices 1578 * 1579 * Unregister all ccw devices that are offline and on the blacklist. 1580 */ 1581 int ccw_purge_blacklisted(void) 1582 { 1583 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n"); 1584 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn); 1585 return 0; 1586 } 1587 1588 static void device_set_disconnected(struct ccw_device *cdev) 1589 { 1590 if (!cdev) 1591 return; 1592 ccw_device_set_timeout(cdev, 0); 1593 cdev->private->flags.fake_irb = 0; 1594 cdev->private->state = DEV_STATE_DISCONNECTED; 1595 if (cdev->online) 1596 ccw_device_schedule_recovery(); 1597 } 1598 1599 void ccw_device_set_notoper(struct ccw_device *cdev) 1600 { 1601 struct subchannel *sch = to_subchannel(cdev->dev.parent); 1602 1603 CIO_TRACE_EVENT(2, "notoper"); 1604 CIO_TRACE_EVENT(2, dev_name(&sch->dev)); 1605 ccw_device_set_timeout(cdev, 0); 1606 cio_disable_subchannel(sch); 1607 cdev->private->state = DEV_STATE_NOT_OPER; 1608 } 1609 1610 static int io_subchannel_sch_event(struct subchannel *sch, int slow) 1611 { 1612 int event, ret, disc; 1613 unsigned long flags; 1614 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action; 1615 struct ccw_device *cdev; 1616 1617 spin_lock_irqsave(sch->lock, flags); 1618 cdev = sch_get_cdev(sch); 1619 disc = device_is_disconnected(cdev); 1620 if (disc && slow) { 1621 /* Disconnected devices are evaluated directly only.*/ 1622 spin_unlock_irqrestore(sch->lock, flags); 1623 return 0; 1624 } 1625 /* No interrupt after machine check - kill pending timers. */ 1626 if (cdev) 1627 ccw_device_set_timeout(cdev, 0); 1628 if (!disc && !slow) { 1629 /* Non-disconnected devices are evaluated on the slow path. */ 1630 spin_unlock_irqrestore(sch->lock, flags); 1631 return -EAGAIN; 1632 } 1633 event = io_subchannel_get_status(sch); 1634 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n", 1635 sch->schid.ssid, sch->schid.sch_no, event, 1636 disc ? "disconnected" : "normal", 1637 slow ? "slow" : "fast"); 1638 /* Analyze subchannel status. */ 1639 action = NONE; 1640 switch (event) { 1641 case CIO_NO_PATH: 1642 if (disc) { 1643 /* Check if paths have become available. */ 1644 action = REPROBE; 1645 break; 1646 } 1647 /* fall through */ 1648 case CIO_GONE: 1649 /* Ask driver what to do with device. */ 1650 if (io_subchannel_notify(sch, event)) 1651 action = DISC; 1652 else 1653 action = UNREGISTER; 1654 break; 1655 case CIO_REVALIDATE: 1656 /* Device will be removed, so no notify necessary. */ 1657 if (disc) 1658 /* Reprobe because immediate unregister might block. */ 1659 action = REPROBE; 1660 else 1661 action = UNREGISTER_PROBE; 1662 break; 1663 case CIO_OPER: 1664 if (disc) 1665 /* Get device operational again. */ 1666 action = REPROBE; 1667 break; 1668 } 1669 /* Perform action. */ 1670 ret = 0; 1671 switch (action) { 1672 case UNREGISTER: 1673 case UNREGISTER_PROBE: 1674 ccw_device_set_notoper(cdev); 1675 /* Unregister device (will use subchannel lock). */ 1676 spin_unlock_irqrestore(sch->lock, flags); 1677 css_sch_device_unregister(sch); 1678 spin_lock_irqsave(sch->lock, flags); 1679 1680 /* Reset intparm to zeroes. */ 1681 sch->config.intparm = 0; 1682 cio_commit_config(sch); 1683 break; 1684 case REPROBE: 1685 ccw_device_trigger_reprobe(cdev); 1686 break; 1687 case DISC: 1688 device_set_disconnected(cdev); 1689 break; 1690 default: 1691 break; 1692 } 1693 spin_unlock_irqrestore(sch->lock, flags); 1694 /* Probe if necessary. */ 1695 if (action == UNREGISTER_PROBE) 1696 ret = css_probe_device(sch->schid); 1697 1698 return ret; 1699 } 1700 1701 #ifdef CONFIG_CCW_CONSOLE 1702 static struct ccw_device console_cdev; 1703 static char console_cdev_name[10] = "0.x.xxxx"; 1704 static struct ccw_device_private console_private; 1705 static int console_cdev_in_use; 1706 1707 static DEFINE_SPINLOCK(ccw_console_lock); 1708 1709 spinlock_t * cio_get_console_lock(void) 1710 { 1711 return &ccw_console_lock; 1712 } 1713 1714 static int ccw_device_console_enable(struct ccw_device *cdev, 1715 struct subchannel *sch) 1716 { 1717 int rc; 1718 1719 /* Attach subchannel private data. */ 1720 sch->private = cio_get_console_priv(); 1721 memset(sch->private, 0, sizeof(struct io_subchannel_private)); 1722 io_subchannel_init_fields(sch); 1723 rc = cio_commit_config(sch); 1724 if (rc) 1725 return rc; 1726 sch->driver = &io_subchannel_driver; 1727 /* Initialize the ccw_device structure. */ 1728 cdev->dev.parent= &sch->dev; 1729 rc = io_subchannel_recog(cdev, sch); 1730 if (rc) 1731 return rc; 1732 1733 /* Now wait for the async. recognition to come to an end. */ 1734 spin_lock_irq(cdev->ccwlock); 1735 while (!dev_fsm_final_state(cdev)) 1736 wait_cons_dev(); 1737 rc = -EIO; 1738 if (cdev->private->state != DEV_STATE_OFFLINE) 1739 goto out_unlock; 1740 ccw_device_online(cdev); 1741 while (!dev_fsm_final_state(cdev)) 1742 wait_cons_dev(); 1743 if (cdev->private->state != DEV_STATE_ONLINE) 1744 goto out_unlock; 1745 rc = 0; 1746 out_unlock: 1747 spin_unlock_irq(cdev->ccwlock); 1748 return 0; 1749 } 1750 1751 struct ccw_device * 1752 ccw_device_probe_console(void) 1753 { 1754 struct subchannel *sch; 1755 int ret; 1756 1757 if (xchg(&console_cdev_in_use, 1) != 0) 1758 return ERR_PTR(-EBUSY); 1759 sch = cio_probe_console(); 1760 if (IS_ERR(sch)) { 1761 console_cdev_in_use = 0; 1762 return (void *) sch; 1763 } 1764 memset(&console_cdev, 0, sizeof(struct ccw_device)); 1765 memset(&console_private, 0, sizeof(struct ccw_device_private)); 1766 console_cdev.private = &console_private; 1767 console_private.cdev = &console_cdev; 1768 ret = ccw_device_console_enable(&console_cdev, sch); 1769 if (ret) { 1770 cio_release_console(); 1771 console_cdev_in_use = 0; 1772 return ERR_PTR(ret); 1773 } 1774 console_cdev.online = 1; 1775 return &console_cdev; 1776 } 1777 1778 1779 const char *cio_get_console_cdev_name(struct subchannel *sch) 1780 { 1781 snprintf(console_cdev_name, 10, "0.%x.%04x", 1782 sch->schid.ssid, sch->schib.pmcw.dev); 1783 return (const char *)console_cdev_name; 1784 } 1785 #endif 1786 1787 /* 1788 * get ccw_device matching the busid, but only if owned by cdrv 1789 */ 1790 static int 1791 __ccwdev_check_busid(struct device *dev, void *id) 1792 { 1793 char *bus_id; 1794 1795 bus_id = id; 1796 1797 return (strcmp(bus_id, dev_name(dev)) == 0); 1798 } 1799 1800 1801 /** 1802 * get_ccwdev_by_busid() - obtain device from a bus id 1803 * @cdrv: driver the device is owned by 1804 * @bus_id: bus id of the device to be searched 1805 * 1806 * This function searches all devices owned by @cdrv for a device with a bus 1807 * id matching @bus_id. 1808 * Returns: 1809 * If a match is found, its reference count of the found device is increased 1810 * and it is returned; else %NULL is returned. 1811 */ 1812 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv, 1813 const char *bus_id) 1814 { 1815 struct device *dev; 1816 struct device_driver *drv; 1817 1818 drv = get_driver(&cdrv->driver); 1819 if (!drv) 1820 return NULL; 1821 1822 dev = driver_find_device(drv, NULL, (void *)bus_id, 1823 __ccwdev_check_busid); 1824 put_driver(drv); 1825 1826 return dev ? to_ccwdev(dev) : NULL; 1827 } 1828 1829 /************************** device driver handling ************************/ 1830 1831 /* This is the implementation of the ccw_driver class. The probe, remove 1832 * and release methods are initially very similar to the device_driver 1833 * implementations, with the difference that they have ccw_device 1834 * arguments. 1835 * 1836 * A ccw driver also contains the information that is needed for 1837 * device matching. 1838 */ 1839 static int 1840 ccw_device_probe (struct device *dev) 1841 { 1842 struct ccw_device *cdev = to_ccwdev(dev); 1843 struct ccw_driver *cdrv = to_ccwdrv(dev->driver); 1844 int ret; 1845 1846 cdev->drv = cdrv; /* to let the driver call _set_online */ 1847 1848 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV; 1849 1850 if (ret) { 1851 cdev->drv = NULL; 1852 return ret; 1853 } 1854 1855 return 0; 1856 } 1857 1858 static int 1859 ccw_device_remove (struct device *dev) 1860 { 1861 struct ccw_device *cdev = to_ccwdev(dev); 1862 struct ccw_driver *cdrv = cdev->drv; 1863 int ret; 1864 1865 if (cdrv->remove) 1866 cdrv->remove(cdev); 1867 if (cdev->online) { 1868 cdev->online = 0; 1869 spin_lock_irq(cdev->ccwlock); 1870 ret = ccw_device_offline(cdev); 1871 spin_unlock_irq(cdev->ccwlock); 1872 if (ret == 0) 1873 wait_event(cdev->private->wait_q, 1874 dev_fsm_final_state(cdev)); 1875 else 1876 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " 1877 "device 0.%x.%04x\n", 1878 ret, cdev->private->dev_id.ssid, 1879 cdev->private->dev_id.devno); 1880 /* Give up reference obtained in ccw_device_set_online(). */ 1881 put_device(&cdev->dev); 1882 } 1883 ccw_device_set_timeout(cdev, 0); 1884 cdev->drv = NULL; 1885 return 0; 1886 } 1887 1888 static void ccw_device_shutdown(struct device *dev) 1889 { 1890 struct ccw_device *cdev; 1891 1892 cdev = to_ccwdev(dev); 1893 if (cdev->drv && cdev->drv->shutdown) 1894 cdev->drv->shutdown(cdev); 1895 disable_cmf(cdev); 1896 } 1897 1898 struct bus_type ccw_bus_type = { 1899 .name = "ccw", 1900 .match = ccw_bus_match, 1901 .uevent = ccw_uevent, 1902 .probe = ccw_device_probe, 1903 .remove = ccw_device_remove, 1904 .shutdown = ccw_device_shutdown, 1905 }; 1906 1907 /** 1908 * ccw_driver_register() - register a ccw driver 1909 * @cdriver: driver to be registered 1910 * 1911 * This function is mainly a wrapper around driver_register(). 1912 * Returns: 1913 * %0 on success and a negative error value on failure. 1914 */ 1915 int ccw_driver_register(struct ccw_driver *cdriver) 1916 { 1917 struct device_driver *drv = &cdriver->driver; 1918 1919 drv->bus = &ccw_bus_type; 1920 drv->name = cdriver->name; 1921 drv->owner = cdriver->owner; 1922 1923 return driver_register(drv); 1924 } 1925 1926 /** 1927 * ccw_driver_unregister() - deregister a ccw driver 1928 * @cdriver: driver to be deregistered 1929 * 1930 * This function is mainly a wrapper around driver_unregister(). 1931 */ 1932 void ccw_driver_unregister(struct ccw_driver *cdriver) 1933 { 1934 driver_unregister(&cdriver->driver); 1935 } 1936 1937 /* Helper func for qdio. */ 1938 struct subchannel_id 1939 ccw_device_get_subchannel_id(struct ccw_device *cdev) 1940 { 1941 struct subchannel *sch; 1942 1943 sch = to_subchannel(cdev->dev.parent); 1944 return sch->schid; 1945 } 1946 1947 MODULE_LICENSE("GPL"); 1948 EXPORT_SYMBOL(ccw_device_set_online); 1949 EXPORT_SYMBOL(ccw_device_set_offline); 1950 EXPORT_SYMBOL(ccw_driver_register); 1951 EXPORT_SYMBOL(ccw_driver_unregister); 1952 EXPORT_SYMBOL(get_ccwdev_by_busid); 1953 EXPORT_SYMBOL(ccw_bus_type); 1954 EXPORT_SYMBOL(ccw_device_work); 1955 EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id); 1956