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