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